{"thread":{"id":"5824","subject":"[PATCH 3/6] git-blame: --show-number (and -n)","startedAt":"2006-10-05T21:06:26Z","lastAt":"2006-10-05T21:06:26Z","messageCount":1,"participants":["Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":6},"messages":[{"id":"28250","messageId":"7vhcyicw19.fsf@assigned-by-dhcp.cox.net","threadId":"5824","inReplyTo":null,"subject":"[PATCH 3/6] git-blame: --show-number (and -n)","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2006-10-05T21:06:26Z","receivedAt":"2006-10-05T21:06:26Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"The new option makes the command's native output format show the\noriginal line number in the blamed revision.\n\nNote: the current implementation of find_orig_linenum involves\nlinear search through the line_map array every time.  It should\nprobably build a reverse map upfront and do a simple look-up to\nspeed things up, but I'll leave it to more clever and beautiful\npeople ;-).\n\nSigned-off-by: Junio C Hamano <junkio@cox.net>\n---\n blame.c |   42 ++++++++++++++++++++++++++++++++++++++----\n 1 files changed, 38 insertions(+), 4 deletions(-)\n\ndiff --git a/blame.c b/blame.c\nindex d830b29..bf4a1a1 100644\n--- a/blame.c\n+++ b/blame.c\n@@ -731,6 +731,25 @@ static int read_ancestry(const char *gra\n \treturn 0;\n }\n \n+static int lineno_width(int lines)\n+{\n+\tint i, width;\n+\n+\tfor (width = 1, i = 10; i <= lines + 1; width++)\n+\t\ti *= 10;\n+\treturn width;\n+}\n+\n+static int find_orig_linenum(struct util_info *u, int lineno)\n+{\n+\tint i;\n+\n+\tfor (i = 0; i < u->num_lines; i++)\n+\t\tif (lineno == u->line_map[i])\n+\t\t\treturn i + 1;\n+\treturn 0;\n+}\n+\n int main(int argc, const char **argv)\n {\n \tint i;\n@@ -750,9 +769,10 @@ int main(int argc, const char **argv)\n \n \tstruct commit_info ci;\n \tconst char *buf;\n-\tint max_digits;\n-\tint longest_file, longest_author;\n+\tint max_digits, max_orig_digits;\n+\tint longest_file, longest_author, longest_file_lines;\n \tint show_name = 0;\n+\tint show_number = 0;\n \n \tconst char *prefix = setup_git_directory();\n \tgit_config(git_default_config);\n@@ -791,6 +811,11 @@ int main(int argc, const char **argv)\n \t\t\t\tshow_name = 1;\n \t\t\t\tcontinue;\n \t\t\t}\n+\t\t\tif (!strcmp(argv[i], \"-n\") ||\n+\t\t\t    !strcmp(argv[i], \"--show-number\")) {\n+\t\t\t\tshow_number = 1;\n+\t\t\t\tcontinue;\n+\t\t\t}\n \t\t\tif (!strcmp(argv[i], \"--\")) {\n \t\t\t\toptions = 0;\n \t\t\t\tcontinue;\n@@ -853,11 +878,11 @@ int main(int argc, const char **argv)\n \tprocess_commits(&rev, filename, &initial);\n \n \tbuf = blame_contents;\n-\tfor (max_digits = 1, i = 10; i <= num_blame_lines + 1; max_digits++)\n-\t\ti *= 10;\n+\tmax_digits = lineno_width(num_blame_lines);\n \n \tlongest_file = 0;\n \tlongest_author = 0;\n+\tlongest_file_lines = 0;\n \tfor (i = 0; i < num_blame_lines; i++) {\n \t\tstruct commit *c = blame_lines[i];\n \t\tstruct util_info *u;\n@@ -869,17 +894,23 @@ int main(int argc, const char **argv)\n \t\t\tshow_name = 1;\n \t\tif (longest_file < strlen(u->pathname))\n \t\t\tlongest_file = strlen(u->pathname);\n+\t\tif (longest_file_lines < u->num_lines)\n+\t\t\tlongest_file_lines = u->num_lines;\n \t\tget_commit_info(c, &ci);\n \t\tif (longest_author < strlen(ci.author))\n \t\t\tlongest_author = strlen(ci.author);\n \t}\n \n+\tmax_orig_digits = lineno_width(longest_file_lines);\n+\n \tfor (i = 0; i < num_blame_lines; i++) {\n \t\tstruct commit *c = blame_lines[i];\n \t\tstruct util_info *u;\n+\t\tint lineno;\n \t\tif (!c)\n \t\t\tc = initial;\n \t\tu = c->util;\n+\t\tlineno = find_orig_linenum(u, i);\n \n \t\tget_commit_info(c, &ci);\n \t\tfwrite(sha1_to_hex(c->object.sha1), sha1_len, 1, stdout);\n@@ -893,6 +924,9 @@ int main(int argc, const char **argv)\n \t\t\tif (show_name)\n \t\t\t\tprintf(\" %-*.*s\", longest_file, longest_file,\n \t\t\t\t       u->pathname);\n+\t\t\tif (show_number)\n+\t\t\t\tprintf(\" %*d\", max_orig_digits,\n+\t\t\t\t       lineno);\n \t\t\tprintf(\" (%-*.*s %10s %*d) \",\n \t\t\t       longest_author, longest_author, ci.author,\n \t\t\t       format_time(ci.author_time, ci.author_tz,\n-- \n1.4.2.3.g866f3\n"}]}