1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
#!/usr/bin/env perl
use strict;
use warnings;
use Path::Tiny qw( path );
use Pithub::GitData ();
my $git = Pithub::GitData->new(
repo => 'Pithub',
token => $ENV{GITHUB_TOKEN},
user => 'plu',
);
my $content = path(__FILE__)->slurp;
# the encoding can also be 'base64', if necessary
my $blob = $git->blobs->create(
data => {
content => $content,
encoding => 'utf-8',
}
);
die 'Could not create blob' unless $blob->success;
# we need the current master reference, actually just its SHA
my $master = $git->references->get( ref => 'heads/master' );
die 'Could not get the heads/master reference' unless $master->success;
# and we need the full commit of this SHA. Later we will
# extract the tree SHA this commit belongs to.
my $base_commit
= $git->commits->get( sha => $master->content->{object}{sha} );
die 'Could not get the base commit' unless $base_commit->success;
# create a new tree, based on the old one, that adds the new blob
my $tree = $git->trees->create(
data => {
base_tree => $base_commit->content->{tree}{sha},
tree => [
{
path => 'examples/gitdata_commit.pl',
mode => '100755',
type => 'blob',
sha => $blob->content->{sha},
}
],
}
);
die 'Could not create the new tree' unless $tree->success;
# create a new commit based on the new tree and
# having the current master as a parent
my $commit = $git->commits->create(
data => {
message => 'Add examples/gitdata_commit.pl.',
parents => [ $master->content->{object}{sha} ],
tree => $tree->content->{sha},
}
);
die 'Could not create the commit' unless $commit->success;
# finally point the master branch to the new commit
my $reference = $git->references->update(
ref => 'heads/master',
data => { sha => $commit->content->{sha} }
);
die 'Could not update the heads/master reference' unless $reference->success;
print "Done.\n";
|