threads / discuss / 33716

Pitfalls in auto-fast-forwarding heads that are not checked out?

Subject: Pitfalls in auto-fast-forwarding heads that are not checked out?

## tl;dr

6 messages between May 3, 2013 and May 7, 2013.

replies: 5people: 4as markdown or json

Martin Langhoff· May 3, 2013, 22:46 UTC · lore

I am building a small git wrapper around puppet, and one of the actions it performs is auto-fastforwarding of branches without checking them out.

In simplified code... we ensure that we are on a head called master, and in some cases "ppg commit", will commit to master and...

  ## early on
  # sanity-check we are on master
  headname=$(git rev-parse --symbolic-full-name --revs-only HEAD)
  if [ "$headname" -ne "refs/heads/headname" ]; then
      echo >&2 "ERROR: can only issue --immediate commit from the
master branch!"
      exit 1
  fi
  ## then
  git commit -bla blarg baz
  ## and then...
  # ensure we can ff
  head_sha1=$(git rev-parse --revs-only master)
  mb=$(git merge-base $production_sha1 refs/heads/master)
  if [[ "$mb" -ne "$production_sha1" ]]; then
      echo >&2 "ERROR: cannot fast-forward master to production"
      exit 1
  fi
  $GIT_EXEC_PATH/git-update-ref -m "ppg immediate commit"
refs/heads/production $head_sha1 $production_sha1 || exit 1

Are there major pitfalls in this approach? I cannot think of any, but git has stayed away from updating my local tracking branches; so maybe there's a reason for that...

cheers,
m
--
 martin.langhoff@gmail.com
 -  ask interesting questions
 - don't get distracted with shiny stuff  - working code first
 ~ http://docs.moodle.org/en/User:Martin_Langhoff
Johannes Sixt· May 4, 2013, 07:34 UTC · re: Martin Langhoff · lore

Re: Pitfalls in auto-fast-forwarding heads that are not checked out?

Am 04.05.2013 00:46, schrieb Martin Langhoff:
Show 11 quoted lines
> I am building a small git wrapper around puppet, and one of the
> actions it performs is auto-fastforwarding of branches without
> checking them out.
> 
> In simplified code... we ensure that we are on a head called master,
> and in some cases "ppg commit", will commit to master and...
> 
>   ## early on
>   # sanity-check we are on master
>   headname=$(git rev-parse --symbolic-full-name --revs-only HEAD)
>   if [ "$headname" -ne "refs/heads/headname" ]; then

You mean "refs/heads/master" and "!=" here because -ne is numeric comparison in a shell script.

Show 14 quoted lines
>       echo >&2 "ERROR: can only issue --immediate commit from the
> master branch!"
>       exit 1
>   fi
> 
>   ## then
>   git commit -bla blarg baz
> 
>   ## and then...
> 
>   # ensure we can ff
>   head_sha1=$(git rev-parse --revs-only master)
>   mb=$(git merge-base $production_sha1 refs/heads/master)
>   if [[ "$mb" -ne "$production_sha1" ]]; then
Your approach looks OK (but note again the incorrect "-ne").
Since git 1.8.0 you can express this check as
    if git merge-base --is-ancestor $production_sha1 refs/heads/master
>       echo >&2 "ERROR: cannot fast-forward master to production"
	echo >&2 "ERROR: cannot fast-forward production to master"
Show 6 quoted lines
>       exit 1
>   fi
>   $GIT_EXEC_PATH/git-update-ref -m "ppg immediate commit"
> refs/heads/production $head_sha1 $production_sha1 || exit 1
> 
> Are there major pitfalls in this approach?
I don't think there are.
> I cannot think of any, but
> git has stayed away from updating my local tracking branches; so maybe
> there's a reason for that...
I don't understand what you are saying here. What is "that"?
-- Hannes
Martin Langhoff· May 4, 2013, 11:35 UTC · re: Johannes Sixt · lore

Re: Pitfalls in auto-fast-forwarding heads that are not checked out?

On Sat, May 4, 2013 at 3:34 AM, Johannes Sixt <j6t@kdbg.org> wrote:
> You mean "refs/heads/master" and "!=" here because -ne is numeric
> comparison in a shell script.
thanks! Yeah, I fixed those up late last night :-)
> Since git 1.8.0 you can express this check as
>
>     if git merge-base --is-ancestor $production_sha1 refs/heads/master

Ah, that's great! Unfortunate it's not there in earlier / more widely used releases of git.

>> Are there major pitfalls in this approach?
>
> I don't think there are.
Thanks...
Show 5 quoted lines
>> I cannot think of any, but
>> git has stayed away from updating my local tracking branches; so maybe
>> there's a reason for that...
>
> I don't understand what you are saying here. What is "that"?

When I do git pull, git is careful to only update the branch I have checked out (if appropriate). It leaves any other branches that track branches on the remote that has just been fetched untouched. I always thought that at some point git pull would learn to evaluate those branches and auto-merge them if the merge is a ff.

I would find that a natural bit of automation in git pull. Of course it would mean a change of semantics, existing scripts could be affected.

cheers,
m
--
 martin.langhoff@gmail.com
 -  ask interesting questions
 - don't get distracted with shiny stuff  - working code first
 ~ http://docs.moodle.org/en/User:Martin_Langhoff
John Szakmeister· May 4, 2013, 13:17 UTC · re: Martin Langhoff · lore

Re: Pitfalls in auto-fast-forwarding heads that are not checked out?

On Sat, May 4, 2013 at 7:35 AM, Martin Langhoff <martin.langhoff@gmail.com> wrote: [snip]

Show 9 quoted lines
> When I do git pull, git is careful to only update the branch I have
> checked out (if appropriate). It leaves any other branches that track
> branches on the remote that has just been fetched untouched. I always
> thought that at some point git pull would learn to evaluate those
> branches and auto-merge them if the merge is a ff.
>
> I would find that a natural bit of automation in git pull. Of course
> it would mean a change of semantics, existing scripts could be
> affected.
I agree.  I've been using this script for quite a while now:
<https://github.com/jszakmeister/etc/blob/master/git-addons/git-ffwd>

I've been pretty happy with it. It's not of my own design, I picked up from StackOverflow:

<http://stackoverflow.com/a/9076361/683080>

And made a couple of minor tweaks to cope with my configuration (I have merge setup to not fast-forward merge by default).

-John
Jonathan Nieder· May 4, 2013, 18:51 UTC · re: Martin Langhoff · lore

Re: Pitfalls in auto-fast-forwarding heads that are not checked out?

Martin Langhoff wrote:
> On Sat, May 4, 2013 at 3:34 AM, Johannes Sixt <j6t@kdbg.org> wrote:
Show 6 quoted lines
>> Since git 1.8.0 you can express this check as
>>
>>     if git merge-base --is-ancestor $production_sha1 refs/heads/master
>
> Ah, that's great! Unfortunate it's not there in earlier / more widely
> used releases of git.
Another trick is to use "git push":
	git push . $production_sha1:refs/heads/master
This advances the "master" branch if
 (1) it is not the current branch, and
 (2) that update is a fast-forward
It can be convenient sometimes.

Hope that helps, Jonathan

Martin Langhoff· May 7, 2013, 14:27 UTC · re: Jonathan Nieder · lore

Re: Pitfalls in auto-fast-forwarding heads that are not checked out?

On Sat, May 4, 2013 at 2:51 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:
> Another trick is to use "git push":
>         git push . $production_sha1:refs/heads/master
Great trick -- thanks! In use in ppg now :-)
m
--
 martin.langhoff@gmail.com
 -  ask interesting questions
 - don't get distracted with shiny stuff  - working code first
 ~ http://docs.moodle.org/en/User:Martin_Langhoff

← back to recent threads