How to configure SemanticMerge


How to configure with Git

This guide will explain how to configure SemanticMerge to be used from Git.

Since there are several different ways to use Git (from command line to different tools) the guide will cover the different options.

Important note (2013/10/22): We have fixed the configuration for all the Git tools since the “difftool” configuration was wrong. We were writing semanticmergetool -s $REMOTE -d $LOCAL and it MUST be semanticmergetool -s $LOCAL -d $REMOTE according to git documentation: $LOCAL is set to the name of the temporary file containing the contents of the diff pre-image and $REMOTE is set to the name of the temporary file containing the contents of the diff post-image.

Command Line Client Configuration

Configuring SemanticMerge to be used as diff and mergetool for Git is rather simple. There are two options:

  • Configure it using “git config” commands.
  • Configure it editing the “.gitconfig” file on your operating system.

Configure using “git config” commands

In order to configure SemanticMerge we will run the following commands:

Configure diff:
git config --global diff.tool semanticdiff
git config --global difftool.semanticdiff.cmd "C:/Users/pablo/AppData/Local/PlasticSCM4/semanticmerge/semanticmergetool.exe -s \"$LOCAL\" -d \"$REMOTE\""
git config --global difftool.prompt false

Which will produce the following “.gitconfig” (except the [user] part that I added for completeness):

[user]
	name = pablo
	email = [email protected]
[diff]
	tool = semanticdiff
[difftool "semanticdiff"]
	cmd = C:/Users/pablo/AppData/Local/PlasticSCM4/semanticmerge/semanticmergetool.exe -s \"$LOCAL\" -d \"$REMOTE\"
[difftool]
	prompt = false

In order to invoke “semanticmerge” as a diff tool we’re using the following command line:

semanticmergetool.exe -s $LOCAL -d $REMOTE

Whenever you run semanticmergetool with only two params (left and right of the diff) it will work as a diff tool. Run semanticmergetool -h to get help on the available params.

We enclose $REMOTE and $LOCAL (git params) in quotation marks to make sure the command is correctly invoked even if the paths contain spaces.

Important note: we used the following “path” for SemanticMerge: C:/Users/pablo/AppData/Local/PlasticSCM4/semanticmerge/ which you will obviously have to replace by the right one where semanticmergetool.exe is located in your computer.
Remark: note that we run the command “git config –global difftool.prompt false” which will affect to all the difftools and means that whenever you run git difftool it won’t ask you whether you really want to run the tool which we consider very annoying, but it is up to you to include this option according to your preferences.
Text diff: using this configuration SemanticMerge will use the included text based diff tool (called Xmerge) to compare the bodies of elements and text blocks. You can configure SemanticMerge to use any external tool you want. Check the Configuring external text diff and merge tools section for more information.
Configure mergetool:
git config --global merge.tool semanticmerge
git config --global mergetool.semanticmerge.cmd "C:/Users/pablo/AppData/Local/PlasticSCM4/semanticmerge/semanticmergetool.exe -d \"$LOCAL\" -s \"$REMOTE\" -b \"$BASE\" -r \"$MERGED\""
git config --global mergetool.semanticmerge.trustExitCode true
git config --global mergetool.prompt false
git config --global mergetool.keepBackup false

Which will produce the following “.gitconfig” (except the [user] part that I added for completeness):

[user]
	name = pablo
	email = [email protected]
[merge]
	tool = semanticmerge
[mergetool "semanticmerge"]
	cmd = C:/Users/pablo/AppData/Local/PlasticSCM4/semanticmerge/semanticmergetool.exe -s \"$REMOTE\" -d \"$LOCAL\" -b \"$BASE\" -r \"$MERGED\"
	trustExitCode = true
[mergetool]
	prompt = false
	keepBackup = false

In order to invoke “semanticmerge” as a merge tool we’re using the following command line:

semanticmergetool.exe -s $REMOTE -d $LOCAL -b $BASE -r $MERGED

Where $REMOTE, $LOCAL, $BASE and $MERGE are git params with the following meaning:

  • $REMOTE: is the file you’re merging (also known as “theirs” or “source” -> “source” is the name we use internally, that’s why the param is -s).
  • $LOCAL: is the file you’re merging to (a.k.a. “yours” or “destination” -> “destination” is the name we use internally, that’s why the param is -d).
  • $BASE: the common ancestor of the two files. Git calculates the right common ancestor (or base) using its internal merge algorithms and does a good job (although our own Plastic SCM does even a better one :P).
  • $MERGED: the file where the merge result will be written.
Important note: we used the following “path” for SemanticMerge: C:/Users/pablo/AppData/Local/PlasticSCM4/semanticmerge/ which you will obviously have to replace by the right one where semanticmergetool.exe is located in your computer.
Remark: please note we configured two global merge settings that we consider useful:
[mergetool]
	prompt = false
	keepBackup = false

but it is up to you to use them or not. prompt means git won’t ask you to confirm that you really want to launch an external tool when you run git mergetool to solve a merge, and keepBackup means git won’t store “.orig” files.

Text merge and diff: using this configuration SemanticMerge will use the included text based diff and merge tool (called Xmerge) to compare the bodies of elements and text blocks and merge them. You can configure SemanticMerge to use any external tool you want. Check the Configuring external text diff and merge tools section for more information.

Configure editing the “.gitconfig” file

The final result is equivalent to running the commands, but you might prefer to edit the “.gitconfig” file directly. (Check the git documentation to locate .gitconfig, on Windows it will normally be inside your user directory (c:\users\pablo in my case)).

The content of the “.gitconfig” file with SemanticMerge configured is:

[user]
	name = pablo
	email = [email protected]
[diff]
	tool = semanticdiff
[difftool "semanticdiff"]
	cmd = C:/Users/pablo/AppData/Local/PlasticSCM4/semanticmerge/semanticmergetool.exe -s \"$LOCAL\" -d \"$REMOTE\"
[difftool]
	prompt = false
[merge]
	tool = semanticmerge
[mergetool "semanticmerge"]
	cmd = C:/Users/pablo/AppData/Local/PlasticSCM4/semanticmerge/semanticmergetool.exe -d \"$LOCAL\" -s \"$REMOTE\" -b \"$BASE\" -r \"$MERGED\"
	trustExitCode = true
[mergetool]
	prompt = false
	keepBackup = false
Important note: we used the following “path” for SemanticMerge: C:/Users/pablo/AppData/Local/PlasticSCM4/semanticmerge/ which you will obviously have to replace by the right one where semanticmergetool.exe is located in your computer.
Remark: We’ve configured optional global parameters like “prompt” (both for diff and merge tools) and “keepBackup” but it is up to you to configure them this way or not.
Text merge and diff: using this configuration SemanticMerge will use the included text based diff and merge tool (called Xmerge) to compare the bodies of elements and text blocks and merge them. You can configure SemanticMerge to use any external tool you want. Check the Configuring external text diff and merge tools section for more information.
Note about file extensions: Git doesn’t allow to configure external tools by extension (as Tortoise Git does) so if you configure the SemanticMerge it will be run for all files when you decide to launch “external diff or merge tool”. SemanticMerge will warn you if it is invoked with an unsupported file type, then you will be able to launch the text based diff or merge tool. In case you want to skip this question and let SemanticMerge run the external text based tool directly add the --nolangwarn argument.
Note about automated merges: if you want SemanticMerge to automate as many conflict resolutions as possible, then add the -a argument to the merge command as follows:
cmd = C:/Users/pablo/AppData/Local/PlasticSCM4/semanticmerge/semanticmergetool.exe -d \"$LOCAL\" -s \"$REMOTE\" -b \"$BASE\" -r \"$MERGED\" -a

Configure Tortoise Git

SemanticMerge can also be invoked to diff and merge from Tortoise Git. Let’s see how to achieve it.

Diff configuration

In order to configure SemanticMerge as diff tool let’s go to the Tortoise Git “Settings”:

Then the following dialog will show up: go to “diff viewer” and then click (on the right) on the “Advanced…” button to configure a new external diff viewer.

Then a new dialog with all the configured tools based on file extensions will be displayed:

In the example we’re configuring the tool to run with c# (.cs) files, but you can configure it for any of the other supported extensions like java (.java), Visual Basic .NET (.vb) and more coming soon.

The “external program” line introduced is:

C:\Users\pablo\AppData\Local\PlasticSCM4\semanticmerge\semanticmergetool.exe -s %base -d %mine
Important note: we used the following “path” for SemanticMerge: C:/Users/pablo/AppData/Local/PlasticSCM4/semanticmerge/ which you will obviously have to replace by the right one where semanticmergetool.exe is located in your computer.

Once it is configured the list with all available tools will look like the following:

And now if you run “diff” from Tortoise Git, the SemanticDiff will show up.

Merge tool configuration

First go to the “Preferences” dialog: select “Merge Tool” on the left and then click on the “Advanced…” button on the right.

This will show up the merge tool configuration dialog as follows:

The command used to configure the tool was:

C:\Users\pablo\AppData\Local\PlasticSCM4\semanticmerge\semanticmergetool.exe -s %theirs -d %mine -b %base -r %merged

Now whenever a conflict resolution is needed, you can run SemanticMerge as follows:

which will launch SemanticMerge.

Advanced options

In case you want to use an external text merge tool instead of the built-in one, then it is recommended to use the SemanticMerge launch options configuration file instead of setting all the arguments in the configuration textbox provided by Tortoise Git.

Check Launch options configuration file for more information.

In case you use semanticmergetool.conf configuration file, then you do not need to set all the extra arguments like -emt or -edt in your tool configuration because they will be obtained from the configuration file.


Configure Atlassian Source Tree

Atlassian Source Tree allows to configure external diff and merge tools so it is rather simple to plug SemanticMerge. In fact both the merge and the diff tool are configured on the same screen.

Go to “Tools” then “Options”:

Then select “Custom” for both the “external diff tool” and the “merge tool” (as the following image shows).

In both cases the “command” will be the path to semanticmergetool.exe.

The arguments will be exactly as the ones configured for the git command line:

  • For “diff command”: -s \"$LOCAL\" -d \"$REMOTE\"
  • For “merge”: -s \"$REMOTE\" -d \"$LOCAL\" -b \"$BASE\" -r \"$MERGED\"

Note: if you want SemanticMerge to automate as many conflicts as possible, then add the -a argument to the merge arguments as follows:
-s \"$REMOTE\" -d \"$LOCAL\" -b \"$BASE\" -r \"$MERGED\" -a
Note about file extensions: Source Tree doesn’t allow to configure external tools by extension (as Tortoise Git does) so if you configure the SemanticMerge it will be run for all files when you decide to launch “external diff or merge tool”. SemanticMerge will warn you if it is invoked with an unsupported file type, then you will be able to launch the text based diff or merge tool. In case you want to skip this question and let SemanticMerge run the external text based tool directly add the --nolangwarn argument.

Advanced options

In case you want to use an external text merge tool instead of the built-in one, then it is recommended to use the SemanticMerge launch options configuration file instead of setting all the arguments in the configuration textbox provided by Atlassian.

Check Launch options configuration file for more information.


Configuring external text diff and merge tools

By default SemanticMerge will use Xmerge, which is included in the installer for Windows, as the default diff and merge tools for text blocks.

If you are on Linux or Mac OS or if you want to use a different tool, then you will need to configure an external diff and merge tool.

Let’s see how to configure Kdiff3 as the external diff and merge tools:

The command line to run SemanticMerge will add two new params:

  • -edt: which means “external diff tool”.
  • -emt: which means “external merge tool”.

And the params will be configured this way:

-edt="kdiff3.exe ""#sourcefile"" ""#destinationfile""" 
-emt="kiff3.exe -b=""#basefile"" ""#sourcefile"" ""#destinationfile"" --L1 ""#basesymbolic"" --L2 ""#sourcesymbolic"" --L3 ""#destinationsymbolic"" -o ""#output"""

And the final SemanticMerge command will be as follows:

semanticmergetool.exe -d \"$LOCAL\" -s \"$REMOTE\" -b \"$BASE\" -r \"$MERGED\" -edt="kdiff3.exe ""#sourcefile"" ""#destinationfile""" -emt="kiff3.exe -b=""#basefile"" ""#sourcefile"" ""#destinationfile"" --L1 ""#basesymbolic"" --L2 ""#sourcesymbolic"" --L3 ""#destinationsymbolic"" -o ""#output"""

And a “.gitconfig” file using Kdiff3 will be:

[user]
	name = pablo
	email = [email protected]
[diff]
	tool = semanticdiff
[difftool "semanticdiff"]
	cmd = C:/Users/pablo/AppData/Local/PlasticSCM4/semanticmerge/semanticmergetool.exe -s \"$LOCAL\" -d \"$REMOTE\" -edt=\"'c:/program files/kdiff3/kdiff3.exe' '"#"sourcefile' '"#"destinationfile'\"
[difftool]
	prompt = false
[merge]
	tool = semanticmerge
[mergetool "semanticmerge"]
	cmd = C:/Users/pablo/AppData/Local/PlasticSCM4/semanticmerge/semanticmergetool.exe -d \"$LOCAL\" -s \"$REMOTE\" -b \"$BASE\" -r \"$MERGED\" -edt=\"'c:/program files/kdiff3/kdiff3.exe' '"#"sourcefile' '"#"destinationfile'\"  -emt=\"'C:/Program Files/KDiff3/kdiff3.exe' -b '"#"basefile' '"#"sourcefile' '"#"destinationfile' --L1 "#"basesymbolic --L2 "#"sourcesymbolic --L3 "#"destinationsymbolic -o '"#"output' \"-e2mt=\"'C:/Program Files/KDiff3/kdiff3.exe' '"#"sourcefile' '"#"destinationfile' --L1 "#"sourcesymbolic --L2 "#"destinationsymbolic -o '"#"output' \"
	trustExitCode = true
[mergetool]
	prompt = false
	keepBackup = false
Note: take care of copying correctly the quotation marks (both ” and ‘).
Note the -e2mt param: which is the configuration for 2-way merge as described in the section Support for two way merge.

Support for two way merge

There are conflicts where two developers added a new method but not exactly with the same contents. If that happens you will need to configure a “2 way merge tool” since the built in tool is not able to handle the case.

Consider the following scenario:

Two developers added the same method in parallel as the image shows. During merge SemanticMerge will detect that the same method has been added twice (instead of adding the same method twice in two different locations as a regular text based merge tool would do) but if the methods bodies are not exactly the same a “2-way merge” will be needed to combine them together.

When such a conflict is detected, SemanticMerge will display the following option:

The “2 merge” (which means 2-way merge) button will run the tool defined in the -e2mt argument.

The configuration for 2-way merge is as follows:

[user]
	name = pablo
	email = [email protected]
[diff]
	tool = semanticdiff
[difftool "semanticdiff"]
	cmd = C:/Users/pablo/AppData/Local/PlasticSCM4/semanticmerge/semanticmergetool.exe -s \"$LOCAL\" -d \"$REMOTE\" -edt=\"'c:/program files/kdiff3/kdiff3.exe' '"#"sourcefile' '"#"destinationfile'\"
[difftool]
	prompt = false
[merge]
	tool = semanticmerge
[mergetool "semanticmerge"]
	cmd = C:/Users/pablo/AppData/Local/PlasticSCM4/semanticmerge/semanticmergetool.exe -d \"$LOCAL\" -s \"$REMOTE\" -b \"$BASE\" -r \"$MERGED\" -edt=\"'c:/program files/kdiff3/kdiff3.exe' '"#"sourcefile' '"#"destinationfile'\"  -emt=\"'C:/Program Files/KDiff3/kdiff3.exe' -b '"#"basefile' '"#"sourcefile' '"#"destinationfile' --L1 "#"basesymbolic --L2 "#"sourcesymbolic --L3 "#"destinationsymbolic -o '"#"output' \" -e2mt=\"'C:/Program Files/KDiff3/kdiff3.exe' '"#"sourcefile' '"#"destinationfile' --L1 "#"sourcesymbolic --L2 "#"destinationsymbolic -o '"#"output' \"
	trustExitCode = true
[mergetool]
	prompt = false
	keepBackup = false

Generic tool configuration

In case you need to configure SemanticMerge for a different tool than the ones included in this guide remember it is a very easy process.

All you need to know is how SemanticMerge is invoked.

Configuring SemanticMerge as a diff tool

To run SemanticMerge as a diff tool you will invoke it this way:

semanticmergetool -d remotefile -s localfile

where:

  • “localfile” is the file you have in your working copy
  • “remotefile” the one to compare to

You can also think about it as -s defining the “source” (the left side of the comparison, the origin of the changes) and -d defining the destination (the right side of the comparison).

You will have to replace “remotefile” and “localfile” by the corresponding variables used by the tool you’re configuring. In Git command line they will be replaced by $REMOTE and $LOCAL but in Tortoise Git they will be %base and %mine.

Also note that normally you will wrap the variables with quotation marks to be able to handle correctly paths that contain spaces:

semanticmegetool -d \"$LOCAL\" -s \"$REMOTE\"

Check semanticmergetool.exe -h for more options.

Configuring SemanticMerge as a merge tool

In case you have to configure SemanticMerge as external diff tool for your preferred version control system, IDE or GUI, remember how to launch SemanticMerge as a merge tool:

semanticmergetool  -s REMOTE -d LOCAL -b BASE -r MERGED

where:

  • REMOTE is also known as “theirs”, it means the code you are merging from or the “source of the merge”. You use the -s argument to define it.
  • LOCAL is also known as “yours”, the destination of the merge, or the code you’re merging “to”. You use the -d argument to define it.
  • BASE is also known as the “common ancestor”. You use the -b argument to define it.
  • MERGED is the result of the merge, the file that will be written with the result of the merge operation. It is defined with the -r argument.

Remember that you will have to replace this names by the correct variables used by your tool ($REMOTE, $LOCAL, $BASE and $MERGED for Git command line but %theirs, %mine, %base and %merged for Tortoise Git).

Also remember that you will normally wrap the variables with quotation marks to be able to deal with paths that contain spaces. This way, the way to configure the tool inside the Git command line is as follows:

semanticmergetool -s \"$REMOTE\" -d \"$LOCAL\" -b \"$BASE\" -r \"$MERGED\"

Please check semanticmergetool -h to find all the possible command line options, including symbolic names that can be passed to the tool to better understand the merge.


How to configure with TFS

Let’s see the steps to get SemanticMerge configured with TeamFoundationServer as both diff and merge tool.


SemanticMerge as Diff Tool for TFS

Go to “Tools/Options”:

And select “Visual Studio Team Foundation Server” then click on “Configure User Tools…” as the figure below shows:

Then a dialog like the one below will show up and you have to choose “Add”:

And you’ll enter in the “Configure Tool” dialog which is rather simple as you can see below. You’ve to set the “.cs” extension, then the “Compare” operation and finally the command and arguments as they’re described below:

  1. Path to SemanticMerge:
    C:\Users\<yourname>\AppData\Local\PlasticSCM4\semanticmerge\semanticmergetool.exe
  2. Arguments: -s=%1 -d=%2

To learn more about the argument magic please go here.

And now you’re done!


SemanticMerge as Merge Tool for TFS

Go to “Tools/Options”:

And select “Visual Studio Team Foundation Server” then click on “Configure User Tools…” as the figure below shows:

Then a dialog like the one below will show up and you have to choose “Add”:

Then you’ll enter in the “Configure Tool” dialog but this time you’ve to select “Merge” as the operation:

As “Command” you’ve to specify the path to semanticmergetool.exe and the arguments are as follows:

-s=%1 -d=%2 -b=%3 -r=%4 -sn=%6 -dn=%7 -bn=%8 -emt=default -edt=default

To learn more about the argument magic please go here.


How to configure with Perforce

This article will explain how to configure SemanticMerge as the diff and merge tools for Perforce.

Important note (2013/11/27): We have done some changes to ease the Perforce setup. Now the .bat file is no longer needed.

Configuring SemanticMerge as the Diff Tool

First go to Preferences:

And the following dialog will show up:

You’ve to click on “Diff” on the left then click on “Add” to add a new external application by extension (we’re going to set it up for C# only, you can set it for VB.net files too repeating the process with .vb extension).

Once you click on “Add” the following dialog will appear:

Make sure you enter the “.cs” extension and then you’ll have to select the path to SemanticMerge as application. Then you have to type the positional arguments which are source (theirs) and destination (yours) and they are set up by Perforce:

  1. Path to SemanticMerge:
    C:\Users\<yourname>\AppData\Local\PlasticSCM4\semanticmerge\semanticmergetool.exe
  2. Arguments: %1 %2

In case you need to modify the order of the arguments you can do that with the configuration file including the --fileparamorder flag (call up semanticmergetool --help for further information).


Configuring SemanticMerge as the Merge Tool

First you’ve to click on “Preferences”:

Then the “Preferences Dialog” will show up and you’ll see something like this:

Select “Merge” then click on “Add” to add a new external application by extension (we’re going to set it up for C# only, you can set it for VB.net files too repeating the process with .vb extension).

Once you do that you’ll see a dialog like the following:

Make sure you enter the “.cs” extension and then you’ll have to select the path to SemanticMerge as application. Then you have to type the positional arguments which are source (theirs), destination (yours), base and output (result) and they are set up by Perforce:.

  1. Path to SemanticMerge:
    C:\Users\<yourname>\AppData\Local\PlasticSCM4\semanticmerge\semanticmergetool.exe
  2. Arguments: %1 %2 %b %r

In case you need to modify the order of the arguments you can do that with the configuration file including the --fileparamorder flag (call up semanticmergetool --help for further information).

And you’re ready to use SemanticMerge with Perforce!


How to configure with Plastic SCM

This article will explain how to configure SemanticMerge as the diff and merge tools for Plastic SCM.

Important note (2014/03/31): We have fixed the required command line to configure SemanticMerge as the Diff Tool by removing an unnecessary new line.

Configuring SemanticMerge as the Diff Tool

First go to Preferences:

The following pop-up dialog will display; select “Diff tools” and click on “Add…”:

Now we need to configure SemanticMerge as the diff tool (see dialog below):

  1. Select “External diff tool”
  2. Select “Use with files that match the following pattern:” and add the .cs extension (We currently only support C# files today)
  3. The command line you need to enter is as follows:
    "C:\Users\<yourname>\AppData\Local\PlasticSCM4\semanticmerge\semanticmergetool.exe" -s="@sourcefile" -sn="@sourcesymbolic" -d="@destinationfile" -dn="@destinationsymbolic" -i="@comparationmethod" -e="@fileencoding" -edt=default
    Note: Remember to substitute <yourname> with your own username 🙂

With this configuration, we will be running Plastic SCM’s Xdiff tool to execute text-based diffs (to compare methods and so on), which is done by adding the -edt=default parameter.

Once you’ve set up the newly configured tool, remember to change the priority to the top of the list, as the following image shows:


Configuring an external Diff Tool

If you prefer to use another diff tool other than Xdiff (ex: kdiff3), you can configure the tool as follows:

"C:\Users\<yourname>\AppData\Local\PlasticSCM4\semanticmerge\semanticmergetool.exe" -s="@sourcefile" -sn="@sourcesymbolic" -d="@destinationfile" -dn="@destinationsymbolic" -i="@comparationmethod" -e="@fileencoding" -edt=""C:\<Path to kdiff3 executable>\kdiff3.exe" ""#sourcefile"" ""#destinationfile"""
Note: Remember to substitute <yourname> and <Path to kdiff3 executable> accordingly 🙂

Using this configuration, when you diff text, you’ll see something like the following:


Configuring SemanticMerge as the Merge Tool

First go to Preferences:

The following pop-up dialog will display; select “Merge tools” and click on “Add…” as the following figure shows:

Add Merge Tool

As with the diff tool, you need to select the appropriate options and enter the right command line to launch SemanticMerge:

  1. Select “External merge tool”
  2. Select “Use with files that match the following pattern:” and add the .cs extension (We currently only support C# files today)
  3. The command line you need to enter is as follows:
    "C:\Users\<yourname>\AppData\Local\PlasticSCM4\semanticmerge\semanticmergetool.exe" -b="@basefile" -bn="@basesymbolic" -s="@sourcefile" -sn="@sourcesymbolic" -d="@destinationfile"  -dn="@destinationsymbolic" -r="@output" -i="@comparationmethod" -e="@fileencoding" -emt=default -edt=default
    Note: Remember to substitute <yourname> accordingly, and don’t forget the quotation marks

Once you’ve set up the newly configured tool, remember to change the priority to the top of the list, as the following image shows.


Configuring an external Merge Tool (like Kdiff3) to deal with text conflicts

SemanticMerge handles the merges at the structure level; however, when two contributors have modified a method, it needs to run a 3-way text-based merge. With this configuration, we will be running Plastic SCM’s Xdiff tool to execute text-based diffs (to compare methods and so on), which is done by adding the -edt and -emt parameters:

"C:\Users\<yourname>\AppData\Local\PlasticSCM4\semanticmerge\semanticmergetool.exe" -b="@basefile" -bn="@basesymbolic" -s="@sourcefile" -sn="@sourcesymbolic" -d="@destinationfile"  -dn="@destinationsymbolic" -r="@output" -i="@comparationmethod" -e="@fileencoding" -edt=""C:\<Path to kdiff3 executable>\kdiff3.exe" ""#sourcefile"" ""#destinationfile""" -emt="" C:\<Path to kdiff3 executable>\kdiff3.exe " -b=""#basefile"" ""#sourcefile"" ""#destinationfile"" --L1 ""#basesymbolic"" --L2 ""#sourcesymbolic"" --L3 ""#destinationsymbolic"" -o ""#output"""
Note: Remember to substitute <yourname> and <Path to kdiff3 executable> accordingly 🙂

This configuration will use Kdiff3 for both diff and merge


How to configure with Mercurial

This article will explain how to configure SemanticMerge as the diff and merge tools for Mercurial.

Important note (2014/02/25): We have fixed the mercurial.ini file and the TortoiseHg Settings picture.
Important note (2014/01/27): We have fixed the configuration for setting up a different diff and merge tool.

Configuring the mercurial.ini file

First you’ve to find the “mercurial.ini” file which is normally at %HOMEPATH%\mercurial.ini.

Once you have it, open it up and make sure you add the following content (remember to replace the <yourname> entries by the right paths, check the paths are correct in yoru case and so on):

[extensions]
extdiff =
[extdiff]
cmd.semanticdiff = C:\Users\<yourname>\AppData\Local\PlasticSCM4\semanticmerge\semanticmergetool.exe
opts.semanticdiff = $parent $child
cmd.kdiff3 = kdiff3.exe
opts.kdiff3 = $parent $child
[merge-tools]
semantic.executable = C:\Users\<yourname>\AppData\Local\PlasticSCM4\semanticmerge\semanticmergetool.exe
semantic.premerge=False
semantic.binary=False
semantic.args=-b=$base -s=$local -d=$other -r=$output -l=csharp -edt="kdiff3.exe ""#sourcefile"" ""#destinationfile""" -emt="kdiff3.exe ""#basefile"" ""#sourcefile"" ""#destinationfile"" --L1 ""#basesymbolic"" --L2 ""#sourcesymbolic"" --L3 ""#destinationsymbolic"" -o ""#output"""
semantic.gui=True
semantic.checkconflicts=True
kdiff3.executable = kdiff3.exe
kdiff3.args = $base $local $other -o $output
kdiff3.gui=True
[merge-patterns]
**.cs = semantic
**.** = kdiff3
[diff-patterns]
**.cs = semanticdiff
**.** = kdiff3
[tortoisehg]
vdiff = semanticdiff
[ui]
username = <yourname>
merge = semantic

Basically you’ve to enable the “diff extension” (first area), then setup the difftool (the key is adding the opts.semanticdiff entry setting up the right params to invoke semantic as difftool.

Then you define a new “merge tool” in the [merge-tools] section. Also specify that you just need the tool to work with “.cs” files and you’re done.


How it looks like inside TortoiseHg

Once it is set up, you’ll see a few changes inside Tortoise Hg. Go to “Preferences” and you’ll see the “semantic” set as diff and merge tool.

And then when you merge you’ll be able to select “semantic” as your merge tool as you can see below:


Setting up a different text-based diff and merge tool

SemanticMerge uses text-based diff and merge tools to compare or merge the method bodies, properties and so on.

By default it will use the included “mergetool.exe” which is the Xdiff&Xmerge; tool included in Plastic SCM.

But you can use your favorite one just modifying the -edt and -emt params as you can see below:

semantic.args=-b=$base -s=$local -d=$other -r=$output -l=csharp -edt="kdiff3.exe ""#sourcefile"" ""#destinationfile""" -emt="kdiff3.exe ""#basefile"" ""#sourcefile"" ""#destinationfile"" --L1 ""#basesymbolic"" --L2 ""#sourcesymbolic"" --L3 ""#destinationsymbolic"" -o ""#output"""

Configuring SemanticMerge with SourceTree

It is possible to use SemanticMerge as diff and merge tool for Mercurial using Atlassian SourceTree. Please read the guide carefully since SourceTree has some issues dealing with external tools in Mercurial.

Configuring SemanticMerge as diff tool for Mercurial with SourceTree

As of SouceTree 1.3.1.o there are some issues in the external diff tool configuration that you must take into account in order to be able to use SemanticMerge. These issues are related to SourceTree and not SemanticMerge but we were able to do some workarounds to allow SourceTree Hg users to take advantage of Semantic.

Check the following image:

As you can see in order to make SemanticMerge work as an external diff tool for SourceTree with Mercurial just keep the arguments empty and it will work.

Note: please consider that the same does not apply for SourceTree with Git, where the arguments seem to work correctly.

Configuring SemanticMerge as merge tool for Mercurial with SourceTree

The merge tool configuration for SourceTree and Mercurial is rather simple. All you have to do is to specify the path to semanticmergetool.exe and then the following arguments:

-d \"$LOCAL\" -s \"$REMOTE\" -b \"$BASE\" -r \"$MERGED\"

As you can see, while the diff tool configuration has to deal with a strange bug, the external merge tool seems to be working fine.

Note: SourceTree invokes the external merge tool in a quite strange way: check the arguments that SemanticMerge receives from SourceTree on a regular merge operation:
  • -d main.cs.orig
  • -s main.cs~other.gwgxtv
  • -b main.cs~base.zlpllj
  • -r main.cs

As you can see it does not respect the file extension except on the destination file (normally version controls respect the “destination” or “yours” because this is the file in the working copy).

SemanticMerge will try to figure out the correct extension from the -d argument and if it can’t find the extension, it will try with the -r argument.

In case none of the files sent as arguments to SemanticMerge has a known file extension, Semantic will fail.

You can always force the -l=csharp or -l=java to “force the language” (or any other supported language).


How to configure with IntelliJ (and Android Studio)

Configuring IntelliJ (also valid for Android Studio) to use SemanticMerge is quite simple, just go to “File/ Settings” and then to “External Diff Tools”.


SemanticMerge as Merge Tool

Go to File/Settings:

Then click to select “Use external merge tool” like the following picture shows:

Select the path to semanticmergetool.exe and then introduce the params as follows: -s %1 -b %2 -d %3 -r %4

Note: we have added support to specify the merge params as:
  • -s=PARAM
  • and also -s PARAM (blank instead of “=”)
Because otherwise IntelliJ won’t be able to correctly parse its params (%1, %2 and so on).

Later when you run a merge using the Git plugin (for instance) you’ll be prompted with the following dialog:

You will have to press “Merge” to launch SemanticMerge.

Note: There is a bug in IntelliJ right now (here) so once you run the merge it goes back to the same screen of “Files Merges with Conflicts” and you’ve to select “Accept Yours”. We expect JetBrains will get the bug fixed soon.

Launch options configuration file

All the common configuration arguments that can be passed to SemanticMerge to define the external text diff or merge took, 2-way merge tool and so on, can be easily configured in a configuration file.

This way you don’t need to setup a long configuration line (potentially having trouble with scape characters) in your version control diff and merge tool configuration because every argument will be defined there.

The file to configure the arguments is named semanticmergetool.conf and can be place under the semantic user directory (C:\Users\pablo\AppData\Local\semanticmerge in my case) or the location where the binaries are.

A typical semanticmergetool.conf file to use external Kdiff3 as text merge tool will be as follows:

# external diff tool -> kdiff3
-edt="c:/program files/kdiff3/kdiff3.exe" "#sourcefile" "#destinationfile"
# external mergetool -> kdiff3 with symbolic info
-emt="C:/Program Files/KDiff3/kdiff3.exe" -b "#basefile" "#sourcefile" "#destinationfile" --L1 "#basesymbolic" --L2 "#sourcesymbolic" --L3 "#destinationsymbolic" -o "#output"
# external 2-way merge -> also Kdiff3
-e2mt="C:/Program Files/KDiff3/kdiff3.exe" "#sourcefile" "#destinationfile" --L1 "#sourcesymbolic" --L2 "#destinationsymbolic" -o "#output"
# try to automate the conflicts as much as possible
-a 
# do not check updates
--nocheckupdates

And this way the configuration to run semanticmerge inside Git, or Mercurial, Perforce and so on, will not need to specify all these params.

Arguments priority

The priority of the arguments is as follows:

  1. First arguments specified in the command line
  2. Then arguments specified in semanticmergetool.conf