Skip to content

Commit 42b0059

Browse files
jastgitster
authored andcommitted
symbolic-ref --short: abbreviate the output unambiguously
It can be helpful to resolve a symbolic ref and output the result in a shortened form, such as for use in shell prompts. Add a "--short" option to do so. Signed-off-by: Jan Krüger <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3724cc7 commit 42b0059

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

Documentation/git-symbolic-ref.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ git-symbolic-ref - Read and modify symbolic refs
88
SYNOPSIS
99
--------
1010
[verse]
11-
'git symbolic-ref' [-q] [-m <reason>] <name> [<ref>]
11+
'git symbolic-ref' [-m <reason>] <name> <ref>
12+
'git symbolic-ref' [-q] [--short] <name>
1213

1314
DESCRIPTION
1415
-----------
@@ -33,6 +34,10 @@ OPTIONS
3334
symbolic ref but a detached HEAD; instead exit with
3435
non-zero status silently.
3536

37+
--short::
38+
When showing the value of <name> as a symbolic ref, try to shorten the
39+
value, e.g. from `refs/heads/master` to `master`.
40+
3641
-m::
3742
Update the reflog for <name> with <reason>. This is valid only
3843
when creating or updating a symbolic ref.

builtin/symbolic-ref.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,25 @@ static const char * const git_symbolic_ref_usage[] = {
88
NULL
99
};
1010

11+
static int shorten;
12+
1113
static void check_symref(const char *HEAD, int quiet)
1214
{
1315
unsigned char sha1[20];
1416
int flag;
15-
const char *refs_heads_master = resolve_ref_unsafe(HEAD, sha1, 0, &flag);
17+
const char *refname = resolve_ref_unsafe(HEAD, sha1, 0, &flag);
1618

17-
if (!refs_heads_master)
19+
if (!refname)
1820
die("No such ref: %s", HEAD);
1921
else if (!(flag & REF_ISSYMREF)) {
2022
if (!quiet)
2123
die("ref %s is not a symbolic ref", HEAD);
2224
else
2325
exit(1);
2426
}
25-
puts(refs_heads_master);
27+
if (shorten)
28+
refname = shorten_unambiguous_ref(refname, 0);
29+
puts(refname);
2630
}
2731

2832
int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
@@ -32,6 +36,7 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
3236
struct option options[] = {
3337
OPT__QUIET(&quiet,
3438
"suppress error message for non-symbolic (detached) refs"),
39+
OPT_BOOL(0, "short", &shorten, "shorten ref output"),
3540
OPT_STRING('m', NULL, &msg, "reason", "reason of the update"),
3641
OPT_END(),
3742
};

0 commit comments

Comments
 (0)