More cleanup.
[invirt/scripts/git-migration.git] / git-migrate
index f3ef98e..c116db5 100755 (executable)
@@ -1,11 +1,38 @@
 #!/usr/bin/python
 
+import glob
 import os
 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):
+    path = '%s/%s' % (base, pkg)
+    pkg = os.path.basename(pkg)
+    
     if not os.path.isdir('%s.git' % pkg):
         if os.path.isdir(pkg):
             shutil.rmtree(pkg)
@@ -16,7 +43,7 @@ def clonePackage(base, pkg):
                                '-Aauthors',
                                '-q',
                                '--no-metadata',
-                               '%s/packages/%s' % (base, pkg)],
+                               '%s' % path],
                               stdout=subprocess.PIPE)
         
         # Then make the repository bare, because git-svn can't do this
@@ -24,11 +51,107 @@ def clonePackage(base, pkg):
         shutil.rmtree(pkg)
         subprocess.check_call(['git', 'config', 'core.bare', 'true'],
                               cwd='%s.git' % 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-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):
+    n = int(n)
+    
+    subprocess.check_call(['git', 'push',
+                           '../%s.git' % new_pkg,
+                           'master:refs/heads/%s' % old_pkg],
+                          cwd='%s.git' % old_pkg)
+    
+    # Find the merge commit
+    if n == 0:
+        p = subprocess.Popen(['git', 'rev-parse',
+                              'base'],
+                             cwd='%s.git' % new_pkg,
+                             stdout=subprocess.PIPE)
+    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('-')
+    
+    # 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',
+                          old_pkg],
+                         cwd='%s.git' % new_pkg,
+                         stdout=subprocess.PIPE)
+    p.wait()
+    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()
+    
+    # Run filter-branch
+    subprocess.call(['git', 'filter-branch',
+                     '--tag-name-filter', 'cat',
+                     '--',
+                     '--all'],
+                    cwd='%s.git' % new_pkg)
+    
+    subprocess.call(['git', 'branch',
+                     '-D',
+                     old_pkg],
+                    cwd='%s.git' % new_pkg)
+    shutil.rmtree('%s.git/refs/original' % new_pkg, True)
+
+def mergeHistories():
+    merges = []
+    for line in open('merges'):
+        line = line.strip()
+        if line == '' or line[0] == '#':
+            continue
+        
+        merges.append(line.split())
+    
+    for merge in merges:
+        mergeHistory(*merge)
+    
+    for merge in merges:
+        shutil.rmtree('%s.git' % merge[0])
+
+def cleanupRepos():
+    for pkg in glob.glob('*.git'):
+        subprocess.check_call(['git', 'tag', '-d', 'base'],
+                              cwd='%s' % pkg)
+        
+        subprocess.check_call(['git', 'gc'],
+                              cwd='%s' % pkg)
+
 if __name__ == '__main__':
     try:
         base = sys.argv[1]
@@ -36,3 +159,5 @@ if __name__ == '__main__':
         base = 'svn://invirt.mit.edu/trunk'
     
     cloneAllPackages(base)
+    mergeHistories()
+    cleanupRepos()