comparison src/org/tmatesoft/hg/repo/HgManifest.java @ 232:b7347daa50e3

Allow to cat a file with changeset revision
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 02 Jun 2011 05:13:39 +0200
parents 373e07cd3991
children a620f0663a37
comparison
equal deleted inserted replaced
231:1792b37650f2 232:b7347daa50e3
16 */ 16 */
17 package org.tmatesoft.hg.repo; 17 package org.tmatesoft.hg.repo;
18 18
19 import static org.tmatesoft.hg.repo.HgRepository.TIP; 19 import static org.tmatesoft.hg.repo.HgRepository.TIP;
20 20
21 import java.io.ByteArrayOutputStream;
21 import java.io.IOException; 22 import java.io.IOException;
22 import java.util.ArrayList; 23 import java.util.ArrayList;
23 import java.util.Arrays; 24 import java.util.Arrays;
24 25
25 import org.tmatesoft.hg.core.HgBadStateException; 26 import org.tmatesoft.hg.core.HgBadStateException;
26 import org.tmatesoft.hg.core.Nodeid; 27 import org.tmatesoft.hg.core.Nodeid;
27 import org.tmatesoft.hg.internal.DataAccess; 28 import org.tmatesoft.hg.internal.DataAccess;
29 import org.tmatesoft.hg.internal.Experimental;
28 import org.tmatesoft.hg.internal.Lifecycle; 30 import org.tmatesoft.hg.internal.Lifecycle;
29 import org.tmatesoft.hg.internal.Pool; 31 import org.tmatesoft.hg.internal.Pool;
30 import org.tmatesoft.hg.internal.RevlogStream; 32 import org.tmatesoft.hg.internal.RevlogStream;
33 import org.tmatesoft.hg.util.Path;
31 34
32 35
33 /** 36 /**
34 * 37 *
35 * @author Artem Tikhomirov 38 * @author Artem Tikhomirov
55 int start0 = fromChangelog(start); 58 int start0 = fromChangelog(start);
56 int end0 = fromChangelog(end); 59 int end0 = fromChangelog(end);
57 content.iterate(start0, end0, true, new ManifestParser(inspector)); 60 content.iterate(start0, end0, true, new ManifestParser(inspector));
58 } 61 }
59 62
63 // manifest revision number that corresponds to the given changeset
60 /*package-local*/ int fromChangelog(int revisionNumber) { 64 /*package-local*/ int fromChangelog(int revisionNumber) {
61 if (HgInternals.wrongLocalRevision(revisionNumber)) { 65 if (HgInternals.wrongLocalRevision(revisionNumber)) {
62 throw new IllegalArgumentException(String.valueOf(revisionNumber)); 66 throw new IllegalArgumentException(String.valueOf(revisionNumber));
63 } 67 }
64 if (revisionMap == null) { 68 if (revisionMap == null) {
66 content.iterate(0, TIP, false, revisionMap); 70 content.iterate(0, TIP, false, revisionMap);
67 } 71 }
68 return revisionMap.at(revisionNumber); 72 return revisionMap.at(revisionNumber);
69 } 73 }
70 74
75 /**
76 * Extracts file revision as it was known at the time of given changeset.
77 *
78 * @param revisionNumber local changeset index
79 * @param file path to file in question
80 * @return file revision or <code>null</code> if manifest at specified revision doesn't list such file
81 */
82 @Experimental(reason="Perhaps, HgDataFile shall own this method")
83 public Nodeid getFileRevision(int revisionNumber, final Path file) {
84 int rev = fromChangelog(revisionNumber);
85 final Nodeid[] rv = new Nodeid[] { null };
86 content.iterate(rev, rev, true, new RevlogStream.Inspector() {
87
88 public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, DataAccess data) {
89 ByteArrayOutputStream bos = new ByteArrayOutputStream();
90 try {
91 byte b;
92 while (!data.isEmpty() && (b = data.readByte()) != '\n') {
93 if (b != 0) {
94 bos.write(b);
95 } else {
96 String fname = new String(bos.toByteArray());
97 bos.reset();
98 if (file.toString().equals(fname)) {
99 byte[] nid = new byte[40];
100 data.readBytes(nid, 0, 40);
101 rv[0] = Nodeid.fromAscii(nid, 0, 40);
102 break;
103 }
104 // else skip to the end of line
105 while (!data.isEmpty() && (b = data.readByte()) != '\n')
106 ;
107 }
108 }
109 } catch (IOException ex) {
110 throw new HgBadStateException(ex);
111 }
112 }
113 });
114 return rv[0];
115 }
116
71 public interface Inspector { 117 public interface Inspector {
72 boolean begin(int mainfestRevision, Nodeid nid, int changelogRevision); 118 boolean begin(int mainfestRevision, Nodeid nid, int changelogRevision);
73 boolean next(Nodeid nid, String fname, String flags); 119 boolean next(Nodeid nid, String fname, String flags);
74 boolean end(int manifestRevision); 120 boolean end(int manifestRevision);
75 } 121 }