Repository navigation
fix(trace): attribute commits made inside Bash calls #3631
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
Open
waveywaves
wants to merge
9
commits into
chainloop-dev:main
Choose a base branch
from
waveywaves:fix/trace-bash-commit-attribution
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,085
−57
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
18775fe
fix(trace): attribute commits made inside Bash calls
waveywaves 65f4468
fix(trace): address Bash attribution edge cases
waveywaves 7221941
fix(trace): ignore Git replacement objects
waveywaves 018d761
fix(trace): use GoGit for effective index
waveywaves 26737fd
docs(trace): document same-call attribution
waveywaves 5be09a6
fix(trace): handle GoGit index edge cases
waveywaves d7c8b99
fix(trace): harden worktree comparison
waveywaves f40bb26
fix(trace): reject symlinked tracked paths
waveywaves 05779be
fix(trace): narrow same-call attribution scope
waveywaves 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,56 @@ | ||
| // | ||
| // 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 claude | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| "github.com/chainloop-dev/chainloop/app/cli/internal/trace" | ||
| "github.com/chainloop-dev/chainloop/app/cli/internal/trace/state" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestTagShellCommand_PreservesInputAndExportsCallToken(t *testing.T) { | ||
| // Given | ||
| input := &trace.HookInput{ | ||
| Command: "git commit -m test", | ||
| RawToolInput: json.RawMessage(`{"command":"git commit -m test","description":"commit changes","timeout":120000}`), | ||
| } | ||
|
|
||
| // When | ||
| out := captureStdout(t, func() { | ||
| require.NoError(t, New().TagShellCommand(input, "opaque-token")) | ||
| }) | ||
|
|
||
| // Then | ||
| var response struct { | ||
| HookSpecificOutput struct { | ||
| HookEventName string `json:"hookEventName"` | ||
| UpdatedInput struct { | ||
| Command string `json:"command"` | ||
| Description string `json:"description"` | ||
| Timeout int `json:"timeout"` | ||
| } `json:"updatedInput"` | ||
| } `json:"hookSpecificOutput"` | ||
| } | ||
| require.NoError(t, json.Unmarshal([]byte(out), &response)) | ||
| require.Equal(t, eventPreToolUse, response.HookSpecificOutput.HookEventName) | ||
| require.Equal(t, "export "+state.ShellCallEnv+"=opaque-token; git commit -m test", | ||
| response.HookSpecificOutput.UpdatedInput.Command) | ||
| require.Equal(t, "commit changes", response.HookSpecificOutput.UpdatedInput.Description) | ||
| require.Equal(t, 120000, response.HookSpecificOutput.UpdatedInput.Timeout) | ||
| } |
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,83 @@ | ||
| // | ||
| // 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 ( | ||
| "fmt" | ||
| "io" | ||
|
|
||
| "github.com/go-git/go-git/v6/plumbing/filemode" | ||
| ) | ||
|
|
||
| // SnapshotIndex returns the content signature currently staged in git's index. | ||
| // It excludes symlinks and submodules to match SnapshotWorktree's file scope. | ||
| func (c *GoGitClient) SnapshotIndex(repoRoot string) (map[string]string, error) { | ||
| repo, err := openRepo(repoRoot) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("open repository: %w", err) | ||
| } | ||
| idx, err := repo.Storer.Index() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("load index: %w", err) | ||
| } | ||
|
|
||
| sig := make(map[string]string, len(idx.Entries)) | ||
| for _, entry := range idx.Entries { | ||
| if entry.Stage != 0 || !entry.Mode.IsFile() || entry.Mode == filemode.Symlink { | ||
| continue | ||
| } | ||
| sig[entry.Name] = entry.Hash.String() | ||
| } | ||
|
|
||
| return sig, nil | ||
| } | ||
|
|
||
| // IndexFile returns the exact staged content for path. | ||
| func (c *GoGitClient) IndexFile(repoRoot, path string) ([]byte, error) { | ||
| repo, err := openRepo(repoRoot) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("open repository: %w", err) | ||
| } | ||
| idx, err := repo.Storer.Index() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("load index: %w", err) | ||
| } | ||
|
|
||
| for _, entry := range idx.Entries { | ||
| if entry.Name != path || entry.Stage != 0 || !entry.Mode.IsFile() || entry.Mode == filemode.Symlink { | ||
| continue | ||
| } | ||
|
|
||
| blob, err := repo.BlobObject(entry.Hash) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("load staged blob %s: %w", path, err) | ||
| } | ||
| reader, err := blob.Reader() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("read staged blob %s: %w", path, err) | ||
| } | ||
| defer func() { _ = reader.Close() }() | ||
|
|
||
| content, err := io.ReadAll(reader) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("read staged content %s: %w", path, err) | ||
| } | ||
|
|
||
| return content, nil | ||
| } | ||
|
|
||
| return nil, fmt.Errorf("staged file %s not found", path) | ||
| } | ||
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,55 @@ | ||
| // | ||
| // 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 ( | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestGoGitClient_SnapshotIndexAndIndexFile_ReadStagedContent(t *testing.T) { | ||
| // Given | ||
| dir := t.TempDir() | ||
| runIndexGit(t, dir, "init", "-q") | ||
| require.NoError(t, os.WriteFile(filepath.Join(dir, "staged.txt"), []byte("staged content\n"), 0o600)) | ||
| runIndexGit(t, dir, "add", "staged.txt") | ||
|
|
||
| // When | ||
| client := NewGoGitClient() | ||
| snapshot, err := client.SnapshotIndex(dir) | ||
| require.NoError(t, err) | ||
| content, err := client.IndexFile(dir, "staged.txt") | ||
| require.NoError(t, err) | ||
|
|
||
| // Then | ||
| require.Contains(t, snapshot, "staged.txt") | ||
| require.Equal(t, []byte("staged content\n"), content) | ||
| } | ||
|
|
||
| func runIndexGit(t *testing.T, dir string, args ...string) string { | ||
| t.Helper() | ||
| cmd := exec.Command("git", args...) | ||
| cmd.Dir = dir | ||
| out, err := cmd.CombinedOutput() | ||
| require.NoError(t, err, "git %v: %s", args, string(out)) | ||
|
|
||
| return strings.TrimSpace(string(out)) | ||
| } |
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
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.