sequencer: simplify building argument list in do_exec()
[git/debian.git] / scalar.c
blob6de9c0ee523abe56881d9d0158b8ff6a7e07c866
1 /*
2 * The Scalar command-line interface.
3 */
5 #include "cache.h"
6 #include "gettext.h"
7 #include "parse-options.h"
8 #include "config.h"
9 #include "run-command.h"
10 #include "simple-ipc.h"
11 #include "fsmonitor-ipc.h"
12 #include "fsmonitor-settings.h"
13 #include "refs.h"
14 #include "dir.h"
15 #include "packfile.h"
16 #include "help.h"
18 static void setup_enlistment_directory(int argc, const char **argv,
19 const char * const *usagestr,
20 const struct option *options,
21 struct strbuf *enlistment_root)
23 struct strbuf path = STRBUF_INIT;
24 int enlistment_is_repo_parent = 0;
25 size_t len;
27 if (startup_info->have_repository)
28 BUG("gitdir already set up?!?");
30 if (argc > 1)
31 usage_with_options(usagestr, options);
33 /* find the worktree, determine its corresponding root */
34 if (argc == 1) {
35 strbuf_add_absolute_path(&path, argv[0]);
36 if (!is_directory(path.buf))
37 die(_("'%s' does not exist"), path.buf);
38 if (chdir(path.buf) < 0)
39 die_errno(_("could not switch to '%s'"), path.buf);
40 } else if (strbuf_getcwd(&path) < 0)
41 die(_("need a working directory"));
43 strbuf_trim_trailing_dir_sep(&path);
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);
53 strbuf_setlen(&path, len);
55 setup_git_directory();
57 if (!the_repository->worktree)
58 die(_("Scalar enlistments require a worktree"));
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);
67 strbuf_release(&path);
70 static int run_git(const char *arg, ...)
72 struct strvec argv = STRVEC_INIT;
73 va_list args;
74 const char *p;
75 int res;
77 va_start(args, arg);
78 strvec_push(&argv, arg);
79 while ((p = va_arg(args, const char *)))
80 strvec_push(&argv, p);
81 va_end(args);
83 res = run_command_v_opt(argv.v, RUN_GIT_CMD);
85 strvec_clear(&argv);
86 return res;
89 struct scalar_config {
90 const char *key;
91 const char *value;
92 int overwrite_on_reconfigure;
95 static int set_scalar_config(const struct scalar_config *config, int reconfigure)
97 char *value = NULL;
98 int res;
100 if ((reconfigure && config->overwrite_on_reconfigure) ||
101 git_config_get_string(config->key, &value)) {
102 trace2_data_string("scalar", the_repository, config->key, "created");
103 res = git_config_set_gently(config->key, config->value);
104 } else {
105 trace2_data_string("scalar", the_repository, config->key, "exists");
106 res = 0;
109 free(value);
110 return res;
113 static int have_fsmonitor_support(void)
115 return fsmonitor_ipc__is_supported() &&
116 fsm_settings__get_reason(the_repository) == FSMONITOR_REASON_OK;
119 static int set_recommended_config(int reconfigure)
121 struct scalar_config config[] = {
122 /* Required */
123 { "am.keepCR", "true", 1 },
124 { "core.FSCache", "true", 1 },
125 { "core.multiPackIndex", "true", 1 },
126 { "core.preloadIndex", "true", 1 },
127 #ifndef WIN32
128 { "core.untrackedCache", "true", 1 },
129 #else
131 * Unfortunately, Scalar's Functional Tests demonstrated
132 * that the untracked cache feature is unreliable on Windows
133 * (which is a bummer because that platform would benefit the
134 * most from it). For some reason, freshly created files seem
135 * not to update the directory's `lastModified` time
136 * immediately, but the untracked cache would need to rely on
137 * that.
139 * Therefore, with a sad heart, we disable this very useful
140 * feature on Windows.
142 { "core.untrackedCache", "false", 1 },
143 #endif
144 { "core.logAllRefUpdates", "true", 1 },
145 { "credential.https://dev.azure.com.useHttpPath", "true", 1 },
146 { "credential.validate", "false", 1 }, /* GCM4W-only */
147 { "gc.auto", "0", 1 },
148 { "gui.GCWarning", "false", 1 },
149 { "index.threads", "true", 1 },
150 { "index.version", "4", 1 },
151 { "merge.stat", "false", 1 },
152 { "merge.renames", "true", 1 },
153 { "pack.useBitmaps", "false", 1 },
154 { "pack.useSparse", "true", 1 },
155 { "receive.autoGC", "false", 1 },
156 { "feature.manyFiles", "false", 1 },
157 { "feature.experimental", "false", 1 },
158 { "fetch.unpackLimit", "1", 1 },
159 { "fetch.writeCommitGraph", "false", 1 },
160 #ifdef WIN32
161 { "http.sslBackend", "schannel", 1 },
162 #endif
163 /* Optional */
164 { "status.aheadBehind", "false" },
165 { "commitGraph.generationVersion", "1" },
166 { "core.autoCRLF", "false" },
167 { "core.safeCRLF", "false" },
168 { "fetch.showForcedUpdates", "false" },
169 { NULL, NULL },
171 int i;
172 char *value;
174 for (i = 0; config[i].key; i++) {
175 if (set_scalar_config(config + i, reconfigure))
176 return error(_("could not configure %s=%s"),
177 config[i].key, config[i].value);
180 if (have_fsmonitor_support()) {
181 struct scalar_config fsmonitor = { "core.fsmonitor", "true" };
182 if (set_scalar_config(&fsmonitor, reconfigure))
183 return error(_("could not configure %s=%s"),
184 fsmonitor.key, fsmonitor.value);
188 * The `log.excludeDecoration` setting is special because it allows
189 * for multiple values.
191 if (git_config_get_string("log.excludeDecoration", &value)) {
192 trace2_data_string("scalar", the_repository,
193 "log.excludeDecoration", "created");
194 if (git_config_set_multivar_gently("log.excludeDecoration",
195 "refs/prefetch/*",
196 CONFIG_REGEX_NONE, 0))
197 return error(_("could not configure "
198 "log.excludeDecoration"));
199 } else {
200 trace2_data_string("scalar", the_repository,
201 "log.excludeDecoration", "exists");
202 free(value);
205 return 0;
208 static int toggle_maintenance(int enable)
210 return run_git("maintenance",
211 enable ? "start" : "unregister",
212 enable ? NULL : "--force",
213 NULL);
216 static int add_or_remove_enlistment(int add)
218 int res;
220 if (!the_repository->worktree)
221 die(_("Scalar enlistments require a worktree"));
223 res = run_git("config", "--global", "--get", "--fixed-value",
224 "scalar.repo", the_repository->worktree, NULL);
227 * If we want to add and the setting is already there, then do nothing.
228 * If we want to remove and the setting is not there, then do nothing.
230 if ((add && !res) || (!add && res))
231 return 0;
233 return run_git("config", "--global", add ? "--add" : "--unset",
234 add ? "--no-fixed-value" : "--fixed-value",
235 "scalar.repo", the_repository->worktree, NULL);
238 static int start_fsmonitor_daemon(void)
240 assert(have_fsmonitor_support());
242 if (fsmonitor_ipc__get_state() != IPC_STATE__LISTENING)
243 return run_git("fsmonitor--daemon", "start", NULL);
245 return 0;
248 static int stop_fsmonitor_daemon(void)
250 assert(have_fsmonitor_support());
252 if (fsmonitor_ipc__get_state() == IPC_STATE__LISTENING)
253 return run_git("fsmonitor--daemon", "stop", NULL);
255 return 0;
258 static int register_dir(void)
260 if (add_or_remove_enlistment(1))
261 return error(_("could not add enlistment"));
263 if (set_recommended_config(0))
264 return error(_("could not set recommended config"));
266 if (toggle_maintenance(1))
267 return error(_("could not turn on maintenance"));
269 if (have_fsmonitor_support() && start_fsmonitor_daemon()) {
270 return error(_("could not start the FSMonitor daemon"));
273 return 0;
276 static int unregister_dir(void)
278 int res = 0;
280 if (toggle_maintenance(0))
281 res = error(_("could not turn off maintenance"));
283 if (add_or_remove_enlistment(0))
284 res = error(_("could not remove enlistment"));
286 return res;
289 /* printf-style interface, expects `<key>=<value>` argument */
290 static int set_config(const char *fmt, ...)
292 struct strbuf buf = STRBUF_INIT;
293 char *value;
294 int res;
295 va_list args;
297 va_start(args, fmt);
298 strbuf_vaddf(&buf, fmt, args);
299 va_end(args);
301 value = strchr(buf.buf, '=');
302 if (value)
303 *(value++) = '\0';
304 res = git_config_set_gently(buf.buf, value);
305 strbuf_release(&buf);
307 return res;
310 static char *remote_default_branch(const char *url)
312 struct child_process cp = CHILD_PROCESS_INIT;
313 struct strbuf out = STRBUF_INIT;
315 cp.git_cmd = 1;
316 strvec_pushl(&cp.args, "ls-remote", "--symref", url, "HEAD", NULL);
317 if (!pipe_command(&cp, NULL, 0, &out, 0, NULL, 0)) {
318 const char *line = out.buf;
320 while (*line) {
321 const char *eol = strchrnul(line, '\n'), *p;
322 size_t len = eol - line;
323 char *branch;
325 if (!skip_prefix(line, "ref: ", &p) ||
326 !strip_suffix_mem(line, &len, "\tHEAD")) {
327 line = eol + (*eol == '\n');
328 continue;
331 eol = line + len;
332 if (skip_prefix(p, "refs/heads/", &p)) {
333 branch = xstrndup(p, eol - p);
334 strbuf_release(&out);
335 return branch;
338 error(_("remote HEAD is not a branch: '%.*s'"),
339 (int)(eol - p), p);
340 strbuf_release(&out);
341 return NULL;
344 warning(_("failed to get default branch name from remote; "
345 "using local default"));
346 strbuf_reset(&out);
348 child_process_init(&cp);
349 cp.git_cmd = 1;
350 strvec_pushl(&cp.args, "symbolic-ref", "--short", "HEAD", NULL);
351 if (!pipe_command(&cp, NULL, 0, &out, 0, NULL, 0)) {
352 strbuf_trim(&out);
353 return strbuf_detach(&out, NULL);
356 strbuf_release(&out);
357 error(_("failed to get default branch name"));
358 return NULL;
361 static int delete_enlistment(struct strbuf *enlistment)
363 #ifdef WIN32
364 struct strbuf parent = STRBUF_INIT;
365 size_t offset;
366 char *path_sep;
367 #endif
369 if (unregister_dir())
370 return error(_("failed to unregister repository"));
372 #ifdef WIN32
374 * Change the current directory to one outside of the enlistment so
375 * that we may delete everything underneath it.
377 offset = offset_1st_component(enlistment->buf);
378 path_sep = find_last_dir_sep(enlistment->buf + offset);
379 strbuf_add(&parent, enlistment->buf,
380 path_sep ? path_sep - enlistment->buf : offset);
381 if (chdir(parent.buf) < 0) {
382 int res = error_errno(_("could not switch to '%s'"), parent.buf);
383 strbuf_release(&parent);
384 return res;
386 strbuf_release(&parent);
387 #endif
389 if (have_fsmonitor_support() && stop_fsmonitor_daemon())
390 return error(_("failed to stop the FSMonitor daemon"));
392 if (remove_dir_recursively(enlistment, 0))
393 return error(_("failed to delete enlistment directory"));
395 return 0;
399 * Dummy implementation; Using `get_version_info()` would cause a link error
400 * without this.
402 void load_builtin_commands(const char *prefix, struct cmdnames *cmds)
404 die("not implemented");
407 static int cmd_clone(int argc, const char **argv)
409 const char *branch = NULL;
410 int full_clone = 0, single_branch = 0;
411 struct option clone_options[] = {
412 OPT_STRING('b', "branch", &branch, N_("<branch>"),
413 N_("branch to checkout after clone")),
414 OPT_BOOL(0, "full-clone", &full_clone,
415 N_("when cloning, create full working directory")),
416 OPT_BOOL(0, "single-branch", &single_branch,
417 N_("only download metadata for the branch that will "
418 "be checked out")),
419 OPT_END(),
421 const char * const clone_usage[] = {
422 N_("scalar clone [<options>] [--] <repo> [<dir>]"),
423 NULL
425 const char *url;
426 char *enlistment = NULL, *dir = NULL;
427 struct strbuf buf = STRBUF_INIT;
428 int res;
430 argc = parse_options(argc, argv, NULL, clone_options, clone_usage, 0);
432 if (argc == 2) {
433 url = argv[0];
434 enlistment = xstrdup(argv[1]);
435 } else if (argc == 1) {
436 url = argv[0];
438 strbuf_addstr(&buf, url);
439 /* Strip trailing slashes, if any */
440 while (buf.len > 0 && is_dir_sep(buf.buf[buf.len - 1]))
441 strbuf_setlen(&buf, buf.len - 1);
442 /* Strip suffix `.git`, if any */
443 strbuf_strip_suffix(&buf, ".git");
445 enlistment = find_last_dir_sep(buf.buf);
446 if (!enlistment) {
447 die(_("cannot deduce worktree name from '%s'"), url);
449 enlistment = xstrdup(enlistment + 1);
450 } else {
451 usage_msg_opt(_("You must specify a repository to clone."),
452 clone_usage, clone_options);
455 if (is_directory(enlistment))
456 die(_("directory '%s' exists already"), enlistment);
458 dir = xstrfmt("%s/src", enlistment);
460 strbuf_reset(&buf);
461 if (branch)
462 strbuf_addf(&buf, "init.defaultBranch=%s", branch);
463 else {
464 char *b = repo_default_branch_name(the_repository, 1);
465 strbuf_addf(&buf, "init.defaultBranch=%s", b);
466 free(b);
469 if ((res = run_git("-c", buf.buf, "init", "--", dir, NULL)))
470 goto cleanup;
472 if (chdir(dir) < 0) {
473 res = error_errno(_("could not switch to '%s'"), dir);
474 goto cleanup;
477 setup_git_directory();
479 /* common-main already logs `argv` */
480 trace2_def_repo(the_repository);
482 if (!branch && !(branch = remote_default_branch(url))) {
483 res = error(_("failed to get default branch for '%s'"), url);
484 goto cleanup;
487 if (set_config("remote.origin.url=%s", url) ||
488 set_config("remote.origin.fetch="
489 "+refs/heads/%s:refs/remotes/origin/%s",
490 single_branch ? branch : "*",
491 single_branch ? branch : "*") ||
492 set_config("remote.origin.promisor=true") ||
493 set_config("remote.origin.partialCloneFilter=blob:none")) {
494 res = error(_("could not configure remote in '%s'"), dir);
495 goto cleanup;
498 if (!full_clone &&
499 (res = run_git("sparse-checkout", "init", "--cone", NULL)))
500 goto cleanup;
502 if (set_recommended_config(0))
503 return error(_("could not configure '%s'"), dir);
505 if ((res = run_git("fetch", "--quiet", "origin", NULL))) {
506 warning(_("partial clone failed; attempting full clone"));
508 if (set_config("remote.origin.promisor") ||
509 set_config("remote.origin.partialCloneFilter")) {
510 res = error(_("could not configure for full clone"));
511 goto cleanup;
514 if ((res = run_git("fetch", "--quiet", "origin", NULL)))
515 goto cleanup;
518 if ((res = set_config("branch.%s.remote=origin", branch)))
519 goto cleanup;
520 if ((res = set_config("branch.%s.merge=refs/heads/%s",
521 branch, branch)))
522 goto cleanup;
524 strbuf_reset(&buf);
525 strbuf_addf(&buf, "origin/%s", branch);
526 res = run_git("checkout", "-f", "-t", buf.buf, NULL);
527 if (res)
528 goto cleanup;
530 res = register_dir();
532 cleanup:
533 free(enlistment);
534 free(dir);
535 strbuf_release(&buf);
536 return res;
539 static int cmd_diagnose(int argc, const char **argv)
541 struct option options[] = {
542 OPT_END(),
544 const char * const usage[] = {
545 N_("scalar diagnose [<enlistment>]"),
546 NULL
548 struct strbuf diagnostics_root = STRBUF_INIT;
549 int res = 0;
551 argc = parse_options(argc, argv, NULL, options,
552 usage, 0);
554 setup_enlistment_directory(argc, argv, usage, options, &diagnostics_root);
555 strbuf_addstr(&diagnostics_root, "/.scalarDiagnostics");
557 res = run_git("diagnose", "--mode=all", "-s", "%Y%m%d_%H%M%S",
558 "-o", diagnostics_root.buf, NULL);
560 strbuf_release(&diagnostics_root);
561 return res;
564 static int cmd_list(int argc, const char **argv)
566 if (argc != 1)
567 die(_("`scalar list` does not take arguments"));
569 if (run_git("config", "--global", "--get-all", "scalar.repo", NULL) < 0)
570 return -1;
571 return 0;
574 static int cmd_register(int argc, const char **argv)
576 struct option options[] = {
577 OPT_END(),
579 const char * const usage[] = {
580 N_("scalar register [<enlistment>]"),
581 NULL
584 argc = parse_options(argc, argv, NULL, options,
585 usage, 0);
587 setup_enlistment_directory(argc, argv, usage, options, NULL);
589 return register_dir();
592 static int get_scalar_repos(const char *key, const char *value, void *data)
594 struct string_list *list = data;
596 if (!strcmp(key, "scalar.repo"))
597 string_list_append(list, value);
599 return 0;
602 static int cmd_reconfigure(int argc, const char **argv)
604 int all = 0;
605 struct option options[] = {
606 OPT_BOOL('a', "all", &all,
607 N_("reconfigure all registered enlistments")),
608 OPT_END(),
610 const char * const usage[] = {
611 N_("scalar reconfigure [--all | <enlistment>]"),
612 NULL
614 struct string_list scalar_repos = STRING_LIST_INIT_DUP;
615 int i, res = 0;
616 struct repository r = { NULL };
617 struct strbuf commondir = STRBUF_INIT, gitdir = STRBUF_INIT;
619 argc = parse_options(argc, argv, NULL, options,
620 usage, 0);
622 if (!all) {
623 setup_enlistment_directory(argc, argv, usage, options, NULL);
625 return set_recommended_config(1);
628 if (argc > 0)
629 usage_msg_opt(_("--all or <enlistment>, but not both"),
630 usage, options);
632 git_config(get_scalar_repos, &scalar_repos);
634 for (i = 0; i < scalar_repos.nr; i++) {
635 const char *dir = scalar_repos.items[i].string;
637 strbuf_reset(&commondir);
638 strbuf_reset(&gitdir);
640 if (chdir(dir) < 0) {
641 warning_errno(_("could not switch to '%s'"), dir);
642 res = -1;
643 } else if (discover_git_directory(&commondir, &gitdir) < 0) {
644 warning_errno(_("git repository gone in '%s'"), dir);
645 res = -1;
646 } else {
647 git_config_clear();
649 the_repository = &r;
650 r.commondir = commondir.buf;
651 r.gitdir = gitdir.buf;
653 if (set_recommended_config(1) < 0)
654 res = -1;
658 string_list_clear(&scalar_repos, 1);
659 strbuf_release(&commondir);
660 strbuf_release(&gitdir);
662 return res;
665 static int cmd_run(int argc, const char **argv)
667 struct option options[] = {
668 OPT_END(),
670 struct {
671 const char *arg, *task;
672 } tasks[] = {
673 { "config", NULL },
674 { "commit-graph", "commit-graph" },
675 { "fetch", "prefetch" },
676 { "loose-objects", "loose-objects" },
677 { "pack-files", "incremental-repack" },
678 { NULL, NULL }
680 struct strbuf buf = STRBUF_INIT;
681 const char *usagestr[] = { NULL, NULL };
682 int i;
684 strbuf_addstr(&buf, N_("scalar run <task> [<enlistment>]\nTasks:\n"));
685 for (i = 0; tasks[i].arg; i++)
686 strbuf_addf(&buf, "\t%s\n", tasks[i].arg);
687 usagestr[0] = buf.buf;
689 argc = parse_options(argc, argv, NULL, options,
690 usagestr, 0);
692 if (!argc)
693 usage_with_options(usagestr, options);
695 if (!strcmp("all", argv[0])) {
696 i = -1;
697 } else {
698 for (i = 0; tasks[i].arg && strcmp(tasks[i].arg, argv[0]); i++)
699 ; /* keep looking for the task */
701 if (i > 0 && !tasks[i].arg) {
702 error(_("no such task: '%s'"), argv[0]);
703 usage_with_options(usagestr, options);
707 argc--;
708 argv++;
709 setup_enlistment_directory(argc, argv, usagestr, options, NULL);
710 strbuf_release(&buf);
712 if (i == 0)
713 return register_dir();
715 if (i > 0)
716 return run_git("maintenance", "run",
717 "--task", tasks[i].task, NULL);
719 if (register_dir())
720 return -1;
721 for (i = 1; tasks[i].arg; i++)
722 if (run_git("maintenance", "run",
723 "--task", tasks[i].task, NULL))
724 return -1;
725 return 0;
728 static int remove_deleted_enlistment(struct strbuf *path)
730 int res = 0;
731 strbuf_realpath_forgiving(path, path->buf, 1);
733 if (run_git("config", "--global",
734 "--unset", "--fixed-value",
735 "scalar.repo", path->buf, NULL) < 0)
736 res = -1;
738 if (run_git("config", "--global",
739 "--unset", "--fixed-value",
740 "maintenance.repo", path->buf, NULL) < 0)
741 res = -1;
743 return res;
746 static int cmd_unregister(int argc, const char **argv)
748 struct option options[] = {
749 OPT_END(),
751 const char * const usage[] = {
752 N_("scalar unregister [<enlistment>]"),
753 NULL
756 argc = parse_options(argc, argv, NULL, options,
757 usage, 0);
760 * Be forgiving when the enlistment or worktree does not even exist any
761 * longer; This can be the case if a user deleted the worktree by
762 * mistake and _still_ wants to unregister the thing.
764 if (argc == 1) {
765 struct strbuf src_path = STRBUF_INIT, workdir_path = STRBUF_INIT;
767 strbuf_addf(&src_path, "%s/src/.git", argv[0]);
768 strbuf_addf(&workdir_path, "%s/.git", argv[0]);
769 if (!is_directory(src_path.buf) && !is_directory(workdir_path.buf)) {
770 /* remove possible matching registrations */
771 int res = -1;
773 strbuf_strip_suffix(&src_path, "/.git");
774 res = remove_deleted_enlistment(&src_path) && res;
776 strbuf_strip_suffix(&workdir_path, "/.git");
777 res = remove_deleted_enlistment(&workdir_path) && res;
779 strbuf_release(&src_path);
780 strbuf_release(&workdir_path);
781 return res;
783 strbuf_release(&src_path);
784 strbuf_release(&workdir_path);
787 setup_enlistment_directory(argc, argv, usage, options, NULL);
789 return unregister_dir();
792 static int cmd_delete(int argc, const char **argv)
794 char *cwd = xgetcwd();
795 struct option options[] = {
796 OPT_END(),
798 const char * const usage[] = {
799 N_("scalar delete <enlistment>"),
800 NULL
802 struct strbuf enlistment = STRBUF_INIT;
803 int res = 0;
805 argc = parse_options(argc, argv, NULL, options,
806 usage, 0);
808 if (argc != 1)
809 usage_with_options(usage, options);
811 setup_enlistment_directory(argc, argv, usage, options, &enlistment);
813 if (dir_inside_of(cwd, enlistment.buf) >= 0)
814 res = error(_("refusing to delete current working directory"));
815 else {
816 close_object_store(the_repository->objects);
817 res = delete_enlistment(&enlistment);
819 strbuf_release(&enlistment);
820 free(cwd);
822 return res;
825 static int cmd_help(int argc, const char **argv)
827 struct option options[] = {
828 OPT_END(),
830 const char * const usage[] = {
831 "scalar help",
832 NULL
835 argc = parse_options(argc, argv, NULL, options,
836 usage, 0);
838 if (argc != 0)
839 usage_with_options(usage, options);
841 return run_git("help", "scalar", NULL);
844 static int cmd_version(int argc, const char **argv)
846 int verbose = 0, build_options = 0;
847 struct option options[] = {
848 OPT__VERBOSE(&verbose, N_("include Git version")),
849 OPT_BOOL(0, "build-options", &build_options,
850 N_("include Git's build options")),
851 OPT_END(),
853 const char * const usage[] = {
854 N_("scalar verbose [-v | --verbose] [--build-options]"),
855 NULL
857 struct strbuf buf = STRBUF_INIT;
859 argc = parse_options(argc, argv, NULL, options,
860 usage, 0);
862 if (argc != 0)
863 usage_with_options(usage, options);
865 get_version_info(&buf, build_options);
866 fprintf(stderr, "%s\n", buf.buf);
867 strbuf_release(&buf);
869 return 0;
872 static struct {
873 const char *name;
874 int (*fn)(int, const char **);
875 } builtins[] = {
876 { "clone", cmd_clone },
877 { "list", cmd_list },
878 { "register", cmd_register },
879 { "unregister", cmd_unregister },
880 { "run", cmd_run },
881 { "reconfigure", cmd_reconfigure },
882 { "delete", cmd_delete },
883 { "help", cmd_help },
884 { "version", cmd_version },
885 { "diagnose", cmd_diagnose },
886 { NULL, NULL},
889 int cmd_main(int argc, const char **argv)
891 struct strbuf scalar_usage = STRBUF_INIT;
892 int i;
894 while (argc > 1 && *argv[1] == '-') {
895 if (!strcmp(argv[1], "-C")) {
896 if (argc < 3)
897 die(_("-C requires a <directory>"));
898 if (chdir(argv[2]) < 0)
899 die_errno(_("could not change to '%s'"),
900 argv[2]);
901 argc -= 2;
902 argv += 2;
903 } else if (!strcmp(argv[1], "-c")) {
904 if (argc < 3)
905 die(_("-c requires a <key>=<value> argument"));
906 git_config_push_parameter(argv[2]);
907 argc -= 2;
908 argv += 2;
909 } else
910 break;
913 if (argc > 1) {
914 argv++;
915 argc--;
917 for (i = 0; builtins[i].name; i++)
918 if (!strcmp(builtins[i].name, argv[0]))
919 return !!builtins[i].fn(argc, argv);
922 strbuf_addstr(&scalar_usage,
923 N_("scalar [-C <directory>] [-c <key>=<value>] "
924 "<command> [<options>]\n\nCommands:\n"));
925 for (i = 0; builtins[i].name; i++)
926 strbuf_addf(&scalar_usage, "\t%s\n", builtins[i].name);
928 usage(scalar_usage.buf);