mirror of
https://github.com/Dasemu/Grove.git
synced 2026-11-27 07:46:13 +00:00
fix(core): purge own snapshot refs on every push URL incl. remote-only ones; LC_ALL=C and no submodule recursion on pushes
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2b8a8c498d
commit
bdf0219842
3 files changed
+126
-15
No files matched your search
@@ -63,7 +63,8 @@ impl Git {
|
|||||||
|
|
||||||
fn command<S: AsRef<OsStr>>(&self, args: &[S]) -> Command {
|
fn command<S: AsRef<OsStr>>(&self, args: &[S]) -> Command {
|
||||||
let mut cmd = Command::new("git");
|
let mut cmd = Command::new("git");
|
||||||
cmd.current_dir(&self.dir).args(args);
|
// Fixed locale: callers parse git's output and messages.
|
||||||
|
cmd.current_dir(&self.dir).args(args).env("LC_ALL", "C");
|
||||||
for (k, v) in &self.envs {
|
for (k, v) in &self.envs {
|
||||||
cmd.env(k, v);
|
cmd.env(k, v);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ use crate::inspect::{facts, state};
|
|||||||
use crate::layout::dir_name;
|
use crate::layout::dir_name;
|
||||||
use crate::snapshot::ref_name;
|
use crate::snapshot::ref_name;
|
||||||
use crate::state::State;
|
use crate::state::State;
|
||||||
|
use std::collections::BTreeSet;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
@@ -94,14 +95,24 @@ pub fn push_snapshot(worktree: &Path, machine: &str, remote: &str) -> Result<(),
|
|||||||
.probe(&["symbolic-ref", "--short", "-q", "HEAD"])?
|
.probe(&["symbolic-ref", "--short", "-q", "HEAD"])?
|
||||||
.ok_or_else(|| GitError::Other("detached HEAD".into()))?;
|
.ok_or_else(|| GitError::Other("detached HEAD".into()))?;
|
||||||
let r = ref_name(machine, &branch);
|
let r = ref_name(machine, &branch);
|
||||||
g.run(&["push", "-q", "--force", remote, &format!("{r}:{r}")])?;
|
g.run(&[
|
||||||
|
"-c",
|
||||||
|
"push.recurseSubmodules=no",
|
||||||
|
"push",
|
||||||
|
"-q",
|
||||||
|
"--force",
|
||||||
|
remote,
|
||||||
|
&format!("{r}:{r}"),
|
||||||
|
])?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deletes this machine's snapshot refs (`refs/sync/<machine>/*`, never anything else) from
|
/// Deletes this machine's snapshot refs (`refs/sync/<machine>/*`, never anything else) from every
|
||||||
/// `remote` and then locally; used when snapshots must no longer be published. A ref the remote
|
/// push URL of `remote` and then locally; used when snapshots must no longer be published.
|
||||||
/// does not have counts as deleted. Returns the refs removed; on a failed push the local ref is
|
/// Refs that exist only on the remote side are removed too. Each URL is pushed separately and
|
||||||
/// kept so the next call retries. Errors carry the first failure.
|
/// counts as clean only when a fresh `ls-remote` shows none of this machine's refs; any other
|
||||||
|
/// outcome (including "repository does not exist") is a failure that keeps the local refs so
|
||||||
|
/// the next call retries. Returns the refs removed.
|
||||||
pub fn purge_own_snapshots(
|
pub fn purge_own_snapshots(
|
||||||
gitdir: &Path,
|
gitdir: &Path,
|
||||||
machine: &str,
|
machine: &str,
|
||||||
@@ -109,22 +120,62 @@ pub fn purge_own_snapshots(
|
|||||||
) -> Result<Vec<String>, GitError> {
|
) -> Result<Vec<String>, GitError> {
|
||||||
let g = Git::new(gitdir);
|
let g = Git::new(gitdir);
|
||||||
let prefix = format!("refs/sync/{machine}/");
|
let prefix = format!("refs/sync/{machine}/");
|
||||||
let refs = g.run(&["for-each-ref", "--format=%(refname)", &prefix])?;
|
let pattern = format!("{prefix}*");
|
||||||
let mut done = Vec::new();
|
let own = |text: &str, refs: &mut BTreeSet<String>| {
|
||||||
|
for l in text.lines() {
|
||||||
|
if let Some(r) = l.split_whitespace().last()
|
||||||
|
&& r.starts_with(&prefix)
|
||||||
|
{
|
||||||
|
refs.insert(r.to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let mut all = BTreeSet::new();
|
||||||
|
own(
|
||||||
|
&g.run(&["for-each-ref", "--format=%(refname)", &prefix])?,
|
||||||
|
&mut all,
|
||||||
|
);
|
||||||
|
let urls = g.run(&["remote", "get-url", "--push", "--all", remote])?;
|
||||||
let mut failure = None;
|
let mut failure = None;
|
||||||
for r in refs.lines().filter(|r| r.starts_with(&prefix)) {
|
for url in urls.lines().map(str::trim).filter(|u| !u.is_empty()) {
|
||||||
match g.run(&["push", "-q", remote, &format!(":{r}")]) {
|
let mut there = BTreeSet::new();
|
||||||
Ok(_) => {}
|
match g.run(&["ls-remote", url, &pattern]) {
|
||||||
Err(e) if e.to_string().contains("does not exist") => {}
|
Ok(t) => own(&t, &mut there),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
failure.get_or_insert(e);
|
failure.get_or_insert(e);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
g.run(&["update-ref", "-d", r])?;
|
all.extend(there.iter().cloned());
|
||||||
done.push(r.to_string());
|
if there.is_empty() {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
failure.map_or(Ok(done), Err)
|
let mut args = vec!["-c", "push.recurseSubmodules=no", "push", "-q", url];
|
||||||
|
let specs: Vec<String> = there.iter().map(|r| format!(":{r}")).collect();
|
||||||
|
args.extend(specs.iter().map(String::as_str));
|
||||||
|
let pushed = g.run(&args);
|
||||||
|
let mut left = BTreeSet::new();
|
||||||
|
match g.run(&["ls-remote", url, &pattern]) {
|
||||||
|
Ok(t) => own(&t, &mut left),
|
||||||
|
Err(e) => {
|
||||||
|
failure.get_or_insert(e);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !left.is_empty() {
|
||||||
|
failure.get_or_insert(pushed.err().unwrap_or_else(|| {
|
||||||
|
GitError::Other(format!("{url}: snapshot refs still present after delete"))
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let Some(e) = failure {
|
||||||
|
return Err(e);
|
||||||
|
}
|
||||||
|
let local = g.run(&["for-each-ref", "--format=%(refname)", &prefix])?;
|
||||||
|
for r in local.lines().filter(|r| r.starts_with(&prefix)) {
|
||||||
|
g.run(&["update-ref", "-d", r])?;
|
||||||
|
}
|
||||||
|
Ok(all.into_iter().collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
|||||||
@@ -294,3 +294,62 @@ fn migrated_repo_with_linked_worktrees_syncs_only_what_it_can() {
|
|||||||
assert!(rep.archived.is_empty(), "{:?}", rep.archived);
|
assert!(rep.archived.is_empty(), "{:?}", rep.archived);
|
||||||
assert!(outside.join("a.txt").exists());
|
assert!(outside.join("a.txt").exists());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn purge_removes_remote_only_own_refs_and_nothing_else() {
|
||||||
|
use grove_core::receive::purge_own_snapshots;
|
||||||
|
let base = tmpdir("purge");
|
||||||
|
let remote = base.join("r.git");
|
||||||
|
Git::new(&base)
|
||||||
|
.run(&[
|
||||||
|
"init",
|
||||||
|
"-q",
|
||||||
|
"--bare",
|
||||||
|
"-b",
|
||||||
|
"main",
|
||||||
|
remote.to_str().unwrap(),
|
||||||
|
])
|
||||||
|
.unwrap();
|
||||||
|
let seed = init(&base.join("seed"));
|
||||||
|
seed.run(&["remote", "add", "origin", remote.to_str().unwrap()])
|
||||||
|
.unwrap();
|
||||||
|
commit(&seed, "a.txt", "1\n");
|
||||||
|
seed.run(&["push", "-q", "-u", "origin", "main"]).unwrap();
|
||||||
|
// Own refs: one with a local copy, one only on the remote. Plus another machine's ref.
|
||||||
|
seed.run(&["push", "-q", "origin", "main:refs/sync/A/main"])
|
||||||
|
.unwrap();
|
||||||
|
seed.run(&["push", "-q", "origin", "main:refs/sync/A/feat/x"])
|
||||||
|
.unwrap();
|
||||||
|
seed.run(&["push", "-q", "origin", "main:refs/sync/B/main"])
|
||||||
|
.unwrap();
|
||||||
|
seed.run(&["update-ref", "refs/sync/A/main", "HEAD"])
|
||||||
|
.unwrap();
|
||||||
|
let done = purge_own_snapshots(seed.dir(), "A", "origin").unwrap();
|
||||||
|
assert_eq!(done, vec!["refs/sync/A/feat/x", "refs/sync/A/main"]);
|
||||||
|
let refs = Git::new(&remote)
|
||||||
|
.run(&["for-each-ref", "--format=%(refname)"])
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(refs, "refs/heads/main\nrefs/sync/B/main");
|
||||||
|
assert!(
|
||||||
|
seed.run(&["for-each-ref", "refs/sync/A/"])
|
||||||
|
.unwrap()
|
||||||
|
.is_empty()
|
||||||
|
);
|
||||||
|
// A remote that cannot be reached is a failure and keeps the local refs.
|
||||||
|
seed.run(&["update-ref", "refs/sync/A/main", "HEAD"])
|
||||||
|
.unwrap();
|
||||||
|
seed.run(&[
|
||||||
|
"remote",
|
||||||
|
"set-url",
|
||||||
|
"origin",
|
||||||
|
base.join("nope.git").to_str().unwrap(),
|
||||||
|
])
|
||||||
|
.unwrap();
|
||||||
|
assert!(purge_own_snapshots(seed.dir(), "A", "origin").is_err());
|
||||||
|
assert!(
|
||||||
|
!seed
|
||||||
|
.run(&["for-each-ref", "refs/sync/A/"])
|
||||||
|
.unwrap()
|
||||||
|
.is_empty()
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in new issue
Block a user