Refactor the Commitdata class to CommitData
[stgit] / stgit / lib / git.py
index 35b9bbf..b3181cb 100644 (file)
@@ -24,13 +24,13 @@ class NoValue(object):
     pass
 
 def make_defaults(defaults):
-    def d(val, attr):
+    def d(val, attr, default_fun = lambda: None):
         if val != NoValue:
             return val
         elif defaults != NoValue:
             return getattr(defaults, attr)
         else:
-            return None
+            return default_fun()
     return d
 
 class TimeZone(tzinfo, Repr):
@@ -154,15 +154,15 @@ class Tree(Repr):
     def __str__(self):
         return 'Tree<%s>' % self.sha1
 
-class Commitdata(Repr):
+class CommitData(Repr):
     """Immutable."""
     def __init__(self, tree = NoValue, parents = NoValue, author = NoValue,
                  committer = NoValue, message = NoValue, defaults = NoValue):
         d = make_defaults(defaults)
         self.__tree = d(tree, 'tree')
         self.__parents = d(parents, 'parents')
-        self.__author = d(author, 'author')
-        self.__committer = d(committer, 'committer')
+        self.__author = d(author, 'author', Person.author)
+        self.__committer = d(committer, 'committer', Person.committer)
         self.__message = d(message, 'message')
     tree = property(lambda self: self.__tree)
     parents = property(lambda self: self.__parents)
@@ -199,7 +199,7 @@ class Commitdata(Repr):
             parents = None
         else:
             parents = [p.sha1 for p in self.parents]
-        return ('Commitdata<tree: %s, parents: %s, author: %s,'
+        return ('CommitData<tree: %s, parents: %s, author: %s,'
                 ' committer: %s, message: "%s">'
                 ) % (tree, parents, self.author, self.committer, self.message)
     @classmethod
@@ -233,7 +233,7 @@ class Commit(Repr):
     @property
     def data(self):
         if self.__data == None:
-            self.__data = Commitdata.parse(
+            self.__data = CommitData.parse(
                 self.__repository,
                 self.__repository.cat_object(self.sha1))
         return self.__data
@@ -298,6 +298,10 @@ class RunWithEnv(object):
     def run(self, args, env = {}):
         return run.Run(*args).env(utils.add_dict(self.env, env))
 
+class RunWithEnvCwd(RunWithEnv):
+    def run(self, args, env = {}):
+        return RunWithEnv.run(self, args, env).cwd(self.cwd)
+
 class Repository(RunWithEnv):
     def __init__(self, directory):
         self.__git_dir = directory
@@ -490,19 +494,20 @@ class Index(RunWithEnv):
 class Worktree(object):
     def __init__(self, directory):
         self.__directory = directory
-    env = property(lambda self: { 'GIT_WORK_TREE': self.__directory })
+    env = property(lambda self: { 'GIT_WORK_TREE': '.' })
     directory = property(lambda self: self.__directory)
 
 class CheckoutException(exception.StgException):
     pass
 
-class IndexAndWorktree(RunWithEnv):
+class IndexAndWorktree(RunWithEnvCwd):
     def __init__(self, index, worktree):
         self.__index = index
         self.__worktree = worktree
     index = property(lambda self: self.__index)
     env = property(lambda self: utils.add_dict(self.__index.env,
                                                self.__worktree.env))
+    cwd = property(lambda self: self.__worktree.directory)
     def checkout(self, old_tree, new_tree):
         # TODO: Optionally do a 3-way instead of doing nothing when we
         # have a problem. Or maybe we should stash changes in a patch?
@@ -512,7 +517,7 @@ class IndexAndWorktree(RunWithEnv):
             self.run(['git', 'read-tree', '-u', '-m',
                       '--exclude-per-directory=.gitignore',
                       old_tree.sha1, new_tree.sha1]
-                     ).cwd(self.__worktree.directory).discard_output()
+                     ).discard_output()
         except run.RunException:
             raise CheckoutException('Index/workdir dirty')
     def merge(self, base, ours, theirs):
@@ -524,8 +529,7 @@ class IndexAndWorktree(RunWithEnv):
                           theirs.sha1],
                          env = { 'GITHEAD_%s' % base.sha1: 'ancestor',
                                  'GITHEAD_%s' % ours.sha1: 'current',
-                                 'GITHEAD_%s' % theirs.sha1: 'patched'}
-                         ).cwd(self.__worktree.directory)
+                                 'GITHEAD_%s' % theirs.sha1: 'patched'})
             r.discard_output()
         except run.RunException, e:
             if r.exitcode == 1: