Debian Codegraph @

IkiWiki/Plugin/branchable.pm

raw
1 #!/usr/bin/perl
2 # Allows a site to be branched.
3 package IkiWiki::Plugin::branchable;
4
5 use warnings;
6 use strict;
7 use IkiWiki 3.00;
8
9 sub import {
10 hook(type => "getsetup", id => "branchable", call => \&getsetup);
11 hook(type => "checkconfig", id => "branchable", call => \&checkconfig,
12 first => 1);
13 hook(type => "sessioncgi", id => "branchable", call => \&sessioncgi);
14 hook(type => "pageactions", id => "branchable", call => \&pageactions,
15 # Want it to be the last thing on the action bar.
16 last => 1);
17 hook(type => "genwrapper", id => "branchable", call => \&genwrapper,
18 last => 1);
19 hook(type => "disable", id => "branchable", call => \&disable);
20 }
21
22 sub getsetup () {
23 return
24 plugin => {
25 safe => 0,
26 rebuild => undef,
27 section => "core",
28 },
29 branchable => {
30 type => "boolean",
31 example => 1,
32 description => "Allow anyone to branch, check out, and copy this site?",
33 safe => 1,
34 rebuild => 1, # disables diffurl and historyurl
35 },
36 anonpush => {
37 type => "boolean",
38 example => 0,
39 description => "Allow anyone to git push verified changes to this site?",
40 safe => 1,
41 rebuild => 0,
42 },
43 branchable_action => {
44 type => "boolean",
45 example => 1,
46 description => "Display \"Branchable\" link on action bar?",
47 safe => 1,
48 rebuild => 1, # update action bar
49 },
50 }
51
52 sub checkconfig {
53 $config{branchable_action}=1 unless defined $config{branchable_action};
54
55 # Automatically configure repositories for repolist plugin.
56 # (This hook needs to run first to do so.)
57 $config{repositories}=[grep defined, (gitsshurl(), gitanonurl())];
58
59 # Sites that are not branchable do not have gitweb configured, so
60 # automatically disable the historyurl and diffurl.
61 # TODO: store old values and re-enable if branchable is enabled
62 # Also, non-branchability disabled anonpush.
63 if (! $config{branchable}) {
64 delete $config{historyurl};
65 delete $config{diffurl};
66 $config{anonpush}=0;
67 }
68
69 # Sites with anonpush enabled have a git_test_receive_wrapper.
70 if ($config{anonpush}) {
71 my $repo=find_git_repository();
72 if (defined $repo) {
73 $config{git_test_receive_wrapper}="$repo/hooks/pre-receive.wrapper";
74 }
75 else {
76 $config{anonpush}=0;
77 delete $config{git_test_receive_wrapper};
78 }
79 }
80 else {
81 delete $config{git_test_receive_wrapper};
82 }
83 }
84
85 my $inbranchable;
86 my $link;
87 sub pageactions (@) {
88 return $link if defined $link;
89 return if $inbranchable;
90 return unless $config{branchable_action};
91 return $link='<a href="'.IkiWiki::cgiurl("do" => "branchable").'">'.
92 "Branchable".'</a>';
93 }
94
95 sub sessioncgi ($$) {
96 my $cgi=shift;
97 my $session=shift;
98
99 return unless defined $cgi->param("do");
100
101 if ($cgi->param("do") eq "branchable") {
102 showbranchable($cgi, $session);
103 }
104 }
105
106 sub showbranchable ($$) {
107 my $cgi=shift;
108 my $session=shift;
109
110 $inbranchable=1;
111
112 eval q{use IkiWiki::Hosting};
113 error $@ if $@;
114 IkiWiki::Hosting::readconfig();
115
116 my $template=IkiWiki::Hosting::ikisite_template("branchable.tmpl");
117 $template->param(wikiname => $config{wikiname});
118 $template->param(url => $config{url});
119 $template->param(cgiurl => $config{cgiurl});
120 $template->param(setupurl => IkiWiki::cgiurl(do => "setup").
121 # jump right to the branchable plugin setup
122 "#setup_core_plugin_branchable");
123 $template->param(gitsshurl => gitsshurl());
124 $template->param(gitanonurl => gitanonurl());
125 $template->param(hostname => $config{hostname});
126 $template->param(branchable => $config{branchable});
127 $template->param(anonpush => $config{anonpush});
128 $template->param(controlsitecgiurl => $config{controlsitecgiurl});
129 IkiWiki::printheader($session);
130 print IkiWiki::cgitemplate($cgi, 'Branchable', $template->output);
131 exit 0;
132 }
133
134 sub genwrapper {
135 # This hook is called whenever wrappers are being built,
136 # and that happens when configuration has changed.
137 # (This hook is set to run last, in order to run after
138 # the creation of the git_test_receive_wrapper.)
139 setbranchable();
140
141 # C code to inject into wrapper.
142 return "";
143 }
144
145 sub disable {
146 # When this plugin is disabled, clean up any previously
147 # configured branchability.
148 $config{branchable}=0;
149 $config{anonpush}=0;
150 setbranchable();
151 }
152
153 sub setbranchable {
154 # When the branchable setting is changed, we need to
155 # touch or remove the git-daemon-export-ok flag file,
156 # and the symlink to gitweb.conf, and tweak the permissions
157 # of the git repo.
158 my $repo=find_git_repository();
159 if (defined $repo) {
160 my $flagfile="$repo/git-daemon-export-ok";
161 my $gitweb_conf_file=$config{srcdir}."/.ikiwiki/gitweb.conf";
162 if ($config{branchable}) {
163 open(FLAG, ">", $flagfile);
164 close FLAG;
165 unlink($gitweb_conf_file); # force refresh symlink
166 symlink("$gitweb_conf_file.real", $gitweb_conf_file);
167 chmod(02775, $repo) || error "chmod $repo: $!";
168 }
169 else {
170 unlink($flagfile);
171 unlink($gitweb_conf_file);
172 chmod(0700, $repo) || error "chmod $repo: $!";
173 }
174 }
175
176 if (defined $repo && $config{anonpush}) {
177 # Ikiwiki will be running as the site's username,
178 my $username=(getpwuid($<))[0];
179 # Write the pre-receive hook, which in turn runs the
180 # wrapper, which runs ikiwiki.
181 # This extra shell script is not suid, so when git-daemon
182 # is receiving a push and runs it, it's run as the same
183 # user git-daemon uses. This allows it to fix the ACL
184 # temp directory so that the ikisite user can access it.
185 my $hook = "$repo/hooks/pre-receive";
186 if (-e "$hook.wrapper") {
187 open (my $fh, ">", $hook) || die $!;
188 print $fh "#!/bin/sh\n";
189 print $fh "set -e\n";
190 print $fh "if [ -n \"\$GIT_QUARANTINE_PATH\" ] && [ -d \"\$GIT_QUARANTINE_PATH\" ]; then\n";
191 print $fh "\tsetfacl -m 'u:$username:rx' \"\$GIT_QUARANTINE_PATH\"\n";
192 print $fh "fi\n";
193 print $fh "exec $hook.wrapper\n";
194 close $fh;
195 chmod(0755, $hook) || error "chmod $hook: $!";
196 }
197
198 # Allow git daemon to push.
199 system("cd $repo && git config daemon.receivepack true");
200 }
201 else {
202 system("cd $repo && git config daemon.receivepack false");
203 # Delete unnecesary hook for speed.
204 if (defined $repo) {
205 unlink("$repo/hooks/pre-receive");
206 unlink("$repo/hooks/pre-receive.wrapper");
207 }
208 }
209 }
210
211 # Parses git_wrapper to find out where the git repository is.
212 sub find_git_repository {
213 if ($config{rcs} eq 'git' &&
214 $config{git_wrapper}=~m!^(.*)/hooks/post-update$!) {
215 return $1;
216 }
217 else {
218 return undef;
219 }
220 }
221
222 sub gitsshurl {
223 # Ikiwiki will be running as the site's username,
224 my $username=(getpwuid($<))[0];
225 if (defined $username && length $username &&
226 defined $config{hostname} && length $config{hostname}) {
227 return "ssh://$username\@$config{hostname}/";
228 }
229 else {
230 return undef;
231 }
232 }
233
234 sub gitanonurl {
235 if ($config{branchable} &&
236 defined $config{hostname} && length $config{hostname}) {
237 return "git://$config{hostname}/";
238 }
239 else {
240 return undef;
241 }
242 }
243
244 1