Forgot to add Mock to the requirements.
[jelmer/gitpython.git] / README
1 ==========
2 GitPython
3 ==========
4
5 GitPython is a python library used to interact with Git repositories.
6
7 GitPython is a port of the grit_ library in Ruby created by 
8 Tom Preston-Werner and Chris Wanstrath.
9
10 .. _grit: http://grit.rubyforge.org
11
12 The ``method_missing`` stuff was `taken from this blog post`_.
13
14 .. _taken from this blog post: http://blog.iffy.us/?p=43
15
16 REQUIREMENTS
17 ============
18
19 * Git_ tested with 1.5.3.7
20 * `Python Nose`_ - used for running the tests  
21 * `Mock by Michael Foord`_ used for tests
22
23 .. _Git: http://git.or.cz/
24 .. _Python Nose: http://code.google.com/p/python-nose/
25 .. _Mock by Michael Foord: http://www.voidspace.org.uk/python/mock.html
26
27 INSTALL
28 =======
29
30         python setup.py install
31
32 SOURCE
33 ======
34
35 GitPython's git repo is available on Gitorious, which can be browsed at:
36
37 http://gitorious.org/projects/git-python/
38
39 and cloned from:
40
41 git://gitorious.org/git-python/mainline.git
42
43 USAGE
44 =====
45
46 GitPython provides object model access to your git repository. Once you have
47 created a repository object, you can traverse it to find parent commit(s),
48 trees, blobs, etc.
49
50 Initialize a Repo object
51 ************************
52
53 The first step is to create a ``Repo`` object to represent your repository.
54
55         >>> from git_python import *
56         >>> repo = Repo("/Users/mtrier/Development/git-python")
57   
58 In the above example, the directory ``/Users/mtrier/Development/git-python`` 
59 is my working repository and contains the ``.git`` directory. You can also 
60 initialize GitPython with a bare repository.
61
62         >>> repo = Repo.init_bare("/var/git/git-python.git")
63
64 Getting a list of commits
65 *************************
66
67 From the ``Repo`` object, you can get a list of ``Commit``
68 objects.
69
70         >>> repo.commits()
71         [<GitPython.Commit "207c0c4418115df0d30820ab1a9acd2ea4bf4431">, 
72          <GitPython.Commit "a91c45eee0b41bf3cdaad3418ca3850664c4a4b4">, 
73          <GitPython.Commit "e17c7e11aed9e94d2159e549a99b966912ce1091">, 
74          <GitPython.Commit "bd795df2d0e07d10e0298670005c0e9d9a5ed867">]
75
76 Called without arguments, ``Repo.commits`` returns a list of up to ten commits
77 reachable by the master branch (starting at the latest commit). You can ask
78 for commits beginning at a different branch, commit, tag, etc.
79
80         >>> repo.commits('mybranch')
81         >>> repo.commits('40d3057d09a7a4d61059bca9dca5ae698de58cbe')
82         >>> repo.commits('v0.1')
83
84 You can specify the maximum number of commits to return.
85
86         >>> repo.commits('master', 100)
87   
88 If you need paging, you can specify a number of commits to skip.
89
90         >>> repo.commits('master', 10, 20)
91
92 The above will return commits 21-30 from the commit list.
93         
94 The Commit object
95 *****************
96
97 Commit objects contain information about a specific commit.
98
99         >>> head = repo.commits()[0]
100
101         >>> head.id
102         '207c0c4418115df0d30820ab1a9acd2ea4bf4431'
103
104         >>> head.parents
105         [<GitPython.Commit "a91c45eee0b41bf3cdaad3418ca3850664c4a4b4">]
106         
107         >>> head.tree
108         <GitPython.Tree "563413aedbeda425d8d9dcbb744247d0c3e8a0ac">
109         
110         >>> head.author
111         <GitPython.Actor "Michael Trier <mtrier@gmail.com>">
112         
113         >>> head.authored_date
114         (2008, 5, 7, 5, 0, 56, 2, 128, 0)
115         
116         >>> head.committer
117         <GitPython.Actor "Michael Trier <mtrier@gmail.com>">
118         
119         >>> head.committed_date
120         (2008, 5, 7, 5, 0, 56, 2, 128, 0)
121         
122         >>> head.message
123         'cleaned up a lot of test information. Fixed escaping so it works with subprocess.'
124
125
126 You can traverse a commit's ancestry by chaining calls to ``parents``.
127
128         >>> repo.commits()[0].parents[0].parents[0].parents[0]
129   
130 The above corresponds to ``master^^^`` or ``master~3`` in git parlance.
131
132 The Tree object
133 ***************
134
135 A tree records pointers to the contents of a directory. Let's say you want
136 the root tree of the latest commit on the master branch.
137
138         >>> tree = repo.commits()[0].tree
139         <GitPython.Tree "a006b5b1a8115185a228b7514cdcd46fed90dc92">
140
141         >>> tree.id
142         'a006b5b1a8115185a228b7514cdcd46fed90dc92'
143   
144 Once you have a tree, you can get the contents.
145
146         >>> contents = tree.contents
147         [<GitPython.Blob "6a91a439ea968bf2f5ce8bb1cd8ddf5bf2cad6c7">, 
148          <GitPython.Blob "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391">, 
149          <GitPython.Tree "eaa0090ec96b054e425603480519e7cf587adfc3">, 
150          <GitPython.Blob "980e72ae16b5378009ba5dfd6772b59fe7ccd2df">]
151
152 This tree contains three ``Blob`` objects and one ``Tree`` object. The trees are
153 subdirectories and the blobs are files. Trees below the root have additional
154 attributes.
155
156         >>> contents = tree.contents[-2]
157         <GitPython.Tree "e5445b9db4a9f08d5b4de4e29e61dffda2f386ba">
158         
159         >>> contents.name
160         'test'
161         
162         >>> contents.mode
163         '040000'
164         
165 There is a convenience method that allows you to get a named sub-object
166 from a tree.
167
168         >>> tree/"lib"
169         <GitPython.Tree "c1c7214dde86f76bc3e18806ac1f47c38b2b7a30">
170   
171 You can also get a tree directly from the repository if you know its name.
172
173         >>> repo.tree()
174         <GitPython.Tree "master">
175         
176         >>> repo.tree("c1c7214dde86f76bc3e18806ac1f47c38b2b7a30")
177         <GitPython.Tree "c1c7214dde86f76bc3e18806ac1f47c38b2b7a30">
178
179 The Blob object
180 ***************
181
182 A blob represents a file. Trees often contain blobs.
183
184         >>> blob = tree.contents[-1]
185         <GitPython.Blob "b19574431a073333ea09346eafd64e7b1908ef49">
186
187 A blob has certain attributes.
188
189         >>> blob.name
190         'urls.py'
191         
192         >>> blob.mode
193         '100644'
194
195         >>> blob.mime_type
196         'text/x-python'
197         
198         >>> len(blob)
199         415
200   
201 You can get the data of a blob as a string.
202
203         >>> blob.data
204         "from django.conf.urls.defaults import *\nfrom django.conf..."
205
206 You can also get a blob directly from the repo if you know its name.
207
208         >>> repo.blob("b19574431a073333ea09346eafd64e7b1908ef49")
209         <GitPython.Blob "b19574431a073333ea09346eafd64e7b1908ef49">
210
211 What Else?
212 **********
213
214 There is more stuff in there, like the ability to tar or gzip repos, stats, blame, and probably a few other things.  Additionally calls to the git instance are handled through a ``method_missing`` construct, which makes available any git commands directly, with a nice conversion of Python dicts to command line parameters.
215
216 Check the unit tests, they're pretty exhaustive.
217
218 LICENSE
219 =======
220
221 New BSD License.  See the LICENSE file.