python git 记录
python git 库类
pygit2:www.pygit2.org
gitpyhon:http://gitpython.readthedocs.io/en/stable/
window python3.5 安装库类时碰到unable to find vcvarsall.bat,可以采用下载whl安装文件进行安装。
stackoverflow上关于python git 相关讨论参考:
https://stackoverflow.com/questions/1456269/python-git-module-experiences
https://stackoverflow.com/questions/7119452/git-commit-from-python
gitpython 相关代码:
# git 提交
from git import Repo, Remote
try:
try:
repo = Repo.init(config_base_path)
origin = repo.create_remote("origin", git_remote_url)
isFirst = True
except Exception as ex:
print(ex)
isFirst = False
repo = Repo.init(config_base_path)
origin = repo.remote()
exists = origin.exists()
assert exists
assert origin == repo.remotes.origin == repo.remotes['origin']
origin.fetch()
if not isFirst:
repo.index.remove(["*"])
repo.index.add(["*"])
repo.index.commit("commit info %s" % commit_info)
info = origin.push(refspec="refs/heads/master:refs/heads/master")[0]
print(info.flags, info.local_ref, info.remote_ref_string, info.remote_ref, info.old_commit, info.summary)
except Exception as ex:
print("ex", ex)
# 回滚
repo.head.reset('HEAD~1', index=True, working_tree=True)
raise ex
在linux搭建git服务:http://www.cnblogs.com/dee0912/p/5815267.html
在自己搭建的git服务push时需指定refspec,
refspec参数解释具体可以参考gitpython->remote.fetch,否则将出现push异常的情况:
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use
注意修改仓库目录的权限:
Error lines received while fetching: error: insufficient permission for adding an object to repository database ./objects
您可能也对下面文章感兴趣:
There are 1 Comments to "python git 记录"