Subject: [PATCH 2/2] Allow "check-ref-format --branch" from subdirectory

From: Jonathan Nieder <[email-masked]>
Date: Thu, 5 Aug 2010 22:39:22 -0500

check-ref-format --branch requires access to the repository
to resolve refs like @{-1}.

Noticed by Duy.

Cc: Nguyễn Thái Ngọc Duy <[email-masked]>
Signed-off-by: Jonathan Nieder <[email-masked]>
---
The original used the RUN_SETUP_GENTLY flag to run setup
unconditionally.  That might still be a good idea, but I am
more comfortable running setup just for this subcommand
for now so commands like "git check-ref-format refs/foo" are not
affected.

 builtin/check-ref-format.c  |    2 ++
 t/t1402-check-ref-format.sh |   17 +++++++++++++++++
 2 files changed, 19 insertions(+), 0 deletions(-)

diff --git a/builtin/check-ref-format.c b/builtin/check-ref-format.c
index 8707ee9..ae3f281 100644
--- a/builtin/check-ref-format.c
+++ b/builtin/check-ref-format.c
@@ -36,7 +36,9 @@ static void collapse_slashes(char *dst, const char *src)
 static int check_ref_format_branch(const char *arg)
 {
 	struct strbuf sb = STRBUF_INIT;
+	int nongit;
 
+	setup_git_directory_gently(&nongit);
 	if (strbuf_check_branch_ref(&sb, arg))
 		die("'%s' is not a valid branch name", arg);
 	printf("%s\n", sb.buf + 11);
diff --git a/t/t1402-check-ref-format.sh b/t/t1402-check-ref-format.sh
index eb45afb..782e75d 100755
--- a/t/t1402-check-ref-format.sh
+++ b/t/t1402-check-ref-format.sh
@@ -41,6 +41,23 @@ test_expect_success "check-ref-format --branch @{-1}" '
 	refname2=$(git check-ref-format --branch @{-2}) &&
 	test "$refname2" = master'
 
+test_expect_success 'check-ref-format --branch from subdir' '
+	mkdir subdir &&
+
+	T=$(git write-tree) &&
+	sha1=$(echo A | git commit-tree $T) &&
+	git update-ref refs/heads/master $sha1 &&
+	git update-ref refs/remotes/origin/master $sha1
+	git checkout master &&
+	git checkout origin/master &&
+	git checkout master &&
+	refname=$(
+		cd subdir &&
+		git check-ref-format --branch @{-1}
+	) &&
+	test "$refname" = "$sha1"
+'
+
 valid_ref_normalized() {
 	test_expect_success "ref name '$1' simplifies to '$2'" "
 		refname=\$(git check-ref-format --print '$1') &&
-- 
1.7.2.1.544.ga752d.dirty