我正在尝试自动化一个更改过程,该过程当前创建手动推送到 Git 的源代码。我正在尝试使用 GitPython:
包装该代码from git import *
# create the local repo
repo = Repo.init("/tmp/test/repo", bare=True)
assert repo.bare == True
# check status of active branch
repo.is_dirty()
# clone the remote repo
remote_repo = repo.clone("http://user:pass@git/repo.git")
# compile source code into repository
# ...
# track untracked files
repo.untracked_files
# commit changes locally
repo.commit("commit changes")
# push changes to master
remote_repo.push()
当我尝试运行这个时,我得到了
回溯(最近一次调用最后一次):
文件“git_test2.py”,第 33 行,位于
repo.commit(“提交更改”)
坏对象:636f6d6d6974206368616e676573
该脚本能够拉取远程存储库,但提交失败。有更好的方法吗?
您正在使用的某些功能可能无法按您期望的方式工作。一般来说,
Repo
方法不相当于同名的git
子命令。
Repo.commit
不是 create 提交,而是 retrieve 现有提交。 由于存储库中没有名为“commitchanges”的提交,因此会引发异常。Repo.clone
在存储库目录结构内名为 http:
的目录中创建 this存储库的克隆,这很可能不是您想要的。
如果您尝试克隆远程存储库,只需一行即可实现:
repo = Repo.clone_from("http://user:pass@git/repo.git", "/tmp/test/repo")
有关如何使用 GitPython 的更多信息,请参阅 API 参考。
您无法针对裸存储库进行提交。您只能推/拉它们。同时考虑一下如何在本地执行此操作。尝试克隆一个裸仓库并执行操作,它们不会起作用。
我对 pythonic git 绑定不是很熟悉,但我想象你需要克隆一个工作存储库,可选择签出给定的分支而不是主分支,完成你的工作,针对这些东西调用 git add ,然后提交.
此外,repo.untracked_files 是一个无操作,它只是列出它们,而不是添加它们。
老实说,看起来你盲目地从 https://pythonhosted.org/GitPython/0.3.1/tutorial.html 复制粘贴,而没有真正阅读它所说的内容。
例如,您需要操作索引对象
index = repo.index
for ( path, stage ), entry in index.entries.iteritems: pass
index.add(['SOMEFILE'])
new_commit = index.commit("YOUR COMMIT MESSAGE")
#do somethign with new commit
我发现的另一个例子
import git
repo = git.Repo( '/home/me/repodir' )
print repo.git.status()
# checkout and track a remote branch
print repo.git.checkout( 'origin/somebranch', b='somebranch' )
# add a file
print repo.git.add( 'somefile' )
# commit
print repo.git.commit( m='my commit message' )
# now we are one commit ahead
print repo.git.status()
# now push