changeset 7:89992b6d2eef

mapping parents properly now
author Scott Chacon <schacon@gmail.com>
date Fri, 24 Apr 2009 14:05:50 -0700
parents c77197123d95
children 2548735d24ef
files dulwich/objects.py git_handler.py
diffstat 2 files changed, 51 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/dulwich/objects.py	Fri Apr 24 11:45:10 2009 -0700
+++ b/dulwich/objects.py	Fri Apr 24 14:05:50 2009 -0700
@@ -502,11 +502,17 @@
         self._text += self._message
 
     def getfile(self, f):
-        return "file contents"
-    
+        import random
+        dt = "file contents:" + str(random.randrange(1, 100))
+        print dt
+        return dt
+        
     def getmode(self, f):
         return ""
-        
+
+    def getfiles(self):
+        return ['test']
+
     @property
     def tree(self):
         """Returns the tree that is the state of this commit"""
--- a/git_handler.py	Fri Apr 24 11:45:10 2009 -0700
+++ b/git_handler.py	Fri Apr 24 14:05:50 2009 -0700
@@ -1,4 +1,4 @@
-import os, errno, sys, time, datetime
+import os, errno, sys, time, datetime, pickle, copy
 import dulwich
 from dulwich.repo import Repo
 from dulwich.client import SimpleFetchGraphWalker
@@ -12,15 +12,33 @@
     def __init__(self, dest_repo, ui):
         self.repo = dest_repo
         self.ui = ui
+        self.load_git()
+        self.load_map()
+        
+    def load_git(self):
         git_dir = os.path.join(self.repo.path, 'git')
         self.git = Repo(git_dir)
-        
+
+    def load_map(self):
+        self._map = {}
+        if os.path.exists(self.repo.join('git-mapfile')):
+            for line in self.repo.opener('git-mapfile'):
+                gitsha, hgsha = line.strip().split(' ', 1)
+                self._map[gitsha] = hgsha
+            
+    def save_map(self):
+        file = self.repo.opener('git-mapfile', 'w+')
+        for gitsha, hgsha in self._map.iteritems():
+            file.write("%s %s\n" % (gitsha, hgsha))
+        file.close()
+
     def fetch(self, git_url):
         self.ui.status(_("fetching from git url: " + git_url + "\n"))
         self.export_git_objects()
         self.fetch_pack(git_url)
         self.import_git_objects()
-    
+        self.save_map()
+
     def fetch_pack(self, git_url):
         client, path = self.get_transport_and_path(git_url)
         graphwalker = SimpleFetchGraphWalker(self.git.heads().values(), self.git.get_parents)
@@ -70,32 +88,42 @@
         print bookmarks.parse(self.repo)
 
     def import_git_commit(self, commit):
-        # check that parents are all in Hg, or no parents
         print commit.parents
 
+        # TODO : have to handle merge contexts at some point (two parent files, etc)
         def getfilectx(repo, memctx, f):
             data = commit.getfile(f)
             e = commit.getmode(f)
             return context.memfilectx(f, data, 'l' in e, 'x' in e, None)
-            
+        
         p1 = "0" * 40
         p2 = "0" * 40
+        # TODO : do something if parent is not mapped yet!
+        if len(commit.parents) > 0:
+            sha = commit.parents[0]
+            p1 = self._map[sha]
+        if len(commit.parents) > 1:
+            sha = commit.parents[1]
+            p2 = self._map[sha]
+        if len(commit.parents) > 2:
+            # TODO : map extra parents to the extras file
+            pass
 
-        files = ['test']
+        files = commit.getfiles()
 
         # get a list of the changed, added, removed files
+        extra = {}
         text = commit.message
-        extra = {}
-        date = datetime.datetime.fromtimestamp(commit.author_time).strftime("%Y-%m-%d")
-        print date
+        date = datetime.datetime.fromtimestamp(commit.author_time).strftime("%Y-%m-%d %H:%M:%S")
         ctx = context.memctx(self.repo, (p1, p2), text, files, getfilectx,
                              commit.author, date, extra)
-        print ctx
         a = self.repo.commitctx(ctx)
-        print a
-        #puts p2 = hex(self.repo.changelog.tip())
-        
-        pass
+
+        # get changeset id
+        p2 = hex(self.repo.changelog.tip())
+        # save changeset to mapping file
+        gitsha = commit.id
+        self._map[gitsha] = p2
         
     def getfilectx(self, source, repo, memctx, f):
         v = files[f]