History

git-commitgraph uses `oid` now (#63)

Sebastian Thiel committed 5 years ago

Commit 0b729661 parent 3930a6f6 files · +35 −46Browse files

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

Unified · Split

4 files
  1. git-commitgraph/src/file/access.rs +7 −7
  2. git-commitgraph/src/file/commit.rs +5 −5
  3. git-commitgraph/src/file/verify.rs +7 −12
  4. git-commitgraph/src/graph/access.rs +5 −5
git-commitgraph/src/file/access.rs+7−7
@@ -1,7 +1,7 @@
11 use crate::file::{self, commit::Commit, File, COMMIT_DATA_ENTRY_SIZE};
22 use git_hash::SIZE_OF_SHA1_DIGEST as SHA1_SIZE;
33 use std::{
4− convert::{TryFrom, TryInto},
4+ convert::TryInto,
55 fmt::{Debug, Formatter},
66 path::Path,
77 };
@@ -34,7 +34,7 @@ impl File { · 26 unmodified lines
3434 /// Returns 20 bytes sha1 at the given index in our list of (sorted) sha1 hashes.
3535 /// The position ranges from 0 to self.num_commits()
3636 // copied from git-odb/src/pack/index/access.rs
37− pub fn id_at(&self, pos: file::Position) -> git_hash::borrowed::Id<'_> {
37+ pub fn id_at(&self, pos: file::Position) -> &git_hash::oid {
3838 assert!(
3939 pos.0 < self.num_commits(),
4040 "expected lexigraphical position less than {}, got {}",
@@ -46,16 +46,16 @@ impl File { · 5 unmodified lines
4646 .try_into()
4747 .expect("an architecture able to hold 32 bits of integer");
4848 let start = self.oid_lookup_offset + (pos * SHA1_SIZE);
49− git_hash::borrowed::Id::try_from(&self.data[start..start + SHA1_SIZE]).expect("20 bytes SHA1 to be alright")
49+ git_hash::oid::try_from(&self.data[start..start + SHA1_SIZE]).expect("20 bytes SHA1 to be alright")
5050 }
5151
5252 /// Return an iterator over all object hashes stored in the base graph.
53− pub fn iter_base_graph_ids(&self) -> impl Iterator<Item = git_hash::borrowed::Id<'_>> {
53+ pub fn iter_base_graph_ids(&self) -> impl Iterator<Item = &git_hash::oid> {
5454 let start = self.base_graphs_list_offset.unwrap_or(0);
5555 let base_graphs_list = &self.data[start..start + (SHA1_SIZE * usize::from(self.base_graph_count))];
5656 base_graphs_list
5757 .chunks(SHA1_SIZE)
58− .map(|bytes| git_hash::borrowed::Id::try_from(bytes).expect("20 bytes SHA1 to be alright"))
58+ .map(|bytes| git_hash::oid::try_from(bytes).expect("20 bytes SHA1 to be alright"))
5959 }
6060
6161 /// return an iterator over all commits in this file.
@@ -64,13 +64,13 @@ impl File { · 2 unmodified lines
6464 }
6565
6666 /// Return an iterator over all object hashes stored in this file.
67− pub fn iter_ids(&self) -> impl Iterator<Item = git_hash::borrowed::Id<'_>> {
67+ pub fn iter_ids(&self) -> impl Iterator<Item = &git_hash::oid> {
6868 (0..self.num_commits()).map(move |i| self.id_at(file::Position(i)))
6969 }
7070
7171 /// Translate the given object hash to its position within this file, if present.
7272 // copied from git-odb/src/pack/index/access.rs
73− pub fn lookup(&self, id: git_hash::borrowed::Id<'_>) -> Option<file::Position> {
73+ pub fn lookup(&self, id: &git_hash::oid) -> Option<file::Position> {
7474 let first_byte = usize::from(id.first_byte());
7575 let mut upper_bound = self.fan[first_byte];
7676 let mut lower_bound = if first_byte != 0 { self.fan[first_byte - 1] } else { 0 };
git-commitgraph/src/file/commit.rs+5−5
@@ -6,7 +6,7 @@ use crate::{ · 5 unmodified lines
66 use byteorder::{BigEndian, ByteOrder};
77 use git_hash::SIZE_OF_SHA1_DIGEST as SHA1_SIZE;
88 use std::{
9− convert::{TryFrom, TryInto},
9+ convert::TryInto,
1010 fmt::{Debug, Formatter},
1111 slice::Chunks,
1212 };
@@ -39,7 +39,7 @@ pub struct Commit<'a> { · 26 unmodified lines
3939 generation: u32,
4040 parent1: ParentEdge,
4141 parent2: ParentEdge,
42− root_tree_id: git_hash::borrowed::Id<'a>,
42+ root_tree_id: &'a git_hash::oid,
4343 }
4444
4545 impl<'a> Commit<'a> {
@@ -48,7 +48,7 @@ impl<'a> Commit<'a> { · 2 unmodified lines
4848 Commit {
4949 file,
5050 pos,
51− root_tree_id: git_hash::borrowed::Id::try_from(&bytes[..SHA1_SIZE]).expect("20 bytes SHA1 to be alright"),
51+ root_tree_id: git_hash::oid::try_from(&bytes[..SHA1_SIZE]).expect("20 bytes SHA1 to be alright"),
5252 parent1: ParentEdge::from_raw(BigEndian::read_u32(&bytes[SHA1_SIZE..SHA1_SIZE + 4])),
5353 parent2: ParentEdge::from_raw(BigEndian::read_u32(&bytes[SHA1_SIZE + 4..SHA1_SIZE + 8])),
5454 generation: BigEndian::read_u32(&bytes[SHA1_SIZE + 8..SHA1_SIZE + 12]) >> 2,
@@ -83,7 +83,7 @@ impl<'a> Commit<'a> { · 28 unmodified lines
8383 }
8484
8585 /// Returns the hash of this commit.
86− pub fn id(&self) -> git_hash::borrowed::Id<'a> {
86+ pub fn id(&self) -> &'a git_hash::oid {
8787 self.file.id_at(self.pos)
8888 }
8989
@@ -98,7 +98,7 @@ impl<'a> Commit<'a> { · 8 unmodified lines
9898 }
9999
100100 /// Return the hash of the tree this commit points to.
101− pub fn root_tree_id(&self) -> git_hash::borrowed::Id<'a> {
101+ pub fn root_tree_id(&self) -> &git_hash::oid {
102102 self.root_tree_id
103103 }
104104 }
git-commitgraph/src/file/verify.rs+7−12
@@ -8,7 +8,6 @@ use git_hash::SIZE_OF_SHA1_DIGEST as SHA1_SIZE; · 7 unmodified lines
88 use std::{
99 cmp::{max, min},
1010 collections::HashMap,
11− convert::TryFrom,
1211 path::Path,
1312 };
1413
@@ -66,9 +65,8 @@ pub struct Outcome { · 51 unmodified lines
6665 /// Verification
6766 impl File {
6867 /// Returns the trailing checksum over the entire content of this file.
69− pub fn checksum(&self) -> git_hash::borrowed::Id<'_> {
70− git_hash::borrowed::Id::try_from(&self.data[self.data.len() - SHA1_SIZE..])
71− .expect("file to be large enough for a hash")
68+ pub fn checksum(&self) -> &git_hash::oid {
69+ git_hash::oid::try_from(&self.data[self.data.len() - SHA1_SIZE..]).expect("file to be large enough for a hash")
7270 }
7371
7472 /// Traverse all [commits][file::Commit] stored in this file and call `processor(commit) -> Result<(), Error>` on it.
@@ -83,7 +81,7 @@ impl File { · 8 unmodified lines
8381 .map_err(|(actual, expected)| Error::Mismatch { actual, expected })?;
8482 verify_split_chain_filename_hash(&self.path, self.checksum()).map_err(Error::Filename)?;
8583
86− let null_id = git_hash::borrowed::Id::null_sha1();
84+ let null_id = git_hash::oid::null_sha1();
8785
8886 let mut stats = Outcome {
8987 max_generation: 0,
@@ -94,7 +92,7 @@ impl File { · 4 unmodified lines
9492 };
9593
9694 // TODO: Verify self.fan values as we go.
97− let mut prev_id: git_hash::borrowed::Id<'a> = null_id;
95+ let mut prev_id: &git_hash::oid = null_id;
9896 for commit in self.iter_commits() {
9997 if commit.id() <= prev_id {
10098 if commit.id() == null_id {
@@ -156,7 +154,7 @@ impl File { · 55 unmodified lines
156154 let actual = git_hash::ObjectId::new_sha1(hasher.digest());
157155
158156 let expected = self.checksum();
159− if actual.to_borrowed() == expected {
157+ if actual.as_ref() == expected {
160158 Ok(actual)
161159 } else {
162160 Err((actual, expected.into()))
@@ -166,17 +164,14 @@ impl File { · 3 unmodified lines
166164
167165 /// If the given path's filename matches "graph-{hash}.graph", check that `hash` matches the
168166 /// expected hash.
169−fn verify_split_chain_filename_hash(
170− path: impl AsRef<Path>,
171− expected: git_hash::borrowed::Id<'_>,
172−) -> Result<(), String> {
167+fn verify_split_chain_filename_hash(path: impl AsRef<Path>, expected: &git_hash::oid) -> Result<(), String> {
173168 let path = path.as_ref();
174169 path.file_name()
175170 .and_then(|filename| filename.to_str())
176171 .and_then(|filename| filename.strip_suffix(".graph"))
177172 .and_then(|stem| stem.strip_prefix("graph-"))
178173 .map_or(Ok(()), |hex| match git_hash::ObjectId::from_hex(hex.as_bytes()) {
179− Ok(actual) if actual.to_borrowed() == expected => Ok(()),
174+ Ok(actual) if actual.as_ref() == expected => Ok(()),
180175 _ => Err(format!("graph-{}.graph", expected.to_sha1_hex().as_bstr())),
181176 })
182177 }
git-commitgraph/src/graph/access.rs+5−5
@@ -15,7 +15,7 @@ impl Graph { · 14 unmodified lines
1515 }
1616
1717 /// Returns the commit matching the given `id`.
18− pub fn commit_by_id(&self, id: git_hash::borrowed::Id<'_>) -> Option<Commit<'_>> {
18+ pub fn commit_by_id(&self, id: &git_hash::oid) -> Option<Commit<'_>> {
1919 let r = self.lookup_by_id(id)?;
2020 Some(r.file.commit_at(r.file_pos))
2121 }
@@ -24,7 +24,7 @@ impl Graph { · 2 unmodified lines
2424 ///
2525 /// # Panics
2626 /// If `pos` is greater or equal to [`num_commits()`][Graph::num_commits()].
27− pub fn id_at(&self, pos: graph::Position) -> git_hash::borrowed::Id<'_> {
27+ pub fn id_at(&self, pos: graph::Position) -> &git_hash::oid {
2828 let r = self.lookup_by_pos(pos);
2929 r.file.id_at(r.pos)
3030 }
@@ -35,12 +35,12 @@ impl Graph { · 4 unmodified lines
3535 }
3636
3737 /// Iterate over commit IDs in unsorted order.
38− pub fn iter_ids(&self) -> impl Iterator<Item = git_hash::borrowed::Id<'_>> {
38+ pub fn iter_ids(&self) -> impl Iterator<Item = &git_hash::oid> {
3939 self.files.iter().flat_map(|file| file.iter_ids())
4040 }
4141
4242 /// Translate the given `id` to its position in the file.
43− pub fn lookup(&self, id: git_hash::borrowed::Id<'_>) -> Option<graph::Position> {
43+ pub fn lookup(&self, id: &git_hash::oid) -> Option<graph::Position> {
4444 Some(self.lookup_by_id(id)?.graph_pos)
4545 }
4646
@@ -52,7 +52,7 @@ impl Graph { · 5 unmodified lines
5252
5353 /// Access fundamentals
5454 impl Graph {
55− pub(crate) fn lookup_by_id(&self, id: git_hash::borrowed::Id<'_>) -> Option<LookupByIdResult<'_>> {
55+ pub(crate) fn lookup_by_id(&self, id: &git_hash::oid) -> Option<LookupByIdResult<'_>> {
5656 let mut current_file_start = 0;
5757 for file in &self.files {
5858 if let Some(lex_pos) = file.lookup(id) {