# [PATCH RFC] for-each-ref: Field with abbreviated objectname

4 messages from 2010-05-03 to 2010-05-14. Participants: Michael J Gruber, Jeff King.
Thread: https://gitlist.dev/t/23685

## Michael J Gruber, 2010-05-03 20:25

Subject: [PATCH RFC] for-each-ref: Field with abbreviated objectname
Message-ID: <61d114759b023b3937f8417ce44c3361f345c6f1.1272918259.git.git@drmicha.warpmail.net>
URL: https://gitlist.dev/e/61d114759b023b3937f8417ce44c3361f345c6f1.1272918259.git.git%40drmicha.warpmail.net

```
Introduce a :short modifier to objectname which outputs the abbreviated
object name.

Signed-off-by: Michael J Gruber <git@drmicha.warpmail.net>
---
Notes:
    I hope this finds the favour of the for-each-ref-man.
    
    TODO: test, doc

 builtin/for-each-ref.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)

diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
index 62be1bb..3a97953 100644
--- a/builtin/for-each-ref.c
+++ b/builtin/for-each-ref.c
@@ -227,6 +227,9 @@ static void grab_common_values(struct atom_value *val, int deref, struct object
 			strcpy(s, sha1_to_hex(obj->sha1));
 			v->s = s;
 		}
+		else if (!strcmp(name, "objectname:short")) {
+			v->s = find_unique_abbrev(obj->sha1, DEFAULT_ABBREV);
+		}
 	}
 }
 
-- 
1.7.1.328.g9993c

```

## Jeff King, 2010-05-04 11:01

Subject: Re: [PATCH RFC] for-each-ref: Field with abbreviated objectname
Message-ID: <20100504110134.GA7947@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20100504110134.GA7947%40coredump.intra.peff.net
In-Reply-To: <61d114759b023b3937f8417ce44c3361f345c6f1.1272918259.git.git@drmicha.warpmail.net>

```
On Mon, May 03, 2010 at 10:25:19PM +0200, Michael J Gruber wrote:

> Introduce a :short modifier to objectname which outputs the abbreviated
> object name.

I don't see any reason not to do this if you have a use for it.

>     TODO: test, doc

Yep.

> +		else if (!strcmp(name, "objectname:short")) {
> +			v->s = find_unique_abbrev(obj->sha1, DEFAULT_ABBREV);
> +		}

In theory there could be multiple such ":$foo" tags, and we would have
to fix this parser. But given that there aren't other modifiers now, I
think we can just push that problem off until it happens (if ever).

-Peff

```

## Michael J Gruber, 2010-05-13 12:31

Subject: [PATCH] for-each-ref: Field with abbreviated objectname
Message-ID: <dc7a785c8051aa1f742b75c03ddd1fdba57272db.1273753841.git.git@drmicha.warpmail.net>
URL: https://gitlist.dev/e/dc7a785c8051aa1f742b75c03ddd1fdba57272db.1273753841.git.git%40drmicha.warpmail.net
In-Reply-To: <20100504110134.GA7947@coredump.intra.peff.net>

```
Introduce a :short modifier to objectname which outputs the abbreviated
object name.

Signed-off-by: Michael J Gruber <git@drmicha.warpmail.net>
---
 Documentation/git-for-each-ref.txt |    1 +
 builtin/for-each-ref.c             |    3 +++
 t/t6300-for-each-ref.sh            |    9 +++++++++
 3 files changed, 13 insertions(+), 0 deletions(-)

diff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt
index 7e83288..390d85c 100644
--- a/Documentation/git-for-each-ref.txt
+++ b/Documentation/git-for-each-ref.txt
@@ -86,6 +86,7 @@ objectsize::
 
 objectname::
 	The object name (aka SHA-1).
+	For a non-ambiguous abbreviation of the object name append `:short`.
 
 upstream::
 	The name of a local ref which can be considered ``upstream''
diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
index 62be1bb..3a97953 100644
--- a/builtin/for-each-ref.c
+++ b/builtin/for-each-ref.c
@@ -227,6 +227,9 @@ static void grab_common_values(struct atom_value *val, int deref, struct object
 			strcpy(s, sha1_to_hex(obj->sha1));
 			v->s = s;
 		}
+		else if (!strcmp(name, "objectname:short")) {
+			v->s = find_unique_abbrev(obj->sha1, DEFAULT_ABBREV);
+		}
 	}
 }
 
diff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh
index 8052c86..7dc8a51 100755
--- a/t/t6300-for-each-ref.sh
+++ b/t/t6300-for-each-ref.sh
@@ -295,6 +295,15 @@ test_expect_success 'Check short upstream format' '
 	test_cmp expected actual
 '
 
+cat >expected <<EOF
+67a36f1
+EOF
+
+test_expect_success 'Check short objectname format' '
+	git for-each-ref --format="%(objectname:short)" refs/heads >actual &&
+	test_cmp expected actual
+'
+
 test_expect_success 'Check for invalid refname format' '
 	test_must_fail git for-each-ref --format="%(refname:INVALID)"
 '
-- 
1.7.1.240.geeaa4d

```

## Jeff King, 2010-05-14 05:12

Subject: Re: [PATCH] for-each-ref: Field with abbreviated objectname
Message-ID: <20100514051257.GG6075@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20100514051257.GG6075%40coredump.intra.peff.net
In-Reply-To: <dc7a785c8051aa1f742b75c03ddd1fdba57272db.1273753841.git.git@drmicha.warpmail.net>

```
On Thu, May 13, 2010 at 02:31:46PM +0200, Michael J Gruber wrote:

> Introduce a :short modifier to objectname which outputs the abbreviated
> object name.

Looks fine to me.

-Peff

```
