Skip to content

Commit 918d8ff

Browse files
sunshinecogitster
authored andcommitted
worktree: retire special-case normalization of main worktree path
In order for "git-worktree list" to present consistent results, get_main_worktree() performs manual normalization on the repository path (returned by get_common_dir()) after passing it through strbuf_add_absolute_path(). In particular, it cleans up the path for three distinct cases when the current working directory is (1) the main worktree, (2) the .git/ subdirectory, or (3) a bare repository. The need for such special-cases is a direct consequence of employing strbuf_add_absolute_path() which, for the sake of efficiency, doesn't bother normalizing the path (such as folding out redundant path components) after making it absolute. Lack of normalization is not typically a problem since redundant path elements make no difference when working with paths at the filesystem level. However, when preparing paths for presentation, possible redundant path components make it difficult to ensure consistency. Eliminate the need for these special cases by instead making the path absolute via strbuf_add_real_path() which normalizes the path for us. Once normalized, the only case we need to handle manually is converting it to the path of the main worktree by stripping the "/.git" suffix. This stripping of the "/.git" suffix is a regular idiom in worktree-related code; for instance, it is employed by get_linked_worktree(), as well. Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1c4854e commit 918d8ff

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

worktree.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,8 @@ static struct worktree *get_main_worktree(void)
4949
struct worktree *worktree = NULL;
5050
struct strbuf worktree_path = STRBUF_INIT;
5151

52-
strbuf_add_absolute_path(&worktree_path, get_git_common_dir());
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 */
52+
strbuf_add_real_path(&worktree_path, get_git_common_dir());
53+
strbuf_strip_suffix(&worktree_path, "/.git");
5654

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

0 commit comments

Comments
 (0)