comparison src/org/tmatesoft/hg/core/HgCatCommand.java @ 367:2fadf8695f8a

Use 'revision index' instead of the vague 'local revision number' concept in the API
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 16 Dec 2011 15:37:27 +0100
parents 91d75e1bac9f
children 8107b95f4280
comparison
equal deleted inserted replaced
366:189dc6dc1c3e 367:2fadf8695f8a
14 * the terms of a license other than GNU General Public License 14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com 15 * contact TMate Software at support@hg4j.com
16 */ 16 */
17 package org.tmatesoft.hg.core; 17 package org.tmatesoft.hg.core;
18 18
19 import static org.tmatesoft.hg.repo.HgInternals.wrongLocalRevision; 19 import static org.tmatesoft.hg.repo.HgInternals.wrongRevisionIndex;
20 import static org.tmatesoft.hg.repo.HgRepository.BAD_REVISION; 20 import static org.tmatesoft.hg.repo.HgRepository.BAD_REVISION;
21 import static org.tmatesoft.hg.repo.HgRepository.TIP; 21 import static org.tmatesoft.hg.repo.HgRepository.TIP;
22 22
23 import java.io.FileNotFoundException; 23 import java.io.FileNotFoundException;
24 import java.io.IOException; 24 import java.io.IOException;
40 */ 40 */
41 public class HgCatCommand extends HgAbstractCommand<HgCatCommand> { 41 public class HgCatCommand extends HgAbstractCommand<HgCatCommand> {
42 42
43 private final HgRepository repo; 43 private final HgRepository repo;
44 private Path file; 44 private Path file;
45 private int localRevision = TIP; 45 private int revisionIndex = TIP;
46 private Nodeid revision; 46 private Nodeid revision;
47 private Nodeid cset; 47 private Nodeid cset;
48 48
49 public HgCatCommand(HgRepository hgRepo) { 49 public HgCatCommand(HgRepository hgRepo) {
50 repo = hgRepo; 50 repo = hgRepo;
63 file = fname; 63 file = fname;
64 return this; 64 return this;
65 } 65 }
66 66
67 /** 67 /**
68 * Select specific local revision of the file to cat. Note, revision numbering is of particular file, not that of 68 * Select specific revision of the file to cat with local revision index. Note, revision numbering is of particular file, not that of
69 * repository (i.e. revision 0 means initial content of the file, irrespective of changeset revision at the time of commit) 69 * repository (i.e. revision 0 means initial content of the file, irrespective of changeset revision at the time of commit)
70 * 70 *
71 * Invocation of this method clears revision set with {@link #revision(Nodeid)} or {@link #revision(int)} earlier. 71 * Invocation of this method clears revision set with {@link #revision(Nodeid)} or {@link #revision(int)} earlier.
72 * 72 *
73 * XXX rev can't be WORKING_COPY (if allowed, need to implement in #execute()) 73 * @param fileRevisionIndex local revision index, non-negative, or one of predefined constants. Note, use of {@link HgRepository#BAD_REVISION},
74 * @param rev local revision number, non-negative, or one of predefined constants. Note, use of {@link HgRepository#BAD_REVISION},
75 * although possible, makes little sense (command would fail if executed). 74 * although possible, makes little sense (command would fail if executed).
76 * @return <code>this</code> for convenience 75 * @return <code>this</code> for convenience
77 */ 76 */
78 public HgCatCommand revision(int rev) { 77 public HgCatCommand revision(int fileRevisionIndex) {
79 if (wrongLocalRevision(rev)) { 78 if (wrongRevisionIndex(fileRevisionIndex)) {
80 throw new IllegalArgumentException(String.valueOf(rev)); 79 throw new IllegalArgumentException(String.valueOf(fileRevisionIndex));
81 } 80 }
82 localRevision = rev; 81 revisionIndex = fileRevisionIndex;
83 revision = null; 82 revision = null;
84 cset = null; 83 cset = null;
85 return this; 84 return this;
86 } 85 }
87 86
88 /** 87 /**
89 * Select revision to read. Note, this revision is file revision (i.e. the one from manifest), not the changeset revision. 88 * Select file revision to read. Note, this revision is file revision (i.e. the one from manifest), not the changeset revision.
90 * 89 *
91 * Invocation of this method clears revision set with {@link #revision(int)} or {@link #revision(Nodeid)} earlier. 90 * Invocation of this method clears revision set with {@link #revision(int)} or {@link #revision(Nodeid)} earlier.
92 * 91 *
93 * @param nodeid - unique file revision identifier, Note, use of <code>null</code> or {@link Nodeid#NULL} is senseless 92 * @param nodeid - unique file revision identifier, Note, use of <code>null</code> or {@link Nodeid#NULL} is senseless
94 * @return <code>this</code> for convenience 93 * @return <code>this</code> for convenience
96 public HgCatCommand revision(Nodeid nodeid) { 95 public HgCatCommand revision(Nodeid nodeid) {
97 if (nodeid != null && nodeid.isNull()) { 96 if (nodeid != null && nodeid.isNull()) {
98 nodeid = null; 97 nodeid = null;
99 } 98 }
100 revision = nodeid; 99 revision = nodeid;
101 localRevision = BAD_REVISION; 100 revisionIndex = BAD_REVISION;
102 cset = null; 101 cset = null;
103 return this; 102 return this;
104 } 103 }
105 104
106 /** 105 /**
121 * 120 *
122 * @param nodeid changeset revision 121 * @param nodeid changeset revision
123 * @return <code>this</code> for convenience 122 * @return <code>this</code> for convenience
124 */ 123 */
125 public HgCatCommand changeset(Nodeid nodeid) { 124 public HgCatCommand changeset(Nodeid nodeid) {
126 localRevision = BAD_REVISION; 125 revisionIndex = BAD_REVISION;
127 revision = null; 126 revision = null;
128 cset = nodeid; 127 cset = nodeid;
129 return this; 128 return this;
130 } 129 }
131 130
135 * @param sink output channel to write data to. 134 * @param sink output channel to write data to.
136 * @throws HgDataStreamException 135 * @throws HgDataStreamException
137 * @throws IllegalArgumentException when command arguments are incomplete or wrong 136 * @throws IllegalArgumentException when command arguments are incomplete or wrong
138 */ 137 */
139 public void execute(ByteChannel sink) throws HgDataStreamException, HgInvalidControlFileException, CancelledException { 138 public void execute(ByteChannel sink) throws HgDataStreamException, HgInvalidControlFileException, CancelledException {
140 if (localRevision == BAD_REVISION && revision == null && cset == null) { 139 if (revisionIndex == BAD_REVISION && revision == null && cset == null) {
141 throw new IllegalArgumentException("File revision, corresponing local number, or a changset nodeid shall be specified"); 140 throw new IllegalArgumentException("File revision, corresponing local number, or a changset nodeid shall be specified");
142 } 141 }
143 if (file == null) { 142 if (file == null) {
144 throw new IllegalArgumentException("Name of the file is missing"); 143 throw new IllegalArgumentException("Name of the file is missing");
145 } 144 }
150 if (!dataFile.exists()) { 149 if (!dataFile.exists()) {
151 throw new HgDataStreamException(file, new FileNotFoundException(file.toString())); 150 throw new HgDataStreamException(file, new FileNotFoundException(file.toString()));
152 } 151 }
153 int revToExtract; 152 int revToExtract;
154 if (cset != null) { 153 if (cset != null) {
155 int csetRev = repo.getChangelog().getLocalRevision(cset); 154 int csetRev = repo.getChangelog().getRevisionIndex(cset);
156 Nodeid toExtract = null; 155 Nodeid toExtract = null;
157 do { 156 do {
158 toExtract = repo.getManifest().getFileRevision(csetRev, file); 157 toExtract = repo.getManifest().getFileRevision(csetRev, file);
159 if (toExtract == null) { 158 if (toExtract == null) {
160 if (dataFile.isCopy()) { 159 if (dataFile.isCopy()) {
166 } 165 }
167 } while (toExtract == null); 166 } while (toExtract == null);
168 if (toExtract == null) { 167 if (toExtract == null) {
169 throw new HgBadStateException(String.format("File %s nor its origins were not known at repository %s revision", file, cset.shortNotation())); 168 throw new HgBadStateException(String.format("File %s nor its origins were not known at repository %s revision", file, cset.shortNotation()));
170 } 169 }
171 revToExtract = dataFile.getLocalRevision(toExtract); 170 revToExtract = dataFile.getRevisionIndex(toExtract);
172 } else if (revision != null) { 171 } else if (revision != null) {
173 revToExtract = dataFile.getLocalRevision(revision); 172 revToExtract = dataFile.getRevisionIndex(revision);
174 } else { 173 } else {
175 revToExtract = localRevision; 174 revToExtract = revisionIndex;
176 } 175 }
177 ByteChannel sinkWrap; 176 ByteChannel sinkWrap;
178 if (getCancelSupport(null, false) == null) { 177 if (getCancelSupport(null, false) == null) {
179 // no command-specific cancel helper, no need for extra proxy 178 // no command-specific cancel helper, no need for extra proxy
180 // sink itself still may supply CS 179 // sink itself still may supply CS