repo.txt 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. >>> mkdir("myrepo")
  19. >>> repo = Repo.init("myrepo")
  20. >>> repo
  21. <Repo at 'myrepo'>
  22. You can already look a the structure of the "myrepo/.git" folder, though it
  23. is mostly empty for now.
  24. Opening an existing repository
  25. ------------------------------
  26. To reopen an existing repository, simply pass its path to the constructor
  27. of ``Repo``::
  28. >>> repo = Repo("myrepo")
  29. >>> repo
  30. <Repo at 'myrepo'>
  31. Opening the index
  32. -----------------
  33. The index is used as a staging area. Once you do a commit,
  34. the files tracked in the index will be recorded as the contents of the new
  35. commit. As mentioned earlier, only non-bare repositories have a working tree,
  36. so only non-bare repositories will have an index, too. To open the index, simply
  37. call::
  38. >>> index = repo.open_index()
  39. >>> repr(index).replace('\\\\', '/')
  40. "Index('myrepo/.git/index')"
  41. Since the repository was just created, the index will be empty::
  42. >>> list(index)
  43. []
  44. Staging new files
  45. -----------------
  46. The repository allows "staging" files. Only files can be staged - directories
  47. aren't tracked explicitly by git. Let's create a simple text file and stage it::
  48. >>> f = open('myrepo/foo', 'w')
  49. >>> f.write("monty")
  50. >>> f.close()
  51. >>> repo.stage(["foo"])
  52. It will now show up in the index::
  53. >>> list(repo.open_index())
  54. ['foo']
  55. Creating new commits
  56. --------------------
  57. Now that we have staged a change, we can commit it. The easiest way to
  58. do this is by using ``Repo.do_commit``. It is also possible to manipulate
  59. the lower-level objects involved in this, but we'll leave that for a
  60. separate chapter of the tutorial.
  61. To create a simple commit on the current branch, it is only necessary
  62. to specify the message. The committer and author will be retrieved from the
  63. repository configuration or global configuration if they are not specified::
  64. >>> commit_id = repo.do_commit(
  65. ... "The first commit", committer="Jelmer Vernooij <jelmer@samba.org>")
  66. ``do_commit`` returns the SHA1 of the commit. Since the commit was to the
  67. default branch, the repository's head will now be set to that commit::
  68. >>> repo.head() == commit_id
  69. True