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

7 messages from 2013-06-13 to 2013-06-20. Participants: Ramkumar Ramachandra, Junio C Hamano, Martin von Zweigbergk.
Thread: https://gitlist.dev/t/34127

## Ramkumar Ramachandra, 2013-06-13 16:06

Subject: [PATCH 0/3] Fix a couple of edge cases in autostash
Message-ID: <1371139573-28047-1-git-send-email-artagnon@gmail.com>
URL: https://gitlist.dev/e/1371139573-28047-1-git-send-email-artagnon%40gmail.com

```
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, 2013-06-13 16:06

Subject: [PATCH 1/3] rebase: guard against missing files in read_basic_state()
Message-ID: <1371139573-28047-2-git-send-email-artagnon@gmail.com>
URL: https://gitlist.dev/e/1371139573-28047-2-git-send-email-artagnon%40gmail.com
In-Reply-To: <1371139573-28047-1-git-send-email-artagnon@gmail.com>

```
Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
---
 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
-- 
1.8.3.1.381.gf08dd97.dirty

```

## Ramkumar Ramachandra, 2013-06-13 16:06

Subject: [PATCH 2/3] rebase: finish_rebase() in fast-forward rebase
Message-ID: <1371139573-28047-3-git-send-email-artagnon@gmail.com>
URL: https://gitlist.dev/e/1371139573-28047-3-git-send-email-artagnon%40gmail.com
In-Reply-To: <1371139573-28047-1-git-send-email-artagnon@gmail.com>

```
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(+)

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, 2013-06-13 16:06

Subject: [PATCH 3/3] rebase: finish_rebase() in noop rebase
Message-ID: <1371139573-28047-4-git-send-email-artagnon@gmail.com>
URL: https://gitlist.dev/e/1371139573-28047-4-git-send-email-artagnon%40gmail.com
In-Reply-To: <1371139573-28047-1-git-send-email-artagnon@gmail.com>

```
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(+)

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

```

## Junio C Hamano, 2013-06-13 22:29

Subject: Re: [PATCH 1/3] rebase: guard against missing files in read_basic_state()
Message-ID: <7vy5adskz3.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vy5adskz3.fsf%40alter.siamese.dyndns.org
In-Reply-To: <1371139573-28047-2-git-send-email-artagnon@gmail.com>

```
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.

> ---
>  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, 2013-06-16 05:45

Subject: Re: [PATCH 1/3] rebase: guard against missing files in read_basic_state()
Message-ID: <CANiSa6hUV-xMMASbJf67j0Zn+Mu25CRcvQThNd1EuemhSBB6Rg@mail.gmail.com>
URL: https://gitlist.dev/e/CANiSa6hUV-xMMASbJf67j0Zn%2BMu25CRcvQThNd1EuemhSBB6Rg%40mail.gmail.com
In-Reply-To: <7vy5adskz3.fsf@alter.siamese.dyndns.org>

```
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.)

```

## Junio C Hamano, 2013-06-20 19:56

Subject: Re: [PATCH 1/3] rebase: guard against missing files in read_basic_state()
Message-ID: <7v1u7wtv23.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v1u7wtv23.fsf%40alter.siamese.dyndns.org
In-Reply-To: <CANiSa6hUV-xMMASbJf67j0Zn+Mu25CRcvQThNd1EuemhSBB6Rg@mail.gmail.com>

```
Martin von Zweigbergk <martinvonz@gmail.com> writes:

> 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(-)

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 &&

```
