How can I trigger automated command after completing a merge in kDiff3?

98 Views Asked by At

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).

1

There are 1 best solutions below

0
On BEST ANSWER

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\"