kitaev@213: /* kitaev@213: * Copyright (c) 2010-2011 TMate Software Ltd kitaev@213: * kitaev@213: * This program is free software; you can redistribute it and/or modify kitaev@213: * it under the terms of the GNU General Public License as published by kitaev@213: * the Free Software Foundation; version 2 of the License. kitaev@213: * kitaev@213: * This program is distributed in the hope that it will be useful, kitaev@213: * but WITHOUT ANY WARRANTY; without even the implied warranty of kitaev@213: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the kitaev@213: * GNU General Public License for more details. kitaev@213: * kitaev@213: * For information on how to redistribute this software under kitaev@213: * the terms of a license other than GNU General Public License kitaev@213: * contact TMate Software at support@hg4j.com kitaev@213: */ kitaev@213: package org.tmatesoft.hg.console; kitaev@213: kitaev@213: import static org.tmatesoft.hg.repo.HgRepository.TIP; kitaev@213: kitaev@213: import java.io.IOException; kitaev@213: import java.io.OutputStream; kitaev@213: import java.nio.ByteBuffer; kitaev@213: kitaev@213: import org.tmatesoft.hg.repo.HgDataFile; kitaev@213: import org.tmatesoft.hg.repo.HgRepository; kitaev@213: import org.tmatesoft.hg.util.ByteChannel; kitaev@213: kitaev@213: kitaev@213: /** kitaev@213: * @author Artem Tikhomirov kitaev@213: * @author TMate Software Ltd. kitaev@213: */ kitaev@213: public class Cat { kitaev@213: kitaev@213: public static void main(String[] args) throws Exception { kitaev@213: Options cmdLineOpts = Options.parse(args); kitaev@213: HgRepository hgRepo = cmdLineOpts.findRepository(); kitaev@213: if (hgRepo.isInvalid()) { kitaev@213: System.err.printf("Can't find repository in: %s\n", hgRepo.getLocation()); kitaev@213: return; kitaev@213: } kitaev@213: int rev = cmdLineOpts.getSingleInt(TIP, "-r", "--rev"); kitaev@213: OutputStreamChannel out = new OutputStreamChannel(System.out); kitaev@213: for (String fname : cmdLineOpts.getList("")) { kitaev@213: System.out.println(fname); kitaev@213: HgDataFile fn = hgRepo.getFileNode(fname); kitaev@213: if (fn.exists()) { kitaev@213: fn.contentWithFilters(rev, out); kitaev@213: System.out.println(); kitaev@213: } else { kitaev@213: System.out.printf("%s not found!\n", fname); kitaev@213: } kitaev@213: } kitaev@213: } kitaev@213: kitaev@213: private static class OutputStreamChannel implements ByteChannel { kitaev@213: kitaev@213: private final OutputStream stream; kitaev@213: kitaev@213: public OutputStreamChannel(OutputStream out) { kitaev@213: stream = out; kitaev@213: } kitaev@213: kitaev@213: public int write(ByteBuffer buffer) throws IOException { kitaev@213: int count = buffer.remaining(); kitaev@213: while(buffer.hasRemaining()) { kitaev@213: stream.write(buffer.get()); kitaev@213: } kitaev@213: return count; kitaev@213: } kitaev@213: } kitaev@213: }