Skip to content

Commit

Permalink
move windows code to file excluded from coverage computing
Browse files Browse the repository at this point in the history
  • Loading branch information
kirill-knize-sonarsource committed Feb 13, 2025
1 parent 469747a commit 1844de1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<String>();
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";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String>();
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"));
}
}
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@

<!-- Exclude Maven Shade plug-in dependency as the test is done via the Maven build itself ... -->
<sonar.coverage.exclusions>buildSrc/maven-shade-ext-bnd-transformer/**</sonar.coverage.exclusions>
<!-- We don't compute coverage on windows UTs -->
<sonar.coverage.exclusions>backend/commons/src/main/java/org/sonarsource/sonarlint/core/commons/util/git/WinGitUtils.java</sonar.coverage.exclusions>
<!-- Exclude from the duplication detection deprecated DTOs that were replaced by new types. They should be removed in 10.3 -->
<sonar.cpd.exclusions>rpc-protocol/src/main/java/org/sonarsource/sonarlint/core/rpc/protocol/client/analysis/*Dto.java</sonar.cpd.exclusions>
</properties>
Expand Down

0 comments on commit 1844de1

Please sign in to comment.