2 package IkiWiki::Plugin::git;
9 use URI::Escape q{uri_escape_utf8};
10 use open qw{:utf8 :std};
12 my $sha1_pattern = qr/[0-9a-fA-F]{40}/; # pattern to validate Git sha1sums
13 my $dummy_commit_msg = 'dummy commit'; # message to skip in recent changes
16 hook(type => "checkconfig", id => "git", call => \&checkconfig);
17 hook(type => "getsetup", id => "git", call => \&getsetup);
18 hook(type => "genwrapper", id => "git", call => \&genwrapper);
19 hook(type => "rcs", id => "rcs_update", call => \&rcs_update);
20 hook(type => "rcs", id => "rcs_prepedit", call => \&rcs_prepedit);
21 hook(type => "rcs", id => "rcs_commit", call => \&rcs_commit);
22 hook(type => "rcs", id => "rcs_commit_staged", call => \&rcs_commit_staged);
23 hook(type => "rcs", id => "rcs_add", call => \&rcs_add);
24 hook(type => "rcs", id => "rcs_remove", call => \&rcs_remove);
25 hook(type => "rcs", id => "rcs_rename", call => \&rcs_rename);
26 hook(type => "rcs", id => "rcs_recentchanges", call => \&rcs_recentchanges);
27 hook(type => "rcs", id => "rcs_diff", call => \&rcs_diff);
28 hook(type => "rcs", id => "rcs_getctime", call => \&rcs_getctime);
29 hook(type => "rcs", id => "rcs_getmtime", call => \&rcs_getmtime);
30 hook(type => "rcs", id => "rcs_receive", call => \&rcs_receive);
31 hook(type => "rcs", id => "rcs_preprevert", call => \&rcs_preprevert);
32 hook(type => "rcs", id => "rcs_revert", call => \&rcs_revert);
36 if (! defined $config{gitorigin_branch}) {
37 $config{gitorigin_branch}="origin";
39 if (! defined $config{gitmaster_branch}) {
40 $config{gitmaster_branch}="master";
42 if (defined $config{git_wrapper} &&
43 length $config{git_wrapper}) {
44 push @{$config{wrappers}}, {
45 wrapper => $config{git_wrapper},
46 wrappermode => (defined $config{git_wrappermode} ? $config{git_wrappermode} : "06755"),
47 wrapper_background_command => $config{git_wrapper_background_command},
51 if (defined $config{git_test_receive_wrapper} &&
52 length $config{git_test_receive_wrapper} &&
53 defined $config{untrusted_committers} &&
54 @{$config{untrusted_committers}}) {
55 push @{$config{wrappers}}, {
57 wrapper => $config{git_test_receive_wrapper},
58 wrappermode => (defined $config{git_wrappermode} ? $config{git_wrappermode} : "06755"),
62 # Avoid notes, parser does not handle and they only slow things down.
63 $ENV{GIT_NOTES_REF}="";
65 # Run receive test only if being called by the wrapper, and not
66 # when generating same.
67 if ($config{test_receive} && ! exists $config{wrapper}) {
68 require IkiWiki::Receive;
69 IkiWiki::Receive::test();
76 safe => 0, # rcs plugin
82 example => "/git/wiki.git/hooks/post-update",
83 description => "git hook to generate",
87 git_wrapper_background_command => {
89 example => "git push github",
90 description => "shell command for git_wrapper to run, in the background",
97 description => "mode for git_wrapper (can safely be made suid)",
101 git_test_receive_wrapper => {
103 example => "/git/wiki.git/hooks/pre-receive",
104 description => "git pre-receive hook to generate",
108 untrusted_committers => {
111 description => "unix users whose commits should be checked by the pre-receive hook",
117 example => "http://git.example.com/gitweb.cgi?p=wiki.git;a=history;f=[[file]];hb=HEAD",
118 description => "gitweb url to show file history ([[file]] substituted)",
124 example => "http://git.example.com/gitweb.cgi?p=wiki.git;a=blobdiff;f=[[file]];h=[[sha1_to]];hp=[[sha1_from]];hb=[[sha1_commit]];hpb=[[sha1_parent]]",
125 description => "gitweb url to show a diff ([[file]], [[sha1_to]], [[sha1_from]], [[sha1_commit]], and [[sha1_parent]] substituted)",
129 gitorigin_branch => {
132 description => "where to pull and push changes (set to empty string to disable)",
133 safe => 0, # paranoia
136 gitmaster_branch => {
139 description => "branch that the wiki is stored in",
140 safe => 0, # paranoia
146 if ($config{test_receive}) {
147 require IkiWiki::Receive;
148 return IkiWiki::Receive::genwrapper();
158 sub in_git_dir ($$) {
167 # Start a child process safely without resorting to /bin/sh.
168 # Returns command output (in list content) or success state
169 # (in scalar context), or runs the specified data handler.
171 my ($error_handler, $data_handler, @cmdline) = @_;
173 my $pid = open my $OUT, "-|";
175 error("Cannot fork: $!") if !defined $pid;
179 # Git commands want to be in wc.
180 if (! defined $git_dir) {
181 chdir $config{srcdir}
182 or error("cannot chdir to $config{srcdir}: $!");
186 or error("cannot chdir to $git_dir: $!");
188 exec @cmdline or error("Cannot exec '@cmdline': $!");
192 # git output is probably utf-8 encoded, but may contain
193 # other encodings or invalidly encoded stuff. So do not rely
194 # on the normal utf-8 IO layer, decode it by hand.
199 $_=decode_utf8($_, 0);
203 if (! defined $data_handler) {
207 last unless $data_handler->($_);
213 $error_handler->("'@cmdline' failed: $!") if $? && $error_handler;
215 return wantarray ? @lines : ($? == 0);
217 # Convenient wrappers.
218 sub run_or_die ($@) { safe_git(\&error, undef, @_) }
219 sub run_or_cry ($@) { safe_git(sub { warn @_ }, undef, @_) }
220 sub run_or_non ($@) { safe_git(undef, undef, @_) }
223 sub merge_past ($$$) {
224 # Unlike with Subversion, Git cannot make a 'svn merge -rN:M file'.
225 # Git merge commands work with the committed changes, except in the
226 # implicit case of '-m' of git checkout(1). So we should invent a
227 # kludge here. In principle, we need to create a throw-away branch
228 # in preparing for the merge itself. Since branches are cheap (and
229 # branching is fast), this shouldn't cost high.
231 # The main problem is the presence of _uncommitted_ local changes. One
232 # possible approach to get rid of this situation could be that we first
233 # make a temporary commit in the master branch and later restore the
234 # initial state (this is possible since Git has the ability to undo a
235 # commit, i.e. 'git reset --soft HEAD^'). The method can be summarized
238 # - create a diff of HEAD:current-sha1
240 # - create a dummy branch and switch to it
241 # - rewind to past (reset --hard to the current-sha1)
242 # - apply the diff and commit
243 # - switch to master and do the merge with the dummy branch
244 # - make a soft reset (undo the last commit of master)
246 # The above method has some drawbacks: (1) it needs a redundant commit
247 # just to get rid of local changes, (2) somewhat slow because of the
248 # required system forks. Until someone points a more straight method
249 # (which I would be grateful) I have implemented an alternative method.
250 # In this approach, we hide all the modified files from Git by renaming
251 # them (using the 'rename' builtin) and later restore those files in
252 # the throw-away branch (that is, we put the files themselves instead
253 # of applying a patch).
255 my ($sha1, $file, $message) = @_;
257 my @undo; # undo stack for cleanup in case of an error
258 my $conflict; # file content with conflict markers
261 # Hide local changes from Git by renaming the modified file.
262 # Relative paths must be converted to absolute for renaming.
263 my ($target, $hidden) = (
264 "$config{srcdir}/${file}", "$config{srcdir}/${file}.${sha1}"
266 rename($target, $hidden)
267 or error("rename '$target' to '$hidden' failed: $!");
268 # Ensure to restore the renamed file on error.
270 return if ! -e "$hidden"; # already renamed
271 rename($hidden, $target)
272 or warn "rename '$hidden' to '$target' failed: $!";
275 my $branch = "throw_away_${sha1}"; # supposed to be unique
277 # Create a throw-away branch and rewind backward.
278 push @undo, sub { run_or_cry('git', 'branch', '-D', $branch) };
279 run_or_die('git', 'branch', $branch, $sha1);
281 # Switch to throw-away branch for the merge operation.
283 if (!run_or_cry('git', 'checkout', $config{gitmaster_branch})) {
284 run_or_cry('git', 'checkout','-f',$config{gitmaster_branch});
287 run_or_die('git', 'checkout', $branch);
289 # Put the modified file in _this_ branch.
290 rename($hidden, $target)
291 or error("rename '$hidden' to '$target' failed: $!");
293 # _Silently_ commit all modifications in the current branch.
294 run_or_non('git', 'commit', '-m', $message, '-a');
295 # ... and re-switch to master.
296 run_or_die('git', 'checkout', $config{gitmaster_branch});
298 # Attempt to merge without complaining.
299 if (!run_or_non('git', 'pull', '--no-commit', '.', $branch)) {
300 $conflict = readfile($target);
301 run_or_die('git', 'reset', '--hard');
306 # Process undo stack (in reverse order). By policy cleanup
307 # actions should normally print a warning on failure.
308 while (my $handle = pop @undo) {
312 error("Git merge failed!\n$failure\n") if $failure;
317 sub decode_git_file ($) {
320 # git does not output utf-8 filenames, but instead
321 # double-quotes them with the utf-8 characters
322 # escaped as \nnn\nnn.
323 if ($file =~ m/^"(.*)"$/) {
324 ($file=$1) =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
327 # strip prefix if in a subdir
328 if (! defined $prefix) {
329 ($prefix) = run_or_die('git', 'rev-parse', '--show-prefix');
330 if (! defined $prefix) {
334 $file =~ s/^\Q$prefix\E//;
336 return decode("utf8", $file);
339 sub parse_diff_tree ($) {
340 # Parse the raw diff tree chunk and return the info hash.
341 # See git-diff-tree(1) for the syntax.
345 return if ! @{ $dt_ref } ||
346 !defined $dt_ref->[0] || !length $dt_ref->[0];
350 while (my $line = shift @{ $dt_ref }) {
351 return if $line !~ m/^(.+) ($sha1_pattern)/;
358 # Identification lines for the commit.
359 while (my $line = shift @{ $dt_ref }) {
360 # Regexps are semi-stolen from gitweb.cgi.
361 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
364 elsif ($line =~ m/^parent ([0-9a-fA-F]{40})$/) {
365 # XXX: collecting in reverse order
366 push @{ $ci{'parents'} }, $1;
368 elsif ($line =~ m/^(author|committer) (.*) ([0-9]+) (.*)$/) {
369 my ($who, $name, $epoch, $tz) =
373 $ci{ "${who}_epoch" } = $epoch;
374 $ci{ "${who}_tz" } = $tz;
376 if ($name =~ m/^([^<]+)\s+<([^@>]+)/) {
377 $ci{"${who}_name"} = $1;
378 $ci{"${who}_username"} = $2;
380 elsif ($name =~ m/^([^<]+)\s+<>$/) {
381 $ci{"${who}_username"} = $1;
384 $ci{"${who}_username"} = $name;
387 elsif ($line =~ m/^$/) {
388 # Trailing empty line signals next section.
393 debug("No 'tree' seen in diff-tree output") if !defined $ci{'tree'};
395 if (defined $ci{'parents'}) {
396 $ci{'parent'} = @{ $ci{'parents'} }[0];
399 $ci{'parent'} = 0 x 40;
402 # Commit message (optional).
403 while ($dt_ref->[0] =~ /^ /) {
404 my $line = shift @{ $dt_ref };
406 push @{ $ci{'comment'} }, $line;
408 shift @{ $dt_ref } if $dt_ref->[0] =~ /^$/;
411 while (my $line = shift @{ $dt_ref }) {
413 (:+) # number of parents
414 ([^\t]+)\t # modes, sha1, status
417 my $num_parents = length $1;
418 my @tmp = split(" ", $2);
419 my ($file, $file_to) = split("\t", $3);
420 my @mode_from = splice(@tmp, 0, $num_parents);
421 my $mode_to = shift(@tmp);
422 my @sha1_from = splice(@tmp, 0, $num_parents);
423 my $sha1_to = shift(@tmp);
424 my $status = shift(@tmp);
427 push @{ $ci{'details'} }, {
428 'file' => decode_git_file($file),
429 'sha1_from' => $sha1_from[0],
430 'sha1_to' => $sha1_to,
431 'mode_from' => $mode_from[0],
432 'mode_to' => $mode_to,
444 sub git_commit_info ($;$) {
445 # Return an array of commit info hashes of num commits
446 # starting from the given sha1sum.
447 my ($sha1, $num) = @_;
450 push @opts, "--max-count=$num" if defined $num;
452 my @raw_lines = run_or_die('git', 'log', @opts,
453 '--pretty=raw', '--raw', '--abbrev=40', '--always', '-c',
454 '-r', $sha1, '--', '.');
457 while (my $parsed = parse_diff_tree(\@raw_lines)) {
461 warn "Cannot parse commit info for '$sha1' commit" if !@ci;
463 return wantarray ? @ci : $ci[0];
467 # Return head sha1sum (of given file).
468 my $file = shift || q{--};
470 # Ignore error since a non-existing file might be given.
471 my ($sha1) = run_or_non('git', 'rev-list', '--max-count=1', 'HEAD',
474 ($sha1) = $sha1 =~ m/($sha1_pattern)/; # sha1 is untainted now
476 return defined $sha1 ? $sha1 : '';
480 # Update working directory.
482 if (length $config{gitorigin_branch}) {
483 run_or_cry('git', 'pull', '--prune', $config{gitorigin_branch});
487 sub rcs_prepedit ($) {
488 # Return the commit sha1sum of the file when editing begins.
489 # This will be later used in rcs_commit if a merge is required.
492 return git_sha1($file);
496 # Try to commit the page; returns undef on _success_ and
497 # a version of the page with the rcs's conflict markers on
501 # Check to see if the page has been changed by someone else since
502 # rcs_prepedit was called.
503 my $cur = git_sha1($params{file});
504 my ($prev) = $params{token} =~ /^($sha1_pattern)$/; # untaint
506 if (defined $cur && defined $prev && $cur ne $prev) {
507 my $conflict = merge_past($prev, $params{file}, $dummy_commit_msg);
508 return $conflict if defined $conflict;
511 return rcs_commit_helper(@_);
514 sub rcs_commit_staged (@) {
515 # Commits all staged changes. Changes can be staged using rcs_add,
516 # rcs_remove, and rcs_rename.
517 return rcs_commit_helper(@_);
520 sub rcs_commit_helper (@) {
525 if (defined $params{session}) {
526 # Set the commit author and email based on web session info.
528 if (defined $params{session}->param("name")) {
529 $u=$params{session}->param("name");
531 elsif (defined $params{session}->remote_addr()) {
532 $u=$params{session}->remote_addr();
536 # MITLOGIN This algorithm could be improved
537 $ENV{GIT_AUTHOR_NAME}=IkiWiki::userinfo_get($u, "realname");
539 if (defined $params{session}->param("nickname")) {
540 $u=encode_utf8($params{session}->param("nickname"));
542 $u=~s/[^-_0-9[:alnum:]]+//g;
545 $ENV{GIT_AUTHOR_EMAIL}="$u\@mit.edu";
549 $params{message} = IkiWiki::possibly_foolish_untaint($params{message});
551 if ($params{message} !~ /\S/) {
552 # Force git to allow empty commit messages.
553 # (If this version of git supports it.)
554 my ($version)=`git --version` =~ /git version (.*)/;
555 if ($version ge "1.5.4") {
556 push @opts, '--cleanup=verbatim';
559 $params{message}.=".";
562 if (exists $params{file}) {
563 push @opts, '--', $params{file};
565 # git commit returns non-zero if nothing really changed.
566 # So we should ignore its exit status (hence run_or_non).
567 if (run_or_non('git', 'commit', '-m', $params{message}, '-q', @opts)) {
568 if (length $config{gitorigin_branch}) {
569 run_or_cry('git', 'push', $config{gitorigin_branch});
574 return undef; # success
578 # Add file to archive.
582 run_or_cry('git', 'add', $file);
586 # Remove file from archive.
590 run_or_cry('git', 'rm', '-f', $file);
593 sub rcs_rename ($$) {
594 my ($src, $dest) = @_;
596 run_or_cry('git', 'mv', '-f', $src, $dest);
599 sub rcs_recentchanges ($) {
600 # List of recent changes.
604 eval q{use Date::Parse};
608 foreach my $ci (git_commit_info('HEAD', $num || 1)) {
609 # Skip redundant commits.
610 next if ($ci->{'comment'} && @{$ci->{'comment'}}[0] eq $dummy_commit_msg);
612 my ($sha1, $when) = (
614 $ci->{'author_epoch'}
618 foreach my $detail (@{ $ci->{'details'} }) {
619 my $file = $detail->{'file'};
620 my $efile = uri_escape_utf8($file);
622 my $diffurl = defined $config{'diffurl'} ? $config{'diffurl'} : "";
623 $diffurl =~ s/\[\[file\]\]/$efile/go;
624 $diffurl =~ s/\[\[sha1_parent\]\]/$ci->{'parent'}/go;
625 $diffurl =~ s/\[\[sha1_from\]\]/$detail->{'sha1_from'}/go;
626 $diffurl =~ s/\[\[sha1_to\]\]/$detail->{'sha1_to'}/go;
627 $diffurl =~ s/\[\[sha1_commit\]\]/$sha1/go;
630 page => pagename($file),
637 foreach my $line (@{$ci->{'comment'}}) {
638 $pastblank=1 if $line eq '';
639 next if $pastblank && $line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i;
640 push @messages, { line => $line };
643 my $user=$ci->{'author_username'};
644 my $web_commit = ($ci->{'author'} =~ /\@web>/);
647 # Set nickname only if a non-url author_username is available,
648 # and author_name is an url.
649 if ($user !~ /:\/\// && defined $ci->{'author_name'} &&
650 $ci->{'author_name'} =~ /:\/\//) {
652 $user=$ci->{'author_name'};
655 # compatability code for old web commit messages
657 defined $messages[0] &&
658 $messages[0]->{line} =~ m/$config{web_commit_regexp}/) {
659 $user = defined $2 ? "$2" : "$3";
660 $messages[0]->{line} = $4;
667 nickname => $nickname,
668 committype => $web_commit ? "web" : "git",
670 message => [@messages],
674 last if @rets >= $num;
683 my ($sha1) = $rev =~ /^($sha1_pattern)$/; # untaint
687 return if defined $maxlines && @lines == $maxlines;
688 push @lines, $line."\n"
689 if (@lines || $line=~/^diff --git/);
692 safe_git(undef, $addlines, "git", "show", $sha1);
697 return join("", @lines);
706 my $id=shift; # 0 = mtime ; 1 = ctime
708 if (! keys %time_cache) {
710 foreach my $line (run_or_die('git', 'log',
711 '--pretty=format:%at',
712 '--name-only', '--relative')) {
713 if (! defined $date && $line =~ /^(\d+)$/) {
716 elsif (! length $line) {
720 my $f=decode_git_file($line);
722 if (! $time_cache{$f}) {
723 $time_cache{$f}[0]=$date; # mtime
725 $time_cache{$f}[1]=$date; # ctime
730 return exists $time_cache{$file} ? $time_cache{$file}[$id] : 0;
735 sub rcs_getctime ($) {
738 return findtimes($file, 1);
741 sub rcs_getmtime ($) {
744 return findtimes($file, 0);
750 # The wiki may not be the only thing in the git repo.
751 # Determine if it is in a subdirectory by examining the srcdir,
752 # and its parents, looking for the .git directory.
754 return @$ret if defined $ret;
757 my $dir=$config{srcdir};
758 while (! -d "$dir/.git") {
759 $subdir=IkiWiki::basename($dir)."/".$subdir;
760 $dir=IkiWiki::dirname($dir);
762 error("cannot determine root of git repo");
766 $ret=[$subdir, $dir];
772 sub git_parse_changes {
773 my $reverted = shift;
776 my ($subdir, $rootdir) = git_find_root();
778 foreach my $ci (@changes) {
779 foreach my $detail (@{ $ci->{'details'} }) {
780 my $file = $detail->{'file'};
782 # check that all changed files are in the subdir
783 if (length $subdir &&
784 ! ($file =~ s/^\Q$subdir\E//)) {
785 error sprintf(gettext("you are not allowed to change %s"), $file);
788 my ($action, $mode, $path);
789 if ($detail->{'status'} =~ /^[M]+\d*$/) {
791 $mode=$detail->{'mode_to'};
793 elsif ($detail->{'status'} =~ /^[AM]+\d*$/) {
794 $action= $reverted ? "remove" : "add";
795 $mode=$detail->{'mode_to'};
797 elsif ($detail->{'status'} =~ /^[DAM]+\d*/) {
798 $action= $reverted ? "add" : "remove";
799 $mode=$detail->{'mode_from'};
802 error "unknown status ".$detail->{'status'};
805 # test that the file mode is ok
806 if ($mode !~ /^100[64][64][64]$/) {
807 error sprintf(gettext("you cannot act on a file with mode %s"), $mode);
809 if ($action eq "change") {
810 if ($detail->{'mode_from'} ne $detail->{'mode_to'}) {
811 error gettext("you are not allowed to change file modes");
815 # extract attachment to temp file
816 if (($action eq 'add' || $action eq 'change') &&
818 eval q{use File::Temp};
821 ($fh, $path)=File::Temp::tempfile(undef, UNLINK => 1);
822 my $cmd = "cd $git_dir && ".
823 "git show $detail->{sha1_to} > '$path'";
824 if (system($cmd) != 0) {
825 error("failed writing temp file '$path'.");
844 my ($oldrev, $newrev, $refname) = split(' ', $_, 3);
846 # only allow changes to gitmaster_branch
847 if ($refname !~ /^refs\/heads\/\Q$config{gitmaster_branch}\E$/) {
848 error sprintf(gettext("you are not allowed to change %s"), $refname);
851 # Avoid chdir when running git here, because the changes
852 # are in the master git repo, not the srcdir repo.
853 # (Also, if a subdir is involved, we don't want to chdir to
854 # it and only see changes in it.)
855 # The pre-receive hook already puts us in the right place.
856 in_git_dir(".", sub {
857 push @rets, git_parse_changes(0, git_commit_info($oldrev."..".$newrev));
861 return reverse @rets;
864 sub rcs_preprevert ($) {
866 my ($sha1) = $rev =~ /^($sha1_pattern)$/; # untaint
868 # Examine changes from root of git repo, not from any subdir,
869 # in order to see all changes.
870 my ($subdir, $rootdir) = git_find_root();
871 in_git_dir($rootdir, sub {
872 my @commits=git_commit_info($sha1, 1);
875 error "unknown commit"; # just in case
878 # git revert will fail on merge commits. Add a nice message.
879 if (exists $commits[0]->{parents} &&
880 @{$commits[0]->{parents}} > 1) {
881 error gettext("you are not allowed to revert a merge");
884 git_parse_changes(1, @commits);
889 # Try to revert the given rev; returns undef on _success_.
891 my ($sha1) = $rev =~ /^($sha1_pattern)$/; # untaint
893 if (run_or_non('git', 'revert', '--no-commit', $sha1)) {
897 run_or_die('git', 'reset', '--hard');
898 return sprintf(gettext("Failed to revert commit %s"), $sha1);