summaryrefslogtreecommitdiff
path: root/IkiWiki/Rcs/svn.pm
blob: be4106a02d143704f1e9bf2496e1e0b56dcf43b2 (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",
  57. "--encoding", "UTF-8", "-m",
  58. possibly_foolish_untaint($message),
  59. "$config{srcdir}") != 0) {
  60. my $conflict=readfile("$config{srcdir}/$file");
  61. if (system("svn", "revert", "--quiet", "$config{srcdir}/$file") != 0) {
  62. warn("svn revert failed\n");
  63. }
  64. return $conflict;
  65. }
  66. }
  67. return undef # success
  68. } #}}}
  69. sub rcs_add ($) { #{{{
  70. # filename is relative to the root of the srcdir
  71. my $file=shift;
  72. if (-d "$config{srcdir}/.svn") {
  73. my $parent=dirname($file);
  74. while (! -d "$config{srcdir}/$parent/.svn") {
  75. $file=$parent;
  76. $parent=dirname($file);
  77. }
  78. if (system("svn", "add", "--quiet", "$config{srcdir}/$file") != 0) {
  79. warn("svn add failed\n");
  80. }
  81. }
  82. } #}}}
  83. sub rcs_recentchanges ($) { #{{{
  84. my $num=shift;
  85. my @ret;
  86. return unless -d "$config{srcdir}/.svn";
  87. eval q{use CGI 'escapeHTML'};
  88. eval q{use Date::Parse};
  89. eval q{use Time::Duration};
  90. eval q{use XML::Simple};
  91. my $svn_url=svn_info("URL", $config{srcdir});
  92. my $xml = XMLin(scalar `svn --xml -v log '$svn_url'`,
  93. ForceArray => [ 'logentry', 'path' ],
  94. GroupTags => { paths => 'path' },
  95. KeyAttr => { path => 'content' },
  96. );
  97. foreach my $logentry (@{$xml->{logentry}}) {
  98. my (@pages, @message);
  99. my $rev = $logentry->{revision};
  100. my $user = $logentry->{author};
  101. my $date = $logentry->{date};
  102. $date =~ s/T/ /;
  103. $date =~ s/\.\d+Z$//;
  104. my $when=concise(ago(time - str2time($date, 'UTC')));
  105. foreach my $msgline (split(/\n/, $logentry->{msg})) {
  106. push @message, { line => escapeHTML($msgline) };
  107. }
  108. my $committype="web";
  109. if (defined $message[0] &&
  110. $message[0]->{line}=~/$svn_webcommit/) {
  111. $user="$1";
  112. $message[0]->{line}=$2;
  113. }
  114. else {
  115. $committype="svn";
  116. }
  117. foreach (keys %{$logentry->{paths}}) {
  118. next unless /^\/\Q$config{svnpath}\E\/([^ ]+)(?:$|\s)/;
  119. my $file=$1;
  120. my $diffurl=$config{diffurl};
  121. $diffurl=~s/\[\[file\]\]/$file/g;
  122. $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
  123. $diffurl=~s/\[\[r2\]\]/$rev/g;
  124. push @pages, {
  125. link => htmllink("", "", pagename($file), 1),
  126. diffurl => $diffurl,
  127. } if length $file;
  128. }
  129. push @ret, { rev => $rev,
  130. user => htmllink("", "", $user, 1),
  131. committype => $committype,
  132. when => $when,
  133. message => [@message],
  134. pages => [@pages],
  135. } if @pages;
  136. return @ret if @ret >= $num;
  137. }
  138. return @ret;
  139. } #}}}
  140. sub rcs_notify () { #{{{
  141. if (! exists $ENV{REV}) {
  142. error("REV is not set, not running from svn post-commit hook, cannot send notifications");
  143. }
  144. my $rev=int(possibly_foolish_untaint($ENV{REV}));
  145. my $user=`svnlook author $config{svnrepo} -r $rev`;
  146. chomp $user;
  147. my $message=`svnlook log $config{svnrepo} -r $rev`;
  148. if ($message=~/$svn_webcommit/) {
  149. $user="$1";
  150. $message=$2;
  151. }
  152. my @changed_pages;
  153. foreach my $change (`svnlook changed $config{svnrepo} -r $rev`) {
  154. chomp $change;
  155. if ($change =~ /^[A-Z]+\s+\Q$config{svnpath}\E\/(.*)/) {
  156. push @changed_pages, $1;
  157. }
  158. }
  159. require IkiWiki::UserInfo;
  160. my @email_recipients=commit_notify_list($user, @changed_pages);
  161. if (@email_recipients) {
  162. # TODO: if a commit spans multiple pages, this will send
  163. # subscribers a diff that might contain pages they did not
  164. # sign up for. Should separate the diff per page and
  165. # reassemble into one mail with just the pages subscribed to.
  166. my $diff=`svnlook diff $config{svnrepo} -r $rev --no-diff-deleted`;
  167. my $subject="$config{wikiname} update of ";
  168. if (@changed_pages > 2) {
  169. $subject.="$changed_pages[0] $changed_pages[1] etc";
  170. }
  171. else {
  172. $subject.=join(" ", @changed_pages);
  173. }
  174. $subject.=" by $user";
  175. my $template=template("notifymail.tmpl");
  176. $template->param(
  177. wikiname => $config{wikiname},
  178. diff => $diff,
  179. user => $user,
  180. message => $message,
  181. );
  182. eval q{use Mail::Sendmail};
  183. foreach my $email (@email_recipients) {
  184. sendmail(
  185. To => $email,
  186. From => "$config{wikiname} <$config{adminemail}>",
  187. Subject => $subject,
  188. Message => $template->output,
  189. ) or error("Failed to send update notification mail");
  190. }
  191. }
  192. } #}}}
  193. sub rcs_getctime ($) { #{{{
  194. my $file=shift;
  195. eval q{use Date::Parse};
  196. my $svn_log_infoline=qr/^r\d+\s+\|\s+[^\s]+\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
  197. my $child = open(SVNLOG, "-|");
  198. if (! $child) {
  199. exec("svn", "log", $file) || error("svn log $file failed to run");
  200. }
  201. my $date;
  202. while (<SVNLOG>) {
  203. if (/$svn_log_infoline/) {
  204. $date=$1;
  205. }
  206. }
  207. close SVNLOG || warn "svn log $file exited $?";
  208. if (! defined $date) {
  209. warn "failed to parse svn log for $file\n";
  210. return 0;
  211. }
  212. $date=str2time($date);
  213. debug("found ctime ".localtime($date)." for $file");
  214. return $date;
  215. } #}}}
  216. 1