Skip to content

Commit 060e9bd

Browse files
feat: auto-stash uncommitted changes during code update process
1 parent 1269469 commit 060e9bd

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

pkg/self/update.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,27 @@ func (su *ShellsUpdater) PullLatestCode() error {
297297
su.logger.Infow("Updates available", "commits_behind", behind)
298298
}
299299

300+
// Check for uncommitted changes and stash them automatically
301+
statusCmd := exec.Command("git", "-C", su.config.SourceDir, "status", "--porcelain")
302+
statusOutput, _ := statusCmd.Output()
303+
hasChanges := len(strings.TrimSpace(string(statusOutput))) > 0
304+
305+
if hasChanges {
306+
su.logger.Infow("Uncommitted changes detected - stashing automatically",
307+
"changes", string(statusOutput),
308+
)
309+
stashCmd := exec.Command("git", "-C", su.config.SourceDir, "stash", "push", "-m", "shells-self-update-auto-stash")
310+
if stashOutput, err := stashCmd.CombinedOutput(); err != nil {
311+
su.logger.Warnw("Failed to stash changes",
312+
"error", err,
313+
"output", string(stashOutput),
314+
)
315+
// Continue anyway - git pull might still work
316+
} else {
317+
su.logger.Infow("Changes stashed successfully")
318+
}
319+
}
320+
300321
// Pull the changes
301322
pullCmd := exec.Command("git", "-C", su.config.SourceDir, "pull", "origin", su.config.GitBranch)
302323
pullOutput, err := pullCmd.CombinedOutput()
@@ -311,6 +332,23 @@ func (su *ShellsUpdater) PullLatestCode() error {
311332
su.logger.Infow("Git pull completed",
312333
"output", strings.TrimSpace(string(pullOutput)),
313334
)
335+
336+
// Restore stashed changes if we stashed them
337+
if hasChanges {
338+
su.logger.Infow("Restoring stashed changes")
339+
popCmd := exec.Command("git", "-C", su.config.SourceDir, "stash", "pop")
340+
if popOutput, err := popCmd.CombinedOutput(); err != nil {
341+
su.logger.Warnw("Failed to restore stashed changes - may have conflicts",
342+
"error", err,
343+
"output", string(popOutput),
344+
"note", "Run 'git stash list' and 'git stash pop' manually in /opt/shells",
345+
)
346+
// Don't fail the update - stash is still available for manual recovery
347+
} else {
348+
su.logger.Infow("Stashed changes restored successfully")
349+
}
350+
}
351+
314352
return nil
315353
}
316354

0 commit comments

Comments
 (0)