GitPython的簡單說明

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

1、 GitDB

2、 async

3、smmap

由於使用easy_install GitPython失敗,於是使用 sudo python setup.py install 來安裝,本人機器上的版本如下,:

  1. async-0.6.1.tar.gz   
  2. smmap-0.8.2.tar.gz  
  3. gitdb-0.5.4.tar.gz   
  4. GitPython-0.3.1-beta2.tar.gz 

命令簡介:

  1. ####head
  2. >>> from git import * 
  3. >>> repo = Repo.init("/home/test/.git") 
  4. >>> repo.heads 
  5. [<git.Head "refs/heads/rt24-build">
  6. >>> repo.heads[0]
    <git.Head "refs/heads/rt24-build">
  7. >>> repo.head
    <git.HEAD "HEAD">
  8. >>> repo.head.reference
    <git.Head "refs/heads/rt24-build">

 

  1. ####commit
  2. >>> repo.head.commit
    <git.Commit "4500d8151fcdd93087cb599544120ead6f943aa7">
  3. >>> repo.commit('rt24-build')
    <git.Commit "4500d8151fcdd93087cb599544120ead6f943aa7">
  4. >>> repo.commit('HEAD~')
    <git.Commit "04b49ab467ad96bd9c9c7844495b80d1959e716b">
  5. >>> repo.head.commit.parents
    (<git.Commit "04b49ab467ad96bd9c9c7844495b80d1959e716b">,)
  6. >>> repo.head.commit.parents[0]
    <git.Commit "04b49ab467ad96bd9c9c7844495b80d1959e716b">
    >>> repo.head.commit.parents[0].parents[0]
    <git.Commit "aa1b8964c4ec1f994e1b54b1a223db005cb90b16">
  7. >>> repo.commit('HEAD~').parents
    (<git.Commit "aa1b8964c4ec1f994e1b54b1a223db005cb90b16">,)
  1. #from http://packages.python.org/GitPython/0.3.1/tutorial.html
  2. headcommit = repo.head.commit 
  3.  
  4. headcommit.hexsha 
  5. '207c0c4418115df0d30820ab1a9acd2ea4bf4431' 
  6.  
  7. headcommit.parents 
  8. (<git.Commit "a91c45eee0b41bf3cdaad3418ca3850664c4a4b4">,) 
  9.  
  10. headcommit.tree 
  11. <git.Tree "563413aedbeda425d8d9dcbb744247d0c3e8a0ac"> 
  12.  
  13. headcommit.author 
  14. <git.Actor "Michael Trier <mtrier@gmail.com>"> 
  15.  
  16. headcommit.authored_date        # seconds since epoch 
  17. 1256291446 
  18.  
  19. headcommit.committer 
  20. <git.Actor "Michael Trier <mtrier@gmail.com>"> 
  21.  
  22. headcommit.committed_date 
  23. 1256291446 
  24.  
  25. headcommit.message 
  26. 'cleaned up a lot of test information. Fixed escaping so it works with 
  27. subprocess.' 
  1. ####tree 
  2. >>> tree = repo.head.commit.tree 
  3. >>> tree 
  4. <git.Tree "94cdc2be1511d765f9bc2fb05c85bda9e729d902"> 
  5. >>> tree.trees 
  6. [<git.Tree "780675fdf24f573f4d11682d804a68422215f345">
  7. <git.Tree "5844c84155668a2a34c5ce735433e66926904064">
  8. <git.Tree "b0e3ec09efc8ec093d4d37c3fa833464d29851a2">
  9. <git.Tree "0fc969b876f0bdbfb8d24dfaa28b5dd604487813">
  10. <git.Tree "4a801fd4d8874efc7422b65b46ec5446d08269a7">
  11. <git.Tree "22907f24d136e4a8a3f0d6dea74658b222fa4885">
  12. >>> tree[0].name 
  13. '.gitignore' 
  14. >>> tree[0].path 
  15. '.gitignore' 
  16. >>> tree[0].abspath 
  17. '/home/test/.gitignore' 

關於tree:是repo的目錄結構,git show 94cdc2be1511d765f9bc2fb05c85bda9e729d902 能顯示該目錄下的所以目錄和文件,每一個目錄需要一個tree,從tree.trees的輸出有6項知道,該目錄下有6個目錄。可以對這6個目錄分別遞歸顯示其下的目錄和文件。

 直接使用git命令:

 GitPython源代碼cmd.py文件中 Git::_call_process 函數提供直接使用git命令接口。

  1. >>> print repo.git.show('4500d8151fcd') 
  2. commit 4500d8151fcdd93087cb599544120ead6f943aa7 ......
  3. >>> repo.git.log('-1') 
  4. '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也能安裝,但是接口會有很大的不一樣,例如:

前者安裝的

  1. >>> dir(git.Repo) 
  2. ['DAEMON_EXPORT_FILE', '__class__', '__delattr__', '__doc__', '__eq__', '__format__',  
  3. '__getattribute__', '__hash__', '__init__', '__module__', '__ne__', '__new__', '__reduce__',  
  4. '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__',  
  5. '__subclasshook__', '_bare', '_clone', '_get_alternates', '_get_config_path',  
  6. '_set_alternates', '_working_tree_dir', 'active_branch', 'alternates', 'archive', 'bare', 
  7.  'blame', 'branches', 'clone', 'clone_from', 'commit', 'config_level', 'config_reader',  
  8. 'config_writer', 'create_head', 'create_remote', 'create_submodule', 'create_tag', 'daemon_export', 
  9.  'delete_head', 'delete_remote', 'delete_tag', 'description', 'git', 'git_dir', 'head', 'heads',  
  10. 'index', 'init', 'is_dirty', 'iter_commits', 'iter_submodules', 'iter_trees', 'odb',  
  11. 're_author_committer_start', 're_hexsha_only', 're_hexsha_shortened', 're_tab_full_line',  
  12. 're_whitespace', 'references', 'refs', 'remote', 'remotes', 'rev_parse', 'submodule',  
  13. 'submodule_update', 'submodules', 'tag', 'tags', 'tree', 'untracked_files', 'working_dir',  
  14. 'working_tree_dir'] 

apt 安裝的是 python-git 0.1.6-1

  1. >>> dir(git.Repo) 
  2. ['DAEMON_EXPORT_FILE', '__class__', '__delattr__', '__dict__', '__doc__', '__format__',  
  3. '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__',  
  4. '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',  
  5. '__weakref__', '_get_alternates', '_set_alternates', 'active_branch', 'alternates', 'archive_tar',  
  6. 'archive_tar_gz', 'blob', 'branches', 'commit', 'commit_count', 'commit_deltas_from', 'commit_diff',  
  7. 'commits', 'commits_between', 'commits_since', 'create', 'daemon_export', 'description', 'diff', 
  8.  'fork_bare', 'heads', 'init_bare', 'is_dirty', 'log', 'tags', 'tree'] 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章