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 02 Python
python XlsxWriter模块创建aexcel表格的实例讲解
May 03 Python
python实现石头剪刀布程序
Jan 20 Python
Python使用while循环花式打印乘法表
Jan 28 Python
PyQt5下拉式复选框QComboCheckBox的实例
Jun 25 Python
django如何实现视图重定向
Jul 24 Python
Django 后台获取文件列表 InMemoryUploadedFile的例子
Aug 07 Python
python文件和文件夹复制函数
Feb 07 Python
Python导入数值型Excel数据并生成矩阵操作
Jun 09 Python
基于Tensorflow的MNIST手写数字识别分类
Jun 17 Python
tensorflow 大于某个值为1,小于为0的实例
Jun 30 Python
Python机器学习之KNN近邻算法
May 14 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 去除html标记--strip_tags与htmlspecialchars的区别详解
2013/06/26 PHP
php操作redis数据库常见方法实例总结
2020/02/20 PHP
用最通俗易懂的代码帮助新手理解javascript闭包 推荐
2012/03/01 Javascript
window.event.keyCode兼容IE和Firefox实现js代码
2013/05/30 Javascript
Node.js中AES加密和其它语言不一致问题解决办法
2014/03/10 Javascript
js实现在同一窗口浏览图片
2014/09/17 Javascript
JavaScript获取伪元素(Pseudo-Element)属性的方法技巧
2015/03/13 Javascript
JavaScript判断表单提交时哪个radio按钮被选中的方法
2015/03/21 Javascript
基于jQuery的网页影音播放器jPlayer的基本使用教程
2016/03/08 Javascript
jQuery实现动态文字搜索功能
2017/01/05 Javascript
vue异步加载高德地图的实现
2018/06/19 Javascript
vue.js 中使用(...)运算符报错的解决方法
2018/08/09 Javascript
chosen实现省市区三级联动
2018/08/16 Javascript
在微信小程序中使用vant的方法
2019/06/07 Javascript
Vue+Bootstrap收藏(点赞)功能逻辑与具体实现
2020/10/22 Javascript
如何利用nodejs实现命令行游戏
2020/11/24 NodeJs
[01:33]真香警告!DOTA2勇士令状不朽珍藏Ⅱ饰品欣赏
2018/06/26 DOTA
python实现文件名批量替换和内容替换
2014/03/20 Python
python中利用Future对象异步返回结果示例代码
2017/09/07 Python
Python模拟三级菜单效果
2017/09/11 Python
Python编程实现从字典中提取子集的方法分析
2018/02/09 Python
pandas.DataFrame.to_json按行转json的方法
2018/06/05 Python
PyQt5多线程刷新界面防假死示例
2019/12/13 Python
keras训练曲线,混淆矩阵,CNN层输出可视化实例
2020/06/15 Python
python speech模块的使用方法
2020/09/09 Python
使用CSS3创建动态菜单效果
2015/07/10 HTML / CSS
Myprotein台湾官方网站:全球领先的运动营养品牌
2018/12/10 全球购物
理工大学毕业生自荐信
2013/11/01 职场文书
师范毕业生个人求职信
2013/12/09 职场文书
竞选部门副经理的自荐书范文
2014/02/11 职场文书
行政工作个人的自我评价
2014/02/13 职场文书
学校安全责任书范本
2014/07/23 职场文书
2015年销售部工作总结范文
2015/04/27 职场文书
2015年秋季小班开学寄语
2015/05/27 职场文书
解决persistence.xml配置文件修改存放路径的问题
2022/02/24 Java/Android
win10如何快速切换窗口 win10切换窗口快捷键分享
2022/07/23 数码科技