Repository navigation
fix(trace): do not count upstream lines when a shell command moves HEAD #3773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jiparis
merged 6 commits into
chainloop-dev:main
from
jiparis:jiparis/pfm-8131-stop-counting-upstream-lines-as-ai-generated-when-an-agent
Nov 4, 2026
+1,236
−32
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7ed4749
fix(trace): do not count upstream lines when a shell command moves HEAD
jiparis b76412a
fix(trace): address review of shell HEAD-move attribution
jiparis 066c66f
fix(trace): annotate gosec false positives in reflog reading
jiparis dcfce80
fix(trace): do not see a HEAD move when the pre-command HEAD was not …
jiparis 0e4bba9
perf(trace): cut git calls of shell HEAD-move attribution
jiparis d103e08
fix(trace): fall back to single reads when the batch reader fails
jiparis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| // | ||
| // Copyright 2026 The Chainloop Authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package git | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "os/exec" | ||
| "strconv" | ||
| "strings" | ||
| ) | ||
|
|
||
| // ErrUnbatchableName reports a name that a BlobReader cannot ask git for, | ||
| // because it holds a newline: read it in another way. | ||
| var ErrUnbatchableName = errors.New("name holds a newline") | ||
|
|
||
| // BlobReader reads files at commits through one long-running | ||
| // "git cat-file --batch" process, so that many reads cost one process, not | ||
| // several processes each. It is not safe for concurrent use. | ||
| type BlobReader struct { | ||
| cmd *exec.Cmd | ||
| in io.WriteCloser | ||
| out *bufio.Reader | ||
| // err is the first failure to talk to git. The output may then be out of | ||
| // step with the requests, so every later read fails too. | ||
| err error | ||
| } | ||
|
|
||
| // NewBlobReader starts a BlobReader for the repository at repoRoot. Close it | ||
| // when done. | ||
| func NewBlobReader(repoRoot string) (*BlobReader, error) { | ||
| cmd := exec.Command("git", "cat-file", "--batch") | ||
| cmd.Dir = repoRoot | ||
| in, err := cmd.StdinPipe() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("git cat-file stdin: %w", err) | ||
| } | ||
| out, err := cmd.StdoutPipe() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("git cat-file stdout: %w", err) | ||
| } | ||
| if err := cmd.Start(); err != nil { | ||
| return nil, fmt.Errorf("start git cat-file: %w", err) | ||
| } | ||
|
|
||
| return &BlobReader{cmd: cmd, in: in, out: bufio.NewReader(out)}, nil | ||
| } | ||
|
|
||
| // FileAt returns the content of path, repo-relative with forward slashes, at | ||
| // rev. It returns nil when rev does not resolve or does not hold path as a | ||
| // file, and ErrUnbatchableName when rev or path holds a newline. | ||
| func (r *BlobReader) FileAt(rev, path string) ([]byte, error) { | ||
| if r.err != nil { | ||
| return nil, r.err | ||
| } | ||
| if strings.ContainsAny(rev+path, "\n") { | ||
| return nil, fmt.Errorf("read %q at %q: %w", path, rev, ErrUnbatchableName) | ||
| } | ||
|
|
||
| content, err := r.read(rev + ":" + path) | ||
| if err != nil { | ||
| r.err = err | ||
| } | ||
|
|
||
| return content, err | ||
| } | ||
|
|
||
| func (r *BlobReader) read(name string) ([]byte, error) { | ||
| if _, err := fmt.Fprintf(r.in, "%s\n", name); err != nil { | ||
| return nil, fmt.Errorf("write git cat-file request: %w", err) | ||
| } | ||
|
|
||
| header, err := r.out.ReadString('\n') | ||
| if err != nil { | ||
| return nil, fmt.Errorf("read git cat-file header: %w", err) | ||
| } | ||
| header = strings.TrimSuffix(header, "\n") | ||
| // "<name> missing" or "<name> ambiguous": the name can hold spaces. | ||
| if strings.HasSuffix(header, " missing") || strings.HasSuffix(header, " ambiguous") { | ||
| return nil, nil | ||
| } | ||
|
|
||
| // "<oid> <type> <size>", then the content and a newline. | ||
| fields := strings.Fields(header) | ||
| if len(fields) != 3 { | ||
| return nil, fmt.Errorf("unexpected git cat-file header %q", header) | ||
| } | ||
| size, err := strconv.Atoi(fields[2]) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("unexpected git cat-file header %q: %w", header, err) | ||
| } | ||
| content := make([]byte, size+1) | ||
| if _, err := io.ReadFull(r.out, content); err != nil { | ||
| return nil, fmt.Errorf("read git cat-file content: %w", err) | ||
| } | ||
| if fields[1] != "blob" { | ||
| return nil, nil | ||
| } | ||
|
|
||
| return content[:size], nil | ||
| } | ||
|
|
||
| // Close stops the git process. | ||
| func (r *BlobReader) Close() error { | ||
| _ = r.in.Close() | ||
|
|
||
| return r.cmd.Wait() | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.