Mercurial > hg4j
changeset 121:b1d6208fb517
Conditionally apply filters to file content
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Fri, 04 Feb 2011 03:37:09 +0100 |
parents | b19f0ac5ee62 |
children | e93101b97e4a |
files | src/org/tmatesoft/hg/core/CatCommand.java src/org/tmatesoft/hg/internal/KeywordFilter.java src/org/tmatesoft/hg/repo/HgDataFile.java test/org/tmatesoft/hg/test/TestByteChannel.java |
diffstat | 4 files changed, 10 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- a/src/org/tmatesoft/hg/core/CatCommand.java Fri Feb 04 03:23:47 2011 +0100 +++ b/src/org/tmatesoft/hg/core/CatCommand.java Fri Feb 04 03:37:09 2011 +0100 @@ -80,6 +80,6 @@ } else { revToExtract = localRevision; } - dataFile.content(revToExtract, sink); + dataFile.content(revToExtract, sink, true); } }
--- a/src/org/tmatesoft/hg/internal/KeywordFilter.java Fri Feb 04 03:23:47 2011 +0100 +++ b/src/org/tmatesoft/hg/internal/KeywordFilter.java Fri Feb 04 03:37:09 2011 +0100 @@ -115,7 +115,7 @@ // not to run into such situation throw new IllegalStateException("Try src buffer of a greater size"); } - rv = ByteBuffer.allocate(keywordStart - x); + rv = ByteBuffer.allocate(keywordStart - copyFrom); } // copy all from source till latest possible kw start copySlice(src, copyFrom, keywordStart, rv);
--- a/src/org/tmatesoft/hg/repo/HgDataFile.java Fri Feb 04 03:23:47 2011 +0100 +++ b/src/org/tmatesoft/hg/repo/HgDataFile.java Fri Feb 04 03:37:09 2011 +0100 @@ -25,6 +25,7 @@ import org.tmatesoft.hg.core.Nodeid; import org.tmatesoft.hg.core.Path; +import org.tmatesoft.hg.internal.FilterByteChannel; import org.tmatesoft.hg.internal.RevlogStream; import org.tmatesoft.hg.util.ByteChannel; @@ -73,15 +74,18 @@ return content(TIP); } - public void content(int revision, ByteChannel sink) throws /*TODO typed*/Exception { + /*XXX not sure applyFilters is the best way to do, perhaps, callers shall add filters themselves?*/ + public void content(int revision, ByteChannel sink, boolean applyFilters) throws /*TODO typed*/Exception { byte[] content = content(revision); ByteBuffer buf = ByteBuffer.allocate(512); int left = content.length; int offset = 0; + ByteChannel _sink = applyFilters ? new FilterByteChannel(sink, getRepo().getFiltersFromRepoToWorkingDir(getPath())) : sink; do { buf.put(content, offset, Math.min(left, buf.remaining())); buf.flip(); - int consumed = sink.write(buf); + // XXX I may not rely on returned number of bytes but track change in buf position instead. + int consumed = _sink.write(buf); buf.compact(); offset += consumed; left -= consumed;
--- a/test/org/tmatesoft/hg/test/TestByteChannel.java Fri Feb 04 03:23:47 2011 +0100 +++ b/test/org/tmatesoft/hg/test/TestByteChannel.java Fri Feb 04 03:37:09 2011 +0100 @@ -33,7 +33,7 @@ public static void main(String[] args) throws Exception { RepositoryFacade rf = new RepositoryFacade(); rf.init(); - HgDataFile file = rf.getRepository().getFileNode("TODO"); + HgDataFile file = rf.getRepository().getFileNode("src/org/tmatesoft/hg/internal/KeywordFilter.java"); for (int i = file.getRevisionCount() - 1; i >= 0; i--) { System.out.print("Content for revision:" + i); compareContent(file, i); @@ -45,7 +45,7 @@ private static void compareContent(HgDataFile file, int rev) throws Exception { byte[] oldAccess = file.content(rev); ByteArrayChannel ch = new ByteArrayChannel(); - file.content(rev, ch); + file.content(rev, ch, false); byte[] newAccess = ch.toArray(); Assert.assertArrayEquals(oldAccess, newAccess); // don't trust anyone (even JUnit)