Skip to contentSkip to conversation

git-blame: Make the output human readable

PATCH In review Fredrik Kuivinen 3 participants 10 messages Jun 15, 2016 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 git-blame: Make the output human readable

The default output mode is slightly different from git-annotate's. However, git-annotate's output mode can be obtained by using the '-c' flag.

---

 Makefile |    4 ++
 blame.c  |  164 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 149 insertions(+), 19 deletions(-)

3459b7c5032d1462d5857cf6afc2d3e3ef61b93b

Makefile +4 -0
index b6d8804..eb1887d 100644
@@ -534,6 +534,10 @@ git-rev-list$X: rev-list.o $(LIB_FILE)
 	$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
 		$(LIBS) $(OPENSSL_LIBSSL)
 
+git-blame$X: blame.o $(LIB_FILE)
+	$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
+		$(LIBS) -lm
+
 init-db.o: init-db.c
 	$(CC) -c $(ALL_CFLAGS) \
 		-DDEFAULT_GIT_TEMPLATE_DIR='"$(template_dir_SQ)"' $*.c
blame.c +145 -19

226 lines not on this page · raw message

Signature · 1 line
1.2.4.g4644-dirty
#1 lore raw
Quoted 3 lines · Fredrik Kuivinen

The default output mode is slightly different from git-annotate's. However, git-annotate's output mode can be obtained by using the '-c' flag.

It might be better to default to human readable and make the script consumption format an option, if only to reduce typing.

Makefile git-rev-list$X: rev-list.o $(LIB_FILE) · line 540

Quoted 12 lines
Makefile +4 -0
index b6d8804..eb1887d 100644
@@ -534,6 +534,10 @@ git-rev-list$X: rev-list.o $(LIB_FILE)
 	$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
 		$(LIBS) $(OPENSSL_LIBSSL)
 
+git-blame$X: blame.o $(LIB_FILE)
+	$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
+		$(LIBS) -lm
+

I wonder what it is about to link this binary different from others...

Makefile git-rev-list$X: rev-list.o $(LIB_FILE)

Quoted 9 lines
Makefile +9 -0
+char* format_time(unsigned long time, const char* tz)
+{
+	static char time_buf[128];
+	time_t t = time;
+
+	strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S ", gmtime(&t));
+	strcat(time_buf, tz);
+	return time_buf;
+}

I think this shows GMT with time offset, which is compatible with the human readable time Johannes did to git-annotate. I do not know what timezone CVS annotate shows its dates offhand (it seems to only show dates). Johannes, is this an attempt to match what CVS does?

I am wondering if we want to be in line with the date formatting convention used for our commits and tags, that is, to show local timestamp with timezone. The code to use would be show_date() from date.c if we go that route.

#2 lore raw
Quoted 9 lines · Junio C Hamano
Quoted 3 lines · Fredrik Kuivinen

The default output mode is slightly different from git-annotate's. However, git-annotate's output mode can be obtained by using the '-c' flag.

It might be better to default to human readable and make the script consumption format an option, if only to reduce typing.

The default output format is human readable, but it is different from the output format used by git-annotate. By default, (when the patch is applied) git-blame outputs lines with on the following form:

<eight first digits of commit SHA1> (<first 15 letters of committer's name> YYYY-MM-DD HH:MM:SS TZ <line number, right justified>) file contents

where as git-annotate uses

<eight first digits of commit SHA1>\t(<committer's name, right justified, padded to 10 characters> YYYY-MM-DD HH:MM:SS TZ\t<line number>)file contents

I find the first format easier to read since everything is aligned (we always output 15 characters for the committer's name padded with spaces if necessary and the line numbers are padded appropriately). It also takes up less space on screen since it doesn't use tabs.

However, I wanted to use the tests for git-annotate to test git-blame too and the tests do, of course, expect the output to be in git-annotate's format. Hence, the '-c' switch.

We may want to add an output format suitable for scripts too, which just lists the SHA1. But I don't think it is much more difficult to parse the format above, at least not if you just want the SHA1s.

Quoted 8 lines
Quoted 8 lines
Makefile +0 -0
index b6d8804..eb1887d 100644
@@ -534,6 +534,10 @@ git-rev-list$X: rev-list.o $(LIB_FILE)
 	$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
 		$(LIBS) $(OPENSSL_LIBSSL)
 
Quoted 7 lines

Makefile git-rev-list$X: rev-list.o $(LIB_FILE) · line 540

Quoted 4 lines
Makefile +4 -0
+git-blame$X: blame.o $(LIB_FILE)
+	$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
+		$(LIBS) -lm
+

I wonder what it is about to link this binary different from others...

It uses log(3) to compute the number of digits needed to represent the last line number. It is probably better to this some other way though...

Quoted 16 lines

Makefile git-rev-list$X: rev-list.o $(LIB_FILE)

Quoted 9 lines
Makefile +9 -0
+char* format_time(unsigned long time, const char* tz)
+{
+	static char time_buf[128];
+	time_t t = time;
+
+	strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S ", gmtime(&t));
+	strcat(time_buf, tz);
+	return time_buf;
+}

I think this shows GMT with time offset, which is compatible with the human readable time Johannes did to git-annotate. I do not know what timezone CVS annotate shows its dates offhand (it seems to only show dates). Johannes, is this an attempt to match what CVS does?

Quoted 5 lines

I am wondering if we want to be in line with the date formatting convention used for our commits and tags, that is, to show local timestamp with timezone. The code to use would be show_date() from date.c if we go that route.

I think it is a good idea. Consistency is good.

- Fredrik

#3 lore raw

PATCH 1/1 blame and annotate: show localtime with timezone.

Earlier they showed gmtime and timezone, which was inconsistent with the way our commits and tags are pretty-printed.

---

 blame.c           |   15 ++++++++++++---
 git-annotate.perl |    8 +++++++-
 2 files changed, 19 insertions(+), 4 deletions(-)

e29f298a17ee898fd0d191d448ba4fc202175896

blame.c +12 -3
index 59146fa..a3a8ddc 100644
@@ -550,13 +550,22 @@ static void get_commit_info(struct commi
 	*tmp = 0;
 }
 
-char* format_time(unsigned long time, const char* tz)
+static const char* format_time(unsigned long time, const char* tz_str)
 {
 	static char time_buf[128];
 	time_t t = time;
+	int minutes, tz;
+	struct tm *tm;
 
-	strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S ", gmtime(&t));
-	strcat(time_buf, tz);
+	tz = atoi(tz_str);
+	minutes = tz < 0 ? -tz : tz;
+	minutes = (minutes / 100)*60 + (minutes % 100);
+	minutes = tz < 0 ? -minutes : minutes;
+	t = time + minutes * 60;
+	tm = gmtime(&t);
+
+	strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S ", tm);
+	strcat(time_buf, tz_str);
 	return time_buf;
 }
 
git-annotate.perl +7 -1
index d93ee19..b113def 100755
@@ -418,7 +418,13 @@ sub format_date {
 		return $_[0];
 	}
 	my ($timestamp, $timezone) = split(' ', $_[0]);
-	return strftime("%Y-%m-%d %H:%M:%S " . $timezone, gmtime($timestamp));
+	my $minutes = abs($timezone);
+	$minutes = int($minutes / 100) * 60 + ($minutes % 100);
+	if ($timezone < 0) {
+	    $minutes = -$minutes;
+	}
+	my $t = $timestamp + $minutes * 60;
+	return strftime("%Y-%m-%d %H:%M:%S " . $timezone, gmtime($t));
 }
 
 # Copied from git-send-email.perl - We need a Git.pm module..
Signature · 1 line
1.2.4.gee5c7
#4 lore raw

PATCH 1/1 blame: avoid -lm by not using log().

In reply to Fredrik Kuivinen

... as suggested on the list.

---

 Makefile |    4 ----
 blame.c  |    6 ++++--
 2 files changed, 4 insertions(+), 6 deletions(-)

e0539773df17e5f8410850b8ffcfe8d2fa14ab7a

Makefile +0 -4
index eb1887d..b6d8804 100644
@@ -534,10 +534,6 @@ git-rev-list$X: rev-list.o $(LIB_FILE)
 	$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
 		$(LIBS) $(OPENSSL_LIBSSL)
 
-git-blame$X: blame.o $(LIB_FILE)
-	$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
-		$(LIBS) -lm
-
 init-db.o: init-db.c
 	$(CC) -c $(ALL_CFLAGS) \
 		-DDEFAULT_GIT_TEMPLATE_DIR='"$(template_dir_SQ)"' $*.c
blame.c +4 -2
index 168b1f5..59146fa 100644
@@ -589,7 +589,7 @@ int main(int argc, const char **argv)
 				usage(blame_usage);
 			else if(!strcmp(argv[i], "-l") ||
 				!strcmp(argv[i], "--long")) {
-				sha1_len = 20;
+				sha1_len = 40;
 				continue;
 			} else if(!strcmp(argv[i], "-c") ||
 				  !strcmp(argv[i], "--compability")) {
@@ -651,7 +651,9 @@ int main(int argc, const char **argv)
 	process_commits(&rev, filename, &initial);
 
 	buf = blame_contents;
-	max_digits = 1 + log(num_blame_lines+1)/log(10);
+	for (max_digits = 1, i = 10; i <= num_blame_lines; max_digits++)
+		i *= 10;
+
 	for (i = 0; i < num_blame_lines; i++) {
 		struct commit *c = blame_lines[i];
 		if (!c)
Signature · 1 line
1.2.4.gee5c7
#5 lore raw

Re: [PATCH] blame: avoid -lm by not using log().

Hi,

blame.c int main(int argc, const char **argv) · line 655

Quoted 3 lines · Junio C Hamano
blame.c +2 -1
-	max_digits = 1 + log(num_blame_lines+1)/log(10);
+	for (max_digits = 1, i = 10; i <= num_blame_lines; max_digits++)
+		i *= 10;

According to my late-night maths skills, these two methods do not do the same thing: if num_blame_lines == 9, the log method assigns 2 to max_digits, but the for loop stops at max_digits == 1. A trivial fix would be to test for i <= num_blame_lines + 1.

Ciao,
Dscho

#6 lore raw
In reply to Fredrik Kuivinen
Quoted 4 lines · Fredrik Kuivinen

I find the first format easier to read since everything is aligned (we always output 15 characters for the committer's name padded with spaces if necessary and the line numbers are padded appropriately). It also takes up less space on screen since it doesn't use tabs.

Careful. The convention for names is to encode them in UTF-8, so if you mean 15 "characters" that might be OK but if it can truncate in the middle of a multibyte sequence of UTF-8 encoded single character it is a no-no.

We are talking about "casual" aligning, so I would not bring up "proportional fonts", but even on a monospace terminal and line-printer output, there still is the issue of byte count not matching columns. In an i18nized environment, aligning columns by counting bytes is a lost battle.

In any case, please make sure you do not chop a character in the middle at least [*1*].

Quoted 3 lines

We may want to add an output format suitable for scripts too, which just lists the SHA1. But I don't think it is much more difficult to parse the format above, at least not if you just want the SHA1s.

Fair enough.

Quoted 3 lines

It uses log(3) to compute the number of digits needed to represent the last line number. It is probably better to this some other way though...

Yup.

[Footnote]

*1* I was kind of impressed that Linus was careful about this issue when I saw the commit log chopping is only done at line boundaries. A very careful coder would have chopped the last line in the middle at character boundary, but if you do not want to bother that much, chopping at line boundary is better than chopping the last line in the middle of a character ;-).

#7 lore raw
Quoted 12 lines · Junio C Hamano
Quoted 4 lines · Fredrik Kuivinen

I find the first format easier to read since everything is aligned (we always output 15 characters for the committer's name padded with spaces if necessary and the line numbers are padded appropriately). It also takes up less space on screen since it doesn't use tabs.

Careful. The convention for names is to encode them in UTF-8, so if you mean 15 "characters" that might be OK but if it can truncate in the middle of a multibyte sequence of UTF-8 encoded single character it is a no-no.

Quoted 9 lines

We are talking about "casual" aligning, so I would not bring up "proportional fonts", but even on a monospace terminal and line-printer output, there still is the issue of byte count not matching columns. In an i18nized environment, aligning columns by counting bytes is a lost battle.

In any case, please make sure you do not chop a character in the middle at least [*1*].

I hadn't thought of that. The code as it is now will occasionally do the wrong thing when encountered with multibyte per code point encodings. If we can assume that the name is in UTF-8 then the code posted by "[email protected]" to the list should do the trick.

However, can we assume that the name is in UTF-8? Do i18n.commitencoding only apply to the commit message, or is it intended to apply to the author/committer information too?

I am beginning to think that it might be best to, by default, store the author, committer and commit messages in UTF-8. And then automatically convert from/to the users locale on input/output. (I just read the old "Rss produced by git is not valid xml?" thread where this issue was discussed quite a bit. However, I didn't see the 'store in UTF-8 by default and convert from/to users locale' method anywhere in that thread.)

- Fredrik

#8 lore raw
In reply to Fredrik Kuivinen

Hi,

Quoted 11 lines · Fredrik Kuivinen
Quoted 9 lines · Junio C Hamano

Makefile git-rev-list$X: rev-list.o $(LIB_FILE) · line 540

Quoted 4 lines · Fredrik Kuivinen
Makefile +4 -0
+git-blame$X: blame.o $(LIB_FILE)
+	$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
+		$(LIBS) -lm
+

I wonder what it is about to link this binary different from others...

Quoted 3 lines

It uses log(3) to compute the number of digits needed to represent the last line number. It is probably better to this some other way though...

How about this:

- max_digits = 1 + log(num_blame_lines+1)/log(10);
+ for (i = 10, max_digits = 1; i <= num_blame_lines + 1;
+ i *= 10, max_digits++);

(Totally untested)

Hth,
Dscho

#9 lore raw
In reply to Junio C Hamano

Hi,

Quoted 17 lines · Junio C Hamano

blame.c static void process_commits(struct rev_i · line 559

Quoted 9 lines · Fredrik Kuivinen
blame.c +9 -0
+char* format_time(unsigned long time, const char* tz)
+{
+	static char time_buf[128];
+	time_t t = time;
+
+	strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S ", gmtime(&t));
+	strcat(time_buf, tz);
+	return time_buf;
+}

I think this shows GMT with time offset, which is compatible with the human readable time Johannes did to git-annotate. I do not know what timezone CVS annotate shows its dates offhand (it seems to only show dates). Johannes, is this an attempt to match what CVS does?

CVS only shows the date, something like

	strftime("%Y-%b-%d", gmtime($timestamp));
Quoted 4 lines

I am wondering if we want to be in line with the date formatting convention used for our commits and tags, that is, to show local timestamp with timezone. The code to use would be show_date() from date.c if we go that route.

I like that approach. Sometimes imitating CVS can be overdone.

Ciao,
Dscho