Skip to content

Commit b700086

Browse files
committed
Merge branch 'jc/maint-blame-unique-abbrev' into maint
"git blame" did not try to make sure that the abbreviated commit object names in its output are unique. * jc/maint-blame-unique-abbrev: blame: compute abbreviation width that ensures uniqueness
2 parents 2e1e8ef + b31272f commit b700086

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

builtin/blame.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,6 +1837,16 @@ static int read_ancestry(const char *graft_file)
18371837
return 0;
18381838
}
18391839

1840+
static int update_auto_abbrev(int auto_abbrev, struct origin *suspect)
1841+
{
1842+
const char *uniq = find_unique_abbrev(suspect->commit->object.sha1,
1843+
auto_abbrev);
1844+
int len = strlen(uniq);
1845+
if (auto_abbrev < len)
1846+
return len;
1847+
return auto_abbrev;
1848+
}
1849+
18401850
/*
18411851
* How many columns do we need to show line numbers, authors,
18421852
* and filenames?
@@ -1847,12 +1857,16 @@ static void find_alignment(struct scoreboard *sb, int *option)
18471857
int longest_dst_lines = 0;
18481858
unsigned largest_score = 0;
18491859
struct blame_entry *e;
1860+
int compute_auto_abbrev = (abbrev < 0);
1861+
int auto_abbrev = default_abbrev;
18501862

18511863
for (e = sb->ent; e; e = e->next) {
18521864
struct origin *suspect = e->suspect;
18531865
struct commit_info ci;
18541866
int num;
18551867

1868+
if (compute_auto_abbrev)
1869+
auto_abbrev = update_auto_abbrev(auto_abbrev, suspect);
18561870
if (strcmp(suspect->path, sb->path))
18571871
*option |= OUTPUT_SHOW_NAME;
18581872
num = strlen(suspect->path);
@@ -1880,6 +1894,10 @@ static void find_alignment(struct scoreboard *sb, int *option)
18801894
max_orig_digits = decimal_width(longest_src_lines);
18811895
max_digits = decimal_width(longest_dst_lines);
18821896
max_score_digits = decimal_width(largest_score);
1897+
1898+
if (compute_auto_abbrev)
1899+
/* one more abbrev length is needed for the boundary commit */
1900+
abbrev = auto_abbrev + 1;
18831901
}
18841902

18851903
/*
@@ -2353,10 +2371,9 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
23532371
parse_done:
23542372
argc = parse_options_end(&ctx);
23552373

2356-
if (abbrev == -1)
2357-
abbrev = default_abbrev;
2358-
/* one more abbrev length is needed for the boundary commit */
2359-
abbrev++;
2374+
if (0 < abbrev)
2375+
/* one more abbrev length is needed for the boundary commit */
2376+
abbrev++;
23602377

23612378
if (revs_file && read_ancestry(revs_file))
23622379
die_errno("reading graft file '%s' failed", revs_file);

0 commit comments

Comments
 (0)