changeset 346:6d2c6b2469fc

Issue 18: Invalid storage path for certain long names
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 22 Nov 2011 04:02:37 +0100
parents 58725dd511b3
children 8da7ade36c57
files src/org/tmatesoft/hg/internal/StoragePathHelper.java test/org/tmatesoft/hg/test/TestStorePath.java
diffstat 2 files changed, 27 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/org/tmatesoft/hg/internal/StoragePathHelper.java	Tue Nov 22 03:14:40 2011 +0100
+++ b/src/org/tmatesoft/hg/internal/StoragePathHelper.java	Tue Nov 22 04:02:37 2011 +0100
@@ -141,7 +141,16 @@
 				int left = MAX_PATH_LEN - sb2.length() - 40 /*digest.length()*/ - STR_DH.length() - ".i".length();
 				assert left >= 0;
 				fnameStart++; // move from / to actual name
-				sb2.append(sb, fnameStart, fnameStart + left > sb.length() ? sb.length() : fnameStart+left);
+				if (fnameStart + left > sb.length()) {
+					// there left less chars in the mangled name that we can fit
+					sb2.append(sb, fnameStart, sb.length());
+					int stillAvailable = (fnameStart+left) - sb.length();
+					// stillAvailable > 0;
+					sb2.append(".i", 0, stillAvailable > 2 ? 2 : stillAvailable);
+				} else {
+					// add as much as we can
+					sb2.append(sb, fnameStart, fnameStart+left);
+				}
 				completeHashName.append(sb2);
 			}
 			completeHashName.append(digest);
--- a/test/org/tmatesoft/hg/test/TestStorePath.java	Tue Nov 22 03:14:40 2011 +0100
+++ b/test/org/tmatesoft/hg/test/TestStorePath.java	Tue Nov 22 04:02:37 2011 +0100
@@ -70,4 +70,21 @@
 		errorCollector.checkThat(storePathHelper.rewrite(n2), CoreMatchers.<CharSequence>equalTo(r2));
 		errorCollector.checkThat(storePathHelper.rewrite(n3), CoreMatchers.<CharSequence>equalTo(r3));
 	}
+
+	@Test
+	public void testIndexFileExtensionIsPartOfTheName() {
+		// with "data/" and ".i" 121 chars
+		String n1 = "src/jgit/main/org.eclipse.jgit.packaging/org.eclipse.jgit.junit.feature/.settings/org.eclipse.core.resources.prefs";
+		String r1 = "store/dh/src/jgit/main/org.ecli/org.ecli/~2esetti/org.eclipse.core.resources.prefs.ie1f4f9eed1009d220cd5afa6e01e7d9a06c02201.i";
+		// with "data/" and ".i" 122 chars
+		String n2 = "src/jgit/main/org.eclipse.jgit.packaging/org.eclipse.jgit.source.feature/.settings/org.eclipse.core.resources.prefs";
+		String r2 = "store/dh/src/jgit/main/org.ecli/org.ecli/~2esetti/org.eclipse.core.resources.prefs.i5193ab724f0225178fa949738444c4aac05e5e00.i";
+		//
+		// with "data/" and ".i" just 118 chars, use as sanity that it's not mangled
+		String n3 = "src/jgit/main/org.eclipse.jgit.packaging/org.eclipse.jgit.updatesite/.settings/org.eclipse.core.resources.prefs";
+		String r3 = "store/data/src/jgit/main/org.eclipse.jgit.packaging/org.eclipse.jgit.updatesite/~2esettings/org.eclipse.core.resources.prefs.i";
+		errorCollector.checkThat(storePathHelper.rewrite(n1), CoreMatchers.<CharSequence>equalTo(r1));
+		errorCollector.checkThat(storePathHelper.rewrite(n2), CoreMatchers.<CharSequence>equalTo(r2));
+		errorCollector.checkThat(storePathHelper.rewrite(n3), CoreMatchers.<CharSequence>equalTo(r3));
+	}
 }