2
0

1-initial-commit 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. ================
  2. Dulwich Tutorial
  3. ================
  4. The Repository
  5. ==============
  6. After this introduction, let's start directly with code::
  7. >>> from dulwich.repo import Repo
  8. The access to every object is through the Repo object. You can open an
  9. existing repository or you can create a new one. There are two types of Git
  10. repositories:
  11. Regular Repositories -- They are the ones you create using "git init" and
  12. you daily use. They contain a ".git" folder.
  13. Bare Repositories -- There is not ".git" folder. The top-level folder
  14. contains itself the "branches", "hooks"... folders. These are used for
  15. published repositories (mirrors).
  16. Let's create a folder and turn it into a repository, like "git init" would::
  17. >>> from os import mkdir
  18. >>> mkdir("myrepo")
  19. >>> repo = Repo.init("myrepo")
  20. >>> repo
  21. <Repo at '/tmp/myrepo/'>
  22. You can already look a the structure of the "myrepo/.git" folder, though it
  23. is mostly empty for now.
  24. Initial commit
  25. ==============
  26. When you use Git, you generally add or modify content. As our repository is
  27. empty for now, we'll start by adding a new file::
  28. >>> from dulwich.objects import Blob
  29. >>> blob = Blob.from_string("My file content")
  30. >>> blob.id
  31. '456a1e689eb87b947be24562e830421cd799388c'
  32. Of course you could create a blob from an existing file using "from_file"
  33. instead.
  34. As said in the introduction, file content is separed from file name. Let's
  35. give this content a name::
  36. >>> from dulwich.objects import Tree
  37. >>> tree = Tree()
  38. >>> tree.add(0100644, "spam", blob.id)
  39. Note that "0100644" is the octal form for a regular file with common
  40. permissions. You can hardcode them or you can use the ``stat`` module.
  41. The tree state of our repository still needs to be placed in time. That's the
  42. job of the commit::
  43. >>> from dulwich.objects import Commit
  44. >>> from time import time
  45. >>> commit = Commit()
  46. >>> commit.tree = tree.id
  47. >>> commit.author = commit.committer = "Your Name <your.email@example.com>"
  48. >>> commit.commit_time = commit.author_time = int(time())
  49. >>> tz = parse_timezone('-0200')
  50. >>> commit.commit_timezone = commit.author_timezone = tz
  51. >>> commit.encoding = "UTF-8"
  52. >>> commit.message = "Initial commit"
  53. Note that the initial commit has no parents.
  54. At this point, the repository is still empty because all operations happen in
  55. memory. Let's "commit" it.
  56. >>> object_store = repo.object_store
  57. >>> object_store.add_object(blob)
  58. Now the ".git/objects" folder contains a first SHA-1 file. Let's continue
  59. saving the changes::
  60. >>> object_store.add_object(tree)
  61. >>> object_store.add_object(commit)
  62. Now the physical repository contains three objects but still has no branch.
  63. Let's create the master branch like Git would::
  64. >>> repo.refs['refs/heads/master'] = commit.id
  65. The master branch now has a commit where to start, but Git itself would not
  66. known what is the current branch. That's another reference::
  67. >>> repo.refs['HEAD'] = 'ref: refs/heads/master'
  68. Now our repository is officialy tracking a branch named "master" refering to a
  69. single commit.
  70. Playing again with Git
  71. ======================
  72. At this point you can come back to the shell, go into the "myrepo" folder and
  73. type "git status" to let Git confirm that this is a regular repository on
  74. branch "master".
  75. Git will tell you that the file "spam" is deleted, which is normal because
  76. Git is comparing the repository state with the current working copy. And we
  77. have absolutely no working copy using Dulwich because we don't need it at
  78. all!
  79. You can checkout the last state using ``git checkout -f``. The force flag
  80. will prevent Git from complaining that there are uncommitted changes in the
  81. checkout.
  82. The file ``spam`` appears and with no surprise::
  83. $ cat spam
  84. My file content
  85. contains the same bytes as the blob.
  86. Remember to reload the repository when you modify it outside of Dulwich!