Skip to content

Instantly share code, notes, and snippets.

@softmoth
Last active August 29, 2015 14:02
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 softmoth/1fe9843610f0ea6062d1 to your computer and use it in GitHub Desktop.
Save softmoth/1fe9843610f0ea6062d1 to your computer and use it in GitHub Desktop.
#! /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