5 GitPython provides object model access to your git repository. Once you have
6 created a repository object, you can traverse it to find parent commit(s),
9 Initialize a Repo object
10 ************************
12 The first step is to create a ``Repo`` object to represent your repository.
15 >>> repo = Repo("/Users/mtrier/Development/git-python")
17 In the above example, the directory ``/Users/mtrier/Development/git-python``
18 is my working repository and contains the ``.git`` directory. You can also
19 initialize GitPython with a bare repository.
21 >>> repo = Repo.create("/var/git/git-python.git")
23 Getting a list of commits
24 *************************
26 From the ``Repo`` object, you can get a list of ``Commit``
30 [<GitPython.Commit "207c0c4418115df0d30820ab1a9acd2ea4bf4431">,
31 <GitPython.Commit "a91c45eee0b41bf3cdaad3418ca3850664c4a4b4">,
32 <GitPython.Commit "e17c7e11aed9e94d2159e549a99b966912ce1091">,
33 <GitPython.Commit "bd795df2d0e07d10e0298670005c0e9d9a5ed867">]
35 Called without arguments, ``Repo.commits`` returns a list of up to ten commits
36 reachable by the master branch (starting at the latest commit). You can ask
37 for commits beginning at a different branch, commit, tag, etc.
39 >>> repo.commits('mybranch')
40 >>> repo.commits('40d3057d09a7a4d61059bca9dca5ae698de58cbe')
41 >>> repo.commits('v0.1')
43 You can specify the maximum number of commits to return.
45 >>> repo.commits('master', 100)
47 If you need paging, you can specify a number of commits to skip.
49 >>> repo.commits('master', 10, 20)
51 The above will return commits 21-30 from the commit list.
56 Commit objects contain information about a specific commit.
58 >>> head = repo.commits()[0]
61 '207c0c4418115df0d30820ab1a9acd2ea4bf4431'
64 [<GitPython.Commit "a91c45eee0b41bf3cdaad3418ca3850664c4a4b4">]
67 <GitPython.Tree "563413aedbeda425d8d9dcbb744247d0c3e8a0ac">
70 <GitPython.Actor "Michael Trier <mtrier@gmail.com>">
72 >>> head.authored_date
73 (2008, 5, 7, 5, 0, 56, 2, 128, 0)
76 <GitPython.Actor "Michael Trier <mtrier@gmail.com>">
78 >>> head.committed_date
79 (2008, 5, 7, 5, 0, 56, 2, 128, 0)
82 'cleaned up a lot of test information. Fixed escaping so it works with
85 Note: date time is represented in a `struct_time`_ format. Conversion to
86 human readable form can be accomplished with the various time module methods.
89 >>> time.asctime(head.committed_date)
90 'Wed May 7 05:56:02 2008'
92 >>> time.strftime("%a, %d %b %Y %H:%M", head.committed_date)
93 'Wed, 7 May 2008 05:56'
95 .. _struct_time: http://docs.python.org/lib/module-time.html
97 You can traverse a commit's ancestry by chaining calls to ``parents``.
99 >>> repo.commits()[0].parents[0].parents[0].parents[0]
101 The above corresponds to ``master^^^`` or ``master~3`` in git parlance.
106 A tree records pointers to the contents of a directory. Let's say you want
107 the root tree of the latest commit on the master branch.
109 >>> tree = repo.commits()[0].tree
110 <GitPython.Tree "a006b5b1a8115185a228b7514cdcd46fed90dc92">
113 'a006b5b1a8115185a228b7514cdcd46fed90dc92'
115 Once you have a tree, you can get the contents.
117 >>> contents = tree.contents
118 [<GitPython.Blob "6a91a439ea968bf2f5ce8bb1cd8ddf5bf2cad6c7">,
119 <GitPython.Blob "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391">,
120 <GitPython.Tree "eaa0090ec96b054e425603480519e7cf587adfc3">,
121 <GitPython.Blob "980e72ae16b5378009ba5dfd6772b59fe7ccd2df">]
123 This tree contains three ``Blob`` objects and one ``Tree`` object. The trees
124 are subdirectories and the blobs are files. Trees below the root have
125 additional attributes.
127 >>> contents = tree.contents[-2]
128 <GitPython.Tree "e5445b9db4a9f08d5b4de4e29e61dffda2f386ba">
136 There is a convenience method that allows you to get a named sub-object
140 <GitPython.Tree "c1c7214dde86f76bc3e18806ac1f47c38b2b7a30">
142 You can also get a tree directly from the repository if you know its name.
145 <GitPython.Tree "master">
147 >>> repo.tree("c1c7214dde86f76bc3e18806ac1f47c38b2b7a30")
148 <GitPython.Tree "c1c7214dde86f76bc3e18806ac1f47c38b2b7a30">
153 A blob represents a file. Trees often contain blobs.
155 >>> blob = tree.contents[-1]
156 <GitPython.Blob "b19574431a073333ea09346eafd64e7b1908ef49">
158 A blob has certain attributes.
172 You can get the data of a blob as a string.
175 "from django.conf.urls.defaults import *\nfrom django.conf..."
177 You can also get a blob directly from the repo if you know its name.
179 >>> repo.blob("b19574431a073333ea09346eafd64e7b1908ef49")
180 <GitPython.Blob "b19574431a073333ea09346eafd64e7b1908ef49">
185 There is more stuff in there, like the ability to tar or gzip repos, stats,
186 log, blame, and probably a few other things. Additionally calls to the git
187 instance are handled through a ``__getattr__`` construct, which makes
188 available any git commands directly, with a nice conversion of Python dicts
189 to command line parameters.
191 Check the unit tests, they're pretty exhaustive.