Skip to content
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "git-link"
version = "0.1.2"
version = "0.1.3"
edition = "2024"
description = "`git link` alias to fetch a link to a remote git repo"
license = "Unlicense"
Expand Down
57 changes: 52 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ struct Cli {

#[derive(Subcommand)]
enum Commands {
/// Link to a pull request for the current branch
/// Link to a pull request / merge request for the current branch
Pr,
/// Link to a merge request / pull request for the current branch
Mr,
}

fn run(cmd: &str, args: &[&str]) -> String {
Expand Down Expand Up @@ -54,10 +56,31 @@ pub fn normalize_remote(remote: &str) -> String {
panic!("Unrecognized remote URL format: {}", remote);
}

fn is_gitlab_repo(repo_url: &str) -> bool {
repo_url.contains("gitlab")
}

/// Create a Pull Request url, assuming the remote is GitHub, or a URL-compatible site.
pub fn pr_url(repo_url: &str, branch: &str) -> String {
format!("{}/pull/new/{}", repo_url, branch)
}

/// Create a Merge Request url, assuming the remote is GitLab, or a URL-compatible site.
pub fn mr_url(repo_url: &str, branch: &str) -> String {
format!(
"{}/-/merge_requests/new?merge_request[source_branch]={}",
repo_url, branch
)
}

fn link_for_command(repo_url: &str, branch: &str) -> String {
if is_gitlab_repo(repo_url) {
mr_url(repo_url, branch)
} else {
pr_url(repo_url, branch)
}
}

fn open_in_browser(url: &str) {
let opener = if cfg!(target_os = "macos") {
"open"
Expand All @@ -80,21 +103,21 @@ fn main() {
let repo_url = normalize_remote(&remote);

let final_url = match cli.command {
Some(Commands::Pr) => {
Some(Commands::Pr | Commands::Mr) => {
let branch = run("git", &["symbolic-ref", "--short", "HEAD"]);
if branch.is_empty() {
eprintln!("Not on a branch");
exit(1);
}
pr_url(&repo_url, &branch)
link_for_command(&repo_url, &branch)
}
None => repo_url,
};

// Always print
// Always print (whether or not opening in a browser).
println!("{}", final_url);

// Optionally open
// Optionally open in web browser.
if cli.open {
open_in_browser(&final_url);
}
Expand Down Expand Up @@ -145,4 +168,28 @@ mod tests {
fn invalid_remote_panics() {
normalize_remote("ssh://example.com/org/project.git");
}

#[test]
fn github_pr_url() {
let repo = "https://github.com/org/project";
let branch = "feature-x";
let expected = "https://github.com/org/project/pull/new/feature-x";
assert_eq!(link_for_command(repo, branch), expected);
}

#[test]
fn gitlab_mr_url() {
let repo = "https://gitlab.com/org/project";
let branch = "feature-x";
let expected = "https://gitlab.com/org/project/-/merge_requests/new?merge_request[source_branch]=feature-x";
assert_eq!(link_for_command(repo, branch), expected);
}

#[test]
fn self_hosted_gitlab_mr_url() {
let repo = "https://gitlab.example.com/org/project";
let branch = "dev";
let expected = "https://gitlab.example.com/org/project/-/merge_requests/new?merge_request[source_branch]=dev";
assert_eq!(link_for_command(repo, branch), expected);
}
}