comparison src/org/tmatesoft/hg/core/HgCatCommand.java @ 131:aa1629f36482

Renamed .core classes to start with Hg prefix
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 16 Feb 2011 20:47:56 +0100
parents src/org/tmatesoft/hg/core/CatCommand.java@7567f4a42fe5
children 4a948ec83980
comparison
equal deleted inserted replaced
130:7567f4a42fe5 131:aa1629f36482
1 /*
2 * Copyright (c) 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.core;
18
19 import static org.tmatesoft.hg.repo.HgRepository.BAD_REVISION;
20 import static org.tmatesoft.hg.repo.HgRepository.TIP;
21
22 import java.io.FileNotFoundException;
23
24 import org.tmatesoft.hg.repo.HgDataFile;
25 import org.tmatesoft.hg.repo.HgRepository;
26 import org.tmatesoft.hg.util.ByteChannel;
27
28 /**
29 * Command to obtain content of a file, 'hg cat' counterpart.
30 *
31 * @author Artem Tikhomirov
32 * @author TMate Software Ltd.
33 */
34 public class HgCatCommand {
35
36 private final HgRepository repo;
37 private Path file;
38 private int localRevision = TIP;
39 private Nodeid revision;
40
41 public HgCatCommand(HgRepository hgRepo) {
42 repo = hgRepo;
43 }
44
45 public HgCatCommand file(Path fname) {
46 file = fname;
47 return this;
48 }
49
50 // rev can't be WORKING_COPY (if allowed, need to implement in #execute())
51 public HgCatCommand revision(int rev) {
52 localRevision = rev;
53 revision = null;
54 return this;
55 }
56
57 public HgCatCommand revision(Nodeid nodeid) {
58 revision = nodeid;
59 localRevision = BAD_REVISION;
60 return this;
61 }
62
63 public void execute(ByteChannel sink) throws Exception /*TODO own exception type*/ {
64 if (localRevision == BAD_REVISION && revision == null) {
65 throw new IllegalArgumentException("Either local file revision number or nodeid shall be specified");
66 }
67 if (file == null) {
68 throw new IllegalArgumentException("Name of the file is missing");
69 }
70 if (sink == null) {
71 throw new IllegalArgumentException();
72 }
73 HgDataFile dataFile = repo.getFileNode(file);
74 if (!dataFile.exists()) {
75 throw new FileNotFoundException();
76 }
77 int revToExtract;
78 if (revision != null) {
79 revToExtract = dataFile.getLocalRevision(revision);
80 } else {
81 revToExtract = localRevision;
82 }
83 dataFile.content(revToExtract, sink, true);
84 }
85 }