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
This commit is contained in:
Junio C Hamano 2017-11-15 12:04:57 +09:00
commit 3be9ac7e56
4 changed files with 32 additions and 4 deletions

View file

@ -77,7 +77,14 @@ reference name expressions (see linkgit:gitrevisions[7]):
. at-open-brace `@{` is used as a notation to access a reflog entry. . at-open-brace `@{` is used as a notation to access a reflog entry.
With the `--branch` option, it expands the ``previous branch syntax'' With the `--branch` option, the command takes a name and checks if
it can be used as a valid branch name (e.g. when creating a new
branch). The rule `git check-ref-format --branch $name` implements
may be stricter than what `git check-ref-format refs/heads/$name`
says (e.g. a dash may appear at the beginning of a ref component,
but it is explicitly forbidden at the beginning of a branch name).
When run with `--branch` option in a repository, the input is first
expanded for the ``previous branch syntax''
`@{-n}`. For example, `@{-1}` is a way to refer the last branch you `@{-n}`. For example, `@{-1}` is a way to refer the last branch you
were on. This option should be used by porcelains to accept this were on. This option should be used by porcelains to accept this
syntax anywhere a branch name is expected, so they can act as if you syntax anywhere a branch name is expected, so they can act as if you

View file

@ -39,12 +39,14 @@ static char *collapse_slashes(const char *refname)
static int check_ref_format_branch(const char *arg) static int check_ref_format_branch(const char *arg)
{ {
struct strbuf sb = STRBUF_INIT; struct strbuf sb = STRBUF_INIT;
const char *name;
int nongit; int nongit;
setup_git_directory_gently(&nongit); setup_git_directory_gently(&nongit);
if (strbuf_check_branch_ref(&sb, arg)) if (strbuf_check_branch_ref(&sb, arg) ||
!skip_prefix(sb.buf, "refs/heads/", &name))
die("'%s' is not a valid branch name", arg); die("'%s' is not a valid branch name", arg);
printf("%s\n", sb.buf + 11); printf("%s\n", name);
strbuf_release(&sb); strbuf_release(&sb);
return 0; return 0;
} }

View file

@ -1331,7 +1331,10 @@ void strbuf_branchname(struct strbuf *sb, const char *name, unsigned allowed)
int strbuf_check_branch_ref(struct strbuf *sb, const char *name) int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
{ {
strbuf_branchname(sb, name, INTERPRET_BRANCH_LOCAL); if (startup_info->have_repository)
strbuf_branchname(sb, name, INTERPRET_BRANCH_LOCAL);
else
strbuf_addstr(sb, name);
if (name[0] == '-') if (name[0] == '-')
return -1; return -1;
strbuf_splice(sb, 0, 0, "refs/heads/", 11); strbuf_splice(sb, 0, 0, "refs/heads/", 11);

View file

@ -144,6 +144,11 @@ test_expect_success "check-ref-format --branch @{-1}" '
refname2=$(git check-ref-format --branch @{-2}) && refname2=$(git check-ref-format --branch @{-2}) &&
test "$refname2" = master' test "$refname2" = master'
test_expect_success 'check-ref-format --branch -naster' '
test_must_fail git check-ref-format --branch -naster >actual &&
test_must_be_empty actual
'
test_expect_success 'check-ref-format --branch from subdir' ' test_expect_success 'check-ref-format --branch from subdir' '
mkdir subdir && mkdir subdir &&
@ -161,6 +166,17 @@ test_expect_success 'check-ref-format --branch from subdir' '
test "$refname" = "$sha1" test "$refname" = "$sha1"
' '
test_expect_success 'check-ref-format --branch @{-1} from non-repo' '
nongit test_must_fail git check-ref-format --branch @{-1} >actual &&
test_must_be_empty actual
'
test_expect_success 'check-ref-format --branch master from non-repo' '
echo master >expect &&
nongit git check-ref-format --branch master >actual &&
test_cmp expect actual
'
valid_ref_normalized() { valid_ref_normalized() {
prereq= prereq=
case $1 in case $1 in