tikhomirov@542: /* tikhomirov@542: * Copyright (c) 2013 TMate Software Ltd tikhomirov@542: * tikhomirov@542: * This program is free software; you can redistribute it and/or modify tikhomirov@542: * it under the terms of the GNU General Public License as published by tikhomirov@542: * the Free Software Foundation; version 2 of the License. tikhomirov@542: * tikhomirov@542: * This program is distributed in the hope that it will be useful, tikhomirov@542: * but WITHOUT ANY WARRANTY; without even the implied warranty of tikhomirov@542: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the tikhomirov@542: * GNU General Public License for more details. tikhomirov@542: * tikhomirov@542: * For information on how to redistribute this software under tikhomirov@542: * the terms of a license other than GNU General Public License tikhomirov@542: * contact TMate Software at support@hg4j.com tikhomirov@542: */ tikhomirov@556: package org.tmatesoft.hg.repo; tikhomirov@542: tikhomirov@542: import static org.tmatesoft.hg.repo.HgRepository.NO_REVISION; tikhomirov@548: import static org.tmatesoft.hg.repo.HgRepository.TIP; tikhomirov@542: tikhomirov@552: import java.util.BitSet; tikhomirov@552: import java.util.LinkedList; tikhomirov@552: import java.util.ListIterator; tikhomirov@552: tikhomirov@552: import org.tmatesoft.hg.core.HgIterateDirection; tikhomirov@542: import org.tmatesoft.hg.core.Nodeid; tikhomirov@556: import org.tmatesoft.hg.internal.ByteArrayChannel; tikhomirov@556: import org.tmatesoft.hg.internal.Callback; tikhomirov@556: import org.tmatesoft.hg.internal.DiffHelper; tikhomirov@556: import org.tmatesoft.hg.internal.Experimental; tikhomirov@556: import org.tmatesoft.hg.internal.IntMap; tikhomirov@556: import org.tmatesoft.hg.internal.IntVector; tikhomirov@551: import org.tmatesoft.hg.internal.DiffHelper.LineSequence; tikhomirov@554: import org.tmatesoft.hg.internal.DiffHelper.LineSequence.ByteChain; tikhomirov@558: import org.tmatesoft.hg.internal.RangeSeq; tikhomirov@556: import org.tmatesoft.hg.repo.HgBlameFacility.RevisionDescriptor.Recipient; tikhomirov@555: import org.tmatesoft.hg.util.Adaptable; tikhomirov@542: import org.tmatesoft.hg.util.CancelledException; tikhomirov@552: import org.tmatesoft.hg.util.Pair; tikhomirov@542: tikhomirov@542: /** tikhomirov@555: * Facility with diff/annotate functionality. tikhomirov@542: * tikhomirov@542: * @author Artem Tikhomirov tikhomirov@542: * @author TMate Software Ltd. tikhomirov@542: */ tikhomirov@556: @Experimental(reason="Unstable API") tikhomirov@556: public final class HgBlameFacility { tikhomirov@549: tikhomirov@549: /** tikhomirov@552: * mimic 'hg diff -r clogRevIndex1 -r clogRevIndex2' tikhomirov@549: */ tikhomirov@552: public void diff(HgDataFile df, int clogRevIndex1, int clogRevIndex2, BlockInspector insp) { tikhomirov@552: int fileRevIndex1 = fileRevIndex(df, clogRevIndex1); tikhomirov@552: int fileRevIndex2 = fileRevIndex(df, clogRevIndex2); tikhomirov@552: FileLinesCache fileInfoCache = new FileLinesCache(df, 5); tikhomirov@552: LineSequence c1 = fileInfoCache.lines(fileRevIndex1); tikhomirov@552: LineSequence c2 = fileInfoCache.lines(fileRevIndex2); tikhomirov@551: DiffHelper pg = new DiffHelper(); tikhomirov@549: pg.init(c1, c2); tikhomirov@556: pg.findMatchingBlocks(new BlameBlockInspector(fileRevIndex2, insp, clogRevIndex1, clogRevIndex2)); tikhomirov@552: } tikhomirov@552: tikhomirov@555: /** tikhomirov@555: * Walk file history up to revision at given changeset and report changes for each revision tikhomirov@555: */ tikhomirov@552: public void annotate(HgDataFile df, int changelogRevisionIndex, BlockInspector insp, HgIterateDirection iterateOrder) { tikhomirov@552: if (!df.exists()) { tikhomirov@552: return; tikhomirov@552: } tikhomirov@552: // Note, changelogRevisionIndex may be TIP, while #implAnnotateChange doesn't tolerate constants tikhomirov@552: // tikhomirov@552: // XXX df.indexWalk(0, fileRevIndex, ) might be more effective tikhomirov@552: int fileRevIndex = fileRevIndex(df, changelogRevisionIndex); tikhomirov@552: int[] fileRevParents = new int[2]; tikhomirov@552: IntVector fileParentRevs = new IntVector((fileRevIndex+1) * 2, 0); tikhomirov@552: fileParentRevs.add(NO_REVISION, NO_REVISION); tikhomirov@552: for (int i = 1; i <= fileRevIndex; i++) { tikhomirov@552: df.parents(i, fileRevParents, null, null); tikhomirov@552: fileParentRevs.add(fileRevParents[0], fileRevParents[1]); tikhomirov@552: } tikhomirov@561: // collect file revisions to visit, from newest to oldest: tikhomirov@561: // traverse parents, starting from the given file revision tikhomirov@561: // this ignores all file revision made in parallel to the one of interest tikhomirov@552: IntVector fileRevsToVisit = new IntVector(fileRevIndex + 1, 0); tikhomirov@552: LinkedList queue = new LinkedList(); tikhomirov@552: BitSet seen = new BitSet(fileRevIndex + 1); tikhomirov@552: queue.add(fileRevIndex); tikhomirov@552: do { tikhomirov@552: int x = queue.removeFirst(); tikhomirov@552: if (seen.get(x)) { tikhomirov@552: continue; tikhomirov@552: } tikhomirov@552: seen.set(x); tikhomirov@552: fileRevsToVisit.add(x); tikhomirov@552: int p1 = fileParentRevs.get(2*x); tikhomirov@552: int p2 = fileParentRevs.get(2*x + 1); tikhomirov@552: if (p1 != NO_REVISION) { tikhomirov@552: queue.addLast(p1); tikhomirov@552: } tikhomirov@552: if (p2 != NO_REVISION) { tikhomirov@552: queue.addLast(p2); tikhomirov@552: } tikhomirov@552: } while (!queue.isEmpty()); tikhomirov@552: FileLinesCache fileInfoCache = new FileLinesCache(df, 10); tikhomirov@561: // make sure no child is processed before we handled all (grand-)parents of the element tikhomirov@561: fileRevsToVisit.sort(false); tikhomirov@552: // fileRevsToVisit now { r10, r7, r6, r5, r0 } tikhomirov@552: // and we'll iterate it from behind, e.g. old to new unless reversed tikhomirov@552: if (iterateOrder == HgIterateDirection.NewToOld) { tikhomirov@552: fileRevsToVisit.reverse(); tikhomirov@552: } tikhomirov@552: for (int i = fileRevsToVisit.size() - 1; i >= 0; i--) { tikhomirov@552: int fri = fileRevsToVisit.get(i); tikhomirov@552: int clogRevIndex = df.getChangesetRevisionIndex(fri); tikhomirov@552: fileRevParents[0] = fileParentRevs.get(fri * 2); tikhomirov@552: fileRevParents[1] = fileParentRevs.get(fri * 2 + 1); tikhomirov@552: implAnnotateChange(fileInfoCache, clogRevIndex, fri, fileRevParents, insp); tikhomirov@552: } tikhomirov@549: } tikhomirov@548: tikhomirov@548: /** tikhomirov@555: * Annotates changes of the file against its parent(s). tikhomirov@555: * Unlike {@link #annotate(HgDataFile, int, BlockInspector, HgIterateDirection)}, doesn't tikhomirov@555: * walk file history, looks at the specified revision only. Handles both parents (if merge revision). tikhomirov@548: */ tikhomirov@555: public void annotateSingleRevision(HgDataFile df, int changelogRevisionIndex, BlockInspector insp) { tikhomirov@545: // TODO detect if file is text/binary (e.g. looking for chars < ' ' and not \t\r\n\f tikhomirov@552: int fileRevIndex = fileRevIndex(df, changelogRevisionIndex); tikhomirov@542: int[] fileRevParents = new int[2]; tikhomirov@542: df.parents(fileRevIndex, fileRevParents, null, null); tikhomirov@552: if (changelogRevisionIndex == TIP) { tikhomirov@552: changelogRevisionIndex = df.getChangesetRevisionIndex(fileRevIndex); tikhomirov@548: } tikhomirov@552: implAnnotateChange(new FileLinesCache(df, 5), changelogRevisionIndex, fileRevIndex, fileRevParents, insp); tikhomirov@548: } tikhomirov@548: tikhomirov@552: private void implAnnotateChange(FileLinesCache fl, int csetRevIndex, int fileRevIndex, int[] fileParentRevs, BlockInspector insp) { tikhomirov@552: final LineSequence fileRevLines = fl.lines(fileRevIndex); tikhomirov@549: if (fileParentRevs[0] != NO_REVISION && fileParentRevs[1] != NO_REVISION) { tikhomirov@552: LineSequence p1Lines = fl.lines(fileParentRevs[0]); tikhomirov@552: LineSequence p2Lines = fl.lines(fileParentRevs[1]); tikhomirov@552: int p1ClogIndex = fl.getChangesetRevisionIndex(fileParentRevs[0]); tikhomirov@552: int p2ClogIndex = fl.getChangesetRevisionIndex(fileParentRevs[1]); tikhomirov@551: DiffHelper pg = new DiffHelper(); tikhomirov@549: pg.init(p2Lines, fileRevLines); tikhomirov@549: EqualBlocksCollector p2MergeCommon = new EqualBlocksCollector(); tikhomirov@549: pg.findMatchingBlocks(p2MergeCommon); tikhomirov@549: // tikhomirov@549: pg.init(p1Lines); tikhomirov@556: BlameBlockInspector bbi = new BlameBlockInspector(fileRevIndex, insp, p1ClogIndex, csetRevIndex); tikhomirov@549: bbi.setMergeParent2(p2MergeCommon, p2ClogIndex); tikhomirov@549: pg.findMatchingBlocks(bbi); tikhomirov@549: } else if (fileParentRevs[0] == fileParentRevs[1]) { tikhomirov@549: // may be equal iff both are unset tikhomirov@549: assert fileParentRevs[0] == NO_REVISION; tikhomirov@549: // everything added tikhomirov@556: BlameBlockInspector bbi = new BlameBlockInspector(fileRevIndex, insp, NO_REVISION, csetRevIndex); tikhomirov@549: bbi.begin(LineSequence.newlines(new byte[0]), fileRevLines); tikhomirov@549: bbi.match(0, fileRevLines.chunkCount()-1, 0); tikhomirov@549: bbi.end(); tikhomirov@549: } else { tikhomirov@549: int soleParent = fileParentRevs[0] == NO_REVISION ? fileParentRevs[1] : fileParentRevs[0]; tikhomirov@549: assert soleParent != NO_REVISION; tikhomirov@552: LineSequence parentLines = fl.lines(soleParent); tikhomirov@549: tikhomirov@552: int parentChangesetRevIndex = fl.getChangesetRevisionIndex(soleParent); tikhomirov@551: DiffHelper pg = new DiffHelper(); tikhomirov@549: pg.init(parentLines, fileRevLines); tikhomirov@556: pg.findMatchingBlocks(new BlameBlockInspector(fileRevIndex, insp, parentChangesetRevIndex, csetRevIndex)); tikhomirov@549: } tikhomirov@549: } tikhomirov@549: tikhomirov@549: private static int fileRevIndex(HgDataFile df, int csetRevIndex) { tikhomirov@549: Nodeid fileRev = df.getRepo().getManifest().getFileRevision(csetRevIndex, df.getPath()); tikhomirov@549: return df.getRevisionIndex(fileRev); tikhomirov@549: } tikhomirov@549: tikhomirov@552: private static class FileLinesCache { tikhomirov@552: private final HgDataFile df; tikhomirov@552: private final LinkedList> lruCache; tikhomirov@552: private final int limit; tikhomirov@552: private IntMap fileToClogIndexMap = new IntMap(20); tikhomirov@552: tikhomirov@552: public FileLinesCache(HgDataFile file, int lruLimit) { tikhomirov@552: df = file; tikhomirov@552: limit = lruLimit; tikhomirov@552: lruCache = new LinkedList>(); tikhomirov@552: } tikhomirov@552: tikhomirov@552: public int getChangesetRevisionIndex(int fileRevIndex) { tikhomirov@552: Integer cached = fileToClogIndexMap.get(fileRevIndex); tikhomirov@552: if (cached == null) { tikhomirov@552: cached = df.getChangesetRevisionIndex(fileRevIndex); tikhomirov@552: fileToClogIndexMap.put(fileRevIndex, cached); tikhomirov@552: } tikhomirov@552: return cached.intValue(); tikhomirov@552: } tikhomirov@552: tikhomirov@552: public LineSequence lines(int fileRevIndex) { tikhomirov@552: Pair cached = checkCache(fileRevIndex); tikhomirov@552: if (cached != null) { tikhomirov@552: return cached.second(); tikhomirov@552: } tikhomirov@552: try { tikhomirov@552: ByteArrayChannel c; tikhomirov@552: df.content(fileRevIndex, c = new ByteArrayChannel()); tikhomirov@552: LineSequence rv = LineSequence.newlines(c.toArray()); tikhomirov@552: lruCache.addFirst(new Pair(fileRevIndex, rv)); tikhomirov@552: if (lruCache.size() > limit) { tikhomirov@552: lruCache.removeLast(); tikhomirov@552: } tikhomirov@552: return rv; tikhomirov@552: } catch (CancelledException ex) { tikhomirov@552: // TODO likely it was bad idea to throw cancelled exception from content() tikhomirov@552: // deprecate and provide alternative? tikhomirov@552: HgInvalidStateException ise = new HgInvalidStateException("ByteArrayChannel never throws CancelledException"); tikhomirov@552: ise.initCause(ex); tikhomirov@552: throw ise; tikhomirov@552: } tikhomirov@552: } tikhomirov@552: tikhomirov@552: private Pair checkCache(int fileRevIndex) { tikhomirov@552: Pair rv = null; tikhomirov@552: for (ListIterator> it = lruCache.listIterator(); it.hasNext(); ) { tikhomirov@552: Pair p = it.next(); tikhomirov@552: if (p.first() == fileRevIndex) { tikhomirov@552: rv = p; tikhomirov@552: it.remove(); tikhomirov@552: break; tikhomirov@552: } tikhomirov@552: } tikhomirov@552: if (rv != null) { tikhomirov@552: lruCache.addFirst(rv); tikhomirov@552: } tikhomirov@552: return rv; tikhomirov@542: } tikhomirov@542: } tikhomirov@542: tikhomirov@555: /** tikhomirov@555: * Client's sink for revision differences. tikhomirov@555: * tikhomirov@555: * When implemented, clients shall not expect new {@link Block blocks} instances in each call. tikhomirov@555: * tikhomirov@555: * In case more information about annotated revision is needed, inspector instances may supply tikhomirov@555: * {@link RevisionDescriptor.Recipient} through {@link Adaptable}. tikhomirov@555: */ tikhomirov@542: @Callback tikhomirov@546: public interface BlockInspector { tikhomirov@545: void same(EqualBlock block); tikhomirov@542: void added(AddBlock block); tikhomirov@542: void changed(ChangeBlock block); tikhomirov@542: void deleted(DeleteBlock block); tikhomirov@542: } tikhomirov@542: tikhomirov@554: /** tikhomirov@554: * Represents content of a block, either as a sequence of bytes or a tikhomirov@554: * sequence of smaller blocks (lines), if appropriate (according to usage context). tikhomirov@554: * tikhomirov@554: * This approach allows line-by-line access to content data along with complete byte sequence for the whole block, i.e. tikhomirov@554: *
tikhomirov@554: 	 *    BlockData bd = addBlock.addedLines()
tikhomirov@554: 	 *    // bd describes data from the addition completely.
tikhomirov@554: 	 *    // elements of the BlockData are lines
tikhomirov@554: 	 *    bd.elementCount() == addBlock.totalAddedLines();
tikhomirov@554: 	 *    // one cat obtain complete addition with
tikhomirov@554: 	 *    byte[] everythingAdded = bd.asArray();
tikhomirov@554: 	 *    // or iterate line by line
tikhomirov@554: 	 *    for (int i = 0; i < bd.elementCount(); i++) {
tikhomirov@554: 	 *    	 byte[] lineContent = bd.elementAt(i);
tikhomirov@554: 	 *       String line = new String(lineContent, fileEncodingCharset);
tikhomirov@554: 	 *    }
tikhomirov@554: 	 *    where bd.elementAt(0) is the line at index addBlock.firstAddedLine() 
tikhomirov@554: 	 * 
tikhomirov@554: * tikhomirov@554: * LineData or ChunkData? tikhomirov@554: */ tikhomirov@554: public interface BlockData { tikhomirov@554: BlockData elementAt(int index); tikhomirov@554: int elementCount(); tikhomirov@554: byte[] asArray(); tikhomirov@554: } tikhomirov@554: tikhomirov@555: /** tikhomirov@555: * {@link BlockInspector} may optionally request extra information about revisions tikhomirov@555: * being inspected, denoting itself as a {@link RevisionDescriptor.Recipient}. This class tikhomirov@555: * provides complete information about file revision under annotation now. tikhomirov@555: */ tikhomirov@555: public interface RevisionDescriptor { tikhomirov@555: /** tikhomirov@555: * @return complete source of the diff origin, never null tikhomirov@555: */ tikhomirov@555: BlockData origin(); tikhomirov@555: /** tikhomirov@555: * @return complete source of the diff target, never null tikhomirov@555: */ tikhomirov@555: BlockData target(); tikhomirov@555: /** tikhomirov@555: * @return changeset revision index of original file, or {@link HgRepository#NO_REVISION} if it's the very first revision tikhomirov@555: */ tikhomirov@555: int originChangesetIndex(); tikhomirov@555: /** tikhomirov@555: * @return changeset revision index of the target file tikhomirov@555: */ tikhomirov@555: int targetChangesetIndex(); tikhomirov@555: /** tikhomirov@555: * @return true if this revision is merge tikhomirov@555: */ tikhomirov@555: boolean isMerge(); tikhomirov@555: /** tikhomirov@555: * @return changeset revision index of the second, merged parent tikhomirov@555: */ tikhomirov@555: int mergeChangesetIndex(); tikhomirov@555: /** tikhomirov@556: * @return revision index of the change in target file's revlog tikhomirov@555: */ tikhomirov@555: int fileRevisionIndex(); tikhomirov@555: tikhomirov@555: /** tikhomirov@555: * Implement to indicate interest in {@link RevisionDescriptor}. tikhomirov@555: * tikhomirov@555: * Note, instance of {@link RevisionDescriptor} is the same for tikhomirov@555: * {@link #start(RevisionDescriptor)} and {@link #done(RevisionDescriptor)} tikhomirov@555: * methods, and not necessarily a new one (i.e. ==) for the next tikhomirov@555: * revision announced. tikhomirov@555: */ tikhomirov@555: @Callback tikhomirov@555: public interface Recipient { tikhomirov@555: /** tikhomirov@555: * Comes prior to any change {@link Block blocks} tikhomirov@555: */ tikhomirov@555: void start(RevisionDescriptor revisionDescription); tikhomirov@555: /** tikhomirov@555: * Comes after all change {@link Block blocks} were dispatched tikhomirov@555: */ tikhomirov@555: void done(RevisionDescriptor revisionDescription); tikhomirov@555: } tikhomirov@555: } tikhomirov@555: tikhomirov@556: /** tikhomirov@556: * Each change block comes from a single origin, blocks that are result of a merge tikhomirov@556: * have {@link #originChangesetIndex()} equal to {@link RevisionDescriptor#mergeChangesetIndex()}. tikhomirov@556: */ tikhomirov@542: public interface Block { tikhomirov@545: int originChangesetIndex(); tikhomirov@545: int targetChangesetIndex(); tikhomirov@542: } tikhomirov@542: tikhomirov@545: public interface EqualBlock extends Block { tikhomirov@545: int originStart(); tikhomirov@545: int targetStart(); tikhomirov@545: int length(); tikhomirov@554: BlockData content(); tikhomirov@545: } tikhomirov@545: tikhomirov@542: public interface AddBlock extends Block { tikhomirov@556: /** tikhomirov@556: * @return line index in the origin where this block is inserted tikhomirov@556: */ tikhomirov@556: int insertedAt(); tikhomirov@556: /** tikhomirov@556: * @return line index of the first added line in the target revision tikhomirov@556: */ tikhomirov@542: int firstAddedLine(); tikhomirov@556: /** tikhomirov@556: * @return number of added lines in this block tikhomirov@556: */ tikhomirov@542: int totalAddedLines(); tikhomirov@556: /** tikhomirov@556: * @return content of added lines tikhomirov@556: */ tikhomirov@554: BlockData addedLines(); tikhomirov@542: } tikhomirov@542: public interface DeleteBlock extends Block { tikhomirov@556: /** tikhomirov@556: * @return line index in the target revision were this deleted block would be tikhomirov@556: */ tikhomirov@556: int removedAt(); tikhomirov@556: /** tikhomirov@556: * @return line index of the first removed line in the original revision tikhomirov@556: */ tikhomirov@542: int firstRemovedLine(); tikhomirov@556: /** tikhomirov@556: * @return number of deleted lines in this block tikhomirov@556: */ tikhomirov@542: int totalRemovedLines(); tikhomirov@556: /** tikhomirov@556: * @return content of deleted lines tikhomirov@556: */ tikhomirov@554: BlockData removedLines(); tikhomirov@542: } tikhomirov@542: public interface ChangeBlock extends AddBlock, DeleteBlock { tikhomirov@542: } tikhomirov@546: tikhomirov@555: private static class BlameBlockInspector extends DiffHelper.DeltaInspector { tikhomirov@546: private final BlockInspector insp; tikhomirov@549: private final int csetOrigin; tikhomirov@545: private final int csetTarget; tikhomirov@549: private EqualBlocksCollector p2MergeCommon; tikhomirov@549: private int csetMergeParent; tikhomirov@549: private IntVector mergeRanges; tikhomirov@555: private final AnnotateRev annotatedRevision; tikhomirov@542: tikhomirov@556: public BlameBlockInspector(int fileRevIndex, BlockInspector inspector, int originCset, int targetCset) { tikhomirov@542: assert inspector != null; tikhomirov@542: insp = inspector; tikhomirov@555: annotatedRevision = new AnnotateRev(); tikhomirov@556: annotatedRevision.set(fileRevIndex); tikhomirov@549: csetOrigin = originCset; tikhomirov@545: csetTarget = targetCset; tikhomirov@545: } tikhomirov@545: tikhomirov@549: public void setMergeParent2(EqualBlocksCollector p2Merge, int parentCset2) { tikhomirov@549: p2MergeCommon = p2Merge; tikhomirov@549: csetMergeParent = parentCset2; tikhomirov@549: mergeRanges = new IntVector(3*10, 3*10); tikhomirov@549: } tikhomirov@549: tikhomirov@545: @Override tikhomirov@545: public void begin(LineSequence s1, LineSequence s2) { tikhomirov@545: super.begin(s1, s2); tikhomirov@555: ContentBlock originContent = new ContentBlock(s1); tikhomirov@555: ContentBlock targetContent = new ContentBlock(s2); tikhomirov@555: annotatedRevision.set(originContent, targetContent); tikhomirov@555: annotatedRevision.set(csetOrigin, csetTarget, p2MergeCommon != null ? csetMergeParent : NO_REVISION); tikhomirov@555: Recipient curious = Adaptable.Factory.getAdapter(insp, Recipient.class, null); tikhomirov@555: if (curious != null) { tikhomirov@555: curious.start(annotatedRevision); tikhomirov@545: } tikhomirov@545: } tikhomirov@545: tikhomirov@545: @Override tikhomirov@545: public void end() { tikhomirov@545: super.end(); tikhomirov@555: Recipient curious = Adaptable.Factory.getAdapter(insp, Recipient.class, null); tikhomirov@555: if (curious != null) { tikhomirov@555: curious.done(annotatedRevision); tikhomirov@545: } tikhomirov@555: p2MergeCommon = null; tikhomirov@542: } tikhomirov@542: tikhomirov@542: @Override tikhomirov@542: protected void changed(int s1From, int s1To, int s2From, int s2To) { tikhomirov@549: if (p2MergeCommon != null) { tikhomirov@549: mergeRanges.clear(); tikhomirov@549: p2MergeCommon.combineAndMarkRangesWithTarget(s2From, s2To - s2From, csetOrigin, csetMergeParent, mergeRanges); tikhomirov@549: tikhomirov@549: /* tikhomirov@557: * Usecases, how it USED TO BE initially: tikhomirov@549: * 3 lines changed to 10 lines. range of 10 lines breaks down to 2 from p2, 3 from p1, and 5 from p2. tikhomirov@549: * We report: 2 lines changed to 2(p2), then 1 line changed with 3(p1) and 5 lines added from p2. tikhomirov@549: * tikhomirov@549: * 10 lines changed to 3 lines, range of 3 lines breaks down to 2 line from p1 and 1 line from p2. tikhomirov@557: * We report: 2 lines changed to 2(p1) and 8 lines changed to 1(p2) tikhomirov@557: * tikhomirov@557: * NOW, lines from p2 are always reported as pure add (since we need their insertion point to be in p2, not in p1) tikhomirov@557: * and we try to consume p1 changes as soon as we see first p1's range tikhomirov@549: */ tikhomirov@549: int s1TotalLines = s1To - s1From, s1ConsumedLines = 0, s1Start = s1From; tikhomirov@549: tikhomirov@549: for (int i = 0; i < mergeRanges.size(); i += 3) { tikhomirov@549: final int rangeOrigin = mergeRanges.get(i); tikhomirov@549: final int rangeStart = mergeRanges.get(i+1); tikhomirov@549: final int rangeLen = mergeRanges.get(i+2); tikhomirov@549: final boolean lastRange = i+3 >= mergeRanges.size(); tikhomirov@549: final int s1LinesLeft = s1TotalLines - s1ConsumedLines; tikhomirov@557: // how many lines we may report as changed (don't use more than in range unless it's the very last range) tikhomirov@549: final int s1LinesToBorrow = lastRange ? s1LinesLeft : Math.min(s1LinesLeft, rangeLen); tikhomirov@557: if (rangeOrigin != csetMergeParent && s1LinesToBorrow > 0) { tikhomirov@555: ChangeBlockImpl block = getChangeBlock(s1Start, s1LinesToBorrow, rangeStart, rangeLen); tikhomirov@549: block.setOriginAndTarget(rangeOrigin, csetTarget); tikhomirov@549: insp.changed(block); tikhomirov@549: s1ConsumedLines += s1LinesToBorrow; tikhomirov@549: s1Start += s1LinesToBorrow; tikhomirov@549: } else { tikhomirov@557: int blockInsPoint = rangeOrigin != csetMergeParent ? s1Start : p2MergeCommon.reverseMapLine(rangeStart); tikhomirov@557: ChangeBlockImpl block = getAddBlock(rangeStart, rangeLen, blockInsPoint); tikhomirov@549: block.setOriginAndTarget(rangeOrigin, csetTarget); tikhomirov@549: insp.added(block); tikhomirov@549: } tikhomirov@549: } tikhomirov@549: if (s1ConsumedLines != s1TotalLines) { tikhomirov@557: assert s1ConsumedLines < s1TotalLines : String.format("Expected to process %d lines, but actually was %d", s1TotalLines, s1ConsumedLines); tikhomirov@557: // either there were no ranges from p1, whole s2From..s2To range came from p2, shall report as deleted tikhomirov@557: // or the ranges found were not enough to consume whole s2From..s2To tikhomirov@557: // The "deletion point" is shifted to the end of last csetOrigin->csetTarget change tikhomirov@557: int s2DeletePoint = s2From + s1ConsumedLines; tikhomirov@557: ChangeBlockImpl block = new ChangeBlockImpl(annotatedRevision.origin, null, s1Start, s1To - s1Start, -1, -1, -1, s2DeletePoint); tikhomirov@557: block.setOriginAndTarget(csetOrigin, csetTarget); tikhomirov@557: insp.deleted(block); tikhomirov@549: } tikhomirov@549: } else { tikhomirov@556: ChangeBlockImpl block = getChangeBlock(s1From, s1To - s1From, s2From, s2To - s2From); tikhomirov@549: block.setOriginAndTarget(csetOrigin, csetTarget); tikhomirov@549: insp.changed(block); tikhomirov@549: } tikhomirov@542: } tikhomirov@542: tikhomirov@542: @Override tikhomirov@542: protected void added(int s1InsertPoint, int s2From, int s2To) { tikhomirov@549: if (p2MergeCommon != null) { tikhomirov@549: mergeRanges.clear(); tikhomirov@549: p2MergeCommon.combineAndMarkRangesWithTarget(s2From, s2To - s2From, csetOrigin, csetMergeParent, mergeRanges); tikhomirov@549: int insPoint = s1InsertPoint; // track changes to insertion point tikhomirov@549: for (int i = 0; i < mergeRanges.size(); i += 3) { tikhomirov@549: int rangeOrigin = mergeRanges.get(i); tikhomirov@549: int rangeStart = mergeRanges.get(i+1); tikhomirov@549: int rangeLen = mergeRanges.get(i+2); tikhomirov@554: ChangeBlockImpl block = getAddBlock(rangeStart, rangeLen, insPoint); tikhomirov@549: block.setOriginAndTarget(rangeOrigin, csetTarget); tikhomirov@549: insp.added(block); tikhomirov@549: // indicate insPoint moved down number of lines we just reported tikhomirov@549: insPoint += rangeLen; tikhomirov@549: } tikhomirov@549: } else { tikhomirov@554: ChangeBlockImpl block = getAddBlock(s2From, s2To - s2From, s1InsertPoint); tikhomirov@549: block.setOriginAndTarget(csetOrigin, csetTarget); tikhomirov@549: insp.added(block); tikhomirov@549: } tikhomirov@542: } tikhomirov@542: tikhomirov@542: @Override tikhomirov@543: protected void deleted(int s2DeletePoint, int s1From, int s1To) { tikhomirov@555: ChangeBlockImpl block = new ChangeBlockImpl(annotatedRevision.origin, null, s1From, s1To - s1From, -1, -1, -1, s2DeletePoint); tikhomirov@549: block.setOriginAndTarget(csetOrigin, csetTarget); tikhomirov@545: insp.deleted(block); tikhomirov@542: } tikhomirov@542: tikhomirov@542: @Override tikhomirov@542: protected void unchanged(int s1From, int s2From, int length) { tikhomirov@555: EqualBlockImpl block = new EqualBlockImpl(s1From, s2From, length, annotatedRevision.target); tikhomirov@549: block.setOriginAndTarget(csetOrigin, csetTarget); tikhomirov@545: insp.same(block); tikhomirov@545: } tikhomirov@549: tikhomirov@554: private ChangeBlockImpl getAddBlock(int start, int len, int insPoint) { tikhomirov@555: return new ChangeBlockImpl(null, annotatedRevision.target, -1, -1, start, len, insPoint, -1); tikhomirov@555: } tikhomirov@555: tikhomirov@556: private ChangeBlockImpl getChangeBlock(int start1, int len1, int start2, int len2) { tikhomirov@556: return new ChangeBlockImpl(annotatedRevision.origin, annotatedRevision.target, start1, len1, start2, len2, start1, start2); tikhomirov@549: } tikhomirov@545: } tikhomirov@545: tikhomirov@555: private static class BlockImpl implements Block { tikhomirov@545: private int originCset; tikhomirov@545: private int targetCset; tikhomirov@545: tikhomirov@545: void setOriginAndTarget(int originChangesetIndex, int targetChangesetIndex) { tikhomirov@545: // XXX perhaps, shall be part of Inspector API, rather than Block's tikhomirov@545: // as they don't change between blocks (although the moment about merged revisions) tikhomirov@545: // is not yet clear to me tikhomirov@545: originCset = originChangesetIndex; tikhomirov@545: targetCset = targetChangesetIndex; tikhomirov@545: } tikhomirov@545: tikhomirov@545: public int originChangesetIndex() { tikhomirov@545: return originCset; tikhomirov@545: } tikhomirov@545: tikhomirov@545: public int targetChangesetIndex() { tikhomirov@545: return targetCset; tikhomirov@542: } tikhomirov@542: } tikhomirov@542: tikhomirov@555: private static class EqualBlockImpl extends BlockImpl implements EqualBlock { tikhomirov@545: private final int start1, start2; tikhomirov@542: private final int length; tikhomirov@554: private final ContentBlock fullContent; tikhomirov@554: private FilterBlock myContent; tikhomirov@542: tikhomirov@554: EqualBlockImpl(int blockStartSeq1, int blockStartSeq2, int blockLength, ContentBlock targetContent) { tikhomirov@545: start1 = blockStartSeq1; tikhomirov@545: start2 = blockStartSeq2; tikhomirov@545: length = blockLength; tikhomirov@554: fullContent = targetContent; tikhomirov@542: } tikhomirov@542: tikhomirov@545: public int originStart() { tikhomirov@545: return start1; tikhomirov@545: } tikhomirov@545: tikhomirov@545: public int targetStart() { tikhomirov@545: return start2; tikhomirov@545: } tikhomirov@545: tikhomirov@545: public int length() { tikhomirov@545: return length; tikhomirov@542: } tikhomirov@542: tikhomirov@554: public BlockData content() { tikhomirov@554: if (myContent == null) { tikhomirov@554: myContent = new FilterBlock(fullContent, start2, length); tikhomirov@554: } tikhomirov@554: return myContent; tikhomirov@554: } tikhomirov@554: tikhomirov@545: @Override tikhomirov@545: public String toString() { tikhomirov@545: return String.format("@@ [%d..%d) == [%d..%d) @@", start1, start1+length, start2, start2+length); tikhomirov@545: } tikhomirov@542: } tikhomirov@542: tikhomirov@555: private static class ChangeBlockImpl extends BlockImpl implements ChangeBlock { tikhomirov@554: private final ContentBlock oldContent; tikhomirov@554: private final ContentBlock newContent; tikhomirov@542: private final int s1Start; tikhomirov@542: private final int s1Len; tikhomirov@542: private final int s2Start; tikhomirov@542: private final int s2Len; tikhomirov@543: private final int s1InsertPoint; tikhomirov@543: private final int s2DeletePoint; tikhomirov@554: private FilterBlock addedBlock, removedBlock; tikhomirov@542: tikhomirov@554: public ChangeBlockImpl(ContentBlock c1, ContentBlock c2, int s1Start, int s1Len, int s2Start, int s2Len, int s1InsertPoint, int s2DeletePoint) { tikhomirov@554: oldContent = c1; tikhomirov@554: newContent = c2; tikhomirov@542: this.s1Start = s1Start; tikhomirov@542: this.s1Len = s1Len; tikhomirov@542: this.s2Start = s2Start; tikhomirov@542: this.s2Len = s2Len; tikhomirov@543: this.s1InsertPoint = s1InsertPoint; tikhomirov@543: this.s2DeletePoint = s2DeletePoint; tikhomirov@543: } tikhomirov@543: tikhomirov@543: public int insertedAt() { tikhomirov@543: return s1InsertPoint; tikhomirov@542: } tikhomirov@542: tikhomirov@542: public int firstAddedLine() { tikhomirov@542: return s2Start; tikhomirov@542: } tikhomirov@542: tikhomirov@542: public int totalAddedLines() { tikhomirov@542: return s2Len; tikhomirov@542: } tikhomirov@542: tikhomirov@554: public BlockData addedLines() { tikhomirov@554: if (addedBlock == null) { tikhomirov@554: addedBlock = new FilterBlock(newContent, firstAddedLine(), totalAddedLines()); tikhomirov@554: } tikhomirov@554: return addedBlock; tikhomirov@542: } tikhomirov@543: tikhomirov@543: public int removedAt() { tikhomirov@543: return s2DeletePoint; tikhomirov@543: } tikhomirov@542: tikhomirov@542: public int firstRemovedLine() { tikhomirov@542: return s1Start; tikhomirov@542: } tikhomirov@542: tikhomirov@542: public int totalRemovedLines() { tikhomirov@542: return s1Len; tikhomirov@542: } tikhomirov@542: tikhomirov@554: public BlockData removedLines() { tikhomirov@554: if (removedBlock == null) { tikhomirov@554: removedBlock = new FilterBlock(oldContent, firstRemovedLine(), totalRemovedLines()); tikhomirov@542: } tikhomirov@554: return removedBlock; tikhomirov@542: } tikhomirov@545: tikhomirov@545: @Override tikhomirov@545: public String toString() { tikhomirov@545: if (s2DeletePoint == -1) { tikhomirov@545: return String.format("@@ -%d,0 +%d,%d @@", insertedAt(), firstAddedLine(), totalAddedLines()); tikhomirov@545: } else if (s1InsertPoint == -1) { tikhomirov@545: // delete only tikhomirov@545: return String.format("@@ -%d,%d +%d,0 @@", firstRemovedLine(), totalRemovedLines(), removedAt()); tikhomirov@545: } tikhomirov@545: return String.format("@@ -%d,%d +%d,%d @@", firstRemovedLine(), totalRemovedLines(), firstAddedLine(), totalAddedLines()); tikhomirov@545: } tikhomirov@542: } tikhomirov@554: tikhomirov@554: private static class SingleLine implements BlockData { tikhomirov@554: private final ByteChain line; tikhomirov@554: tikhomirov@554: public SingleLine(ByteChain lineContent) { tikhomirov@554: line = lineContent; tikhomirov@554: } tikhomirov@554: tikhomirov@554: public BlockData elementAt(int index) { tikhomirov@554: assert false; tikhomirov@554: return null; tikhomirov@554: } tikhomirov@554: tikhomirov@554: public int elementCount() { tikhomirov@554: return 0; tikhomirov@554: } tikhomirov@554: tikhomirov@554: public byte[] asArray() { tikhomirov@554: return line.data(); tikhomirov@554: } tikhomirov@554: } tikhomirov@554: tikhomirov@554: private static class ContentBlock implements BlockData { tikhomirov@554: private final LineSequence seq; tikhomirov@554: tikhomirov@554: public ContentBlock(LineSequence sequence) { tikhomirov@554: seq = sequence; tikhomirov@554: } tikhomirov@554: tikhomirov@554: public BlockData elementAt(int index) { tikhomirov@554: return new SingleLine(seq.chunk(index)); tikhomirov@554: } tikhomirov@554: tikhomirov@554: public int elementCount() { tikhomirov@554: return seq.chunkCount() - 1; tikhomirov@554: } tikhomirov@554: tikhomirov@554: public byte[] asArray() { tikhomirov@554: return seq.data(0, seq.chunkCount() - 1); tikhomirov@554: } tikhomirov@554: } tikhomirov@554: tikhomirov@554: private static class FilterBlock implements BlockData { tikhomirov@554: private final ContentBlock contentBlock; tikhomirov@554: private final int from; tikhomirov@554: private final int length; tikhomirov@554: tikhomirov@554: public FilterBlock(ContentBlock bd, int startFrom, int len) { tikhomirov@554: assert bd != null; tikhomirov@554: assert startFrom + len < bd.seq.chunkCount(); // there's one extra chunk in the end, so strict less is ok tikhomirov@554: contentBlock = bd; tikhomirov@554: from = startFrom; tikhomirov@554: length = len; tikhomirov@554: } tikhomirov@554: tikhomirov@554: public BlockData elementAt(int index) { tikhomirov@554: if (index < 0 || index >= length) { tikhomirov@554: throw new IllegalArgumentException(String.format("Expected value from [0..%d), got %d", length, index)); tikhomirov@554: } tikhomirov@554: return contentBlock.elementAt(from + index); tikhomirov@554: } tikhomirov@554: tikhomirov@554: public int elementCount() { tikhomirov@554: return length; tikhomirov@554: } tikhomirov@554: tikhomirov@554: public byte[] asArray() { tikhomirov@554: return contentBlock.seq.data(from, from + length); tikhomirov@554: } tikhomirov@554: } tikhomirov@554: tikhomirov@549: tikhomirov@558: private static class EqualBlocksCollector implements DiffHelper.MatchInspector { tikhomirov@558: private final RangeSeq matches = new RangeSeq(); tikhomirov@549: tikhomirov@549: public void begin(LineSequence s1, LineSequence s2) { tikhomirov@549: } tikhomirov@549: tikhomirov@549: public void match(int startSeq1, int startSeq2, int matchLength) { tikhomirov@558: matches.add(startSeq1, startSeq2, matchLength); tikhomirov@549: } tikhomirov@549: tikhomirov@549: public void end() { tikhomirov@549: } tikhomirov@558: tikhomirov@558: public int reverseMapLine(int ln) { tikhomirov@558: return matches.reverseMapLine(ln); tikhomirov@549: } tikhomirov@558: tikhomirov@549: public void intersectWithTarget(int start, int length, IntVector result) { tikhomirov@549: int s = start; tikhomirov@549: for (int l = start, x = start + length; l < x; l++) { tikhomirov@558: if (!matches.includesTargetLine(l)) { tikhomirov@549: if (l - s > 0) { tikhomirov@549: result.add(s); tikhomirov@549: result.add(l - s); tikhomirov@549: } tikhomirov@549: s = l+1; tikhomirov@549: } tikhomirov@549: } tikhomirov@549: if (s < start+length) { tikhomirov@549: result.add(s); tikhomirov@549: result.add((start + length) - s); tikhomirov@549: } tikhomirov@549: } tikhomirov@549: tikhomirov@549: /* tikhomirov@549: * intersects [start..start+length) with ranges of target lines, and based on the intersection tikhomirov@549: * breaks initial range into smaller ranges and records them into result, with marker to indicate tikhomirov@549: * whether the range is from initial range (markerSource) or is a result of the intersection with target tikhomirov@549: * (markerTarget) tikhomirov@549: */ tikhomirov@549: public void combineAndMarkRangesWithTarget(int start, int length, int markerSource, int markerTarget, IntVector result) { tikhomirov@549: int sourceStart = start, targetStart = start, sourceEnd = start + length; tikhomirov@549: for (int l = sourceStart; l < sourceEnd; l++) { tikhomirov@558: if (matches.includesTargetLine(l)) { tikhomirov@549: // l is from target tikhomirov@549: if (sourceStart < l) { tikhomirov@549: // few lines from source range were not in the target, report them tikhomirov@549: result.add(markerSource); tikhomirov@549: result.add(sourceStart); tikhomirov@549: result.add(l - sourceStart); tikhomirov@549: } tikhomirov@549: // indicate the earliest line from source range to use tikhomirov@549: sourceStart = l + 1; tikhomirov@549: } else { tikhomirov@549: // l is not in target tikhomirov@549: if (targetStart < l) { tikhomirov@549: // report lines from target range tikhomirov@549: result.add(markerTarget); tikhomirov@549: result.add(targetStart); tikhomirov@549: result.add(l - targetStart); tikhomirov@549: } tikhomirov@549: // next line *may* be from target tikhomirov@549: targetStart = l + 1; tikhomirov@549: } tikhomirov@549: } tikhomirov@549: // if source range end with line from target, sourceStart would be == sourceEnd, and we need to add range with markerTarget tikhomirov@549: // if source range doesn't end with target line, targetStart == sourceEnd, while sourceStart < sourceEnd tikhomirov@549: if (sourceStart < sourceEnd) { tikhomirov@549: assert targetStart == sourceEnd; tikhomirov@549: // something left from the source range tikhomirov@549: result.add(markerSource); tikhomirov@549: result.add(sourceStart); tikhomirov@549: result.add(sourceEnd - sourceStart); tikhomirov@549: } else if (targetStart < sourceEnd) { tikhomirov@549: assert sourceStart == sourceEnd; tikhomirov@549: result.add(markerTarget); tikhomirov@549: result.add(targetStart); tikhomirov@549: result.add(sourceEnd - targetStart); tikhomirov@549: } tikhomirov@549: } tikhomirov@549: } tikhomirov@549: tikhomirov@555: private static class AnnotateRev implements RevisionDescriptor { tikhomirov@555: public ContentBlock origin, target; tikhomirov@555: public int originCset, targetCset, mergeCset, fileRevIndex; tikhomirov@555: tikhomirov@556: public void set(int fileRev) { tikhomirov@556: fileRevIndex = fileRev; tikhomirov@556: } tikhomirov@555: public void set(ContentBlock o, ContentBlock t) { tikhomirov@555: origin = o; tikhomirov@555: target = t; tikhomirov@555: } tikhomirov@555: public void set(int o, int t, int m) { tikhomirov@555: originCset = o; tikhomirov@555: targetCset = t; tikhomirov@555: mergeCset = m; tikhomirov@555: } tikhomirov@555: tikhomirov@555: public BlockData origin() { tikhomirov@555: return origin; tikhomirov@555: } tikhomirov@555: tikhomirov@555: public BlockData target() { tikhomirov@555: return target; tikhomirov@555: } tikhomirov@555: tikhomirov@555: public int originChangesetIndex() { tikhomirov@555: return originCset; tikhomirov@555: } tikhomirov@555: tikhomirov@555: public int targetChangesetIndex() { tikhomirov@555: return targetCset; tikhomirov@555: } tikhomirov@555: tikhomirov@555: public boolean isMerge() { tikhomirov@555: return mergeCset != NO_REVISION; tikhomirov@555: } tikhomirov@555: tikhomirov@555: public int mergeChangesetIndex() { tikhomirov@555: return mergeCset; tikhomirov@555: } tikhomirov@555: tikhomirov@555: public int fileRevisionIndex() { tikhomirov@555: return fileRevIndex; tikhomirov@555: } tikhomirov@557: @Override tikhomirov@557: public String toString() { tikhomirov@557: if (isMerge()) { tikhomirov@557: return String.format("[%d,%d->%d]", originCset, mergeCset, targetCset); tikhomirov@557: } tikhomirov@557: return String.format("[%d->%d]", originCset, targetCset); tikhomirov@557: } tikhomirov@555: } tikhomirov@555: tikhomirov@549: public static void main(String[] args) { tikhomirov@549: EqualBlocksCollector bc = new EqualBlocksCollector(); tikhomirov@549: bc.match(-1, 5, 3); tikhomirov@549: bc.match(-1, 10, 2); tikhomirov@549: bc.match(-1, 15, 3); tikhomirov@549: bc.match(-1, 20, 3); tikhomirov@549: IntVector r = new IntVector(); tikhomirov@549: bc.intersectWithTarget(7, 10, r); tikhomirov@549: for (int i = 0; i < r.size(); i+=2) { tikhomirov@549: System.out.printf("[%d..%d) ", r.get(i), r.get(i) + r.get(i+1)); tikhomirov@549: } tikhomirov@549: System.out.println(); tikhomirov@549: r.clear(); tikhomirov@549: bc.combineAndMarkRangesWithTarget(0, 16, 508, 514, r); tikhomirov@549: for (int i = 0; i < r.size(); i+=3) { tikhomirov@549: System.out.printf("%d:[%d..%d) ", r.get(i), r.get(i+1), r.get(i+1) + r.get(i+2)); tikhomirov@549: } tikhomirov@549: } tikhomirov@542: }