@@ -19,7 +19,6 @@ use crate::{
19
19
diff:: * ,
20
20
file_system,
21
21
file_system:: { directory, DirectoryContents , Label } ,
22
- vcs,
23
22
vcs:: git:: {
24
23
error:: * ,
25
24
reference:: { glob:: RefGlob , Ref , Rev } ,
@@ -35,7 +34,6 @@ use crate::{
35
34
} ,
36
35
} ;
37
36
use directory:: Directory ;
38
- use nonempty:: NonEmpty ;
39
37
use radicle_git_ext:: Oid ;
40
38
use std:: {
41
39
collections:: { BTreeSet , HashSet } ,
@@ -51,7 +49,7 @@ pub(super) enum CommitHistory {
51
49
}
52
50
53
51
/// A `History` that uses `git2::Commit` as the underlying artifact.
54
- pub type History = vcs:: History < Commit > ;
52
+ pub type History = crate :: vcs:: History < Commit > ;
55
53
56
54
/// Wrapper around the `git2`'s `git2::Repository` type.
57
55
/// This is to to limit the functionality that we can do
@@ -142,18 +140,6 @@ impl<'a> RepositoryRef<'a> {
142
140
Ok ( namespaces?. into_iter ( ) . collect ( ) )
143
141
}
144
142
145
- pub ( super ) fn ref_history < R > ( & self , reference : R ) -> Result < History , Error >
146
- where
147
- R : Into < Ref > ,
148
- {
149
- let reference = match self . which_namespace ( ) ? {
150
- None => reference. into ( ) ,
151
- Some ( namespace) => reference. into ( ) . namespaced ( namespace) ,
152
- }
153
- . find_ref ( self ) ?;
154
- self . to_history ( & reference)
155
- }
156
-
157
143
/// Get the [`Diff`] between two commits.
158
144
pub fn diff ( & self , from : & Rev , to : & Rev ) -> Result < Diff , Error > {
159
145
let from_commit = self . rev_to_commit ( from) ?;
@@ -169,6 +155,19 @@ impl<'a> RepositoryRef<'a> {
169
155
. and_then ( |diff| Diff :: try_from ( diff) . map_err ( Error :: from) )
170
156
}
171
157
158
+ /// Get the diff introduced by a particlar rev.
159
+ pub fn diff_of_rev ( & self , rev : & Rev ) -> Result < Diff , Error > {
160
+ let oid = self . rev_oid ( rev) ?;
161
+ let commit = self . get_commit ( oid) ?;
162
+ match commit. parents . first ( ) {
163
+ Some ( parent) => {
164
+ let parent_rev = ( * parent) . into ( ) ;
165
+ self . diff ( & parent_rev, rev)
166
+ } ,
167
+ None => self . initial_diff ( rev) ,
168
+ }
169
+ }
170
+
172
171
/// Parse an [`Oid`] from the given string.
173
172
pub fn oid ( & self , oid : & str ) -> Result < Oid , Error > {
174
173
Ok ( self . repo_ref . revparse_single ( oid) ?. id ( ) . into ( ) )
@@ -198,7 +197,7 @@ impl<'a> RepositoryRef<'a> {
198
197
/// Gets stats of `rev`.
199
198
pub fn get_stats ( & self , rev : & Rev ) -> Result < Stats , Error > {
200
199
let branches = self . list_branches ( RefScope :: Local ) ?. len ( ) ;
201
- let history = self . history ( rev. clone ( ) ) ?;
200
+ let history = self . history ( rev) ?;
202
201
let commits = history. len ( ) ;
203
202
204
203
let contributors = history
@@ -256,10 +255,19 @@ impl<'a> RepositoryRef<'a> {
256
255
pub ( super ) fn rev_to_commit ( & self , rev : & Rev ) -> Result < git2:: Commit , Error > {
257
256
match rev {
258
257
Rev :: Oid ( oid) => Ok ( self . repo_ref . find_commit ( ( * oid) . into ( ) ) ?) ,
259
- Rev :: Ref ( reference) => Ok ( reference . find_ref ( self ) ?. peel_to_commit ( ) ?) ,
258
+ Rev :: Ref ( reference) => Ok ( self . find_git2_ref ( reference ) ?. peel_to_commit ( ) ?) ,
260
259
}
261
260
}
262
261
262
+ fn find_git2_ref ( & self , reference : & Ref ) -> Result < git2:: Reference < ' _ > , Error > {
263
+ let reference = match self . which_namespace ( ) ? {
264
+ None => reference. clone ( ) ,
265
+ Some ( namespace) => reference. clone ( ) . namespaced ( namespace) ,
266
+ } ;
267
+ let git2_ref = self . repo_ref . find_reference ( & reference. to_string ( ) ) ?;
268
+ Ok ( git2_ref)
269
+ }
270
+
263
271
/// Switch to a `namespace`
264
272
pub fn switch_namespace ( & self , namespace : & str ) -> Result < ( ) , Error > {
265
273
Ok ( self . repo_ref . set_namespace ( namespace) ?)
@@ -271,18 +279,11 @@ impl<'a> RepositoryRef<'a> {
271
279
Ok ( commit)
272
280
}
273
281
274
- /// Turn a [`git2::Reference`] into a [`History`] by completing
275
- /// a revwalk over the first commit in the reference.
276
- pub ( super ) fn to_history ( & self , history : & git2:: Reference < ' a > ) -> Result < History , Error > {
277
- let head = history. peel_to_commit ( ) ?;
278
- self . commit_to_history ( head)
279
- }
280
-
281
282
/// Turn a [`git2::Reference`] into a [`History`] by completing
282
283
/// a revwalk over the first commit in the reference.
283
284
pub ( super ) fn commit_to_history ( & self , head : git2:: Commit ) -> Result < History , Error > {
284
285
let head_id = head. id ( ) ;
285
- let mut commits = NonEmpty :: new ( Commit :: try_from ( head) ?) ;
286
+ let mut commits = History :: new ( Commit :: try_from ( head) ?) ;
286
287
let mut revwalk = self . repo_ref . revwalk ( ) ?;
287
288
288
289
// Set the revwalk to the head commit
@@ -302,7 +303,7 @@ impl<'a> RepositoryRef<'a> {
302
303
commits. push ( commit) ;
303
304
}
304
305
305
- Ok ( vcs :: History ( commits) )
306
+ Ok ( commits)
306
307
}
307
308
308
309
/// Extract the signature from a commit
@@ -363,7 +364,7 @@ impl<'a> RepositoryRef<'a> {
363
364
Ok ( other == git2_oid || is_descendant)
364
365
}
365
366
366
- /// Get the history of the file system where the head of the [`NonEmpty`] is
367
+ /// Get the history of the file system where the head of the `commit` is
367
368
/// the latest commit.
368
369
pub ( super ) fn file_history (
369
370
& self ,
@@ -531,14 +532,9 @@ impl<'a> RepositoryRef<'a> {
531
532
}
532
533
533
534
/// Returns the history of `rev`.
534
- pub fn history ( & self , rev : Rev ) -> Result < History , Error > {
535
- match rev {
536
- Rev :: Ref ( reference) => self . ref_history ( reference) ,
537
- Rev :: Oid ( oid) => {
538
- let commit = self . get_git2_commit ( oid) ?;
539
- self . commit_to_history ( commit)
540
- } ,
541
- }
535
+ pub fn history ( & self , rev : & Rev ) -> Result < History , Error > {
536
+ let commit = self . rev_to_commit ( rev) ?;
537
+ self . commit_to_history ( commit)
542
538
}
543
539
}
544
540
@@ -562,7 +558,7 @@ impl Repository {
562
558
563
559
/// Since our operations are read-only when it comes to surfing a repository
564
560
/// we have a separate struct called [`RepositoryRef`]. This turns an owned
565
- /// [`Repository`], the one returend by [`Repository::new`], into a
561
+ /// [`Repository`], the one returned by [`Repository::new`], into a
566
562
/// [`RepositoryRef`].
567
563
pub fn as_ref ( & ' _ self ) -> RepositoryRef < ' _ > {
568
564
RepositoryRef { repo_ref : & self . 0 }
0 commit comments