Last active
August 29, 2015 14:02
-
-
Save softmoth/1fe9843610f0ea6062d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env perl6 | |
constant $me = 'YOUR-GITHUB-USER'; | |
constant $token = 'YOUR-GITHUB-OAUTH-TOKEN'; | |
sub MAIN(Bool :n(:$dry-run) = False) { | |
my %remotes; | |
for qqx<< git remote -v >>.lines { | |
/ | |
^ (<-[\t]>+) | |
\t | |
(.*) | |
' (' (fetch|push) ')' | |
$ | |
/; | |
unless $/ { | |
warn "Bad git remote '$_'"; | |
next; | |
} | |
my ($name, $uri, $op) = @$/».Str; | |
%remotes{$name}{$op} = $uri; | |
} | |
if %remotes{$me} -> $r { | |
say $r<push> // $r<fetch>; | |
exit unless $dry-run; | |
} | |
my $from = %remotes<origin> ?? 'origin' !! %remotes.keys.grep({$_ ne $me})[0]; | |
die "No remote to fork from in ", %remotes.perl unless $from; | |
my $remote = %remotes{$from}<fetch> // %remotes{$from}<push>; | |
note "Fork from $remote"; | |
unless $remote ~~ m{ | |
^ | |
[git | https?] ['@' | '://'] | |
[<-[.]>+ \.]? 'github.com' | |
[':' | '/'] | |
(<-[/]>+) '/' (.+?) '.git'? | |
$ | |
} { | |
die "Not a github remote '$remote'"; | |
} | |
my ($user, $repo) = @$/; | |
my $api-uri = "https://api.github.com/repos/$user/$repo/forks"; | |
my @cmd; | |
@cmd = << | |
curl | |
-H "Authorization: token $token" | |
-X POST | |
$api-uri | |
>>; | |
say [@cmd].perl; | |
run @cmd unless $dry-run; | |
@cmd = <<git remote add $me "git@github.com:$me/$repo.git">>; | |
say [@cmd].perl; | |
run @cmd unless $dry-run; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment