Conflicts in go.mod, go.sum conflicts can be tedious to fix,
the steps below help setup a custom git mergtool
which helps fix those conflicts.
ref: golang/go#32485
Caveats: this is by no means an exhaustive solution.
- Create an executable Git mergetool script in $PATH,
/usr/local/sbin/gomod-mergetool
#!/bin/bash
set -e
fileMerge=$4 ## The file containing the conflict markers
echo "$fileMerge: purging conflict markers.."
awk '!/======/' $fileMerge | awk '!/>>>>>>/' | awk '!/<<<<<<</' > ${fileMerge}_union
mv ${fileMerge}_union ${fileMerge}
exit 0
- Declare mergetool in ~/.gitconfig as
gomod
(or whatever else, make sure to reference it with the same name in step 4.)
[mergetool "gomod"]
cmd = /usr/local/sbin/gomod-mergetool "$BASE" "$LOCAL" "$REMOTE" "$MERGED"
trustExitCode = true
- Now attempt the rebase (or another merge command) which introduces the conflicts in
go.mod
,go.sum
❯ git rebase -i main
hint: Waiting for your editor to close the file... vim-latex-live-preview: python required
Press ENTER or type command to continue
Auto-merging go.mod
CONFLICT (content): Merge conflict in go.mod
Auto-merging go.sum
CONFLICT (content): Merge conflict in go.sum
error: could not apply 61713a0... go: update deps
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply 61713a0... go: update deps
- Finally, run the
gomod
mergetool withgo mod tidy
❯ git mergetool -t gomod go.mod go.sum && go mod tidy
- If the above command returned with no errors, you can continue with the rebase/merge
❯ git status
...
Unmerged paths:
(use "git restore --staged <file>..." to unstage)
(use "git add <file>..." to mark resolution)
both modified: go.mod
both modified: go.sum
❯ git add go.mod go.sum && \
git commit -m "fixes merge conflicts in go.mod, go.sum" && \
git rebase --continue
Do you have an example of an actual tool that does this?