diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java index 720fdedef..6d0318c02 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java @@ -735,4 +735,29 @@ public class FileUtils { } return name; } + + /** + * Best-effort variation of {@link File#getCanonicalFile()} returning the + * input file if the file cannot be canonicalized instead of throwing + * {@link IOException}. + * + * @param file + * to be canonicalized; may be {@code null} + * @return canonicalized file, or the unchanged input file if + * canonicalization failed or if {@code file == null} + * @throws SecurityException + * if {@link File#getCanonicalFile()} throws one + * @since 4.2 + */ + public static File canonicalize(File file) { + if (file == null) { + return null; + } + try { + return file.getCanonicalFile(); + } catch (IOException e) { + return file; + } + } + }