如何检出和合并分支到 master - GITPYTHON

How to checkout and merge branches to 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 分支在 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">]

为了稍微简化配方,如果您尝试将所有远程分支合并到本地主分支中,这应该可以完成工作:

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.