|
1 | 1 | # An updated design for radicle-surf
|
2 | 2 |
|
3 |
| -Now we have ported the `radicle-surf` crate from its own github repo to be part of the `radicle-git` repo. We are taking this opportunity to refactor its design as well. Intuitively, `radicle-surf` provides an API so that one can use it to create a github-like UI for a git repo: |
| 3 | +## Introduction |
4 | 4 |
|
5 |
| -- Given a commit (or other types of ref), list the content: i.e. files and directories. |
6 |
| -- Generate a diff between two commits. |
7 |
| -- List the history of the commits. |
8 |
| -- List refs: Branches, Tags. |
| 5 | +Now we have ported the `radicle-surf` crate from its own github repo to be |
| 6 | +part of the `radicle-git` repo. We are taking this opportunity to refactor |
| 7 | +its design as well. Intuitively, `radicle-surf` provides an API so that one |
| 8 | +can use it to create a GitHub-like UI for a git repo: |
9 | 9 |
|
10 |
| -The main goals of the changes are: |
| 10 | +1. Code browsing: given a specific commit/ref, browse files and directories. |
| 11 | +2. Diff between two revisions that resolve into two commits. |
| 12 | +3. Retrieve the history of the commits. |
| 13 | +4. Retrieve a specific object: all its metadata. |
| 14 | +5. Retrieve the refs: Branches, Tags, Remotes, Notes and user-defined |
| 15 | +"categories", where a category is: refs/<category>/<...>. |
11 | 16 |
|
12 |
| -- make API simpler whenever possible. |
13 |
| -- address open issues in the original `radicle-surf` repo as much as possible. |
14 |
| -- not to shy away from being `git` specific. (i.e. not to consider supporting other VCS systems) |
| 17 | +## Motivation |
15 | 18 |
|
16 |
| -## Make API simpler |
| 19 | +The `radicle-surf` crate aims to provide a safe and easy-to-use API that |
| 20 | +supports the features listed in [Introduction]. Based on the existing API, |
| 21 | +the main goals of the refactoring are: |
17 | 22 |
|
18 |
| -The current API has quite a bit accidental complexity that is not inherent with the requirements, especially when we can be git specific and don't care about other VCS systems. |
| 23 | +- API review: make API simpler whenever possible. |
| 24 | +- Address open issues in the original `radicle-surf` repo as much as possible. |
| 25 | +- Not to shy away from being `git` specific. (i.e. not to consider supporting |
| 26 | +other VCS systems) |
| 27 | +- Hide away `git2` from the API. The use of `git2` should be an implementation |
| 28 | +detail. |
| 29 | + |
| 30 | +## API review |
| 31 | + |
| 32 | +The current API has quite a bit accidental complexity that is not inherent with |
| 33 | +the requirements, especially when we can be git specific and don't care about |
| 34 | +other VCS systems. |
19 | 35 |
|
20 | 36 | ### Remove the `Browser`
|
21 | 37 |
|
22 | 38 | The type `Browser` is awkward as of today:
|
23 | 39 |
|
24 |
| -- it is not a source of truth of any information. For example, `list_branches` method is just a wrapper of `Repository::list_branches`. |
| 40 | +- it is not a source of truth of any information. For example, `list_branches` |
| 41 | +method is just a wrapper of `Repository::list_branches`. |
25 | 42 | - it takes in `History`, but really works at the `Snapshot` level.
|
26 | 43 | - it is mutable but its state does not help much.
|
27 | 44 |
|
28 |
| -Can we just remove `Browser` and implement its functionalities using other types? |
| 45 | +Can we just remove `Browser` and implement its functionalities using other |
| 46 | +types? |
29 | 47 |
|
30 | 48 | - For iteratoring the history, use `History`.
|
31 | 49 | - For generating `Directory`, use `Repository` directly given a `Rev`.
|
32 | 50 | - For accessing `Branch`, `Tag` or `Commit`, use `Repository`.
|
33 | 51 |
|
34 |
| -## Remove the `Snapshot` |
| 52 | +## Remove the `Snapshot` type |
| 53 | + |
| 54 | +A `Snapshot` should be really just a tree (or `Directory`) of a `Commit` in |
| 55 | +git. Currently it is a function that returns a `Directory`. Because it is OK |
| 56 | +to be git specific, we don't need to have this generic function to create a |
| 57 | +snapshot across different VCS systems. |
| 58 | + |
| 59 | +The snapshot function can be easily implement as a method of `RepositoryRef`. |
| 60 | + |
| 61 | +## Simplify `Directory` and remove the `Tree` and `Forest` types |
| 62 | + |
| 63 | +The `Directory` type represents the file system view of a snapshot. Its field |
| 64 | +`sub_directories` is defined a `Forest` based on `Tree`. The types are |
| 65 | +over-engineered from such a simple concept. We could refactor `Directory` to |
| 66 | +use `DirectoryContents` for its items and not to use `Tree` or `Forest` at all. |
| 67 | + |
| 68 | +We also found the `list_directory()` method duplicates with `iter()` method. |
| 69 | +Hence `list_directory()` is removed, together with `SystemType` type. |
| 70 | + |
| 71 | +## The new API |
| 72 | + |
| 73 | +With the changes proposed in the previous section, we describe what the new API |
| 74 | +would look like and how they meet the requirements. |
| 75 | + |
| 76 | +### Common principles |
| 77 | + |
| 78 | +#### How to identify things that resolve into a commit |
| 79 | + |
| 80 | +In our API, it will be good to have a single way to identify all refs and |
| 81 | +objects that resolve into commits. In other words, we try to avoid using |
| 82 | +different ways at different places. Currently there are multiple types in the |
| 83 | +API for this purpose: |
| 84 | + |
| 85 | +- Commit |
| 86 | +- Oid |
| 87 | +- Rev |
| 88 | + |
| 89 | +Because `Rev` is the most high level among those and supports refs already, |
| 90 | +I think we should use `Rev` in our API as much as possible. |
| 91 | + |
| 92 | +#### How to identify History |
| 93 | + |
| 94 | +TBD |
35 | 95 |
|
36 |
| -A `Snapshot` should be really just a tree (or `Directory`) of a `Commit` in git. Currently it is a function that returns a `Directory`. Because it is OK to be git specific, we don't need to have this generic function to create a snapshot across different VCS systems. |
| 96 | +### Code browsing |
37 | 97 |
|
38 |
| -The only `snapshot` function defined currently: |
| 98 | +The user should be able to browse the files and directories for any given |
| 99 | +commits or references. The core API is: |
39 | 100 |
|
| 101 | +- Create a root Directory: |
40 | 102 | ```Rust
|
41 |
| -let snapshot = Box::new(|repository: &RepositoryRef<'a>, history: &History| { |
42 |
| - let tree = Self::get_tree(repository.repo_ref, history.0.first())?; |
43 |
| - Ok(directory::Directory::from_hash_map(tree)) |
44 |
| - }); |
| 103 | +imp RepositoryRef { |
| 104 | + pub fn snapshot(&self, rev: &Rev) -> Result<Directory, Error>; |
| 105 | +} |
45 | 106 | ```
|
46 | 107 |
|
47 |
| -The above function can be easily implement as a method of `Repository`. |
| 108 | +- Browse a Directory: |
| 109 | +```Rust |
| 110 | +impl Directory { |
| 111 | + pub fn contents(&self) -> impl Iterator<Item = &DirectoryContents>; |
| 112 | +} |
| 113 | +``` |
| 114 | +where `DirectoryContents` supports both files and sub-directories: |
| 115 | +```Rust |
| 116 | +pub enum DirectoryContents { |
| 117 | + /// The `File` variant contains the file's name and the [`File`] itself. |
| 118 | + File { |
| 119 | + /// The name of the file. |
| 120 | + name: Label, |
| 121 | + /// The file data. |
| 122 | + file: File, |
| 123 | + }, |
| 124 | + /// The `Directory` variant contains a sub-directory to the current one. |
| 125 | + Directory(Directory), |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +### Diffs |
| 130 | + |
| 131 | +The user would be able to create a diff between any two revisions that resolve |
| 132 | +into two commits. |
| 133 | + |
| 134 | +The main change is to use `Rev` instead of `Oid` to identify `from` and `to`. |
| 135 | +The core API is: |
| 136 | + |
| 137 | +```Rust |
| 138 | +imp RepositoryRef { |
| 139 | + pub fn diff(&self, from: &Rev, to: &Rev) -> Result<Diff, Error>; |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | +To help convert from `Oid` to `Rev`, we provide a helper method: |
| 144 | +```Rust |
| 145 | +imp RepositoryRef { |
| 146 | + /// Returns the Oid of `rev`. |
| 147 | + pub fn rev_oid(&self, rev: &Rev) -> Result<Oid, Error>; |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +## Error handling |
| 152 | + |
| 153 | +TBD |
0 commit comments