Skip to contentSkip to conversation

Fix a couple of edge cases in autostash

PATCH In review Ramkumar Ramachandra 3 participants 7 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

cover Fix a couple of edge cases in autostash

Hi,

I apologize for having missed these two trivial cases in the original series.

Ramkumar Ramachandra (3):
rebase: guard against missing files in read_basic_state()
rebase: finish_rebase() in fast-forward rebase
rebase: finish_rebase() in noop rebase

 git-rebase.sh               |  4 ++++
 t/t3420-rebase-autostash.sh | 22 ++++++++++++++++++++++
 2 files changed, 26 insertions(+)
Signature · 1 line
1.8.3.1.381.gf08dd97.dirty
#1 lore raw

PATCH 1/3 rebase: guard against missing files in read_basic_state()

---
 git-rebase.sh | 2 ++
 1 file changed, 2 insertions(+)
git-rebase.sh +2 -0
index d0c11a9..2122fe0 100755
@@ -84,6 +84,8 @@ keep_empty=
 test "$(git config --bool rebase.autosquash)" = "true" && autosquash=t
 
 read_basic_state () {
+	test -f "$state_dir/head-name" &&
+	test -f "$state_dir/onto" &&
 	head_name=$(cat "$state_dir"/head-name) &&
 	onto=$(cat "$state_dir"/onto) &&
 	# We always write to orig-head, but interactive rebase used to write to
Signature · 1 line
1.8.3.1.381.gf08dd97.dirty
#2 lore raw

Re: [PATCH 1/3] rebase: guard against missing files in read_basic_state()

Quoted 1 line · Ramkumar Ramachandra

This does not affect correctness; i.e. head_name=$(cat that-file) will error out if the file is missing, right?

A more troublesome is that nobody seems to check the return value of this function. If head-name, onto or orig-head is missing, is that an error condition that should make the callers of read_basic_state stop and refuse to proceed?

The way the && cascade is used seems to indicate that, but up to the point where it sents $verbose. If and only if head-name, onto, orig-head and quiet can be read in state-dir, verbose in state-dir is checked and only then $verbose is set.

Martin, this seems to be from your series around early Feburary 2011. Do you recall why these checks are cascaded this way? I do not offhand think of a good reason.

Quoted 17 lines
---
 git-rebase.sh | 2 ++
 1 file changed, 2 insertions(+)
git-rebase.sh +2 -0
index d0c11a9..2122fe0 100755
@@ -84,6 +84,8 @@ keep_empty=
 test "$(git config --bool rebase.autosquash)" = "true" && autosquash=t
 
 read_basic_state () {
+	test -f "$state_dir/head-name" &&
+	test -f "$state_dir/onto" &&
 	head_name=$(cat "$state_dir"/head-name) &&
 	onto=$(cat "$state_dir"/onto) &&
 	# We always write to orig-head, but interactive rebase used to write to
#3 lore raw

Re: [PATCH 1/3] rebase: guard against missing files in read_basic_state()

Quoted 6 lines · Junio C Hamano

Ramkumar Ramachandra <[email protected]> writes:

A more troublesome is that nobody seems to check the return value of this function. If head-name, onto or orig-head is missing, is that an error condition that should make the callers of read_basic_state stop and refuse to proceed?

Since we unconditionally write those three (and 'quiet'), it seems reasonable to require all of them to be there when continuing, so I think you're right that we should fail fast.

Quoted 8 lines

The way the && cascade is used seems to indicate that, but up to the point where it sents $verbose. If and only if head-name, onto, orig-head and quiet can be read in state-dir, verbose in state-dir is checked and only then $verbose is set.

Martin, this seems to be from your series around early Feburary 2011. Do you recall why these checks are cascaded this way? I do not offhand think of a good reason.

Neither do I. I think the cascading after 'quiet' is just a mistake on my part. The consequences are probably close to none, since if one of earlier commands fail, the other files will probably not be there either. (Not defending it; I'm happy if it gets fixed, e.g. by making it fail fast.)

#4 lore raw

Re: [PATCH 1/3] rebase: guard against missing files in read_basic_state()

Quoted 21 lines · Martin von Zweigbergk
Quoted 6 lines · Junio C Hamano

Ramkumar Ramachandra <[email protected]> writes:

A more troublesome is that nobody seems to check the return value of this function. If head-name, onto or orig-head is missing, is that an error condition that should make the callers of read_basic_state stop and refuse to proceed?

Since we unconditionally write those three (and 'quiet'), it seems reasonable to require all of them to be there when continuing, so I think you're right that we should fail fast.

Quoted 8 lines

The way the && cascade is used seems to indicate that, but up to the point where it sents $verbose. If and only if head-name, onto, orig-head and quiet can be read in state-dir, verbose in state-dir is checked and only then $verbose is set.

Martin, this seems to be from your series around early Feburary 2011. Do you recall why these checks are cascaded this way? I do not offhand think of a good reason.

Quoted 5 lines

Neither do I. I think the cascading after 'quiet' is just a mistake on my part. The consequences are probably close to none, since if one of earlier commands fail, the other files will probably not be there either. (Not defending it; I'm happy if it gets fixed, e.g. by making it fail fast.)

I think this is probably the right thing to do, if we want to honor the original intention of the earlier part of && cascade. Everything before this new "|| die" reads from a file that should always exist (e.g. even when not asked to be quiet, that state is not signaled by the lack of $state_dir/quiet, but by having an empty string in it), while everything after check optional state variable files (e.g. if $state_dir/verbose does not exist, it is not an error, but signals that the user did not ask us to be verbose).

Note that applying this patch _could_ uncover latent bug that was masked by the lack of "die" here (maybe later codepath may depended on not having orig_head at all and the only observable effect was that in such a case, both quiet and verbose were silently ignored, because the control did not reach the GIT_QUIET=... and verbose=t assignments.

 git-rebase.sh | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
git-rebase.sh +3 -1
index d0c11a9..90506ba 100755
@@ -95,7 +95,9 @@ read_basic_state () {
 	else
 		orig_head=$(cat "$state_dir"/head)
 	fi &&
-	GIT_QUIET=$(cat "$state_dir"/quiet) &&
+	GIT_QUIET=$(cat "$state_dir"/quiet) ||
+	die "failed to read basic rebase state from $state_dir"
+
 	test -f "$state_dir"/verbose && verbose=t
 	test -f "$state_dir"/strategy && strategy="$(cat "$state_dir"/strategy)"
 	test -f "$state_dir"/strategy_opts &&
#5 lore raw

PATCH 2/3 rebase: finish_rebase() in fast-forward rebase

In reply to Ramkumar Ramachandra

In the following case

  $ git rebase master
  Fast-forwarded autostash-fix to master.

The autostash is not applied automatically, because this codepath forgets to call finish_rebase(). Fix this. Also add a test to guard against regressions.

---
 git-rebase.sh               |  1 +
 t/t3420-rebase-autostash.sh | 11 +++++++++++
 2 files changed, 12 insertions(+)
git-rebase.sh +1 -0
index 2122fe0..154d4be 100755
@@ -579,6 +579,7 @@ if test "$mb" = "$orig_head"
 then
 	say "$(eval_gettext "Fast-forwarded \$branch_name to \$onto_name.")"
 	move_to_original_branch
+	finish_rebase
 	exit 0
 fi
 
t/t3420-rebase-autostash.sh +11 -0
index 479cbb2..1bde007 100755
@@ -141,6 +141,17 @@ testrebase() {
 	'
 }
 
+test_expect_success "rebase: fast-forward rebase" '
+	test_config rebase.autostash true &&
+	git reset --hard &&
+	git checkout -b behind-feature-branch feature-branch~1 &&
+	test_when_finished git branch -D behind-feature-branch &&
+	echo dirty >>file1 &&
+	git rebase feature-branch &&
+	grep dirty file1 &&
+	git checkout feature-branch
+'
+
 testrebase "" .git/rebase-apply
 testrebase " --merge" .git/rebase-merge
 testrebase " --interactive" .git/rebase-merge
Signature · 1 line
1.8.3.1.381.gf08dd97.dirty
#6 lore raw

PATCH 3/3 rebase: finish_rebase() in noop rebase

In reply to Ramkumar Ramachandra

In the following case

  $ git rebase master
  Current branch autostash-fix is up to date.

the autostash is not applied automatically, because this codepath forgets to call finish_rebase(). Fix this. Also add a test to guard against regressions.

---
 git-rebase.sh               |  1 +
 t/t3420-rebase-autostash.sh | 11 +++++++++++
 2 files changed, 12 insertions(+)
git-rebase.sh +1 -0
index 154d4be..2d5c2bd 100755
@@ -547,6 +547,7 @@ then
 		# Lazily switch to the target branch if needed...
 		test -z "$switch_to" || git checkout "$switch_to" --
 		say "$(eval_gettext "Current branch \$branch_name is up to date.")"
+		finish_rebase
 		exit 0
 	else
 		say "$(eval_gettext "Current branch \$branch_name is up to date, rebase forced.")"
t/t3420-rebase-autostash.sh +11 -0
index 1bde007..90eb264 100755
@@ -152,6 +152,17 @@ test_expect_success "rebase: fast-forward rebase" '
 	git checkout feature-branch
 '
 
+test_expect_success "rebase: noop rebase" '
+	test_config rebase.autostash true &&
+	git reset --hard &&
+	git checkout -b same-feature-branch feature-branch &&
+	test_when_finished git branch -D same-feature-branch &&
+	echo dirty >>file1 &&
+	git rebase feature-branch &&
+	grep dirty file1 &&
+	git checkout feature-branch
+'
+
 testrebase "" .git/rebase-apply
 testrebase " --merge" .git/rebase-merge
 testrebase " --interactive" .git/rebase-merge
Signature · 1 line
1.8.3.1.381.gf08dd97.dirty