GitPython git pythonの開発ライブラリ
7470 ワード
工事住所:https://pypi.python.org/pypi/GitPython/インストール先インストールが必要:gitdbhttps://pypi.python.org/pypi/gitdbGitPythonは、モジュールオブジェクトを使用してgit構成ライブラリにアクセスします.倉庫操作
データベース・オブジェクト
GitDB
GitCmdObjectDB
参照アクションのインスタンス
from git import *
repo = Repo(repo_path)
assert repo.bare == False
repo = Repo.init(repo_path,bare=True)
assert repo.bare == True
/ heads/tags/remotes
repo.config_reader() #
repo.config_writer() #
、
repo.is_dirty() #
repo.untracked_files #
cloned_repo = repo.clone(to/this/path)
new_repo = repo.init(path/for/new/repo)
データベース・オブジェクト
repo ,repo 。
GitDB
,GitDB , 2 5
repo = Repo('path/to/repo',odbt=GitDB)
GitCmdObjectDB
git-cat-file , , GitDB 。
repo = Repo('path/to/repo',odbt=GitCmdObjectDB)
参照アクションのインスタンス
head
heads = repo.heads
master = heads.master #lists can be accessed by name for convenience
master.commit #the commit pointed to by head called master
master.rename('new_name') #rename heads
tag(tag ) commit tag
tags = repo.tags
tagref = tags[0] #tag tag ,
tagref.commit #tag commit
repo.delete_tag(tagref) # tag
repo.create_tag('my_tag') # tag
commit
head = repo.head #the head points to the active branch/ref
master = head.reference #but they always point to commits
master.commit #from here you use it as any other reference
reflog
log = master.log()
log[0] #first reflog entry
log[-1] #last reflog entry
、
repo.delete_head('master') #delete an existing head
master = repo.create_head('master') #create a new one
master.commit = 'HEAD~10' #set branch to another commit without changing index or working tree
、 tags
new_tag = repo.create_tag('my_tag','my message')
repo.delete_tag(new_tag)
new_branch = repo.craete_head('new_branch')
repo.head.reference = new_branch
git
git git 。 、 、 20 SHA1 。
git Blobs、Trees、Commits and Tags
git , git , 。
hc = repo.head.commit
hct = hc.tree
hc != hct
hc != repo.tags[0]
hc == repo.head.reference.commit
git
hct.type
hct.size
hct.hexsha
hct.binsha
git , Trees/Blobs Submodules , 。
hct.path #root tree has no path
hct.trees[0].path #the first subdirectory has one though
hct.mode #trees have the mode of a linux directory
hct.blobs[0].mode #blobs have a specific mode though compareable to a standard linux fs
stream blob
hct.blobs[0].data_stream.read() #stream object to read data from
hct.blobs[0].stream_data(open("blob_data","w")) #write data to given stream
Commit
commit commit 。 commit
repo.commit('master')
repo.commit('v0.1')
repo.commit('HEAD~10')
100 100commit
repo.iter_commits('master',max_count=100)
21-30
repo.iter_commits('master',max_count=10,skip=20)
headcommit = repo.head.commit
headcommit.hexsha
headcommit.parents
headcommit.author
headcommit.tree
headcommit.committer
headcommit.committed_date
headcommit.message
import time
time.asctime(time.gmtime(headcommit.committed_date)) #'Web May 7 05:56:02 2013'
tiem.strftime("%a,%d %b %Y %H:%M",time.gmtime(headcommit.committed_date)) #'Web,7 May 2013 05:56'
commit
headcommit.parents[0].parents[0].parents[0].parents[0]
master^^^^ master~4
Tree
tree 。 master tree
tree = repo.heads.master.commit.tree
tree
tree.trees #trees are subdirectories
tree.blobs #blobs are files
tree
tree[0] = tree['dir'] #access by index and by sub-path
blob = tree[0][0]
blob.name
blob.path
blob.abspath
tree/"lib"
tree/"dir/file" == blob
tree git
repo.tree() # <git.Tree "master">
repo.tree("c1c7214dde86...")
repo.tree('0.1.6')
tree
tree.traverse()
for entry in tree.traverse():do_something_with(entry)
tree , head commit
git commit 。
index = repo.index
、 、 ,Commit :
for stage,blob in index.iter_blobs():do_something(...) #Access blob object
for (path,stage),entry in index.entries.iteritems: pass #Access the entries directly
index.add(['my_new_file']) #add a new file to the index
index.remove(['dir/existing_file'])
new_commit = index.commit("my commit message")
tree merge
tmp_index = Index.from_tree(repo,'HEAD~1') #load a tree into a temporary index
merge_index = Index.from_tree(repo,'base','HEAD','some_branch') #merge two trees three-way
merge_index.write('merged_index')
, push fetch
test_remote = repo.create_remote('test','git@server:repo.git')
repo.delete_remote(test_remote) # create and delete remotes
origin = repo.remotes.origin #get default remote by name
origin.refs #local remote reference
o = origin.rename('new_origin') #rename remotes
o.fetch() #fetch,pull and push from and to the remote
o.pull()
o.push()
o.url
o.config_writer.set('pushurl','other_url')
index Trees Index working tree trees trees trees working copy
hcommit = repo.head.commit
idiff = hcommit.diff() #diff tree against index
tdiff = hcommit.diff('HEAD~1') #diff tree against previous tree
wdiff = hcommit.diff(None) #diff tree against working tree
index = repo.index
index.diff() #diff index agginst itself yielding empty idff
index.diff(None) #diff index against working copy
index.diff('HEAD') #diff index against current HEAD tree
Diff ,
for diff_added in wdiff.iter_change_type('A'): do_something_with(diff_added)
, HEAD , index
repo.head.reference = repo.heads.other_branch
repo.head.reset(index=True,working_tree=True)
,
repo.heads.master.checkout() #checkout the branch using git-checkout
repo.heads.other_branch.checkout()
git
git git
git = repo.git
git.checkout('head',b='my_new_branch') #default command
git.for_each_ref() #'-' becomes '_' when calling it