MCPcopy
hub / github.com/eth0izzle/shhgit / processRepositoryOrGist

Function processRepositoryOrGist

main.go:50–129  ·  view source on GitHub ↗
(url string)

Source from the content-addressed store, hash-verified

48}
49
50func processRepositoryOrGist(url string) {
51 var (
52 matches []string
53 matchedAny bool = false
54 )
55
56 dir := core.GetTempDir(core.GetHash(string(url)))
57 _, err := core.CloneRepository(session, url, dir)
58
59 if err != nil {
60 session.Log.Debug("[%s] Cloning failed: %s", url, err.Error())
61 os.RemoveAll(dir)
62 return
63 }
64
65 session.Log.Debug("[%s] Cloning in to %s", url, strings.Replace(dir, *session.Options.TempDirectory, "", -1))
66
67 for _, file := range core.GetMatchingFiles(dir) {
68 relativeFileName := strings.Replace(file.Path, *session.Options.TempDirectory, "", -1)
69
70 if *session.Options.SearchQuery != "" {
71 queryRegex := regexp.MustCompile(*session.Options.SearchQuery)
72 for _, match := range queryRegex.FindAllSubmatch(file.Contents, -1) {
73 matches = append(matches, string(match[0]))
74 }
75
76 if matches != nil {
77 count := len(matches)
78 m := strings.Join(matches, ", ")
79 session.Log.Important("[%s] %d %s for %s in file %s: %s", url, count, core.Pluralize(count, "match", "matches"), color.GreenString("Search Query"), relativeFileName, color.YellowString(m))
80 session.WriteToCsv([]string{url, "Search Query", relativeFileName, m})
81 }
82 } else {
83 for _, signature := range session.Signatures {
84 if matched, part := signature.Match(file); matched {
85 matchedAny = true
86
87 if part == core.PartContents {
88 if matches = signature.GetContentsMatches(file); matches != nil {
89 count := len(matches)
90 m := strings.Join(matches, ", ")
91 session.Log.Important("[%s] %d %s for %s in file %s: %s", url, count, core.Pluralize(count, "match", "matches"), color.GreenString(signature.Name()), relativeFileName, color.YellowString(m))
92 session.WriteToCsv([]string{url, signature.Name(), relativeFileName, m})
93 }
94 } else {
95 if *session.Options.PathChecks {
96 session.Log.Important("[%s] Matching file %s for %s", url, color.YellowString(relativeFileName), color.GreenString(signature.Name()))
97 session.WriteToCsv([]string{url, signature.Name(), relativeFileName, ""})
98 }
99
100 if *session.Options.EntropyThreshold > 0 && file.CanCheckEntropy() {
101 scanner := bufio.NewScanner(bytes.NewReader(file.Contents))
102
103 for scanner.Scan() {
104 line := scanner.Text()
105
106 if len(line) > 6 && len(line) < 100 {
107 entropy := core.GetEntropy(scanner.Text())

Callers 2

ProcessRepositoriesFunction · 0.85
ProcessGistsFunction · 0.85

Calls 14

GetTempDirFunction · 0.92
GetHashFunction · 0.92
CloneRepositoryFunction · 0.92
GetMatchingFilesFunction · 0.92
PluralizeFunction · 0.92
GetEntropyFunction · 0.92
DebugMethod · 0.80
ErrorMethod · 0.80
ImportantMethod · 0.80
WriteToCsvMethod · 0.80
CanCheckEntropyMethod · 0.80
MatchMethod · 0.65

Tested by

no test coverage detected