Skip to contentSkip to conversation

git log -z still outputting newlines?

PATCH In review Randal L. Schwartz 6 participants 17 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw
$ git log -z --format='%cE' -5 | od -c
0000000    g   i   t   s   t   e   r   @   p   o   b   o   x   .   c   o
0000020    m  \n   g   i   t   s   t   e   r   @   p   o   b   o   x   .
0000040    c   o   m  \n   g   i   t   s   t   e   r   @   p   o   b   o
0000060    x   .   c   o   m  \n   g   i   t   s   t   e   r   @   p   o
0000100    b   o   x   .   c   o   m  \n   g   i   t   s   t   e   r   @
0000120    p   o   b   o   x   .   c   o   m  \n                        
0000132

Why are all those newlines in there? Bug? Misfeature? Feature? If feature, how do I ensure \0 in my output? If I add %x00, I get both \0 *and* \n in output. :(

Signature · 4 lines
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[email protected]> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion
#1 lore raw

PATCH 1/1 log-tree: use custom line terminator in line termination mode

When using a custom format in line termination mode (as opposed to line separation mode), the configured line terminator is not used, so things like "git log --pretty=tformat:%H -z" do not work properly.

Make it use the line terminator the user ordered.

---
 log-tree.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
log-tree.c +1 -1
index 34c49e7..44f0268 100644
@@ -682,7 +682,7 @@ void show_log(struct rev_info *opt)
 	if (opt->use_terminator) {
 		if (!opt->missing_newline)
 			graph_show_padding(opt->graph);
-		putchar('\n');
+		putchar(opt->diffopt.line_termination);
 	}
 
 	strbuf_release(&msgbuf);
Signature · 1 line
1.7.10
#2 lore raw

Re: [PATCH] log-tree: use custom line terminator in line termination mode

Quoted 16 lines · Jan Krüger

When using a custom format in line termination mode (as opposed to line separation mode), the configured line terminator is not used, so things like "git log --pretty=tformat:%H -z" do not work properly.

Make it use the line terminator the user ordered.

---
 log-tree.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
log-tree.c +0 -0
index 34c49e7..44f0268 100644

log-tree.c void show_log(struct rev_info *opt) · line 688

Quoted 8 lines
log-tree.c +1 -1
@@ -682,7 +682,7 @@ void show_log(struct rev_info *opt)
 	if (opt->use_terminator) {
 		if (!opt->missing_newline)
 			graph_show_padding(opt->graph);
-		putchar('\n');
+		putchar(opt->diffopt.line_termination);
 	}
 
 	strbuf_release(&msgbuf);

Looks sensible. Perhaps we would want to add a test?

#3 lore raw

PATCH v2 1/1 log-tree: use custom line terminator in line termination mode

When using a custom format in line termination mode (as opposed to line separation mode), the configured line terminator is not used, so things like "git log --pretty=tformat:%H -z" do not work properly.

Make it use the line terminator the user ordered.

---
Here are two simple tests, for both format: and tformat: with -z.

 log-tree.c                    |    2 +-
 t/t4205-log-pretty-formats.sh |   12 ++++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)
log-tree.c +1 -1
index 34c49e7..44f0268 100644
@@ -682,7 +682,7 @@ void show_log(struct rev_info *opt)
 	if (opt->use_terminator) {
 		if (!opt->missing_newline)
 			graph_show_padding(opt->graph);
-		putchar('\n');
+		putchar(opt->diffopt.line_termination);
 	}
 
 	strbuf_release(&msgbuf);
t/t4205-log-pretty-formats.sh +12 -0
index 2ae9faa..03a73ba 100755
@@ -71,4 +71,16 @@ test_expect_success 'alias loop' '
 	test_must_fail git log --pretty=test-foo
 '
 
+printf "add bar\0initial" > expected
+test_expect_success 'NUL separation' '
+	git log -z --pretty="format:%s" >actual &&
+	test_cmp expected actual
+'
+
+printf "add bar\0initial\0" > expected
+test_expect_success 'NUL termination' '
+	git log -z --pretty="tformat:%s" >actual &&
+	test_cmp expected actual
+'
+
 test_done
Signature · 1 line
1.7.10.406.g0017
#4 lore raw

Re: [PATCH v2] log-tree: use custom line terminator in line termination mode

Quoted 9 lines · Jan Krüger

When using a custom format in line termination mode (as opposed to line separation mode), the configured line terminator is not used, so things like "git log --pretty=tformat:%H -z" do not work properly.

Make it use the line terminator the user ordered.

---
Here are two simple tests, for both format: and tformat: with -z.

Thanks for being thorough. Very much appreciated.

Having said that, are we sure that

printf "add bar\0initial"

works per specification, or merely works by accident in some implementation?

In C, we have to write this as printf("add bar%cinitial", 0), and the above makes my stomach feel a bit queasy.

Admittedly we have "printf "\0\0" in t6024 and we haven't seen anybody complain for the past 6 years, so perhaps I shouldn't be worried too much about this.

Quoted 21 lines
t/t4205-log-pretty-formats.sh +12 -0
index 2ae9faa..03a73ba 100755
@@ -71,4 +71,16 @@ test_expect_success 'alias loop' '
 	test_must_fail git log --pretty=test-foo
 '
 
+printf "add bar\0initial" > expected
+test_expect_success 'NUL separation' '
+	git log -z --pretty="format:%s" >actual &&
+	test_cmp expected actual
+'
+
+printf "add bar\0initial\0" > expected
+test_expect_success 'NUL termination' '
+	git log -z --pretty="tformat:%s" >actual &&
+	test_cmp expected actual
+'
+
 test_done
#5 lore raw

Re: [PATCH v2] log-tree: use custom line terminator in line termination mode

Quoted 6 lines · Junio C Hamano

Having said that, are we sure that

printf "add bar\0initial"

works per specification, or merely works by accident in some implementation?

I don't know for certain. In these cases I go by what dash (Debian's sh implementation) does. To quote its manpage:

"Only features designated by POSIX, plus a few Berkeley extensions, are being incorporated into this shell."

It does support this syntax for printf. That's enough for me personally. Is it enough for everyone? I don't know.

-Jan

#6 lore raw

Re: [PATCH v2] log-tree: use custom line terminator in line termination mode

In reply to Junio C Hamano
Quoted 6 lines · Junio C Hamano

Having said that, are we sure that

printf "add bar\0initial"

works per specification, or merely works by accident in some implementation?

Since the backslash is not followed by $ ` " \ <newline> it is not special to the shell.

Andreas.

Signature · 3 lines
Andreas Schwab, [email protected]
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#7 lore raw

Re: [PATCH v2] log-tree: use custom line terminator in line termination mode

Quoted 11 lines · Andreas Schwab
Quoted 6 lines · Junio C Hamano

Having said that, are we sure that

printf "add bar\0initial"

works per specification, or merely works by accident in some implementation?

Since the backslash is not followed by $ ` " \ <newline> it is not special to the shell.

Yeah, but I wasn't worried about what shell does in the first place. I was worried about what printf(1) does.

#15 lore raw
In reply to Randal L. Schwartz
Quoted 9 lines · [email protected] (Randal L. Schwartz) writes:
$ git log -z --format='%cE' -5 | od -c
0000000    g   i   t   s   t   e   r   @   p   o   b   o   x   .   c   o
0000020    m  \n   g   i   t   s   t   e   r   @   p   o   b   o   x   .
0000040    c   o   m  \n   g   i   t   s   t   e   r   @   p   o   b   o
0000060    x   .   c   o   m  \n   g   i   t   s   t   e   r   @   p   o
0000100    b   o   x   .   c   o   m  \n   g   i   t   s   t   e   r   @
0000120    p   o   b   o   x   .   c   o   m  \n                        
0000132
Quoted 3 lines

Why are all those newlines in there? Bug? Misfeature? Feature? If feature, how do I ensure \0 in my output? If I add %x00, I get both \0 *and* \n in output. :(

--format=format:%cE respects the -z option.

Andreas.

Signature · 3 lines
Andreas Schwab, [email protected]
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#16 lore raw
Quoted 11 lines · Andreas Schwab
Quoted 9 lines · [email protected] (Randal L. Schwartz) writes:
$ git log -z --format='%cE' -5 | od -c
0000000    g   i   t   s   t   e   r   @   p   o   b   o   x   .   c   o
0000020    m  \n   g   i   t   s   t   e   r   @   p   o   b   o   x   .
0000040    c   o   m  \n   g   i   t   s   t   e   r   @   p   o   b   o
0000060    x   .   c   o   m  \n   g   i   t   s   t   e   r   @   p   o
0000100    b   o   x   .   c   o   m  \n   g   i   t   s   t   e   r   @
0000120    p   o   b   o   x   .   c   o   m  \n                        
0000132
Quoted 5 lines
Quoted 3 lines

Why are all those newlines in there? Bug? Misfeature? Feature? If feature, how do I ensure \0 in my output? If I add %x00, I get both \0 *and* \n in output. :(

--format=format:%cE respects the -z option.

The underlying problem is apparently that --format=%cE triggers the format-guessing logic, which assumes you meant --pretty=tformat:%cE instead of --pretty=format:%cE.

It's probably a bug that --pretty=tformat:%cE does not use \0 here. After all the manual states

· tformat:

The tformat: format works exactly like format:, except that it provides "terminator" semantics instead of "separator" semantics.

Fixing it may be as easy as the patch below, but I haven't spent much time on it.

log-tree.c +1 -1
index 34c49e7..44f0268 100644
@@ -682,7 +682,7 @@ void show_log(struct rev_info *opt)
 	if (opt->use_terminator) {
 		if (!opt->missing_newline)
 			graph_show_padding(opt->graph);
-		putchar('\n');
+		putchar(opt->diffopt.line_termination);
 	}
 
 	strbuf_release(&msgbuf);
Signature · 2 lines
Thomas Rast
trast@{inf,student}.ethz.ch
#8 lore raw

Re: [PATCH v2] log-tree: use custom line terminator in line termination mode

In reply to Junio C Hamano
Quoted 8 lines · Junio C Hamano
Quoted 6 lines · Andreas Schwab
Quoted 4 lines · Junio C Hamano

Having said that, are we sure that

printf "add bar\0initial"

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

works per specification, or merely works by accident in some implementation?

Since the backslash is not followed by $ ` " \ <newline> it is not special to the shell.

Yeah, but I wasn't worried about what shell does in the first place. I was worried about what printf(1) does.

Oh, I was confused becaused you mentioned the C case where the \NNN interpretation is done by the compiler, not printf(3).

Andreas.

Signature · 3 lines
Andreas Schwab, [email protected]
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#9 lore raw

Re: [PATCH v2] log-tree: use custom line terminator in line termination mode

In reply to Junio C Hamano
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Junio" == Junio C Hamano <[email protected]> writes:

Junio> Having said that, are we sure that

Junio> printf "add bar\0initial"

Junio> works per specification, or merely works by accident in some Junio> implementation?

Quoted 1 line

From the POSIX spec

(http://pubs.opengroup.org/onlinepubs/9699919799/utilities/printf.html):

In addition to the escape sequences shown in XBD File Format Notation ( '\\' , '\a' , '\b' , '\f' , '\n' , '\r' , '\t' , '\v' ), "\ddd" , where ddd is a one, two, or three-digit octal number, shall be written as a byte with the numeric value specified by the octal number.

Looks pretty intentional to me. \0 is a nul.

Signature · 4 lines
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[email protected]> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion
#10 lore raw

Re: [PATCH v2] log-tree: use custom line terminator in line termination mode

Quoted 13 lines · [email protected] (Randal L. Schwartz) writes:
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Junio" == Junio C Hamano <[email protected]> writes:

Junio> Having said that, are we sure that

Junio> printf "add bar\0initial"

Junio> works per specification, or merely works by accident in some Junio> implementation?

From the POSIX spec
(http://pubs.opengroup.org/onlinepubs/9699919799/utilities/printf.html):

Quoted 6 lines

In addition to the escape sequences shown in XBD File Format Notation ( '\\' , '\a' , '\b' , '\f' , '\n' , '\r' , '\t' , '\v' ), "\ddd" , where ddd is a one, two, or three-digit octal number, shall be written as a byte with the numeric value specified by the octal number.

Looks pretty intentional to me. \0 is a nul.

I was staring at the same passage, but somehow it didn't "click" for me that the above is _not_ saying that the NUL would terminate the string.

So I was worried too much and needlessly. Thanks.

#11 lore raw

Re: [PATCH] log-tree: use custom line terminator in line termination mode

In reply to Junio C Hamano
Quoted 25 lines · Junio C Hamano
Quoted 23 lines · Jan Krüger

When using a custom format in line termination mode (as opposed to line separation mode), the configured line terminator is not used, so things like "git log --pretty=tformat:%H -z" do not work properly.

Make it use the line terminator the user ordered.

---
 log-tree.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
log-tree.c +1 -1
index 34c49e7..44f0268 100644
@@ -682,7 +682,7 @@ void show_log(struct rev_info *opt)
 	if (opt->use_terminator) {
 		if (!opt->missing_newline)
 			graph_show_padding(opt->graph);
-		putchar('\n');
+		putchar(opt->diffopt.line_termination);
 	}
 
Quoted 3 lines

log-tree.c void show_log(struct rev_info *opt) · line 688

Quoted 1 line
log-tree.c +0 -0
 	strbuf_release(&msgbuf);

Looks sensible. Perhaps we would want to add a test?

Hmm. This came up before, and the issue is (or can be) slightly more complex:

http://thread.gmane.org/gmane.comp.version-control.git/122478/focus=122568

-Peff

#12 lore raw

Re: [PATCH] log-tree: use custom line terminator in line termination mode

Quoted 27 lines · Jeff King
Quoted 25 lines · Junio C Hamano
Quoted 23 lines · Jan Krüger

When using a custom format in line termination mode (as opposed to line separation mode), the configured line terminator is not used, so things like "git log --pretty=tformat:%H -z" do not work properly.

Make it use the line terminator the user ordered.

---
 log-tree.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
log-tree.c +1 -1
index 34c49e7..44f0268 100644
@@ -682,7 +682,7 @@ void show_log(struct rev_info *opt)
 	if (opt->use_terminator) {
 		if (!opt->missing_newline)
 			graph_show_padding(opt->graph);
-		putchar('\n');
+		putchar(opt->diffopt.line_termination);
 	}
 
Quoted 8 lines
Quoted 3 lines

log-tree.c void show_log(struct rev_info *opt) · line 688

Quoted 1 line
log-tree.c +0 -0
 	strbuf_release(&msgbuf);

Looks sensible. Perhaps we would want to add a test?

Hmm. This came up before, and the issue is (or can be) slightly more complex:

http://thread.gmane.org/gmane.comp.version-control.git/122478/focus=122568

Yeah, the test given completely forgets about "log -p" case, as you said in the above:

But we can't just modify that to use the specified line terminator, because sometimes it is acting as a separator between commit message and diff, and sometimes it is acting as the terminator of the whole record.

So the patch is not quite right for the "log -p -z" (or "log --stat -z") case.

The correct output would have NUL after each commit, so "-z --format=%s" would have a single-liner subject with the line-terminating LF replaced with NUL, and "-p/--stat -z --format=%s" would have a single-liner subject with its line-terminating LF, followed by the diff/diffstat in which the terminating LF of the last line is replaced with NUL, but to be consistent with what "-p/--stat -z --pretty=format:%s" does, I think it is OK to append NUL to the diff/diffstat part instead of replacing its last LF with NUL.

#13 lore raw

Re: [PATCH] log-tree: use custom line terminator in line termination mode

Quoted 17 lines · Junio C Hamano
Quoted 4 lines · Jeff King

Hmm. This came up before, and the issue is (or can be) slightly more complex:

http://thread.gmane.org/gmane.comp.version-control.git/122478/focus=122568

Yeah, the test given completely forgets about "log -p" case, as you said in the above:

But we can't just modify that to use the specified line terminator, because sometimes it is acting as a separator between commit message and diff, and sometimes it is acting as the terminator of the whole record.

So the patch is not quite right for the "log -p -z" (or "log --stat -z") case.

Quoted 8 lines

The correct output would have NUL after each commit, so "-z --format=%s" would have a single-liner subject with the line-terminating LF replaced with NUL, and "-p/--stat -z --format=%s" would have a single-liner subject with its line-terminating LF, followed by the diff/diffstat in which the terminating LF of the last line is replaced with NUL, but to be consistent with what "-p/--stat -z --pretty=format:%s" does, I think it is OK to append NUL to the diff/diffstat part instead of replacing its last LF with NUL.

In other words, this test on top (the last one only demonstrates the breakage).

 t/t4205-log-pretty-formats.sh |   16 ++++++++++++++++
 1 file changed, 16 insertions(+)
t/t4205-log-pretty-formats.sh +16 -0
index f00e446..4afd778 100755
@@ -83,4 +83,20 @@ test_expect_success 'NUL termination' '
 	test_cmp expected actual
 '
 
+test_expect_success 'NUL separation with --stat' '
+	stat0_part=$(git diff --stat HEAD^ HEAD) &&
+	stat1_part=$(git diff --stat --root HEAD^) &&
+	printf "add bar\n$stat0_part\n\0initial\n$stat1_part\n" >expected &&
+	git log -z --stat --pretty="format:%s" >actual &&
+	test_cmp expected actual
+'
+
+test_expect_failure 'NUL termination with --stat' '
+	stat0_part=$(git diff --stat HEAD^ HEAD) &&
+	stat1_part=$(git diff --stat --root HEAD^) &&
+	printf "add bar\n$stat0_part\n\0initial\n$stat1_part\n\0" >expected &&
+	git log -z --stat --pretty="tformat:%s" >actual &&
+	test_cmp expected actual
+'
+
 test_done
#14 lore raw

Re: [PATCH] log-tree: use custom line terminator in line termination mode

Quoted 11 lines · Junio C Hamano
Quoted 8 lines

The correct output would have NUL after each commit, so "-z --format=%s" would have a single-liner subject with the line-terminating LF replaced with NUL, and "-p/--stat -z --format=%s" would have a single-liner subject with its line-terminating LF, followed by the diff/diffstat in which the terminating LF of the last line is replaced with NUL, but to be consistent with what "-p/--stat -z --pretty=format:%s" does, I think it is OK to append NUL to the diff/diffstat part instead of replacing its last LF with NUL.

In other words, this test on top (the last one only demonstrates the breakage).

Just a short hint if anybody wants to take a stab at it while I am deeply in today's integration cycle.

The code for "separator" semantics was a well tested code when "terminator" semantics was bolted on, and operated like this:

for each record
do
if we have shown a record already then
show the termination character
fi
show the record
done

The only difference between the two semantics is if we append the termination character after all the above is done, so the code should be structured that way, but that may not be how we currently do it. So the proper way to add the "terminator" semantics ought to be:

if we have shown any record in the loop && opt->use_terminator then
show the termination character
fi

at the end of the above loop. If we see opt->use_terminator anywhere else in the existing code, it is an indication of a bug.

There is one small glitch. If a record is _not_ terminated with a newline, e.g. "log --pretty=format:%s" without --stat/-p, "separator" semantics will end up giving an incomplete line at the end. Most of the time we will be piping out output to the pager so it may not be a problem in the real life, but it would be nicer to also terminate such an output.

So the resulting logic should look something like:

for each record
do
if we have shown a record already then
show the termination character
fi
show the record
remember if the record ended with a LF
done
if we have shown any record in the loop &&
(opt->use_terminator ||
(opt->diffopt.line_termination == '\n' &&
the last record did not end with a LF))
then
show the termination character
fi