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