Skip to content

Commit 7ba1c2c

Browse files
removed remote repo commit validation
1 parent f0a1d4a commit 7ba1c2c

File tree

1 file changed

+8
-44
lines changed

1 file changed

+8
-44
lines changed

main.go

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"os"
1010
"os/exec"
1111
"os/signal"
12-
"path/filepath"
1312
"runtime"
1413
"strconv"
1514
"strings"
@@ -692,9 +691,11 @@ func runSingleScan(ctx context.Context, cmd string, cfg engine.Config) (metrics,
692691
var ref sources.JobProgressRef
693692
switch cmd {
694693
case gitScan.FullCommand():
695-
// validate the commit
696-
if *gitScanSinceCommit != "" && !isValidCommit(*gitScanURI, *gitScanSinceCommit) {
697-
ctx.Logger().Info("Warning: The provided commit hash appears to be invalid.")
694+
// validate the commit for local repository only
695+
if *gitScanSinceCommit != "" && strings.HasPrefix(*gitScanURI, "file") {
696+
if !isValidCommit(*gitScanURI, *gitScanSinceCommit) {
697+
ctx.Logger().Info("Warning: The provided commit hash appears to be invalid.")
698+
}
698699
}
699700

700701
gitCfg := sources.GitConfig{
@@ -1033,46 +1034,9 @@ func printAverageDetectorTime(e *engine.Engine) {
10331034

10341035
// Function to check if the commit is valid
10351036
func isValidCommit(uri, commit string) bool {
1036-
if strings.HasPrefix(uri, "file") {
1037-
// handle file:// urls
1038-
repoPath, _ := strings.CutPrefix(uri, "file://") // remove the prefix to validate against the repo path
1039-
output, err := exec.Command("git", "-C", repoPath, "cat-file", "-t", commit).Output()
1040-
if err != nil {
1041-
return false
1042-
}
1043-
1044-
return strings.TrimSpace(string(output)) == "commit"
1045-
} else if strings.HasPrefix(uri, "https") {
1046-
return isValidCommitRemote(uri, commit)
1047-
} else {
1048-
return false
1049-
}
1050-
}
1051-
1052-
func isValidCommitRemote(repoURL, commit string) bool {
1053-
// create temporary directory
1054-
tempDir, err := os.MkdirTemp("", "git-verify-*")
1055-
if err != nil {
1056-
return false
1057-
}
1058-
defer os.RemoveAll(tempDir)
1059-
1060-
// clone with blob filter, no checkout, and bare repository
1061-
repoPath := filepath.Join(tempDir, "repo.git")
1062-
cloneCmd := exec.Command("git", "clone",
1063-
"--filter=blob:none",
1064-
"--no-checkout",
1065-
"--bare",
1066-
repoURL,
1067-
repoPath)
1068-
1069-
if err := cloneCmd.Run(); err != nil {
1070-
return false
1071-
}
1072-
1073-
// verify the commit using --git-dir since it's a bare repository
1074-
cmd := exec.Command("git", "--git-dir", repoPath, "cat-file", "-t", commit)
1075-
output, err := cmd.Output()
1037+
// handle file:// urls
1038+
repoPath, _ := strings.CutPrefix(uri, "file://") // remove the prefix to validate against the repo path
1039+
output, err := exec.Command("git", "-C", repoPath, "cat-file", "-t", commit).Output()
10761040
if err != nil {
10771041
return false
10781042
}

0 commit comments

Comments
 (0)