Skip to content

Instantly share code, notes, and snippets.

@hellojukay
Last active December 8, 2021 02:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hellojukay/159a64c376f8c518eb1ed9b9d6ac3248 to your computer and use it in GitHub Desktop.
Save hellojukay/159a64c376f8c518eb1ed9b9d6ac3248 to your computer and use it in GitHub Desktop.
通过命令行在浏览器中打开当前仓库
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long;
GetOptions("p|pipeline" => \(my $pipeline),
"h|help" => \(my $help));
sub usage($) {
my $code = shift;
my $msg = <<EOF;
git open [options] open git repo with default browser .
-h
--help Print help message
-p
--pipeline Open repo pipelines
EOF
print($msg);
exit($code);
}
if($help) {
usage(0);
}
# 是否是 github 的仓库
sub is_github_repo ($) {
my $url = shift;
return $url =~ /github\.com/ ;
}
# 获取 pipeline 页面的地址
sub pipeline_url (@) {
my $url = shift;
if(shift) {
# github
$url .= "/actions";
} else {
# gitlab
$url .= "/-/pipelines";
}
return $url;
}
# 用默认浏览器打开页面
sub _open ($) {
my $url = shift;
`/usr/bin/xdg-open $url > /dev/null 2> /dev/null`;
}
# 获取 origin 地址,转化为 web 页面的地址
my $remote = `git remote -v | grep fetch`;
if($remote =~ m/.*?\s+(.*?)\s+.*/) {
$remote ="$1";
}
$remote = $remote =~ s/\.git$//r;
$remote = $remote =~ s/^git@/https:\/\//r;
$remote = $remote =~ s/(:)([^\/])/\/$2/r;
# 打开 pipeline 地址
if($pipeline) {
_open(pipeline_url($remote,is_github_repo($remote)));
exit(0);
}
_open($remote);
exit(0);
@hellojukay
Copy link
Author

hellojukay commented Dec 7, 2021

hellojukay@local bashrc (master) $ git-open --help
git open [options]  open git repo with default browser .
    -h
    --help        Print help message

    -p
    --pipeline    Open repo pipelines

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment