]> git.ipfire.org Git - thirdparty/git.git/blob - contrib/scalar/scalar.c
Merge branch 'ma/header-dup-cleanup'
[thirdparty/git.git] / contrib / scalar / scalar.c
1 /*
2 * The Scalar command-line interface.
3 */
4
5 #include "cache.h"
6 #include "gettext.h"
7 #include "parse-options.h"
8 #include "config.h"
9 #include "run-command.h"
10 #include "refs.h"
11 #include "dir.h"
12 #include "packfile.h"
13 #include "help.h"
14
15 /*
16 * Remove the deepest subdirectory in the provided path string. Path must not
17 * include a trailing path separator. Returns 1 if parent directory found,
18 * otherwise 0.
19 */
20 static int strbuf_parent_directory(struct strbuf *buf)
21 {
22 size_t len = buf->len;
23 size_t offset = offset_1st_component(buf->buf);
24 char *path_sep = find_last_dir_sep(buf->buf + offset);
25 strbuf_setlen(buf, path_sep ? path_sep - buf->buf : offset);
26
27 return buf->len < len;
28 }
29
30 static void setup_enlistment_directory(int argc, const char **argv,
31 const char * const *usagestr,
32 const struct option *options,
33 struct strbuf *enlistment_root)
34 {
35 struct strbuf path = STRBUF_INIT;
36 char *root;
37 int enlistment_found = 0;
38
39 if (startup_info->have_repository)
40 BUG("gitdir already set up?!?");
41
42 if (argc > 1)
43 usage_with_options(usagestr, options);
44
45 /* find the worktree, determine its corresponding root */
46 if (argc == 1)
47 strbuf_add_absolute_path(&path, argv[0]);
48 else if (strbuf_getcwd(&path) < 0)
49 die(_("need a working directory"));
50
51 strbuf_trim_trailing_dir_sep(&path);
52 do {
53 const size_t len = path.len;
54
55 /* check if currently in enlistment root with src/ workdir */
56 strbuf_addstr(&path, "/src");
57 if (is_nonbare_repository_dir(&path)) {
58 if (enlistment_root)
59 strbuf_add(enlistment_root, path.buf, len);
60
61 enlistment_found = 1;
62 break;
63 }
64
65 /* reset to original path */
66 strbuf_setlen(&path, len);
67
68 /* check if currently in workdir */
69 if (is_nonbare_repository_dir(&path)) {
70 if (enlistment_root) {
71 /*
72 * If the worktree's directory's name is `src`, the enlistment is the
73 * parent directory, otherwise it is identical to the worktree.
74 */
75 root = strip_path_suffix(path.buf, "src");
76 strbuf_addstr(enlistment_root, root ? root : path.buf);
77 free(root);
78 }
79
80 enlistment_found = 1;
81 break;
82 }
83 } while (strbuf_parent_directory(&path));
84
85 if (!enlistment_found)
86 die(_("could not find enlistment root"));
87
88 if (chdir(path.buf) < 0)
89 die_errno(_("could not switch to '%s'"), path.buf);
90
91 strbuf_release(&path);
92 setup_git_directory();
93 }
94
95 static int run_git(const char *arg, ...)
96 {
97 struct strvec argv = STRVEC_INIT;
98 va_list args;
99 const char *p;
100 int res;
101
102 va_start(args, arg);
103 strvec_push(&argv, arg);
104 while ((p = va_arg(args, const char *)))
105 strvec_push(&argv, p);
106 va_end(args);
107
108 res = run_command_v_opt(argv.v, RUN_GIT_CMD);
109
110 strvec_clear(&argv);
111 return res;
112 }
113
114 static int set_recommended_config(int reconfigure)
115 {
116 struct {
117 const char *key;
118 const char *value;
119 int overwrite_on_reconfigure;
120 } 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
129 /*
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.
137 *
138 * Therefore, with a sad heart, we disable this very useful
139 * feature on Windows.
140 */
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.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 { "reset.quiet", "true", 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 },
170 };
171 int i;
172 char *value;
173
174 for (i = 0; config[i].key; i++) {
175 if ((reconfigure && config[i].overwrite_on_reconfigure) ||
176 git_config_get_string(config[i].key, &value)) {
177 trace2_data_string("scalar", the_repository, config[i].key, "created");
178 if (git_config_set_gently(config[i].key,
179 config[i].value) < 0)
180 return error(_("could not configure %s=%s"),
181 config[i].key, config[i].value);
182 } else {
183 trace2_data_string("scalar", the_repository, config[i].key, "exists");
184 free(value);
185 }
186 }
187
188 /*
189 * The `log.excludeDecoration` setting is special because it allows
190 * for multiple values.
191 */
192 if (git_config_get_string("log.excludeDecoration", &value)) {
193 trace2_data_string("scalar", the_repository,
194 "log.excludeDecoration", "created");
195 if (git_config_set_multivar_gently("log.excludeDecoration",
196 "refs/prefetch/*",
197 CONFIG_REGEX_NONE, 0))
198 return error(_("could not configure "
199 "log.excludeDecoration"));
200 } else {
201 trace2_data_string("scalar", the_repository,
202 "log.excludeDecoration", "exists");
203 free(value);
204 }
205
206 return 0;
207 }
208
209 static int toggle_maintenance(int enable)
210 {
211 return run_git("maintenance", enable ? "start" : "unregister", NULL);
212 }
213
214 static int add_or_remove_enlistment(int add)
215 {
216 int res;
217
218 if (!the_repository->worktree)
219 die(_("Scalar enlistments require a worktree"));
220
221 res = run_git("config", "--global", "--get", "--fixed-value",
222 "scalar.repo", the_repository->worktree, NULL);
223
224 /*
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.
227 */
228 if ((add && !res) || (!add && res))
229 return 0;
230
231 return run_git("config", "--global", add ? "--add" : "--unset",
232 add ? "--no-fixed-value" : "--fixed-value",
233 "scalar.repo", the_repository->worktree, NULL);
234 }
235
236 static int register_dir(void)
237 {
238 int res = add_or_remove_enlistment(1);
239
240 if (!res)
241 res = set_recommended_config(0);
242
243 if (!res)
244 res = toggle_maintenance(1);
245
246 return res;
247 }
248
249 static int unregister_dir(void)
250 {
251 int res = 0;
252
253 if (toggle_maintenance(0) < 0)
254 res = -1;
255
256 if (add_or_remove_enlistment(0) < 0)
257 res = -1;
258
259 return res;
260 }
261
262 /* printf-style interface, expects `<key>=<value>` argument */
263 static int set_config(const char *fmt, ...)
264 {
265 struct strbuf buf = STRBUF_INIT;
266 char *value;
267 int res;
268 va_list args;
269
270 va_start(args, fmt);
271 strbuf_vaddf(&buf, fmt, args);
272 va_end(args);
273
274 value = strchr(buf.buf, '=');
275 if (value)
276 *(value++) = '\0';
277 res = git_config_set_gently(buf.buf, value);
278 strbuf_release(&buf);
279
280 return res;
281 }
282
283 static char *remote_default_branch(const char *url)
284 {
285 struct child_process cp = CHILD_PROCESS_INIT;
286 struct strbuf out = STRBUF_INIT;
287
288 cp.git_cmd = 1;
289 strvec_pushl(&cp.args, "ls-remote", "--symref", url, "HEAD", NULL);
290 if (!pipe_command(&cp, NULL, 0, &out, 0, NULL, 0)) {
291 const char *line = out.buf;
292
293 while (*line) {
294 const char *eol = strchrnul(line, '\n'), *p;
295 size_t len = eol - line;
296 char *branch;
297
298 if (!skip_prefix(line, "ref: ", &p) ||
299 !strip_suffix_mem(line, &len, "\tHEAD")) {
300 line = eol + (*eol == '\n');
301 continue;
302 }
303
304 eol = line + len;
305 if (skip_prefix(p, "refs/heads/", &p)) {
306 branch = xstrndup(p, eol - p);
307 strbuf_release(&out);
308 return branch;
309 }
310
311 error(_("remote HEAD is not a branch: '%.*s'"),
312 (int)(eol - p), p);
313 strbuf_release(&out);
314 return NULL;
315 }
316 }
317 warning(_("failed to get default branch name from remote; "
318 "using local default"));
319 strbuf_reset(&out);
320
321 child_process_init(&cp);
322 cp.git_cmd = 1;
323 strvec_pushl(&cp.args, "symbolic-ref", "--short", "HEAD", NULL);
324 if (!pipe_command(&cp, NULL, 0, &out, 0, NULL, 0)) {
325 strbuf_trim(&out);
326 return strbuf_detach(&out, NULL);
327 }
328
329 strbuf_release(&out);
330 error(_("failed to get default branch name"));
331 return NULL;
332 }
333
334 static int delete_enlistment(struct strbuf *enlistment)
335 {
336 #ifdef WIN32
337 struct strbuf parent = STRBUF_INIT;
338 #endif
339
340 if (unregister_dir())
341 die(_("failed to unregister repository"));
342
343 #ifdef WIN32
344 /*
345 * Change the current directory to one outside of the enlistment so
346 * that we may delete everything underneath it.
347 */
348 strbuf_addbuf(&parent, enlistment);
349 strbuf_parent_directory(&parent);
350 if (chdir(parent.buf) < 0)
351 die_errno(_("could not switch to '%s'"), parent.buf);
352 strbuf_release(&parent);
353 #endif
354
355 if (remove_dir_recursively(enlistment, 0))
356 die(_("failed to delete enlistment directory"));
357
358 return 0;
359 }
360
361 /*
362 * Dummy implementation; Using `get_version_info()` would cause a link error
363 * without this.
364 */
365 void load_builtin_commands(const char *prefix, struct cmdnames *cmds)
366 {
367 die("not implemented");
368 }
369
370 static int cmd_clone(int argc, const char **argv)
371 {
372 const char *branch = NULL;
373 int full_clone = 0, single_branch = 0;
374 struct option clone_options[] = {
375 OPT_STRING('b', "branch", &branch, N_("<branch>"),
376 N_("branch to checkout after clone")),
377 OPT_BOOL(0, "full-clone", &full_clone,
378 N_("when cloning, create full working directory")),
379 OPT_BOOL(0, "single-branch", &single_branch,
380 N_("only download metadata for the branch that will "
381 "be checked out")),
382 OPT_END(),
383 };
384 const char * const clone_usage[] = {
385 N_("scalar clone [<options>] [--] <repo> [<dir>]"),
386 NULL
387 };
388 const char *url;
389 char *enlistment = NULL, *dir = NULL;
390 struct strbuf buf = STRBUF_INIT;
391 int res;
392
393 argc = parse_options(argc, argv, NULL, clone_options, clone_usage, 0);
394
395 if (argc == 2) {
396 url = argv[0];
397 enlistment = xstrdup(argv[1]);
398 } else if (argc == 1) {
399 url = argv[0];
400
401 strbuf_addstr(&buf, url);
402 /* Strip trailing slashes, if any */
403 while (buf.len > 0 && is_dir_sep(buf.buf[buf.len - 1]))
404 strbuf_setlen(&buf, buf.len - 1);
405 /* Strip suffix `.git`, if any */
406 strbuf_strip_suffix(&buf, ".git");
407
408 enlistment = find_last_dir_sep(buf.buf);
409 if (!enlistment) {
410 die(_("cannot deduce worktree name from '%s'"), url);
411 }
412 enlistment = xstrdup(enlistment + 1);
413 } else {
414 usage_msg_opt(_("You must specify a repository to clone."),
415 clone_usage, clone_options);
416 }
417
418 if (is_directory(enlistment))
419 die(_("directory '%s' exists already"), enlistment);
420
421 dir = xstrfmt("%s/src", enlistment);
422
423 strbuf_reset(&buf);
424 if (branch)
425 strbuf_addf(&buf, "init.defaultBranch=%s", branch);
426 else {
427 char *b = repo_default_branch_name(the_repository, 1);
428 strbuf_addf(&buf, "init.defaultBranch=%s", b);
429 free(b);
430 }
431
432 if ((res = run_git("-c", buf.buf, "init", "--", dir, NULL)))
433 goto cleanup;
434
435 if (chdir(dir) < 0) {
436 res = error_errno(_("could not switch to '%s'"), dir);
437 goto cleanup;
438 }
439
440 setup_git_directory();
441
442 /* common-main already logs `argv` */
443 trace2_def_repo(the_repository);
444
445 if (!branch && !(branch = remote_default_branch(url))) {
446 res = error(_("failed to get default branch for '%s'"), url);
447 goto cleanup;
448 }
449
450 if (set_config("remote.origin.url=%s", url) ||
451 set_config("remote.origin.fetch="
452 "+refs/heads/%s:refs/remotes/origin/%s",
453 single_branch ? branch : "*",
454 single_branch ? branch : "*") ||
455 set_config("remote.origin.promisor=true") ||
456 set_config("remote.origin.partialCloneFilter=blob:none")) {
457 res = error(_("could not configure remote in '%s'"), dir);
458 goto cleanup;
459 }
460
461 if (!full_clone &&
462 (res = run_git("sparse-checkout", "init", "--cone", NULL)))
463 goto cleanup;
464
465 if (set_recommended_config(0))
466 return error(_("could not configure '%s'"), dir);
467
468 if ((res = run_git("fetch", "--quiet", "origin", NULL))) {
469 warning(_("partial clone failed; attempting full clone"));
470
471 if (set_config("remote.origin.promisor") ||
472 set_config("remote.origin.partialCloneFilter")) {
473 res = error(_("could not configure for full clone"));
474 goto cleanup;
475 }
476
477 if ((res = run_git("fetch", "--quiet", "origin", NULL)))
478 goto cleanup;
479 }
480
481 if ((res = set_config("branch.%s.remote=origin", branch)))
482 goto cleanup;
483 if ((res = set_config("branch.%s.merge=refs/heads/%s",
484 branch, branch)))
485 goto cleanup;
486
487 strbuf_reset(&buf);
488 strbuf_addf(&buf, "origin/%s", branch);
489 res = run_git("checkout", "-f", "-t", buf.buf, NULL);
490 if (res)
491 goto cleanup;
492
493 res = register_dir();
494
495 cleanup:
496 free(enlistment);
497 free(dir);
498 strbuf_release(&buf);
499 return res;
500 }
501
502 static int cmd_list(int argc, const char **argv)
503 {
504 if (argc != 1)
505 die(_("`scalar list` does not take arguments"));
506
507 if (run_git("config", "--global", "--get-all", "scalar.repo", NULL) < 0)
508 return -1;
509 return 0;
510 }
511
512 static int cmd_register(int argc, const char **argv)
513 {
514 struct option options[] = {
515 OPT_END(),
516 };
517 const char * const usage[] = {
518 N_("scalar register [<enlistment>]"),
519 NULL
520 };
521
522 argc = parse_options(argc, argv, NULL, options,
523 usage, 0);
524
525 setup_enlistment_directory(argc, argv, usage, options, NULL);
526
527 return register_dir();
528 }
529
530 static int get_scalar_repos(const char *key, const char *value, void *data)
531 {
532 struct string_list *list = data;
533
534 if (!strcmp(key, "scalar.repo"))
535 string_list_append(list, value);
536
537 return 0;
538 }
539
540 static int cmd_reconfigure(int argc, const char **argv)
541 {
542 int all = 0;
543 struct option options[] = {
544 OPT_BOOL('a', "all", &all,
545 N_("reconfigure all registered enlistments")),
546 OPT_END(),
547 };
548 const char * const usage[] = {
549 N_("scalar reconfigure [--all | <enlistment>]"),
550 NULL
551 };
552 struct string_list scalar_repos = STRING_LIST_INIT_DUP;
553 int i, res = 0;
554 struct repository r = { NULL };
555 struct strbuf commondir = STRBUF_INIT, gitdir = STRBUF_INIT;
556
557 argc = parse_options(argc, argv, NULL, options,
558 usage, 0);
559
560 if (!all) {
561 setup_enlistment_directory(argc, argv, usage, options, NULL);
562
563 return set_recommended_config(1);
564 }
565
566 if (argc > 0)
567 usage_msg_opt(_("--all or <enlistment>, but not both"),
568 usage, options);
569
570 git_config(get_scalar_repos, &scalar_repos);
571
572 for (i = 0; i < scalar_repos.nr; i++) {
573 const char *dir = scalar_repos.items[i].string;
574
575 strbuf_reset(&commondir);
576 strbuf_reset(&gitdir);
577
578 if (chdir(dir) < 0) {
579 warning_errno(_("could not switch to '%s'"), dir);
580 res = -1;
581 } else if (discover_git_directory(&commondir, &gitdir) < 0) {
582 warning_errno(_("git repository gone in '%s'"), dir);
583 res = -1;
584 } else {
585 git_config_clear();
586
587 the_repository = &r;
588 r.commondir = commondir.buf;
589 r.gitdir = gitdir.buf;
590
591 if (set_recommended_config(1) < 0)
592 res = -1;
593 }
594 }
595
596 string_list_clear(&scalar_repos, 1);
597 strbuf_release(&commondir);
598 strbuf_release(&gitdir);
599
600 return res;
601 }
602
603 static int cmd_run(int argc, const char **argv)
604 {
605 struct option options[] = {
606 OPT_END(),
607 };
608 struct {
609 const char *arg, *task;
610 } tasks[] = {
611 { "config", NULL },
612 { "commit-graph", "commit-graph" },
613 { "fetch", "prefetch" },
614 { "loose-objects", "loose-objects" },
615 { "pack-files", "incremental-repack" },
616 { NULL, NULL }
617 };
618 struct strbuf buf = STRBUF_INIT;
619 const char *usagestr[] = { NULL, NULL };
620 int i;
621
622 strbuf_addstr(&buf, N_("scalar run <task> [<enlistment>]\nTasks:\n"));
623 for (i = 0; tasks[i].arg; i++)
624 strbuf_addf(&buf, "\t%s\n", tasks[i].arg);
625 usagestr[0] = buf.buf;
626
627 argc = parse_options(argc, argv, NULL, options,
628 usagestr, 0);
629
630 if (!argc)
631 usage_with_options(usagestr, options);
632
633 if (!strcmp("all", argv[0])) {
634 i = -1;
635 } else {
636 for (i = 0; tasks[i].arg && strcmp(tasks[i].arg, argv[0]); i++)
637 ; /* keep looking for the task */
638
639 if (i > 0 && !tasks[i].arg) {
640 error(_("no such task: '%s'"), argv[0]);
641 usage_with_options(usagestr, options);
642 }
643 }
644
645 argc--;
646 argv++;
647 setup_enlistment_directory(argc, argv, usagestr, options, NULL);
648 strbuf_release(&buf);
649
650 if (i == 0)
651 return register_dir();
652
653 if (i > 0)
654 return run_git("maintenance", "run",
655 "--task", tasks[i].task, NULL);
656
657 if (register_dir())
658 return -1;
659 for (i = 1; tasks[i].arg; i++)
660 if (run_git("maintenance", "run",
661 "--task", tasks[i].task, NULL))
662 return -1;
663 return 0;
664 }
665
666 static int remove_deleted_enlistment(struct strbuf *path)
667 {
668 int res = 0;
669 strbuf_realpath_forgiving(path, path->buf, 1);
670
671 if (run_git("config", "--global",
672 "--unset", "--fixed-value",
673 "scalar.repo", path->buf, NULL) < 0)
674 res = -1;
675
676 if (run_git("config", "--global",
677 "--unset", "--fixed-value",
678 "maintenance.repo", path->buf, NULL) < 0)
679 res = -1;
680
681 return res;
682 }
683
684 static int cmd_unregister(int argc, const char **argv)
685 {
686 struct option options[] = {
687 OPT_END(),
688 };
689 const char * const usage[] = {
690 N_("scalar unregister [<enlistment>]"),
691 NULL
692 };
693
694 argc = parse_options(argc, argv, NULL, options,
695 usage, 0);
696
697 /*
698 * Be forgiving when the enlistment or worktree does not even exist any
699 * longer; This can be the case if a user deleted the worktree by
700 * mistake and _still_ wants to unregister the thing.
701 */
702 if (argc == 1) {
703 struct strbuf src_path = STRBUF_INIT, workdir_path = STRBUF_INIT;
704
705 strbuf_addf(&src_path, "%s/src/.git", argv[0]);
706 strbuf_addf(&workdir_path, "%s/.git", argv[0]);
707 if (!is_directory(src_path.buf) && !is_directory(workdir_path.buf)) {
708 /* remove possible matching registrations */
709 int res = -1;
710
711 strbuf_strip_suffix(&src_path, "/.git");
712 res = remove_deleted_enlistment(&src_path) && res;
713
714 strbuf_strip_suffix(&workdir_path, "/.git");
715 res = remove_deleted_enlistment(&workdir_path) && res;
716
717 strbuf_release(&src_path);
718 strbuf_release(&workdir_path);
719 return res;
720 }
721 strbuf_release(&src_path);
722 strbuf_release(&workdir_path);
723 }
724
725 setup_enlistment_directory(argc, argv, usage, options, NULL);
726
727 return unregister_dir();
728 }
729
730 static int cmd_delete(int argc, const char **argv)
731 {
732 char *cwd = xgetcwd();
733 struct option options[] = {
734 OPT_END(),
735 };
736 const char * const usage[] = {
737 N_("scalar delete <enlistment>"),
738 NULL
739 };
740 struct strbuf enlistment = STRBUF_INIT;
741 int res = 0;
742
743 argc = parse_options(argc, argv, NULL, options,
744 usage, 0);
745
746 if (argc != 1)
747 usage_with_options(usage, options);
748
749 setup_enlistment_directory(argc, argv, usage, options, &enlistment);
750
751 if (dir_inside_of(cwd, enlistment.buf) >= 0)
752 res = error(_("refusing to delete current working directory"));
753 else {
754 close_object_store(the_repository->objects);
755 res = delete_enlistment(&enlistment);
756 }
757 strbuf_release(&enlistment);
758 free(cwd);
759
760 return res;
761 }
762
763 static int cmd_version(int argc, const char **argv)
764 {
765 int verbose = 0, build_options = 0;
766 struct option options[] = {
767 OPT__VERBOSE(&verbose, N_("include Git version")),
768 OPT_BOOL(0, "build-options", &build_options,
769 N_("include Git's build options")),
770 OPT_END(),
771 };
772 const char * const usage[] = {
773 N_("scalar verbose [-v | --verbose] [--build-options]"),
774 NULL
775 };
776 struct strbuf buf = STRBUF_INIT;
777
778 argc = parse_options(argc, argv, NULL, options,
779 usage, 0);
780
781 if (argc != 0)
782 usage_with_options(usage, options);
783
784 get_version_info(&buf, build_options);
785 fprintf(stderr, "%s\n", buf.buf);
786 strbuf_release(&buf);
787
788 return 0;
789 }
790
791 static struct {
792 const char *name;
793 int (*fn)(int, const char **);
794 } builtins[] = {
795 { "clone", cmd_clone },
796 { "list", cmd_list },
797 { "register", cmd_register },
798 { "unregister", cmd_unregister },
799 { "run", cmd_run },
800 { "reconfigure", cmd_reconfigure },
801 { "delete", cmd_delete },
802 { "version", cmd_version },
803 { NULL, NULL},
804 };
805
806 int cmd_main(int argc, const char **argv)
807 {
808 struct strbuf scalar_usage = STRBUF_INIT;
809 int i;
810
811 if (argc > 1) {
812 argv++;
813 argc--;
814
815 for (i = 0; builtins[i].name; i++)
816 if (!strcmp(builtins[i].name, argv[0]))
817 return !!builtins[i].fn(argc, argv);
818 }
819
820 strbuf_addstr(&scalar_usage,
821 N_("scalar <command> [<options>]\n\nCommands:\n"));
822 for (i = 0; builtins[i].name; i++)
823 strbuf_addf(&scalar_usage, "\t%s\n", builtins[i].name);
824
825 usage(scalar_usage.buf);
826 }