+ # preserve indentation of previous in_git_dir code for now
+ do {
+ my @commits=git_commit_info($rootdir, $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($rootdir, 1, @commits);
+
+ my $failure;
+ my @ret;
+ eval {
+ my $branch = "ikiwiki_revert_${sha1}"; # supposed to be unique
+
+ push @undo, sub {
+ run_or_cry_in($rootdir, 'git', 'branch', '-D', $branch) if $failure;
+ };
+ if (run_or_non_in($rootdir, 'git', 'rev-parse', '--quiet', '--verify', $branch)) {
+ run_or_non_in($rootdir, 'git', 'branch', '-D', $branch);
+ }
+ run_or_die_in($rootdir, 'git', 'branch', $branch, $config{gitmaster_branch});
+
+ my $working = create_temp_working_dir($rootdir, $branch);
+
+ push @undo, sub {
+ remove_tree($working);
+ };
+
+ 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');
+
+ my @raw_lines;
+ @raw_lines = run_or_die_in($rootdir, 'git', 'diff', '--pretty=raw',
+ '--raw', '--abbrev=40', '--always', '--no-renames',
+ "..${branch}");
+
+ my $ci = {
+ details => [parse_changed_files($rootdir, \@raw_lines)],
+ };
+
+ @ret = git_parse_changes($rootdir, 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;
+ };