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@542: package org.tmatesoft.hg.internal; 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@542: import org.tmatesoft.hg.core.Nodeid; tikhomirov@544: import org.tmatesoft.hg.internal.PatchGenerator.LineSequence; tikhomirov@542: import org.tmatesoft.hg.repo.HgDataFile; tikhomirov@542: import org.tmatesoft.hg.repo.HgInvalidStateException; tikhomirov@548: import org.tmatesoft.hg.repo.HgRepository; tikhomirov@542: import org.tmatesoft.hg.util.CancelledException; tikhomirov@542: tikhomirov@542: /** tikhomirov@542: * tikhomirov@542: * @author Artem Tikhomirov tikhomirov@542: * @author TMate Software Ltd. tikhomirov@542: */ tikhomirov@542: @Experimental(reason="work in progress") tikhomirov@542: public class AnnotateFacility { tikhomirov@548: tikhomirov@548: /** tikhomirov@548: * Annotate file revision, line by line. tikhomirov@548: */ tikhomirov@548: public void annotate(HgDataFile df, int changesetRevisionIndex, LineInspector insp) { tikhomirov@548: if (!df.exists()) { tikhomirov@548: return; tikhomirov@548: } tikhomirov@548: Nodeid fileRev = df.getRepo().getManifest().getFileRevision(changesetRevisionIndex, df.getPath()); tikhomirov@548: int fileRevIndex = df.getRevisionIndex(fileRev); tikhomirov@548: int[] fileRevParents = new int[2]; tikhomirov@548: FileAnnotation fa = new FileAnnotation(insp); tikhomirov@548: do { tikhomirov@548: // also covers changesetRevisionIndex == TIP, #implAnnotateChange doesn't tolerate constants tikhomirov@548: changesetRevisionIndex = df.getChangesetRevisionIndex(fileRevIndex); tikhomirov@548: df.parents(fileRevIndex, fileRevParents, null, null); tikhomirov@548: implAnnotateChange(df, changesetRevisionIndex, fileRevIndex, fileRevParents, fa); tikhomirov@548: fileRevIndex = fileRevParents[0]; tikhomirov@548: } while (fileRevIndex != NO_REVISION); tikhomirov@548: } tikhomirov@548: tikhomirov@544: /** tikhomirov@544: * Annotates changes of the file against its parent(s) tikhomirov@544: */ tikhomirov@546: public void annotateChange(HgDataFile df, int changesetRevisionIndex, BlockInspector insp) { tikhomirov@545: // TODO detect if file is text/binary (e.g. looking for chars < ' ' and not \t\r\n\f tikhomirov@545: Nodeid fileRev = df.getRepo().getManifest().getFileRevision(changesetRevisionIndex, df.getPath()); tikhomirov@542: int fileRevIndex = df.getRevisionIndex(fileRev); tikhomirov@542: int[] fileRevParents = new int[2]; tikhomirov@542: df.parents(fileRevIndex, fileRevParents, null, null); tikhomirov@548: if (changesetRevisionIndex == TIP) { tikhomirov@548: changesetRevisionIndex = df.getChangesetRevisionIndex(fileRevIndex); tikhomirov@548: } tikhomirov@548: implAnnotateChange(df, changesetRevisionIndex, fileRevIndex, fileRevParents, insp); tikhomirov@548: } tikhomirov@548: tikhomirov@548: private void implAnnotateChange(HgDataFile df, int csetRevIndex, int fileRevIndex, int[] fileParentRevs, BlockInspector insp) { tikhomirov@545: try { tikhomirov@548: if (fileParentRevs[0] != NO_REVISION && fileParentRevs[1] != NO_REVISION) { tikhomirov@545: // merge tikhomirov@548: } else if (fileParentRevs[0] == fileParentRevs[1]) { tikhomirov@545: // may be equal iff both are unset tikhomirov@548: assert fileParentRevs[0] == NO_REVISION; tikhomirov@545: // everything added tikhomirov@545: ByteArrayChannel c; tikhomirov@545: df.content(fileRevIndex, c = new ByteArrayChannel()); tikhomirov@548: BlameBlockInspector bbi = new BlameBlockInspector(insp, NO_REVISION, csetRevIndex); tikhomirov@545: LineSequence cls = LineSequence.newlines(c.toArray()); tikhomirov@545: bbi.begin(LineSequence.newlines(new byte[0]), cls); tikhomirov@545: bbi.match(0, cls.chunkCount()-1, 0); tikhomirov@545: bbi.end(); tikhomirov@545: } else { tikhomirov@548: int soleParent = fileParentRevs[0] == NO_REVISION ? fileParentRevs[1] : fileParentRevs[0]; tikhomirov@545: assert soleParent != NO_REVISION; tikhomirov@542: ByteArrayChannel c1, c2; tikhomirov@542: df.content(soleParent, c1 = new ByteArrayChannel()); tikhomirov@542: df.content(fileRevIndex, c2 = new ByteArrayChannel()); tikhomirov@542: int parentChangesetRevIndex = df.getChangesetRevisionIndex(soleParent); tikhomirov@544: PatchGenerator pg = new PatchGenerator(); tikhomirov@544: pg.init(LineSequence.newlines(c1.toArray()), LineSequence.newlines(c2.toArray())); tikhomirov@548: pg.findMatchingBlocks(new BlameBlockInspector(insp, parentChangesetRevIndex, csetRevIndex)); tikhomirov@542: } tikhomirov@545: } catch (CancelledException ex) { tikhomirov@545: // TODO likely it was bad idea to throw cancelled exception from content() tikhomirov@545: // deprecate and provide alternative? tikhomirov@545: HgInvalidStateException ise = new HgInvalidStateException("ByteArrayChannel never throws CancelledException"); tikhomirov@545: ise.initCause(ex); tikhomirov@545: throw ise; tikhomirov@542: } tikhomirov@542: } tikhomirov@542: 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@545: @Callback tikhomirov@546: public interface BlockInspectorEx extends BlockInspector { // XXX better name tikhomirov@545: // XXX perhaps, shall pass object instead of separate values for future extension? tikhomirov@545: void start(int originLineCount, int targetLineCount); tikhomirov@545: void done(); tikhomirov@545: } tikhomirov@545: tikhomirov@542: public interface Block { tikhomirov@545: int originChangesetIndex(); tikhomirov@545: int targetChangesetIndex(); tikhomirov@542: // boolean isMergeRevision(); tikhomirov@542: // int fileRevisionIndex(); tikhomirov@542: // int originFileRevisionIndex(); tikhomirov@542: // String[] lines(); tikhomirov@542: // byte[] data(); tikhomirov@542: } tikhomirov@542: tikhomirov@545: public interface EqualBlock extends Block { tikhomirov@545: int originStart(); tikhomirov@545: int targetStart(); tikhomirov@545: int length(); tikhomirov@545: } tikhomirov@545: tikhomirov@542: public interface AddBlock extends Block { tikhomirov@543: int insertedAt(); // line index in the old file tikhomirov@542: int firstAddedLine(); tikhomirov@542: int totalAddedLines(); tikhomirov@542: String[] addedLines(); tikhomirov@542: } tikhomirov@542: public interface DeleteBlock extends Block { tikhomirov@543: int removedAt(); // line index in the new file tikhomirov@542: int firstRemovedLine(); tikhomirov@542: int totalRemovedLines(); tikhomirov@542: String[] removedLines(); tikhomirov@542: } tikhomirov@542: public interface ChangeBlock extends AddBlock, DeleteBlock { tikhomirov@542: } tikhomirov@546: tikhomirov@546: @Callback tikhomirov@546: public interface LineInspector { tikhomirov@548: /** tikhomirov@548: * Not necessarily invoked sequentially by line numbers tikhomirov@548: */ tikhomirov@546: void line(int lineNumber, int changesetRevIndex, LineDescriptor ld); tikhomirov@546: } tikhomirov@546: tikhomirov@546: public interface LineDescriptor { tikhomirov@546: int totalLines(); tikhomirov@546: } tikhomirov@546: tikhomirov@546: tikhomirov@542: tikhomirov@544: static class BlameBlockInspector extends PatchGenerator.DeltaInspector { tikhomirov@546: private final BlockInspector insp; tikhomirov@545: private final int csetP1; tikhomirov@545: private final int csetTarget; tikhomirov@542: tikhomirov@546: public BlameBlockInspector(BlockInspector inspector, int parentCset1, int targetCset) { tikhomirov@542: assert inspector != null; tikhomirov@542: insp = inspector; tikhomirov@545: csetP1 = parentCset1; tikhomirov@545: csetTarget = targetCset; tikhomirov@545: } tikhomirov@545: tikhomirov@545: @Override tikhomirov@545: public void begin(LineSequence s1, LineSequence s2) { tikhomirov@545: super.begin(s1, s2); tikhomirov@546: if (insp instanceof BlockInspectorEx) { tikhomirov@546: ((BlockInspectorEx) insp).start(s1.chunkCount() - 1, s2.chunkCount() - 1); tikhomirov@545: } tikhomirov@545: } tikhomirov@545: tikhomirov@545: @Override tikhomirov@545: public void end() { tikhomirov@545: super.end(); tikhomirov@546: if(insp instanceof BlockInspectorEx) { tikhomirov@546: ((BlockInspectorEx) insp).done(); tikhomirov@545: } tikhomirov@542: } tikhomirov@542: tikhomirov@542: @Override tikhomirov@542: protected void changed(int s1From, int s1To, int s2From, int s2To) { tikhomirov@545: BlockImpl2 block = new BlockImpl2(seq1, seq2, s1From, s1To-s1From, s2From, s2To - s2From, s1From, s2From); tikhomirov@545: block.setOriginAndTarget(csetP1, csetTarget); tikhomirov@545: insp.changed(block); tikhomirov@542: } tikhomirov@542: tikhomirov@542: @Override tikhomirov@542: protected void added(int s1InsertPoint, int s2From, int s2To) { tikhomirov@545: BlockImpl2 block = new BlockImpl2(null, seq2, -1, -1, s2From, s2To - s2From, s1InsertPoint, -1); tikhomirov@545: block.setOriginAndTarget(csetP1, csetTarget); tikhomirov@545: insp.added(block); tikhomirov@542: } tikhomirov@542: tikhomirov@542: @Override tikhomirov@543: protected void deleted(int s2DeletePoint, int s1From, int s1To) { tikhomirov@545: BlockImpl2 block = new BlockImpl2(seq1, null, s1From, s1To - s1From, -1, -1, -1, s2DeletePoint); tikhomirov@545: block.setOriginAndTarget(csetP1, 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@545: BlockImpl1 block = new BlockImpl1(s1From, s2From, length); tikhomirov@545: block.setOriginAndTarget(csetP1, csetTarget); tikhomirov@545: insp.same(block); tikhomirov@545: } tikhomirov@545: } tikhomirov@545: tikhomirov@545: static class BlockImpl implements Block { tikhomirov@545: 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@545: static class BlockImpl1 extends BlockImpl implements EqualBlock { tikhomirov@545: private final int start1, start2; tikhomirov@542: private final int length; tikhomirov@542: tikhomirov@545: BlockImpl1(int blockStartSeq1, int blockStartSeq2, int blockLength) { tikhomirov@545: start1 = blockStartSeq1; tikhomirov@545: start2 = blockStartSeq2; tikhomirov@545: length = blockLength; 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@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@545: static class BlockImpl2 extends BlockImpl implements ChangeBlock { tikhomirov@542: tikhomirov@545: private final LineSequence oldSeq; tikhomirov@545: private final LineSequence newSeq; 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@542: tikhomirov@545: public BlockImpl2(LineSequence s1, LineSequence s2, int s1Start, int s1Len, int s2Start, int s2Len, int s1InsertPoint, int s2DeletePoint) { tikhomirov@542: oldSeq = s1; tikhomirov@542: newSeq = s2; 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@542: public String[] addedLines() { tikhomirov@542: return generateLines(totalAddedLines(), firstAddedLine()); 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@542: public String[] removedLines() { tikhomirov@542: return generateLines(totalRemovedLines(), firstRemovedLine()); tikhomirov@542: } tikhomirov@542: tikhomirov@542: private String[] generateLines(int count, int startFrom) { tikhomirov@542: String[] rv = new String[count]; tikhomirov@542: for (int i = 0; i < count; i++) { tikhomirov@543: rv[i] = String.format("LINE %d", startFrom + i+1); tikhomirov@542: } tikhomirov@542: return rv; 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@542: }