summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2017-11-15 12:04:57 +0900
committerJunio C Hamano <gitster@pobox.com>2017-11-15 12:04:57 +0900
commit3be9ac7e56d4bb4ae805543a91de5987e148b661 (patch)
treeb0ea94ad987a76b159765993d5a70326a4732136
parent2e138796d82794f5bb2bccee8680b10346953518 (diff)
parent89dd32aedcb52a77240f3923a687edc8a61662e7 (diff)
downloadgit-3be9ac7e56d4bb4ae805543a91de5987e148b661.tar.gz
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
-rw-r--r--Documentation/git-check-ref-format.txt9
-rw-r--r--builtin/check-ref-format.c6
-rw-r--r--sha1_name.c5
-rwxr-xr-xt/t1402-check-ref-format.sh16
4 files changed, 32 insertions, 4 deletions
diff --git a/Documentation/git-check-ref-format.txt b/Documentation/git-check-ref-format.txt
index 92777cef25..cf0a0b7df2 100644
--- a/Documentation/git-check-ref-format.txt
+++ b/Documentation/git-check-ref-format.txt
@@ -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
diff --git a/builtin/check-ref-format.c b/builtin/check-ref-format.c
index 6c40ff110b..bc67d3f0a8 100644
--- a/builtin/check-ref-format.c
+++ b/builtin/check-ref-format.c
@@ -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;
}
diff --git a/sha1_name.c b/sha1_name.c
index c7c5ab376c..603e667faa 100644
--- a/sha1_name.c
+++ b/sha1_name.c
@@ -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)
{
- 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);
diff --git a/t/t1402-check-ref-format.sh b/t/t1402-check-ref-format.sh
index 0790edf60d..98e4a8613b 100755
--- a/t/t1402-check-ref-format.sh
+++ b/t/t1402-check-ref-format.sh
@@ -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