changeset 300:650b45d290b1

Share range check code
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Sat, 17 Sep 2011 13:41:04 +0200
parents 45dc79e545f5
children 88c58edc0857
files src/org/tmatesoft/hg/internal/RevlogStream.java src/org/tmatesoft/hg/repo/HgDataFile.java src/org/tmatesoft/hg/repo/HgInternals.java src/org/tmatesoft/hg/repo/HgManifest.java
diffstat 4 files changed, 26 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/org/tmatesoft/hg/internal/RevlogStream.java	Sat Sep 17 13:26:52 2011 +0200
+++ b/src/org/tmatesoft/hg/internal/RevlogStream.java	Sat Sep 17 13:41:04 2011 +0200
@@ -27,6 +27,7 @@
 
 import org.tmatesoft.hg.core.HgBadStateException;
 import org.tmatesoft.hg.core.Nodeid;
+import org.tmatesoft.hg.repo.HgInternals;
 import org.tmatesoft.hg.repo.HgRepository;
 
 
@@ -204,15 +205,7 @@
 		if (start == TIP) {
 			start = indexSize - 1;
 		}
-		if (start < 0 || start >= indexSize) {
-			throw new IllegalArgumentException(String.format("Bad left range boundary %d in [0..%d]", start, indexSize-1));
-		}
-		if (end >= indexSize) {
-			throw new IllegalArgumentException(String.format("Bad right range boundary %d in [0..%d]", end, indexSize-1));
-		}
-		if (end < start) {
-			throw new IllegalArgumentException(String.format("Bad range [%d..%d]", start, end));
-		}
+		HgInternals.checkRevlogRange(start, end, indexSize-1);
 		// XXX may cache [start .. end] from index with a single read (pre-read)
 		
 		ReaderN1 r = new ReaderN1(needData, inspector);
--- a/src/org/tmatesoft/hg/repo/HgDataFile.java	Sat Sep 17 13:26:52 2011 +0200
+++ b/src/org/tmatesoft/hg/repo/HgDataFile.java	Sat Sep 17 13:41:04 2011 +0200
@@ -234,14 +234,14 @@
 			throw new IllegalStateException("Can't get history of invalid repository file node"); 
 		}
 		final int last = getLastRevision();
-		if (start < 0 || start > last) {
-			throw new IllegalArgumentException();
-		}
 		if (end == TIP) {
 			end = last;
-		} else if (end < start || end > last) {
-			throw new IllegalArgumentException();
 		}
+		if (start == TIP) {
+			start = last;
+		}
+		HgInternals.checkRevlogRange(start, end, last);
+
 		final int[] commitRevisions = new int[end - start + 1];
 		final boolean[] needsSorting = { false };
 		RevlogStream.Inspector insp = new RevlogStream.Inspector() {
--- a/src/org/tmatesoft/hg/repo/HgInternals.java	Sat Sep 17 13:26:52 2011 +0200
+++ b/src/org/tmatesoft/hg/repo/HgInternals.java	Sat Sep 17 13:41:04 2011 +0200
@@ -152,4 +152,17 @@
 	public static boolean wrongLocalRevision(int rev) {
 		return rev < 0 && rev != TIP && rev != WORKING_COPY && rev != BAD_REVISION; 
 	}
+
+	// throws IllegalArgumentException if [start..end] range is not a subrange of [0..lastRevision]
+	public static void checkRevlogRange(int start, int end, int lastRevision) {
+		if (start < 0 || start > lastRevision) {
+			throw new IllegalArgumentException(String.format("Bad left range boundary %d in [0..%d]", start, lastRevision));
+		}
+		if (end < 0 || end > lastRevision) {
+			throw new IllegalArgumentException(String.format("Bad right range boundary %d in [0..%d]", end, lastRevision));
+		}
+		if (end < start) {
+			throw new IllegalArgumentException(String.format("Bad range [%d..%d]", start, end));
+		}
+	}
 }
--- a/src/org/tmatesoft/hg/repo/HgManifest.java	Sat Sep 17 13:26:52 2011 +0200
+++ b/src/org/tmatesoft/hg/repo/HgManifest.java	Sat Sep 17 13:41:04 2011 +0200
@@ -101,6 +101,12 @@
 		}
 		int start0 = fromChangelog(start);
 		int end0 = fromChangelog(end);
+		if (end0 < start0) {
+			// there are tool-constructed repositories that got order of changeset revisions completely different from that of manifest
+			int x = end0;
+			end0 = start0;
+			start0 = x;
+		}
 		content.iterate(start0, end0, true, new ManifestParser(inspector));
 	}