瀏覽代碼

tutorial: Fix 2-change-file.txt, add example of write_tree_diff usage.

Jelmer Vernooij 14 年之前
父節點
當前提交
eb77127b3f
共有 2 個文件被更改,包括 39 次插入11 次删除
  1. 38 11
      docs/tutorial/2-change-file.txt
  2. 1 0
      dulwich/tests/__init__.py

+ 38 - 11
docs/tutorial/2-change-file.txt

@@ -7,11 +7,23 @@ As seen in the introduction, it's about making a path in a tree point to a
 new blob. The old blob will remain to compute the diff. The tree is altered
 new blob. The old blob will remain to compute the diff. The tree is altered
 and the new commit'task is to point to this new version.
 and the new commit'task is to point to this new version.
 
 
-In the following examples, we assume we still have the ``repo`` and ``tree``
-object from the previous chapter.
+In the following examples, we will need a ``repo`` and an initial commit
+(``c1``) similar to the one made in the previous chapter::
+
+  >>> from dulwich.repo import Repo
+  >>> from dulwich.objects import Blob, Tree
+  >>> repo = Repo.init("myrepo", mkdir=True)
+  >>> blob = Blob.from_string("My file content\n")
+  >>> tree = Tree()
+  >>> tree.add(0100644, "spam", blob.id)
+  >>> repo.object_store.add_object(blob)
+  >>> repo.object_store.add_object(tree)
+  >>> c1_id = repo.do_commit("Initial commit.",
+  ...     committer="JaneExample <jane@example.com>", tree=tree.id)
 
 
 Let's first build the blob::
 Let's first build the blob::
 
 
+  >>> from dulwich.objects import Blob
   >>> spam = Blob.from_string("My new file content\n")
   >>> spam = Blob.from_string("My new file content\n")
   >>> spam.id
   >>> spam.id
   '16ee2682887a962f854ebd25a61db16ef4efe49f'
   '16ee2682887a962f854ebd25a61db16ef4efe49f'
@@ -29,12 +41,14 @@ opportunity of changing its mode::
 
 
 Now let's record the change::
 Now let's record the change::
 
 
+  >>> from dulwich.objects import Commit
+  >>> from time import time
   >>> c2 = Commit()
   >>> c2 = Commit()
   >>> c2.tree = tree.id
   >>> c2.tree = tree.id
-  >>> c2.parents = [commit.id]
-  >>> c2.author = c2.committer = author
+  >>> c2.parents = [c1_id]
+  >>> c2.author = c2.committer = "John Doe <john@example.com>"
   >>> c2.commit_time = c2.author_time = int(time())
   >>> c2.commit_time = c2.author_time = int(time())
-  >>> c2.commit_timezone = c2.author_timezone = tz
+  >>> c2.commit_timezone = c2.author_timezone = 0
   >>> c2.encoding = "UTF-8"
   >>> c2.encoding = "UTF-8"
   >>> c2.message = 'Changing "spam"'
   >>> c2.message = 'Changing "spam"'
 
 
@@ -44,18 +58,31 @@ may happen to have several parents after merging branches.
 
 
 Remain to record this whole new family::
 Remain to record this whole new family::
 
 
-  >>> object_store.add_object(spam)
-  >>> object_store.add_object(tree)
-  >>> object_store.add_object(c2)
+  >>> repo.object_store.add_object(spam)
+  >>> repo.object_store.add_object(tree)
+  >>> repo.object_store.add_object(c2)
 
 
 You can already ask git to introspect this commit using ``git show`` and the
 You can already ask git to introspect this commit using ``git show`` and the
-value of ``commit.id`` as an argument. You'll see the difference will the
+value of ``c2.id`` as an argument. You'll see the difference will the
 previous blob recorded as "spam".
 previous blob recorded as "spam".
 
 
+The diff between the previous head and the new one can be printed using
+write_tree_diff::
+
+  >>> from dulwich.patch import write_tree_diff
+  >>> import sys
+  >>> write_tree_diff(sys.stdout, repo.object_store, repo[c1_id].tree, tree.id)
+  diff --git a/spam b/spam
+  index c55063a..16ee268 100644
+  --- a/spam
+  +++ b/spam
+  @@ -1,1 +1,1 @@
+  -My file content
+  +My new file content
+
 You won't see it using git log because the head is still the previous
 You won't see it using git log because the head is still the previous
 commit. It's easy to remedy::
 commit. It's easy to remedy::
 
 
   >>> repo.refs['refs/heads/master'] = c2.id
   >>> repo.refs['refs/heads/master'] = c2.id
 
 
-Now all git tools will work as expected. Though don't forget that Dulwich is
-still open!
+Now all git tools will work as expected.

+ 1 - 0
dulwich/tests/__init__.py

@@ -81,6 +81,7 @@ def test_suite():
     tutorial = [
     tutorial = [
         '0-introduction',
         '0-introduction',
         '1-initial-commit',
         '1-initial-commit',
+        '2-change-file',
         ]
         ]
     tutorial_files = ["../../docs/tutorial/%s.txt" % name for name in tutorial]
     tutorial_files = ["../../docs/tutorial/%s.txt" % name for name in tutorial]
     def setup(test):
     def setup(test):