All of lore.kernel.org
 help / color / mirror / Atom feed
* How to pre-empt git pull merge error?
@ 2013-11-27 15:17 Pete Forman
  2013-11-27 15:42 ` Konstantin Khomoutov
  0 siblings, 1 reply; 7+ messages in thread
From: Pete Forman @ 2013-11-27 15:17 UTC (permalink / raw)
  To: git

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to pre-empt git pull merge error?
  2013-11-27 15:17 How to pre-empt git pull merge error? Pete Forman
@ 2013-11-27 15:42 ` Konstantin Khomoutov
  2013-11-27 15:54   ` Matthieu Moy
  0 siblings, 1 reply; 7+ messages in thread
From: Konstantin Khomoutov @ 2013-11-27 15:42 UTC (permalink / raw)
  To: Pete Forman; +Cc: git

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

?

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to pre-empt git pull merge error?
  2013-11-27 15:42 ` Konstantin Khomoutov
@ 2013-11-27 15:54   ` Matthieu Moy
  2013-11-27 16:38     ` Antoine Pelisse
  0 siblings, 1 reply; 7+ messages in thread
From: Matthieu Moy @ 2013-11-27 15:54 UTC (permalink / raw)
  To: Konstantin Khomoutov; +Cc: Pete Forman, git

Konstantin Khomoutov <flatworm@users.sourceforge.net> 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
>> 
>> 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/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to pre-empt git pull merge error?
  2013-11-27 15:54   ` Matthieu Moy
@ 2013-11-27 16:38     ` Antoine Pelisse
  2013-11-27 19:06       ` Thomas Rast
  2013-11-27 20:09       ` Junio C Hamano
  0 siblings, 2 replies; 7+ messages in thread
From: Antoine Pelisse @ 2013-11-27 16:38 UTC (permalink / raw)
  To: Matthieu Moy, Thomas Rast; +Cc: Konstantin Khomoutov, Pete Forman, git

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to pre-empt git pull merge error?
  2013-11-27 16:38     ` Antoine Pelisse
@ 2013-11-27 19:06       ` Thomas Rast
  2013-11-28  8:25         ` Pete Forman
  2013-11-27 20:09       ` Junio C Hamano
  1 sibling, 1 reply; 7+ messages in thread
From: Thomas Rast @ 2013-11-27 19:06 UTC (permalink / raw)
  To: Antoine Pelisse; +Cc: Matthieu Moy, Konstantin Khomoutov, Pete Forman, git

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

-- 
Thomas Rast
tr@thomasrast.ch

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to pre-empt git pull merge error?
  2013-11-27 16:38     ` Antoine Pelisse
  2013-11-27 19:06       ` Thomas Rast
@ 2013-11-27 20:09       ` Junio C Hamano
  1 sibling, 0 replies; 7+ messages in thread
From: Junio C Hamano @ 2013-11-27 20:09 UTC (permalink / raw)
  To: Antoine Pelisse
  Cc: Matthieu Moy, Thomas Rast, Konstantin Khomoutov, Pete Forman, git

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

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to pre-empt git pull merge error?
  2013-11-27 19:06       ` Thomas Rast
@ 2013-11-28  8:25         ` Pete Forman
  0 siblings, 0 replies; 7+ messages in thread
From: Pete Forman @ 2013-11-28  8:25 UTC (permalink / raw)
  To: git

Thomas Rast <tr@thomasrast.ch> writes:

> 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

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2013-11-28  8:25 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-11-27 15:17 How to pre-empt git pull merge error? Pete Forman
2013-11-27 15:42 ` Konstantin Khomoutov
2013-11-27 15:54   ` Matthieu Moy
2013-11-27 16:38     ` Antoine Pelisse
2013-11-27 19:06       ` Thomas Rast
2013-11-28  8:25         ` Pete Forman
2013-11-27 20:09       ` Junio C Hamano

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.