| 1 |
#!/usr/bin/perl |
| 2 |
package IkiWiki::Plugin::gitpush; |
| 3 |
|
| 4 |
use warnings; |
| 5 |
use strict; |
| 6 |
use IkiWiki 3.00; |
| 7 |
|
| 8 |
sub import { |
| 9 |
hook(type => "getsetup", id => "gitpush", call => \&getsetup); |
| 10 |
hook(type => "refresh", id => "gitpush", call => \&refresh); |
| 11 |
hook(type => "savestate", id => "gitpush", call => \&savestate); |
| 12 |
} |
| 13 |
|
| 14 |
sub getsetup () { |
| 15 |
my $keyurl=length $config{cgiurl} ? IkiWiki::cgiurl(do => "sshkey") : ""; |
| 16 |
return |
| 17 |
plugin => { |
| 18 |
safe => 1, |
| 19 |
rebuild => 0, |
| 20 |
section => "core", |
| 21 |
}, |
| 22 |
git_push_to => { |
| 23 |
type => "string", |
| 24 |
example => [], |
| 25 |
description => "git repository urls that changes are pushed to", |
| 26 |
htmldescription => "git repository urls that changes are pushed to (using <a href=\"$keyurl\">this ssh key</a>)", |
| 27 |
safe => 1, |
| 28 |
rebuild => 0, |
| 29 |
}, |
| 30 |
} |
| 31 |
|
| 32 |
# Pushing is deferred to the end to avoid running at the same time |
| 33 |
# as site building. |
| 34 |
my $need_push; |
| 35 |
|
| 36 |
sub refresh { |
| 37 |
$need_push=1; |
| 38 |
} |
| 39 |
|
| 40 |
sub savestate { |
| 41 |
if ($need_push && $config{rcs} eq 'git' && |
| 42 |
ref($config{git_push_to}) eq 'ARRAY' && |
| 43 |
scalar @{$config{git_push_to}}) { |
| 44 |
# daemonise |
| 45 |
defined(my $pid = fork) or error("Can't fork: $!"); |
| 46 |
return if $pid; |
| 47 |
chdir '/'; |
| 48 |
open STDIN, '/dev/null'; |
| 49 |
open STDOUT, '>/dev/null'; |
| 50 |
POSIX::setsid() or error("Can't start a new session: $!"); |
| 51 |
open STDERR, '>&STDOUT' or error("Can't dup stdout: $!"); |
| 52 |
|
| 53 |
# Don't need to keep a lock on the wiki as a daemon. |
| 54 |
IkiWiki::unlockwiki(); |
| 55 |
|
| 56 |
# Low priority job. |
| 57 |
eval q{use POSIX; POSIX::nice(10)}; |
| 58 |
|
| 59 |
# Ensure we have a ssh key generated to use. |
| 60 |
IkiWiki::Plugin::ikiwikihosting::get_ssh_public_keys(); |
| 61 |
# Avoid ssh host key checking prompts. |
| 62 |
$ENV{GIT_SSH}="iki-ssh-unsafe"; |
| 63 |
|
| 64 |
chdir "$ENV{HOME}/source.git/" || die $!; |
| 65 |
|
| 66 |
foreach my $url (@{$config{git_push_to}}) { |
| 67 |
next unless length $url; |
| 68 |
|
| 69 |
system('git', 'push', '--quiet', '--mirror', $url); |
| 70 |
} |
| 71 |
|
| 72 |
exit 0; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
1 |