I need to merge all my branches to the master using python. branch local path,buildno getting via environment variables. I have the following code to checkout each branch and merge to master. but it gives some errors when executing the code.
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)
when I execute the code, it gives the following error. (I manually modified the om.docker.dev/xxx/releasekit/VERSION and commit to bug branch and pushed to the bug branch' and trying to execute the script, expected behaviour is new VERSION file should be merge with the master.
error is actually correct,as my bug branch is ahead of 1 commit to master, but I need to merge that commit to master. but it gives 128 exit code.
if conflict happens it should display an error. (I dont have any exceptions at the moment). if I want to add any exceptions for merge failures and merge conflict how should I modify this. second question is why it is giving exit code 128.
im using 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)'
I don't think you're using the API properly.
>>> 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)'
I don't think you mean to pass these strings to merge-base
. Passing the branch names has the desired effect:
>>> repo.merge_base('origin/master', 'master')
[<git.Commit "5999d3082d4051acd9d107eb3e70b3ee0e14d33f">]
To simplify the recipe a bit, if you're trying to merge all remote branches into local master this should do the job:
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)
If there are merge conflicts then you should get a GitCommandError
.