From 57b22126e7b8743927c49b024f2f951a424a5493 Mon Sep 17 00:00:00 2001 From: Daniel Mohedano Date: Wed, 25 Jun 2025 12:02:02 +0200 Subject: [PATCH 1/2] add repo root as safe directory on git client creation --- .../civisibility/git/tree/ShellGitClient.java | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/git/tree/ShellGitClient.java b/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/git/tree/ShellGitClient.java index 4d78112a646..f4fa22f9c4a 100644 --- a/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/git/tree/ShellGitClient.java +++ b/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/git/tree/ShellGitClient.java @@ -609,6 +609,23 @@ public LineDiff getGitDiff(String baseCommit, String targetCommit) } } + private void addSafeDirectory() { + // Some CI envs check out the repo as a different user than the one running the command + // This will avoid the "dubious ownership" error + try { + commandExecutor.executeCommand( + ShellCommandExecutor.OutputParser.IGNORE, + "git", + "config", + "--global", + "--add", + "safe.directory", + repoRoot); + } catch (IOException | TimeoutException | InterruptedException e) { + LOGGER.debug("Failed to add safe directory", e); + } + } + @Override public String toString() { return "GitClient{" + repoRoot + "}"; @@ -654,8 +671,11 @@ public Factory(Config config, CiVisibilityMetricCollector metricCollector) { public GitClient create(@Nullable String repoRoot) { long commandTimeoutMillis = config.getCiVisibilityGitCommandTimeoutMillis(); if (repoRoot != null) { - return new ShellGitClient( - metricCollector, repoRoot, "1 month ago", 1000, commandTimeoutMillis); + ShellGitClient client = + new ShellGitClient( + metricCollector, repoRoot, "1 month ago", 1000, commandTimeoutMillis); + client.addSafeDirectory(); + return client; } else { LOGGER.debug("Could not determine repository root, using no-op git client"); return NoOpGitClient.INSTANCE; From ddeeecb55588204a2cd384c82ffaf46a3cc32505 Mon Sep 17 00:00:00 2001 From: Daniel Mohedano Date: Wed, 25 Jun 2025 15:00:17 +0200 Subject: [PATCH 2/2] rename method --- .../datadog/trace/civisibility/git/tree/ShellGitClient.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/git/tree/ShellGitClient.java b/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/git/tree/ShellGitClient.java index f4fa22f9c4a..c2575a2306d 100644 --- a/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/git/tree/ShellGitClient.java +++ b/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/git/tree/ShellGitClient.java @@ -609,7 +609,7 @@ public LineDiff getGitDiff(String baseCommit, String targetCommit) } } - private void addSafeDirectory() { + private void makeRepoRootSafeDirectory() { // Some CI envs check out the repo as a different user than the one running the command // This will avoid the "dubious ownership" error try { @@ -674,7 +674,7 @@ public GitClient create(@Nullable String repoRoot) { ShellGitClient client = new ShellGitClient( metricCollector, repoRoot, "1 month ago", 1000, commandTimeoutMillis); - client.addSafeDirectory(); + client.makeRepoRootSafeDirectory(); return client; } else { LOGGER.debug("Could not determine repository root, using no-op git client");