cache.h: remove expand_user_path()
[alt-git.git] / scalar.c
blobfe61a3ebdd7834e680c5b184b22d49d5d11c9154
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"
19 static void setup_enlistment_directory(int argc, const char **argv,
20 const char * const *usagestr,
21 const struct option *options,
22 struct strbuf *enlistment_root)
24 struct strbuf path = STRBUF_INIT;
25 int enlistment_is_repo_parent = 0;
26 size_t len;
28 if (startup_info->have_repository)
29 BUG("gitdir already set up?!?");
31 if (argc > 1)
32 usage_with_options(usagestr, options);
34 /* find the worktree, determine its corresponding root */
35 if (argc == 1) {
36 strbuf_add_absolute_path(&path, argv[0]);
37 if (!is_directory(path.buf))
38 die(_("'%s' does not exist"), path.buf);
39 if (chdir(path.buf) < 0)
40 die_errno(_("could not switch to '%s'"), path.buf);
41 } else if (strbuf_getcwd(&path) < 0)
42 die(_("need a working directory"));
44 strbuf_trim_trailing_dir_sep(&path);
46 /* check if currently in enlistment root with src/ workdir */
47 len = path.len;
48 strbuf_addstr(&path, "/src");
49 if (is_nonbare_repository_dir(&path)) {
50 enlistment_is_repo_parent = 1;
51 if (chdir(path.buf) < 0)
52 die_errno(_("could not switch to '%s'"), path.buf);
54 strbuf_setlen(&path, len);
56 setup_git_directory();
58 if (!the_repository->worktree)
59 die(_("Scalar enlistments require a worktree"));
61 if (enlistment_root) {
62 if (enlistment_is_repo_parent)
63 strbuf_addbuf(enlistment_root, &path);
64 else
65 strbuf_addstr(enlistment_root, the_repository->worktree);
68 strbuf_release(&path);
71 static int run_git(const char *arg, ...)
73 struct child_process cmd = CHILD_PROCESS_INIT;
74 va_list args;
75 const char *p;
77 va_start(args, arg);
78 strvec_push(&cmd.args, arg);
79 while ((p = va_arg(args, const char *)))
80 strvec_push(&cmd.args, p);
81 va_end(args);
83 cmd.git_cmd = 1;
84 return run_command(&cmd);
87 struct scalar_config {
88 const char *key;
89 const char *value;
90 int overwrite_on_reconfigure;
93 static int set_scalar_config(const struct scalar_config *config, int reconfigure)
95 char *value = NULL;
96 int res;
98 if ((reconfigure && config->overwrite_on_reconfigure) ||
99 git_config_get_string(config->key, &value)) {
100 trace2_data_string("scalar", the_repository, config->key, "created");
101 res = git_config_set_gently(config->key, config->value);
102 } else {
103 trace2_data_string("scalar", the_repository, config->key, "exists");
104 res = 0;
107 free(value);
108 return res;
111 static int have_fsmonitor_support(void)
113 return fsmonitor_ipc__is_supported() &&
114 fsm_settings__get_reason(the_repository) == FSMONITOR_REASON_OK;
117 static int set_recommended_config(int reconfigure)
119 struct scalar_config config[] = {
120 /* Required */
121 { "am.keepCR", "true", 1 },
122 { "core.FSCache", "true", 1 },
123 { "core.multiPackIndex", "true", 1 },
124 { "core.preloadIndex", "true", 1 },
125 #ifndef WIN32
126 { "core.untrackedCache", "true", 1 },
127 #else
129 * Unfortunately, Scalar's Functional Tests demonstrated
130 * that the untracked cache feature is unreliable on Windows
131 * (which is a bummer because that platform would benefit the
132 * most from it). For some reason, freshly created files seem
133 * not to update the directory's `lastModified` time
134 * immediately, but the untracked cache would need to rely on
135 * that.
137 * Therefore, with a sad heart, we disable this very useful
138 * feature on Windows.
140 { "core.untrackedCache", "false", 1 },
141 #endif
142 { "core.logAllRefUpdates", "true", 1 },
143 { "credential.https://dev.azure.com.useHttpPath", "true", 1 },
144 { "credential.validate", "false", 1 }, /* GCM4W-only */
145 { "gc.auto", "0", 1 },
146 { "gui.GCWarning", "false", 1 },
147 { "index.skipHash", "false", 1 },
148 { "index.threads", "true", 1 },
149 { "index.version", "4", 1 },
150 { "merge.stat", "false", 1 },
151 { "merge.renames", "true", 1 },
152 { "pack.useBitmaps", "false", 1 },
153 { "pack.useSparse", "true", 1 },
154 { "receive.autoGC", "false", 1 },
155 { "feature.manyFiles", "false", 1 },
156 { "feature.experimental", "false", 1 },
157 { "fetch.unpackLimit", "1", 1 },
158 { "fetch.writeCommitGraph", "false", 1 },
159 #ifdef WIN32
160 { "http.sslBackend", "schannel", 1 },
161 #endif
162 /* Optional */
163 { "status.aheadBehind", "false" },
164 { "commitGraph.generationVersion", "1" },
165 { "core.autoCRLF", "false" },
166 { "core.safeCRLF", "false" },
167 { "fetch.showForcedUpdates", "false" },
168 { NULL, NULL },
170 int i;
171 char *value;
173 for (i = 0; config[i].key; i++) {
174 if (set_scalar_config(config + i, reconfigure))
175 return error(_("could not configure %s=%s"),
176 config[i].key, config[i].value);
179 if (have_fsmonitor_support()) {
180 struct scalar_config fsmonitor = { "core.fsmonitor", "true" };
181 if (set_scalar_config(&fsmonitor, reconfigure))
182 return error(_("could not configure %s=%s"),
183 fsmonitor.key, fsmonitor.value);
187 * The `log.excludeDecoration` setting is special because it allows
188 * for multiple values.
190 if (git_config_get_string("log.excludeDecoration", &value)) {
191 trace2_data_string("scalar", the_repository,
192 "log.excludeDecoration", "created");
193 if (git_config_set_multivar_gently("log.excludeDecoration",
194 "refs/prefetch/*",
195 CONFIG_REGEX_NONE, 0))
196 return error(_("could not configure "
197 "log.excludeDecoration"));
198 } else {
199 trace2_data_string("scalar", the_repository,
200 "log.excludeDecoration", "exists");
201 free(value);
204 return 0;
207 static int toggle_maintenance(int enable)
209 return run_git("maintenance",
210 enable ? "start" : "unregister",
211 enable ? NULL : "--force",
212 NULL);
215 static int add_or_remove_enlistment(int add)
217 int res;
219 if (!the_repository->worktree)
220 die(_("Scalar enlistments require a worktree"));
222 res = run_git("config", "--global", "--get", "--fixed-value",
223 "scalar.repo", the_repository->worktree, NULL);
226 * If we want to add and the setting is already there, then do nothing.
227 * If we want to remove and the setting is not there, then do nothing.
229 if ((add && !res) || (!add && res))
230 return 0;
232 return run_git("config", "--global", add ? "--add" : "--unset",
233 add ? "--no-fixed-value" : "--fixed-value",
234 "scalar.repo", the_repository->worktree, NULL);
237 static int start_fsmonitor_daemon(void)
239 assert(have_fsmonitor_support());
241 if (fsmonitor_ipc__get_state() != IPC_STATE__LISTENING)
242 return run_git("fsmonitor--daemon", "start", NULL);
244 return 0;
247 static int stop_fsmonitor_daemon(void)
249 assert(have_fsmonitor_support());
251 if (fsmonitor_ipc__get_state() == IPC_STATE__LISTENING)
252 return run_git("fsmonitor--daemon", "stop", NULL);
254 return 0;
257 static int register_dir(void)
259 if (add_or_remove_enlistment(1))
260 return error(_("could not add enlistment"));
262 if (set_recommended_config(0))
263 return error(_("could not set recommended config"));
265 if (toggle_maintenance(1))
266 warning(_("could not turn on maintenance"));
268 if (have_fsmonitor_support() && start_fsmonitor_daemon()) {
269 return error(_("could not start the FSMonitor daemon"));
272 return 0;
275 static int unregister_dir(void)
277 int res = 0;
279 if (toggle_maintenance(0))
280 res = error(_("could not turn off maintenance"));
282 if (add_or_remove_enlistment(0))
283 res = error(_("could not remove enlistment"));
285 return res;
288 /* printf-style interface, expects `<key>=<value>` argument */
289 static int set_config(const char *fmt, ...)
291 struct strbuf buf = STRBUF_INIT;
292 char *value;
293 int res;
294 va_list args;
296 va_start(args, fmt);
297 strbuf_vaddf(&buf, fmt, args);
298 va_end(args);
300 value = strchr(buf.buf, '=');
301 if (value)
302 *(value++) = '\0';
303 res = git_config_set_gently(buf.buf, value);
304 strbuf_release(&buf);
306 return res;
309 static char *remote_default_branch(const char *url)
311 struct child_process cp = CHILD_PROCESS_INIT;
312 struct strbuf out = STRBUF_INIT;
314 cp.git_cmd = 1;
315 strvec_pushl(&cp.args, "ls-remote", "--symref", url, "HEAD", NULL);
316 if (!pipe_command(&cp, NULL, 0, &out, 0, NULL, 0)) {
317 const char *line = out.buf;
319 while (*line) {
320 const char *eol = strchrnul(line, '\n'), *p;
321 size_t len = eol - line;
322 char *branch;
324 if (!skip_prefix(line, "ref: ", &p) ||
325 !strip_suffix_mem(line, &len, "\tHEAD")) {
326 line = eol + (*eol == '\n');
327 continue;
330 eol = line + len;
331 if (skip_prefix(p, "refs/heads/", &p)) {
332 branch = xstrndup(p, eol - p);
333 strbuf_release(&out);
334 return branch;
337 error(_("remote HEAD is not a branch: '%.*s'"),
338 (int)(eol - p), p);
339 strbuf_release(&out);
340 return NULL;
343 warning(_("failed to get default branch name from remote; "
344 "using local default"));
345 strbuf_reset(&out);
347 child_process_init(&cp);
348 cp.git_cmd = 1;
349 strvec_pushl(&cp.args, "symbolic-ref", "--short", "HEAD", NULL);
350 if (!pipe_command(&cp, NULL, 0, &out, 0, NULL, 0)) {
351 strbuf_trim(&out);
352 return strbuf_detach(&out, NULL);
355 strbuf_release(&out);
356 error(_("failed to get default branch name"));
357 return NULL;
360 static int delete_enlistment(struct strbuf *enlistment)
362 #ifdef WIN32
363 struct strbuf parent = STRBUF_INIT;
364 size_t offset;
365 char *path_sep;
366 #endif
368 if (unregister_dir())
369 return error(_("failed to unregister repository"));
371 #ifdef WIN32
373 * Change the current directory to one outside of the enlistment so
374 * that we may delete everything underneath it.
376 offset = offset_1st_component(enlistment->buf);
377 path_sep = find_last_dir_sep(enlistment->buf + offset);
378 strbuf_add(&parent, enlistment->buf,
379 path_sep ? path_sep - enlistment->buf : offset);
380 if (chdir(parent.buf) < 0) {
381 int res = error_errno(_("could not switch to '%s'"), parent.buf);
382 strbuf_release(&parent);
383 return res;
385 strbuf_release(&parent);
386 #endif
388 if (have_fsmonitor_support() && stop_fsmonitor_daemon())
389 return error(_("failed to stop the FSMonitor daemon"));
391 if (remove_dir_recursively(enlistment, 0))
392 return error(_("failed to delete enlistment directory"));
394 return 0;
398 * Dummy implementation; Using `get_version_info()` would cause a link error
399 * without this.
401 void load_builtin_commands(const char *prefix, struct cmdnames *cmds)
403 die("not implemented");
406 static int cmd_clone(int argc, const char **argv)
408 const char *branch = NULL;
409 int full_clone = 0, single_branch = 0, show_progress = isatty(2);
410 struct option clone_options[] = {
411 OPT_STRING('b', "branch", &branch, N_("<branch>"),
412 N_("branch to checkout after clone")),
413 OPT_BOOL(0, "full-clone", &full_clone,
414 N_("when cloning, create full working directory")),
415 OPT_BOOL(0, "single-branch", &single_branch,
416 N_("only download metadata for the branch that will "
417 "be checked out")),
418 OPT_END(),
420 const char * const clone_usage[] = {
421 N_("scalar clone [<options>] [--] <repo> [<dir>]"),
422 NULL
424 const char *url;
425 char *enlistment = NULL, *dir = NULL;
426 struct strbuf buf = STRBUF_INIT;
427 int res;
429 argc = parse_options(argc, argv, NULL, clone_options, clone_usage, 0);
431 if (argc == 2) {
432 url = argv[0];
433 enlistment = xstrdup(argv[1]);
434 } else if (argc == 1) {
435 url = argv[0];
437 strbuf_addstr(&buf, url);
438 /* Strip trailing slashes, if any */
439 while (buf.len > 0 && is_dir_sep(buf.buf[buf.len - 1]))
440 strbuf_setlen(&buf, buf.len - 1);
441 /* Strip suffix `.git`, if any */
442 strbuf_strip_suffix(&buf, ".git");
444 enlistment = find_last_dir_sep(buf.buf);
445 if (!enlistment) {
446 die(_("cannot deduce worktree name from '%s'"), url);
448 enlistment = xstrdup(enlistment + 1);
449 } else {
450 usage_msg_opt(_("You must specify a repository to clone."),
451 clone_usage, clone_options);
454 if (is_directory(enlistment))
455 die(_("directory '%s' exists already"), enlistment);
457 dir = xstrfmt("%s/src", enlistment);
459 strbuf_reset(&buf);
460 if (branch)
461 strbuf_addf(&buf, "init.defaultBranch=%s", branch);
462 else {
463 char *b = repo_default_branch_name(the_repository, 1);
464 strbuf_addf(&buf, "init.defaultBranch=%s", b);
465 free(b);
468 if ((res = run_git("-c", buf.buf, "init", "--", dir, NULL)))
469 goto cleanup;
471 if (chdir(dir) < 0) {
472 res = error_errno(_("could not switch to '%s'"), dir);
473 goto cleanup;
476 setup_git_directory();
478 /* common-main already logs `argv` */
479 trace2_def_repo(the_repository);
481 if (!branch && !(branch = remote_default_branch(url))) {
482 res = error(_("failed to get default branch for '%s'"), url);
483 goto cleanup;
486 if (set_config("remote.origin.url=%s", url) ||
487 set_config("remote.origin.fetch="
488 "+refs/heads/%s:refs/remotes/origin/%s",
489 single_branch ? branch : "*",
490 single_branch ? branch : "*") ||
491 set_config("remote.origin.promisor=true") ||
492 set_config("remote.origin.partialCloneFilter=blob:none")) {
493 res = error(_("could not configure remote in '%s'"), dir);
494 goto cleanup;
497 if (!full_clone &&
498 (res = run_git("sparse-checkout", "init", "--cone", NULL)))
499 goto cleanup;
501 if (set_recommended_config(0))
502 return error(_("could not configure '%s'"), dir);
504 if ((res = run_git("fetch", "--quiet",
505 show_progress ? "--progress" : "--no-progress",
506 "origin", NULL))) {
507 warning(_("partial clone failed; attempting full clone"));
509 if (set_config("remote.origin.promisor") ||
510 set_config("remote.origin.partialCloneFilter")) {
511 res = error(_("could not configure for full clone"));
512 goto cleanup;
515 if ((res = run_git("fetch", "--quiet",
516 show_progress ? "--progress" : "--no-progress",
517 "origin", NULL)))
518 goto cleanup;
521 if ((res = set_config("branch.%s.remote=origin", branch)))
522 goto cleanup;
523 if ((res = set_config("branch.%s.merge=refs/heads/%s",
524 branch, branch)))
525 goto cleanup;
527 strbuf_reset(&buf);
528 strbuf_addf(&buf, "origin/%s", branch);
529 res = run_git("checkout", "-f", "-t", buf.buf, NULL);
530 if (res)
531 goto cleanup;
533 res = register_dir();
535 cleanup:
536 free(enlistment);
537 free(dir);
538 strbuf_release(&buf);
539 return res;
542 static int cmd_diagnose(int argc, const char **argv)
544 struct option options[] = {
545 OPT_END(),
547 const char * const usage[] = {
548 N_("scalar diagnose [<enlistment>]"),
549 NULL
551 struct strbuf diagnostics_root = STRBUF_INIT;
552 int res = 0;
554 argc = parse_options(argc, argv, NULL, options,
555 usage, 0);
557 setup_enlistment_directory(argc, argv, usage, options, &diagnostics_root);
558 strbuf_addstr(&diagnostics_root, "/.scalarDiagnostics");
560 res = run_git("diagnose", "--mode=all", "-s", "%Y%m%d_%H%M%S",
561 "-o", diagnostics_root.buf, NULL);
563 strbuf_release(&diagnostics_root);
564 return res;
567 static int cmd_list(int argc, const char **argv)
569 if (argc != 1)
570 die(_("`scalar list` does not take arguments"));
572 if (run_git("config", "--global", "--get-all", "scalar.repo", NULL) < 0)
573 return -1;
574 return 0;
577 static int cmd_register(int argc, const char **argv)
579 struct option options[] = {
580 OPT_END(),
582 const char * const usage[] = {
583 N_("scalar register [<enlistment>]"),
584 NULL
587 argc = parse_options(argc, argv, NULL, options,
588 usage, 0);
590 setup_enlistment_directory(argc, argv, usage, options, NULL);
592 return register_dir();
595 static int get_scalar_repos(const char *key, const char *value, void *data)
597 struct string_list *list = data;
599 if (!strcmp(key, "scalar.repo"))
600 string_list_append(list, value);
602 return 0;
605 static int remove_deleted_enlistment(struct strbuf *path)
607 int res = 0;
608 strbuf_realpath_forgiving(path, path->buf, 1);
610 if (run_git("config", "--global",
611 "--unset", "--fixed-value",
612 "scalar.repo", path->buf, NULL) < 0)
613 res = -1;
615 if (run_git("config", "--global",
616 "--unset", "--fixed-value",
617 "maintenance.repo", path->buf, NULL) < 0)
618 res = -1;
620 return res;
623 static int cmd_reconfigure(int argc, const char **argv)
625 int all = 0;
626 struct option options[] = {
627 OPT_BOOL('a', "all", &all,
628 N_("reconfigure all registered enlistments")),
629 OPT_END(),
631 const char * const usage[] = {
632 N_("scalar reconfigure [--all | <enlistment>]"),
633 NULL
635 struct string_list scalar_repos = STRING_LIST_INIT_DUP;
636 int i, res = 0;
637 struct repository r = { NULL };
638 struct strbuf commondir = STRBUF_INIT, gitdir = STRBUF_INIT;
640 argc = parse_options(argc, argv, NULL, options,
641 usage, 0);
643 if (!all) {
644 setup_enlistment_directory(argc, argv, usage, options, NULL);
646 return set_recommended_config(1);
649 if (argc > 0)
650 usage_msg_opt(_("--all or <enlistment>, but not both"),
651 usage, options);
653 git_config(get_scalar_repos, &scalar_repos);
655 for (i = 0; i < scalar_repos.nr; i++) {
656 const char *dir = scalar_repos.items[i].string;
658 strbuf_reset(&commondir);
659 strbuf_reset(&gitdir);
661 if (chdir(dir) < 0) {
662 struct strbuf buf = STRBUF_INIT;
664 if (errno != ENOENT) {
665 warning_errno(_("could not switch to '%s'"), dir);
666 res = -1;
667 continue;
670 strbuf_addstr(&buf, dir);
671 if (remove_deleted_enlistment(&buf))
672 res = error(_("could not remove stale "
673 "scalar.repo '%s'"), dir);
674 else
675 warning(_("removing stale scalar.repo '%s'"),
676 dir);
677 strbuf_release(&buf);
678 } else if (discover_git_directory(&commondir, &gitdir) < 0) {
679 warning_errno(_("git repository gone in '%s'"), dir);
680 res = -1;
681 } else {
682 git_config_clear();
684 the_repository = &r;
685 r.commondir = commondir.buf;
686 r.gitdir = gitdir.buf;
688 if (set_recommended_config(1) < 0)
689 res = -1;
693 string_list_clear(&scalar_repos, 1);
694 strbuf_release(&commondir);
695 strbuf_release(&gitdir);
697 return res;
700 static int cmd_run(int argc, const char **argv)
702 struct option options[] = {
703 OPT_END(),
705 struct {
706 const char *arg, *task;
707 } tasks[] = {
708 { "config", NULL },
709 { "commit-graph", "commit-graph" },
710 { "fetch", "prefetch" },
711 { "loose-objects", "loose-objects" },
712 { "pack-files", "incremental-repack" },
713 { NULL, NULL }
715 struct strbuf buf = STRBUF_INIT;
716 const char *usagestr[] = { NULL, NULL };
717 int i;
719 strbuf_addstr(&buf, N_("scalar run <task> [<enlistment>]\nTasks:\n"));
720 for (i = 0; tasks[i].arg; i++)
721 strbuf_addf(&buf, "\t%s\n", tasks[i].arg);
722 usagestr[0] = buf.buf;
724 argc = parse_options(argc, argv, NULL, options,
725 usagestr, 0);
727 if (!argc)
728 usage_with_options(usagestr, options);
730 if (!strcmp("all", argv[0])) {
731 i = -1;
732 } else {
733 for (i = 0; tasks[i].arg && strcmp(tasks[i].arg, argv[0]); i++)
734 ; /* keep looking for the task */
736 if (i > 0 && !tasks[i].arg) {
737 error(_("no such task: '%s'"), argv[0]);
738 usage_with_options(usagestr, options);
742 argc--;
743 argv++;
744 setup_enlistment_directory(argc, argv, usagestr, options, NULL);
745 strbuf_release(&buf);
747 if (i == 0)
748 return register_dir();
750 if (i > 0)
751 return run_git("maintenance", "run",
752 "--task", tasks[i].task, NULL);
754 if (register_dir())
755 return -1;
756 for (i = 1; tasks[i].arg; i++)
757 if (run_git("maintenance", "run",
758 "--task", tasks[i].task, NULL))
759 return -1;
760 return 0;
763 static int cmd_unregister(int argc, const char **argv)
765 struct option options[] = {
766 OPT_END(),
768 const char * const usage[] = {
769 N_("scalar unregister [<enlistment>]"),
770 NULL
773 argc = parse_options(argc, argv, NULL, options,
774 usage, 0);
777 * Be forgiving when the enlistment or worktree does not even exist any
778 * longer; This can be the case if a user deleted the worktree by
779 * mistake and _still_ wants to unregister the thing.
781 if (argc == 1) {
782 struct strbuf src_path = STRBUF_INIT, workdir_path = STRBUF_INIT;
784 strbuf_addf(&src_path, "%s/src/.git", argv[0]);
785 strbuf_addf(&workdir_path, "%s/.git", argv[0]);
786 if (!is_directory(src_path.buf) && !is_directory(workdir_path.buf)) {
787 /* remove possible matching registrations */
788 int res = -1;
790 strbuf_strip_suffix(&src_path, "/.git");
791 res = remove_deleted_enlistment(&src_path) && res;
793 strbuf_strip_suffix(&workdir_path, "/.git");
794 res = remove_deleted_enlistment(&workdir_path) && res;
796 strbuf_release(&src_path);
797 strbuf_release(&workdir_path);
798 return res;
800 strbuf_release(&src_path);
801 strbuf_release(&workdir_path);
804 setup_enlistment_directory(argc, argv, usage, options, NULL);
806 return unregister_dir();
809 static int cmd_delete(int argc, const char **argv)
811 char *cwd = xgetcwd();
812 struct option options[] = {
813 OPT_END(),
815 const char * const usage[] = {
816 N_("scalar delete <enlistment>"),
817 NULL
819 struct strbuf enlistment = STRBUF_INIT;
820 int res = 0;
822 argc = parse_options(argc, argv, NULL, options,
823 usage, 0);
825 if (argc != 1)
826 usage_with_options(usage, options);
828 setup_enlistment_directory(argc, argv, usage, options, &enlistment);
830 if (dir_inside_of(cwd, enlistment.buf) >= 0)
831 res = error(_("refusing to delete current working directory"));
832 else {
833 close_object_store(the_repository->objects);
834 res = delete_enlistment(&enlistment);
836 strbuf_release(&enlistment);
837 free(cwd);
839 return res;
842 static int cmd_help(int argc, const char **argv)
844 struct option options[] = {
845 OPT_END(),
847 const char * const usage[] = {
848 "scalar help",
849 NULL
852 argc = parse_options(argc, argv, NULL, options,
853 usage, 0);
855 if (argc != 0)
856 usage_with_options(usage, options);
858 return run_git("help", "scalar", NULL);
861 static int cmd_version(int argc, const char **argv)
863 int verbose = 0, build_options = 0;
864 struct option options[] = {
865 OPT__VERBOSE(&verbose, N_("include Git version")),
866 OPT_BOOL(0, "build-options", &build_options,
867 N_("include Git's build options")),
868 OPT_END(),
870 const char * const usage[] = {
871 N_("scalar verbose [-v | --verbose] [--build-options]"),
872 NULL
874 struct strbuf buf = STRBUF_INIT;
876 argc = parse_options(argc, argv, NULL, options,
877 usage, 0);
879 if (argc != 0)
880 usage_with_options(usage, options);
882 get_version_info(&buf, build_options);
883 fprintf(stderr, "%s\n", buf.buf);
884 strbuf_release(&buf);
886 return 0;
889 static struct {
890 const char *name;
891 int (*fn)(int, const char **);
892 } builtins[] = {
893 { "clone", cmd_clone },
894 { "list", cmd_list },
895 { "register", cmd_register },
896 { "unregister", cmd_unregister },
897 { "run", cmd_run },
898 { "reconfigure", cmd_reconfigure },
899 { "delete", cmd_delete },
900 { "help", cmd_help },
901 { "version", cmd_version },
902 { "diagnose", cmd_diagnose },
903 { NULL, NULL},
906 int cmd_main(int argc, const char **argv)
908 struct strbuf scalar_usage = STRBUF_INIT;
909 int i;
911 while (argc > 1 && *argv[1] == '-') {
912 if (!strcmp(argv[1], "-C")) {
913 if (argc < 3)
914 die(_("-C requires a <directory>"));
915 if (chdir(argv[2]) < 0)
916 die_errno(_("could not change to '%s'"),
917 argv[2]);
918 argc -= 2;
919 argv += 2;
920 } else if (!strcmp(argv[1], "-c")) {
921 if (argc < 3)
922 die(_("-c requires a <key>=<value> argument"));
923 git_config_push_parameter(argv[2]);
924 argc -= 2;
925 argv += 2;
926 } else
927 break;
930 if (argc > 1) {
931 argv++;
932 argc--;
934 for (i = 0; builtins[i].name; i++)
935 if (!strcmp(builtins[i].name, argv[0]))
936 return !!builtins[i].fn(argc, argv);
939 strbuf_addstr(&scalar_usage,
940 N_("scalar [-C <directory>] [-c <key>=<value>] "
941 "<command> [<options>]\n\nCommands:\n"));
942 for (i = 0; builtins[i].name; i++)
943 strbuf_addf(&scalar_usage, "\t%s\n", builtins[i].name);
945 usage(scalar_usage.buf);