GitHubBackup module for backup github project
authorKen-ichi Mito <mitty@mitty.jp>
Sat, 17 Aug 2013 18:21:49 +0000 (03:21 +0900)
committerKen-ichi Mito <mitty@mitty.jp>
Sat, 17 Aug 2013 18:21:49 +0000 (03:21 +0900)
 * work in progress
 * done with GitHubBackup::Repository->clone_git()

Dev/github/GitHubBackup.pm

index c0427e6..b153d9b 100644 (file)
@@ -21,6 +21,7 @@ sub json_api {
     return $json->decode($res->content);
 }
 
+
 package GitHubBackup;
 use base qw(Class::Accessor::Fast);
 
@@ -41,18 +42,24 @@ sub new {
     my $class = shift;
     
     my $args = (ref $_[0] eq 'HASH') ? $_[0] : {@_};
+    if (! $args->{directory}) {
+        $args->{directory} = ".";
+    }
+    
     return $class->SUPER::new($args);
 }
 
 sub repos {
     my $self = shift;
+    return $self->{repos} if ($self->{repos});
     
     my $account = $self->account or croak "account is not set";
     if (my $repository = $self->repository) {
         $self->{repos} = [
-            GitHubBackup::Repository->new(
-                {full_name => "$account/$repository"}
-            )
+            GitHubBackup::Repository->new({
+                __super   => $self,
+                full_name => "$account/$repository",
+            })
         ];
         
         return $self->{repos};
@@ -74,6 +81,7 @@ sub repos {
     foreach my $repos (@repos) {
         push @{$self->{repos}},
             GitHubBackup::Repository->new({
+                __super   => $self,
                 full_name => $repos->{full_name},
                 clone_url => $repos->{clone_url},
             })
@@ -91,6 +99,48 @@ use strict;
 use warnings;
 use utf8;
 use Carp qw(croak);
+use Git::Repository;
+use File::chdir;
+
+sub new {
+    my $class = shift;
+    my $args  = shift;
+    
+    if (! exists $args->{clone_url}) {
+        my $result = utils::json_api('/repos/' . $args->{full_name});
+        $args->{clone_url} = $result->{clone_url};
+    }
+    
+    return $class->SUPER::new($args);
+}
+
+sub clone_git {
+    my $self = shift;
+    
+    my $dir = $self->{__super}->directory .'/'. $self->{full_name};
+    if (-d "$dir") {
+        local $CWD = $dir;
+        print "fetch ", $self->{full_name}, "\n";
+        Git::Repository->run(fetch => '--all');
+        return $self;
+    }
+    
+    print "clone ", $self->{full_name}, "\n";
+    Git::Repository->run(clone => '--mirror' => $self->{clone_url} => $dir);
+    return $self;
+}
+
+sub set_forks {
+    my $self = shift;
+}
+
+sub clone_wiki {
+    my $self = shift;
+}
+
+sub save_issues {
+    my $self = shift;
+}