##// END OF EJS Templates
filemerge: add internal:tagmerge merge tool...
Angel Ezquerra -
r21922:50e20154 default
parent child Browse files
Show More
@@ -0,0 +1,265 b''
1 # tagmerge.py - merge .hgtags files
2 #
3 # Copyright 2014 Angel Ezquerra <angel.ezquerra@gmail.com>
4 #
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
7
8 # This module implements an automatic merge algorithm for mercurial's tag files
9 #
10 # The tagmerge algorithm implemented in this module is able to resolve most
11 # merge conflicts that currently would trigger a .hgtags merge conflict. The
12 # only case that it does not (and cannot) handle is that in which two tags point
13 # to different revisions on each merge parent _and_ their corresponding tag
14 # histories have the same rank (i.e. the same length). In all other cases the
15 # merge algorithm will choose the revision belonging to the parent with the
16 # highest ranked tag history. The merged tag history is the combination of both
17 # tag histories (special care is taken to try to combine common tag histories
18 # where possible).
19 #
20 # In addition to actually merging the tags from two parents, taking into
21 # account the base, the algorithm also tries to minimize the difference
22 # between the merged tag file and the first parent's tag file (i.e. it tries to
23 # make the merged tag order as as similar as possible to the first parent's tag
24 # file order).
25 #
26 # The algorithm works as follows:
27 # 1. read the tags from p1, p2 and the base
28 # - when reading the p1 tags, also get the line numbers associated to each
29 # tag node (these will be used to sort the merged tags in a way that
30 # minimizes the diff to p1). Ignore the file numbers when reading p2 and
31 # the base
32 # 2. recover the "lost tags" (i.e. those that are found in the base but not on
33 # p1 or p2) and add them back to p1 and/or p2
34 # - at this point the only tags that are on p1 but not on p2 are those new
35 # tags that were introduced in p1. Same thing for the tags that are on p2
36 # but not on p2
37 # 3. take all tags that are only on p1 or only on p2 (but not on the base)
38 # - Note that these are the tags that were introduced between base and p1
39 # and between base and p2, possibly on separate clones
40 # 4. for each tag found both on p1 and p2 perform the following merge algorithm:
41 # - the tags conflict if their tag "histories" have the same "rank" (i.e.
42 # length) _AND_ the last (current) tag is _NOT_ the same
43 # - for non conflicting tags:
44 # - choose which are the high and the low ranking nodes
45 # - the high ranking list of nodes is the one that is longer.
46 # In case of draw favor p1
47 # - the merged node list is made of 3 parts:
48 # - first the nodes that are common to the beginning of both
49 # the low and the high ranking nodes
50 # - second the non common low ranking nodes
51 # - finally the non common high ranking nodes (with the last
52 # one being the merged tag node)
53 # - note that this is equivalent to putting the whole low ranking
54 # node list first, followed by the non common high ranking nodes
55 # - note that during the merge we keep the "node line numbers", which will
56 # be used when writing the merged tags to the tag file
57 # 5. write the merged tags taking into account to their positions in the first
58 # parent (i.e. try to keep the relative ordering of the nodes that come
59 # from p1). This minimizes the diff between the merged and the p1 tag files
60 # This is donw by using the following algorithm
61 # - group the nodes for a given tag that must be written next to each other
62 # - A: nodes that come from consecutive lines on p1
63 # - B: nodes that come from p2 (i.e. whose associated line number is
64 # None) and are next to one of the a nodes in A
65 # - each group is associated with a line number coming from p1
66 # - generate a "tag block" for each of the groups
67 # - a tag block is a set of consecutive "node tag" lines belonging to
68 # the same tag and which will be written next to each other on the
69 # merged tags file
70 # - sort the "tag blocks" according to their associated number line
71 # - put blocks whose nodes come all from p2 first
72 # - write the tag blocks in the sorted order
73
74 import tags
75 import util
76 from node import nullid, hex
77 from i18n import _
78 import operator
79 hexnullid = hex(nullid)
80
81 def readtagsformerge(ui, repo, lines, fn='', keeplinenums=False):
82 '''read the .hgtags file into a structure that is suitable for merging
83
84 Sepending on the keeplinenumbers flag, clear the line numbers associated
85 with each tag. Rhis is done because only the line numbers of the first
86 parent are useful for merging
87 '''
88 filetags = tags._readtaghist(ui, repo, lines, fn=fn, recode=None,
89 calcnodelines=True)[1]
90 for tagname, taginfo in filetags.items():
91 if not keeplinenums:
92 for el in taginfo:
93 el[1] = None
94 return filetags
95
96 def grouptagnodesbyline(tagnodes):
97 '''
98 Group nearby nodes (i.e. those that must be written next to each other)
99
100 The input is a list of [node, position] pairs, corresponding to a given tag
101 The position is the line number where the node was found on the first parent
102 .hgtags file, or None for those nodes that came from the base or the second
103 parent .hgtags files.
104
105 This function groups those [node, position] pairs, returning a list of
106 groups of nodes that must be written next to each other because their
107 positions are consecutive or have no position preference (because their
108 position is None).
109
110 The result is a list of [position, [consecutive node list]]
111 '''
112 firstlinenum = None
113 for hexnode, linenum in tagnodes:
114 firstlinenum = linenum
115 if firstlinenum is not None:
116 break
117 if firstlinenum is None:
118 return [[None, [el[0] for el in tagnodes]]]
119 tagnodes[0][1] = firstlinenum
120 groupednodes = [[firstlinenum, []]]
121 prevlinenum = firstlinenum
122 for hexnode, linenum in tagnodes:
123 if linenum is not None and linenum - prevlinenum > 1:
124 groupednodes.append([linenum, []])
125 groupednodes[-1][1].append(hexnode)
126 if linenum is not None:
127 prevlinenum = linenum
128 return groupednodes
129
130 def writemergedtags(repo, mergedtags):
131 '''
132 write the merged tags while trying to minimize the diff to the first parent
133
134 This function uses the ordering info stored on the merged tags dict to
135 generate an .hgtags file which is correct (in the sense that its contents
136 correspond to the result of the tag merge) while also being as close as
137 possible to the first parent's .hgtags file.
138 '''
139 # group the node-tag pairs that must be written next to each other
140 for tname, taglist in mergedtags.items():
141 mergedtags[tname] = grouptagnodesbyline(taglist)
142
143 # convert the grouped merged tags dict into a format that resembles the
144 # final .hgtags file (i.e. a list of blocks of 'node tag' pairs)
145 def taglist2string(tlist, tname):
146 return '\n'.join(['%s %s' % (hexnode, tname) for hexnode in tlist])
147
148 finaltags = []
149 for tname, tags in mergedtags.items():
150 for block in tags:
151 block[1] = taglist2string(block[1], tname)
152 finaltags += tags
153
154 # the tag groups are linked to a "position" that can be used to sort them
155 # before writing them
156 # the position is calculated to ensure that the diff of the merged .hgtags
157 # file to the first parent's .hgtags file is as small as possible
158 finaltags.sort(key=operator.itemgetter(0))
159
160 # finally we can join the sorted groups to get the final contents of the
161 # merged .hgtags file, and then write it to disk
162 mergedtagstring = '\n'.join([tags for rank, tags in finaltags if tags])
163 fp = repo.wfile('.hgtags', 'wb')
164 fp.write(mergedtagstring + '\n')
165 fp.close()
166
167 def singletagmerge(p1nodes, p2nodes):
168 '''
169 merge the nodes corresponding to a single tag
170
171 Note that the inputs are lists of node-linenum pairs (i.e. not just lists
172 of nodes)
173 '''
174 if not p2nodes:
175 return p1nodes
176 if not p1nodes:
177 return p2nodes
178
179 # there is no conflict unless both tags point to different revisions
180 # and have a non identical tag history
181 p1currentnode = p1nodes[-1][0]
182 p2currentnode = p2nodes[-1][0]
183 if p1currentnode != p2currentnode and len(p1nodes) == len(p2nodes):
184 # cannot merge two tags with same rank pointing to different nodes
185 return None
186
187 # which are the highest ranking (hr) / lowest ranking (lr) nodes?
188 if len(p1nodes) >= len(p2nodes):
189 hrnodes, lrnodes = p1nodes, p2nodes
190 else:
191 hrnodes, lrnodes = p2nodes, p1nodes
192
193 # the lowest ranking nodes will be written first, followed by the highest
194 # ranking nodes
195 # to avoid unwanted tag rank explosion we try to see if there are some
196 # common nodes that can be written only once
197 commonidx = len(lrnodes)
198 for n in range(len(lrnodes)):
199 if hrnodes[n][0] != lrnodes[n][0]:
200 commonidx = n
201 break
202 lrnodes[n][1] = p1nodes[n][1]
203
204 # the merged node list has 3 parts:
205 # - common nodes
206 # - non common lowest ranking nodes
207 # - non common highest ranking nodes
208 # note that the common nodes plus the non common lowest ranking nodes is the
209 # whole list of lr nodes
210 return lrnodes + hrnodes[commonidx:]
211
212 def merge(repo, fcd, fco, fca):
213 '''
214 Merge the tags of two revisions, taking into account the base tags
215 Try to minimize the diff between the merged tags and the first parent tags
216 '''
217 ui = repo.ui
218 # read the p1, p2 and base tags
219 # only keep the line numbers for the p1 tags
220 p1tags = readtagsformerge(
221 ui, repo, fcd.data().splitlines(), fn="p1 tags",
222 keeplinenums=True)
223 p2tags = readtagsformerge(
224 ui, repo, fco.data().splitlines(), fn="p2 tags",
225 keeplinenums=False)
226 basetags = readtagsformerge(
227 ui, repo, fca.data().splitlines(), fn="base tags",
228 keeplinenums=False)
229
230 # recover the list of "lost tags" (i.e. those that were found on the base
231 # revision but not on one of the revisions being merged)
232 basetagset = set(basetags)
233 for n, pntags in enumerate((p1tags, p2tags)):
234 pntagset = set(pntags)
235 pnlosttagset = basetagset - pntagset
236 for t in pnlosttagset:
237 pntags[t] = basetags[t]
238 if pntags[t][-1][0] != hexnullid:
239 pntags[t].append([hexnullid, None])
240
241 conflictedtags = [] # for reporting purposes
242 mergedtags = util.sortdict(p1tags)
243 # sortdict does not implement iteritems()
244 for tname, p2nodes in p2tags.items():
245 if tname not in mergedtags:
246 mergedtags[tname] = p2nodes
247 continue
248 p1nodes = mergedtags[tname]
249 mergednodes = singletagmerge(p1nodes, p2nodes)
250 if mergednodes is None:
251 conflictedtags.append(tname)
252 continue
253 mergedtags[tname] = mergednodes
254
255 if conflictedtags:
256 numconflicts = len(conflictedtags)
257 ui.warn(_('automatic .hgtags merge failed\n'
258 'the following %d tags are in conflict: %s\n')
259 % (numconflicts, ', '.join(sorted(conflictedtags))))
260 return True, 1
261
262 writemergedtags(repo, mergedtags)
263 ui.note(_('.hgtags merged successfully\n'))
264 return False, 0
265
@@ -9,6 +9,7 b' from node import short'
9 from i18n import _
9 from i18n import _
10 import util, simplemerge, match, error, templater, templatekw
10 import util, simplemerge, match, error, templater, templatekw
11 import os, tempfile, re, filecmp
11 import os, tempfile, re, filecmp
12 import tagmerge
12
13
13 def _toolstr(ui, tool, part, default=""):
14 def _toolstr(ui, tool, part, default=""):
14 return ui.config("merge-tools", tool + "." + part, default)
15 return ui.config("merge-tools", tool + "." + part, default)
@@ -221,6 +222,16 b' def _imerge(repo, mynode, orig, fcd, fco'
221 return True, r
222 return True, r
222 return False, 0
223 return False, 0
223
224
225 @internaltool('tagmerge', True,
226 _("automatic tag merging of %s failed! "
227 "(use 'hg resolve --tool internal:merge' or another merge "
228 "tool of your choice)\n"))
229 def _itagmerge(repo, mynode, orig, fcd, fco, fca, toolconf, files, labels=None):
230 """
231 Uses the internal tag merge algorithm (experimental).
232 """
233 return tagmerge.merge(repo, fcd, fco, fca)
234
224 @internaltool('dump', True)
235 @internaltool('dump', True)
225 def _idump(repo, mynode, orig, fcd, fco, fca, toolconf, files, labels=None):
236 def _idump(repo, mynode, orig, fcd, fco, fca, toolconf, files, labels=None):
226 """
237 """
@@ -403,3 +403,204 b' commit hook on tag used to be run withou'
403 adding file changes
403 adding file changes
404 added 2 changesets with 2 changes to 2 files
404 added 2 changesets with 2 changes to 2 files
405
405
406 automatically merge resolvable tag conflicts (i.e. tags that differ in rank)
407 create two clones with some different tags as well as some common tags
408 check that we can merge tags that differ in rank
409
410 $ hg init repo-automatic-tag-merge
411 $ cd repo-automatic-tag-merge
412 $ echo c0 > f0
413 $ hg ci -A -m0
414 adding f0
415 $ hg tag tbase
416 $ cd ..
417 $ hg clone repo-automatic-tag-merge repo-automatic-tag-merge-clone
418 updating to branch default
419 2 files updated, 0 files merged, 0 files removed, 0 files unresolved
420 $ cd repo-automatic-tag-merge-clone
421 $ echo c1 > f1
422 $ hg ci -A -m1
423 adding f1
424 $ hg tag t1 t2 t3
425 $ hg tag --remove t2
426 $ hg tag t5
427 $ echo c2 > f2
428 $ hg ci -A -m2
429 adding f2
430 $ hg tag -f t3
431
432 $ cd ../repo-automatic-tag-merge
433 $ echo c3 > f3
434 $ hg ci -A -m3
435 adding f3
436 $ hg tag -f t4 t5 t6
437 $ hg tag --remove t5
438 $ echo c4 > f4
439 $ hg ci -A -m4
440 adding f4
441 $ hg tag t2
442 $ hg tag -f t6
443
444 $ cd ../repo-automatic-tag-merge-clone
445 $ hg pull
446 pulling from $TESTTMP/repo-automatic-tag-merge (glob)
447 searching for changes
448 adding changesets
449 adding manifests
450 adding file changes
451 added 6 changesets with 6 changes to 3 files (+1 heads)
452 (run 'hg heads' to see heads, 'hg merge' to merge)
453 $ hg merge --tool internal:tagmerge
454 merging .hgtags
455 2 files updated, 1 files merged, 0 files removed, 0 files unresolved
456 (branch merge, don't forget to commit)
457 $ hg status
458 M .hgtags
459 M f3
460 M f4
461 $ hg resolve -l
462 R .hgtags
463 $ cat .hgtags
464 9aa4e1292a27a248f8d07339bed9931d54907be7 t4
465 9aa4e1292a27a248f8d07339bed9931d54907be7 t6
466 9aa4e1292a27a248f8d07339bed9931d54907be7 t6
467 09af2ce14077a94effef208b49a718f4836d4338 t6
468 6cee5c8f3e5b4ae1a3996d2f6489c3e08eb5aea7 tbase
469 4f3e9b90005b68b4d8a3f4355cedc302a8364f5c t1
470 929bca7b18d067cbf3844c3896319a940059d748 t2
471 4f3e9b90005b68b4d8a3f4355cedc302a8364f5c t2
472 4f3e9b90005b68b4d8a3f4355cedc302a8364f5c t3
473 4f3e9b90005b68b4d8a3f4355cedc302a8364f5c t2
474 0000000000000000000000000000000000000000 t2
475 875517b4806a848f942811a315a5bce30804ae85 t5
476 9aa4e1292a27a248f8d07339bed9931d54907be7 t5
477 9aa4e1292a27a248f8d07339bed9931d54907be7 t5
478 0000000000000000000000000000000000000000 t5
479 4f3e9b90005b68b4d8a3f4355cedc302a8364f5c t3
480 79505d5360b07e3e79d1052e347e73c02b8afa5b t3
481
482 check that the merge tried to minimize the diff witht he first merge parent
483
484 $ hg diff --git -r 'p1()' .hgtags
485 diff --git a/.hgtags b/.hgtags
486 --- a/.hgtags
487 +++ b/.hgtags
488 @@ -1,9 +1,17 @@
489 +9aa4e1292a27a248f8d07339bed9931d54907be7 t4
490 +9aa4e1292a27a248f8d07339bed9931d54907be7 t6
491 +9aa4e1292a27a248f8d07339bed9931d54907be7 t6
492 +09af2ce14077a94effef208b49a718f4836d4338 t6
493 6cee5c8f3e5b4ae1a3996d2f6489c3e08eb5aea7 tbase
494 4f3e9b90005b68b4d8a3f4355cedc302a8364f5c t1
495 +929bca7b18d067cbf3844c3896319a940059d748 t2
496 4f3e9b90005b68b4d8a3f4355cedc302a8364f5c t2
497 4f3e9b90005b68b4d8a3f4355cedc302a8364f5c t3
498 4f3e9b90005b68b4d8a3f4355cedc302a8364f5c t2
499 0000000000000000000000000000000000000000 t2
500 875517b4806a848f942811a315a5bce30804ae85 t5
501 +9aa4e1292a27a248f8d07339bed9931d54907be7 t5
502 +9aa4e1292a27a248f8d07339bed9931d54907be7 t5
503 +0000000000000000000000000000000000000000 t5
504 4f3e9b90005b68b4d8a3f4355cedc302a8364f5c t3
505 79505d5360b07e3e79d1052e347e73c02b8afa5b t3
506
507 detect merge tag conflicts
508
509 $ hg update -C -r tip
510 3 files updated, 0 files merged, 2 files removed, 0 files unresolved
511 $ hg tag t7
512 $ hg update -C -r 'first(sort(head()))'
513 3 files updated, 0 files merged, 2 files removed, 0 files unresolved
514 $ printf "%s %s\n" `hg log -r . --template "{node} t7"` >> .hgtags
515 $ hg commit -m "manually add conflicting t7 tag"
516 $ hg merge --tool internal:tagmerge
517 merging .hgtags
518 automatic .hgtags merge failed
519 the following 1 tags are in conflict: t7
520 automatic tag merging of .hgtags failed! (use 'hg resolve --tool internal:merge' or another merge tool of your choice)
521 2 files updated, 0 files merged, 0 files removed, 1 files unresolved
522 use 'hg resolve' to retry unresolved file merges or 'hg update -C .' to abandon
523 [1]
524 $ hg resolve -l
525 U .hgtags
526 $ cat .hgtags
527 6cee5c8f3e5b4ae1a3996d2f6489c3e08eb5aea7 tbase
528 4f3e9b90005b68b4d8a3f4355cedc302a8364f5c t1
529 4f3e9b90005b68b4d8a3f4355cedc302a8364f5c t2
530 4f3e9b90005b68b4d8a3f4355cedc302a8364f5c t3
531 4f3e9b90005b68b4d8a3f4355cedc302a8364f5c t2
532 0000000000000000000000000000000000000000 t2
533 875517b4806a848f942811a315a5bce30804ae85 t5
534 4f3e9b90005b68b4d8a3f4355cedc302a8364f5c t3
535 79505d5360b07e3e79d1052e347e73c02b8afa5b t3
536 ea918d56be86a4afc5a95312e8b6750e1428d9d2 t7
537
538 $ cd ..
539
540 handle the loss of tags
541
542 $ hg clone repo-automatic-tag-merge-clone repo-merge-lost-tags
543 updating to branch default
544 4 files updated, 0 files merged, 0 files removed, 0 files unresolved
545 $ cd repo-merge-lost-tags
546 $ echo c5 > f5
547 $ hg ci -A -m5
548 adding f5
549 $ hg tag -f t7
550 $ hg update -r 'p1(t7)'
551 1 files updated, 0 files merged, 1 files removed, 0 files unresolved
552 $ printf '' > .hgtags
553 $ hg commit -m 'delete all tags'
554 created new head
555 $ hg update -r 'max(t7::)'
556 2 files updated, 0 files merged, 0 files removed, 0 files unresolved
557 $ hg merge -r tip --tool internal:tagmerge
558 merging .hgtags
559 0 files updated, 1 files merged, 0 files removed, 0 files unresolved
560 (branch merge, don't forget to commit)
561 $ hg resolve -l
562 R .hgtags
563 $ cat .hgtags
564 6cee5c8f3e5b4ae1a3996d2f6489c3e08eb5aea7 tbase
565 0000000000000000000000000000000000000000 tbase
566 4f3e9b90005b68b4d8a3f4355cedc302a8364f5c t1
567 0000000000000000000000000000000000000000 t1
568 4f3e9b90005b68b4d8a3f4355cedc302a8364f5c t2
569 4f3e9b90005b68b4d8a3f4355cedc302a8364f5c t3
570 4f3e9b90005b68b4d8a3f4355cedc302a8364f5c t2
571 0000000000000000000000000000000000000000 t2
572 875517b4806a848f942811a315a5bce30804ae85 t5
573 0000000000000000000000000000000000000000 t5
574 4f3e9b90005b68b4d8a3f4355cedc302a8364f5c t3
575 79505d5360b07e3e79d1052e347e73c02b8afa5b t3
576 0000000000000000000000000000000000000000 t3
577 ea918d56be86a4afc5a95312e8b6750e1428d9d2 t7
578 0000000000000000000000000000000000000000 t7
579 ea918d56be86a4afc5a95312e8b6750e1428d9d2 t7
580 fd3a9e394ce3afb354a496323bf68ac1755a30de t7
581
582 also check that we minimize the diff with the 1st merge parent
583
584 $ hg diff --git -r 'p1()' .hgtags
585 diff --git a/.hgtags b/.hgtags
586 --- a/.hgtags
587 +++ b/.hgtags
588 @@ -1,12 +1,17 @@
589 6cee5c8f3e5b4ae1a3996d2f6489c3e08eb5aea7 tbase
590 +0000000000000000000000000000000000000000 tbase
591 4f3e9b90005b68b4d8a3f4355cedc302a8364f5c t1
592 +0000000000000000000000000000000000000000 t1
593 4f3e9b90005b68b4d8a3f4355cedc302a8364f5c t2
594 4f3e9b90005b68b4d8a3f4355cedc302a8364f5c t3
595 4f3e9b90005b68b4d8a3f4355cedc302a8364f5c t2
596 0000000000000000000000000000000000000000 t2
597 875517b4806a848f942811a315a5bce30804ae85 t5
598 +0000000000000000000000000000000000000000 t5
599 4f3e9b90005b68b4d8a3f4355cedc302a8364f5c t3
600 79505d5360b07e3e79d1052e347e73c02b8afa5b t3
601 +0000000000000000000000000000000000000000 t3
602 ea918d56be86a4afc5a95312e8b6750e1428d9d2 t7
603 +0000000000000000000000000000000000000000 t7
604 ea918d56be86a4afc5a95312e8b6750e1428d9d2 t7
605 fd3a9e394ce3afb354a496323bf68ac1755a30de t7
606
General Comments 0
You need to be logged in to leave comments. Login now