gix-revwalk-0.13.1/.cargo_vcs_info.json0000644000000001510000000000100133530ustar { "git": { "sha1": "4f98e94e0e8b79ed2899b35bef40f3c30b3025b0" }, "path_in_vcs": "gix-revwalk" }gix-revwalk-0.13.1/Cargo.toml0000644000000024370000000000100113620ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" rust-version = "1.65" name = "gix-revwalk" version = "0.13.1" authors = ["Sebastian Thiel "] build = false include = [ "src/**/*", "LICENSE-*", ] autobins = false autoexamples = false autotests = false autobenches = false description = "A crate providing utilities for walking the revision graph" readme = false license = "MIT OR Apache-2.0" repository = "https://github.com/Byron/gitoxide" [lib] name = "gix_revwalk" path = "src/lib.rs" doctest = false [dependencies.gix-commitgraph] version = "^0.24.2" [dependencies.gix-date] version = "^0.8.6" [dependencies.gix-hash] version = "^0.14.2" [dependencies.gix-hashtable] version = "^0.5.2" [dependencies.gix-object] version = "^0.42.2" [dependencies.smallvec] version = "1.10.0" [dependencies.thiserror] version = "1.0.26" gix-revwalk-0.13.1/Cargo.toml.orig000064400000000000000000000013321046102023000150340ustar 00000000000000[package] name = "gix-revwalk" version = "0.13.1" repository = "https://github.com/Byron/gitoxide" license = "MIT OR Apache-2.0" description = "A crate providing utilities for walking the revision graph" authors = ["Sebastian Thiel "] edition = "2021" include = ["src/**/*", "LICENSE-*"] rust-version = "1.65" [lib] doctest = false [dependencies] gix-hash = { version = "^0.14.2", path = "../gix-hash" } gix-object = { version = "^0.42.2", path = "../gix-object" } gix-date = { version = "^0.8.6", path = "../gix-date" } gix-hashtable = { version = "^0.5.2", path = "../gix-hashtable" } gix-commitgraph = { version = "^0.24.2", path = "../gix-commitgraph" } thiserror = "1.0.26" smallvec = "1.10.0" gix-revwalk-0.13.1/LICENSE-APACHE000064400000000000000000000247461046102023000141070ustar 00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. gix-revwalk-0.13.1/LICENSE-MIT000064400000000000000000000017771046102023000136160ustar 00000000000000Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. gix-revwalk-0.13.1/src/graph/commit.rs000064400000000000000000000140021046102023000156710ustar 00000000000000use gix_date::SecondsSinceUnixEpoch; use smallvec::SmallVec; use super::LazyCommit; use crate::graph::{Commit, Either, Generation}; impl<'graph> LazyCommit<'graph> { /// Return an iterator over the parents of this commit. pub fn iter_parents(&self) -> Parents<'graph> { let backing = match &self.backing { Either::Left(buf) => Either::Left(gix_object::CommitRefIter::from_bytes(buf)), Either::Right((cache, pos)) => Either::Right((*cache, cache.commit_at(*pos).iter_parents())), }; Parents { backing } } /// Returns the timestamp at which this commit was created. /// /// This is the single-most important date for determining recency of commits. /// Note that this can only fail if the commit is backed by the object database *and* parsing fails. pub fn committer_timestamp(&self) -> Result { Ok(match &self.backing { Either::Left(buf) => gix_object::CommitRefIter::from_bytes(buf).committer()?.time.seconds, Either::Right((cache, pos)) => cache.commit_at(*pos).committer_timestamp() as SecondsSinceUnixEpoch, // a cast as we cannot represent the error and trying seems overkill }) } /// Returns the generation of the commit if it is backed by a commit graph. pub fn generation(&self) -> Option { match &self.backing { Either::Left(_) => None, Either::Right((cache, pos)) => cache.commit_at(*pos).generation().into(), } } /// Convert ourselves into an owned version, which effectively detaches us from the underlying graph. /// Use `new_data()` to provide the `data` field for the owned `Commit`. pub fn to_owned(&self, new_data: impl FnOnce() -> T) -> Result, to_owned::Error> { let data = new_data(); Ok(match &self.backing { Either::Left(buf) => { use gix_object::commit::ref_iter::Token; let iter = gix_object::CommitRefIter::from_bytes(buf); let mut parents = SmallVec::default(); let mut timestamp = None; for token in iter { match token? { Token::Tree { .. } => {} Token::Parent { id } => parents.push(id), Token::Author { .. } => {} Token::Committer { signature } => { timestamp = Some(signature.time.seconds); break; } _ => { unreachable!( "we break naturally after seeing the committer which is always at the same spot" ) } } } Commit { parents, commit_time: timestamp.unwrap_or_default(), generation: None, data, } } Either::Right((cache, pos)) => { let mut parents = SmallVec::default(); let commit = cache.commit_at(*pos); for pos in commit.iter_parents() { let pos = pos?; parents.push(cache.commit_at(pos).id().to_owned()); } Commit { parents, commit_time: commit.committer_timestamp().try_into().map_err(|_| { to_owned::Error::CommitGraphTime { actual: commit.committer_timestamp(), } })?, generation: Some(commit.generation()), data, } } }) } } /// An iterator over the parents of a commit. pub struct Parents<'graph> { backing: Either< gix_object::CommitRefIter<'graph>, ( &'graph gix_commitgraph::Graph, gix_commitgraph::file::commit::Parents<'graph>, ), >, } impl<'graph> Iterator for Parents<'graph> { type Item = Result; fn next(&mut self) -> Option { match &mut self.backing { Either::Left(it) => { for token in it { match token { Ok(gix_object::commit::ref_iter::Token::Tree { .. }) => continue, Ok(gix_object::commit::ref_iter::Token::Parent { id }) => return Some(Ok(id)), Ok(_unused_token) => break, Err(err) => return Some(Err(err.into())), } } None } Either::Right((cache, it)) => it .next() .map(|r| r.map(|pos| cache.id_at(pos).to_owned()).map_err(Into::into)), } } } /// #[allow(clippy::empty_docs)] pub mod iter_parents { /// The error returned by the [`Parents`][super::Parents] iterator. #[derive(Debug, thiserror::Error)] #[allow(missing_docs)] pub enum Error { #[error("An error occurred when parsing commit parents")] DecodeCommit(#[from] gix_object::decode::Error), #[error("An error occurred when parsing parents from the commit graph")] DecodeCommitGraph(#[from] gix_commitgraph::file::commit::Error), } } /// #[allow(clippy::empty_docs)] pub mod to_owned { /// The error returned by [`to_owned()`][crate::graph::LazyCommit::to_owned()]. #[derive(Debug, thiserror::Error)] #[allow(missing_docs)] pub enum Error { #[error("A commit could not be decoded during traversal")] Decode(#[from] gix_object::decode::Error), #[error("Could not find commit position in graph when traversing parents")] CommitGraphParent(#[from] gix_commitgraph::file::commit::Error), #[error("Commit-graph time could not be presented as signed integer: {actual}")] CommitGraphTime { actual: u64 }, } } gix-revwalk-0.13.1/src/graph/mod.rs000064400000000000000000000322561046102023000151730ustar 00000000000000use std::{fmt::Formatter, ops::Index}; use gix_hash::oid; use smallvec::SmallVec; use crate::Graph; /// A mapping between an object id and arbitrary data, and produced when calling [`Graph::detach()`]. pub type IdMap = gix_hashtable::HashMap; /// #[allow(clippy::empty_docs)] pub mod commit; mod errors { /// #[allow(clippy::empty_docs)] pub mod insert_parents { use crate::graph::commit::iter_parents; /// The error returned by [`insert_parents()`](crate::Graph::insert_parents()). #[derive(Debug, thiserror::Error)] #[allow(missing_docs)] pub enum Error { #[error(transparent)] Lookup(#[from] gix_object::find::existing_iter::Error), #[error("A commit could not be decoded during traversal")] Decode(#[from] gix_object::decode::Error), #[error(transparent)] Parent(#[from] iter_parents::Error), } } /// #[allow(clippy::empty_docs)] pub mod try_lookup_or_insert_default { use crate::graph::commit::to_owned; /// The error returned by [`try_lookup_or_insert_default()`](crate::Graph::try_lookup_or_insert_default()). #[derive(Debug, thiserror::Error)] #[allow(missing_docs)] pub enum Error { #[error(transparent)] Lookup(#[from] gix_object::find::existing_iter::Error), #[error(transparent)] ToOwned(#[from] to_owned::Error), } } } pub use errors::{insert_parents, try_lookup_or_insert_default}; use gix_date::SecondsSinceUnixEpoch; /// The generation away from the HEAD of graph, useful to limit algorithms by topological depth as well. /// /// 0 would mean the starting point of the hierarchy, and 1 their parents. /// This number is only available natively if there is a commit-graph. pub type Generation = u32; impl<'find, T: std::fmt::Debug> std::fmt::Debug for Graph<'find, T> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { std::fmt::Debug::fmt(&self.map, f) } } impl<'find, T: Default> Graph<'find, T> { /// Lookup `id` without failing if the commit doesn't exist, and assure that `id` is inserted into our set. /// If it wasn't, associate it with the default value. Assure `update_data(data)` gets run. /// Return the commit when done. /// Note that none of the data updates happen if there was no commit named `id`. pub fn try_lookup_or_insert( &mut self, id: gix_hash::ObjectId, update_data: impl FnOnce(&mut T), ) -> Result>, try_lookup_or_insert_default::Error> { self.try_lookup_or_insert_default(id, T::default, update_data) } } /// Access and mutation impl<'find, T> Graph<'find, T> { /// Returns true if `id` has data associated with it, meaning that we processed it already. pub fn contains(&self, id: &gix_hash::oid) -> bool { self.map.contains_key(id.as_ref()) } /// Returns the data associated with `id` if available. pub fn get(&self, id: &gix_hash::oid) -> Option<&T> { self.map.get(id) } /// Returns the data associated with `id` if available as mutable reference. pub fn get_mut(&mut self, id: &gix_hash::oid) -> Option<&mut T> { self.map.get_mut(id) } /// Insert `id` into the graph and associate it with `value`, returning the previous value associated with it if it existed. pub fn insert(&mut self, id: gix_hash::ObjectId, value: T) -> Option { self.map.insert(id, value) } /// Remove all data from the graph to start over. pub fn clear(&mut self) { self.map.clear(); } /// Insert the parents of commit named `id` to the graph and associate new parents with data /// by calling `new_parent_data(parent_id, committer_timestamp)`, or update existing parents /// data with `update_existing(parent_id, &mut existing_data)`. /// If `first_parent` is `true`, only the first parent of commits will be looked at. pub fn insert_parents( &mut self, id: &gix_hash::oid, new_parent_data: &mut dyn FnMut(gix_hash::ObjectId, SecondsSinceUnixEpoch) -> T, update_existing: &mut dyn FnMut(gix_hash::ObjectId, &mut T), first_parent: bool, ) -> Result<(), insert_parents::Error> { let commit = self.lookup(id)?; let parents: SmallVec<[_; 2]> = commit.iter_parents().collect(); for parent_id in parents { let parent_id = parent_id?; match self.map.entry(parent_id) { gix_hashtable::hash_map::Entry::Vacant(entry) => { let parent = match try_lookup(&parent_id, &*self.find, self.cache.as_ref(), &mut self.parent_buf)? { Some(p) => p, None => continue, // skip missing objects, this is due to shallow clones for instance. }; let parent_commit_date = parent.committer_timestamp().unwrap_or_default(); entry.insert(new_parent_data(parent_id, parent_commit_date)); } gix_hashtable::hash_map::Entry::Occupied(mut entry) => { update_existing(parent_id, entry.get_mut()); } } if first_parent { break; } } Ok(()) } /// Turn ourselves into the underlying graph structure, which is a mere mapping between object ids and their data. pub fn detach(self) -> IdMap { self.map } } /// Initialization impl<'find, T> Graph<'find, T> { /// Create a new instance with `objects` to retrieve commits and optionally `cache` to accelerate commit access. /// /// ### Performance /// /// `find` should be optimized to access the same object repeatedly, ideally with an object cache to keep the last couple of /// most recently used commits. /// Furthermore, **none-existing commits should not trigger the pack-db to be refreshed.** Otherwise, performance may be sub-optimal /// in shallow repositories as running into non-existing commits will trigger a refresh of the `packs` directory. pub fn new(objects: impl gix_object::Find + 'find, cache: impl Into>) -> Self { Graph { find: Box::new(objects), cache: cache.into(), map: gix_hashtable::HashMap::default(), buf: Vec::new(), parent_buf: Vec::new(), } } } /// commit access impl<'find, T> Graph<'find, Commit> { /// Lookup `id` without failing if the commit doesn't exist, and assure that `id` is inserted into our set /// with a commit with `new_data()` assigned. /// `update_data(data)` gets run either on existing or on new data. /// /// Note that none of the data updates happen if `id` didn't exist. pub fn try_lookup_or_insert_commit_default( &mut self, id: gix_hash::ObjectId, new_data: impl FnOnce() -> T, update_data: impl FnOnce(&mut T), ) -> Result>, try_lookup_or_insert_default::Error> { match self.map.entry(id) { gix_hashtable::hash_map::Entry::Vacant(entry) => { let res = try_lookup(&id, &*self.find, self.cache.as_ref(), &mut self.buf)?; let commit = match res { None => return Ok(None), Some(commit) => commit, }; let mut commit = commit.to_owned(new_data)?; update_data(&mut commit.data); entry.insert(commit); } gix_hashtable::hash_map::Entry::Occupied(mut entry) => { update_data(&mut entry.get_mut().data); } }; Ok(self.map.get_mut(&id)) } } /// commit access impl<'find, T: Default> Graph<'find, Commit> { /// Lookup `id` without failing if the commit doesn't exist or `id` isn't a commit, /// and assure that `id` is inserted into our set with a commit and default data assigned. /// `update_data(data)` gets run either on existing or on new data. /// /// Note that none of the data updates happen if `id` didn't exist. /// /// If only commit data is desired without the need for attaching custom data, use /// [`try_lookup(id).to_owned()`][Graph::try_lookup()] instead. pub fn try_lookup_or_insert_commit( &mut self, id: gix_hash::ObjectId, update_data: impl FnOnce(&mut T), ) -> Result>, try_lookup_or_insert_default::Error> { self.try_lookup_or_insert_commit_default(id, T::default, update_data) } } /// Lazy commit access impl<'find, T> Graph<'find, T> { /// Lookup `id` without failing if the commit doesn't exist or `id` isn't a commit, /// and assure that `id` is inserted into our set /// with a `default` value assigned to it. /// `update_data(data)` gets run either on existing or no new data. /// Return the commit when done. /// /// Note that none of the data updates happen if `id` didn't exist. /// /// If only commit data is desired without the need for attaching custom data, use /// [`try_lookup(id)`][Graph::try_lookup()] instead. pub fn try_lookup_or_insert_default( &mut self, id: gix_hash::ObjectId, default: impl FnOnce() -> T, update_data: impl FnOnce(&mut T), ) -> Result>, try_lookup_or_insert_default::Error> { let res = try_lookup(&id, &*self.find, self.cache.as_ref(), &mut self.buf)?; Ok(res.map(|commit| { match self.map.entry(id) { gix_hashtable::hash_map::Entry::Vacant(entry) => { let mut data = default(); update_data(&mut data); entry.insert(data); } gix_hashtable::hash_map::Entry::Occupied(mut entry) => { update_data(entry.get_mut()); } }; commit })) } /// Try to lookup `id` and return a handle to it for accessing its data, but don't fail if the commit doesn't exist /// or isn't a commit. /// /// It's possible that commits don't exist if the repository is shallow. pub fn try_lookup( &mut self, id: &gix_hash::oid, ) -> Result>, gix_object::find::existing_iter::Error> { try_lookup(id, &*self.find, self.cache.as_ref(), &mut self.buf) } /// Lookup `id` and return a handle to it, or fail if it doesn't exist or is no commit. pub fn lookup(&mut self, id: &gix_hash::oid) -> Result, gix_object::find::existing_iter::Error> { self.try_lookup(id)? .ok_or(gix_object::find::existing_iter::Error::NotFound { oid: id.to_owned() }) } } fn try_lookup<'graph>( id: &gix_hash::oid, objects: &dyn gix_object::Find, cache: Option<&'graph gix_commitgraph::Graph>, buf: &'graph mut Vec, ) -> Result>, gix_object::find::existing_iter::Error> { if let Some(cache) = cache { if let Some(pos) = cache.lookup(id) { return Ok(Some(LazyCommit { backing: Either::Right((cache, pos)), })); } } #[allow(clippy::manual_map)] Ok( match objects .try_find(id, buf) .map_err(gix_object::find::existing_iter::Error::Find)? { Some(data) => data.kind.is_commit().then_some(LazyCommit { backing: Either::Left(buf), }), None => None, }, ) } impl<'a, 'find, T> Index<&'a gix_hash::oid> for Graph<'find, T> { type Output = T; fn index(&self, index: &'a oid) -> &Self::Output { &self.map[index] } } /// A commit that contains all information we can obtain through the commit-graph, which is typically enough to fuel any graph iteration. pub struct Commit { /// The parents of the commit. pub parents: SmallVec<[gix_hash::ObjectId; 1]>, /// The time at which the commit was created. pub commit_time: SecondsSinceUnixEpoch, /// The generation of the commit, if available. pub generation: Option, /// Any kind of data to associate with this commit. pub data: T, } impl std::fmt::Debug for Commit where T: std::fmt::Debug, { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { f.debug_struct("Commit") .field("parents", &self.parents) .field("commit_time", &self.commit_time) .field("generation", &self.generation) .field("data", &self.data) .finish() } } impl Clone for Commit where T: Clone, { fn clone(&self) -> Self { Commit { parents: self.parents.clone(), commit_time: self.commit_time, generation: self.generation, data: self.data.clone(), } } } /// A commit that provides access to graph-related information, on demand. /// /// The owned version of this type is called [`Commit`] and can be obtained by calling [`LazyCommit::to_owned()`]. pub struct LazyCommit<'graph> { backing: Either<&'graph [u8], (&'graph gix_commitgraph::Graph, gix_commitgraph::Position)>, } enum Either { Left(T), Right(U), } gix-revwalk-0.13.1/src/lib.rs000064400000000000000000000050121046102023000140470ustar 00000000000000//! Utility types for traversing the git commit-graph. //! //! This crate considers itself very much *plumbing* and is meant for consumption by other plumbing crates. #![deny(missing_docs, rust_2018_idioms)] #![forbid(unsafe_code)] /// A graph of commits which additionally allows to associate data with commits. /// /// It starts empty, but each access may fill it with commit information. /// Note that the traversal can be accelerated if a [commit-graph][gix_commitgraph::Graph] is also made available. /// /// ### About replacements /// /// Object replacements is an object database feature to substitute one object with another. We assume that this is transparently /// implemented by the `find` function that returns objects. Also we assume that the commitgraph as been written with replacements /// active to provide a consistent view. /// /// ### Odb or `find` configuration /// /// The `find` handle should be setup to *quickly determine if an object exists or not* to assure quick operation *on shallow repositories*. /// This typically means that it should not re-read the odb if there is an object miss. /// /// Most usage of the Graph will benefit from fast ODB lookups, so setting up an object cache will be beneficial. If that's not the case, /// the method docs will inform about that. /// /// Additionally, and only if `T` is [`Commit`][graph::Commit], there is *no need for an object cache* as we keep track of /// everything related to commit traversal in our own hashmap. pub struct Graph<'find, T> { /// A way to resolve a commit from the object database. find: Box, /// A way to speedup commit access, essentially a multi-file commit database. cache: Option, /// The set of cached commits that we have seen once, along with data associated with them. map: graph::IdMap, /// A buffer for writing commit data into. buf: Vec, /// Another buffer we typically use to store parents. parent_buf: Vec, } /// #[allow(clippy::empty_docs)] pub mod graph; /// A utility type implementing a queue which can be used to automatically sort data by its time in ascending order. /// /// Note that the performance of this queue is very relevant to overall algorithm performance of many graph-walking algorithms, /// and as it stands our implementation is about 6% slower in practice, probably also depending on the size of the stored data. #[derive(Default)] pub struct PriorityQueue(std::collections::BinaryHeap>); mod queue; gix-revwalk-0.13.1/src/queue.rs000064400000000000000000000063221046102023000144320ustar 00000000000000use std::{cmp::Ordering, collections::BinaryHeap}; use crate::PriorityQueue; pub(crate) struct Item { key: K, value: T, } impl std::fmt::Debug for Item { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "({:?}: {:?})", self.key, self.value) } } impl std::fmt::Debug for PriorityQueue { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { std::fmt::Debug::fmt(&self.0, f) } } impl PartialEq for Item { fn eq(&self, other: &Self) -> bool { Ord::cmp(self, other).is_eq() } } impl Eq for Item {} impl PartialOrd for Item { fn partial_cmp(&self, other: &Self) -> Option { Some(Ord::cmp(self, other)) } } impl Ord for Item { fn cmp(&self, other: &Self) -> Ordering { self.key.cmp(&other.key) } } impl Clone for Item where K: Clone, T: Clone, { fn clone(&self) -> Self { Item { key: self.key.clone(), value: self.value.clone(), } } } impl Clone for PriorityQueue where K: Clone + Ord, T: Clone, { fn clone(&self) -> Self { Self(self.0.clone()) } } impl PriorityQueue { /// Create a new instance. pub fn new() -> Self { PriorityQueue(Default::default()) } /// Insert `value` so that it is ordered according to `key`. pub fn insert(&mut self, key: K, value: T) { self.0.push(Item { key, value }); } /// Pop the highest-priority item value off the queue. pub fn pop_value(&mut self) -> Option { self.0.pop().map(|t| t.value) } /// Pop the highest-priority item key and value off the queue. pub fn pop(&mut self) -> Option<(K, T)> { self.0.pop().map(|t| (t.key, t.value)) } /// Iterate all items ordered from highest to lowest priority. pub fn iter_unordered(&self) -> impl Iterator { self.0.iter().map(|t| &t.value) } /// Turn this instance into an iterator over its keys and values in arbitrary order. pub fn into_iter_unordered(self) -> impl Iterator { self.0.into_vec().into_iter().map(|item| (item.key, item.value)) } /// Return true if the queue is empty. pub fn is_empty(&self) -> bool { self.0.is_empty() } /// Return true the amount of items on the queue. pub fn len(&self) -> usize { self.0.len() } /// Returns the greatest item `(K, T)` tuple, as ordered by `K`, if the queue is not empty, without removing it. pub fn peek(&self) -> Option<(&K, &T)> { self.0.peek().map(|e| (&e.key, &e.value)) } /// Drop all items from the queue, without changing its capacity. pub fn clear(&mut self) { self.0.clear() } } impl FromIterator<(K, T)> for PriorityQueue { fn from_iter>(iter: I) -> Self { let mut q = PriorityQueue(BinaryHeap::new()); for (k, v) in iter { q.insert(k, v); } q } }