git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Matthieu Moy <matthieu.moy@grenoble-inp.fr>
Date
Nov 27, 2013, 15:54 UTC
Message-ID
<vpq8uw9q1r4.fsf@anie.imag.fr>
In-Reply-To
<20131127194240.2abaff5575961b3d73e1970f@domain007.com>
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/
Previous: Konstantin KhomoutovNext: Antoine Pelisse
Message 3 of 7 in “How to pre-empt git pull merge error?”
  1. Pete FormanNov 27, 2013
  2. Konstantin KhomoutovNov 27, 2013
  3. Matthieu MoyNov 27, 2013
  4. Antoine PelisseNov 27, 2013
  5. Thomas RastNov 27, 2013
  6. Pete FormanNov 28, 2013
  7. Junio C HamanoNov 27, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.