• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

boinger / confvis / 22011396192

14 Feb 2026 04:43AM UTC coverage: 83.992% (-0.05%) from 84.037%
22011396192

push

github

boinger
refactor: codebase cleanup — split impl_test.go, modernize idioms, update docs

- Split impl_test.go (5,038 lines) into 7 focused test files by command:
  generate, fetch, aggregate, gauge, check, comment_github, baseline
- Consolidate duplicate intPtrI/intPtrH test helpers (drop intPtrI)
- Replace interface{} with any in httpclient unexported methods
- Add compile-time interface compliance checks (var _ Fetcher = (*Client)(nil))
  for snyk, ghactions, gitleaks, gosec, trufflehog
- Update docs/architecture.md with missing CLI and source entries
- Track future feature ideas; remove completed modularize-gauge plan

2 of 2 new or added lines in 1 file covered. (100.0%)

75 existing lines in 7 files now uncovered.

3956 of 4710 relevant lines covered (83.99%)

10.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.3
/internal/gitutil/gitutil.go
1
// Package gitutil provides shared git helper functions used by baseline and history packages.
2
package gitutil
3

4
import (
5
        "bytes"
6
        "context"
7
        "errors"
8
        "fmt"
9
        "os/exec"
10
        "strings"
11
        "sync"
12
        "time"
13
)
14

15
// CommandTimeout is the timeout for git commands.
16
const CommandTimeout = 30 * time.Second
17

18
// ZeroSHA is the 40-character zero SHA used to indicate "ref does not exist"
19
// in git update-ref's compare-and-swap mode.
20
const ZeroSHA = "0000000000000000000000000000000000000000"
21

22
// ErrRefConflict is returned when a compare-and-swap write fails because the
23
// ref's current value doesn't match the expected old SHA.
24
var ErrRefConflict = errors.New("git ref conflict: current value differs from expected")
25

26
var (
27
        gitPath     string
28
        gitPathOnce sync.Once
29
)
30

31
// ResolveGitPath finds the git executable path once and caches it.
32
// This satisfies security scanners that warn about PATH-based command execution.
33
func ResolveGitPath() string {
44✔
34
        gitPathOnce.Do(func() {
45✔
35
                path, err := exec.LookPath("git")
1✔
36
                if err != nil {
1✔
UNCOV
37
                        // Fall back to "git" if LookPath fails (will use PATH at runtime)
×
UNCOV
38
                        gitPath = "git"
×
39
                } else {
1✔
40
                        gitPath = path
1✔
41
                }
1✔
42
        })
43
        return gitPath
44✔
44
}
45

46
// GetCurrentCommit returns the current git commit hash, or empty string if not in a repo.
47
func GetCurrentCommit() string {
1✔
48
        ctx, cancel := context.WithTimeout(context.Background(), CommandTimeout)
1✔
49
        defer cancel()
1✔
50

1✔
51
        cmd := exec.CommandContext(ctx, ResolveGitPath(), "rev-parse", "HEAD") //#nosec G204 -- git path resolved via exec.LookPath, args are internal
1✔
52
        var stdout bytes.Buffer
1✔
53
        cmd.Stdout = &stdout
1✔
54

1✔
55
        if err := cmd.Run(); err != nil {
1✔
UNCOV
56
                return ""
×
UNCOV
57
        }
×
58

59
        return strings.TrimSpace(stdout.String())
1✔
60
}
61

62
// GetCurrentBranch returns the current git branch name, or empty string if not on a branch.
63
func GetCurrentBranch() string {
1✔
64
        ctx, cancel := context.WithTimeout(context.Background(), CommandTimeout)
1✔
65
        defer cancel()
1✔
66

1✔
67
        cmd := exec.CommandContext(ctx, ResolveGitPath(), "rev-parse", "--abbrev-ref", "HEAD") //#nosec G204 -- git path resolved via exec.LookPath, args are internal
1✔
68
        var stdout bytes.Buffer
1✔
69
        cmd.Stdout = &stdout
1✔
70

1✔
71
        if err := cmd.Run(); err != nil {
1✔
UNCOV
72
                return ""
×
UNCOV
73
        }
×
74

75
        branch := strings.TrimSpace(stdout.String())
1✔
76
        // "HEAD" means detached head state
1✔
77
        if branch == "HEAD" {
1✔
UNCOV
78
                return ""
×
UNCOV
79
        }
×
80
        return branch
1✔
81
}
82

83
// IsGitRepo checks if the current directory is inside a git repository.
UNCOV
84
func IsGitRepo() bool {
×
UNCOV
85
        ctx, cancel := context.WithTimeout(context.Background(), CommandTimeout)
×
UNCOV
86
        defer cancel()
×
UNCOV
87

×
UNCOV
88
        cmd := exec.CommandContext(ctx, ResolveGitPath(), "rev-parse", "--git-dir") //#nosec G204 -- git path resolved via exec.LookPath, args are internal
×
UNCOV
89
        return cmd.Run() == nil
×
UNCOV
90
}
×
91

92
// RefExists checks if a git ref exists.
93
func RefExists(ref string) bool {
8✔
94
        ctx, cancel := context.WithTimeout(context.Background(), CommandTimeout)
8✔
95
        defer cancel()
8✔
96

8✔
97
        cmd := exec.CommandContext(ctx, ResolveGitPath(), "show-ref", "--verify", "--quiet", ref) //#nosec G204 -- git path resolved via exec.LookPath, args are internal
8✔
98
        return cmd.Run() == nil
8✔
99
}
8✔
100

101
// ReadRef resolves a git ref to its current SHA. Returns ("", false) if the
102
// ref does not exist.
103
func ReadRef(ref string) (string, bool) {
4✔
104
        ctx, cancel := context.WithTimeout(context.Background(), CommandTimeout)
4✔
105
        defer cancel()
4✔
106

4✔
107
        cmd := exec.CommandContext(ctx, ResolveGitPath(), "rev-parse", "--verify", ref) //#nosec G204 -- git path resolved via exec.LookPath, args are internal
4✔
108
        var stdout bytes.Buffer
4✔
109
        cmd.Stdout = &stdout
4✔
110

4✔
111
        if err := cmd.Run(); err != nil {
5✔
112
                return "", false
1✔
113
        }
1✔
114

115
        return strings.TrimSpace(stdout.String()), true
3✔
116
}
117

118
// ReadRefContent reads the blob content stored at a git ref. Returns
119
// (nil, nil) if the ref does not exist.
120
func ReadRefContent(ref string) ([]byte, error) {
7✔
121
        if !RefExists(ref) {
8✔
122
                return nil, nil
1✔
123
        }
1✔
124

125
        ctx, cancel := context.WithTimeout(context.Background(), CommandTimeout)
6✔
126
        defer cancel()
6✔
127

6✔
128
        cmd := exec.CommandContext(ctx, ResolveGitPath(), "cat-file", "-p", ref) //#nosec G204 -- git path resolved via exec.LookPath, args are internal
6✔
129
        var stdout, stderr bytes.Buffer
6✔
130
        cmd.Stdout = &stdout
6✔
131
        cmd.Stderr = &stderr
6✔
132

6✔
133
        if err := cmd.Run(); err != nil {
6✔
UNCOV
134
                return nil, fmt.Errorf("reading git ref %s: %w: %s", ref, err, stderr.String())
×
UNCOV
135
        }
×
136

137
        return stdout.Bytes(), nil
6✔
138
}
139

140
// WriteRef writes content as a blob and updates a git ref to point to it.
141
//
142
// The oldSHA parameter controls compare-and-swap behavior:
143
//   - "" (empty): unconditional write (no CAS)
144
//   - ZeroSHA: create-only (fails if ref already exists)
145
//   - any other SHA: CAS (fails if ref's current value != oldSHA)
146
//
147
// Returns ErrRefConflict if the CAS check fails.
148
func WriteRef(ref string, content []byte, oldSHA string) error {
12✔
149
        ctx, cancel := context.WithTimeout(context.Background(), CommandTimeout)
12✔
150
        defer cancel()
12✔
151

12✔
152
        // Step 1: Create a blob object with the content
12✔
153
        cmd := exec.CommandContext(ctx, ResolveGitPath(), "hash-object", "-w", "--stdin") //#nosec G204 -- git path resolved via exec.LookPath, args are internal
12✔
154
        cmd.Stdin = bytes.NewReader(content)
12✔
155
        var stdout, stderr bytes.Buffer
12✔
156
        cmd.Stdout = &stdout
12✔
157
        cmd.Stderr = &stderr
12✔
158

12✔
159
        if err := cmd.Run(); err != nil {
12✔
UNCOV
160
                return fmt.Errorf("creating git blob: %w: %s", err, stderr.String())
×
UNCOV
161
        }
×
162

163
        newSHA := strings.TrimSpace(stdout.String())
12✔
164

12✔
165
        // Step 2: Update the ref, optionally with CAS
12✔
166
        args := []string{"update-ref", ref, newSHA}
12✔
167
        if oldSHA != "" {
16✔
168
                args = append(args, oldSHA)
4✔
169
        }
4✔
170

171
        cmd = exec.CommandContext(ctx, ResolveGitPath(), args...) //#nosec G204 -- git path resolved via exec.LookPath, args are internal
12✔
172
        stderr.Reset()
12✔
173
        cmd.Stderr = &stderr
12✔
174

12✔
175
        if err := cmd.Run(); err != nil {
14✔
176
                if oldSHA != "" {
4✔
177
                        // CAS failure — the ref's current value didn't match oldSHA
2✔
178
                        return fmt.Errorf("%w: updating ref %s: %s", ErrRefConflict, ref, stderr.String())
2✔
179
                }
2✔
UNCOV
180
                return fmt.Errorf("updating git ref %s: %w: %s", ref, err, stderr.String())
×
181
        }
182

183
        return nil
10✔
184
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc