Redefine semantics of find_unique_abbrev()
The function returned NULL when no object that matches the name was found, but that made the callers more complicated, as nobody used that NULL return as an indication that no object with such a name exists. They (at least the careful ones) instead took the full 40-hexdigit and used in such a case, and the careless ones segfaulted.
With this "git rev-parse --short 5555555555555555555555555555555555555555" would stop segfaulting.
--- builtin-reset.c | 4 ---- builtin-send-pack.c | 4 +--- diff.c | 2 -- sha1_name.c | 8 +++----- 4 files changed, 4 insertions(+), 14 deletions(-)
builtin-reset.c +0 -4
@@ -66,10 +66,6 @@ static void print_new_head_line(struct commit *commit)const char *hex, *dots ="..." , *body; hex =find_unique_abbrev (commit->object.sha1,DEFAULT_ABBREV ); -if (!hex) { - hex =sha1_to_hex (commit->object.sha1); - dots ="" ; - }printf ("HEAD is now at %s%s" , hex, dots); body =strstr (commit->buffer,"\n\n" );if (body) {
builtin-send-pack.c +1 -3
@@ -264,9 +264,7 @@ static void print_ref_status(char flag, const char *summary, struct ref *to, strstatic const char *status_abbrev (unsigned char sha1[20 ]) { -const char *abbrev; - abbrev =find_unique_abbrev (sha1,DEFAULT_ABBREV ); -return abbrev ? abbrev :sha1_to_hex (sha1); +return find_unique_abbrev (sha1,DEFAULT_ABBREV ); }static void print_ok_ref_status (struct ref *ref)
diff.c +0 -2
@@ -2581,8 +2581,6 @@ const char *diff_unique_abbrev(const unsigned char *sha1, int len)return sha1_to_hex (sha1); abbrev =find_unique_abbrev (sha1, len); -if (!abbrev) -return sha1_to_hex (sha1); abblen =strlen (abbrev);if (abblen <37 ) {static char hex[41 ];
sha1_name.c +3 -5
@@ -202,16 +202,14 @@ const char *find_unique_abbrev(const unsigned char *sha1, int len)while (len <40 ) {unsigned char sha1_ret[20 ]; status =get_short_sha1 (hex, len, sha1_ret,1 ); -if (!status || - (is_null && status !=SHORT_NAME_AMBIGUOUS )) { +if ((!is_null && !status) || + (is_null && status ==SHORT_NAME_NOT_FOUND )) { hex[len] =0 ;return hex; } -if (status !=SHORT_NAME_AMBIGUOUS ) -return NULL ; len++; } -return NULL ; +return hex; }static int ambiguous_path (const char *path,int len)
Quoted 9 lines · Junio C Hamano
The function returned NULL when no object that matches the name was found, but that made the callers more complicated, as nobody used that NULL return as an indication that no object with such a name exists. They (at least the careful ones) instead took the full 40-hexdigit and used in such a case, and the careless ones segfaulted.
With this "git rev-parse --short 5555555555555555555555555555555555555555" would stop segfaulting.
I have been meaning to clean up and submit a similar patch from the 1.5.4 freeze period. However, your patch will always print the 40-hexdigit version, which looks quite ugly in status output. Instead, we can do much better by finding the longest subsequence we _do_ know about, and adding one digit to it.
So you get:
# this is the current master, abbreviated $ git rev-parse --short 97b97c58e609a1cd23b3c2514f41cdb0405870ee 97b97c5
# this is an object we don't have, but similar to that $ git rev-parse --short 97b97c58e609a1cd23b3c2514f41cdb0405870ff 97b97c58e609a1cd23b3c2514f41cdb0405870f
# and less similar means we can abbreviate more $ git rev-parse --short 97b97c58e609a1cd23b3c2514fffffffffffffff 97b97c58e609a1cd23b3c2514ff
Implementation is below:
Quoted 10 lines
sha1_name.c +2
-2@@ -202,16 +202,14 @@ const char *find_unique_abbrev(const unsigned char *sha1, int len)while (len <40 ) {unsigned char sha1_ret[20 ]; status =get_short_sha1 (hex, len, sha1_ret,1 ); -if (!status || - (is_null && status !=SHORT_NAME_AMBIGUOUS )) { +if ((!is_null && !status) || + (is_null && status ==SHORT_NAME_NOT_FOUND )) {
sha1_name.c const char *find_unique_abbrev(const unsigned char *sha1, int len) · line 213
Quoted 10 lines
sha1_name.c +1
-3hex[len] =0 ;return hex; } -if (status !=SHORT_NAME_AMBIGUOUS ) -return NULL ; len++; } -return NULL ; +return hex; }
All we have to do is treat the "missing" case like we treat "is_null" (and in fact, is_null is just a specialized case of something we don't have). So replace this hunk with:
sha1_name.c +4 -4
@@ -192,18 +192,18 @@ static int get_short_sha1(const char *name, int len, unsigned char *sha1,const char *find_unique_abbrev (const unsigned char *sha1,int len) { -int status, is_null; +int status, missing;static char hex[41 ]; - is_null =is_null_sha1 (sha1); + missing = !has_sha1_file (sha1);memcpy (hex,sha1_to_hex (sha1),40 );if (len ==40 || !len)return hex;while (len <40 ) {unsigned char sha1_ret[20 ]; status =get_short_sha1 (hex, len, sha1_ret,1 ); -if ((!is_null && !status) || - (is_null && status ==SHORT_NAME_NOT_FOUND )) { +if ((!missing && !status) || + (missing && status ==SHORT_NAME_NOT_FOUND )) { hex[len] =0 ;return hex; }
-Peff
Quoted 5 lines · Jeff King
I have been meaning to clean up and submit a similar patch from the 1.5.4 freeze period. However, your patch will always print the 40-hexdigit version, which looks quite ugly in status output. Instead, we can do much better by finding the longest subsequence we _do_ know about, and adding one digit to it.
Here is what I ended up with. Instead of saying "missing", I said "exists", which I think makes the logic easier to read, at least to me.
That is, "for objects we have, make sure it uniquely identifies, otherwise, make sure the phoney name is long enough such that it would not name any existing object".
---
sha1_name.c | 13 ++++++------- 1 files changed, 6 insertions(+), 7 deletions(-)
sha1_name.c +6 -7
@@ -192,26 +192,25 @@ static int get_short_sha1(const char *name, int len, unsigned char *sha1,const char *find_unique_abbrev (const unsigned char *sha1,int len) { -int status, is_null; +int status, exists;static char hex[41 ]; - is_null =is_null_sha1 (sha1); + exists =has_sha1_file (sha1);memcpy (hex,sha1_to_hex (sha1),40 );if (len ==40 || !len)return hex;while (len <40 ) {unsigned char sha1_ret[20 ]; status =get_short_sha1 (hex, len, sha1_ret,1 ); -if (!status || - (is_null && status !=SHORT_NAME_AMBIGUOUS )) { +if (exists + ? !status + : status ==SHORT_NAME_NOT_FOUND ) { hex[len] =0 ;return hex; } -if (status !=SHORT_NAME_AMBIGUOUS ) -return NULL ; len++; } -return NULL ; +return hex; }static int ambiguous_path (const char *path,int len)
Quoted 3 lines · Junio C Hamano
That is, "for objects we have, make sure it uniquely identifies, otherwise, make sure the phoney name is long enough such that it would not name any existing object".
I think your logic is correct, and I think naming it 'exists' is more readable (I don't have a tendency not to double-negate).
But...
sha1_name.c static int get_short_sha1(const char *name, int len, unsigned char *sha1, · line 210
Quoted 8 lines
sha1_name.c +3
-2-if (!status || - (is_null && status !=SHORT_NAME_AMBIGUOUS )) { +if (exists + ? !status + : status ==SHORT_NAME_NOT_FOUND ) { hex[len] =0 ;return hex; }
Maybe it is just me, but I find the ternary operator here reduces readability. I would have liked the more verbose:
if ((exists && !status) ||
(!exists && status == SHORT_NAME_NOT_FOUND)) {But now I am just painting your bikeshed.
-Peff
Quoted 23 lines · Jeff King
Quoted 3 lines · Junio C Hamano
That is, "for objects we have, make sure it uniquely identifies, otherwise, make sure the phoney name is long enough such that it would not name any existing object".
I think your logic is correct, and I think naming it 'exists' is more readable (I don't have a tendency not to double-negate).
But...
sha1_name.c static int get_short_sha1(const char *name, int len, unsigned char *sha1, · line 210
Quoted 8 lines
sha1_name.c +3
-2-if (!status || - (is_null && status !=SHORT_NAME_AMBIGUOUS )) { +if (exists + ? !status + : status ==SHORT_NAME_NOT_FOUND ) { hex[len] =0 ;return hex; }Maybe it is just me, but I find the ternary operator here reduces readability. I would have liked the more verbose:
Quoted 4 lines
if ((exists && !status) || (!exists && status == SHORT_NAME_NOT_FOUND)) {But now I am just painting your bikeshed.
Heh, the ternary is a mini "if-then-else" by itself. Turn your head sideways (just like you do when you meet a smiley) and the parse tree will jump at you ;-).
PATCH 1/1 Clean up find_unique_abbrev() callers
In reply to Jeff KingNow find_unique_abbrev() never returns NULL, there is no need for callers to prepare for seeing NULL and fall back to giving the full 40 hexdigits.
While we are at it, drop "..." in the "git reset" output that reports the location of the new HEAD, between the abbreviated commit object name and the one line commit summary. Because we are always showing the HEAD (which cannot be missing!), we never had a case where we show the full 40 hexdigits that is not followed by three dots, and these three dots were stealing 3 columns from the precious horizontal screen real estate out of 80 that can better be used for the one line commit summary.
---
builtin-reset.c | 8 ++------ builtin-send-pack.c | 4 +--- diff.c | 2 -- 3 files changed, 3 insertions(+), 11 deletions(-)
builtin-reset.c +2 -6
@@ -63,14 +63,10 @@ static int reset_index_file(const unsigned char *sha1, int is_hard_reset)static void print_new_head_line (struct commit *commit) { -const char *hex, *dots ="..." , *body; +const char *hex, *body; hex =find_unique_abbrev (commit->object.sha1,DEFAULT_ABBREV ); -if (!hex) { - hex =sha1_to_hex (commit->object.sha1); - dots ="" ; - } -printf ("HEAD is now at %s%s" , hex, dots); +printf ("HEAD is now at %s" , hex); body =strstr (commit->buffer,"\n\n" );if (body) {const char *eol;
builtin-send-pack.c +1 -3
@@ -264,9 +264,7 @@ static void print_ref_status(char flag, const char *summary, struct ref *to, strstatic const char *status_abbrev (unsigned char sha1[20 ]) { -const char *abbrev; - abbrev =find_unique_abbrev (sha1,DEFAULT_ABBREV ); -return abbrev ? abbrev :sha1_to_hex (sha1); +return find_unique_abbrev (sha1,DEFAULT_ABBREV ); }static void print_ok_ref_status (struct ref *ref)
diff.c +0 -2
@@ -2581,8 +2581,6 @@ const char *diff_unique_abbrev(const unsigned char *sha1, int len)return sha1_to_hex (sha1); abbrev =find_unique_abbrev (sha1, len); -if (!abbrev) -return sha1_to_hex (sha1); abblen =strlen (abbrev);if (abblen <37 ) {static char hex[41 ];
Re: [PATCH] Clean up find_unique_abbrev() callers
Quoted 1 line · Junio C Hamano
3 files changed, 3 insertions(+), 11 deletions(-)
I like it. :)
-Peff