comparison src/org/tmatesoft/hg/core/HgFileRevision.java @ 316:ee6b467c1a5f

Supply HGFileRevision with copy information when possible, calculate it otherwise
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 28 Sep 2011 13:09:16 +0200
parents 4c3b9f679412
children 58725dd511b3
comparison
equal deleted inserted replaced
315:8952f89be195 316:ee6b467c1a5f
15 * contact TMate Software at support@hg4j.com 15 * contact TMate Software at support@hg4j.com
16 */ 16 */
17 package org.tmatesoft.hg.core; 17 package org.tmatesoft.hg.core;
18 18
19 import org.tmatesoft.hg.repo.HgDataFile; 19 import org.tmatesoft.hg.repo.HgDataFile;
20 import org.tmatesoft.hg.repo.HgInternals;
20 import org.tmatesoft.hg.repo.HgRepository; 21 import org.tmatesoft.hg.repo.HgRepository;
21 import org.tmatesoft.hg.util.ByteChannel; 22 import org.tmatesoft.hg.util.ByteChannel;
22 import org.tmatesoft.hg.util.CancelledException; 23 import org.tmatesoft.hg.util.CancelledException;
23 import org.tmatesoft.hg.util.Path; 24 import org.tmatesoft.hg.util.Path;
24 25
30 */ 31 */
31 public final class HgFileRevision { 32 public final class HgFileRevision {
32 private final HgRepository repo; 33 private final HgRepository repo;
33 private final Nodeid revision; 34 private final Nodeid revision;
34 private final Path path; 35 private final Path path;
35 36 private Path origin;
37 private Boolean isCopy = null; // null means not yet known
38
36 public HgFileRevision(HgRepository hgRepo, Nodeid rev, Path p) { 39 public HgFileRevision(HgRepository hgRepo, Nodeid rev, Path p) {
37 if (hgRepo == null || rev == null || p == null) { 40 if (hgRepo == null || rev == null || p == null) {
38 // since it's package local, it is our code to blame for non validated arguments 41 // since it's package local, it is our code to blame for non validated arguments
39 throw new HgBadStateException(); 42 throw new IllegalArgumentException();
40 } 43 }
41 repo = hgRepo; 44 repo = hgRepo;
42 revision = rev; 45 revision = rev;
43 path = p; 46 path = p;
47 }
48
49 // this cons shall be used when we know whether p was a copy. Perhaps, shall pass Map<Path,Path> instead to stress orig argument is not optional
50 HgFileRevision(HgRepository hgRepo, Nodeid rev, Path p, Path orig) {
51 this(hgRepo, rev, p);
52 isCopy = Boolean.valueOf(orig == null);
53 origin = orig;
44 } 54 }
45 55
46 public Path getPath() { 56 public Path getPath() {
47 return path; 57 return path;
48 } 58 }
49 public Nodeid getRevision() { 59 public Nodeid getRevision() {
50 return revision; 60 return revision;
51 } 61 }
62 public boolean wasCopied() {
63 if (isCopy == null) {
64 checkCopy();
65 }
66 return isCopy.booleanValue();
67 }
68 /**
69 * @return <code>null</code> if {@link #wasCopied()} is <code>false</code>, name of the copy source otherwise.
70 */
71 public Path getOriginIfCopy() {
72 if (wasCopied()) {
73 return origin;
74 }
75 return null;
76 }
77
52 public void putContentTo(ByteChannel sink) throws HgDataStreamException, CancelledException { 78 public void putContentTo(ByteChannel sink) throws HgDataStreamException, CancelledException {
53 HgDataFile fn = repo.getFileNode(path); 79 HgDataFile fn = repo.getFileNode(path);
54 int localRevision = fn.getLocalRevision(revision); 80 int localRevision = fn.getLocalRevision(revision);
55 fn.contentWithFilters(localRevision, sink); 81 fn.contentWithFilters(localRevision, sink);
56 } 82 }
57 83
84 private void checkCopy() {
85 HgDataFile fn = repo.getFileNode(path);
86 try {
87 if (fn.isCopy()) {
88 if (fn.getRevision(0).equals(revision)) {
89 // this HgFileRevision represents first revision of the copy
90 isCopy = Boolean.TRUE;
91 origin = fn.getCopySourceName();
92 return;
93 }
94 }
95 } catch (HgDataStreamException ex) {
96 HgInternals.getContext(repo).getLog().error(getClass(), ex, null);
97 }
98 isCopy = Boolean.FALSE;
99 }
58 } 100 }