0.1.4: settings decide where a vault shares; leaving always finishes
- A folder found sharing somewhere other than its settings say is set back (every 30 s, under the vault's op lock). Two programs racing — the 0.1.1 CLI upload against the 0.1.2 app's check-in — had left Brain's settings on the hub and its folder on Gitea, so it kept "syncing" to the wrong place. - Leaving hands over to the vault's own hub, never to wherever the folder points. If the hub refuses the history (an older entry has a possible secret), the device hands over its current notes as one entry with those lines replaced, instead of retrying forever with the folder read-only. - The app says "leaving" while that's in progress instead of "Synced". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -290,6 +290,22 @@ impl Vault {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Where this vault shares its history now, if anywhere.
|
||||
pub fn remote_url(&self) -> Option<String> {
|
||||
self.repo.find_remote("origin").ok().and_then(|r| r.url().ok().map(String::from))
|
||||
}
|
||||
|
||||
/// Makes the folder share where its settings say: their remote, or nowhere for a vault on this device only.
|
||||
/// The settings are what the app shows, so they win. Returns whether anything changed.
|
||||
pub fn follow(&self, remote: &str) -> Result<bool> {
|
||||
match (self.remote_url(), remote.is_empty()) {
|
||||
(None, true) => Ok(false),
|
||||
(Some(_), true) => self.unlink().map(|_| true),
|
||||
(Some(url), false) if url == remote => Ok(false),
|
||||
_ => self.relink(remote).map(|_| true),
|
||||
}
|
||||
}
|
||||
|
||||
/// A vault with no remote lives on this device only: it keeps saving history, and shares nowhere.
|
||||
pub fn is_local(&self) -> bool {
|
||||
self.repo.find_remote("origin").is_err()
|
||||
@@ -400,6 +416,45 @@ impl Vault {
|
||||
|
||||
/// Sends this device's whole history to `dst` on the remote (the hub's handover area for this device).
|
||||
pub fn hand_over(&self, dst: &str) -> Result<()> {
|
||||
self.push_handover(&self.branch_ref(), dst)
|
||||
}
|
||||
|
||||
/// The current notes as one entry, for a handover the hub refused because older history has something that
|
||||
/// looks like a secret (P3): every such line is replaced with `pointer`. It sits on top of the last shared
|
||||
/// state, so reviewing it shows just what this device changed. Returns what was handed over.
|
||||
pub fn hand_over_current(&self, dst: &str, pointer: &str) -> Result<Oid> {
|
||||
let head = self.head_commit()?.context("nothing to hand over")?;
|
||||
let tree = head.tree()?;
|
||||
let mut cleaned = vec![];
|
||||
tree.walk(TreeWalkMode::PreOrder, |dir, e| {
|
||||
if let Ok(blob) = e.to_object(&self.repo).and_then(|o| o.peel_to_blob()) {
|
||||
let hits = secrets::scan_bytes(blob.content(), None, &HashSet::new());
|
||||
if !hits.is_empty() {
|
||||
let text = String::from_utf8_lossy(blob.content()).into_owned();
|
||||
cleaned.push((format!("{dir}{}", e.name().unwrap_or_default()), secrets::replace_with_pointer(&text, &hits, None, pointer)));
|
||||
}
|
||||
}
|
||||
TreeWalkResult::Ok
|
||||
})?;
|
||||
let mut update = TreeUpdateBuilder::new();
|
||||
for (path, text) in &cleaned {
|
||||
update.upsert(path, self.repo.blob(text.as_bytes())?, FileMode::Blob);
|
||||
}
|
||||
let tree = self.repo.find_tree(update.create_updated(&self.repo, &tree)?)?;
|
||||
let parent = self.remote_commit()?;
|
||||
let sig = self.signature()?;
|
||||
let message = format!("Handed over by {} ({} with possible secrets removed)", self.label, cleaned.len());
|
||||
let id = self.repo.commit(None, &sig, &sig, &message, &tree, &parent.iter().collect::<Vec<_>>())?;
|
||||
const TEMP: &str = "refs/lune/handover";
|
||||
self.repo.reference(TEMP, id, true, "lune: handover")?;
|
||||
let sent = self.push_handover(TEMP, dst);
|
||||
if let Ok(mut r) = self.repo.find_reference(TEMP) {
|
||||
let _ = r.delete();
|
||||
}
|
||||
sent.map(|_| id)
|
||||
}
|
||||
|
||||
fn push_handover(&self, src: &str, dst: &str) -> Result<()> {
|
||||
let mut remote = self.repo.find_remote("origin")?;
|
||||
let rejected = RefCell::new(None::<String>);
|
||||
let mut cb = remote::callbacks();
|
||||
@@ -411,7 +466,7 @@ impl Vault {
|
||||
});
|
||||
let mut opts = PushOptions::new();
|
||||
opts.remote_callbacks(cb);
|
||||
remote.push(&[format!("+{}:{dst}", self.branch_ref())], Some(&mut opts))?;
|
||||
remote.push(&[format!("+{src}:{dst}")], Some(&mut opts))?;
|
||||
drop(opts);
|
||||
if let Some(why) = rejected.into_inner() {
|
||||
bail!("the hub didn't take the handover: {why}");
|
||||
|
||||
Reference in New Issue
Block a user