Browse Source

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 <junkio@cox.net>
maint
Junio C Hamano 19 years ago
parent
commit
297a1aadbe
  1. 9
      combine-diff.c
  2. 12
      diff.c
  3. 6
      sha1_name.c

9
combine-diff.c

@ -716,10 +716,7 @@ static int show_patch_diff(struct combine_diff_path *elem, int num_parent, @@ -716,10 +716,7 @@ static int show_patch_diff(struct combine_diff_path *elem, int num_parent,

if (show_hunks || mode_differs) {
const char *abb;
char null_abb[DEFAULT_ABBREV + 1];

memset(null_abb, '0', DEFAULT_ABBREV);
null_abb[DEFAULT_ABBREV] = 0;
if (header) {
shown_header++;
puts(header);
@ -734,17 +731,11 @@ static int show_patch_diff(struct combine_diff_path *elem, int num_parent, @@ -734,17 +731,11 @@ static int show_patch_diff(struct combine_diff_path *elem, int num_parent,
for (i = 0; i < num_parent; i++) {
if (elem->parent[i].mode != elem->mode)
mode_differs = 1;
if (memcmp(elem->parent[i].sha1, null_sha1, 20))
abb = find_unique_abbrev(elem->parent[i].sha1,
DEFAULT_ABBREV);
else
abb = null_abb;
printf("%s%s", i ? "," : "", abb);
}
if (memcmp(elem->sha1, null_sha1, 20))
abb = find_unique_abbrev(elem->sha1, DEFAULT_ABBREV);
else
abb = null_abb;
printf("..%s\n", abb);

if (mode_differs) {

12
diff.c

@ -963,7 +963,7 @@ void diff_free_filepair(struct diff_filepair *p) @@ -963,7 +963,7 @@ void diff_free_filepair(struct diff_filepair *p)
}

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

abbrev = find_unique_abbrev(sha1, len);
if (!abbrev) {
if (!memcmp(sha1, null_sha1, 20)) {
char *buf = sha1_to_hex(null_sha1);
if (len < 37)
strcpy(buf + len, "...");
return buf;
}
else
if (!abbrev)
return sha1_to_hex(sha1);
}
abblen = strlen(abbrev);
if (abblen < 37) {
static char hex[41];

6
sha1_name.c

@ -186,16 +186,18 @@ static int get_short_sha1(const char *name, int len, unsigned char *sha1, @@ -186,16 +186,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;
int status, is_null;
static char hex[41];

is_null = !memcmp(sha1, null_sha1, 20);
memcpy(hex, sha1_to_hex(sha1), 40);
if (len == 40)
return hex;
while (len < 40) {
unsigned char sha1_ret[20];
status = get_short_sha1(hex, len, sha1_ret, 1);
if (!status) {
if (!status ||
(is_null && status != SHORT_NAME_AMBIGUOUS)) {
hex[len] = 0;
return hex;
}

Loading…
Cancel
Save