diff --git a/backend/commons/src/main/java/org/sonarsource/sonarlint/core/commons/util/git/GitUtils.java b/backend/commons/src/main/java/org/sonarsource/sonarlint/core/commons/util/git/GitUtils.java index 04132a8bac..37507f02e7 100644 --- a/backend/commons/src/main/java/org/sonarsource/sonarlint/core/commons/util/git/GitUtils.java +++ b/backend/commons/src/main/java/org/sonarsource/sonarlint/core/commons/util/git/GitUtils.java @@ -29,7 +29,6 @@ import java.net.URI; import java.nio.charset.StandardCharsets; import java.nio.file.Path; -import java.util.LinkedList; import java.util.List; import java.util.Optional; import java.util.Set; @@ -61,6 +60,7 @@ import static java.util.Optional.ofNullable; import static org.eclipse.jgit.lib.Constants.GITIGNORE_FILENAME; import static org.sonarsource.sonarlint.core.commons.util.git.BlameParser.parseBlameOutput; +import static org.sonarsource.sonarlint.core.commons.util.git.WinGitUtils.locateGitOnWindows; public class GitUtils { private static final SonarLintLogger LOG = SonarLintLogger.get(); @@ -163,23 +163,6 @@ private static String getNativeGitExecutable() { return nativeGitExecutable; } - private static String locateGitOnWindows() throws IOException { - // Windows will search current directory in addition to the PATH variable, which is unsecure. - // To avoid it we use where.exe to find git binary only in PATH. - LOG.debug("Looking for git command in the PATH using where.exe (Windows)"); - var whereCommandResult = new LinkedList(); - new ProcessWrapperFactory() - .create(null, whereCommandResult::add, "C:\\Windows\\System32\\where.exe", "$PATH:git.exe") - .execute(); - - if (!whereCommandResult.isEmpty()) { - var out = whereCommandResult.get(0).trim(); - LOG.debug("Found git.exe at {}", out); - return out; - } - throw new IllegalStateException("git.exe not found in PATH. PATH value was: " + System.getProperty("PATH")); - } - private static String getGitExecutable() throws IOException { return SystemUtils.IS_OS_WINDOWS ? locateGitOnWindows() : "git"; } diff --git a/backend/commons/src/main/java/org/sonarsource/sonarlint/core/commons/util/git/WinGitUtils.java b/backend/commons/src/main/java/org/sonarsource/sonarlint/core/commons/util/git/WinGitUtils.java new file mode 100644 index 0000000000..c758bcdf5c --- /dev/null +++ b/backend/commons/src/main/java/org/sonarsource/sonarlint/core/commons/util/git/WinGitUtils.java @@ -0,0 +1,49 @@ +/* + * SonarLint Core - Commons + * Copyright (C) 2016-2025 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonarsource.sonarlint.core.commons.util.git; + +import java.io.IOException; +import java.util.LinkedList; +import org.sonarsource.sonarlint.core.commons.log.SonarLintLogger; + +public class WinGitUtils { + private static final SonarLintLogger LOG = SonarLintLogger.get(); + + private WinGitUtils() { + // utils + } + + public static String locateGitOnWindows() throws IOException { + // Windows will search current directory in addition to the PATH variable, which is unsecure. + // To avoid it we use where.exe to find git binary only in PATH. + LOG.debug("Looking for git command in the PATH using where.exe (Windows)"); + var whereCommandResult = new LinkedList(); + new ProcessWrapperFactory() + .create(null, whereCommandResult::add, "C:\\Windows\\System32\\where.exe", "$PATH:git.exe") + .execute(); + + if (!whereCommandResult.isEmpty()) { + var out = whereCommandResult.get(0).trim(); + LOG.debug("Found git.exe at {}", out); + return out; + } + throw new IllegalStateException("git.exe not found in PATH. PATH value was: " + System.getProperty("PATH")); + } +} diff --git a/pom.xml b/pom.xml index bc6f3c77ad..5f3a798ae6 100644 --- a/pom.xml +++ b/pom.xml @@ -82,6 +82,8 @@ buildSrc/maven-shade-ext-bnd-transformer/** + + backend/commons/src/main/java/org/sonarsource/sonarlint/core/commons/util/git/WinGitUtils.java rpc-protocol/src/main/java/org/sonarsource/sonarlint/core/rpc/protocol/client/analysis/*Dto.java