gittle是一个高级纯python git 库。构建在dulwich之上,提供了大部分的低层机制。
install itpip install gittle
examples :clone a repositoryfrom 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 pathrepo = gittle.init(path)
get repository information# get list of objectsrepo.commits # get list of branchesrepo.branches # get list of modified files (in current working directory)repo.modified_files # get diff between latest commitsrepo.diff('head', 'head~1')
commit# stage single filerepo.stage('file.txt') # stage multiple filesrepo.stage(['other1.txt', 'other2.txt']) # do the commitrepo.commit(name=samy pesse, email=samy@friendco.de, message=this is a commit)
pullrepo = gittle(repo_path, origin_uri=repo_url) # authentication with rsa private keykey_file = open('/users/me/keys/rsa/private_rsa')repo.auth(pkey=key_file) # do pullrepo.pull()
pushrepo = gittle(repo_path, origin_uri=repo_url) # authentication with rsa private keykey_file = open('/users/me/keys/rsa/private_rsa')repo.auth(pkey=key_file) # do pushrepo.push()
authentication for remote operations# with a keykey_file = open('/users/me/keys/rsa/private_rsa')repo.auth(pkey=key_file) # with username and passwordrepo.auth(username=your_name, password=your_password)
branch# create branch off masterrepo.create_branch('dev', 'master') # checkout the branchrepo.switch_branch('dev') # create an empty branch (like 'git checkout --orphan')repo.create_orphan_branch('newbranchname') # print a list of branchesprint(repo.branches) # remove a branchrepo.remove_branch('dev') # print a list of branchesprint(repo.branches)
get file versionversions = 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 commitsrepo.commit_count
get information for commitslist 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 treeprint repo.commit_tree(commit) # list files in a subpathprint repo.commit_ls(commit, testdir) # read a fileprint repo.commit_file(commit, testdir/test.txt)
create a git serverfrom gittle import gitserver # read onlygitserver('/', 'localhost').serve_forever() # read/writegitserver('/', 'localhost', perm='rw').serve_forever()