From 8728bff1987cc7fc96a998dbd97e130a6570895e Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Sun, 27 Apr 2008 15:19:32 -0400 Subject: [PATCH] Use the user's Cygwin home directory if invoked by a Cygwin shell If we are running within a Cygwin based environment and the user has given us a HOME environment variable that points to their Cygwin home directory and they have given us cygpath.exe in PATH we should translate their Cygwin based HOME directory into a form that the Windows JRE can read, and use that rather than their own Windows home directory. This is based under the assumption that anyone using Cygwin with jgit is most likely using a Cygwin based Git implementation ad thus will want jgit to play alongside it as much as possible. Signed-off-by: Shawn O. Pearce --- .../src/org/spearce/jgit/lib/RepositoryConfig.java | 3 +- org.spearce.jgit/src/org/spearce/jgit/util/FS.java | 38 ++++++++++++++++++++++ .../src/org/spearce/jgit/util/FS_Win32_Cygwin.java | 13 ++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java b/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java index 0bb8dca5..c2ea2ab8 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java +++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java @@ -49,8 +49,7 @@ public class RepositoryConfig { * configuration file from their home directory. */ public static RepositoryConfig openUserConfig() { - return new RepositoryConfig(null, new File(System - .getProperty("user.home"), ".gitconfig")); + return new RepositoryConfig(null, new File(FS.userHome(), ".gitconfig")); } private final RepositoryConfig baseConfig; diff --git a/org.spearce.jgit/src/org/spearce/jgit/util/FS.java b/org.spearce.jgit/src/org/spearce/jgit/util/FS.java index 41cb7821..4c92beda 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/util/FS.java +++ b/org.spearce.jgit/src/org/spearce/jgit/util/FS.java @@ -17,6 +17,8 @@ package org.spearce.jgit.util; import java.io.File; +import java.security.AccessController; +import java.security.PrivilegedAction; /** Abstraction to support various file system operations not in Java. */ public abstract class FS { @@ -116,4 +118,40 @@ public abstract class FS { return abspn; return new File(dir, name); } + + /** + * Determine the user's home directory (location where preferences are). + *

+ * This method can be expensive on the first invocation if path name + * translation is required. Subsequent invocations return a cached result. + *

+ * Not all platforms and JREs require path name translation. Currently only + * Cygwin on Win32 requires translation of the Cygwin HOME directory. + * + * @return the user's home directory; null if the user does not have one. + */ + public static File userHome() { + return USER_HOME.home; + } + + private static class USER_HOME { + static final File home = INSTANCE.userHomeImpl(); + } + + /** + * Determine the user's home directory (location where preferences are). + * + * @return the user's home directory; null if the user does not have one. + */ + protected File userHomeImpl() { + final String home = AccessController + .doPrivileged(new PrivilegedAction() { + public String run() { + return System.getProperty("user.home"); + } + }); + if (home == null || home.length() == 0) + return null; + return new File(home).getAbsoluteFile(); + } } diff --git a/org.spearce.jgit/src/org/spearce/jgit/util/FS_Win32_Cygwin.java b/org.spearce.jgit/src/org/spearce/jgit/util/FS_Win32_Cygwin.java index d9b174ed..6f42e786 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/util/FS_Win32_Cygwin.java +++ b/org.spearce.jgit/src/org/spearce/jgit/util/FS_Win32_Cygwin.java @@ -78,4 +78,17 @@ class FS_Win32_Cygwin extends FS_Win32 { } return super.resolveImpl(dir, pn); } + + @Override + protected File userHomeImpl() { + final String home = AccessController + .doPrivileged(new PrivilegedAction() { + public String run() { + return System.getenv("HOME"); + } + }); + if (home == null || home.length() == 0) + return super.userHomeImpl(); + return resolveImpl(new File("."), home); + } } -- 2.11.4.GIT