navid/veins / commit

0c07122b0d0f

Navid <unknown> · 2026-11-30

parents: c24e60840b5e

Browse files at this commit · Download source

all: fix rs impl for cloning
--- a/veins/src/cli/mod.rs+++ b/veins/src/cli/mod.rs@@ -14,6 +14,7 @@ use crate::Result;  pub fn run(arguments: &[String]) -> u8 {+    crate::protocol::promisor::install();     match dispatch(arguments) {         Ok(code) => code,         Err(error) => {--- a/veins/src/cli/net_cmd.rs+++ b/veins/src/cli/net_cmd.rs@@ -221,6 +221,16 @@     refs.iter()         .find(|reference| reference.kind == 0)         .map(|reference| reference.name.clone())+}++fn selected_commits(refs: &[RefInfo], policy: &str) -> Vec<Id> {+    if policy == "none" {+        return Vec::new();+    }+    if policy == "head" {+        return default_target(refs).into_iter().collect();+    }+    refs.iter().map(|reference| reference.target).collect() }  fn set_bookmark(view: &mut crate::object::view::View, name: &str, target: Id) {@@ -367,6 +377,13 @@     }     client.negotiate(&wants, &[])?;     client.fetch_skeleton(&root, &wants)?;+    {+        let mut store = Store::open(&root)?;+        let commits = selected_commits(&refs, &policy);+        let chunks = chunks_for_commits(&mut store, &commits)?;+        send_chunks(&mut client, &root, &chunks)?;+    }+    drop(client);     apply_refs(&root, &refs, "o", true)?;     core::repo_config_set(&root, "promisor", "o");     let mut store = Store::open(&root)?;@@ -433,6 +450,12 @@     }     client.negotiate(&wants, &haves)?;     client.fetch_skeleton(&root, &wants)?;+    {+        let mut store = Store::open(&root)?;+        let chunks = chunks_for_commits(&mut store, &wants)?;+        send_missing_chunks(&mut client, &root, &chunks)?;+    }+    drop(client);     apply_refs(&root, &refs, &remote_name, false)?;     output::info_prefix();     println!("fetched from {remote_name}");@@ -577,6 +600,8 @@     trees: HashSet<Id>,     objects: HashSet<Id>,     output: Vec<Id>,+    chunks: HashSet<Id>,+    chunk_output: Vec<Id>, }  impl ObjectWalk {@@ -600,6 +625,9 @@         match blob.form {             BlobForm::Leaf => {                 for entry in blob.entries {+                    if self.chunks.insert(entry.id) {+                        self.chunk_output.push(entry.id);+                    }                     self.add_object(entry.id);                 }             }@@ -722,7 +750,17 @@             }         }         Ok(())+    }+}++fn chunks_for_commits(store: &mut Store, commits: &[Id]) -> Result<Vec<Id>> {+    let mut walk = ObjectWalk::default();+    for id in commits {+        if let Some(commit) = graph::load_commit(store, *id)? {+            walk.walk_tree(store, commit.root_tree)?;+        }     }+    Ok(walk.chunk_output) }  fn objects_for_commit_deltas(@@ -773,6 +811,27 @@         }     }     missing+}++fn send_chunks(client: &mut Client, root: &Path, chunks: &[Id]) -> Result<()> {+    const BATCH: usize = 4096;+    for batch in chunks.chunks(BATCH) {+        client.get_chunks(root, batch)?;+    }+    Ok(())+}++fn send_missing_chunks(client: &mut Client, root: &Path, chunks: &[Id]) -> Result<()> {+    if chunks.is_empty() {+        return Ok(());+    }+    let mut store = Store::open(root)?;+    let missing: Vec<Id> = chunks+        .iter()+        .copied()+        .filter(|id| store.find(*id).is_none())+        .collect();+    send_chunks(client, root, &missing) }  fn push(arguments: &[String]) -> Result<u8> {--- a/veins/src/protocol/mod.rs+++ b/veins/src/protocol/mod.rs@@ -1,4 +1,5 @@ pub mod client; pub mod messages;+pub mod promisor; pub mod tls; pub mod wire;--- /dev/null+++ b/veins/src/protocol/promisor.rs@@ -0,0 +1,73 @@+use std::cell::Cell;+use std::path::Path;++use crate::error::{Error, Result};+use crate::object::id::Id;+use crate::store;++use super::client::Client;+use super::messages;++thread_local! {+    static ACTIVE: Cell<bool> = const { Cell::new(false) };+}++struct Guard<'a>(&'a Cell<bool>);++impl Drop for Guard<'_> {+    fn drop(&mut self) {+        self.0.set(false);+    }+}++fn remote_field(remote: &str, field: &str) -> String {+    format!("remote.{remote}.{field}")+}++fn remote_config(root: &Path) -> Option<(String, String, String)> {+    let promisor = crate::core::repo_config_value(root, "promisor")?;+    if promisor.is_empty() {+        return None;+    }+    let url = crate::core::repo_config_value(root, &remote_field(&promisor, "url"))?;+    if url.is_empty() {+        return None;+    }+    let key =+        crate::core::repo_config_value(root, &remote_field(&promisor, "key")).unwrap_or_default();+    let pin = crate::core::repo_config_value(root, &remote_field(&promisor, "tls-pin"))+        .unwrap_or_default();+    Some((url, key, pin))+}++pub fn fetch_chunks(root: &Path, ids: &[Id]) -> Result<()> {+    if ids.is_empty() {+        return Ok(());+    }+    let (url, key, pin) =+        remote_config(root).ok_or_else(|| Error::new("no promisor remote configured"))?;+    let repo_name = messages::url_repo_name(&url);+    let mut client = Client::connect(&url, &key, &repo_name, &pin)?;+    const BATCH: usize = 4096;+    for batch in ids.chunks(BATCH) {+        client.get_chunks(root, batch)?;+    }+    Ok(())+}++fn fetch_missing_chunk(root: &Path, id: Id) -> bool {+    ACTIVE.with(|active| {+        if active.get() {+            return false;+        }+        active.set(true);+        let _guard = Guard(active);+        fetch_chunks(root, &[id]).is_ok()+    })+}++/// Register the lazy-chunk fetch hook used by the store. Safe to call more than+/// once; the last registration wins.+pub fn install() {+    store::set_promisor_fetcher(Some(Box::new(fetch_missing_chunk)));+}--- a/veins/src/worktree/checkout.rs+++ b/veins/src/worktree/checkout.rs@@ -5,11 +5,13 @@  use crate::core; use crate::error::{Error, Result};+use crate::object::blob::{self, BlobForm}; use crate::object::conflict::{self, Conflict, ConflictValue}; use crate::object::id::Id; use crate::object::path::valid_checkout_path; use crate::object::tree::{self, FlatEntry}; use crate::platform;+use crate::protocol::promisor; use crate::store::Txn; use crate::store::graph; use crate::store::layout;@@ -201,7 +203,19 @@                 }             }             Err(error) if error.kind() == std::io::ErrorKind::NotFound => {-                fs::create_dir(&current)?;+                match fs::create_dir(&current) {+                    Ok(()) => {}+                    Err(create_error)+                        if create_error.kind() == std::io::ErrorKind::AlreadyExists =>+                    {+                        if !fs::symlink_metadata(&current).is_ok_and(|metadata| {+                            metadata.is_dir() && !metadata.file_type().is_symlink()+                        }) {+                            return Err(Error::new("unsafe path component"));+                        }+                    }+                    Err(create_error) => return Err(create_error.into()),+                }             }             Err(error) => return Err(error.into()),         }@@ -278,7 +292,95 @@             &data,             entry.kind == tree::KIND_EXECUTABLE,         )+    }+}++fn collect_blob_chunks(+    store: &Store,+    id: Id,+    seen: &mut std::collections::HashSet<Id>,+    output: &mut Vec<Id>,+) -> Result<()> {+    if !seen.insert(id) {+        return Ok(());+    }+    let Some(body) = store.body_loaded(id, crate::object::TYPE_BLOB)? else {+        return Ok(());+    };+    let Ok(blob) = blob::decode(&body) else {+        return Ok(());+    };+    match blob.form {+        BlobForm::Leaf => {+            for entry in blob.entries {+                output.push(entry.id);+            }+        }+        BlobForm::Interior => {+            for entry in blob.entries {+                collect_blob_chunks(store, entry.id, seen, output)?;+            }+        }+        BlobForm::Inline => {}+    }+    Ok(())+}++fn collect_conflict_chunks(+    store: &mut Store,+    id: Id,+    seen_conflicts: &mut std::collections::HashSet<Id>,+    seen_blobs: &mut std::collections::HashSet<Id>,+    output: &mut Vec<Id>,+) -> Result<()> {+    if !seen_conflicts.insert(id) {+        return Ok(());+    }+    let Some(body) = store.body_loaded(id, crate::object::TYPE_CONFLICT)? else {+        return Ok(());+    };+    let Ok(conflict) = conflict::decode(&body) else {+        return Ok(());+    };+    for value in conflict.adds.iter().chain(conflict.removes.iter()) {+        let Some(target) = value.target else {+            continue;+        };+        match value.kind {+            tree::KIND_SUBTREE => {+                for entry in tree::flatten(store, target)? {+                    collect_entry_chunks(store, &entry, seen_conflicts, seen_blobs, output)?;+                }+            }+            tree::KIND_CONFLICT => {+                collect_conflict_chunks(store, target, seen_conflicts, seen_blobs, output)?;+            }+            tree::KIND_FILE | tree::KIND_EXECUTABLE | tree::KIND_SYMLINK => {+                collect_blob_chunks(store, target, seen_blobs, output)?;+            }+            _ => {}+        }+    }+    Ok(())+}++fn collect_entry_chunks(+    store: &mut Store,+    entry: &FlatEntry,+    seen_conflicts: &mut std::collections::HashSet<Id>,+    seen_blobs: &mut std::collections::HashSet<Id>,+    output: &mut Vec<Id>,+) -> Result<()> {+    match entry.kind {+        tree::KIND_CONFLICT => {+            collect_conflict_chunks(store, entry.target, seen_conflicts, seen_blobs, output)?;+        }+        tree::KIND_FILE | tree::KIND_EXECUTABLE | tree::KIND_SYMLINK => {+            collect_blob_chunks(store, entry.target, seen_blobs, output)?;+        }+        _ => {}     }+    Ok(()) }  fn delete_stale(root: &Path, old_entries: &[FlatEntry], new_entries: &[FlatEntry]) -> Result<()> {@@ -451,11 +553,9 @@                 String::from_utf8_lossy(&entry.path)             )));         }-        let mut start = 0;         for index in 0..entry.path.len() {             if entry.path[index] == b'/' {-                directories.push(entry.path[start..index].to_vec());-                start = index + 1;+                directories.push(entry.path[..index].to_vec());             }         }         match entry.kind {@@ -488,6 +588,8 @@     }      delete_stale(root, &old_entries, &new_entries)?;+    directories.sort();+    directories.dedup();     for directory in &directories {         ensure_safe_dirs(root, directory)?;     }@@ -494,6 +596,31 @@     ensure_safe_dirs(root, b"")?;      store.preload()?;+    if store.has_promisor() {+        let mut seen_conflicts = std::collections::HashSet::new();+        let mut seen_blobs = std::collections::HashSet::new();+        let mut chunks = Vec::new();+        for entry in &new_entries {+            collect_entry_chunks(+                store,+                entry,+                &mut seen_conflicts,+                &mut seen_blobs,+                &mut chunks,+            )?;+        }+        let mut missing = Vec::new();+        for id in chunks {+            if !store.contains(id) {+                missing.push(id);+            }+        }+        if !missing.is_empty() {+            promisor::fetch_chunks(root, &missing)?;+            store.refresh()?;+            store.preload()?;+        }+    }     if let Some(progress) = progress         && (!files.is_empty() || !conflicts.is_empty())     {