History

[commitgraph] refactor Graph, Position, and access module

Sebastian Thiel committed 6 years ago

Commit 3c8640e1 parent 2ed00378 files · +47 −32Browse files

Showing the changes to git-commitgraph/src (5 of 8 files). Show all

Unified · Split

5 files
  1. git-commitgraph/src/file/commit.rs +10 −10
  2. git-commitgraph/src/graph/access.rs +16 −10
  3. git-commitgraph/src/graph/init.rs +8 −4
  4. git-commitgraph/src/graph/mod.rs +5 −5
  5. git-commitgraph/src/lib.rs +1 −1
git-commitgraph/src/file/commit.rs+10−10
@@ -1,5 +1,5 @@
11 use crate::file::{File, LexPosition};
2−use crate::graph::GraphPosition;
2+use crate::graph::Position;
33 use byteorder::{BigEndian, ByteOrder};
44 use git_object::{borrowed, owned, SHA1_SIZE};
55 use quick_error::quick_error;
@@ -79,7 +79,7 @@ impl<'a> Commit<'a> { · 73 unmodified lines
7979 self.generation
8080 }
8181
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 {
8383 // I didn't find a combinator approach that a) was as strict as ParentIterator, b) supported
8484 // fuse-after-first-error behavior, and b) was significantly shorter or more understandable
8585 // than ParentIterator. So here we are.
@@ -93,7 +93,7 @@ impl<'a> Commit<'a> { · 7 unmodified lines
9393 self.file.id_at(self.lex_pos)
9494 }
9595
96− pub fn parent1(&self) -> Result<Option<GraphPosition>, Error> {
96+ pub fn parent1(&self) -> Result<Option<Position>, Error> {
9797 self.iter_parents().next().transpose()
9898 }
9999
@@ -131,7 +131,7 @@ pub struct ParentIterator<'a> { · 31 unmodified lines
131131 }
132132
133133 impl<'a> Iterator for ParentIterator<'a> {
134− type Item = Result<GraphPosition, Error>;
134+ type Item = Result<Position, Error>;
135135
136136 fn next(&mut self) -> Option<Self::Item> {
137137 let state = std::mem::replace(&mut self.state, ParentIteratorState::Exhausted);
@@ -219,7 +219,7 @@ enum ParentIteratorState<'a> { · 81 unmodified lines
219219 #[derive(Clone, Copy, Debug)]
220220 enum ParentEdge {
221221 None,
222− GraphPosition(GraphPosition),
222+ GraphPosition(Position),
223223 ExtraEdgeIndex(u32),
224224 }
225225
@@ -231,7 +231,7 @@ impl ParentEdge { · 5 unmodified lines
231231 if raw & EXTENDED_EDGES_MASK != 0 {
232232 ParentEdge::ExtraEdgeIndex(raw & !EXTENDED_EDGES_MASK)
233233 } else {
234− ParentEdge::GraphPosition(GraphPosition(raw))
234+ ParentEdge::GraphPosition(Position(raw))
235235 }
236236 }
237237 }
@@ -239,16 +239,16 @@ impl ParentEdge { · 1 unmodified line
239239 const LAST_EXTENDED_EDGE_MASK: u32 = 0x8000_0000;
240240
241241 enum ExtraEdge {
242− Internal(GraphPosition),
243− Last(GraphPosition),
242+ Internal(Position),
243+ Last(Position),
244244 }
245245
246246 impl ExtraEdge {
247247 pub fn from_raw(raw: u32) -> Self {
248248 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))
250250 } else {
251− Self::Internal(GraphPosition(raw))
251+ Self::Internal(Position(raw))
252252 }
253253 }
254254 }
git-commitgraph/src/graph/access.rs+16−10
@@ -1,9 +1,12 @@
1−use crate::file::{Commit, File, LexPosition};
2−use crate::graph::{Graph, GraphPosition};
1+use crate::{
2+ file::{Commit, File, LexPosition},
3+ graph::{Graph, Position},
4+};
35 use git_object::borrowed;
46
7+/// Access convenience
58 impl Graph {
6− pub fn commit_at(&self, pos: GraphPosition) -> Commit<'_> {
9+ pub fn commit_at(&self, pos: Position) -> Commit<'_> {
710 let r = self.lookup_by_pos(pos);
811 r.file.commit_at(r.lex_pos)
912 }
@@ -13,7 +16,7 @@ impl Graph { · 3 unmodified lines
1316 Some(r.file.commit_at(r.lex_pos))
1417 }
1518
16− pub fn id_at(&self, pos: GraphPosition) -> borrowed::Id<'_> {
19+ pub fn id_at(&self, pos: Position) -> borrowed::Id<'_> {
1720 let r = self.lookup_by_pos(pos);
1821 r.file.id_at(r.lex_pos)
1922 }
@@ -28,7 +31,7 @@ impl Graph { · 8 unmodified lines
2831 self.files.iter().flat_map(|file| file.iter_ids())
2932 }
3033
31− pub fn lookup(&self, id: borrowed::Id<'_>) -> Option<GraphPosition> {
34+ pub fn lookup(&self, id: borrowed::Id<'_>) -> Option<Position> {
3235 Some(self.lookup_by_id(id)?.graph_pos)
3336 }
3437
@@ -37,15 +40,16 @@ impl Graph { · 2 unmodified lines
3740 }
3841 }
3942
43+/// Access fundamentals
4044 impl Graph {
4145 fn lookup_by_id(&self, id: borrowed::Id<'_>) -> Option<LookupByIdResult<'_>> {
4246 let mut current_file_start = 0;
43− for file in self.files.iter() {
47+ for file in &self.files {
4448 if let Some(lex_pos) = file.lookup(id) {
4549 return Some(LookupByIdResult {
4650 file,
4751 lex_pos,
48− graph_pos: GraphPosition(current_file_start + lex_pos.0),
52+ graph_pos: Position(current_file_start + lex_pos.0),
4953 });
5054 }
5155 current_file_start += file.num_commits();
@@ -53,9 +57,9 @@ impl Graph { · 1 unmodified line
5357 None
5458 }
5559
56− fn lookup_by_pos(&self, pos: GraphPosition) -> LookupByPositionResult<'_> {
60+ fn lookup_by_pos(&self, pos: Position) -> LookupByPositionResult<'_> {
5761 let mut remaining = pos.0;
58− for file in self.files.iter() {
62+ for file in &self.files {
5963 match remaining.checked_sub(file.num_commits()) {
6064 Some(v) => remaining = v,
6165 None => {
@@ -70,12 +74,14 @@ impl Graph { · 8 unmodified lines
7074 }
7175 }
7276
77+#[derive(Clone)]
7378 struct LookupByIdResult<'a> {
7479 pub file: &'a File,
75− pub graph_pos: GraphPosition,
80+ pub graph_pos: Position,
7681 pub lex_pos: LexPosition,
7782 }
7883
84+#[derive(Clone)]
7985 struct LookupByPositionResult<'a> {
8086 pub file: &'a File,
8187 pub lex_pos: LexPosition,
git-commitgraph/src/graph/init.rs+8−4
@@ -1,9 +1,13 @@
1−use crate::file::File;
2−use crate::{file, Graph, MAX_COMMITS};
1+use crate::{
2+ file::{self, File},
3+ Graph, MAX_COMMITS,
4+};
35 use git_object::HashKind;
46 use quick_error::quick_error;
5−use std::io::{BufRead, BufReader};
6−use std::path::{Path, PathBuf};
7+use std::{
8+ io::{BufRead, BufReader},
9+ path::{Path, PathBuf},
10+};
711
812 quick_error! {
913 #[derive(Debug)]
git-commitgraph/src/graph/mod.rs+5−5
@@ -3,7 +3,7 @@ mod access; · 2 unmodified lines
33 mod init;
44
55 use crate::file::File;
6−use std::fmt::{Display, Formatter};
6+use std::fmt;
77
88 /// A complete commit graph.
99 ///
@@ -14,11 +14,11 @@ pub struct Graph { · 4 unmodified lines
1414 files: Vec<File>,
1515 }
1616
17−#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
18−pub struct GraphPosition(pub u32);
17+#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
18+pub struct Position(pub u32);
1919
20−impl Display for GraphPosition {
21− fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
20+impl fmt::Display for Position {
21+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2222 self.0.fmt(f)
2323 }
2424 }
git-commitgraph/src/lib.rs+1−1
@@ -4,7 +4,7 @@ · 3 unmodified lines
44 pub mod file;
55 pub mod graph;
66
7−pub use graph::{Graph, GraphPosition};
7+pub use graph::Graph;
88
99 /// The maximum number of commits that can be stored in a commit graph.
1010 pub const MAX_COMMITS: u32 = (1 << 30) + (1 << 29) + (1 << 28) - 1;