diff: document what --name-only shows
[alt-git.git] / scalar.c
blobca19b95ce46cfdaa6f501eb487d4ba869fd0c5bd
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 child_process cmd = CHILD_PROCESS_INIT;
73 va_list args;
74 const char *p;
76 va_start(args, arg);
77 strvec_push(&cmd.args, arg);
78 while ((p = va_arg(args, const char *)))
79 strvec_push(&cmd.args, p);
80 va_end(args);
82 cmd.git_cmd = 1;
83 return run_command(&cmd);
86 struct scalar_config {
87 const char *key;
88 const char *value;
89 int overwrite_on_reconfigure;
92 static int set_scalar_config(const struct scalar_config *config, int reconfigure)
94 char *value = NULL;
95 int res;
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;
106 free(value);
107 return res;
110 static int have_fsmonitor_support(void)
112 return fsmonitor_ipc__is_supported() &&
113 fsm_settings__get_reason(the_repository) == FSMONITOR_REASON_OK;
116 static int set_recommended_config(int reconfigure)
118 struct scalar_config config[] = {
119 /* Required */
120 { "am.keepCR", "true", 1 },
121 { "core.FSCache", "true", 1 },
122 { "core.multiPackIndex", "true", 1 },
123 { "core.preloadIndex", "true", 1 },
124 #ifndef WIN32
125 { "core.untrackedCache", "true", 1 },
126 #else
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.
136 * Therefore, with a sad heart, we disable this very useful
137 * feature on Windows.
139 { "core.untrackedCache", "false", 1 },
140 #endif
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.skipHash", "false", 1 },
147 { "index.threads", "true", 1 },
148 { "index.version", "4", 1 },
149 { "merge.stat", "false", 1 },
150 { "merge.renames", "true", 1 },
151 { "pack.useBitmaps", "false", 1 },
152 { "pack.useSparse", "true", 1 },
153 { "receive.autoGC", "false", 1 },
154 { "feature.manyFiles", "false", 1 },
155 { "feature.experimental", "false", 1 },
156 { "fetch.unpackLimit", "1", 1 },
157 { "fetch.writeCommitGraph", "false", 1 },
158 #ifdef WIN32
159 { "http.sslBackend", "schannel", 1 },
160 #endif
161 /* Optional */
162 { "status.aheadBehind", "false" },
163 { "commitGraph.generationVersion", "1" },
164 { "core.autoCRLF", "false" },
165 { "core.safeCRLF", "false" },
166 { "fetch.showForcedUpdates", "false" },
167 { NULL, NULL },
169 int i;
170 char *value;
172 for (i = 0; config[i].key; i++) {
173 if (set_scalar_config(config + i, reconfigure))
174 return error(_("could not configure %s=%s"),
175 config[i].key, config[i].value);
178 if (have_fsmonitor_support()) {
179 struct scalar_config fsmonitor = { "core.fsmonitor", "true" };
180 if (set_scalar_config(&fsmonitor, reconfigure))
181 return error(_("could not configure %s=%s"),
182 fsmonitor.key, fsmonitor.value);
186 * The `log.excludeDecoration` setting is special because it allows
187 * for multiple values.
189 if (git_config_get_string("log.excludeDecoration", &value)) {
190 trace2_data_string("scalar", the_repository,
191 "log.excludeDecoration", "created");
192 if (git_config_set_multivar_gently("log.excludeDecoration",
193 "refs/prefetch/*",
194 CONFIG_REGEX_NONE, 0))
195 return error(_("could not configure "
196 "log.excludeDecoration"));
197 } else {
198 trace2_data_string("scalar", the_repository,
199 "log.excludeDecoration", "exists");
200 free(value);
203 return 0;
206 static int toggle_maintenance(int enable)
208 return run_git("maintenance",
209 enable ? "start" : "unregister",
210 enable ? NULL : "--force",
211 NULL);
214 static int add_or_remove_enlistment(int add)
216 int res;
218 if (!the_repository->worktree)
219 die(_("Scalar enlistments require a worktree"));
221 res = run_git("config", "--global", "--get", "--fixed-value",
222 "scalar.repo", the_repository->worktree, NULL);
225 * If we want to add and the setting is already there, then do nothing.
226 * If we want to remove and the setting is not there, then do nothing.
228 if ((add && !res) || (!add && res))
229 return 0;
231 return run_git("config", "--global", add ? "--add" : "--unset",
232 add ? "--no-fixed-value" : "--fixed-value",
233 "scalar.repo", the_repository->worktree, NULL);
236 static int start_fsmonitor_daemon(void)
238 assert(have_fsmonitor_support());
240 if (fsmonitor_ipc__get_state() != IPC_STATE__LISTENING)
241 return run_git("fsmonitor--daemon", "start", NULL);
243 return 0;
246 static int stop_fsmonitor_daemon(void)
248 assert(have_fsmonitor_support());
250 if (fsmonitor_ipc__get_state() == IPC_STATE__LISTENING)
251 return run_git("fsmonitor--daemon", "stop", NULL);
253 return 0;
256 static int register_dir(void)
258 if (add_or_remove_enlistment(1))
259 return error(_("could not add enlistment"));
261 if (set_recommended_config(0))
262 return error(_("could not set recommended config"));
264 if (toggle_maintenance(1))
265 warning(_("could not turn on maintenance"));
267 if (have_fsmonitor_support() && start_fsmonitor_daemon()) {
268 return error(_("could not start the FSMonitor daemon"));
271 return 0;
274 static int unregister_dir(void)
276 int res = 0;
278 if (toggle_maintenance(0))
279 res = error(_("could not turn off maintenance"));
281 if (add_or_remove_enlistment(0))
282 res = error(_("could not remove enlistment"));
284 return res;
287 /* printf-style interface, expects `<key>=<value>` argument */
288 static int set_config(const char *fmt, ...)
290 struct strbuf buf = STRBUF_INIT;
291 char *value;
292 int res;
293 va_list args;
295 va_start(args, fmt);
296 strbuf_vaddf(&buf, fmt, args);
297 va_end(args);
299 value = strchr(buf.buf, '=');
300 if (value)
301 *(value++) = '\0';
302 res = git_config_set_gently(buf.buf, value);
303 strbuf_release(&buf);
305 return res;
308 static char *remote_default_branch(const char *url)
310 struct child_process cp = CHILD_PROCESS_INIT;
311 struct strbuf out = STRBUF_INIT;
313 cp.git_cmd = 1;
314 strvec_pushl(&cp.args, "ls-remote", "--symref", url, "HEAD", NULL);
315 if (!pipe_command(&cp, NULL, 0, &out, 0, NULL, 0)) {
316 const char *line = out.buf;
318 while (*line) {
319 const char *eol = strchrnul(line, '\n'), *p;
320 size_t len = eol - line;
321 char *branch;
323 if (!skip_prefix(line, "ref: ", &p) ||
324 !strip_suffix_mem(line, &len, "\tHEAD")) {
325 line = eol + (*eol == '\n');
326 continue;
329 eol = line + len;
330 if (skip_prefix(p, "refs/heads/", &p)) {
331 branch = xstrndup(p, eol - p);
332 strbuf_release(&out);
333 return branch;
336 error(_("remote HEAD is not a branch: '%.*s'"),
337 (int)(eol - p), p);
338 strbuf_release(&out);
339 return NULL;
342 warning(_("failed to get default branch name from remote; "
343 "using local default"));
344 strbuf_reset(&out);
346 child_process_init(&cp);
347 cp.git_cmd = 1;
348 strvec_pushl(&cp.args, "symbolic-ref", "--short", "HEAD", NULL);
349 if (!pipe_command(&cp, NULL, 0, &out, 0, NULL, 0)) {
350 strbuf_trim(&out);
351 return strbuf_detach(&out, NULL);
354 strbuf_release(&out);
355 error(_("failed to get default branch name"));
356 return NULL;
359 static int delete_enlistment(struct strbuf *enlistment)
361 #ifdef WIN32
362 struct strbuf parent = STRBUF_INIT;
363 size_t offset;
364 char *path_sep;
365 #endif
367 if (unregister_dir())
368 return error(_("failed to unregister repository"));
370 #ifdef WIN32
372 * Change the current directory to one outside of the enlistment so
373 * that we may delete everything underneath it.
375 offset = offset_1st_component(enlistment->buf);
376 path_sep = find_last_dir_sep(enlistment->buf + offset);
377 strbuf_add(&parent, enlistment->buf,
378 path_sep ? path_sep - enlistment->buf : offset);
379 if (chdir(parent.buf) < 0) {
380 int res = error_errno(_("could not switch to '%s'"), parent.buf);
381 strbuf_release(&parent);
382 return res;
384 strbuf_release(&parent);
385 #endif
387 if (have_fsmonitor_support() && stop_fsmonitor_daemon())
388 return error(_("failed to stop the FSMonitor daemon"));
390 if (remove_dir_recursively(enlistment, 0))
391 return error(_("failed to delete enlistment directory"));
393 return 0;
397 * Dummy implementation; Using `get_version_info()` would cause a link error
398 * without this.
400 void load_builtin_commands(const char *prefix, struct cmdnames *cmds)
402 die("not implemented");
405 static int cmd_clone(int argc, const char **argv)
407 const char *branch = NULL;
408 int full_clone = 0, single_branch = 0, show_progress = isatty(2);
409 struct option clone_options[] = {
410 OPT_STRING('b', "branch", &branch, N_("<branch>"),
411 N_("branch to checkout after clone")),
412 OPT_BOOL(0, "full-clone", &full_clone,
413 N_("when cloning, create full working directory")),
414 OPT_BOOL(0, "single-branch", &single_branch,
415 N_("only download metadata for the branch that will "
416 "be checked out")),
417 OPT_END(),
419 const char * const clone_usage[] = {
420 N_("scalar clone [<options>] [--] <repo> [<dir>]"),
421 NULL
423 const char *url;
424 char *enlistment = NULL, *dir = NULL;
425 struct strbuf buf = STRBUF_INIT;
426 int res;
428 argc = parse_options(argc, argv, NULL, clone_options, clone_usage, 0);
430 if (argc == 2) {
431 url = argv[0];
432 enlistment = xstrdup(argv[1]);
433 } else if (argc == 1) {
434 url = argv[0];
436 strbuf_addstr(&buf, url);
437 /* Strip trailing slashes, if any */
438 while (buf.len > 0 && is_dir_sep(buf.buf[buf.len - 1]))
439 strbuf_setlen(&buf, buf.len - 1);
440 /* Strip suffix `.git`, if any */
441 strbuf_strip_suffix(&buf, ".git");
443 enlistment = find_last_dir_sep(buf.buf);
444 if (!enlistment) {
445 die(_("cannot deduce worktree name from '%s'"), url);
447 enlistment = xstrdup(enlistment + 1);
448 } else {
449 usage_msg_opt(_("You must specify a repository to clone."),
450 clone_usage, clone_options);
453 if (is_directory(enlistment))
454 die(_("directory '%s' exists already"), enlistment);
456 dir = xstrfmt("%s/src", enlistment);
458 strbuf_reset(&buf);
459 if (branch)
460 strbuf_addf(&buf, "init.defaultBranch=%s", branch);
461 else {
462 char *b = repo_default_branch_name(the_repository, 1);
463 strbuf_addf(&buf, "init.defaultBranch=%s", b);
464 free(b);
467 if ((res = run_git("-c", buf.buf, "init", "--", dir, NULL)))
468 goto cleanup;
470 if (chdir(dir) < 0) {
471 res = error_errno(_("could not switch to '%s'"), dir);
472 goto cleanup;
475 setup_git_directory();
477 /* common-main already logs `argv` */
478 trace2_def_repo(the_repository);
480 if (!branch && !(branch = remote_default_branch(url))) {
481 res = error(_("failed to get default branch for '%s'"), url);
482 goto cleanup;
485 if (set_config("remote.origin.url=%s", url) ||
486 set_config("remote.origin.fetch="
487 "+refs/heads/%s:refs/remotes/origin/%s",
488 single_branch ? branch : "*",
489 single_branch ? branch : "*") ||
490 set_config("remote.origin.promisor=true") ||
491 set_config("remote.origin.partialCloneFilter=blob:none")) {
492 res = error(_("could not configure remote in '%s'"), dir);
493 goto cleanup;
496 if (!full_clone &&
497 (res = run_git("sparse-checkout", "init", "--cone", NULL)))
498 goto cleanup;
500 if (set_recommended_config(0))
501 return error(_("could not configure '%s'"), dir);
503 if ((res = run_git("fetch", "--quiet",
504 show_progress ? "--progress" : "--no-progress",
505 "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",
515 show_progress ? "--progress" : "--no-progress",
516 "origin", NULL)))
517 goto cleanup;
520 if ((res = set_config("branch.%s.remote=origin", branch)))
521 goto cleanup;
522 if ((res = set_config("branch.%s.merge=refs/heads/%s",
523 branch, branch)))
524 goto cleanup;
526 strbuf_reset(&buf);
527 strbuf_addf(&buf, "origin/%s", branch);
528 res = run_git("checkout", "-f", "-t", buf.buf, NULL);
529 if (res)
530 goto cleanup;
532 res = register_dir();
534 cleanup:
535 free(enlistment);
536 free(dir);
537 strbuf_release(&buf);
538 return res;
541 static int cmd_diagnose(int argc, const char **argv)
543 struct option options[] = {
544 OPT_END(),
546 const char * const usage[] = {
547 N_("scalar diagnose [<enlistment>]"),
548 NULL
550 struct strbuf diagnostics_root = STRBUF_INIT;
551 int res = 0;
553 argc = parse_options(argc, argv, NULL, options,
554 usage, 0);
556 setup_enlistment_directory(argc, argv, usage, options, &diagnostics_root);
557 strbuf_addstr(&diagnostics_root, "/.scalarDiagnostics");
559 res = run_git("diagnose", "--mode=all", "-s", "%Y%m%d_%H%M%S",
560 "-o", diagnostics_root.buf, NULL);
562 strbuf_release(&diagnostics_root);
563 return res;
566 static int cmd_list(int argc, const char **argv)
568 if (argc != 1)
569 die(_("`scalar list` does not take arguments"));
571 if (run_git("config", "--global", "--get-all", "scalar.repo", NULL) < 0)
572 return -1;
573 return 0;
576 static int cmd_register(int argc, const char **argv)
578 struct option options[] = {
579 OPT_END(),
581 const char * const usage[] = {
582 N_("scalar register [<enlistment>]"),
583 NULL
586 argc = parse_options(argc, argv, NULL, options,
587 usage, 0);
589 setup_enlistment_directory(argc, argv, usage, options, NULL);
591 return register_dir();
594 static int get_scalar_repos(const char *key, const char *value, void *data)
596 struct string_list *list = data;
598 if (!strcmp(key, "scalar.repo"))
599 string_list_append(list, value);
601 return 0;
604 static int remove_deleted_enlistment(struct strbuf *path)
606 int res = 0;
607 strbuf_realpath_forgiving(path, path->buf, 1);
609 if (run_git("config", "--global",
610 "--unset", "--fixed-value",
611 "scalar.repo", path->buf, NULL) < 0)
612 res = -1;
614 if (run_git("config", "--global",
615 "--unset", "--fixed-value",
616 "maintenance.repo", path->buf, NULL) < 0)
617 res = -1;
619 return res;
622 static int cmd_reconfigure(int argc, const char **argv)
624 int all = 0;
625 struct option options[] = {
626 OPT_BOOL('a', "all", &all,
627 N_("reconfigure all registered enlistments")),
628 OPT_END(),
630 const char * const usage[] = {
631 N_("scalar reconfigure [--all | <enlistment>]"),
632 NULL
634 struct string_list scalar_repos = STRING_LIST_INIT_DUP;
635 int i, res = 0;
636 struct repository r = { NULL };
637 struct strbuf commondir = STRBUF_INIT, gitdir = STRBUF_INIT;
639 argc = parse_options(argc, argv, NULL, options,
640 usage, 0);
642 if (!all) {
643 setup_enlistment_directory(argc, argv, usage, options, NULL);
645 return set_recommended_config(1);
648 if (argc > 0)
649 usage_msg_opt(_("--all or <enlistment>, but not both"),
650 usage, options);
652 git_config(get_scalar_repos, &scalar_repos);
654 for (i = 0; i < scalar_repos.nr; i++) {
655 const char *dir = scalar_repos.items[i].string;
657 strbuf_reset(&commondir);
658 strbuf_reset(&gitdir);
660 if (chdir(dir) < 0) {
661 struct strbuf buf = STRBUF_INIT;
663 if (errno != ENOENT) {
664 warning_errno(_("could not switch to '%s'"), dir);
665 res = -1;
666 continue;
669 strbuf_addstr(&buf, dir);
670 if (remove_deleted_enlistment(&buf))
671 res = error(_("could not remove stale "
672 "scalar.repo '%s'"), dir);
673 else
674 warning(_("removing stale scalar.repo '%s'"),
675 dir);
676 strbuf_release(&buf);
677 } else if (discover_git_directory(&commondir, &gitdir) < 0) {
678 warning_errno(_("git repository gone in '%s'"), dir);
679 res = -1;
680 } else {
681 git_config_clear();
683 the_repository = &r;
684 r.commondir = commondir.buf;
685 r.gitdir = gitdir.buf;
687 if (set_recommended_config(1) < 0)
688 res = -1;
692 string_list_clear(&scalar_repos, 1);
693 strbuf_release(&commondir);
694 strbuf_release(&gitdir);
696 return res;
699 static int cmd_run(int argc, const char **argv)
701 struct option options[] = {
702 OPT_END(),
704 struct {
705 const char *arg, *task;
706 } tasks[] = {
707 { "config", NULL },
708 { "commit-graph", "commit-graph" },
709 { "fetch", "prefetch" },
710 { "loose-objects", "loose-objects" },
711 { "pack-files", "incremental-repack" },
712 { NULL, NULL }
714 struct strbuf buf = STRBUF_INIT;
715 const char *usagestr[] = { NULL, NULL };
716 int i;
718 strbuf_addstr(&buf, N_("scalar run <task> [<enlistment>]\nTasks:\n"));
719 for (i = 0; tasks[i].arg; i++)
720 strbuf_addf(&buf, "\t%s\n", tasks[i].arg);
721 usagestr[0] = buf.buf;
723 argc = parse_options(argc, argv, NULL, options,
724 usagestr, 0);
726 if (!argc)
727 usage_with_options(usagestr, options);
729 if (!strcmp("all", argv[0])) {
730 i = -1;
731 } else {
732 for (i = 0; tasks[i].arg && strcmp(tasks[i].arg, argv[0]); i++)
733 ; /* keep looking for the task */
735 if (i > 0 && !tasks[i].arg) {
736 error(_("no such task: '%s'"), argv[0]);
737 usage_with_options(usagestr, options);
741 argc--;
742 argv++;
743 setup_enlistment_directory(argc, argv, usagestr, options, NULL);
744 strbuf_release(&buf);
746 if (i == 0)
747 return register_dir();
749 if (i > 0)
750 return run_git("maintenance", "run",
751 "--task", tasks[i].task, NULL);
753 if (register_dir())
754 return -1;
755 for (i = 1; tasks[i].arg; i++)
756 if (run_git("maintenance", "run",
757 "--task", tasks[i].task, NULL))
758 return -1;
759 return 0;
762 static int cmd_unregister(int argc, const char **argv)
764 struct option options[] = {
765 OPT_END(),
767 const char * const usage[] = {
768 N_("scalar unregister [<enlistment>]"),
769 NULL
772 argc = parse_options(argc, argv, NULL, options,
773 usage, 0);
776 * Be forgiving when the enlistment or worktree does not even exist any
777 * longer; This can be the case if a user deleted the worktree by
778 * mistake and _still_ wants to unregister the thing.
780 if (argc == 1) {
781 struct strbuf src_path = STRBUF_INIT, workdir_path = STRBUF_INIT;
783 strbuf_addf(&src_path, "%s/src/.git", argv[0]);
784 strbuf_addf(&workdir_path, "%s/.git", argv[0]);
785 if (!is_directory(src_path.buf) && !is_directory(workdir_path.buf)) {
786 /* remove possible matching registrations */
787 int res = -1;
789 strbuf_strip_suffix(&src_path, "/.git");
790 res = remove_deleted_enlistment(&src_path) && res;
792 strbuf_strip_suffix(&workdir_path, "/.git");
793 res = remove_deleted_enlistment(&workdir_path) && res;
795 strbuf_release(&src_path);
796 strbuf_release(&workdir_path);
797 return res;
799 strbuf_release(&src_path);
800 strbuf_release(&workdir_path);
803 setup_enlistment_directory(argc, argv, usage, options, NULL);
805 return unregister_dir();
808 static int cmd_delete(int argc, const char **argv)
810 char *cwd = xgetcwd();
811 struct option options[] = {
812 OPT_END(),
814 const char * const usage[] = {
815 N_("scalar delete <enlistment>"),
816 NULL
818 struct strbuf enlistment = STRBUF_INIT;
819 int res = 0;
821 argc = parse_options(argc, argv, NULL, options,
822 usage, 0);
824 if (argc != 1)
825 usage_with_options(usage, options);
827 setup_enlistment_directory(argc, argv, usage, options, &enlistment);
829 if (dir_inside_of(cwd, enlistment.buf) >= 0)
830 res = error(_("refusing to delete current working directory"));
831 else {
832 close_object_store(the_repository->objects);
833 res = delete_enlistment(&enlistment);
835 strbuf_release(&enlistment);
836 free(cwd);
838 return res;
841 static int cmd_help(int argc, const char **argv)
843 struct option options[] = {
844 OPT_END(),
846 const char * const usage[] = {
847 "scalar help",
848 NULL
851 argc = parse_options(argc, argv, NULL, options,
852 usage, 0);
854 if (argc != 0)
855 usage_with_options(usage, options);
857 return run_git("help", "scalar", NULL);
860 static int cmd_version(int argc, const char **argv)
862 int verbose = 0, build_options = 0;
863 struct option options[] = {
864 OPT__VERBOSE(&verbose, N_("include Git version")),
865 OPT_BOOL(0, "build-options", &build_options,
866 N_("include Git's build options")),
867 OPT_END(),
869 const char * const usage[] = {
870 N_("scalar verbose [-v | --verbose] [--build-options]"),
871 NULL
873 struct strbuf buf = STRBUF_INIT;
875 argc = parse_options(argc, argv, NULL, options,
876 usage, 0);
878 if (argc != 0)
879 usage_with_options(usage, options);
881 get_version_info(&buf, build_options);
882 fprintf(stderr, "%s\n", buf.buf);
883 strbuf_release(&buf);
885 return 0;
888 static struct {
889 const char *name;
890 int (*fn)(int, const char **);
891 } builtins[] = {
892 { "clone", cmd_clone },
893 { "list", cmd_list },
894 { "register", cmd_register },
895 { "unregister", cmd_unregister },
896 { "run", cmd_run },
897 { "reconfigure", cmd_reconfigure },
898 { "delete", cmd_delete },
899 { "help", cmd_help },
900 { "version", cmd_version },
901 { "diagnose", cmd_diagnose },
902 { NULL, NULL},
905 int cmd_main(int argc, const char **argv)
907 struct strbuf scalar_usage = STRBUF_INIT;
908 int i;
910 while (argc > 1 && *argv[1] == '-') {
911 if (!strcmp(argv[1], "-C")) {
912 if (argc < 3)
913 die(_("-C requires a <directory>"));
914 if (chdir(argv[2]) < 0)
915 die_errno(_("could not change to '%s'"),
916 argv[2]);
917 argc -= 2;
918 argv += 2;
919 } else if (!strcmp(argv[1], "-c")) {
920 if (argc < 3)
921 die(_("-c requires a <key>=<value> argument"));
922 git_config_push_parameter(argv[2]);
923 argc -= 2;
924 argv += 2;
925 } else
926 break;
929 if (argc > 1) {
930 argv++;
931 argc--;
933 for (i = 0; builtins[i].name; i++)
934 if (!strcmp(builtins[i].name, argv[0]))
935 return !!builtins[i].fn(argc, argv);
938 strbuf_addstr(&scalar_usage,
939 N_("scalar [-C <directory>] [-c <key>=<value>] "
940 "<command> [<options>]\n\nCommands:\n"));
941 for (i = 0; builtins[i].name; i++)
942 strbuf_addf(&scalar_usage, "\t%s\n", builtins[i].name);
944 usage(scalar_usage.buf);