【发布时间】:2018-10-24 23:23:16
【问题描述】:
我需要使用 python 将我的所有分支合并到主分支。分支本地路径,buildno 通过环境变量获取。我有以下代码来检查每个分支并合并到 master。但是在执行代码时会出现一些错误。
from git import Repo
import git
def mergeRepo(user, buildno, repo, branches, gdwlocalpath, ref="master" ):
branch_list = branches.split(",")
repo = git.Repo(gdwlocalpath)
repo.git.checkout(ref)
current = repo.active_branch
remote_branches = []
for ref in repo.git.branch('-r').split('\n'):
remote_branches.append(ref.replace("origin/","").strip())
print(remote_branches)
mergeFailure = False
mergeResult = None
prefix='origin/'
for branch in branch_list:
if branch in remote_branches:
print(prefix+branch)
current=repo.git.checkout(prefix+branch)
master = repo.git.checkout('master')
base = repo.merge_base( master,current)
repo.git.index.merge_tree(master, base=base)
repo.index.commit("Merge into master",parent_commits=(current.commit, master.commit))
current.checkout(force=True)
else:
print("Branch doesn't exist:",branch)
当我执行代码时,它给出了以下错误。 (我手动修改了 om.docker.dev/xxx/releasekit/VERSION 并提交到 bug 分支并推送到 bug 分支'并尝试执行脚本,预期的行为是新的 VERSION 文件应该与主文件合并。
错误实际上是正确的,因为我的错误分支在 1 次提交到 master 之前,但我需要将该提交合并到 master。但它给出了 128 个退出代码。
如果发生冲突,它应该显示一个错误。 (我目前没有任何例外)。如果我想为合并失败和合并冲突添加任何异常,我应该如何修改它。第二个问题是为什么它给出退出代码 128。
我使用 python 3.6
Traceback (most recent call last):
File "./SingleMergeUp.py", line 82, in <module>
mergeRepo(username,buildno,arguments.repo,arguments.sources,gdwpath)
File "./SingleMergeUp.py", line 40, in mergeRepo
base = repo.merge_base( master,current)
File "/Users/ab/.virtualenvs/python_core/lib/python3.6/site-packages/git/repo/base.py", line 490, in merge_base
lines = self.git.merge_base(*rev, **kwargs).splitlines()
File "/Users/ab/.virtualenvs/python_core/lib/python3.6/site-packages/git/cmd.py", line 440, in <lambda>
return lambda *args, **kwargs: self._call_process(name, *args, **kwargs)
File "/Users/ab/.virtualenvs/python_core/lib/python3.6/site-packages/git/cmd.py", line 834, in _call_process
return self.execute(make_call(), **_kwargs)
File "/Users/ab/.virtualenvs/python_core/lib/python3.6/site-packages/git/cmd.py", line 627, in execute
raise GitCommandError(command, status, stderr_value)
git.exc.GitCommandError: 'git merge-base Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits) ' returned with exit code 128
stderr: 'fatal: Not a valid object name Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)'
【问题讨论】:
标签: python python-3.x git github gitpython