|
1 | 1 | /*
|
2 |
| - * Copyright 2020-2023 DiffPlug |
| 2 | + * Copyright 2020-2025 DiffPlug |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 | package com.diffplug.gradle.spotless;
|
17 | 17 |
|
18 | 18 | import java.io.File;
|
| 19 | +import java.io.IOException; |
| 20 | +import java.util.concurrent.atomic.AtomicReference; |
19 | 21 |
|
20 | 22 | import javax.annotation.Nullable;
|
21 | 23 |
|
| 24 | +import org.eclipse.jgit.errors.ConfigInvalidException; |
22 | 25 | import org.eclipse.jgit.lib.Config;
|
| 26 | +import org.eclipse.jgit.lib.StoredConfig; |
23 | 27 | import org.eclipse.jgit.storage.file.FileBasedConfig;
|
24 | 28 | import org.eclipse.jgit.util.FS;
|
25 | 29 | import org.eclipse.jgit.util.SystemReader;
|
|
28 | 32 |
|
29 | 33 | /** Gradle implementation of GitRatchet. */
|
30 | 34 | public class GitRatchetGradle extends GitRatchet<File> {
|
| 35 | + private static final String[] GIT_EXEC_CANDIDATES = {"git", "git.exe", "git.cmd"}; |
| 36 | + |
31 | 37 | static {
|
32 |
| - preventJGitFromCallingExecutables(); |
| 38 | + GitRatchetGradle.redirectJGitExecutions(); |
33 | 39 | }
|
34 | 40 |
|
35 |
| - static void preventJGitFromCallingExecutables() { |
36 |
| - SystemReader reader = SystemReader.getInstance(); |
37 |
| - SystemReader.setInstance(new DelegatingSystemReader(reader) { |
| 41 | + static void redirectJGitExecutions() { |
| 42 | + SystemReader existing = SystemReader.getInstance(); |
| 43 | + SystemReader.setInstance(new DelegatingSystemReader(existing) { |
| 44 | + private AtomicReference<FileBasedConfig> systemConfig = new AtomicReference<>(); |
| 45 | + |
| 46 | + @Override |
| 47 | + public StoredConfig getSystemConfig() throws ConfigInvalidException, IOException { |
| 48 | + FileBasedConfig c = systemConfig.get(); |
| 49 | + if (c == null) { |
| 50 | + systemConfig.compareAndSet(null, |
| 51 | + this.openSystemConfig(this.getJGitConfig(), FS.DETECTED)); |
| 52 | + c = systemConfig.get(); |
| 53 | + } |
| 54 | + updateAll(c); |
| 55 | + return c; |
| 56 | + } |
| 57 | + |
| 58 | + // lifted from SystemReader since it's private |
| 59 | + private void updateAll(Config config) throws ConfigInvalidException, IOException { |
| 60 | + if (config == null) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + updateAll(config.getBaseConfig()); |
| 65 | + if (config instanceof FileBasedConfig) { |
| 66 | + FileBasedConfig cfg = (FileBasedConfig) config; |
| 67 | + if (cfg.isOutdated()) { |
| 68 | + cfg.load(); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
38 | 73 | @Override
|
39 |
| - public String getenv(String variable) { |
40 |
| - if ("PATH".equals(variable)) { |
41 |
| - return ""; |
42 |
| - } else { |
43 |
| - return super.getenv(variable); |
| 74 | + public FileBasedConfig openSystemConfig(final Config parent, final FS fs) { |
| 75 | + // cgit logic: https://git.kernel.org/pub/scm/git/git.git/tree/config.c#n1973 - in git_system_config() |
| 76 | + // They check the GIT_CONFIG_SYSTEM env var first, then follow up with logic based on compile-time parameters |
| 77 | + // We can't replicate this exactly so we'll do the closest approximation that Gradle will allow. |
| 78 | + final String systemPath = this.getenv("GIT_CONFIG_SYSTEM"); |
| 79 | + if (systemPath != null) { |
| 80 | + fs.setGitSystemConfig(new File(systemPath).getAbsoluteFile()); |
| 81 | + return super.openSystemConfig(parent, fs); |
| 82 | + } |
| 83 | + |
| 84 | + // match FS.searchPath |
| 85 | + File gitExec = null; |
| 86 | + final String path = this.getenv("PATH"); |
| 87 | + if (path != null) { |
| 88 | + outer: for (final String p : path.split(File.pathSeparator)) { |
| 89 | + for (final String name : GIT_EXEC_CANDIDATES) { |
| 90 | + final File candidate = new File(p, name); |
| 91 | + if (candidate.isFile() && candidate.canExecute()) { |
| 92 | + gitExec = candidate.getAbsoluteFile(); |
| 93 | + break outer; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // Guess at common locations |
| 100 | + if (gitExec != null) { |
| 101 | + // If git exec is at <prefix>/bin/git, this returns <prefix> |
| 102 | + File prefix = gitExec.getParentFile().getParentFile(); |
| 103 | + |
| 104 | + // Then we try to resolve a config |
| 105 | + final File systemConfig = new File(prefix, "etc/gitconfig"); |
| 106 | + if (systemConfig.exists()) { |
| 107 | + fs.setGitSystemConfig(systemConfig); |
| 108 | + return super.openSystemConfig(parent, fs); |
| 109 | + } |
44 | 110 | }
|
| 111 | + |
| 112 | + // Fallback to the non-prefixed path (this is not the logic that cgit uses, but oh well) |
| 113 | + fs.setGitSystemConfig(new File("/etc/gitconfig")); |
| 114 | + return super.openSystemConfig(parent, fs); |
45 | 115 | }
|
46 | 116 | });
|
47 | 117 | }
|
@@ -70,7 +140,7 @@ public String getHostname() {
|
70 | 140 |
|
71 | 141 | @Override
|
72 | 142 | public String getenv(String variable) {
|
73 |
| - return reader.getProperty(variable); |
| 143 | + return reader.getenv(variable); |
74 | 144 | }
|
75 | 145 |
|
76 | 146 | @Override
|
|
0 commit comments