Skip to content

Instantly share code, notes, and snippets.

@ar-tama
Created October 21, 2013 11:29
Show Gist options
  • Save ar-tama/7082382 to your computer and use it in GitHub Desktop.
Save ar-tama/7082382 to your computer and use it in GitHub Desktop.
hubのミニマム版。GitHubだけではなく、bitbucket, GH:eにも対応。 リポジトリ(fetch)を開く、p-r画面を開く、ブランチを比較する、p-rをみる、issueをみることができます。
#! /usr/local/bin/perl
use strict;
use warnings;
use Getopt::Compact::WithCmd;
my $go = Getopt::Compact::WithCmd->new(
command_struct => {
open => {
desc => 'open repo url.',
},
pr => {
desc => 'open send pr page. see "bit help pr"',
other_usage => 'bit pr [head branch(default:current)] [base branch(default:master)]',
},
compare => {
desc => 'compare branches. see "bit help compare"',
other_usage => 'bit compare [head branch(default:current)] [base branch(default:master)]',
},
pulls => {
desc => 'open pull requests. see "bit help pulls"',
other_usage => 'bit pulls [pulls id]',
},
issues => {
desc => 'open pull requests. see "bit help issues"',
other_usage => 'bit issues [issue id]',
},
},
);
my $opts = $go->opts;
my $cmd = $go->command || $go->show_usage;
__PACKAGE__->can($cmd)->($opts);
exit;
sub open {
my $url = _get_url();
unless ($url) {
print "Couldn't get valid url.\n";
return;
}
print "$url\n";
`open $url`;
}
sub pr {
my $url = _get_url();
unless ($url) {
print "Couldn't get valid url.\n";
return;
}
my $branch = `git symbolic-ref --short HEAD`;
unless ($branch) {
print "Couldn't get valid branch name.\n";
return;
}
my $pr_url = join '/', $url, 'pull/new', $branch;
print "$pr_url\n";
`open $pr_url`;
}
sub compare {
my ($head_branch, $base_branch) = @ARGV;
if (!$head_branch || $head_branch eq 'current') {
$head_branch = `git symbolic-ref --short HEAD`;
}
$head_branch =~ s/\//;/;
$base_branch ||= 'master';
$base_branch =~ s/\//;/;
my $url = _get_url();
unless ($url) {
print "Couldn't get valid url.\n";
return;
}
my $compare_url = "$url/compare/$base_branch...$head_branch";
print $compare_url;
`open "$compare_url"`;
}
sub pulls {
my $num = shift @ARGV;
my $url = _get_url();
unless ($url) {
print "Couldn't get valid url.\n";
return;
}
my $suffix = ($num) ? "pulls/$num" : 'pulls';
my $pull_url = join '/', $url, $suffix;
print "$pull_url\n";
`open "$pull_url"`;
}
sub issues {
my $num = shift @ARGV;
my $url = _get_url();
unless ($url) {
print "Couldn't get valid url.\n";
return;
}
my $suffix = ($num) ? "issues/$num" : 'issues';
my $iss_url = join '/', $url, $suffix;
print "$iss_url\n";
`open "$iss_url"`;
}
sub _get_url {
my @remotes = `git remote -v`;
unless (@remotes) {
print "* You must exec it at git controlled directory.\n";
return;
}
my $source = shift @remotes;
my $url = '';
if ($source =~ qr/(http.+)\s.+/) {
$url = $1;
$url =~ s/\w+\@|\.git$//g;
}
elsif ($source =~ qr/(git\@.+)\s.+/) {
$url = $1;
$url =~ s|:|/|;
$url =~ s|git\@|http://|;
$url =~ s|.git$||;
}
return $url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment