I have a script running some Mercurial commands in non-interactive mode on a build server. One of the commands merges two branches and there is always a conflict in the .hgtags file during the merge because of the way the build scripts are set up.
How can I force Mercurial to always merge the .hgtags file using changes from both files, first from one, then from the other?
For example, if I the files to merge were
A
B
C
and
A
B
D
I would like the result to be
A
B
C
D
I am guessing I will need a custom merge tool. What tool provides this functionality?
Please see the answer below by Magras de La Mancha for better solution with Mercurial 3.1. The below is a simpler and more naive solution for older versions of Mercurial.
Yes, you need to configure a custom merge tool for your
.hgtagsfile. Mercurial doesn’t provide any special merge tool for.hgtags, you’re expected to merge it by hand using your normal three-way merge tool.Conflicts in the
.hgtagsfile can have two types:Silly conflicts: This is the situation you have and there is not really a conflict here. What happens is that one branch has
and the other branch has
Each tag refers to exactly one changeset, so there’s no conflict here. The merge should of course be the union on of the two files:
Real conflicts: There one branch has
and the other branch has
There is a real conflict here:
hg tag Cwas done on both branches, but the tags refer to different changesets. Resolving this is a manual task.If you can guarantee that you’ll only have silly conflicts and that you only have one tag per changeset, then you can use
to generate a new
.hgtagsfile. The key insight is that Mercurial knows how to merge tags internally! It does this all the time when you have two heads with different.hgtagsfiles. The above template simply generates a new.hgtagsfile based on this internal merge.If you might have more than one tag per changeset, then the above wont work — all the tags are printed on one line so you get a tag
foo barinstead of two tagsfooandbar. You can then use this style file instead:It outputs one line per tag, not changeset. You save this style somewhere and configure a merge tool:
You now have automatic tag merges. There are some caveats:
Three or more heads: The technique only works if you have two heads at the time of merging. If you have three heads or more, it’s possible for a deleted tag to re-appear. If you have heads X, Y, and Z, and the tag
Ais deleted in X, then Mercurial is normally able to figure out thatAis deleted overall. It does this based on a000...0 Aline in the.hgtagsfile in X. However, if you merge X and Y to get W, then the approach suggested will not contain any such000...0 Aline. The definition ofAfrom Z will now suddenly take effect and re-introduceA.Real conflicts: If you have real conflicts in
.hgtags, then the above method will silently pick the tag from the most recent head for you. The merge tool basically saveshg tagsin.hgtags, and the behavior ofhg tagswith multiple heads is explained in the wiki. Sincehg tagsunconditionally reads and silently merges the.hgtagsfile from all heads, there’s nothing we can do about it with this simple approach. Dealing with this would require a bigger script that reads the two.hgtagsfiles and detects the conflict.