GitPython git python 的开发库

工程地址: https://pypi.python.org/pypi/GitPython/ 须要安装先安装: gitdb https://pypi.python.org/pypi/gitdb GitPython使用模块对象访问git配置库。 仓库操做 初始仓库对象 from git import * repo = Repo(repo_path) assert repo.bare == Falsepython

建立裸库
    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对象通常用于获取大数据和新增对象。linux

GitDB 在操做大文件时,GitDB可使用更少的内存,但处理速度慢2到5倍 repo = Repo('path/to/repo',odbt=GitDB)git

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](https://my.oschina.net/servingyou):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

提交 repo = Repo(repo_path)
index = repo.index index.add([file_name]) # add 须要提交的文件 index.commit(‘提交说明’) # 这里无论是否有修改的文件都会建立新的commitfetch

相关文章
相关标签/搜索