107
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
1 /*
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
2 * Copyright (c) 2011 TMate Software Ltd
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
3 *
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
4 * This program is free software; you can redistribute it and/or modify
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
5 * it under the terms of the GNU General Public License as published by
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
6 * the Free Software Foundation; version 2 of the License.
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
7 *
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
8 * This program is distributed in the hope that it will be useful,
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
11 * GNU General Public License for more details.
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
12 *
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
13 * For information on how to redistribute this software under
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
14 * the terms of a license other than GNU General Public License
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
15 * contact TMate Software at support@svnkit.com
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
16 */
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
17 package org.tmatesoft.hg.core;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
18
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
19 import static org.tmatesoft.hg.repo.HgRepository.BAD_REVISION;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
20 import static org.tmatesoft.hg.repo.HgRepository.TIP;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
21
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
22 import java.io.FileNotFoundException;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
23
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
24 import org.tmatesoft.hg.repo.HgDataFile;
|
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>
diff
changeset
|
26 import org.tmatesoft.hg.util.ByteChannel;
|
107
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
27
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
28 /**
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
29 * Command to obtain content of a
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
30 *
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
31 * @author Artem Tikhomirov
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
32 * @author TMate Software Ltd.
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
33 */
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
34 public class CatCommand {
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
35
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
36 private final HgRepository repo;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
37 private Path file;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
38 private int localRevision = TIP;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
39 private Nodeid revision;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
40
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
41 public CatCommand(HgRepository hgRepo) {
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
42 repo = hgRepo;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
43 }
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
44
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
45 public CatCommand file(Path fname) {
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
46 file = fname;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
47 return this;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
48 }
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
49
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
50 // rev can't be WORKING_COPY (if allowed, need to implement in #execute())
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
51 public CatCommand revision(int rev) {
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
52 localRevision = rev;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
53 revision = null;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
54 return this;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
55 }
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
56
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
57 public CatCommand revision(Nodeid nodeid) {
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
58 revision = nodeid;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
59 localRevision = BAD_REVISION;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
60 return this;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
61 }
|
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>
diff
changeset
|
63 public void execute(ByteChannel sink) throws Exception /*TODO own exception type*/ {
|
107
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
64 if (localRevision == BAD_REVISION && revision == null) {
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
65 throw new IllegalArgumentException("Either local file revision number or nodeid shall be specified");
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
66 }
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
67 if (file == null) {
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
68 throw new IllegalArgumentException("Name of the file is missing");
|
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>
diff
changeset
|
70 if (sink == null) {
|
107
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
71 throw new IllegalArgumentException();
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
72 }
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
73 HgDataFile dataFile = repo.getFileNode(file);
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
74 if (!dataFile.exists()) {
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
75 throw new FileNotFoundException();
|
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>
diff
changeset
|
77 int revToExtract;
|
107
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>
diff
changeset
|
79 revToExtract = dataFile.getLocalRevision(revision);
|
107
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>
diff
changeset
|
81 revToExtract = localRevision;
|
107
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>
diff
changeset
|
83 dataFile.content(revToExtract, sink);
|
107
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
84 }
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
85 }
|