Skip to content

Commit 77a2330

Browse files
committed
discard local changes when re-syncing to env
we literally just migrated their unstaged changes to the env, so this should be equivalent and much more foolproof Signed-off-by: Alex Suraci <suraci.alex@gmail.com>
1 parent b362d18 commit 77a2330

4 files changed

Lines changed: 10 additions & 15 deletions

File tree

cmd/container-use/apply.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ git commit -m "Add backend API implementation"`,
4343

4444
env := args[0]
4545

46-
if err := repo.Apply(ctx, env, os.Stdout); err != nil {
46+
if err := repo.Apply(ctx, env, os.Stdout, false); err != nil {
4747
return fmt.Errorf("failed to apply environment: %w", err)
4848
}
4949

environment/integration/merge_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func TestRepositoryApply(t *testing.T) {
7979

8080
// Apply the environment (squash merge)
8181
var applyOutput bytes.Buffer
82-
err = repo.Apply(ctx, env.ID, &applyOutput)
82+
err = repo.Apply(ctx, env.ID, &applyOutput, true)
8383
require.NoError(t, err, "Apply should succeed: %s", applyOutput.String())
8484

8585
// Verify we're still on the initial branch
@@ -146,7 +146,7 @@ func TestRepositoryApplyNonExistent(t *testing.T) {
146146

147147
// Try to apply non-existent environment
148148
var applyOutput bytes.Buffer
149-
err := repo.Apply(ctx, "non-existent-env", &applyOutput)
149+
err := repo.Apply(ctx, "non-existent-env", &applyOutput, true)
150150
assert.Error(t, err, "Applying non-existent environment should fail")
151151
assert.Contains(t, err.Error(), "not found")
152152
})
@@ -203,7 +203,7 @@ func TestRepositoryApplyWithConflicts(t *testing.T) {
203203

204204
// Try to apply - this should fail due to conflict
205205
var applyOutput bytes.Buffer
206-
err = repo.Apply(ctx, env.ID, &applyOutput)
206+
err = repo.Apply(ctx, env.ID, &applyOutput, true)
207207

208208
// The apply should fail due to conflict
209209
assert.Error(t, err, "Apply should fail due to conflict")

mcpserver/tools.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ var EnvironmentEnableTrackingTool = &Tool{
867867
var EnvironmentSyncFromUserTool = &Tool{
868868
Definition: newEnvironmentTool(
869869
"environment_sync_from_user",
870-
"Apply the user's unstaged changes to the environment and apply the environment's to the user's local worktree. ONLY RUN WHEN EXPLICITLY REQUESTED BY THE USER.",
870+
"Apply the user's unstaged changes to the environment. ONLY RUN WHEN EXPLICITLY REQUESTED BY THE USER.",
871871
),
872872
Handler: func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
873873
repo, env, err := openEnvironment(ctx, request)
@@ -899,17 +899,12 @@ var EnvironmentSyncFromUserTool = &Tool{
899899
return nil, fmt.Errorf("failed to pull changes to environment: %w", err)
900900
}
901901

902-
if err := repo.Update(ctx, env, request.GetString("explanation", "")); err != nil {
903-
return nil, fmt.Errorf("unable to update the environment: %w", err)
904-
}
905-
906902
if err := repo.ResetUserLocalChanges(ctx); err != nil {
907903
return nil, fmt.Errorf("unable to reset user's worktree: %w", err)
908904
}
909905

910-
var buf strings.Builder
911-
if err := repo.Apply(ctx, env.ID, &buf); err != nil {
912-
return nil, fmt.Errorf("unable to apply changes to user's worktree: %w\n\nlogs:\n%s", err, buf.String())
906+
if err := repo.Update(ctx, env, request.GetString("explanation", "")); err != nil {
907+
return nil, fmt.Errorf("unable to update the environment: %w", err)
913908
}
914909

915910
return mcp.NewToolResultText("Patch applied successfully to the environment:\n\n```patch\n" + string(patch) + "\n```"), nil

repository/repository.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ func (r *Repository) Update(ctx context.Context, env *environment.Environment, e
293293
if currentBranch == env.State.TrackingBranch {
294294
// Apply environment changes to the user's working tree
295295
var logs strings.Builder
296-
if err := r.Apply(ctx, env.ID, &logs); err != nil {
296+
if err := r.Apply(ctx, env.ID, &logs, false); err != nil {
297297
return fmt.Errorf("failed to apply tracking changes to working tree: %w\n\nlogs:\n%s\n", err, logs.String())
298298
}
299299
}
@@ -431,7 +431,7 @@ func (r *Repository) Merge(ctx context.Context, id string, w io.Writer) error {
431431
return RunInteractiveGitCommand(ctx, r.userRepoPath, w, "merge", "--no-ff", "--autostash", "-m", "Merge environment "+envInfo.ID, "--", "container-use/"+envInfo.ID)
432432
}
433433

434-
func (r *Repository) Apply(ctx context.Context, id string, w io.Writer) (rerr error) {
434+
func (r *Repository) Apply(ctx context.Context, id string, w io.Writer, discard bool) (rerr error) {
435435
envInfo, err := r.Info(ctx, id)
436436
if err != nil {
437437
return err
@@ -468,7 +468,7 @@ func (r *Repository) Apply(ctx context.Context, id string, w io.Writer) (rerr er
468468
}
469469

470470
// Apply user changes back
471-
if hasUnstagedChanges {
471+
if hasUnstagedChanges && !discard {
472472
fmt.Fprintf(w, "Restoring user changes...\n")
473473

474474
// 1. Temporarily commit the agent's changes

0 commit comments

Comments
 (0)