builtin/show: do not prune by pathspec
[git/mjg.git] / tmp-objdir.c
bloba8e4553f2749bad1df72269b625d22a7fbd5b2b2
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "tmp-objdir.h"
5 #include "abspath.h"
6 #include "chdir-notify.h"
7 #include "dir.h"
8 #include "environment.h"
9 #include "object-file.h"
10 #include "path.h"
11 #include "string-list.h"
12 #include "strbuf.h"
13 #include "strvec.h"
14 #include "quote.h"
15 #include "object-store-ll.h"
17 struct tmp_objdir {
18 struct strbuf path;
19 struct strvec env;
20 struct object_directory *prev_odb;
21 int will_destroy;
25 * Allow only one tmp_objdir at a time in a running process, which simplifies
26 * our atexit cleanup routines. It's doubtful callers will ever need
27 * more than one, and we can expand later if so. You can have many such
28 * tmp_objdirs simultaneously in many processes, of course.
30 static struct tmp_objdir *the_tmp_objdir;
32 static void tmp_objdir_free(struct tmp_objdir *t)
34 strbuf_release(&t->path);
35 strvec_clear(&t->env);
36 free(t);
39 int tmp_objdir_destroy(struct tmp_objdir *t)
41 int err;
43 if (!t)
44 return 0;
46 if (t == the_tmp_objdir)
47 the_tmp_objdir = NULL;
49 if (t->prev_odb)
50 restore_primary_odb(t->prev_odb, t->path.buf);
52 err = remove_dir_recursively(&t->path, 0);
54 tmp_objdir_free(t);
56 return err;
59 static void remove_tmp_objdir(void)
61 tmp_objdir_destroy(the_tmp_objdir);
64 void tmp_objdir_discard_objects(struct tmp_objdir *t)
66 remove_dir_recursively(&t->path, REMOVE_DIR_KEEP_TOPLEVEL);
70 * These env_* functions are for setting up the child environment; the
71 * "replace" variant overrides the value of any existing variable with that
72 * "key". The "append" variant puts our new value at the end of a list,
73 * separated by PATH_SEP (which is what separate values in
74 * GIT_ALTERNATE_OBJECT_DIRECTORIES).
76 static void env_append(struct strvec *env, const char *key, const char *val)
78 struct strbuf quoted = STRBUF_INIT;
79 const char *old;
82 * Avoid quoting if it's not necessary, for maximum compatibility
83 * with older parsers which don't understand the quoting.
85 if (*val == '"' || strchr(val, PATH_SEP)) {
86 strbuf_addch(&quoted, '"');
87 quote_c_style(val, &quoted, NULL, 1);
88 strbuf_addch(&quoted, '"');
89 val = quoted.buf;
92 old = getenv(key);
93 if (!old)
94 strvec_pushf(env, "%s=%s", key, val);
95 else
96 strvec_pushf(env, "%s=%s%c%s", key, old, PATH_SEP, val);
98 strbuf_release(&quoted);
101 static void env_replace(struct strvec *env, const char *key, const char *val)
103 strvec_pushf(env, "%s=%s", key, val);
106 static int setup_tmp_objdir(const char *root)
108 char *path;
109 int ret = 0;
111 path = xstrfmt("%s/pack", root);
112 ret = mkdir(path, 0777);
113 free(path);
115 return ret;
118 struct tmp_objdir *tmp_objdir_create(const char *prefix)
120 static int installed_handlers;
121 struct tmp_objdir *t;
123 if (the_tmp_objdir)
124 BUG("only one tmp_objdir can be used at a time");
126 t = xcalloc(1, sizeof(*t));
127 strbuf_init(&t->path, 0);
128 strvec_init(&t->env);
131 * Use a string starting with tmp_ so that the builtin/prune.c code
132 * can recognize any stale objdirs left behind by a crash and delete
133 * them.
135 strbuf_addf(&t->path, "%s/tmp_objdir-%s-XXXXXX", get_object_directory(), prefix);
137 if (!mkdtemp(t->path.buf)) {
138 /* free, not destroy, as we never touched the filesystem */
139 tmp_objdir_free(t);
140 return NULL;
143 the_tmp_objdir = t;
144 if (!installed_handlers) {
145 atexit(remove_tmp_objdir);
146 installed_handlers++;
149 if (setup_tmp_objdir(t->path.buf)) {
150 tmp_objdir_destroy(t);
151 return NULL;
154 env_append(&t->env, ALTERNATE_DB_ENVIRONMENT,
155 absolute_path(get_object_directory()));
156 env_replace(&t->env, DB_ENVIRONMENT, absolute_path(t->path.buf));
157 env_replace(&t->env, GIT_QUARANTINE_ENVIRONMENT,
158 absolute_path(t->path.buf));
160 return t;
164 * Make sure we copy packfiles and their associated metafiles in the correct
165 * order. All of these ends_with checks are slightly expensive to do in
166 * the midst of a sorting routine, but in practice it shouldn't matter.
167 * We will have a relatively small number of packfiles to order, and loose
168 * objects exit early in the first line.
170 static int pack_copy_priority(const char *name)
172 if (!starts_with(name, "pack"))
173 return 0;
174 if (ends_with(name, ".keep"))
175 return 1;
176 if (ends_with(name, ".pack"))
177 return 2;
178 if (ends_with(name, ".rev"))
179 return 3;
180 if (ends_with(name, ".idx"))
181 return 4;
182 return 5;
185 static int pack_copy_cmp(const char *a, const char *b)
187 return pack_copy_priority(a) - pack_copy_priority(b);
190 static int read_dir_paths(struct string_list *out, const char *path)
192 DIR *dh;
193 struct dirent *de;
195 dh = opendir(path);
196 if (!dh)
197 return -1;
199 while ((de = readdir(dh)))
200 if (de->d_name[0] != '.')
201 string_list_append(out, de->d_name);
203 closedir(dh);
204 return 0;
207 static int migrate_paths(struct strbuf *src, struct strbuf *dst);
209 static int migrate_one(struct strbuf *src, struct strbuf *dst)
211 struct stat st;
213 if (stat(src->buf, &st) < 0)
214 return -1;
215 if (S_ISDIR(st.st_mode)) {
216 if (!mkdir(dst->buf, 0777)) {
217 if (adjust_shared_perm(dst->buf))
218 return -1;
219 } else if (errno != EEXIST)
220 return -1;
221 return migrate_paths(src, dst);
223 return finalize_object_file(src->buf, dst->buf);
226 static int migrate_paths(struct strbuf *src, struct strbuf *dst)
228 size_t src_len = src->len, dst_len = dst->len;
229 struct string_list paths = STRING_LIST_INIT_DUP;
230 int i;
231 int ret = 0;
233 if (read_dir_paths(&paths, src->buf) < 0)
234 return -1;
235 paths.cmp = pack_copy_cmp;
236 string_list_sort(&paths);
238 for (i = 0; i < paths.nr; i++) {
239 const char *name = paths.items[i].string;
241 strbuf_addf(src, "/%s", name);
242 strbuf_addf(dst, "/%s", name);
244 ret |= migrate_one(src, dst);
246 strbuf_setlen(src, src_len);
247 strbuf_setlen(dst, dst_len);
250 string_list_clear(&paths, 0);
251 return ret;
254 int tmp_objdir_migrate(struct tmp_objdir *t)
256 struct strbuf src = STRBUF_INIT, dst = STRBUF_INIT;
257 int ret;
259 if (!t)
260 return 0;
262 if (t->prev_odb) {
263 if (the_repository->objects->odb->will_destroy)
264 BUG("migrating an ODB that was marked for destruction");
265 restore_primary_odb(t->prev_odb, t->path.buf);
266 t->prev_odb = NULL;
269 strbuf_addbuf(&src, &t->path);
270 strbuf_addstr(&dst, get_object_directory());
272 ret = migrate_paths(&src, &dst);
274 strbuf_release(&src);
275 strbuf_release(&dst);
277 tmp_objdir_destroy(t);
278 return ret;
281 const char **tmp_objdir_env(const struct tmp_objdir *t)
283 if (!t)
284 return NULL;
285 return t->env.v;
288 void tmp_objdir_add_as_alternate(const struct tmp_objdir *t)
290 add_to_alternates_memory(t->path.buf);
293 void tmp_objdir_replace_primary_odb(struct tmp_objdir *t, int will_destroy)
295 if (t->prev_odb)
296 BUG("the primary object database is already replaced");
297 t->prev_odb = set_temporary_primary_odb(t->path.buf, will_destroy);
298 t->will_destroy = will_destroy;
301 struct tmp_objdir *tmp_objdir_unapply_primary_odb(void)
303 if (!the_tmp_objdir || !the_tmp_objdir->prev_odb)
304 return NULL;
306 restore_primary_odb(the_tmp_objdir->prev_odb, the_tmp_objdir->path.buf);
307 the_tmp_objdir->prev_odb = NULL;
308 return the_tmp_objdir;
311 void tmp_objdir_reapply_primary_odb(struct tmp_objdir *t, const char *old_cwd,
312 const char *new_cwd)
314 char *path;
316 path = reparent_relative_path(old_cwd, new_cwd, t->path.buf);
317 strbuf_reset(&t->path);
318 strbuf_addstr(&t->path, path);
319 free(path);
320 tmp_objdir_replace_primary_odb(t, t->will_destroy);