git-another.pl 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Copyright (C) 2014 Alex-Daniel Jakimenko <alex.jakimenko@gmail.com>
  2. # Copyright (C) 2011 Alex Schroeder <alex@gnu.org>
  3. # This program is free software: you can redistribute it and/or modify it under
  4. # the terms of the GNU General Public License as published by the Free Software
  5. # Foundation, either version 3 of the License, or (at your option) any later
  6. # version.
  7. #
  8. # This program is distributed in the hope that it will be useful, but WITHOUT
  9. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License along with
  13. # this program. If not, see <http://www.gnu.org/licenses/>.
  14. use strict;
  15. use v5.10;
  16. AddModuleDescription('git-another.pl', 'Another Git Extension');
  17. use Cwd;
  18. use Capture::Tiny ':all';
  19. our (%Page, $DataDir, $FootnoteNumber);
  20. our ($GitBinary, $GitMail);
  21. $GitBinary = 'git';
  22. $GitMail = 'unknown@oddmuse.org';
  23. sub GitCommit {
  24. my ($message, $author) = @_;
  25. my $oldDir = cwd;
  26. ChangeDir("$DataDir/page");
  27. capture {
  28. system($GitBinary, qw(add -A));
  29. system($GitBinary, qw(commit -q -m), $message, "--author=$author <$GitMail>");
  30. };
  31. ChangeDir($oldDir);
  32. }
  33. sub GitInitRepository {
  34. return if IsDir("$DataDir/page/.git");
  35. capture {
  36. system($GitBinary, qw(init -q --), encode_utf8("$DataDir/page"));
  37. };
  38. GitCommit('Initial import', 'Oddmuse');
  39. }
  40. sub RenderHtmlCacheWithoutPrinting { # requires an open page
  41. $FootnoteNumber = 0;
  42. my ($blocks, $flags);
  43. capture {
  44. ($blocks, $flags) = ApplyRules(QuoteHtml($Page{text}), 1, 1, $Page{revision}, 'p');
  45. };
  46. if ($Page{blocks} ne $blocks and $Page{flags} ne $flags) {
  47. $Page{blocks} = $blocks;
  48. $Page{flags} = $flags;
  49. SavePage();
  50. }
  51. }
  52. *GitOldSave = \&Save;
  53. *Save = \&GitNewSave;
  54. sub GitNewSave {
  55. GitInitRepository();
  56. GitCommit('No description available', 'Oddmuse'); # commit any changes before this edit
  57. GitOldSave(@_);
  58. RenderHtmlCacheWithoutPrinting();
  59. my $message = $Page{summary};
  60. $message =~ s/^\s+$//;
  61. $message ||= T('No summary provided');
  62. my $author = $Page{username} || T('Anonymous');
  63. GitCommit($message, $author); # commit this edit
  64. }