Skip to content

Automatic pull with merge when git push fails

Alessio Caiazza requested to merge push-fail-pull-rebase into master

If the target branch advances during the merge train execution, the push will be rejected because of the missing commits.

Now when this happens, we do perform an automatic pull with a merge operation and retry the push

Closes gitlab-com/gl-infra/delivery#19457 (closed)

Test run

This change was tested on the https://gitlab.com/nolith-tests/playground/ repository using feature as the source branch and changing files on the master branch during the merge-train execution.

As we can see on the log below, the merge-train generated https://gitlab.com/nolith-tests/playground/-/commit/fd0accbcd9975318823b6195fd6161509fa03349, but because of https://gitlab.com/nolith-tests/playground/-/commit/a3748d904f3a220753e20917584ce898f22c3917 a new merge commit has been generated and pushed https://gitlab.com/nolith-tests/playground/-/commit/356bdb69ccd328ff4d89a2bbd029bbb41087e345

git log

*   356bdb69ccd328ff4d89a2bbd029bbb41087e345 (HEAD -> master, origin/master, origin/HEAD) Merge branch 'master' of gitlab.com:nolith-tests/playground
|\
| * a3748d904f3a220753e20917584ce898f22c3917 Update README.md
* |   fd0accbcd9975318823b6195fd6161509fa03349 Automatic merge of nolith-tests/playground feature
|\ \
| |/
|/|
| * ef3f04ccee8f5c53b8472163eaaf93c59f9ba090 (source-project/feature) my feature
* | 22525204e000a4b9e717887e93c9fe942e7a74a0 Update README.md
* | 1ed6d091e1d2955e1c27b33af8564559a5379cc7 Update README.md
|/
* afd15e3e22f6d16016387b192d25eb693ba52412 Update .gitlab-ci.yml

merge-train log

>>> SOURCE_PROJECT: nolith-tests/playground
>>> SOURCE_BRANCH: feature
>>> TARGET_PROJECT: nolith-tests/playground
>>> TARGET_BRANCH: master

>>> Updating existing clone of nolith-tests/playground
>>> Updating existing clone of nolith-tests/playground
>>> Configuring merge drivers
>>> Updating remote
>>> Resetting working directory
>>> Merging nolith-tests/playground@feature into nolith-tests/playground@master
>>> Pushing changes to nolith-tests/playground@master
fd0accb (HEAD -> master) Automatic merge of nolith-tests/playground feature
2252520 (origin/master, origin/HEAD) Update README.md
1ed6d09 Update README.md
To gitlab.com:nolith-tests/playground.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'gitlab.com:nolith-tests/playground.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
>>> Push attempt 1 failed
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 434 bytes | 144.00 KiB/s, done.
From gitlab.com:nolith-tests/playground
 * branch            master     -> FETCH_HEAD
   2252520..a3748d9  master     -> origin/master
Merge made by the 'ort' strategy.
 README.md | 1 +
 1 file changed, 1 insertion(+)
356bdb6 (HEAD -> master) Merge branch 'master' of gitlab.com:nolith-tests/playground
a3748d9 (origin/master, origin/HEAD) Update README.md
fd0accb Automatic merge of nolith-tests/playground feature
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 10 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 936 bytes | 936.00 KiB/s, done.
Total 4 (delta 3), reused 0 (delta 0), pack-reused 0
To gitlab.com:nolith-tests/playground.git
   a3748d9..356bdb6  master -> master
Edited by Alessio Caiazza

Merge request reports