# Load Git repo into object
from dulwich.repo import Repo
repo = Repo("/home/jjasinsk/Sites/env/proj/gitdjango/")
# Find sha hash of Git repo head
repo.head()
# 'e80b3ada92d2a02ce353c83683a6533e25240a1a'
# Find the Commit object of the repo head
repo[repo.head()]
# Commit e80b3ada92d2a02ce353c83683a6533e25240a1a
# Find all the repo tags
repo.refs.subkeys('refs/tags')
# set(['v0.0.1'])
# Find all the repo branches
repo.refs.subkeys('refs/heads')
# set(['master', 'develop'])
# Find all the repo refs and their sha hashes
repo.get_refs()
# {'HEAD': 'e80b3ada92d2a02ce353c83683a6533e25240a1a',
'refs/heads/develop': 'b53fe83f58f293decae39a70106a5ae78886ba5e',
'refs/tags/v0.0.1': '5554750c44f8dc17ab9dc5926129cf409d06d0bb',
'refs/heads/master': 'e80b3ada92d2a02ce353c83683a6533e25240a1a'}
# Find all the commits tracing back to head Commit
# Find all the commits tracing back to head Commit
repo.revision_history(repo.head())
# [Commit fcc1e1eb645f6731c4a8701b5facf852738850a9,
Commit 39eaa48156e3df2fd2cbe9922e33787cbafdad90,
Commit e80b3ada92d2a02ce353c83683a6533e25240a1a,
...
TAG OBJECTS
# Get the Tag object by looking up it's ref path
repo['refs/tags/v0.0.1']
repo['refs/tags/v0.0.1']
# Tag 5554750c44f8dc17ab9dc5926129cf409d06d0b;
# Find Tag by sha hash
repo.get_object('5554750c44f8dc17ab9dc5926129cf409d06d0bb')
# Tag 5554750c44f8dc17ab9dc5926129cf409d06d0bb
repo.tag("5554750c44f8dc17ab9dc5926129cf409d06d0bb")
# Tag 5554750c44f8dc17ab9dc5926129cf409d06d0bb
repo['5554750c44f8dc17ab9dc5926129cf409d06d0bb']
# Tag 5554750c44f8dc17ab9dc5926129cf409d06d0bb
# Find information about a tag
# Find information about a tag
t = repo['5554750c44f8dc17ab9dc5926129cf409d06d0bb']
# Find tagger
t.tagger
# 'Joe Jasinski joe.jasinski@example.com'
# Find tag date
# Find tag date
from datetime import datetime
datetime.utcfromtimestamp(t.tag_time)
datetime.datetime(2012, 3, 30, 6, 58, 53)
# Print tag contents
# Print tag contents
print t.as_pretty_string()
# ...
# Find Tag sha hash (id)
# Find Tag sha hash (id)
t.id
# '5554750c44f8dc17ab9dc5926129cf409d06d0bb'
# Find Tag message
OR
s = t.sha()
s = t.sha()
s.hexdigest()
# '5554750c44f8dc17ab9dc5926129cf409d06d0bb'
# Find Tag message
t.message
# 'test tag\n'
# Find Tag Type
# Find Tag Type
t.get_type()
# 4
COMMIT OBJECTS
# Find Commit object from ref spec
repo['refs/heads/master']
repo['refs/heads/master']
# Commit e80b3ada92d2a02ce353c83683a6533e25240a1a
# Look up commit several different ways
repo.get_object('b53fe83f58f293decae39a70106a5ae78886ba5e')
# Commit b53fe83f58f293decae39a70106a5ae78886ba5e
repo.commit("b53fe83f58f293decae39a70106a5ae78886ba5e")
# Commit b53fe83f58f293decae39a70106a5ae78886ba5e
repo['b53fe83f58f293decae39a70106a5ae78886ba5e']
# Commit b53fe83f58f293decae39a70106a5ae78886ba5e
# Find Commit hash
repo.ref('refs/heads/master')
# 'e80b3ada92d2a02ce353c83683a6533e25240a1a'
# Find commit author
repo['refs/heads/master'].author
# 'Joe Jasinski joe.jasinski@example.com'
# Find committer
repo['refs/heads/master'].committer
# 'Joe Jasinski joe.jasinski@example.com'
# Find Commit message
repo['refs/heads/master'].message
# 'updated style\n'
# Find commit time
from datetime import datetime
datetime.utcfromtimestamp(repo['refs/heads/master'].commit_time)
# Find Trees associated with commit
repo['b53fe83f58f293decae39a70106a5ae78886ba5e'].tree
'b3437c8329fc3f6306936c7ca1986cefc1629862'
repo[c.tree].items()
[TreeEntry(path='.gitignore', mode=33188, sha='13609a506289eba22b61df5eab0f89b7aede323d'),
TreeEntry(path='example', mode=16384, sha='f8e95712570c4316868ee9af32088324e8d3e498'),
TreeEntry(path='gitdjango', mode=16384, sha='32b164fac85e5b40dd8d4265a0a8df765bf57300'),
TreeEntry(path='manage.py', mode=33261, sha='2605e3768e2bab92ef0550d452036472e9a6a210'),
...
repo[repo[c.tree].items()[0].sha]
TREE OBJECTS
# Find Tree associated with hash
repo.get_object('b3437c8329fc3f6306936c7ca1986cefc1629862')
repo.get_object('b3437c8329fc3f6306936c7ca1986cefc1629862')
# Tree b3437c8329fc3f6306936c7ca1986cefc1629862
# Find all items under a Tree
repo[b3437c8329fc3f6306936c7ca1986cefc1629862].items()
# Find all items under a Tree
repo[b3437c8329fc3f6306936c7ca1986cefc1629862].items()
BLOB OBJECTS
# Find Blob associated with hash
repo.get_blob("2605e3768e2bab92ef0550d452036472e9a6a210")
# Blob 2605e3768e2bab92ef0550d452036472e9a6a210
repo['2605e3768e2bab92ef0550d452036472e9a6a210']
# Blob 2605e3768e2bab92ef0550d452036472e9a6a210
MISC
# Iterate through all objects under a gree
for entry in repo.object_store.iter_tree_contents(repo['refs/heads/master'].tree):
for entry in repo.object_store.iter_tree_contents(repo['refs/heads/master'].tree):
print entry
# TreeEntry(path='.gitignore', mode=33188, sha='13609a506289eba22b61df5eab0f89b7aede323d')
# TreeEntry(path='example/__init__.py', mode=33188, sha='e69de29bb2d1d6434b8b29ae775ad8c2e48c5391')
# TreeEntry(path='example/__init__.pyc', mode=33188, sha='ced988bb5f3f22f4f1956bf852f106b23eea5c09')
# ...
# Find a the hash of an object by specifying a filesystem path
from dulwich.object_store import tree_lookup_path
(mode, hash) = tree_lookup_path(repo.get_object, repo['refs/heads/master'].tree, 'gitdjango/models.py')
# Find an object filehandle relative to inside the .git directory
repo.get_named_file('../requirements.pip')
# open file '/home/jjasinsk/Sites/env/proj/gitdjango/.git/../requirements.pip', mode 'rb' at 0x101cdf938
f.read()
# 'Django==1.4\nGitPython==0.3.2.RC1\nasync==0.6.1\ngitdb==0.5.4\ninclude-server==3.1-toolwhip.1\nsmmap==0.8.2\nwsgiref==0.1.2\n'