1
- use crate :: { bstr:: BStr , Commit , Tree } ;
1
+ use crate :: { bstr:: BStr , Commit , DetachedObject , Tree } ;
2
2
3
3
mod error {
4
4
use crate :: object;
@@ -18,8 +18,26 @@ mod error {
18
18
}
19
19
}
20
20
21
+ use crate :: id:: Ancestors ;
22
+
21
23
pub use error:: Error ;
22
24
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
+
23
41
impl < ' repo > Commit < ' repo > {
24
42
/// Turn this objects id into a shortened id with a length in hex as configured by `core.abbrev`.
25
43
pub fn short_id ( & self ) -> Result < git_hash:: Prefix , crate :: id:: prefix:: Error > {
@@ -51,6 +69,21 @@ impl<'repo> Commit<'repo> {
51
69
git_object:: CommitRef :: from_bytes ( & self . data )
52
70
}
53
71
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
+
54
87
/// Parse the commit and return the the tree object it points to.
55
88
pub fn tree ( & self ) -> Result < Tree < ' repo > , Error > {
56
89
let tree_id = self . tree_id ( ) . ok_or ( Error :: Decode ) ?;
@@ -64,4 +97,15 @@ impl<'repo> Commit<'repo> {
64
97
pub fn tree_id ( & self ) -> Option < git_hash:: ObjectId > {
65
98
git_object:: CommitRefIter :: from_bytes ( & self . data ) . tree_id ( )
66
99
}
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
+ }
67
111
}
0 commit comments