Mercurial > hg4j
comparison src/org/tmatesoft/hg/internal/ForwardAnnotateInspector.java @ 676:3219cfadda49
Switch to alternative annotate producer (walks from parents to children). Refactor FileAnnotation to match updated annotate approach
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Thu, 18 Jul 2013 18:03:51 +0200 |
parents | |
children | 1c49c0cee540 |
comparison
equal
deleted
inserted
replaced
675:a20121a2bba6 | 676:3219cfadda49 |
---|---|
1 /* | |
2 * Copyright (c) 2013 TMate Software Ltd | |
3 * | |
4 * This program is free software; you can redistribute it and/or modify | |
5 * it under the terms of the GNU General Public License as published by | |
6 * the Free Software Foundation; version 2 of the License. | |
7 * | |
8 * This program is distributed in the hope that it will be useful, | |
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 * GNU General Public License for more details. | |
12 * | |
13 * For information on how to redistribute this software under | |
14 * the terms of a license other than GNU General Public License | |
15 * contact TMate Software at support@hg4j.com | |
16 */ | |
17 package org.tmatesoft.hg.internal; | |
18 | |
19 import org.tmatesoft.hg.core.HgAnnotateCommand.Inspector; | |
20 import org.tmatesoft.hg.core.HgAnnotateCommand.LineInfo; | |
21 import org.tmatesoft.hg.core.HgBlameInspector; | |
22 import org.tmatesoft.hg.core.HgCallbackTargetException; | |
23 import org.tmatesoft.hg.core.HgDiffCommand; | |
24 import org.tmatesoft.hg.core.HgException; | |
25 import org.tmatesoft.hg.core.HgIterateDirection; | |
26 import org.tmatesoft.hg.repo.HgLookup; | |
27 import org.tmatesoft.hg.repo.HgRepository; | |
28 import org.tmatesoft.hg.util.CancelSupport; | |
29 import org.tmatesoft.hg.util.CancelledException; | |
30 import org.tmatesoft.hg.util.ProgressSupport; | |
31 | |
32 /** | |
33 * Annotate file history iterating from parents to children | |
34 * | |
35 * At the moment, doesn't handle start from any revision but 0 | |
36 * | |
37 * (+) May report annotate for any revision in the visited range. | |
38 * | |
39 * @see ReverseAnnotateInspector | |
40 * @author Artem Tikhomirov | |
41 * @author TMate Software Ltd. | |
42 */ | |
43 public class ForwardAnnotateInspector implements HgBlameInspector, HgBlameInspector.RevisionDescriptor.Recipient { | |
44 final IntMap<IntSliceSeq> all = new IntMap<IntSliceSeq>(100); | |
45 // revision->map(lineNumber->lineContent) | |
46 private final IntMap<IntMap<byte[]>> lineContent = new IntMap<IntMap<byte[]>>(100); | |
47 private IntSliceSeq current; | |
48 private RevisionDescriptor revDescriptor; | |
49 | |
50 /** | |
51 * @return desired order of iteration for diff | |
52 */ | |
53 public HgIterateDirection iterateDirection() { | |
54 return HgIterateDirection.OldToNew; | |
55 } | |
56 | |
57 public void report(int revision, Inspector insp, ProgressSupport progress, CancelSupport cancel) throws HgCallbackTargetException, CancelledException { | |
58 int totalLines = 0; | |
59 for (IntTuple t : all.get(revision)) { | |
60 totalLines += t.at(0); | |
61 } | |
62 progress.start(totalLines); | |
63 LineImpl li = new LineImpl(); | |
64 int line = 1; | |
65 for (IntTuple t : all.get(revision)) { | |
66 IntMap<byte[]> revLines = lineContent.get(t.at(1)); | |
67 for (int i = 0, x = t.at(0); i < x; i++) { | |
68 final int lineInRev = t.at(2) + i; | |
69 final byte[] lc = revLines.get(lineInRev); | |
70 li.init(line++, t.at(1), lc); | |
71 insp.next(li); | |
72 progress.worked(1); | |
73 cancel.checkCancelled(); | |
74 } | |
75 } | |
76 progress.done(); | |
77 } | |
78 | |
79 public void start(RevisionDescriptor rd) throws HgCallbackTargetException { | |
80 all.put(rd.targetChangesetIndex(), current = new IntSliceSeq(3)); | |
81 revDescriptor = rd; | |
82 } | |
83 | |
84 public void done(RevisionDescriptor rd) throws HgCallbackTargetException { | |
85 revDescriptor = null; | |
86 } | |
87 | |
88 public void same(EqualBlock block) throws HgCallbackTargetException { | |
89 copyBlock(block.originChangesetIndex(), block.originStart(), block.length()); | |
90 } | |
91 | |
92 public void added(AddBlock block) throws HgCallbackTargetException { | |
93 if (revDescriptor.isMerge() && block.originChangesetIndex() == revDescriptor.mergeChangesetIndex()) { | |
94 copyBlock(block.originChangesetIndex(), block.insertedAt(), block.totalAddedLines()); | |
95 return; | |
96 } | |
97 BlockData addedLines = block.addedLines(); | |
98 IntMap<byte[]> revLines = lineContent.get(block.targetChangesetIndex()); | |
99 if (revLines == null) { | |
100 lineContent.put(block.targetChangesetIndex(), revLines = new IntMap<byte[]>(block.totalAddedLines())); | |
101 } | |
102 for (int i = 0; i < block.totalAddedLines(); i++) { | |
103 revLines.put(block.firstAddedLine() + i, addedLines.elementAt(i).asArray()); | |
104 } | |
105 current.add(block.totalAddedLines(), block.targetChangesetIndex(), block.firstAddedLine()); | |
106 } | |
107 | |
108 public void changed(ChangeBlock block) throws HgCallbackTargetException { | |
109 added(block); | |
110 } | |
111 | |
112 public void deleted(DeleteBlock block) throws HgCallbackTargetException { | |
113 } | |
114 | |
115 private void copyBlock(int originChangesetIndex, int originStart, int length) { | |
116 IntSliceSeq origin = all.get(originChangesetIndex); | |
117 assert origin != null; // shall visit parents before came to this child | |
118 int originPos = 0; | |
119 int targetBlockLen = length; | |
120 for (IntTuple t : origin) { | |
121 int originBlockLen = t.at(0); | |
122 int originBlockEnd = originPos + originBlockLen; | |
123 if (originBlockEnd > originStart) { | |
124 int originBlockOverlap = Math.min(originBlockLen, originBlockEnd - originStart); | |
125 assert originBlockOverlap > 0; | |
126 originBlockOverlap = Math.min(originBlockOverlap, targetBlockLen); | |
127 int originBlockLine = t.at(2); | |
128 if (originPos < originStart) { | |
129 originBlockLine += originBlockLen-originBlockOverlap; | |
130 } | |
131 // copy fragment of original block; | |
132 current.add(originBlockOverlap, t.at(1), originBlockLine); | |
133 targetBlockLen -= originBlockOverlap; | |
134 if (targetBlockLen == 0) { | |
135 break; | |
136 } | |
137 } | |
138 originPos += originBlockLen; | |
139 } | |
140 } | |
141 | |
142 | |
143 public static void main(String[] args) throws HgCallbackTargetException, CancelledException, HgException { | |
144 HgRepository repo = new HgLookup().detect("/home/artem/hg/junit-test-repos/test-annotate/"); | |
145 HgDiffCommand cmd = new HgDiffCommand(repo); | |
146 cmd.file(repo.getFileNode("file1")).order(HgIterateDirection.OldToNew); | |
147 cmd.range(0, 8); | |
148 final ForwardAnnotateInspector c2 = new ForwardAnnotateInspector(); | |
149 cmd.executeAnnotate(c2); | |
150 for (IntTuple t : c2.all.get(8)) { | |
151 System.out.printf("Block %d lines from revision %d (starts with line %d in the origin)\n", t.at(0), t.at(1), t.at(2)); | |
152 } | |
153 for (IntTuple t : c2.all.get(8)) { | |
154 System.out.printf("Block %d lines from revision %d (starts with line %d in the origin)\n", t.at(0), t.at(1), 1+t.at(2)); | |
155 } | |
156 c2.report(8, new Inspector() { | |
157 | |
158 public void next(LineInfo lineInfo) throws HgCallbackTargetException { | |
159 System.out.printf("%3d:%3d: %s", lineInfo.getChangesetIndex(), lineInfo.getLineNumber(), new String(lineInfo.getContent())); | |
160 } | |
161 }, ProgressSupport.Factory.get(null), CancelSupport.Factory.get(null)); | |
162 } | |
163 } |