threads / patch / 34127

patch, 3 partsrebase: guard against missing files in read_basic_state()

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

## tl;dr

7 messages between Jun 13, 2013 and Jun 20, 2013. Diffs are folded; open one to read it.

replies: 6people: 3as markdown or json

Ramkumar Ramachandra· Jun 13, 2013, 16:06 UTC · lore

[PATCH 0/3] 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(+)
-- 
1.8.3.1.381.gf08dd97.dirty
Ramkumar Ramachandra· Jun 13, 2013, 16:06 UTC · re: Ramkumar Ramachandra · lore
Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
---
 git-rebase.sh | 2 ++
 1 file changed, 2 insertions(+)
Show changes to git-rebase.sh +2 −0
diff --git a/git-rebase.sh b/git-rebase.sh
index d0c11a9..2122fe0 100755
--- a/git-rebase.sh
+++ b/git-rebase.sh
@@ -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
-- 
1.8.3.1.381.gf08dd97.dirty
Junio C Hamano· Jun 13, 2013, 22:29 UTC · re: Ramkumar Ramachandra · lore

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

Ramkumar Ramachandra <artagnon@gmail.com> writes:
> Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>

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.
Show 17 quoted lines
> ---
>  git-rebase.sh | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/git-rebase.sh b/git-rebase.sh
> index d0c11a9..2122fe0 100755
> --- a/git-rebase.sh
> +++ b/git-rebase.sh
> @@ -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
Martin von Zweigbergk· Jun 16, 2013, 05:45 UTC · re: Junio C Hamano · lore

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

On Thu, Jun 13, 2013 at 3:29 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 6 quoted lines
> Ramkumar Ramachandra <artagnon@gmail.com> 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.

Show 8 quoted 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.)

Junio C Hamano· Jun 20, 2013, 19:56 UTC · re: Martin von Zweigbergk · lore

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

Martin von Zweigbergk <martinvonz@gmail.com> writes:
Show 26 quoted lines
> On Thu, Jun 13, 2013 at 3:29 PM, Junio C Hamano <gitster@pobox.com> wrote:
>> Ramkumar Ramachandra <artagnon@gmail.com> 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.
>
>> 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.)

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(-)
Show changes to git-rebase.sh +3 −1
diff --git a/git-rebase.sh b/git-rebase.sh
index d0c11a9..90506ba 100755
--- a/git-rebase.sh
+++ b/git-rebase.sh
@@ -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 &&
Ramkumar Ramachandra· Jun 13, 2013, 16:06 UTC · re: Ramkumar Ramachandra · lore

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

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.

Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
---
 git-rebase.sh               |  1 +
 t/t3420-rebase-autostash.sh | 11 +++++++++++
 2 files changed, 12 insertions(+)
Show changes to 2 files +12 −0

git-rebase.sh, t/t3420-rebase-autostash.sh

diff --git a/git-rebase.sh b/git-rebase.sh
index 2122fe0..154d4be 100755
--- a/git-rebase.sh
+++ b/git-rebase.sh
@@ -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
 
diff --git a/t/t3420-rebase-autostash.sh b/t/t3420-rebase-autostash.sh
index 479cbb2..1bde007 100755
--- a/t/t3420-rebase-autostash.sh
+++ b/t/t3420-rebase-autostash.sh
@@ -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
-- 
1.8.3.1.381.gf08dd97.dirty
Ramkumar Ramachandra· Jun 13, 2013, 16:06 UTC · re: Ramkumar Ramachandra · lore

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

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.

Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
---
 git-rebase.sh               |  1 +
 t/t3420-rebase-autostash.sh | 11 +++++++++++
 2 files changed, 12 insertions(+)
Show changes to 2 files +12 −0

git-rebase.sh, t/t3420-rebase-autostash.sh

diff --git a/git-rebase.sh b/git-rebase.sh
index 154d4be..2d5c2bd 100755
--- a/git-rebase.sh
+++ b/git-rebase.sh
@@ -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.")"
diff --git a/t/t3420-rebase-autostash.sh b/t/t3420-rebase-autostash.sh
index 1bde007..90eb264 100755
--- a/t/t3420-rebase-autostash.sh
+++ b/t/t3420-rebase-autostash.sh
@@ -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
-- 
1.8.3.1.381.gf08dd97.dirty

← back to recent threads