2 * The Scalar command-line interface.
8 #include "parse-options.h"
10 #include "run-command.h"
11 #include "simple-ipc.h"
12 #include "fsmonitor-ipc.h"
13 #include "fsmonitor-settings.h"
20 static void setup_enlistment_directory(int argc
, const char **argv
,
21 const char * const *usagestr
,
22 const struct option
*options
,
23 struct strbuf
*enlistment_root
)
25 struct strbuf path
= STRBUF_INIT
;
26 int enlistment_is_repo_parent
= 0;
29 if (startup_info
->have_repository
)
30 BUG("gitdir already set up?!?");
33 usage_with_options(usagestr
, options
);
35 /* find the worktree, determine its corresponding root */
37 strbuf_add_absolute_path(&path
, argv
[0]);
38 if (!is_directory(path
.buf
))
39 die(_("'%s' does not exist"), path
.buf
);
40 if (chdir(path
.buf
) < 0)
41 die_errno(_("could not switch to '%s'"), path
.buf
);
42 } else if (strbuf_getcwd(&path
) < 0)
43 die(_("need a working directory"));
45 strbuf_trim_trailing_dir_sep(&path
);
47 /* check if currently in enlistment root with src/ workdir */
49 strbuf_addstr(&path
, "/src");
50 if (is_nonbare_repository_dir(&path
)) {
51 enlistment_is_repo_parent
= 1;
52 if (chdir(path
.buf
) < 0)
53 die_errno(_("could not switch to '%s'"), path
.buf
);
55 strbuf_setlen(&path
, len
);
57 setup_git_directory();
59 if (!the_repository
->worktree
)
60 die(_("Scalar enlistments require a worktree"));
62 if (enlistment_root
) {
63 if (enlistment_is_repo_parent
)
64 strbuf_addbuf(enlistment_root
, &path
);
66 strbuf_addstr(enlistment_root
, the_repository
->worktree
);
69 strbuf_release(&path
);
72 static int run_git(const char *arg
, ...)
74 struct child_process cmd
= CHILD_PROCESS_INIT
;
79 strvec_push(&cmd
.args
, arg
);
80 while ((p
= va_arg(args
, const char *)))
81 strvec_push(&cmd
.args
, p
);
85 return run_command(&cmd
);
88 struct scalar_config
{
91 int overwrite_on_reconfigure
;
94 static int set_scalar_config(const struct scalar_config
*config
, int reconfigure
)
99 if ((reconfigure
&& config
->overwrite_on_reconfigure
) ||
100 git_config_get_string(config
->key
, &value
)) {
101 trace2_data_string("scalar", the_repository
, config
->key
, "created");
102 res
= git_config_set_gently(config
->key
, config
->value
);
104 trace2_data_string("scalar", the_repository
, config
->key
, "exists");
112 static int have_fsmonitor_support(void)
114 return fsmonitor_ipc__is_supported() &&
115 fsm_settings__get_reason(the_repository
) == FSMONITOR_REASON_OK
;
118 static int set_recommended_config(int reconfigure
)
120 struct scalar_config config
[] = {
122 { "am.keepCR", "true", 1 },
123 { "core.FSCache", "true", 1 },
124 { "core.multiPackIndex", "true", 1 },
125 { "core.preloadIndex", "true", 1 },
127 { "core.untrackedCache", "true", 1 },
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
138 * Therefore, with a sad heart, we disable this very useful
139 * feature on Windows.
141 { "core.untrackedCache", "false", 1 },
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.skipHash", "false", 1 },
149 { "index.threads", "true", 1 },
150 { "index.version", "4", 1 },
151 { "merge.stat", "false", 1 },
152 { "merge.renames", "true", 1 },
153 { "pack.useBitmaps", "false", 1 },
154 { "pack.useSparse", "true", 1 },
155 { "receive.autoGC", "false", 1 },
156 { "feature.manyFiles", "false", 1 },
157 { "feature.experimental", "false", 1 },
158 { "fetch.unpackLimit", "1", 1 },
159 { "fetch.writeCommitGraph", "false", 1 },
161 { "http.sslBackend", "schannel", 1 },
164 { "status.aheadBehind", "false" },
165 { "commitGraph.generationVersion", "1" },
166 { "core.autoCRLF", "false" },
167 { "core.safeCRLF", "false" },
168 { "fetch.showForcedUpdates", "false" },
174 for (i
= 0; config
[i
].key
; i
++) {
175 if (set_scalar_config(config
+ i
, reconfigure
))
176 return error(_("could not configure %s=%s"),
177 config
[i
].key
, config
[i
].value
);
180 if (have_fsmonitor_support()) {
181 struct scalar_config fsmonitor
= { "core.fsmonitor", "true" };
182 if (set_scalar_config(&fsmonitor
, reconfigure
))
183 return error(_("could not configure %s=%s"),
184 fsmonitor
.key
, fsmonitor
.value
);
188 * The `log.excludeDecoration` setting is special because it allows
189 * for multiple values.
191 if (git_config_get_string("log.excludeDecoration", &value
)) {
192 trace2_data_string("scalar", the_repository
,
193 "log.excludeDecoration", "created");
194 if (git_config_set_multivar_gently("log.excludeDecoration",
196 CONFIG_REGEX_NONE
, 0))
197 return error(_("could not configure "
198 "log.excludeDecoration"));
200 trace2_data_string("scalar", the_repository
,
201 "log.excludeDecoration", "exists");
208 static int toggle_maintenance(int enable
)
210 return run_git("maintenance",
211 enable
? "start" : "unregister",
212 enable
? NULL
: "--force",
216 static int add_or_remove_enlistment(int add
)
220 if (!the_repository
->worktree
)
221 die(_("Scalar enlistments require a worktree"));
223 res
= run_git("config", "--global", "--get", "--fixed-value",
224 "scalar.repo", the_repository
->worktree
, NULL
);
227 * If we want to add and the setting is already there, then do nothing.
228 * If we want to remove and the setting is not there, then do nothing.
230 if ((add
&& !res
) || (!add
&& res
))
233 return run_git("config", "--global", add
? "--add" : "--unset",
234 add
? "--no-fixed-value" : "--fixed-value",
235 "scalar.repo", the_repository
->worktree
, NULL
);
238 static int start_fsmonitor_daemon(void)
240 assert(have_fsmonitor_support());
242 if (fsmonitor_ipc__get_state() != IPC_STATE__LISTENING
)
243 return run_git("fsmonitor--daemon", "start", NULL
);
248 static int stop_fsmonitor_daemon(void)
250 assert(have_fsmonitor_support());
252 if (fsmonitor_ipc__get_state() == IPC_STATE__LISTENING
)
253 return run_git("fsmonitor--daemon", "stop", NULL
);
258 static int register_dir(void)
260 if (add_or_remove_enlistment(1))
261 return error(_("could not add enlistment"));
263 if (set_recommended_config(0))
264 return error(_("could not set recommended config"));
266 if (toggle_maintenance(1))
267 warning(_("could not turn on maintenance"));
269 if (have_fsmonitor_support() && start_fsmonitor_daemon()) {
270 return error(_("could not start the FSMonitor daemon"));
276 static int unregister_dir(void)
280 if (toggle_maintenance(0))
281 res
= error(_("could not turn off maintenance"));
283 if (add_or_remove_enlistment(0))
284 res
= error(_("could not remove enlistment"));
289 /* printf-style interface, expects `<key>=<value>` argument */
290 static int set_config(const char *fmt
, ...)
292 struct strbuf buf
= STRBUF_INIT
;
298 strbuf_vaddf(&buf
, fmt
, args
);
301 value
= strchr(buf
.buf
, '=');
304 res
= git_config_set_gently(buf
.buf
, value
);
305 strbuf_release(&buf
);
310 static char *remote_default_branch(const char *url
)
312 struct child_process cp
= CHILD_PROCESS_INIT
;
313 struct strbuf out
= STRBUF_INIT
;
316 strvec_pushl(&cp
.args
, "ls-remote", "--symref", url
, "HEAD", NULL
);
317 if (!pipe_command(&cp
, NULL
, 0, &out
, 0, NULL
, 0)) {
318 const char *line
= out
.buf
;
321 const char *eol
= strchrnul(line
, '\n'), *p
;
322 size_t len
= eol
- line
;
325 if (!skip_prefix(line
, "ref: ", &p
) ||
326 !strip_suffix_mem(line
, &len
, "\tHEAD")) {
327 line
= eol
+ (*eol
== '\n');
332 if (skip_prefix(p
, "refs/heads/", &p
)) {
333 branch
= xstrndup(p
, eol
- p
);
334 strbuf_release(&out
);
338 error(_("remote HEAD is not a branch: '%.*s'"),
340 strbuf_release(&out
);
344 warning(_("failed to get default branch name from remote; "
345 "using local default"));
348 child_process_init(&cp
);
350 strvec_pushl(&cp
.args
, "symbolic-ref", "--short", "HEAD", NULL
);
351 if (!pipe_command(&cp
, NULL
, 0, &out
, 0, NULL
, 0)) {
353 return strbuf_detach(&out
, NULL
);
356 strbuf_release(&out
);
357 error(_("failed to get default branch name"));
361 static int delete_enlistment(struct strbuf
*enlistment
)
364 struct strbuf parent
= STRBUF_INIT
;
369 if (unregister_dir())
370 return error(_("failed to unregister repository"));
374 * Change the current directory to one outside of the enlistment so
375 * that we may delete everything underneath it.
377 offset
= offset_1st_component(enlistment
->buf
);
378 path_sep
= find_last_dir_sep(enlistment
->buf
+ offset
);
379 strbuf_add(&parent
, enlistment
->buf
,
380 path_sep
? path_sep
- enlistment
->buf
: offset
);
381 if (chdir(parent
.buf
) < 0) {
382 int res
= error_errno(_("could not switch to '%s'"), parent
.buf
);
383 strbuf_release(&parent
);
386 strbuf_release(&parent
);
389 if (have_fsmonitor_support() && stop_fsmonitor_daemon())
390 return error(_("failed to stop the FSMonitor daemon"));
392 if (remove_dir_recursively(enlistment
, 0))
393 return error(_("failed to delete enlistment directory"));
399 * Dummy implementation; Using `get_version_info()` would cause a link error
402 void load_builtin_commands(const char *prefix
, struct cmdnames
*cmds
)
404 die("not implemented");
407 static int cmd_clone(int argc
, const char **argv
)
409 const char *branch
= NULL
;
410 int full_clone
= 0, single_branch
= 0, show_progress
= isatty(2);
411 struct option clone_options
[] = {
412 OPT_STRING('b', "branch", &branch
, N_("<branch>"),
413 N_("branch to checkout after clone")),
414 OPT_BOOL(0, "full-clone", &full_clone
,
415 N_("when cloning, create full working directory")),
416 OPT_BOOL(0, "single-branch", &single_branch
,
417 N_("only download metadata for the branch that will "
421 const char * const clone_usage
[] = {
422 N_("scalar clone [<options>] [--] <repo> [<dir>]"),
426 char *enlistment
= NULL
, *dir
= NULL
;
427 struct strbuf buf
= STRBUF_INIT
;
430 argc
= parse_options(argc
, argv
, NULL
, clone_options
, clone_usage
, 0);
434 enlistment
= xstrdup(argv
[1]);
435 } else if (argc
== 1) {
438 strbuf_addstr(&buf
, url
);
439 /* Strip trailing slashes, if any */
440 while (buf
.len
> 0 && is_dir_sep(buf
.buf
[buf
.len
- 1]))
441 strbuf_setlen(&buf
, buf
.len
- 1);
442 /* Strip suffix `.git`, if any */
443 strbuf_strip_suffix(&buf
, ".git");
445 enlistment
= find_last_dir_sep(buf
.buf
);
447 die(_("cannot deduce worktree name from '%s'"), url
);
449 enlistment
= xstrdup(enlistment
+ 1);
451 usage_msg_opt(_("You must specify a repository to clone."),
452 clone_usage
, clone_options
);
455 if (is_directory(enlistment
))
456 die(_("directory '%s' exists already"), enlistment
);
458 dir
= xstrfmt("%s/src", enlistment
);
462 strbuf_addf(&buf
, "init.defaultBranch=%s", branch
);
464 char *b
= repo_default_branch_name(the_repository
, 1);
465 strbuf_addf(&buf
, "init.defaultBranch=%s", b
);
469 if ((res
= run_git("-c", buf
.buf
, "init", "--", dir
, NULL
)))
472 if (chdir(dir
) < 0) {
473 res
= error_errno(_("could not switch to '%s'"), dir
);
477 setup_git_directory();
479 /* common-main already logs `argv` */
480 trace2_def_repo(the_repository
);
482 if (!branch
&& !(branch
= remote_default_branch(url
))) {
483 res
= error(_("failed to get default branch for '%s'"), url
);
487 if (set_config("remote.origin.url=%s", url
) ||
488 set_config("remote.origin.fetch="
489 "+refs/heads/%s:refs/remotes/origin/%s",
490 single_branch
? branch
: "*",
491 single_branch
? branch
: "*") ||
492 set_config("remote.origin.promisor=true") ||
493 set_config("remote.origin.partialCloneFilter=blob:none")) {
494 res
= error(_("could not configure remote in '%s'"), dir
);
499 (res
= run_git("sparse-checkout", "init", "--cone", NULL
)))
502 if (set_recommended_config(0))
503 return error(_("could not configure '%s'"), dir
);
505 if ((res
= run_git("fetch", "--quiet",
506 show_progress
? "--progress" : "--no-progress",
508 warning(_("partial clone failed; attempting full clone"));
510 if (set_config("remote.origin.promisor") ||
511 set_config("remote.origin.partialCloneFilter")) {
512 res
= error(_("could not configure for full clone"));
516 if ((res
= run_git("fetch", "--quiet",
517 show_progress
? "--progress" : "--no-progress",
522 if ((res
= set_config("branch.%s.remote=origin", branch
)))
524 if ((res
= set_config("branch.%s.merge=refs/heads/%s",
529 strbuf_addf(&buf
, "origin/%s", branch
);
530 res
= run_git("checkout", "-f", "-t", buf
.buf
, NULL
);
534 res
= register_dir();
539 strbuf_release(&buf
);
543 static int cmd_diagnose(int argc
, const char **argv
)
545 struct option options
[] = {
548 const char * const usage
[] = {
549 N_("scalar diagnose [<enlistment>]"),
552 struct strbuf diagnostics_root
= STRBUF_INIT
;
555 argc
= parse_options(argc
, argv
, NULL
, options
,
558 setup_enlistment_directory(argc
, argv
, usage
, options
, &diagnostics_root
);
559 strbuf_addstr(&diagnostics_root
, "/.scalarDiagnostics");
561 res
= run_git("diagnose", "--mode=all", "-s", "%Y%m%d_%H%M%S",
562 "-o", diagnostics_root
.buf
, NULL
);
564 strbuf_release(&diagnostics_root
);
568 static int cmd_list(int argc
, const char **argv UNUSED
)
571 die(_("`scalar list` does not take arguments"));
573 if (run_git("config", "--global", "--get-all", "scalar.repo", NULL
) < 0)
578 static int cmd_register(int argc
, const char **argv
)
580 struct option options
[] = {
583 const char * const usage
[] = {
584 N_("scalar register [<enlistment>]"),
588 argc
= parse_options(argc
, argv
, NULL
, options
,
591 setup_enlistment_directory(argc
, argv
, usage
, options
, NULL
);
593 return register_dir();
596 static int get_scalar_repos(const char *key
, const char *value
, void *data
)
598 struct string_list
*list
= data
;
600 if (!strcmp(key
, "scalar.repo"))
601 string_list_append(list
, value
);
606 static int remove_deleted_enlistment(struct strbuf
*path
)
609 strbuf_realpath_forgiving(path
, path
->buf
, 1);
611 if (run_git("config", "--global",
612 "--unset", "--fixed-value",
613 "scalar.repo", path
->buf
, NULL
) < 0)
616 if (run_git("config", "--global",
617 "--unset", "--fixed-value",
618 "maintenance.repo", path
->buf
, NULL
) < 0)
624 static int cmd_reconfigure(int argc
, const char **argv
)
627 struct option options
[] = {
628 OPT_BOOL('a', "all", &all
,
629 N_("reconfigure all registered enlistments")),
632 const char * const usage
[] = {
633 N_("scalar reconfigure [--all | <enlistment>]"),
636 struct string_list scalar_repos
= STRING_LIST_INIT_DUP
;
638 struct repository r
= { NULL
};
639 struct strbuf commondir
= STRBUF_INIT
, gitdir
= STRBUF_INIT
;
641 argc
= parse_options(argc
, argv
, NULL
, options
,
645 setup_enlistment_directory(argc
, argv
, usage
, options
, NULL
);
647 return set_recommended_config(1);
651 usage_msg_opt(_("--all or <enlistment>, but not both"),
654 git_config(get_scalar_repos
, &scalar_repos
);
656 for (i
= 0; i
< scalar_repos
.nr
; i
++) {
657 const char *dir
= scalar_repos
.items
[i
].string
;
659 strbuf_reset(&commondir
);
660 strbuf_reset(&gitdir
);
662 if (chdir(dir
) < 0) {
663 struct strbuf buf
= STRBUF_INIT
;
665 if (errno
!= ENOENT
) {
666 warning_errno(_("could not switch to '%s'"), dir
);
671 strbuf_addstr(&buf
, dir
);
672 if (remove_deleted_enlistment(&buf
))
673 res
= error(_("could not remove stale "
674 "scalar.repo '%s'"), dir
);
676 warning(_("removing stale scalar.repo '%s'"),
678 strbuf_release(&buf
);
679 } else if (discover_git_directory(&commondir
, &gitdir
) < 0) {
680 warning_errno(_("git repository gone in '%s'"), dir
);
686 r
.commondir
= commondir
.buf
;
687 r
.gitdir
= gitdir
.buf
;
689 if (set_recommended_config(1) < 0)
694 string_list_clear(&scalar_repos
, 1);
695 strbuf_release(&commondir
);
696 strbuf_release(&gitdir
);
701 static int cmd_run(int argc
, const char **argv
)
703 struct option options
[] = {
707 const char *arg
, *task
;
710 { "commit-graph", "commit-graph" },
711 { "fetch", "prefetch" },
712 { "loose-objects", "loose-objects" },
713 { "pack-files", "incremental-repack" },
716 struct strbuf buf
= STRBUF_INIT
;
717 const char *usagestr
[] = { NULL
, NULL
};
720 strbuf_addstr(&buf
, N_("scalar run <task> [<enlistment>]\nTasks:\n"));
721 for (i
= 0; tasks
[i
].arg
; i
++)
722 strbuf_addf(&buf
, "\t%s\n", tasks
[i
].arg
);
723 usagestr
[0] = buf
.buf
;
725 argc
= parse_options(argc
, argv
, NULL
, options
,
729 usage_with_options(usagestr
, options
);
731 if (!strcmp("all", argv
[0])) {
734 for (i
= 0; tasks
[i
].arg
&& strcmp(tasks
[i
].arg
, argv
[0]); i
++)
735 ; /* keep looking for the task */
737 if (i
> 0 && !tasks
[i
].arg
) {
738 error(_("no such task: '%s'"), argv
[0]);
739 usage_with_options(usagestr
, options
);
745 setup_enlistment_directory(argc
, argv
, usagestr
, options
, NULL
);
746 strbuf_release(&buf
);
749 return register_dir();
752 return run_git("maintenance", "run",
753 "--task", tasks
[i
].task
, NULL
);
757 for (i
= 1; tasks
[i
].arg
; i
++)
758 if (run_git("maintenance", "run",
759 "--task", tasks
[i
].task
, NULL
))
764 static int cmd_unregister(int argc
, const char **argv
)
766 struct option options
[] = {
769 const char * const usage
[] = {
770 N_("scalar unregister [<enlistment>]"),
774 argc
= parse_options(argc
, argv
, NULL
, options
,
778 * Be forgiving when the enlistment or worktree does not even exist any
779 * longer; This can be the case if a user deleted the worktree by
780 * mistake and _still_ wants to unregister the thing.
783 struct strbuf src_path
= STRBUF_INIT
, workdir_path
= STRBUF_INIT
;
785 strbuf_addf(&src_path
, "%s/src/.git", argv
[0]);
786 strbuf_addf(&workdir_path
, "%s/.git", argv
[0]);
787 if (!is_directory(src_path
.buf
) && !is_directory(workdir_path
.buf
)) {
788 /* remove possible matching registrations */
791 strbuf_strip_suffix(&src_path
, "/.git");
792 res
= remove_deleted_enlistment(&src_path
) && res
;
794 strbuf_strip_suffix(&workdir_path
, "/.git");
795 res
= remove_deleted_enlistment(&workdir_path
) && res
;
797 strbuf_release(&src_path
);
798 strbuf_release(&workdir_path
);
801 strbuf_release(&src_path
);
802 strbuf_release(&workdir_path
);
805 setup_enlistment_directory(argc
, argv
, usage
, options
, NULL
);
807 return unregister_dir();
810 static int cmd_delete(int argc
, const char **argv
)
812 char *cwd
= xgetcwd();
813 struct option options
[] = {
816 const char * const usage
[] = {
817 N_("scalar delete <enlistment>"),
820 struct strbuf enlistment
= STRBUF_INIT
;
823 argc
= parse_options(argc
, argv
, NULL
, options
,
827 usage_with_options(usage
, options
);
829 setup_enlistment_directory(argc
, argv
, usage
, options
, &enlistment
);
831 if (dir_inside_of(cwd
, enlistment
.buf
) >= 0)
832 res
= error(_("refusing to delete current working directory"));
834 close_object_store(the_repository
->objects
);
835 res
= delete_enlistment(&enlistment
);
837 strbuf_release(&enlistment
);
843 static int cmd_help(int argc
, const char **argv
)
845 struct option options
[] = {
848 const char * const usage
[] = {
853 argc
= parse_options(argc
, argv
, NULL
, options
,
857 usage_with_options(usage
, options
);
859 return run_git("help", "scalar", NULL
);
862 static int cmd_version(int argc
, const char **argv
)
864 int verbose
= 0, build_options
= 0;
865 struct option options
[] = {
866 OPT__VERBOSE(&verbose
, N_("include Git version")),
867 OPT_BOOL(0, "build-options", &build_options
,
868 N_("include Git's build options")),
871 const char * const usage
[] = {
872 N_("scalar verbose [-v | --verbose] [--build-options]"),
875 struct strbuf buf
= STRBUF_INIT
;
877 argc
= parse_options(argc
, argv
, NULL
, options
,
881 usage_with_options(usage
, options
);
883 get_version_info(&buf
, build_options
);
884 fprintf(stderr
, "%s\n", buf
.buf
);
885 strbuf_release(&buf
);
892 int (*fn
)(int, const char **);
894 { "clone", cmd_clone
},
895 { "list", cmd_list
},
896 { "register", cmd_register
},
897 { "unregister", cmd_unregister
},
899 { "reconfigure", cmd_reconfigure
},
900 { "delete", cmd_delete
},
901 { "help", cmd_help
},
902 { "version", cmd_version
},
903 { "diagnose", cmd_diagnose
},
907 int cmd_main(int argc
, const char **argv
)
909 struct strbuf scalar_usage
= STRBUF_INIT
;
912 while (argc
> 1 && *argv
[1] == '-') {
913 if (!strcmp(argv
[1], "-C")) {
915 die(_("-C requires a <directory>"));
916 if (chdir(argv
[2]) < 0)
917 die_errno(_("could not change to '%s'"),
921 } else if (!strcmp(argv
[1], "-c")) {
923 die(_("-c requires a <key>=<value> argument"));
924 git_config_push_parameter(argv
[2]);
935 for (i
= 0; builtins
[i
].name
; i
++)
936 if (!strcmp(builtins
[i
].name
, argv
[0]))
937 return !!builtins
[i
].fn(argc
, argv
);
940 strbuf_addstr(&scalar_usage
,
941 N_("scalar [-C <directory>] [-c <key>=<value>] "
942 "<command> [<options>]\n\nCommands:\n"));
943 for (i
= 0; builtins
[i
].name
; i
++)
944 strbuf_addf(&scalar_usage
, "\t%s\n", builtins
[i
].name
);
946 usage(scalar_usage
.buf
);