Skip to content

Commit

Permalink
fixup! tmp-objdir: new API for creating temporary writable databases
Browse files Browse the repository at this point in the history
When setup_work_tree executes, it redoes setup of the object database
path and various other aspects of the_repository.  This destroys the
temporary object database state.

This commit removes the temporary object database and reapplies it
around the operations in the chdir_notify callback.

Signed-off-by: Neeraj Singh <[email protected]>
Signed-off-by: Johannes Schindelin <[email protected]>
  • Loading branch information
neerajsi-msft authored and dscho committed Oct 27, 2021
1 parent b83c4e4 commit 08a757b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
5 changes: 5 additions & 0 deletions environment.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "commit.h"
#include "strvec.h"
#include "object-store.h"
#include "tmp-objdir.h"
#include "chdir-notify.h"
#include "shallow.h"

Expand Down Expand Up @@ -343,10 +344,14 @@ static void update_relative_gitdir(const char *name,
void *data)
{
char *path = reparent_relative_path(old_cwd, new_cwd, get_git_dir());
struct tmp_objdir *tmp_objdir = tmp_objdir_unapply_primary_odb();
trace_printf_key(&trace_setup_key,
"setup: move $GIT_DIR to '%s'",
path);

set_git_dir_1(path);
if (tmp_objdir)
tmp_objdir_reapply_primary_odb(tmp_objdir, old_cwd, new_cwd);
free(path);
}

Expand Down
25 changes: 25 additions & 0 deletions tmp-objdir.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "cache.h"
#include "tmp-objdir.h"
#include "chdir-notify.h"
#include "dir.h"
#include "sigchain.h"
#include "string-list.h"
Expand All @@ -12,6 +13,7 @@ struct tmp_objdir {
struct strbuf path;
struct strvec env;
struct object_directory *prev_odb;
int will_destroy;
};

/*
Expand Down Expand Up @@ -315,4 +317,27 @@ void tmp_objdir_replace_primary_odb(struct tmp_objdir *t, int will_destroy)
if (t->prev_odb)
BUG("the primary object database is already replaced");
t->prev_odb = set_temporary_primary_odb(t->path.buf, will_destroy);
t->will_destroy = will_destroy;
}

struct tmp_objdir *tmp_objdir_unapply_primary_odb(void)
{
if (!the_tmp_objdir || !the_tmp_objdir->prev_odb)
return NULL;

restore_primary_odb(the_tmp_objdir->prev_odb, the_tmp_objdir->path.buf);
the_tmp_objdir->prev_odb = NULL;
return the_tmp_objdir;
}

void tmp_objdir_reapply_primary_odb(struct tmp_objdir *t, const char *old_cwd,
const char *new_cwd)
{
char *path;

path = reparent_relative_path(old_cwd, new_cwd, t->path.buf);
strbuf_reset(&t->path);
strbuf_addstr(&t->path, path);
free(path);
tmp_objdir_replace_primary_odb(t, t->will_destroy);
}
15 changes: 15 additions & 0 deletions tmp-objdir.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,19 @@ void tmp_objdir_add_as_alternate(const struct tmp_objdir *);
*/
void tmp_objdir_replace_primary_odb(struct tmp_objdir *, int will_destroy);

/*
* If the primary object database was replaced by a temporary object directory,
* restore it to its original value while keeping the directory contents around.
* Returns NULL if the primary object database was not replaced.
*/
struct tmp_objdir *tmp_objdir_unapply_primary_odb(void);

/*
* Reapplies the former primary temporary object database, after protentially
* changing its relative path.
*/
void tmp_objdir_reapply_primary_odb(struct tmp_objdir *, const char *old_cwd,
const char *new_cwd);


#endif /* TMP_OBJDIR_H */

0 comments on commit 08a757b

Please sign in to comment.