Skip to content

Commit 3fbb9e9

Browse files
Fix git commit validation (#4192)
* cleanup git commit validation * fixed local git repo validation * added remote commit validation * removed remote repo commit validation
1 parent da08d9b commit 3fbb9e9

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

main.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -372,17 +372,6 @@ func main() {
372372
}
373373
}
374374

375-
// Function to check if the commit is valid
376-
func isValidCommit(commit string) bool {
377-
cmd := exec.Command("git", "cat-file", "-t", commit)
378-
output, err := cmd.Output()
379-
if err != nil {
380-
return false
381-
}
382-
383-
return strings.TrimSpace(string(output)) == "commit"
384-
}
385-
386375
func run(state overseer.State) {
387376

388377
ctx, cancel := context.WithCancelCause(context.Background())
@@ -423,9 +412,6 @@ func run(state overseer.State) {
423412
// When setting a base commit, chunks must be scanned in order.
424413
if *gitScanSinceCommit != "" {
425414
*concurrency = 1
426-
if !isValidCommit(*gitScanSinceCommit) {
427-
logger.Info("Warning: The provided commit hash appears to be invalid.")
428-
}
429415
}
430416

431417
if *profile {
@@ -717,6 +703,13 @@ func runSingleScan(ctx context.Context, cmd string, cfg engine.Config) (metrics,
717703
var refs []sources.JobProgressRef
718704
switch cmd {
719705
case gitScan.FullCommand():
706+
// validate the commit for local repository only
707+
if *gitScanSinceCommit != "" && strings.HasPrefix(*gitScanURI, "file") {
708+
if !isValidCommit(*gitScanURI, *gitScanSinceCommit) {
709+
ctx.Logger().Info("Warning: The provided commit hash appears to be invalid.")
710+
}
711+
}
712+
720713
gitCfg := sources.GitConfig{
721714
URI: *gitScanURI,
722715
IncludePathsFile: *gitScanIncludePaths,
@@ -1097,3 +1090,15 @@ func printAverageDetectorTime(e *engine.Engine) {
10971090
fmt.Fprintf(os.Stderr, "%s: %s\n", detectorName, duration)
10981091
}
10991092
}
1093+
1094+
// Function to check if the commit is valid
1095+
func isValidCommit(uri, commit string) bool {
1096+
// handle file:// urls
1097+
repoPath, _ := strings.CutPrefix(uri, "file://") // remove the prefix to validate against the repo path
1098+
output, err := exec.Command("git", "-C", repoPath, "cat-file", "-t", commit).Output()
1099+
if err != nil {
1100+
return false
1101+
}
1102+
1103+
return strings.TrimSpace(string(output)) == "commit"
1104+
}

0 commit comments

Comments
 (0)