Re: [PATCH] worktree: avoid dead-code in conditional
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Jun 24, 2020, 21:20 UTC
- Message-ID
- <xmqqsgek5fg3.fsf@gitster.c.googlers.com>
- In-Reply-To
- <20200624190541.5253-1-sunshine@sunshineco.com>
Eric Sunshine <sunshine@sunshineco.com> writes:
Show 9 quoted lines
> 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)
It is unclear from the above but I would assume that you are talking about the returned path from get_git_common_dir().
I can certainly understand why there needs two distinct cases (i.e.. bare vs non-bare), but why is this codepath (or any caller of get_git_common_dir()) forced to care about the two cases?
I wonder if the right "fix" to this instance, at the same time preventing similar breakages in the future, is rather make sure get_git_common_dir() not to return the redundant path with ".git/." suffixed? For that matter, I do not know why the bare case must need "/." suffix. There seem to be about a dozen callers of the function, but don't some of them share a similar issue?
Let's look at the other two grep hits from worktree.c
strbuf_reset(&path); strbuf_addf(&path, "%s/worktrees/%s/HEAD", get_git_common_dir(), id);
worktree = xcalloc(1, sizeof(*worktree)); worktree->path = strbuf_detach(&worktree_path, NULL); worktree->id = xstrdup(id); add_head_info(worktree);
done: strbuf_release(&path); strbuf_release(&worktree_path); return worktree; }
This looks somewhat bogus. "sturct strbuf path" is populated, but is released without ever getting used, isn't it? Am I grossly misreading the code?
The other one
strbuf_addf(&path, "%s/worktrees", get_git_common_dir());
dir = opendir(path.buf);
strbuf_release(&path);
if (dir) {
while ((d = readdir(dir)) != NULL) {
...is a regular filesystem access, but it ends up opening the directory with a path like "foo/.git/./worktrees", where we should be using a more reasonable "foo/.git/worktrees" to access it. The redundant "/./" is not wrong per-se, but it looks sloppy to depend on "not wrong per-se".
Puzzled.