joequery
7/22/2013 - 7:00 PM

This script searches through the git log for the current directory you're in and starts an interactive rebase on a "base commit", which is s

This script searches through the git log for the current directory you're in and starts an interactive rebase on a "base commit", which is simply a commit that doesn't start with "fixup! "

#!/usr/bin/env python

import subprocess
p = subprocess.Popen(["git", "log", "--format='%s'"], stdout=subprocess.PIPE)
out, err = p.communicate()

# Keep going until we don't find a "fixup! " 
fixup_count = 0 
max_list_size = 10                                                                                                                                                                                                                                                              
commits = out.split('\n')[:max_list_size]

for c in commits:
    if "fixup! " not in c:
        break
    fixup_count += 1

head_str = "HEAD~%d" % (fixup_count + 1)
print("Fixup count: %d" % fixup_count)

if fixup_count == max_list_size:
    print("No base commit found in the last %d commits!" % max_list_size)
    print("You should REALLY consider rebasing...")
elif fixup_count == 0:
    print("No fixup commits found. Nothing to do")
else:
    print("Found base commit %d commits back: %s" % (fixup_count, c)) 
    print("*** Calling interactive rebase on %s..." % head_str)

    subprocess.call(["git", "rebase", "-i", "--autosquash", head_str])