6 After this introduction, let's start directly with code::
8 >>> from dulwich.repo import Repo
10 The access to a repository is through the Repo object. You can open an
11 existing repository or you can create a new one. There are two types of Git
14 Regular Repositories -- They are the ones you create using ``git init`` and
15 you daily use. They contain a ``.git`` folder.
17 Bare Repositories -- There is no ".git" folder. The top-level folder
18 contains itself the "branches", "hooks"... folders. These are used for
19 published repositories (mirrors). They do not have a working tree.
24 Let's create a folder and turn it into a repository, like ``git init`` would::
26 >>> from os import mkdir
29 >>> repo = Repo.init("myrepo")
33 You can already look at the structure of the "myrepo/.git" folder, though it
34 is mostly empty for now.
36 Opening an existing repository
37 ------------------------------
39 To reopen an existing repository, simply pass its path to the constructor
42 >>> repo = Repo("myrepo")
49 The index is used as a staging area. Once you do a commit,
50 the files tracked in the index will be recorded as the contents of the new
51 commit. As mentioned earlier, only non-bare repositories have a working tree,
52 so only non-bare repositories will have an index, too. To open the index, simply
55 >>> index = repo.open_index()
56 >>> print(index.path.decode(sys.getfilesystemencoding()))
59 Since the repository was just created, the index will be empty::
67 The repository allows "staging" files. Only files can be staged - directories
68 aren't tracked explicitly by git. Let's create a simple text file and stage it::
70 >>> f = open('myrepo/foo', 'wb')
71 >>> _ = f.write(b"monty")
74 >>> repo.stage([b"foo"])
76 It will now show up in the index::
78 >>> print(",".join([f.decode(sys.getfilesystemencoding()) for f in repo.open_index()]))
85 Now that we have staged a change, we can commit it. The easiest way to
86 do this is by using ``Repo.do_commit``. It is also possible to manipulate
87 the lower-level objects involved in this, but we'll leave that for a
88 separate chapter of the tutorial.
90 To create a simple commit on the current branch, it is only necessary
91 to specify the message. The committer and author will be retrieved from the
92 repository configuration or global configuration if they are not specified::
94 >>> commit_id = repo.do_commit(
95 ... b"The first commit", committer=b"Jelmer Vernooij <jelmer@samba.org>")
97 ``do_commit`` returns the SHA1 of the commit. Since the commit was to the
98 default branch, the repository's head will now be set to that commit::
100 >>> repo.head() == commit_id