From: Eric Sunshine <sunshine@sunshineco.com>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>,
Duy Nguyen <pclouds@gmail.com>,
Michael Rappazzo <rappazzo@gmail.com>,
Eric Sunshine <sunshine@sunshineco.com>
Subject: [PATCH 4/4] worktree: retire special-case normalization of main worktree path
Date: Fri, 31 Jul 2020 19:32:14 -0400 [thread overview]
Message-ID: <20200731233214.22131-5-sunshine@sunshineco.com> (raw)
In-Reply-To: <20200731233214.22131-1-sunshine@sunshineco.com>
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 <sunshine@sunshineco.com>
---
Notes:
This is a followup to 5f4ee57ad9 (worktree: avoid dead-code in
conditional, 2020-06-24) which dropped dead code from the manual
normalization done by get_main_worktree() but which did not
eliminate the special cases.
It's also a direct response to Junio's observation[1] that it would
be better if we didn't have to handle these special cases in the
first place.
[1]: https://lore.kernel.org/git/xmqqbll8569x.fsf@gitster.c.googlers.com/
worktree.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/worktree.c b/worktree.c
index 355824bf87..62217b4a6b 100644
--- a/worktree.c
+++ b/worktree.c
@@ -49,10 +49,8 @@ static struct worktree *get_main_worktree(void)
struct worktree *worktree = NULL;
struct strbuf worktree_path = STRBUF_INIT;
- strbuf_add_absolute_path(&worktree_path, get_git_common_dir());
- 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 */
+ strbuf_add_real_path(&worktree_path, get_git_common_dir());
+ strbuf_strip_suffix(&worktree_path, "/.git");
worktree = xcalloc(1, sizeof(*worktree));
worktree->path = strbuf_detach(&worktree_path, NULL);
--
2.28.0.203.gce1f2e0ef1
prev parent reply other threads:[~2020-07-31 23:36 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-07-31 23:32 [PATCH 0/4] worktree: cleanups & simplification Eric Sunshine
2020-07-31 23:32 ` [PATCH 1/4] worktree: drop pointless strbuf_release() Eric Sunshine
2020-07-31 23:32 ` [PATCH 2/4] worktree: drop unused code from get_linked_worktree() Eric Sunshine
2020-07-31 23:32 ` [PATCH 3/4] worktree: drop bogus and unnecessary path munging Eric Sunshine
2020-07-31 23:32 ` Eric Sunshine [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200731233214.22131-5-sunshine@sunshineco.com \
--to=sunshine@sunshineco.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=pclouds@gmail.com \
--cc=rappazzo@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).