GitPython is a python library used to interact with git repositories. It provides abstractions of git objects for easy access of repository data, and additionally allows you to access the git repository more directly using either a pure python implementation, or the faster, but more resource intensive git command implementation.
安裝:
reference http://packages.python.org/GitPython/0.3.1/intro.html
3、smmap
由於使用easy_install GitPython失敗,於是使用 sudo python setup.py install 來安裝,本人機器上的版本如下,:
- async-0.6.1.tar.gz
- smmap-0.8.2.tar.gz
- gitdb-0.5.4.tar.gz
- GitPython-0.3.1-beta2.tar.gz
命令簡介:
- ####head
- >>> from git import *
- >>> repo = Repo.init("/home/test/.git")
- >>> repo.heads
- [<git.Head "refs/heads/rt24-build">]
- >>> repo.heads[0]
<git.Head "refs/heads/rt24-build">- >>> repo.head
<git.HEAD "HEAD">- >>> repo.head.reference
<git.Head "refs/heads/rt24-build">
- ####commit
- >>> repo.head.commit
<git.Commit "4500d8151fcdd93087cb599544120ead6f943aa7">- >>> repo.commit('rt24-build')
<git.Commit "4500d8151fcdd93087cb599544120ead6f943aa7">- >>> repo.commit('HEAD~')
<git.Commit "04b49ab467ad96bd9c9c7844495b80d1959e716b">- >>> repo.head.commit.parents
(<git.Commit "04b49ab467ad96bd9c9c7844495b80d1959e716b">,)- >>> repo.head.commit.parents[0]
<git.Commit "04b49ab467ad96bd9c9c7844495b80d1959e716b">
>>> repo.head.commit.parents[0].parents[0]
<git.Commit "aa1b8964c4ec1f994e1b54b1a223db005cb90b16">- >>> repo.commit('HEAD~').parents
(<git.Commit "aa1b8964c4ec1f994e1b54b1a223db005cb90b16">,)
- #from http://packages.python.org/GitPython/0.3.1/tutorial.html
- headcommit = repo.head.commit
- headcommit.hexsha
- '207c0c4418115df0d30820ab1a9acd2ea4bf4431'
- headcommit.parents
- (<git.Commit "a91c45eee0b41bf3cdaad3418ca3850664c4a4b4">,)
- headcommit.tree
- <git.Tree "563413aedbeda425d8d9dcbb744247d0c3e8a0ac">
- headcommit.author
- <git.Actor "Michael Trier <mtrier@gmail.com>">
- headcommit.authored_date # seconds since epoch
- 1256291446
- headcommit.committer
- <git.Actor "Michael Trier <mtrier@gmail.com>">
- headcommit.committed_date
- 1256291446
- headcommit.message
- 'cleaned up a lot of test information. Fixed escaping so it works with
- subprocess.'
- ####tree
- >>> tree = repo.head.commit.tree
- >>> tree
- <git.Tree "94cdc2be1511d765f9bc2fb05c85bda9e729d902">
- >>> tree.trees
- [<git.Tree "780675fdf24f573f4d11682d804a68422215f345">,
- <git.Tree "5844c84155668a2a34c5ce735433e66926904064">,
- <git.Tree "b0e3ec09efc8ec093d4d37c3fa833464d29851a2">,
- <git.Tree "0fc969b876f0bdbfb8d24dfaa28b5dd604487813">,
- <git.Tree "4a801fd4d8874efc7422b65b46ec5446d08269a7">,
- <git.Tree "22907f24d136e4a8a3f0d6dea74658b222fa4885">]
- >>> tree[0].name
- '.gitignore'
- >>> tree[0].path
- '.gitignore'
- >>> tree[0].abspath
- '/home/test/.gitignore'
關於tree:是repo的目錄結構,git show 94cdc2be1511d765f9bc2fb05c85bda9e729d902 能顯示該目錄下的所以目錄和文件,每一個目錄需要一個tree,從tree.trees的輸出有6項知道,該目錄下有6個目錄。可以對這6個目錄分別遞歸顯示其下的目錄和文件。
直接使用git命令:
GitPython源代碼cmd.py文件中 Git::_call_process 函數提供直接使用git命令接口。
- >>> print repo.git.show('4500d8151fcd')
- commit 4500d8151fcdd93087cb599544120ead6f943aa7 ......
- >>> repo.git.log('-1')
- 'commit 6cba56e7816dd8a7dc591bd097e8e51c5d631679\nAuthor:......'
python setup.py install 來安裝python包,如何卸載呢?
只能手動刪除安裝的文件,可以使用如下命令
python setup.py install --record files.txt 記錄安裝後文件的路徑
cat files.txt | xargs rm -rf 刪除這些文件
另外sudo apt-get install python-git也能安裝,但是接口會有很大的不一樣,例如:
前者安裝的
- >>> dir(git.Repo)
- ['DAEMON_EXPORT_FILE', '__class__', '__delattr__', '__doc__', '__eq__', '__format__',
- '__getattribute__', '__hash__', '__init__', '__module__', '__ne__', '__new__', '__reduce__',
- '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__',
- '__subclasshook__', '_bare', '_clone', '_get_alternates', '_get_config_path',
- '_set_alternates', '_working_tree_dir', 'active_branch', 'alternates', 'archive', 'bare',
- 'blame', 'branches', 'clone', 'clone_from', 'commit', 'config_level', 'config_reader',
- 'config_writer', 'create_head', 'create_remote', 'create_submodule', 'create_tag', 'daemon_export',
- 'delete_head', 'delete_remote', 'delete_tag', 'description', 'git', 'git_dir', 'head', 'heads',
- 'index', 'init', 'is_dirty', 'iter_commits', 'iter_submodules', 'iter_trees', 'odb',
- 're_author_committer_start', 're_hexsha_only', 're_hexsha_shortened', 're_tab_full_line',
- 're_whitespace', 'references', 'refs', 'remote', 'remotes', 'rev_parse', 'submodule',
- 'submodule_update', 'submodules', 'tag', 'tags', 'tree', 'untracked_files', 'working_dir',
- 'working_tree_dir']
apt 安裝的是 python-git 0.1.6-1
- >>> dir(git.Repo)
- ['DAEMON_EXPORT_FILE', '__class__', '__delattr__', '__dict__', '__doc__', '__format__',
- '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__',
- '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
- '__weakref__', '_get_alternates', '_set_alternates', 'active_branch', 'alternates', 'archive_tar',
- 'archive_tar_gz', 'blob', 'branches', 'commit', 'commit_count', 'commit_deltas_from', 'commit_diff',
- 'commits', 'commits_between', 'commits_since', 'create', 'daemon_export', 'description', 'diff',
- 'fork_bare', 'heads', 'init_bare', 'is_dirty', 'log', 'tags', 'tree']