#!/usr/bin/python2.6 import os, re, subprocess, sys class GitError(EnvironmentError): def __init__(self, err, out, returncode): EnvironmentError.__init__(self, err) self.stdout = out self.returncode = returncode def git(*args, **kw): p = subprocess.Popen(('git',) + args, **kw) out, err = p.communicate() if p.returncode: raise GitError(err, out, p.returncode) return out def main(): svn_id_re = re.compile('\n+git-svn-id: .*\n*$') grafts = [] key = None skip = False try: for log in git('log', '-z', '--pretty=format:%H %P %an %B', stdout=subprocess.PIPE, *sys.argv[1:]).split('\0'): # If there is more than 1 parent, 'k' will start # with their hash and will always differ from 'key'. # Thus, we never squash merge commits. h, p, k = log.split(' ', 2) k = svn_id_re.sub('', k) if k == key: skip = True else: if skip: grafts.append((parent, h)) key = k parent = h skip = False if skip: grafts.append((parent,)) if grafts: git_dir = git('rev-parse', '--git-dir', stdout=subprocess.PIPE).strip() with open(os.path.join(git_dir, 'info', 'grafts'), 'a') as f: for graft in grafts: f.write(' '.join(graft) + '\n') except GitError, e: err = str(e) if err: sys.stderr.write(err) return e.returncode if __name__ == '__main__': sys.exit(main()) # #!/bin/sh -e # tmp=`mktemp -d` # trap "rm -r $tmp" 0 # if git log --pretty=format:'%H %an %s' |uniq -cds 41 |while read n h line # do git rev-parse -q --verify $h^2 >/dev/null || git rev-list $h~$n..$h~ # done | (cd "$tmp"; xargs touch 2>/dev/null) # then git filter-branch --commit-filter "if rm $tmp/\$GIT_COMMIT 2>/dev/null # then skip_commit "\$@" # else git commit-tree "\$@" # fi" --tag-name-filter cat "$@" # fi