Browse Source

Add convenience function for creating a new commit in a git repository.

Jelmer Vernooij 15 năm trước cách đây
mục cha
commit
94281c2930
3 tập tin đã thay đổi với 61 bổ sung1 xóa
  1. 13 0
      bin/dulwich
  2. 2 1
      dulwich/index.py
  3. 46 0
      dulwich/repo.py

+ 13 - 0
bin/dulwich

@@ -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,

+ 2 - 1
dulwich/index.py

@@ -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):

+ 46 - 0
dulwich/repo.py

@@ -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")