66import datadog .trace .api .civisibility .telemetry .CiVisibilityDistributionMetric ;
77import datadog .trace .api .civisibility .telemetry .CiVisibilityMetricCollector ;
88import datadog .trace .api .civisibility .telemetry .tag .Command ;
9+ import datadog .trace .api .git .GitUtils ;
910import datadog .trace .civisibility .diff .LineDiff ;
1011import datadog .trace .civisibility .utils .ShellCommandExecutor ;
1112import datadog .trace .util .Strings ;
@@ -149,13 +150,13 @@ public void unshallow(@Nullable String remoteCommitReference)
149150 .trim ();
150151
151152 // refetch data from the server for the given period of time
152- if (remoteCommitReference != null ) {
153+ if (remoteCommitReference != null && GitUtils . isValidRef ( remoteCommitReference ) ) {
153154 String headSha = getSha (remoteCommitReference );
154155 commandExecutor .executeCommand (
155156 ShellCommandExecutor .OutputParser .IGNORE ,
156157 "git" ,
157158 "fetch" ,
158- String .format ("--shallow-since== '%s'" , latestCommitsSince ),
159+ String .format ("--shallow-since='%s'" , latestCommitsSince ),
159160 "--update-shallow" ,
160161 "--filter=blob:none" ,
161162 "--recurse-submodules=no" ,
@@ -166,7 +167,7 @@ public void unshallow(@Nullable String remoteCommitReference)
166167 ShellCommandExecutor .OutputParser .IGNORE ,
167168 "git" ,
168169 "fetch" ,
169- String .format ("--shallow-since== '%s'" , latestCommitsSince ),
170+ String .format ("--shallow-since='%s'" , latestCommitsSince ),
170171 "--update-shallow" ,
171172 "--filter=blob:none" ,
172173 "--recurse-submodules=no" ,
@@ -231,6 +232,9 @@ public String getRepoRoot() throws IOException, TimeoutException, InterruptedExc
231232 @ Override
232233 public String getRemoteUrl (String remoteName )
233234 throws IOException , TimeoutException , InterruptedException {
235+ if (!GitUtils .isValidRef (remoteName )) {
236+ return null ;
237+ }
234238 return executeCommand (
235239 Command .GET_REPOSITORY ,
236240 () ->
@@ -274,6 +278,9 @@ public String getCurrentBranch() throws IOException, TimeoutException, Interrupt
274278 @ Override
275279 public List <String > getTags (String commit )
276280 throws IOException , TimeoutException , InterruptedException {
281+ if (GitUtils .isNotValidCommit (commit )) {
282+ return Collections .emptyList ();
283+ }
277284 return executeCommand (
278285 Command .OTHER ,
279286 () -> {
@@ -302,6 +309,9 @@ public List<String> getTags(String commit)
302309 @ Override
303310 public String getSha (String reference )
304311 throws IOException , TimeoutException , InterruptedException {
312+ if (GitUtils .isNotValidCommit (reference )) {
313+ return null ;
314+ }
305315 return executeCommand (
306316 Command .OTHER ,
307317 () ->
@@ -324,6 +334,9 @@ public String getSha(String reference)
324334 @ Override
325335 public String getFullMessage (String commit )
326336 throws IOException , TimeoutException , InterruptedException {
337+ if (GitUtils .isNotValidCommit (commit )) {
338+ return null ;
339+ }
327340 return executeCommand (
328341 Command .OTHER ,
329342 () ->
@@ -346,6 +359,9 @@ public String getFullMessage(String commit)
346359 @ Override
347360 public String getAuthorName (String commit )
348361 throws IOException , TimeoutException , InterruptedException {
362+ if (GitUtils .isNotValidCommit (commit )) {
363+ return null ;
364+ }
349365 return executeCommand (
350366 Command .OTHER ,
351367 () ->
@@ -368,6 +384,9 @@ public String getAuthorName(String commit)
368384 @ Override
369385 public String getAuthorEmail (String commit )
370386 throws IOException , TimeoutException , InterruptedException {
387+ if (GitUtils .isNotValidCommit (commit )) {
388+ return null ;
389+ }
371390 return executeCommand (
372391 Command .OTHER ,
373392 () ->
@@ -390,6 +409,9 @@ public String getAuthorEmail(String commit)
390409 @ Override
391410 public String getAuthorDate (String commit )
392411 throws IOException , TimeoutException , InterruptedException {
412+ if (GitUtils .isNotValidCommit (commit )) {
413+ return null ;
414+ }
393415 return executeCommand (
394416 Command .OTHER ,
395417 () ->
@@ -412,6 +434,9 @@ public String getAuthorDate(String commit)
412434 @ Override
413435 public String getCommitterName (String commit )
414436 throws IOException , TimeoutException , InterruptedException {
437+ if (GitUtils .isNotValidCommit (commit )) {
438+ return null ;
439+ }
415440 return executeCommand (
416441 Command .OTHER ,
417442 () ->
@@ -434,6 +459,9 @@ public String getCommitterName(String commit)
434459 @ Override
435460 public String getCommitterEmail (String commit )
436461 throws IOException , TimeoutException , InterruptedException {
462+ if (GitUtils .isNotValidCommit (commit )) {
463+ return null ;
464+ }
437465 return executeCommand (
438466 Command .OTHER ,
439467 () ->
@@ -456,6 +484,9 @@ public String getCommitterEmail(String commit)
456484 @ Override
457485 public String getCommitterDate (String commit )
458486 throws IOException , TimeoutException , InterruptedException {
487+ if (GitUtils .isNotValidCommit (commit )) {
488+ return null ;
489+ }
459490 return executeCommand (
460491 Command .OTHER ,
461492 () ->
@@ -601,6 +632,10 @@ private Path createTempDirectory() throws IOException {
601632 public String getBaseCommitSha (
602633 @ Nullable String baseBranch , @ Nullable String settingsDefaultBranch )
603634 throws IOException , TimeoutException , InterruptedException {
635+ if ((baseBranch != null && !GitUtils .isValidRef (baseBranch ))
636+ || (settingsDefaultBranch != null && !GitUtils .isValidRef (settingsDefaultBranch ))) {
637+ return null ;
638+ }
604639 return executeCommand (
605640 Command .BASE_COMMIT_SHA ,
606641 () -> {
@@ -956,10 +991,10 @@ String getMergeBase(String baseBranch, String sourceBranch)
956991 @ Override
957992 public LineDiff getGitDiff (String baseCommit , String targetCommit )
958993 throws IOException , TimeoutException , InterruptedException {
959- if (Strings .isBlank (baseCommit )) {
994+ if (Strings .isBlank (baseCommit ) || ! GitUtils . isValidCommitSha ( baseCommit ) ) {
960995 LOGGER .debug ("Base commit info is not available, returning empty git diff" );
961996 return null ;
962- } else if (Strings .isNotBlank (targetCommit )) {
997+ } else if (Strings .isNotBlank (targetCommit ) && GitUtils . isValidCommitSha ( targetCommit ) ) {
963998 return executeCommand (
964999 Command .DIFF ,
9651000 () ->
@@ -1041,7 +1076,7 @@ public Factory(Config config, CiVisibilityMetricCollector metricCollector) {
10411076 @ Override
10421077 public GitClient create (@ Nullable String repoRoot ) {
10431078 long commandTimeoutMillis = config .getCiVisibilityGitCommandTimeoutMillis ();
1044- if (repoRoot != null ) {
1079+ if (repoRoot != null && GitUtils . isValidPath ( repoRoot ) ) {
10451080 ShellGitClient client =
10461081 new ShellGitClient (
10471082 metricCollector , repoRoot , "1 month ago" , 1000 , commandTimeoutMillis );
0 commit comments