-
Notifications
You must be signed in to change notification settings - Fork 881
Relativize CycloneDX-JSON file component paths against --base-path #4966
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
AgentGymLeader
wants to merge
2
commits into
anchore:main
Choose a base branch
from
AgentGymLeader:fix/cyclonedx-json-base-path
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.
Open
Changes from all commits
Commits
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,58 @@ | ||
| package internal | ||
|
|
||
| import ( | ||
| "path" | ||
| "strings" | ||
| ) | ||
|
|
||
| // Rel returns a forward-slash relative path from base to target, equivalent to | ||
| // filepath.Rel but for already-cleaned, forward-slash paths. Both base and target | ||
| // should be absolute-style (leading "/") paths. The result preserves ".." segments | ||
| // for targets that escape the base, so symlinks whose real path lives outside the | ||
| // scanned base directory are represented correctly (e.g. "../foo"). | ||
| func splitPath(p string) []string { | ||
| parts := strings.Split(strings.TrimPrefix(p, "/"), "/") | ||
| out := parts[:0] | ||
| for _, s := range parts { | ||
| if s != "" { | ||
| out = append(out, s) | ||
| } | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| func Rel(base, target string) string { | ||
| if base == target { | ||
| return "." | ||
| } | ||
|
|
||
| baseSegs := splitPath(base) | ||
| targSegs := splitPath(target) | ||
|
|
||
| // Find the length of the common prefix. | ||
| n := 0 | ||
| for n < len(baseSegs) && n < len(targSegs) && baseSegs[n] == targSegs[n] { | ||
| n++ | ||
| } | ||
|
|
||
| // Build up-segments for any base segments beyond the common prefix. | ||
| up := make([]string, len(baseSegs)-n) | ||
| for i := range up { | ||
| up[i] = ".." | ||
| } | ||
|
|
||
| result := strings.Join(append(up, targSegs[n:]...), "/") | ||
| if result == "" { | ||
| return "." | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| // ConvertAbsoluteToRelative strips the leading "/" from an absolute path. | ||
| // If the path is already relative it is returned unchanged. | ||
| func ConvertAbsoluteToRelative(absPath string) string { | ||
| if !path.IsAbs(absPath) { | ||
| return absPath | ||
| } | ||
| return strings.TrimPrefix(absPath, "/") | ||
| } |
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,102 @@ | ||
| package internal | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestConvertAbsoluteToRelative(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| absPath string | ||
| want string | ||
| }{ | ||
| { | ||
| name: "absolute path", | ||
| absPath: "/usr/bin/foo", | ||
| want: "usr/bin/foo", | ||
| }, | ||
| { | ||
| name: "relative path", | ||
| absPath: "relative/path/bar", | ||
| want: "relative/path/bar", | ||
| }, | ||
| { | ||
| name: "root path", | ||
| absPath: "/", | ||
| want: "", | ||
| }, | ||
| { | ||
| name: "dot relative path", | ||
| absPath: "./foo", | ||
| want: "./foo", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got := ConvertAbsoluteToRelative(tt.absPath) | ||
| assert.Equal(t, tt.want, got) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestRel(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| base string | ||
| target string | ||
| want string | ||
| }{ | ||
| { | ||
| name: "escaping symlink: target is sibling of base parent", | ||
| base: "/root/subdir", | ||
| target: "/root/foo", | ||
| want: "../foo", | ||
| }, | ||
| { | ||
| name: "target is child of base", | ||
| base: "/root/subdir", | ||
| target: "/root/subdir/foo", | ||
| want: "foo", | ||
| }, | ||
| { | ||
| name: "base equals target", | ||
| base: "/root", | ||
| target: "/root", | ||
| want: ".", | ||
| }, | ||
| { | ||
| name: "siblings under common parent", | ||
| base: "/root/a", | ||
| target: "/root/b", | ||
| want: "../b", | ||
| }, | ||
| { | ||
| name: "target is immediate child of root", | ||
| base: "/", | ||
| target: "/foo", | ||
| want: "foo", | ||
| }, | ||
| { | ||
| name: "deeper nesting", | ||
| base: "/a/b/c", | ||
| target: "/a/d/e", | ||
| want: "../../d/e", | ||
| }, | ||
| { | ||
| name: "target equals base with trailing content", | ||
| base: "/a/b", | ||
| target: "/a/b/c/d", | ||
| want: "c/d", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got := Rel(tt.base, tt.target) | ||
| assert.Equal(t, tt.want, got) | ||
| }) | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can we avoid these import renames?