Mercurial > jhg
annotate src/org/tmatesoft/hg/core/HgCatCommand.java @ 423:9c9c442b5f2e
Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Fri, 23 Mar 2012 22:51:18 +0100 |
parents | 0ae53c32ecef |
children | 31a89587eb04 |
rev | line source |
---|---|
107 | 1 /* |
396
0ae53c32ecef
Straighten out exceptions thrown when file access failed - three is too much
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
368
diff
changeset
|
2 * Copyright (c) 2011-2012 TMate Software Ltd |
107 | 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 | |
367
2fadf8695f8a
Use 'revision index' instead of the vague 'local revision number' concept in the API
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
356
diff
changeset
|
19 import static org.tmatesoft.hg.repo.HgInternals.wrongRevisionIndex; |
107 | 20 import static org.tmatesoft.hg.repo.HgRepository.BAD_REVISION; |
21 import static org.tmatesoft.hg.repo.HgRepository.TIP; | |
22 | |
322
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
23 import java.io.IOException; |
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
24 import java.nio.ByteBuffer; |
107 | 25 |
26 import org.tmatesoft.hg.repo.HgDataFile; | |
423
9c9c442b5f2e
Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
396
diff
changeset
|
27 import org.tmatesoft.hg.repo.HgInvalidControlFileException; |
9c9c442b5f2e
Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
396
diff
changeset
|
28 import org.tmatesoft.hg.repo.HgInvalidFileException; |
9c9c442b5f2e
Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
396
diff
changeset
|
29 import org.tmatesoft.hg.repo.HgInvalidRevisionException; |
107 | 30 import org.tmatesoft.hg.repo.HgRepository; |
322
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
31 import org.tmatesoft.hg.util.Adaptable; |
115
c0cc2535462c
Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
107
diff
changeset
|
32 import org.tmatesoft.hg.util.ByteChannel; |
322
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
33 import org.tmatesoft.hg.util.CancelSupport; |
148
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
34 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
|
35 import org.tmatesoft.hg.util.Path; |
107 | 36 |
37 /** | |
131
aa1629f36482
Renamed .core classes to start with Hg prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
130
diff
changeset
|
38 * Command to obtain content of a file, 'hg cat' counterpart. |
107 | 39 * |
40 * @author Artem Tikhomirov | |
41 * @author TMate Software Ltd. | |
42 */ | |
215
41a778e3fd31
Issue 5: Facilities for progress and cancellation. More specific exceptions. Exceptions from callbacks as RuntimeException
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
157
diff
changeset
|
43 public class HgCatCommand extends HgAbstractCommand<HgCatCommand> { |
107 | 44 |
45 private final HgRepository repo; | |
46 private Path file; | |
367
2fadf8695f8a
Use 'revision index' instead of the vague 'local revision number' concept in the API
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
356
diff
changeset
|
47 private int revisionIndex = TIP; |
107 | 48 private Nodeid revision; |
232
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
49 private Nodeid cset; |
107 | 50 |
131
aa1629f36482
Renamed .core classes to start with Hg prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
130
diff
changeset
|
51 public HgCatCommand(HgRepository hgRepo) { |
107 | 52 repo = hgRepo; |
53 } | |
54 | |
148
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
55 /** |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
56 * File to read, required parameter |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
57 * @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
|
58 * @return <code>this</code> for convenience |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
59 * @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
|
60 */ |
131
aa1629f36482
Renamed .core classes to start with Hg prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
130
diff
changeset
|
61 public HgCatCommand file(Path fname) { |
148
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
62 if (fname == null || fname.isDirectory()) { |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
63 throw new IllegalArgumentException(String.valueOf(fname)); |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
64 } |
107 | 65 file = fname; |
66 return this; | |
67 } | |
68 | |
148
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
69 /** |
368
8107b95f4280
Update Javadoc with 'revision index'
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
367
diff
changeset
|
70 * Select specific revision of the file to cat with revision local index. Note, revision numbering is of particular file, not that of |
232
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
71 * repository (i.e. revision 0 means initial content of the file, irrespective of changeset revision at the time of commit) |
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
72 * |
148
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
73 * Invocation of this method clears revision set with {@link #revision(Nodeid)} or {@link #revision(int)} earlier. |
232
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
74 * |
368
8107b95f4280
Update Javadoc with 'revision index'
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
367
diff
changeset
|
75 * @param fileRevisionIndex - revision local index, non-negative, or one of predefined constants. Note, use of {@link HgRepository#BAD_REVISION}, |
148
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
76 * 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
|
77 * @return <code>this</code> for convenience |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
78 */ |
367
2fadf8695f8a
Use 'revision index' instead of the vague 'local revision number' concept in the API
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
356
diff
changeset
|
79 public HgCatCommand revision(int fileRevisionIndex) { |
2fadf8695f8a
Use 'revision index' instead of the vague 'local revision number' concept in the API
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
356
diff
changeset
|
80 if (wrongRevisionIndex(fileRevisionIndex)) { |
2fadf8695f8a
Use 'revision index' instead of the vague 'local revision number' concept in the API
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
356
diff
changeset
|
81 throw new IllegalArgumentException(String.valueOf(fileRevisionIndex)); |
148
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
82 } |
367
2fadf8695f8a
Use 'revision index' instead of the vague 'local revision number' concept in the API
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
356
diff
changeset
|
83 revisionIndex = fileRevisionIndex; |
107 | 84 revision = null; |
232
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
85 cset = null; |
107 | 86 return this; |
87 } | |
88 | |
148
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
89 /** |
367
2fadf8695f8a
Use 'revision index' instead of the vague 'local revision number' concept in the API
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
356
diff
changeset
|
90 * Select file revision to read. Note, this revision is file revision (i.e. the one from manifest), not the changeset revision. |
232
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
91 * |
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
92 * Invocation of this method clears revision set with {@link #revision(int)} or {@link #revision(Nodeid)} earlier. |
148
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
93 * |
232
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
94 * @param nodeid - unique file revision identifier, Note, use of <code>null</code> or {@link Nodeid#NULL} is senseless |
148
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
95 * @return <code>this</code> for convenience |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
96 */ |
131
aa1629f36482
Renamed .core classes to start with Hg prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
130
diff
changeset
|
97 public HgCatCommand revision(Nodeid nodeid) { |
148
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
98 if (nodeid != null && nodeid.isNull()) { |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
99 nodeid = null; |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
100 } |
107 | 101 revision = nodeid; |
367
2fadf8695f8a
Use 'revision index' instead of the vague 'local revision number' concept in the API
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
356
diff
changeset
|
102 revisionIndex = BAD_REVISION; |
232
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
103 cset = null; |
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
104 return this; |
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
105 } |
248
3fbfce107f94
Issue 8: Means to find out information about given file at specific changeset. Inner ManifestRevisionInspector got promoted to ManifestRevision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
237
diff
changeset
|
106 |
3fbfce107f94
Issue 8: Means to find out information about given file at specific changeset. Inner ManifestRevisionInspector got promoted to ManifestRevision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
237
diff
changeset
|
107 /** |
3fbfce107f94
Issue 8: Means to find out information about given file at specific changeset. Inner ManifestRevisionInspector got promoted to ManifestRevision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
237
diff
changeset
|
108 * Parameterize the command from file revision object. |
3fbfce107f94
Issue 8: Means to find out information about given file at specific changeset. Inner ManifestRevisionInspector got promoted to ManifestRevision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
237
diff
changeset
|
109 * |
3fbfce107f94
Issue 8: Means to find out information about given file at specific changeset. Inner ManifestRevisionInspector got promoted to ManifestRevision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
237
diff
changeset
|
110 * @param fileRev file revision to cat |
3fbfce107f94
Issue 8: Means to find out information about given file at specific changeset. Inner ManifestRevisionInspector got promoted to ManifestRevision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
237
diff
changeset
|
111 * @return <code>this</code> for convenience |
3fbfce107f94
Issue 8: Means to find out information about given file at specific changeset. Inner ManifestRevisionInspector got promoted to ManifestRevision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
237
diff
changeset
|
112 */ |
3fbfce107f94
Issue 8: Means to find out information about given file at specific changeset. Inner ManifestRevisionInspector got promoted to ManifestRevision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
237
diff
changeset
|
113 public HgCatCommand revision(HgFileRevision fileRev) { |
3fbfce107f94
Issue 8: Means to find out information about given file at specific changeset. Inner ManifestRevisionInspector got promoted to ManifestRevision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
237
diff
changeset
|
114 return file(fileRev.getPath()).revision(fileRev.getRevision()); |
3fbfce107f94
Issue 8: Means to find out information about given file at specific changeset. Inner ManifestRevisionInspector got promoted to ManifestRevision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
237
diff
changeset
|
115 } |
232
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
116 |
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
117 /** |
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
118 * Select whatever revision of the file that was actual at the time of the specified changeset. Unlike {@link #revision(int)} or {@link #revision(Nodeid)}, this method |
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
119 * operates in terms of repository global revisions (aka changesets). |
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
120 * |
368
8107b95f4280
Update Javadoc with 'revision index'
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
367
diff
changeset
|
121 * Invocation of this method clears selection of a file revision with its index. |
232
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
122 * |
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
123 * @param nodeid changeset revision |
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
124 * @return <code>this</code> for convenience |
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
125 */ |
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
126 public HgCatCommand changeset(Nodeid nodeid) { |
367
2fadf8695f8a
Use 'revision index' instead of the vague 'local revision number' concept in the API
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
356
diff
changeset
|
127 revisionIndex = BAD_REVISION; |
232
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
128 revision = null; |
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
129 cset = nodeid; |
107 | 130 return this; |
131 } | |
132 | |
148
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
133 /** |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
134 * 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
|
135 * |
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
136 * @param sink output channel to write data to. |
396
0ae53c32ecef
Straighten out exceptions thrown when file access failed - three is too much
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
368
diff
changeset
|
137 * |
0ae53c32ecef
Straighten out exceptions thrown when file access failed - three is too much
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
368
diff
changeset
|
138 * @throws HgBadArgumentException if no target file node found |
0ae53c32ecef
Straighten out exceptions thrown when file access failed - three is too much
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
368
diff
changeset
|
139 * @throws HgInvalidControlFileException if access to revlog index/data entry failed |
0ae53c32ecef
Straighten out exceptions thrown when file access failed - three is too much
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
368
diff
changeset
|
140 * @throws HgInvalidFileException if access to file in working directory failed |
0ae53c32ecef
Straighten out exceptions thrown when file access failed - three is too much
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
368
diff
changeset
|
141 * @throws HgException in case of some other library issue |
0ae53c32ecef
Straighten out exceptions thrown when file access failed - three is too much
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
368
diff
changeset
|
142 * @throws CancelledException if execution of the operation was cancelled |
0ae53c32ecef
Straighten out exceptions thrown when file access failed - three is too much
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
368
diff
changeset
|
143 * @throws HgInvalidRevisionException if supplied argument doesn't represent revision index in this revlog (<em>runtime exception</em>) |
148
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
144 * @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
|
145 */ |
396
0ae53c32ecef
Straighten out exceptions thrown when file access failed - three is too much
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
368
diff
changeset
|
146 public void execute(ByteChannel sink) throws HgException, CancelledException { |
0ae53c32ecef
Straighten out exceptions thrown when file access failed - three is too much
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
368
diff
changeset
|
147 // XXX perhaps, IAE together with HgBadArgumentException is not the best idea |
367
2fadf8695f8a
Use 'revision index' instead of the vague 'local revision number' concept in the API
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
356
diff
changeset
|
148 if (revisionIndex == BAD_REVISION && revision == null && cset == null) { |
232
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
149 throw new IllegalArgumentException("File revision, corresponing local number, or a changset nodeid shall be specified"); |
107 | 150 } |
151 if (file == null) { | |
152 throw new IllegalArgumentException("Name of the file is missing"); | |
153 } | |
115
c0cc2535462c
Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
107
diff
changeset
|
154 if (sink == null) { |
148
1a7a9a20e1f9
Exceptions, javadoc. Initial cancel and progress support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
133
diff
changeset
|
155 throw new IllegalArgumentException("Need an output channel"); |
107 | 156 } |
157 HgDataFile dataFile = repo.getFileNode(file); | |
158 if (!dataFile.exists()) { | |
396
0ae53c32ecef
Straighten out exceptions thrown when file access failed - three is too much
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
368
diff
changeset
|
159 // TODO may benefit from repo.getStoragePath to print revlog location in addition to human-friendly file path |
0ae53c32ecef
Straighten out exceptions thrown when file access failed - three is too much
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
368
diff
changeset
|
160 throw new HgBadArgumentException(String.format("File %s not found in the repository", file), null).setFileName(file); |
107 | 161 } |
115
c0cc2535462c
Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
107
diff
changeset
|
162 int revToExtract; |
232
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
163 if (cset != null) { |
367
2fadf8695f8a
Use 'revision index' instead of the vague 'local revision number' concept in the API
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
356
diff
changeset
|
164 int csetRev = repo.getChangelog().getRevisionIndex(cset); |
232
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
165 Nodeid toExtract = null; |
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
166 do { |
423
9c9c442b5f2e
Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
396
diff
changeset
|
167 // TODO post-1.0 perhaps, HgChangesetFileSneaker may come handy? |
232
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
168 toExtract = repo.getManifest().getFileRevision(csetRev, file); |
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
169 if (toExtract == null) { |
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
170 if (dataFile.isCopy()) { |
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
171 file = dataFile.getCopySourceName(); |
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
172 dataFile = repo.getFileNode(file); |
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
173 } else { |
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
174 break; |
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
175 } |
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
176 } |
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
177 } while (toExtract == null); |
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
178 if (toExtract == null) { |
423
9c9c442b5f2e
Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
396
diff
changeset
|
179 // TODO explicit FileNotFoundException? |
9c9c442b5f2e
Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
396
diff
changeset
|
180 throw new HgBadArgumentException(String.format("File %s nor its origins were not known at repository %s revision", file, cset.shortNotation()), null); |
232
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
181 } |
367
2fadf8695f8a
Use 'revision index' instead of the vague 'local revision number' concept in the API
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
356
diff
changeset
|
182 revToExtract = dataFile.getRevisionIndex(toExtract); |
232
b7347daa50e3
Allow to cat a file with changeset revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
215
diff
changeset
|
183 } else if (revision != null) { |
367
2fadf8695f8a
Use 'revision index' instead of the vague 'local revision number' concept in the API
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
356
diff
changeset
|
184 revToExtract = dataFile.getRevisionIndex(revision); |
107 | 185 } else { |
367
2fadf8695f8a
Use 'revision index' instead of the vague 'local revision number' concept in the API
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
356
diff
changeset
|
186 revToExtract = revisionIndex; |
107 | 187 } |
322
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
188 ByteChannel sinkWrap; |
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
189 if (getCancelSupport(null, false) == null) { |
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
190 // no command-specific cancel helper, no need for extra proxy |
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
191 // sink itself still may supply CS |
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
192 sinkWrap = sink; |
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
193 } else { |
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
194 // try CS from sink, if any. at least there is CS from command |
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
195 CancelSupport cancelHelper = getCancelSupport(sink, true); |
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
196 cancelHelper.checkCancelled(); |
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
197 sinkWrap = new ByteChannelProxy(sink, cancelHelper); |
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
198 } |
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
199 dataFile.contentWithFilters(revToExtract, sinkWrap); |
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
200 } |
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
201 |
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
202 private static class ByteChannelProxy implements ByteChannel, Adaptable { |
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
203 private final ByteChannel delegate; |
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
204 private final CancelSupport cancelHelper; |
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
205 |
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
206 public ByteChannelProxy(ByteChannel _delegate, CancelSupport cs) { |
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
207 assert _delegate != null; |
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
208 delegate = _delegate; |
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
209 cancelHelper = cs; |
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
210 } |
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
211 public int write(ByteBuffer buffer) throws IOException, CancelledException { |
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
212 return delegate.write(buffer); |
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
213 } |
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
214 |
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
215 public <T> T getAdapter(Class<T> adapterClass) { |
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
216 if (CancelSupport.class == adapterClass) { |
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
217 return adapterClass.cast(cancelHelper); |
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
218 } |
356
91d75e1bac9f
Consistent approach to deal with adaptable objects. Give adaptable precedence over instanceof to allow conditional response when classes do implement desired interface
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
354
diff
changeset
|
219 return Adaptable.Factory.getAdapter(delegate, adapterClass, null); |
322
d68dcb3b5f49
Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
248
diff
changeset
|
220 } |
107 | 221 } |
222 } |