Skip to contentSkip to conversation

stash: use --quiet rather than using 2>/dev/null

PATCH In review Benjamin C Meyer 3 participants 9 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 stash: use --quiet rather than using 2>/dev/null

---
 git-stash.sh |   12 ++++++------
 1 files changed, 6 insertions(+), 6 deletions(-)
git-stash.sh +6 -6
index aa47e54..2533185 100755
@@ -39,7 +39,7 @@ clear_stash () {
 	then
 		die "git stash clear with parameters is unimplemented"
 	fi
-	if current=$(git rev-parse --verify $ref_stash 2>/dev/null)
+	if current=$(git rev-parse --quiet --verify $ref_stash)
 	then
 		git update-ref -d $ref_stash $current
 	fi
@@ -201,7 +201,7 @@ save_stash () {
 }
 
 have_stash () {
-	git rev-parse --verify $ref_stash >/dev/null 2>&1
+	git rev-parse --quiet --verify $ref_stash >/dev/null
 }
 
 list_stash () {
@@ -342,16 +342,16 @@ drop_stash () {
 	fi
 	# Verify supplied argument looks like a stash entry
 	s=$(git rev-parse --verify "$@") &&
-	git rev-parse --verify "$s:"   > /dev/null 2>&1 &&
-	git rev-parse --verify "$s^1:" > /dev/null 2>&1 &&
-	git rev-parse --verify "$s^2:" > /dev/null 2>&1 ||
+	git rev-parse --quiet --verify "$s:"   > /dev/null &&
+	git rev-parse --quiet --verify "$s^1:" > /dev/null &&
+	git rev-parse --quiet --verify "$s^2:" > /dev/null ||
 		die "$*: not a valid stashed state"
 
 	git reflog delete --updateref --rewrite "$@" &&
 		say "Dropped $* ($s)" || die "$*: Could not drop stash entry"
 
 	# clear_stash if we just dropped the last stash entry
-	git rev-parse --verify "$ref_stash@{0}" > /dev/null 2>&1 || clear_stash
+	git rev-parse --quiet --verify "$ref_stash@{0}" > /dev/null || clear_stash
 }
 
 apply_to_branch () {
Signature · 1 line
1.7.0.2
#1 lore raw

PATCH 2/4 pull: use --quiet rather than 2>/dev/null

---
 git-pull.sh |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
git-pull.sh +1 -1
index d45b50c..08dac0a 100755
@@ -206,7 +206,7 @@ test true = "$rebase" && {
 	. git-parse-remote &&
 	remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
 	oldremoteref="$(git rev-parse -q --verify "$remoteref")" &&
-	for reflog in $(git rev-list -g $remoteref 2>/dev/null)
+	for reflog in $(git rev-list --quiet -g $remoteref)
 	do
 		if test "$reflog" = "$(git merge-base $reflog $curr_branch)"
 		then
Signature · 1 line
1.7.0.2
#2 lore raw

Re: [PATCH 2/4] pull: use --quiet rather than 2>/dev/null

git-pull.sh test true = "$rebase" && { · line 209

Quoted 2 lines · Benjamin C Meyer
git-pull.sh +1 -1
-	for reflog in $(git rev-list -g $remoteref 2>/dev/null)
+	for reflog in $(git rev-list --quiet -g $remoteref)

Are you sure? My local copy of git-rev-list.1 says

--quiet
Don’t print anything to standard output. This form is primarily meant to allow the caller to test the exit status to see if a range of objects is fully connected (or not). It is faster than redirecting stdout to /dev/null as the output does not have to be formatted.

A similar question applies to the other patches in this series: are you sure they suppress the right output? (I haven’t checked, just asking.)

Aside: that for loop looks like it could be improved. Maybe it is worth factoring this into a separate function, something like:

old_upstream() {
	remoteref=$1 &&
	curr_branch=$2 &&

	oldremoteref="$(git rev-parse -q --verify "$remoteref")" &&
	{ git rev-list -g "$remoteref" 2>/dev/null || return $?; } |
	while read reflog
	do
		if test -z "$(git rev-list "$curr_branch".."$reflog" | head -n 1)"
		then
			printf "%s\n" "$reflog"
			return 0
		fi
	done &&
	printf "%s\n" "$oldremoteref"
}

In other words, we can avoid walking the whole reflog before starting to look for an ancestor for the current branch.

Jonathan

#3 lore raw

Re: [PATCH 2/4] pull: use --quiet rather than 2>/dev/null

Quoted 13 lines · Jonathan Nieder

git-pull.sh test true = "$rebase" && { · line 209

Quoted 2 lines · Benjamin C Meyer
git-pull.sh +1 -1
-	for reflog in $(git rev-list -g $remoteref 2>/dev/null)
+	for reflog in $(git rev-list --quiet -g $remoteref)

Are you sure? My local copy of git-rev-list.1 says

--quiet
Don’t print anything to standard output. This form is primarily meant to allow the caller to test the exit status to see if a range of objects is fully connected (or not). It is faster than redirecting stdout to /dev/null as the output does not have to be formatted.

Quoted 3 lines

A similar question applies to the other patches in this series: are you sure they suppress the right output? (I haven’t checked, just asking.)

Thanks for reviewing the change

Yah looks like I made a mistake there, rev-list --quiet suppresses everything and not just stderr. When going through this janitor task I noticed that the --quiet is very inconsistent among git commands. Some suppress error messages, some suppress all messages. This might be something to improve in the future as even though I kept referring to the documentation this error slipped in.

re-checking the other patches I think they are correct in their usage. I ran the matching tests, the rebase ones passed, 3903-stash.sh is already failing before my patch and t3904-stash-patch.sh continues passing with the patch. Other then running the tests in t/ is there anything I should do to verify patches?

Quoted 20 lines

Aside: that for loop looks like it could be improved. Maybe it is worth factoring this into a separate function, something like:

old_upstream() {
	remoteref=$1 &&
	curr_branch=$2 &&

	oldremoteref="$(git rev-parse -q --verify "$remoteref")" &&
	{ git rev-list -g "$remoteref" 2>/dev/null || return $?; } |
	while read reflog
	do
		if test -z "$(git rev-list "$curr_branch".."$reflog" | head -n 1)"
		then
			printf "%s\n" "$reflog"
			return 0
		fi
	done &&
	printf "%s\n" "$oldremoteref"
}
Quoted 2 lines

In other words, we can avoid walking the whole reflog before starting to look for an ancestor for the current branch.

That looks like it would speed up that bit of code, but I am still figuring out the internals of git so I can't really comment on if this is the best solution etc.

-Benjamin Meyer

#4 lore raw

Re: [PATCH 2/4] pull: use --quiet rather than 2>/dev/null

Hi again,

Quoted 2 lines · Benjamin Meyer

re-checking the other patches I think they are correct in their usage.

Thanks for checking.

Quoted 5 lines

I ran the matching tests, the rebase ones passed, 3903-stash.sh is already failing before my patch and t3904-stash-patch.sh continues passing with the patch. Other then running the tests in t/ is there anything I should do to verify patches?

Regarding testing: the best thing to do IMHO is to explain in the commit message what effect the patch will have. Then reviewers (including you) can read it and try it out to make sure what you say is true.

The test suite is not very good for checking that a patch does what it’s meant to do, but that’s okay, since it has a different purpose [1].

As an example of what I mean, here is your patch 1/4 again (untested, though). I didn’t add any tests since it would be kind of hard to provoke address space exhaustion at just the right moment.

[1] See http://thread.gmane.org/gmane.comp.version-control.git/142480/focus=142745

-- %< --
From: Benjamin C Meyer <[email protected]>
Subject: scripts: use rev-parse --verify -q rather than using 2>/dev/null

b1b3596 (2008-04-26) taught rev-parse --verify a --quiet option to suppress the “fatal: Needed a single revision” message when its argument is not a valid object. Use it; this simplifies scripts somewhat, and it allows other errors (such as address space exhaustion) to be reported properly.

Since --quiet does not suppress warnings about ambiguous ref names and inadequate reflogs, the redirection has been kept for call sites that might involve either of these phenomena.

---
t3903-stash.sh fails for me with your original patch. Maybe you forgot to run 'make' before running tests? I fixed it by putting back in the 2>&1 on the ... || clear_stash line.

 contrib/completion/git-completion.bash |    2 +-
 contrib/examples/git-checkout.sh       |   10 +++++-----
 contrib/examples/git-commit.sh         |    2 +-
 contrib/examples/git-fetch.sh          |    4 ++--
 contrib/examples/git-merge.sh          |   10 +++++-----
 contrib/examples/git-reset.sh          |    2 +-
 contrib/examples/git-revert.sh         |    4 ++--
 git-cvsimport.perl                     |    2 +-
 git-stash.sh                           |   13 +++++++------
 templates/hooks--pre-commit.sample     |    2 +-
 10 files changed, 26 insertions(+), 25 deletions(-)
contrib/completion/git-completion.bash +1 -1
index 733ac39..5b287e1 100755
@@ -151,7 +151,7 @@ __git_ps1 ()
 				fi
 			fi
 			if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ]; then
-			        git rev-parse --verify refs/stash >/dev/null 2>&1 && s="$"
+				git rev-parse --verify -q refs/stash >/dev/null && s="$"
 			fi
 
 			if [ -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" ]; then
contrib/examples/git-checkout.sh +5 -5
index 1a7689a..f735209 100755
@@ -16,7 +16,7 @@ SUBDIRECTORY_OK=Sometimes
 require_work_tree
 
 old_name=HEAD
-old=$(git rev-parse --verify $old_name 2>/dev/null)
+old=$(git rev-parse --verify -q $old_name)
 oldbranch=$(git symbolic-ref $old_name 2>/dev/null)
 new=
 new_name=
@@ -71,8 +71,8 @@ while test $# != 0; do
 done
 
 arg="$1"
-rev=$(git rev-parse --verify "$arg" 2>/dev/null)
-if rev=$(git rev-parse --verify "$rev^0" 2>/dev/null)
+rev=$(git rev-parse --verify -q "$arg" 2>/dev/null)
+if rev=$(git rev-parse --verify -q "$rev^0" 2>/dev/null)
 then
 	[ -z "$rev" ] && die "unknown flag $arg"
 	new_name="$arg"
@@ -83,7 +83,7 @@ then
 	fi
 	new="$rev"
 	shift
-elif rev=$(git rev-parse --verify "$rev^{tree}" 2>/dev/null)
+elif rev=$(git rev-parse --verify -q "$rev^{tree}" 2>/dev/null)
 then
 	# checking out selected paths from a tree-ish.
 	new="$rev"
@@ -151,7 +151,7 @@ else
 	# but switching branches.
 	if test '' != "$new"
 	then
-		git rev-parse --verify "$new^{commit}" >/dev/null 2>&1 ||
+		git rev-parse --verify -q "$new^{commit}" >/dev/null 2>&1 ||
 		die "Cannot switch branch to a non-commit."
 	fi
 fi
contrib/examples/git-commit.sh +1 -1
index 5c72f65..35626c5 100755
@@ -9,7 +9,7 @@ OPTIONS_SPEC=
 . git-sh-setup
 require_work_tree
 
-git rev-parse --verify HEAD >/dev/null 2>&1 || initial_commit=t
+git rev-parse --verify -q HEAD >/dev/null || initial_commit=t
 
 case "$0" in
 *status)
contrib/examples/git-fetch.sh +2 -2
index e44af2c..a35f997 100755
@@ -124,7 +124,7 @@ append_fetch_head () {
 # repository is always fine.
 if test -z "$update_head_ok" && test $(is_bare_repository) = false
 then
-	orig_head=$(git rev-parse --verify HEAD 2>/dev/null)
+	orig_head=$(git rev-parse --verify -q HEAD)
 fi
 
 # Allow --notags from remote.$1.tagopt
@@ -365,7 +365,7 @@ case "$orig_head" in
 '')
 	;;
 ?*)
-	curr_head=$(git rev-parse --verify HEAD 2>/dev/null)
+	curr_head=$(git rev-parse --verify -q HEAD)
 	if test "$curr_head" != "$orig_head"
 	then
 	    git update-ref \
contrib/examples/git-merge.sh +5 -5
index 8f617fc..5ff2999 100755
@@ -130,7 +130,7 @@ finish () {
 
 merge_name () {
 	remote="$1"
-	rh=$(git rev-parse --verify "$remote^0" 2>/dev/null) || return
+	rh=$(git rev-parse --verify -q "$remote^0" 2>/dev/null) || return
 	bh=$(git show-ref -s --verify "refs/heads/$remote" 2>/dev/null)
 	if test "$rh" = "$bh"
 	then
@@ -226,15 +226,15 @@ fi
 # have "-m" so it is an additional safety measure to check for it.
 
 if test -z "$have_message" &&
-	second_token=$(git rev-parse --verify "$2^0" 2>/dev/null) &&
-	head_commit=$(git rev-parse --verify "HEAD" 2>/dev/null) &&
+	second_token=$(git rev-parse --verify -q "$2^0" 2>/dev/null) &&
+	head_commit=$(git rev-parse --verify -q "HEAD") &&
 	test "$second_token" = "$head_commit"
 then
 	merge_msg="$1"
 	shift
 	head_arg="$1"
 	shift
-elif ! git rev-parse --verify HEAD >/dev/null 2>&1
+elif ! git rev-parse --verify -q HEAD >/dev/null
 then
 	# If the merged head is a valid one there is no reason to
 	# forbid "git merge" into a branch yet to be born.  We do
@@ -277,7 +277,7 @@ set_reflog_action "merge $*"
 remoteheads=
 for remote
 do
-	remotehead=$(git rev-parse --verify "$remote"^0 2>/dev/null) ||
+	remotehead=$(git rev-parse --verify -q "$remote"^0 2>/dev/null) ||
 	    die "$remote - not something we can merge"
 	remoteheads="${remoteheads}$remotehead "
 	eval GITHEAD_$remotehead='"$remote"'
contrib/examples/git-reset.sh +1 -1
index bafeb52..e09a9c4 100755
@@ -75,7 +75,7 @@ else
 fi
 
 # Any resets update HEAD to the head being switched to.
-if orig=$(git rev-parse --verify HEAD 2>/dev/null)
+if orig=$(git rev-parse --verify -q HEAD)
 then
 	echo "$orig" >"$GIT_DIR/ORIG_HEAD"
 else
contrib/examples/git-revert.sh +2 -2
index 49f0032..5672b4f 100755
@@ -78,9 +78,9 @@ esac
 rev=$(git-rev-parse --verify "$@") &&
 commit=$(git-rev-parse --verify "$rev^0") ||
 	die "Not a single commit $@"
-prev=$(git-rev-parse --verify "$commit^1" 2>/dev/null) ||
+prev=$(git rev-parse --verify -q "$commit^1") ||
 	die "Cannot run $me a root commit"
-git-rev-parse --verify "$commit^2" >/dev/null 2>&1 &&
+git rev-parse --verify -q "$commit^2" >/dev/null &&
 	die "Cannot run $me a multi-parent commit."
 
 encoding=$(git config i18n.commitencoding || echo UTF-8)
git-cvsimport.perl +1 -1
index 9e03eee..0b13d23 100755
@@ -573,7 +573,7 @@ sub is_sha1 {
 
 sub get_headref ($) {
 	my $name = shift;
-	my $r = `git rev-parse --verify '$name' 2>/dev/null`;
+	my $r = `git rev-parse --verify -q '$name' 2>/dev/null`;
 	return undef unless $? == 0;
 	chomp $r;
 	return $r;
git-stash.sh +7 -6
index aa47e54..f6bd623 100755
@@ -39,7 +39,7 @@ clear_stash () {
 	then
 		die "git stash clear with parameters is unimplemented"
 	fi
-	if current=$(git rev-parse --verify $ref_stash 2>/dev/null)
+	if current=$(git rev-parse --verify -q $ref_stash)
 	then
 		git update-ref -d $ref_stash $current
 	fi
@@ -201,7 +201,7 @@ save_stash () {
 }
 
 have_stash () {
-	git rev-parse --verify $ref_stash >/dev/null 2>&1
+	git rev-parse --verify -q $ref_stash >/dev/null
 }
 
 list_stash () {
@@ -342,16 +342,17 @@ drop_stash () {
 	fi
 	# Verify supplied argument looks like a stash entry
 	s=$(git rev-parse --verify "$@") &&
-	git rev-parse --verify "$s:"   > /dev/null 2>&1 &&
-	git rev-parse --verify "$s^1:" > /dev/null 2>&1 &&
-	git rev-parse --verify "$s^2:" > /dev/null 2>&1 ||
+	git rev-parse --verify -q "$s:"   > /dev/null &&
+	git rev-parse --verify -q "$s^1:" > /dev/null &&
+	git rev-parse --verify -q "$s^2:" > /dev/null ||
 		die "$*: not a valid stashed state"
 
 	git reflog delete --updateref --rewrite "$@" &&
 		say "Dropped $* ($s)" || die "$*: Could not drop stash entry"
 
 	# clear_stash if we just dropped the last stash entry
-	git rev-parse --verify "$ref_stash@{0}" > /dev/null 2>&1 || clear_stash
+	git rev-parse --verify -q "$ref_stash@{0}" > /dev/null 2>&1 ||
+		clear_stash
 }
 
 apply_to_branch () {
templates/hooks--pre-commit.sample +1 -1
index 439eefd..a1ebe75 100755
@@ -7,7 +7,7 @@
 #
 # To enable this hook, rename this file to "pre-commit".
 
-if git-rev-parse --verify HEAD >/dev/null 2>&1
+if git rev-parse --verify -q HEAD >/dev/null
 then
 	against=HEAD
 else
Signature · 1 line
1.7.0.2
#5 lore raw

Re: [PATCH 2/4] pull: use --quiet rather than 2>/dev/null

Quoted 19 lines · Jonathan Nieder

Hi again,

Quoted 2 lines · Benjamin Meyer

re-checking the other patches I think they are correct in their usage.

Thanks for checking.

Quoted 5 lines

I ran the matching tests, the rebase ones passed, 3903-stash.sh is already failing before my patch and t3904-stash-patch.sh continues passing with the patch. Other then running the tests in t/ is there anything I should do to verify patches?

Regarding testing: the best thing to do IMHO is to explain in the commit message what effect the patch will have. Then reviewers (including you) can read it and try it out to make sure what you say is true.

Quoted 6 lines

The test suite is not very good for checking that a patch does what it’s meant to do, but that’s okay, since it has a different purpose [1].

As an example of what I mean, here is your patch 1/4 again (untested, though). I didn’t add any tests since it would be kind of hard to provoke address space exhaustion at just the right moment.

[snip]

Thanks for the example. I will try to include a more full explanation for my changes in the future so it can be more easily reviewed.

-Benjamin Meyer

#6 lore raw

Re: [PATCH 2/4] pull: use --quiet rather than 2>/dev/null

Quoted 3 lines · Benjamin Meyer

Thanks for the example. I will try to include a more full explanation for my changes in the future so it can be more easily reviewed.

Thanks for your work! I got a kick out of reading
<http://meyerhome.net/icefox/git-achievements>, by the way.

Jonathan

#7 lore raw

PATCH 3/4 rebase: use --quiet rather than 2>/dev/null

In reply to Benjamin C Meyer
---
 git-rebase.sh |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
git-rebase.sh +1 -1
index fb4fef7..bf93019 100755
@@ -101,7 +101,7 @@ call_merge () {
 	cmt="$(cat "$dotest/cmt.$1")"
 	echo "$cmt" > "$dotest/current"
 	hd=$(git rev-parse --verify HEAD)
-	cmt_name=$(git symbolic-ref HEAD 2> /dev/null || echo HEAD)
+	cmt_name=$(git symbolic-ref --quiet HEAD || echo HEAD)
 	msgnum=$(cat "$dotest/msgnum")
 	end=$(cat "$dotest/end")
 	eval GITHEAD_$cmt='"${cmt_name##refs/heads/}~$(($end - $msgnum))"'
Signature · 1 line
1.7.0.2
#8 lore raw

PATCH 4/4 rebase-interactive: use --quiet rather than 2>/dev/null

In reply to Benjamin C Meyer
---
 git-rebase--interactive.sh |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
git-rebase--interactive.sh +1 -1
index 3e4fd14..6f6748c 100755
@@ -793,7 +793,7 @@ first and then run 'git rebase --continue' again."
 		mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
 
 		: > "$DOTEST"/interactive || die "Could not mark as interactive"
-		git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
+		git symbolic-ref --quiet HEAD > "$DOTEST"/head-name ||
 			echo "detached HEAD" > "$DOTEST"/head-name
 
 		echo $HEAD > "$DOTEST"/head
Signature · 1 line
1.7.0.2