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 @@
1use crate::file::{File, LexPosition};1use crate::file::{File, LexPosition};
2use crate::graph::GraphPosition;2use crate::graph::Position;
3use byteorder::{BigEndian, ByteOrder};3use byteorder::{BigEndian, ByteOrder};
4use git_object::{borrowed, owned, SHA1_SIZE};4use git_object::{borrowed, owned, SHA1_SIZE};
5use quick_error::quick_error;5use quick_error::quick_error;
@@ -79,7 +79,7 @@ impl<'a> Commit<'a> { · 73 unmodified lines
79 self.generation79 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) supported83 // 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 understandable84 // 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
133impl<'a> Iterator for ParentIterator<'a> {133impl<'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)]
220enum ParentEdge {220enum 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
239const LAST_EXTENDED_EDGE_MASK: u32 = 0x8000_0000;239const LAST_EXTENDED_EDGE_MASK: u32 = 0x8000_0000;
240 240
241enum ExtraEdge {241enum ExtraEdge {
242 Internal(GraphPosition),242 Internal(Position),
243 Last(GraphPosition),243 Last(Position),
244}244}
245 245
246impl ExtraEdge {246impl 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}
git-commitgraph/src/graph/access.rs+16−10
@@ -1,9 +1,12 @@
1use crate::file::{Commit, File, LexPosition};1use crate::{
2use crate::graph::{Graph, GraphPosition};2 file::{Commit, File, LexPosition},
3 graph::{Graph, Position},
4};
3use git_object::borrowed;5use git_object::borrowed;
4 6
7/// Access convenience
5impl Graph {8impl 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
40impl Graph {44impl 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 None57 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)]
73struct LookupByIdResult<'a> {78struct 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)]
79struct LookupByPositionResult<'a> {85struct LookupByPositionResult<'a> {
80 pub file: &'a File,86 pub file: &'a File,
81 pub lex_pos: LexPosition,87 pub lex_pos: LexPosition,
git-commitgraph/src/graph/init.rs+8−4
@@ -1,9 +1,13 @@
1use crate::file::File;1use crate::{
2use crate::{file, Graph, MAX_COMMITS};2 file::{self, File},
3 Graph, MAX_COMMITS,
4};
3use git_object::HashKind;5use git_object::HashKind;
4use quick_error::quick_error;6use quick_error::quick_error;
5use std::io::{BufRead, BufReader};7use std::{
6use std::path::{Path, PathBuf};8 io::{BufRead, BufReader},
9 path::{Path, PathBuf},
10};
7 11
8quick_error! {12quick_error! {
9 #[derive(Debug)]13 #[derive(Debug)]
git-commitgraph/src/graph/mod.rs+5−5
@@ -3,7 +3,7 @@ mod access; · 2 unmodified lines
3mod init;3mod init;
4 4
5use crate::file::File;5use crate::file::File;
6use std::fmt::{Display, Formatter};6use 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)]
18pub struct GraphPosition(pub u32);18pub struct Position(pub u32);
19 19
20impl Display for GraphPosition {20impl 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}
git-commitgraph/src/lib.rs+1−1
@@ -4,7 +4,7 @@ · 3 unmodified lines
4pub mod file;4pub mod file;
5pub mod graph;5pub mod graph;
6 6
7pub use graph::{Graph, GraphPosition};7pub 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.
10pub const MAX_COMMITS: u32 = (1 << 30) + (1 << 29) + (1 << 28) - 1;10pub const MAX_COMMITS: u32 = (1 << 30) + (1 << 29) + (1 << 28) - 1;