@matheusbsbs / matheus-bagliano / commits / a31dca0393
Fork 0

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 <gitster@pobox.com>

Junio C Hamano committed Mar 21, 2009 at 14:19 UTC a31dca0393fefae894b7a155ae24000107bcc383
2 files changed +22
Documentation/git-check-ref-format.txt
+12
@@ -7,7 +7,9 @@ git-check-ref-format - Make sure ref name is well formed
7
8 SYNOPSIS
9 --------
10 +[verse]
11 'git check-ref-format' <refname>
12 +'git check-ref-format' [--branch] <branchname-shorthand>
13
14 DESCRIPTION
15 -----------
@@ -49,6 +51,16 @@ refname expressions (see linkgit:git-rev-parse[1]). Namely:
51 It may also be used to select a specific object such as with
52 'git-cat-file': "git cat-file blob v1.3.3:refs.c".
53
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 +
64
65 GIT
66 ---
builtin-check-ref-format.c
+10
@@ -5,9 +5,19 @@
5 #include "cache.h"
6 #include "refs.h"
7 #include "builtin.h"
8 +#include "strbuf.h"
9
10 int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
11 {
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 + }
21 if (argc != 2)
22 usage("git check-ref-format refname");
23 return !!check_ref_format(argv[1]);