Skip to content

Commit 59d75fc

Browse files
committed
cleaner API for detaching objects, now for commits as well (#364)
Also: parent_ids() queryable directly on commit.
1 parent c51a925 commit 59d75fc

File tree

3 files changed

+67
-20
lines changed

3 files changed

+67
-20
lines changed

git-repository/src/object/commit.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{bstr::BStr, Commit, Tree};
1+
use crate::{bstr::BStr, Commit, DetachedObject, Tree};
22

33
mod error {
44
use crate::object;
@@ -18,8 +18,26 @@ mod error {
1818
}
1919
}
2020

21+
use crate::id::Ancestors;
22+
2123
pub use error::Error;
2224

25+
impl<'repo> Commit<'repo> {
26+
/// Create an owned instance of this object, copying our data in the process.
27+
pub fn detached(&self) -> DetachedObject {
28+
DetachedObject {
29+
id: self.id,
30+
kind: git_object::Kind::Commit,
31+
data: self.data.clone(),
32+
}
33+
}
34+
35+
/// Sever the connection to the `Repository` and turn this instance into a standalone object.
36+
pub fn detach(self) -> DetachedObject {
37+
self.into()
38+
}
39+
}
40+
2341
impl<'repo> Commit<'repo> {
2442
/// Turn this objects id into a shortened id with a length in hex as configured by `core.abbrev`.
2543
pub fn short_id(&self) -> Result<git_hash::Prefix, crate::id::prefix::Error> {
@@ -51,6 +69,21 @@ impl<'repo> Commit<'repo> {
5169
git_object::CommitRef::from_bytes(&self.data)
5270
}
5371

72+
/// Return an iterator over tokens, representing this commit piece by piece.
73+
pub fn iter(&self) -> git_object::CommitRefIter<'_> {
74+
git_object::CommitRefIter::from_bytes(&self.data)
75+
}
76+
77+
// TODO: tests
78+
/// Decode this commits parent ids on the fly without allocating.
79+
pub fn parent_ids(&self) -> impl Iterator<Item = crate::Id<'repo>> + '_ {
80+
use crate::ext::ObjectIdExt;
81+
let repo = self.repo;
82+
git_object::CommitRefIter::from_bytes(&self.data)
83+
.parent_ids()
84+
.map(move |id| id.attach(repo))
85+
}
86+
5487
/// Parse the commit and return the the tree object it points to.
5588
pub fn tree(&self) -> Result<Tree<'repo>, Error> {
5689
let tree_id = self.tree_id().ok_or(Error::Decode)?;
@@ -64,4 +97,15 @@ impl<'repo> Commit<'repo> {
6497
pub fn tree_id(&self) -> Option<git_hash::ObjectId> {
6598
git_object::CommitRefIter::from_bytes(&self.data).tree_id()
6699
}
100+
101+
/// Return our id own id with connection to this repository.
102+
pub fn id(&self) -> crate::Id<'repo> {
103+
use crate::ext::ObjectIdExt;
104+
self.id.attach(self.repo)
105+
}
106+
107+
/// Obtain a platform for traversing ancestors of this commit.
108+
pub fn ancestors(&self) -> Ancestors<'repo> {
109+
self.id().ancestors()
110+
}
67111
}

git-repository/src/object/impls.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,32 @@ impl<'repo> std::fmt::Debug for Object<'repo> {
99
}
1010

1111
impl<'repo> From<Object<'repo>> for DetachedObject {
12-
fn from(r: Object<'repo>) -> Self {
13-
r.into_owned()
12+
fn from(mut v: Object<'repo>) -> Self {
13+
DetachedObject {
14+
id: v.id,
15+
kind: v.kind,
16+
data: std::mem::take(&mut v.data),
17+
}
18+
}
19+
}
20+
21+
impl<'repo> From<Commit<'repo>> for DetachedObject {
22+
fn from(mut v: Commit<'repo>) -> Self {
23+
DetachedObject {
24+
id: v.id,
25+
kind: git_object::Kind::Commit,
26+
data: std::mem::take(&mut v.data),
27+
}
1428
}
1529
}
1630

1731
impl<'repo> From<Commit<'repo>> for Object<'repo> {
18-
fn from(mut r: Commit<'repo>) -> Self {
32+
fn from(mut v: Commit<'repo>) -> Self {
1933
Object {
20-
id: r.id,
34+
id: v.id,
2135
kind: git_object::Kind::Commit,
22-
data: steal_from_freelist(&mut r.data),
23-
repo: r.repo,
36+
data: steal_from_freelist(&mut v.data),
37+
repo: v.repo,
2438
}
2539
}
2640
}

git-repository/src/object/mod.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,26 +94,15 @@ impl<'repo> Object<'repo> {
9494

9595
impl<'repo> Object<'repo> {
9696
/// Create an owned instance of this object, copying our data in the process.
97-
pub fn to_owned(&self) -> DetachedObject {
97+
pub fn detached(&self) -> DetachedObject {
9898
DetachedObject {
9999
id: self.id,
100100
kind: self.kind,
101101
data: self.data.clone(),
102102
}
103103
}
104104

105-
/// Turn this instance into an owned one, copying our data in the process.
106-
pub fn into_owned(mut self) -> DetachedObject {
107-
DetachedObject {
108-
id: self.id,
109-
kind: self.kind,
110-
data: std::mem::take(&mut self.data),
111-
}
112-
}
113-
114-
/// Sever the connection to `Easy` and turn this instance into a standalone object.
115-
///
116-
/// Note that the data buffer will be copied in the process.
105+
/// Sever the connection to the `Repository` and turn this instance into a standalone object.
117106
pub fn detach(self) -> DetachedObject {
118107
self.into()
119108
}

0 commit comments

Comments
 (0)