Mercurial > hg4j
annotate src/org/tmatesoft/hg/core/HgCatCommand.java @ 147:a05145db4d0c
Bring test repos along with us to recreate testbench
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Tue, 22 Feb 2011 15:49:26 +0100 |
parents | 4a948ec83980 |
children | 1a7a9a20e1f9 |
rev | line source |
---|---|
107 | 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 | |
130
7567f4a42fe5
Correct contact address
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
121
diff
changeset
|
15 * contact TMate Software at support@hg4j.com |
107 | 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; | |
115
c0cc2535462c
Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
107
diff
changeset
|
26 import org.tmatesoft.hg.util.ByteChannel; |
133
4a948ec83980
core.Path to util.Path as it's not Hg repo dependant
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
131
diff
changeset
|
27 import org.tmatesoft.hg.util.Path; |
107 | 28 |
29 /** | |
131
aa1629f36482
Renamed .core classes to start with Hg prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
130
diff
changeset
|
30 * Command to obtain content of a file, 'hg cat' counterpart. |
107 | 31 * |
32 * @author Artem Tikhomirov | |
33 * @author TMate Software Ltd. | |
34 */ | |
131
aa1629f36482
Renamed .core classes to start with Hg prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
130
diff
changeset
|
35 public class HgCatCommand { |
107 | 36 |
37 private final HgRepository repo; | |
38 private Path file; | |
39 private int localRevision = TIP; | |
40 private Nodeid revision; | |
41 | |
131
aa1629f36482
Renamed .core classes to start with Hg prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
130
diff
changeset
|
42 public HgCatCommand(HgRepository hgRepo) { |
107 | 43 repo = hgRepo; |
44 } | |
45 | |
131
aa1629f36482
Renamed .core classes to start with Hg prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
130
diff
changeset
|
46 public HgCatCommand file(Path fname) { |
107 | 47 file = fname; |
48 return this; | |
49 } | |
50 | |
51 // rev can't be WORKING_COPY (if allowed, need to implement in #execute()) | |
131
aa1629f36482
Renamed .core classes to start with Hg prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
130
diff
changeset
|
52 public HgCatCommand revision(int rev) { |
107 | 53 localRevision = rev; |
54 revision = null; | |
55 return this; | |
56 } | |
57 | |
131
aa1629f36482
Renamed .core classes to start with Hg prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
130
diff
changeset
|
58 public HgCatCommand revision(Nodeid nodeid) { |
107 | 59 revision = nodeid; |
60 localRevision = BAD_REVISION; | |
61 return this; | |
62 } | |
63 | |
115
c0cc2535462c
Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
107
diff
changeset
|
64 public void execute(ByteChannel sink) throws Exception /*TODO own exception type*/ { |
107 | 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 } | |
115
c0cc2535462c
Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
107
diff
changeset
|
71 if (sink == null) { |
107 | 72 throw new IllegalArgumentException(); |
73 } | |
74 HgDataFile dataFile = repo.getFileNode(file); | |
75 if (!dataFile.exists()) { | |
76 throw new FileNotFoundException(); | |
77 } | |
115
c0cc2535462c
Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
107
diff
changeset
|
78 int revToExtract; |
107 | 79 if (revision != null) { |
115
c0cc2535462c
Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
107
diff
changeset
|
80 revToExtract = dataFile.getLocalRevision(revision); |
107 | 81 } else { |
115
c0cc2535462c
Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
107
diff
changeset
|
82 revToExtract = localRevision; |
107 | 83 } |
121
b1d6208fb517
Conditionally apply filters to file content
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
115
diff
changeset
|
84 dataFile.content(revToExtract, sink, true); |
107 | 85 } |
86 } |