]> git.ipfire.org Git - thirdparty/git.git/blobdiff - object-file.c
Merge branch 'js/t1450-making-it-writable-does-not-need-full-posixperm'
[thirdparty/git.git] / object-file.c
index eb1426f98c2529b81b9812805aa5a95cd1355055..8be57f48de738a7f61de8b18e10a41e2007ac045 100644 (file)
@@ -680,6 +680,49 @@ void add_to_alternates_memory(const char *reference)
                             '\n', NULL, 0);
 }
 
+struct object_directory *set_temporary_primary_odb(const char *dir, int will_destroy)
+{
+       struct object_directory *new_odb;
+
+       /*
+        * Make sure alternates are initialized, or else our entry may be
+        * overwritten when they are.
+        */
+       prepare_alt_odb(the_repository);
+
+       /*
+        * Make a new primary odb and link the old primary ODB in as an
+        * alternate
+        */
+       new_odb = xcalloc(1, sizeof(*new_odb));
+       new_odb->path = xstrdup(dir);
+
+       /*
+        * Disable ref updates while a temporary odb is active, since
+        * the objects in the database may roll back.
+        */
+       new_odb->disable_ref_updates = 1;
+       new_odb->will_destroy = will_destroy;
+       new_odb->next = the_repository->objects->odb;
+       the_repository->objects->odb = new_odb;
+       return new_odb->next;
+}
+
+void restore_primary_odb(struct object_directory *restore_odb, const char *old_path)
+{
+       struct object_directory *cur_odb = the_repository->objects->odb;
+
+       if (strcmp(old_path, cur_odb->path))
+               BUG("expected %s as primary object store; found %s",
+                   old_path, cur_odb->path);
+
+       if (cur_odb->next != restore_odb)
+               BUG("we expect the old primary object store to be the first alternate");
+
+       the_repository->objects->odb = restore_odb;
+       free_object_directory(cur_odb);
+}
+
 /*
  * Compute the exact path an alternate is at and returns it. In case of
  * error NULL is returned and the human readable error is added to `err`
@@ -1806,8 +1849,11 @@ int hash_object_file(const struct git_hash_algo *algo, const void *buf,
 /* Finalize a file on disk, and close it. */
 static void close_loose_object(int fd)
 {
-       if (fsync_object_files)
-               fsync_or_die(fd, "loose object file");
+       if (!the_repository->objects->odb->will_destroy) {
+               if (fsync_object_files)
+                       fsync_or_die(fd, "loose object file");
+       }
+
        if (close(fd) != 0)
                die_errno(_("error when closing loose object file"));
 }