comparison src/org/tmatesoft/hg/internal/diff/ForwardAnnotateInspector.java @ 703:7839ff0bfd78

Refactor: move diff/blame related code to a separate package
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 14 Aug 2013 14:51:51 +0200
parents src/org/tmatesoft/hg/internal/ForwardAnnotateInspector.java@f1f095e42555
children 497e697636fc
comparison
equal deleted inserted replaced
702:992fa84e7885 703:7839ff0bfd78
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.diff;
18
19 import org.tmatesoft.hg.core.HgAnnotateCommand.Inspector;
20 import org.tmatesoft.hg.core.HgBlameInspector;
21 import org.tmatesoft.hg.core.HgCallbackTargetException;
22 import org.tmatesoft.hg.core.HgIterateDirection;
23 import org.tmatesoft.hg.internal.IntMap;
24 import org.tmatesoft.hg.internal.IntSliceSeq;
25 import org.tmatesoft.hg.internal.IntTuple;
26 import org.tmatesoft.hg.util.CancelSupport;
27 import org.tmatesoft.hg.util.CancelledException;
28 import org.tmatesoft.hg.util.ProgressSupport;
29
30 /**
31 * Annotate file history iterating from parents to children
32 *
33 * At the moment, doesn't handle start from any revision but 0
34 *
35 * (+) May report annotate for any revision (with actual file change) in the visited range.
36 *
37 * @see ReverseAnnotateInspector
38 * @author Artem Tikhomirov
39 * @author TMate Software Ltd.
40 */
41 public class ForwardAnnotateInspector implements HgBlameInspector, HgBlameInspector.RevisionDescriptor.Recipient {
42 final IntMap<IntSliceSeq> all = new IntMap<IntSliceSeq>(100);
43 // revision->map(lineNumber->lineContent)
44 private final IntMap<IntMap<byte[]>> lineContent = new IntMap<IntMap<byte[]>>(100);
45 private IntSliceSeq current;
46 private RevisionDescriptor revDescriptor;
47
48 /**
49 * @return desired order of iteration for diff
50 */
51 public HgIterateDirection iterateDirection() {
52 return HgIterateDirection.OldToNew;
53 }
54
55 public void report(int revision, Inspector insp, ProgressSupport progress, CancelSupport cancel) throws HgCallbackTargetException, CancelledException {
56 int totalLines = 0;
57 if (!all.containsKey(revision)) {
58 throw new IllegalArgumentException(String.format("Revision %d has not been visited", revision));
59 }
60 for (IntTuple t : all.get(revision)) {
61 totalLines += t.at(0);
62 }
63 progress.start(totalLines);
64 LineImpl li = new LineImpl();
65 int line = 1;
66 for (IntTuple t : all.get(revision)) {
67 IntMap<byte[]> revLines = lineContent.get(t.at(1));
68 for (int i = 0, x = t.at(0); i < x; i++) {
69 final int lineInRev = t.at(2) + i;
70 final byte[] lc = revLines.get(lineInRev);
71 li.init(line++, lineInRev+1, t.at(1), lc);
72 insp.next(li);
73 progress.worked(1);
74 cancel.checkCancelled();
75 }
76 }
77 progress.done();
78 }
79
80 public void start(RevisionDescriptor rd) throws HgCallbackTargetException {
81 all.put(rd.targetChangesetIndex(), current = new IntSliceSeq(3));
82 revDescriptor = rd;
83 }
84
85 public void done(RevisionDescriptor rd) throws HgCallbackTargetException {
86 revDescriptor = null;
87 }
88
89 public void same(EqualBlock block) throws HgCallbackTargetException {
90 copyBlock(block.originChangesetIndex(), block.originStart(), block.length());
91 }
92
93 public void added(AddBlock block) throws HgCallbackTargetException {
94 if (revDescriptor.isMerge() && block.originChangesetIndex() == revDescriptor.mergeChangesetIndex()) {
95 copyBlock(block.originChangesetIndex(), block.insertedAt(), block.totalAddedLines());
96 return;
97 }
98 BlockData addedLines = block.addedLines();
99 IntMap<byte[]> revLines = lineContent.get(block.targetChangesetIndex());
100 if (revLines == null) {
101 lineContent.put(block.targetChangesetIndex(), revLines = new IntMap<byte[]>(block.totalAddedLines()));
102 }
103 for (int i = 0; i < block.totalAddedLines(); i++) {
104 revLines.put(block.firstAddedLine() + i, addedLines.elementAt(i).asArray());
105 }
106 current.add(block.totalAddedLines(), block.targetChangesetIndex(), block.firstAddedLine());
107 }
108
109 public void changed(ChangeBlock block) throws HgCallbackTargetException {
110 added(block);
111 }
112
113 public void deleted(DeleteBlock block) throws HgCallbackTargetException {
114 }
115
116 private void copyBlock(int originChangesetIndex, int blockStart, int length) {
117 IntSliceSeq origin = all.get(originChangesetIndex);
118 assert origin != null; // shall visit parents before came to this child
119 int originPos = 0;
120 int targetBlockLen = length;
121 for (IntTuple t : origin) {
122 int originBlockLen = t.at(0);
123 int originBlockEnd = originPos + originBlockLen;
124 if (originBlockEnd > blockStart) {
125 // part of origin block from blockStart up to originBlockEnd, but not more
126 // than size of the block (when blockStart is out of block start, i.e. < originPos)
127 int originBlockOverlap = Math.min(originBlockLen, originBlockEnd - blockStart);
128 assert originBlockOverlap > 0;
129 // eat as much as there's left in the block being copied
130 int originBlockConsumed = Math.min(originBlockOverlap, targetBlockLen);
131 int originBlockLine = t.at(2);
132 if (originPos < blockStart) {
133 originBlockLine += originBlockLen-originBlockOverlap;
134 }
135 // copy fragment of original block;
136 current.add(originBlockConsumed, t.at(1), originBlockLine);
137 targetBlockLen -= originBlockConsumed;
138 if (targetBlockLen == 0) {
139 break;
140 }
141 }
142 originPos += originBlockLen;
143 }
144 }
145 }