1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//!
use std::ops::Deref;

use git_hash::{oid, ObjectId};

use crate::{
    easy,
    easy::{object::find, Object, Oid},
};

/// An [object id][ObjectId] infused with `Easy`.
impl<'repo> Oid<'repo> {
    /// Find the [`Object`] associated with this object id, and consider it an error if it doesn't exist.
    ///
    /// # Note
    ///
    /// There can only be one `ObjectRef` per `Easy`. To increase that limit, clone the `Easy`.
    pub fn object(&self) -> Result<Object<'repo>, find::existing::OdbError> {
        self.handle.find_object(self.inner)
    }

    /// Try to find the [`Object`] associated with this object id, and return `None` if it's not available locally.
    ///
    /// # Note
    ///
    /// There can only be one `ObjectRef` per `Easy`. To increase that limit, clone the `Easy`.
    pub fn try_object(&self) -> Result<Option<Object<'repo>>, find::OdbError> {
        self.handle.try_find_object(self.inner)
    }
}

impl<'repo> Deref for Oid<'repo> {
    type Target = oid;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl<'repo> Oid<'repo> {
    pub(crate) fn from_id(id: impl Into<ObjectId>, handle: &'repo easy::Handle) -> Self {
        Oid {
            inner: id.into(),
            handle,
        }
    }

    /// Turn this instance into its bare [ObjectId].
    pub fn detach(self) -> ObjectId {
        self.inner
    }
}

/// A platform to traverse commit ancestors, also referred to as commit history.
pub struct Ancestors<'repo> {
    handle: &'repo easy::Handle,
    tips: Box<dyn Iterator<Item = ObjectId>>,
    sorting: git_traverse::commit::Sorting,
    parents: git_traverse::commit::Parents,
}

///
pub mod ancestors {
    use git_odb::Find;

    use crate::{
        easy,
        easy::{oid::Ancestors, Oid},
        ext::ObjectIdExt,
    };

    impl<'repo> Oid<'repo> {
        /// Obtain a platform for traversing ancestors of this commit.
        pub fn ancestors(&self) -> Ancestors<'repo> {
            Ancestors {
                handle: self.handle,
                tips: Box::new(Some(self.inner).into_iter()),
                sorting: Default::default(),
                parents: Default::default(),
            }
        }
    }

    impl<'repo> Ancestors<'repo> {
        /// Set the sort mode for commits to the given value. The default is to order by topology.
        pub fn sorting(mut self, sorting: git_traverse::commit::Sorting) -> Self {
            self.sorting = sorting;
            self
        }

        /// Only traverse the first parent of the commit graph.
        pub fn first_parent_only(mut self) -> Self {
            self.parents = git_traverse::commit::Parents::First;
            self
        }

        /// Return an iterator to traverse all commits in the history of the commit the parent [Oid] is pointing to.
        pub fn all(&mut self) -> Iter<'_, 'repo> {
            let tips = std::mem::replace(&mut self.tips, Box::new(None.into_iter()));
            let parents = self.parents;
            let sorting = self.sorting;
            Iter {
                handle: self.handle,
                inner: Box::new(
                    git_traverse::commit::Ancestors::new(
                        tips,
                        git_traverse::commit::ancestors::State::default(),
                        move |oid, buf| {
                            self.handle
                                .objects
                                .try_find(oid, buf)
                                .ok()
                                .flatten()
                                .and_then(|obj| obj.try_into_commit_iter())
                        },
                    )
                    .sorting(sorting)
                    .parents(parents),
                ),
            }
        }
    }

    /// The iterator returned by [`Ancestors::all()`].
    pub struct Iter<'a, 'repo> {
        handle: &'repo easy::Handle,
        inner: Box<dyn Iterator<Item = Result<git_hash::ObjectId, git_traverse::commit::ancestors::Error>> + 'a>,
    }

    impl<'a, 'repo> Iterator for Iter<'a, 'repo> {
        type Item = Result<Oid<'repo>, git_traverse::commit::ancestors::Error>;

        fn next(&mut self) -> Option<Self::Item> {
            self.inner.next().map(|res| res.map(|oid| oid.attach(self.handle)))
        }
    }
}

mod impls {
    use std::{cmp::Ordering, hash::Hasher};

    use git_hash::{oid, ObjectId};

    use crate::easy::{DetachedObject, Object, Oid};
    // Eq, Hash, Ord, PartialOrd,

    impl<'a> std::hash::Hash for Oid<'a> {
        fn hash<H: Hasher>(&self, state: &mut H) {
            self.inner.hash(state)
        }
    }

    impl<'a> PartialOrd<Oid<'a>> for Oid<'a> {
        fn partial_cmp(&self, other: &Oid<'a>) -> Option<Ordering> {
            self.inner.partial_cmp(&other.inner)
        }
    }

    impl<'repo> PartialEq<Oid<'repo>> for Oid<'repo> {
        fn eq(&self, other: &Oid<'repo>) -> bool {
            self.inner == other.inner
        }
    }

    impl<'repo> PartialEq<ObjectId> for Oid<'repo> {
        fn eq(&self, other: &ObjectId) -> bool {
            &self.inner == other
        }
    }

    impl<'repo> PartialEq<oid> for Oid<'repo> {
        fn eq(&self, other: &oid) -> bool {
            self.inner == other
        }
    }

    impl<'repo> PartialEq<Object<'repo>> for Oid<'repo> {
        fn eq(&self, other: &Object<'repo>) -> bool {
            self.inner == other.id
        }
    }

    impl<'repo> PartialEq<DetachedObject> for Oid<'repo> {
        fn eq(&self, other: &DetachedObject) -> bool {
            self.inner == other.id
        }
    }

    impl<'repo> std::fmt::Debug for Oid<'repo> {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            self.inner.fmt(f)
        }
    }

    impl<'repo> AsRef<oid> for Oid<'repo> {
        fn as_ref(&self) -> &oid {
            &self.inner
        }
    }

    impl<'repo> From<Oid<'repo>> for ObjectId {
        fn from(v: Oid<'repo>) -> Self {
            v.inner
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn size_of_oid() {
        assert_eq!(
            std::mem::size_of::<Oid<'_>>(),
            32,
            "size of oid shouldn't change without notice"
        )
    }
}