Skip to content

Commit 49cc460

Browse files
jrngitster
authored andcommitted
Allow "check-ref-format --branch" from subdirectory
check-ref-format --branch requires access to the repository to resolve refs like @{-1}. Noticed by Nguyễn Thái Ngọc Duy. Cc: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent cfbe22f commit 49cc460

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

builtin/check-ref-format.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ static void collapse_slashes(char *dst, const char *src)
3636
static int check_ref_format_branch(const char *arg)
3737
{
3838
struct strbuf sb = STRBUF_INIT;
39+
int nongit;
3940

41+
setup_git_directory_gently(&nongit);
4042
if (strbuf_check_branch_ref(&sb, arg))
4143
die("'%s' is not a valid branch name", arg);
4244
printf("%s\n", sb.buf + 11);

t/t1402-check-ref-format.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,23 @@ test_expect_success "check-ref-format --branch @{-1}" '
4141
refname2=$(git check-ref-format --branch @{-2}) &&
4242
test "$refname2" = master'
4343

44+
test_expect_success 'check-ref-format --branch from subdir' '
45+
mkdir subdir &&
46+
47+
T=$(git write-tree) &&
48+
sha1=$(echo A | git commit-tree $T) &&
49+
git update-ref refs/heads/master $sha1 &&
50+
git update-ref refs/remotes/origin/master $sha1
51+
git checkout master &&
52+
git checkout origin/master &&
53+
git checkout master &&
54+
refname=$(
55+
cd subdir &&
56+
git check-ref-format --branch @{-1}
57+
) &&
58+
test "$refname" = "$sha1"
59+
'
60+
4461
valid_ref_normalized() {
4562
test_expect_success "ref name '$1' simplifies to '$2'" "
4663
refname=\$(git check-ref-format --print '$1') &&

0 commit comments

Comments
 (0)