@@ -980,6 +980,23 @@ public LineDiff getGitDiff(String baseCommit, String targetCommit)
980
980
}
981
981
}
982
982
983
+ private void makeRepoRootSafeDirectory () {
984
+ // Some CI envs check out the repo as a different user than the one running the command
985
+ // This will avoid the "dubious ownership" error
986
+ try {
987
+ commandExecutor .executeCommand (
988
+ ShellCommandExecutor .OutputParser .IGNORE ,
989
+ "git" ,
990
+ "config" ,
991
+ "--global" ,
992
+ "--add" ,
993
+ "safe.directory" ,
994
+ repoRoot );
995
+ } catch (IOException | TimeoutException | InterruptedException e ) {
996
+ LOGGER .debug ("Failed to add safe directory" , e );
997
+ }
998
+ }
999
+
983
1000
@ Override
984
1001
public String toString () {
985
1002
return "GitClient{" + repoRoot + "}" ;
@@ -1025,8 +1042,11 @@ public Factory(Config config, CiVisibilityMetricCollector metricCollector) {
1025
1042
public GitClient create (@ Nullable String repoRoot ) {
1026
1043
long commandTimeoutMillis = config .getCiVisibilityGitCommandTimeoutMillis ();
1027
1044
if (repoRoot != null ) {
1028
- return new ShellGitClient (
1029
- metricCollector , repoRoot , "1 month ago" , 1000 , commandTimeoutMillis );
1045
+ ShellGitClient client =
1046
+ new ShellGitClient (
1047
+ metricCollector , repoRoot , "1 month ago" , 1000 , commandTimeoutMillis );
1048
+ client .makeRepoRootSafeDirectory ();
1049
+ return client ;
1030
1050
} else {
1031
1051
LOGGER .debug ("Could not determine repository root, using no-op git client" );
1032
1052
return NoOpGitClient .INSTANCE ;
0 commit comments