comparison src/org/tmatesoft/hg/core/HgFileInformer.java @ 248:3fbfce107f94

Issue 8: Means to find out information about given file at specific changeset. Inner ManifestRevisionInspector got promoted to ManifestRevision
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 12 Aug 2011 18:48:57 +0200
parents
children 8c951645bea0
comparison
equal deleted inserted replaced
247:f052f40839ec 248:3fbfce107f94
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
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.core;
18
19 import org.tmatesoft.hg.internal.ManifestRevision;
20 import org.tmatesoft.hg.repo.HgDataFile;
21 import org.tmatesoft.hg.repo.HgRepository;
22 import org.tmatesoft.hg.util.Path;
23
24 /**
25 * Primary purpose is to provide information about file revisions at specific changeset. Multiple {@link #check(Path)} calls
26 * are possible once {@link #changeset(Nodeid)} (and optionally, {@link #followRenames(boolean)}) were set.
27 *
28 * @author Artem Tikhomirov
29 * @author TMate Software Ltd.
30 */
31 public class HgFileInformer {
32
33 private final HgRepository repo;
34 private boolean followRenames;
35 private Nodeid cset;
36 private ManifestRevision cachedManifest;
37 private HgFileRevision fileRevision;
38 private boolean checked, renamed;
39
40 public HgFileInformer(HgRepository hgRepo) {
41 repo = hgRepo;
42 }
43
44 public HgFileInformer changeset(Nodeid nid) {
45 if (nid == null || Nodeid.NULL.equals(nid)) {
46 throw new IllegalArgumentException();
47 }
48 cset = nid;
49 cachedManifest = null;
50 fileRevision = null;
51 return this;
52 }
53
54 public HgFileInformer followRenames(boolean follow) {
55 followRenames = follow;
56 fileRevision = null;
57 return this;
58 }
59
60 public boolean check(Path file) { // XXX IStatus instead of boolean?
61 fileRevision = null;
62 checked = false;
63 renamed = false;
64 if (cset == null || file == null || file.isDirectory()) {
65 throw new IllegalArgumentException();
66 }
67 HgDataFile dataFile = repo.getFileNode(file);
68 if (!dataFile.exists()) {
69 return false;
70 }
71 if (cachedManifest == null) {
72 int csetRev = repo.getChangelog().getLocalRevision(cset);
73 cachedManifest = new ManifestRevision(null, null); // XXX how about context and cached manifest revisions
74 repo.getManifest().walk(csetRev, csetRev, cachedManifest);
75 // cachedManifest shall be meaningful - changelog.getLocalRevision above ensures we've got version that exists.
76 }
77 Nodeid toExtract = cachedManifest.nodeid(file.toString());
78 try {
79 if (toExtract == null && followRenames) {
80 while (toExtract == null && dataFile.isCopy()) {
81 renamed = true;
82 file = dataFile.getCopySourceName();
83 dataFile = repo.getFileNode(file);
84 toExtract = cachedManifest.nodeid(file.toString());
85 }
86 }
87 } catch (HgDataStreamException ex) {
88 ex.printStackTrace(); // XXX log(INFO)
89 // ignore now, however if there's IStatus retval, might report error with reasonable explanation.
90 // Perhaps, may add a String reason() method with such info?
91 }
92 checked = true;
93 if (toExtract != null) {
94 fileRevision = new HgFileRevision(repo, toExtract, dataFile.getPath());
95 return true;
96 } // else String.format("File %s nor its origins were not known at repository %s revision", file, cset.shortNotation())
97 return false;
98 }
99
100 /**
101 * @return result of the last {@link #check(Path)} call.
102 */
103 public boolean exists() {
104 assertCheckRan();
105 return fileRevision != null;
106 }
107
108 /**
109 * @return <code>true</code> if checked file was known by another name at the time of specified changeset.
110 */
111 public boolean hasAnotherName() {
112 assertCheckRan();
113 return renamed;
114 }
115
116 /**
117 * @return holder for file revision information
118 */
119 public HgFileRevision getFileRevision() {
120 assertCheckRan();
121 return fileRevision;
122 }
123
124 /**
125 * Name of the checked file as it was known at the time of the specified changeset.
126 *
127 * @return handy shortcut for <code>getFileRevision().getPath()</code>
128 */
129 public Path filename() {
130 assertCheckRan();
131 return fileRevision.getPath();
132 }
133
134 /**
135 * Revision of the checked file
136 *
137 * @return handy shortcut for <code>getFileRevision().getRevision()</code>
138 */
139 public Nodeid revision() {
140 assertCheckRan();
141 return fileRevision.getRevision();
142 }
143
144 private void assertCheckRan() {
145 if (!checked) {
146 throw new HgBadStateException("Shall invoke #check(Path) first");
147 }
148 }
149 }