From 42b00599be684e20462c431a718f86c2b6fce575 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kr=C3=BCger?= Date: Mon, 27 Feb 2012 23:10:38 +0100 Subject: symbolic-ref --short: abbreviate the output unambiguously MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Junio C Hamano diff --git a/Documentation/git-symbolic-ref.txt b/Documentation/git-symbolic-ref.txt index a45d4c4..981d3a8 100644 --- a/Documentation/git-symbolic-ref.txt +++ b/Documentation/git-symbolic-ref.txt @@ -8,7 +8,8 @@ git-symbolic-ref - Read and modify symbolic refs SYNOPSIS -------- [verse] -'git symbolic-ref' [-q] [-m ] [] +'git symbolic-ref' [-m ] +'git symbolic-ref' [-q] [--short] DESCRIPTION ----------- @@ -33,6 +34,10 @@ OPTIONS symbolic ref but a detached HEAD; instead exit with non-zero status silently. +--short:: + When showing the value of as a symbolic ref, try to shorten the + value, e.g. from `refs/heads/master` to `master`. + -m:: Update the reflog for with . This is valid only when creating or updating a symbolic ref. diff --git a/builtin/symbolic-ref.c b/builtin/symbolic-ref.c index 2ef5962..801d62e 100644 --- a/builtin/symbolic-ref.c +++ b/builtin/symbolic-ref.c @@ -8,13 +8,15 @@ static const char * const git_symbolic_ref_usage[] = { NULL }; +static int shorten; + static void check_symref(const char *HEAD, int quiet) { unsigned char sha1[20]; 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); else if (!(flag & REF_ISSYMREF)) { if (!quiet) @@ -22,7 +24,9 @@ static void check_symref(const char *HEAD, int quiet) else 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) @@ -32,6 +36,7 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix) struct option options[] = { OPT__QUIET(&quiet, "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_END(), }; -- cgit v0.10.2-6-g49f6