builtin/stash: fix leak in `show_stash()`
[git/gitster.git] / scalar.c
blob7234049a1b87c046d26db57082e368b9ba68812f
1 /*
2 * The Scalar command-line interface.
3 */
5 #include "git-compat-util.h"
6 #include "abspath.h"
7 #include "gettext.h"
8 #include "parse-options.h"
9 #include "config.h"
10 #include "run-command.h"
11 #include "simple-ipc.h"
12 #include "fsmonitor-ipc.h"
13 #include "fsmonitor-settings.h"
14 #include "refs.h"
15 #include "dir.h"
16 #include "packfile.h"
17 #include "help.h"
18 #include "setup.h"
19 #include "trace2.h"
21 static void setup_enlistment_directory(int argc, const char **argv,
22 const char * const *usagestr,
23 const struct option *options,
24 struct strbuf *enlistment_root)
26 struct strbuf path = STRBUF_INIT;
27 int enlistment_is_repo_parent = 0;
28 size_t len;
30 if (startup_info->have_repository)
31 BUG("gitdir already set up?!?");
33 if (argc > 1)
34 usage_with_options(usagestr, options);
36 /* find the worktree, determine its corresponding root */
37 if (argc == 1) {
38 strbuf_add_absolute_path(&path, argv[0]);
39 if (!is_directory(path.buf))
40 die(_("'%s' does not exist"), path.buf);
41 if (chdir(path.buf) < 0)
42 die_errno(_("could not switch to '%s'"), path.buf);
43 } else if (strbuf_getcwd(&path) < 0)
44 die(_("need a working directory"));
46 strbuf_trim_trailing_dir_sep(&path);
48 /* check if currently in enlistment root with src/ workdir */
49 len = path.len;
50 strbuf_addstr(&path, "/src");
51 if (is_nonbare_repository_dir(&path)) {
52 enlistment_is_repo_parent = 1;
53 if (chdir(path.buf) < 0)
54 die_errno(_("could not switch to '%s'"), path.buf);
56 strbuf_setlen(&path, len);
58 setup_git_directory();
60 if (!the_repository->worktree)
61 die(_("Scalar enlistments require a worktree"));
63 if (enlistment_root) {
64 if (enlistment_is_repo_parent)
65 strbuf_addbuf(enlistment_root, &path);
66 else
67 strbuf_addstr(enlistment_root, the_repository->worktree);
70 strbuf_release(&path);
73 static int run_git(const char *arg, ...)
75 struct child_process cmd = CHILD_PROCESS_INIT;
76 va_list args;
77 const char *p;
79 va_start(args, arg);
80 strvec_push(&cmd.args, arg);
81 while ((p = va_arg(args, const char *)))
82 strvec_push(&cmd.args, p);
83 va_end(args);
85 cmd.git_cmd = 1;
86 return run_command(&cmd);
89 struct scalar_config {
90 const char *key;
91 const char *value;
92 int overwrite_on_reconfigure;
95 static int set_scalar_config(const struct scalar_config *config, int reconfigure)
97 char *value = NULL;
98 int res;
100 if ((reconfigure && config->overwrite_on_reconfigure) ||
101 git_config_get_string(config->key, &value)) {
102 trace2_data_string("scalar", the_repository, config->key, "created");
103 res = git_config_set_gently(config->key, config->value);
104 } else {
105 trace2_data_string("scalar", the_repository, config->key, "exists");
106 res = 0;
109 free(value);
110 return res;
113 static int have_fsmonitor_support(void)
115 return fsmonitor_ipc__is_supported() &&
116 fsm_settings__get_reason(the_repository) == FSMONITOR_REASON_OK;
119 static int set_recommended_config(int reconfigure)
121 struct scalar_config config[] = {
122 /* Required */
123 { "am.keepCR", "true", 1 },
124 { "core.FSCache", "true", 1 },
125 { "core.multiPackIndex", "true", 1 },
126 { "core.preloadIndex", "true", 1 },
127 #ifndef WIN32
128 { "core.untrackedCache", "true", 1 },
129 #else
131 * Unfortunately, Scalar's Functional Tests demonstrated
132 * that the untracked cache feature is unreliable on Windows
133 * (which is a bummer because that platform would benefit the
134 * most from it). For some reason, freshly created files seem
135 * not to update the directory's `lastModified` time
136 * immediately, but the untracked cache would need to rely on
137 * that.
139 * Therefore, with a sad heart, we disable this very useful
140 * feature on Windows.
142 { "core.untrackedCache", "false", 1 },
143 #endif
144 { "core.logAllRefUpdates", "true", 1 },
145 { "credential.https://dev.azure.com.useHttpPath", "true", 1 },
146 { "credential.validate", "false", 1 }, /* GCM4W-only */
147 { "gc.auto", "0", 1 },
148 { "gui.GCWarning", "false", 1 },
149 { "index.skipHash", "false", 1 },
150 { "index.threads", "true", 1 },
151 { "index.version", "4", 1 },
152 { "merge.stat", "false", 1 },
153 { "merge.renames", "true", 1 },
154 { "pack.useBitmaps", "false", 1 },
155 { "pack.useSparse", "true", 1 },
156 { "receive.autoGC", "false", 1 },
157 { "feature.manyFiles", "false", 1 },
158 { "feature.experimental", "false", 1 },
159 { "fetch.unpackLimit", "1", 1 },
160 { "fetch.writeCommitGraph", "false", 1 },
161 #ifdef WIN32
162 { "http.sslBackend", "schannel", 1 },
163 #endif
164 /* Optional */
165 { "status.aheadBehind", "false" },
166 { "commitGraph.generationVersion", "1" },
167 { "core.autoCRLF", "false" },
168 { "core.safeCRLF", "false" },
169 { "fetch.showForcedUpdates", "false" },
170 { NULL, NULL },
172 int i;
173 char *value;
175 for (i = 0; config[i].key; i++) {
176 if (set_scalar_config(config + i, reconfigure))
177 return error(_("could not configure %s=%s"),
178 config[i].key, config[i].value);
181 if (have_fsmonitor_support()) {
182 struct scalar_config fsmonitor = { "core.fsmonitor", "true" };
183 if (set_scalar_config(&fsmonitor, reconfigure))
184 return error(_("could not configure %s=%s"),
185 fsmonitor.key, fsmonitor.value);
189 * The `log.excludeDecoration` setting is special because it allows
190 * for multiple values.
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);
206 return 0;
209 static int toggle_maintenance(int enable)
211 return run_git("maintenance",
212 enable ? "start" : "unregister",
213 enable ? NULL : "--force",
214 NULL);
217 static int add_or_remove_enlistment(int add)
219 int res;
221 if (!the_repository->worktree)
222 die(_("Scalar enlistments require a worktree"));
224 res = run_git("config", "--global", "--get", "--fixed-value",
225 "scalar.repo", the_repository->worktree, NULL);
228 * If we want to add and the setting is already there, then do nothing.
229 * If we want to remove and the setting is not there, then do nothing.
231 if ((add && !res) || (!add && res))
232 return 0;
234 return run_git("config", "--global", add ? "--add" : "--unset",
235 add ? "--no-fixed-value" : "--fixed-value",
236 "scalar.repo", the_repository->worktree, NULL);
239 static int start_fsmonitor_daemon(void)
241 assert(have_fsmonitor_support());
243 if (fsmonitor_ipc__get_state() != IPC_STATE__LISTENING)
244 return run_git("fsmonitor--daemon", "start", NULL);
246 return 0;
249 static int stop_fsmonitor_daemon(void)
251 assert(have_fsmonitor_support());
253 if (fsmonitor_ipc__get_state() == IPC_STATE__LISTENING)
254 return run_git("fsmonitor--daemon", "stop", NULL);
256 return 0;
259 static int register_dir(void)
261 if (add_or_remove_enlistment(1))
262 return error(_("could not add enlistment"));
264 if (set_recommended_config(0))
265 return error(_("could not set recommended config"));
267 if (toggle_maintenance(1))
268 warning(_("could not turn on maintenance"));
270 if (have_fsmonitor_support() && start_fsmonitor_daemon()) {
271 return error(_("could not start the FSMonitor daemon"));
274 return 0;
277 static int unregister_dir(void)
279 int res = 0;
281 if (toggle_maintenance(0))
282 res = error(_("could not turn off maintenance"));
284 if (add_or_remove_enlistment(0))
285 res = error(_("could not remove enlistment"));
287 return res;
290 /* printf-style interface, expects `<key>=<value>` argument */
291 static int set_config(const char *fmt, ...)
293 struct strbuf buf = STRBUF_INIT;
294 char *value;
295 int res;
296 va_list args;
298 va_start(args, fmt);
299 strbuf_vaddf(&buf, fmt, args);
300 va_end(args);
302 value = strchr(buf.buf, '=');
303 if (value)
304 *(value++) = '\0';
305 res = git_config_set_gently(buf.buf, value);
306 strbuf_release(&buf);
308 return res;
311 static char *remote_default_branch(const char *url)
313 struct child_process cp = CHILD_PROCESS_INIT;
314 struct strbuf out = STRBUF_INIT;
316 cp.git_cmd = 1;
317 strvec_pushl(&cp.args, "ls-remote", "--symref", url, "HEAD", NULL);
318 if (!pipe_command(&cp, NULL, 0, &out, 0, NULL, 0)) {
319 const char *line = out.buf;
321 while (*line) {
322 const char *eol = strchrnul(line, '\n'), *p;
323 size_t len = eol - line;
324 char *branch;
326 if (!skip_prefix(line, "ref: ", &p) ||
327 !strip_suffix_mem(line, &len, "\tHEAD")) {
328 line = eol + (*eol == '\n');
329 continue;
332 eol = line + len;
333 if (skip_prefix(p, "refs/heads/", &p)) {
334 branch = xstrndup(p, eol - p);
335 strbuf_release(&out);
336 return branch;
339 error(_("remote HEAD is not a branch: '%.*s'"),
340 (int)(eol - p), p);
341 strbuf_release(&out);
342 return NULL;
345 warning(_("failed to get default branch name from remote; "
346 "using local default"));
347 strbuf_reset(&out);
349 child_process_init(&cp);
350 cp.git_cmd = 1;
351 strvec_pushl(&cp.args, "symbolic-ref", "--short", "HEAD", NULL);
352 if (!pipe_command(&cp, NULL, 0, &out, 0, NULL, 0)) {
353 strbuf_trim(&out);
354 return strbuf_detach(&out, NULL);
357 strbuf_release(&out);
358 error(_("failed to get default branch name"));
359 return NULL;
362 static int delete_enlistment(struct strbuf *enlistment)
364 #ifdef WIN32
365 struct strbuf parent = STRBUF_INIT;
366 size_t offset;
367 char *path_sep;
368 #endif
370 if (unregister_dir())
371 return error(_("failed to unregister repository"));
373 #ifdef WIN32
375 * Change the current directory to one outside of the enlistment so
376 * that we may delete everything underneath it.
378 offset = offset_1st_component(enlistment->buf);
379 path_sep = find_last_dir_sep(enlistment->buf + offset);
380 strbuf_add(&parent, enlistment->buf,
381 path_sep ? path_sep - enlistment->buf : offset);
382 if (chdir(parent.buf) < 0) {
383 int res = error_errno(_("could not switch to '%s'"), parent.buf);
384 strbuf_release(&parent);
385 return res;
387 strbuf_release(&parent);
388 #endif
390 if (have_fsmonitor_support() && stop_fsmonitor_daemon())
391 return error(_("failed to stop the FSMonitor daemon"));
393 if (remove_dir_recursively(enlistment, 0))
394 return error(_("failed to delete enlistment directory"));
396 return 0;
400 * Dummy implementation; Using `get_version_info()` would cause a link error
401 * without this.
403 void load_builtin_commands(const char *prefix, struct cmdnames *cmds)
405 die("not implemented");
408 static int cmd_clone(int argc, const char **argv)
410 const char *branch = NULL;
411 int full_clone = 0, single_branch = 0, show_progress = isatty(2);
412 int src = 1;
413 struct option clone_options[] = {
414 OPT_STRING('b', "branch", &branch, N_("<branch>"),
415 N_("branch to checkout after clone")),
416 OPT_BOOL(0, "full-clone", &full_clone,
417 N_("when cloning, create full working directory")),
418 OPT_BOOL(0, "single-branch", &single_branch,
419 N_("only download metadata for the branch that will "
420 "be checked out")),
421 OPT_BOOL(0, "src", &src,
422 N_("create repository within 'src' directory")),
423 OPT_END(),
425 const char * const clone_usage[] = {
426 N_("scalar clone [--single-branch] [--branch <main-branch>] [--full-clone]\n"
427 "\t[--[no-]src] <url> [<enlistment>]"),
428 NULL
430 const char *url;
431 char *enlistment = NULL, *dir = NULL;
432 struct strbuf buf = STRBUF_INIT;
433 int res;
435 argc = parse_options(argc, argv, NULL, clone_options, clone_usage, 0);
437 if (argc == 2) {
438 url = argv[0];
439 enlistment = xstrdup(argv[1]);
440 } else if (argc == 1) {
441 url = argv[0];
443 strbuf_addstr(&buf, url);
444 /* Strip trailing slashes, if any */
445 while (buf.len > 0 && is_dir_sep(buf.buf[buf.len - 1]))
446 strbuf_setlen(&buf, buf.len - 1);
447 /* Strip suffix `.git`, if any */
448 strbuf_strip_suffix(&buf, ".git");
450 enlistment = find_last_dir_sep(buf.buf);
451 if (!enlistment) {
452 die(_("cannot deduce worktree name from '%s'"), url);
454 enlistment = xstrdup(enlistment + 1);
455 } else {
456 usage_msg_opt(_("You must specify a repository to clone."),
457 clone_usage, clone_options);
460 if (is_directory(enlistment))
461 die(_("directory '%s' exists already"), enlistment);
463 if (src)
464 dir = xstrfmt("%s/src", enlistment);
465 else
466 dir = xstrdup(enlistment);
468 strbuf_reset(&buf);
469 if (branch)
470 strbuf_addf(&buf, "init.defaultBranch=%s", branch);
471 else {
472 char *b = repo_default_branch_name(the_repository, 1);
473 strbuf_addf(&buf, "init.defaultBranch=%s", b);
474 free(b);
477 if ((res = run_git("-c", buf.buf, "init", "--", dir, NULL)))
478 goto cleanup;
480 if (chdir(dir) < 0) {
481 res = error_errno(_("could not switch to '%s'"), dir);
482 goto cleanup;
485 setup_git_directory();
487 /* common-main already logs `argv` */
488 trace2_def_repo(the_repository);
490 if (!branch && !(branch = remote_default_branch(url))) {
491 res = error(_("failed to get default branch for '%s'"), url);
492 goto cleanup;
495 if (set_config("remote.origin.url=%s", url) ||
496 set_config("remote.origin.fetch="
497 "+refs/heads/%s:refs/remotes/origin/%s",
498 single_branch ? branch : "*",
499 single_branch ? branch : "*") ||
500 set_config("remote.origin.promisor=true") ||
501 set_config("remote.origin.partialCloneFilter=blob:none")) {
502 res = error(_("could not configure remote in '%s'"), dir);
503 goto cleanup;
506 if (!full_clone &&
507 (res = run_git("sparse-checkout", "init", "--cone", NULL)))
508 goto cleanup;
510 if (set_recommended_config(0))
511 return error(_("could not configure '%s'"), dir);
513 if ((res = run_git("fetch", "--quiet",
514 show_progress ? "--progress" : "--no-progress",
515 "origin", NULL))) {
516 warning(_("partial clone failed; attempting full clone"));
518 if (set_config("remote.origin.promisor") ||
519 set_config("remote.origin.partialCloneFilter")) {
520 res = error(_("could not configure for full clone"));
521 goto cleanup;
524 if ((res = run_git("fetch", "--quiet",
525 show_progress ? "--progress" : "--no-progress",
526 "origin", NULL)))
527 goto cleanup;
530 if ((res = set_config("branch.%s.remote=origin", branch)))
531 goto cleanup;
532 if ((res = set_config("branch.%s.merge=refs/heads/%s",
533 branch, branch)))
534 goto cleanup;
536 strbuf_reset(&buf);
537 strbuf_addf(&buf, "origin/%s", branch);
538 res = run_git("checkout", "-f", "-t", buf.buf, NULL);
539 if (res)
540 goto cleanup;
542 res = register_dir();
544 cleanup:
545 free(enlistment);
546 free(dir);
547 strbuf_release(&buf);
548 return res;
551 static int cmd_diagnose(int argc, const char **argv)
553 struct option options[] = {
554 OPT_END(),
556 const char * const usage[] = {
557 N_("scalar diagnose [<enlistment>]"),
558 NULL
560 struct strbuf diagnostics_root = STRBUF_INIT;
561 int res = 0;
563 argc = parse_options(argc, argv, NULL, options,
564 usage, 0);
566 setup_enlistment_directory(argc, argv, usage, options, &diagnostics_root);
567 strbuf_addstr(&diagnostics_root, "/.scalarDiagnostics");
569 res = run_git("diagnose", "--mode=all", "-s", "%Y%m%d_%H%M%S",
570 "-o", diagnostics_root.buf, NULL);
572 strbuf_release(&diagnostics_root);
573 return res;
576 static int cmd_list(int argc, const char **argv UNUSED)
578 if (argc != 1)
579 die(_("`scalar list` does not take arguments"));
581 if (run_git("config", "--global", "--get-all", "scalar.repo", NULL) < 0)
582 return -1;
583 return 0;
586 static int cmd_register(int argc, const char **argv)
588 struct option options[] = {
589 OPT_END(),
591 const char * const usage[] = {
592 N_("scalar register [<enlistment>]"),
593 NULL
596 argc = parse_options(argc, argv, NULL, options,
597 usage, 0);
599 setup_enlistment_directory(argc, argv, usage, options, NULL);
601 return register_dir();
604 static int get_scalar_repos(const char *key, const char *value,
605 const struct config_context *ctx UNUSED,
606 void *data)
608 struct string_list *list = data;
610 if (!strcmp(key, "scalar.repo"))
611 string_list_append(list, value);
613 return 0;
616 static int remove_deleted_enlistment(struct strbuf *path)
618 int res = 0;
619 strbuf_realpath_forgiving(path, path->buf, 1);
621 if (run_git("config", "--global",
622 "--unset", "--fixed-value",
623 "scalar.repo", path->buf, NULL) < 0)
624 res = -1;
626 if (run_git("config", "--global",
627 "--unset", "--fixed-value",
628 "maintenance.repo", path->buf, NULL) < 0)
629 res = -1;
631 return res;
634 static int cmd_reconfigure(int argc, const char **argv)
636 int all = 0;
637 struct option options[] = {
638 OPT_BOOL('a', "all", &all,
639 N_("reconfigure all registered enlistments")),
640 OPT_END(),
642 const char * const usage[] = {
643 N_("scalar reconfigure [--all | <enlistment>]"),
644 NULL
646 struct string_list scalar_repos = STRING_LIST_INIT_DUP;
647 int i, res = 0;
648 struct strbuf commondir = STRBUF_INIT, gitdir = STRBUF_INIT;
650 argc = parse_options(argc, argv, NULL, options,
651 usage, 0);
653 if (!all) {
654 setup_enlistment_directory(argc, argv, usage, options, NULL);
656 return set_recommended_config(1);
659 if (argc > 0)
660 usage_msg_opt(_("--all or <enlistment>, but not both"),
661 usage, options);
663 git_config(get_scalar_repos, &scalar_repos);
665 for (i = 0; i < scalar_repos.nr; i++) {
666 int succeeded = 0;
667 struct repository *old_repo, r = { NULL };
668 const char *dir = scalar_repos.items[i].string;
670 strbuf_reset(&commondir);
671 strbuf_reset(&gitdir);
673 if (chdir(dir) < 0) {
674 struct strbuf buf = STRBUF_INIT;
676 if (errno != ENOENT) {
677 warning_errno(_("could not switch to '%s'"), dir);
678 goto loop_end;
681 strbuf_addstr(&buf, dir);
682 if (remove_deleted_enlistment(&buf))
683 error(_("could not remove stale "
684 "scalar.repo '%s'"), dir);
685 else {
686 warning(_("removed stale scalar.repo '%s'"),
687 dir);
688 succeeded = 1;
690 strbuf_release(&buf);
691 goto loop_end;
694 switch (discover_git_directory_reason(&commondir, &gitdir)) {
695 case GIT_DIR_INVALID_OWNERSHIP:
696 warning(_("repository at '%s' has different owner"), dir);
697 goto loop_end;
699 case GIT_DIR_INVALID_GITFILE:
700 case GIT_DIR_INVALID_FORMAT:
701 warning(_("repository at '%s' has a format issue"), dir);
702 goto loop_end;
704 case GIT_DIR_DISCOVERED:
705 succeeded = 1;
706 break;
708 default:
709 warning(_("repository not found in '%s'"), dir);
710 break;
713 git_config_clear();
715 if (repo_init(&r, gitdir.buf, commondir.buf))
716 goto loop_end;
718 old_repo = the_repository;
719 the_repository = &r;
721 if (set_recommended_config(1) >= 0)
722 succeeded = 1;
724 the_repository = old_repo;
726 loop_end:
727 if (!succeeded) {
728 res = -1;
729 warning(_("to unregister this repository from Scalar, run\n"
730 "\tgit config --global --unset --fixed-value scalar.repo \"%s\""),
731 dir);
735 string_list_clear(&scalar_repos, 1);
736 strbuf_release(&commondir);
737 strbuf_release(&gitdir);
739 return res;
742 static int cmd_run(int argc, const char **argv)
744 struct option options[] = {
745 OPT_END(),
747 struct {
748 const char *arg, *task;
749 } tasks[] = {
750 { "config", NULL },
751 { "commit-graph", "commit-graph" },
752 { "fetch", "prefetch" },
753 { "loose-objects", "loose-objects" },
754 { "pack-files", "incremental-repack" },
755 { NULL, NULL }
757 struct strbuf buf = STRBUF_INIT;
758 const char *usagestr[] = { NULL, NULL };
759 int i;
761 strbuf_addstr(&buf, N_("scalar run <task> [<enlistment>]\nTasks:\n"));
762 for (i = 0; tasks[i].arg; i++)
763 strbuf_addf(&buf, "\t%s\n", tasks[i].arg);
764 usagestr[0] = buf.buf;
766 argc = parse_options(argc, argv, NULL, options,
767 usagestr, 0);
769 if (!argc)
770 usage_with_options(usagestr, options);
772 if (!strcmp("all", argv[0])) {
773 i = -1;
774 } else {
775 for (i = 0; tasks[i].arg && strcmp(tasks[i].arg, argv[0]); i++)
776 ; /* keep looking for the task */
778 if (i > 0 && !tasks[i].arg) {
779 error(_("no such task: '%s'"), argv[0]);
780 usage_with_options(usagestr, options);
784 argc--;
785 argv++;
786 setup_enlistment_directory(argc, argv, usagestr, options, NULL);
787 strbuf_release(&buf);
789 if (i == 0)
790 return register_dir();
792 if (i > 0)
793 return run_git("maintenance", "run",
794 "--task", tasks[i].task, NULL);
796 if (register_dir())
797 return -1;
798 for (i = 1; tasks[i].arg; i++)
799 if (run_git("maintenance", "run",
800 "--task", tasks[i].task, NULL))
801 return -1;
802 return 0;
805 static int cmd_unregister(int argc, const char **argv)
807 struct option options[] = {
808 OPT_END(),
810 const char * const usage[] = {
811 N_("scalar unregister [<enlistment>]"),
812 NULL
815 argc = parse_options(argc, argv, NULL, options,
816 usage, 0);
819 * Be forgiving when the enlistment or worktree does not even exist any
820 * longer; This can be the case if a user deleted the worktree by
821 * mistake and _still_ wants to unregister the thing.
823 if (argc == 1) {
824 struct strbuf src_path = STRBUF_INIT, workdir_path = STRBUF_INIT;
826 strbuf_addf(&src_path, "%s/src/.git", argv[0]);
827 strbuf_addf(&workdir_path, "%s/.git", argv[0]);
828 if (!is_directory(src_path.buf) && !is_directory(workdir_path.buf)) {
829 /* remove possible matching registrations */
830 int res = -1;
832 strbuf_strip_suffix(&src_path, "/.git");
833 res = remove_deleted_enlistment(&src_path) && res;
835 strbuf_strip_suffix(&workdir_path, "/.git");
836 res = remove_deleted_enlistment(&workdir_path) && res;
838 strbuf_release(&src_path);
839 strbuf_release(&workdir_path);
840 return res;
842 strbuf_release(&src_path);
843 strbuf_release(&workdir_path);
846 setup_enlistment_directory(argc, argv, usage, options, NULL);
848 return unregister_dir();
851 static int cmd_delete(int argc, const char **argv)
853 char *cwd = xgetcwd();
854 struct option options[] = {
855 OPT_END(),
857 const char * const usage[] = {
858 N_("scalar delete <enlistment>"),
859 NULL
861 struct strbuf enlistment = STRBUF_INIT;
862 int res = 0;
864 argc = parse_options(argc, argv, NULL, options,
865 usage, 0);
867 if (argc != 1)
868 usage_with_options(usage, options);
870 setup_enlistment_directory(argc, argv, usage, options, &enlistment);
872 if (dir_inside_of(cwd, enlistment.buf) >= 0)
873 res = error(_("refusing to delete current working directory"));
874 else {
875 close_object_store(the_repository->objects);
876 res = delete_enlistment(&enlistment);
878 strbuf_release(&enlistment);
879 free(cwd);
881 return res;
884 static int cmd_help(int argc, const char **argv)
886 struct option options[] = {
887 OPT_END(),
889 const char * const usage[] = {
890 "scalar help",
891 NULL
894 argc = parse_options(argc, argv, NULL, options,
895 usage, 0);
897 if (argc != 0)
898 usage_with_options(usage, options);
900 return run_git("help", "scalar", NULL);
903 static int cmd_version(int argc, const char **argv)
905 int verbose = 0, build_options = 0;
906 struct option options[] = {
907 OPT__VERBOSE(&verbose, N_("include Git version")),
908 OPT_BOOL(0, "build-options", &build_options,
909 N_("include Git's build options")),
910 OPT_END(),
912 const char * const usage[] = {
913 N_("scalar verbose [-v | --verbose] [--build-options]"),
914 NULL
916 struct strbuf buf = STRBUF_INIT;
918 argc = parse_options(argc, argv, NULL, options,
919 usage, 0);
921 if (argc != 0)
922 usage_with_options(usage, options);
924 get_version_info(&buf, build_options);
925 fprintf(stderr, "%s\n", buf.buf);
926 strbuf_release(&buf);
928 return 0;
931 static struct {
932 const char *name;
933 int (*fn)(int, const char **);
934 } builtins[] = {
935 { "clone", cmd_clone },
936 { "list", cmd_list },
937 { "register", cmd_register },
938 { "unregister", cmd_unregister },
939 { "run", cmd_run },
940 { "reconfigure", cmd_reconfigure },
941 { "delete", cmd_delete },
942 { "help", cmd_help },
943 { "version", cmd_version },
944 { "diagnose", cmd_diagnose },
945 { NULL, NULL},
948 int cmd_main(int argc, const char **argv)
950 struct strbuf scalar_usage = STRBUF_INIT;
951 int i;
953 while (argc > 1 && *argv[1] == '-') {
954 if (!strcmp(argv[1], "-C")) {
955 if (argc < 3)
956 die(_("-C requires a <directory>"));
957 if (chdir(argv[2]) < 0)
958 die_errno(_("could not change to '%s'"),
959 argv[2]);
960 argc -= 2;
961 argv += 2;
962 } else if (!strcmp(argv[1], "-c")) {
963 if (argc < 3)
964 die(_("-c requires a <key>=<value> argument"));
965 git_config_push_parameter(argv[2]);
966 argc -= 2;
967 argv += 2;
968 } else
969 break;
972 if (argc > 1) {
973 argv++;
974 argc--;
976 for (i = 0; builtins[i].name; i++)
977 if (!strcmp(builtins[i].name, argv[0]))
978 return !!builtins[i].fn(argc, argv);
981 strbuf_addstr(&scalar_usage,
982 N_("scalar [-C <directory>] [-c <key>=<value>] "
983 "<command> [<options>]\n\nCommands:\n"));
984 for (i = 0; builtins[i].name; i++)
985 strbuf_addf(&scalar_usage, "\t%s\n", builtins[i].name);
987 usage(scalar_usage.buf);