changeset 320:678e326fd27c

Issue 15: Exception accessing oddly named file from history
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 30 Sep 2011 06:22:11 +0200
parents fa4aea41746e
children ac38e75c9e8e
files src/org/tmatesoft/hg/core/HgCallbackTargetException.java src/org/tmatesoft/hg/core/HgException.java src/org/tmatesoft/hg/internal/EncodingHelper.java src/org/tmatesoft/hg/repo/HgManifest.java src/org/tmatesoft/hg/repo/HgStatusCollector.java
diffstat 5 files changed, 63 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/org/tmatesoft/hg/core/HgCallbackTargetException.java	Thu Sep 29 04:37:52 2011 +0200
+++ b/src/org/tmatesoft/hg/core/HgCallbackTargetException.java	Fri Sep 30 06:22:11 2011 +0200
@@ -56,6 +56,9 @@
 	@Override
 	public String getMessage() {
 		StringBuilder sb = new StringBuilder();
+		sb.append("Original exception thrown: ");
+		sb.append(getCause().getClass().getName());
+		sb.append(" at ");
 		appendDetails(sb);
 		return sb.toString();
 	}
--- a/src/org/tmatesoft/hg/core/HgException.java	Thu Sep 29 04:37:52 2011 +0200
+++ b/src/org/tmatesoft/hg/core/HgException.java	Fri Sep 30 06:22:11 2011 +0200
@@ -84,10 +84,13 @@
 	
 	protected void appendDetails(StringBuilder sb) {
 		if (filename != null) {
+			sb.append("file:'");
 			sb.append(filename);
-			sb.append(':');
+			sb.append('\'');
+			sb.append(';');
 			sb.append(' ');
 		}
+		sb.append("rev:");
 		if (revNumber != BAD_REVISION) {
 			sb.append(revNumber);
 			if (revision != null) {
@@ -99,6 +102,15 @@
 		}
 	}
 
+	@Override
+	public String toString() {
+		StringBuilder sb = new StringBuilder(super.toString());
+		sb.append(' ');
+		sb.append('(');
+		appendDetails(sb);
+		sb.append(')');
+		return sb.toString();
+	}
 //	/* XXX CONSIDER capability to pass extra information about errors */
 //	public static class Status {
 //		public Status(String message, Throwable cause, int errorCode, Object extraData) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/tmatesoft/hg/internal/EncodingHelper.java	Fri Sep 30 06:22:11 2011 +0200
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2011 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
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * For information on how to redistribute this software under
+ * the terms of a license other than GNU General Public License
+ * contact TMate Software at support@hg4j.com
+ */
+package org.tmatesoft.hg.internal;
+
+import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
+
+import org.tmatesoft.hg.core.HgBadStateException;
+
+/**
+ * Keep all encoding-related issues in the single place
+ * @author Artem Tikhomirov
+ * @author TMate Software Ltd.
+ */
+public class EncodingHelper {
+	// XXX perhaps, shall not be full of statics, but rather an instance coming from e.g. HgRepository?
+
+	public static String fromManifest(byte[] data, int start, int length) {
+		try {
+			return new String(data, start, length, "ISO-8859-1");
+		} catch (UnsupportedEncodingException ex) {
+			// can't happen
+			throw new HgBadStateException(ex);
+		}
+	}
+}
--- a/src/org/tmatesoft/hg/repo/HgManifest.java	Thu Sep 29 04:37:52 2011 +0200
+++ b/src/org/tmatesoft/hg/repo/HgManifest.java	Fri Sep 30 06:22:11 2011 +0200
@@ -20,6 +20,7 @@
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.Arrays;
 
@@ -27,6 +28,7 @@
 import org.tmatesoft.hg.core.Nodeid;
 import org.tmatesoft.hg.internal.DataAccess;
 import org.tmatesoft.hg.internal.DigestHelper;
+import org.tmatesoft.hg.internal.EncodingHelper;
 import org.tmatesoft.hg.internal.Experimental;
 import org.tmatesoft.hg.internal.IterateControlMediator;
 import org.tmatesoft.hg.internal.Lifecycle;
@@ -268,7 +270,7 @@
 		
 		public Path freeze() {
 			if (result == null) {
-				result = Path.create(new String(data, start, length));
+				result = Path.create(EncodingHelper.fromManifest(data, start, length));
 				// release reference to bigger data array, make a copy of relevant part only
 				// use original bytes, not those from String above to avoid cache misses due to different encodings 
 				byte[] d = new byte[length];
--- a/src/org/tmatesoft/hg/repo/HgStatusCollector.java	Thu Sep 29 04:37:52 2011 +0200
+++ b/src/org/tmatesoft/hg/repo/HgStatusCollector.java	Fri Sep 30 06:22:11 2011 +0200
@@ -290,6 +290,10 @@
 	
 	/*package-local*/static Path getOriginIfCopy(HgRepository hgRepo, Path fname, Collection<Path> originals, int originalChangelogRevision) throws HgDataStreamException {
 		HgDataFile df = hgRepo.getFileNode(fname);
+		if (!df.exists()) {
+			String msg = String.format("Didn't find file '%s' in the repo. Perhaps, bad storage name conversion?", fname);
+			throw new HgDataStreamException(fname, msg, null).setRevisionNumber(originalChangelogRevision);
+		}
 		while (df.isCopy()) {
 			Path original = df.getCopySourceName();
 			if (originals.contains(original)) {