Merge branch 'jk/curl-avoid-deprecated-api'
[git.git] / scalar.c
blobb49bb8c24ec74d4eabdfca504275291f5d4bfa32
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 return error(_("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;
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", "origin", NULL))) {
504 warning(_("partial clone failed; attempting full clone"));
506 if (set_config("remote.origin.promisor") ||
507 set_config("remote.origin.partialCloneFilter")) {
508 res = error(_("could not configure for full clone"));
509 goto cleanup;
512 if ((res = run_git("fetch", "--quiet", "origin", NULL)))
513 goto cleanup;
516 if ((res = set_config("branch.%s.remote=origin", branch)))
517 goto cleanup;
518 if ((res = set_config("branch.%s.merge=refs/heads/%s",
519 branch, branch)))
520 goto cleanup;
522 strbuf_reset(&buf);
523 strbuf_addf(&buf, "origin/%s", branch);
524 res = run_git("checkout", "-f", "-t", buf.buf, NULL);
525 if (res)
526 goto cleanup;
528 res = register_dir();
530 cleanup:
531 free(enlistment);
532 free(dir);
533 strbuf_release(&buf);
534 return res;
537 static int cmd_diagnose(int argc, const char **argv)
539 struct option options[] = {
540 OPT_END(),
542 const char * const usage[] = {
543 N_("scalar diagnose [<enlistment>]"),
544 NULL
546 struct strbuf diagnostics_root = STRBUF_INIT;
547 int res = 0;
549 argc = parse_options(argc, argv, NULL, options,
550 usage, 0);
552 setup_enlistment_directory(argc, argv, usage, options, &diagnostics_root);
553 strbuf_addstr(&diagnostics_root, "/.scalarDiagnostics");
555 res = run_git("diagnose", "--mode=all", "-s", "%Y%m%d_%H%M%S",
556 "-o", diagnostics_root.buf, NULL);
558 strbuf_release(&diagnostics_root);
559 return res;
562 static int cmd_list(int argc, const char **argv)
564 if (argc != 1)
565 die(_("`scalar list` does not take arguments"));
567 if (run_git("config", "--global", "--get-all", "scalar.repo", NULL) < 0)
568 return -1;
569 return 0;
572 static int cmd_register(int argc, const char **argv)
574 struct option options[] = {
575 OPT_END(),
577 const char * const usage[] = {
578 N_("scalar register [<enlistment>]"),
579 NULL
582 argc = parse_options(argc, argv, NULL, options,
583 usage, 0);
585 setup_enlistment_directory(argc, argv, usage, options, NULL);
587 return register_dir();
590 static int get_scalar_repos(const char *key, const char *value, void *data)
592 struct string_list *list = data;
594 if (!strcmp(key, "scalar.repo"))
595 string_list_append(list, value);
597 return 0;
600 static int remove_deleted_enlistment(struct strbuf *path)
602 int res = 0;
603 strbuf_realpath_forgiving(path, path->buf, 1);
605 if (run_git("config", "--global",
606 "--unset", "--fixed-value",
607 "scalar.repo", path->buf, NULL) < 0)
608 res = -1;
610 if (run_git("config", "--global",
611 "--unset", "--fixed-value",
612 "maintenance.repo", path->buf, NULL) < 0)
613 res = -1;
615 return res;
618 static int cmd_reconfigure(int argc, const char **argv)
620 int all = 0;
621 struct option options[] = {
622 OPT_BOOL('a', "all", &all,
623 N_("reconfigure all registered enlistments")),
624 OPT_END(),
626 const char * const usage[] = {
627 N_("scalar reconfigure [--all | <enlistment>]"),
628 NULL
630 struct string_list scalar_repos = STRING_LIST_INIT_DUP;
631 int i, res = 0;
632 struct repository r = { NULL };
633 struct strbuf commondir = STRBUF_INIT, gitdir = STRBUF_INIT;
635 argc = parse_options(argc, argv, NULL, options,
636 usage, 0);
638 if (!all) {
639 setup_enlistment_directory(argc, argv, usage, options, NULL);
641 return set_recommended_config(1);
644 if (argc > 0)
645 usage_msg_opt(_("--all or <enlistment>, but not both"),
646 usage, options);
648 git_config(get_scalar_repos, &scalar_repos);
650 for (i = 0; i < scalar_repos.nr; i++) {
651 const char *dir = scalar_repos.items[i].string;
653 strbuf_reset(&commondir);
654 strbuf_reset(&gitdir);
656 if (chdir(dir) < 0) {
657 struct strbuf buf = STRBUF_INIT;
659 if (errno != ENOENT) {
660 warning_errno(_("could not switch to '%s'"), dir);
661 res = -1;
662 continue;
665 strbuf_addstr(&buf, dir);
666 if (remove_deleted_enlistment(&buf))
667 res = error(_("could not remove stale "
668 "scalar.repo '%s'"), dir);
669 else
670 warning(_("removing stale scalar.repo '%s'"),
671 dir);
672 strbuf_release(&buf);
673 } else if (discover_git_directory(&commondir, &gitdir) < 0) {
674 warning_errno(_("git repository gone in '%s'"), dir);
675 res = -1;
676 } else {
677 git_config_clear();
679 the_repository = &r;
680 r.commondir = commondir.buf;
681 r.gitdir = gitdir.buf;
683 if (set_recommended_config(1) < 0)
684 res = -1;
688 string_list_clear(&scalar_repos, 1);
689 strbuf_release(&commondir);
690 strbuf_release(&gitdir);
692 return res;
695 static int cmd_run(int argc, const char **argv)
697 struct option options[] = {
698 OPT_END(),
700 struct {
701 const char *arg, *task;
702 } tasks[] = {
703 { "config", NULL },
704 { "commit-graph", "commit-graph" },
705 { "fetch", "prefetch" },
706 { "loose-objects", "loose-objects" },
707 { "pack-files", "incremental-repack" },
708 { NULL, NULL }
710 struct strbuf buf = STRBUF_INIT;
711 const char *usagestr[] = { NULL, NULL };
712 int i;
714 strbuf_addstr(&buf, N_("scalar run <task> [<enlistment>]\nTasks:\n"));
715 for (i = 0; tasks[i].arg; i++)
716 strbuf_addf(&buf, "\t%s\n", tasks[i].arg);
717 usagestr[0] = buf.buf;
719 argc = parse_options(argc, argv, NULL, options,
720 usagestr, 0);
722 if (!argc)
723 usage_with_options(usagestr, options);
725 if (!strcmp("all", argv[0])) {
726 i = -1;
727 } else {
728 for (i = 0; tasks[i].arg && strcmp(tasks[i].arg, argv[0]); i++)
729 ; /* keep looking for the task */
731 if (i > 0 && !tasks[i].arg) {
732 error(_("no such task: '%s'"), argv[0]);
733 usage_with_options(usagestr, options);
737 argc--;
738 argv++;
739 setup_enlistment_directory(argc, argv, usagestr, options, NULL);
740 strbuf_release(&buf);
742 if (i == 0)
743 return register_dir();
745 if (i > 0)
746 return run_git("maintenance", "run",
747 "--task", tasks[i].task, NULL);
749 if (register_dir())
750 return -1;
751 for (i = 1; tasks[i].arg; i++)
752 if (run_git("maintenance", "run",
753 "--task", tasks[i].task, NULL))
754 return -1;
755 return 0;
758 static int cmd_unregister(int argc, const char **argv)
760 struct option options[] = {
761 OPT_END(),
763 const char * const usage[] = {
764 N_("scalar unregister [<enlistment>]"),
765 NULL
768 argc = parse_options(argc, argv, NULL, options,
769 usage, 0);
772 * Be forgiving when the enlistment or worktree does not even exist any
773 * longer; This can be the case if a user deleted the worktree by
774 * mistake and _still_ wants to unregister the thing.
776 if (argc == 1) {
777 struct strbuf src_path = STRBUF_INIT, workdir_path = STRBUF_INIT;
779 strbuf_addf(&src_path, "%s/src/.git", argv[0]);
780 strbuf_addf(&workdir_path, "%s/.git", argv[0]);
781 if (!is_directory(src_path.buf) && !is_directory(workdir_path.buf)) {
782 /* remove possible matching registrations */
783 int res = -1;
785 strbuf_strip_suffix(&src_path, "/.git");
786 res = remove_deleted_enlistment(&src_path) && res;
788 strbuf_strip_suffix(&workdir_path, "/.git");
789 res = remove_deleted_enlistment(&workdir_path) && res;
791 strbuf_release(&src_path);
792 strbuf_release(&workdir_path);
793 return res;
795 strbuf_release(&src_path);
796 strbuf_release(&workdir_path);
799 setup_enlistment_directory(argc, argv, usage, options, NULL);
801 return unregister_dir();
804 static int cmd_delete(int argc, const char **argv)
806 char *cwd = xgetcwd();
807 struct option options[] = {
808 OPT_END(),
810 const char * const usage[] = {
811 N_("scalar delete <enlistment>"),
812 NULL
814 struct strbuf enlistment = STRBUF_INIT;
815 int res = 0;
817 argc = parse_options(argc, argv, NULL, options,
818 usage, 0);
820 if (argc != 1)
821 usage_with_options(usage, options);
823 setup_enlistment_directory(argc, argv, usage, options, &enlistment);
825 if (dir_inside_of(cwd, enlistment.buf) >= 0)
826 res = error(_("refusing to delete current working directory"));
827 else {
828 close_object_store(the_repository->objects);
829 res = delete_enlistment(&enlistment);
831 strbuf_release(&enlistment);
832 free(cwd);
834 return res;
837 static int cmd_help(int argc, const char **argv)
839 struct option options[] = {
840 OPT_END(),
842 const char * const usage[] = {
843 "scalar help",
844 NULL
847 argc = parse_options(argc, argv, NULL, options,
848 usage, 0);
850 if (argc != 0)
851 usage_with_options(usage, options);
853 return run_git("help", "scalar", NULL);
856 static int cmd_version(int argc, const char **argv)
858 int verbose = 0, build_options = 0;
859 struct option options[] = {
860 OPT__VERBOSE(&verbose, N_("include Git version")),
861 OPT_BOOL(0, "build-options", &build_options,
862 N_("include Git's build options")),
863 OPT_END(),
865 const char * const usage[] = {
866 N_("scalar verbose [-v | --verbose] [--build-options]"),
867 NULL
869 struct strbuf buf = STRBUF_INIT;
871 argc = parse_options(argc, argv, NULL, options,
872 usage, 0);
874 if (argc != 0)
875 usage_with_options(usage, options);
877 get_version_info(&buf, build_options);
878 fprintf(stderr, "%s\n", buf.buf);
879 strbuf_release(&buf);
881 return 0;
884 static struct {
885 const char *name;
886 int (*fn)(int, const char **);
887 } builtins[] = {
888 { "clone", cmd_clone },
889 { "list", cmd_list },
890 { "register", cmd_register },
891 { "unregister", cmd_unregister },
892 { "run", cmd_run },
893 { "reconfigure", cmd_reconfigure },
894 { "delete", cmd_delete },
895 { "help", cmd_help },
896 { "version", cmd_version },
897 { "diagnose", cmd_diagnose },
898 { NULL, NULL},
901 int cmd_main(int argc, const char **argv)
903 struct strbuf scalar_usage = STRBUF_INIT;
904 int i;
906 while (argc > 1 && *argv[1] == '-') {
907 if (!strcmp(argv[1], "-C")) {
908 if (argc < 3)
909 die(_("-C requires a <directory>"));
910 if (chdir(argv[2]) < 0)
911 die_errno(_("could not change to '%s'"),
912 argv[2]);
913 argc -= 2;
914 argv += 2;
915 } else if (!strcmp(argv[1], "-c")) {
916 if (argc < 3)
917 die(_("-c requires a <key>=<value> argument"));
918 git_config_push_parameter(argv[2]);
919 argc -= 2;
920 argv += 2;
921 } else
922 break;
925 if (argc > 1) {
926 argv++;
927 argc--;
929 for (i = 0; builtins[i].name; i++)
930 if (!strcmp(builtins[i].name, argv[0]))
931 return !!builtins[i].fn(argc, argv);
934 strbuf_addstr(&scalar_usage,
935 N_("scalar [-C <directory>] [-c <key>=<value>] "
936 "<command> [<options>]\n\nCommands:\n"));
937 for (i = 0; builtins[i].name; i++)
938 strbuf_addf(&scalar_usage, "\t%s\n", builtins[i].name);
940 usage(scalar_usage.buf);