From 48e9dada9c4e49f322078002238b628caa57fffb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Hohwiller?= Date: Tue, 16 Jan 2024 12:43:37 +0100 Subject: [PATCH] #6: fix git hangs via non interactive invocation (#174) --- .../tools/ide/context/AbstractIdeContext.java | 2 +- .../tools/ide/process/ProcessContext.java | 13 +++++++++++++ .../tools/ide/process/ProcessContextImpl.java | 19 +++++++++++++------ .../tools/ide/version/VersionSegment.java | 1 - 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/cli/src/main/java/com/devonfw/tools/ide/context/AbstractIdeContext.java b/cli/src/main/java/com/devonfw/tools/ide/context/AbstractIdeContext.java index 8bda33623..d9945a70a 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/context/AbstractIdeContext.java +++ b/cli/src/main/java/com/devonfw/tools/ide/context/AbstractIdeContext.java @@ -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 remotes = result.getOut(); diff --git a/cli/src/main/java/com/devonfw/tools/ide/process/ProcessContext.java b/cli/src/main/java/com/devonfw/tools/ide/process/ProcessContext.java index 85eb99632..bcab1cfb9 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/process/ProcessContext.java +++ b/cli/src/main/java/com/devonfw/tools/ide/process/ProcessContext.java @@ -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 diff --git a/cli/src/main/java/com/devonfw/tools/ide/process/ProcessContextImpl.java b/cli/src/main/java/com/devonfw/tools/ide/process/ProcessContextImpl.java index 596f9c316..21f6a9a0e 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/process/ProcessContextImpl.java +++ b/cli/src/main/java/com/devonfw/tools/ide/process/ProcessContextImpl.java @@ -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; @@ -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) { @@ -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)) { @@ -211,6 +218,7 @@ 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)) { @@ -218,8 +226,8 @@ private String findBashOnWindows() { } // 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) { @@ -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."); } - } diff --git a/cli/src/main/java/com/devonfw/tools/ide/version/VersionSegment.java b/cli/src/main/java/com/devonfw/tools/ide/version/VersionSegment.java index 1bb3d01f0..0c84b68e2 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/version/VersionSegment.java +++ b/cli/src/main/java/com/devonfw/tools/ide/version/VersionSegment.java @@ -33,7 +33,6 @@ public class VersionSegment implements VersionObject { * @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) {