Skip to content

Commit

Permalink
#6: fix git hangs via non interactive invocation (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
hohwille authored Jan 16, 2024
1 parent 79f4e76 commit 48e9dad
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ public void gitPullOrClone(Path target, String gitRepoUrl) {
if (!gitRepoUrl.startsWith("http")) {
throw new IllegalArgumentException("Invalid git URL '" + gitRepoUrl + "'!");
}
ProcessContext pc = newProcess().directory(target).executable("git");
ProcessContext pc = newProcess().directory(target).executable("git").withEnvVar("GIT_TERMINAL_PROMPT", "0");
if (Files.isDirectory(target.resolve(".git"))) {
ProcessResult result = pc.addArg("remote").run(true);
List<String> remotes = result.getOut();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@ default ProcessContext addArgs(List<?>... args) {
return this;
}

/**
* Sets or overrides the specified environment variable only for the planned {@link #run() process execution}. Please
* note that the environment variables are initialized when the {@link ProcessContext} is created. This method
* explicitly set an additional or overrides an existing environment and will have effect for each {@link #run()
* process execution} invoked from this {@link ProcessContext} instance. Be aware of such side-effects when reusing
* the same {@link ProcessContext} to {@link #run() run} multiple commands.
*
* @param key the name of the environment variable (E.g. "PATH").
* @param value the value of the environment variable.
* @return this {@link ProcessContext} for fluent API calls.
*/
ProcessContext withEnvVar(String key, String value);

/**
* Runs the previously configured {@link #executable(Path) command} with the configured {@link #addArgs(String...)
* arguments}. Will reset the {@link #addArgs(String...) arguments} but not the {@link #executable(Path) command} for
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.devonfw.tools.ide.process;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.lang.ProcessBuilder.Redirect;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -89,6 +89,13 @@ public ProcessContext addArg(String arg) {
return this;
}

@Override
public ProcessContext withEnvVar(String key, String value) {

this.processBuilder.environment().put(key, value);
return this;
}

@Override
public ProcessResult run(boolean capture) {

Expand Down Expand Up @@ -195,7 +202,7 @@ private String createCommandMessage(String suffix) {
String message = sb.toString();
return message;
}

private boolean hasSheBang(Path file) {

try (InputStream in = Files.newInputStream(file)) {
Expand All @@ -211,15 +218,16 @@ private boolean hasSheBang(Path file) {
}

private String findBashOnWindows() {

// Check if Git Bash exists in the default location
Path defaultPath = Paths.get("C:\\Program Files\\Git\\bin\\bash.exe");
if (Files.exists(defaultPath)) {
return defaultPath.toString();
}

// If not found in the default location, try the registry query
String[] bashVariants = {"GitForWindows", "Cygwin\\setup"};
String[] registryKeys = {"HKEY_LOCAL_MACHINE","HKEY_CURRENT_USER"};
String[] bashVariants = { "GitForWindows", "Cygwin\\setup" };
String[] registryKeys = { "HKEY_LOCAL_MACHINE", "HKEY_CURRENT_USER" };
String regQueryResult;
for (String bashVariant : bashVariants) {
for (String registryKey : registryKeys) {
Expand Down Expand Up @@ -256,9 +264,8 @@ private String findBashOnWindows() {
}
}
}
//no bash found
// no bash found
throw new IllegalStateException("Could not find Bash. Please install Git for Windows and rerun.");
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public class VersionSegment implements VersionObject<VersionSegment> {
* @param separator the {@link #getSeparator() separator}.
* @param letters the {@link #getLettersString() letters}.
* @param digits the {@link #getDigits() digits}.
* @param pattern the {@link #getPattern() pattern}.
*/
VersionSegment(String separator, String letters, String digits) {

Expand Down

0 comments on commit 48e9dad

Please sign in to comment.