GitPythonでgitを観察する

とりあえず各種オブジェクトとHEADとbranchesとtagsとindexの変化を観察できることを確認したけど、あと何が必要だろう。

# easy_install GitPython

http://packages.python.org/GitPython/0.3.1/intro.html#getting-started

>>> from git import *
>>> r = Repo(".")
>>> r.index.entries
{('README', 0): (100644, 432a4b6de3c8318e1e59178492608cc10e48b00a, 0, README)}

https://github.com/gitpython-developers/gitdb/blob/master/doc/source/tutorial.rst

>>> for k in r.odb.sha_iter():
...     r.odb.info(k)
... 
('"b\xde\x0c\x12\x1f"\xdf\x8ex\xf5\xa3}n\x11O\xd3"\xc0\xb0', 'blob', 5)
('C*Km\xe3\xc81\x8e\x1eY\x17\x84\x92`\x8c\xc1\x0eH\xb0\n', 'blob', 10)
("I'T\x08\xd5T\xd5[\xaa\xd5\x99\xe5\xb4\x19\xed\x81\xecn\xbd0", 'commit', 190)
('\xd2\x12\x83\xc7\x04E\x08\xcf:J\xac\xce\xfd\x91x\xf6u\x9c#\xda', 'tree', 34)

>>> for k in r.odb.sha_iter():
...     r.odb.stream(k).read()
... 
'hoge\n'
'hoge\nhoge\n'
'tree d21283c7044508cf3a4aaccefd9178f6759c23da\nauthor NISHIO Hirokazu <nishio.hirokazu@gmail.com> 1303459640 +0900\ncommitter NISHIO Hirokazu <nishio.hirokazu@gmail.com> 1303459640 +0900\n\nmsg\n'
'100644 README\x00"b\xde\x0c\x12\x1f"\xdf\x8ex\xf5\xa3}n\x11O\xd3"\xc0\xb0'
>>> r.branches # equals to r.heads?
[<git.Head "refs/heads/master">]
>>> r.refs
[<git.Head "refs/heads/master">]
>>> r.tags
[]

[1]+  Stopped                 python
f$ git branch foo
f$ cat >> README
hogehoge
f$ git commit -a
Waiting for Emacs...
[master 4c1458d] hogehoge
 1 files changed, 1 insertions(+), 0 deletions(-)

f$ fg
python

>>> r.branches
[<git.Head "refs/heads/foo">, <git.Head "refs/heads/master">]
>>> [x.object for x in r.branches]
[<git.Commit "49275408d554d55baad599e5b419ed81ec6ebd30">, <git.Commit "4c1458d2547c77d3166edd081b000f87cceb218e">]

[1]+  Stopped                 python
f$ git reset HEAD^
Unstaged changes after reset:
M	README
f$ fg
python

>>> [x.object for x in r.branches]
[<git.Commit "49275408d554d55baad599e5b419ed81ec6ebd30">, <git.Commit "49275408d554d55baad599e5b419ed81ec6ebd30">]

>>> r.head.object
<git.Commit "49275408d554d55baad599e5b419ed81ec6ebd30">
>>> 

同じ内容だとblobが同一なのがわかる

index: {'hexsha': '2262de0c121f22df8e78f5a37d6e114fd322c0b0', 'stage': 0, 'flags': 6, 'path': 'README'}
index: {'hexsha': '2262de0c121f22df8e78f5a37d6e114fd322c0b0', 'stage': 0, 'flags': 10, 'path': 'foo/README'}