#!/usr/bin/python

import os
import sys
import subprocess
import shutil

def clonePackage(base, pkg):
    if not os.path.isdir('%s.git' % pkg):
        if os.path.isdir(pkg):
            shutil.rmtree(pkg)
        # Use --no-follow-parent because we're going to handle that with
        # grafts.
        subprocess.check_call(['git', 'svn', 'clone',
                               '--no-follow-parent',
                               '-Aauthors',
                               '-q',
                               '--no-metadata',
                               '%s/packages/%s' % (base, pkg)],
                              stdout=subprocess.PIPE)
        
        # Then make the repository bare, because git-svn can't do this
        shutil.move('%s/.git' % pkg, '%s.git' % 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-files'],
                         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)

def cloneAllPackages(base):
    for pkg in open('package-list'):
        clonePackage(base, pkg.strip())

def mergeHistory(old_pkg, new_pkg, n):
    cwd = os.getcwd()
    subprocess.check_call(['git', 'push',
                           'file:///%s/%s.git' % (cwd, 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)
    
    # If n isn't 0, then n has a parent commit already that we
    # shouldn't forget about.
    if n != 0:
        p = subprocess.Popen(['git', 'rev-parse',
                              '%s^' % new_rev],
                             cwd='%s.git' % new_pkg,
                             stdout=subprocess.PIPE)
        p.wait()
        graft.append(p.stdout.read().strip())
    
    # And regardless, the HEAD of old_pkg should be a parent of
    # new_pkg
    p = subprocess.Popen(['git', 'rev-parse',
                          'master'],
                         cwd='%s.git' % old_pkg,
                         stdout=subprocess.PIPE)
    p.wait()
    graft.append(p.stdout.read().strip())
    
    f = open('%s.git/info/grafts' % new_pkg, 'a')
    
    print >>f, ' '.join(graft)

def mergeHistories():
    merges = []
    for line in open('merges'):
        line = line.strip()
        if line[0] == '#' or line == '':
            continue
        
        merges.append(line.split())
    
    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:
        base = sys.argv[1]
    except:
        base = 'svn://invirt.mit.edu/trunk'
    
    cloneAllPackages(base)
    mergeHistories()
