欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python的高级Git库 Gittle

程序员文章站 2023-11-03 21:17:16
gittle是一个高级纯python git 库。构建在dulwich之上,提供了大部分的低层机制。 install it pip install gittle ex...

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()