git-blame: --show-number (and -n)
The new option makes the command's native output format show the original line number in the blamed revision. Note: the current implementation of find_orig_linenum involves linear search through the line_map array every time. It should probably build a reverse map upfront and do a simple look-up to speed things up, but I'll leave it to more clever and beautiful people ;-). Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Oct 5, 2006 at 14:06 UTC
cf54a029ff82ce9b565e075bfa5d97ec82841283
1 file changed
+38
-4
blame.c
+38
-4
@@ -731,6 +731,25 @@ static int read_ancestry(const char *graft_file,
731
return 0;
732
}
733
734
+static int lineno_width(int lines)
735
+{
736
+ int i, width;
737
+
738
+ for (width = 1, i = 10; i <= lines + 1; width++)
739
+ i *= 10;
740
+ return width;
741
+}
742
+
743
+static int find_orig_linenum(struct util_info *u, int lineno)
744
+{
745
+ int i;
746
+
747
+ for (i = 0; i < u->num_lines; i++)
748
+ if (lineno == u->line_map[i])
749
+ return i + 1;
750
+ return 0;
751
+}
752
+
753
int main(int argc, const char **argv)
754
{
755
int i;
@@ -750,9 +769,10 @@ int main(int argc, const char **argv)
769
770
struct commit_info ci;
771
const char *buf;
753
- int max_digits;
754
- int longest_file, longest_author;
772
+ int max_digits, max_orig_digits;
773
+ int longest_file, longest_author, longest_file_lines;
774
int show_name = 0;
775
+ int show_number = 0;
776
777
const char *prefix = setup_git_directory();
778
git_config(git_default_config);
@@ -791,6 +811,11 @@ int main(int argc, const char **argv)
811
show_name = 1;
812
continue;
813
}
814
+ if (!strcmp(argv[i], "-n") ||
815
+ !strcmp(argv[i], "--show-number")) {
816
+ show_number = 1;
817
+ continue;
818
+ }
819
if (!strcmp(argv[i], "--")) {
820
options = 0;
821
continue;
@@ -853,11 +878,11 @@ int main(int argc, const char **argv)
878
process_commits(&rev, filename, &initial);
879
880
buf = blame_contents;
856
- for (max_digits = 1, i = 10; i <= num_blame_lines + 1; max_digits++)
857
- i *= 10;
881
+ max_digits = lineno_width(num_blame_lines);
882
883
longest_file = 0;
884
longest_author = 0;
885
+ longest_file_lines = 0;
886
for (i = 0; i < num_blame_lines; i++) {
887
struct commit *c = blame_lines[i];
888
struct util_info *u;
@@ -869,17 +894,23 @@ int main(int argc, const char **argv)
894
show_name = 1;
895
if (longest_file < strlen(u->pathname))
896
longest_file = strlen(u->pathname);
897
+ if (longest_file_lines < u->num_lines)
898
+ longest_file_lines = u->num_lines;
899
get_commit_info(c, &ci);
900
if (longest_author < strlen(ci.author))
901
longest_author = strlen(ci.author);
902
}
903
904
+ max_orig_digits = lineno_width(longest_file_lines);
905
+
906
for (i = 0; i < num_blame_lines; i++) {
907
struct commit *c = blame_lines[i];
908
struct util_info *u;
909
+ int lineno;
910
if (!c)
911
c = initial;
912
u = c->util;
913
+ lineno = find_orig_linenum(u, i);
914
915
get_commit_info(c, &ci);
916
fwrite(sha1_to_hex(c->object.sha1), sha1_len, 1, stdout);
@@ -893,6 +924,9 @@ int main(int argc, const char **argv)
924
if (show_name)
925
printf(" %-*.*s", longest_file, longest_file,
926
u->pathname);
927
+ if (show_number)
928
+ printf(" %*d", max_orig_digits,
929
+ lineno);
930
printf(" (%-*.*s %10s %*d) ",
931
longest_author, longest_author, ci.author,
932
format_time(ci.author_time, ci.author_tz,