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>
Junio C Hamano committed
Feb 10, 2006 at 01:51 UTC
297a1aadbe22c978dca60d6512e5c851ebc6688c
3 files changed
+10
-25
combine-diff.c
+3
-12
@@ -716,10 +716,7 @@ static int show_patch_diff(struct combine_diff_path *elem, int num_parent,
716
717
if (show_hunks || mode_differs) {
718
const char *abb;
719
- char null_abb[DEFAULT_ABBREV + 1];
719
721
- memset(null_abb, '0', DEFAULT_ABBREV);
722
- null_abb[DEFAULT_ABBREV] = 0;
720
if (header) {
721
shown_header++;
722
puts(header);
@@ -734,17 +731,11 @@ static int show_patch_diff(struct combine_diff_path *elem, int num_parent,
731
for (i = 0; i < num_parent; i++) {
732
if (elem->parent[i].mode != elem->mode)
733
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);
736
printf("%s%s", i ? "," : "", abb);
737
}
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);
739
printf("..%s\n", abb);
740
741
if (mode_differs) {
diff.c
+3
-11
@@ -963,7 +963,7 @@ void diff_free_filepair(struct diff_filepair *p)
963
}
964
965
/* 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.
967
*/
968
const char *diff_unique_abbrev(const unsigned char *sha1, int len)
969
{
@@ -973,16 +973,8 @@ const char *diff_unique_abbrev(const unsigned char *sha1, int len)
973
return sha1_to_hex(sha1);
974
975
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);
978
abblen = strlen(abbrev);
979
if (abblen < 37) {
980
static char hex[41];
sha1_name.c
+4
-2
@@ -186,16 +186,18 @@ static int get_short_sha1(const char *name, int len, unsigned char *sha1,
186
187
const char *find_unique_abbrev(const unsigned char *sha1, int len)
188
{
189
- int status;
189
+ int status, is_null;
190
static char hex[41];
191
192
+ is_null = !memcmp(sha1, null_sha1, 20);
193
memcpy(hex, sha1_to_hex(sha1), 40);
194
if (len == 40)
195
return hex;
196
while (len < 40) {
197
unsigned char sha1_ret[20];
198
status = get_short_sha1(hex, len, sha1_ret, 1);
198
- if (!status) {
199
+ if (!status ||
200
+ (is_null && status != SHORT_NAME_AMBIGUOUS)) {
201
hex[len] = 0;
202
return hex;
203
}