Re: [PATCH v3 1/3] get_main_worktree(): allow it to be called in the Git directory
- From
Eric Sunshine <sunshine@sunshineco.com>
- Date
- Feb 24, 2020, 01:42 UTC
- Message-ID
- <CAPig+cTh-uu-obh9aeDOV9ptbVwRmkujgucbu9ei1Qa3qSNG_A@mail.gmail.com>
- In-Reply-To
- <8718facbc951614f19407afa6ca8d6110507483d.1582484231.git.gitgitgadget@gmail.com>
On Sun, Feb 23, 2020 at 1:57 PM Hariom Verma via GitGitGadget <gitgitgadget@gmail.com> wrote:
> get_main_worktree(): allow it to be called in the Git directory
This title is a bit too generic; it fails to explain what this patch is really fixing. Perhaps:
get_main_worktree: correctly normalize worktree path when in .git dir
or something.
> When called in the Git directory of a non-bare repository, this function > would not return the directory of the main worktree, but of the Git > directory instead.
"Git directory" is imprecise. As a reader, I can't tell if this means the main worktree into which the project is checked out or the `.git` directory itself. Please write it instead as "`.git` directory".
Show 13 quoted lines
> The reason: when the Git directory is the current working directory, the > absolute path of the common directory will be reported with a trailing > `/.git/.`, which the code of `get_main_worktree()` does not handle > correctly. > > Signed-off-by: Hariom Verma <hariom18599@gmail.com> > --- > diff --git a/worktree.c b/worktree.c > @@ -51,6 +51,7 @@ static struct worktree *get_main_worktree(void) > 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, "/.");
This change makes the code unnecessarily confusing and effectively turns the final line into dead code. I would much rather see the three cases spelled out explicitly, perhaps like this:
if (!strbuf_strip_suffix(&worktree_path, "/.git/.") && /* in .git dir */
!strbuf_strip_suffix(&worktree_path, "/.git/")) /* in worktree */
strbuf_strip_suffix(&worktree_path, "/."); /* in bare repo */Also, please add a test to ensure that this behavior doesn't regress in the future. You can probably test it via the "git worktree list" command, so perhaps add the test to t/t2402-worktree-list.sh.