dce0bf3feat: browse a repository's files and history8d | 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | use crate::domain::{ |
| 8 | Actor, CommitSummary, ObjectId, OrgName, RefName, RepoName, RepoPath, |
| 9 | repository::{MembershipRepository, OrgRepository, RepoRepository}, |
| 10 | }; |
| 11 | |
| 12 | use super::{ |
| 13 | error::Result, |
| 14 | port::{Blob, GitQuery}, |
| 15 | repo::view_repo, |
| 16 | }; |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | pub const MAX_BLOB_BYTES: u64 = 1024 * 1024; |
| 24 | |
| 25 | |
| 26 | pub const LOG_LIMIT: usize = 50; |
| 27 | |
| 28 | |
| 29 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 30 | pub struct FileView { |
| 31 | pub id: ObjectId, |
| 32 | pub size: u64, |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | pub text: Option<String>, |
| 38 | pub too_large: bool, |
| 39 | } |
| 40 | |
| 41 | impl FileView { |
| 42 | |
| 43 | pub fn is_binary(&self) -> bool { |
| 44 | self.text.is_none() && !self.too_large |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | |
| 49 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 50 | pub enum Browsed { |
| 51 | |
| 52 | |
| 53 | |
| 54 | Empty, |
| 55 | Directory { |
| 56 | rev: RefName, |
| 57 | path: RepoPath, |
| 58 | |
| 59 | entries: Vec<crate::domain::TreeEntry>, |
| 60 | }, |
| 61 | File { |
| 62 | rev: RefName, |
| 63 | path: RepoPath, |
| 64 | file: FileView, |
| 65 | }, |
| 66 | } |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | #[allow(clippy::too_many_arguments)] |
| 77 | pub async fn browse_repo( |
| 78 | handle: &OrgName, |
| 79 | name: &RepoName, |
| 80 | rev: Option<&RefName>, |
| 81 | path: &RepoPath, |
| 82 | actor: &Actor, |
| 83 | orgs: &impl OrgRepository, |
| 84 | memberships: &impl MembershipRepository, |
| 85 | repos: &impl RepoRepository, |
| 86 | queries: &impl GitQuery, |
| 87 | ) -> Result<Option<Browsed>> { |
| 88 | if view_repo(handle, name, actor, orgs, memberships, repos) |
| 89 | .await? |
| 90 | .is_none() |
| 91 | { |
| 92 | return Ok(None); |
| 93 | } |
| 94 | |
| 95 | let Some(rev) = resolve_revision(handle, name, rev, queries).await? else { |
| 96 | return Ok(Some(Browsed::Empty)); |
| 97 | }; |
| 98 | |
| 99 | |
| 100 | if let Some(mut entries) = queries.list_tree(handle, name, &rev, path).await? { |
| 101 | entries.sort_by_key(crate::domain::TreeEntry::ordering_key); |
| 102 | |
| 103 | return Ok(Some(Browsed::Directory { |
| 104 | rev, |
| 105 | path: path.clone(), |
| 106 | entries, |
| 107 | })); |
| 108 | } |
| 109 | |
| 110 | let Some(blob) = queries |
| 111 | .read_blob(handle, name, &rev, path, MAX_BLOB_BYTES) |
| 112 | .await? |
| 113 | else { |
| 114 | return Ok(None); |
| 115 | }; |
| 116 | |
| 117 | Ok(Some(Browsed::File { |
| 118 | rev, |
| 119 | path: path.clone(), |
| 120 | file: view_of(blob), |
| 121 | })) |
| 122 | } |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | #[allow(clippy::too_many_arguments)] |
| 129 | pub async fn repo_log( |
| 130 | handle: &OrgName, |
| 131 | name: &RepoName, |
| 132 | rev: Option<&RefName>, |
| 133 | actor: &Actor, |
| 134 | orgs: &impl OrgRepository, |
| 135 | memberships: &impl MembershipRepository, |
| 136 | repos: &impl RepoRepository, |
| 137 | queries: &impl GitQuery, |
| 138 | ) -> Result<Option<Vec<CommitSummary>>> { |
| 139 | if view_repo(handle, name, actor, orgs, memberships, repos) |
| 140 | .await? |
| 141 | .is_none() |
| 142 | { |
| 143 | return Ok(None); |
| 144 | } |
| 145 | |
| 146 | let Some(rev) = resolve_revision(handle, name, rev, queries).await? else { |
| 147 | return Ok(Some(Vec::new())); |
| 148 | }; |
| 149 | |
| 150 | Ok(Some(queries.log(handle, name, &rev, LOG_LIMIT).await?)) |
| 151 | } |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | async fn resolve_revision( |
| 158 | handle: &OrgName, |
| 159 | name: &RepoName, |
| 160 | rev: Option<&RefName>, |
| 161 | queries: &impl GitQuery, |
| 162 | ) -> Result<Option<RefName>> { |
| 163 | match rev { |
| 164 | Some(rev) => Ok(Some(rev.clone())), |
| 165 | None => Ok(queries.default_branch(handle, name).await?), |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | fn view_of(blob: Blob) -> FileView { |
| 174 | let too_large = blob.content.is_none(); |
| 175 | let text = blob.content.and_then(|bytes| String::from_utf8(bytes).ok()); |
| 176 | |
| 177 | FileView { |
| 178 | id: blob.id, |
| 179 | size: blob.size, |
| 180 | text, |
| 181 | too_large, |
| 182 | } |
| 183 | } |