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