]> git.ipfire.org Git - thirdparty/git.git/commitdiff
worktree: avoid dead-code in conditional
authorEric Sunshine <sunshine@sunshineco.com>
Wed, 24 Jun 2020 19:05:41 +0000 (15:05 -0400)
committerJunio C Hamano <gitster@pobox.com>
Thu, 25 Jun 2020 00:39:28 +0000 (17:39 -0700)
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 45f274fbb1 (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 <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
worktree.c

index eba4fd3a03812f046dadc1baf9d7644ba8dfcce9..8d773367925bc6021eaef39d9ceb8492aa756241 100644 (file)
@@ -50,9 +50,9 @@ static struct worktree *get_main_worktree(void)
        struct strbuf worktree_path = STRBUF_INIT;
 
        strbuf_add_absolute_path(&worktree_path, get_git_common_dir());
-       strbuf_strip_suffix(&worktree_path, "/.");
-       if (!strbuf_strip_suffix(&worktree_path, "/.git"))
-               strbuf_strip_suffix(&worktree_path, "/.");
+       if (!strbuf_strip_suffix(&worktree_path, "/.git/.") && /* in .git */
+           !strbuf_strip_suffix(&worktree_path, "/.git")) /* in worktree */
+               strbuf_strip_suffix(&worktree_path, "/."); /* in bare repo */
 
        worktree = xcalloc(1, sizeof(*worktree));
        worktree->path = strbuf_detach(&worktree_path, NULL);