+ # 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;
+ # If it looks OK, do it for real, on a branch.
+ eval {
+ IkiWiki::disable_commit_hook();
+ push @undo, sub {
+ IkiWiki::enable_commit_hook();
+ };
+ 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});
+
+ push @undo, sub {
+ if (!run_or_cry('git', 'checkout', '--quiet', $config{gitmaster_branch})) {
+ run_or_cry('git', 'checkout','-f', '--quiet', $config{gitmaster_branch});
+ }
+ };
+ run_or_die('git', 'checkout', '--quiet', $branch);
+
+ run_or_die('git', 'revert', '--no-commit', $sha1);
+ run_or_non('git', 'commit', '-m', "revert $sha1", '-a');
+
+ # Re-switch to master.
+ run_or_die('git', 'checkout', '--quiet', $config{gitmaster_branch});