Commits

Mark Roddy  committed 678ce14 Draft

initial stab at adding support for git repos to be mirrored as well as mercurial. hg mirror actually appears to be broken, possibly due to changes in the hg-git plugin

  • Participants
  • Parent commits 854c25d

Comments (0)

Files changed (2)

 bitbucket2github.egg*
 build*
 dist*
-
+venv

File bitbucket2github/bitbucket2github.py

 import bitbucket, github
 
 bitbucket_url = 'http://bitbucket.org/{0}/{1}'
-github_url = 'git+ssh://git@github.com/{0}/{1}.git'
+github_url = 'git@github.com/{0}/{1}.git'
 tmp_dir = tempfile.gettempdir()
 
+def HgPullOrClone(remote_repo, local_repo):
+    if os.path.exists(local_repo):
+        return sh('hg pull {0} -R {1}'.format(remote_repo, local_repo))
+    else:
+        return sh('hg clone {0} {1}'.format(remote_repo, local_repo))
+
+def GitPullOrClone(remote_repo, local_repo):
+    if os.path.exists(local_repo):
+        os.chdir(local_repo)
+        cmd = 'git --git-dir="{1}/.git" --work-tree="{1}" pull {0} master'.format(remote_repo, local_repo)
+        print cmd
+        return sh(cmd)
+    else:
+        cmd = 'git clone {0} {1}'.format(remote_repo, local_repo)
+    print cmd
+    return sh(cmd)
+    
 def backup(repo, bitbucket_username, github_username, github_api_token):
     print "Syncing %s from BitBucket to GitHub" % repo['name']
-    github.create_repo(repo, github_username, github_api_token)
+    # github.create_repo(repo, github_username, github_api_token)
 
     bitbucket_repo = bitbucket_url.format(bitbucket_username, repo['name'])
     github_repo = github_url.format(github_username, repo['name'])
     local_repo = os.path.join(tmp_dir, repo['name'])
 
-    if os.path.exists(local_repo):
-        sh('hg pull {0} -R {1}'.format(bitbucket_repo, local_repo))
+    if repo['scm'] == 'hg':
+        # TODO GET HG+GIT WORKING
+        return 0 # skipping for now
+        if (0 != HgPullOrClone(bitbucket_repo, local_repo)):
+            print "Error getting repo"
+            return 1
+        sh('hg bookmark master -f -R {0}'.format(local_repo))
+        github_repo = "git+ssh://" + github_repo
+        if (0 != sh('hg push {0} -R {1}'.format(github_repo, local_repo))):
+            print "Error pushing changes"
+            return 1
     else:
-        sh('hg clone {0} {1}'.format(bitbucket_repo, local_repo))
-
-    sh('hg bookmark master -f -R {0}'.format(local_repo))
-    sh('hg push {0} -R {1}'.format(github_repo, local_repo))
+        # assume it's git
+        if (0 != GitPullOrClone(bitbucket_repo, local_repo)):
+            print "Error getting repo"
+            return 1;
+        cmd = 'git --git-dir={1}/.git --work-tree={1} push ssh://{0}'.format(github_repo, local_repo)
+        print cmd
+        if (0 != sh(cmd)):
+            print "Error pushing changes"
+            return 1
+    return 0
 
 def main():
     bitbucket_username = vault.get('bitbucket.org', 'username')
     
 
     for repo in bitbucket.repos(bitbucket_username):
-        backup(repo, bitbucket_username, github_username, github_api_token)
+        if (0 != backup(repo, bitbucket_username, github_username, github_api_token)):
+            break
 
 
 if __name__ == '__main__':