Skip to content

Commit 3c8640e

Browse files
committed
[commitgraph] refactor Graph, Position, and access module
1 parent 2ed0037 commit 3c8640e

File tree

8 files changed

+47
-32
lines changed

8 files changed

+47
-32
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,9 @@ become available.
197197
* [ ] API documentation with examples
198198

199199
### git-commitgraph
200-
* Access to all capabilities provided by the file format, as well as their maintenance
200+
* [x] read-only access
201+
* [x] Graph lookup of commit information to obtain timestamps, generation and parents
202+
* [ ] create and update graphs and graph files
201203
* [ ] API documentation with examples
202204

203205
### git-config

git-commitgraph/src/file/commit.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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> {
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> {
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> {
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> {
219219
#[derive(Clone, Copy, Debug)]
220220
enum ParentEdge {
221221
None,
222-
GraphPosition(GraphPosition),
222+
GraphPosition(Position),
223223
ExtraEdgeIndex(u32),
224224
}
225225

@@ -231,24 +231,24 @@ impl ParentEdge {
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
}
238238

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {
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 {
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,25 +40,26 @@ impl Graph {
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();
5256
}
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 {
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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ mod access;
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 {
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
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;

git-commitgraph/tests/commitgraph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use git_commitgraph::{Graph, GraphPosition};
1+
use git_commitgraph::{graph::Position as GraphPosition, Graph};
22
use git_object::{borrowed, owned};
33
use std::{
44
collections::{HashMap, HashSet},

tasks.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
* [ ] full docs
2121
* **git-commitgraph** review
2222
* [x] adjust tests to disable gpgsignatures
23+
* [ ] ~~do graph results need a reference to their owning file?~~
24+
* Yes, as it allows to obtain additional information related to the item in the file itself, like `File::commit_at(…)`
25+
* [ ] feature-toggled support for serde
2326
* [ ] ~~make tests depend on checked-in fixtures, instead of generating them (and depend on git on CI), making it easy to recreate them~~
2427
* the tests currently rely on calling git, see `inspect_refs(…)`
2528
* **git-config**

0 commit comments

Comments
 (0)