rebase --continue: refuse to commit after failed command
[alt-git.git] / tmp-objdir.c
blob5adad1925d1a051c38c08f911dd6d5824e5de305
1 #include "cache.h"
2 #include "tmp-objdir.h"
3 #include "abspath.h"
4 #include "chdir-notify.h"
5 #include "dir.h"
6 #include "environment.h"
7 #include "sigchain.h"
8 #include "string-list.h"
9 #include "strbuf.h"
10 #include "strvec.h"
11 #include "quote.h"
12 #include "object-store.h"
14 struct tmp_objdir {
15 struct strbuf path;
16 struct strvec env;
17 struct object_directory *prev_odb;
18 int will_destroy;
22 * Allow only one tmp_objdir at a time in a running process, which simplifies
23 * our atexit cleanup routines. It's doubtful callers will ever need
24 * more than one, and we can expand later if so. You can have many such
25 * tmp_objdirs simultaneously in many processes, of course.
27 static struct tmp_objdir *the_tmp_objdir;
29 static void tmp_objdir_free(struct tmp_objdir *t)
31 strbuf_release(&t->path);
32 strvec_clear(&t->env);
33 free(t);
36 int tmp_objdir_destroy(struct tmp_objdir *t)
38 int err;
40 if (!t)
41 return 0;
43 if (t == the_tmp_objdir)
44 the_tmp_objdir = NULL;
46 if (t->prev_odb)
47 restore_primary_odb(t->prev_odb, t->path.buf);
49 err = remove_dir_recursively(&t->path, 0);
51 tmp_objdir_free(t);
53 return err;
56 static void remove_tmp_objdir(void)
58 tmp_objdir_destroy(the_tmp_objdir);
61 void tmp_objdir_discard_objects(struct tmp_objdir *t)
63 remove_dir_recursively(&t->path, REMOVE_DIR_KEEP_TOPLEVEL);
67 * These env_* functions are for setting up the child environment; the
68 * "replace" variant overrides the value of any existing variable with that
69 * "key". The "append" variant puts our new value at the end of a list,
70 * separated by PATH_SEP (which is what separate values in
71 * GIT_ALTERNATE_OBJECT_DIRECTORIES).
73 static void env_append(struct strvec *env, const char *key, const char *val)
75 struct strbuf quoted = STRBUF_INIT;
76 const char *old;
79 * Avoid quoting if it's not necessary, for maximum compatibility
80 * with older parsers which don't understand the quoting.
82 if (*val == '"' || strchr(val, PATH_SEP)) {
83 strbuf_addch(&quoted, '"');
84 quote_c_style(val, &quoted, NULL, 1);
85 strbuf_addch(&quoted, '"');
86 val = quoted.buf;
89 old = getenv(key);
90 if (!old)
91 strvec_pushf(env, "%s=%s", key, val);
92 else
93 strvec_pushf(env, "%s=%s%c%s", key, old, PATH_SEP, val);
95 strbuf_release(&quoted);
98 static void env_replace(struct strvec *env, const char *key, const char *val)
100 strvec_pushf(env, "%s=%s", key, val);
103 static int setup_tmp_objdir(const char *root)
105 char *path;
106 int ret = 0;
108 path = xstrfmt("%s/pack", root);
109 ret = mkdir(path, 0777);
110 free(path);
112 return ret;
115 struct tmp_objdir *tmp_objdir_create(const char *prefix)
117 static int installed_handlers;
118 struct tmp_objdir *t;
120 if (the_tmp_objdir)
121 BUG("only one tmp_objdir can be used at a time");
123 t = xcalloc(1, sizeof(*t));
124 strbuf_init(&t->path, 0);
125 strvec_init(&t->env);
128 * Use a string starting with tmp_ so that the builtin/prune.c code
129 * can recognize any stale objdirs left behind by a crash and delete
130 * them.
132 strbuf_addf(&t->path, "%s/tmp_objdir-%s-XXXXXX", get_object_directory(), prefix);
134 if (!mkdtemp(t->path.buf)) {
135 /* free, not destroy, as we never touched the filesystem */
136 tmp_objdir_free(t);
137 return NULL;
140 the_tmp_objdir = t;
141 if (!installed_handlers) {
142 atexit(remove_tmp_objdir);
143 installed_handlers++;
146 if (setup_tmp_objdir(t->path.buf)) {
147 tmp_objdir_destroy(t);
148 return NULL;
151 env_append(&t->env, ALTERNATE_DB_ENVIRONMENT,
152 absolute_path(get_object_directory()));
153 env_replace(&t->env, DB_ENVIRONMENT, absolute_path(t->path.buf));
154 env_replace(&t->env, GIT_QUARANTINE_ENVIRONMENT,
155 absolute_path(t->path.buf));
157 return t;
161 * Make sure we copy packfiles and their associated metafiles in the correct
162 * order. All of these ends_with checks are slightly expensive to do in
163 * the midst of a sorting routine, but in practice it shouldn't matter.
164 * We will have a relatively small number of packfiles to order, and loose
165 * objects exit early in the first line.
167 static int pack_copy_priority(const char *name)
169 if (!starts_with(name, "pack"))
170 return 0;
171 if (ends_with(name, ".keep"))
172 return 1;
173 if (ends_with(name, ".pack"))
174 return 2;
175 if (ends_with(name, ".rev"))
176 return 3;
177 if (ends_with(name, ".idx"))
178 return 4;
179 return 5;
182 static int pack_copy_cmp(const char *a, const char *b)
184 return pack_copy_priority(a) - pack_copy_priority(b);
187 static int read_dir_paths(struct string_list *out, const char *path)
189 DIR *dh;
190 struct dirent *de;
192 dh = opendir(path);
193 if (!dh)
194 return -1;
196 while ((de = readdir(dh)))
197 if (de->d_name[0] != '.')
198 string_list_append(out, de->d_name);
200 closedir(dh);
201 return 0;
204 static int migrate_paths(struct strbuf *src, struct strbuf *dst);
206 static int migrate_one(struct strbuf *src, struct strbuf *dst)
208 struct stat st;
210 if (stat(src->buf, &st) < 0)
211 return -1;
212 if (S_ISDIR(st.st_mode)) {
213 if (!mkdir(dst->buf, 0777)) {
214 if (adjust_shared_perm(dst->buf))
215 return -1;
216 } else if (errno != EEXIST)
217 return -1;
218 return migrate_paths(src, dst);
220 return finalize_object_file(src->buf, dst->buf);
223 static int migrate_paths(struct strbuf *src, struct strbuf *dst)
225 size_t src_len = src->len, dst_len = dst->len;
226 struct string_list paths = STRING_LIST_INIT_DUP;
227 int i;
228 int ret = 0;
230 if (read_dir_paths(&paths, src->buf) < 0)
231 return -1;
232 paths.cmp = pack_copy_cmp;
233 string_list_sort(&paths);
235 for (i = 0; i < paths.nr; i++) {
236 const char *name = paths.items[i].string;
238 strbuf_addf(src, "/%s", name);
239 strbuf_addf(dst, "/%s", name);
241 ret |= migrate_one(src, dst);
243 strbuf_setlen(src, src_len);
244 strbuf_setlen(dst, dst_len);
247 string_list_clear(&paths, 0);
248 return ret;
251 int tmp_objdir_migrate(struct tmp_objdir *t)
253 struct strbuf src = STRBUF_INIT, dst = STRBUF_INIT;
254 int ret;
256 if (!t)
257 return 0;
259 if (t->prev_odb) {
260 if (the_repository->objects->odb->will_destroy)
261 BUG("migrating an ODB that was marked for destruction");
262 restore_primary_odb(t->prev_odb, t->path.buf);
263 t->prev_odb = NULL;
266 strbuf_addbuf(&src, &t->path);
267 strbuf_addstr(&dst, get_object_directory());
269 ret = migrate_paths(&src, &dst);
271 strbuf_release(&src);
272 strbuf_release(&dst);
274 tmp_objdir_destroy(t);
275 return ret;
278 const char **tmp_objdir_env(const struct tmp_objdir *t)
280 if (!t)
281 return NULL;
282 return t->env.v;
285 void tmp_objdir_add_as_alternate(const struct tmp_objdir *t)
287 add_to_alternates_memory(t->path.buf);
290 void tmp_objdir_replace_primary_odb(struct tmp_objdir *t, int will_destroy)
292 if (t->prev_odb)
293 BUG("the primary object database is already replaced");
294 t->prev_odb = set_temporary_primary_odb(t->path.buf, will_destroy);
295 t->will_destroy = will_destroy;
298 struct tmp_objdir *tmp_objdir_unapply_primary_odb(void)
300 if (!the_tmp_objdir || !the_tmp_objdir->prev_odb)
301 return NULL;
303 restore_primary_odb(the_tmp_objdir->prev_odb, the_tmp_objdir->path.buf);
304 the_tmp_objdir->prev_odb = NULL;
305 return the_tmp_objdir;
308 void tmp_objdir_reapply_primary_odb(struct tmp_objdir *t, const char *old_cwd,
309 const char *new_cwd)
311 char *path;
313 path = reparent_relative_path(old_cwd, new_cwd, t->path.buf);
314 strbuf_reset(&t->path);
315 strbuf_addstr(&t->path, path);
316 free(path);
317 tmp_objdir_replace_primary_odb(t, t->will_destroy);