Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions core/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type Interface interface {
ApplyPatch(ctx context.Context, patch []byte) error
RevParse(ctx context.Context, ref string) (string, error)
Commit(ctx context.Context, message string, options ...string) error
Clean(ctx context.Context) error
Reset(ctx context.Context) error
SubmoduleUpdate(ctx context.Context) error
FileHashes(ctx context.Context, ref string) (map[string][]byte, error)
}
Expand Down Expand Up @@ -102,6 +104,20 @@ func (c *impl) Commit(ctx context.Context, message string, options ...string) er
return c.runner.run(ctx, c.directory, "git", args...)
}

// Clean removes untracked files and directories (git clean -fdx).
func (c *impl) Clean(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, _gitTimeout)
defer cancel()
return c.runner.run(ctx, c.directory, "git", "clean", "-fdx")
}

// Reset discards all local changes (git reset --hard).
func (c *impl) Reset(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, _gitTimeout)
defer cancel()
return c.runner.run(ctx, c.directory, "git", "reset", "--hard")
}

// SubmoduleUpdate updates the submodules in the repository.
func (c *impl) SubmoduleUpdate(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, _gitTimeout)
Expand Down
28 changes: 28 additions & 0 deletions core/git/gitmock/gitmock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion core/workspace/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,15 @@ func (w *workspace) ApplyRequests(ctx context.Context, requests []Request) error
return nil
}

// Checkout checks out the given reference in the workspace.
// Checkout resets the workspace to a clean state and checks out the given ref.
func (w *workspace) Checkout(ctx context.Context, remote string, ref string) error {
if err := w.git.Reset(ctx); err != nil {
return fmt.Errorf("git reset --hard: %w", err)
}
if err := w.git.Clean(ctx); err != nil {

@xytan0056 xytan0056 Feb 26, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this potentially can be slow to delete, if there are folders like "bazel-out" or "pkg/mod/" stuff. But i see current TA does this too. And it's necessary if request is cancel in the middle of git checkout, repo is in a dirty state. We can try this out.
Another way is to delete the whole WS and ask repo manager to re-clone with --local. We can do this if clean -fdx is too slow
wdyt? @yushan8

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'll helpful to benchmark the performance after and see which one is faster with reseting the workspace

return fmt.Errorf("git clean -fdx: %w", err)
}

// check if base ref exists in local repository
commit := fmt.Sprintf("%s^{commit}", ref)
_, err := w.git.RevParse(ctx, commit)
Expand Down
5 changes: 4 additions & 1 deletion core/workspace/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ func TestWorkspace_Checkout_RevParseSuccess(t *testing.T) {

ref := "abc123"
commitRef := ref + "^{commit}"
g.EXPECT().Reset(gomock.Any()).Return(nil)
g.EXPECT().Clean(gomock.Any()).Return(nil)
g.EXPECT().RevParse(gomock.Any(), commitRef).Return("abc123", nil)
// expect Checkout with commit hash and workspace path as option
g.EXPECT().Checkout(gomock.Any(), "abc123").Return(nil)

err := w.Checkout(context.Background(), "origin", ref)
Expand All @@ -89,6 +90,8 @@ func TestWorkspace_Checkout_FetchError(t *testing.T) {
ref := "refs/heads/feature"
commitRef := ref + "^{commit}"

g.EXPECT().Reset(gomock.Any()).Return(nil)
g.EXPECT().Clean(gomock.Any()).Return(nil)
g.EXPECT().RevParse(gomock.Any(), commitRef).Return("", errors.New("missing"))
g.EXPECT().Fetch(gomock.Any(), remote, ref).Return(errors.New("fetch failed"))

Expand Down