comparison src/org/tmatesoft/hg/core/CatCommand.java @ 107:7ec89d637f50

CatCommand
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Sat, 29 Jan 2011 02:16:01 +0100
parents
children c0cc2535462c
comparison
equal deleted inserted replaced
106:5c43e937e15b 107:7ec89d637f50
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@svnkit.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 import java.io.IOException;
24 import java.io.OutputStream;
25
26 import org.tmatesoft.hg.repo.HgDataFile;
27 import org.tmatesoft.hg.repo.HgRepository;
28
29 /**
30 * Command to obtain content of a
31 *
32 * @author Artem Tikhomirov
33 * @author TMate Software Ltd.
34 */
35 public class CatCommand {
36
37 private final HgRepository repo;
38 private Path file;
39 private int localRevision = TIP;
40 private Nodeid revision;
41
42 public CatCommand(HgRepository hgRepo) {
43 repo = hgRepo;
44 }
45
46 public CatCommand file(Path fname) {
47 file = fname;
48 return this;
49 }
50
51 // rev can't be WORKING_COPY (if allowed, need to implement in #execute())
52 public CatCommand revision(int rev) {
53 localRevision = rev;
54 revision = null;
55 return this;
56 }
57
58 public CatCommand revision(Nodeid nodeid) {
59 revision = nodeid;
60 localRevision = BAD_REVISION;
61 return this;
62 }
63
64 public void execute(OutputStream os) throws IOException /*TODO own exception type*/ {
65 if (localRevision == BAD_REVISION && revision == null) {
66 throw new IllegalArgumentException("Either local file revision number or nodeid shall be specified");
67 }
68 if (file == null) {
69 throw new IllegalArgumentException("Name of the file is missing");
70 }
71 if (os == null) {
72 throw new IllegalArgumentException();
73 }
74 HgDataFile dataFile = repo.getFileNode(file);
75 if (!dataFile.exists()) {
76 throw new FileNotFoundException();
77 }
78 byte[] content;
79 if (revision != null) {
80 content = dataFile.content(revision);
81 } else {
82 content = dataFile.content(localRevision);
83 }
84 os.write(content);
85 }
86 }