threads / discuss / 35412

How to pre-empt git pull merge error?

Subject: How to pre-empt git pull merge error?

## tl;dr

7 messages between Nov 27, 2013 and Nov 28, 2013.

replies: 6people: 6as markdown or json

Pete Forman· Nov 27, 2013, 15:17 UTC · lore

I am looking for a way of detecting up front whether a git pull or git merge would fail. The sort of script I want to perform is to update a server.

    git fetch
    git okay
    stop server
    backup data
    git merge
    start server
Here git okay is a place holder for the command I am asking for.

If a file has been changed outside of a commit then git pull fails with the following error.

error: Your local changes to '...' would be overwritten by merge. Aborting. Please, commit your changes or stash them before you can merge.

I would like git okay to perform the pre-merge checks described in git merge and return non-zero status so that the script aborts before the server is stopped.

Possibilities I have looked for and not found include git merge --dry-run. My best line of thought is git status --porcelain | pre-merge-okay. That seems like a lot of work to make a pre-merge-okay that deals with things like benign untracked files.

I have also asked this question on Stack Overflow but received no answers.

http://stackoverflow.com/questions/20221383/
-- 
Pete Forman
Konstantin Khomoutov· Nov 27, 2013, 15:42 UTC · re: Pete Forman · lore

Re: How to pre-empt git pull merge error?

On Wed, 27 Nov 2013 15:17:27 +0000 Pete Forman <petef4+usenet@gmail.com> wrote:

Show 19 quoted lines
> I am looking for a way of detecting up front whether a git pull or git
> merge would fail. The sort of script I want to perform is to update a
> server.
> 
>     git fetch
>     git okay
>     stop server
>     backup data
>     git merge
>     start server
> 
> Here git okay is a place holder for the command I am asking for.
> 
> If a file has been changed outside of a commit then git pull fails
> with the following error.
> 
> error: Your local changes to '...' would be overwritten by merge.
> Aborting. Please, commit your changes or stash them before you can
> merge.
What's wrong with "git okay" being
if git merge whatever 2>/dev/null; then
  ... OK path
else
  ... "merge failed" path
fi
?
Matthieu Moy· Nov 27, 2013, 15:54 UTC · re: Konstantin Khomoutov · lore

Re: How to pre-empt git pull merge error?

Konstantin Khomoutov <flatworm@users.sourceforge.net> writes:
Show 30 quoted lines
> On Wed, 27 Nov 2013 15:17:27 +0000
> Pete Forman <petef4+usenet@gmail.com> wrote:
>
>> I am looking for a way of detecting up front whether a git pull or git
>> merge would fail. The sort of script I want to perform is to update a
>> server.
>> 
>>     git fetch
>>     git okay
>>     stop server
>>     backup data
>>     git merge
>>     start server
>> 
>> Here git okay is a place holder for the command I am asking for.
>> 
>> If a file has been changed outside of a commit then git pull fails
>> with the following error.
>> 
>> error: Your local changes to '...' would be overwritten by merge.
>> Aborting. Please, commit your changes or stash them before you can
>> merge.
>
> What's wrong with "git okay" being
>
> if git merge whatever 2>/dev/null; then
>   ... OK path
> else
>   ... "merge failed" path
> fi

The idea seems to be to stop the server before actually doing the merge (and avoid doing so if the merge is bound to fail).

I don't know a simple way to do the pre-merge check without actually doing the merge (other than patching git merge to add a --dry-run option), but you can do a pessimistic check by using the require_work_tree_exists shell function defined in git-sh-setup (copied below, but you can call it from a shell script after doing . "$(git --exec-path)/git-sh-setup"):

require_clean_work_tree () {
	git rev-parse --verify HEAD >/dev/null || exit 1
	git update-index -q --ignore-submodules --refresh
	err=0
	if ! git diff-files --quiet --ignore-submodules
	then
		echo >&2 "Cannot $1: You have unstaged changes."
		err=1
	fi
	if ! git diff-index --cached --quiet --ignore-submodules HEAD --
	then
		if [ $err = 0 ]
		then
		    echo >&2 "Cannot $1: Your index contains uncommitted changes."
		else
		    echo >&2 "Additionally, your index contains uncommitted changes."
		fi
		err=1
	fi
	if [ $err = 1 ]
	then
		test -n "$2" && echo >&2 "$2"
		exit 1
	fi
}

Additionally, you may want to check that the merge is a fast-forward (hence can't result in merge conflict), e.g. by checking that the current commit is the merge base between itself and the commit to merge (git merge-base HEAD $commit).

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/
Antoine Pelisse· Nov 27, 2013, 16:38 UTC · re: Matthieu Moy · lore

Re: How to pre-empt git pull merge error?

Show 17 quoted lines
>> On Wed, 27 Nov 2013 15:17:27 +0000
>> Pete Forman <petef4+usenet@gmail.com> wrote:
>>
>>> I am looking for a way of detecting up front whether a git pull or git
>>> merge would fail. The sort of script I want to perform is to update a
>>> server.
>>>
>>>     git fetch
>>>     git okay
>>>     stop server
>>>     backup data
>>>     git merge
>>>     start server
>>>
> I don't know a simple way to do the pre-merge check without actually
> doing the merge (other than patching git merge to add a --dry-run
> option)

Wouldn't that be a nice use-case for git-recursive-merge --index-only ($gmane/236753) ?

Thomas Rast· Nov 27, 2013, 19:06 UTC · re: Antoine Pelisse · lore

Re: How to pre-empt git pull merge error?

Antoine Pelisse <apelisse@gmail.com> writes:
Show 20 quoted lines
>>> On Wed, 27 Nov 2013 15:17:27 +0000
>>> Pete Forman <petef4+usenet@gmail.com> wrote:
>>>
>>>> I am looking for a way of detecting up front whether a git pull or git
>>>> merge would fail. The sort of script I want to perform is to update a
>>>> server.
>>>>
>>>>     git fetch
>>>>     git okay
>>>>     stop server
>>>>     backup data
>>>>     git merge
>>>>     start server
>>>>
>> I don't know a simple way to do the pre-merge check without actually
>> doing the merge (other than patching git merge to add a --dry-run
>> option)
>
> Wouldn't that be a nice use-case for git-recursive-merge --index-only
> ($gmane/236753) ?

Possibly, but most of the use-cases for merge --dry-run are better answered by the XY Problem question:

Can you step back and explain what the *underlying* goal is?

The above sounds a lot like a deployment script, and such scripts are almost always better served by using an actual deployment tool, or failing that, by using some form of checkout -f instead, to ensure that they get whatever they are supposed to deploy.

(Using a merge to update is really terrible in the face of non-fast-forward updates, especially when caused by rewriting history to not include some commits.)

-- 
Thomas Rast
tr@thomasrast.ch
Pete Forman· Nov 28, 2013, 08:25 UTC · re: Thomas Rast · lore

Re: How to pre-empt git pull merge error?

Thomas Rast <tr@thomasrast.ch> writes:
Show 36 quoted lines
> Antoine Pelisse <apelisse@gmail.com> writes:
>
>>>> On Wed, 27 Nov 2013 15:17:27 +0000
>>>> Pete Forman <petef4+usenet@gmail.com> wrote:
>>>>
>>>>> I am looking for a way of detecting up front whether a git pull or
>>>>> git merge would fail. The sort of script I want to perform is to
>>>>> update a server.
>>>>>
>>>>>     git fetch
>>>>>     git okay
>>>>>     stop server
>>>>>     backup data
>>>>>     git merge
>>>>>     start server
>>>>>
>>> I don't know a simple way to do the pre-merge check without actually
>>> doing the merge (other than patching git merge to add a --dry-run
>>> option)
>>
>> Wouldn't that be a nice use-case for git-recursive-merge --index-only
>> ($gmane/236753) ?
>
> Possibly, but most of the use-cases for merge --dry-run are better
> answered by the XY Problem question:
>
> Can you step back and explain what the *underlying* goal is?
>
> The above sounds a lot like a deployment script, and such scripts are
> almost always better served by using an actual deployment tool, or
> failing that, by using some form of checkout -f instead, to ensure
> that they get whatever they are supposed to deploy.
>
> (Using a merge to update is really terrible in the face of
> non-fast-forward updates, especially when caused by rewriting history
> to not include some commits.)

It is a deployment script and updates are fast-forward. There was a problem on a test server where a file had been hacked to investigate an issue. The next deploy failed with the merge error.

There are three approaches, which might all be done with git or an actual deployment tool.

1. test early, bail out if deploy would fail
2. set target to good state before applying the merge
2a. discard changes
2b. stash changes

I intend to use (1). First I will need to clean up the stray files or add more entries into .gitignore.

  test -z "$(git status --porcelain)"
-- 
Pete Forman
http://petef.22web.org/payg.html
Junio C Hamano· Nov 27, 2013, 20:09 UTC · re: Antoine Pelisse · lore

Re: How to pre-empt git pull merge error?

Antoine Pelisse <apelisse@gmail.com> writes:
Show 20 quoted lines
>>> On Wed, 27 Nov 2013 15:17:27 +0000
>>> Pete Forman <petef4+usenet@gmail.com> wrote:
>>>
>>>> I am looking for a way of detecting up front whether a git pull or git
>>>> merge would fail. The sort of script I want to perform is to update a
>>>> server.
>>>>
>>>>     git fetch
>>>>     git okay
>>>>     stop server
>>>>     backup data
>>>>     git merge
>>>>     start server
>>>>
>> I don't know a simple way to do the pre-merge check without actually
>> doing the merge (other than patching git merge to add a --dry-run
>> option)
>
> Wouldn't that be a nice use-case for git-recursive-merge --index-only
> ($gmane/236753) ?

As the original mentions "error: Your local changes to ...", I do not think it would be a good fit.

I have to say that the safest and sanest way may be to:
 (1) Commit any such local change(s);
     server$ git commit -a
 (2) Pull that down to a pre-deploy repository from the "server";
     prepare$ git pull ...to grab the "local changes" above...
 (3) Merge in whatever the update you want to have on the "server";
     prepare$ git merge ...whatever...
 (4) and then stop the server, fast-forward to the result of (3),
     and then restart.
     server$ stop server
     server$ git pull --ff-only ...the prepared result of (3)...
     server$ start server

← back to recent threads