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 import java.io.IOException;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
24 import java.io.OutputStream;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
25
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
26 import org.tmatesoft.hg.repo.HgDataFile;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
27 import org.tmatesoft.hg.repo.HgRepository;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
28
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
29 /**
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
30 * Command to obtain content of a
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
31 *
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
32 * @author Artem Tikhomirov
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
33 * @author TMate Software Ltd.
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
34 */
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
35 public class CatCommand {
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
36
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
37 private final HgRepository repo;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
38 private Path file;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
39 private int localRevision = TIP;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
40 private Nodeid revision;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
41
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
42 public CatCommand(HgRepository hgRepo) {
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
43 repo = hgRepo;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
44 }
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
45
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
46 public CatCommand file(Path fname) {
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
47 file = fname;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
48 return this;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
49 }
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
50
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
51 // rev can't be WORKING_COPY (if allowed, need to implement in #execute())
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
52 public CatCommand revision(int rev) {
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
53 localRevision = rev;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
54 revision = null;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
55 return this;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
56 }
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
57
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
58 public CatCommand revision(Nodeid nodeid) {
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
59 revision = nodeid;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
60 localRevision = BAD_REVISION;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
61 return this;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
62 }
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
63
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
64 public void execute(OutputStream os) throws IOException /*TODO own exception type*/ {
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
65 if (localRevision == BAD_REVISION && revision == null) {
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
66 throw new IllegalArgumentException("Either local file revision number or nodeid shall be specified");
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
67 }
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
68 if (file == null) {
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
69 throw new IllegalArgumentException("Name of the file is missing");
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
70 }
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
71 if (os == null) {
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
72 throw new IllegalArgumentException();
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
73 }
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
74 HgDataFile dataFile = repo.getFileNode(file);
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
75 if (!dataFile.exists()) {
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
76 throw new FileNotFoundException();
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
77 }
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
78 byte[] content;
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
79 if (revision != null) {
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
80 content = dataFile.content(revision);
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
81 } else {
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
82 content = dataFile.content(localRevision);
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
83 }
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
84 os.write(content);
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
85 }
|
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
86 }
|