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@542: tikhomirov@542: import org.tmatesoft.hg.core.Nodeid; tikhomirov@542: import org.tmatesoft.hg.internal.PatchGenerator.ChunkSequence; tikhomirov@542: import org.tmatesoft.hg.repo.HgDataFile; tikhomirov@542: import org.tmatesoft.hg.repo.HgInvalidStateException; tikhomirov@542: 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@542: tikhomirov@542: public void annotate(HgDataFile df, int changestRevisionIndex, Inspector insp) { tikhomirov@542: Nodeid fileRev = df.getRepo().getManifest().getFileRevision(changestRevisionIndex, 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@542: if (fileRevParents[0] != NO_REVISION && fileRevParents[1] != NO_REVISION) { tikhomirov@542: // merge tikhomirov@542: } else if (fileRevParents[0] == fileRevParents[1]) { tikhomirov@542: // may be equal iff both are unset tikhomirov@542: assert fileRevParents[0] == NO_REVISION; tikhomirov@542: // everything added tikhomirov@542: insp.added(null); tikhomirov@542: } else { tikhomirov@542: int soleParent = fileRevParents[0] == NO_REVISION ? fileRevParents[1] : fileRevParents[0]; tikhomirov@542: assert soleParent != NO_REVISION; tikhomirov@542: try { 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@542: PatchGenerator pg = new PatchGenerator(); tikhomirov@542: pg.init(c1.toArray(), c2.toArray()); tikhomirov@542: pg.findMatchingBlocks(new BlameBlockInspector(insp)); tikhomirov@542: } catch (CancelledException ex) { tikhomirov@542: // TODO likely it was bad idea to throw cancelled exception from content() tikhomirov@542: // deprecate and provide alternative? tikhomirov@542: HgInvalidStateException ise = new HgInvalidStateException("ByteArrayChannel never throws CancelledException"); tikhomirov@542: ise.initCause(ex); tikhomirov@542: throw ise; tikhomirov@542: } tikhomirov@542: } tikhomirov@542: } tikhomirov@542: tikhomirov@542: @Callback tikhomirov@542: public interface Inspector { tikhomirov@542: void same(Block block); tikhomirov@542: void added(AddBlock block); tikhomirov@542: void changed(ChangeBlock block); tikhomirov@542: void deleted(DeleteBlock block); tikhomirov@542: } tikhomirov@542: tikhomirov@542: public interface Block { 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@542: public interface AddBlock extends Block { tikhomirov@542: int firstAddedLine(); tikhomirov@542: int totalAddedLines(); tikhomirov@542: String[] addedLines(); tikhomirov@542: } tikhomirov@542: public interface DeleteBlock extends Block { 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@542: tikhomirov@542: static class BlameBlockInspector extends PatchGenerator.DeltaInspector { tikhomirov@542: private final Inspector insp; tikhomirov@542: tikhomirov@542: public BlameBlockInspector(Inspector inspector) { tikhomirov@542: assert inspector != null; tikhomirov@542: insp = inspector; tikhomirov@542: } tikhomirov@542: tikhomirov@542: @Override tikhomirov@542: protected void changed(int s1From, int s1To, int s2From, int s2To) { tikhomirov@542: insp.changed(new BlockImpl2(seq1, seq2, s1From, s1To-s1From, s2From, s2To - s2From)); tikhomirov@542: } tikhomirov@542: tikhomirov@542: @Override tikhomirov@542: protected void added(int s1InsertPoint, int s2From, int s2To) { tikhomirov@542: insp.added(new BlockImpl2(null, seq2, -1, -1, s2From, s2To - s2From)); tikhomirov@542: } tikhomirov@542: tikhomirov@542: @Override tikhomirov@542: protected void deleted(int s1From, int s1To) { tikhomirov@542: insp.deleted(new BlockImpl2(seq1, null, s1From, s1To - s1From, -1, -1)); tikhomirov@542: } tikhomirov@542: tikhomirov@542: @Override tikhomirov@542: protected void unchanged(int s1From, int s2From, int length) { tikhomirov@542: insp.same(new BlockImpl(seq2, s2From, length)); tikhomirov@542: } tikhomirov@542: } tikhomirov@542: tikhomirov@542: static class BlockImpl implements Block { tikhomirov@542: private final ChunkSequence seq; tikhomirov@542: private final int start; tikhomirov@542: private final int length; tikhomirov@542: tikhomirov@542: BlockImpl() { tikhomirov@542: // FIXME delete this cons tikhomirov@542: seq = null; tikhomirov@542: start = length = -1; tikhomirov@542: } tikhomirov@542: tikhomirov@542: BlockImpl(ChunkSequence s, int blockStart, int blockLength) { tikhomirov@542: seq = s; tikhomirov@542: start = blockStart; tikhomirov@542: length = blockLength; tikhomirov@542: } tikhomirov@542: tikhomirov@542: } tikhomirov@542: tikhomirov@542: static class BlockImpl2 implements ChangeBlock { tikhomirov@542: tikhomirov@542: private final ChunkSequence oldSeq; tikhomirov@542: private final ChunkSequence 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@542: tikhomirov@542: public BlockImpl2(ChunkSequence s1, ChunkSequence s2, int s1Start, int s1Len, int s2Start, int s2Len) { 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@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@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@542: rv[i] = String.format("LINE %d", startFrom + i); tikhomirov@542: } tikhomirov@542: return rv; tikhomirov@542: } tikhomirov@542: } tikhomirov@542: }