Skip to content

Commit a31dca0

Browse files
committed
check-ref-format --branch: give Porcelain a way to grok branch shorthand
The command may not be the best place to add this new feature, but $ git check-ref-format --branch "@{-1}" allows Porcelains to figure out what branch you were on before the last branch switching. Signed-off-by: Junio C Hamano <[email protected]>
1 parent a552de7 commit a31dca0

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

Documentation/git-check-ref-format.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ git-check-ref-format - Make sure ref name is well formed
77

88
SYNOPSIS
99
--------
10+
[verse]
1011
'git check-ref-format' <refname>
12+
'git check-ref-format' [--branch] <branchname-shorthand>
1113

1214
DESCRIPTION
1315
-----------
@@ -49,6 +51,16 @@ refname expressions (see linkgit:git-rev-parse[1]). Namely:
4951
It may also be used to select a specific object such as with
5052
'git-cat-file': "git cat-file blob v1.3.3:refs.c".
5153

54+
With the `--branch` option, it expands a branch name shorthand and
55+
prints the name of the branch the shorthand refers to.
56+
57+
EXAMPLE
58+
-------
59+
60+
git check-ref-format --branch @{-1}::
61+
62+
Print the name of the previous branch.
63+
5264

5365
GIT
5466
---

builtin-check-ref-format.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,19 @@
55
#include "cache.h"
66
#include "refs.h"
77
#include "builtin.h"
8+
#include "strbuf.h"
89

910
int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
1011
{
12+
if (argc == 3 && !strcmp(argv[1], "--branch")) {
13+
struct strbuf sb = STRBUF_INIT;
14+
strbuf_branchname(&sb, argv[2]);
15+
strbuf_splice(&sb, 0, 0, "refs/heads/", 11);
16+
if (check_ref_format(sb.buf))
17+
die("'%s' is not a valid branch name", argv[2]);
18+
printf("%s\n", sb.buf + 11);
19+
exit(0);
20+
}
1121
if (argc != 2)
1222
usage("git check-ref-format refname");
1323
return !!check_ref_format(argv[1]);

0 commit comments

Comments
 (0)