summaryrefslogtreecommitdiff
path: root/IkiWiki/Rcs/SVN.pm
blob: 946412320a35d305e3a9d6aff15411f3ad15eec5 (plain)
  1. #!/usr/bin/perl -T
  2. # For subversion support.
  3. use warnings;
  4. use strict;
  5. package IkiWiki;
  6. sub svn_info ($$) { #{{{
  7. my $field=shift;
  8. my $file=shift;
  9. my $info=`LANG=C svn info $file`;
  10. my ($ret)=$info=~/^$field: (.*)$/m;
  11. return $ret;
  12. } #}}}
  13. sub rcs_update () { #{{{
  14. if (-d "$config{srcdir}/.svn") {
  15. if (system("svn", "update", "--quiet", $config{srcdir}) != 0) {
  16. warn("svn update failed\n");
  17. }
  18. }
  19. } #}}}
  20. sub rcs_prepedit ($) { #{{{
  21. # Prepares to edit a file under revision control. Returns a token
  22. # that must be passed into rcs_commit when the file is ready
  23. # for committing.
  24. # The file is relative to the srcdir.
  25. my $file=shift;
  26. if (-d "$config{srcdir}/.svn") {
  27. # For subversion, return the revision of the file when
  28. # editing begins.
  29. my $rev=svn_info("Revision", "$config{srcdir}/$file");
  30. return defined $rev ? $rev : "";
  31. }
  32. } #}}}
  33. sub rcs_commit ($$$) { #{{{
  34. # Tries to commit the page; returns undef on _success_ and
  35. # a version of the page with the rcs's conflict markers on failure.
  36. # The file is relative to the srcdir.
  37. my $file=shift;
  38. my $message=shift;
  39. my $rcstoken=shift;
  40. if (-d "$config{srcdir}/.svn") {
  41. # Check to see if the page has been changed by someone
  42. # else since rcs_prepedit was called.
  43. my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
  44. my $rev=svn_info("Revision", "$config{srcdir}/$file");
  45. if (defined $rev && defined $oldrev && $rev != $oldrev) {
  46. # Merge their changes into the file that we've
  47. # changed.
  48. chdir($config{srcdir}); # svn merge wants to be here
  49. if (system("svn", "merge", "--quiet", "-r$oldrev:$rev",
  50. "$config{srcdir}/$file") != 0) {
  51. warn("svn merge -r$oldrev:$rev failed\n");
  52. }
  53. }
  54. if (system("svn", "commit", "--quiet", "-m",
  55. possibly_foolish_untaint($message),
  56. "$config{srcdir}") != 0) {
  57. my $conflict=readfile("$config{srcdir}/$file");
  58. if (system("svn", "revert", "--quiet", "$config{srcdir}/$file") != 0) {
  59. warn("svn revert failed\n");
  60. }
  61. return $conflict;
  62. }
  63. }
  64. return undef # success
  65. } #}}}
  66. sub rcs_add ($) { #{{{
  67. # filename is relative to the root of the srcdir
  68. my $file=shift;
  69. if (-d "$config{srcdir}/.svn") {
  70. my $parent=dirname($file);
  71. while (! -d "$config{srcdir}/$parent/.svn") {
  72. $file=$parent;
  73. $parent=dirname($file);
  74. }
  75. if (system("svn", "add", "--quiet", "$config{srcdir}/$file") != 0) {
  76. warn("svn add failed\n");
  77. }
  78. }
  79. } #}}}
  80. sub rcs_recentchanges ($) { #{{{
  81. my $num=shift;
  82. my @ret;
  83. eval q{use CGI 'escapeHTML'};
  84. eval q{use Date::Parse};
  85. eval q{use Time::Duration};
  86. if (-d "$config{srcdir}/.svn") {
  87. my $svn_url=svn_info("URL", $config{srcdir});
  88. # FIXME: currently assumes that the wiki is somewhere
  89. # under trunk in svn, doesn't support other layouts.
  90. my ($svn_base)=$svn_url=~m!(/trunk(?:/.*)?)$!;
  91. my $div=qr/^--------------------+$/;
  92. my $infoline=qr/^r(\d+)\s+\|\s+([^\s]+)\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
  93. my $state='start';
  94. my ($rev, $user, $when, @pages, @message);
  95. foreach (`LANG=C svn log --limit $num -v '$svn_url'`) {
  96. chomp;
  97. if ($state eq 'start' && /$div/) {
  98. $state='header';
  99. }
  100. elsif ($state eq 'header' && /$infoline/) {
  101. $rev=$1;
  102. $user=$2;
  103. $when=concise(ago(time - str2time($3)));
  104. }
  105. elsif ($state eq 'header' && /^\s+[A-Z]\s+\Q$svn_base\E\/([^ ]+)(?:$|\s)/) {
  106. my $file=$1;
  107. my $diffurl=$config{diffurl};
  108. $diffurl=~s/\[\[file\]\]/$file/g;
  109. $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
  110. $diffurl=~s/\[\[r2\]\]/$rev/g;
  111. push @pages, {
  112. link => htmllink("", pagename($file), 1),
  113. diffurl => $diffurl,
  114. } if length $file;
  115. }
  116. elsif ($state eq 'header' && /^$/) {
  117. $state='body';
  118. }
  119. elsif ($state eq 'body' && /$div/) {
  120. my $committype="web";
  121. if (defined $message[0] &&
  122. $message[0]->{line}=~/^web commit by (\w+):?(.*)/) {
  123. $user="$1";
  124. $message[0]->{line}=$2;
  125. }
  126. else {
  127. $committype="svn";
  128. }
  129. push @ret, { rev => $rev,
  130. user => htmllink("", $user, 1),
  131. committype => $committype,
  132. when => $when, message => [@message],
  133. pages => [@pages],
  134. } if @pages;
  135. return @ret if @ret >= $num;
  136. $state='header';
  137. $rev=$user=$when=undef;
  138. @pages=@message=();
  139. }
  140. elsif ($state eq 'body') {
  141. push @message, {line => escapeHTML($_)},
  142. }
  143. }
  144. }
  145. return @ret;
  146. } #}}}
  147. 1