History

[repository #190] reflog tests

Sebastian Thiel committed 5 years ago

Commit 641edde1 parent 4a36ded4 files · +88 −71Browse files

Unified · Split

4 files
  1. git-repository/src/easy/head.rs +13 −1
  2. git-repository/src/easy/reference/log.rs +44 −0
  3. git-repository/src/easy/reference/mod.rs +1 −44
  4. git-repository/tests/easy/ext/object.rs +30 −26
git-repository/src/easy/head.rs+13−1
@@ -3,7 +3,7 @@ · 2 unmodified lines
33 use git_hash::ObjectId;
44 use git_ref::FullNameRef;
55
6−use crate::easy::Head;
6+use crate::{easy, easy::Head, ext::ReferenceExt};
77
88 pub enum Kind {
99 /// The existing reference the symbolic HEAD points to.
@@ -35,6 +35,18 @@ impl<'repo, A> Head<'repo, A> { · 25 unmodified lines
3535 }
3636 }
3737
38+impl<'repo, A> Head<'repo, A>
39+where
40+ A: easy::Access + Sized,
41+{
42+ pub fn into_reference(self) -> easy::Reference<'repo, A> {
43+ match self.kind {
44+ Kind::Symbolic(r) => r.attach(self.access),
45+ _ => panic!("BUG: Expected head to be a born symbolic reference"),
46+ }
47+ }
48+}
49+
3850 pub mod peel {
3951 use git_hash::ObjectId;
4052
git-repository/src/easy/reference/log.rs+44−0
@@ -0,0 +1,44 @@
1+use std::{cell::RefMut, ops::DerefMut};
2+
3+use git_ref::file::ReferenceExt;
4+
5+use crate::{easy, easy::Reference};
6+
7+pub struct Buffer<'a, 'repo, A> {
8+ reference: &'a Reference<'repo, A>,
9+ buf: RefMut<'repo, Vec<u8>>,
10+}
11+
12+#[derive(Debug, thiserror::Error)]
13+pub enum Error {
14+ #[error(transparent)]
15+ Io(#[from] std::io::Error),
16+ #[error(transparent)]
17+ BorrowRepo(#[from] easy::borrow::repo::Error),
18+}
19+
20+impl<'a, 'repo, A> Buffer<'a, 'repo, A>
21+where
22+ A: easy::Access + Sized,
23+{
24+ pub fn reverse_iter(&mut self) -> Result<Option<git_ref::file::log::iter::Reverse<'_, std::fs::File>>, Error> {
25+ let buf = self.buf.deref_mut();
26+ buf.resize(512, 0);
27+ Ok(self
28+ .reference
29+ .inner
30+ .log_iter_rev(&self.reference.access.repo()?.refs, buf)?)
31+ }
32+}
33+
34+impl<'repo, A> Reference<'repo, A>
35+where
36+ A: easy::Access + Sized,
37+{
38+ pub fn log(&self) -> Result<Buffer<'_, 'repo, A>, easy::borrow::state::Error> {
39+ Ok(Buffer {
40+ reference: self,
41+ buf: self.access.state().try_borrow_mut_buf()?,
42+ })
43+ }
44+}
git-repository/src/easy/reference/mod.rsrenamed from git-repository/src/easy/reference.rs+1−44
@@ -83,50 +83,7 @@ where · 82 unmodified lines
8383 }
8484 }
8585
86−pub mod log {
87− use crate::easy;
88− use crate::easy::Reference;
89− use git_ref::file::ReferenceExt;
90− use std::cell::RefMut;
91− use std::ops::DerefMut;
92−
93− pub struct Buffer<'a, 'repo, A> {
94− reference: &'a Reference<'repo, A>,
95− buf: RefMut<'repo, Vec<u8>>,
96− }
97−
98− #[derive(Debug, thiserror::Error)]
99− pub enum Error {
100− #[error(transparent)]
101− Io(#[from] std::io::Error),
102− #[error(transparent)]
103− BorrowRepo(#[from] easy::borrow::repo::Error),
104− }
105−
106− impl<'a, 'repo, A> Buffer<'a, 'repo, A>
107− where
108− A: easy::Access + Sized,
109− {
110− pub fn reverse_iter(&mut self) -> Result<Option<git_ref::file::log::iter::Reverse<'_, std::fs::File>>, Error> {
111− Ok(self
112− .reference
113− .inner
114− .log_iter_rev(&self.reference.access.repo()?.refs, self.buf.deref_mut())?)
115− }
116− }
117−
118− impl<'repo, A> Reference<'repo, A>
119− where
120− A: easy::Access + Sized,
121− {
122− pub fn log(&self) -> Result<Buffer<'_, 'repo, A>, easy::borrow::state::Error> {
123− Ok(Buffer {
124− reference: self,
125− buf: self.access.state().try_borrow_mut_buf()?,
126− })
127− }
128− }
129−}
86+pub mod log;
13087
13188 pub mod find {
13289 use crate::easy;
git-repository/tests/easy/ext/object.rs+30−26
@@ -21,28 +21,36 @@ mod commit { · 20 unmodified lines
2121 use git_testtools::hex_to_id;
2222
2323 #[test]
24− fn single_line_initial_commit_empty_tree_ref_nonexisting() {
25− let tmp = tempfile::tempdir().unwrap();
26− let repo = git::init_bare(&tmp).unwrap().into_easy();
27− let empty_tree_id = repo.write_object(&git::objs::Tree::empty().into()).unwrap();
24+ fn single_line_initial_commit_empty_tree_ref_nonexisting() -> crate::Result {
25+ let tmp = tempfile::tempdir()?;
26+ let repo = git::init(&tmp)?.into_easy();
27+ let empty_tree_id = repo.write_object(&git::objs::Tree::empty().into())?;
2828 let author = git::actor::Signature::empty();
29− let commit_id = repo
30− .commit(
31− "HEAD",
32− "initial",
33− author.clone(),
34− author,
35− empty_tree_id,
36− git::commit::NO_PARENT_IDS,
37− )
38− .unwrap();
29+ let commit_id = repo.commit(
30+ "HEAD",
31+ "initial",
32+ author.clone(),
33+ author,
34+ empty_tree_id,
35+ git::commit::NO_PARENT_IDS,
36+ )?;
3937 assert_eq!(
4038 commit_id,
4139 hex_to_id("302ea5640358f98ba23cda66c1e664a6f274643f"),
4240 "the commit id is stable"
4341 );
4442
45− // TODO: check reflog
43+ let head = repo.head()?.into_reference();
44+ assert_eq!(
45+ head.log()?
46+ .reverse_iter()?
47+ .expect("log present")
48+ .next()
49+ .expect("one line")??
50+ .message,
51+ "commit (initial): initial"
52+ );
53+ Ok(())
4654 }
4755
4856 #[test]
@@ -96,19 +104,15 @@ mod commit { · 47 unmodified lines
96104 let current_commit = branch.peel_to_id_in_place().unwrap();
97105 assert_eq!(current_commit, second_commit_id, "the commit was set");
98106
107+ let mut log = branch.log().unwrap();
108+ let mut log_iter = log.reverse_iter().unwrap().expect("log present");
99109 assert_eq!(
100− branch
101− .log()
102− .unwrap()
103− .reverse_iter()
104− .unwrap()
105− .expect("log present")
106− .next()
107− .expect("one line")
108− .unwrap()
109− .unwrap()
110− .message,
110+ log_iter.next().expect("one line").unwrap().unwrap().message,
111111 "commit: committing into a new branch creates it"
112112 );
113+ assert!(
114+ log_iter.next().is_none(),
115+ "there is only one log line in the new branch"
116+ );
113117 }
114118 }