Skip to content

Commit c334eff

Browse files
committed
Merge branch 'jc/diff-unique-abbrev-comments'
A bit more comments in a tricky code. * jc/diff-unique-abbrev-comments: diff_unique_abbrev(): document its assumption and limitation
2 parents 6598894 + d709f1f commit c334eff

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

diff.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4136,7 +4136,8 @@ void diff_free_filepair(struct diff_filepair *p)
41364136
free(p);
41374137
}
41384138

4139-
/* This is different from find_unique_abbrev() in that
4139+
/*
4140+
* This is different from find_unique_abbrev() in that
41404141
* it stuffs the result with dots for alignment.
41414142
*/
41424143
const char *diff_unique_abbrev(const unsigned char *sha1, int len)
@@ -4148,6 +4149,26 @@ const char *diff_unique_abbrev(const unsigned char *sha1, int len)
41484149

41494150
abbrev = find_unique_abbrev(sha1, len);
41504151
abblen = strlen(abbrev);
4152+
4153+
/*
4154+
* In well-behaved cases, where the abbbreviated result is the
4155+
* same as the requested length, append three dots after the
4156+
* abbreviation (hence the whole logic is limited to the case
4157+
* where abblen < 37); when the actual abbreviated result is a
4158+
* bit longer than the requested length, we reduce the number
4159+
* of dots so that they match the well-behaved ones. However,
4160+
* if the actual abbreviation is longer than the requested
4161+
* length by more than three, we give up on aligning, and add
4162+
* three dots anyway, to indicate that the output is not the
4163+
* full object name. Yes, this may be suboptimal, but this
4164+
* appears only in "diff --raw --abbrev" output and it is not
4165+
* worth the effort to change it now. Note that this would
4166+
* likely to work fine when the automatic sizing of default
4167+
* abbreviation length is used--we would be fed -1 in "len" in
4168+
* that case, and will end up always appending three-dots, but
4169+
* the automatic sizing is supposed to give abblen that ensures
4170+
* uniqueness across all objects (statistically speaking).
4171+
*/
41514172
if (abblen < 37) {
41524173
static char hex[41];
41534174
if (len < abblen && abblen <= len + 2)

0 commit comments

Comments
 (0)