X-Git-Url: http://xvm.mit.edu/gitweb/invirt/scripts/git-migration.git/blobdiff_plain/abbc5e9ccf467827545eb44f3603b6bc26bf0a14..f042480e435cbf19bac8bb6ae0d4a16ba57e517f:/git-migrate diff --git a/git-migrate b/git-migrate index 65c7a12..f03884f 100755 --- a/git-migrate +++ b/git-migrate @@ -5,6 +5,29 @@ import sys import subprocess import shutil +def tagBase(pkg): + p = subprocess.Popen(['git', 'tag', + '-l', + 'base'], + cwd='%s.git' % pkg, + stdout=subprocess.PIPE) + p.wait() + if p.stdout.read().strip() != '': + return + + p = subprocess.Popen(['git', 'rev-list', + '--reverse', + 'master'], + cwd='%s.git' % pkg, + stdout=subprocess.PIPE) + p.wait() + base = p.stdout.read().split()[0] + + subprocess.check_call(['git', 'tag', + 'base', + base], + cwd='%s.git' % pkg) + def clonePackage(base, pkg): if not os.path.isdir('%s.git' % pkg): if os.path.isdir(pkg): @@ -27,58 +50,74 @@ def clonePackage(base, pkg): # Some of these repos have a rev where everything was deleted # as a result of the move. We don't want that rev to exist. - p = subprocess.Popen(['git', 'ls-files'], + p = subprocess.Popen(['git', 'ls-tree', 'HEAD'], cwd='%s.git' % pkg, stdout=subprocess.PIPE) p.wait() if len(p.stdout.read()) == 0: subprocess.check_call(['git', 'reset', '--soft', 'HEAD^'], cwd='%s.git' % pkg) + + tagBase(pkg) def cloneAllPackages(base): for pkg in open('package-list'): clonePackage(base, pkg.strip()) def mergeHistory(old_pkg, new_pkg, n): - cwd = os.getcwd() + n = int(n) + subprocess.check_call(['git', 'push', - 'file:///%s/%s.git' % (cwd, new_pkg), + '../%s.git' % new_pkg, 'master:refs/heads/%s' % old_pkg], - cwd='%s.git' % new_pkg) - - graft = [] - p = subprocess.Popen(['git', 'rev-list', - '--reverse', - '--skip=%s' % n, - 'master'], - cwd='%s.git' % new_pkg, - stdout=subprocess.PIPE) - p.wait() - new_rev = p.stdout.read().split()[0] - graft.append(new_rev) + cwd='%s.git' % old_pkg) - # If n isn't 0, then n has a parent commit already that we - # shouldn't forget about. - if n != 0: + # Find the merge commit + if n == 0: p = subprocess.Popen(['git', 'rev-parse', - '%s^' % new_rev], + 'base'], cwd='%s.git' % new_pkg, stdout=subprocess.PIPE) - p.wait() - graft.append(p.stdout.read().strip()) + else: + p = subprocess.Popen(['git', 'rev-list', + '--reverse', + '--boundary', + '--skip=%s' % (n - 1), + 'base..master'], + cwd='%s.git' % new_pkg, + stdout=subprocess.PIPE) + p.wait() + new_rev = p.stdout.read().split()[0].strip('-') - # And regardless, the HEAD of old_pkg should be a parent of - # new_pkg + # Find any other parents of the merge commit + p = subprocess.Popen(['git', 'log', + '-1', + '--pretty=format:%P', + new_rev], + cwd='%s.git' % new_pkg, + stdout=subprocess.PIPE) + p.wait() + parents = p.stdout.read().split() + + # Find the additional parent we're adding p = subprocess.Popen(['git', 'rev-parse', - 'master'], - cwd='%s.git' % old_pkg, + old_pkg], + cwd='%s.git' % new_pkg, stdout=subprocess.PIPE) p.wait() - graft.append(p.stdout.read().strip()) + parents.append(p.stdout.read().strip()) + # Write out the grafts file f = open('%s.git/info/grafts' % new_pkg, 'a') + print >>f, '%s %s' % (new_rev, ' '.join(parents)) + f.close() - print >>f, ' '.join(graft) + # Run filter-branch + subprocess.call(['git', 'filter-branch', + '--tag-name-filter', 'cat', + '--', + '--all'], + cwd='%s.git' % new_pkg) def mergeHistories(): merges = [] @@ -91,16 +130,6 @@ def mergeHistories(): for merge in merges: mergeHistory(*merge) - - for line in open('package-list'): - line = line.strip() - subprocess.check_call(['git', 'filter-branch', - '--', - '--all'], - cwd='%s.git' % line) - - for merge in merges: - shutil.rmtree('%s.git' % merge[0]) if __name__ == '__main__': try: