add per-commit publishing from sync status
Rust / build (macos-latest, stable) (push) Waiting to run
Rust / build (ubuntu-latest, stable) (push) Waiting to run
Rust / build (windows-latest, stable) (push) Waiting to run
Rust / build (macos-latest, stable) (pull_request) Waiting to run
Rust / build (ubuntu-latest, stable) (pull_request) Waiting to run
Rust / build (windows-latest, stable) (pull_request) Waiting to run

This commit is contained in:
2026-11-19 05:14:52 +07:00
parent 41f34d0192
commit c7c1d0c741
13 changed files with 518 additions and 244 deletions
+65
View File
@@ -829,13 +829,78 @@ impl Repo {
refs.push((name.to_owned(), sync));
}
let ahead_commits = self.head_ahead_commits(remote_refs);
Ok(RepoSyncStatus {
refs,
ahead_total,
behind_total,
head: local_refs.head.clone(),
ahead_commits,
})
}
/// Commit ids reachable from HEAD but not from any remote ref, newest first.
fn head_ahead_commits(&self, remote_refs: &[(String, String)]) -> Vec<String> {
let Ok(head) = self.inner.head_id() else {
return Vec::new();
};
let mut hidden = Vec::with_capacity(remote_refs.len());
for (_, commit) in remote_refs {
let Some(id) = self.inner.rev_parse_single(commit.as_bytes()).ok() else {
return Vec::new();
};
let Ok(object) = id.object() else {
return Vec::new();
};
let Ok(commit) = object.peel_to_commit() else {
return Vec::new();
};
hidden.push(commit.id().detach());
}
let Ok(walk) = self.inner.rev_walk([head]).with_hidden(hidden).all() else {
return Vec::new();
};
let ahead: Vec<String> = walk
.filter_map(Result::ok)
.take(MAX_LISTED_COMMITS)
.map(|info| info.id().to_string())
.collect();
let Some(branch) = self.current_branch() else {
return Vec::new();
};
let name = format!("refs/heads/{branch}");
let Some((_, published)) = remote_refs.iter().find(|(refname, _)| refname == &name) else {
// A branch missing on Nostr creates nothing, so every commit is safe.
return ahead;
};
// None of the ahead commits can extend a tip HEAD no longer descends from.
let fast_forward = self
.merge_base(&head.to_string(), published)
.ok()
.flatten()
.is_some_and(|base| base == *published);
if !fast_forward {
return Vec::new();
}
ahead
.into_iter()
.filter(|commit| {
self.merge_base(published, commit)
.is_ok_and(|base| base.as_deref() == Some(published.as_str()))
})
.collect()
}
/// Classifies a branch present on both sides.
fn branch_sync(&self, local_commit: &str, remote_commit: &str) -> RefSync {
if local_commit == remote_commit {
+4
View File
@@ -18,6 +18,10 @@ pub struct RepoSyncStatus {
pub refs: Vec<(String, RefSync)>,
pub ahead_total: usize,
pub behind_total: usize,
/// Current branch short name of the checkout, `None` when detached.
pub head: Option<String>,
/// Ids of commits reachable from HEAD that publish the current branch as a fast-forward.
pub ahead_commits: Vec<String>,
}
impl RepoSyncStatus {
+140
View File
@@ -429,6 +429,146 @@ fn merge_base_finds_the_fork_point_and_reports_unrelated_history() {
assert!(repo.merge_base("orphan", "no-such-ref").is_err());
}
#[test]
fn sync_status_lists_commits_missing_from_nostr() {
let (dir, repo) = fixture(&[("a.txt", b"one")]);
commit_all(&repo, "initial");
let root = git_in(dir.path(), &["rev-parse", "HEAD"]).expect("root");
std::fs::write(dir.path().join("b.txt"), b"two").expect("write");
commit_all(&repo, "second");
let second = git_in(dir.path(), &["rev-parse", "HEAD"]).expect("second");
std::fs::write(dir.path().join("c.txt"), b"three").expect("write");
commit_all(&repo, "third");
let third = git_in(dir.path(), &["rev-parse", "HEAD"]).expect("third");
let branch = repo.current_branch().expect("branch");
let ahead = repo
.sync_status(&[(format!("refs/heads/{branch}"), root.clone())])
.expect("status");
assert_eq!(ahead.head.as_deref(), Some(branch.as_str()));
assert_eq!(ahead.ahead_total, 2);
assert_eq!(ahead.ahead_commits.len(), 2);
assert!(ahead.ahead_commits.contains(&second));
assert!(ahead.ahead_commits.contains(&third));
assert!(!ahead.ahead_commits.contains(&root));
let in_sync = repo
.sync_status(&[(format!("refs/heads/{branch}"), third)])
.expect("status");
assert!(in_sync.ahead_commits.is_empty());
let missing = repo
.sync_status(&[(format!("refs/heads/{branch}"), "0".repeat(40))])
.expect("status");
assert!(missing.ahead_commits.is_empty());
}
#[test]
fn publishing_a_commit_advances_the_remote_tip() {
let server = tempfile::tempdir().expect("tempdir");
let server_repo = bare_server(server.path(), "npub1test", "my-repo");
let (dir, repo) = fixture(&[("a.txt", b"one")]);
commit_all(&repo, "initial");
let root = git_in(dir.path(), &["rev-parse", "HEAD"]).expect("root");
std::fs::write(dir.path().join("b.txt"), b"two").expect("write");
commit_all(&repo, "second");
let second = git_in(dir.path(), &["rev-parse", "HEAD"]).expect("second");
std::fs::write(dir.path().join("c.txt"), b"three").expect("write");
commit_all(&repo, "third");
let third = git_in(dir.path(), &["rev-parse", "HEAD"]).expect("third");
let branch = repo.current_branch().expect("branch");
let refname = format!("refs/heads/{branch}");
let base_url = format!("file://{}", server.path().display());
let root_refspec = format!("+{root}:{refname}");
repo.push_refspecs(&base_url, "npub1test", "my-repo", &[root_refspec.as_str()])
.expect("publish root");
assert_eq!(
git_in(&server_repo, &["rev-parse", refname.as_str()]).expect("server tip"),
root
);
let status = repo
.sync_status(&[(refname.clone(), root.clone())])
.expect("status");
assert_eq!(status.ahead_commits.len(), 2);
assert!(status.ahead_commits.contains(&second));
assert!(status.ahead_commits.contains(&third));
let second_refspec = format!("+{second}:{refname}");
repo.push_refspecs(&base_url, "npub1test", "my-repo", &[second_refspec.as_str()])
.expect("publish second");
assert_eq!(
git_in(&server_repo, &["rev-parse", refname.as_str()]).expect("server tip"),
second
);
let status = repo
.sync_status(&[(refname, second.clone())])
.expect("status");
assert_eq!(status.ahead_commits, vec![third]);
}
#[test]
fn sync_status_skips_a_merged_commit_that_would_rewind_the_branch() {
let (dir, repo) = fixture(&[("a.txt", b"one")]);
commit_all(&repo, "initial");
let root = git_in(dir.path(), &["rev-parse", "HEAD"]).expect("root");
let branch = repo.current_branch().expect("branch");
let refname = format!("refs/heads/{branch}");
git_run(dir.path(), &["checkout", "-q", "-b", "side"]);
std::fs::write(dir.path().join("side.txt"), b"side").expect("write");
commit_all(&repo, "side work");
let side = git_in(dir.path(), &["rev-parse", "HEAD"]).expect("side");
git_run(dir.path(), &["checkout", "-q", branch.as_str()]);
std::fs::write(dir.path().join("main.txt"), b"main").expect("write");
commit_all(&repo, "main work");
let mainline = git_in(dir.path(), &["rev-parse", "HEAD"]).expect("mainline");
assert_ne!(root, mainline);
git_run(
dir.path(),
&["merge", "-q", "--no-ff", "-m", "merge side", "side"],
);
let merge = git_in(dir.path(), &["rev-parse", "HEAD"]).expect("merge");
let status = repo.sync_status(&[(refname, mainline)]).expect("status");
assert!(status.ahead_commits.contains(&merge));
assert!(!status.ahead_commits.contains(&root));
// Publishing the side commit would drop the merge and everything after it.
assert!(!status.ahead_commits.contains(&side));
}
#[test]
fn sync_status_skips_a_diverged_history() {
let (dir, repo) = fixture(&[("a.txt", b"one")]);
commit_all(&repo, "initial");
let root = git_in(dir.path(), &["rev-parse", "HEAD"]).expect("root");
let branch = repo.current_branch().expect("branch");
let refname = format!("refs/heads/{branch}");
std::fs::write(dir.path().join("published.txt"), b"published").expect("write");
commit_all(&repo, "published");
let published = git_in(dir.path(), &["rev-parse", "HEAD"]).expect("published");
git_run(dir.path(), &["reset", "-q", "--hard", root.as_str()]);
std::fs::write(dir.path().join("local.txt"), b"local").expect("write");
commit_all(&repo, "local");
let local = git_in(dir.path(), &["rev-parse", "HEAD"]).expect("local");
let status = repo.sync_status(&[(refname, published)]).expect("status");
assert!(status.ahead_commits.is_empty());
assert!(!status.ahead_commits.contains(&local));
}
#[test]
fn split_patch_series_splits_real_multi_commit_mboxes() {
let dir = tempfile::tempdir().expect("tempdir");