Mercurial > jhg
annotate src/org/tmatesoft/hg/core/HgCatCommand.java @ 132:6778075cd2b4
Fix reference to class name in the comment
| author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
|---|---|
| date | Wed, 16 Feb 2011 20:56:43 +0100 |
| parents | aa1629f36482 |
| children | 4a948ec83980 |
| 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; |
| 107 | 27 |
| 28 /** | |
|
131
aa1629f36482
Renamed .core classes to start with Hg prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
130
diff
changeset
|
29 * Command to obtain content of a file, 'hg cat' counterpart. |
| 107 | 30 * |
| 31 * @author Artem Tikhomirov | |
| 32 * @author TMate Software Ltd. | |
| 33 */ | |
|
131
aa1629f36482
Renamed .core classes to start with Hg prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
130
diff
changeset
|
34 public class HgCatCommand { |
| 107 | 35 |
| 36 private final HgRepository repo; | |
| 37 private Path file; | |
| 38 private int localRevision = TIP; | |
| 39 private Nodeid revision; | |
| 40 | |
|
131
aa1629f36482
Renamed .core classes to start with Hg prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
130
diff
changeset
|
41 public HgCatCommand(HgRepository hgRepo) { |
| 107 | 42 repo = hgRepo; |
| 43 } | |
| 44 | |
|
131
aa1629f36482
Renamed .core classes to start with Hg prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
130
diff
changeset
|
45 public HgCatCommand file(Path fname) { |
| 107 | 46 file = fname; |
| 47 return this; | |
| 48 } | |
| 49 | |
| 50 // 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
|
51 public HgCatCommand revision(int rev) { |
| 107 | 52 localRevision = rev; |
| 53 revision = null; | |
| 54 return this; | |
| 55 } | |
| 56 | |
|
131
aa1629f36482
Renamed .core classes to start with Hg prefix
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
130
diff
changeset
|
57 public HgCatCommand revision(Nodeid nodeid) { |
| 107 | 58 revision = nodeid; |
| 59 localRevision = BAD_REVISION; | |
| 60 return this; | |
| 61 } | |
| 62 | |
|
115
c0cc2535462c
Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
107
diff
changeset
|
63 public void execute(ByteChannel sink) throws Exception /*TODO own exception type*/ { |
| 107 | 64 if (localRevision == BAD_REVISION && revision == null) { |
| 65 throw new IllegalArgumentException("Either local file revision number or nodeid shall be specified"); | |
| 66 } | |
| 67 if (file == null) { | |
| 68 throw new IllegalArgumentException("Name of the file is missing"); | |
| 69 } | |
|
115
c0cc2535462c
Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
107
diff
changeset
|
70 if (sink == null) { |
| 107 | 71 throw new IllegalArgumentException(); |
| 72 } | |
| 73 HgDataFile dataFile = repo.getFileNode(file); | |
| 74 if (!dataFile.exists()) { | |
| 75 throw new FileNotFoundException(); | |
| 76 } | |
|
115
c0cc2535462c
Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
107
diff
changeset
|
77 int revToExtract; |
| 107 | 78 if (revision != null) { |
|
115
c0cc2535462c
Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
107
diff
changeset
|
79 revToExtract = dataFile.getLocalRevision(revision); |
| 107 | 80 } else { |
|
115
c0cc2535462c
Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
107
diff
changeset
|
81 revToExtract = localRevision; |
| 107 | 82 } |
|
121
b1d6208fb517
Conditionally apply filters to file content
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
115
diff
changeset
|
83 dataFile.content(revToExtract, sink, true); |
| 107 | 84 } |
| 85 } |
