navid/veins / commit
6583c2b8315a
Navid M <navidmomtahen@gmail.com> · 2026-11-29
parents: b08e18b14120
Browse files at this commit · Download source
--- a/veinsrs/src/cli/net_cmd.rs+++ b/veinsrs/src/cli/net_cmd.rs@@ -547,23 +547,118 @@ Ok(graph::collect_commits(store, &[descendant])?.contains(&ancestor)) } -fn collect_objects(- store: &mut Store,- commits: &[Id],- output: &mut Vec<Id>,- seen: &mut HashSet<Id>,- progress: Option<&crate::core::Progress>,-) -> Result<()> {- fn walk_tree(- store: &mut Store,- id: Id,- output: &mut Vec<Id>,- seen: &mut HashSet<Id>,- ) -> Result<()> {- if !seen.insert(id) {- return Ok(());+fn collect_commits_until(store: &mut Store, start: &[Id], stop: &HashSet<Id>) -> Result<Vec<Id>> {+ let mut output = Vec::new();+ let mut stack = Vec::new();+ let mut seen = HashSet::new();+ for id in start {+ if !stop.contains(id) && seen.insert(*id) {+ stack.push(*id); }+ }+ while let Some(id) = stack.pop() { output.push(id);+ let Some(commit) = graph::load_commit(store, id)? else {+ continue;+ };+ for parent in commit.parents {+ if !stop.contains(&parent) && seen.insert(parent) {+ stack.push(parent);+ }+ }+ }+ Ok(output)+}++#[derive(Default)]+struct ObjectWalk {+ blobs: HashSet<Id>,+ conflicts: HashSet<Id>,+ trees: HashSet<Id>,+ objects: HashSet<Id>,+ output: Vec<Id>,+}++impl ObjectWalk {+ fn add_object(&mut self, id: Id) {+ if self.objects.insert(id) {+ self.output.push(id);+ }+ }++ fn walk_blob(&mut self, store: &mut Store, id: Id) -> Result<()> {+ if !self.blobs.insert(id) {+ return Ok(());+ }+ self.add_object(id);+ let Some(body) = store.body(id, TYPE_BLOB)? else {+ return Ok(());+ };+ let Ok(blob) = blob::decode(&body) else {+ return Ok(());+ };+ match blob.form {+ BlobForm::Leaf => {+ for entry in blob.entries {+ self.add_object(entry.id);+ }+ }+ BlobForm::Interior => {+ for entry in blob.entries {+ self.walk_blob(store, entry.id)?;+ }+ }+ BlobForm::Inline => {}+ }+ Ok(())+ }++ fn walk_conflict(&mut self, store: &mut Store, id: Id) -> Result<()> {+ if !self.conflicts.insert(id) {+ return Ok(());+ }+ self.add_object(id);+ let Some(body) = store.body(id, crate::object::TYPE_CONFLICT)? else {+ return Ok(());+ };+ let Ok(conflict) = crate::object::conflict::decode(&body) else {+ return Ok(());+ };+ for value in conflict.adds.iter().chain(conflict.removes.iter()) {+ if let Some(target) = value.target {+ self.walk_value(store, value.kind, target)?;+ }+ }+ Ok(())+ }++ fn walk_value(&mut self, store: &mut Store, kind: u8, id: Id) -> Result<()> {+ match kind {+ KIND_SUBTREE => self.walk_tree(store, id)?,+ tree::KIND_CONFLICT => self.walk_conflict(store, id)?,+ tree::KIND_FILE | tree::KIND_EXECUTABLE | tree::KIND_SYMLINK => {+ self.walk_blob(store, id)?;+ }+ _ => {}+ }+ Ok(())+ }++ fn walk_entry(&mut self, store: &mut Store, entry: &tree::TreeEntry) -> Result<()> {+ match entry.kind {+ KIND_SUBTREE => self.walk_tree(store, entry.target)?,+ tree::KIND_CONFLICT => self.walk_conflict(store, entry.target)?,+ tree::KIND_SUBMODULE => {}+ _ => self.walk_blob(store, entry.target)?,+ }+ Ok(())+ }++ fn walk_tree(&mut self, store: &mut Store, id: Id) -> Result<()> {+ if !self.trees.insert(id) {+ return Ok(());+ }+ self.add_object(id); let Some(body) = store.body(id, TYPE_TREE)? else { return Ok(()); };@@ -570,61 +665,114 @@ let Ok(tree) = tree::decode(&body) else { return Ok(()); };- for entry in tree.entries {- if entry.kind == KIND_SUBTREE {- walk_tree(store, entry.target, output, seen)?;- } else if entry.kind != tree::KIND_SUBMODULE {- walk_object(store, entry.target, output, seen)?;- }+ for entry in &tree.entries {+ self.walk_entry(store, entry)?; } Ok(()) }- fn walk_object(- store: &mut Store,- id: Id,- output: &mut Vec<Id>,- seen: &mut HashSet<Id>,- ) -> Result<()> {- if !seen.insert(id) {++ fn walk_tree_delta(&mut self, store: &mut Store, id: Id, old: Option<Id>) -> Result<()> {+ if old == Some(id) { return Ok(()); }- output.push(id);- let Some(body) = store.body(id, TYPE_BLOB)? else {+ if !self.trees.insert(id) {+ return Ok(());+ }+ self.add_object(id);+ let Some(body) = store.body(id, TYPE_TREE)? else { return Ok(()); };- if let Ok(blob) = blob::decode(&body) {- match blob.form {- BlobForm::Leaf => {- for entry in blob.entries {- if seen.insert(entry.id) {- output.push(entry.id);- }- }- }- BlobForm::Interior => {- for entry in blob.entries {- walk_object(store, entry.id, output, seen)?;- }- }- BlobForm::Inline => {}+ let Ok(tree) = tree::decode(&body) else {+ return Ok(());+ };+ let old_tree = match old {+ Some(old) => match store.body(old, TYPE_TREE)? {+ Some(body) => tree::decode(&body).ok(),+ None => None,+ },+ None => None,+ };+ let Some(old_tree) = old_tree else {+ for entry in &tree.entries {+ self.walk_entry(store, entry)?;+ }+ return Ok(());+ };+ let mut old_index = 0;+ for entry in &tree.entries {+ while old_index < old_tree.entries.len()+ && old_tree.entries[old_index].name < entry.name+ {+ old_index += 1;+ }+ if old_index >= old_tree.entries.len() || old_tree.entries[old_index].name != entry.name+ {+ self.walk_entry(store, entry)?;+ continue;+ }+ let old_entry = &old_tree.entries[old_index];+ old_index += 1;+ if old_entry.kind == entry.kind && old_entry.target == entry.target {+ continue;+ }+ if entry.kind == KIND_SUBTREE && old_entry.kind == KIND_SUBTREE {+ self.walk_tree_delta(store, entry.target, Some(old_entry.target))?;+ } else if entry.kind != tree::KIND_SUBMODULE {+ self.walk_entry(store, entry)?; } } Ok(()) }+}++fn objects_for_commit_deltas(+ store: &mut Store,+ commits: &[Id],+ progress: Option<&crate::core::Progress>,+) -> Result<Vec<Id>> {+ let mut walk = ObjectWalk::default(); for id in commits {- if !seen.insert(*id) {- continue;+ walk.add_object(*id);+ if let Some(commit) = graph::load_commit(store, *id)? {+ let mut old_tree = None;+ let mut tree_covered = false;+ for parent_id in &commit.parents {+ let Some(parent) = graph::load_commit(store, *parent_id)? else {+ continue;+ };+ if parent.root_tree == commit.root_tree {+ tree_covered = true;+ break;+ }+ if old_tree.is_none() {+ old_tree = Some(parent.root_tree);+ }+ }+ if !tree_covered {+ walk.walk_tree_delta(store, commit.root_tree, old_tree)?;+ } }- output.push(*id);- let Some(commit) = graph::load_commit(store, *id)? else {- continue;- };- walk_tree(store, commit.root_tree, output, seen)?; if let Some(progress) = progress { progress.advance(1); } }- Ok(())+ Ok(walk.output)+}++fn missing_objects(client: &mut Client, objects: &[Id]) -> Vec<Id> {+ const BATCH: usize = 100_000;+ let mut missing = Vec::new();+ for batch in objects.chunks(BATCH) {+ let Ok(bits) = client.have_query(batch) else {+ return objects.to_vec();+ };+ for (index, id) in batch.iter().enumerate() {+ if bits.get(index).copied().unwrap_or(0) == 0 {+ missing.push(*id);+ }+ }+ }+ missing } fn push(arguments: &[String]) -> Result<u8> {@@ -686,51 +834,70 @@ }; let mut client = Client::connect(&url, &key, &repo_name, &pin)?; let remote_refs = client.list_refs()?;- let mut commits = vec![bookmark_target]; let mut updates = Vec::new(); let remote_bookmark = remote_refs .iter() .find(|reference| reference.kind == 0 && reference.name == bookmark) .map(|reference| reference.target);- updates.push(UpdateRef {- name: bookmark.clone(),- expected: remote_bookmark,- target: Some(bookmark_target),- });+ if remote_bookmark != Some(bookmark_target) {+ updates.push(UpdateRef {+ name: bookmark.clone(),+ expected: remote_bookmark,+ target: Some(bookmark_target),+ });+ }+ let mut tag_updates = 0usize; for tag in &view.tags { let name = String::from_utf8_lossy(&tag.name).into_owned();- commits.push(tag.target); let remote = remote_refs .iter() .find(|reference| reference.kind == 1 && reference.name == name) .map(|reference| reference.target);+ if remote == Some(tag.target) {+ continue;+ } updates.push(UpdateRef { name: format!("refs/tags/{name}"), expected: remote, target: Some(tag.target), });+ tag_updates += 1;+ }+ if updates.is_empty() {+ output::info_prefix();+ println!("nothing to push, everything is up to date");+ return Ok(0);+ }+ let remote_commits: HashSet<Id> = remote_refs+ .iter()+ .map(|reference| reference.target)+ .collect();+ let mut roots = Vec::with_capacity(1 + view.tags.len());+ roots.push(bookmark_target);+ for tag in &view.tags {+ roots.push(tag.target); }+ let commits = collect_commits_until(&mut store, &roots, &remote_commits)?; let mut objects = Vec::new();- let mut seen = HashSet::new();- let collecting = crate::core::Progress::new("collecting objects", commits.len());- collect_objects(- &mut store,- &commits,- &mut objects,- &mut seen,- Some(&collecting),- )?;- collecting.finish();- let pushing = crate::core::Progress::new("pushing objects", objects.len());- client.push_objects(&root, &objects, Some(&pushing))?;- pushing.finish();+ if !commits.is_empty() {+ let collecting = crate::core::Progress::new("collecting objects", commits.len());+ objects = objects_for_commit_deltas(&mut store, &commits, Some(&collecting))?;+ collecting.finish();+ }+ if !objects.is_empty() {+ let missing = missing_objects(&mut client, &objects);+ if !missing.is_empty() {+ let pushing = crate::core::Progress::new("pushing objects", missing.len());+ client.push_objects(&root, &missing, Some(&pushing))?;+ pushing.finish();+ }+ } client.update_refs(&updates)?; output::info_prefix(); println!("pushed {bookmark}");- let tag_count = updates.len() - 1;- if tag_count > 0 {+ if tag_updates > 0 { output::info_prefix();- println!("pushed {tag_count} tag(s)");+ println!("pushed {tag_updates} tag(s)"); } Ok(0) }