]> source.dussan.org Git - jgit.git/commitdiff
Normalize paths on OS X 96/15396/21
authorRobin Rosenberg <robin.rosenberg@dewire.com>
Wed, 10 Jul 2013 23:11:12 +0000 (01:11 +0200)
committerMatthias Sohn <matthias.sohn@sap.com>
Wed, 12 Feb 2014 22:00:36 +0000 (23:00 +0100)
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>
org.eclipse.jgit.java7/src/org/eclipse/jgit/util/FS_POSIX_Java7.java
org.eclipse.jgit.java7/src/org/eclipse/jgit/util/FileUtil.java
org.eclipse.jgit/src/org/eclipse/jgit/treewalk/FileTreeIterator.java
org.eclipse.jgit/src/org/eclipse/jgit/treewalk/WorkingTreeIterator.java
org.eclipse.jgit/src/org/eclipse/jgit/util/FS.java

index e8307fc2b09bb45a5e0b0b6ce651de49dfc2a582..0879e8021f108542f984062434618fb9a8fe6400 100644 (file)
@@ -156,4 +156,20 @@ public class FS_POSIX_Java7 extends FS_POSIX {
        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);
+       }
 }
index 78dc2c3934ee49659bedf2d09f842c398e8e8dc7..c958494d04bea8d129aa27569d4e1e96a25a042a 100644 (file)
@@ -222,4 +222,24 @@ class FileUtil {
                }
        }
 
+       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;
+       }
+
 }
index 8dc8276b3fb44be7f8934d2e05563e9188206489..8d2cb1d8cdc6ce16feb13bc91cff00b87dfea859 100644 (file)
@@ -163,8 +163,9 @@ public class FileTreeIterator extends WorkingTreeIterator {
                 * @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;
index b57176b481cabd5929265d7f2ffb06cba2f2474e..633865ed4d38779673a4f268bf51def43bd39f50 100644 (file)
@@ -921,8 +921,8 @@ public abstract class WorkingTreeIterator extends AbstractTreeIterator {
                        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 @@ public abstract class WorkingTreeIterator extends AbstractTreeIterator {
                }
        }
 
-       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 {
index 9e964fc363d5279f95957d45f0b5afc73aecbfa4..dcece62894e9eb912b7f2c2223bc37402bcfd216 100644 (file)
@@ -780,4 +780,26 @@ public abstract class FS {
                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;
+       }
 }