Skip to content

Commit dd93c12

Browse files
committed
specify username for all hg commands
this avoids interactive mode and errors due to no user being set fixes #4849
1 parent fe7a26c commit dd93c12

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/util/Executor.java

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.io.Reader;
3434
import java.lang.Thread.UncaughtExceptionHandler;
3535
import java.util.Arrays;
36+
import java.util.HashMap;
3637
import java.util.List;
3738
import java.util.Map;
3839
import java.util.Optional;
@@ -67,6 +68,7 @@ public class Executor {
6768
private byte[] stdout;
6869
private byte[] stderr;
6970
private int timeout; // in milliseconds, 0 means no timeout
71+
private final Map<String, String> environ;
7072

7173
/**
7274
* Create a new instance of the Executor.
@@ -88,38 +90,44 @@ public Executor(List<String> cmdList) {
8890
* Create a new instance of the Executor with default command timeout value.
8991
* The timeout value will be based on the running context (indexer or web application).
9092
* @param cmdList A list containing the command to execute
91-
* @param workingDirectory The directory the process should have as the
92-
* working directory
93+
* @param workingDirectory The directory the process should have as the working directory
9394
*/
9495
public Executor(List<String> cmdList, File workingDirectory) {
95-
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
96-
int timeoutSec = env.isIndexer() ? env.getIndexerCommandTimeout() : env.getInteractiveCommandTimeout();
97-
98-
this.cmdList = cmdList;
99-
this.workingDirectory = workingDirectory;
100-
this.timeout = timeoutSec * 1000;
96+
this(cmdList, workingDirectory, new HashMap<>());
10197
}
10298

10399
/**
104100
* Create a new instance of the Executor with specific timeout value.
105101
* @param cmdList A list containing the command to execute
106-
* @param workingDirectory The directory the process should have as the
107-
* working directory
102+
* @param workingDirectory The directory the process should have as the working directory
108103
* @param timeout If the command runs longer than the timeout (seconds),
109104
* it will be terminated. If the value is 0, no timer
110105
* will be set up.
111106
*/
112107
public Executor(List<String> cmdList, File workingDirectory, int timeout) {
108+
this(cmdList, workingDirectory, new HashMap<>());
109+
110+
this.timeout = timeout * 1000;
111+
}
112+
113+
/**
114+
* Create a new instance of the Executor with default command timeout value.
115+
* The timeout value will be based on the running context (indexer or web application).
116+
* @param cmdList A list containing the command to execute
117+
* @param workingDirectory The directory the process should have as the working directory
118+
*/
119+
public Executor(List<String> cmdList, File workingDirectory, Map<String, String> environ) {
113120
this.cmdList = cmdList;
114121
this.workingDirectory = workingDirectory;
115-
this.timeout = timeout * 1000;
122+
this.environ = environ;
123+
124+
setDefaultTimeout();
116125
}
117126

118127
/**
119128
* Create a new instance of the Executor with or without timeout.
120129
* @param cmdList A list containing the command to execute
121-
* @param workingDirectory The directory the process should have as the
122-
* working directory
130+
* @param workingDirectory The directory the process should have as the working directory
123131
* @param useTimeout terminate the process after default timeout or not
124132
*/
125133
public Executor(List<String> cmdList, File workingDirectory, boolean useTimeout) {
@@ -129,6 +137,13 @@ public Executor(List<String> cmdList, File workingDirectory, boolean useTimeout)
129137
}
130138
}
131139

140+
private void setDefaultTimeout() {
141+
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
142+
int timeoutSec = env.isIndexer() ? env.getIndexerCommandTimeout() : env.getInteractiveCommandTimeout();
143+
144+
this.timeout = timeoutSec * 1000;
145+
}
146+
132147
/**
133148
* Execute the command and collect the output. All exceptions will be
134149
* logged.
@@ -180,6 +195,8 @@ public int exec(final boolean reportExceptions, StreamHandler handler) {
180195
.map(File::toString)
181196
.orElseGet(() -> System.getProperty("user.dir"));
182197

198+
processBuilder.environment().putAll(environ);
199+
183200
String envStr = "";
184201
if (LOGGER.isLoggable(Level.FINER)) {
185202
Map<String, String> envMap = processBuilder.environment();

opengrok-indexer/src/test/java/org/opengrok/indexer/history/MercurialRepositoryTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@
5050
import java.util.ArrayList;
5151
import java.util.Arrays;
5252
import java.util.Collections;
53+
import java.util.HashMap;
5354
import java.util.List;
55+
import java.util.Map;
5456
import java.util.Objects;
5557
import java.util.Set;
5658
import java.util.TreeSet;
@@ -193,7 +195,9 @@ public static void runHgCommand(File reposRoot, String... args) {
193195
cmdargs.add(repo.getRepoCommand());
194196
cmdargs.addAll(Arrays.asList(args));
195197

196-
Executor exec = new Executor(cmdargs, reposRoot);
198+
Map<String, String> env = new HashMap<>();
199+
env.put("HGUSER", "foo <[email protected]>");
200+
Executor exec = new Executor(cmdargs, reposRoot, env);
197201
int exitCode = exec.exec();
198202
assertEquals(0, exitCode, "hg command '" + cmdargs + "' failed."
199203
+ "\nexit code: " + exitCode

0 commit comments

Comments
 (0)