Skip to content

Commit 3be9ac7

Browse files
committed
Merge branch 'jc/check-ref-format-oor' into maint
"git check-ref-format --branch @{-1}" bit a "BUG()" when run outside a repository for obvious reasons; clarify the documentation and make sure we do not even try to expand the at-mark magic in such a case, but still call the validation logic for branch names. * jc/check-ref-format-oor: check-ref-format doc: --branch validates and expands <branch> check-ref-format --branch: strip refs/heads/ using skip_prefix check-ref-format --branch: do not expand @{...} outside repository
2 parents 2e13879 + 89dd32a commit 3be9ac7

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

Documentation/git-check-ref-format.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,14 @@ reference name expressions (see linkgit:gitrevisions[7]):
7777

7878
. at-open-brace `@{` is used as a notation to access a reflog entry.
7979

80-
With the `--branch` option, it expands the ``previous branch syntax''
80+
With the `--branch` option, the command takes a name and checks if
81+
it can be used as a valid branch name (e.g. when creating a new
82+
branch). The rule `git check-ref-format --branch $name` implements
83+
may be stricter than what `git check-ref-format refs/heads/$name`
84+
says (e.g. a dash may appear at the beginning of a ref component,
85+
but it is explicitly forbidden at the beginning of a branch name).
86+
When run with `--branch` option in a repository, the input is first
87+
expanded for the ``previous branch syntax''
8188
`@{-n}`. For example, `@{-1}` is a way to refer the last branch you
8289
were on. This option should be used by porcelains to accept this
8390
syntax anywhere a branch name is expected, so they can act as if you

builtin/check-ref-format.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,14 @@ static char *collapse_slashes(const char *refname)
3939
static int check_ref_format_branch(const char *arg)
4040
{
4141
struct strbuf sb = STRBUF_INIT;
42+
const char *name;
4243
int nongit;
4344

4445
setup_git_directory_gently(&nongit);
45-
if (strbuf_check_branch_ref(&sb, arg))
46+
if (strbuf_check_branch_ref(&sb, arg) ||
47+
!skip_prefix(sb.buf, "refs/heads/", &name))
4648
die("'%s' is not a valid branch name", arg);
47-
printf("%s\n", sb.buf + 11);
49+
printf("%s\n", name);
4850
strbuf_release(&sb);
4951
return 0;
5052
}

sha1_name.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1331,7 +1331,10 @@ void strbuf_branchname(struct strbuf *sb, const char *name, unsigned allowed)
13311331

13321332
int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
13331333
{
1334-
strbuf_branchname(sb, name, INTERPRET_BRANCH_LOCAL);
1334+
if (startup_info->have_repository)
1335+
strbuf_branchname(sb, name, INTERPRET_BRANCH_LOCAL);
1336+
else
1337+
strbuf_addstr(sb, name);
13351338
if (name[0] == '-')
13361339
return -1;
13371340
strbuf_splice(sb, 0, 0, "refs/heads/", 11);

t/t1402-check-ref-format.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ test_expect_success "check-ref-format --branch @{-1}" '
144144
refname2=$(git check-ref-format --branch @{-2}) &&
145145
test "$refname2" = master'
146146

147+
test_expect_success 'check-ref-format --branch -naster' '
148+
test_must_fail git check-ref-format --branch -naster >actual &&
149+
test_must_be_empty actual
150+
'
151+
147152
test_expect_success 'check-ref-format --branch from subdir' '
148153
mkdir subdir &&
149154
@@ -161,6 +166,17 @@ test_expect_success 'check-ref-format --branch from subdir' '
161166
test "$refname" = "$sha1"
162167
'
163168

169+
test_expect_success 'check-ref-format --branch @{-1} from non-repo' '
170+
nongit test_must_fail git check-ref-format --branch @{-1} >actual &&
171+
test_must_be_empty actual
172+
'
173+
174+
test_expect_success 'check-ref-format --branch master from non-repo' '
175+
echo master >expect &&
176+
nongit git check-ref-format --branch master >actual &&
177+
test_cmp expect actual
178+
'
179+
164180
valid_ref_normalized() {
165181
prereq=
166182
case $1 in

0 commit comments

Comments
 (0)