From 5c20398699165a91af2d81ea2d20385bc8dd3627 Mon Sep 17 00:00:00 2001 From: Emily Shaffer Date: Mon, 2 Mar 2020 20:05:06 -0800 Subject: prefix_path: show gitdir if worktree unavailable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 e0020b2f ("prefix_path: show gitdir when arg is outside repo", 2020-02-14). Signed-off-by: Emily Shaffer [jc: added minimum tests, with help from Szeder Gábor] Signed-off-by: Junio C Hamano --- setup.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'setup.c') 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; } -- cgit v1.3