-
Notifications
You must be signed in to change notification settings - Fork 1
fix(project): case-sensitive isGitRepo + injectable GitChecker #163
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
neversettle17-101
wants to merge
2
commits into
main
Choose a base branch
from
feat/issue-97
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.
+131
−26
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package project | ||
|
|
||
| // Test-only handles to unexported internals, so external tests can assert the | ||
| // isGitRepo behavior without widening the package's public surface. | ||
|
|
||
| // SamePathForTest exposes samePath to tests. | ||
| var SamePathForTest = samePath |
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,53 @@ | ||
| package project | ||
|
|
||
| import ( | ||
| "os/exec" | ||
| "path/filepath" | ||
| "runtime" | ||
| "strings" | ||
| ) | ||
|
|
||
| // GitChecker reports whether a filesystem path is the root of a git repository. | ||
| // It is the seam that lets the project Service be exercised without a real git | ||
| // binary or working tree. | ||
| type GitChecker interface { | ||
| IsRepo(path string) bool | ||
| } | ||
|
|
||
| // isGitRepo reports whether path is a git repository root, using the production | ||
| // git checker. Free-function workspace registration calls this directly; the | ||
| // Service's own repo check goes through the injectable GitChecker seam instead. | ||
| func isGitRepo(path string) bool { return execGitChecker{}.IsRepo(path) } | ||
|
|
||
| // execGitChecker is the production GitChecker: it shells out to git. | ||
| type execGitChecker struct{} | ||
|
|
||
| func (execGitChecker) IsRepo(path string) bool { | ||
| cmd := exec.Command("git", "-C", path, "rev-parse", "--show-toplevel") | ||
| out, err := cmd.Output() | ||
| if err != nil { | ||
| return false | ||
| } | ||
| top := filepath.Clean(strings.TrimSpace(string(out))) | ||
| path = filepath.Clean(path) | ||
| top, err = filepath.EvalSymlinks(top) | ||
| if err != nil { | ||
| return false | ||
| } | ||
| path, err = filepath.EvalSymlinks(path) | ||
| if err != nil { | ||
| return false | ||
| } | ||
| return samePath(top, path) | ||
| } | ||
|
|
||
| // samePath compares two cleaned, symlink-resolved paths. It is case-insensitive | ||
| // only on filesystems that are conventionally case-insensitive (macOS, Windows); | ||
| // on case-sensitive filesystems (Linux), "/home/u/Repo" and "/home/u/repo" are | ||
| // distinct directories and must not be treated as equal. | ||
| func samePath(a, b string) bool { | ||
| if runtime.GOOS == "darwin" || runtime.GOOS == "windows" { | ||
| return strings.EqualFold(a, b) | ||
| } | ||
| return a == b | ||
| } | ||
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,60 @@ | ||
| package project_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "runtime" | ||
| "testing" | ||
|
|
||
| "github.com/aoagents/agent-orchestrator/backend/internal/service/project" | ||
| "github.com/aoagents/agent-orchestrator/backend/internal/storage/sqlite" | ||
| ) | ||
|
|
||
| // fakeGitChecker is a GitChecker that answers from an in-memory set of repo | ||
| // paths — no git binary, no working tree. It is the seam that lets the project | ||
| // service be exercised in a unit test. | ||
| type fakeGitChecker struct{ repos map[string]bool } | ||
|
|
||
| func (f fakeGitChecker) IsRepo(path string) bool { return f.repos[path] } | ||
|
|
||
| func newManagerWithGit(t *testing.T, git project.GitChecker) project.Manager { | ||
| t.Helper() | ||
| store, err := sqlite.Open(t.TempDir()) | ||
| if err != nil { | ||
| t.Fatalf("open store: %v", err) | ||
| } | ||
| t.Cleanup(func() { _ = store.Close() }) | ||
| return project.NewWithGitChecker(store, git) | ||
| } | ||
|
|
||
| func TestAdd_UsesInjectedGitChecker(t *testing.T) { | ||
| ctx := context.Background() | ||
| dir := t.TempDir() | ||
| m := newManagerWithGit(t, fakeGitChecker{repos: map[string]bool{dir: true}}) | ||
|
|
||
| // A path the fake recognizes as a repo is accepted — without shelling out to git. | ||
| if _, err := m.Add(ctx, project.AddInput{Path: dir, ProjectID: ptr("ao")}); err != nil { | ||
| t.Fatalf("Add on a fake-recognized repo: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestAdd_RejectsNonRepoViaGitChecker(t *testing.T) { | ||
| ctx := context.Background() | ||
| m := newManagerWithGit(t, fakeGitChecker{repos: map[string]bool{}}) | ||
|
|
||
| _, err := m.Add(ctx, project.AddInput{Path: t.TempDir(), ProjectID: ptr("ao")}) | ||
| wantCode(t, err, "NOT_A_GIT_REPO") | ||
| } | ||
|
|
||
| // TestSamePath_CaseSensitivity guards the isGitRepo fix: paths differing only in | ||
| // case must be treated as distinct on case-sensitive filesystems (Linux) and as | ||
| // equal on the conventionally case-insensitive ones (macOS, Windows). | ||
| func TestSamePath_CaseSensitivity(t *testing.T) { | ||
| // Document the platform contract so a regression in samePath is caught here. | ||
| caseInsensitive := runtime.GOOS == "darwin" || runtime.GOOS == "windows" | ||
|
neversettle17-101 marked this conversation as resolved.
|
||
| if got := project.SamePathForTest("/a/Repo", "/a/repo"); got != caseInsensitive { | ||
| t.Fatalf("samePath(/a/Repo, /a/repo) = %v on %s, want %v", got, runtime.GOOS, caseInsensitive) | ||
| } | ||
| if !project.SamePathForTest("/a/repo", "/a/repo") { | ||
| t.Fatalf("samePath of identical paths must be true") | ||
| } | ||
| } | ||
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.
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.