All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] builtin/symbolic-ref.c: add option to fall back to normal ref
@ 2012-02-27 22:08 Jan Krüger
  2012-02-27 22:21 ` Junio C Hamano
  0 siblings, 1 reply; 3+ messages in thread
From: Jan Krüger @ 2012-02-27 22:08 UTC (permalink / raw)
  To: git

Frequently, people want to determine the current value of HEAD in
scripts. However, there is no tool that can always output it, since "git
symbolic-ref" will fail if HEAD isn't currently a symref, and other
tools (e.g. "git rev-parse --symbolic-full-name") will also fail in
one of HEAD's possible modes.

To resolve this situation, add the new -f option to symbolic-ref that
falls back to outputting the value of HEAD as a normal ref if necessary.

Signed-off-by: Jan Krüger <jk@jk.gs>
---
 Documentation/git-symbolic-ref.txt |    7 ++++++-
 builtin/symbolic-ref.c             |   16 ++++++++++++++--
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/Documentation/git-symbolic-ref.txt b/Documentation/git-symbolic-ref.txt
index a45d4c4..a05819b 100644
--- a/Documentation/git-symbolic-ref.txt
+++ b/Documentation/git-symbolic-ref.txt
@@ -8,7 +8,7 @@ git-symbolic-ref - Read and modify symbolic refs
 SYNOPSIS
 --------
 [verse]
-'git symbolic-ref' [-q] [-m <reason>] <name> [<ref>]
+'git symbolic-ref' [-q] [-f] [-m <reason>] <name> [<ref>]
 
 DESCRIPTION
 -----------
@@ -33,6 +33,11 @@ OPTIONS
 	symbolic ref but a detached HEAD; instead exit with
 	non-zero status silently.
 
+-f::
+	When showing the current value of <name>, do not fail if it is
+	not a symbolic ref; instead output the SHA1 value referenced by
+	<name>.
+
 -m::
 	Update the reflog for <name> with <reason>.  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..2e0a86f 100644
--- a/builtin/symbolic-ref.c
+++ b/builtin/symbolic-ref.c
@@ -8,6 +8,8 @@ static const char * const git_symbolic_ref_usage[] = {
 	NULL
 };
 
+static int fallback_regular_ref;
+
 static void check_symref(const char *HEAD, int quiet)
 {
 	unsigned char sha1[20];
@@ -17,10 +19,18 @@ static void check_symref(const char *HEAD, int quiet)
 	if (!refs_heads_master)
 		die("No such ref: %s", HEAD);
 	else if (!(flag & REF_ISSYMREF)) {
-		if (!quiet)
+		if (fallback_regular_ref) {
+			char sha1[20];
+			if (!get_sha1(HEAD, sha1))
+				puts(sha1_to_hex(sha1));
+			else
+				die("failed to resolve ref %s", HEAD);
+			return;
+		} else if (!quiet) {
 			die("ref %s is not a symbolic ref", HEAD);
-		else
+		} else {
 			exit(1);
+		}
 	}
 	puts(refs_heads_master);
 }
@@ -32,6 +42,8 @@ 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_BOOLEAN('f', NULL, &fallback_regular_ref,
+					"fall back to showing as a regular ref"),
 		OPT_STRING('m', NULL, &msg, "reason", "reason of the update"),
 		OPT_END(),
 	};
-- 
1.7.9.2.302.g3724c.dirty


From 1fffb746a65aac88d3af9bae785b1cfa58cbf31c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Kr=C3=BCger?= <jk@jk.gs>
Date: Mon, 27 Feb 2012 22:40:13 +0100
Subject: [PATCH 2/2] builtin/symbolic-ref.c: add option to output shortened
 ref
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

In scripts meant to generate user-consumable output, 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 new -s option to allow this.

Signed-off-by: Jan Krüger <jk@jk.gs>
---
 Documentation/git-symbolic-ref.txt |    6 +++++-
 builtin/symbolic-ref.c             |    5 +++++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/Documentation/git-symbolic-ref.txt b/Documentation/git-symbolic-ref.txt
index a05819b..7f108ce 100644
--- a/Documentation/git-symbolic-ref.txt
+++ b/Documentation/git-symbolic-ref.txt
@@ -8,7 +8,7 @@ git-symbolic-ref - Read and modify symbolic refs
 SYNOPSIS
 --------
 [verse]
-'git symbolic-ref' [-q] [-f] [-m <reason>] <name> [<ref>]
+'git symbolic-ref' [-q] [-f] [-s] [-m <reason>] <name> [<ref>]
 
 DESCRIPTION
 -----------
@@ -38,6 +38,10 @@ OPTIONS
 	not a symbolic ref; instead output the SHA1 value referenced by
 	<name>.
 
+-s::
+	When showing the value of <name> as a symbolic ref, try to shorten the
+	value, e.g. from `refs/heads/master` to `master`.
+
 -m::
 	Update the reflog for <name> with <reason>.  This is valid only
 	when creating or updating a symbolic ref.
diff --git a/builtin/symbolic-ref.c b/builtin/symbolic-ref.c
index 2e0a86f..df8da11 100644
--- a/builtin/symbolic-ref.c
+++ b/builtin/symbolic-ref.c
@@ -9,6 +9,7 @@ static const char * const git_symbolic_ref_usage[] = {
 };
 
 static int fallback_regular_ref;
+static int shorten;
 
 static void check_symref(const char *HEAD, int quiet)
 {
@@ -32,6 +33,9 @@ static void check_symref(const char *HEAD, int quiet)
 			exit(1);
 		}
 	}
+	if (shorten)
+		refs_heads_master = shorten_unambiguous_ref(
+			refs_heads_master, 0);
 	puts(refs_heads_master);
 }
 
@@ -44,6 +48,7 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
 			"suppress error message for non-symbolic (detached) refs"),
 		OPT_BOOLEAN('f', NULL, &fallback_regular_ref,
 					"fall back to showing as a regular ref"),
+		OPT_BOOLEAN('s', NULL, &shorten, "shorten ref output"),
 		OPT_STRING('m', NULL, &msg, "reason", "reason of the update"),
 		OPT_END(),
 	};
-- 
1.7.9.2.302.g3724c.dirty

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH 1/2] builtin/symbolic-ref.c: add option to fall back to normal ref
  2012-02-27 22:08 [PATCH 1/2] builtin/symbolic-ref.c: add option to fall back to normal ref Jan Krüger
@ 2012-02-27 22:21 ` Junio C Hamano
  2012-02-27 22:40   ` Jan Krüger
  0 siblings, 1 reply; 3+ messages in thread
From: Junio C Hamano @ 2012-02-27 22:21 UTC (permalink / raw)
  To: Jan Krüger; +Cc: git

Jan Krüger <jk@jk.gs> writes:

> Frequently, people want to determine the current value of HEAD in
> scripts. However, there is no tool that can always output it, since "git
> symbolic-ref" will fail if HEAD isn't currently a symref, and other
> tools (e.g. "git rev-parse --symbolic-full-name") will also fail in
> one of HEAD's possible modes.

What is "the current value of HEAD"?

The symbolic-ref command is there for people who _care_ about the
distinction between a HEAD that points at a branch and a HEAD that points
directly at a commit.  There is no room for the command to "fall back"
anywhere, as that will only introduce an unnecessary ambiguity to the
command whose sole purpose is to be able to tell them apart.

If the caller does not need to know if the HEAD is detached or not, and
wants to know what commit it points at, why is it insufficient to just use
rev-parse, e.g. "git rev-parse --verify HEAD"?

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH 1/2] builtin/symbolic-ref.c: add option to fall back to normal ref
  2012-02-27 22:21 ` Junio C Hamano
@ 2012-02-27 22:40   ` Jan Krüger
  0 siblings, 0 replies; 3+ messages in thread
From: Jan Krüger @ 2012-02-27 22:40 UTC (permalink / raw)
  To: git

On 02/27/2012 11:21 PM, Junio C Hamano wrote:
> What is "the current value of HEAD"?

Well, essentially it contains a "type" and a "value". The type is either
"symbolic" or "not symbolic" and the value is a ref if HEAD is currently
symbolic or a SHA1 otherwise.

These definitions don't come from the glossary, obviously. It's just the
way I interpret HEAD. If you think the patch would be better off with a
different wording, I'd be happy for suggestions.

In any case, if you have the "value" without the "type", you can
actually derive the "type" from the "value": if it has the shape of a
SHA1, it was a direct ref; otherwise it was a symbolic ref (unless the
user has a ref that looks like a SHA1 but in that case some other tools
do funny things, too).

> The symbolic-ref command is there for people who _care_ about the
> distinction between a HEAD that points at a branch and a HEAD that points
> directly at a commit.  There is no room for the command to "fall back"
> anywhere, as that will only introduce an unnecessary ambiguity to the
> command whose sole purpose is to be able to tell them apart.

That's why it's an optional thing.

> If the caller does not need to know if the HEAD is detached or not, and
> wants to know what commit it points at, why is it insufficient to just use
> rev-parse, e.g. "git rev-parse --verify HEAD"?

The use case here is having one convenient command to remember what is
currently checked out, and being able to go back to that later. "git
checkout" will do the right thing if you just give it "master" or a
complete SHA1, so all we need to remember is that.

"git rev-parse --verify HEAD" will only give us the SHA1, even if HEAD
isn't actually detached.

Incidentally, the -s switch in the second patch makes this even easier:

    OLDHEAD=`git symbolic-ref -s -f HEAD`
    # do stuff here that involves switching branches or detaching or
    # whatever
    git checkout $OLDHEAD

Without these patches, the shortest way I can think of to do the same
thing would be:

    OLDHEAD=`git rev-parse --symbolic-full-name HEAD`
    [ "$OLDHEAD" = "HEAD" ] && OLDHEAD=`git rev-parse HEAD`
    # ...

-Jan

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-02-27 22:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-02-27 22:08 [PATCH 1/2] builtin/symbolic-ref.c: add option to fall back to normal ref Jan Krüger
2012-02-27 22:21 ` Junio C Hamano
2012-02-27 22:40   ` Jan Krüger

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.