summaryrefslogtreecommitdiff
path: root/IkiWiki/Rcs/svn.pm
blob: 5474e3be2537a311896e10c71613fa19da243031 (plain)
  1. #!/usr/bin/perl
  2. # For subversion support.
  3. use warnings;
  4. use strict;
  5. use IkiWiki;
  6. package IkiWiki;
  7. my $svn_webcommit=qr/^web commit by (\w+):?(.*)/;
  8. sub svn_info ($$) { #{{{
  9. my $field=shift;
  10. my $file=shift;
  11. my $info=`LANG=C svn info $file`;
  12. my ($ret)=$info=~/^$field: (.*)$/m;
  13. return $ret;
  14. } #}}}
  15. sub rcs_update () { #{{{
  16. if (-d "$config{srcdir}/.svn") {
  17. if (system("svn", "update", "--quiet", $config{srcdir}) != 0) {
  18. warn("svn update failed\n");
  19. }
  20. }
  21. } #}}}
  22. sub rcs_prepedit ($) { #{{{
  23. # Prepares to edit a file under revision control. Returns a token
  24. # that must be passed into rcs_commit when the file is ready
  25. # for committing.
  26. # The file is relative to the srcdir.
  27. my $file=shift;
  28. if (-d "$config{srcdir}/.svn") {
  29. # For subversion, return the revision of the file when
  30. # editing begins.
  31. my $rev=svn_info("Revision", "$config{srcdir}/$file");
  32. return defined $rev ? $rev : "";
  33. }
  34. } #}}}
  35. sub rcs_commit ($$$) { #{{{
  36. # Tries to commit the page; returns undef on _success_ and
  37. # a version of the page with the rcs's conflict markers on failure.
  38. # The file is relative to the srcdir.
  39. my $file=shift;
  40. my $message=shift;
  41. my $rcstoken=shift;
  42. if (-d "$config{srcdir}/.svn") {
  43. # Check to see if the page has been changed by someone
  44. # else since rcs_prepedit was called.
  45. my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
  46. my $rev=svn_info("Revision", "$config{srcdir}/$file");
  47. if (defined $rev && defined $oldrev && $rev != $oldrev) {
  48. # Merge their changes into the file that we've
  49. # changed.
  50. chdir($config{srcdir}); # svn merge wants to be here
  51. if (system("svn", "merge", "--quiet", "-r$oldrev:$rev",
  52. "$config{srcdir}/$file") != 0) {
  53. warn("svn merge -r$oldrev:$rev failed\n");
  54. }
  55. }
  56. if (system("svn", "commit", "--quiet", "-m",
  57. possibly_foolish_untaint($message),
  58. "$config{srcdir}") != 0) {
  59. my $conflict=readfile("$config{srcdir}/$file");
  60. if (system("svn", "revert", "--quiet", "$config{srcdir}/$file") != 0) {
  61. warn("svn revert failed\n");
  62. }
  63. return $conflict;
  64. }
  65. }
  66. return undef # success
  67. } #}}}
  68. sub rcs_add ($) { #{{{
  69. # filename is relative to the root of the srcdir
  70. my $file=shift;
  71. if (-d "$config{srcdir}/.svn") {
  72. my $parent=dirname($file);
  73. while (! -d "$config{srcdir}/$parent/.svn") {
  74. $file=$parent;
  75. $parent=dirname($file);
  76. }
  77. if (system("svn", "add", "--quiet", "$config{srcdir}/$file") != 0) {
  78. warn("svn add failed\n");
  79. }
  80. }
  81. } #}}}
  82. sub rcs_recentchanges ($) { #{{{
  83. my $num=shift;
  84. my @ret;
  85. return unless -d "$config{srcdir}/.svn";
  86. eval q{use CGI 'escapeHTML'};
  87. eval q{use Date::Parse};
  88. eval q{use Time::Duration};
  89. eval q{use XML::Simple};
  90. my $svn_url=svn_info("URL", $config{srcdir});
  91. my $xml = XMLin(scalar `svn --xml -v log '$svn_url'`,
  92. ForceArray => [ 'logentry', 'path' ],
  93. GroupTags => { paths => 'path' },
  94. KeyAttr => { path => 'content' },
  95. );
  96. foreach my $logentry (@{$xml->{logentry}}) {
  97. my (@pages, @message);
  98. my $rev = $logentry->{revision};
  99. my $user = $logentry->{author};
  100. my $date = $logentry->{date};
  101. $date =~ s/T/ /;
  102. $date =~ s/\.\d+Z$//;
  103. my $when=concise(ago(time - str2time($date, 'UTC')));
  104. foreach my $msgline (split(/\n/, $logentry->{msg})) {
  105. push @message, { line => escapeHTML($msgline) };
  106. }
  107. my $committype="web";
  108. if (defined $message[0] &&
  109. $message[0]->{line}=~/$svn_webcommit/) {
  110. $user="$1";
  111. $message[0]->{line}=$2;
  112. }
  113. else {
  114. $committype="svn";
  115. }
  116. foreach (keys %{$logentry->{paths}}) {
  117. next unless /^\/\Q$config{svnpath}\E\/([^ ]+)(?:$|\s)/;
  118. my $file=$1;
  119. my $diffurl=$config{diffurl};
  120. $diffurl=~s/\[\[file\]\]/$file/g;
  121. $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
  122. $diffurl=~s/\[\[r2\]\]/$rev/g;
  123. push @pages, {
  124. link => htmllink("", "", pagename($file), 1),
  125. diffurl => $diffurl,
  126. } if length $file;
  127. }
  128. push @ret, { rev => $rev,
  129. user => htmllink("", "", $user, 1),
  130. committype => $committype,
  131. when => $when,
  132. message => [@message],
  133. pages => [@pages],
  134. } if @pages;
  135. return @ret if @ret >= $num;
  136. }
  137. return @ret;
  138. } #}}}
  139. sub rcs_notify () { #{{{
  140. if (! exists $ENV{REV}) {
  141. error("REV is not set, not running from svn post-commit hook, cannot send notifications");
  142. }
  143. my $rev=int(possibly_foolish_untaint($ENV{REV}));
  144. my $user=`svnlook author $config{svnrepo} -r $rev`;
  145. chomp $user;
  146. my $message=`svnlook log $config{svnrepo} -r $rev`;
  147. if ($message=~/$svn_webcommit/) {
  148. $user="$1";
  149. $message=$2;
  150. }
  151. my @changed_pages;
  152. foreach my $change (`svnlook changed $config{svnrepo} -r $rev`) {
  153. chomp $change;
  154. if ($change =~ /^[A-Z]+\s+\Q$config{svnpath}\E\/(.*)/) {
  155. push @changed_pages, $1;
  156. }
  157. }
  158. require IkiWiki::UserInfo;
  159. my @email_recipients=commit_notify_list($user, @changed_pages);
  160. if (@email_recipients) {
  161. # TODO: if a commit spans multiple pages, this will send
  162. # subscribers a diff that might contain pages they did not
  163. # sign up for. Should separate the diff per page and
  164. # reassemble into one mail with just the pages subscribed to.
  165. my $diff=`svnlook diff $config{svnrepo} -r $rev --no-diff-deleted`;
  166. my $subject="$config{wikiname} update of ";
  167. if (@changed_pages > 2) {
  168. $subject.="$changed_pages[0] $changed_pages[1] etc";
  169. }
  170. else {
  171. $subject.=join(" ", @changed_pages);
  172. }
  173. $subject.=" by $user";
  174. my $template=HTML::Template->new(
  175. filename => "$config{templatedir}/notifymail.tmpl"
  176. );
  177. $template->param(
  178. wikiname => $config{wikiname},
  179. diff => $diff,
  180. user => $user,
  181. message => $message,
  182. );
  183. eval q{use Mail::Sendmail};
  184. foreach my $email (@email_recipients) {
  185. sendmail(
  186. To => $email,
  187. From => "$config{wikiname} <$config{adminemail}>",
  188. Subject => $subject,
  189. Message => $template->output,
  190. ) or error("Failed to send update notification mail");
  191. }
  192. }
  193. } #}}}
  194. sub rcs_getctime ($) { #{{{
  195. my $file=shift;
  196. eval q{use Date::Parse};
  197. my $svn_log_infoline=qr/^r\d+\s+\|\s+[^\s]+\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
  198. my $child = open(SVNLOG, "-|");
  199. if (! $child) {
  200. exec("svn", "log", $file) || error("svn log $file failed to run");
  201. }
  202. my $date;
  203. while (<SVNLOG>) {
  204. if (/$svn_log_infoline/) {
  205. $date=$1;
  206. }
  207. }
  208. close SVNLOG || warn "svn log $file exited $?";
  209. if (! defined $date) {
  210. warn "failed to parse svn log for $file\n";
  211. return 0;
  212. }
  213. $date=str2time($date);
  214. debug("found ctime ".localtime($date)." for $file");
  215. return $date;
  216. } #}}}
  217. 1