cocci: remove 'unused.cocci'
[git.git] / scalar.c
blobde07c37d21001d54d026fb6f5f1de0ebccaf7e9c
1 /*
2 * The Scalar command-line interface.
3 */
5 #include "cache.h"
6 #include "abspath.h"
7 #include "gettext.h"
8 #include "parse-options.h"
9 #include "config.h"
10 #include "run-command.h"
11 #include "simple-ipc.h"
12 #include "fsmonitor-ipc.h"
13 #include "fsmonitor-settings.h"
14 #include "refs.h"
15 #include "dir.h"
16 #include "packfile.h"
17 #include "help.h"
18 #include "setup.h"
20 static void setup_enlistment_directory(int argc, const char **argv,
21 const char * const *usagestr,
22 const struct option *options,
23 struct strbuf *enlistment_root)
25 struct strbuf path = STRBUF_INIT;
26 int enlistment_is_repo_parent = 0;
27 size_t len;
29 if (startup_info->have_repository)
30 BUG("gitdir already set up?!?");
32 if (argc > 1)
33 usage_with_options(usagestr, options);
35 /* find the worktree, determine its corresponding root */
36 if (argc == 1) {
37 strbuf_add_absolute_path(&path, argv[0]);
38 if (!is_directory(path.buf))
39 die(_("'%s' does not exist"), path.buf);
40 if (chdir(path.buf) < 0)
41 die_errno(_("could not switch to '%s'"), path.buf);
42 } else if (strbuf_getcwd(&path) < 0)
43 die(_("need a working directory"));
45 strbuf_trim_trailing_dir_sep(&path);
47 /* check if currently in enlistment root with src/ workdir */
48 len = path.len;
49 strbuf_addstr(&path, "/src");
50 if (is_nonbare_repository_dir(&path)) {
51 enlistment_is_repo_parent = 1;
52 if (chdir(path.buf) < 0)
53 die_errno(_("could not switch to '%s'"), path.buf);
55 strbuf_setlen(&path, len);
57 setup_git_directory();
59 if (!the_repository->worktree)
60 die(_("Scalar enlistments require a worktree"));
62 if (enlistment_root) {
63 if (enlistment_is_repo_parent)
64 strbuf_addbuf(enlistment_root, &path);
65 else
66 strbuf_addstr(enlistment_root, the_repository->worktree);
69 strbuf_release(&path);
72 static int run_git(const char *arg, ...)
74 struct child_process cmd = CHILD_PROCESS_INIT;
75 va_list args;
76 const char *p;
78 va_start(args, arg);
79 strvec_push(&cmd.args, arg);
80 while ((p = va_arg(args, const char *)))
81 strvec_push(&cmd.args, p);
82 va_end(args);
84 cmd.git_cmd = 1;
85 return run_command(&cmd);
88 struct scalar_config {
89 const char *key;
90 const char *value;
91 int overwrite_on_reconfigure;
94 static int set_scalar_config(const struct scalar_config *config, int reconfigure)
96 char *value = NULL;
97 int res;
99 if ((reconfigure && config->overwrite_on_reconfigure) ||
100 git_config_get_string(config->key, &value)) {
101 trace2_data_string("scalar", the_repository, config->key, "created");
102 res = git_config_set_gently(config->key, config->value);
103 } else {
104 trace2_data_string("scalar", the_repository, config->key, "exists");
105 res = 0;
108 free(value);
109 return res;
112 static int have_fsmonitor_support(void)
114 return fsmonitor_ipc__is_supported() &&
115 fsm_settings__get_reason(the_repository) == FSMONITOR_REASON_OK;
118 static int set_recommended_config(int reconfigure)
120 struct scalar_config config[] = {
121 /* Required */
122 { "am.keepCR", "true", 1 },
123 { "core.FSCache", "true", 1 },
124 { "core.multiPackIndex", "true", 1 },
125 { "core.preloadIndex", "true", 1 },
126 #ifndef WIN32
127 { "core.untrackedCache", "true", 1 },
128 #else
130 * Unfortunately, Scalar's Functional Tests demonstrated
131 * that the untracked cache feature is unreliable on Windows
132 * (which is a bummer because that platform would benefit the
133 * most from it). For some reason, freshly created files seem
134 * not to update the directory's `lastModified` time
135 * immediately, but the untracked cache would need to rely on
136 * that.
138 * Therefore, with a sad heart, we disable this very useful
139 * feature on Windows.
141 { "core.untrackedCache", "false", 1 },
142 #endif
143 { "core.logAllRefUpdates", "true", 1 },
144 { "credential.https://dev.azure.com.useHttpPath", "true", 1 },
145 { "credential.validate", "false", 1 }, /* GCM4W-only */
146 { "gc.auto", "0", 1 },
147 { "gui.GCWarning", "false", 1 },
148 { "index.skipHash", "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 warning(_("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, show_progress = isatty(2);
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",
506 show_progress ? "--progress" : "--no-progress",
507 "origin", NULL))) {
508 warning(_("partial clone failed; attempting full clone"));
510 if (set_config("remote.origin.promisor") ||
511 set_config("remote.origin.partialCloneFilter")) {
512 res = error(_("could not configure for full clone"));
513 goto cleanup;
516 if ((res = run_git("fetch", "--quiet",
517 show_progress ? "--progress" : "--no-progress",
518 "origin", NULL)))
519 goto cleanup;
522 if ((res = set_config("branch.%s.remote=origin", branch)))
523 goto cleanup;
524 if ((res = set_config("branch.%s.merge=refs/heads/%s",
525 branch, branch)))
526 goto cleanup;
528 strbuf_reset(&buf);
529 strbuf_addf(&buf, "origin/%s", branch);
530 res = run_git("checkout", "-f", "-t", buf.buf, NULL);
531 if (res)
532 goto cleanup;
534 res = register_dir();
536 cleanup:
537 free(enlistment);
538 free(dir);
539 strbuf_release(&buf);
540 return res;
543 static int cmd_diagnose(int argc, const char **argv)
545 struct option options[] = {
546 OPT_END(),
548 const char * const usage[] = {
549 N_("scalar diagnose [<enlistment>]"),
550 NULL
552 struct strbuf diagnostics_root = STRBUF_INIT;
553 int res = 0;
555 argc = parse_options(argc, argv, NULL, options,
556 usage, 0);
558 setup_enlistment_directory(argc, argv, usage, options, &diagnostics_root);
559 strbuf_addstr(&diagnostics_root, "/.scalarDiagnostics");
561 res = run_git("diagnose", "--mode=all", "-s", "%Y%m%d_%H%M%S",
562 "-o", diagnostics_root.buf, NULL);
564 strbuf_release(&diagnostics_root);
565 return res;
568 static int cmd_list(int argc, const char **argv UNUSED)
570 if (argc != 1)
571 die(_("`scalar list` does not take arguments"));
573 if (run_git("config", "--global", "--get-all", "scalar.repo", NULL) < 0)
574 return -1;
575 return 0;
578 static int cmd_register(int argc, const char **argv)
580 struct option options[] = {
581 OPT_END(),
583 const char * const usage[] = {
584 N_("scalar register [<enlistment>]"),
585 NULL
588 argc = parse_options(argc, argv, NULL, options,
589 usage, 0);
591 setup_enlistment_directory(argc, argv, usage, options, NULL);
593 return register_dir();
596 static int get_scalar_repos(const char *key, const char *value, void *data)
598 struct string_list *list = data;
600 if (!strcmp(key, "scalar.repo"))
601 string_list_append(list, value);
603 return 0;
606 static int remove_deleted_enlistment(struct strbuf *path)
608 int res = 0;
609 strbuf_realpath_forgiving(path, path->buf, 1);
611 if (run_git("config", "--global",
612 "--unset", "--fixed-value",
613 "scalar.repo", path->buf, NULL) < 0)
614 res = -1;
616 if (run_git("config", "--global",
617 "--unset", "--fixed-value",
618 "maintenance.repo", path->buf, NULL) < 0)
619 res = -1;
621 return res;
624 static int cmd_reconfigure(int argc, const char **argv)
626 int all = 0;
627 struct option options[] = {
628 OPT_BOOL('a', "all", &all,
629 N_("reconfigure all registered enlistments")),
630 OPT_END(),
632 const char * const usage[] = {
633 N_("scalar reconfigure [--all | <enlistment>]"),
634 NULL
636 struct string_list scalar_repos = STRING_LIST_INIT_DUP;
637 int i, res = 0;
638 struct repository r = { NULL };
639 struct strbuf commondir = STRBUF_INIT, gitdir = STRBUF_INIT;
641 argc = parse_options(argc, argv, NULL, options,
642 usage, 0);
644 if (!all) {
645 setup_enlistment_directory(argc, argv, usage, options, NULL);
647 return set_recommended_config(1);
650 if (argc > 0)
651 usage_msg_opt(_("--all or <enlistment>, but not both"),
652 usage, options);
654 git_config(get_scalar_repos, &scalar_repos);
656 for (i = 0; i < scalar_repos.nr; i++) {
657 const char *dir = scalar_repos.items[i].string;
659 strbuf_reset(&commondir);
660 strbuf_reset(&gitdir);
662 if (chdir(dir) < 0) {
663 struct strbuf buf = STRBUF_INIT;
665 if (errno != ENOENT) {
666 warning_errno(_("could not switch to '%s'"), dir);
667 res = -1;
668 continue;
671 strbuf_addstr(&buf, dir);
672 if (remove_deleted_enlistment(&buf))
673 res = error(_("could not remove stale "
674 "scalar.repo '%s'"), dir);
675 else
676 warning(_("removing stale scalar.repo '%s'"),
677 dir);
678 strbuf_release(&buf);
679 } else if (discover_git_directory(&commondir, &gitdir) < 0) {
680 warning_errno(_("git repository gone in '%s'"), dir);
681 res = -1;
682 } else {
683 git_config_clear();
685 the_repository = &r;
686 r.commondir = commondir.buf;
687 r.gitdir = gitdir.buf;
689 if (set_recommended_config(1) < 0)
690 res = -1;
694 string_list_clear(&scalar_repos, 1);
695 strbuf_release(&commondir);
696 strbuf_release(&gitdir);
698 return res;
701 static int cmd_run(int argc, const char **argv)
703 struct option options[] = {
704 OPT_END(),
706 struct {
707 const char *arg, *task;
708 } tasks[] = {
709 { "config", NULL },
710 { "commit-graph", "commit-graph" },
711 { "fetch", "prefetch" },
712 { "loose-objects", "loose-objects" },
713 { "pack-files", "incremental-repack" },
714 { NULL, NULL }
716 struct strbuf buf = STRBUF_INIT;
717 const char *usagestr[] = { NULL, NULL };
718 int i;
720 strbuf_addstr(&buf, N_("scalar run <task> [<enlistment>]\nTasks:\n"));
721 for (i = 0; tasks[i].arg; i++)
722 strbuf_addf(&buf, "\t%s\n", tasks[i].arg);
723 usagestr[0] = buf.buf;
725 argc = parse_options(argc, argv, NULL, options,
726 usagestr, 0);
728 if (!argc)
729 usage_with_options(usagestr, options);
731 if (!strcmp("all", argv[0])) {
732 i = -1;
733 } else {
734 for (i = 0; tasks[i].arg && strcmp(tasks[i].arg, argv[0]); i++)
735 ; /* keep looking for the task */
737 if (i > 0 && !tasks[i].arg) {
738 error(_("no such task: '%s'"), argv[0]);
739 usage_with_options(usagestr, options);
743 argc--;
744 argv++;
745 setup_enlistment_directory(argc, argv, usagestr, options, NULL);
746 strbuf_release(&buf);
748 if (i == 0)
749 return register_dir();
751 if (i > 0)
752 return run_git("maintenance", "run",
753 "--task", tasks[i].task, NULL);
755 if (register_dir())
756 return -1;
757 for (i = 1; tasks[i].arg; i++)
758 if (run_git("maintenance", "run",
759 "--task", tasks[i].task, NULL))
760 return -1;
761 return 0;
764 static int cmd_unregister(int argc, const char **argv)
766 struct option options[] = {
767 OPT_END(),
769 const char * const usage[] = {
770 N_("scalar unregister [<enlistment>]"),
771 NULL
774 argc = parse_options(argc, argv, NULL, options,
775 usage, 0);
778 * Be forgiving when the enlistment or worktree does not even exist any
779 * longer; This can be the case if a user deleted the worktree by
780 * mistake and _still_ wants to unregister the thing.
782 if (argc == 1) {
783 struct strbuf src_path = STRBUF_INIT, workdir_path = STRBUF_INIT;
785 strbuf_addf(&src_path, "%s/src/.git", argv[0]);
786 strbuf_addf(&workdir_path, "%s/.git", argv[0]);
787 if (!is_directory(src_path.buf) && !is_directory(workdir_path.buf)) {
788 /* remove possible matching registrations */
789 int res = -1;
791 strbuf_strip_suffix(&src_path, "/.git");
792 res = remove_deleted_enlistment(&src_path) && res;
794 strbuf_strip_suffix(&workdir_path, "/.git");
795 res = remove_deleted_enlistment(&workdir_path) && res;
797 strbuf_release(&src_path);
798 strbuf_release(&workdir_path);
799 return res;
801 strbuf_release(&src_path);
802 strbuf_release(&workdir_path);
805 setup_enlistment_directory(argc, argv, usage, options, NULL);
807 return unregister_dir();
810 static int cmd_delete(int argc, const char **argv)
812 char *cwd = xgetcwd();
813 struct option options[] = {
814 OPT_END(),
816 const char * const usage[] = {
817 N_("scalar delete <enlistment>"),
818 NULL
820 struct strbuf enlistment = STRBUF_INIT;
821 int res = 0;
823 argc = parse_options(argc, argv, NULL, options,
824 usage, 0);
826 if (argc != 1)
827 usage_with_options(usage, options);
829 setup_enlistment_directory(argc, argv, usage, options, &enlistment);
831 if (dir_inside_of(cwd, enlistment.buf) >= 0)
832 res = error(_("refusing to delete current working directory"));
833 else {
834 close_object_store(the_repository->objects);
835 res = delete_enlistment(&enlistment);
837 strbuf_release(&enlistment);
838 free(cwd);
840 return res;
843 static int cmd_help(int argc, const char **argv)
845 struct option options[] = {
846 OPT_END(),
848 const char * const usage[] = {
849 "scalar help",
850 NULL
853 argc = parse_options(argc, argv, NULL, options,
854 usage, 0);
856 if (argc != 0)
857 usage_with_options(usage, options);
859 return run_git("help", "scalar", NULL);
862 static int cmd_version(int argc, const char **argv)
864 int verbose = 0, build_options = 0;
865 struct option options[] = {
866 OPT__VERBOSE(&verbose, N_("include Git version")),
867 OPT_BOOL(0, "build-options", &build_options,
868 N_("include Git's build options")),
869 OPT_END(),
871 const char * const usage[] = {
872 N_("scalar verbose [-v | --verbose] [--build-options]"),
873 NULL
875 struct strbuf buf = STRBUF_INIT;
877 argc = parse_options(argc, argv, NULL, options,
878 usage, 0);
880 if (argc != 0)
881 usage_with_options(usage, options);
883 get_version_info(&buf, build_options);
884 fprintf(stderr, "%s\n", buf.buf);
885 strbuf_release(&buf);
887 return 0;
890 static struct {
891 const char *name;
892 int (*fn)(int, const char **);
893 } builtins[] = {
894 { "clone", cmd_clone },
895 { "list", cmd_list },
896 { "register", cmd_register },
897 { "unregister", cmd_unregister },
898 { "run", cmd_run },
899 { "reconfigure", cmd_reconfigure },
900 { "delete", cmd_delete },
901 { "help", cmd_help },
902 { "version", cmd_version },
903 { "diagnose", cmd_diagnose },
904 { NULL, NULL},
907 int cmd_main(int argc, const char **argv)
909 struct strbuf scalar_usage = STRBUF_INIT;
910 int i;
912 while (argc > 1 && *argv[1] == '-') {
913 if (!strcmp(argv[1], "-C")) {
914 if (argc < 3)
915 die(_("-C requires a <directory>"));
916 if (chdir(argv[2]) < 0)
917 die_errno(_("could not change to '%s'"),
918 argv[2]);
919 argc -= 2;
920 argv += 2;
921 } else if (!strcmp(argv[1], "-c")) {
922 if (argc < 3)
923 die(_("-c requires a <key>=<value> argument"));
924 git_config_push_parameter(argv[2]);
925 argc -= 2;
926 argv += 2;
927 } else
928 break;
931 if (argc > 1) {
932 argv++;
933 argc--;
935 for (i = 0; builtins[i].name; i++)
936 if (!strcmp(builtins[i].name, argv[0]))
937 return !!builtins[i].fn(argc, argv);
940 strbuf_addstr(&scalar_usage,
941 N_("scalar [-C <directory>] [-c <key>=<value>] "
942 "<command> [<options>]\n\nCommands:\n"));
943 for (i = 0; builtins[i].name; i++)
944 strbuf_addf(&scalar_usage, "\t%s\n", builtins[i].name);
946 usage(scalar_usage.buf);