Mercurial > hg4j
annotate src/org/tmatesoft/hg/core/HgCatCommand.java @ 209:9ce3b26798c4
Few branches (distinct BranchChains from distinct heads) may end up with same nodes. Building BC structure fixed to reuse chain elements
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Thu, 28 Apr 2011 03:44:45 +0200 |
parents | d5268ca7715b |
children | 41a778e3fd31 |
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 | |
148
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
19 import static org.tmatesoft.hg.repo.HgInternals.wrongLocalRevision; |
107 | 20 import static org.tmatesoft.hg.repo.HgRepository.BAD_REVISION; |
21 import static org.tmatesoft.hg.repo.HgRepository.TIP; | |
22 | |
23 import java.io.FileNotFoundException; | |
148
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
24 import java.io.IOException; |
107 | 25 |
26 import org.tmatesoft.hg.repo.HgDataFile; | |
27 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
|
28 import org.tmatesoft.hg.util.ByteChannel; |
148
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
29 import org.tmatesoft.hg.util.CancelledException; |
133
4a948ec83980
core.Path to util.Path as it's not Hg repo dependant
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
131
diff
changeset
|
30 import org.tmatesoft.hg.util.Path; |
107 | 31 |
32 /** | |
131
aa1629f36482
Renamed .core classes to start with Hg prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
130
diff
changeset
|
33 * Command to obtain content of a file, 'hg cat' counterpart. |
107 | 34 * |
35 * @author Artem Tikhomirov | |
36 * @author TMate Software Ltd. | |
37 */ | |
131
aa1629f36482
Renamed .core classes to start with Hg prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
130
diff
changeset
|
38 public class HgCatCommand { |
107 | 39 |
40 private final HgRepository repo; | |
41 private Path file; | |
42 private int localRevision = TIP; | |
43 private Nodeid revision; | |
44 | |
131
aa1629f36482
Renamed .core classes to start with Hg prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
130
diff
changeset
|
45 public HgCatCommand(HgRepository hgRepo) { |
107 | 46 repo = hgRepo; |
47 } | |
48 | |
148
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
49 /** |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
50 * File to read, required parameter |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
51 * @param fname path to a repository file, can't be <code>null</code> |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
52 * @return <code>this</code> for convenience |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
53 * @throws IllegalArgumentException if supplied fname is null or points to directory |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
54 */ |
131
aa1629f36482
Renamed .core classes to start with Hg prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
130
diff
changeset
|
55 public HgCatCommand file(Path fname) { |
148
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
56 if (fname == null || fname.isDirectory()) { |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
57 throw new IllegalArgumentException(String.valueOf(fname)); |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
58 } |
107 | 59 file = fname; |
60 return this; | |
61 } | |
62 | |
148
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
63 /** |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
64 * Invocation of this method clears revision set with {@link #revision(Nodeid)} or {@link #revision(int)} earlier. |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
65 * XXX rev can't be WORKING_COPY (if allowed, need to implement in #execute()) |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
66 * @param rev local revision number, non-negative, or one of predefined constants. Note, use of {@link HgRepository#BAD_REVISION}, |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
67 * although possible, makes little sense (command would fail if executed). |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
68 * @return <code>this</code> for convenience |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
69 */ |
131
aa1629f36482
Renamed .core classes to start with Hg prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
130
diff
changeset
|
70 public HgCatCommand revision(int rev) { |
148
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
71 if (wrongLocalRevision(rev)) { |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
72 throw new IllegalArgumentException(String.valueOf(rev)); |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
73 } |
107 | 74 localRevision = rev; |
75 revision = null; | |
76 return this; | |
77 } | |
78 | |
148
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
79 /** |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
80 * Select revision to read. Invocation of this method clears revision set with {@link #revision(int)} or {@link #revision(Nodeid)} earlier. |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
81 * |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
82 * @param nodeid - unique revision identifier, Note, use of <code>null</code> or {@link Nodeid#NULL} is senseless |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
83 * @return <code>this</code> for convenience |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
84 */ |
131
aa1629f36482
Renamed .core classes to start with Hg prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
130
diff
changeset
|
85 public HgCatCommand revision(Nodeid nodeid) { |
148
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
86 if (nodeid != null && nodeid.isNull()) { |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
87 nodeid = null; |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
88 } |
107 | 89 revision = nodeid; |
90 localRevision = BAD_REVISION; | |
91 return this; | |
92 } | |
93 | |
148
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
94 /** |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
95 * Runs the command with current set of parameters and pipes data to provided sink. |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
96 * |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
97 * @param sink output channel to write data to. |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
98 * @throws HgDataStreamException |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
99 * @throws IllegalArgumentException when command arguments are incomplete or wrong |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
100 */ |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
101 public void execute(ByteChannel sink) throws HgDataStreamException, IOException, CancelledException { |
107 | 102 if (localRevision == BAD_REVISION && revision == null) { |
103 throw new IllegalArgumentException("Either local file revision number or nodeid shall be specified"); | |
104 } | |
105 if (file == null) { | |
106 throw new IllegalArgumentException("Name of the file is missing"); | |
107 } | |
115
c0cc2535462c
Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
107
diff
changeset
|
108 if (sink == null) { |
148
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
109 throw new IllegalArgumentException("Need an output channel"); |
107 | 110 } |
111 HgDataFile dataFile = repo.getFileNode(file); | |
112 if (!dataFile.exists()) { | |
148
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
113 throw new HgDataStreamException(file.toString(), new FileNotFoundException(file.toString())); |
107 | 114 } |
115
c0cc2535462c
Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
107
diff
changeset
|
115 int revToExtract; |
107 | 116 if (revision != null) { |
115
c0cc2535462c
Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
107
diff
changeset
|
117 revToExtract = dataFile.getLocalRevision(revision); |
107 | 118 } else { |
115
c0cc2535462c
Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
107
diff
changeset
|
119 revToExtract = localRevision; |
107 | 120 } |
157
d5268ca7715b
Merged branch wrap-data-access into default for resource-friendly data access. Updated API to promote that friendliness to clients (channels, not byte[]). More exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
148
diff
changeset
|
121 dataFile.contentWithFilters(revToExtract, sink); |
107 | 122 } |
123 } |