comparison hg4j-cli/src/main/java/org/tmatesoft/hg/console/Cat.java @ 213:6ec4af642ba8 gradle

Project uses Gradle for build - actual changes
author Alexander Kitaev <kitaev@gmail.com>
date Tue, 10 May 2011 10:52:53 +0200
parents
children
comparison
equal deleted inserted replaced
212:edb2e2829352 213:6ec4af642ba8
1 /*
2 * Copyright (c) 2010-2011 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.console;
18
19 import static org.tmatesoft.hg.repo.HgRepository.TIP;
20
21 import java.io.IOException;
22 import java.io.OutputStream;
23 import java.nio.ByteBuffer;
24
25 import org.tmatesoft.hg.repo.HgDataFile;
26 import org.tmatesoft.hg.repo.HgRepository;
27 import org.tmatesoft.hg.util.ByteChannel;
28
29
30 /**
31 * @author Artem Tikhomirov
32 * @author TMate Software Ltd.
33 */
34 public class Cat {
35
36 public static void main(String[] args) throws Exception {
37 Options cmdLineOpts = Options.parse(args);
38 HgRepository hgRepo = cmdLineOpts.findRepository();
39 if (hgRepo.isInvalid()) {
40 System.err.printf("Can't find repository in: %s\n", hgRepo.getLocation());
41 return;
42 }
43 int rev = cmdLineOpts.getSingleInt(TIP, "-r", "--rev");
44 OutputStreamChannel out = new OutputStreamChannel(System.out);
45 for (String fname : cmdLineOpts.getList("")) {
46 System.out.println(fname);
47 HgDataFile fn = hgRepo.getFileNode(fname);
48 if (fn.exists()) {
49 fn.contentWithFilters(rev, out);
50 System.out.println();
51 } else {
52 System.out.printf("%s not found!\n", fname);
53 }
54 }
55 }
56
57 private static class OutputStreamChannel implements ByteChannel {
58
59 private final OutputStream stream;
60
61 public OutputStreamChannel(OutputStream out) {
62 stream = out;
63 }
64
65 public int write(ByteBuffer buffer) throws IOException {
66 int count = buffer.remaining();
67 while(buffer.hasRemaining()) {
68 stream.write(buffer.get());
69 }
70 return count;
71 }
72 }
73 }