From: Ken-ichi Mito Date: Sun, 18 Aug 2013 12:56:40 +0000 (+0900) Subject: add forks as remotes/'full_name' X-Git-Url: http://lab.mitty.jp/git/?a=commitdiff_plain;h=682d2d96820ebfdfb0bd63e6f3cbd57a2134e50d;p=lab.git add forks as remotes/'full_name' --- diff --git a/Dev/github/GitHubBackup.pm b/Dev/github/GitHubBackup.pm index 130c6f6..8dfea63 100644 --- a/Dev/github/GitHubBackup.pm +++ b/Dev/github/GitHubBackup.pm @@ -164,7 +164,7 @@ sub directory { sub clone_git { my $self = shift; - my $dir = $self->directory; + my $dir = $self->directory . '.git'; if (-d "$dir") { local $CWD = $dir; print "fetch ", $dir, "\n"; @@ -178,8 +178,47 @@ sub clone_git { return $self; } +sub get_forks { + my $self = shift; + return $self->{forks} if ($self->{forks}); + + my $page = 1; + while (1) { + my $result = utils::json_api("/repos/" . $self->{full_name} . "/forks?per_page=100&page=$page"); + if (ref($result) eq 'ARRAY' && scalar @$result > 0) { + push @{$self->{forks}}, @$result; + $page++; + + next; + } + last; + } + + return $self; +} + sub set_forks { my $self = shift; + + $self->get_forks; + + my $dir = $self->directory . '.git'; + local $CWD = $dir; + + my $remotes = Git::Repository->run(branch => '--remotes'); + foreach my $fork (@{$self->{forks}}) { + if ($remotes =~ /$fork->{full_name}/) { + print "skip ", $fork->{full_name}, "\n"; + next; + } + print "add ", $fork->{full_name}, "\n"; + Git::Repository->run(remote => add => $fork->{full_name} => $fork->{clone_url}); + } + + print "fetch ", $dir, "\n"; + Git::Repository->run(fetch => '--all'); + + return $self; } sub clone_wiki {