new layout (wip) (#34)
Reviewed-on: #34
This commit was merged in pull request #34.
This commit is contained in:
@@ -727,7 +727,7 @@ impl Backend {
|
||||
cx: &mut Context<Self>,
|
||||
) -> Task<Result<PushOutcome, Error>> {
|
||||
let path = Mirrors::path(&announcement.addr());
|
||||
self.push_repo_from(announcement, path, None, GitPushPolicy::Reconcile, cx)
|
||||
self.push_repo_from(announcement, path, None, GitPushPolicy::Reconcile, None, cx)
|
||||
}
|
||||
|
||||
/// Republishes the mirror and deletes grasp refs the mirror does not have.
|
||||
@@ -737,14 +737,22 @@ impl Backend {
|
||||
cx: &mut Context<Self>,
|
||||
) -> Task<Result<PushOutcome, Error>> {
|
||||
let path = Mirrors::path(&announcement.addr());
|
||||
self.push_repo_from(announcement, path, None, GitPushPolicy::ResetRemote, cx)
|
||||
self.push_repo_from(
|
||||
announcement,
|
||||
path,
|
||||
None,
|
||||
GitPushPolicy::ResetRemote,
|
||||
None,
|
||||
cx,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn push_checkout(
|
||||
pub fn push_checkout_commit(
|
||||
&mut self,
|
||||
announcement: Announcement,
|
||||
checkout: PathBuf,
|
||||
announced_head: Option<String>,
|
||||
commit: String,
|
||||
cx: &mut Context<Self>,
|
||||
) -> Task<Result<PushOutcome, Error>> {
|
||||
self.push_repo_from(
|
||||
@@ -752,6 +760,7 @@ impl Backend {
|
||||
checkout,
|
||||
announced_head,
|
||||
GitPushPolicy::Reconcile,
|
||||
Some(commit),
|
||||
cx,
|
||||
)
|
||||
}
|
||||
@@ -762,6 +771,7 @@ impl Backend {
|
||||
path: PathBuf,
|
||||
announced_head: Option<String>,
|
||||
policy: GitPushPolicy,
|
||||
tip: Option<String>,
|
||||
cx: &mut Context<Self>,
|
||||
) -> Task<Result<PushOutcome, Error>> {
|
||||
let addr = announcement.addr();
|
||||
@@ -791,18 +801,104 @@ impl Backend {
|
||||
}
|
||||
});
|
||||
|
||||
let mut state = {
|
||||
let work = cx.background_spawn({
|
||||
let path = path.clone();
|
||||
async move { SyncState::from_repo(&path) }
|
||||
});
|
||||
work.await?
|
||||
};
|
||||
state.policy = policy;
|
||||
let mut state = match tip {
|
||||
Some(commit) => {
|
||||
let owner = announcement.owner.to_bech32().map_err(|error| {
|
||||
anyhow!("could not encode the repository owner: {error}")
|
||||
})?;
|
||||
|
||||
// Fetch grasp refs the local repository lacks before the state event is signed,
|
||||
// so the servers' full ref map is declared.
|
||||
state.reconcile(&announcement, cx).await?;
|
||||
let urls: Vec<String> = announcement
|
||||
.relays
|
||||
.iter()
|
||||
.filter_map(grasp_base_url)
|
||||
.map(|base| format!("{base}/{owner}/{}.git", announcement.id))
|
||||
.collect();
|
||||
|
||||
let ls_path = path.clone();
|
||||
let target = commit.clone();
|
||||
|
||||
let (branch, published) = cx
|
||||
.background_spawn(async move {
|
||||
let repo = Repo::open(&ls_path)?;
|
||||
|
||||
let Some(branch) = repo.current_branch() else {
|
||||
bail!("the checkout has a detached HEAD");
|
||||
};
|
||||
|
||||
let Some(tip) = repo.head() else {
|
||||
bail!("the checkout has no commits");
|
||||
};
|
||||
|
||||
if repo.merge_base(&tip, &target)? != Some(target.clone()) {
|
||||
bail!("commit {target} is not on {branch}");
|
||||
}
|
||||
|
||||
for url in &urls {
|
||||
if let Ok(refs) = repo.ls_remote(url) {
|
||||
let name = format!("refs/heads/{branch}");
|
||||
if let Some((_, published)) =
|
||||
refs.iter().find(|(refname, _)| refname == &name)
|
||||
{
|
||||
let base = repo.merge_base(published, &target).map_err(
|
||||
|error| {
|
||||
anyhow!(
|
||||
"could not compare {target} with the \
|
||||
published {branch}: {error}"
|
||||
)
|
||||
},
|
||||
)?;
|
||||
if base.as_deref() != Some(published.as_str()) {
|
||||
bail!(
|
||||
"commit {target} would rewind {branch} on the \
|
||||
grasp servers"
|
||||
);
|
||||
}
|
||||
}
|
||||
return Ok::<_, Error>((branch, refs));
|
||||
}
|
||||
}
|
||||
bail!("could not read the published refs from any grasp server")
|
||||
})
|
||||
.await?;
|
||||
|
||||
let name = format!("refs/heads/{branch}");
|
||||
let commit = Sha1Hash::from_str(&commit)
|
||||
.map_err(|_| anyhow!("invalid commit: {commit}"))?;
|
||||
|
||||
let mut refs = published
|
||||
.into_iter()
|
||||
.map(|(name, commit)| GitRef::new(name, &commit))
|
||||
.collect::<Result<Vec<_>, Error>>()?;
|
||||
|
||||
match refs.iter_mut().find(|git_ref| git_ref.name == name) {
|
||||
Some(git_ref) => git_ref.commit = commit,
|
||||
None => refs.push(GitRef {
|
||||
name: name.clone(),
|
||||
commit,
|
||||
}),
|
||||
}
|
||||
|
||||
SyncState {
|
||||
refs,
|
||||
head: Some(branch),
|
||||
path: Some(path.clone()),
|
||||
mode: GitPushMode::Ref(name),
|
||||
policy: GitPushPolicy::Reconcile,
|
||||
}
|
||||
}
|
||||
None => {
|
||||
let mut state = {
|
||||
let work = cx.background_spawn({
|
||||
let path = path.clone();
|
||||
async move { SyncState::from_repo(&path) }
|
||||
});
|
||||
work.await?
|
||||
};
|
||||
state.policy = policy;
|
||||
state.reconcile(&announcement, cx).await?;
|
||||
state
|
||||
}
|
||||
};
|
||||
|
||||
// Keep the announced default branch in `HEAD` when it is among the pushed refs.
|
||||
if let Some(head) = announced_head
|
||||
|
||||
@@ -154,7 +154,8 @@ impl Inbox {
|
||||
me: PublicKey,
|
||||
state: &InboxReadState,
|
||||
) -> Result<(), Error> {
|
||||
let event = EventBuilder::new(Kind::ApplicationSpecificData, serde_json::to_string(state)?)
|
||||
let event = EventBuilder::new(Kind::ApplicationSpecificData)
|
||||
.content(serde_json::to_string(state)?)
|
||||
.tags([Tag::identifier(Self::inbox_state_d_tag(me))])
|
||||
.finalize(&Keys::generate())?;
|
||||
|
||||
|
||||
@@ -177,7 +177,7 @@ mod tests {
|
||||
fn announcement(secret: &str, id: &str) -> Announcement {
|
||||
let keys = Keys::new(SecretKey::from_hex(secret).expect("secret"));
|
||||
let tag = Tag::parse(vec!["d", id]).expect("tag");
|
||||
let event = EventBuilder::new(Kind::GitRepoAnnouncement, "")
|
||||
let event = EventBuilder::new(Kind::GitRepoAnnouncement)
|
||||
.tags(vec![tag])
|
||||
.finalize(&keys)
|
||||
.expect("signed");
|
||||
|
||||
@@ -1134,7 +1134,7 @@ impl RepoStore {
|
||||
return;
|
||||
};
|
||||
|
||||
let builder = EventBuilder::new(status.kind(), "").tags([
|
||||
let builder = EventBuilder::new(status.kind()).tags([
|
||||
root_ref,
|
||||
Tag::public_key(addr.public_key()),
|
||||
Tag::public_key(root.pubkey),
|
||||
@@ -1190,9 +1190,10 @@ impl RepoStore {
|
||||
self.run_push(push, None, cx)
|
||||
}
|
||||
|
||||
pub fn push_checkout(
|
||||
pub fn push_checkout_commit(
|
||||
&mut self,
|
||||
path: PathBuf,
|
||||
commit: String,
|
||||
cx: &mut Context<Self>,
|
||||
) -> Task<Result<(), Error>> {
|
||||
if self.pushing {
|
||||
@@ -1210,12 +1211,11 @@ impl RepoStore {
|
||||
};
|
||||
|
||||
// The state event's `HEAD` stays the announced default branch.
|
||||
// The checkout may be on a side branch.
|
||||
let head = self.head.clone();
|
||||
|
||||
let backend = Backend::global(cx);
|
||||
|
||||
let push = backend.update(cx, |backend, cx| {
|
||||
backend.push_checkout(announcement, path.clone(), head, cx)
|
||||
backend.push_checkout_commit(announcement, path.clone(), head, commit, cx)
|
||||
});
|
||||
|
||||
self.run_push(push, Some((addr, path)), cx)
|
||||
@@ -1508,7 +1508,10 @@ where
|
||||
tags.push(tag);
|
||||
}
|
||||
|
||||
let builder = EventBuilder::new(Kind::GitPatch, part.clone()).tags(tags);
|
||||
let builder = EventBuilder::new(Kind::GitPatch)
|
||||
.content(part.clone())
|
||||
.tags(tags);
|
||||
|
||||
let event = builder.finalize_async(signer).await?;
|
||||
let output = client.send_event(&event).broadcast().await?;
|
||||
|
||||
|
||||
@@ -31,20 +31,21 @@ impl GitRef {
|
||||
}
|
||||
|
||||
/// How much to push to the grasp servers.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Default)]
|
||||
pub enum GitPushMode {
|
||||
/// Mirror all branches and tags.
|
||||
#[default]
|
||||
All,
|
||||
/// Push `refs/heads/main` only (repo creation).
|
||||
Main,
|
||||
/// Push only the named ref from the declared state.
|
||||
Ref(String),
|
||||
}
|
||||
|
||||
/// How to handle server-only refs when pushing.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
pub enum GitPushPolicy {
|
||||
/// Reconcile the local refs with the grasp servers,
|
||||
/// fetching missing refs before pushing.
|
||||
/// Reconcile the local refs with the grasp servers, fetching missing refs before pushing.
|
||||
#[default]
|
||||
Reconcile,
|
||||
/// Declare the local refs only and delete server-only refs while pushing.
|
||||
@@ -116,7 +117,7 @@ impl SyncState {
|
||||
);
|
||||
}
|
||||
|
||||
EventBuilder::new(Kind::RepoState, "").tags(tags)
|
||||
EventBuilder::new(Kind::RepoState).tags(tags)
|
||||
}
|
||||
|
||||
pub fn parse(event: &Event) -> Self {
|
||||
@@ -280,7 +281,7 @@ impl SyncState {
|
||||
.await?;
|
||||
|
||||
if !missing.is_empty() {
|
||||
let (mode, policy) = (self.mode, self.policy);
|
||||
let (mode, policy) = (self.mode.clone(), self.policy);
|
||||
*self = Self::from_repo(&snapshot_path)?;
|
||||
self.mode = mode;
|
||||
self.policy = policy;
|
||||
@@ -319,7 +320,11 @@ impl SyncState {
|
||||
.map_err(|error| anyhow!("could not encode the repository owner: {error}"))?;
|
||||
|
||||
let mut outcome = PushOutcome::default();
|
||||
|
||||
let policy = self.policy;
|
||||
let mode = self.mode.clone();
|
||||
let refs = self.refs.clone();
|
||||
|
||||
let declared: Vec<String> = self
|
||||
.refs
|
||||
.iter()
|
||||
@@ -374,7 +379,8 @@ impl SyncState {
|
||||
let base = base.clone();
|
||||
let owner = owner.clone();
|
||||
let repo_id = announcement.id.clone();
|
||||
let mode = self.mode;
|
||||
let mode = mode.clone();
|
||||
let refs = refs.clone();
|
||||
let declared = declared.clone();
|
||||
|
||||
async move {
|
||||
@@ -382,6 +388,14 @@ impl SyncState {
|
||||
|
||||
match mode {
|
||||
GitPushMode::Main => repo.push_main(&base, &owner, &repo_id),
|
||||
GitPushMode::Ref(name) => {
|
||||
let Some(git_ref) = refs.iter().find(|git_ref| git_ref.name == name)
|
||||
else {
|
||||
return Err(anyhow!("ref {name} is not declared for this push"));
|
||||
};
|
||||
let refspec = format!("+{}:{}", git_ref.commit, name);
|
||||
repo.push_refspecs(&base, &owner, &repo_id, &[refspec.as_str()])
|
||||
}
|
||||
GitPushMode::All => {
|
||||
let mut refspecs = force_refspecs();
|
||||
if policy == GitPushPolicy::ResetRemote {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::cell::RefCell;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::path::PathBuf;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::Error;
|
||||
@@ -319,6 +319,44 @@ impl SyncStatusDirectory {
|
||||
.unwrap_or(0)
|
||||
}
|
||||
|
||||
/// Commit ids missing on Nostr, mapped to the checkout that holds them.
|
||||
///
|
||||
/// When `checkout` matches a tracked checkout, only that checkout contributes.
|
||||
pub fn ahead_commits_of(
|
||||
&self,
|
||||
addr: &RepoAddr,
|
||||
checkout: Option<&Path>,
|
||||
cx: &App,
|
||||
) -> HashMap<String, PathBuf> {
|
||||
self.queue(addr, cx);
|
||||
|
||||
let Some(statuses) = self.statuses.get(addr) else {
|
||||
return HashMap::new();
|
||||
};
|
||||
|
||||
let selected: Vec<&CheckoutSyncStatus> = match checkout
|
||||
.and_then(|path| statuses.iter().find(|status| status.path.as_path() == path))
|
||||
{
|
||||
Some(status) => vec![status],
|
||||
None => statuses.iter().collect(),
|
||||
};
|
||||
|
||||
let mut commits = HashMap::new();
|
||||
|
||||
for status in selected {
|
||||
if status.status.head.is_none() {
|
||||
continue;
|
||||
}
|
||||
for id in &status.status.ahead_commits {
|
||||
commits
|
||||
.entry(id.clone())
|
||||
.or_insert_with(|| status.path.clone());
|
||||
}
|
||||
}
|
||||
|
||||
commits
|
||||
}
|
||||
|
||||
fn queue(&self, addr: &RepoAddr, cx: &App) {
|
||||
if self.with_pending(|pending| pending.insert(addr.clone())) {
|
||||
let store = SyncStatusStore::global(cx);
|
||||
|
||||
Reference in New Issue
Block a user