annotate src/org/tmatesoft/hg/core/CatCommand.java @ 115:c0cc2535462c

Introduced channels to pipeline (and easily filter) data streams
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 03 Feb 2011 23:32:08 +0100
parents 7ec89d637f50
children b1d6208fb517
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
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
15 * contact TMate Software at support@svnkit.com
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;
107
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
28 /**
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
29 * Command to obtain content of a
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
30 *
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
31 * @author Artem Tikhomirov
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32 * @author TMate Software Ltd.
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 */
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
34 public class CatCommand {
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
35
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
36 private final HgRepository repo;
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
37 private Path file;
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
38 private int localRevision = TIP;
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39 private Nodeid revision;
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
41 public CatCommand(HgRepository hgRepo) {
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42 repo = hgRepo;
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43 }
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
44
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
45 public CatCommand file(Path fname) {
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
46 file = fname;
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
47 return this;
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
48 }
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
49
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
50 // rev can't be WORKING_COPY (if allowed, need to implement in #execute())
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
51 public CatCommand revision(int rev) {
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52 localRevision = rev;
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53 revision = null;
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
54 return this;
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
55 }
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
56
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
57 public CatCommand revision(Nodeid nodeid) {
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
58 revision = nodeid;
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
59 localRevision = BAD_REVISION;
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
60 return this;
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
61 }
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
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
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
64 if (localRevision == BAD_REVISION && revision == null) {
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65 throw new IllegalArgumentException("Either local file revision number or nodeid shall be specified");
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
66 }
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
67 if (file == null) {
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
68 throw new IllegalArgumentException("Name of the file is missing");
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
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
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
71 throw new IllegalArgumentException();
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
72 }
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
73 HgDataFile dataFile = repo.getFileNode(file);
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
74 if (!dataFile.exists()) {
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
75 throw new FileNotFoundException();
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
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
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
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
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
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
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
82 }
115
c0cc2535462c Introduced channels to pipeline (and easily filter) data streams
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 107
diff changeset
83 dataFile.content(revToExtract, sink);
107
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
84 }
7ec89d637f50 CatCommand
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
85 }