Merge "SystemReader: Add support for XDG_CACHE_HOME"
diff --git a/org.eclipse.jgit/resources/org/eclipse/jgit/internal/JGitText.properties b/org.eclipse.jgit/resources/org/eclipse/jgit/internal/JGitText.properties
index 27270a1..bbec0fd 100644
--- a/org.eclipse.jgit/resources/org/eclipse/jgit/internal/JGitText.properties
+++ b/org.eclipse.jgit/resources/org/eclipse/jgit/internal/JGitText.properties
@@ -496,6 +496,7 @@
 logLargerFiletimeDiff={}: inconsistent duration from file timestamps on {}, {}: diff = {} > {} (last good value). Aborting measurement.
 logSmallerFiletime={}: got smaller file timestamp on {}, {}: {} < {}. Aborting measurement at resolution {}.
 logXDGConfigHomeInvalid=Environment variable XDG_CONFIG_HOME contains an invalid path {}
+logXDGCacheHomeInvalid=Environment variable XDG_CACHE_HOME contains an invalid path {}
 looseObjectHandleIsStale=loose-object {0} file handle is stale. retry {1} of {2}
 maxCountMustBeNonNegative=max count must be >= 0
 mergeConflictOnNonNoteEntries=Merge conflict on non-note entries: base = {0}, ours = {1}, theirs = {2}
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java
index bf252f9..bbdadc1 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java
@@ -526,6 +526,8 @@ public static JGitText get() {
 	/***/ public String logLargerFiletimeDiff;
 	/***/ public String logSmallerFiletime;
 	/***/ public String logXDGConfigHomeInvalid;
+
+	/***/ public String logXDGCacheHomeInvalid;
 	/***/ public String looseObjectHandleIsStale;
 	/***/ public String maxCountMustBeNonNegative;
 	/***/ public String mergeConflictOnNonNoteEntries;
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Constants.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Constants.java
index 997f4ed..9de8392 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Constants.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Constants.java
@@ -345,6 +345,15 @@ public final class Constants {
 	public static final String XDG_CONFIG_HOME = "XDG_CONFIG_HOME";
 
 	/**
+	 * The key of the XDG_CACHE_HOME directory defined in the
+	 * <a href="https://wiki.archlinux.org/index.php/XDG_Base_Directory">
+	 * XDG Base Directory specification</a>.
+	 *
+	 * @since 7.3
+	 */
+	public static final String XDG_CACHE_HOME = "XDG_CACHE_HOME";
+
+	/**
 	 * The environment variable that limits how close to the root of the file
 	 * systems JGit will traverse when looking for a repository root.
 	 */
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/SystemReader.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/SystemReader.java
index 22b82b3..0b7c620 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/util/SystemReader.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/SystemReader.java
@@ -492,6 +492,36 @@ public Path getXdgConfigDirectory(FS fileSystem) {
 	}
 
 	/**
+	 * Gets the directory denoted by environment variable XDG_CACHE_HOME. If
+	 * the variable is not set or empty, return a path for
+	 * {@code $HOME/.cache}.
+	 *
+	 * @param fileSystem
+	 *            {@link FS} to get the user's home directory
+	 * @return a {@link Path} denoting the directory, which may exist or not, or
+	 *         {@code null} if the environment variable is not set and there is
+	 *         no home directory, or the path is invalid.
+	 * @since 7.3
+	 */
+	public Path getXdgCacheDirectory(FS fileSystem) {
+		String cacheHomePath = getenv(Constants.XDG_CACHE_HOME);
+		if (StringUtils.isEmptyOrNull(cacheHomePath)) {
+			File home = fileSystem.userHome();
+			if (home == null) {
+				return null;
+			}
+			cacheHomePath = new File(home, ".cache").getAbsolutePath(); //$NON-NLS-1$
+		}
+		try {
+			return Paths.get(cacheHomePath);
+		} catch (InvalidPathException e) {
+			LOG.error(JGitText.get().logXDGCacheHomeInvalid, cacheHomePath,
+					e);
+		}
+		return null;
+	}
+
+	/**
 	 * Update config and its parents if they seem modified
 	 *
 	 * @param config