Repository navigation
Expand file tree
/
Copy pathtracker_doctor_git.go
More file actions
370 lines (349 loc) · 16.1 KB
/
Copy pathtracker_doctor_git.go
File metadata and controls
370 lines (349 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// ABOUTME: Doctor Git Requires check — previews the run-time git preflight gate.
// ABOUTME: Split from tracker_doctor.go (#453) — behavior-preserving extraction.
package tracker
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/2389-research/tracker/pipeline"
)
// checkGitRequires evaluates the workflow's `requires:` list against the
// current environment and the resolved --git= policy. Runs only when a
// pipeline file is provided. The result mirrors what would happen at
// `tracker run` time, so users can preview the gate without burning spend.
//
// Policy modeling matches pipeline.Preflight:
// - off → Skip (bypass)
// - auto + workflow doesn't require git → OK
// - require / init → forces the check regardless of workflow
// - missing git → Error (downgraded to Warn under warn policy)
// - missing repo + policy != init → Error (downgraded to Warn under warn)
// - missing repo + policy == init: model the auto-init outcome:
// safety latches would pass → OK with hint ("auto-init would
// create .git here at run start")
// safety latches would refuse → Error with the latch reason
// This avoids the false-positive Error the previous implementation
// reported when --git=init --allow-init would actually succeed.
func checkGitRequires(ctx context.Context, cfg DoctorConfig) CheckResult {
out := CheckResult{Name: "Git Requires"}
if cfg.PipelineFile == "" {
out.Status = CheckStatusSkip
out.Message = "no pipeline file provided"
return out
}
graph, loadMsg, ok := loadGraphForGitRequires(ctx, cfg.PipelineFile)
if !ok {
out.Status = CheckStatusSkip
out.Message = loadMsg
return out
}
policy := cfg.gitCfg.policy
requiresGit, hasUnknownDeps := scanGitRequiresDeps(&out, graph.RequiredDeps())
// Off-bypass comes AFTER the dep scan so unrecognized-dep warnings
// still surface under --git=off. Top-level Status escalates to Warn
// when any details warn, so `tracker doctor`'s exit code reflects
// the diagnostic (Doctor() counts CheckResult.Status, not Details).
if policy == GitPreflightOff {
return gitRequiresOffBypass(out, hasUnknownDeps)
}
if policy == GitPreflightRequire || policy == GitPreflightInit {
requiresGit = true
}
if !requiresGit {
return gitRequiresNotRequired(out, hasUnknownDeps)
}
return evaluateGitState(ctx, out, cfg, hasUnknownDeps)
}
// scanGitRequiresDeps classifies the workflow's declared deps, surfacing
// unrecognized entries as CheckDetail warnings so the doctor preview matches
// what runtime pipeline.Preflight emits on stderr. The off-bypass in the
// caller must not silence these — runtime preflight scans deps before its own
// off bypass for the same reason (forward-declared deps stay visible).
func scanGitRequiresDeps(out *CheckResult, deps []string) (requiresGit, hasUnknownDeps bool) {
for _, d := range deps {
switch strings.ToLower(strings.TrimSpace(d)) {
case "":
// empty entry; skip
case "git":
requiresGit = true
default:
hasUnknownDeps = true
out.Details = append(out.Details, CheckDetail{
Status: CheckStatusWarn,
Message: fmt.Sprintf("requires %q is not yet implemented; runtime will warn and continue", d),
})
}
}
return requiresGit, hasUnknownDeps
}
// gitRequiresOffBypass renders the --git=off result, still escalating to Warn
// when unrecognized deps were surfaced.
func gitRequiresOffBypass(out CheckResult, hasUnknownDeps bool) CheckResult {
if hasUnknownDeps {
out.Status = CheckStatusWarn
out.Message = "--git=off; bypassing git check (unrecognized requires: entries surfaced as warnings)"
} else {
out.Status = CheckStatusSkip
out.Message = "--git=off; bypassing"
}
return out
}
// gitRequiresNotRequired renders the result when git isn't required; unknown
// deps may still warrant a top-level Warn.
func gitRequiresNotRequired(out CheckResult, hasUnknownDeps bool) CheckResult {
if hasUnknownDeps {
out.Status = CheckStatusWarn
out.Message = "workflow does not require git; unrecognized requires: entries surfaced as warnings"
} else {
out.Status = CheckStatusOK
out.Message = "workflow does not require git"
}
return out
}
// evaluateGitState probes the working directory's git state and routes to the
// matching remediation: probe failure, git-not-installed, bare repo, no repo
// (auto-init preview), or born-HEAD verification.
func evaluateGitState(ctx context.Context, out CheckResult, cfg DoctorConfig, hasUnknownDeps bool) CheckResult {
policy := cfg.gitCfg.policy
installed, isRepo, isBare, probeErr := probeGitForDoctor(ctx, cfg.WorkDir)
if probeErr != nil {
// ctx cancellation or unexpected execution failure. Treat as Error
// so the operator sees the actual cause rather than a misleading
// "not a repo" preview.
out.Status = CheckStatusError
out.Message = fmt.Sprintf("git probe failed: %v", probeErr)
out.Hint = "if the context was cancelled, retry; otherwise investigate the PATH/permissions"
return out
}
if !installed {
out.Status = doctorStatusForPolicy(policy, CheckStatusError)
out.Message = "workflow requires git, but git is not in PATH"
out.Hint = "install git (brew install git / apt install git / https://git-scm.com)"
return out
}
if isBare {
// Inside a bare repo (or .git dir): "git init" here is wrong; the
// user needs to operate from a checkout/worktree. Distinct
// remediation from the plain non-repo case below.
out.Status = doctorStatusForPolicy(policy, CheckStatusError)
out.Message = fmt.Sprintf("workflow requires a working tree; %s is a bare git repository (no work tree)", cfg.WorkDir)
out.Hint = "cd into a checkout of this repo (clone or worktree) and run from there"
return out
}
if !isRepo {
return gitRequiresNoRepo(ctx, out, cfg, hasUnknownDeps)
}
return gitRequiresBornHEAD(ctx, out, cfg, hasUnknownDeps)
}
// gitRequiresNoRepo handles a workdir that is not inside a repo: model the
// auto-init outcome under --git=init --allow-init, else report the plain
// not-a-repo error.
func gitRequiresNoRepo(ctx context.Context, out CheckResult, cfg DoctorConfig, hasUnknownDeps bool) CheckResult {
policy := cfg.gitCfg.policy
if policy == GitPreflightInit && cfg.gitCfg.allowInit {
return gitRequiresAutoInitPreview(ctx, out, cfg, hasUnknownDeps)
}
out.Status = doctorStatusForPolicy(policy, CheckStatusError)
out.Message = fmt.Sprintf("workflow requires a git repository; %s is not inside one", cfg.WorkDir)
// Offer both paths so a workdir with existing files doesn't end up with a
// born-but-empty HEAD (Copilot:3261104615) — the `--allow-empty` path is
// correct only for an empty workdir; a dir with source files almost always
// wants `git add .` first.
out.Hint = "run `git init && git add . && git commit -m initial` to capture existing files, OR `git init && git commit --allow-empty -m initial` for an empty baseline, OR `tracker <workflow> --git=init --allow-init` in an empty directory"
return out
}
// gitRequiresAutoInitPreview models what runAutoInit would do under
// --git=init --allow-init: it gates on (a) safety latches and (b) the
// workdir-content latch (refuses if the dir has files outside `.git`), so
// `doctor --git=init --allow-init` doesn't say OK where the runtime refuses.
func gitRequiresAutoInitPreview(ctx context.Context, out CheckResult, cfg DoctorConfig, hasUnknownDeps bool) CheckResult {
if latchErr := pipeline.SafetyLatches(ctx, cfg.WorkDir); latchErr != nil {
out.Status = CheckStatusError
out.Message = fmt.Sprintf("auto-init would refuse: %v", latchErr)
out.Hint = "cd into a project subdirectory, or run `git init` manually"
return out
}
hasContent, contentErr := pipeline.WorkdirHasContent(cfg.WorkDir)
if contentErr != nil {
out.Status = CheckStatusError
out.Message = fmt.Sprintf("could not scan workdir for auto-init preview: %v", contentErr)
out.Hint = "check filesystem permissions on the workdir"
return out
}
if hasContent {
out.Status = CheckStatusError
out.Message = fmt.Sprintf("auto-init would refuse: %s is not empty (auto-init makes an empty initial commit; user files would stay untracked)", cfg.WorkDir)
out.Hint = "stage your own initial commit: `git init && git add . && git commit -m initial`"
return out
}
// Auto-init preview is OK. Preserve unknown-dependency warn severity at
// the check level (CodeRabbit:3260803551) — pre-fix this branch returned
// CheckStatusOK unconditionally, suppressing the warning even though the
// individual unrecognized-dep warnings had been emitted.
if hasUnknownDeps {
out.Status = CheckStatusWarn
out.Message = fmt.Sprintf("workflow requires git; --git=init --allow-init would auto-init %s at run start (unrecognized requires: entries surfaced as warnings)", cfg.WorkDir)
} else {
out.Status = CheckStatusOK
out.Message = fmt.Sprintf("workflow requires git; --git=init --allow-init would auto-init %s at run start", cfg.WorkDir)
}
out.Hint = ".git will be created here at run start, before the first node executes"
return out
}
// gitRequiresBornHEAD handles a workdir that IS a repo: verify HEAD is born —
// the same probe Preflight uses, so the doctor preview agrees with the runtime
// check (Copilot:3260568737).
func gitRequiresBornHEAD(ctx context.Context, out CheckResult, cfg DoctorConfig, hasUnknownDeps bool) CheckResult {
policy := cfg.gitCfg.policy
born, headErr := pipeline.HasBornHEAD(ctx, cfg.WorkDir)
if headErr != nil {
out.Status = CheckStatusError
out.Message = fmt.Sprintf("could not verify HEAD: %v", headErr)
out.Hint = "retry if ctx was cancelled; otherwise investigate the repo state"
return out
}
if !born {
out.Status = doctorStatusForPolicy(policy, CheckStatusError)
out.Message = fmt.Sprintf("workflow requires a git repository with at least one commit; %s has no commits (unborn HEAD)", cfg.WorkDir)
// Quote the workdir so paths containing spaces / special chars
// produce copy/pasteable commands (Copilot:3260796999,
// CodeRabbit:3260803559).
out.Hint = fmt.Sprintf("create an initial commit: `git -C %q commit --allow-empty -m initial` (or `git -C %q add . && git -C %q commit -m initial` to capture existing files)", cfg.WorkDir, cfg.WorkDir, cfg.WorkDir)
return out
}
if hasUnknownDeps {
// Git satisfied but unknown deps still warrant a top-level Warn
// so `tracker doctor`'s exit code reflects the diagnostic.
out.Status = CheckStatusWarn
out.Message = "workflow requires git; env satisfies it (unrecognized requires: entries surfaced as warnings)"
} else {
out.Status = CheckStatusOK
out.Message = "workflow requires git; env satisfies it"
}
return out
}
// loadGraphForGitRequires loads the entry graph from a `.dip` source file
// OR a `.dipx` bundle, returning the same `*pipeline.Graph` shape either
// way. The doctor's Git Requires check needs `graph.RequiredDeps()` and
// nothing else — running tracker's full bundle validation would duplicate
// what checkPipelineBundle already covers. Returns (nil, "...", false)
// when the file can't be loaded; the caller maps that to CheckStatusSkip.
//
// .dipx bundles are routed through pipeline.LoadDipxBundle so a
// `tracker doctor <bundle.dipx>` invocation accurately previews what
// runtime preflight (which also goes through LoadDipxBundle internally
// in the loader path) would see. Pre-fix the .dipx branch fell through
// to parsePipelineSource which choked on ZIP bytes and silently Skip'd
// — bundle inputs got no Git Requires preview at all.
func loadGraphForGitRequires(ctx context.Context, pipelineFile string) (*pipeline.Graph, string, bool) {
if strings.EqualFold(filepath.Ext(pipelineFile), ".dipx") {
entry, _, _, _, err := pipeline.LoadDipxBundle(ctx, pipelineFile)
if err != nil {
return nil, fmt.Sprintf("cannot load bundle %s: %v", pipelineFile, err), false
}
return entry, "", true
}
fileBytes, err := os.ReadFile(pipelineFile)
if err != nil {
if info, ok := builtinForDoctor(pipelineFile); ok {
return loadBuiltinForGitRequires(info)
}
return nil, fmt.Sprintf("cannot read %s: %v", pipelineFile, err), false
}
graph, err := parsePipelineFile(string(fileBytes), pipelineFile)
if err != nil {
return nil, fmt.Sprintf("cannot parse %s: %v", pipelineFile, err), false
}
return graph, "", true
}
// loadBuiltinForGitRequires is the bare-built-in-name branch of
// loadGraphForGitRequires: the workflow and its sidecars come from the embed FS.
func loadBuiltinForGitRequires(info WorkflowInfo) (*pipeline.Graph, string, bool) {
graph, err := loadEmbeddedGraph(info)
if err != nil {
return nil, fmt.Sprintf("cannot parse built-in %s: %v", info.Name, err), false
}
return graph, "", true
}
// doctorStatusForPolicy maps preflight policy to a CheckStatus, downgrading
// to warn when policy == warn.
func doctorStatusForPolicy(policy GitPreflight, hardStatus CheckStatus) CheckStatus {
if policy == GitPreflightWarn {
return CheckStatusWarn
}
return hardStatus
}
// probeGitForDoctor performs the same two probes as pipeline.checkGit but
// without reaching into the pipeline-internal helper. Local copy keeps the
// doctor file's dependency surface clean.
//
// Uses `--is-inside-work-tree` (NOT `--git-dir`) so bare repositories don't
// count as "repo OK": workflows that declare `requires: git` need a work
// tree for `git commit`/`git merge`, both of which fail in a bare repo.
// Matches the pipeline.checkGit fix from the same review pass.
// probeGitForDoctor returns (installed, isRepo, isBare, err). Mirrors
// pipeline.checkGit's contract:
// - installed=false means git missing from PATH (benign)
// - isRepo=true means inside a real work tree
// - isBare=true means inside a bare repo or .git directory (no work tree)
// - all three false means outside any repo (plain dir)
// - err is non-nil only on cancellation or unexpected execution failure;
// "not a repo" stderr discrimination keeps dubious-ownership /
// safe.directory failures from being mis-classified as benign
//
// Bare distinction is necessary so the doctor preview can emit the right
// remediation: bare-repo users need "cd into a checkout," not "git init."
func probeGitForDoctor(ctx context.Context, workDir string) (installed bool, isRepo bool, isBare bool, err error) {
if _, lerr := exec.LookPath("git"); lerr != nil {
if errors.Is(lerr, exec.ErrNotFound) {
return false, false, false, nil
}
return false, false, false, fmt.Errorf("locate git in PATH: %w", lerr)
}
cmd := exec.CommandContext(ctx, "git", "-C", workDir, "rev-parse", "--is-inside-work-tree")
// Strip sensitive env AND force LANG/LC_ALL=C so the "not a git
// repository" classifier below can rely on stable English stderr
// regardless of operator locale. Pre-fix the doctor on a localized
// install would have reported a plain non-repo as `git rev-parse
// refused: <translated phrase>` instead of the documented
// not-a-repo remediation.
cmd.Env = pipeline.GitProbeEnv()
out, runErr := cmd.Output()
if runErr == nil {
return classifyGitWorkTree(string(out))
}
return classifyGitProbeError(ctx, runErr)
}
// classifyGitWorkTree maps successful `rev-parse --is-inside-work-tree` stdout
// to the (installed, isRepo, isBare) probe contract.
func classifyGitWorkTree(out string) (installed bool, isRepo bool, isBare bool, err error) {
stdout := strings.TrimSpace(out)
switch stdout {
case "true":
return true, true, false, nil
case "false":
return true, false, true, nil
default:
return true, false, false, fmt.Errorf("git rev-parse --is-inside-work-tree: unexpected output %q", stdout)
}
}
// classifyGitProbeError discriminates a failed git probe: context
// cancellation, benign "not a repo" stderr, dubious-ownership / safe.directory
// refusals, and unexpected execution failures.
func classifyGitProbeError(ctx context.Context, runErr error) (installed bool, isRepo bool, isBare bool, err error) {
if ctxErr := ctx.Err(); ctxErr != nil {
return true, false, false, ctxErr
}
var exitErr *exec.ExitError
if errors.As(runErr, &exitErr) {
if strings.Contains(string(exitErr.Stderr), "not a git repository") {
return true, false, false, nil // expected — outside any repo
}
return true, false, false, fmt.Errorf("git rev-parse refused: %s", strings.TrimSpace(string(exitErr.Stderr)))
}
return true, false, false, fmt.Errorf("git rev-parse --is-inside-work-tree: %w", runErr)
}