Skip to content

Commit 5f4ee57

Browse files
sunshinecogitster
authored andcommitted
worktree: avoid dead-code in conditional
get_worktrees() retrieves a list of all worktrees associated with a repository, including the main worktree. The location of the main worktree is determined by get_main_worktree() which needs to handle three distinct cases for the main worktree after absolute-path conversion: * <bare-repository>/. * <main-worktree>/.git/. (when $CWD is .git) * <main-worktree>/.git (when $CWD is any worktree) They all need to be normalized to just the <path> portion, dropping any "/." or "/.git" suffix. It turns out, however, that get_main_worktree() was only handling the first and last cases, i.e.: if (!strip_suffix(path, "/.git")) strip_suffix(path, "/."); This shortcoming was addressed by 45f274f (get_main_worktree(): allow it to be called in the Git directory, 2020-02-23) by changing the logic to: strip_suffix(path, "/."); if (!strip_suffix(path, "/.git")) strip_suffix(path, "/."); which makes the final strip_suffix() invocation dead-code. Fix this oversight by enumerating the three distinct cases explicitly rather than attempting to strip the suffix(es) incrementally. Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent af6b65d commit 5f4ee57

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

worktree.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ static struct worktree *get_main_worktree(void)
5050
struct strbuf worktree_path = STRBUF_INIT;
5151

5252
strbuf_add_absolute_path(&worktree_path, get_git_common_dir());
53-
strbuf_strip_suffix(&worktree_path, "/.");
54-
if (!strbuf_strip_suffix(&worktree_path, "/.git"))
55-
strbuf_strip_suffix(&worktree_path, "/.");
53+
if (!strbuf_strip_suffix(&worktree_path, "/.git/.") && /* in .git */
54+
!strbuf_strip_suffix(&worktree_path, "/.git")) /* in worktree */
55+
strbuf_strip_suffix(&worktree_path, "/."); /* in bare repo */
5656

5757
worktree = xcalloc(1, sizeof(*worktree));
5858
worktree->path = strbuf_detach(&worktree_path, NULL);

0 commit comments

Comments
 (0)