Normalize paths on OS X

Java normalizes paths to NFC, but other source may not, e.g Eclipse.

Bug: 413390
Change-Id: I08649ac58c9b3cb8bf12794703e4137b1b4e94d5
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
This commit is contained in:
Robin Rosenberg 2013-07-11 01:11:12 +02:00 committed by Matthias Sohn
parent b434241e73
commit 5a51779e84
5 changed files with 66 additions and 7 deletions

View File

@ -156,4 +156,20 @@ public void createSymLink(File path, String target) throws IOException {
public Attributes getAttributes(File path) {
return FileUtil.getFileAttributesPosix(this, path);
}
/**
* @since 3.3
*/
@Override
public File normalize(File file) {
return FileUtil.normalize(file);
}
/**
* @since 3.3
*/
@Override
public String normalize(String name) {
return FileUtil.normalize(name);
}
}

View File

@ -222,4 +222,24 @@ static Attributes getFileAttributesPosix(FS fs, File path) {
}
}
public static File normalize(File file) {
if (SystemReader.getInstance().isMacOS()) {
// TODO: Would it be faster to check with isNormalized first
// assuming normalized paths are much more common
String normalized = Normalizer.normalize(file.getPath(),
Normalizer.Form.NFC);
return new File(normalized);
}
return file;
}
public static String normalize(String name) {
if (SystemReader.getInstance().isMacOS()) {
if (name == null)
return null;
return Normalizer.normalize(name, Normalizer.Form.NFC);
}
return name;
}
}

View File

@ -163,8 +163,9 @@ static public class FileEntry extends Entry {
* @param fs
* file system
*/
public FileEntry(final File f, FS fs) {
public FileEntry(File f, FS fs) {
this.fs = fs;
f = fs.normalize(f);
attributes = fs.getAttributes(f);
if (attributes.isSymbolicLink())
mode = FileMode.SYMLINK;

View File

@ -921,8 +921,8 @@ private boolean contentCheck(DirCacheEntry entry, ObjectReader reader)
return false;
} else {
if (mode == FileMode.SYMLINK.getBits())
return !new File(readContentAsString(current()))
.equals(new File(readContentAsString(entry)));
return !new File(readContentAsNormalizedString(current()))
.equals(new File((readContentAsNormalizedString(entry))));
// Content differs: that's a real change, perhaps
if (reader == null) // deprecated use, do no further checks
return true;
@ -971,19 +971,19 @@ private boolean contentCheck(DirCacheEntry entry, ObjectReader reader)
}
}
private String readContentAsString(DirCacheEntry entry)
private String readContentAsNormalizedString(DirCacheEntry entry)
throws MissingObjectException, IOException {
ObjectLoader open = repository.open(entry.getObjectId());
byte[] cachedBytes = open.getCachedBytes();
return RawParseUtils.decode(cachedBytes);
return FS.detect().normalize(RawParseUtils.decode(cachedBytes));
}
private static String readContentAsString(Entry entry) throws IOException {
private static String readContentAsNormalizedString(Entry entry) throws IOException {
long length = entry.getLength();
byte[] content = new byte[(int) length];
InputStream is = entry.openInputStream();
IO.readFully(is, content, 0, (int) length);
return RawParseUtils.decode(content);
return FS.detect().normalize(RawParseUtils.decode(content));
}
private long computeLength(InputStream in) throws IOException {

View File

@ -780,4 +780,26 @@ public Attributes getAttributes(File path) {
return new Attributes(this, path, exists, isDirectory, canExecute,
isSymlink, isFile, createTime, lastModified, -1);
}
/**
* Normalize the unicode path to composed form.
*
* @param file
* @return NFC-format File
* @since 3.3
*/
public File normalize(File file) {
return file;
}
/**
* Normalize the unicode path to composed form.
*
* @param name
* @return NFC-format string
* @since 3.3
*/
public String normalize(String name) {
return name;
}
}