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 <jk@jk.gs> Signed-off-by: Junio C Hamano <gitster@pobox.com>maint
parent
3724cc7c58
commit
42b00599be
|
@ -8,7 +8,8 @@ git-symbolic-ref - Read and modify symbolic refs
|
||||||
SYNOPSIS
|
SYNOPSIS
|
||||||
--------
|
--------
|
||||||
[verse]
|
[verse]
|
||||||
'git symbolic-ref' [-q] [-m <reason>] <name> [<ref>]
|
'git symbolic-ref' [-m <reason>] <name> <ref>
|
||||||
|
'git symbolic-ref' [-q] [--short] <name>
|
||||||
|
|
||||||
DESCRIPTION
|
DESCRIPTION
|
||||||
-----------
|
-----------
|
||||||
|
@ -33,6 +34,10 @@ OPTIONS
|
||||||
symbolic ref but a detached HEAD; instead exit with
|
symbolic ref but a detached HEAD; instead exit with
|
||||||
non-zero status silently.
|
non-zero status silently.
|
||||||
|
|
||||||
|
--short::
|
||||||
|
When showing the value of <name> as a symbolic ref, try to shorten the
|
||||||
|
value, e.g. from `refs/heads/master` to `master`.
|
||||||
|
|
||||||
-m::
|
-m::
|
||||||
Update the reflog for <name> with <reason>. This is valid only
|
Update the reflog for <name> with <reason>. This is valid only
|
||||||
when creating or updating a symbolic ref.
|
when creating or updating a symbolic ref.
|
||||||
|
|
|
@ -8,13 +8,15 @@ static const char * const git_symbolic_ref_usage[] = {
|
||||||
NULL
|
NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static int shorten;
|
||||||
|
|
||||||
static void check_symref(const char *HEAD, int quiet)
|
static void check_symref(const char *HEAD, int quiet)
|
||||||
{
|
{
|
||||||
unsigned char sha1[20];
|
unsigned char sha1[20];
|
||||||
int flag;
|
int flag;
|
||||||
const char *refs_heads_master = resolve_ref_unsafe(HEAD, sha1, 0, &flag);
|
const char *refname = resolve_ref_unsafe(HEAD, sha1, 0, &flag);
|
||||||
|
|
||||||
if (!refs_heads_master)
|
if (!refname)
|
||||||
die("No such ref: %s", HEAD);
|
die("No such ref: %s", HEAD);
|
||||||
else if (!(flag & REF_ISSYMREF)) {
|
else if (!(flag & REF_ISSYMREF)) {
|
||||||
if (!quiet)
|
if (!quiet)
|
||||||
|
@ -22,7 +24,9 @@ static void check_symref(const char *HEAD, int quiet)
|
||||||
else
|
else
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
puts(refs_heads_master);
|
if (shorten)
|
||||||
|
refname = shorten_unambiguous_ref(refname, 0);
|
||||||
|
puts(refname);
|
||||||
}
|
}
|
||||||
|
|
||||||
int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
|
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)
|
||||||
struct option options[] = {
|
struct option options[] = {
|
||||||
OPT__QUIET(&quiet,
|
OPT__QUIET(&quiet,
|
||||||
"suppress error message for non-symbolic (detached) refs"),
|
"suppress error message for non-symbolic (detached) refs"),
|
||||||
|
OPT_BOOL(0, "short", &shorten, "shorten ref output"),
|
||||||
OPT_STRING('m', NULL, &msg, "reason", "reason of the update"),
|
OPT_STRING('m', NULL, &msg, "reason", "reason of the update"),
|
||||||
OPT_END(),
|
OPT_END(),
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue