Konstantin Khomoutov <flatw...@users.sourceforge.net> writes:

> On Wed, 27 Nov 2013 15:17:27 +0000
> Pete Forman <petef4+use...@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

        if ! git diff-files --quiet --ignore-submodules
                echo >&2 "Cannot $1: You have unstaged changes."

        if ! git diff-index --cached --quiet --ignore-submodules HEAD --
                if [ $err = 0 ]
                    echo >&2 "Cannot $1: Your index contains uncommitted 
                    echo >&2 "Additionally, your index contains uncommitted 

        if [ $err = 1 ]
                test -n "$2" && echo >&2 "$2"
                exit 1

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
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to