summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/tla.pm
blob: f5ad0cc96bd658810492880f9c91bf87c60bdc92 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::tla;
  3. use warnings;
  4. use strict;
  5. use IkiWiki;
  6. sub import {
  7. hook(type => "checkconfig", id => "tla", call => \&checkconfig);
  8. hook(type => "getsetup", id => "tla", call => \&getsetup);
  9. hook(type => "rcs", id => "rcs_update", call => \&rcs_update);
  10. hook(type => "rcs", id => "rcs_prepedit", call => \&rcs_prepedit);
  11. hook(type => "rcs", id => "rcs_commit", call => \&rcs_commit);
  12. hook(type => "rcs", id => "rcs_commit_staged", call => \&rcs_commit_staged);
  13. hook(type => "rcs", id => "rcs_add", call => \&rcs_add);
  14. hook(type => "rcs", id => "rcs_remove", call => \&rcs_remove);
  15. hook(type => "rcs", id => "rcs_rename", call => \&rcs_rename);
  16. hook(type => "rcs", id => "rcs_recentchanges", call => \&rcs_recentchanges);
  17. hook(type => "rcs", id => "rcs_diff", call => \&rcs_diff);
  18. hook(type => "rcs", id => "rcs_getctime", call => \&rcs_getctime);
  19. hook(type => "rcs", id => "rcs_getmtime", call => \&rcs_getmtime);
  20. }
  21. sub checkconfig () {
  22. if (defined $config{tla_wrapper} && length $config{tla_wrapper}) {
  23. push @{$config{wrappers}}, {
  24. wrapper => $config{tla_wrapper},
  25. wrappermode => (defined $config{tla_wrappermode} ? $config{tla_wrappermode} : "06755"),
  26. };
  27. }
  28. }
  29. sub getsetup () {
  30. return
  31. plugin => {
  32. safe => 0, # rcs plugin
  33. rebuild => undef,
  34. section => "rcs",
  35. },
  36. tla_wrapper => {
  37. type => "string",
  38. #example => "", # TODO example
  39. description => "tla post-commit hook to generate",
  40. safe => 0, # file
  41. rebuild => 0,
  42. },
  43. tla_wrappermode => {
  44. type => "string",
  45. example => '06755',
  46. description => "mode for tla_wrapper (can safely be made suid)",
  47. safe => 0,
  48. rebuild => 0,
  49. },
  50. historyurl => {
  51. type => "string",
  52. #example => "", # TODO example
  53. description => "url to show file history ([[file]] substituted)",
  54. safe => 1,
  55. rebuild => 1,
  56. },
  57. diffurl => {
  58. type => "string",
  59. #example => "", # TODO example
  60. description => "url to show a diff ([[file]] and [[rev]] substituted)",
  61. safe => 1,
  62. rebuild => 1,
  63. },
  64. }
  65. sub quiet_system (@) {
  66. # See Debian bug #385939.
  67. open (SAVEOUT, ">&STDOUT");
  68. close STDOUT;
  69. open (STDOUT, ">/dev/null");
  70. my $ret=system(@_);
  71. close STDOUT;
  72. open (STDOUT, ">&SAVEOUT");
  73. close SAVEOUT;
  74. return $ret;
  75. }
  76. sub rcs_update () {
  77. if (-d "$config{srcdir}/{arch}") {
  78. if (quiet_system("tla", "replay", "-d", $config{srcdir}) != 0) {
  79. warn("tla replay failed\n");
  80. }
  81. }
  82. }
  83. sub rcs_prepedit ($) {
  84. my $file=shift;
  85. if (-d "$config{srcdir}/{arch}") {
  86. # For Arch, return the tree-id of archive when
  87. # editing begins.
  88. my $rev=`tla tree-id $config{srcdir}`;
  89. return defined $rev ? $rev : "";
  90. }
  91. }
  92. sub rcs_commit ($$$;$$) {
  93. my $file=shift;
  94. my $message=shift;
  95. my $rcstoken=shift;
  96. my $user=shift;
  97. my $ipaddr=shift;
  98. if (defined $user) {
  99. $message="web commit by $user".(length $message ? ": $message" : "");
  100. }
  101. elsif (defined $ipaddr) {
  102. $message="web commit from $ipaddr".(length $message ? ": $message" : "");
  103. }
  104. if (-d "$config{srcdir}/{arch}") {
  105. # Check to see if the page has been changed by someone
  106. # else since rcs_prepedit was called.
  107. my ($oldrev)=$rcstoken=~/^([A-Za-z0-9@\/._-]+)$/; # untaint
  108. my $rev=`tla tree-id $config{srcdir}`;
  109. if (defined $rev && defined $oldrev && $rev ne $oldrev) {
  110. # Merge their changes into the file that we've
  111. # changed.
  112. if (quiet_system("tla", "update", "-d",
  113. "$config{srcdir}") != 0) {
  114. warn("tla update failed\n");
  115. }
  116. }
  117. if (quiet_system("tla", "commit",
  118. "-L".IkiWiki::possibly_foolish_untaint($message),
  119. '-d', $config{srcdir}) != 0) {
  120. my $conflict=readfile("$config{srcdir}/$file");
  121. if (system("tla", "undo", "-n", "--quiet", "-d", "$config{srcdir}") != 0) {
  122. warn("tla undo failed\n");
  123. }
  124. return $conflict;
  125. }
  126. }
  127. return undef # success
  128. }
  129. sub rcs_commit_staged ($$$) {
  130. # Commits all staged changes. Changes can be staged using rcs_add,
  131. # rcs_remove, and rcs_rename.
  132. my ($message, $user, $ipaddr)=@_;
  133. error("rcs_commit_staged not implemented for tla"); # TODO
  134. }
  135. sub rcs_add ($) {
  136. my $file=shift;
  137. if (-d "$config{srcdir}/{arch}") {
  138. if (quiet_system("tla", "add", "$config{srcdir}/$file") != 0) {
  139. warn("tla add failed\n");
  140. }
  141. }
  142. }
  143. sub rcs_remove ($) {
  144. my $file = shift;
  145. error("rcs_remove not implemented for tla"); # TODO
  146. }
  147. sub rcs_rename ($$) {
  148. my ($src, $dest) = @_;
  149. error("rcs_rename not implemented for tla"); # TODO
  150. }
  151. sub rcs_recentchanges ($) {
  152. my $num=shift;
  153. my @ret;
  154. return unless -d "$config{srcdir}/{arch}";
  155. eval q{use Date::Parse};
  156. error($@) if $@;
  157. eval q{use Mail::Header};
  158. error($@) if $@;
  159. my $logs = `tla logs -d $config{srcdir}`;
  160. my @changesets = reverse split(/\n/, $logs);
  161. for (my $i=0; $i<$num && $i<$#changesets; $i++) {
  162. my ($change)=$changesets[$i]=~/^([A-Za-z0-9@\/._-]+)$/; # untaint
  163. open(LOG, "tla cat-log -d $config{srcdir} $change|");
  164. my $head = Mail::Header->new(\*LOG);
  165. close(LOG);
  166. my $rev = $head->get("Revision");
  167. my $summ = $head->get("Summary");
  168. my $newfiles = $head->get("New-files");
  169. my $modfiles = $head->get("Modified-files");
  170. my $remfiles = $head->get("Removed-files");
  171. my $user = $head->get("Creator");
  172. my @paths = grep { !/^(.*\/)?\.arch-ids\/.*\.id$/ }
  173. split(/ /, "$newfiles $modfiles .arch-ids/fake.id");
  174. my $sdate = $head->get("Standard-date");
  175. my $when = str2time($sdate, 'UTC');
  176. my $committype = "web";
  177. if (defined $summ && $summ =~ /$config{web_commit_regexp}/) {
  178. $user = defined $2 ? "$2" : "$3";
  179. $summ = $4;
  180. }
  181. else {
  182. $committype="tla";
  183. }
  184. my @message;
  185. push @message, { line => $summ };
  186. my @pages;
  187. foreach my $file (@paths) {
  188. my $diffurl=defined $config{diffurl} ? $config{diffurl} : "";
  189. $diffurl=~s/\[\[file\]\]/$file/g;
  190. $diffurl=~s/\[\[rev\]\]/$change/g;
  191. push @pages, {
  192. page => pagename($file),
  193. diffurl => $diffurl,
  194. } if length $file;
  195. }
  196. push @ret, {
  197. rev => $change,
  198. user => $user,
  199. committype => $committype,
  200. when => $when,
  201. message => [@message],
  202. pages => [@pages],
  203. } if @pages;
  204. last if $i == $num;
  205. }
  206. return @ret;
  207. }
  208. sub rcs_diff ($) {
  209. my $rev=shift;
  210. my $logs = `tla logs -d $config{srcdir}`;
  211. my @changesets = reverse split(/\n/, $logs);
  212. my $i;
  213. for($i=0;$i<$#changesets;$i++) {
  214. last if $changesets[$i] eq $rev;
  215. }
  216. my $revminusone = $changesets[$i+1];
  217. return `tla diff -d $config{srcdir} $revminusone`;
  218. }
  219. sub rcs_getctime ($) {
  220. my $file=shift;
  221. eval q{use Date::Parse};
  222. error($@) if $@;
  223. eval q{use Mail::Header};
  224. error($@) if $@;
  225. my $logs = `tla logs -d $config{srcdir}`;
  226. my @changesets = reverse split(/\n/, $logs);
  227. my $sdate;
  228. for (my $i=0; $i<$#changesets; $i++) {
  229. my $change = $changesets[$i];
  230. open(LOG, "tla cat-log -d $config{srcdir} $change|");
  231. my $head = Mail::Header->new(\*LOG);
  232. close(LOG);
  233. $sdate = $head->get("Standard-date");
  234. my $newfiles = $head->get("New-files");
  235. my ($lastcreation) = grep {/^$file$/} split(/ /, "$newfiles");
  236. last if defined($lastcreation);
  237. }
  238. my $date=str2time($sdate, 'UTC');
  239. debug("found ctime ".localtime($date)." for $file");
  240. return $date;
  241. }
  242. sub rcs_getmtime ($) {
  243. error "rcs_getmtime is not implemented for tla\n"; # TODO
  244. }
  245. 1
s="hl opt">, $origin_branch);
  • }
  • return undef; # success
  • } #}}}
  • sub rcs_add ($) { # {{{
  • # Add file to archive.
  • my ($file) = @_;
  • run_or_cry('git-add', $file);
  • } #}}}
  • sub rcs_recentchanges ($) { #{{{
  • # List of recent changes.
  • my ($num) = @_;
  • eval q{use CGI 'escapeHTML'};
  • eval q{use Date::Parse};
  • my ($sha1, $type, $when, $diffurl, $user, @pages, @message, @rets);
  • INFO: foreach my $ci (git_commit_info('HEAD', $num)) {
  • my $title = @{ $ci->{'comment'} }[0];
  • # Skip redundant commits.
  • next INFO if ($title eq $dummy_commit_msg);
  • $sha1 = $ci->{'sha1'};
  • $type = "web";
  • $when = time - $ci->{'author_epoch'};
  • foreach my $bit (@{ $ci->{'details'} }) {
  • my $diffurl = $config{'diffurl'};
  • my $file = $bit->{'file'};
  • $diffurl =~ s/\[\[file\]\]/$file/go;
  • $diffurl =~ s/\[\[sha1_parent\]\]/$ci->{'parent'}/go;
  • $diffurl =~ s/\[\[sha1_from\]\]/$bit->{'sha1_from'}/go;
  • $diffurl =~ s/\[\[sha1_to\]\]/$bit->{'sha1_to'}/go;
  • push @pages, {
  • page => pagename($file),
  • diffurl => $diffurl,
  • },
  • }
  • push @message, { line => escapeHTML($title) };
  • if (defined $message[0] &&
  • $message[0]->{line} =~ m/$web_commit_msg/) {
  • $user=defined $2 ? "$2" : "$3";
  • $message[0]->{line}=$4;
  • } else {
  • $type ="git";
  • $user = $ci->{'author_username'};
  • }
  • push @rets, {
  • rev => $sha1,
  • user => $user,
  • committype => $type,
  • when => $when,
  • message => [@message],
  • pages => [@pages],
  • } if @pages;
  • $sha1 = $type = $when = $diffurl = $user = undef;
  • @pages = @message = ();
  • }
  • return @rets;
  • } #}}}
  • sub rcs_notify () { #{{{
  • # Send notification mail to subscribed users.
  • #
  • # In usual Git usage, hooks/update script is presumed to send
  • # notification mails (see git-receive-pack(1)). But we prefer
  • # hooks/post-update to support IkiWiki commits coming from a
  • # cloned repository (through command line) because post-update
  • # is called _after_ each ref in repository is updated (update
  • # hook is called _before_ the repository is updated). Since
  • # post-update hook does not accept command line arguments, we
  • # don't have an $ENV variable in this function.
  • #
  • # Here, we rely on a simple fact: we can extract all parts of the
  • # notification content by parsing the "HEAD" commit (which also
  • # triggers a refresh of IkiWiki pages) and we can obtain the diff
  • # by comparing HEAD and HEAD^ (the previous commit).
  • my $sha1 = 'HEAD'; # the commit which triggers this action
  • my $ci = git_commit_info($sha1);
  • return if !defined $ci;
  • my @changed_pages = map { $_->{'file'} } @{ $ci->{'details'} };
  • my ($user, $message);
  • if (@{ $ci->{'comment'} }[0] =~ m/$web_commit_msg/) {
  • $user = defined $2 ? "$2" : "$3";
  • $message = $4;
  • } else {
  • $user = $ci->{'author_username'};
  • $message = join "\n", @{ $ci->{'comment'} };
  • }
  • require IkiWiki::UserInfo;
  • my @email_recipients = commit_notify_list($user, @changed_pages);
  • return if !@email_recipients;
  • # TODO: if a commit spans multiple pages, this will send
  • # subscribers a diff that might contain pages they did not
  • # sign up for. Should separate the diff per page and
  • # reassemble into one mail with just the pages subscribed to.
  • my $diff = join "\n", run_or_die('git-diff', "${sha1}^", $sha1);
  • my $subject = "$config{wikiname} update of ";
  • if (@changed_pages > 2) {
  • $subject .= "$changed_pages[0] $changed_pages[1] etc";
  • } else {
  • $subject .= join " ", @changed_pages;
  • }
  • $subject .= " by $user";
  • my $template = template("notifymail.tmpl");
  • $template->param(
  • wikiname => $config{wikiname},
  • diff => $diff,
  • user => $user,
  • message => $message,
  • );
  • eval q{use Mail::Sendmail};
  • foreach my $email (@email_recipients) {
  • sendmail(
  • To => $email,
  • From => "$config{wikiname} <$config{adminemail}>",
  • Subject => $subject,
  • Message => $template->output,
  • ) or error("Failed to send update notification mail: $!");
  • }
  • } #}}}
  • sub rcs_getctime ($) { #{{{
  • # Get the ctime of file.
  • my ($file) = @_;
  • my $sha1 = git_sha1($file);
  • my $ci = git_commit_info($sha1);
  • my $ctime = $ci->{'author_epoch'};
  • debug("ctime for '$file': ". localtime($ctime) . "\n");
  • return $ctime;
  • } #}}}
  • 1