History

[commitgraph] Rename LexPosition to 'file::Position'

That way it's in line with 'graph::Position'

Sebastian Thiel committed 6 years ago

Commit 6f90bee1 parent d2eec1d6 files · +72 −55Browse files

Showing the changes to git-commitgraph (5 of 6 files). Show all

Unified · Split

5 files
  1. git-commitgraph/src/file/access.rs +16 −14
  2. git-commitgraph/src/file/commit.rs +20 −16
  3. git-commitgraph/src/file/init.rs +5 −3
  4. git-commitgraph/src/file/mod.rs +13 −10
  5. git-commitgraph/src/graph/access.rs +12 −12
git-commitgraph/src/file/access.rs+16−14
@@ -1,10 +1,12 @@
1−use crate::file::commit::Commit;
2−use crate::file::{File, LexPosition, COMMIT_DATA_ENTRY_SIZE};
1+use crate::file::{self, commit::Commit, File, COMMIT_DATA_ENTRY_SIZE};
32 use git_object::{borrowed, HashKind, SHA1_SIZE};
4−use std::convert::{TryFrom, TryInto};
5−use std::fmt::{Debug, Formatter};
6−use std::path::Path;
3+use std::{
4+ convert::{TryFrom, TryInto},
5+ fmt::{Debug, Formatter},
6+ path::Path,
7+};
78
9+/// Access
810 impl File {
911 /// Returns the commit data for the commit located at the given lex position.
1012 ///
@@ -13,7 +15,7 @@ impl File { · 2 unmodified lines
1315 /// # Panics
1416 ///
1517 /// Panics if `pos` is out of bounds.
16− pub fn commit_at(&self, pos: LexPosition) -> Commit<'_> {
18+ pub fn commit_at(&self, pos: file::Position) -> Commit<'_> {
1719 Commit::new(self, pos)
1820 }
1921
@@ -24,7 +26,7 @@ impl File { · 4 unmodified lines
2426 // copied from git-odb/src/pack/index/access.rs
2527 /// Returns 20 bytes sha1 at the given index in our list of (sorted) sha1 hashes.
2628 /// The position ranges from 0 to self.num_commits()
27− pub fn id_at(&self, pos: LexPosition) -> borrowed::Id<'_> {
29+ pub fn id_at(&self, pos: file::Position) -> borrowed::Id<'_> {
2830 assert!(
2931 pos.0 < self.num_commits(),
3032 "expected lex position less than {}, got {}",
@@ -50,15 +52,15 @@ impl File { · 19 unmodified lines
5052 }
5153
5254 pub fn iter_commits(&self) -> impl Iterator<Item = Commit<'_>> {
53− (0..self.num_commits()).map(move |i| self.commit_at(LexPosition(i)))
55+ (0..self.num_commits()).map(move |i| self.commit_at(file::Position(i)))
5456 }
5557
5658 pub fn iter_ids(&self) -> impl Iterator<Item = borrowed::Id<'_>> {
57− (0..self.num_commits()).map(move |i| self.id_at(LexPosition(i)))
59+ (0..self.num_commits()).map(move |i| self.id_at(file::Position(i)))
5860 }
5961
6062 // copied from git-odb/src/pack/index/access.rs
61− pub fn lookup(&self, id: borrowed::Id<'_>) -> Option<LexPosition> {
63+ pub fn lookup(&self, id: borrowed::Id<'_>) -> Option<file::Position> {
6264 let first_byte = id.first_byte() as usize;
6365 let mut upper_bound = self.fan[first_byte];
6466 let mut lower_bound = if first_byte != 0 { self.fan[first_byte - 1] } else { 0 };
@@ -69,12 +71,12 @@ impl File { · 4 unmodified lines
6971 // it should not be if the bytes match up and the type has no destructor.
7072 while lower_bound < upper_bound {
7173 let mid = (lower_bound + upper_bound) / 2;
72− let mid_sha = self.id_at(LexPosition(mid));
74+ let mid_sha = self.id_at(file::Position(mid));
7375
7476 use std::cmp::Ordering::*;
7577 match id.cmp(&mid_sha) {
7678 Less => upper_bound = mid,
77− Equal => return Some(LexPosition(mid)),
79+ Equal => return Some(file::Position(mid)),
7880 Greater => lower_bound = mid + 1,
7981 }
8082 }
@@ -83,7 +85,7 @@ impl File { · 2 unmodified lines
8385
8486 /// Returns the number of commits in this graph file.
8587 ///
86− /// The maximum valid `LexPosition` that can be used with this file is one less than
88+ /// The maximum valid `Lexfile::Position` that can be used with this file is one less than
8789 /// `num_commits()`.
8890 pub fn num_commits(&self) -> u32 {
8991 self.fan[255]
@@ -96,7 +98,7 @@ impl File { · 6 unmodified lines
9698
9799 impl File {
98100 /// Returns the byte slice for the given commit in this file's Commit Data (CDAT) chunk.
99− pub(crate) fn commit_data_bytes(&self, pos: LexPosition) -> &[u8] {
101+ pub(crate) fn commit_data_bytes(&self, pos: file::Position) -> &[u8] {
100102 assert!(
101103 pos.0 < self.num_commits(),
102104 "expected lex position less than {}, got {}",
git-commitgraph/src/file/commit.rs+20−16
@@ -1,11 +1,15 @@
1−use crate::file::{File, LexPosition};
2−use crate::graph::Position;
1+use crate::{
2+ file::{self, File},
3+ graph,
4+};
35 use byteorder::{BigEndian, ByteOrder};
46 use git_object::{borrowed, owned, SHA1_SIZE};
57 use quick_error::quick_error;
6−use std::convert::{TryFrom, TryInto};
7−use std::fmt::{Debug, Formatter};
8−use std::slice::Chunks;
8+use std::{
9+ convert::{TryFrom, TryInto},
10+ fmt::{Debug, Formatter},
11+ slice::Chunks,
12+};
913
1014 quick_error! {
1115 #[derive(Debug)]
@@ -41,7 +45,7 @@ const EXTENDED_EDGES_MASK: u32 = 0x8000_0000; · 29 unmodified lines
4145
4246 pub struct Commit<'a> {
4347 file: &'a File,
44− lex_pos: LexPosition,
48+ lex_pos: file::Position,
4549 // We can parse the below fields lazily if needed.
4650 commit_timestamp: u64,
4751 generation: u32,
@@ -51,7 +55,7 @@ pub struct Commit<'a> { · 3 unmodified lines
5155 }
5256
5357 impl<'a> Commit<'a> {
54− pub(crate) fn new(file: &'a File, pos: LexPosition) -> Self {
58+ pub(crate) fn new(file: &'a File, pos: file::Position) -> Self {
5559 let bytes = file.commit_data_bytes(pos);
5660 Commit {
5761 file,
@@ -79,7 +83,7 @@ impl<'a> Commit<'a> { · 21 unmodified lines
7983 self.generation
8084 }
8185
82− pub fn iter_parents(&'a self) -> impl Iterator<Item = Result<Position, Error>> + 'a {
86+ pub fn iter_parents(&'a self) -> impl Iterator<Item = Result<graph::Position, Error>> + 'a {
8387 // I didn't find a combinator approach that a) was as strict as ParentIterator, b) supported
8488 // fuse-after-first-error behavior, and b) was significantly shorter or more understandable
8589 // than ParentIterator. So here we are.
@@ -93,7 +97,7 @@ impl<'a> Commit<'a> { · 7 unmodified lines
9397 self.file.id_at(self.lex_pos)
9498 }
9599
96− pub fn parent1(&self) -> Result<Option<Position>, Error> {
100+ pub fn parent1(&self) -> Result<Option<graph::Position>, Error> {
97101 self.iter_parents().next().transpose()
98102 }
99103
@@ -131,7 +135,7 @@ pub struct ParentIterator<'a> { · 31 unmodified lines
131135 }
132136
133137 impl<'a> Iterator for ParentIterator<'a> {
134− type Item = Result<Position, Error>;
138+ type Item = Result<graph::Position, Error>;
135139
136140 fn next(&mut self) -> Option<Self::Item> {
137141 let state = std::mem::replace(&mut self.state, ParentIteratorState::Exhausted);
@@ -219,7 +223,7 @@ enum ParentIteratorState<'a> { · 81 unmodified lines
219223 #[derive(Clone, Copy, Debug)]
220224 enum ParentEdge {
221225 None,
222− GraphPosition(Position),
226+ GraphPosition(graph::Position),
223227 ExtraEdgeIndex(u32),
224228 }
225229
@@ -231,7 +235,7 @@ impl ParentEdge { · 5 unmodified lines
231235 if raw & EXTENDED_EDGES_MASK != 0 {
232236 ParentEdge::ExtraEdgeIndex(raw & !EXTENDED_EDGES_MASK)
233237 } else {
234− ParentEdge::GraphPosition(Position(raw))
238+ ParentEdge::GraphPosition(graph::Position(raw))
235239 }
236240 }
237241 }
@@ -239,16 +243,16 @@ impl ParentEdge { · 1 unmodified line
239243 const LAST_EXTENDED_EDGE_MASK: u32 = 0x8000_0000;
240244
241245 enum ExtraEdge {
242− Internal(Position),
243− Last(Position),
246+ Internal(graph::Position),
247+ Last(graph::Position),
244248 }
245249
246250 impl ExtraEdge {
247251 pub fn from_raw(raw: u32) -> Self {
248252 if raw & LAST_EXTENDED_EDGE_MASK != 0 {
249− Self::Last(Position(raw & !LAST_EXTENDED_EDGE_MASK))
253+ Self::Last(graph::Position(raw & !LAST_EXTENDED_EDGE_MASK))
250254 } else {
251− Self::Internal(Position(raw))
255+ Self::Internal(graph::Position(raw))
252256 }
253257 }
254258 }
git-commitgraph/src/file/init.rs+5−3
@@ -4,9 +4,11 @@ use byteorder::{BigEndian, ByteOrder}; · 3 unmodified lines
44 use filebuffer::FileBuffer;
55 use git_object::SHA1_SIZE;
66 use quick_error::quick_error;
7−use std::convert::{TryFrom, TryInto};
8−use std::ops::Range;
9−use std::path::Path;
7+use std::{
8+ convert::{TryFrom, TryInto},
9+ ops::Range,
10+ path::Path,
11+};
1012
1113 type ChunkId = [u8; 4];
1214
git-commitgraph/src/file/mod.rs+13−10
@@ -1,15 +1,18 @@
11 //! Operations on a single commit-graph file.
22 mod access;
33 pub mod commit;
4+
45 mod init;
6+pub use init::Error;
57
68 pub use commit::Commit;
79 use filebuffer::FileBuffer;
810 use git_object::SHA1_SIZE;
9−pub use init::Error;
10−use std::fmt::{Display, Formatter};
11−use std::ops::Range;
12−use std::path::PathBuf;
11+use std::{
12+ fmt::{Display, Formatter},
13+ ops::Range,
14+ path::PathBuf,
15+};
1316
1417 const COMMIT_DATA_ENTRY_SIZE: usize = SHA1_SIZE + 16;
1518 const FAN_LEN: usize = 256;
@@ -32,16 +35,16 @@ pub struct File { · 16 unmodified lines
3235
3336 /// The position of a given commit within a graph file, starting at 0.
3437 ///
35−/// Commits within a graph file are sorted in lexicographical order by OID; a commit's lex position
38+/// Commits within a graph file are sorted in lexicographical order by OID; a commit's lexigraphical position
3639 /// is its position in this ordering. If a commit graph spans multiple files, each file's commits
37−/// start at lex position 0, so lex position is unique across a single file but is not unique across
38−/// the whole commit graph. Each commit also has a graph position (`GraphPosition`), which is unique
39−/// across the whole commit graph. In order to avoid accidentally mixing lex positions with graph
40+/// start at lexigraphical position 0, so it is unique across a single file but is not unique across
41+/// the whole commit graph. Each commit also has a graph position (`graph::Position`), which is unique
42+/// across the whole commit graph. In order to avoid accidentally mixing lexigraphical positions with graph
4043 /// positions, distinct types are used for each.
4144 #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
42−pub struct LexPosition(pub u32);
45+pub struct Position(pub u32);
4346
44−impl Display for LexPosition {
47+impl Display for Position {
4548 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
4649 self.0.fmt(f)
4750 }
git-commitgraph/src/graph/access.rs+12−12
@@ -1,12 +1,12 @@
11 use crate::{
2− file::{Commit, File, LexPosition},
3− graph::{Graph, Position},
2+ file::{self, Commit, File},
3+ graph::{self, Graph},
44 };
55 use git_object::borrowed;
66
7−/// Access convenience
7+/// Access
88 impl Graph {
9− pub fn commit_at(&self, pos: Position) -> Commit<'_> {
9+ pub fn commit_at(&self, pos: graph::Position) -> Commit<'_> {
1010 let r = self.lookup_by_pos(pos);
1111 r.file.commit_at(r.lex_pos)
1212 }
@@ -16,7 +16,7 @@ impl Graph { · 3 unmodified lines
1616 Some(r.file.commit_at(r.lex_pos))
1717 }
1818
19− pub fn id_at(&self, pos: Position) -> borrowed::Id<'_> {
19+ pub fn id_at(&self, pos: graph::Position) -> borrowed::Id<'_> {
2020 let r = self.lookup_by_pos(pos);
2121 r.file.id_at(r.lex_pos)
2222 }
@@ -31,7 +31,7 @@ impl Graph { · 8 unmodified lines
3131 self.files.iter().flat_map(|file| file.iter_ids())
3232 }
3333
34− pub fn lookup(&self, id: borrowed::Id<'_>) -> Option<Position> {
34+ pub fn lookup(&self, id: borrowed::Id<'_>) -> Option<graph::Position> {
3535 Some(self.lookup_by_id(id)?.graph_pos)
3636 }
3737
@@ -49,7 +49,7 @@ impl Graph { · 11 unmodified lines
4949 return Some(LookupByIdResult {
5050 file,
5151 lex_pos,
52− graph_pos: Position(current_file_start + lex_pos.0),
52+ graph_pos: graph::Position(current_file_start + lex_pos.0),
5353 });
5454 }
5555 current_file_start += file.num_commits();
@@ -57,7 +57,7 @@ impl Graph { · 1 unmodified line
5757 None
5858 }
5959
60− fn lookup_by_pos(&self, pos: Position) -> LookupByPositionResult<'_> {
60+ fn lookup_by_pos(&self, pos: graph::Position) -> LookupByPositionResult<'_> {
6161 let mut remaining = pos.0;
6262 for file in &self.files {
6363 match remaining.checked_sub(file.num_commits()) {
@@ -65,7 +65,7 @@ impl Graph { · 1 unmodified line
6565 None => {
6666 return LookupByPositionResult {
6767 file,
68− lex_pos: LexPosition(remaining),
68+ lex_pos: file::Position(remaining),
6969 }
7070 }
7171 }
@@ -77,12 +77,12 @@ impl Graph { · 5 unmodified lines
7777 #[derive(Clone)]
7878 struct LookupByIdResult<'a> {
7979 pub file: &'a File,
80− pub graph_pos: Position,
81− pub lex_pos: LexPosition,
80+ pub graph_pos: graph::Position,
81+ pub lex_pos: file::Position,
8282 }
8383
8484 #[derive(Clone)]
8585 struct LookupByPositionResult<'a> {
8686 pub file: &'a File,
87− pub lex_pos: LexPosition,
87+ pub lex_pos: file::Position,
8888 }