Skip to contentSkip to conversation

rev-parse --show-prefix: add in trailing newline

PATCH In review Ross Lagerwall 5 participants 9 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rev-parse --show-prefix: add in trailing newline

Print out a trailing newline when --show-prefix is run with cwd at the top level of the tree which results in an empty prefix. Behavior is now like --show-cdup.

Fixes an expected failure in t1501.

---
 builtin/rev-parse.c |    2 ++
 t/t1501-worktree.sh |    2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
builtin/rev-parse.c +2 -0
index 98d1cbe..733f626 100644
@@ -634,6 +634,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 			if (!strcmp(arg, "--show-prefix")) {
 				if (prefix)
 					puts(prefix);
+				else
+					putchar('\n');
 				continue;
 			}
 			if (!strcmp(arg, "--show-cdup")) {
t/t1501-worktree.sh +1 -1
index e661147..8f36aa9 100755
@@ -68,7 +68,7 @@ test_expect_success 'inside work tree' '
 	)
 '
 
-test_expect_failure 'empty prefix is actually written out' '
+test_expect_success 'empty prefix is actually written out' '
 	echo >expected &&
 	(
 		cd work &&
Signature · 1 line
1.7.10
#1 lore raw

(cc-ing Duy)
Hi,

builtin/rev-parse.c int cmd_rev_parse(int argc, const char **argv, const char *prefix) · line 639

Quoted 9 lines · Ross Lagerwall
builtin/rev-parse.c +2 -0
@@ -634,6 +634,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 			if (!strcmp(arg, "--show-prefix")) {
 				if (prefix)
 					puts(prefix);
+				else
+					putchar('\n');
 				continue;

This makes the output more consistent but it is a little puzzling how it does that. Why is prefix NULL instead of "" when we are at the toplevel of the worktree?

Thanks,
Jonathan

#2 lore raw
Quoted 5 lines · Jonathan Nieder

(cc-ing Duy)
Hi,

Ross Lagerwall wrote:

Quoted 13 lines

builtin/rev-parse.c int cmd_rev_parse(int argc, const char **argv, const char *prefix) · line 639

Quoted 9 lines
builtin/rev-parse.c +2 -0
@@ -634,6 +634,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 			if (!strcmp(arg, "--show-prefix")) {
 				if (prefix)
 					puts(prefix);
+				else
+					putchar('\n');
 				continue;

This makes the output more consistent but it is a little puzzling how it does that. Why is prefix NULL instead of "" when we are at the toplevel of the worktree?

Interesting point ;-)

#3 lore raw
In reply to Jonathan Nieder

2012/4/11 Jonathan Nieder <[email protected]>:

Quoted 5 lines

(cc-ing Duy)
Hi,

Ross Lagerwall wrote:

Quoted 13 lines

builtin/rev-parse.c int cmd_rev_parse(int argc, const char **argv, const char *prefix) · line 639

Quoted 9 lines
builtin/rev-parse.c +2 -0
@@ -634,6 +634,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                       if (!strcmp(arg, "--show-prefix")) {
                               if (prefix)
                                       puts(prefix);
+                             else
+                                     putchar('\n');
                               continue;

This makes the output more consistent but it is a little puzzling how it does that.  Why is prefix NULL instead of "" when we are at the toplevel of the worktree?

It dates back to the first version of setup_git_directory() in d288a70 ([PATCH] Make "git diff" work inside relative subdirectories - 2005-08-16). If we get "", setup_git_.. converts it to NULL. Although I think Linus made a mistake in the first version of "rev-parse --git-dir" doing "puts(prefix);" unconditionally in the same commit. Junio fixed --git-dir in 4866ccf (Rationalize output selection in rev-parse. - 2005-08-24) with the "if (prefix)" clause and should have added the "else putchar('\n');" there too, I think.

Signature · 1 line
Duy
#4 lore raw
In reply to Ross Lagerwall

t/t1501-worktree.sh test_expect_success 'inside work tree' ' · line 74

Quoted 13 lines · Ross Lagerwall
t/t1501-worktree.sh +1 -1
index e661147..8f36aa9 100755
@@ -68,7 +68,7 @@ test_expect_success 'inside work tree' '
 	)
 '
 
-test_expect_failure 'empty prefix is actually written out' '
+test_expect_success 'empty prefix is actually written out' '
 	echo >expected &&
 	(
 		cd work &&

fc68875 (t1501 (rev-parse): clarify, 2010-07-24) says it is a "possible bug", but I do not think the expectation of the new test added by that commit is entirely correct.

Because the assignment will strip the trailing LF away adding an extra newline at the end would not matter in practice in many scripts, e.g.

	a=$(git rev-parse --show-prefix)
        if test -z "$a"
        then
		echo we are at toplevel
	fi

or

	pfx=$(git rev-parse --show-prefix)
	cd_to_toplevel
	for arg
        do
		case "$arg" in
                /*)
                       	user refers to "$arg" that is a full path ;;
		*)
			user refers to "$pfx$arg" ;;
		esac
	done

But it will break existing scripts if they expect the following to work:

	file="$(git rev-parse --show-prefix)/$1"
        cd_to_toplevel
        test -f "$file"
#5 lore raw
Quoted 5 lines · Junio C Hamano

But it will break existing scripts if they expect the following to work:

	file="$(git rev-parse --show-prefix)/$1"
        cd_to_toplevel
        test -f "$file"

Huh? I tried this:

	$ foo="$(printf '\n')/bar"
	$ printf '%s\n' "$foo"
	/bar

If that didn't work, it would mean that "git rev-parse --show-prefix" would never work when the prefix is nonempty, because it is followed by a newline.

#6 lore raw
In reply to Junio C Hamano
Quoted 7 lines · Junio C Hamano

fc68875 (t1501 (rev-parse): clarify, 2010-07-24) says it is a "possible bug", but I do not think the expectation of the new test added by that commit is entirely correct.

Because the assignment will strip the trailing LF away adding an extra newline at the end would not matter in practice in many scripts, e.g.

True, and the current behavior does exactly what the man page says:
"When the command is invoked from a subdirectory, show the path of the current directory relative to the top-level directory."

So then perhaps the test should be removed?

Also, --show-cdup has a similar description for the man page but does output a newline when invoked from the root dir. Should this behavior be made consistent?

Signature · 1 line
Ross Lagerwall
#7 lore raw
In reply to Junio C Hamano
Quoted 1 line · Junio C Hamano

Because the assignment will strip the trailing LF away

No, it doesn't. Command substitution does.

Quoted 5 lines

But it will break existing scripts if they expect the following to work:

	file="$(git rev-parse --show-prefix)/$1"
        cd_to_toplevel
        test -f "$file"

This is already broken because of the extra slash.

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."
#8 lore raw
Quoted 1 line · Andreas Schwab

This is already broken because of the extra slash.

Yeah, you are right.