git怎么读(sonar怎么读)

花猫语:今天在查如何用Python操作Gitlab的时候,看到了这篇文章,觉得还不错。我想与你分享它。本文还提到了其他几种操作Git的方法。有机会的话,陆续分享一下~ ~

作者:Python

资料来源:https://note.qidong.name/2018/01/gitpython

有时候需要做复杂的Git操作,中间逻辑很多。用Shell做复杂的逻辑运算和流程控制,简直是灾难。因此,用Python实现它是一个愉快的选择。此时,你需要用Python操作Git的库。

GitPython 简介

GitPython是一个与git库交互的Python库,包括底层命令(管道)和高层命令(瓷器)。可以实现大部分Git读写操作,避免频繁与Shell交互的异常代码。它不是纯Python实现,而是一部分依赖于git命令的直接执行,另一部分依赖于GitDB。

GitDB也是一个Python库。它建立了一个数据库模型。git/objects,可以实现直接读写。由于采用了流读写,所以运行效率高,占用内存少。

GitPython安装

Pip install gitPython,依赖GitDB,会自动安装,但是可执行的Git命令需要额外安装。

基本用法init

导入Git repo = git.repo.init (path = ' . ')这会在当前目录中创建一个git库。当然,路径可以自定义。

饭桶。Repo可以与with一起使用,因为它实现__enter__和__exit__。

With git.repo.init (path = ' . ')as repo: # dosh with repo不过,这个表单不一定要用,因为它只实现了一些清理操作,关闭后仍然可以读写。详情见附录。

clone

克隆人有两种。一种是从当前库克隆到另一个位置:

New _ repo = repo.clone (path = '../new’)其次,从URL克隆到本地位置:

new_repo = git。repo . clone _ from(URL = ' git @ github . com:USER/repo . git ',to_path= '../new ')commit with open(' test . file ',' w ')as fobj:fobj . write(' 1st line N ')repo . index . add(items =[' test . file '])repo . index . commit(' write a line into test . file '))with open(' test . file ',' aw ')as fobj:fobj . write(' 2nd line N ')repo . index . add(items =[' test . file '])MIT(' write another line into test . file ')status git python不

& gt& gt& gtrepo . is _ dirty()False & gt;& gt& gt用open('test.file ',' aw ')作为fobj:& gt;& gt& gtfobj . write(' dirty line n ')& gt;& gt& gtrepo . is _ dirty()True & gt;& gt& gtrepo . un tracked _ files[]& gt;& gt& gt用open('untracked.file ',' w ')作为fobj:& gt;& gt& gtfobj . write(' ')& gt;& gt& gtrepo . un tracked _ files[' un tracked . file ']check out(清除所有修改)>:& gt& gtrepo . is _ dirty()True & gt;& gt& gtrepo . index . check out(force = True)& lt;生成器对象& ltgenexpr & gtat 0x 7 F2 BF 35 e6b 40 & gt;& gt& gt& gtRepo.is_dirty() Falsebranch获取当前分支:

Head = repo.head新分行:

new _ head = repo . create _ head(' new _ head ',' head ')开关分支:

new _ head . check out()head . check out()删除分支:

Git.head.delete (repo,new _ head)# orgit . head . delete(repo,' new _ head') merge下面演示了如何将另一个分支(master)合并到一个分支(other)中。

master = repo . heads . master other = repo . create _ head(' other ','head^')other . check out()repo . index . merge _ tree(master)repo . index . commit('从主服务器合并到其他服务器')remote,fetch,pull,push

创建remote:

Remote = repo . create _ Remote(name = ' git lab ',URL = ' git @ git lab . com:user/repo . git ')远程交互:Remote = repo . Remote()Remote . fetch()Remote . pull()Remote . push()delete Remote:repo . delete _ Remote(Remote)#或repo.delete_remote('gitlab ')其他相关操作如标签、子模块等不常用,此处不再介绍。

GitPython的优点是在做读操作的时候可以轻松获取内部信息,缺点是做写操作的时候感觉比较别扭。当然,它也支持直接执行git操作。

git = repo . git git . status()git . check out(' head ',b = " my _ new _ branch ")git . branch(' another-new-one ')git . branch('-d ',' another-new-one ')。

其它操作Git的方法subprocess

这就是所谓的“老路”。在另一个进程中,执行Shell命令,返回的结果由stdio解析。

import subprocess subprocess . call([' Git ',' status']) Dulwich Dulwich是一个用纯Python实现的Git交互库。以后用空研究一下吧。

官方网站:https://www.dulwich.io/

pygit2

Pygit2是一个基于libgit2的Python库。底层是C,上层Python只是一个接口,应该运行效率最高。然而,我放弃了。缺点是需要在环境中预装libgit2。相比之下,GitPython只需要环境预置Git,简单很多。

官方网站:http://www.pygit2.org/

参考《GitPython documentation》《Welcome to GitDB’s documentation!》《Git - 底层命令 (Plumbing) 和高层命令 (Porcelain)》《GitPython | Hom》附录

git中上下文相关接口的实现。回购如下:

def _ _ enter _ _(self):return self def _ _ exit _ _(self,exc_type,exc_value,trace back):self . close()def _ _ del _ _(self):try:self . close()except:pass def close(self):if self . git:self . git . clear _ cache()GC . collect()git db . util . mman . collect()GC . collect()可以看作只是一些清理操作,关闭的必要性不高。即使它是关闭的,git的实例。回购还是可以读写的。

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。系信息发布平台,仅提供信息存储空间服务。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。

本文来自网络,若有侵权,请联系删除,作者:高悦明,如若转载,请注明出处:

发表回复

登录后才能评论