changeset 535:d9c07e1432c4

Issue 42: tolerate lines in .hgtags that do not conform to its format specification
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 05 Feb 2013 15:54:37 +0100
parents 243202f1bda5
children 2813a26b8999
files src/org/tmatesoft/hg/core/Nodeid.java src/org/tmatesoft/hg/repo/HgTags.java
diffstat 2 files changed, 14 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/org/tmatesoft/hg/core/Nodeid.java	Mon Feb 04 18:00:55 2013 +0100
+++ b/src/org/tmatesoft/hg/core/Nodeid.java	Tue Feb 05 15:54:37 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010-2011 TMate Software Ltd
+ * Copyright (c) 2010-2013 TMate Software Ltd
  *  
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -166,7 +166,7 @@
 	 * @return object representation
 	 * @throws HgBadNodeidFormatException custom {@link IllegalArgumentException} subclass when argument doesn't match encoded form of 20-bytes sha1 digest. 
 	 */
-	public static Nodeid fromAscii(String asciiRepresentation) {
+	public static Nodeid fromAscii(String asciiRepresentation) throws HgBadNodeidFormatException {
 		if (asciiRepresentation.length() != 40) {
 			throw new HgBadNodeidFormatException(String.format("Bad value: %s", asciiRepresentation));
 		}
@@ -178,9 +178,9 @@
 	 * Parse encoded representation. Similar to {@link #fromAscii(String)}.
 	 * @throws HgBadNodeidFormatException custom {@link IllegalArgumentException} subclass when bytes are not hex digits or number of bytes != 40 (160 bits) 
 	 */
-	public static Nodeid fromAscii(byte[] asciiRepresentation, int offset, int length) {
+	public static Nodeid fromAscii(byte[] asciiRepresentation, int offset, int length) throws HgBadNodeidFormatException {
 		if (length != 40) {
-			throw new IllegalArgumentException();
+			throw new HgBadNodeidFormatException(String.format("Expected 40 hex characters for nodeid, not %d", length));
 		}
 		try {
 			byte[] data = new byte[20];
@@ -189,6 +189,8 @@
 				return NULL;
 			}
 			return new Nodeid(data, false);
+		} catch (HgBadNodeidFormatException ex) {
+			throw ex;
 		} catch (IllegalArgumentException ex) {
 			throw new HgBadNodeidFormatException(ex.getMessage());
 		}
--- a/src/org/tmatesoft/hg/repo/HgTags.java	Mon Feb 04 18:00:55 2013 +0100
+++ b/src/org/tmatesoft/hg/repo/HgTags.java	Tue Feb 05 15:54:37 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011-2012 TMate Software Ltd
+ * Copyright (c) 2011-2013 TMate Software Ltd
  *  
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -16,6 +16,7 @@
  */
 package org.tmatesoft.hg.repo;
 
+import static org.tmatesoft.hg.util.LogFacility.Severity.Error;
 import static org.tmatesoft.hg.util.LogFacility.Severity.Warn;
 
 import java.io.BufferedReader;
@@ -31,6 +32,7 @@
 import java.util.Map;
 import java.util.TreeMap;
 
+import org.tmatesoft.hg.core.HgBadNodeidFormatException;
 import org.tmatesoft.hg.core.Nodeid;
 
 /**
@@ -108,12 +110,12 @@
 			if (line.length() == 0) {
 				continue;
 			}
-			if (line.length() < 40+2 /*nodeid, space and at least single-char tagname*/) {
+			final int spacePos = line.indexOf(' ');
+			if (line.length() < 40+2 /*nodeid, space and at least single-char tagname*/ || spacePos != 40) {
 				repo.getSessionContext().getLog().dump(getClass(), Warn, "Bad tags line: %s", line); 
 				continue;
 			}
-			int spacePos = line.indexOf(' ');
-			if (spacePos != -1) {
+			try {
 				assert spacePos == 40;
 				final byte[] nodeidBytes = line.substring(0, spacePos).getBytes();
 				Nodeid nid = Nodeid.fromAscii(nodeidBytes, 0, nodeidBytes.length);
@@ -151,9 +153,8 @@
 					// !contains because we don't care about order of the tags per revision
 					revTags.add(tagName);
 				}
-				
-			} else {
-				repo.getSessionContext().getLog().dump(getClass(), Warn, "Bad tags line: %s", line);
+			} catch (HgBadNodeidFormatException ex) {
+				repo.getSessionContext().getLog().dump(getClass(), Error, "Bad revision '%s' in line '%s':%s", line.substring(0, spacePos), line, ex.getMessage()); 
 			}
 		}
 	}