4

我正在尝试自动化一个更改过程,该过程当前创建手动推送到 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

该脚本能够拉取远程存储库,但在提交时失败。有更好的方法吗?

4

2 回答 2

4

您正在使用的某些功能可能无法按您期望的方式工作。通常,Repo方法等同于git具有相同名称的子命令。

如果您尝试克隆远程存储库,这可以在一行中实现:

repo = Repo.clone_from("http://user:pass@git/repo.git", "/tmp/test/repo")

有关如何使用 GitPython 的更多信息,请参阅API 参考。

于 2014-03-26T19:22:22.947 回答
1

您不能针对裸存储库提交。你只能推/拉他们。通过并行考虑如何在本地执行此操作。尝试克隆一个裸仓库并执行操作,它们将不起作用。

我对 pythonic git 绑定并不十分熟悉,但可以想象您需要克隆一个工作存储库,可选择签出给定分支而不是 master,完成您的工作,仅针对这些内容调用 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
于 2014-03-26T19:14:23.747 回答