2006-06-02 07:32:20 +02:00
|
|
|
#!/usr/bin/perl
|
2008-07-27 04:27:58 +02:00
|
|
|
package IkiWiki::Plugin::git;
|
2008-07-11 12:07:48 +02:00
|
|
|
|
2006-06-02 07:32:20 +02:00
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
use IkiWiki;
|
2006-07-05 22:54:10 +02:00
|
|
|
use Encode;
|
2012-03-13 16:49:29 +01:00
|
|
|
use URI::Escape q{uri_escape_utf8};
|
2006-07-05 22:54:10 +02:00
|
|
|
use open qw{:utf8 :std};
|
2006-06-02 07:32:20 +02:00
|
|
|
|
|
|
|
my $sha1_pattern = qr/[0-9a-fA-F]{40}/; # pattern to validate Git sha1sums
|
2006-06-03 20:03:41 +02:00
|
|
|
my $dummy_commit_msg = 'dummy commit'; # message to skip in recent changes
|
2006-06-02 07:32:20 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub import {
|
2008-07-27 04:27:58 +02:00
|
|
|
hook(type => "checkconfig", id => "git", call => \&checkconfig);
|
|
|
|
hook(type => "getsetup", id => "git", call => \&getsetup);
|
2009-09-10 22:09:19 +02:00
|
|
|
hook(type => "genwrapper", id => "git", call => \&genwrapper);
|
2008-07-27 04:27:58 +02:00
|
|
|
hook(type => "rcs", id => "rcs_update", call => \&rcs_update);
|
|
|
|
hook(type => "rcs", id => "rcs_prepedit", call => \&rcs_prepedit);
|
|
|
|
hook(type => "rcs", id => "rcs_commit", call => \&rcs_commit);
|
|
|
|
hook(type => "rcs", id => "rcs_commit_staged", call => \&rcs_commit_staged);
|
|
|
|
hook(type => "rcs", id => "rcs_add", call => \&rcs_add);
|
|
|
|
hook(type => "rcs", id => "rcs_remove", call => \&rcs_remove);
|
|
|
|
hook(type => "rcs", id => "rcs_rename", call => \&rcs_rename);
|
|
|
|
hook(type => "rcs", id => "rcs_recentchanges", call => \&rcs_recentchanges);
|
|
|
|
hook(type => "rcs", id => "rcs_diff", call => \&rcs_diff);
|
|
|
|
hook(type => "rcs", id => "rcs_getctime", call => \&rcs_getctime);
|
2010-04-16 23:02:29 +02:00
|
|
|
hook(type => "rcs", id => "rcs_getmtime", call => \&rcs_getmtime);
|
2008-10-23 22:29:50 +02:00
|
|
|
hook(type => "rcs", id => "rcs_receive", call => \&rcs_receive);
|
2010-10-04 22:33:36 +02:00
|
|
|
hook(type => "rcs", id => "rcs_preprevert", call => \&rcs_preprevert);
|
2010-09-29 07:14:19 +02:00
|
|
|
hook(type => "rcs", id => "rcs_revert", call => \&rcs_revert);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-07-27 04:27:58 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub checkconfig () {
|
2008-07-27 00:29:33 +02:00
|
|
|
if (! defined $config{gitorigin_branch}) {
|
|
|
|
$config{gitorigin_branch}="origin";
|
|
|
|
}
|
|
|
|
if (! defined $config{gitmaster_branch}) {
|
|
|
|
$config{gitmaster_branch}="master";
|
|
|
|
}
|
2008-10-23 02:52:34 +02:00
|
|
|
if (defined $config{git_wrapper} &&
|
|
|
|
length $config{git_wrapper}) {
|
2008-07-27 03:00:11 +02:00
|
|
|
push @{$config{wrappers}}, {
|
|
|
|
wrapper => $config{git_wrapper},
|
|
|
|
wrappermode => (defined $config{git_wrappermode} ? $config{git_wrappermode} : "06755"),
|
2010-07-01 22:20:03 +02:00
|
|
|
wrapper_background_command => $config{git_wrapper_background_command},
|
2008-07-27 03:00:11 +02:00
|
|
|
};
|
|
|
|
}
|
2009-09-10 22:09:19 +02:00
|
|
|
|
2008-10-23 02:52:34 +02:00
|
|
|
if (defined $config{git_test_receive_wrapper} &&
|
2010-07-26 19:30:45 +02:00
|
|
|
length $config{git_test_receive_wrapper} &&
|
|
|
|
defined $config{untrusted_committers} &&
|
|
|
|
@{$config{untrusted_committers}}) {
|
2008-10-23 02:52:34 +02:00
|
|
|
push @{$config{wrappers}}, {
|
|
|
|
test_receive => 1,
|
|
|
|
wrapper => $config{git_test_receive_wrapper},
|
2008-10-24 21:47:42 +02:00
|
|
|
wrappermode => (defined $config{git_wrappermode} ? $config{git_wrappermode} : "06755"),
|
2008-10-23 02:52:34 +02:00
|
|
|
};
|
|
|
|
}
|
2010-01-20 05:42:04 +01:00
|
|
|
|
|
|
|
# Avoid notes, parser does not handle and they only slow things down.
|
|
|
|
$ENV{GIT_NOTES_REF}="";
|
2009-09-10 22:09:19 +02:00
|
|
|
|
|
|
|
# Run receive test only if being called by the wrapper, and not
|
|
|
|
# when generating same.
|
|
|
|
if ($config{test_receive} && ! exists $config{wrapper}) {
|
|
|
|
require IkiWiki::Receive;
|
|
|
|
IkiWiki::Receive::test();
|
|
|
|
}
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-07-27 00:29:33 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub getsetup () {
|
2008-07-27 00:10:01 +02:00
|
|
|
return
|
2008-08-03 22:40:12 +02:00
|
|
|
plugin => {
|
|
|
|
safe => 0, # rcs plugin
|
|
|
|
rebuild => undef,
|
2010-02-12 04:24:15 +01:00
|
|
|
section => "rcs",
|
2008-08-03 22:40:12 +02:00
|
|
|
},
|
2008-07-27 03:00:11 +02:00
|
|
|
git_wrapper => {
|
|
|
|
type => "string",
|
|
|
|
example => "/git/wiki.git/hooks/post-update",
|
2008-09-11 21:53:25 +02:00
|
|
|
description => "git hook to generate",
|
2008-07-27 03:00:11 +02:00
|
|
|
safe => 0, # file
|
|
|
|
rebuild => 0,
|
|
|
|
},
|
2010-07-01 22:20:03 +02:00
|
|
|
git_wrapper_background_command => {
|
|
|
|
type => "string",
|
|
|
|
example => "git push github",
|
|
|
|
description => "shell command for git_wrapper to run, in the background",
|
|
|
|
safe => 0, # command
|
|
|
|
rebuild => 0,
|
|
|
|
},
|
2008-07-27 03:00:11 +02:00
|
|
|
git_wrappermode => {
|
|
|
|
type => "string",
|
|
|
|
example => '06755',
|
|
|
|
description => "mode for git_wrapper (can safely be made suid)",
|
|
|
|
safe => 0,
|
|
|
|
rebuild => 0,
|
|
|
|
},
|
2008-10-23 02:52:34 +02:00
|
|
|
git_test_receive_wrapper => {
|
|
|
|
type => "string",
|
|
|
|
example => "/git/wiki.git/hooks/pre-receive",
|
|
|
|
description => "git pre-receive hook to generate",
|
|
|
|
safe => 0, # file
|
|
|
|
rebuild => 0,
|
|
|
|
},
|
2008-10-23 22:29:50 +02:00
|
|
|
untrusted_committers => {
|
2008-10-23 02:52:34 +02:00
|
|
|
type => "string",
|
|
|
|
example => [],
|
|
|
|
description => "unix users whose commits should be checked by the pre-receive hook",
|
|
|
|
safe => 0,
|
|
|
|
rebuild => 0,
|
|
|
|
},
|
2008-07-27 00:10:01 +02:00
|
|
|
historyurl => {
|
|
|
|
type => "string",
|
2010-07-24 12:18:19 +02:00
|
|
|
example => "http://git.example.com/gitweb.cgi?p=wiki.git;a=history;f=[[file]];hb=HEAD",
|
2008-07-27 00:10:01 +02:00
|
|
|
description => "gitweb url to show file history ([[file]] substituted)",
|
|
|
|
safe => 1,
|
|
|
|
rebuild => 1,
|
|
|
|
},
|
|
|
|
diffurl => {
|
|
|
|
type => "string",
|
2009-01-06 00:26:09 +01:00
|
|
|
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]]",
|
|
|
|
description => "gitweb url to show a diff ([[file]], [[sha1_to]], [[sha1_from]], [[sha1_commit]], and [[sha1_parent]] substituted)",
|
2008-07-27 00:10:01 +02:00
|
|
|
safe => 1,
|
|
|
|
rebuild => 1,
|
|
|
|
},
|
|
|
|
gitorigin_branch => {
|
|
|
|
type => "string",
|
2008-07-27 03:00:11 +02:00
|
|
|
example => "origin",
|
2008-07-27 00:33:18 +02:00
|
|
|
description => "where to pull and push changes (set to empty string to disable)",
|
2008-07-27 00:10:01 +02:00
|
|
|
safe => 0, # paranoia
|
|
|
|
rebuild => 0,
|
|
|
|
},
|
|
|
|
gitmaster_branch => {
|
|
|
|
type => "string",
|
2008-07-27 03:00:11 +02:00
|
|
|
example => "master",
|
2008-07-27 00:10:01 +02:00
|
|
|
description => "branch that the wiki is stored in",
|
|
|
|
safe => 0, # paranoia
|
|
|
|
rebuild => 0,
|
|
|
|
},
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-07-27 00:10:01 +02:00
|
|
|
|
2009-09-10 22:09:19 +02:00
|
|
|
sub genwrapper {
|
|
|
|
if ($config{test_receive}) {
|
|
|
|
require IkiWiki::Receive;
|
|
|
|
return IkiWiki::Receive::genwrapper();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-07 07:59:05 +01:00
|
|
|
my $git_dir=undef;
|
|
|
|
my $prefix=undef;
|
|
|
|
|
|
|
|
sub in_git_dir ($$) {
|
|
|
|
$git_dir=shift;
|
|
|
|
my @ret=shift->();
|
|
|
|
$git_dir=undef;
|
|
|
|
$prefix=undef;
|
|
|
|
return @ret;
|
|
|
|
}
|
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub safe_git (&@) {
|
2010-12-30 00:58:49 +01:00
|
|
|
# Start a child process safely without resorting to /bin/sh.
|
|
|
|
# Returns command output (in list content) or success state
|
|
|
|
# (in scalar context), or runs the specified data handler.
|
2006-06-02 07:32:20 +02:00
|
|
|
|
2010-12-30 00:58:49 +01:00
|
|
|
my ($error_handler, $data_handler, @cmdline) = @_;
|
2006-06-02 07:32:20 +02:00
|
|
|
|
|
|
|
my $pid = open my $OUT, "-|";
|
|
|
|
|
|
|
|
error("Cannot fork: $!") if !defined $pid;
|
|
|
|
|
|
|
|
if (!$pid) {
|
|
|
|
# In child.
|
|
|
|
# Git commands want to be in wc.
|
2010-10-23 22:19:16 +02:00
|
|
|
if (! defined $git_dir) {
|
2008-10-23 22:56:40 +02:00
|
|
|
chdir $config{srcdir}
|
2010-10-23 22:19:16 +02:00
|
|
|
or error("cannot chdir to $config{srcdir}: $!");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
chdir $git_dir
|
|
|
|
or error("cannot chdir to $git_dir: $!");
|
2008-10-23 22:56:40 +02:00
|
|
|
}
|
2006-06-02 07:32:20 +02:00
|
|
|
exec @cmdline or error("Cannot exec '@cmdline': $!");
|
|
|
|
}
|
|
|
|
# In parent.
|
|
|
|
|
2009-03-09 19:18:55 +01:00
|
|
|
# git output is probably utf-8 encoded, but may contain
|
|
|
|
# other encodings or invalidly encoded stuff. So do not rely
|
|
|
|
# on the normal utf-8 IO layer, decode it by hand.
|
|
|
|
binmode($OUT);
|
|
|
|
|
2006-06-02 07:32:20 +02:00
|
|
|
my @lines;
|
|
|
|
while (<$OUT>) {
|
2009-03-09 19:18:55 +01:00
|
|
|
$_=decode_utf8($_, 0);
|
|
|
|
|
2006-06-02 07:32:20 +02:00
|
|
|
chomp;
|
2009-01-26 05:13:14 +01:00
|
|
|
|
2010-12-30 00:58:49 +01:00
|
|
|
if (! defined $data_handler) {
|
|
|
|
push @lines, $_;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
last unless $data_handler->($_);
|
|
|
|
}
|
2006-06-02 07:32:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
close $OUT;
|
|
|
|
|
2007-10-11 01:27:11 +02:00
|
|
|
$error_handler->("'@cmdline' failed: $!") if $? && $error_handler;
|
2006-06-02 07:32:20 +02:00
|
|
|
|
|
|
|
return wantarray ? @lines : ($? == 0);
|
|
|
|
}
|
|
|
|
# Convenient wrappers.
|
2010-12-30 00:58:49 +01:00
|
|
|
sub run_or_die ($@) { safe_git(\&error, undef, @_) }
|
|
|
|
sub run_or_cry ($@) { safe_git(sub { warn @_ }, undef, @_) }
|
|
|
|
sub run_or_non ($@) { safe_git(undef, undef, @_) }
|
2006-06-02 07:32:20 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
|
|
|
|
sub merge_past ($$$) {
|
2006-06-02 07:32:20 +02:00
|
|
|
# Unlike with Subversion, Git cannot make a 'svn merge -rN:M file'.
|
|
|
|
# Git merge commands work with the committed changes, except in the
|
2007-10-31 22:37:33 +01:00
|
|
|
# implicit case of '-m' of git checkout(1). So we should invent a
|
2006-06-02 07:32:20 +02:00
|
|
|
# kludge here. In principle, we need to create a throw-away branch
|
|
|
|
# in preparing for the merge itself. Since branches are cheap (and
|
|
|
|
# branching is fast), this shouldn't cost high.
|
|
|
|
#
|
|
|
|
# The main problem is the presence of _uncommitted_ local changes. One
|
|
|
|
# possible approach to get rid of this situation could be that we first
|
|
|
|
# make a temporary commit in the master branch and later restore the
|
|
|
|
# initial state (this is possible since Git has the ability to undo a
|
2007-10-31 22:37:33 +01:00
|
|
|
# commit, i.e. 'git reset --soft HEAD^'). The method can be summarized
|
2006-06-02 07:32:20 +02:00
|
|
|
# as follows:
|
|
|
|
#
|
|
|
|
# - create a diff of HEAD:current-sha1
|
|
|
|
# - dummy commit
|
|
|
|
# - create a dummy branch and switch to it
|
|
|
|
# - rewind to past (reset --hard to the current-sha1)
|
|
|
|
# - apply the diff and commit
|
|
|
|
# - switch to master and do the merge with the dummy branch
|
|
|
|
# - make a soft reset (undo the last commit of master)
|
|
|
|
#
|
|
|
|
# The above method has some drawbacks: (1) it needs a redundant commit
|
|
|
|
# just to get rid of local changes, (2) somewhat slow because of the
|
|
|
|
# required system forks. Until someone points a more straight method
|
|
|
|
# (which I would be grateful) I have implemented an alternative method.
|
|
|
|
# In this approach, we hide all the modified files from Git by renaming
|
|
|
|
# them (using the 'rename' builtin) and later restore those files in
|
|
|
|
# the throw-away branch (that is, we put the files themselves instead
|
|
|
|
# of applying a patch).
|
|
|
|
|
|
|
|
my ($sha1, $file, $message) = @_;
|
|
|
|
|
|
|
|
my @undo; # undo stack for cleanup in case of an error
|
|
|
|
my $conflict; # file content with conflict markers
|
|
|
|
|
|
|
|
eval {
|
|
|
|
# Hide local changes from Git by renaming the modified file.
|
|
|
|
# Relative paths must be converted to absolute for renaming.
|
|
|
|
my ($target, $hidden) = (
|
|
|
|
"$config{srcdir}/${file}", "$config{srcdir}/${file}.${sha1}"
|
|
|
|
);
|
|
|
|
rename($target, $hidden)
|
|
|
|
or error("rename '$target' to '$hidden' failed: $!");
|
|
|
|
# Ensure to restore the renamed file on error.
|
|
|
|
push @undo, sub {
|
|
|
|
return if ! -e "$hidden"; # already renamed
|
|
|
|
rename($hidden, $target)
|
2006-06-03 20:03:41 +02:00
|
|
|
or warn "rename '$hidden' to '$target' failed: $!";
|
2006-06-02 07:32:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
my $branch = "throw_away_${sha1}"; # supposed to be unique
|
|
|
|
|
|
|
|
# Create a throw-away branch and rewind backward.
|
2007-10-31 22:37:33 +01:00
|
|
|
push @undo, sub { run_or_cry('git', 'branch', '-D', $branch) };
|
|
|
|
run_or_die('git', 'branch', $branch, $sha1);
|
2006-06-02 07:32:20 +02:00
|
|
|
|
|
|
|
# Switch to throw-away branch for the merge operation.
|
|
|
|
push @undo, sub {
|
2007-10-31 22:37:33 +01:00
|
|
|
if (!run_or_cry('git', 'checkout', $config{gitmaster_branch})) {
|
|
|
|
run_or_cry('git', 'checkout','-f',$config{gitmaster_branch});
|
2006-06-02 07:32:20 +02:00
|
|
|
}
|
|
|
|
};
|
2007-10-31 22:37:33 +01:00
|
|
|
run_or_die('git', 'checkout', $branch);
|
2006-06-02 07:32:20 +02:00
|
|
|
|
|
|
|
# Put the modified file in _this_ branch.
|
|
|
|
rename($hidden, $target)
|
|
|
|
or error("rename '$hidden' to '$target' failed: $!");
|
|
|
|
|
|
|
|
# _Silently_ commit all modifications in the current branch.
|
2007-10-31 22:37:33 +01:00
|
|
|
run_or_non('git', 'commit', '-m', $message, '-a');
|
2006-06-02 07:32:20 +02:00
|
|
|
# ... and re-switch to master.
|
2007-10-31 22:37:33 +01:00
|
|
|
run_or_die('git', 'checkout', $config{gitmaster_branch});
|
2006-06-02 07:32:20 +02:00
|
|
|
|
|
|
|
# Attempt to merge without complaining.
|
2007-10-31 22:37:33 +01:00
|
|
|
if (!run_or_non('git', 'pull', '--no-commit', '.', $branch)) {
|
2006-06-02 07:32:20 +02:00
|
|
|
$conflict = readfile($target);
|
2007-10-31 22:37:33 +01:00
|
|
|
run_or_die('git', 'reset', '--hard');
|
2006-06-02 07:32:20 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
my $failure = $@;
|
|
|
|
|
|
|
|
# Process undo stack (in reverse order). By policy cleanup
|
|
|
|
# actions should normally print a warning on failure.
|
|
|
|
while (my $handle = pop @undo) {
|
|
|
|
$handle->();
|
|
|
|
}
|
|
|
|
|
|
|
|
error("Git merge failed!\n$failure\n") if $failure;
|
|
|
|
|
|
|
|
return $conflict;
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-06-02 07:32:20 +02:00
|
|
|
|
2010-06-16 05:21:55 +02:00
|
|
|
sub decode_git_file ($) {
|
|
|
|
my $file=shift;
|
|
|
|
|
|
|
|
# git does not output utf-8 filenames, but instead
|
|
|
|
# double-quotes them with the utf-8 characters
|
|
|
|
# escaped as \nnn\nnn.
|
|
|
|
if ($file =~ m/^"(.*)"$/) {
|
|
|
|
($file=$1) =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
|
|
|
|
}
|
|
|
|
|
|
|
|
# strip prefix if in a subdir
|
|
|
|
if (! defined $prefix) {
|
2010-06-16 22:07:41 +02:00
|
|
|
($prefix) = run_or_die('git', 'rev-parse', '--show-prefix');
|
2010-06-16 05:21:55 +02:00
|
|
|
if (! defined $prefix) {
|
|
|
|
$prefix="";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$file =~ s/^\Q$prefix\E//;
|
|
|
|
|
|
|
|
return decode("utf8", $file);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub parse_diff_tree ($) {
|
2006-06-02 07:32:20 +02:00
|
|
|
# Parse the raw diff tree chunk and return the info hash.
|
|
|
|
# See git-diff-tree(1) for the syntax.
|
2010-06-16 05:21:55 +02:00
|
|
|
my $dt_ref = shift;
|
2006-06-02 07:32:20 +02:00
|
|
|
|
|
|
|
# End of stream?
|
2012-12-17 21:44:54 +01:00
|
|
|
return if ! @{ $dt_ref } ||
|
|
|
|
!defined $dt_ref->[0] || !length $dt_ref->[0];
|
2006-06-02 07:32:20 +02:00
|
|
|
|
|
|
|
my %ci;
|
|
|
|
# Header line.
|
2007-08-24 20:07:15 +02:00
|
|
|
while (my $line = shift @{ $dt_ref }) {
|
2006-06-19 05:33:23 +02:00
|
|
|
return if $line !~ m/^(.+) ($sha1_pattern)/;
|
2006-06-02 07:32:20 +02:00
|
|
|
|
2006-11-26 21:05:57 +01:00
|
|
|
my $sha1 = $2;
|
2006-06-02 07:32:20 +02:00
|
|
|
$ci{'sha1'} = $sha1;
|
2007-08-24 20:07:15 +02:00
|
|
|
last;
|
2006-06-02 07:32:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Identification lines for the commit.
|
2007-08-24 20:07:15 +02:00
|
|
|
while (my $line = shift @{ $dt_ref }) {
|
2006-06-02 07:32:20 +02:00
|
|
|
# Regexps are semi-stolen from gitweb.cgi.
|
2007-10-31 23:42:14 +01:00
|
|
|
if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
|
2006-06-02 07:32:20 +02:00
|
|
|
$ci{'tree'} = $1;
|
2006-11-26 21:07:00 +01:00
|
|
|
}
|
|
|
|
elsif ($line =~ m/^parent ([0-9a-fA-F]{40})$/) {
|
2006-06-02 07:32:20 +02:00
|
|
|
# XXX: collecting in reverse order
|
|
|
|
push @{ $ci{'parents'} }, $1;
|
2006-11-26 21:07:00 +01:00
|
|
|
}
|
|
|
|
elsif ($line =~ m/^(author|committer) (.*) ([0-9]+) (.*)$/) {
|
2006-06-02 07:32:20 +02:00
|
|
|
my ($who, $name, $epoch, $tz) =
|
|
|
|
($1, $2, $3, $4 );
|
|
|
|
|
|
|
|
$ci{ $who } = $name;
|
|
|
|
$ci{ "${who}_epoch" } = $epoch;
|
|
|
|
$ci{ "${who}_tz" } = $tz;
|
|
|
|
|
2010-06-23 21:32:27 +02:00
|
|
|
if ($name =~ m/^([^<]+)\s+<([^@>]+)/) {
|
|
|
|
$ci{"${who}_name"} = $1;
|
|
|
|
$ci{"${who}_username"} = $2;
|
2008-07-17 22:03:16 +02:00
|
|
|
}
|
|
|
|
elsif ($name =~ m/^([^<]+)\s+<>$/) {
|
|
|
|
$ci{"${who}_username"} = $1;
|
2006-11-26 21:07:00 +01:00
|
|
|
}
|
|
|
|
else {
|
2008-07-17 22:03:16 +02:00
|
|
|
$ci{"${who}_username"} = $name;
|
2006-06-02 07:32:20 +02:00
|
|
|
}
|
2006-11-26 21:07:00 +01:00
|
|
|
}
|
|
|
|
elsif ($line =~ m/^$/) {
|
2006-06-02 07:32:20 +02:00
|
|
|
# Trailing empty line signals next section.
|
2007-08-24 20:07:15 +02:00
|
|
|
last;
|
2006-06-02 07:32:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-12 18:53:46 +01:00
|
|
|
debug("No 'tree' seen in diff-tree output") if !defined $ci{'tree'};
|
2008-07-17 22:53:54 +02:00
|
|
|
|
2007-11-12 18:53:46 +01:00
|
|
|
if (defined $ci{'parents'}) {
|
|
|
|
$ci{'parent'} = @{ $ci{'parents'} }[0];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$ci{'parent'} = 0 x 40;
|
|
|
|
}
|
2006-06-02 07:32:20 +02:00
|
|
|
|
2008-07-17 22:53:54 +02:00
|
|
|
# Commit message (optional).
|
|
|
|
while ($dt_ref->[0] =~ /^ /) {
|
|
|
|
my $line = shift @{ $dt_ref };
|
2006-06-02 07:32:20 +02:00
|
|
|
$line =~ s/^ //;
|
|
|
|
push @{ $ci{'comment'} }, $line;
|
|
|
|
}
|
2008-07-17 22:53:54 +02:00
|
|
|
shift @{ $dt_ref } if $dt_ref->[0] =~ /^$/;
|
2006-06-02 07:32:20 +02:00
|
|
|
|
|
|
|
# Modified files.
|
2007-08-24 20:07:15 +02:00
|
|
|
while (my $line = shift @{ $dt_ref }) {
|
2008-01-17 13:42:34 +01:00
|
|
|
if ($line =~ m{^
|
|
|
|
(:+) # number of parents
|
|
|
|
([^\t]+)\t # modes, sha1, status
|
|
|
|
(.*) # file names
|
2006-06-02 07:32:20 +02:00
|
|
|
$}xo) {
|
2008-01-17 13:42:34 +01:00
|
|
|
my $num_parents = length $1;
|
|
|
|
my @tmp = split(" ", $2);
|
|
|
|
my ($file, $file_to) = split("\t", $3);
|
|
|
|
my @mode_from = splice(@tmp, 0, $num_parents);
|
|
|
|
my $mode_to = shift(@tmp);
|
|
|
|
my @sha1_from = splice(@tmp, 0, $num_parents);
|
|
|
|
my $sha1_to = shift(@tmp);
|
|
|
|
my $status = shift(@tmp);
|
2006-06-02 07:32:20 +02:00
|
|
|
|
|
|
|
if (length $file) {
|
|
|
|
push @{ $ci{'details'} }, {
|
2010-06-16 05:21:55 +02:00
|
|
|
'file' => decode_git_file($file),
|
2008-01-17 13:42:34 +01:00
|
|
|
'sha1_from' => $sha1_from[0],
|
2006-06-02 07:32:20 +02:00
|
|
|
'sha1_to' => $sha1_to,
|
2008-10-23 02:52:34 +02:00
|
|
|
'mode_from' => $mode_from[0],
|
|
|
|
'mode_to' => $mode_to,
|
|
|
|
'status' => $status,
|
2006-06-02 07:32:20 +02:00
|
|
|
};
|
|
|
|
}
|
2007-08-24 20:07:15 +02:00
|
|
|
next;
|
2006-06-02 07:32:20 +02:00
|
|
|
};
|
2007-08-24 20:07:15 +02:00
|
|
|
last;
|
2006-06-02 07:32:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return \%ci;
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-06-02 07:32:20 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub git_commit_info ($;$) {
|
2008-10-23 02:52:34 +02:00
|
|
|
# Return an array of commit info hashes of num commits
|
2006-11-26 21:05:57 +01:00
|
|
|
# starting from the given sha1sum.
|
2006-06-02 07:32:20 +02:00
|
|
|
my ($sha1, $num) = @_;
|
|
|
|
|
2008-10-24 00:05:57 +02:00
|
|
|
my @opts;
|
|
|
|
push @opts, "--max-count=$num" if defined $num;
|
|
|
|
|
|
|
|
my @raw_lines = run_or_die('git', 'log', @opts,
|
2007-10-31 23:32:22 +01:00
|
|
|
'--pretty=raw', '--raw', '--abbrev=40', '--always', '-c',
|
2007-10-22 00:59:18 +02:00
|
|
|
'-r', $sha1, '--', '.');
|
2006-06-02 07:32:20 +02:00
|
|
|
|
|
|
|
my @ci;
|
2010-06-16 05:21:55 +02:00
|
|
|
while (my $parsed = parse_diff_tree(\@raw_lines)) {
|
2006-06-02 07:32:20 +02:00
|
|
|
push @ci, $parsed;
|
|
|
|
}
|
|
|
|
|
2006-06-19 05:33:23 +02:00
|
|
|
warn "Cannot parse commit info for '$sha1' commit" if !@ci;
|
|
|
|
|
2006-06-02 07:32:20 +02:00
|
|
|
return wantarray ? @ci : $ci[0];
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-06-02 07:32:20 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub git_sha1 (;$) {
|
2006-06-02 07:32:20 +02:00
|
|
|
# Return head sha1sum (of given file).
|
|
|
|
my $file = shift || q{--};
|
|
|
|
|
2006-06-03 20:03:41 +02:00
|
|
|
# Ignore error since a non-existing file might be given.
|
2008-05-02 19:03:42 +02:00
|
|
|
my ($sha1) = run_or_non('git', 'rev-list', '--max-count=1', 'HEAD',
|
|
|
|
'--', $file);
|
2012-03-22 18:06:08 +01:00
|
|
|
if (defined $sha1) {
|
2006-06-03 20:03:41 +02:00
|
|
|
($sha1) = $sha1 =~ m/($sha1_pattern)/; # sha1 is untainted now
|
2010-01-18 18:34:19 +01:00
|
|
|
}
|
2012-03-22 18:06:08 +01:00
|
|
|
return defined $sha1 ? $sha1 : '';
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-06-02 07:32:20 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub rcs_update () {
|
2006-06-02 07:32:20 +02:00
|
|
|
# Update working directory.
|
|
|
|
|
2007-10-22 03:47:30 +02:00
|
|
|
if (length $config{gitorigin_branch}) {
|
2010-09-14 21:45:38 +02:00
|
|
|
run_or_cry('git', 'pull', '--prune', $config{gitorigin_branch});
|
2007-10-22 03:47:30 +02:00
|
|
|
}
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-06-02 07:32:20 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub rcs_prepedit ($) {
|
2006-06-02 07:32:20 +02:00
|
|
|
# Return the commit sha1sum of the file when editing begins.
|
|
|
|
# This will be later used in rcs_commit if a merge is required.
|
|
|
|
my ($file) = @_;
|
|
|
|
|
2006-06-03 20:03:41 +02:00
|
|
|
return git_sha1($file);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-06-02 07:32:20 +02:00
|
|
|
|
2010-06-23 23:35:21 +02:00
|
|
|
sub rcs_commit (@) {
|
2006-06-02 07:32:20 +02:00
|
|
|
# Try to commit the page; returns undef on _success_ and
|
|
|
|
# a version of the page with the rcs's conflict markers on
|
|
|
|
# failure.
|
2010-06-23 23:35:21 +02:00
|
|
|
my %params=@_;
|
2006-11-22 15:28:38 +01:00
|
|
|
|
2006-06-02 07:32:20 +02:00
|
|
|
# Check to see if the page has been changed by someone else since
|
|
|
|
# rcs_prepedit was called.
|
2010-06-23 23:35:21 +02:00
|
|
|
my $cur = git_sha1($params{file});
|
|
|
|
my ($prev) = $params{token} =~ /^($sha1_pattern)$/; # untaint
|
2006-06-02 07:32:20 +02:00
|
|
|
|
|
|
|
if (defined $cur && defined $prev && $cur ne $prev) {
|
2010-06-23 23:35:21 +02:00
|
|
|
my $conflict = merge_past($prev, $params{file}, $dummy_commit_msg);
|
2006-06-02 07:32:20 +02:00
|
|
|
return $conflict if defined $conflict;
|
|
|
|
}
|
2008-07-22 22:14:33 +02:00
|
|
|
|
2010-11-29 18:42:03 +01:00
|
|
|
return rcs_commit_helper(@_);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-07-22 22:14:33 +02:00
|
|
|
|
2010-06-23 23:35:21 +02:00
|
|
|
sub rcs_commit_staged (@) {
|
2008-07-22 22:14:33 +02:00
|
|
|
# Commits all staged changes. Changes can be staged using rcs_add,
|
|
|
|
# rcs_remove, and rcs_rename.
|
2010-11-29 18:42:03 +01:00
|
|
|
return rcs_commit_helper(@_);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub rcs_commit_helper (@) {
|
2010-06-23 23:35:21 +02:00
|
|
|
my %params=@_;
|
|
|
|
|
2008-07-17 22:03:16 +02:00
|
|
|
my %env=%ENV;
|
2010-06-23 23:35:21 +02:00
|
|
|
|
|
|
|
if (defined $params{session}) {
|
|
|
|
# Set the commit author and email based on web session info.
|
|
|
|
my $u;
|
|
|
|
if (defined $params{session}->param("name")) {
|
|
|
|
$u=$params{session}->param("name");
|
|
|
|
}
|
|
|
|
elsif (defined $params{session}->remote_addr()) {
|
|
|
|
$u=$params{session}->remote_addr();
|
|
|
|
}
|
|
|
|
if (defined $u) {
|
|
|
|
$u=encode_utf8($u);
|
|
|
|
$ENV{GIT_AUTHOR_NAME}=$u;
|
2010-06-24 01:44:41 +02:00
|
|
|
}
|
2010-06-24 02:26:09 +02:00
|
|
|
if (defined $params{session}->param("nickname")) {
|
|
|
|
$u=encode_utf8($params{session}->param("nickname"));
|
2010-07-04 22:44:38 +02:00
|
|
|
$u=~s/\s+/_/g;
|
|
|
|
$u=~s/[^-_0-9[:alnum:]]+//g;
|
2010-06-24 01:44:41 +02:00
|
|
|
}
|
|
|
|
if (defined $u) {
|
2010-06-23 23:35:21 +02:00
|
|
|
$ENV{GIT_AUTHOR_EMAIL}="$u\@web";
|
|
|
|
}
|
2008-07-17 22:03:16 +02:00
|
|
|
}
|
2006-06-02 07:32:20 +02:00
|
|
|
|
2010-06-23 23:35:21 +02:00
|
|
|
$params{message} = IkiWiki::possibly_foolish_untaint($params{message});
|
2008-08-01 01:23:54 +02:00
|
|
|
my @opts;
|
2010-06-23 23:35:21 +02:00
|
|
|
if ($params{message} !~ /\S/) {
|
2008-08-01 01:23:54 +02:00
|
|
|
# Force git to allow empty commit messages.
|
|
|
|
# (If this version of git supports it.)
|
|
|
|
my ($version)=`git --version` =~ /git version (.*)/;
|
|
|
|
if ($version ge "1.5.4") {
|
|
|
|
push @opts, '--cleanup=verbatim';
|
|
|
|
}
|
|
|
|
else {
|
2010-06-23 23:35:21 +02:00
|
|
|
$params{message}.=".";
|
2008-08-01 01:23:54 +02:00
|
|
|
}
|
|
|
|
}
|
2010-11-29 18:42:03 +01:00
|
|
|
if (exists $params{file}) {
|
|
|
|
push @opts, '--', $params{file};
|
|
|
|
}
|
|
|
|
# git commit returns non-zero if nothing really changed.
|
|
|
|
# So we should ignore its exit status (hence run_or_non).
|
|
|
|
if (run_or_non('git', 'commit', '-m', $params{message}, '-q', @opts)) {
|
2007-10-22 03:47:30 +02:00
|
|
|
if (length $config{gitorigin_branch}) {
|
2007-10-31 22:37:33 +01:00
|
|
|
run_or_cry('git', 'push', $config{gitorigin_branch});
|
2007-10-22 03:47:30 +02:00
|
|
|
}
|
2006-06-02 07:32:20 +02:00
|
|
|
}
|
2008-07-17 22:03:16 +02:00
|
|
|
|
|
|
|
%ENV=%env;
|
2006-06-02 07:32:20 +02:00
|
|
|
return undef; # success
|
2008-07-22 22:14:33 +02:00
|
|
|
}
|
2006-06-02 07:32:20 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub rcs_add ($) {
|
2006-06-02 07:32:20 +02:00
|
|
|
# Add file to archive.
|
|
|
|
|
|
|
|
my ($file) = @_;
|
|
|
|
|
2007-10-31 22:37:33 +01:00
|
|
|
run_or_cry('git', 'add', $file);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-06-02 07:32:20 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub rcs_remove ($) {
|
2008-07-21 19:40:06 +02:00
|
|
|
# Remove file from archive.
|
|
|
|
|
|
|
|
my ($file) = @_;
|
|
|
|
|
|
|
|
run_or_cry('git', 'rm', '-f', $file);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-07-21 19:40:06 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub rcs_rename ($$) {
|
2008-07-22 22:14:33 +02:00
|
|
|
my ($src, $dest) = @_;
|
|
|
|
|
|
|
|
run_or_cry('git', 'mv', '-f', $src, $dest);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-07-22 22:14:33 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub rcs_recentchanges ($) {
|
2006-06-02 07:32:20 +02:00
|
|
|
# List of recent changes.
|
|
|
|
|
|
|
|
my ($num) = @_;
|
|
|
|
|
|
|
|
eval q{use Date::Parse};
|
2006-11-08 22:03:33 +01:00
|
|
|
error($@) if $@;
|
2006-06-02 07:32:20 +02:00
|
|
|
|
2006-11-26 21:05:57 +01:00
|
|
|
my @rets;
|
2008-10-23 02:52:34 +02:00
|
|
|
foreach my $ci (git_commit_info('HEAD', $num || 1)) {
|
2006-06-02 07:32:20 +02:00
|
|
|
# Skip redundant commits.
|
2008-07-17 22:53:54 +02:00
|
|
|
next if ($ci->{'comment'} && @{$ci->{'comment'}}[0] eq $dummy_commit_msg);
|
2006-06-02 07:32:20 +02:00
|
|
|
|
2006-11-26 21:05:57 +01:00
|
|
|
my ($sha1, $when) = (
|
|
|
|
$ci->{'sha1'},
|
2008-01-29 04:57:22 +01:00
|
|
|
$ci->{'author_epoch'}
|
2006-11-26 21:05:57 +01:00
|
|
|
);
|
2006-06-02 07:32:20 +02:00
|
|
|
|
2008-07-17 22:03:16 +02:00
|
|
|
my @pages;
|
2007-08-24 20:07:15 +02:00
|
|
|
foreach my $detail (@{ $ci->{'details'} }) {
|
2007-10-22 00:59:18 +02:00
|
|
|
my $file = $detail->{'file'};
|
2012-03-13 16:49:29 +01:00
|
|
|
my $efile = uri_escape_utf8($file);
|
2006-06-02 07:32:20 +02:00
|
|
|
|
2008-07-27 06:54:15 +02:00
|
|
|
my $diffurl = defined $config{'diffurl'} ? $config{'diffurl'} : "";
|
2012-03-13 16:49:29 +01:00
|
|
|
$diffurl =~ s/\[\[file\]\]/$efile/go;
|
2006-06-02 07:32:20 +02:00
|
|
|
$diffurl =~ s/\[\[sha1_parent\]\]/$ci->{'parent'}/go;
|
2006-10-14 05:10:33 +02:00
|
|
|
$diffurl =~ s/\[\[sha1_from\]\]/$detail->{'sha1_from'}/go;
|
|
|
|
$diffurl =~ s/\[\[sha1_to\]\]/$detail->{'sha1_to'}/go;
|
2008-10-27 19:45:54 +01:00
|
|
|
$diffurl =~ s/\[\[sha1_commit\]\]/$sha1/go;
|
2006-06-02 07:32:20 +02:00
|
|
|
|
|
|
|
push @pages, {
|
2006-09-03 21:53:23 +02:00
|
|
|
page => pagename($file),
|
2006-06-02 07:32:20 +02:00
|
|
|
diffurl => $diffurl,
|
2006-10-14 05:10:33 +02:00
|
|
|
};
|
2006-06-02 07:32:20 +02:00
|
|
|
}
|
2008-05-16 00:03:44 +02:00
|
|
|
|
2008-07-17 22:03:16 +02:00
|
|
|
my @messages;
|
|
|
|
my $pastblank=0;
|
|
|
|
foreach my $line (@{$ci->{'comment'}}) {
|
|
|
|
$pastblank=1 if $line eq '';
|
|
|
|
next if $pastblank && $line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i;
|
|
|
|
push @messages, { line => $line };
|
|
|
|
}
|
2006-06-02 07:32:20 +02:00
|
|
|
|
2010-06-24 02:12:26 +02:00
|
|
|
my $user=$ci->{'author_username'};
|
2008-07-18 01:23:00 +02:00
|
|
|
my $web_commit = ($ci->{'author'} =~ /\@web>/);
|
2010-06-24 02:12:26 +02:00
|
|
|
my $nickname;
|
|
|
|
|
|
|
|
# Set nickname only if a non-url author_username is available,
|
|
|
|
# and author_name is an url.
|
|
|
|
if ($user !~ /:\/\// && defined $ci->{'author_name'} &&
|
|
|
|
$ci->{'author_name'} =~ /:\/\//) {
|
|
|
|
$nickname=$user;
|
|
|
|
$user=$ci->{'author_name'};
|
2010-06-23 21:32:27 +02:00
|
|
|
}
|
|
|
|
|
2008-07-17 22:03:16 +02:00
|
|
|
# compatability code for old web commit messages
|
2008-07-17 22:35:18 +02:00
|
|
|
if (! $web_commit &&
|
|
|
|
defined $messages[0] &&
|
|
|
|
$messages[0]->{line} =~ m/$config{web_commit_regexp}/) {
|
2006-11-26 21:07:00 +01:00
|
|
|
$user = defined $2 ? "$2" : "$3";
|
|
|
|
$messages[0]->{line} = $4;
|
2008-07-18 01:25:10 +02:00
|
|
|
$web_commit=1;
|
2006-11-26 21:07:00 +01:00
|
|
|
}
|
2006-06-02 07:32:20 +02:00
|
|
|
|
|
|
|
push @rets, {
|
2008-01-29 03:23:56 +01:00
|
|
|
rev => $sha1,
|
2006-09-03 21:53:23 +02:00
|
|
|
user => $user,
|
2010-06-24 02:12:26 +02:00
|
|
|
nickname => $nickname,
|
2008-07-17 22:03:16 +02:00
|
|
|
committype => $web_commit ? "web" : "git",
|
2006-06-02 07:32:20 +02:00
|
|
|
when => $when,
|
2006-11-26 21:05:57 +01:00
|
|
|
message => [@messages],
|
2006-06-02 07:32:20 +02:00
|
|
|
pages => [@pages],
|
2007-10-31 23:32:22 +01:00
|
|
|
} if @pages;
|
2006-06-02 07:32:20 +02:00
|
|
|
|
2007-08-24 20:07:15 +02:00
|
|
|
last if @rets >= $num;
|
2006-06-02 07:32:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return @rets;
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-06-02 07:32:20 +02:00
|
|
|
|
2010-12-30 00:58:49 +01:00
|
|
|
sub rcs_diff ($;$) {
|
2008-03-03 21:53:34 +01:00
|
|
|
my $rev=shift;
|
2010-12-30 00:58:49 +01:00
|
|
|
my $maxlines=shift;
|
2008-03-03 21:53:34 +01:00
|
|
|
my ($sha1) = $rev =~ /^($sha1_pattern)$/; # untaint
|
2008-03-12 20:45:10 +01:00
|
|
|
my @lines;
|
2010-12-30 00:58:49 +01:00
|
|
|
my $addlines=sub {
|
|
|
|
my $line=shift;
|
|
|
|
return if defined $maxlines && @lines == $maxlines;
|
|
|
|
push @lines, $line."\n"
|
|
|
|
if (@lines || $line=~/^diff --git/);
|
2010-12-30 01:10:28 +01:00
|
|
|
return 1;
|
2010-12-30 00:58:49 +01:00
|
|
|
};
|
|
|
|
safe_git(undef, $addlines, "git", "show", $sha1);
|
2008-03-12 20:45:10 +01:00
|
|
|
if (wantarray) {
|
|
|
|
return @lines;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return join("", @lines);
|
|
|
|
}
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-03-03 21:53:34 +01:00
|
|
|
|
2010-04-17 00:29:45 +02:00
|
|
|
{
|
|
|
|
my %time_cache;
|
|
|
|
|
|
|
|
sub findtimes ($$) {
|
2007-10-10 20:15:15 +02:00
|
|
|
my $file=shift;
|
2010-04-17 00:29:45 +02:00
|
|
|
my $id=shift; # 0 = mtime ; 1 = ctime
|
|
|
|
|
|
|
|
if (! keys %time_cache) {
|
|
|
|
my $date;
|
|
|
|
foreach my $line (run_or_die('git', 'log',
|
2010-10-31 18:53:06 +01:00
|
|
|
'--pretty=format:%at',
|
2010-04-17 00:29:45 +02:00
|
|
|
'--name-only', '--relative')) {
|
|
|
|
if (! defined $date && $line =~ /^(\d+)$/) {
|
|
|
|
$date=$line;
|
|
|
|
}
|
|
|
|
elsif (! length $line) {
|
|
|
|
$date=undef;
|
|
|
|
}
|
|
|
|
else {
|
2010-06-16 05:21:55 +02:00
|
|
|
my $f=decode_git_file($line);
|
|
|
|
|
|
|
|
if (! $time_cache{$f}) {
|
|
|
|
$time_cache{$f}[0]=$date; # mtime
|
2010-04-17 00:29:45 +02:00
|
|
|
}
|
2010-06-16 05:21:55 +02:00
|
|
|
$time_cache{$f}[1]=$date; # ctime
|
2010-04-17 00:29:45 +02:00
|
|
|
}
|
|
|
|
}
|
2009-10-12 05:07:31 +02:00
|
|
|
}
|
2006-06-02 07:32:20 +02:00
|
|
|
|
2010-04-17 00:29:45 +02:00
|
|
|
return exists $time_cache{$file} ? $time_cache{$file}[$id] : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
sub rcs_getctime ($) {
|
|
|
|
my $file=shift;
|
|
|
|
|
|
|
|
return findtimes($file, 1);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-06-02 07:32:20 +02:00
|
|
|
|
2010-04-16 23:02:29 +02:00
|
|
|
sub rcs_getmtime ($) {
|
2010-04-17 00:29:45 +02:00
|
|
|
my $file=shift;
|
|
|
|
|
|
|
|
return findtimes($file, 0);
|
2010-04-16 23:02:29 +02:00
|
|
|
}
|
|
|
|
|
2010-10-01 06:06:00 +02:00
|
|
|
{
|
2010-10-23 22:19:16 +02:00
|
|
|
my $ret;
|
2010-10-01 06:06:00 +02:00
|
|
|
sub git_find_root {
|
2010-10-04 22:03:00 +02:00
|
|
|
# The wiki may not be the only thing in the git repo.
|
|
|
|
# Determine if it is in a subdirectory by examining the srcdir,
|
|
|
|
# and its parents, looking for the .git directory.
|
|
|
|
|
2010-10-23 22:19:16 +02:00
|
|
|
return @$ret if defined $ret;
|
2010-10-04 22:03:00 +02:00
|
|
|
|
|
|
|
my $subdir="";
|
|
|
|
my $dir=$config{srcdir};
|
|
|
|
while (! -d "$dir/.git") {
|
|
|
|
$subdir=IkiWiki::basename($dir)."/".$subdir;
|
|
|
|
$dir=IkiWiki::dirname($dir);
|
|
|
|
if (! length $dir) {
|
|
|
|
error("cannot determine root of git repo");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-23 22:19:16 +02:00
|
|
|
$ret=[$subdir, $dir];
|
|
|
|
return @$ret;
|
2010-10-01 06:06:00 +02:00
|
|
|
}
|
2010-10-04 22:03:00 +02:00
|
|
|
|
2010-10-01 06:06:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub git_parse_changes {
|
2011-09-05 20:51:49 +02:00
|
|
|
my $reverted = shift;
|
2010-10-04 22:22:50 +02:00
|
|
|
my @changes = @_;
|
|
|
|
|
2010-10-23 22:19:16 +02:00
|
|
|
my ($subdir, $rootdir) = git_find_root();
|
2010-10-04 22:22:50 +02:00
|
|
|
my @rets;
|
|
|
|
foreach my $ci (@changes) {
|
|
|
|
foreach my $detail (@{ $ci->{'details'} }) {
|
|
|
|
my $file = $detail->{'file'};
|
|
|
|
|
|
|
|
# check that all changed files are in the subdir
|
|
|
|
if (length $subdir &&
|
|
|
|
! ($file =~ s/^\Q$subdir\E//)) {
|
|
|
|
error sprintf(gettext("you are not allowed to change %s"), $file);
|
|
|
|
}
|
|
|
|
|
|
|
|
my ($action, $mode, $path);
|
|
|
|
if ($detail->{'status'} =~ /^[M]+\d*$/) {
|
|
|
|
$action="change";
|
|
|
|
$mode=$detail->{'mode_to'};
|
|
|
|
}
|
|
|
|
elsif ($detail->{'status'} =~ /^[AM]+\d*$/) {
|
2011-09-05 20:51:49 +02:00
|
|
|
$action= $reverted ? "remove" : "add";
|
2010-10-04 22:22:50 +02:00
|
|
|
$mode=$detail->{'mode_to'};
|
|
|
|
}
|
|
|
|
elsif ($detail->{'status'} =~ /^[DAM]+\d*/) {
|
2011-09-05 20:51:49 +02:00
|
|
|
$action= $reverted ? "add" : "remove";
|
2010-10-04 22:22:50 +02:00
|
|
|
$mode=$detail->{'mode_from'};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
error "unknown status ".$detail->{'status'};
|
|
|
|
}
|
|
|
|
|
|
|
|
# test that the file mode is ok
|
|
|
|
if ($mode !~ /^100[64][64][64]$/) {
|
|
|
|
error sprintf(gettext("you cannot act on a file with mode %s"), $mode);
|
|
|
|
}
|
|
|
|
if ($action eq "change") {
|
|
|
|
if ($detail->{'mode_from'} ne $detail->{'mode_to'}) {
|
|
|
|
error gettext("you are not allowed to change file modes");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# extract attachment to temp file
|
|
|
|
if (($action eq 'add' || $action eq 'change') &&
|
|
|
|
! pagetype($file)) {
|
|
|
|
eval q{use File::Temp};
|
|
|
|
die $@ if $@;
|
|
|
|
my $fh;
|
2010-11-29 17:01:50 +01:00
|
|
|
($fh, $path)=File::Temp::tempfile(undef, UNLINK => 1);
|
2010-10-23 22:19:16 +02:00
|
|
|
my $cmd = "cd $git_dir && ".
|
|
|
|
"git show $detail->{sha1_to} > '$path'";
|
2010-10-04 22:22:50 +02:00
|
|
|
if (system($cmd) != 0) {
|
|
|
|
error("failed writing temp file '$path'.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
push @rets, {
|
|
|
|
file => $file,
|
|
|
|
action => $action,
|
|
|
|
path => $path,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return @rets;
|
2010-10-01 06:06:00 +02:00
|
|
|
}
|
2008-10-23 02:52:34 +02:00
|
|
|
|
2010-10-01 06:06:00 +02:00
|
|
|
sub rcs_receive () {
|
2008-10-23 22:29:50 +02:00
|
|
|
my @rets;
|
2008-10-23 02:52:34 +02:00
|
|
|
while (<>) {
|
|
|
|
chomp;
|
|
|
|
my ($oldrev, $newrev, $refname) = split(' ', $_, 3);
|
2010-10-01 06:06:00 +02:00
|
|
|
|
2008-10-23 02:52:34 +02:00
|
|
|
# only allow changes to gitmaster_branch
|
|
|
|
if ($refname !~ /^refs\/heads\/\Q$config{gitmaster_branch}\E$/) {
|
2008-10-23 22:29:50 +02:00
|
|
|
error sprintf(gettext("you are not allowed to change %s"), $refname);
|
2008-10-23 02:52:34 +02:00
|
|
|
}
|
2010-10-01 06:06:00 +02:00
|
|
|
|
2008-10-23 22:56:40 +02:00
|
|
|
# Avoid chdir when running git here, because the changes
|
|
|
|
# are in the master git repo, not the srcdir repo.
|
2010-10-23 22:19:16 +02:00
|
|
|
# (Also, if a subdir is involved, we don't want to chdir to
|
|
|
|
# it and only see changes in it.)
|
2010-10-01 06:06:00 +02:00
|
|
|
# The pre-receive hook already puts us in the right place.
|
2012-02-07 07:59:05 +01:00
|
|
|
in_git_dir(".", sub {
|
|
|
|
push @rets, git_parse_changes(0, git_commit_info($oldrev."..".$newrev));
|
|
|
|
});
|
2008-10-23 02:52:34 +02:00
|
|
|
}
|
|
|
|
|
2008-10-24 00:05:57 +02:00
|
|
|
return reverse @rets;
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-10-23 02:52:34 +02:00
|
|
|
|
2010-10-09 00:02:47 +02:00
|
|
|
sub rcs_preprevert ($) {
|
|
|
|
my $rev=shift;
|
2010-10-09 00:58:47 +02:00
|
|
|
my ($sha1) = $rev =~ /^($sha1_pattern)$/; # untaint
|
2010-10-09 00:02:47 +02:00
|
|
|
|
2010-10-23 22:19:16 +02:00
|
|
|
# Examine changes from root of git repo, not from any subdir,
|
|
|
|
# in order to see all changes.
|
|
|
|
my ($subdir, $rootdir) = git_find_root();
|
2012-02-07 07:59:05 +01:00
|
|
|
in_git_dir($rootdir, sub {
|
|
|
|
my @commits=git_commit_info($sha1, 1);
|
|
|
|
|
|
|
|
if (! @commits) {
|
|
|
|
error "unknown commit"; # just in case
|
|
|
|
}
|
2010-10-23 23:13:04 +02:00
|
|
|
|
2012-02-07 07:59:05 +01:00
|
|
|
# git revert will fail on merge commits. Add a nice message.
|
|
|
|
if (exists $commits[0]->{parents} &&
|
|
|
|
@{$commits[0]->{parents}} > 1) {
|
|
|
|
error gettext("you are not allowed to revert a merge");
|
|
|
|
}
|
2010-12-30 01:19:58 +01:00
|
|
|
|
2012-02-07 07:59:05 +01:00
|
|
|
git_parse_changes(1, @commits);
|
|
|
|
});
|
2010-09-29 07:14:19 +02:00
|
|
|
}
|
|
|
|
|
2010-10-06 21:08:12 +02:00
|
|
|
sub rcs_revert ($) {
|
|
|
|
# Try to revert the given rev; returns undef on _success_.
|
2010-10-09 00:02:47 +02:00
|
|
|
my $rev = shift;
|
2010-10-09 00:58:47 +02:00
|
|
|
my ($sha1) = $rev =~ /^($sha1_pattern)$/; # untaint
|
2010-09-29 07:14:19 +02:00
|
|
|
|
2010-10-09 00:58:47 +02:00
|
|
|
if (run_or_non('git', 'revert', '--no-commit', $sha1)) {
|
2010-10-06 21:08:12 +02:00
|
|
|
return undef;
|
2010-10-04 22:22:50 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
run_or_die('git', 'reset', '--hard');
|
2010-10-09 00:58:47 +02:00
|
|
|
return sprintf(gettext("Failed to revert commit %s"), $sha1);
|
2010-10-04 22:22:50 +02:00
|
|
|
}
|
2010-09-29 07:14:19 +02:00
|
|
|
}
|
|
|
|
|
2006-06-02 07:32:20 +02:00
|
|
|
1
|