Skip to content

Commit 9ca78fc

Browse files
committed
Added a tutorial when merge conflict happens, and show the error message.
1 parent 6d10e4e commit 9ca78fc

File tree

1 file changed

+46
-12
lines changed

1 file changed

+46
-12
lines changed

homu/main.py

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,32 @@ def create_merge(state, repo_cfg, branch, logger, git_cfg,
833833
state.body)
834834

835835
desc = 'Merge conflict'
836+
comment = (
837+
'This pull request and the master branch diverged in a way that cannot'
838+
' be automatically merged. Please rebase on top of the latest master'
839+
' branch, and let the reviewer approve again.\n'
840+
'\n'
841+
'<details><summary>How do I rebase?</summary>\n\n'
842+
'Assuming `self` is your fork and `upstream` is this repository,'
843+
' you can resolve the conflict following these steps:\n\n'
844+
'1. `git checkout {branch}` *(switch to your branch)*\n'
845+
'2. `git fetch upstream master` *(retrieve the latest master)*\n'
846+
'3. `git rebase upstream/master -p` *(rebase on top of it)*\n'
847+
'4. Follow the on-screen instruction to resolve conflicts'
848+
' (check `git status` if you got lost).\n'
849+
'5. `git push self {branch} --force-with-lease` *(update this PR)*\n\n'
850+
'You may also read'
851+
' [*Git Rebasing to Resolve Conflicts* by Drew Blessing](http://blessing.io/git/git-rebase/open-source/2015/08/23/git-rebasing-to-resolve-conflicts.html)' # noqa
852+
' for a short tutorial.\n\n'
853+
'Please avoid the ["**Resolve conflicts**" button](https://help.github.com/articles/resolving-a-merge-conflict-on-github/) on GitHub.' #noqa
854+
' It uses `git merge` instead of `git rebase` which makes the PR commit'
855+
' history more difficult to read.\n\n'
856+
'Sometimes step 4 will complete without asking for resolution. This is'
857+
' usually due to difference between how `Cargo.lock` conflict is'
858+
' handled during merge and rebase. This is normal, and you should still'
859+
' perform step 5 to update this PR.\n\n'
860+
'</details>\n\n'
861+
).format(branch=state.head_ref.split(':', 1)[1])
836862

837863
if git_cfg['local_git']:
838864

@@ -861,6 +887,7 @@ def create_merge(state, repo_cfg, branch, logger, git_cfg,
861887
utils.silent_call(git_cmd('rebase', '--abort'))
862888
if utils.silent_call(git_cmd('rebase', base_sha)) == 0:
863889
desc = 'Auto-squashing failed'
890+
comment = ''
864891
else:
865892
ap = '<try>' if state.try_ else state.approved_by
866893
text = '\nCloses: #{}\nApproved by: {}'.format(state.num, ap)
@@ -903,22 +930,29 @@ def create_merge(state, repo_cfg, branch, logger, git_cfg,
903930
merge_base_sha, base_sha))
904931
except subprocess.CalledProcessError:
905932
desc = 'Auto-squashing failed'
933+
comment = ''
906934
ok = False
907935

908936
if ok:
909937
utils.logged_call(git_cmd('checkout', '-B', branch, base_sha))
910938
try:
911-
utils.logged_call(git_cmd(
912-
'-c',
913-
'user.name=' + git_cfg['name'],
914-
'-c',
915-
'user.email=' + git_cfg['email'],
916-
'merge',
917-
'heads/homu-tmp',
918-
'--no-ff',
919-
'-m',
920-
merge_msg))
921-
except subprocess.CalledProcessError:
939+
subprocess.check_output(
940+
git_cmd(
941+
'-c',
942+
'user.name=' + git_cfg['name'],
943+
'-c',
944+
'user.email=' + git_cfg['email'],
945+
'merge',
946+
'heads/homu-tmp',
947+
'--no-ff',
948+
'-m',
949+
merge_msg),
950+
stderr=subprocess.STDOUT,
951+
universal_newlines=True)
952+
except subprocess.CalledProcessError as e:
953+
comment += '<details><summary>Error message</summary>\n\n```text\n'
954+
comment += e.output
955+
comment += '\n```\n\n</details>'
922956
pass
923957
else:
924958
if ensure_merge_equal:
@@ -964,7 +998,7 @@ def create_merge(state, repo_cfg, branch, logger, git_cfg,
964998
desc,
965999
context='homu')
9661000

967-
state.add_comment(':lock: ' + desc)
1001+
state.add_comment(':lock: {}\n\n{}'.format(desc, comment))
9681002
state.change_labels(LabelEvent.CONFLICT)
9691003

9701004
return ''

0 commit comments

Comments
 (0)