Skip to contentSkip to conversation

blame: color line by commit

PATCH In review Stefan Beller 4 participants 18 messages Apr 16, 2018 – Jun 14, 2018 Documentation

lore
Share & tools
lore
#0 lore raw

cover blame: color line by commit

This is a resend of sb/blame-color, dropping the patch
"add option to color metadata fields separately" as I think it is not needed.

Quoted 3 lines

Expecting a reroll.
cf. https://public-inbox.org/git/[email protected]/#t
error messages are funny, can segfault, ...

The error messages have been addressed. However a default mode that is enabled by color.ui has not been added yet.

Thanks,
Stefan

Stefan Beller (2):
builtin/blame: dim uninteresting metadata lines
builtin/blame: highlight recently changed lines

 Documentation/config.txt |  23 ++++++++
 builtin/blame.c          | 118 +++++++++++++++++++++++++++++++++++++--
 color.h                  |   1 +
 t/t8012-blame-colors.sh  |  54 ++++++++++++++++++
 4 files changed, 192 insertions(+), 4 deletions(-)
 create mode 100755 t/t8012-blame-colors.sh
Signature · 1 line
2.17.0.484.g0c8726318c-goog
#1 lore raw

PATCH 1/2 builtin/blame: dim uninteresting metadata lines

When using git-blame lots of lines contain redundant information, for example in hunks that consist of multiple lines, the metadata (commit name, author, date) are repeated. A reader may not be interested in those, so offer an option to color the information that is repeated from the previous line differently. Both the command line option '--color-lines' as well as a config option 'color.blame.colorLines' is provided.

---
 Documentation/config.txt |  5 +++++
 builtin/blame.c          | 42 ++++++++++++++++++++++++++++++++++++----
 color.h                  |  1 +
 t/t8012-blame-colors.sh  | 29 +++++++++++++++++++++++++++
 4 files changed, 73 insertions(+), 4 deletions(-)
 create mode 100755 t/t8012-blame-colors.sh
Documentation/config.txt +5 -0
index 2659153cb3..a223232263 100644
@@ -1218,6 +1218,11 @@ color.status.<slot>::
 	status short-format), or
 	`unmerged` (files which have unmerged changes).
 
+color.blame.repeatedMeta::
+	Use the customized color for the part of git-blame output that
+	is repeated meta information per line (such as commit id,
+	author name, date and timezone). Defaults to dark gray.
+
 color.ui::
 	This variable determines the default value for variables such
 	as `color.diff` and `color.grep` that control the use of color
builtin/blame.c +38 -4

104 lines not on this page · raw message

color.h +1 -0
index cd0bcedd08..196be16058 100644
@@ -30,6 +30,7 @@ struct strbuf;
 #define GIT_COLOR_BLUE		"\033[34m"
 #define GIT_COLOR_MAGENTA	"\033[35m"
 #define GIT_COLOR_CYAN		"\033[36m"
+#define GIT_COLOR_DARK		"\033[1;30m"
 #define GIT_COLOR_BOLD_RED	"\033[1;31m"
 #define GIT_COLOR_BOLD_GREEN	"\033[1;32m"
 #define GIT_COLOR_BOLD_YELLOW	"\033[1;33m"
t/t8012-blame-colors.sh new file +29 -0
new file mode 100755
index 0000000000..bb0fe20e76
@@ -0,0 +1,29 @@
+#!/bin/sh
+
+test_description='colored git blame'
+. ./test-lib.sh
+
+PROG='git blame -c'
+. "$TEST_DIRECTORY"/annotate-tests.sh
+
+test_expect_success 'colored blame colors contiguous lines' '
+	git blame --abbrev=12 --color-lines hello.c >actual.raw &&
+	test_decode_color <actual.raw >actual &&
+	grep "<BOLD;BLACK>" <actual >darkened &&
+	grep "(F" darkened > F.expect &&
+	grep "(H" darkened > H.expect &&
+	test_line_count = 2 F.expect &&
+	test_line_count = 3 H.expect
+'
+
+test_expect_success 'colored blame colors contiguous lines via config' '
+	git -c color.blame.repeatedLines=yellow blame --abbrev=12 hello.c >actual.raw &&
+	test_decode_color <actual.raw >actual &&
+	grep "<YELLOW>" <actual >darkened &&
+	grep "(F" darkened > F.expect &&
+	grep "(H" darkened > H.expect &&
+	test_line_count = 2 F.expect &&
+	test_line_count = 3 H.expect
+'
+
+test_done
Signature · 1 line
2.17.0.484.g0c8726318c-goog
#2 lore raw

Re: [PATCH 1/2] builtin/blame: dim uninteresting metadata lines

builtin/blame.c static const char *format_time(timestamp_t time, const char *tz_str, · line 322

Quoted 8 lines · Stefan Beller
builtin/blame.c +2 -2
@@ -316,10 +318,11 @@ static const char *format_time(timestamp_t time, const char *tz_str,
 #define OUTPUT_PORCELAIN	010
 #define OUTPUT_SHOW_NAME	020
 #define OUTPUT_SHOW_NUMBER	040
-#define OUTPUT_SHOW_SCORE      0100
-#define OUTPUT_NO_AUTHOR       0200
+#define OUTPUT_SHOW_SCORE	0100
+#define OUTPUT_NO_AUTHOR	0200

I wondered what these are about; they are about aligning with HT assuming 8-place tab stop like the other lines.

builtin/blame.c static const char *format_time(timestamp_t time, const char *tz_str, · line 324

Quoted 3 lines
builtin/blame.c +1 -1
 #define OUTPUT_SHOW_EMAIL	0400
-#define OUTPUT_LINE_PORCELAIN 01000
+#define OUTPUT_LINE_PORCELAIN 	01000

But then this line has SP plus HT here; it should have been just a single HT (i.e. replace a single SP with HT), to be consistent.

Quoted 13 lines
builtin/blame.c +2 -0
+#define OUTPUT_COLOR_LINE	02000
 
 static void emit_porcelain_details(struct blame_origin *suspect, int repeat)
 {
@@ -375,6 +378,7 @@ static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int
 	struct commit_info ci;
 	char hex[GIT_MAX_HEXSZ + 1];
 	int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP);
+	const char *color = "", *reset = "";
 
 	get_commit_info(suspect->commit, &ci, 1);
 	oid_to_hex_r(hex, &suspect->commit->object.oid);

builtin/blame.c static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int · line 397

Quoted 10 lines
builtin/blame.c +7 -0
@@ -384,6 +388,19 @@ static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int
 		char ch;
 		int length = (opt & OUTPUT_LONG_OBJECT_NAME) ? GIT_SHA1_HEXSZ : abbrev;
 
+		if (!(opt & OUTPUT_ANNOTATE_COMPAT)) {
+			if (opt & OUTPUT_COLOR_LINE) {
+				if (cnt > 0) {
+					color = repeated_meta_color;
+					reset = GIT_COLOR_RESET;
+				} else  {
+					color ="";

You need a SP after '=' that assigns an empty string to 'color'.

builtin/blame.c static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int · line 402

Quoted 5 lines
builtin/blame.c +5 -0
+					reset = "";
+				}
+			}
+			fputs(color, stdout);
+		}

This fputs() should be hidden to the case where color is *NOT* an empty string, no? In any case, it should be inside "if color-line is in effect" block, I would think.

Users of "git annotate" would not pass the --color option, so I do not think the outer if () block is worth the loss of readability due to increased indent level.

I would say that it is a job of the caller of git_config() to make sure color.blame.lines would not take effect when the command is annotate, if what you are worried about is the configuration in this code.

builtin/blame.c static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int · line 455

Quoted 7 lines
builtin/blame.c +3 -0
@@ -433,6 +450,9 @@ static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int
 			printf(" %*d) ",
 			       max_digits, ent->lno + 1 + cnt);
 		}
+		if (!(opt & OUTPUT_ANNOTATE_COMPAT) &&
+		    (opt & OUTPUT_COLOR_LINE))
+			fputs(reset, stdout);

Likewise.

builtin/blame.c int cmd_blame(int argc, const char **argv, const char *prefix) · line 987

Quoted 11 lines
builtin/blame.c +5 -1
@@ -949,8 +979,12 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
 
 	blame_coalesce(&sb);
 
-	if (!(output_option & OUTPUT_PORCELAIN))
+	if (!(output_option & OUTPUT_PORCELAIN)) {
 		find_alignment(&sb, &output_option);
+		if (!*repeated_meta_color &&
+		    (output_option & OUTPUT_COLOR_LINE))
+			strcpy(repeated_meta_color, GIT_COLOR_DARK);
+	}

This code does not check OUTPUT_ANNOTATE_COMPAT because it assumes that OUTPUT_COLOR_LINE won't be in output_option when we are working in annotate compatibility mode. The deeper codepaths we saw above should do the same. It should be enough to drop color-line when anno-compat is set, or something like that, immediately after reading the config and overriding them from the command line.

color.h struct strbuf; · line 36

Quoted 12 lines
color.h +1 -0
index cd0bcedd08..196be16058 100644
@@ -30,6 +30,7 @@ struct strbuf;
 #define GIT_COLOR_BLUE		"\033[34m"
 #define GIT_COLOR_MAGENTA	"\033[35m"
 #define GIT_COLOR_CYAN		"\033[36m"
+#define GIT_COLOR_DARK		"\033[1;30m"
 #define GIT_COLOR_BOLD_RED	"\033[1;31m"
 #define GIT_COLOR_BOLD_GREEN	"\033[1;32m"
 #define GIT_COLOR_BOLD_YELLOW	"\033[1;33m"

I wonder if it is worth adding a new color only to give this a different default.

Traditionally, we use CYAN for lines that are less interesting than others (e.g. hunk header), so reusing it might make the life easier to the users, especially because I envision that we may want to introduce another "logical" level to give another redirection between the configuration keys like color.diff.frag and color.blame.repeatedlines and the actual ANSI sequence like "\033[36m". I.e. we update the system so that these two configuration keys take "uninteresting" (which is one of the "logical" colors) by default, and then map "uninteresting" to "\033[36m" at the physical level by default. The users could then change the mapping from "uninteresting" to "\033[1;30m" and consistently modify both diff.frag and blame.repeated if they wanted to. Of course, if they want them to be different, they can come up with a different "logical" color and split the two. And from that point of view, adding new colors to the default set we have above will make life harder for the end users.

#3 lore raw

Re: [PATCH 1/2] builtin/blame: dim uninteresting metadata lines

Hi Junio,

Quoted 7 lines

builtin/blame.c static const char *format_time(timestamp_t time, const char *tz_str, · line 322

Quoted 4 lines
builtin/blame.c +2 -2
-#define OUTPUT_SHOW_SCORE      0100
-#define OUTPUT_NO_AUTHOR       0200
+#define OUTPUT_SHOW_SCORE    0100
+#define OUTPUT_NO_AUTHOR     0200

I wondered what these are about; they are about aligning with HT assuming 8-place tab stop like the other lines.

correct.

Quoted 5 lines

builtin/blame.c static const char *format_time(timestamp_t time, const char *tz_str, · line 324

Quoted 2 lines
builtin/blame.c +1 -1
-#define OUTPUT_LINE_PORCELAIN 01000
+#define OUTPUT_LINE_PORCELAIN        01000

But then this line has SP plus HT here; it should have been just a single HT (i.e. replace a single SP with HT), to be consistent.

fixed

Quoted 24 lines

builtin/blame.c static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int · line 397

Quoted 11 lines
builtin/blame.c +7 -0
@@ -384,6 +388,19 @@ static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int
              char ch;
              int length = (opt & OUTPUT_LONG_OBJECT_NAME) ? GIT_SHA1_HEXSZ : abbrev;
 
+             if (!(opt & OUTPUT_ANNOTATE_COMPAT)) {
+                     if (opt & OUTPUT_COLOR_LINE) {
+                             if (cnt > 0) {
+                                     color = repeated_meta_color;
+                                     reset = GIT_COLOR_RESET;
+                             } else  {
+                                     color ="";

You need a SP after '=' that assigns an empty string to 'color'.

builtin/blame.c static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int · line 402

Quoted 5 lines
builtin/blame.c +5 -0
+                                     reset = "";
+                             }
+                     }
+                     fputs(color, stdout);
+             }

This fputs() should be hidden to the case where color is *NOT* an empty string, no? In any case, it should be inside "if color-line is in effect" block, I would think.

Quoted 8 lines

Users of "git annotate" would not pass the --color option, so I do not think the outer if () block is worth the loss of readability due to increased indent level.

I would say that it is a job of the caller of git_config() to make sure color.blame.lines would not take effect when the command is annotate, if what you are worried about is the configuration in this code.

ok, so we'll have to correct these mis aligned configs before hand and here we just go by the bits set in the flags.

Quoted 4 lines
Quoted 4 lines
builtin/blame.c +0 -0
@@ -949,8 +979,12 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
 
      blame_coalesce(&sb);
 
Quoted 14 lines

builtin/blame.c int cmd_blame(int argc, const char **argv, const char *prefix) · line 987

Quoted 7 lines
builtin/blame.c +5 -1
-     if (!(output_option & OUTPUT_PORCELAIN))
+     if (!(output_option & OUTPUT_PORCELAIN)) {
              find_alignment(&sb, &output_option);
+             if (!*repeated_meta_color &&
+                 (output_option & OUTPUT_COLOR_LINE))
+                     strcpy(repeated_meta_color, GIT_COLOR_DARK);
+     }

This code does not check OUTPUT_ANNOTATE_COMPAT because it assumes that OUTPUT_COLOR_LINE won't be in output_option when we are working in annotate compatibility mode. The deeper codepaths we saw above should do the same. It should be enough to drop color-line when anno-compat is set, or something like that, immediately after reading the config and overriding them from the command line.

Makes sense.

Quoted 17 lines

color.h struct strbuf; · line 36

Quoted 13 lines
color.h +1 -0
index cd0bcedd08..196be16058 100644
@@ -30,6 +30,7 @@ struct strbuf;
 #define GIT_COLOR_BLUE               "\033[34m"
 #define GIT_COLOR_MAGENTA    "\033[35m"
 #define GIT_COLOR_CYAN               "\033[36m"
+#define GIT_COLOR_DARK               "\033[1;30m"
 #define GIT_COLOR_BOLD_RED   "\033[1;31m"
 #define GIT_COLOR_BOLD_GREEN "\033[1;32m"
 #define GIT_COLOR_BOLD_YELLOW        "\033[1;33m"

I wonder if it is worth adding a new color only to give this a different default.

Quoted 16 lines

Traditionally, we use CYAN for lines that are less interesting than others (e.g. hunk header), so reusing it might make the life easier to the users, especially because I envision that we may want to introduce another "logical" level to give another redirection between the configuration keys like color.diff.frag and color.blame.repeatedlines and the actual ANSI sequence like "\033[36m". I.e. we update the system so that these two configuration keys take "uninteresting" (which is one of the "logical" colors) by default, and then map "uninteresting" to "\033[36m" at the physical level by default. The users could then change the mapping from "uninteresting" to "\033[1;30m" and consistently modify both diff.frag and blame.repeated if they wanted to. Of course, if they want them to be different, they can come up with a different "logical" color and split the two. And from that point of view, adding new colors to the default set we have above will make life harder for the end users.

That is a good longer term vision. I'll switch to cyan for now. Thanks,
Stefan

#4 lore raw

PATCH 2/2 builtin/blame: highlight recently changed lines

In reply to Stefan Beller

Choose a different color for dates and imitate a 'temperature cool down' depending upon age.

Originally I had planned to have the temperature cooldown dependent on the age of the project or file for example, as that might scale better, but that can be added on top of this commit, e.g. instead of giving a date, you could imagine giving a percentage that would be the linearly interpolated between now and the beginning of the file.

---
 Documentation/config.txt | 18 ++++++++++
 builtin/blame.c          | 78 +++++++++++++++++++++++++++++++++++++++-
 t/t8012-blame-colors.sh  | 25 +++++++++++++
 3 files changed, 120 insertions(+), 1 deletion(-)
Documentation/config.txt +18 -0
index a223232263..acc456e1fd 100644
@@ -1223,6 +1223,24 @@ color.blame.repeatedMeta::
 	is repeated meta information per line (such as commit id,
 	author name, date and timezone). Defaults to dark gray.
 
+color.blame.highlightRecent::
+	This can be used to color the author and date of a blame line.
+	This overrides `color.blame.repeatedMeta` setting, which colors
+	repetitions.
++
+This setting should be set to a comma-separated list of color and date settings,
+starting and ending with a color, the dates should be set from oldest to newest.
+The metadata will be colored given the colors if the the line was introduced
+before the given timestamp, overwriting older timestamped colors.
++
+Instead of an absolute timestamp relative timestamps work as well, e.g.
+2.weeks.ago is valid to address anything older than 2 weeks.
++
+It defaults to "blue,12 month ago,white,1 month ago,red", which colors
+everything older than one year blue, recent changes between one month and
+one year old are kept white, and lines introduced within the last month are
+colored red.
+
 color.ui::
 	This variable determines the default value for variables such
 	as `color.diff` and `color.grep` that control the use of color
builtin/blame.c +77 -1

132 lines not on this page · raw message

t/t8012-blame-colors.sh +25 -0
index bb0fe20e76..d6b2932bf8 100755
@@ -26,4 +26,29 @@ test_expect_success 'colored blame colors contiguous lines via config' '
 	test_line_count = 3 H.expect
 '
 
+test_expect_success 'heated line coloring for old code' '
+	git blame --heated-lines hello.c >actual.raw &&
+	test_decode_color <actual.raw >actual &&
+	grep "<BLUE>" <actual >colored &&
+	test_line_count = 10 colored
+'
+
+test_expect_success 'heated line coloring' '
+	cat >>hello.c <<-EOF &&
+		void qfunc();
+	EOF
+	git add hello.c &&
+	GIT_AUTHOR_DATE="" git commit -m "new commit" &&
+
+	git blame --heated-lines hello.c >actual.raw &&
+	test_decode_color <actual.raw >actual &&
+
+	grep "<BLUE>" <actual >colored &&
+	test_line_count = 10 colored &&
+
+	grep "<RED>" <actual >colored &&
+	test_line_count = 1 colored &&
+	grep qfunc colored
+'
+
 test_done
Signature · 1 line
2.17.0.484.g0c8726318c-goog
#5 lore raw

Re: [PATCH 2/2] builtin/blame: highlight recently changed lines

Quoted 10 lines · Stefan Beller

Choose a different color for dates and imitate a 'temperature cool down' depending upon age.

Originally I had planned to have the temperature cooldown dependent on the age of the project or file for example, as that might scale better, but that can be added on top of this commit, e.g. instead of giving a date, you could imagine giving a percentage that would be the linearly interpolated between now and the beginning of the file.
...

builtin/blame.c static const char *format_time(timestamp_t time, const char *tz_str, · line 327

Quoted 4 lines
builtin/blame.c +1 -0
@@ -323,6 +324,7 @@ static const char *format_time(timestamp_t time, const char *tz_str,
 #define OUTPUT_SHOW_EMAIL	0400
 #define OUTPUT_LINE_PORCELAIN 	01000
 #define OUTPUT_COLOR_LINE	02000
+#define OUTPUT_HEATED_LINES	04000

How about calling it OUTPUT_SHOW_AGE_WITH_COLOR or something like that instead? Anything with "AGE" in it, if that is what you are trying to indicate, would be more appropriate than "HEATED", which does not convey much meaning to readers unless you explain what determines the temperature in your mind.

#6 lore raw

Re: [PATCH 2/2] builtin/blame: highlight recently changed lines

In reply to Stefan Beller

It seems that this

$ git -c color.blame.repeatedlines=cyan blame --heated-lines builtin/blame.c

refuses to run.

Would it work if the configuration is in .git/config instead, or would it forever disable --heated-lines once somebody choses to use --color-lines feature by default by configuring it in?

#7 lore raw

Re: [PATCH 2/2] builtin/blame: highlight recently changed lines

Quoted 9 lines · Junio C Hamano

It seems that this

$ git -c color.blame.repeatedlines=cyan blame --heated-lines builtin/blame.c

refuses to run.

Would it work if the configuration is in .git/config instead, or would it forever disable --heated-lines once somebody choses to use --color-lines feature by default by configuring it in?

That is the unfortunate part of this series, I have not figured out how to treat these two options at the same time.

One could take the approach to check the config first and see if there are conflicts and then overlay it with the command line options (and resolve conflicts there, but CLI taking precedence over config).

Or we'd need to introduce another config blame.coloring={none, repeatedlines, highlightrecent} which breaks the tie.

Thanks,
Stefan

#8 lore raw

Re: [PATCH 2/2] builtin/blame: highlight recently changed lines

Quoted 14 lines · Stefan Beller
Quoted 9 lines · Junio C Hamano

It seems that this

$ git -c color.blame.repeatedlines=cyan blame --heated-lines builtin/blame.c

refuses to run.

Would it work if the configuration is in .git/config instead, or would it forever disable --heated-lines once somebody choses to use --color-lines feature by default by configuring it in?

That is the unfortunate part of this series, I have not figured out how to treat these two options at the same time.

Quoted 7 lines

One could take the approach to check the config first and see if there are conflicts and then overlay it with the command line options (and resolve conflicts there, but CLI taking precedence over config).

Or we'd need to introduce another config blame.coloring={none, repeatedlines, highlightrecent} which breaks the tie.

Nevermind, we can overlay these color schemes just fine, which I'll do in a resend.

#9 lore raw

PATCH 1/2 builtin/blame: dim uninteresting metadata lines

When using git-blame lots of lines contain redundant information, for example in hunks that consist of multiple lines, the metadata (commit name, author, date) are repeated. A reader may not be interested in those, so offer an option to color the information that is repeated from the previous line differently. Both the command line option '--color-lines' as well as a config option 'color.blame.colorLines' is provided.

---
 Documentation/config.txt |  5 +++++
 builtin/blame.c          | 42 ++++++++++++++++++++++++++++++++++++----
 t/t8012-blame-colors.sh  | 29 +++++++++++++++++++++++++++
 3 files changed, 72 insertions(+), 4 deletions(-)
 create mode 100755 t/t8012-blame-colors.sh
Documentation/config.txt +5 -0
index 2659153cb3..8128eee4f9 100644
@@ -1218,6 +1218,11 @@ color.status.<slot>::
 	status short-format), or
 	`unmerged` (files which have unmerged changes).
 
+color.blame.repeatedMeta::
+	Use the customized color for the part of git-blame output that
+	is repeated meta information per line (such as commit id,
+	author name, date and timezone). Defaults to cyan.
+
 color.ui::
 	This variable determines the default value for variables such
 	as `color.diff` and `color.grep` that control the use of color
builtin/blame.c +38 -4

104 lines not on this page · raw message

t/t8012-blame-colors.sh new file +29 -0
new file mode 100755
index 0000000000..9fc32ff813
@@ -0,0 +1,29 @@
+#!/bin/sh
+
+test_description='colored git blame'
+. ./test-lib.sh
+
+PROG='git blame -c'
+. "$TEST_DIRECTORY"/annotate-tests.sh
+
+test_expect_success 'colored blame colors contiguous lines' '
+	git blame --abbrev=12 --color-lines hello.c >actual.raw &&
+	test_decode_color <actual.raw >actual &&
+	grep "<CYAN>" <actual >darkened &&
+	grep "(F" darkened > F.expect &&
+	grep "(H" darkened > H.expect &&
+	test_line_count = 2 F.expect &&
+	test_line_count = 3 H.expect
+'
+
+test_expect_success 'colored blame colors contiguous lines via config' '
+	git -c color.blame.repeatedLines=yellow blame --abbrev=12 hello.c >actual.raw &&
+	test_decode_color <actual.raw >actual &&
+	grep "<YELLOW>" <actual >darkened &&
+	grep "(F" darkened > F.expect &&
+	grep "(H" darkened > H.expect &&
+	test_line_count = 2 F.expect &&
+	test_line_count = 3 H.expect
+'
+
+test_done
Signature · 1 line
2.17.0.484.g0c8726318c-goog
#10 lore raw

PATCH 2/2 builtin/blame: highlight recently changed lines

Choose a different color for dates and imitate a 'temperature cool down' depending upon age.

Originally I had planned to have the temperature cooldown dependent on the age of the project or file for example, as that might scale better, but that can be added on top of this commit, e.g. instead of giving a date, you could imagine giving a percentage that would be the linearly interpolated between now and the beginning of the file.

# Conflicts:
# builtin/blame.c

---
 Documentation/config.txt | 17 +++++++++
 builtin/blame.c          | 81 ++++++++++++++++++++++++++++++++++++++--
 t/t8012-blame-colors.sh  | 25 +++++++++++++
 3 files changed, 119 insertions(+), 4 deletions(-)
Documentation/config.txt +17 -0
index 8128eee4f9..eae88be662 100644
@@ -1223,6 +1223,23 @@ color.blame.repeatedMeta::
 	is repeated meta information per line (such as commit id,
 	author name, date and timezone). Defaults to cyan.
 
+color.blame.highlightRecent::
+	This can be used to color the metadata of a blame line depending
+	on age of the line.
++
+This setting should be set to a comma-separated list of color and date settings,
+starting and ending with a color, the dates should be set from oldest to newest.
+The metadata will be colored given the colors if the the line was introduced
+before the given timestamp, overwriting older timestamped colors.
++
+Instead of an absolute timestamp relative timestamps work as well, e.g.
+2.weeks.ago is valid to address anything older than 2 weeks.
++
+It defaults to "blue,12 month ago,white,1 month ago,red", which colors
+everything older than one year blue, recent changes between one month and
+one year old are kept white, and lines introduced within the last month are
+colored red.
+
 color.ui::
 	This variable determines the default value for variables such
 	as `color.diff` and `color.grep` that control the use of color
builtin/blame.c +77 -4

140 lines not on this page · raw message

t/t8012-blame-colors.sh +25 -0
index 9fc32ff813..6f6be6c5dc 100755
@@ -26,4 +26,29 @@ test_expect_success 'colored blame colors contiguous lines via config' '
 	test_line_count = 3 H.expect
 '
 
+test_expect_success 'color by age consistently colors old code' '
+	git blame --color-by-age hello.c >actual.raw &&
+	test_decode_color <actual.raw >actual &&
+	grep "<BLUE>" <actual >colored &&
+	test_line_count = 10 colored
+'
+
+test_expect_success 'blame color by age: new code is different' '
+	cat >>hello.c <<-EOF &&
+		void qfunc();
+	EOF
+	git add hello.c &&
+	GIT_AUTHOR_DATE="" git commit -m "new commit" &&
+
+	git blame --color-by-age hello.c >actual.raw &&
+	test_decode_color <actual.raw >actual &&
+
+	grep "<BLUE>" <actual >colored &&
+	test_line_count = 10 colored &&
+
+	grep "<RED>" <actual >colored &&
+	test_line_count = 1 colored &&
+	grep qfunc colored
+'
+
 test_done
Signature · 1 line
2.17.0.484.g0c8726318c-goog
#11 lore raw

Re: [PATCH 2/2] builtin/blame: highlight recently changed lines

Quoted 9 lines · Stefan Beller

Choose a different color for dates and imitate a 'temperature cool down' depending upon age.

Originally I had planned to have the temperature cooldown dependent on the age of the project or file for example, as that might scale better, but that can be added on top of this commit, e.g. instead of giving a date, you could imagine giving a percentage that would be the linearly interpolated between now and the beginning of the file.

Quoted 4 lines

# Conflicts:
# builtin/blame.c

Meh.

#12 lore raw

Re: [PATCH 2/2] builtin/blame: highlight recently changed lines

Quoted 10 lines · Eric Sunshine
Quoted 9 lines · Stefan Beller

Choose a different color for dates and imitate a 'temperature cool down' depending upon age.

Originally I had planned to have the temperature cooldown dependent on the age of the project or file for example, as that might scale better, but that can be added on top of this commit, e.g. instead of giving a date, you could imagine giving a percentage that would be the linearly interpolated between now and the beginning of the file.

Quoted 6 lines
Quoted 4 lines

# Conflicts:
# builtin/blame.c

Meh.

Sloppy.

Will edit it out.

Thanks.

#13 lore raw

Re: [PATCH 2/2] builtin/blame: highlight recently changed lines

In reply to Stefan Beller

Am 17.04.2018 um 23:30 schrieb Stefan Beller:

Review

Quoted 9 lines
(quoted diff) +9 -0
+static void parse_color_fields(const char *s)
+{
+	struct string_list l = STRING_LIST_INIT_DUP;
+	struct string_list_item *item;
+	enum { EXPECT_DATE, EXPECT_COLOR } next = EXPECT_COLOR;
+
+	colorfield_nr = 0;
+
+	/* Ideally this would be stripped and split at the same time? */

Why? Both approxidate() and color_parse() handle spaces.

Quoted 14 lines
(quoted diff) +14 -0
+	string_list_split(&l, s, ',', -1);
+	ALLOC_GROW(colorfield, colorfield_nr + 1, colorfield_alloc);
+
+	for_each_string_list_item(item, &l) {
+		switch (next) {
+		case EXPECT_DATE:
+			colorfield[colorfield_nr].hop = approxidate(item->string);
+			next = EXPECT_COLOR;
+			colorfield_nr++;
+			ALLOC_GROW(colorfield, colorfield_nr + 1, colorfield_alloc);
+			break;
+		case EXPECT_COLOR:
+			if (color_parse(item->string, colorfield[colorfield_nr].col))
+				die(_("expecting a color: %s"), item->string);

Review

Quoted 10 lines
(quoted diff) +10 -0
+			next = EXPECT_DATE;
+			break;
+		}
+	}
+
+	if (next == EXPECT_COLOR)
+		die (_("must end with a color"));
+
+	colorfield[colorfield_nr].hop = TIME_MAX;
+}

This adds a minor memory leak; fix below.

-- >8 --
Subject: [PATCH] blame: release string_list after use in parse_color_fields()

---
 builtin/blame.c | 1 +
 1 file changed, 1 insertion(+)
builtin/blame.c +1 -0
index 4202584f97..3295718841 100644
@@ -411,6 +411,7 @@ static void parse_color_fields(const char *s)
 		die (_("must end with a color"));
 
 	colorfield[colorfield_nr].hop = TIME_MAX;
+	string_list_clear(&l, 0);
 }
 
 static void setup_default_color_by_age(void)
Signature · 1 line
2.17.1
#14 lore raw

Re: [PATCH 2/2] builtin/blame: highlight recently changed lines

Quoted 13 lines · René Scharfe

Am 17.04.2018 um 23:30 schrieb Stefan Beller:

Review

Quoted 9 lines
(quoted diff) +9 -0
+static void parse_color_fields(const char *s)
+{
+     struct string_list l = STRING_LIST_INIT_DUP;
+     struct string_list_item *item;
+     enum { EXPECT_DATE, EXPECT_COLOR } next = EXPECT_COLOR;
+
+     colorfield_nr = 0;
+
+     /* Ideally this would be stripped and split at the same time? */

Why? Both approxidate() and color_parse() handle spaces.

I think that comment is stale; I remember experimenting with different internal structs to write a parser until I came up with this implementation. It is a left over.

Quoted 1 line

This adds a minor memory leak; fix below.

Thanks!

Quoted 22 lines

-- >8 --
Subject: [PATCH] blame: release string_list after use in parse_color_fields()

---
 builtin/blame.c | 1 +
 1 file changed, 1 insertion(+)
builtin/blame.c +1 -1
index 4202584f97..3295718841 100644
@@ -411,6 +411,7 @@ static void parse_color_fields(const char *s)
                die (_("must end with a color"));
 
        colorfield[colorfield_nr].hop = TIME_MAX;
+       string_list_clear(&l, 0);
 }
 
 static void setup_default_color_by_age(void)
--

2.17.1

#15 lore raw

Re: [PATCH 2/2] builtin/blame: highlight recently changed lines

In reply to René Scharfe
Quoted 8 lines · René Scharfe

This adds a minor memory leak; fix below.

-- >8 --
Subject: [PATCH] blame: release string_list after use in parse_color_fields()

---

Thanks. Will apply.

Quoted 15 lines
 builtin/blame.c | 1 +
 1 file changed, 1 insertion(+)
builtin/blame.c +1 -0
index 4202584f97..3295718841 100644
@@ -411,6 +411,7 @@ static void parse_color_fields(const char *s)
 		die (_("must end with a color"));
 
 	colorfield[colorfield_nr].hop = TIME_MAX;
+	string_list_clear(&l, 0);
 }
 
 static void setup_default_color_by_age(void)
#16 lore raw

Re: [PATCH 1/2] builtin/blame: dim uninteresting metadata lines

In reply to Stefan Beller
Quoted 9 lines · Stefan Beller
builtin/blame.c +1 -0
@@ -375,6 +378,7 @@ static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int
 	struct commit_info ci;
 	char hex[GIT_MAX_HEXSZ + 1];
 	int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP);
+	const char *color = NULL, *reset = NULL;
 
 	get_commit_info(suspect->commit, &ci, 1);
 	oid_to_hex_r(hex, &suspect->commit->object.oid);

builtin/blame.c static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int · line 397

Quoted 10 lines
builtin/blame.c +7 -0
@@ -384,6 +388,18 @@ static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int
 		char ch;
 		int length = (opt & OUTPUT_LONG_OBJECT_NAME) ? GIT_SHA1_HEXSZ : abbrev;
 
+		if (opt & OUTPUT_COLOR_LINE) {
+			if (cnt > 0) {
+				color = repeated_meta_color;
+				reset = GIT_COLOR_RESET;
+			} else  {
+				color = "";
+				reset = "";

Shouldn't these be NULL as you do not want to fputs() these when not in painting mode anyway? Which would make it consistent with ...

builtin/blame.c static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int · line 402

Quoted 5 lines
builtin/blame.c +5 -0
+			}
+		}
+		if (color)
+			fputs(color, stdout);
+

... this thing which otherwise needs to be "if (color && *color)", but if you do NULL, can be left as-is ;-)

builtin/blame.c static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int · line 453

Quoted 6 lines
builtin/blame.c +2 -0
@@ -433,6 +449,8 @@ static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int
 			printf(" %*d) ",
 			       max_digits, ent->lno + 1 + cnt);
 		}
+		if (reset)
+			fputs(reset, stdout);

Likewise.

#17 lore raw

Re: [PATCH 2/2] builtin/blame: highlight recently changed lines

In reply to Stefan Beller
Quoted 5 lines · Stefan Beller
Quoted 4 lines · Junio C Hamano

It seems that this

$ git -c color.blame.repeatedlines=cyan blame --heated-lines builtin/blame.c
Quoted 8 lines
Quoted 5 lines

refuses to run.

Would it work if the configuration is in .git/config instead, or would it forever disable --heated-lines once somebody choses to use --color-lines feature by default by configuring it in?

That is the unfortunate part of this series, I have not figured out how to treat these two options at the same time.

Perhaps I wasn't clear enough, but I did not want to use both at the same time. "git blame --color-lines --heated-lines" that errors out saying these cannot be used at the same time is an acceptable limitation.

My sole complaint was that just like command line is used to override (weaker) configs, the wish to use "repeatedlines" painting by default expressed in the configuration form should be overriden when there is an explicit command line --heated-lines option that is incompatible with it.

In this particular case, you might be able to come up with a scheme where both can be made effective at the same time, but the principle still stands, and that is the more important lesson I'd like to see you learn.

Thanks.