Showing the changes to git-commitgraph/src (5 of 8 files). Show all
Unified · Split
| @@ -1,5 +1,5 @@ | |||
| 1 | use crate::file::{File, LexPosition}; | 1 | use crate::file::{File, LexPosition}; |
| 2 | use crate::graph::GraphPosition; | 2 | use crate::graph::Position; |
| 3 | use byteorder::{BigEndian, ByteOrder}; | 3 | use byteorder::{BigEndian, ByteOrder}; |
| 4 | use git_object::{borrowed, owned, SHA1_SIZE}; | 4 | use git_object::{borrowed, owned, SHA1_SIZE}; |
| 5 | use quick_error::quick_error; | 5 | use quick_error::quick_error; |
| @@ -79,7 +79,7 @@ impl<'a> Commit<'a> { · 73 unmodified lines | |||
| 79 | self.generation | 79 | self.generation |
| 80 | } | 80 | } |
| 81 | 81 | ||
| 82 | pub fn iter_parents(&'a self) -> impl Iterator<Item = Result<GraphPosition, Error>> + 'a { | 82 | pub fn iter_parents(&'a self) -> impl Iterator<Item = Result<Position, Error>> + 'a { |
| 83 | // I didn't find a combinator approach that a) was as strict as ParentIterator, b) supported | 83 | // I didn't find a combinator approach that a) was as strict as ParentIterator, b) supported |
| 84 | // fuse-after-first-error behavior, and b) was significantly shorter or more understandable | 84 | // fuse-after-first-error behavior, and b) was significantly shorter or more understandable |
| 85 | // than ParentIterator. So here we are. | 85 | // than ParentIterator. So here we are. |
| @@ -93,7 +93,7 @@ impl<'a> Commit<'a> { · 7 unmodified lines | |||
| 93 | self.file.id_at(self.lex_pos) | 93 | self.file.id_at(self.lex_pos) |
| 94 | } | 94 | } |
| 95 | 95 | ||
| 96 | pub fn parent1(&self) -> Result<Option<GraphPosition>, Error> { | 96 | pub fn parent1(&self) -> Result<Option<Position>, Error> { |
| 97 | self.iter_parents().next().transpose() | 97 | self.iter_parents().next().transpose() |
| 98 | } | 98 | } |
| 99 | 99 | ||
| @@ -131,7 +131,7 @@ pub struct ParentIterator<'a> { · 31 unmodified lines | |||
| 131 | } | 131 | } |
| 132 | 132 | ||
| 133 | impl<'a> Iterator for ParentIterator<'a> { | 133 | impl<'a> Iterator for ParentIterator<'a> { |
| 134 | type Item = Result<GraphPosition, Error>; | 134 | type Item = Result<Position, Error>; |
| 135 | 135 | ||
| 136 | fn next(&mut self) -> Option<Self::Item> { | 136 | fn next(&mut self) -> Option<Self::Item> { |
| 137 | let state = std::mem::replace(&mut self.state, ParentIteratorState::Exhausted); | 137 | let state = std::mem::replace(&mut self.state, ParentIteratorState::Exhausted); |
| @@ -219,7 +219,7 @@ enum ParentIteratorState<'a> { · 81 unmodified lines | |||
| 219 | #[derive(Clone, Copy, Debug)] | 219 | #[derive(Clone, Copy, Debug)] |
| 220 | enum ParentEdge { | 220 | enum ParentEdge { |
| 221 | None, | 221 | None, |
| 222 | GraphPosition(GraphPosition), | 222 | GraphPosition(Position), |
| 223 | ExtraEdgeIndex(u32), | 223 | ExtraEdgeIndex(u32), |
| 224 | } | 224 | } |
| 225 | 225 | ||
| @@ -231,7 +231,7 @@ impl ParentEdge { · 5 unmodified lines | |||
| 231 | if raw & EXTENDED_EDGES_MASK != 0 { | 231 | if raw & EXTENDED_EDGES_MASK != 0 { |
| 232 | ParentEdge::ExtraEdgeIndex(raw & !EXTENDED_EDGES_MASK) | 232 | ParentEdge::ExtraEdgeIndex(raw & !EXTENDED_EDGES_MASK) |
| 233 | } else { | 233 | } else { |
| 234 | ParentEdge::GraphPosition(GraphPosition(raw)) | 234 | ParentEdge::GraphPosition(Position(raw)) |
| 235 | } | 235 | } |
| 236 | } | 236 | } |
| 237 | } | 237 | } |
| @@ -239,16 +239,16 @@ impl ParentEdge { · 1 unmodified line | |||
| 239 | const LAST_EXTENDED_EDGE_MASK: u32 = 0x8000_0000; | 239 | const LAST_EXTENDED_EDGE_MASK: u32 = 0x8000_0000; |
| 240 | 240 | ||
| 241 | enum ExtraEdge { | 241 | enum ExtraEdge { |
| 242 | Internal(GraphPosition), | 242 | Internal(Position), |
| 243 | Last(GraphPosition), | 243 | Last(Position), |
| 244 | } | 244 | } |
| 245 | 245 | ||
| 246 | impl ExtraEdge { | 246 | impl ExtraEdge { |
| 247 | pub fn from_raw(raw: u32) -> Self { | 247 | pub fn from_raw(raw: u32) -> Self { |
| 248 | if raw & LAST_EXTENDED_EDGE_MASK != 0 { | 248 | if raw & LAST_EXTENDED_EDGE_MASK != 0 { |
| 249 | Self::Last(GraphPosition(raw & !LAST_EXTENDED_EDGE_MASK)) | 249 | Self::Last(Position(raw & !LAST_EXTENDED_EDGE_MASK)) |
| 250 | } else { | 250 | } else { |
| 251 | Self::Internal(GraphPosition(raw)) | 251 | Self::Internal(Position(raw)) |
| 252 | } | 252 | } |
| 253 | } | 253 | } |
| 254 | } | 254 | } |
| @@ -1,9 +1,12 @@ | |||
| 1 | use crate::file::{Commit, File, LexPosition}; | 1 | use crate::{ |
| 2 | use crate::graph::{Graph, GraphPosition}; | 2 | file::{Commit, File, LexPosition}, |
| 3 | graph::{Graph, Position}, | ||
| 4 | }; | ||
| 3 | use git_object::borrowed; | 5 | use git_object::borrowed; |
| 4 | 6 | ||
| 7 | /// Access convenience | ||
| 5 | impl Graph { | 8 | impl Graph { |
| 6 | pub fn commit_at(&self, pos: GraphPosition) -> Commit<'_> { | 9 | pub fn commit_at(&self, pos: Position) -> Commit<'_> { |
| 7 | let r = self.lookup_by_pos(pos); | 10 | let r = self.lookup_by_pos(pos); |
| 8 | r.file.commit_at(r.lex_pos) | 11 | r.file.commit_at(r.lex_pos) |
| 9 | } | 12 | } |
| @@ -13,7 +16,7 @@ impl Graph { · 3 unmodified lines | |||
| 13 | Some(r.file.commit_at(r.lex_pos)) | 16 | Some(r.file.commit_at(r.lex_pos)) |
| 14 | } | 17 | } |
| 15 | 18 | ||
| 16 | pub fn id_at(&self, pos: GraphPosition) -> borrowed::Id<'_> { | 19 | pub fn id_at(&self, pos: Position) -> borrowed::Id<'_> { |
| 17 | let r = self.lookup_by_pos(pos); | 20 | let r = self.lookup_by_pos(pos); |
| 18 | r.file.id_at(r.lex_pos) | 21 | r.file.id_at(r.lex_pos) |
| 19 | } | 22 | } |
| @@ -28,7 +31,7 @@ impl Graph { · 8 unmodified lines | |||
| 28 | self.files.iter().flat_map(|file| file.iter_ids()) | 31 | self.files.iter().flat_map(|file| file.iter_ids()) |
| 29 | } | 32 | } |
| 30 | 33 | ||
| 31 | pub fn lookup(&self, id: borrowed::Id<'_>) -> Option<GraphPosition> { | 34 | pub fn lookup(&self, id: borrowed::Id<'_>) -> Option<Position> { |
| 32 | Some(self.lookup_by_id(id)?.graph_pos) | 35 | Some(self.lookup_by_id(id)?.graph_pos) |
| 33 | } | 36 | } |
| 34 | 37 | ||
| @@ -37,15 +40,16 @@ impl Graph { · 2 unmodified lines | |||
| 37 | } | 40 | } |
| 38 | } | 41 | } |
| 39 | 42 | ||
| 43 | /// Access fundamentals | ||
| 40 | impl Graph { | 44 | impl Graph { |
| 41 | fn lookup_by_id(&self, id: borrowed::Id<'_>) -> Option<LookupByIdResult<'_>> { | 45 | fn lookup_by_id(&self, id: borrowed::Id<'_>) -> Option<LookupByIdResult<'_>> { |
| 42 | let mut current_file_start = 0; | 46 | let mut current_file_start = 0; |
| 43 | for file in self.files.iter() { | 47 | for file in &self.files { |
| 44 | if let Some(lex_pos) = file.lookup(id) { | 48 | if let Some(lex_pos) = file.lookup(id) { |
| 45 | return Some(LookupByIdResult { | 49 | return Some(LookupByIdResult { |
| 46 | file, | 50 | file, |
| 47 | lex_pos, | 51 | lex_pos, |
| 48 | graph_pos: GraphPosition(current_file_start + lex_pos.0), | 52 | graph_pos: Position(current_file_start + lex_pos.0), |
| 49 | }); | 53 | }); |
| 50 | } | 54 | } |
| 51 | current_file_start += file.num_commits(); | 55 | current_file_start += file.num_commits(); |
| @@ -53,9 +57,9 @@ impl Graph { · 1 unmodified line | |||
| 53 | None | 57 | None |
| 54 | } | 58 | } |
| 55 | 59 | ||
| 56 | fn lookup_by_pos(&self, pos: GraphPosition) -> LookupByPositionResult<'_> { | 60 | fn lookup_by_pos(&self, pos: Position) -> LookupByPositionResult<'_> { |
| 57 | let mut remaining = pos.0; | 61 | let mut remaining = pos.0; |
| 58 | for file in self.files.iter() { | 62 | for file in &self.files { |
| 59 | match remaining.checked_sub(file.num_commits()) { | 63 | match remaining.checked_sub(file.num_commits()) { |
| 60 | Some(v) => remaining = v, | 64 | Some(v) => remaining = v, |
| 61 | None => { | 65 | None => { |
| @@ -70,12 +74,14 @@ impl Graph { · 8 unmodified lines | |||
| 70 | } | 74 | } |
| 71 | } | 75 | } |
| 72 | 76 | ||
| 77 | #[derive(Clone)] | ||
| 73 | struct LookupByIdResult<'a> { | 78 | struct LookupByIdResult<'a> { |
| 74 | pub file: &'a File, | 79 | pub file: &'a File, |
| 75 | pub graph_pos: GraphPosition, | 80 | pub graph_pos: Position, |
| 76 | pub lex_pos: LexPosition, | 81 | pub lex_pos: LexPosition, |
| 77 | } | 82 | } |
| 78 | 83 | ||
| 84 | #[derive(Clone)] | ||
| 79 | struct LookupByPositionResult<'a> { | 85 | struct LookupByPositionResult<'a> { |
| 80 | pub file: &'a File, | 86 | pub file: &'a File, |
| 81 | pub lex_pos: LexPosition, | 87 | pub lex_pos: LexPosition, |
| @@ -1,9 +1,13 @@ | |||
| 1 | use crate::file::File; | 1 | use crate::{ |
| 2 | use crate::{file, Graph, MAX_COMMITS}; | 2 | file::{self, File}, |
| 3 | Graph, MAX_COMMITS, | ||
| 4 | }; | ||
| 3 | use git_object::HashKind; | 5 | use git_object::HashKind; |
| 4 | use quick_error::quick_error; | 6 | use quick_error::quick_error; |
| 5 | use std::io::{BufRead, BufReader}; | 7 | use std::{ |
| 6 | use std::path::{Path, PathBuf}; | 8 | io::{BufRead, BufReader}, |
| 9 | path::{Path, PathBuf}, | ||
| 10 | }; | ||
| 7 | 11 | ||
| 8 | quick_error! { | 12 | quick_error! { |
| 9 | #[derive(Debug)] | 13 | #[derive(Debug)] |
| @@ -3,7 +3,7 @@ mod access; · 2 unmodified lines | |||
| 3 | mod init; | 3 | mod init; |
| 4 | 4 | ||
| 5 | use crate::file::File; | 5 | use crate::file::File; |
| 6 | use std::fmt::{Display, Formatter}; | 6 | use std::fmt; |
| 7 | 7 | ||
| 8 | /// A complete commit graph. | 8 | /// A complete commit graph. |
| 9 | /// | 9 | /// |
| @@ -14,11 +14,11 @@ pub struct Graph { · 4 unmodified lines | |||
| 14 | files: Vec<File>, | 14 | files: Vec<File>, |
| 15 | } | 15 | } |
| 16 | 16 | ||
| 17 | #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)] | 17 | #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)] |
| 18 | pub struct GraphPosition(pub u32); | 18 | pub struct Position(pub u32); |
| 19 | 19 | ||
| 20 | impl Display for GraphPosition { | 20 | impl fmt::Display for Position { |
| 21 | fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | 21 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 22 | self.0.fmt(f) | 22 | self.0.fmt(f) |
| 23 | } | 23 | } |
| 24 | } | 24 | } |
| @@ -4,7 +4,7 @@ · 3 unmodified lines | |||
| 4 | pub mod file; | 4 | pub mod file; |
| 5 | pub mod graph; | 5 | pub mod graph; |
| 6 | 6 | ||
| 7 | pub use graph::{Graph, GraphPosition}; | 7 | pub use graph::Graph; |
| 8 | 8 | ||
| 9 | /// The maximum number of commits that can be stored in a commit graph. | 9 | /// The maximum number of commits that can be stored in a commit graph. |
| 10 | pub const MAX_COMMITS: u32 = (1 << 30) + (1 << 29) + (1 << 28) - 1; | 10 | pub const MAX_COMMITS: u32 = (1 << 30) + (1 << 29) + (1 << 28) - 1; |