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