44GitPython Tutorial
55==================
66
7- GitPython provides object model access to your git repository. Once you have
8- created a repository object, you can traverse it to find parent commit(s),
9- trees, blobs, etc.
7+ GitPython provides object model access to your git repository. This tutorial is
8+ composed of multiple sections, each of which explain a real-life usecase.
109
1110Initialize a Repo object
1211************************
@@ -22,83 +21,109 @@ initialize GitPython with a bare repository.
2221
2322 >>> repo = Repo.create(" /var/git/git-python.git" )
2423
25- Getting a list of commits
26- *************************
24+ Examining References
25+ ********************
26+
27+ References are the tips of your commit graph from which you can easily examine
28+ the history of your project.
29+
30+ >>> heads = repo.heads
31+ >>> master = heads.master # lists can be accessed by name for convenience
32+ >>> master.commit # the commit pointed to by head called master
33+ >>> master.rename(" new_name" ) # rename individual heads or
34+ >>> repo.delete_head(master) # delete them or
35+ >>> repo.create_head(' master' ) # create them
36+
37+ Tags are (usually immutable) references to a commit and/or a tag object.
38+
39+ >>> tags = repo.tags
40+ >>> tagref = tags[0 ]
41+ >>> tagref.tag # tags may have tag objects carrying additional information
42+ >>> tagref.commit # but they always point to commits
43+ >>> repo.delete_tag(tagref) # delete or
44+ >>> repo.create_tag(" my_tag" ) # create tags using the repo
45+
46+ Understanding Objects
47+ *********************
48+ An Object is anything storable in gits object database. Objects contain information
49+ about their type, their uncompressed size as well as their data. Each object is
50+ uniquely identified by a SHA1 hash, being 40 hexadecimal characters in size.
51+
52+ Git only knows 4 distinct object types being Blobs, Trees, Commits and Tags.
53+
54+ In Git-Pyhton, all objects can be accessed through their common base, compared
55+ and hashed, as shown in the following example.
56+
57+ >>>
58+
59+ Index Objects are objects that can be put into gits index. These objects are trees
60+ and blobs which additionally know about their path in the filesystem as well as their
61+ mode.
2762
28- From the `` Repo `` object, you can get a list of `` Commit ``
29- objects.
63+ The Commit object
64+ *****************
3065
31- >>> repo.commits()
32- [<git.Commit "207c0c4418115df0d30820ab1a9acd2ea4bf4431">,
33- <git.Commit "a91c45eee0b41bf3cdaad3418ca3850664c4a4b4">,
34- <git.Commit "e17c7e11aed9e94d2159e549a99b966912ce1091">,
35- <git.Commit "bd795df2d0e07d10e0298670005c0e9d9a5ed867">]
66+ Commit objects contain information about a specific commit. Obtain commits using
67+ references as done in 'Examining References' or as follows
3668
37- Called without arguments, ``Repo.commits `` returns a list of up to ten commits
38- reachable by the master branch (starting at the latest commit). You can ask
39- for commits beginning at a different branch, commit, tag, etc.
69+ Obtain commits at the specified revision:
4070
41- >>> repo.commits( ' mybranch ' )
42- >>> repo.commits( ' 40d3057d09a7a4d61059bca9dca5ae698de58cbe ' )
43- >>> repo.commits( ' v0.1 ' )
71+ >>> repo.commit( ' master ' )
72+ >>> repo.commit( ' v0.1 ' )
73+ >>> repo.commit( ' HEAD~10 ' )
4474
45- You can specify the maximum number of commits to return.
75+ Iterate 100 commits
4676
47- >>> repo.commits (' master' , max_count = 100 )
77+ >>> repo.iter_commits (' master' , max_count = 100 )
4878
4979If you need paging, you can specify a number of commits to skip.
5080
51- >>> repo.commits (' master' , max_count = 10 , skip = 20 )
81+ >>> repo.iter_commits (' master' , max_count = 10 , skip = 20 )
5282
5383The above will return commits 21-30 from the commit list.
5484
55- The Commit object
56- *****************
57-
58- Commit objects contain information about a specific commit.
59-
60- >>> head = repo.commits()[0 ]
85+ >>> headcommit = repo.headcommit.commit
6186
62- >>> head.id
87+ >>> headcommit.sha
6388 '207c0c4418115df0d30820ab1a9acd2ea4bf4431'
6489
65- >>> head .parents
90+ >>> headcommit .parents
6691 [<git.Commit "a91c45eee0b41bf3cdaad3418ca3850664c4a4b4">]
6792
68- >>> head .tree
93+ >>> headcommit .tree
6994 <git.Tree "563413aedbeda425d8d9dcbb744247d0c3e8a0ac">
7095
71- >>> head .author
96+ >>> headcommit .author
7297 <git.Actor "Michael Trier <mtrier@gmail.com>">
7398
74- >>> head .authored_date
75- (2008, 5, 7, 5, 0, 56, 2, 128, 0)
99+ >>> headcommit .authored_date # seconds since epoch
100+ 1256291446
76101
77- >>> head .committer
102+ >>> headcommit .committer
78103 <git.Actor "Michael Trier <mtrier@gmail.com>">
79104
80- >>> head .committed_date
81- (2008, 5, 7, 5, 0, 56, 2, 128, 0)
105+ >>> headcommit .committed_date
106+ 1256291446
82107
83- >>> head .message
108+ >>> headcommit .message
84109 'cleaned up a lot of test information. Fixed escaping so it works with
85110 subprocess.'
86111
87- Note: date time is represented in a `struct_time `_ format. Conversion to
112+ Note: date time is represented in a `seconds since epock `_ format. Conversion to
88113human readable form can be accomplished with the various time module methods.
89114
90115 >>> import time
91- >>> time.asctime(head. committed_date)
116+ >>> time.asctime(time.gmtime(headcommit. committed_date) )
92117 'Wed May 7 05:56:02 2008'
93118
94- >>> time.strftime(" %a , %d %b %Y %H:%M" , head. committed_date)
119+ >>> time.strftime(" %a , %d %b %Y %H:%M" , time.gmtime(headcommit. committed_date) )
95120 'Wed, 7 May 2008 05:56'
96121
97122.. _struct_time : http://docs.python.org/library/time.html
98123
99124You can traverse a commit's ancestry by chaining calls to ``parents ``.
100125
101- >>> repo.commits()[ 0 ] .parents[0 ].parents[0 ].parents[0 ]
126+ >>> headcommit .parents[0 ].parents[0 ].parents[0 ]
102127
103128The above corresponds to ``master^^^ `` or ``master~3 `` in git parlance.
104129
0 commit comments