python - 如何签出和合并分支到 master - GITPYTHON
问题描述
我需要使用 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)'
解决方案
我认为您没有正确使用 API。
>>> current=repo.git.checkout('origin/master')
>>> current
''
>>> master = repo.git.checkout('master')
>>> master
'Your branch is ahead of \'origin/master\' by 1 commit.\n (use "git push" to
publish your local commits)'
我认为您的意思不是将这些字符串传递给merge-base
. 传递分支名称具有预期的效果:
>>> repo.merge_base('origin/master', 'master')
[<git.Commit "5999d3082d4051acd9d107eb3e70b3ee0e14d33f">]
为了稍微简化一下配方,如果您尝试将所有远程分支合并到本地 master 中,这应该可以完成这项工作:
repo = git.Repo(gdwlocalpath)
repo.git.fetch()
remote_branches = repo.git.branch('-r').splitlines()
repo.git.checkout(ref)
for branch in remote_branches:
repo.git.merge(branch)
如果存在合并冲突,那么您应该得到一个GitCommandError
.
推荐阅读
- c - 如何解释linux内核陷阱错误行?
- ruby-on-rails - 如何在 Rails 中显示索引翻译(rails-i18n)?
- javascript - 使用 Firebase 处理用户身份验证错误
- ios - 我可以通过 iOS Developer University Program 将应用程序发布到 Apple Store
- javascript - 如何在 Gatsby 中使用 GraphQL 查询,该查询在高级自定义字段 (ACF) 中返回一个数组?
- regex - 从 PHP 到 JS 的正则表达式
- android - 为什么使用 Retrofit Multipart 上传时图像没有显示在服务器上?
- google-chrome - 即使在对该资源使用“预连接”资源提示后,Chrome 的网络日志也会显示资源请求的“is_preconnect=false”
- excel - 命名工作表上的行删除
- css - 如何使用css在django模板中更改颜色n次?