Add convenience function for creating a new commit in a git repository.
authorJelmer Vernooij <jelmer@samba.org>
Sun, 18 Oct 2009 17:24:17 +0000 (19:24 +0200)
committerJelmer Vernooij <jelmer@samba.org>
Sun, 18 Oct 2009 17:24:17 +0000 (19:24 +0200)
bin/dulwich
dulwich/index.py
dulwich/repo.py

index 7acefb56b7c8aa001f4c65821d4cdcc04f7077bb..1d3f7ed19b79c9ba1d0a5af4e492208160f32c35 100755 (executable)
@@ -161,7 +161,20 @@ def cmd_clone(args):
     commit()
 
 
+def cmd_commit(args):
+    from dulwich.repo import Repo
+    import os
+    opts, args = getopt(args, "", ["message"])
+    opts = dict(opts)
+    r = Repo(".")
+    committer = "%s <%s>" % (os.getenv("GIT_COMMITTER_NAME"), 
+                             os.getenv("GIT_COMMITTER_EMAIL"))
+    author = "%s <%s>" % (os.getenv("GIT_AUTHOR_NAME"), 
+                          os.getenv("GIT_AUTHOR_EMAIL"))
+    r.do_commit(committer=committer, author=author, message=opts["--message"])
+
 commands = {
+    "commit": cmd_commit,
     "fetch-pack": cmd_fetch_pack,
     "dump-pack": cmd_dump_pack,
     "dump-index": cmd_dump_index,
index b5f6014e5978c08e7f2b15632506f705ceaec7cb..d8dd203c4678fd6a0352ec5ed093e32b8dc72224 100644 (file)
@@ -218,7 +218,8 @@ class Index(object):
 
     def iterblobs(self):
         """Iterate over path, sha, mode tuples for use with commit_tree."""
-        for path, entry in self:
+        for path in self:
+            entry = self[path]
             yield path, entry[-2], cleanup_mode(entry[-6])
 
     def clear(self):
index d9d4758e44042b2dc35209886971274e8feb73ce..2be4bf1eb7ab6878818147f51641675caafcae7c 100644 (file)
@@ -409,6 +409,52 @@ class Repo(object):
             del self.refs[name]
         raise ValueError(name)
 
+    def do_commit(self, committer, message,
+                  author=None, commit_timestamp=None,
+                  commit_timezone=None, author_timestamp=None, 
+                  author_timezone=None, tree=None):
+        """Create a new commit.
+
+        :param committer: Committer fullname
+        :param message: Commit message
+        :param author: Author fullname (defaults to committer)
+        :param commit_timestamp: Commit timestamp (defaults to now)
+        :param commit_timezone: Commit timestamp timezone (defaults to GMT)
+        :param author_timestamp: Author timestamp (defaults to commit timestamp)
+        :param author_timezone: Author timestamp timezone 
+            (defaults to commit timestamp timezone)
+        :param tree: SHA1 of the tree root to use (if not specified the current index will be committed).
+        :return: New commit SHA1
+        """
+        from dulwich.index import commit_index
+        import time
+        index = self.open_index()
+        c = Commit()
+        if tree is None:
+            c.tree = commit_index(self.object_store, index)
+        else:
+            c.tree = tree
+        c.committer = committer
+        if commit_timestamp is None:
+            commit_timestamp = time.time()
+        c.commit_time = int(commit_timestamp)
+        if commit_timezone is None:
+            commit_timezone = 0
+        c.commit_timezone = commit_timezone
+        if author is None:
+            author = committer
+        c.author = author
+        if author_timestamp is None:
+            author_timestamp = commit_timestamp
+        c.author_time = int(author_timestamp)
+        if author_timezone is None:
+            author_timezone = commit_timezone
+        c.author_timezone = author_timezone
+        c.message = message
+        self.object_store.add_object(c)
+        self.refs["HEAD"] = c.id
+        return c.id
+
     @classmethod
     def init(cls, path, mkdir=True):
         controldir = os.path.join(path, ".git")