changeset 17:571e1b2cc3f7

Query file for its parents. Demo of recently added ignore and digest support from within cat cmd
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Mon, 27 Dec 2010 01:43:08 +0100
parents 254078595653
children 02ee376bee79
files design.txt src/com/tmate/hgkit/console/Cat.java src/com/tmate/hgkit/console/Main.java src/com/tmate/hgkit/ll/DigestHelper.java src/com/tmate/hgkit/ll/HgDataFile.java
diffstat 5 files changed, 81 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/design.txt	Mon Dec 27 01:00:53 2010 +0100
+++ b/design.txt	Mon Dec 27 01:43:08 2010 +0100
@@ -32,6 +32,8 @@
 Changeset to get index (local revision number)
 .hgignored processing
 RevisionWalker (on manifest) and WorkingCopyWalker (io.File) talking to ? and/or dirstate 
+Revlog.Inspector to get nodeid array of meaningful data exact size (nor heading 00 bytes, nor 12 extra bytes from the spec) 
+
 
 
 ??? encodings of fncache, .hgignore, dirstate
--- a/src/com/tmate/hgkit/console/Cat.java	Mon Dec 27 01:00:53 2010 +0100
+++ b/src/com/tmate/hgkit/console/Cat.java	Mon Dec 27 01:43:08 2010 +0100
@@ -1,11 +1,14 @@
-/**
+/*
  * Copyright (c) 2010 Artem Tikhomirov 
  */
 package com.tmate.hgkit.console;
 
 import com.tmate.hgkit.fs.RepositoryLookup;
+import com.tmate.hgkit.ll.DigestHelper;
 import com.tmate.hgkit.ll.HgDataFile;
+import com.tmate.hgkit.ll.HgIgnore;
 import com.tmate.hgkit.ll.HgRepository;
+import com.tmate.hgkit.ll.LocalHgRepo;
 
 /**
  * @author artem
@@ -21,6 +24,11 @@
 			System.err.printf("Can't find repository in: %s\n", hgRepo.getLocation());
 			return;
 		}
+		HgIgnore ignore = ((LocalHgRepo) hgRepo).loadIgnore();
+		for (String s : new String[] {"design.txt", "src/com/tmate/hgkit/ll/Changelog.java", "src/Extras.java", "bin/com/tmate/hgkit/ll/Changelog.class"} ) {
+			System.out.println("Ignored " + s + ": " + ignore.isIgnored(s));
+		}
+		DigestHelper dh = new DigestHelper();
 		for (String fname : cmdLineOpts.files) {
 			System.out.println(fname);
 			HgDataFile fn = hgRepo.getFileNode(fname);
@@ -31,6 +39,11 @@
 					byte[] content = fn.content(i);
 					System.out.println("==========>");
 					System.out.println(new String(content));
+					int[] parentRevisions = new int[2];
+					byte[] parent1 = new byte[20];
+					byte[] parent2 = new byte[20];
+					fn.parents(i, parentRevisions, parent1, parent2);
+					System.out.println(dh.sha1(parent1, parent2, content));
 				}
 			} else {
 				System.out.println(">>>Not found!");
--- a/src/com/tmate/hgkit/console/Main.java	Mon Dec 27 01:00:53 2010 +0100
+++ b/src/com/tmate/hgkit/console/Main.java	Mon Dec 27 01:43:08 2010 +0100
@@ -18,8 +18,8 @@
 public class Main {
 
 	public static void main(String[] args) throws Exception {
-		String filename = "store/00changelog.i";
-		//String filename = "store/data/hello.c.i";
+//		String filename = "store/00changelog.i";
+		String filename = "store/data/hello.c.i";
 //		String filename = "store/data/docs/readme.i";
 		LinkedList<Changeset> changelog = new LinkedList<Changeset>();
 		//
--- a/src/com/tmate/hgkit/ll/DigestHelper.java	Mon Dec 27 01:00:53 2010 +0100
+++ b/src/com/tmate/hgkit/ll/DigestHelper.java	Mon Dec 27 01:43:08 2010 +0100
@@ -31,8 +31,10 @@
 	}
 
 	// XXX perhaps, digest functions should throw an exception, as it's caller responsibility to deal with eof, etc
-	public String sha1(byte[] data) {
+	public String sha1(byte[] nodeidParent1, byte[] nodeidParent2, byte[] data) {
 		MessageDigest alg = getSHA1();
+		alg.update(nodeidParent1);
+		alg.update(nodeidParent2);
 		byte[] digest = alg.digest(data);
 		assert digest.length == 20;
 		return toHexString(digest, 0, 20);
--- a/src/com/tmate/hgkit/ll/HgDataFile.java	Mon Dec 27 01:00:53 2010 +0100
+++ b/src/com/tmate/hgkit/ll/HgDataFile.java	Mon Dec 27 01:43:08 2010 +0100
@@ -1,10 +1,12 @@
-/**
+/*
  * Copyright (c) 2010 Artem Tikhomirov 
  */
 package com.tmate.hgkit.ll;
 
 import static com.tmate.hgkit.ll.HgRepository.TIP;
 
+import java.util.Arrays;
+
 /**
  * Extends Revlog/uses RevlogStream?
  * ? name:HgFileNode?
@@ -71,4 +73,61 @@
 	public void history(int start, int end, Changeset.Inspector i) {
 		throw HgRepository.notImplemented();
 	}
+
+	/**
+	 * XXX perhaps, return value Nodeid[2] and boolean needNodeids is better (and higher level) API for this query?
+	 * 
+	 * @param revision - revision to query parents, or {@link HgRepository#TIP}
+	 * @param parentRevisions - int[2] to get local revision numbers of parents (e.g. {6, -1})
+	 * @param parent1 - byte[20] or null, if parent's nodeid is not needed
+	 * @param parent2 - byte[20] or null, if second parent's nodeid is not needed
+	 * @return
+	 */
+	public void parents(int revision, int[] parentRevisions, byte[] parent1, byte[] parent2) {
+		if (revision != TIP && !(revision >= 0 && revision < content.revisionCount())) {
+			throw new IllegalArgumentException(String.valueOf(revision));
+		}
+		if (parentRevisions == null || parentRevisions.length < 2) {
+			throw new IllegalArgumentException(String.valueOf(parentRevisions));
+		}
+		if (parent1 != null && parent1.length < 20) {
+			throw new IllegalArgumentException(parent1.toString());
+		}
+		if (parent2 != null && parent2.length < 20) {
+			throw new IllegalArgumentException(parent2.toString());
+		}
+		class ParentCollector implements Revlog.Inspector {
+			public int p1 = -1;
+			public int p2 = -1;
+			public byte[] nodeid;
+			
+			public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, byte[] data) {
+				p1 = parent1Revision;
+				p2 = parent2Revision;
+				this.nodeid = new byte[20];
+				// nodeid arg now comes in 32 byte from (as in file format description), however upper 12 bytes are zeros.
+				System.arraycopy(nodeid, nodeid.length > 20 ? nodeid.length - 20 : 0, this.nodeid, 0, 20);
+			}
+		};
+		ParentCollector pc = new ParentCollector();
+		content.iterate(revision, revision, false, pc);
+		parentRevisions[0] = pc.p1;
+		parentRevisions[1] = pc.p2;
+		if (parent1 != null) {
+			if (parentRevisions[0] == -1) {
+				Arrays.fill(parent1, 0, 20, (byte) 0);
+			} else {
+				content.iterate(parentRevisions[0], parentRevisions[0], false, pc);
+				System.arraycopy(pc.nodeid, 0, parent1, 0, 20);
+			}
+		}
+		if (parent2 != null) {
+			if (parentRevisions[1] == -1) {
+				Arrays.fill(parent2, 0, 20, (byte) 0);
+			} else {
+				content.iterate(parentRevisions[1], parentRevisions[1], false, pc);
+				System.arraycopy(pc.nodeid, 0, parent2, 0, 20);
+			}
+		}
+	}
 }