repo.txt 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. .. _tutorial-repo:
  2. The repository
  3. ==============
  4. After this introduction, let's start directly with code::
  5. >>> from dulwich.repo import Repo
  6. The access to a repository is through the Repo object. You can open an
  7. existing repository or you can create a new one. There are two types of Git
  8. repositories:
  9. Regular Repositories -- They are the ones you create using ``git init`` and
  10. you daily use. They contain a ``.git`` folder.
  11. Bare Repositories -- There is no ".git" folder. The top-level folder
  12. contains itself the "branches", "hooks"... folders. These are used for
  13. published repositories (mirrors). They do not have a working tree.
  14. Creating a repository
  15. ---------------------
  16. Let's create a folder and turn it into a repository, like ``git init`` would::
  17. >>> from os import mkdir
  18. >>> import sys
  19. >>> mkdir("myrepo")
  20. >>> repo = Repo.init("myrepo")
  21. >>> repo
  22. <Repo at 'myrepo'>
  23. You can already look at the structure of the "myrepo/.git" folder, though it
  24. is mostly empty for now.
  25. Opening an existing repository
  26. ------------------------------
  27. To reopen an existing repository, simply pass its path to the constructor
  28. of ``Repo``::
  29. >>> repo = Repo("myrepo")
  30. >>> repo
  31. <Repo at 'myrepo'>
  32. Opening the index
  33. -----------------
  34. The index is used as a staging area. Once you do a commit,
  35. the files tracked in the index will be recorded as the contents of the new
  36. commit. As mentioned earlier, only non-bare repositories have a working tree,
  37. so only non-bare repositories will have an index, too. To open the index, simply
  38. call::
  39. >>> index = repo.open_index()
  40. >>> print(index.path)
  41. myrepo/.git/index
  42. Since the repository was just created, the index will be empty::
  43. >>> list(index)
  44. []
  45. Staging new files
  46. -----------------
  47. The repository allows "staging" files. Only files can be staged - directories
  48. aren't tracked explicitly by git. Let's create a simple text file and stage it::
  49. >>> f = open('myrepo/foo', 'wb')
  50. >>> _ = f.write(b"monty")
  51. >>> f.close()
  52. >>> repo.stage([b"foo"])
  53. It will now show up in the index::
  54. >>> print(",".join([f.decode(sys.getfilesystemencoding()) for f in repo.open_index()]))
  55. foo
  56. Creating new commits
  57. --------------------
  58. Now that we have staged a change, we can commit it. The easiest way to
  59. do this is by using ``Repo.do_commit``. It is also possible to manipulate
  60. the lower-level objects involved in this, but we'll leave that for a
  61. separate chapter of the tutorial.
  62. To create a simple commit on the current branch, it is only necessary
  63. to specify the message. The committer and author will be retrieved from the
  64. repository configuration or global configuration if they are not specified::
  65. >>> commit_id = repo.do_commit(
  66. ... b"The first commit", committer=b"Jelmer Vernooij <jelmer@samba.org>")
  67. ``do_commit`` returns the SHA1 of the commit. Since the commit was to the
  68. default branch, the repository's head will now be set to that commit::
  69. >>> repo.head() == commit_id
  70. True