Skip to content

Commit 297a1aa

Browse files
author
Junio C Hamano
committed
find_unique_abbrev() simplification.
Earlier it did not grok the 0{40} SHA1 very well, but what it needed to do was to find the shortest 0{N} that is not used as a valid object name to be consistent with the way names of valid objects are abbreviated. This makes some users simpler. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0a79807 commit 297a1aa

File tree

3 files changed

+10
-25
lines changed

3 files changed

+10
-25
lines changed

combine-diff.c

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -716,10 +716,7 @@ static int show_patch_diff(struct combine_diff_path *elem, int num_parent,
716716

717717
if (show_hunks || mode_differs) {
718718
const char *abb;
719-
char null_abb[DEFAULT_ABBREV + 1];
720719

721-
memset(null_abb, '0', DEFAULT_ABBREV);
722-
null_abb[DEFAULT_ABBREV] = 0;
723720
if (header) {
724721
shown_header++;
725722
puts(header);
@@ -734,17 +731,11 @@ static int show_patch_diff(struct combine_diff_path *elem, int num_parent,
734731
for (i = 0; i < num_parent; i++) {
735732
if (elem->parent[i].mode != elem->mode)
736733
mode_differs = 1;
737-
if (memcmp(elem->parent[i].sha1, null_sha1, 20))
738-
abb = find_unique_abbrev(elem->parent[i].sha1,
739-
DEFAULT_ABBREV);
740-
else
741-
abb = null_abb;
734+
abb = find_unique_abbrev(elem->parent[i].sha1,
735+
DEFAULT_ABBREV);
742736
printf("%s%s", i ? "," : "", abb);
743737
}
744-
if (memcmp(elem->sha1, null_sha1, 20))
745-
abb = find_unique_abbrev(elem->sha1, DEFAULT_ABBREV);
746-
else
747-
abb = null_abb;
738+
abb = find_unique_abbrev(elem->sha1, DEFAULT_ABBREV);
748739
printf("..%s\n", abb);
749740

750741
if (mode_differs) {

diff.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,7 @@ void diff_free_filepair(struct diff_filepair *p)
963963
}
964964

965965
/* This is different from find_unique_abbrev() in that
966-
* it needs to deal with 0{40} SHA1.
966+
* it stuffs the result with dots for alignment.
967967
*/
968968
const char *diff_unique_abbrev(const unsigned char *sha1, int len)
969969
{
@@ -973,16 +973,8 @@ const char *diff_unique_abbrev(const unsigned char *sha1, int len)
973973
return sha1_to_hex(sha1);
974974

975975
abbrev = find_unique_abbrev(sha1, len);
976-
if (!abbrev) {
977-
if (!memcmp(sha1, null_sha1, 20)) {
978-
char *buf = sha1_to_hex(null_sha1);
979-
if (len < 37)
980-
strcpy(buf + len, "...");
981-
return buf;
982-
}
983-
else
984-
return sha1_to_hex(sha1);
985-
}
976+
if (!abbrev)
977+
return sha1_to_hex(sha1);
986978
abblen = strlen(abbrev);
987979
if (abblen < 37) {
988980
static char hex[41];

sha1_name.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,16 +186,18 @@ static int get_short_sha1(const char *name, int len, unsigned char *sha1,
186186

187187
const char *find_unique_abbrev(const unsigned char *sha1, int len)
188188
{
189-
int status;
189+
int status, is_null;
190190
static char hex[41];
191191

192+
is_null = !memcmp(sha1, null_sha1, 20);
192193
memcpy(hex, sha1_to_hex(sha1), 40);
193194
if (len == 40)
194195
return hex;
195196
while (len < 40) {
196197
unsigned char sha1_ret[20];
197198
status = get_short_sha1(hex, len, sha1_ret, 1);
198-
if (!status) {
199+
if (!status ||
200+
(is_null && status != SHORT_NAME_AMBIGUOUS)) {
199201
hex[len] = 0;
200202
return hex;
201203
}

0 commit comments

Comments
 (0)