git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Emily Shaffer <emilyshaffer@google.com>
To: git@vger.kernel.org
Cc: Emily Shaffer <emilyshaffer@google.com>,
	Jonathan Nieder <jrnieder@gmail.com>,
	"brian m. carlson" <sandals@crustytoothpaste.net>
Subject: [PATCH] prefix_path: show gitdir if worktree unavailable
Date: Mon,  2 Mar 2020 20:05:06 -0800	[thread overview]
Message-ID: <20200303040506.241376-1-emilyshaffer@google.com> (raw)
In-Reply-To: <20200228195805.GA190372@google.com>

If there is no worktree at present, we can still hint the user about
Git's current directory by showing them the absolute path to the Git
directory. Even though the Git directory doesn't make it as easy to
locate the worktree in question, it can still help a user figure out
what's going on while developing a script.

This fixes a segmentation fault introduced in e0020b2f829.

Signed-off-by: Emily Shaffer <emilyshaffer@google.com>
---
This patch is built on top of es/outside-repo-errmsg-hints. I think it
doesn't need to be, since that change is in master, though.

Sounds like there's a segfault in the wild:
https://lore.kernel.org/git/20200228195805.GA190372@google.com

I figured since the error doesn't specify "this is the worktree" or
"this is the gitdir" to the user, it didn't need more prettying when
falling back to gitdir in this patch.

It seems to me that if we are checking whether something is inside or
outside of the repository, we definitely will get a good result from
get_git_dir() - if there's no Git dir, then there's no repository to be
in or out of.

CI run here: https://github.com/gitgitgadget/git/pull/572/checks

 - Emily

 pathspec.c | 8 ++++++--
 setup.c    | 8 ++++++--
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/pathspec.c b/pathspec.c
index 166d255642..8243e06eab 100644
--- a/pathspec.c
+++ b/pathspec.c
@@ -438,9 +438,13 @@ static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
 	} else {
 		match = prefix_path_gently(prefix, prefixlen,
 					   &prefixlen, copyfrom);
-		if (!match)
+		if (!match) {
+			const char *hint_path = get_git_work_tree();
+			if (!hint_path)
+				hint_path = get_git_dir();
 			die(_("%s: '%s' is outside repository at '%s'"), elt,
-			    copyfrom, absolute_path(get_git_work_tree()));
+			    copyfrom, absolute_path(hint_path));
+		}
 	}
 
 	item->match = match;
diff --git a/setup.c b/setup.c
index 17814a080b..f4897287f7 100644
--- a/setup.c
+++ b/setup.c
@@ -120,9 +120,13 @@ char *prefix_path_gently(const char *prefix, int len,
 char *prefix_path(const char *prefix, int len, const char *path)
 {
 	char *r = prefix_path_gently(prefix, len, NULL, path);
-	if (!r)
+	if (!r) {
+		const char *hint_path = get_git_work_tree();
+		if (!hint_path)
+			hint_path = get_git_dir();
 		die(_("'%s' is outside repository at '%s'"), path,
-		    absolute_path(get_git_work_tree()));
+		    absolute_path(hint_path));
+	}
 	return r;
 }
 
-- 
2.25.0.265.gbab2e86ba0-goog


  parent reply	other threads:[~2020-03-03  4:05 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-14 23:29 [RFC PATCH] prefix_path: show gitdir when arg is outside repo emilyshaffer
2020-02-15  0:02 ` brian m. carlson
2020-02-15  0:46   ` Emily Shaffer
2020-02-15  1:00 ` [RFC PATCH v2] " Emily Shaffer
2020-02-15  2:56   ` brian m. carlson
2020-02-28 19:58   ` Jonathan Nieder
2020-03-03  2:19     ` Emily Shaffer
2020-03-03  4:05     ` Emily Shaffer [this message]
2020-03-03 17:06       ` [PATCH] prefix_path: show gitdir if worktree unavailable Junio C Hamano

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200303040506.241376-1-emilyshaffer@google.com \
    --to=emilyshaffer@google.com \
    --cc=git@vger.kernel.org \
    --cc=jrnieder@gmail.com \
    --cc=sandals@crustytoothpaste.net \
    /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.
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 NNTP newsgroup(s).