diff options
author | Gunnar Wagenknecht <gunnar@wagenknecht.org> | 2019-02-26 14:46:51 -0500 |
---|---|---|
committer | Gerrit Code Review @ Eclipse.org <gerrit@eclipse.org> | 2019-02-26 14:46:51 -0500 |
commit | 98df7ec801a50a3900d1347d7e8c3bd7080bb220 (patch) | |
tree | 88a903909158a34cfd50a23198fab86971d2466d | |
parent | 20b633d95d50fcc803aff2bd3ae7213771fa5e77 (diff) | |
parent | ca011107b864d87c0f0fa7a93450ea489c009686 (diff) | |
download | jgit-98df7ec801a50a3900d1347d7e8c3bd7080bb220.tar.gz jgit-98df7ec801a50a3900d1347d7e8c3bd7080bb220.zip |
Merge "On Windows use %APPDATA%\gnupg as GPG directory if it exists"
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/lib/internal/BouncyCastleGpgKeyLocator.java | 45 |
1 files changed, 39 insertions, 6 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/internal/BouncyCastleGpgKeyLocator.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/internal/BouncyCastleGpgKeyLocator.java index c7cbe360c2..091667db01 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/internal/BouncyCastleGpgKeyLocator.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/internal/BouncyCastleGpgKeyLocator.java @@ -46,10 +46,12 @@ import static java.nio.file.Files.exists; import static java.nio.file.Files.newInputStream; import java.io.BufferedInputStream; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URISyntaxException; import java.nio.file.Files; +import java.nio.file.InvalidPathException; import java.nio.file.Path; import java.nio.file.Paths; import java.text.MessageFormat; @@ -81,6 +83,8 @@ import org.eclipse.jgit.annotations.NonNull; import org.eclipse.jgit.api.errors.CanceledException; import org.eclipse.jgit.errors.UnsupportedCredentialItem; import org.eclipse.jgit.internal.JGitText; +import org.eclipse.jgit.util.FS; +import org.eclipse.jgit.util.SystemReader; /** * Locates GPG keys from either <code>~/.gnupg/private-keys-v1.d</code> or @@ -88,19 +92,48 @@ import org.eclipse.jgit.internal.JGitText; */ class BouncyCastleGpgKeyLocator { - private static final Path USER_KEYBOX_PATH = Paths - .get(System.getProperty("user.home"), ".gnupg", "pubring.kbx"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + private static final Path GPG_DIRECTORY = findGpgDirectory(); - private static final Path USER_SECRET_KEY_DIR = Paths.get( - System.getProperty("user.home"), ".gnupg", "private-keys-v1.d"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + private static final Path USER_KEYBOX_PATH = GPG_DIRECTORY + .resolve("pubring.kbx"); //$NON-NLS-1$ - private static final Path USER_PGP_LEGACY_SECRING_FILE = Paths - .get(System.getProperty("user.home"), ".gnupg", "secring.gpg"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + private static final Path USER_SECRET_KEY_DIR = GPG_DIRECTORY + .resolve("private-keys-v1.d"); //$NON-NLS-1$ + + private static final Path USER_PGP_LEGACY_SECRING_FILE = GPG_DIRECTORY + .resolve("secring.gpg"); //$NON-NLS-1$ private final String signingKey; private BouncyCastleGpgKeyPassphrasePrompt passphrasePrompt; + private static Path findGpgDirectory() { + SystemReader system = SystemReader.getInstance(); + if (system.isWindows()) { + // On Windows prefer %APPDATA%\gnupg if it exists, even if Cygwin is + // used. + String appData = system.getenv("APPDATA"); //$NON-NLS-1$ + if (appData != null && !appData.isEmpty()) { + try { + Path directory = Paths.get(appData).resolve("gnupg"); //$NON-NLS-1$ + if (Files.isDirectory(directory)) { + return directory; + } + } catch (SecurityException | InvalidPathException e) { + // Ignore and return the default location below. + } + } + } + // All systems, including Cygwin and even Windows if + // %APPDATA%\gnupg doesn't exist: ~/.gnupg + File home = FS.DETECTED.userHome(); + if (home == null) { + // Oops. What now? + home = new File(".").getAbsoluteFile(); //$NON-NLS-1$ + } + return home.toPath().resolve(".gnupg"); //$NON-NLS-1$ + } + /** * Create a new key locator for the specified signing key. * <p> |