Merge branch 'jc/check-ref-format-oor'

"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-06 14:24:28 +09:00
Родитель f113d4bc79 89dd32aedc
Коммит 5a74ce22e6
4 изменённых файлов: 32 добавлений и 4 удалений

Просмотреть файл

@ -77,7 +77,14 @@ reference name expressions (see linkgit:gitrevisions[7]):
. 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
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

Просмотреть файл

@ -39,12 +39,14 @@ static char *collapse_slashes(const char *refname)
static int check_ref_format_branch(const char *arg)
{
struct strbuf sb = STRBUF_INIT;
const char *name;
int 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);
printf("%s\n", sb.buf + 11);
printf("%s\n", name);
strbuf_release(&sb);
return 0;
}

Просмотреть файл

@ -1434,7 +1434,10 @@ void strbuf_branchname(struct strbuf *sb, const char *name, unsigned allowed)
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] == '-')
return -1;
strbuf_splice(sb, 0, 0, "refs/heads/", 11);

Просмотреть файл

@ -144,6 +144,11 @@ 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 -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' '
mkdir subdir &&
@ -161,6 +166,17 @@ test_expect_success 'check-ref-format --branch from subdir' '
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() {
prereq=
case $1 in