Skip to content

Commit 7fe6993

Browse files
committed
Merge remote-tracking branch 'han/new-design-3'
* han/new-design-3: radicle-surf: refactor the design: part 3 Signed-off-by: Fintan Halpenny <[email protected]>
2 parents 207b554 + a4bc76f commit 7fe6993

File tree

6 files changed

+86
-130
lines changed

6 files changed

+86
-130
lines changed

radicle-surf/docs/refactor-design.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ use `DirectoryContents` for its items and not to use `Tree` or `Forest` at all.
6868
We also found the `list_directory()` method duplicates with `iter()` method.
6969
Hence `list_directory()` is removed, together with `SystemType` type.
7070

71+
## Remove `Vcs` trait
72+
73+
The `Vcs` trait was introduced to support different version control backends,
74+
for example both Git and Pijul, and potentially others. However, since this
75+
port is part of `radicle-git` repo, we are only supporting Git going forward.
76+
We no longer need another layer of indirection defined by `Vcs` trait.
77+
7178
## The new API
7279

7380
With the changes proposed in the previous section, we describe what the new API

radicle-surf/src/commit.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ use crate::{
3030
file_system,
3131
person::Person,
3232
revision::Revision,
33-
vcs::{
34-
git::{self, BranchName, RepositoryRef, Rev},
35-
Vcs,
36-
},
33+
vcs::git::{self, BranchName, RepositoryRef, Rev},
3734
};
3835

3936
use radicle_git_ext::Oid;
@@ -244,7 +241,7 @@ where
244241
};
245242

246243
let stats = repo.get_stats(&rev)?;
247-
let headers = repo.get_history(rev)?.iter().map(Header::from).collect();
244+
let headers = repo.history(rev)?.iter().map(Header::from).collect();
248245

249246
Ok(Commits { headers, stats })
250247
}

radicle-surf/src/object/tree.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ use crate::{
3232
git::RepositoryRef,
3333
object::{Error, Info, ObjectType},
3434
revision::Revision,
35-
vcs::{
36-
git::{Branch, Rev},
37-
Vcs,
38-
},
35+
vcs::git::{Branch, Rev},
3936
};
4037

4138
/// Result of a directory listing, carries other trees and blobs.
@@ -158,7 +155,7 @@ where
158155
entries.sort_by(|a, b| a.info.object_type.cmp(&b.info.object_type));
159156

160157
let last_commit = if path.is_root() {
161-
Some(commit::Header::from(repo.get_history(rev).unwrap().first()))
158+
Some(commit::Header::from(repo.history(rev).unwrap().first()))
162159
} else {
163160
None
164161
};

radicle-surf/src/vcs.rs

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515
// You should have received a copy of the GNU General Public License
1616
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1717

18-
//! A model of a general VCS. The components consist of a [`History`]
19-
//! and a [`Vcs`] trait.
18+
//! A model of a general VCS History.
2019
21-
// use crate::file_system::directory::Directory;
2220
use nonempty::NonEmpty;
2321

2422
pub mod git;
@@ -134,34 +132,3 @@ impl<A> History<A> {
134132
.collect()
135133
}
136134
}
137-
138-
pub(crate) trait GetVcs<Error>
139-
where
140-
Self: Sized,
141-
{
142-
/// The way to identify a Repository.
143-
type RepoId;
144-
145-
/// Find a Repository
146-
fn get_repo(identifier: Self::RepoId) -> Result<Self, Error>;
147-
}
148-
149-
/// The `VCS` trait encapsulates the minimal amount of information for
150-
/// interacting with some notion of `History` from a given
151-
/// Version-Control-System.
152-
pub trait Vcs<A, Error> {
153-
/// The way to identify a History.
154-
type HistoryId;
155-
156-
/// The way to identify an artefact.
157-
type ArtefactId;
158-
159-
/// Find a History in a Repo given a way to identify it
160-
fn get_history(&self, identifier: Self::HistoryId) -> Result<History<A>, Error>;
161-
162-
/// Find all histories in a Repo
163-
fn get_histories(&self) -> Result<Vec<History<A>>, Error>;
164-
165-
/// Identify artefacts of a Repository
166-
fn get_identifier(artefact: &A) -> Self::ArtefactId;
167-
}

radicle-surf/src/vcs/git/repo.rs

Lines changed: 24 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,18 @@ use crate::{
2020
file_system,
2121
file_system::{directory, DirectoryContents, Label},
2222
vcs,
23-
vcs::{
24-
git::{
25-
error::*,
26-
reference::{glob::RefGlob, Ref, Rev},
27-
Branch,
28-
BranchName,
29-
Commit,
30-
Namespace,
31-
RefScope,
32-
Signature,
33-
Stats,
34-
Tag,
35-
TagName,
36-
},
37-
Vcs,
23+
vcs::git::{
24+
error::*,
25+
reference::{glob::RefGlob, Ref, Rev},
26+
Branch,
27+
BranchName,
28+
Commit,
29+
Namespace,
30+
RefScope,
31+
Signature,
32+
Stats,
33+
Tag,
34+
TagName,
3835
},
3936
};
4037
use directory::Directory;
@@ -145,21 +142,15 @@ impl<'a> RepositoryRef<'a> {
145142
Ok(namespaces?.into_iter().collect())
146143
}
147144

148-
pub(super) fn reference<R, P>(&self, reference: R, check: P) -> Result<History, Error>
145+
pub(super) fn ref_history<R>(&self, reference: R) -> Result<History, Error>
149146
where
150147
R: Into<Ref>,
151-
P: FnOnce(&git2::Reference) -> Option<Error>,
152148
{
153149
let reference = match self.which_namespace()? {
154150
None => reference.into(),
155151
Some(namespace) => reference.into().namespaced(namespace),
156152
}
157153
.find_ref(self)?;
158-
159-
if let Some(err) = check(&reference) {
160-
return Err(err);
161-
}
162-
163154
self.to_history(&reference)
164155
}
165156

@@ -207,7 +198,7 @@ impl<'a> RepositoryRef<'a> {
207198
/// Gets stats of `rev`.
208199
pub fn get_stats(&self, rev: &Rev) -> Result<Stats, Error> {
209200
let branches = self.list_branches(RefScope::Local)?.len();
210-
let history = self.get_history(rev.clone())?;
201+
let history = self.history(rev.clone())?;
211202
let commits = history.len();
212203

213204
let contributors = history
@@ -280,12 +271,6 @@ impl<'a> RepositoryRef<'a> {
280271
Ok(commit)
281272
}
282273

283-
/// Build a [`History`] using the `head` reference.
284-
pub fn head_history(&self) -> Result<History, Error> {
285-
let head = self.repo_ref.head()?;
286-
self.to_history(&head)
287-
}
288-
289274
/// Turn a [`git2::Reference`] into a [`History`] by completing
290275
/// a revwalk over the first commit in the reference.
291276
pub(super) fn to_history(&self, history: &git2::Reference<'a>) -> Result<History, Error> {
@@ -439,10 +424,15 @@ impl<'a> RepositoryRef<'a> {
439424
opts.skip_binary_check(true);
440425
}
441426

442-
let diff =
427+
let mut diff =
443428
self.repo_ref
444429
.diff_tree_to_tree(old_tree.as_ref(), Some(&new_tree), Some(&mut opts))?;
445430

431+
// Detect renames by default.
432+
let mut find_opts = git2::DiffFindOptions::new();
433+
find_opts.renames(true);
434+
diff.find_similar(Some(&mut find_opts))?;
435+
446436
Ok(diff)
447437
}
448438

@@ -539,40 +529,17 @@ impl<'a> RepositoryRef<'a> {
539529
rev: commit.id().to_string(),
540530
})
541531
}
542-
}
543-
544-
impl<'a> Vcs<Commit, Error> for RepositoryRef<'a> {
545-
type HistoryId = Rev;
546-
type ArtefactId = Oid;
547532

548-
fn get_history(&self, history_id: Self::HistoryId) -> Result<History, Error> {
549-
match history_id {
550-
Rev::Ref(reference) => self.reference(reference, |_| None),
533+
/// 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),
551537
Rev::Oid(oid) => {
552538
let commit = self.get_git2_commit(oid)?;
553539
self.commit_to_history(commit)
554540
},
555541
}
556542
}
557-
558-
fn get_histories(&self) -> Result<Vec<History>, Error> {
559-
self.repo_ref
560-
.references()
561-
.map_err(Error::from)
562-
.and_then(|mut references| {
563-
references.try_fold(vec![], |mut acc, reference| {
564-
reference.map_err(Error::from).and_then(|r| {
565-
let history = self.to_history(&r)?;
566-
acc.push(history);
567-
Ok(acc)
568-
})
569-
})
570-
})
571-
}
572-
573-
fn get_identifier(artifact: &Commit) -> Self::ArtefactId {
574-
artifact.id
575-
}
576543
}
577544

578545
impl<'a> std::fmt::Debug for RepositoryRef<'a> {
@@ -608,16 +575,6 @@ impl<'a> From<&'a Repository> for RepositoryRef<'a> {
608575
}
609576
}
610577

611-
impl vcs::GetVcs<Error> for Repository {
612-
type RepoId = String;
613-
614-
fn get_repo(repo_id: Self::RepoId) -> Result<Self, Error> {
615-
git2::Repository::open(&repo_id)
616-
.map(Repository)
617-
.map_err(Error::from)
618-
}
619-
}
620-
621578
impl From<git2::Repository> for Repository {
622579
fn from(repo: git2::Repository) -> Self {
623580
Repository(repo)

0 commit comments

Comments
 (0)