+my @git_dir_stack;
+my $prefix;
+
+sub in_git_dir ($$) {
+ unshift @git_dir_stack, shift;
+ my @ret=shift->();
+ shift @git_dir_stack;
+ $prefix=undef;
+ return @ret;
+}
+
+# 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;
+}
+
+sub safe_git {