Skip to content

Commit 88a8b79

Browse files
Fredrik KuivinenJunio C Hamano
authored andcommitted
blame: Nicer output
As pointed out by Junio, it may be dangerous to cut off people's names after 15 bytes. If the name is encoded in an encoding which uses more than one byte per code point we may end up with outputting garbage. Instead of trying to do something smart, just output the entire name. We don't gain much screen space by chopping it off anyway. Furthermore, only output the file name if we actually found any renames. Signed-off-by: Fredrik Kuivinen <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6169858 commit 88a8b79

1 file changed

Lines changed: 30 additions & 5 deletions

File tree

blame.c

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,8 @@ int main(int argc, const char **argv)
742742
struct commit_info ci;
743743
const char *buf;
744744
int max_digits;
745+
size_t longest_file, longest_author;
746+
int found_rename;
745747

746748
const char* prefix = setup_git_directory();
747749

@@ -818,6 +820,25 @@ int main(int argc, const char **argv)
818820
for (max_digits = 1, i = 10; i <= num_blame_lines + 1; max_digits++)
819821
i *= 10;
820822

823+
longest_file = 0;
824+
longest_author = 0;
825+
found_rename = 0;
826+
for (i = 0; i < num_blame_lines; i++) {
827+
struct commit *c = blame_lines[i];
828+
struct util_info* u;
829+
if (!c)
830+
c = initial;
831+
u = c->object.util;
832+
833+
if (!found_rename && strcmp(filename, u->pathname))
834+
found_rename = 1;
835+
if (longest_file < strlen(u->pathname))
836+
longest_file = strlen(u->pathname);
837+
get_commit_info(c, &ci);
838+
if (longest_author < strlen(ci.author))
839+
longest_author = strlen(ci.author);
840+
}
841+
821842
for (i = 0; i < num_blame_lines; i++) {
822843
struct commit *c = blame_lines[i];
823844
struct util_info* u;
@@ -828,14 +849,18 @@ int main(int argc, const char **argv)
828849
u = c->object.util;
829850
get_commit_info(c, &ci);
830851
fwrite(sha1_to_hex(c->object.sha1), sha1_len, 1, stdout);
831-
if(compability)
852+
if(compability) {
832853
printf("\t(%10s\t%10s\t%d)", ci.author,
833854
format_time(ci.author_time, ci.author_tz), i+1);
834-
else
835-
printf(" %s (%-15.15s %10s %*d) ", u->pathname,
836-
ci.author, format_time(ci.author_time,
837-
ci.author_tz),
855+
} else {
856+
if (found_rename)
857+
printf(" %-*.*s", longest_file, longest_file,
858+
u->pathname);
859+
printf(" (%-*.*s %10s %*d) ",
860+
longest_author, longest_author, ci.author,
861+
format_time(ci.author_time, ci.author_tz),
838862
max_digits, i+1);
863+
}
839864

840865
if(i == num_blame_lines - 1) {
841866
fwrite(buf, blame_len - (buf - blame_contents),

0 commit comments

Comments
 (0)