Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sourcehut link parsing #1190

Merged
merged 3 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/Bitbucket etc)
- Improved `git blame` display (syntax highlighting; `--hyperlinks` formats commits as links to GitHub/GitLab/SourceHut etc)
- 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/Bitbucket page (`--hyperlinks`).
- Commit hashes can be formatted as terminal [hyperlinks](https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda) to the GitHub/GitLab/SourceHut 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
Expand Down
97 changes: 78 additions & 19 deletions src/git_config/git_config_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,22 @@ pub enum GitConfigEntry {

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum GitRemoteRepo {
GitHubRepo { repo_slug: String },
GitLabRepo { repo_slug: String },
GitHub { slug: String },
GitLab { slug: String },
SourceHut { slug: String },
}

impl GitRemoteRepo {
pub fn format_commit_url(&self, commit: &str) -> String {
match self {
Self::GitHubRepo { repo_slug } => {
format!("https://github.com/{}/commit/{}", repo_slug, commit)
Self::GitHub { slug } => {
format!("https://github.com/{}/commit/{}", slug, commit)
}
Self::GitLabRepo { repo_slug } => {
format!("https://gitlab.com/{}/-/commit/{}", repo_slug, commit)
Self::GitLab { slug } => {
format!("https://gitlab.com/{}/-/commit/{}", slug, commit)
}
Self::SourceHut { slug } => {
format!("https://git.sr.ht/{}/commit/{}", slug, commit)
}
}
}
Expand Down Expand Up @@ -61,30 +65,51 @@ lazy_static! {
"
)
.unwrap();
static ref SOURCEHUT_REMOTE_URL: Regex = Regex::new(
r"(?x)
^
(?:https://|git@)? # Support both HTTPS and SSH URLs, SSH URLs optionally omitting the git@
git\.sr\.ht
[:/] # This separator differs between SSH and HTTPS URLs
~([^/]+) # Capture the username
/
(.+) # Capture the repo name
$
"
)
.unwrap();
}

impl FromStr for GitRemoteRepo {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if let Some(caps) = GITHUB_REMOTE_URL.captures(s) {
Ok(Self::GitHubRepo {
repo_slug: format!(
Ok(Self::GitHub {
slug: format!(
"{user}/{repo}",
user = caps.get(1).unwrap().as_str(),
repo = caps.get(2).unwrap().as_str()
),
})
} else if let Some(caps) = GITLAB_REMOTE_URL.captures(s) {
Ok(Self::GitLabRepo {
repo_slug: format!(
Ok(Self::GitLab {
slug: format!(
"{user}{groups}/{repo}",
user = caps.get(1).unwrap().as_str(),
groups = caps.get(2).map(|x| x.as_str()).unwrap_or_default(),
repo = caps.get(3).unwrap().as_str()
),
})
} else if let Some(caps) = SOURCEHUT_REMOTE_URL.captures(s) {
Ok(Self::SourceHut {
slug: format!(
"~{user}/{repo}",
user = caps.get(1).unwrap().as_str(),
repo = caps.get(2).unwrap().as_str()
),
})
} else {
Err("Not a GitHub or GitLab repo.".into())
Err("Not a GitHub, GitLab or SourceHut repo.".into())
}
}
}
Expand All @@ -108,17 +133,17 @@ mod tests {
assert!(parsed.is_ok());
assert_eq!(
parsed.unwrap(),
GitRemoteRepo::GitHubRepo {
repo_slug: "dandavison/delta".to_string()
GitRemoteRepo::GitHub {
slug: "dandavison/delta".to_string()
}
);
}
}

#[test]
fn test_format_github_commit_link() {
let repo = GitRemoteRepo::GitHubRepo {
repo_slug: "dandavison/delta".to_string(),
let repo = GitRemoteRepo::GitHub {
slug: "dandavison/delta".to_string(),
};
let commit_hash = "d3b07384d113edec49eaa6238ad5ff00";
assert_eq!(
Expand Down Expand Up @@ -153,22 +178,56 @@ mod tests {
assert!(parsed.is_ok());
assert_eq!(
parsed.unwrap(),
GitRemoteRepo::GitLabRepo {
repo_slug: expected.to_string()
GitRemoteRepo::GitLab {
slug: expected.to_string()
}
);
}
}

#[test]
fn test_format_gitlab_commit_link() {
let repo = GitRemoteRepo::GitLabRepo {
repo_slug: "proj/grp/repo".to_string(),
let repo = GitRemoteRepo::GitLab {
slug: "proj/grp/repo".to_string(),
};
let commit_hash = "d3b07384d113edec49eaa6238ad5ff00";
assert_eq!(
repo.format_commit_url(commit_hash),
format!("https://gitlab.com/proj/grp/repo/-/commit/{}", commit_hash)
)
}

#[test]
fn test_parse_sourcehut_urls() {
let urls = &[
"https://git.sr.ht/~someuser/somerepo",
"git@git.sr.ht:~someuser/somerepo",
"git.sr.ht:~someuser/somerepo",
];
for url in urls {
let parsed = GitRemoteRepo::from_str(url);
assert!(parsed.is_ok());
assert_eq!(
parsed.unwrap(),
GitRemoteRepo::SourceHut {
slug: "~someuser/somerepo".to_string()
}
);
}
}

#[test]
fn test_format_sourcehut_commit_link() {
let repo = GitRemoteRepo::SourceHut {
slug: "~someuser/somerepo".to_string(),
};
let commit_hash = "df41ac86f08a40e64c76062fd67e238522c14990";
assert_eq!(
repo.format_commit_url(commit_hash),
format!(
"https://git.sr.ht/~someuser/somerepo/commit/{}",
commit_hash
)
)
}
}