+ return in_git_dir($rootdir, sub {
+ my @commits=git_commit_info($sha1, 1);
+
+ if (! @commits) {
+ error "unknown commit"; # just in case
+ }
+
+ # 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");
+ }
+
+ # 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.
+ git_parse_changes(1, @commits);
+
+ my $failure;
+ my @ret;
+ eval {
+ my $branch = "ikiwiki_revert_${sha1}"; # supposed to be unique
+
+ push @undo, sub {
+ run_or_cry('git', 'branch', '-D', $branch) if $failure;
+ };
+ if (run_or_non('git', 'rev-parse', '--quiet', '--verify', $branch)) {
+ run_or_non('git', 'branch', '-D', $branch);
+ }
+ run_or_die('git', 'branch', $branch, $config{gitmaster_branch});
+
+ my $working = create_temp_working_dir($rootdir, $branch);
+
+ push @undo, sub {
+ remove_tree($working);
+ };
+
+ in_git_dir($working, sub {
+ run_or_die('git', 'checkout', '--quiet', '--force', $branch);
+ run_or_die('git', 'revert', '--no-commit', $sha1);
+ run_or_die('git', 'commit', '-m', "revert $sha1", '-a');
+ });
+
+ my @raw_lines;
+ @raw_lines = run_or_die('git', 'diff', '--pretty=raw',
+ '--raw', '--abbrev=40', '--always', '--no-renames',
+ "..${branch}");
+
+ my $ci = {
+ details => [parse_changed_files(\@raw_lines)],
+ };
+
+ @ret = git_parse_changes(0, $ci);
+ };
+ $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;
+ });