Cleanup previous PR

Keep edit_loop free of side effects: it now reports whether the editor
was started and edit_and_upload decides whether to keep the local copy.
Print the recovery path verbatim instead of Go-quoted.
This commit is contained in:
Kovid Goyal 2026-11-01 11:06:40 +05:30
parent 72dddbd463
commit a65a01e099
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
3 changed files with 22 additions and 14 deletions

View file

@ -162,10 +162,23 @@ func get_editor() []string {
return []string{"vim"}
}
func edit_loop(master *ControlMaster, editor []string) (err error) {
// edit_and_upload runs the edit loop and, if the final upload fails after the
// editor was started, keeps the local copy so the user's edits are not lost.
func edit_and_upload(master *ControlMaster, editor []string) error {
editor_started, err := edit_loop(master, editor)
if err != nil && editor_started {
master.keep_local_copy = true
return fmt.Errorf("%w\nYour edited file is saved at: %s", err, master.Dest)
}
return err
}
// edit_loop returns whether the editor was started, so callers can tell
// failures before editing from failures to upload the edits.
func edit_loop(master *ControlMaster, editor []string) (editor_started bool, err error) {
st, err := os.Stat(master.Dest)
if err != nil {
return err
return false, err
}
mtime := st.ModTime()
// Mirrors main.py handle_action's edit branch: reset the terminal (clear
@ -176,14 +189,8 @@ func edit_loop(master *ControlMaster, editor []string) (err error) {
cmd := exec.Command(argv[0], argv[1:]...)
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
if err := cmd.Start(); err != nil {
return err
return false, err
}
defer func() {
master.keep_local_copy = err != nil
if err != nil {
err = fmt.Errorf("%w\nYour edited file is saved at: %q", err, master.Dest)
}
}()
done := make(chan error, 1)
go func() { done <- cmd.Wait() }()
for {
@ -192,11 +199,11 @@ func edit_loop(master *ControlMaster, editor []string) (err error) {
reset_terminal()
if master.IsAlive() {
if err := master.Upload(false); err != nil {
return fmt.Errorf("failed to upload %s: %w", master.remote_path, err)
return true, fmt.Errorf("failed to upload %s: %w", master.remote_path, err)
}
return nil
return true, nil
}
return fmt.Errorf("failed to upload %s, SSH master process died", master.remote_path)
return true, fmt.Errorf("failed to upload %s, SSH master process died", master.remote_path)
case <-time.After(100 * time.Millisecond):
if st, err := os.Stat(master.Dest); err == nil && st.ModTime().After(mtime) {
mtime = st.ModTime()

View file

@ -352,7 +352,7 @@ func handle_action(action string, opts *Options) (result string, err error) {
master_show_error(master, "Failed to download "+remote_path)
return "", nil
}
if err := edit_loop(master, get_editor()); err != nil {
if err := edit_and_upload(master, get_editor()); err != nil {
master_show_error(master, err.Error())
}
case "save":

View file

@ -129,6 +129,7 @@ func TestPythonSplitExt(t *testing.T) {
}
}
}
func TestRemoteEditFinalUpload(t *testing.T) {
const ssh_script = `#!/bin/sh
for arg in "$@"; do
@ -194,7 +195,7 @@ printf '%s' "$1" > "$KITTY_TEST_EDITED_PATH"
}
err := func() error {
defer master.Close()
return edit_loop(master, []string{editor})
return edit_and_upload(master, []string{editor})
}()
if (err != nil) != tc.want_error {
t.Fatalf("edit_loop error: %v, want error: %v", err, tc.want_error)