-
Notifications
You must be signed in to change notification settings - Fork 364
Show the path from where the skill is loaded #2869
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
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package path | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| ) | ||
|
|
||
| // RelativeTo returns p relative to baseDir when both paths are absolute and | ||
| // filepath.Rel can compute a relative path. Otherwise it returns p cleaned. | ||
| func RelativeTo(p, baseDir string) string { | ||
| if p == "" { | ||
| return p | ||
| } | ||
|
|
||
| p = filepath.Clean(p) | ||
| if baseDir == "" || !filepath.IsAbs(p) || !filepath.IsAbs(baseDir) { | ||
| return p | ||
| } | ||
|
|
||
| rel, err := filepath.Rel(filepath.Clean(baseDir), p) | ||
| if err != nil { | ||
| return p | ||
| } | ||
| return rel | ||
| } | ||
|
|
||
| // ShortenHome replaces the current user's home directory prefix with "~". | ||
| func ShortenHome(p string) string { | ||
| homeDir, err := os.UserHomeDir() | ||
| if err != nil || homeDir == "" { | ||
| return p | ||
| } | ||
| return ShortenHomeDir(p, homeDir) | ||
| } | ||
|
|
||
| // ShortenHomeDir replaces a leading homeDir prefix with "~". | ||
| func ShortenHomeDir(p, homeDir string) string { | ||
| if p == "" || homeDir == "" { | ||
| return p | ||
| } | ||
|
|
||
| p = filepath.Clean(p) | ||
| homeDir = filepath.Clean(homeDir) | ||
| if !IsWithin(p, homeDir) { | ||
| return p | ||
| } | ||
|
|
||
| rel, err := filepath.Rel(homeDir, p) | ||
| if err != nil { | ||
| return p | ||
| } | ||
| if rel == "." { | ||
| return "~" | ||
| } | ||
| return filepath.Join("~", rel) | ||
| } | ||
|
|
||
| // IsWithin reports whether p is equal to dir or contained by dir. | ||
| func IsWithin(p, dir string) bool { | ||
| if p == "" || dir == "" { | ||
| return false | ||
| } | ||
|
|
||
| rel, err := filepath.Rel(filepath.Clean(dir), filepath.Clean(p)) | ||
| return err == nil && rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator)) | ||
| } | ||
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,37 @@ | ||
| package path | ||
|
|
||
| import ( | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestRelativeTo(t *testing.T) { | ||
| t.Parallel() | ||
| base := t.TempDir() | ||
|
|
||
| assert.Equal(t, filepath.Join("dir", "file.txt"), RelativeTo(filepath.Join(base, "dir", "file.txt"), base)) | ||
| assert.Equal(t, filepath.Join("..", "sibling"), RelativeTo(filepath.Join(filepath.Dir(base), "sibling"), base)) | ||
| assert.Equal(t, "relative/path", RelativeTo("relative/path", base)) | ||
| assert.Equal(t, filepath.Join(base, "file.txt"), RelativeTo(filepath.Join(base, "file.txt"), "relative/base")) | ||
| } | ||
|
|
||
| func TestShortenHomeDir(t *testing.T) { | ||
| t.Parallel() | ||
| home := t.TempDir() | ||
|
|
||
| assert.Equal(t, "~", ShortenHomeDir(home, home)) | ||
| assert.Equal(t, filepath.Join("~", ".agents", "skills", "commit"), ShortenHomeDir(filepath.Join(home, ".agents", "skills", "commit"), home)) | ||
| assert.Equal(t, filepath.Join(filepath.Dir(home), "elsewhere"), ShortenHomeDir(filepath.Join(filepath.Dir(home), "elsewhere"), home)) | ||
| } | ||
|
|
||
| func TestIsWithin(t *testing.T) { | ||
| t.Parallel() | ||
| base := t.TempDir() | ||
|
|
||
| assert.True(t, IsWithin(base, base)) | ||
| assert.True(t, IsWithin(filepath.Join(base, "child"), base)) | ||
| assert.False(t, IsWithin(filepath.Dir(base), base)) | ||
| assert.False(t, IsWithin(filepath.Join(filepath.Dir(base), filepath.Base(base)+"-suffix"), base)) | ||
| } |
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
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
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
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.
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.
[LOW] ShortenHome calls os.UserHomeDir() on every invocation
ShortenHomecallsos.UserHomeDir()directly on each call, which involves an environment variable lookup or syscall each time. The previousShortenPathhelper used the cached result frompaths.GetHomeDir()(an atomic pointer). Since this function is called from multiple TUI rendering paths (directorytree, editfile, listdirectory, readfile, readmultiplefiles, searchfilescontent) which may be invoked on every render frame, this is a minor performance regression. Consider caching the home dir at package init or accepting it as a parameter (asShortenHomeDiralready does).