git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/9] worktree: don't die() in library function find_worktree()

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Aug 28, 2018, 21:20 UTC
Message-ID
<20180828212026.21989-2-sunshine@sunshineco.com>
In-Reply-To
<20180828212026.21989-1-sunshine@sunshineco.com>

Callers don't expect library function find_worktree() to die(); they expect it to return the named worktree if found, or NULL if not. Although find_worktree() itself never invokes die(), it calls real_pathdup() with 'die_on_error' incorrectly set to 'true', thus will die() indirectly if the user-provided path is not to real_pathdup()'s liking. This can be observed, for instance, with any git-worktree command which searches for an existing worktree:

    $ git worktree unlock foo
    fatal: 'foo' is not a working tree
    $ git worktree unlock foo/bar
    fatal: Invalid path '.../foo': No such file or directory

The first error message is the expected one from "git worktree unlock" not finding the specified worktree; the second is from find_worktree() invoking real_pathdup() incorrectly and die()ing prematurely.

Aside from the inconsistent error message between the two cases, this bug hasn't otherwise been a serious problem since existing callers all die() anyhow when the worktree can't be found. However, that may not be true of callers added in the future, so fix find_worktree() to avoid die()ing.

Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
---
 t/t2028-worktree-move.sh | 8 ++++++++
 worktree.c               | 6 +++++-
 2 files changed, 13 insertions(+), 1 deletion(-)
diff --git a/t/t2028-worktree-move.sh b/t/t2028-worktree-move.sh
index 5f7d45b7b7..60aba7c41a 100755
--- a/t/t2028-worktree-move.sh
+++ b/t/t2028-worktree-move.sh
@@ -141,4 +141,12 @@ test_expect_success 'NOT remove missing-but-locked worktree' '
 	test_path_is_dir .git/worktrees/gone-but-locked
 '
 
+test_expect_success 'proper error when worktree not found' '
+	for i in noodle noodle/bork
+	do
+		test_must_fail git worktree lock $i 2>err &&
+		test_i18ngrep "not a working tree" err || return 1
+	done
+'
+
 test_done
diff --git a/worktree.c b/worktree.c
index 97cda5f97b..b0d0b5426d 100644
--- a/worktree.c
+++ b/worktree.c
@@ -217,7 +217,11 @@ struct worktree *find_worktree(struct worktree **list,
 
 	if (prefix)
 		arg = to_free = prefix_filename(prefix, arg);
-	path = real_pathdup(arg, 1);
+	path = real_pathdup(arg, 0);
+	if (!path) {
+		free(to_free);
+		return NULL;
+	}
 	for (; *list; list++)
 		if (!fspathcmp(path, real_path((*list)->path)))
 			break;
-- 
2.19.0.rc1.350.ge57e33dbd1
Previous: Eric SunshineNext: Eric Sunshine
Message 2 of 28 in “worktree: fix bugs and broaden --force applicability”
  1. 0/9 worktree: fix bugs and broaden --force applicabilityEric Sunshine, Aug 28, 2018
  2. 1/9 worktree: don't die() in library function find_worktree()Eric Sunshine, Aug 28, 2018
  3. 2/9 worktree: move delete_git_dir() earlier in file for upcoming new callersEric Sunshine, Aug 28, 2018
  4. 4/9 worktree: prepare for more checks of whether path can become worktreeEric Sunshine, Aug 28, 2018
  5. 3/9 worktree: generalize delete_git_dir() to reduce code duplicationEric Sunshine, Aug 28, 2018
  6. Jeff KingAug 30, 2018
  7. Eric SunshineAug 30, 2018
  8. 6/9 worktree: teach 'add' to respect --force for registered but missing pathEric Sunshine, Aug 28, 2018
  9. Jeff KingAug 30, 2018
  10. Eric SunshineAug 30, 2018
  11. 8/9 worktree: teach 'remove' to override lock when --force given twiceEric Sunshine, Aug 28, 2018
  12. Jeff KingAug 30, 2018
  13. Eric SunshineAug 30, 2018
  14. 9/9 worktree: delete .git/worktrees if empty after 'remove'Eric Sunshine, Aug 28, 2018
  15. 7/9 worktree: teach 'move' to override lock when --force given twiceEric Sunshine, Aug 28, 2018
  16. Jeff KingAug 30, 2018
  17. Eric SunshineAug 30, 2018
  18. 5/9 worktree: disallow adding same path multiple timesEric Sunshine, Aug 28, 2018
  19. Jeff KingAug 30, 2018
  20. Eric SunshineAug 30, 2018
  21. Jeff KingAug 30, 2018
  22. Eric SunshineAug 30, 2018
  23. Eric SunshineAug 30, 2018
  24. Jeff KingAug 30, 2018
  25. Junio C HamanoAug 30, 2018
  26. Ramsay JonesAug 30, 2018
  27. Jeff KingAug 31, 2018
  28. Ramsay JonesAug 31, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.