axil's blog
  • Home
  • Categories
  • Tags
  • Archives

git fast-forward

Running git merge with --ff-only argument is a nice precaution to avoid unnecessary merges.

It is convenient to abbreviate it as git ff:

git config --global ff "merge --ff-only"

In the earlier versions of django this command didn't go out of the box.

The script below did pretty much the same as git merge --ff-only does now: it fast-forwards the current branch against its tracking branch and shows all kind of relevant info if there's an error.

Prerequisites:

python of any version (>=2.7 or >=3.5)

Installation:

  1. Download the git-ff script.

  2. Copy the script:

    (linux) to any directory in your path on linux (eg ~/bin provided it is in the path)
    (windows) to "C:\Program Files\Git\libexec\git-core" or "C:\Program Files\Git\mingw64\libexec\git-core" (use your git installation path if it is different)

PS The script itself

#!/usr/bin/python
# vim: filetype=python :

from __future__ import print_function
import re, os, sys

def mystrip(a):
    return re.sub('refs/heads/', '', a)

def call(a):
    return os.popen(a).read().strip()

def get_id(branch):
    return call('git rev-parse %s' % branch)

if len(sys.argv) > 2:
    print('usage: git ff [<remote>]')
    exit(1)
branch = mystrip(call('git symbolic-ref HEAD'))    # git name-rev --name-only HEAD would also do
if len(sys.argv) == 2:
    tracked = '/'.join((sys.argv[1], branch))
else:
    remote = call('git config branch.%s.remote' % branch)
    if remote == '':
        print('branch not tracking and no remote given, aborting')
        exit(1)
    merge = call('git config branch.%s.merge' % branch)
    if remote=='.':
        tracked = merge
    else:
        tracked = '/'.join(( remote, mystrip(merge) ))
base_id = call('git merge-base %s %s' % (branch, tracked))

print(get_id(branch)[:7], branch, '(current)')
print(get_id(tracked)[:7], tracked, '(tracked)')
print(base_id[:7], '(base)')

if base_id != get_id(branch):
    print('Cannot fast-forward')
    sys.exit(1)
n = call('git rev-list %s..%s |wc -l' % (branch, tracked))
if n=='0':
    print('Your branch is already up-to-date')
    sys.exit(0)
print('Fast-forwarding %s revision(s)' % n)
print(call('git merge %s' % tracked))
  • « How to install a python wheel with one doubleclick
  • Improving the MS Office ribbon »
Comments
comments powered by Disqus

Published

Nov 7, 2016

Last Updated

2020-07-10 17:30:42.286330+07:00

Category

software

Tags

  • fast-forward 1
  • git 2
  • linux 2
  • windows 3
  • Powered by Pelican. Theme: Elegant by Talha Mansoor