# [PATCH 0/2] Slightly prettier reflog message from checkout

10 messages from 2013-06-15 to 2013-06-18. Participants: Ramkumar Ramachandra, Junio C Hamano, Phil Hord.
Thread: https://gitlist.dev/t/34151

## Ramkumar Ramachandra, 2013-06-15 17:38

Subject: [PATCH 0/2] Slightly prettier reflog message from checkout
Message-ID: <1371317906-3991-1-git-send-email-artagnon@gmail.com>
URL: https://gitlist.dev/e/1371317906-3991-1-git-send-email-artagnon%40gmail.com

```
[1/2] is important.  [2/2] is a minor prettification, that wouldn't
have been possible without [1/2].

Thanks.

Ramkumar Ramachandra (2):
  sha1_name: stop hard-coding 40-character hex checks
  checkout: do not write full sha1 to reflog

 builtin/checkout.c | 2 +-
 sha1_name.c        | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)

-- 
1.8.3.1.438.g96d34e8

```

## Ramkumar Ramachandra, 2013-06-15 17:38

Subject: [PATCH 1/2] sha1_name: stop hard-coding 40-character hex checks
Message-ID: <1371317906-3991-2-git-send-email-artagnon@gmail.com>
URL: https://gitlist.dev/e/1371317906-3991-2-git-send-email-artagnon%40gmail.com
In-Reply-To: <1371317906-3991-1-git-send-email-artagnon@gmail.com>

```
In two places, get_sha1_basic() assumes that strings are possibly sha1
hexes if they are 40 characters long, and calls get_sha1_hex() in these
two cases.  This 40-character check is ugly and wrong: there is nothing
preventing a revision or branch name from being exactly 40 characters.
Replace it with a call to the more robust get_short_sha1().

Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
---
 sha1_name.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/sha1_name.c b/sha1_name.c
index 90419ef..d862af3 100644
--- a/sha1_name.c
+++ b/sha1_name.c
@@ -451,7 +451,7 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
 	int refs_found = 0;
 	int at, reflog_len, nth_prior = 0;
 
-	if (len == 40 && !get_sha1_hex(str, sha1)) {
+	if (!get_short_sha1(str, strlen(str), sha1, GET_SHA1_QUIETLY)) {
 		refs_found = dwim_ref(str, len, tmp_sha1, &real_ref);
 		if (refs_found > 0 && warn_ambiguous_refs) {
 			warning(warn_msg, len, str);
@@ -492,9 +492,9 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
 		int detached;
 
 		if (interpret_nth_prior_checkout(str, &buf) > 0) {
-			detached = (buf.len == 40 && !get_sha1_hex(buf.buf, sha1));
+			detached = get_short_sha1(buf.buf, buf.len, sha1, GET_SHA1_QUIETLY);
 			strbuf_release(&buf);
-			if (detached)
+			if (detached != SHORT_NAME_NOT_FOUND)
 				return 0;
 		}
 	}
-- 
1.8.3.1.438.g96d34e8

```

## Ramkumar Ramachandra, 2013-06-15 17:38

Subject: [PATCH 2/2] checkout: do not write full sha1 to reflog
Message-ID: <1371317906-3991-3-git-send-email-artagnon@gmail.com>
URL: https://gitlist.dev/e/1371317906-3991-3-git-send-email-artagnon%40gmail.com
In-Reply-To: <1371317906-3991-1-git-send-email-artagnon@gmail.com>

```
A snippet from a typical git session may look like this:

  $ git checkout @~3
  ...
  $ git checkout -

The reflog entries corresponding to the two checkouts look like:

  f855138: checkout: moving from bdff0e3a374617dce784f801b97500d9ba2e4705 to co-reflog
  bdff0e3: checkout: moving from co-reflog to HEAD~3

There is no need to write the full SHA-1 to the user-visible reflog; use
find_unique_abbrev() to shorten the first line like:

  f855138: checkout: moving from bdff0e3 to co-reflog

Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
---
 builtin/checkout.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/builtin/checkout.c b/builtin/checkout.c
index f5b50e5..41b1929 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -619,7 +619,7 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
 
 	old_desc = old->name;
 	if (!old_desc && old->commit)
-		old_desc = sha1_to_hex(old->commit->object.sha1);
+		old_desc = find_unique_abbrev(old->commit->object.sha1, DEFAULT_ABBREV);
 	strbuf_addf(&msg, "checkout: moving from %s to %s",
 		    old_desc ? old_desc : "(invalid)", new->name);
 
-- 
1.8.3.1.438.g96d34e8

```

## Ramkumar Ramachandra, 2013-06-15 17:42

Subject: Re: [PATCH 0/2] Slightly prettier reflog message from checkout
Message-ID: <CALkWK0=s_oza=2mUmAFhLZCbFzW4ZsW+ohkxOthmu0+iGhKLSw@mail.gmail.com>
URL: https://gitlist.dev/e/CALkWK0%3Ds_oza%3D2mUmAFhLZCbFzW4ZsW%2BohkxOthmu0%2BiGhKLSw%40mail.gmail.com
In-Reply-To: <1371317906-3991-1-git-send-email-artagnon@gmail.com>

```
Ramkumar Ramachandra wrote:
> [1/2] is important.  [2/2] is a minor prettification, that wouldn't
> have been possible without [1/2].

I forgot to mention: some tests fail, and I'm investigating.  This is
an early preview.

```

## Junio C Hamano, 2013-06-16 01:24

Subject: Re: [PATCH 0/2] Slightly prettier reflog message from checkout
Message-ID: <7vmwqqkftv.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vmwqqkftv.fsf%40alter.siamese.dyndns.org
In-Reply-To: <1371317906-3991-1-git-send-email-artagnon@gmail.com>

```
Ramkumar Ramachandra <artagnon@gmail.com> writes:

> [1/2] is important.  [2/2] is a minor prettification, that wouldn't
> have been possible without [1/2].
>
> Thanks.
>
> Ramkumar Ramachandra (2):
>   sha1_name: stop hard-coding 40-character hex checks
>   checkout: do not write full sha1 to reflog
>
>  builtin/checkout.c | 2 +-
>  sha1_name.c        | 6 +++---
>  2 files changed, 4 insertions(+), 4 deletions(-)

I view the two codepaths touched by these patches the other way
around.

An abbreviated unique SHA-1 you have today may not be unique
tomorrow.  There is no reason to deliberately lose information
(e.g. by using "Then, instead of the absolute minimum, let's record
a bit more bytes" heuristics) when we record. The reflog recording
code in checkout writes full 40-characters on purpose and there is
no reason not to do so (i.e. the codepath that is the topic of 2/2).

That is a more important design decision between the two codepaths.

Once we accept that design principle of not losing information when
we do not have to, it naturally follows that the writing side should
write full 40-hex, and also the reading side (i.e. the codepath that
is the topic of 1/2) should make sure that it reads 40-hex and
nothing else.  This also reduces the risk of a funny branch name
that consists only of [0-9a-f] getting mistaken as an object name,
but that is not the primary point.

So I am fairly strongly negative on both changes.

```

## Ramkumar Ramachandra, 2013-06-16 09:21

Subject: Re: [PATCH 0/2] Slightly prettier reflog message from checkout
Message-ID: <CALkWK0ki9C9OL36b3j14C-mVMPy07Uj5FXcrfMZJs_g3zBRC9A@mail.gmail.com>
URL: https://gitlist.dev/e/CALkWK0ki9C9OL36b3j14C-mVMPy07Uj5FXcrfMZJs_g3zBRC9A%40mail.gmail.com
In-Reply-To: <7vmwqqkftv.fsf@alter.siamese.dyndns.org>

```
Junio C Hamano wrote:
> I view the two codepaths touched by these patches the other way
> around.

I see.  Thanks for the early feedback.  I have some doubts.

> An abbreviated unique SHA-1 you have today may not be unique
> tomorrow.  There is no reason to deliberately lose information
> (e.g. by using "Then, instead of the absolute minimum, let's record
> a bit more bytes" heuristics) when we record. The reflog recording
> code in checkout writes full 40-characters on purpose and there is
> no reason not to do so (i.e. the codepath that is the topic of 2/2).

When did we guarantee that the messages written by the reflog are invariant?

  $ git checkout @^
  $ git reflog | head -n 1
  b1d94f2 HEAD@{2 seconds ago}: checkout: moving from checkout-dash to HEAD^

What does HEAD^ even mean?  What guarantees that checkout-dash will
not be something else tomorrow?  If you want invariance, isn't that
what the first field is for (b1d94f2)?  As I understand it, the
messages are purely to convey end-user information about the
breadcrumb trail: they were later made semi-semantic (like the @{-N}
parser using them).

> Once we accept that design principle of not losing information when
> we do not have to, it naturally follows that the writing side should
> write full 40-hex, and also the reading side (i.e. the codepath that
> is the topic of 1/2) should make sure that it reads 40-hex and
> nothing else.  This also reduces the risk of a funny branch name
> that consists only of [0-9a-f] getting mistaken as an object name,
> but that is not the primary point.

As I already explained, I don't know what information loss you're
talking about.  And yes, I noticed advice.object_name_warning.

> So I am fairly strongly negative on both changes.

Okay, but please explain it for me.

```

## Phil Hord, 2013-06-16 13:28

Subject: Re: [PATCH 1/2] sha1_name: stop hard-coding 40-character hex checks
Message-ID: <CABURp0qs7uPkczmS8EC-iNgCVh2eTaeCKsh7NwVuS4j92jMn+A@mail.gmail.com>
URL: https://gitlist.dev/e/CABURp0qs7uPkczmS8EC-iNgCVh2eTaeCKsh7NwVuS4j92jMn%2BA%40mail.gmail.com
In-Reply-To: <1371317906-3991-2-git-send-email-artagnon@gmail.com>

```
On Sat, Jun 15, 2013 at 1:38 PM, Ramkumar Ramachandra
<artagnon@gmail.com> wrote:
> In two places, get_sha1_basic() assumes that strings are possibly sha1
> hexes if they are 40 characters long, and calls get_sha1_hex() in these
> two cases.  This 40-character check is ugly and wrong: there is nothing
> preventing a revision or branch name from being exactly 40 characters.
> Replace it with a call to the more robust get_short_sha1().

I share your disdain for the bare '40's in the code.  But I think this
code is less clear than the previous version with the magic number.

>
> Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
> ---
>  sha1_name.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/sha1_name.c b/sha1_name.c
> index 90419ef..d862af3 100644
> --- a/sha1_name.c
> +++ b/sha1_name.c
> @@ -451,7 +451,7 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
>         int refs_found = 0;
>         int at, reflog_len, nth_prior = 0;
>
> -       if (len == 40 && !get_sha1_hex(str, sha1)) {
> +       if (!get_short_sha1(str, strlen(str), sha1, GET_SHA1_QUIETLY)) {

Use len instead of strlen(str) here.  It's faster and more correct.

But also get_short_sha1 is much heavier than get_sha1_hex and does not
seem appropriate here.

>                 refs_found = dwim_ref(str, len, tmp_sha1, &real_ref);
>                 if (refs_found > 0 && warn_ambiguous_refs) {
>                         warning(warn_msg, len, str);
> @@ -492,9 +492,9 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
>                 int detached;
>
>                 if (interpret_nth_prior_checkout(str, &buf) > 0) {
> -                       detached = (buf.len == 40 && !get_sha1_hex(buf.buf, sha1));
> +                       detached = get_short_sha1(buf.buf, buf.len, sha1, GET_SHA1_QUIETLY);
>                         strbuf_release(&buf);
> -                       if (detached)
> +                       if (detached != SHORT_NAME_NOT_FOUND)

The semantic meaning of 'detached' seems less clear now if you have to
compare against an enumerated constant to determine the result.  But
also, I do not see why you have to test '!= SHORT_NAME_NOT_FOUND' here
but you did not have to in the other instance.

I think it would be improved if you did this comparison in the
assignment of detached so 'detached' could keep its original boolean
meaning.

But anyway, having looked inside get_short_sha1, it really does seem
to do much more than you want here.

>                                 return 0;
>                 }
>         }
> --
> 1.8.3.1.438.g96d34e8
>
> --
> To unsubscribe from this list: send the line "unsubscribe git" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

```

## Junio C Hamano, 2013-06-17 02:59

Subject: Re: [PATCH 0/2] Slightly prettier reflog message from checkout
Message-ID: <7vtxkxigrb.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vtxkxigrb.fsf%40alter.siamese.dyndns.org
In-Reply-To: <CALkWK0ki9C9OL36b3j14C-mVMPy07Uj5FXcrfMZJs_g3zBRC9A@mail.gmail.com>

```
Ramkumar Ramachandra <artagnon@gmail.com> writes:

> Junio C Hamano wrote:
>> I view the two codepaths touched by these patches the other way
>> around.
>
> I see.  Thanks for the early feedback.  I have some doubts.
>
>> An abbreviated unique SHA-1 you have today may not be unique
>> tomorrow.

> When did we guarantee that the messages written by the reflog are invariant?

That is not the point.  From the proposed log message for your 2/2:

  f855138: checkout: moving from bdff0e3a374617dce784f801b97500d9ba2e4705 to co-reflog
  f855138: checkout: moving from bdff0e3 to co-reflog

The "bdff0e3" may be a unique abbreviation to identify the commit
bdff0e3a374617dce784f801b97500d9ba2e4705 when the reflog entry was
written.  But the latter can become meaningless once you have an
unrelated commit in your history that shares the prefix.

That is the "deliberate loss of information" I saw in the proposal.
Recording full 40-hex does not have to worry about that issue; you
do not even have to argue "but in this case we do not even have to
have unique SHA-1, nobody uses it" vs. "some other codepaths you are
not aware of may want to take advantage and start using it".  In
other words, we will have one less thing we have to worry about.

```

## Ramkumar Ramachandra, 2013-06-18 07:03

Subject: Re: [PATCH 1/2] sha1_name: stop hard-coding 40-character hex checks
Message-ID: <CALkWK0=_Ac+dQV23xiXty3peWdhn2iFzgSPb7LYDFmoCzYhxQQ@mail.gmail.com>
URL: https://gitlist.dev/e/CALkWK0%3D_Ac%2BdQV23xiXty3peWdhn2iFzgSPb7LYDFmoCzYhxQQ%40mail.gmail.com
In-Reply-To: <CABURp0qs7uPkczmS8EC-iNgCVh2eTaeCKsh7NwVuS4j92jMn+A@mail.gmail.com>

```
Phil Hord wrote:
> I share your disdain for the bare '40's in the code.  But I think this
> code is less clear than the previous version with the magic number.

Please read the cover-letter: I was just toying around to see if this
was a good idea, and Junio points out that it's not.

```

## Junio C Hamano, 2013-06-18 14:27

Subject: Re: [PATCH 1/2] sha1_name: stop hard-coding 40-character hex checks
Message-ID: <7vehbza3yq.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vehbza3yq.fsf%40alter.siamese.dyndns.org
In-Reply-To: <CALkWK0=_Ac+dQV23xiXty3peWdhn2iFzgSPb7LYDFmoCzYhxQQ@mail.gmail.com>

```
Ramkumar Ramachandra <artagnon@gmail.com> writes:

> Phil Hord wrote:
>> I share your disdain for the bare '40's in the code.  But I think this
>> code is less clear than the previous version with the magic number.
>
> Please read the cover-letter: 

Which was...

>> [1/2] is important.  [2/2] is a minor prettification, that wouldn't
>> have been possible without [1/2].

...and I do not find how the above is connected with

> I was just toying around to see if this
> was a good idea,...

...this in any way.  Puzzled.

```
