【问题标题】:How to checkout and merge branches to master - GITPYTHON如何签出和合并分支到 master - GITPYTHON
【发布时间】: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


    【解决方案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">]
    

    为了简化配方,如果您尝试将所有远程分支合并到本地 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

    【讨论】:

    • 首先我需要将本地分支合并到本地 master ,然后将 master 合并到 origin/master ?
    • 从您的解释中我不能完全确定您要在此代码 sn-p 中完成什么。你为什么不直接签出 master 并合并分支?在尝试编写脚本之前,请确保您有一个可靠的配方可以手动执行...但是要回答有关错误的具体问题,您应该将分支的名称作为字符串传递给 repo.git.checkout
    • 我的期望是将所有存储库克隆到我的 CI/CD 服务器,然后将所有分支合并到 master,然后推送到 origin/master
    猜你喜欢
    • 2018-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-14
    • 1970-01-01
    • 2016-04-30
    • 2017-06-21
    相关资源
    最近更新 更多