From 716cf74864be0087751a188c9cb0820ce0eea59e Mon Sep 17 00:00:00 2001 From: Alexey Date: Thu, 23 Apr 2026 23:41:03 +0500 Subject: [PATCH] autocommit: pull-rebase before push to avoid races with other vault writers --- crates/mantra-ui/src/api/mod.rs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/crates/mantra-ui/src/api/mod.rs b/crates/mantra-ui/src/api/mod.rs index 766228f..9aaab66 100644 --- a/crates/mantra-ui/src/api/mod.rs +++ b/crates/mantra-ui/src/api/mod.rs @@ -440,14 +440,25 @@ async fn spawn_git_autocommit(source_slug: &str, para_id: &str) { let slug = source_slug.to_string(); let pid = para_id.to_string(); tokio::spawn(async move { + // Pull-rebase before push to avoid racing with other writers + // (e.g. Mac's vault-push timer). Rebase auto-stashes local + // changes so our pending commit survives. If rebase conflicts + // somehow (shouldn't for append-only files), the push will + // simply fail and we'll catch it on the next save. + let script = format!( + "set -e; cd {dir}; \ + git add -A; \ + git diff --cached --quiet && exit 0; \ + git commit -m 'note: {slug} {pid}'; \ + git pull --rebase --autostash origin main 2>&1 || true; \ + git push origin main 2>&1", + dir = shell_escape(&dir), + slug = shell_escape(&slug), + pid = shell_escape(&pid), + ); let _ = tokio::process::Command::new("sh") .arg("-c") - .arg(format!( - "cd {dir} && git add -A && git commit -m 'note: {slug} {pid}' && git push 2>&1", - dir = shell_escape(&dir), - slug = shell_escape(&slug), - pid = shell_escape(&pid), - )) + .arg(&script) .output() .await; });