Skip to content

Commit 85eb304

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 85eb304

4 files changed

Lines changed: 8 additions & 8 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ var EnvironmentSyncFromUserTool = &Tool{
908908
}
909909

910910
var buf strings.Builder
911-
if err := repo.Apply(ctx, env.ID, &buf); err != nil {
911+
if err := repo.Apply(ctx, env.ID, &buf, true); err != nil {
912912
return nil, fmt.Errorf("unable to apply changes to user's worktree: %w\n\nlogs:\n%s", err, buf.String())
913913
}
914914

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)