helix icon indicating copy to clipboard operation
helix copied to clipboard

Protect against overwriting external changes

Open kov opened this issue 2 years ago • 4 comments

When saving, check that the file has not changed before our last known save before writing the file. Only overwrite external changes if asked to force.

I've almost lost a bunch of work because I did it on a separate instance of helix in another terminal window, but had the same file opened on an earlier instance and saved the file without realizing. Thankfully I had a backup (a cat in another tab xD)

kov avatar Nov 13 '22 22:11 kov

I think this should be configurable: I don't quite remember the context where this has happened to me previously (maybe it involved containers, virtual machines or external formatters) but if you're in a situation where mtime isn't reliable then you have to fight the editor to save files every time.

the-mikedavis avatar Jan 17 '23 15:01 the-mikedavis

Perhaps we could check if the buffer's content differs from the file's if the file was written at a later mtime?

kirawi avatar Jan 17 '23 16:01 kirawi

I think this should be configurable: I don't quite remember the context where this has happened to me previously (maybe it involved containers, virtual machines or external formatters) but if you're in a situation where mtime isn't reliable then you have to fight the editor to save files every time.

That sounds reasonable and something I didn't think about. Adding a simple configuration flag like editor.enable-overwrite-protection should be pretty trivial. I think it would be good to enable that by default tough. mtime being unreliable is probably an exception rather than the rule.

pascalkuthe avatar Jan 17 '23 16:01 pascalkuthe

Perhaps we could check if the buffer's content differs from the file's if the file was written at a later mtime?

That would work but I think that would add a bunch of extra overhead. Especially when writing large files so probably not ideal. I think a config option as @the-mikedavis suggested should be good enough as mtime usually works fine and if mtime doesn't work it usually doesn't ever work so you would want to just want to totally disable the check in the config then.

I can't really conceive how you would get a false positive here (false negatives are possible because any fs operation is fundamentally racy but there is no way around that) but in the unlikely case they occur you can simply use :w! which is not too bad.

pascalkuthe avatar Jan 17 '23 16:01 pascalkuthe

Since a lot of people are waiting on this feature, i took the liberty to apply requested feedback and merge with the latest master: https://github.com/divarvel/helix/tree/protect-against-overwrites

@kov I have opened a PR against your branch, but the merge from master makes it a bit hard to read. You can also cherry-pick commits if you want and rebase everything on top of a recent master (the conflicts are trivial to solve).

@pascalkuthe i can also open a new PR and rebase everything on top of the latest master, if that's simpler for maintainers.

divarvel avatar Feb 03 '23 14:02 divarvel

Since a lot of people are waiting on this feature, i took the liberty to apply requested feedback and merge with the latest master: divarvel/helix@protect-against-overwrites

@kov I have opened a PR against your branch, but the merge from master makes it a bit hard to read. You can also cherry-pick commits if you want and rebase everything on top of a recent master (the conflicts are trivial to solve).

@pascalkuthe i can also open a new PR and rebase everything on top of the latest master, if that's simpler for maintainers.

Since there wasn't repose here in a while you can just open a new PR that supersedes. Aslong as you keep the original commits around to give credit and give credit in the PR description that is fine.

pascalkuthe avatar Feb 03 '23 14:02 pascalkuthe

Alright, done in https://github.com/helix-editor/helix/pull/5805

divarvel avatar Feb 03 '23 14:02 divarvel

I'm closing this in favour of #5805 thank you =)

kov avatar Feb 05 '23 01:02 kov