0

Suppose I'm in the middle of a rebase (interactive, or otherwise), and a merge-conflict is detected.

The merge stops, and I run (on the cmd line) git mergetool, which has been configured to use kdiff3 as its merge-manager.

I choose my merge resolution in the KDiff3 interface, and am ready to continue.

At some point after this, and before "I have now committed the merged change (and am going to move on to the next commit)", I want to execute a custom command w.r.t. the current state of the resulting file.

Is there anywhere that I can hook this in to happen automatically? Either in git itself, or in kDiff.


Analogous hooks at other points are the textconv command hook in git, or the Pre-processing command in KDiff3.

Happy with either "a command that gets given the file path" (like textconv) or "a command that has the current state of the file piped into it" (like kDiff pre-processor).

isherwood
  • 58,414
  • 16
  • 114
  • 157
Brondahl
  • 7,402
  • 5
  • 45
  • 74
  • 2
    You could theoretically just write a shell script that calls kdiff3 and then does what you want to do after that and set *that* to be your mergetool. You'll have to make sure that you transport all arguments correctly and only take your action on success, but that should be fine. – Joachim Sauer Dec 12 '20 at 09:53
  • @JoachimSauer that sounds like a great plan. I don't suppose you know where I'd find the spec for the "being a git mergetool" interface? – Brondahl Dec 12 '20 at 22:10
  • This works excellently. Will post an answer documenting the process. Feel free to ping me if I don't :D – Brondahl Dec 18 '20 at 10:17

1 Answers1

0

CREDIT: All of this was @Joachim's idea, in his comment to my question, above.

The detail of how to actually achieve this was mainly drawn from here: https://blog.beardhatcode.be/2018/03/your-own-git-mergetool.html, which offered a lot of insight into what the "command interface" for the merge script looks like.


As per @Joachim's suggestion, you can achieve this by wrapping up the kdiff exe in your own script, which also does the post-merge op, and then setting that as the mergetool.

The full gory details of a solution that implements this can be find in my Github: https://github.com/Brondahl/AzureAdfDataflowGitPrettifier

The most interesting files are:

As a whole that is a solution for (losslessly) prettifying all instances of a certain filetype in my repository before every diff or comparison operation, and then (again losslessly) uglifying it again after any write operations that use this prettified version ... which in practice is just the kdiff3 merge.


The most crtical and subject-specific steps in there are probably:

In the kdiff-wrapping bash script:

  • kdiffPath=$(git config --get mergetool.kdiff3.path) in order to get the current kdiff path.
  • applyPreProcessorConfig="PreProcessorCmd=.\\dataflowGitDiffTool\\AdfDataflowFilePrettifier.exe -prettify -fromStdIn" which we'll pass to kdiff when invoking it, to tell it to use the prettifier
  • invokeKDiff=("$kdiffPath" "$@" "--cs" "$applyPreProcessorConfig") followed by "${invokeKDiff[@]}" in order to successfully pass all the args from the script ($@) along to kdiff.
  • mergeOutput=$5 in order to know where the now-prettified, post-merge output file is which we should be uglifying.

And then the following to configure my git config to use the script appropriately:

git config merge.tool kdiff3_with_uglification
git config mergetool.keepBackup false
git config mergetool.kdiff3_with_uglification.cmd './dataflowGitDiffTool/kdiff3_with_uglification.sh "$BASE" "$LOCAL" "$REMOTE" -o "$MERGED"'

which produces a .git\config file that looks like this:

[merge]
    tool = kdiff3_with_uglification
[mergetool]
    keepBackup = false
[mergetool "kdiff3_with_uglification"]
    cmd = ./dataflowGitDiffTool/kdiff3_with_uglification.sh \"$BASE\" \"$LOCAL\" \"$REMOTE\" -o \"$MERGED\"
Brondahl
  • 7,402
  • 5
  • 45
  • 74