Skip to content

Commit d6cece5

Browse files
peffgitster
authored andcommitted
diff_aligned_abbrev: use "struct oid"
Since we're modifying this function anyway, it's a good time to update it to the more modern "struct oid". We can also drop some of the magic numbers in favor of GIT_SHA1_HEXSZ, along with some descriptive comments. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d5e3b01 commit d6cece5

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

combine-diff.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,9 +1203,9 @@ static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct re
12031203

12041204
/* Show sha1's */
12051205
for (i = 0; i < num_parent; i++)
1206-
printf(" %s", diff_aligned_abbrev(p->parent[i].oid.hash,
1206+
printf(" %s", diff_aligned_abbrev(&p->parent[i].oid,
12071207
opt->abbrev));
1208-
printf(" %s ", diff_aligned_abbrev(p->oid.hash, opt->abbrev));
1208+
printf(" %s ", diff_aligned_abbrev(&p->oid, opt->abbrev));
12091209
}
12101210

12111211
if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {

diff.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4157,14 +4157,15 @@ void diff_free_filepair(struct diff_filepair *p)
41574157
free(p);
41584158
}
41594159

4160-
const char *diff_aligned_abbrev(const unsigned char *sha1, int len)
4160+
const char *diff_aligned_abbrev(const struct object_id *oid, int len)
41614161
{
41624162
int abblen;
41634163
const char *abbrev;
4164-
if (len == 40)
4165-
return sha1_to_hex(sha1);
41664164

4167-
abbrev = find_unique_abbrev(sha1, len);
4165+
if (len == GIT_SHA1_HEXSZ)
4166+
return oid_to_hex(oid);
4167+
4168+
abbrev = find_unique_abbrev(oid->hash, len);
41684169
abblen = strlen(abbrev);
41694170

41704171
/*
@@ -4186,15 +4187,16 @@ const char *diff_aligned_abbrev(const unsigned char *sha1, int len)
41864187
* the automatic sizing is supposed to give abblen that ensures
41874188
* uniqueness across all objects (statistically speaking).
41884189
*/
4189-
if (abblen < 37) {
4190-
static char hex[41];
4190+
if (abblen < GIT_SHA1_HEXSZ - 3) {
4191+
static char hex[GIT_SHA1_HEXSZ + 1];
41914192
if (len < abblen && abblen <= len + 2)
41924193
xsnprintf(hex, sizeof(hex), "%s%.*s", abbrev, len+3-abblen, "..");
41934194
else
41944195
xsnprintf(hex, sizeof(hex), "%s...", abbrev);
41954196
return hex;
41964197
}
4197-
return sha1_to_hex(sha1);
4198+
4199+
return oid_to_hex(oid);
41984200
}
41994201

42004202
static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt)
@@ -4205,9 +4207,9 @@ static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt)
42054207
fprintf(opt->file, "%s", diff_line_prefix(opt));
42064208
if (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) {
42074209
fprintf(opt->file, ":%06o %06o %s ", p->one->mode, p->two->mode,
4208-
diff_aligned_abbrev(p->one->oid.hash, opt->abbrev));
4210+
diff_aligned_abbrev(&p->one->oid, opt->abbrev));
42094211
fprintf(opt->file, "%s ",
4210-
diff_aligned_abbrev(p->two->oid.hash, opt->abbrev));
4212+
diff_aligned_abbrev(&p->two->oid, opt->abbrev));
42114213
}
42124214
if (p->score) {
42134215
fprintf(opt->file, "%c%03d%c", p->status, similarity_index(p),

diff.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ extern void diff_warn_rename_limit(const char *varname, int needed, int degraded
344344
* This is different from find_unique_abbrev() in that
345345
* it stuffs the result with dots for alignment.
346346
*/
347-
extern const char *diff_aligned_abbrev(const unsigned char *sha1, int);
347+
extern const char *diff_aligned_abbrev(const struct object_id *sha1, int);
348348

349349
/* do not report anything on removed paths */
350350
#define DIFF_SILENT_ON_REMOVED 01

0 commit comments

Comments
 (0)