[Bps-public-commit] git-sync branch, master, updated. 47395fac7d2dcc8ea40ca909a0b2ef4678efa6f0

Alex M Vandiver alexmv at bestpractical.com
Wed Mar 3 16:22:41 EST 2010


The branch, master has been updated
       via  47395fac7d2dcc8ea40ca909a0b2ef4678efa6f0 (commit)
       via  3216d161321db9235a786ed8ea3402176d4ee6db (commit)
       via  e85d7486002e973d8aff0cb10bbacceed9dc3b5e (commit)
       via  9aca9c981ad9cc3de2ab57a4dca50b8d35857143 (commit)
       via  27b52fe9d93062be7fcc1386e9100491adf77742 (commit)
       via  6ab41821a528659962ed0cbcbdf3271fbf6e64cf (commit)
      from  5ba90333654c4b9242f91f557528a5e6ecae1de8 (commit)

Summary of changes:
 Makefile.PL |    4 +-
 git-sync    |  126 ++++++++++++++++++++++++++++++++++++++++++++++-------------
 2 files changed, 100 insertions(+), 30 deletions(-)

- Log -----------------------------------------------------------------
commit 6ab41821a528659962ed0cbcbdf3271fbf6e64cf
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Wed Mar 3 15:56:32 2010 -0500

    Add a $VERSION

diff --git a/git-sync b/git-sync
index d472c9f..fbb9dcd 100755
--- a/git-sync
+++ b/git-sync
@@ -156,6 +156,8 @@ use Pod::Usage;
 use LWP::Simple qw();
 use File::Path qw();
 
+our $VERSION = 1.0;
+
 sub colored { (-t STDOUT) ? Term::ANSIColor::colored(@_) : $_[0] }
 
 $SIG{INT} = sub {

commit 27b52fe9d93062be7fcc1386e9100491adf77742
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Wed Mar 3 15:57:23 2010 -0500

    Factor out requests to github API

diff --git a/git-sync b/git-sync
index fbb9dcd..fc821cb 100755
--- a/git-sync
+++ b/git-sync
@@ -44,6 +44,11 @@ writable clone via the SSH is attempted; if this fails, C<git-sync>
 falls back to the read-only C<git> protocol.  If a clone already
 exists, it is updated as above.
 
+The configuration can provide a login name (and optionally,
+authorization token) that will be used to access Github, and (if
+possible) also to determine if cloning via SSH (rather than git://) is
+possible.
+
 =item Remote
 
 All of the repositories in a specific directory (or set of
@@ -83,6 +88,9 @@ users) are cloned and updated:
             # cloned and updated under this directory
             into = /home/alexmv/github
 
+The C<login> and C<token> keys are used to provide github
+authentication, though this is not required.
+
 The presence of C<host> and C<path> keys configures a remote directory
 to clone from.  Multiple values for C<path> can be given; alternately,
 if only one path is needed, C<host> can be specified as
@@ -153,7 +161,7 @@ use warnings;
 use Term::ANSIColor qw//;
 use Getopt::Long;
 use Pod::Usage;
-use LWP::Simple qw();
+use LWP::UserAgent qw();
 use File::Path qw();
 
 our $VERSION = 1.0;
@@ -246,15 +254,9 @@ sub sync_all_github {
 
     my @accounts = ref $config{github} ? @{$config{github}} : ($config{github});
     for my $acct (@accounts) {
-        my $content = LWP::Simple::get("http://github.com/api/v1/json/$acct");
-        print colored("  GET of http://github.com/api/v1/json/$acct failed!\n", "bold red") and next
-            unless defined $content;
-
-        my $data = eval {$decoder->jsonToObj($content)};
-        print colored("  Parsing of GitHub JSON response failed! $@\n", "bold red")."\n$content\n" and next
-            unless defined $data;
+        my $data = github("repos/show/$acct", $config{login}, $config{token}) or next;
 
-        for my $repo (@{$data->{user}{repositories} || []}) {
+        for my $repo (@{$data->{repositories} || []}) {
             my $reponame = $repo->{name};
             my $root = "$config{into}/$reponame";
 
@@ -532,5 +534,31 @@ our $JSON_DECODER = undef;
 sub get_json_decoder {
     $JSON_DECODER ||= eval { require JSON::Any; JSON::Any->import; JSON::Any->new };
     return $JSON_DECODER;
+}
+
+our $UA = undef;
+sub github {
+    my ($url, $login, $token) = @_;
+    $UA ||= LWP::UserAgent->new(
+        agent => "git-sync/$VERSION",
+        env_proxy => 1,
+        keep_alive => 1,
+    );
+
+    $url = "https://github.com/api/v2/json/$url?";
+    $url .= "login=$login&" if $login;
+    $url .= "token=$token" if $token;
+
+    my $response = $UA->get($url);
+    print colored("  GET of $url failed!\n", "bold red")
+        . colored("  ".$response->status_line . "\n", "bold red") and return
+        unless $response->is_success;
+
+    my $decoder = get_json_decoder();
+    my $data = eval {$decoder->jsonToObj($response->content)};
+    print colored("  Parsing of GitHub JSON response failed! $@\n", "bold red")
+        ."\n".$response->content."\n" and return
+        unless defined $data;
 
+    return $data;
 }

commit 9aca9c981ad9cc3de2ab57a4dca50b8d35857143
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Wed Mar 3 15:58:53 2010 -0500

    Use github api to know if user has git at github.com: access

diff --git a/git-sync b/git-sync
index fc821cb..3c8a55e 100755
--- a/git-sync
+++ b/git-sync
@@ -265,9 +265,16 @@ sub sync_all_github {
             if (-e $root) {
                 update($root, 1);
             } else {
-                my $auth = clone($root => "git\@github.com:$acct/$reponame.git" => $config{email});
+                my $auth = 1;
+                if ($config{login}) {
+                    my $data = github("repos/show/$acct/$reponame/collaborators");
+                    $auth = 0 if $data and not grep {$_ eq $config{login}} @{$data->{collaborators}};
+                }
+                if ($auth) {
+                    $auth = clone($root => "git\@github.com:$acct/$reponame.git" => $config{email});
+                    printf "  %-40s ", "...trying again, anonymously" unless $auth;
+                }
                 unless ($auth) {
-                    printf "  %-40s ", "...trying again, anonymously";
                     clone($root => "git://github.com/$acct/$reponame.git" => $config{email});
                 }
             }

commit e85d7486002e973d8aff0cb10bbacceed9dc3b5e
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Wed Mar 3 15:59:20 2010 -0500

    Rework and fix fetching of github network

diff --git a/git-sync b/git-sync
index 3c8a55e..903ad91 100755
--- a/git-sync
+++ b/git-sync
@@ -49,6 +49,9 @@ authorization token) that will be used to access Github, and (if
 possible) also to determine if cloning via SSH (rather than git://) is
 possible.
 
+It can also be configured to add, and keep up-to-update, all
+repositories in Github's "network" as remotes.
+
 =item Remote
 
 All of the repositories in a specific directory (or set of
@@ -89,7 +92,9 @@ users) are cloned and updated:
             into = /home/alexmv/github
 
 The C<login> and C<token> keys are used to provide github
-authentication, though this is not required.
+authentication, though this is not required.  The C<network> key, if
+set, causes C<git-sync> to set up (and fetch) the github network as
+remotes for each repository, additionally.
 
 The presence of C<host> and C<path> keys configures a remote directory
 to clone from.  Multiple values for C<path> can be given; alternately,
@@ -263,6 +268,12 @@ sub sync_all_github {
             printf "  %-40s ", $reponame;
             next if already($root => 1);
             if (-e $root) {
+                add_github_remotes(
+                    %config,
+                    account => $acct,
+                    repository => $reponame,
+                    fetch => 0,
+                ) if $config{network};
                 update($root, 1);
             } else {
                 my $auth = 1;
@@ -277,28 +288,42 @@ sub sync_all_github {
                 unless ($auth) {
                     clone($root => "git://github.com/$acct/$reponame.git" => $config{email});
                 }
+                add_github_remotes(
+                    %config,
+                    account => $acct,
+                    repository => $reponame,
+                    fetch => 1,
+                ) if $config{network};
             }
-            #    my @clones = get_github_clone_list($acct, $reponame);
-            #add_new_github_remotes(@clones); 
-
         }
     }
 }
 
+sub add_github_remotes {
+    my %config = @_;
 
-sub get_github_clone_list {
-    my $user = shift;
-    my $repo = shift;
-    my @owners;
-    my $decoder = get_json_decoder();
-    my $content = LWP::Simple::get("http://github.com/api/v2/json/repos/show/$user/$repo/network");
-    my $data    = eval { $decoder->jsonToObj($content) };
-    print colored( "  Parsing of GitHub JSON response failed! $@\n", "bold red" ) . "\n$content\n" and next
-        unless defined $data;
-    foreach my $record ( @{ $data->{network} } ) {
-        push @owners, $record->{owner} unless $record->{owner} eq $user;
+    my $data = github("repos/show/$config{account}/$config{repository}/network", $config{login}, $config{token}) or return;
+
+    my %existing;
+    my $repo = Git->repository(Directory => "$config{into}/$config{repository}");
+    $existing{$_}++ for map {chomp; $_} $repo->command("remote", "show");
+
+    my @network = sort grep {$_ ne $config{account} and not $existing{$_}} map {$_->{owner}} @{$data->{network}};
+    return unless @network;
+    printf colored("    %-40s ", "dark"), "Adding github network remotes";
+    for my $remote (grep {not $existing{$_}} @network) {
+        my $uri = "git://github.com/$remote/$config{repository}.git";
+        $uri = "git\@github.com:$config{login}/$config{repository}.git"
+            if $config{login} and $config{login} eq $remote;
+        $repo->command(
+            "remote", "add",
+            $remote => $uri,
+        );
     }
-    return @owners;
+    my $retval = eval { $repo->command( [qw/remote update --prune/], STDERR => 0 ); }
+        if $config{fetch};
+
+    print colored("{ @network }\n", "dark green");
 }
 
 sub sync_all_remote {

commit 3216d161321db9235a786ed8ea3402176d4ee6db
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Wed Mar 3 16:14:12 2010 -0500

    Fix documentation typo

diff --git a/git-sync b/git-sync
index 903ad91..8862b5b 100755
--- a/git-sync
+++ b/git-sync
@@ -108,7 +108,7 @@ C<hostname:/path>.
             # under this directory
             into = /home/alexmv/example.com
 
-The If the path includes a '*', it is treated as a list of remote
+If the path includes a '*', it is treated as a list of remote
 repository paths.  Otherwise, all subdirectories of the path which are
 git repositories are acted on.
 

commit 47395fac7d2dcc8ea40ca909a0b2ef4678efa6f0
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Wed Mar 3 16:22:33 2010 -0500

    Minor packaging fixups

diff --git a/Makefile.PL b/Makefile.PL
index 6e9be6b..b3624ff 100644
--- a/Makefile.PL
+++ b/Makefile.PL
@@ -1,8 +1,6 @@
 use inc::Module::Install;
 name     'Git-Sync';
-#all_from 'git-sync';
-#author   q{};
-#license  '';
+all_from 'git-sync';
 requires( qw(
         Git 0
         Term::ANSIColor 0
diff --git a/git-sync b/git-sync
index 8862b5b..327d39b 100755
--- a/git-sync
+++ b/git-sync
@@ -158,8 +158,18 @@ Other parameters passed on the command line are taken to be categories
 to be synchronized; if no other arguments are given, synchronzies all
 categories.
 
+=head1 AUTHOR
+
+Alex Vandiver C<< <alexmv at bestpractical.com> >>
+
+=head1 COPYRIGHT
+
+This program is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself.
+
 =cut
 
+use 5.008;
 use Git;
 use strict;
 use warnings;

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list