summaryrefslogtreecommitdiffstats
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
parenta7bab81665828fd7aabf3b358a9bdfc652c5b31e (diff)
Add codeberg link parsing (#1194)
-rw-r--r--README.md4
-rw-r--r--src/git_config/git_config_entry.rs62
2 files changed, 63 insertions, 3 deletions
diff --git a/README.md b/README.md
index 07160e4a..ce043f78 100644
--- a/README.md
+++ b/README.md
@@ -47,12 +47,12 @@ Delta has many features and is very customizable; please see the [user manual](h
- Line numbering
- `n` and `N` keybindings to move between files in large diffs, and between diffs in `log -p` views (`--navigate`)
- Improved merge conflict display
-- Improved `git blame` display (syntax highlighting; `--hyperlinks` formats commits as links to GitHub/GitLab/SourceHut etc)
+- Improved `git blame` display (syntax highlighting; `--hyperlinks` formats commits as links to hosting provider etc. Supported hosting providers are: GitHub, GitLab, SourceHut, Codeberg)
- Syntax-highlights grep output from `rg`, `git grep`, `grep`, etc
- Support for Git's `--color-moved` feature.
- Code can be copied directly from the diff (`-/+` markers are removed by default).
- `diff-highlight` and `diff-so-fancy` emulation modes
-- Commit hashes can be formatted as terminal [hyperlinks](https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda) to the GitHub/GitLab/SourceHut page (`--hyperlinks`).
+- Commit hashes can be formatted as terminal [hyperlinks](https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda) to the hosting provider page (`--hyperlinks`).
File paths can also be formatted as hyperlinks for opening in your OS.
- Stylable box/line decorations to draw attention to commit, file and hunk header sections.
- Style strings (foreground color, background color, font attributes) are supported for >20 stylable elements, using the same color/style language as git
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)
+ )
+ }
}