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

时间:2018-10-24 23:23:17

标签: python python-3.x git github gitpython

我需要使用python将所有分支合并到主服务器。分支本地路径,buildno通过环境变量获取。我有以下代码来签出每个分支并合并到母版。但是执行代码时会出现一些错误。

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文件应与主文件合并。

错误实际上是正确的,因为我的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)'

1 个答案:

答案 0 :(得分:1)

我认为您没有正确使用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