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;
|
2016-12-28 19:04:34 +01:00
|
|
|
use File::Path qw{remove_tree};
|
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);
|
2013-11-16 22:26:20 +01:00
|
|
|
hook(type => "rcs", id => "rcs_find_changes", call => \&rcs_find_changes);
|
|
|
|
hook(type => "rcs", id => "rcs_get_current_rev", call => \&rcs_get_current_rev);
|
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 "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-28 19:04:34 +01:00
|
|
|
# Loosely based on git-new-workdir from git contrib.
|
|
|
|
sub create_temp_working_dir ($$) {
|
|
|
|
my $rootdir = shift;
|
|
|
|
my $branch = shift;
|
|
|
|
my $working = "$rootdir/.git/ikiwiki-temp-working";
|
|
|
|
remove_tree($working);
|
|
|
|
|
|
|
|
foreach my $dir ("", ".git") {
|
|
|
|
if (!mkdir("$working/$dir")) {
|
|
|
|
error("Unable to create $working/$dir: $!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Hooks are deliberately not included: we will commit to the temporary
|
|
|
|
# branch that is used in the temporary working tree, and we don't want
|
|
|
|
# to run the post-commit hook there.
|
|
|
|
#
|
|
|
|
# logs/refs is not included because we don't use the reflog.
|
|
|
|
# remotes, rr-cache, svn are similarly excluded.
|
|
|
|
foreach my $link ("config", "refs", "objects", "info", "packed-refs") {
|
|
|
|
if (!symlink("../../$link", "$working/.git/$link")) {
|
|
|
|
error("Unable to create symlink $working/.git/$link: $!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
open (my $out, '>', "$working/.git/HEAD") or
|
|
|
|
error("failed to write $working.git/HEAD: $!");
|
|
|
|
print $out "ref: refs/heads/$branch\n" or
|
|
|
|
error("failed to write $working.git/HEAD: $!");
|
|
|
|
close $out or
|
|
|
|
error("failed to write $working.git/HEAD: $!");
|
|
|
|
return $working;
|
|
|
|
}
|
|
|
|
|
2016-12-28 20:26:33 +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
|
|
|
|
2016-12-28 20:26:33 +01:00
|
|
|
my %params = @_;
|
2006-06-02 07:32:20 +02:00
|
|
|
|
|
|
|
my $pid = open my $OUT, "-|";
|
|
|
|
|
2017-01-09 14:42:07 +01:00
|
|
|
error("Working directory not specified") unless defined $params{chdir};
|
2006-06-02 07:32:20 +02:00
|
|
|
error("Cannot fork: $!") if !defined $pid;
|
|
|
|
|
|
|
|
if (!$pid) {
|
|
|
|
# In child.
|
|
|
|
# Git commands want to be in wc.
|
2017-01-09 14:42:07 +01:00
|
|
|
if ($params{chdir} ne '.') {
|
2017-01-05 18:47:50 +01:00
|
|
|
chdir $params{chdir}
|
|
|
|
or error("cannot chdir to $params{chdir}: $!");
|
|
|
|
}
|
2016-12-28 20:35:14 +01:00
|
|
|
|
|
|
|
if ($params{stdout}) {
|
|
|
|
open(STDOUT, '>&', $params{stdout}) or error("Cannot reopen stdout: $!");
|
|
|
|
}
|
|
|
|
|
2016-12-28 20:26:33 +01:00
|
|
|
exec @{$params{cmdline}} or error("Cannot exec '@{$params{cmdline}}': $!");
|
2006-06-02 07:32:20 +02:00
|
|
|
}
|
|
|
|
# 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
|
|
|
|
2016-12-28 20:26:33 +01:00
|
|
|
if (! defined $params{data_handler}) {
|
2010-12-30 00:58:49 +01:00
|
|
|
push @lines, $_;
|
|
|
|
}
|
|
|
|
else {
|
2016-12-28 20:26:33 +01:00
|
|
|
last unless $params{data_handler}->($_);
|
2010-12-30 00:58:49 +01:00
|
|
|
}
|
2006-06-02 07:32:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
close $OUT;
|
|
|
|
|
2016-12-28 20:26:33 +01:00
|
|
|
$params{error_handler}->("'@{$params{cmdline}}' failed: $!") if $? && $params{error_handler};
|
2006-06-02 07:32:20 +02:00
|
|
|
|
|
|
|
return wantarray ? @lines : ($? == 0);
|
|
|
|
}
|
|
|
|
# Convenient wrappers.
|
2017-01-05 18:47:50 +01:00
|
|
|
sub run_or_die_in ($$@) {
|
|
|
|
my $dir = shift;
|
|
|
|
safe_git(chdir => $dir, error_handler => \&error, cmdline => \@_);
|
|
|
|
}
|
|
|
|
sub run_or_cry_in ($$@) {
|
|
|
|
my $dir = shift;
|
|
|
|
safe_git(chdir => $dir, error_handler => sub { warn @_ }, cmdline => \@_);
|
|
|
|
}
|
|
|
|
sub run_or_non_in ($$@) {
|
|
|
|
my $dir = shift;
|
|
|
|
safe_git(chdir => $dir, cmdline => \@_);
|
|
|
|
}
|
2006-06-02 07:32:20 +02:00
|
|
|
|
2017-01-09 14:42:07 +01:00
|
|
|
sub ensure_committer ($) {
|
|
|
|
my $dir = shift;
|
|
|
|
|
2015-11-30 21:46:12 +01:00
|
|
|
if (! length $ENV{GIT_AUTHOR_NAME} || ! length $ENV{GIT_COMMITTER_NAME}) {
|
2017-01-09 14:42:07 +01:00
|
|
|
my $name = join('', run_or_non_in($dir, "git", "config", "user.name"));
|
2015-11-30 21:46:12 +01:00
|
|
|
if (! length $name) {
|
2017-01-09 14:42:07 +01:00
|
|
|
run_or_die_in($dir, "git", "config", "user.name", "IkiWiki");
|
2015-11-30 21:46:12 +01:00
|
|
|
}
|
2015-11-30 20:34:04 +01:00
|
|
|
}
|
|
|
|
|
2015-11-30 21:46:12 +01:00
|
|
|
if (! length $ENV{GIT_AUTHOR_EMAIL} || ! length $ENV{GIT_COMMITTER_EMAIL}) {
|
2017-01-09 14:42:07 +01:00
|
|
|
my $email = join('', run_or_non_in($dir, "git", "config", "user.email"));
|
2015-11-30 21:46:12 +01:00
|
|
|
if (! length $email) {
|
2017-01-09 14:42:07 +01:00
|
|
|
run_or_die_in($dir, "git", "config", "user.email", "ikiwiki.info");
|
2015-11-30 21:46:12 +01:00
|
|
|
}
|
2015-11-30 20:34:04 +01: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
|
|
|
|
|
2017-01-09 14:42:07 +01:00
|
|
|
ensure_committer($config{srcdir});
|
2015-11-30 20:34:04 +01:00
|
|
|
|
2006-06-02 07:32:20 +02:00
|
|
|
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.
|
2017-01-09 14:42:07 +01:00
|
|
|
push @undo, sub { run_or_cry_in($config{srcdir}, 'git', 'branch', '-D', $branch) };
|
|
|
|
run_or_die_in($config{srcdir}, 'git', 'branch', $branch, $sha1);
|
2006-06-02 07:32:20 +02:00
|
|
|
|
|
|
|
# Switch to throw-away branch for the merge operation.
|
|
|
|
push @undo, sub {
|
2017-01-09 14:42:07 +01:00
|
|
|
if (!run_or_cry_in($config{srcdir}, 'git', 'checkout', $config{gitmaster_branch})) {
|
|
|
|
run_or_cry_in($config{srcdir}, 'git', 'checkout','-f',$config{gitmaster_branch});
|
2006-06-02 07:32:20 +02:00
|
|
|
}
|
|
|
|
};
|
2017-01-09 14:42:07 +01:00
|
|
|
run_or_die_in($config{srcdir}, '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.
|
2017-01-09 14:42:07 +01:00
|
|
|
run_or_non_in($config{srcdir}, 'git', 'commit', '-m', $message, '-a');
|
2006-06-02 07:32:20 +02:00
|
|
|
# ... and re-switch to master.
|
2017-01-09 14:42:07 +01:00
|
|
|
run_or_die_in($config{srcdir}, 'git', 'checkout', $config{gitmaster_branch});
|
2006-06-02 07:32:20 +02:00
|
|
|
|
|
|
|
# Attempt to merge without complaining.
|
2017-01-09 14:42:07 +01:00
|
|
|
if (!run_or_non_in($config{srcdir}, 'git', 'pull', '--no-commit', '.', $branch)) {
|
2006-06-02 07:32:20 +02:00
|
|
|
$conflict = readfile($target);
|
2017-01-09 14:42:07 +01:00
|
|
|
run_or_die_in($config{srcdir}, '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
|
|
|
|
2017-01-09 14:42:07 +01:00
|
|
|
{
|
|
|
|
my %prefix_cache;
|
|
|
|
|
|
|
|
sub decode_git_file ($$) {
|
|
|
|
my $dir=shift;
|
2010-06-16 05:21:55 +02:00
|
|
|
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
|
2017-01-09 14:42:07 +01:00
|
|
|
if (! defined $prefix_cache{$dir}) {
|
|
|
|
($prefix_cache{$dir}) = run_or_die_in($dir, 'git', 'rev-parse', '--show-prefix');
|
|
|
|
if (! defined $prefix_cache{$dir}) {
|
|
|
|
$prefix_cache{$dir}="";
|
2010-06-16 05:21:55 +02:00
|
|
|
}
|
|
|
|
}
|
2017-01-09 14:42:07 +01:00
|
|
|
$file =~ s/^\Q$prefix_cache{$dir}\E//;
|
2010-06-16 05:21:55 +02:00
|
|
|
|
|
|
|
return decode("utf8", $file);
|
|
|
|
}
|
2017-01-09 14:42:07 +01:00
|
|
|
}
|
2010-06-16 05:21:55 +02:00
|
|
|
|
2017-01-09 14:42:07 +01:00
|
|
|
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.
|
2017-01-09 14:42:07 +01:00
|
|
|
my $dir = shift;
|
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
|
|
|
|
2017-01-09 14:42:07 +01:00
|
|
|
$ci{details} = [parse_changed_files($dir, $dt_ref)];
|
2016-12-19 14:48:56 +01:00
|
|
|
|
|
|
|
return \%ci;
|
|
|
|
}
|
|
|
|
|
2017-01-09 14:42:07 +01:00
|
|
|
sub parse_changed_files ($$) {
|
|
|
|
my $dir = shift;
|
2016-12-19 14:48:56 +01:00
|
|
|
my $dt_ref = shift;
|
|
|
|
|
|
|
|
my @files;
|
|
|
|
|
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) {
|
2016-12-19 14:48:56 +01:00
|
|
|
push @files, {
|
2017-01-09 14:42:07 +01:00
|
|
|
'file' => decode_git_file($dir, $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
|
|
|
}
|
|
|
|
|
2016-12-19 14:48:56 +01:00
|
|
|
return @files;
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-06-02 07:32:20 +02:00
|
|
|
|
2017-01-09 14:42:07 +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.
|
2017-01-09 14:42:07 +01:00
|
|
|
my ($dir, $sha1, $num) = @_;
|
2006-06-02 07:32:20 +02:00
|
|
|
|
2008-10-24 00:05:57 +02:00
|
|
|
my @opts;
|
|
|
|
push @opts, "--max-count=$num" if defined $num;
|
|
|
|
|
2017-01-09 14:42:07 +01:00
|
|
|
my @raw_lines = run_or_die_in($dir, 'git', 'log', @opts,
|
2007-10-31 23:32:22 +01:00
|
|
|
'--pretty=raw', '--raw', '--abbrev=40', '--always', '-c',
|
2016-09-04 00:29:37 +02:00
|
|
|
'-r', $sha1, '--no-renames', '--', '.');
|
2006-06-02 07:32:20 +02:00
|
|
|
|
|
|
|
my @ci;
|
2017-01-09 14:42:07 +01:00
|
|
|
while (my $parsed = parse_diff_tree($dir, \@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
|
|
|
|
2013-11-16 22:26:20 +01:00
|
|
|
sub rcs_find_changes ($) {
|
|
|
|
my $oldrev=shift;
|
2006-06-02 07:32:20 +02:00
|
|
|
|
2014-04-06 01:09:05 +02:00
|
|
|
# Note that git log will sometimes show files being added that
|
|
|
|
# don't exist. Particularly, git merge -s ours can result in a
|
|
|
|
# merge commit where some files were not really added.
|
|
|
|
# This is why the code below verifies that the files really
|
|
|
|
# exist.
|
2017-01-09 14:42:07 +01:00
|
|
|
my @raw_lines = run_or_die_in($config{srcdir}, 'git', 'log',
|
2013-11-16 22:26:20 +01:00
|
|
|
'--pretty=raw', '--raw', '--abbrev=40', '--always', '-c',
|
|
|
|
'--no-renames', , '--reverse',
|
|
|
|
'-r', "$oldrev..HEAD", '--', '.');
|
|
|
|
|
|
|
|
# Due to --reverse, we see changes in chronological order.
|
|
|
|
my %changed;
|
|
|
|
my %deleted;
|
|
|
|
my $nullsha = 0 x 40;
|
2013-11-17 01:48:23 +01:00
|
|
|
my $newrev=$oldrev;
|
2017-01-09 14:42:07 +01:00
|
|
|
while (my $ci = parse_diff_tree($config{srcdir}, \@raw_lines)) {
|
2013-11-16 22:26:20 +01:00
|
|
|
$newrev=$ci->{sha1};
|
|
|
|
foreach my $i (@{$ci->{details}}) {
|
|
|
|
my $file=$i->{file};
|
2013-11-16 23:56:39 +01:00
|
|
|
if ($i->{sha1_to} eq $nullsha) {
|
2014-04-06 01:09:05 +02:00
|
|
|
if (! -e "$config{srcdir}/$file") {
|
|
|
|
delete $changed{$file};
|
|
|
|
$deleted{$file}=1;
|
|
|
|
}
|
2013-11-16 22:26:20 +01:00
|
|
|
}
|
|
|
|
else {
|
2014-04-06 01:09:05 +02:00
|
|
|
if (-e "$config{srcdir}/$file") {
|
|
|
|
delete $deleted{$file};
|
|
|
|
$changed{$file}=1;
|
|
|
|
}
|
2013-11-16 22:26:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (\%changed, \%deleted, $newrev);
|
|
|
|
}
|
|
|
|
|
2017-01-09 14:42:07 +01:00
|
|
|
sub git_sha1_file ($$) {
|
|
|
|
my $dir=shift;
|
2013-11-16 22:26:20 +01:00
|
|
|
my $file=shift;
|
2017-01-09 14:58:58 +01:00
|
|
|
return git_sha1($dir, $file);
|
2013-11-16 22:26:20 +01:00
|
|
|
}
|
|
|
|
|
2017-01-09 14:42:07 +01:00
|
|
|
sub git_sha1 ($@) {
|
|
|
|
my $dir = shift;
|
2006-06-03 20:03:41 +02:00
|
|
|
# Ignore error since a non-existing file might be given.
|
2017-01-09 14:42:07 +01:00
|
|
|
my ($sha1) = run_or_non_in($dir, 'git', 'rev-list', '--max-count=1', 'HEAD',
|
2013-11-16 22:26:20 +01:00
|
|
|
'--', @_);
|
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
|
|
|
|
2013-11-16 22:26:20 +01:00
|
|
|
sub rcs_get_current_rev () {
|
2017-01-09 14:42:07 +01:00
|
|
|
return git_sha1($config{srcdir});
|
2013-11-16 22:26:20 +01:00
|
|
|
}
|
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub rcs_update () {
|
2006-06-02 07:32:20 +02:00
|
|
|
# Update working directory.
|
2017-01-09 14:42:07 +01:00
|
|
|
ensure_committer($config{srcdir});
|
2015-11-30 20:34:04 +01:00
|
|
|
|
2007-10-22 03:47:30 +02:00
|
|
|
if (length $config{gitorigin_branch}) {
|
2017-01-09 14:42:07 +01:00
|
|
|
run_or_cry_in($config{srcdir}, '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) = @_;
|
|
|
|
|
2017-01-09 14:42:07 +01:00
|
|
|
return git_sha1_file($config{srcdir}, $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.
|
2017-01-09 14:42:07 +01:00
|
|
|
my $cur = git_sha1_file($config{srcdir}, $params{file});
|
2016-12-24 15:34:30 +01:00
|
|
|
my $prev;
|
|
|
|
if (defined $params{token}) {
|
|
|
|
($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();
|
|
|
|
}
|
2016-12-24 15:35:01 +01:00
|
|
|
if (length $u) {
|
2015-05-14 17:37:47 +02:00
|
|
|
$u=encode_utf8(IkiWiki::cloak($u));
|
2010-06-23 23:35:21 +02:00
|
|
|
$ENV{GIT_AUTHOR_NAME}=$u;
|
2010-06-24 01:44:41 +02:00
|
|
|
}
|
2016-12-24 15:35:01 +01:00
|
|
|
else {
|
|
|
|
$u = 'anonymous';
|
|
|
|
}
|
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
|
|
|
}
|
2016-12-24 15:35:01 +01:00
|
|
|
if (length $u) {
|
2010-06-23 23:35:21 +02:00
|
|
|
$ENV{GIT_AUTHOR_EMAIL}="$u\@web";
|
|
|
|
}
|
2016-12-24 15:35:01 +01:00
|
|
|
else {
|
|
|
|
$ENV{GIT_AUTHOR_EMAIL}='anonymous@web';
|
|
|
|
}
|
2008-07-17 22:03:16 +02:00
|
|
|
}
|
2006-06-02 07:32:20 +02:00
|
|
|
|
2017-01-09 14:42:07 +01:00
|
|
|
ensure_committer($config{srcdir});
|
2015-11-30 21:46:12 +01: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 (.*)/;
|
2013-07-11 03:52:43 +02:00
|
|
|
if ($version ge "1.7.8") {
|
|
|
|
push @opts, "--allow-empty-message", "--no-edit";
|
|
|
|
}
|
2013-07-11 03:48:16 +02:00
|
|
|
if ($version ge "1.7.2") {
|
|
|
|
push @opts, "--allow-empty-message";
|
|
|
|
}
|
|
|
|
elsif ($version ge "1.5.4") {
|
2008-08-01 01:23:54 +02:00
|
|
|
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.
|
2017-01-09 14:42:07 +01:00
|
|
|
# So we should ignore its exit status (hence run_or_non_in).
|
|
|
|
if (run_or_non_in($config{srcdir}, 'git', 'commit', '-m', $params{message}, '-q', @opts)) {
|
2007-10-22 03:47:30 +02:00
|
|
|
if (length $config{gitorigin_branch}) {
|
2017-01-09 14:42:07 +01:00
|
|
|
run_or_cry_in($config{srcdir}, 'git', 'push', $config{gitorigin_branch}, $config{gitmaster_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) = @_;
|
|
|
|
|
2017-01-09 14:42:07 +01:00
|
|
|
ensure_committer($config{srcdir});
|
|
|
|
run_or_cry_in($config{srcdir}, '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) = @_;
|
|
|
|
|
2017-01-09 14:42:07 +01:00
|
|
|
ensure_committer($config{srcdir});
|
|
|
|
run_or_cry_in($config{srcdir}, '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) = @_;
|
|
|
|
|
2017-01-09 14:42:07 +01:00
|
|
|
ensure_committer($config{srcdir});
|
|
|
|
run_or_cry_in($config{srcdir}, '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;
|
2017-01-09 14:42:07 +01:00
|
|
|
foreach my $ci (git_commit_info($config{srcdir}, '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'};
|
2013-12-31 02:27:21 +01:00
|
|
|
my $efile = join('/',
|
|
|
|
map { uri_escape_utf8($_) } split('/', $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
|
|
|
};
|
2016-12-28 20:26:33 +01:00
|
|
|
safe_git(
|
2017-01-09 14:42:07 +01:00
|
|
|
chdir => $config{srcdir},
|
2016-12-28 20:26:33 +01:00
|
|
|
error_handler => undef,
|
|
|
|
data_handler => $addlines,
|
|
|
|
cmdline => ["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;
|
2017-01-09 14:42:07 +01:00
|
|
|
foreach my $line (run_or_die_in($config{srcdir}, '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 {
|
2017-01-09 14:42:07 +01:00
|
|
|
my $f=decode_git_file($config{srcdir}, $line);
|
2010-06-16 05:21:55 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-01-09 14:42:07 +01:00
|
|
|
sub git_parse_changes ($$@) {
|
|
|
|
my $dir = shift;
|
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);
|
2016-12-28 20:35:14 +01:00
|
|
|
safe_git(
|
2017-01-09 14:42:07 +01:00
|
|
|
chdir => $dir,
|
2016-12-28 20:35:14 +01:00
|
|
|
error_handler => sub { error("failed writing temp file '$path': ".shift."."); },
|
|
|
|
stdout => $fh,
|
|
|
|
cmdline => ['git', 'show', $detail->{sha1_to}],
|
|
|
|
);
|
2010-10-04 22:22:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2017-01-09 14:42:07 +01:00
|
|
|
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
|
|
|
|
2016-12-19 14:48:56 +01:00
|
|
|
my @undo; # undo stack for cleanup in case of an error
|
|
|
|
|
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();
|
2017-01-09 14:42:07 +01:00
|
|
|
ensure_committer($rootdir);
|
|
|
|
|
|
|
|
# preserve indentation of previous in_git_dir code for now
|
|
|
|
do {
|
|
|
|
my @commits=git_commit_info($rootdir, $sha1, 1);
|
|
|
|
|
2012-02-07 07:59:05 +01:00
|
|
|
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
|
|
|
|
2016-12-19 14:48:56 +01:00
|
|
|
# Due to the presence of rename-detection, we cannot actually
|
|
|
|
# see what will happen in a revert without trying it.
|
|
|
|
# But we can guess, which is enough to rule out most changes
|
|
|
|
# that we won't allow reverting.
|
2017-01-09 14:42:07 +01:00
|
|
|
git_parse_changes($rootdir, 1, @commits);
|
2016-12-19 14:48:56 +01:00
|
|
|
|
|
|
|
my $failure;
|
|
|
|
my @ret;
|
|
|
|
eval {
|
|
|
|
my $branch = "ikiwiki_revert_${sha1}"; # supposed to be unique
|
|
|
|
|
|
|
|
push @undo, sub {
|
2017-01-05 18:47:50 +01:00
|
|
|
run_or_cry_in($rootdir, 'git', 'branch', '-D', $branch) if $failure;
|
2016-12-19 14:48:56 +01:00
|
|
|
};
|
2017-01-05 18:47:50 +01:00
|
|
|
if (run_or_non_in($rootdir, 'git', 'rev-parse', '--quiet', '--verify', $branch)) {
|
|
|
|
run_or_non_in($rootdir, 'git', 'branch', '-D', $branch);
|
2016-12-19 14:48:56 +01:00
|
|
|
}
|
2017-01-05 18:47:50 +01:00
|
|
|
run_or_die_in($rootdir, 'git', 'branch', $branch, $config{gitmaster_branch});
|
2016-12-19 14:48:56 +01:00
|
|
|
|
2016-12-28 19:04:34 +01:00
|
|
|
my $working = create_temp_working_dir($rootdir, $branch);
|
|
|
|
|
2016-12-19 14:48:56 +01:00
|
|
|
push @undo, sub {
|
2016-12-28 19:04:34 +01:00
|
|
|
remove_tree($working);
|
2016-12-19 14:48:56 +01:00
|
|
|
};
|
|
|
|
|
2017-01-05 18:47:50 +01:00
|
|
|
run_or_die_in($working, 'git', 'checkout', '--quiet', '--force', $branch);
|
|
|
|
run_or_die_in($working, 'git', 'revert', '--no-commit', $sha1);
|
|
|
|
run_or_die_in($working, 'git', 'commit', '-m', "revert $sha1", '-a');
|
2016-12-19 14:48:56 +01:00
|
|
|
|
|
|
|
my @raw_lines;
|
2017-01-05 18:47:50 +01:00
|
|
|
@raw_lines = run_or_die_in($rootdir, 'git', 'diff', '--pretty=raw',
|
2016-12-19 14:48:56 +01:00
|
|
|
'--raw', '--abbrev=40', '--always', '--no-renames',
|
2016-12-28 19:04:34 +01:00
|
|
|
"..${branch}");
|
2016-12-19 14:48:56 +01:00
|
|
|
|
|
|
|
my $ci = {
|
2017-01-09 14:42:07 +01:00
|
|
|
details => [parse_changed_files($rootdir, \@raw_lines)],
|
2016-12-19 14:48:56 +01:00
|
|
|
};
|
|
|
|
|
2017-01-09 14:42:07 +01:00
|
|
|
@ret = git_parse_changes($rootdir, 0, $ci);
|
2016-12-19 14:48:56 +01:00
|
|
|
};
|
|
|
|
$failure = $@;
|
|
|
|
|
|
|
|
# Process undo stack (in reverse order). By policy cleanup
|
|
|
|
# actions should normally print a warning on failure.
|
|
|
|
while (my $handle = pop @undo) {
|
|
|
|
$handle->();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($failure) {
|
|
|
|
my $message = sprintf(gettext("Failed to revert commit %s"), $sha1);
|
|
|
|
error("$message\n$failure\n");
|
|
|
|
}
|
|
|
|
return @ret;
|
2017-01-09 14:42:07 +01:00
|
|
|
};
|
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
|
|
|
|
2017-01-09 14:42:07 +01:00
|
|
|
ensure_committer($config{srcdir});
|
2015-11-30 20:34:04 +01:00
|
|
|
|
2017-01-09 14:42:07 +01:00
|
|
|
if (run_or_non_in($config{srcdir}, 'git', 'cherry-pick', '--no-commit', "ikiwiki_revert_$sha1")) {
|
2010-10-06 21:08:12 +02:00
|
|
|
return undef;
|
2010-10-04 22:22:50 +02:00
|
|
|
}
|
|
|
|
else {
|
2017-01-09 14:42:07 +01:00
|
|
|
run_or_non_in($config{srcdir}, 'git', 'branch', '-D', "ikiwiki_revert_$sha1");
|
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
|