Skip to content

Instantly share code, notes, and snippets.

@typester
Created January 27, 2009 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save typester/53293 to your computer and use it in GitHub Desktop.
Save typester/53293 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use Cwd;
use File::chdir;
use Path::Class qw/dir/;
use WWW::Mechanize;
use Getopt::Long;
GetOptions(
\my %opt,
qw/private anonymous/
);
my $cwd = dir($ARGV[0] || getcwd);
my $github = do {
local $CWD = $cwd;
{
login => qx{ git config github.user },
token => qx{ git config github.token },
};
};
die "required git config github.user & github.token"
unless $github->{login} && $github->{token};
chomp($github->{login}, $github->{token});
my $mech = WWW::Mechanize->new( stack_depth => 1 );
$mech->get('http://gist.github.com');
die $mech->status unless $mech->success;
my $i = 0;
my $data = {};
$cwd->recurse( callback => sub {
my $f = shift;
return unless -f $f;
(my $name = $f) =~ s!^$cwd/!!;
return if $name =~ m!^\.git($|/)!;
my $index = 'gistfile' . ++$i;
$data->{"file_name[${index}]"} = $name;
$data->{"file_contents[${index}]"} = $f->slurp;
});
$mech->submit_form(
fields => {
%$data,
$opt{anonymous} ? () : (%$github),
},
form_number => 2,
$opt{private} ? (button => 'action_button') : (),
);
die $mech->status unless $mech->success;
my ($repo) = $mech->content =~ m!(git\@gist\.github\.com:.+?.git)!;
die "can't find repository path" unless $repo;
{
local $CWD = $cwd;
qx{ git remote add origin $repo };
qx{ git pull origin master };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment