summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorChinmay Dalal <dalal.chinmay.0101@gmail.com>2022-10-06 23:08:30 +0530
committerGitHub <noreply@github.com>2022-10-06 13:38:30 -0400
commitae4f77493655b50f763b683d3c2edb43e3bc8f41 (patch)
tree179f5c5693fad95544faf19c354afb879b27b242 /src
parenta7bab81665828fd7aabf3b358a9bdfc652c5b31e (diff)
Add codeberg link parsing (#1194)
Diffstat (limited to 'src')
-rw-r--r--src/git_config/git_config_entry.rs62
1 files changed, 61 insertions, 1 deletions
diff --git a/src/git_config/git_config_entry.rs b/src/git_config/git_config_entry.rs
index 56dcc1c2..b7bd6d2f 100644
--- a/src/git_config/git_config_entry.rs
+++ b/src/git_config/git_config_entry.rs
@@ -17,6 +17,7 @@ pub enum GitRemoteRepo {
GitHub { slug: String },
GitLab { slug: String },
SourceHut { slug: String },
+ Codeberg { slug: String },
}
impl GitRemoteRepo {
@@ -31,6 +32,9 @@ impl GitRemoteRepo {
Self::SourceHut { slug } => {
format!("https://git.sr.ht/{}/commit/{}", slug, commit)
}
+ Self::Codeberg { slug } => {
+ format!("https://codeberg.org/{}/commit/{}", slug, commit)
+ }
}
}
}
@@ -78,6 +82,20 @@ lazy_static! {
"
)
.unwrap();
+ static ref CODEBERG_REMOTE_URL: Regex = Regex::new(
+ r"(?x)
+ ^
+ (?:https://|git@)? # Support both HTTPS and SSH URLs, SSH URLs optionally omitting the git@
+ codeberg\.org
+ [:/] # This separator differs between SSH and HTTPS URLs
+ ([^/]+) # Capture the user/org name
+ /
+ (.+?) # Capture the repo name (lazy to avoid consuming '.git' if present)
+ (?:\.git)? # Non-capturing group to consume '.git' if present
+ $
+ "
+ )
+ .unwrap();
}
impl FromStr for GitRemoteRepo {
@@ -108,8 +126,16 @@ impl FromStr for GitRemoteRepo {
repo = caps.get(2).unwrap().as_str()
),
})
+ } else if let Some(caps) = CODEBERG_REMOTE_URL.captures(s) {
+ Ok(Self::Codeberg {
+ slug: format!(
+ "{user}/{repo}",
+ user = caps.get(1).unwrap().as_str(),
+ repo = caps.get(2).unwrap().as_str()
+ ),
+ })
} else {
- Err("Not a GitHub, GitLab or SourceHut repo.".into())
+ Err("Not a GitHub, GitLab, SourceHut or Codeberg repo.".into())
}
}
}
@@ -230,4 +256,38 @@ mod tests {
)
)
}
+
+ #[test]
+ fn test_parse_codeberg_urls() {
+ let urls = &[
+ "https://codeberg.org/someuser/somerepo.git",
+ "https://codeberg.org/someuser/somerepo",
+ "git@codeberg.org:someuser/somerepo.git",
+ "git@codeberg.org:someuser/somerepo",
+ "codeberg.org:someuser/somerepo.git",
+ "codeberg.org:someuser/somerepo",
+ ];
+ for url in urls {
+ let parsed = GitRemoteRepo::from_str(url);
+ assert!(parsed.is_ok());
+ assert_eq!(
+ parsed.unwrap(),
+ GitRemoteRepo::Codeberg {
+ slug: "someuser/somerepo".to_string()
+ }
+ );
+ }
+ }
+
+ #[test]
+ fn test_format_codeberg_commit_link() {
+ let repo = GitRemoteRepo::Codeberg {
+ slug: "dnkl/foot".to_string(),
+ };
+ let commit_hash = "1c072856ebf12419378c5098ad543c497197c6da";
+ assert_eq!(
+ repo.format_commit_url(commit_hash),
+ format!("https://codeberg.org/dnkl/foot/commit/{}", commit_hash)
+ )
+ }
}