annotate src/org/tmatesoft/hg/core/HgCatCommand.java @ 136:947bf231acbb

Strip off comments in config file
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 16 Feb 2011 22:08:58 +0100
parents 4a948ec83980
children 1a7a9a20e1f9
rev   line source
107
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2011 TMate Software Ltd
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
3 *
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
6 * the Free Software Foundation; version 2 of the License.
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
7 *
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
11 * GNU General Public License for more details.
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
12 *
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
13 * For information on how to redistribute this software under
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
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
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
17 package org.tmatesoft.hg.core;
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
19 import static org.tmatesoft.hg.repo.HgRepository.BAD_REVISION;
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
20 import static org.tmatesoft.hg.repo.HgRepository.TIP;
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
21
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
22 import java.io.FileNotFoundException;
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
23
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24 import org.tmatesoft.hg.repo.HgDataFile;
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
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
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
28
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
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
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
31 *
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32 * @author Artem Tikhomirov
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 * @author TMate Software Ltd.
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
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
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
36
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
37 private final HgRepository repo;
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
38 private Path file;
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39 private int localRevision = TIP;
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40 private Nodeid revision;
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
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
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43 repo = hgRepo;
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
44 }
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
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
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
47 file = fname;
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
48 return this;
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
49 }
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
50
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
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
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53 localRevision = rev;
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
54 revision = null;
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
55 return this;
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
56 }
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
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
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
59 revision = nodeid;
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
60 localRevision = BAD_REVISION;
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
61 return this;
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
62 }
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
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
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65 if (localRevision == BAD_REVISION && revision == null) {
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
66 throw new IllegalArgumentException("Either local file revision number or nodeid shall be specified");
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
67 }
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
68 if (file == null) {
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
69 throw new IllegalArgumentException("Name of the file is missing");
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
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
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
72 throw new IllegalArgumentException();
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
73 }
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
74 HgDataFile dataFile = repo.getFileNode(file);
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
75 if (!dataFile.exists()) {
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
76 throw new FileNotFoundException();
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
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
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
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
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
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
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
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
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
85 }
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
86 }