美文网首页
python 调用git

python 调用git

作者: 云上听风 | 来源:发表于2018-12-14 11:52 被阅读0次

    使用GitPython可以调用git命令。

    官方文档在此

    安装


    pip install gitpython
    

    使用例子


    直接代码说话,懒得写了。

    from git import Repo
    
    repo = Repo(sourceDir) #sourceDir是你的项目的路径,不会修改覆盖你的项目,可以放心调用。
    
    #获取master最新版本的hexsha值
    head = repo.head
    master = head.reference
    log = master.log()
    newhexsha = (log[-1].newhexsha)
    print(newhexsha)
    
    if repo.is_dirty(): #是否修改过
        for v in repo.untracked_files: #untracked文件列表
            print(v) #字符串,路径
    
    index = repo.index
        for v in index.diff(None): #本地修改未提交列表
           print(v.b_path) ##本地文件路径
           print(v. change_type) ###修改类型,返回"m"表示modified。
    
    

    更多的参考官方文档。

    相关文章

      网友评论

          本文标题:python 调用git

          本文链接:https://www.haomeiwen.com/subject/figwhqtx.html