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