]>
Commit | Line | Data |
---|---|---|
0a43fb22 JS |
1 | /* |
2 | * The Scalar command-line interface. | |
3 | */ | |
4 | ||
5 | #include "cache.h" | |
6 | #include "gettext.h" | |
7 | #include "parse-options.h" | |
d0feac4e DS |
8 | #include "config.h" |
9 | #include "run-command.h" | |
546f822d | 10 | #include "refs.h" |
d85ada7c MJC |
11 | #include "dir.h" |
12 | #include "packfile.h" | |
ddc35d83 | 13 | #include "help.h" |
d0feac4e DS |
14 | |
15 | /* | |
16 | * Remove the deepest subdirectory in the provided path string. Path must not | |
17 | * include a trailing path separator. Returns 1 if parent directory found, | |
18 | * otherwise 0. | |
19 | */ | |
20 | static int strbuf_parent_directory(struct strbuf *buf) | |
21 | { | |
22 | size_t len = buf->len; | |
23 | size_t offset = offset_1st_component(buf->buf); | |
24 | char *path_sep = find_last_dir_sep(buf->buf + offset); | |
25 | strbuf_setlen(buf, path_sep ? path_sep - buf->buf : offset); | |
26 | ||
27 | return buf->len < len; | |
28 | } | |
29 | ||
30 | static void setup_enlistment_directory(int argc, const char **argv, | |
31 | const char * const *usagestr, | |
32 | const struct option *options, | |
33 | struct strbuf *enlistment_root) | |
34 | { | |
35 | struct strbuf path = STRBUF_INIT; | |
36 | char *root; | |
37 | int enlistment_found = 0; | |
38 | ||
39 | if (startup_info->have_repository) | |
40 | BUG("gitdir already set up?!?"); | |
41 | ||
42 | if (argc > 1) | |
43 | usage_with_options(usagestr, options); | |
44 | ||
45 | /* find the worktree, determine its corresponding root */ | |
b4485574 | 46 | if (argc == 1) { |
d0feac4e | 47 | strbuf_add_absolute_path(&path, argv[0]); |
b4485574 JS |
48 | if (!is_directory(path.buf)) |
49 | die(_("'%s' does not exist"), path.buf); | |
50 | } else if (strbuf_getcwd(&path) < 0) | |
d0feac4e DS |
51 | die(_("need a working directory")); |
52 | ||
53 | strbuf_trim_trailing_dir_sep(&path); | |
54 | do { | |
55 | const size_t len = path.len; | |
56 | ||
57 | /* check if currently in enlistment root with src/ workdir */ | |
58 | strbuf_addstr(&path, "/src"); | |
59 | if (is_nonbare_repository_dir(&path)) { | |
60 | if (enlistment_root) | |
61 | strbuf_add(enlistment_root, path.buf, len); | |
62 | ||
63 | enlistment_found = 1; | |
64 | break; | |
65 | } | |
66 | ||
67 | /* reset to original path */ | |
68 | strbuf_setlen(&path, len); | |
69 | ||
70 | /* check if currently in workdir */ | |
71 | if (is_nonbare_repository_dir(&path)) { | |
72 | if (enlistment_root) { | |
73 | /* | |
74 | * If the worktree's directory's name is `src`, the enlistment is the | |
75 | * parent directory, otherwise it is identical to the worktree. | |
76 | */ | |
77 | root = strip_path_suffix(path.buf, "src"); | |
78 | strbuf_addstr(enlistment_root, root ? root : path.buf); | |
79 | free(root); | |
80 | } | |
81 | ||
82 | enlistment_found = 1; | |
83 | break; | |
84 | } | |
85 | } while (strbuf_parent_directory(&path)); | |
86 | ||
87 | if (!enlistment_found) | |
88 | die(_("could not find enlistment root")); | |
89 | ||
90 | if (chdir(path.buf) < 0) | |
91 | die_errno(_("could not switch to '%s'"), path.buf); | |
92 | ||
93 | strbuf_release(&path); | |
94 | setup_git_directory(); | |
95 | } | |
96 | ||
97 | static int run_git(const char *arg, ...) | |
98 | { | |
99 | struct strvec argv = STRVEC_INIT; | |
100 | va_list args; | |
101 | const char *p; | |
102 | int res; | |
103 | ||
104 | va_start(args, arg); | |
105 | strvec_push(&argv, arg); | |
106 | while ((p = va_arg(args, const char *))) | |
107 | strvec_push(&argv, p); | |
108 | va_end(args); | |
109 | ||
110 | res = run_command_v_opt(argv.v, RUN_GIT_CMD); | |
111 | ||
112 | strvec_clear(&argv); | |
113 | return res; | |
114 | } | |
115 | ||
cb59d55e | 116 | static int set_recommended_config(int reconfigure) |
d0feac4e DS |
117 | { |
118 | struct { | |
119 | const char *key; | |
120 | const char *value; | |
cb59d55e | 121 | int overwrite_on_reconfigure; |
d0feac4e | 122 | } config[] = { |
cb59d55e JS |
123 | /* Required */ |
124 | { "am.keepCR", "true", 1 }, | |
125 | { "core.FSCache", "true", 1 }, | |
126 | { "core.multiPackIndex", "true", 1 }, | |
127 | { "core.preloadIndex", "true", 1 }, | |
d0feac4e | 128 | #ifndef WIN32 |
cb59d55e | 129 | { "core.untrackedCache", "true", 1 }, |
d0feac4e DS |
130 | #else |
131 | /* | |
132 | * Unfortunately, Scalar's Functional Tests demonstrated | |
133 | * that the untracked cache feature is unreliable on Windows | |
134 | * (which is a bummer because that platform would benefit the | |
135 | * most from it). For some reason, freshly created files seem | |
136 | * not to update the directory's `lastModified` time | |
137 | * immediately, but the untracked cache would need to rely on | |
138 | * that. | |
139 | * | |
140 | * Therefore, with a sad heart, we disable this very useful | |
141 | * feature on Windows. | |
142 | */ | |
cb59d55e | 143 | { "core.untrackedCache", "false", 1 }, |
d0feac4e | 144 | #endif |
cb59d55e JS |
145 | { "core.logAllRefUpdates", "true", 1 }, |
146 | { "credential.https://dev.azure.com.useHttpPath", "true", 1 }, | |
147 | { "credential.validate", "false", 1 }, /* GCM4W-only */ | |
148 | { "gc.auto", "0", 1 }, | |
149 | { "gui.GCWarning", "false", 1 }, | |
150 | { "index.threads", "true", 1 }, | |
151 | { "index.version", "4", 1 }, | |
152 | { "merge.stat", "false", 1 }, | |
153 | { "merge.renames", "true", 1 }, | |
154 | { "pack.useBitmaps", "false", 1 }, | |
155 | { "pack.useSparse", "true", 1 }, | |
156 | { "receive.autoGC", "false", 1 }, | |
cb59d55e JS |
157 | { "feature.manyFiles", "false", 1 }, |
158 | { "feature.experimental", "false", 1 }, | |
159 | { "fetch.unpackLimit", "1", 1 }, | |
160 | { "fetch.writeCommitGraph", "false", 1 }, | |
d0feac4e | 161 | #ifdef WIN32 |
cb59d55e | 162 | { "http.sslBackend", "schannel", 1 }, |
d0feac4e | 163 | #endif |
cb59d55e | 164 | /* Optional */ |
d0feac4e DS |
165 | { "status.aheadBehind", "false" }, |
166 | { "commitGraph.generationVersion", "1" }, | |
167 | { "core.autoCRLF", "false" }, | |
168 | { "core.safeCRLF", "false" }, | |
169 | { "fetch.showForcedUpdates", "false" }, | |
170 | { NULL, NULL }, | |
171 | }; | |
172 | int i; | |
173 | char *value; | |
174 | ||
175 | for (i = 0; config[i].key; i++) { | |
cb59d55e JS |
176 | if ((reconfigure && config[i].overwrite_on_reconfigure) || |
177 | git_config_get_string(config[i].key, &value)) { | |
d0feac4e DS |
178 | trace2_data_string("scalar", the_repository, config[i].key, "created"); |
179 | if (git_config_set_gently(config[i].key, | |
180 | config[i].value) < 0) | |
181 | return error(_("could not configure %s=%s"), | |
182 | config[i].key, config[i].value); | |
183 | } else { | |
184 | trace2_data_string("scalar", the_repository, config[i].key, "exists"); | |
185 | free(value); | |
186 | } | |
187 | } | |
188 | ||
189 | /* | |
190 | * The `log.excludeDecoration` setting is special because it allows | |
191 | * for multiple values. | |
192 | */ | |
193 | if (git_config_get_string("log.excludeDecoration", &value)) { | |
194 | trace2_data_string("scalar", the_repository, | |
195 | "log.excludeDecoration", "created"); | |
196 | if (git_config_set_multivar_gently("log.excludeDecoration", | |
197 | "refs/prefetch/*", | |
198 | CONFIG_REGEX_NONE, 0)) | |
199 | return error(_("could not configure " | |
200 | "log.excludeDecoration")); | |
201 | } else { | |
202 | trace2_data_string("scalar", the_repository, | |
203 | "log.excludeDecoration", "exists"); | |
204 | free(value); | |
205 | } | |
206 | ||
207 | return 0; | |
208 | } | |
209 | ||
c76a53eb | 210 | static int toggle_maintenance(int enable) |
d0feac4e | 211 | { |
c76a53eb | 212 | return run_git("maintenance", enable ? "start" : "unregister", NULL); |
d0feac4e DS |
213 | } |
214 | ||
c76a53eb | 215 | static int add_or_remove_enlistment(int add) |
d0feac4e DS |
216 | { |
217 | int res; | |
218 | ||
219 | if (!the_repository->worktree) | |
220 | die(_("Scalar enlistments require a worktree")); | |
221 | ||
222 | res = run_git("config", "--global", "--get", "--fixed-value", | |
223 | "scalar.repo", the_repository->worktree, NULL); | |
224 | ||
225 | /* | |
c76a53eb DS |
226 | * If we want to add and the setting is already there, then do nothing. |
227 | * If we want to remove and the setting is not there, then do nothing. | |
d0feac4e | 228 | */ |
c76a53eb | 229 | if ((add && !res) || (!add && res)) |
d0feac4e DS |
230 | return 0; |
231 | ||
c76a53eb DS |
232 | return run_git("config", "--global", add ? "--add" : "--unset", |
233 | add ? "--no-fixed-value" : "--fixed-value", | |
d0feac4e DS |
234 | "scalar.repo", the_repository->worktree, NULL); |
235 | } | |
236 | ||
237 | static int register_dir(void) | |
238 | { | |
c76a53eb | 239 | int res = add_or_remove_enlistment(1); |
d0feac4e DS |
240 | |
241 | if (!res) | |
cb59d55e | 242 | res = set_recommended_config(0); |
d0feac4e DS |
243 | |
244 | if (!res) | |
c76a53eb DS |
245 | res = toggle_maintenance(1); |
246 | ||
247 | return res; | |
248 | } | |
249 | ||
250 | static int unregister_dir(void) | |
251 | { | |
252 | int res = 0; | |
253 | ||
254 | if (toggle_maintenance(0) < 0) | |
255 | res = -1; | |
256 | ||
257 | if (add_or_remove_enlistment(0) < 0) | |
258 | res = -1; | |
d0feac4e DS |
259 | |
260 | return res; | |
261 | } | |
262 | ||
546f822d JS |
263 | /* printf-style interface, expects `<key>=<value>` argument */ |
264 | static int set_config(const char *fmt, ...) | |
265 | { | |
266 | struct strbuf buf = STRBUF_INIT; | |
267 | char *value; | |
268 | int res; | |
269 | va_list args; | |
270 | ||
271 | va_start(args, fmt); | |
272 | strbuf_vaddf(&buf, fmt, args); | |
273 | va_end(args); | |
274 | ||
275 | value = strchr(buf.buf, '='); | |
276 | if (value) | |
277 | *(value++) = '\0'; | |
278 | res = git_config_set_gently(buf.buf, value); | |
279 | strbuf_release(&buf); | |
280 | ||
281 | return res; | |
282 | } | |
283 | ||
284 | static char *remote_default_branch(const char *url) | |
285 | { | |
286 | struct child_process cp = CHILD_PROCESS_INIT; | |
287 | struct strbuf out = STRBUF_INIT; | |
288 | ||
289 | cp.git_cmd = 1; | |
290 | strvec_pushl(&cp.args, "ls-remote", "--symref", url, "HEAD", NULL); | |
291 | if (!pipe_command(&cp, NULL, 0, &out, 0, NULL, 0)) { | |
292 | const char *line = out.buf; | |
293 | ||
294 | while (*line) { | |
295 | const char *eol = strchrnul(line, '\n'), *p; | |
296 | size_t len = eol - line; | |
297 | char *branch; | |
298 | ||
299 | if (!skip_prefix(line, "ref: ", &p) || | |
300 | !strip_suffix_mem(line, &len, "\tHEAD")) { | |
301 | line = eol + (*eol == '\n'); | |
302 | continue; | |
303 | } | |
304 | ||
305 | eol = line + len; | |
306 | if (skip_prefix(p, "refs/heads/", &p)) { | |
307 | branch = xstrndup(p, eol - p); | |
308 | strbuf_release(&out); | |
309 | return branch; | |
310 | } | |
311 | ||
312 | error(_("remote HEAD is not a branch: '%.*s'"), | |
313 | (int)(eol - p), p); | |
314 | strbuf_release(&out); | |
315 | return NULL; | |
316 | } | |
317 | } | |
318 | warning(_("failed to get default branch name from remote; " | |
319 | "using local default")); | |
320 | strbuf_reset(&out); | |
321 | ||
322 | child_process_init(&cp); | |
323 | cp.git_cmd = 1; | |
324 | strvec_pushl(&cp.args, "symbolic-ref", "--short", "HEAD", NULL); | |
325 | if (!pipe_command(&cp, NULL, 0, &out, 0, NULL, 0)) { | |
326 | strbuf_trim(&out); | |
327 | return strbuf_detach(&out, NULL); | |
328 | } | |
329 | ||
330 | strbuf_release(&out); | |
331 | error(_("failed to get default branch name")); | |
332 | return NULL; | |
333 | } | |
334 | ||
d85ada7c MJC |
335 | static int delete_enlistment(struct strbuf *enlistment) |
336 | { | |
337 | #ifdef WIN32 | |
338 | struct strbuf parent = STRBUF_INIT; | |
339 | #endif | |
340 | ||
341 | if (unregister_dir()) | |
342 | die(_("failed to unregister repository")); | |
343 | ||
344 | #ifdef WIN32 | |
345 | /* | |
346 | * Change the current directory to one outside of the enlistment so | |
347 | * that we may delete everything underneath it. | |
348 | */ | |
349 | strbuf_addbuf(&parent, enlistment); | |
350 | strbuf_parent_directory(&parent); | |
351 | if (chdir(parent.buf) < 0) | |
352 | die_errno(_("could not switch to '%s'"), parent.buf); | |
353 | strbuf_release(&parent); | |
354 | #endif | |
355 | ||
356 | if (remove_dir_recursively(enlistment, 0)) | |
357 | die(_("failed to delete enlistment directory")); | |
358 | ||
359 | return 0; | |
360 | } | |
361 | ||
ddc35d83 JS |
362 | /* |
363 | * Dummy implementation; Using `get_version_info()` would cause a link error | |
364 | * without this. | |
365 | */ | |
366 | void load_builtin_commands(const char *prefix, struct cmdnames *cmds) | |
367 | { | |
368 | die("not implemented"); | |
369 | } | |
370 | ||
546f822d JS |
371 | static int cmd_clone(int argc, const char **argv) |
372 | { | |
373 | const char *branch = NULL; | |
4368e40b | 374 | int full_clone = 0, single_branch = 0; |
546f822d JS |
375 | struct option clone_options[] = { |
376 | OPT_STRING('b', "branch", &branch, N_("<branch>"), | |
377 | N_("branch to checkout after clone")), | |
378 | OPT_BOOL(0, "full-clone", &full_clone, | |
379 | N_("when cloning, create full working directory")), | |
4368e40b JS |
380 | OPT_BOOL(0, "single-branch", &single_branch, |
381 | N_("only download metadata for the branch that will " | |
382 | "be checked out")), | |
546f822d JS |
383 | OPT_END(), |
384 | }; | |
385 | const char * const clone_usage[] = { | |
386 | N_("scalar clone [<options>] [--] <repo> [<dir>]"), | |
387 | NULL | |
388 | }; | |
389 | const char *url; | |
390 | char *enlistment = NULL, *dir = NULL; | |
391 | struct strbuf buf = STRBUF_INIT; | |
392 | int res; | |
393 | ||
394 | argc = parse_options(argc, argv, NULL, clone_options, clone_usage, 0); | |
395 | ||
396 | if (argc == 2) { | |
397 | url = argv[0]; | |
398 | enlistment = xstrdup(argv[1]); | |
399 | } else if (argc == 1) { | |
400 | url = argv[0]; | |
401 | ||
402 | strbuf_addstr(&buf, url); | |
403 | /* Strip trailing slashes, if any */ | |
404 | while (buf.len > 0 && is_dir_sep(buf.buf[buf.len - 1])) | |
405 | strbuf_setlen(&buf, buf.len - 1); | |
406 | /* Strip suffix `.git`, if any */ | |
407 | strbuf_strip_suffix(&buf, ".git"); | |
408 | ||
409 | enlistment = find_last_dir_sep(buf.buf); | |
410 | if (!enlistment) { | |
411 | die(_("cannot deduce worktree name from '%s'"), url); | |
412 | } | |
413 | enlistment = xstrdup(enlistment + 1); | |
414 | } else { | |
415 | usage_msg_opt(_("You must specify a repository to clone."), | |
416 | clone_usage, clone_options); | |
417 | } | |
418 | ||
419 | if (is_directory(enlistment)) | |
420 | die(_("directory '%s' exists already"), enlistment); | |
421 | ||
422 | dir = xstrfmt("%s/src", enlistment); | |
423 | ||
424 | strbuf_reset(&buf); | |
425 | if (branch) | |
426 | strbuf_addf(&buf, "init.defaultBranch=%s", branch); | |
427 | else { | |
428 | char *b = repo_default_branch_name(the_repository, 1); | |
429 | strbuf_addf(&buf, "init.defaultBranch=%s", b); | |
430 | free(b); | |
431 | } | |
432 | ||
433 | if ((res = run_git("-c", buf.buf, "init", "--", dir, NULL))) | |
434 | goto cleanup; | |
435 | ||
436 | if (chdir(dir) < 0) { | |
437 | res = error_errno(_("could not switch to '%s'"), dir); | |
438 | goto cleanup; | |
439 | } | |
440 | ||
441 | setup_git_directory(); | |
442 | ||
443 | /* common-main already logs `argv` */ | |
444 | trace2_def_repo(the_repository); | |
445 | ||
446 | if (!branch && !(branch = remote_default_branch(url))) { | |
447 | res = error(_("failed to get default branch for '%s'"), url); | |
448 | goto cleanup; | |
449 | } | |
450 | ||
451 | if (set_config("remote.origin.url=%s", url) || | |
452 | set_config("remote.origin.fetch=" | |
4368e40b JS |
453 | "+refs/heads/%s:refs/remotes/origin/%s", |
454 | single_branch ? branch : "*", | |
455 | single_branch ? branch : "*") || | |
546f822d JS |
456 | set_config("remote.origin.promisor=true") || |
457 | set_config("remote.origin.partialCloneFilter=blob:none")) { | |
458 | res = error(_("could not configure remote in '%s'"), dir); | |
459 | goto cleanup; | |
460 | } | |
461 | ||
462 | if (!full_clone && | |
463 | (res = run_git("sparse-checkout", "init", "--cone", NULL))) | |
464 | goto cleanup; | |
465 | ||
cb59d55e | 466 | if (set_recommended_config(0)) |
546f822d JS |
467 | return error(_("could not configure '%s'"), dir); |
468 | ||
469 | if ((res = run_git("fetch", "--quiet", "origin", NULL))) { | |
470 | warning(_("partial clone failed; attempting full clone")); | |
471 | ||
472 | if (set_config("remote.origin.promisor") || | |
473 | set_config("remote.origin.partialCloneFilter")) { | |
474 | res = error(_("could not configure for full clone")); | |
475 | goto cleanup; | |
476 | } | |
477 | ||
478 | if ((res = run_git("fetch", "--quiet", "origin", NULL))) | |
479 | goto cleanup; | |
480 | } | |
481 | ||
482 | if ((res = set_config("branch.%s.remote=origin", branch))) | |
483 | goto cleanup; | |
484 | if ((res = set_config("branch.%s.merge=refs/heads/%s", | |
485 | branch, branch))) | |
486 | goto cleanup; | |
487 | ||
488 | strbuf_reset(&buf); | |
489 | strbuf_addf(&buf, "origin/%s", branch); | |
490 | res = run_git("checkout", "-f", "-t", buf.buf, NULL); | |
491 | if (res) | |
492 | goto cleanup; | |
493 | ||
494 | res = register_dir(); | |
495 | ||
496 | cleanup: | |
497 | free(enlistment); | |
498 | free(dir); | |
499 | strbuf_release(&buf); | |
500 | return res; | |
501 | } | |
502 | ||
2b710457 DS |
503 | static int cmd_list(int argc, const char **argv) |
504 | { | |
505 | if (argc != 1) | |
506 | die(_("`scalar list` does not take arguments")); | |
507 | ||
508 | if (run_git("config", "--global", "--get-all", "scalar.repo", NULL) < 0) | |
509 | return -1; | |
510 | return 0; | |
511 | } | |
512 | ||
d0feac4e DS |
513 | static int cmd_register(int argc, const char **argv) |
514 | { | |
515 | struct option options[] = { | |
516 | OPT_END(), | |
517 | }; | |
518 | const char * const usage[] = { | |
519 | N_("scalar register [<enlistment>]"), | |
520 | NULL | |
521 | }; | |
522 | ||
523 | argc = parse_options(argc, argv, NULL, options, | |
524 | usage, 0); | |
525 | ||
526 | setup_enlistment_directory(argc, argv, usage, options, NULL); | |
527 | ||
528 | return register_dir(); | |
529 | } | |
0a43fb22 | 530 | |
45826760 JS |
531 | static int get_scalar_repos(const char *key, const char *value, void *data) |
532 | { | |
533 | struct string_list *list = data; | |
534 | ||
535 | if (!strcmp(key, "scalar.repo")) | |
536 | string_list_append(list, value); | |
537 | ||
538 | return 0; | |
539 | } | |
540 | ||
cb59d55e JS |
541 | static int cmd_reconfigure(int argc, const char **argv) |
542 | { | |
45826760 | 543 | int all = 0; |
cb59d55e | 544 | struct option options[] = { |
45826760 JS |
545 | OPT_BOOL('a', "all", &all, |
546 | N_("reconfigure all registered enlistments")), | |
cb59d55e JS |
547 | OPT_END(), |
548 | }; | |
549 | const char * const usage[] = { | |
45826760 | 550 | N_("scalar reconfigure [--all | <enlistment>]"), |
cb59d55e JS |
551 | NULL |
552 | }; | |
45826760 JS |
553 | struct string_list scalar_repos = STRING_LIST_INIT_DUP; |
554 | int i, res = 0; | |
555 | struct repository r = { NULL }; | |
556 | struct strbuf commondir = STRBUF_INIT, gitdir = STRBUF_INIT; | |
cb59d55e JS |
557 | |
558 | argc = parse_options(argc, argv, NULL, options, | |
559 | usage, 0); | |
560 | ||
45826760 JS |
561 | if (!all) { |
562 | setup_enlistment_directory(argc, argv, usage, options, NULL); | |
563 | ||
564 | return set_recommended_config(1); | |
565 | } | |
566 | ||
567 | if (argc > 0) | |
568 | usage_msg_opt(_("--all or <enlistment>, but not both"), | |
569 | usage, options); | |
570 | ||
571 | git_config(get_scalar_repos, &scalar_repos); | |
cb59d55e | 572 | |
45826760 JS |
573 | for (i = 0; i < scalar_repos.nr; i++) { |
574 | const char *dir = scalar_repos.items[i].string; | |
575 | ||
576 | strbuf_reset(&commondir); | |
577 | strbuf_reset(&gitdir); | |
578 | ||
579 | if (chdir(dir) < 0) { | |
580 | warning_errno(_("could not switch to '%s'"), dir); | |
581 | res = -1; | |
582 | } else if (discover_git_directory(&commondir, &gitdir) < 0) { | |
583 | warning_errno(_("git repository gone in '%s'"), dir); | |
584 | res = -1; | |
585 | } else { | |
586 | git_config_clear(); | |
587 | ||
588 | the_repository = &r; | |
589 | r.commondir = commondir.buf; | |
590 | r.gitdir = gitdir.buf; | |
591 | ||
592 | if (set_recommended_config(1) < 0) | |
593 | res = -1; | |
594 | } | |
595 | } | |
596 | ||
597 | string_list_clear(&scalar_repos, 1); | |
598 | strbuf_release(&commondir); | |
599 | strbuf_release(&gitdir); | |
600 | ||
601 | return res; | |
cb59d55e JS |
602 | } |
603 | ||
7020c88c DS |
604 | static int cmd_run(int argc, const char **argv) |
605 | { | |
606 | struct option options[] = { | |
607 | OPT_END(), | |
608 | }; | |
609 | struct { | |
610 | const char *arg, *task; | |
611 | } tasks[] = { | |
612 | { "config", NULL }, | |
613 | { "commit-graph", "commit-graph" }, | |
614 | { "fetch", "prefetch" }, | |
615 | { "loose-objects", "loose-objects" }, | |
616 | { "pack-files", "incremental-repack" }, | |
617 | { NULL, NULL } | |
618 | }; | |
619 | struct strbuf buf = STRBUF_INIT; | |
620 | const char *usagestr[] = { NULL, NULL }; | |
621 | int i; | |
622 | ||
623 | strbuf_addstr(&buf, N_("scalar run <task> [<enlistment>]\nTasks:\n")); | |
624 | for (i = 0; tasks[i].arg; i++) | |
625 | strbuf_addf(&buf, "\t%s\n", tasks[i].arg); | |
626 | usagestr[0] = buf.buf; | |
627 | ||
628 | argc = parse_options(argc, argv, NULL, options, | |
629 | usagestr, 0); | |
630 | ||
631 | if (!argc) | |
632 | usage_with_options(usagestr, options); | |
633 | ||
634 | if (!strcmp("all", argv[0])) { | |
635 | i = -1; | |
636 | } else { | |
637 | for (i = 0; tasks[i].arg && strcmp(tasks[i].arg, argv[0]); i++) | |
638 | ; /* keep looking for the task */ | |
639 | ||
640 | if (i > 0 && !tasks[i].arg) { | |
641 | error(_("no such task: '%s'"), argv[0]); | |
642 | usage_with_options(usagestr, options); | |
643 | } | |
644 | } | |
645 | ||
646 | argc--; | |
647 | argv++; | |
648 | setup_enlistment_directory(argc, argv, usagestr, options, NULL); | |
649 | strbuf_release(&buf); | |
650 | ||
651 | if (i == 0) | |
652 | return register_dir(); | |
653 | ||
654 | if (i > 0) | |
655 | return run_git("maintenance", "run", | |
656 | "--task", tasks[i].task, NULL); | |
657 | ||
658 | if (register_dir()) | |
659 | return -1; | |
660 | for (i = 1; tasks[i].arg; i++) | |
661 | if (run_git("maintenance", "run", | |
662 | "--task", tasks[i].task, NULL)) | |
663 | return -1; | |
664 | return 0; | |
665 | } | |
666 | ||
f5f0842d JS |
667 | static int remove_deleted_enlistment(struct strbuf *path) |
668 | { | |
669 | int res = 0; | |
670 | strbuf_realpath_forgiving(path, path->buf, 1); | |
671 | ||
672 | if (run_git("config", "--global", | |
673 | "--unset", "--fixed-value", | |
674 | "scalar.repo", path->buf, NULL) < 0) | |
675 | res = -1; | |
676 | ||
677 | if (run_git("config", "--global", | |
678 | "--unset", "--fixed-value", | |
679 | "maintenance.repo", path->buf, NULL) < 0) | |
680 | res = -1; | |
681 | ||
682 | return res; | |
683 | } | |
684 | ||
c76a53eb DS |
685 | static int cmd_unregister(int argc, const char **argv) |
686 | { | |
687 | struct option options[] = { | |
688 | OPT_END(), | |
689 | }; | |
690 | const char * const usage[] = { | |
691 | N_("scalar unregister [<enlistment>]"), | |
692 | NULL | |
693 | }; | |
694 | ||
695 | argc = parse_options(argc, argv, NULL, options, | |
696 | usage, 0); | |
697 | ||
f5f0842d JS |
698 | /* |
699 | * Be forgiving when the enlistment or worktree does not even exist any | |
700 | * longer; This can be the case if a user deleted the worktree by | |
701 | * mistake and _still_ wants to unregister the thing. | |
702 | */ | |
703 | if (argc == 1) { | |
704 | struct strbuf src_path = STRBUF_INIT, workdir_path = STRBUF_INIT; | |
705 | ||
706 | strbuf_addf(&src_path, "%s/src/.git", argv[0]); | |
707 | strbuf_addf(&workdir_path, "%s/.git", argv[0]); | |
708 | if (!is_directory(src_path.buf) && !is_directory(workdir_path.buf)) { | |
709 | /* remove possible matching registrations */ | |
710 | int res = -1; | |
711 | ||
712 | strbuf_strip_suffix(&src_path, "/.git"); | |
713 | res = remove_deleted_enlistment(&src_path) && res; | |
714 | ||
715 | strbuf_strip_suffix(&workdir_path, "/.git"); | |
716 | res = remove_deleted_enlistment(&workdir_path) && res; | |
717 | ||
718 | strbuf_release(&src_path); | |
719 | strbuf_release(&workdir_path); | |
720 | return res; | |
721 | } | |
722 | strbuf_release(&src_path); | |
723 | strbuf_release(&workdir_path); | |
724 | } | |
725 | ||
c76a53eb DS |
726 | setup_enlistment_directory(argc, argv, usage, options, NULL); |
727 | ||
728 | return unregister_dir(); | |
729 | } | |
730 | ||
d85ada7c MJC |
731 | static int cmd_delete(int argc, const char **argv) |
732 | { | |
733 | char *cwd = xgetcwd(); | |
734 | struct option options[] = { | |
735 | OPT_END(), | |
736 | }; | |
737 | const char * const usage[] = { | |
738 | N_("scalar delete <enlistment>"), | |
739 | NULL | |
740 | }; | |
741 | struct strbuf enlistment = STRBUF_INIT; | |
742 | int res = 0; | |
743 | ||
744 | argc = parse_options(argc, argv, NULL, options, | |
745 | usage, 0); | |
746 | ||
747 | if (argc != 1) | |
748 | usage_with_options(usage, options); | |
749 | ||
750 | setup_enlistment_directory(argc, argv, usage, options, &enlistment); | |
751 | ||
752 | if (dir_inside_of(cwd, enlistment.buf) >= 0) | |
753 | res = error(_("refusing to delete current working directory")); | |
754 | else { | |
755 | close_object_store(the_repository->objects); | |
756 | res = delete_enlistment(&enlistment); | |
757 | } | |
758 | strbuf_release(&enlistment); | |
759 | free(cwd); | |
760 | ||
761 | return res; | |
762 | } | |
763 | ||
ddc35d83 JS |
764 | static int cmd_version(int argc, const char **argv) |
765 | { | |
766 | int verbose = 0, build_options = 0; | |
767 | struct option options[] = { | |
768 | OPT__VERBOSE(&verbose, N_("include Git version")), | |
769 | OPT_BOOL(0, "build-options", &build_options, | |
770 | N_("include Git's build options")), | |
771 | OPT_END(), | |
772 | }; | |
773 | const char * const usage[] = { | |
774 | N_("scalar verbose [-v | --verbose] [--build-options]"), | |
775 | NULL | |
776 | }; | |
777 | struct strbuf buf = STRBUF_INIT; | |
778 | ||
779 | argc = parse_options(argc, argv, NULL, options, | |
780 | usage, 0); | |
781 | ||
782 | if (argc != 0) | |
783 | usage_with_options(usage, options); | |
784 | ||
785 | get_version_info(&buf, build_options); | |
786 | fprintf(stderr, "%s\n", buf.buf); | |
787 | strbuf_release(&buf); | |
788 | ||
789 | return 0; | |
790 | } | |
791 | ||
0a43fb22 JS |
792 | static struct { |
793 | const char *name; | |
794 | int (*fn)(int, const char **); | |
795 | } builtins[] = { | |
546f822d | 796 | { "clone", cmd_clone }, |
2b710457 | 797 | { "list", cmd_list }, |
d0feac4e | 798 | { "register", cmd_register }, |
c76a53eb | 799 | { "unregister", cmd_unregister }, |
7020c88c | 800 | { "run", cmd_run }, |
cb59d55e | 801 | { "reconfigure", cmd_reconfigure }, |
d85ada7c | 802 | { "delete", cmd_delete }, |
ddc35d83 | 803 | { "version", cmd_version }, |
0a43fb22 JS |
804 | { NULL, NULL}, |
805 | }; | |
806 | ||
807 | int cmd_main(int argc, const char **argv) | |
808 | { | |
809 | struct strbuf scalar_usage = STRBUF_INIT; | |
810 | int i; | |
811 | ||
2ae8eb5d JS |
812 | while (argc > 1 && *argv[1] == '-') { |
813 | if (!strcmp(argv[1], "-C")) { | |
814 | if (argc < 3) | |
815 | die(_("-C requires a <directory>")); | |
816 | if (chdir(argv[2]) < 0) | |
817 | die_errno(_("could not change to '%s'"), | |
818 | argv[2]); | |
819 | argc -= 2; | |
820 | argv += 2; | |
821 | } else if (!strcmp(argv[1], "-c")) { | |
822 | if (argc < 3) | |
823 | die(_("-c requires a <key>=<value> argument")); | |
824 | git_config_push_parameter(argv[2]); | |
825 | argc -= 2; | |
826 | argv += 2; | |
827 | } else | |
828 | break; | |
829 | } | |
830 | ||
0a43fb22 JS |
831 | if (argc > 1) { |
832 | argv++; | |
833 | argc--; | |
834 | ||
835 | for (i = 0; builtins[i].name; i++) | |
836 | if (!strcmp(builtins[i].name, argv[0])) | |
837 | return !!builtins[i].fn(argc, argv); | |
838 | } | |
839 | ||
840 | strbuf_addstr(&scalar_usage, | |
2ae8eb5d JS |
841 | N_("scalar [-C <directory>] [-c <key>=<value>] " |
842 | "<command> [<options>]\n\nCommands:\n")); | |
0a43fb22 JS |
843 | for (i = 0; builtins[i].name; i++) |
844 | strbuf_addf(&scalar_usage, "\t%s\n", builtins[i].name); | |
845 | ||
846 | usage(scalar_usage.buf); | |
847 | } |