| [1866] | 1 | #!/usr/bin/python | 
|---|
 | 2 |  | 
|---|
| [1896] | 3 | import glob | 
|---|
| [1874] | 4 | import os | 
|---|
| [1866] | 5 | import sys | 
|---|
 | 6 | import subprocess | 
|---|
| [1869] | 7 | import shutil | 
|---|
| [1866] | 8 |  | 
|---|
| [1893] | 9 | def tagBase(pkg): | 
|---|
 | 10 |     p = subprocess.Popen(['git', 'tag', | 
|---|
 | 11 |                           '-l', | 
|---|
 | 12 |                           'base'], | 
|---|
 | 13 |                          cwd='%s.git' % pkg, | 
|---|
 | 14 |                          stdout=subprocess.PIPE) | 
|---|
 | 15 |     p.wait() | 
|---|
 | 16 |     if p.stdout.read().strip() != '': | 
|---|
 | 17 |         return | 
|---|
 | 18 |      | 
|---|
 | 19 |     p = subprocess.Popen(['git', 'rev-list', | 
|---|
 | 20 |                           '--reverse', | 
|---|
 | 21 |                           'master'], | 
|---|
 | 22 |                          cwd='%s.git' % pkg, | 
|---|
 | 23 |                          stdout=subprocess.PIPE) | 
|---|
 | 24 |     p.wait() | 
|---|
 | 25 |     base = p.stdout.read().split()[0] | 
|---|
 | 26 |      | 
|---|
 | 27 |     subprocess.check_call(['git', 'tag', | 
|---|
 | 28 |                            'base', | 
|---|
 | 29 |                            base], | 
|---|
 | 30 |                           cwd='%s.git' % pkg) | 
|---|
 | 31 |  | 
|---|
| [1866] | 32 | def clonePackage(base, pkg): | 
|---|
| [1895] | 33 |     path = '%s/%s' % (base, pkg) | 
|---|
 | 34 |     pkg = os.path.basename(pkg) | 
|---|
 | 35 |      | 
|---|
| [1874] | 36 |     if not os.path.isdir('%s.git' % pkg): | 
|---|
 | 37 |         if os.path.isdir(pkg): | 
|---|
 | 38 |             shutil.rmtree(pkg) | 
|---|
 | 39 |         # Use --no-follow-parent because we're going to handle that with | 
|---|
 | 40 |         # grafts. | 
|---|
| [1875] | 41 |         subprocess.check_call(['git', 'svn', 'clone', | 
|---|
 | 42 |                                '--no-follow-parent', | 
|---|
| [1874] | 43 |                                '-Aauthors', | 
|---|
 | 44 |                                '-q', | 
|---|
| [1875] | 45 |                                '--no-metadata', | 
|---|
| [1895] | 46 |                                '%s' % path], | 
|---|
| [1874] | 47 |                               stdout=subprocess.PIPE) | 
|---|
 | 48 |          | 
|---|
 | 49 |         # Then make the repository bare, because git-svn can't do this | 
|---|
 | 50 |         shutil.move('%s/.git' % pkg, '%s.git' % pkg) | 
|---|
| [1875] | 51 |         shutil.rmtree(pkg) | 
|---|
 | 52 |         subprocess.check_call(['git', 'config', 'core.bare', 'true'], | 
|---|
 | 53 |                               cwd='%s.git' % pkg) | 
|---|
| [1876] | 54 |          | 
|---|
 | 55 |     # Some of these repos have a rev where everything was deleted | 
|---|
 | 56 |     # as a result of the move. We don't want that rev to exist. | 
|---|
| [1891] | 57 |     p = subprocess.Popen(['git', 'ls-tree', 'HEAD'], | 
|---|
| [1876] | 58 |                          cwd='%s.git' % pkg, | 
|---|
 | 59 |                          stdout=subprocess.PIPE) | 
|---|
 | 60 |     p.wait() | 
|---|
 | 61 |     if len(p.stdout.read()) == 0: | 
|---|
| [1888] | 62 |         subprocess.check_call(['git', 'reset', '--soft', 'HEAD^'], | 
|---|
| [1876] | 63 |                               cwd='%s.git' % pkg) | 
|---|
| [1893] | 64 |      | 
|---|
 | 65 |     tagBase(pkg) | 
|---|
| [1866] | 66 |  | 
|---|
 | 67 | def cloneAllPackages(base): | 
|---|
 | 68 |     for pkg in open('package-list'): | 
|---|
 | 69 |         clonePackage(base, pkg.strip()) | 
|---|
 | 70 |  | 
|---|
| [1880] | 71 | def mergeHistory(old_pkg, new_pkg, n): | 
|---|
| [1893] | 72 |     n = int(n) | 
|---|
 | 73 |      | 
|---|
| [1881] | 74 |     subprocess.check_call(['git', 'push', | 
|---|
| [1892] | 75 |                            '../%s.git' % new_pkg, | 
|---|
| [1881] | 76 |                            'master:refs/heads/%s' % old_pkg], | 
|---|
| [1890] | 77 |                           cwd='%s.git' % old_pkg) | 
|---|
| [1893] | 78 |      | 
|---|
 | 79 |     # Find the merge commit | 
|---|
 | 80 |     if n == 0: | 
|---|
 | 81 |         p = subprocess.Popen(['git', 'rev-parse', | 
|---|
 | 82 |                               'base'], | 
|---|
 | 83 |                              cwd='%s.git' % new_pkg, | 
|---|
 | 84 |                              stdout=subprocess.PIPE) | 
|---|
 | 85 |     else: | 
|---|
 | 86 |         p = subprocess.Popen(['git', 'rev-list', | 
|---|
 | 87 |                               '--reverse', | 
|---|
 | 88 |                               '--boundary', | 
|---|
 | 89 |                               '--skip=%s' % (n - 1), | 
|---|
 | 90 |                               'base..master'], | 
|---|
 | 91 |                              cwd='%s.git' % new_pkg, | 
|---|
 | 92 |                              stdout=subprocess.PIPE) | 
|---|
 | 93 |     p.wait() | 
|---|
 | 94 |     new_rev = p.stdout.read().split()[0].strip('-') | 
|---|
 | 95 |      | 
|---|
 | 96 |     # Find any other parents of the merge commit | 
|---|
 | 97 |     p = subprocess.Popen(['git', 'log', | 
|---|
 | 98 |                           '-1', | 
|---|
 | 99 |                           '--pretty=format:%P', | 
|---|
 | 100 |                           new_rev], | 
|---|
 | 101 |                          cwd='%s.git' % new_pkg, | 
|---|
 | 102 |                          stdout=subprocess.PIPE) | 
|---|
 | 103 |     p.wait() | 
|---|
 | 104 |     parents = p.stdout.read().split() | 
|---|
 | 105 |      | 
|---|
 | 106 |     # Find the additional parent we're adding | 
|---|
 | 107 |     p = subprocess.Popen(['git', 'rev-parse', | 
|---|
 | 108 |                           old_pkg], | 
|---|
 | 109 |                          cwd='%s.git' % new_pkg, | 
|---|
 | 110 |                          stdout=subprocess.PIPE) | 
|---|
 | 111 |     p.wait() | 
|---|
 | 112 |     parents.append(p.stdout.read().strip()) | 
|---|
 | 113 |      | 
|---|
 | 114 |     # Write out the grafts file | 
|---|
 | 115 |     f = open('%s.git/info/grafts' % new_pkg, 'a') | 
|---|
 | 116 |     print >>f, '%s %s' % (new_rev, ' '.join(parents)) | 
|---|
 | 117 |     f.close() | 
|---|
 | 118 |      | 
|---|
 | 119 |     # Run filter-branch | 
|---|
 | 120 |     subprocess.call(['git', 'filter-branch', | 
|---|
 | 121 |                      '--tag-name-filter', 'cat', | 
|---|
 | 122 |                      '--', | 
|---|
 | 123 |                      '--all'], | 
|---|
 | 124 |                     cwd='%s.git' % new_pkg) | 
|---|
| [1894] | 125 |      | 
|---|
 | 126 |     subprocess.call(['git', 'branch', | 
|---|
 | 127 |                      '-D', | 
|---|
 | 128 |                      old_pkg], | 
|---|
 | 129 |                     cwd='%s.git' % new_pkg) | 
|---|
 | 130 |     shutil.rmtree('%s.git/refs/original' % new_pkg, True) | 
|---|
| [1880] | 131 |  | 
|---|
 | 132 | def mergeHistories(): | 
|---|
| [1884] | 133 |     merges = [] | 
|---|
 | 134 |     for line in open('merges'): | 
|---|
| [1880] | 135 |         line = line.strip() | 
|---|
| [1889] | 136 |         if line == '' or line[0] == '#': | 
|---|
| [1880] | 137 |             continue | 
|---|
 | 138 |          | 
|---|
| [1884] | 139 |         merges.append(line.split()) | 
|---|
| [1883] | 140 |      | 
|---|
| [1884] | 141 |     for merge in merges: | 
|---|
 | 142 |         mergeHistory(*merge) | 
|---|
| [1894] | 143 |      | 
|---|
 | 144 |     for merge in merges: | 
|---|
 | 145 |         shutil.rmtree('%s.git' % merge[0]) | 
|---|
| [1880] | 146 |  | 
|---|
| [1896] | 147 | def cleanupRepos(): | 
|---|
 | 148 |     for pkg in glob.glob('*.git'): | 
|---|
 | 149 |         subprocess.check_call(['git', 'tag', '-d', 'base'], | 
|---|
 | 150 |                               cwd='%s' % pkg) | 
|---|
 | 151 |          | 
|---|
 | 152 |         subprocess.check_call(['git', 'gc'], | 
|---|
 | 153 |                               cwd='%s' % pkg) | 
|---|
 | 154 |  | 
|---|
| [1866] | 155 | if __name__ == '__main__': | 
|---|
 | 156 |     try: | 
|---|
 | 157 |         base = sys.argv[1] | 
|---|
 | 158 |     except: | 
|---|
 | 159 |         base = 'svn://invirt.mit.edu/trunk' | 
|---|
 | 160 |      | 
|---|
 | 161 |     cloneAllPackages(base) | 
|---|
| [1880] | 162 |     mergeHistories() | 
|---|
| [1896] | 163 |     cleanupRepos() | 
|---|