Python的高级Git库 Gittle


Posted in Python onSeptember 22, 2014

Gittle是一个高级纯python git 库。构建在dulwich之上,提供了大部分的低层机制。

Install it

pip install gittle

Examples :

Clone a repository

from gittle import Gittle
 
repo_path = '/tmp/gittle_bare'
repo_url = 'git://github.com/FriendCode/gittle.git'
 
repo = Gittle.clone(repo_url, repo_path)

With authentication (see Authentication section for more information) :

auth = GittleAuth(pkey=key)
Gittle.clone(repo_url, repo_path, auth=auth)

Or clone bare repository (no working directory) :

repo = Gittle.clone(repo_url, repo_path, bare=True)

Init repository from a path

repo = Gittle.init(path)

Get repository information

# Get list of objects
repo.commits
 
# Get list of branches
repo.branches
 
# Get list of modified files (in current working directory)
repo.modified_files
 
# Get diff between latest commits
repo.diff('HEAD', 'HEAD~1')

Commit

# Stage single file
repo.stage('file.txt')
 
# Stage multiple files
repo.stage(['other1.txt', 'other2.txt'])
 
# Do the commit
repo.commit(name="Samy Pesse", email="samy@friendco.de", message="This is a commit")

Pull

repo = Gittle(repo_path, origin_uri=repo_url)
 
# Authentication with RSA private key
key_file = open('/Users/Me/keys/rsa/private_rsa')
repo.auth(pkey=key_file)
 
# Do pull
repo.pull()

Push

repo = Gittle(repo_path, origin_uri=repo_url)
 
# Authentication with RSA private key
key_file = open('/Users/Me/keys/rsa/private_rsa')
repo.auth(pkey=key_file)
 
# Do push
repo.push()

Authentication for remote operations

# With a key
key_file = open('/Users/Me/keys/rsa/private_rsa')
repo.auth(pkey=key_file)
 
# With username and password
repo.auth(username="your_name", password="your_password")

Branch

# Create branch off master
repo.create_branch('dev', 'master')
 
# Checkout the branch
repo.switch_branch('dev')
 
# Create an empty branch (like 'git checkout --orphan')
repo.create_orphan_branch('NewBranchName')
 
# Print a list of branches
print(repo.branches)
 
# Remove a branch
repo.remove_branch('dev')
 
# Print a list of branches
print(repo.branches)

Get file version

versions = repo.get_file_versions('gittle/gittle.py')
print("Found %d versions out of a total of %d commits" % (len(versions), repo.commit_count()))

Get list of modified files (in current working directory)

repo.modified_files

Count number of commits

repo.commit_count

Get information for commits

List commits :

# Get 20 first commits repo.commit_info(start=0, end=20)

With a given commit :

commit = "a2105a0d528bf770021de874baf72ce36f6c3ccc"

Diff with another commit :

old_commit = repo.get_previous_commit(commit, n=1)
print repo.diff(commit, old_commit)

Explore commit files using :

commit = "a2105a0d528bf770021de874baf72ce36f6c3ccc"
 
# Files tree
print repo.commit_tree(commit)
 
# List files in a subpath
print repo.commit_ls(commit, "testdir")
 
# Read a file
print repo.commit_file(commit, "testdir/test.txt")

Create a GIT server

from gittle import GitServer
 
# Read only
GitServer('/', 'localhost').serve_forever()
 
# Read/Write
GitServer('/', 'localhost', perm='rw').serve_forever()
Python 相关文章推荐
Python 流程控制实例代码
Sep 25 Python
Python选课系统开发程序
Sep 02 Python
TensorFlow利用saver保存和提取参数的实例
Jul 26 Python
python3实现爬取淘宝美食代码分享
Sep 23 Python
用python标准库difflib比较两份文件的异同详解
Nov 16 Python
python 自动重连wifi windows的方法
Dec 18 Python
在django view中给form传入参数的例子
Jul 19 Python
numpy:找到指定元素的索引示例
Nov 26 Python
解决pycharm每次打开项目都需要配置解释器和安装库问题
Feb 26 Python
python GUI库图形界面开发之PyQt5中QMainWindow, QWidget以及QDialog的区别和选择
Feb 26 Python
python数字图像处理:图像的绘制
Jun 28 Python
Python使用pandas导入xlsx格式的excel文件内容操作代码
Dec 24 Python
Python实现抓取网页并且解析的实例
Sep 20 #Python
跟老齐学Python之字典,你还记得吗?
Sep 20 #Python
跟老齐学Python之再深点,更懂list
Sep 20 #Python
跟老齐学Python之画圈还不简单吗?
Sep 20 #Python
跟老齐学Python之list和str比较
Sep 20 #Python
Python显示进度条的方法
Sep 20 #Python
python中对list去重的多种方法
Sep 18 #Python
You might like
用PHP编程语言开发动态WAP页面
2006/10/09 PHP
网站当前的在线人数
2006/10/09 PHP
解析php防止form重复提交的方法
2013/07/01 PHP
让CodeIgniter数据库缓存自动过期的处理的方法
2014/06/12 PHP
Yii2.0使用阿里云OSS的SDK上传图片、下载、删除图片示例
2017/09/20 PHP
laravel 修改.htaccess文件 重定向public的解决方法
2019/10/12 PHP
jQuery插件开发详细教程
2014/06/06 Javascript
JavaScript实现的链表数据结构实例
2015/04/02 Javascript
js文件中直接alert()中文出来的是乱码的解决方法
2016/11/01 Javascript
抖音上用记事本编写爱心小程序教程
2019/04/17 Javascript
小程序扫描普通链接二维码跳转小程序指定界面方法
2019/05/07 Javascript
jquery+ajax实现上传图片并显示上传进度功能【附php后台接收】
2019/06/06 jQuery
微信小程序sessionid不一致问题解决
2019/08/30 Javascript
vue 解决provide和inject响应的问题
2020/11/12 Javascript
[56:17]NB vs Infamous 2019国际邀请赛淘汰赛 败者组 BO3 第三场 8.22
2019/09/05 DOTA
python提取内容关键词的方法
2015/03/16 Python
浅谈django model的get和filter方法的区别(必看篇)
2017/05/23 Python
TF-IDF与余弦相似性的应用(二) 找出相似文章
2017/12/21 Python
Python实现的HMacMD5加密算法示例
2018/04/03 Python
Python 实现在文件中的每一行添加一个逗号
2018/04/29 Python
用python实现刷点击率的示例代码
2019/02/21 Python
Django 接收Post请求数据,并保存到数据库的实现方法
2019/07/12 Python
Python3 实现爬取网站下所有URL方式
2020/01/16 Python
HTML5 Canvas自定义圆角矩形与虚线示例代码
2013/08/02 HTML / CSS
Guess美国官网:美国知名服装品牌
2019/04/08 全球购物
普师专业个人自荐信范文
2013/11/26 职场文书
公务员平时考核实施方案
2014/03/11 职场文书
淘宝客服专员岗位职责
2014/04/11 职场文书
大学生社会实践活动总结
2014/07/03 职场文书
商务经理岗位职责
2014/08/03 职场文书
房产证明范本
2015/06/19 职场文书
运动员入场词
2015/07/18 职场文书
python超详细实现完整学生成绩管理系统
2022/03/17 Python
Axios代理配置及封装响应拦截处理方式
2022/04/07 Vue.js
Nginx隐藏式跳转(浏览器URL跳转后保持不变)
2022/04/07 Servers
gtx1650怎么样 gtx1650显卡相当于什么级别
2022/04/08 数码科技