Search code examples
visual-studio-codemercurialmergetool

How to use Visual Studio Code as the default MergeTool for mercurial


To change the default merge method, what is configuration is needed to provide the .hgrc file. I tried the google but it didn't work.


Solution

  • I assume you wanted to use "Visual Studio Code"'s version control - Merge Conflicts feature. And I guess that you wanted the same feature, but with mercurial's source control.

    So, the first thing we have to do is to configure mercurial to use VSCode as the merge tool. To do it, I look at the following documentation.

    Seems like the mercurial want to know the Registry value of VSCode for activating it. I looked around the internet and found this gitlab Issue with the relevant keys.

    So the current additional configuration is:

    [merge-tools]
    vscode.regkey=SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{EA457B21-F73E-494C-ACAB-524FDE069978}_is1
    vscode.regname=DisplayIcon
    vscode.args= --wait $output
    vscode.binary=False
    vscode.gui=True
    vscode.checkconflicts=True
    vscode.premerge=keep
    
    [extensions]
    extdiff=
    
    [merge-patterns]
    *.*=vscode
    
    [ui]
    merge= :vscode
    
    

    BAM! Works!

    Possible problems

    Missing line terminator

    During the testing of this configuration, I encountered a problem when there is a conflict that has no line terminator (\n) at the end.

    Notice: There is no a '\n' at the end of the file

    file.txt (branch 'foo')

    foo
    

    file.txt (branch 'bar')

    bar
    

    The result of $output is:

    file.txt (merge output)

    <<<<<<< working copy
    foo=======
    bar>>>>>>> merge rev
    

    and VSCode's merge conflict solver cannot detect this part of the code as a conflict because the code that detects ======= and >>>>>>> assumes that they are places at the begging of the line like this:

    <<<<<<< working copy
    foo
    =======
    bar
    >>>>>>> merge rev
    

    We can see this assumption here and here. I think it is perfectly fine, but I had to add this to avoid any future problems.

    Here you have it, have fun.