5import (
6 "context"
7▶ "errors"
8 "fmt"
9 "io"
· · ·
125// collected --depth commits. iter.ForEach surfaces whatever the callback
126// returns, so we can compare it back at the call site directly.
127▶var errStopIter = errors.New("history: stop iteration")
128
129// Bucketing divides [From, To] into N equal time slices. Used by the timeline
· · ·
201// (newest first → oldest first), and feeds every commit's first-parent diff
202// to the observer.
203▶func runHistory(repoPath string, observer CommitObserver) (HistoryWindow, error) {
204 repo, err := git.PlainOpenWithOptions(repoPath, &git.PlainOpenOptions{DetectDotGit: true})
205 if err != nil {
· · ·
206▶ return HistoryWindow{}, fmt.Errorf("open git repository: %w", err)
207 }
208
· · ·
209 head, err := repo.Head()
210 if err != nil {
211▶ if errors.Is(err, plumbing.ErrReferenceNotFound) {
212 observer.Finalise(HistoryWindow{}, emptySnapshot())
213 return HistoryWindow{}, nil
+ 12 more matches in this file
335// Timeline selects an over-time view. With ByAuthor, runs the author
336// timeline report (plan 04); alone, runs the languages-over-time report
337▶// (plan 05). With Hotspots set, the combination errors out.
338var Timeline = false
339
· · ·
461 t := strings.Split(s, ":")
462 if len(t) != 2 {
463▶ printError(fmt.Sprintf("ignoring malformed count-as rule %q: expected format <from>:<to>", s))
464 continue
465 }
· · ·
481 // extension, so no mapping was registered. Warn rather than silently
482 // ignoring the rule, since count-as cannot mint new categories yet.
483▶ printError(fmt.Sprintf("ignoring count-as rule %q: target %q is not a known language or extension", s, t[1]))
484 }
485}
· · ·
519// Because regex patterns and paths legitimately contain ':', name and baselang
520// are peeled from the right and the pattern is whatever remains in between.
521▶func parseCountAsPattern(s string) (CountRule, error) {
522 engine := MatchGlob
523 rest := s
· · ·
535 lastColon := strings.LastIndex(rest, ":")
536 if lastColon == -1 {
537▶ return CountRule{}, fmt.Errorf("expected format [engine:]pattern:name:baselang")
538 }
539 baseLanguage := rest[lastColon+1:]
+ 9 more matches in this file
77// existing default-named file (so a bare `--report` is non-destructive),
78// then collects data and renders the HTML output.
79▶func runReport(paths []string) error {
80 path := "."
81 if len(paths) > 0 {
· · ·
108// The io.Reader / io.Writer seam keeps the function unit-testable without
109// poking at os.Stdin / os.Stderr.
110▶func confirmReportOverwrite(outPath string, usedDefaultName, stdinIsTTY bool, in io.Reader, out io.Writer) error {
111 if !usedDefaultName {
112 return nil
· · ·
115 return nil
116 } else if err != nil {
117▶ // Some other stat error (permission?) — let the subsequent
118 // os.Create surface the underlying problem with a clearer
119 // "create report file: ..." wrapper instead of a stat error
· · ·
119▶ // "create report file: ..." wrapper instead of a stat error
120 // nobody asked for.
121 return nil
· · ·
122 }
123 if !stdinIsTTY {
124▶ return fmt.Errorf("%s already exists; rerun with --report=%s to overwrite explicitly", outPath, outPath)
125 }
126 _, _ = fmt.Fprintf(out, "%s already exists. Overwrite? [y/N]: ", outPath)
+ 7 more matches in this file
253// scc's analysis modes (ULOC, line-length, per-file table) are gated by
254// process-wide globals. The report mode flips them on inside a single
255▶// invocation; we snapshot and restore via defer so panics, errors, or
256// in-process re-entrancy don't leak state into a later scc call.
257type reportFlagState struct {
· · ·
284// snapshotted and restored via defer, but callers should not assume the
285// flags retain their on-entry values during the call.
286▶func CollectReportData(path string) (ReportData, error) {
287 start := time.Now()
288
· · ·
439// rollup and a flat per-file slice. Reuses aggregateLanguageSummary by
440// feeding it the same FileJobs through a buffered channel.
441▶func walkAndAggregate(path string) ([]*FileJob, []LanguageSummary, Totals, error) {
442 if path == "" {
443 path = "."
· · ·
447 info, err := os.Stat(fpath)
448 if err != nil {
449▶ return nil, nil, Totals{}, fmt.Errorf("file or directory could not be read: %s", fpath)
450 }
451
· · ·
466 if len(dirPaths) > 0 {
467 fileWalker := gocodewalker.NewParallelFileWalker(dirPaths, potentialFilesQueue)
468▶ fileWalker.SetErrorHandler(func(e error) bool {
469 printError(e.Error())
470 return true
+ 3 more matches in this file
272// walks history with baseline seeding, and writes the chosen format to
273// stdout or FileOutput.
274▶func runAuthorsReport(repoPath string) error {
275 observer := newHistoryAuthorsObserver()
276 if _, err := runHistory(repoPath, observer); err != nil {
· · ·
292}
293
294▶func renderAuthors(o *historyAuthorsObserver) (string, error) {
295 switch strings.ToLower(Format) {
296 case "", "tabular", "wide":
· · ·
301 return renderAuthorsJSON(o)
302 default:
303▶ return "", fmt.Errorf("unsupported --format %q for --by-author (supported: tabular, csv, json)", Format)
304 }
305}
· · ·
480}
481
482▶func renderAuthorsCSV(o *historyAuthorsObserver) (string, error) {
483 var sb strings.Builder
484 sb.WriteString(formatWindowComment(o.window))
· · ·
513 }
514 w.Flush()
515▶ if err := w.Error(); err != nil {
516 return "", err
517 }
+ 1 more matches in this file
3import (
4 "context"
5▶ "errors"
6 "fmt"
7 "math"
· · ·
63
64 if filterBad(loc) {
65▶ log.Error().Str(uniqueCode, "bfee4bd8").Str("loc", loc.String()).Msg("filter bad")
66 return
67 }
· · ·
71 res, err := process(1, loc)
72 if err != nil {
73▶ log.Error().Str(uniqueCode, "03ec75c3").Err(err).Str("loc", loc.String()).Send()
74 w.WriteHeader(http.StatusBadRequest)
75 _, _ = w.Write([]byte("something bad happened sorry"))
· · ·
108 addr := ":8080"
109 log.Info().Str(uniqueCode, "1876ce1e").Str("addr", addr).Msg("serving")
110▶ if err := http.ListenAndServe(addr, nil); err != nil && !errors.Is(err, http.ErrServerClosed) {
111 log.Error().Str(uniqueCode, "c28556e8").Err(err).Send()
112 os.Exit(1)
· · ·
111▶ log.Error().Str(uniqueCode, "c28556e8").Err(err).Send()
112 os.Exit(1)
113 }
+ 7 more matches in this file
6 "bytes"
7 "cmp"
8▶ "errors"
9 "slices"
10 "strings"
· · ·
12
13var (
14▶ errMissingShebang = errors.New("missing shebang")
15 errUnknownShebang = errors.New("unknown shebang")
16 errUnableToDetermineShebangCmd = errors.New("unable to determine shebang command")
· · ·
15▶ errUnknownShebang = errors.New("unknown shebang")
16 errUnableToDetermineShebangCmd = errors.New("unable to determine shebang command")
17)
· · ·
16▶ errUnableToDetermineShebangCmd = errors.New("unable to determine shebang command")
17)
18
· · ·
19▶// DetectLanguage detects a language based on the filename returns the language extension and error
20func DetectLanguage(name string) ([]string, string) {
21 extension := ""
+ 2 more matches in this file
81
82 errLogger := log.New(os.Stderr, "scc-mcp: ", log.LstdFlags)
83▶ if err := server.ServeStdio(mcpServer, server.WithErrorLogger(errLogger)); err != nil {
84 _, _ = fmt.Fprintf(os.Stderr, "scc-mcp: server error: %v\n", err)
85 os.Exit(1)
· · ·
84▶ _, _ = fmt.Fprintf(os.Stderr, "scc-mcp: server error: %v\n", err)
85 os.Exit(1)
86 }
· · ·
149}
150
151▶func mcpAnalyzeHandler(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
152 args := request.GetArguments()
153
· · ·
161 absPath, err := filepath.Abs(path)
162 if err != nil {
163▶ return mcp.NewToolResultError(fmt.Sprintf("invalid path: %v", err)), nil
164 }
165
· · ·
166 // Verify path can be accessed
167 if _, err := os.Stat(absPath); err != nil {
168▶ return mcp.NewToolResultError(fmt.Sprintf("path cannot be accessed: %s: %v", absPath, err)), nil
169 }
170
+ 3 more matches in this file
186// when --by-author --timeline is set. Opens the repo, walks the window with
187// the configured bucket count, and writes the chosen format.
188▶func runAuthorTimelineReport(repoPath string) error {
189 observer := newHistoryAuthorTimelineObserver(HistoryBuckets)
190 if _, err := runHistory(repoPath, observer); err != nil {
· · ·
206}
207
208▶func renderAuthorTimeline(o *historyAuthorTimelineObserver) (string, error) {
209 switch strings.ToLower(Format) {
210 case "", "tabular", "wide":
· · ·
215 return renderAuthorTimelineJSON(o)
216 default:
217▶ return "", fmt.Errorf("unsupported --format %q for --by-author --timeline (supported: tabular, csv, json)", Format)
218 }
219}
· · ·
322}
323
324▶func renderAuthorTimelineCSV(o *historyAuthorTimelineObserver) (string, error) {
325 var sb strings.Builder
326 sb.WriteString(formatWindowComment(o.window))
· · ·
344 }
345 w.Flush()
346▶ if err := w.Error(); err != nil {
347 return "", err
348 }
+ 1 more matches in this file
66 }
67
68▶ var err error
69 symPath, err = filepath.EvalSymlinks(path)
70 if err != nil {
· · ·
71▶ printError(err.Error())
72 return nil
73 }
· · ·
74 fileInfo, err = os.Lstat(symPath)
75 if err != nil {
76▶ printError(err.Error())
77 return nil
78 }
188// walks the window with the configured bucket count, and writes the chosen
189// format.
190▶func runLanguagesTimelineReport(repoPath string) error {
191 observer := newHistoryLanguagesObserver(HistoryBuckets)
192 if _, err := runHistory(repoPath, observer); err != nil {
· · ·
208}
209
210▶func renderLanguagesTimeline(o *historyLanguagesObserver) (string, error) {
211 switch strings.ToLower(Format) {
212 case "", "tabular", "wide":
· · ·
217 return renderLanguagesTimelineJSON(o)
218 default:
219▶ return "", fmt.Errorf("unsupported --format %q for --timeline (supported: tabular, csv, json)", Format)
220 }
221}
· · ·
280}
281
282▶func renderLanguagesTimelineCSV(o *historyLanguagesObserver) (string, error) {
283 var sb strings.Builder
284 sb.WriteString(formatWindowComment(o.window))
· · ·
305 }
306 w.Flush()
307▶ if err := w.Error(); err != nil {
308 return "", err
309 }
+ 1 more matches in this file
22// --hotspots is set. Opens the repo at repoPath, walks history, and writes
23// the chosen format to stdout or FileOutput.
24▶func runHotspotsReport(repoPath string) error {
25 observer := newHotspotsObserver()
26 if _, err := runHistory(repoPath, observer); err != nil {
· · ·
198
199// renderHotspots returns the formatted output for the chosen --format.
200▶func renderHotspots(o *hotspotsObserver) (string, error) {
201 switch strings.ToLower(Format) {
202 case "", "tabular", "wide":
· · ·
207 return renderHotspotsJSON(o)
208 default:
209▶ return "", fmt.Errorf("unsupported --format %q for --hotspots (supported: tabular, csv, json)", Format)
210 }
211}
· · ·
291}
292
293▶func renderHotspotsCSV(o *hotspotsObserver) (string, error) {
294 var sb strings.Builder
295 sb.WriteString(formatWindowComment(o.window))
· · ·
319 }
320 w.Flush()
321▶ if err := w.Error(); err != nil {
322 return "", err
323 }
+ 1 more matches in this file
16// ProcessResult runs the same pipeline as Process but returns structured results
17// instead of formatting to stdout. Useful for programmatic consumers like MCP servers.
18▶func ProcessResult() ([]LanguageSummary, error) {
19 ProcessConstants()
20 processFlags()
· · ·
33 s, err := os.Stat(fpath)
34 if err != nil {
35▶ return nil, fmt.Errorf("file or directory could not be read: %s", fpath)
36 }
37
· · ·
55
56 fileWalker := gocodewalker.NewParallelFileWalker(dirPaths, potentialFilesQueue)
57▶ fileWalker.SetErrorHandler(func(e error) bool {
58 printError(e.Error())
59 return true
· · ·
58▶ printError(e.Error())
59 return true
60 })
· · ·
78 excludePathRegexes = append(excludePathRegexes, regexpResult)
79 } else {
80▶ printError(err.Error())
81 }
82 }
+ 1 more matches in this file
30// Reads all .json files in the current folder
31// and encodes them as strings literals in constants.go
32▶func generateConstants() error {
33 files, _ := os.ReadDir(".")
34 buf := &bytes.Buffer{}
· · ·
40 f, err := os.Open(f.Name())
41 if err != nil {
42▶ return fmt.Errorf("failed to open file '%s': %v", f.Name(), err)
43 }
44 defer func(file *os.File) {
· · ·
50 // validate the json by decoding into an empty struct
51 if err := json.NewDecoder(f).Decode(&data); err != nil {
52▶ return fmt.Errorf("failed to validate json in file '%s': %v", f.Name(), err)
53 }
54
· · ·
61 }).Parse(langTemplate)
62 if err != nil {
63▶ return fmt.Errorf("failed to parse template file: %v", err)
64 }
65
· · ·
66 if err := t.Execute(buf, langs); err != nil {
67▶ return fmt.Errorf("failed to execute template file: %v", err)
68 }
69
+ 5 more matches in this file
4
5import (
6▶ "errors"
7 "fmt"
8 "os"
· · ·
16)
17
18▶func printShellCompletion(cmd *cobra.Command, command string) error {
19 switch command {
20 case "bash":
· · ·
27 return cmd.GenPowerShellCompletion(os.Stdout)
28 default:
29▶ return errors.New("Unknown shell: " + command)
30 }
31}
· · ·
65 b, err := os.ReadFile(filepath)
66 if err != nil {
67▶ fmt.Printf("Error reading flags from a file: %s\n", err)
68 os.Exit(1)
69 }
· · ·
642 err := printShellCompletion(rootCmd, args[3])
643 if err != nil {
644▶ _, _ = fmt.Fprintf(os.Stderr, "Error printing shell completion: %s\n", err)
645 }
646 return
+ 3 more matches in this file
35// parses them, and produces a matcher. Respects the existing --no-ignore
36// (Ignore) and --no-scc-ignore (SccIgnore) flag globals.
37▶func buildHistoryIgnore(repo *git.Repository, head plumbing.Hash) (*historyIgnore, error) {
38 commit, err := repo.CommitObject(head)
39 if err != nil {
· · ·
46
47 var patterns []gitignore.Pattern
48▶ err = tree.Files().ForEach(func(f *object.File) error {
49 if f.Mode == filemode.Dir || f.Mode == filemode.Submodule || f.Mode == filemode.Symlink {
50 return nil
4
5import (
6▶ "errors"
7 "fmt"
8 "io"
· · ·
11
12// validateHistoryFlags checks the global flag state for the history reports
13▶// (--hotspots, --by-author, --timeline). Hard errors are returned and should
14// abort the run; recoverable conditions are written to warnDst as a single
15// line each and execution continues.
· · ·
16▶func validateHistoryFlags(warnDst io.Writer) error {
17 if !Hotspots && !ByAuthor && !Timeline {
18 return nil
· · ·
20
21 if HistoryDepth < 0 {
22▶ return errors.New("--depth must be >= 0 (0 means entire history)")
23 }
24
· · ·
25 if Timeline && HistoryBuckets < 1 {
26▶ return errors.New("--buckets must be >= 1")
27 }
28
14 levelDebug
15 levelWarn
16▶ levelError
17)
18
· · ·
25 case levelWarn:
26 return "WARN"
27▶ case levelError:
28 return "ERROR"
29 default:
· · ·
28▶ return "ERROR"
29 default:
30 return ""
· · ·
92
93// Used when explicitly for os.exit output when crashing out
94▶func printError(msg string) {
95 doPrint(os.Stderr, levelError, msg, nil)
96}
· · ·
95▶ doPrint(os.Stderr, levelError, msg, nil)
96}
97
17func TestIsWhitespace(t *testing.T) {
18 if !isWhitespace(' ') {
19▶ t.Errorf("Expected to be true")
20 }
21}
· · ·
25
26 if !isBinary(0, 0) {
27▶ t.Errorf("Expected to be true")
28 }
29}
· · ·
33
34 if isBinary(0, 0) {
35▶ t.Errorf("Expected to be false")
36 }
37}
· · ·
51 CountStats(&fileJob)
52 if fileJob.Lines != 0 {
53▶ t.Errorf("Zero lines expected got %d", fileJob.Lines)
54 }
55
· · ·
60 CountStats(&fileJob)
61 if fileJob.Lines != 1 {
62▶ t.Errorf("One line expected got %d", fileJob.Lines)
63 }
64
+ 126 more matches in this file
23
24// ReadFile actually reads the file into a buffer size controlled by LargeByteCount
25▶func (reader *FileReader) ReadFile(path string, size int) ([]byte, error) {
26 fd, err := os.Open(path)
27 if err != nil {
· · ·
28▶ return nil, fmt.Errorf("error opening %s: %v", path, err)
29 }
30 defer func(file *os.File) {
· · ·
48 _, err = io.Copy(reader.Buffer, fd)
49 if err != nil {
50▶ return nil, fmt.Errorf("error reading %s: %v", path, err)
51 }
52
11## If this is an exe, change fileType, silentArgs, and validExitCodes
12
13▶$ErrorActionPreference = 'Stop'; # stop on all errors
14$packageArgs = @{
15 packageName = $env:ChocolateyPackageName
14)
15
16▶func writeTestFile(path, content string) error {
17 return os.WriteFile(path, []byte(content), 0o644)
18}
· · ·
37
38 if !data.GitAvailable {
39▶ t.Errorf("expected GitAvailable=true for fixture repo, got false")
40 }
41 if data.RepoName == "" {
· · ·
42▶ t.Errorf("expected RepoName to be derived from path, got empty")
43 }
44 if data.SccVersion == "" {
· · ·
45▶ t.Errorf("expected SccVersion to be set")
46 }
47 if data.GeneratedAt.IsZero() {
· · ·
48▶ t.Errorf("expected GeneratedAt to be set")
49 }
50
+ 89 more matches in this file
29}
30
31▶func runSCC(args ...string) (string, error) {
32 args = slices.Insert(args, 0, sccTestFlag)
33 cmd := exec.Command(sccBinPath, args...)
· · ·
265 }
266 if !strings.Contains(output, "MATLAB") {
267▶ t.Errorf("can not find MATLAB, output: %s", output)
268 }
269 if !strings.Contains(output, "Objective C") {
· · ·
270▶ t.Errorf("can not find Objective C, output:\n%s", output)
271 }
272}
· · ·
276 output, err := runSCC("--not-a-real-option")
277 if err == nil {
278▶ t.Fatal("scc should exit with error code")
279 }
280 if !strings.Contains(output, "Error: unknown flag: --not-a-real-option") {
· · ·
280▶ if !strings.Contains(output, "Error: unknown flag: --not-a-real-option") {
281 t.Fatalf("scc should report invalid options, output:\n%s", output)
282 }
+ 28 more matches in this file
18
19 if !strings.Contains(str.String(), "Estimated Schedule Effort (organic) 0.22 months") {
20▶ t.Error("expected to match got", str.String())
21 }
22}
· · ·
27
28 if !strings.Contains(str.String(), "Processed 1 bytes, 0.000 megabytes (SI)") {
29▶ t.Error("expected to match got", str.String())
30 }
31}
· · ·
36
37 if !strings.Contains(str.String(), "Processed 1000000 bytes, 1.000 megabytes (SI)") {
38▶ t.Error("expected to match got", str.String())
39 }
40}
· · ·
95
96 if summary.Files[0].Filename != "aaaa.go" {
97▶ t.Error("Sorting on lines failed", val)
98 }
99 }
· · ·
105
106 if summary.Files[0].Filename != "aaaa.go" {
107▶ t.Error("Sorting on blank failed", val)
108 }
109 }
+ 93 more matches in this file