| 1 | use std::result::Result; |
| 2 | use std::str::FromStr; |
| 3 | |
| 4 | use lazy_static::lazy_static; |
| 5 | use regex::Regex; |
| 6 | |
| 7 | use crate::errors::*; |
| 8 | |
| 9 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 10 | pub enum GitRemoteRepo { |
| 11 | GitHub { slug: String }, |
| 12 | GitLab { slug: String }, |
| 13 | SourceHut { slug: String }, |
| 14 | Codeberg { slug: String }, |
| 15 | } |
| 16 | |
| 17 | impl GitRemoteRepo { |
| 18 | pub fn format_commit_url(&self, commit: &str) -> String { |
| 19 | match self { |
| 20 | Self::GitHub { slug } => { |
| 21 | format!("https://github.com/{slug}/commit/{commit}") |
| 22 | } |
| 23 | Self::GitLab { slug } => { |
| 24 | format!("https://gitlab.com/{slug}/-/commit/{commit}") |
| 25 | } |
| 26 | Self::SourceHut { slug } => { |
| 27 | format!("https://git.sr.ht/{slug}/commit/{commit}") |
| 28 | } |
| 29 | Self::Codeberg { slug } => { |
| 30 | format!("https://codeberg.org/{slug}/commit/{commit}") |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | #[cfg(test)] |
| 36 | pub fn for_testing() -> Option<GitRemoteRepo> { |
| 37 | Some(GitRemoteRepo::GitHub { |
| 38 | slug: "dandavison/delta".to_string(), |
| 39 | }) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | lazy_static! { |
| 44 | static ref GITHUB_REMOTE_URL: Regex = Regex::new( |
| 45 | r"(?x) |
| 46 | ^ |
| 47 | (?:https://|[^@]+@)? # Support both HTTPS and SSH URLs |
| 48 | github\.com |
| 49 | [:/] # This separator differs between SSH and HTTPS URLs |
| 50 | ([^/]+) # Capture the user/org name |
| 51 | / |
| 52 | (.+?) # Capture the repo name (lazy to avoid consuming '.git' if present) |
| 53 | (?:\.git)? # Non-capturing group to consume '.git' if present |
| 54 | $ |
| 55 | " |
| 56 | ) |
| 57 | .unwrap(); |
| 58 | static ref GITLAB_REMOTE_URL: Regex = Regex::new( |
| 59 | r"(?x) |
| 60 | ^ |
| 61 | (?:https://|git@)? # Support both HTTPS and SSH URLs, SSH URLs optionally omitting the git@ |
| 62 | gitlab\.com |
| 63 | [:/] # This separator differs between SSH and HTTPS URLs |
| 64 | ([^/]+) # Capture the user/org name |
| 65 | (/.*)? # Capture group(s), if any |
| 66 | / |
| 67 | (.+?) # Capture the repo name (lazy to avoid consuming '.git' if present) |
| 68 | (?:\.git)? # Non-capturing group to consume '.git' if present |
| 69 | $ |
| 70 | " |
| 71 | ) |
| 72 | .unwrap(); |
| 73 | static ref SOURCEHUT_REMOTE_URL: Regex = Regex::new( |
| 74 | r"(?x) |
| 75 | ^ |
| 76 | (?:https://|git@)? # Support both HTTPS and SSH URLs, SSH URLs optionally omitting the git@ |
| 77 | git\.sr\.ht |
| 78 | [:/] # This separator differs between SSH and HTTPS URLs |
| 79 | ~([^/]+) # Capture the username |
| 80 | / |
| 81 | (.+) # Capture the repo name |
| 82 | $ |
| 83 | " |
| 84 | ) |
| 85 | .unwrap(); |
| 86 | static ref CODEBERG_REMOTE_URL: Regex = Regex::new( |
| 87 | r"(?x) |
| 88 | ^ |
| 89 | (?:https://|git@)? # Support both HTTPS and SSH URLs, SSH URLs optionally omitting the git@ |
| 90 | codeberg\.org |
| 91 | [:/] # This separator differs between SSH and HTTPS URLs |
| 92 | ([^/]+) # Capture the user/org name |
| 93 | / |
| 94 | (.+?) # Capture the repo name (lazy to avoid consuming '.git' if present) |
| 95 | (?:\.git)? # Non-capturing group to consume '.git' if present |
| 96 | $ |
| 97 | " |
| 98 | ) |
| 99 | .unwrap(); |
| 100 | } |
| 101 | |
| 102 | impl FromStr for GitRemoteRepo { |
| 103 | type Err = Error; |
| 104 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 105 | if let Some(caps) = GITHUB_REMOTE_URL.captures(s) { |
| 106 | Ok(Self::GitHub { |
| 107 | slug: format!( |
| 108 | "{user}/{repo}", |
| 109 | user = caps.get(1).unwrap().as_str(), |
| 110 | repo = caps.get(2).unwrap().as_str() |
| 111 | ), |
| 112 | }) |
| 113 | } else if let Some(caps) = GITLAB_REMOTE_URL.captures(s) { |
| 114 | Ok(Self::GitLab { |
| 115 | slug: format!( |
| 116 | "{user}{groups}/{repo}", |
| 117 | user = caps.get(1).unwrap().as_str(), |
| 118 | groups = caps.get(2).map(|x| x.as_str()).unwrap_or_default(), |
| 119 | repo = caps.get(3).unwrap().as_str() |
| 120 | ), |
| 121 | }) |
| 122 | } else if let Some(caps) = SOURCEHUT_REMOTE_URL.captures(s) { |
| 123 | Ok(Self::SourceHut { |
| 124 | slug: format!( |
| 125 | "~{user}/{repo}", |
| 126 | user = caps.get(1).unwrap().as_str(), |
| 127 | repo = caps.get(2).unwrap().as_str() |
| 128 | ), |
| 129 | }) |
| 130 | } else if let Some(caps) = CODEBERG_REMOTE_URL.captures(s) { |
| 131 | Ok(Self::Codeberg { |
| 132 | slug: format!( |
| 133 | "{user}/{repo}", |
| 134 | user = caps.get(1).unwrap().as_str(), |
| 135 | repo = caps.get(2).unwrap().as_str() |
| 136 | ), |
| 137 | }) |
| 138 | } else { |
| 139 | Err(anyhow!("Not a GitHub, GitLab, SourceHut or Codeberg repo.")) |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | #[cfg(test)] |
| 145 | mod tests { |
| 146 | use super::*; |
| 147 | |
| 148 | #[test] |
| 149 | fn test_parse_github_urls() { |
| 150 | let urls = &[ |
| 151 | "https://github.com/dandavison/delta.git", |
| 152 | "https://github.com/dandavison/delta", |
| 153 | "[email protected]:dandavison/delta.git", |
| 154 | "[email protected]:dandavison/delta", |
| 155 | "github.com:dandavison/delta.git", |
| 156 | "github.com:dandavison/delta", |
| 157 | ]; |
| 158 | for url in urls { |
| 159 | let parsed = GitRemoteRepo::from_str(url); |
| 160 | assert!(parsed.is_ok()); |
| 161 | assert_eq!( |
| 162 | parsed.unwrap(), |
| 163 | GitRemoteRepo::GitHub { |
| 164 | slug: "dandavison/delta".to_string() |
| 165 | } |
| 166 | ); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | #[test] |
| 171 | fn test_format_github_commit_link() { |
| 172 | let repo = GitRemoteRepo::GitHub { |
| 173 | slug: "dandavison/delta".to_string(), |
| 174 | }; |
| 175 | let commit_hash = "d3b07384d113edec49eaa6238ad5ff00"; |
| 176 | assert_eq!( |
| 177 | repo.format_commit_url(commit_hash), |
| 178 | format!("https://github.com/dandavison/delta/commit/{commit_hash}") |
| 179 | ) |
| 180 | } |
| 181 | |
| 182 | #[test] |
| 183 | fn test_parse_gitlab_urls() { |
| 184 | let urls = &[ |
| 185 | ( |
| 186 | "https://gitlab.com/proj/grp/subgrp/repo.git", |
| 187 | "proj/grp/subgrp/repo", |
| 188 | ), |
| 189 | ("https://gitlab.com/proj/grp/repo.git", "proj/grp/repo"), |
| 190 | ("https://gitlab.com/proj/repo.git", "proj/repo"), |
| 191 | ("https://gitlab.com/proj/repo", "proj/repo"), |
| 192 | ( |
| 193 | "[email protected]:proj/grp/subgrp/repo.git", |
| 194 | "proj/grp/subgrp/repo", |
| 195 | ), |
| 196 | ("[email protected]:proj/repo.git", "proj/repo"), |
| 197 | ("[email protected]:proj/repo", "proj/repo"), |
| 198 | ("gitlab.com:proj/grp/repo.git", "proj/grp/repo"), |
| 199 | ("gitlab.com:proj/repo.git", "proj/repo"), |
| 200 | ("gitlab.com:proj/repo", "proj/repo"), |
| 201 | ]; |
| 202 | |
| 203 | for (url, expected) in urls { |
| 204 | let parsed = GitRemoteRepo::from_str(url); |
| 205 | assert!(parsed.is_ok()); |
| 206 | assert_eq!( |
| 207 | parsed.unwrap(), |
| 208 | GitRemoteRepo::GitLab { |
| 209 | slug: expected.to_string() |
| 210 | } |
| 211 | ); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | #[test] |
| 216 | fn test_format_gitlab_commit_link() { |
| 217 | let repo = GitRemoteRepo::GitLab { |
| 218 | slug: "proj/grp/repo".to_string(), |
| 219 | }; |
| 220 | let commit_hash = "d3b07384d113edec49eaa6238ad5ff00"; |
| 221 | assert_eq!( |
| 222 | repo.format_commit_url(commit_hash), |
| 223 | format!("https://gitlab.com/proj/grp/repo/-/commit/{commit_hash}") |
| 224 | ) |
| 225 | } |
| 226 | |
| 227 | #[test] |
| 228 | fn test_parse_sourcehut_urls() { |
| 229 | let urls = &[ |
| 230 | "https://git.sr.ht/~someuser/somerepo", |
| 231 | "[email protected]:~someuser/somerepo", |
| 232 | "git.sr.ht:~someuser/somerepo", |
| 233 | ]; |
| 234 | for url in urls { |
| 235 | let parsed = GitRemoteRepo::from_str(url); |
| 236 | assert!(parsed.is_ok()); |
| 237 | assert_eq!( |
| 238 | parsed.unwrap(), |
| 239 | GitRemoteRepo::SourceHut { |
| 240 | slug: "~someuser/somerepo".to_string() |
| 241 | } |
| 242 | ); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | #[test] |
| 247 | fn test_format_sourcehut_commit_link() { |
| 248 | let repo = GitRemoteRepo::SourceHut { |
| 249 | slug: "~someuser/somerepo".to_string(), |
| 250 | }; |
| 251 | let commit_hash = "df41ac86f08a40e64c76062fd67e238522c14990"; |
| 252 | assert_eq!( |
| 253 | repo.format_commit_url(commit_hash), |
| 254 | format!("https://git.sr.ht/~someuser/somerepo/commit/{commit_hash}") |
| 255 | ) |
| 256 | } |
| 257 | |
| 258 | #[test] |
| 259 | fn test_parse_codeberg_urls() { |
| 260 | let urls = &[ |
| 261 | "https://codeberg.org/someuser/somerepo.git", |
| 262 | "https://codeberg.org/someuser/somerepo", |
| 263 | "[email protected]:someuser/somerepo.git", |
| 264 | "[email protected]:someuser/somerepo", |
| 265 | "codeberg.org:someuser/somerepo.git", |
| 266 | "codeberg.org:someuser/somerepo", |
| 267 | ]; |
| 268 | for url in urls { |
| 269 | let parsed = GitRemoteRepo::from_str(url); |
| 270 | assert!(parsed.is_ok()); |
| 271 | assert_eq!( |
| 272 | parsed.unwrap(), |
| 273 | GitRemoteRepo::Codeberg { |
| 274 | slug: "someuser/somerepo".to_string() |
| 275 | } |
| 276 | ); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | #[test] |
| 281 | fn test_format_codeberg_commit_link() { |
| 282 | let repo = GitRemoteRepo::Codeberg { |
| 283 | slug: "dnkl/foot".to_string(), |
| 284 | }; |
| 285 | let commit_hash = "1c072856ebf12419378c5098ad543c497197c6da"; |
| 286 | assert_eq!( |
| 287 | repo.format_commit_url(commit_hash), |
| 288 | format!("https://codeberg.org/dnkl/foot/commit/{commit_hash}") |
| 289 | ) |
| 290 | } |
| 291 | } |