2 * The Scalar command-line interface.
7 #include "parse-options.h"
9 #include "run-command.h"
10 #include "simple-ipc.h"
11 #include "fsmonitor-ipc.h"
12 #include "fsmonitor-settings.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;
27 if (startup_info
->have_repository
)
28 BUG("gitdir already set up?!?");
31 usage_with_options(usagestr
, options
);
33 /* find the worktree, determine its corresponding root */
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 */
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
);
64 strbuf_addstr(enlistment_root
, the_repository
->worktree
);
67 strbuf_release(&path
);
70 static int run_git(const char *arg
, ...)
72 struct strvec argv
= STRVEC_INIT
;
78 strvec_push(&argv
, arg
);
79 while ((p
= va_arg(args
, const char *)))
80 strvec_push(&argv
, p
);
83 res
= run_command_v_opt(argv
.v
, RUN_GIT_CMD
);
89 struct scalar_config
{
92 int overwrite_on_reconfigure
;
95 static int set_scalar_config(const struct scalar_config
*config
, int reconfigure
)
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
);
105 trace2_data_string("scalar", the_repository
, config
->key
, "exists");
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
[] = {
123 { "am.keepCR", "true", 1 },
124 { "core.FSCache", "true", 1 },
125 { "core.multiPackIndex", "true", 1 },
126 { "core.preloadIndex", "true", 1 },
128 { "core.untrackedCache", "true", 1 },
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
139 * Therefore, with a sad heart, we disable this very useful
140 * feature on Windows.
142 { "core.untrackedCache", "false", 1 },
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.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 return error(_("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;
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", "origin", NULL
))) {
506 warning(_("partial clone failed; attempting full clone"));
508 if (set_config("remote.origin.promisor") ||
509 set_config("remote.origin.partialCloneFilter")) {
510 res
= error(_("could not configure for full clone"));
514 if ((res
= run_git("fetch", "--quiet", "origin", NULL
)))
518 if ((res
= set_config("branch.%s.remote=origin", branch
)))
520 if ((res
= set_config("branch.%s.merge=refs/heads/%s",
525 strbuf_addf(&buf
, "origin/%s", branch
);
526 res
= run_git("checkout", "-f", "-t", buf
.buf
, NULL
);
530 res
= register_dir();
535 strbuf_release(&buf
);
539 static int cmd_diagnose(int argc
, const char **argv
)
541 struct option options
[] = {
544 const char * const usage
[] = {
545 N_("scalar diagnose [<enlistment>]"),
548 struct strbuf diagnostics_root
= STRBUF_INIT
;
551 argc
= parse_options(argc
, argv
, NULL
, options
,
554 setup_enlistment_directory(argc
, argv
, usage
, options
, &diagnostics_root
);
555 strbuf_addstr(&diagnostics_root
, "/.scalarDiagnostics");
557 res
= run_git("diagnose", "--mode=all", "-s", "%Y%m%d_%H%M%S",
558 "-o", diagnostics_root
.buf
, NULL
);
560 strbuf_release(&diagnostics_root
);
564 static int cmd_list(int argc
, const char **argv
)
567 die(_("`scalar list` does not take arguments"));
569 if (run_git("config", "--global", "--get-all", "scalar.repo", NULL
) < 0)
574 static int cmd_register(int argc
, const char **argv
)
576 struct option options
[] = {
579 const char * const usage
[] = {
580 N_("scalar register [<enlistment>]"),
584 argc
= parse_options(argc
, argv
, NULL
, options
,
587 setup_enlistment_directory(argc
, argv
, usage
, options
, NULL
);
589 return register_dir();
592 static int get_scalar_repos(const char *key
, const char *value
, void *data
)
594 struct string_list
*list
= data
;
596 if (!strcmp(key
, "scalar.repo"))
597 string_list_append(list
, value
);
602 static int cmd_reconfigure(int argc
, const char **argv
)
605 struct option options
[] = {
606 OPT_BOOL('a', "all", &all
,
607 N_("reconfigure all registered enlistments")),
610 const char * const usage
[] = {
611 N_("scalar reconfigure [--all | <enlistment>]"),
614 struct string_list scalar_repos
= STRING_LIST_INIT_DUP
;
616 struct repository r
= { NULL
};
617 struct strbuf commondir
= STRBUF_INIT
, gitdir
= STRBUF_INIT
;
619 argc
= parse_options(argc
, argv
, NULL
, options
,
623 setup_enlistment_directory(argc
, argv
, usage
, options
, NULL
);
625 return set_recommended_config(1);
629 usage_msg_opt(_("--all or <enlistment>, but not both"),
632 git_config(get_scalar_repos
, &scalar_repos
);
634 for (i
= 0; i
< scalar_repos
.nr
; i
++) {
635 const char *dir
= scalar_repos
.items
[i
].string
;
637 strbuf_reset(&commondir
);
638 strbuf_reset(&gitdir
);
640 if (chdir(dir
) < 0) {
641 warning_errno(_("could not switch to '%s'"), dir
);
643 } else if (discover_git_directory(&commondir
, &gitdir
) < 0) {
644 warning_errno(_("git repository gone in '%s'"), dir
);
650 r
.commondir
= commondir
.buf
;
651 r
.gitdir
= gitdir
.buf
;
653 if (set_recommended_config(1) < 0)
658 string_list_clear(&scalar_repos
, 1);
659 strbuf_release(&commondir
);
660 strbuf_release(&gitdir
);
665 static int cmd_run(int argc
, const char **argv
)
667 struct option options
[] = {
671 const char *arg
, *task
;
674 { "commit-graph", "commit-graph" },
675 { "fetch", "prefetch" },
676 { "loose-objects", "loose-objects" },
677 { "pack-files", "incremental-repack" },
680 struct strbuf buf
= STRBUF_INIT
;
681 const char *usagestr
[] = { NULL
, NULL
};
684 strbuf_addstr(&buf
, N_("scalar run <task> [<enlistment>]\nTasks:\n"));
685 for (i
= 0; tasks
[i
].arg
; i
++)
686 strbuf_addf(&buf
, "\t%s\n", tasks
[i
].arg
);
687 usagestr
[0] = buf
.buf
;
689 argc
= parse_options(argc
, argv
, NULL
, options
,
693 usage_with_options(usagestr
, options
);
695 if (!strcmp("all", argv
[0])) {
698 for (i
= 0; tasks
[i
].arg
&& strcmp(tasks
[i
].arg
, argv
[0]); i
++)
699 ; /* keep looking for the task */
701 if (i
> 0 && !tasks
[i
].arg
) {
702 error(_("no such task: '%s'"), argv
[0]);
703 usage_with_options(usagestr
, options
);
709 setup_enlistment_directory(argc
, argv
, usagestr
, options
, NULL
);
710 strbuf_release(&buf
);
713 return register_dir();
716 return run_git("maintenance", "run",
717 "--task", tasks
[i
].task
, NULL
);
721 for (i
= 1; tasks
[i
].arg
; i
++)
722 if (run_git("maintenance", "run",
723 "--task", tasks
[i
].task
, NULL
))
728 static int remove_deleted_enlistment(struct strbuf
*path
)
731 strbuf_realpath_forgiving(path
, path
->buf
, 1);
733 if (run_git("config", "--global",
734 "--unset", "--fixed-value",
735 "scalar.repo", path
->buf
, NULL
) < 0)
738 if (run_git("config", "--global",
739 "--unset", "--fixed-value",
740 "maintenance.repo", path
->buf
, NULL
) < 0)
746 static int cmd_unregister(int argc
, const char **argv
)
748 struct option options
[] = {
751 const char * const usage
[] = {
752 N_("scalar unregister [<enlistment>]"),
756 argc
= parse_options(argc
, argv
, NULL
, options
,
760 * Be forgiving when the enlistment or worktree does not even exist any
761 * longer; This can be the case if a user deleted the worktree by
762 * mistake and _still_ wants to unregister the thing.
765 struct strbuf src_path
= STRBUF_INIT
, workdir_path
= STRBUF_INIT
;
767 strbuf_addf(&src_path
, "%s/src/.git", argv
[0]);
768 strbuf_addf(&workdir_path
, "%s/.git", argv
[0]);
769 if (!is_directory(src_path
.buf
) && !is_directory(workdir_path
.buf
)) {
770 /* remove possible matching registrations */
773 strbuf_strip_suffix(&src_path
, "/.git");
774 res
= remove_deleted_enlistment(&src_path
) && res
;
776 strbuf_strip_suffix(&workdir_path
, "/.git");
777 res
= remove_deleted_enlistment(&workdir_path
) && res
;
779 strbuf_release(&src_path
);
780 strbuf_release(&workdir_path
);
783 strbuf_release(&src_path
);
784 strbuf_release(&workdir_path
);
787 setup_enlistment_directory(argc
, argv
, usage
, options
, NULL
);
789 return unregister_dir();
792 static int cmd_delete(int argc
, const char **argv
)
794 char *cwd
= xgetcwd();
795 struct option options
[] = {
798 const char * const usage
[] = {
799 N_("scalar delete <enlistment>"),
802 struct strbuf enlistment
= STRBUF_INIT
;
805 argc
= parse_options(argc
, argv
, NULL
, options
,
809 usage_with_options(usage
, options
);
811 setup_enlistment_directory(argc
, argv
, usage
, options
, &enlistment
);
813 if (dir_inside_of(cwd
, enlistment
.buf
) >= 0)
814 res
= error(_("refusing to delete current working directory"));
816 close_object_store(the_repository
->objects
);
817 res
= delete_enlistment(&enlistment
);
819 strbuf_release(&enlistment
);
825 static int cmd_help(int argc
, const char **argv
)
827 struct option options
[] = {
830 const char * const usage
[] = {
835 argc
= parse_options(argc
, argv
, NULL
, options
,
839 usage_with_options(usage
, options
);
841 return run_git("help", "scalar", NULL
);
844 static int cmd_version(int argc
, const char **argv
)
846 int verbose
= 0, build_options
= 0;
847 struct option options
[] = {
848 OPT__VERBOSE(&verbose
, N_("include Git version")),
849 OPT_BOOL(0, "build-options", &build_options
,
850 N_("include Git's build options")),
853 const char * const usage
[] = {
854 N_("scalar verbose [-v | --verbose] [--build-options]"),
857 struct strbuf buf
= STRBUF_INIT
;
859 argc
= parse_options(argc
, argv
, NULL
, options
,
863 usage_with_options(usage
, options
);
865 get_version_info(&buf
, build_options
);
866 fprintf(stderr
, "%s\n", buf
.buf
);
867 strbuf_release(&buf
);
874 int (*fn
)(int, const char **);
876 { "clone", cmd_clone
},
877 { "list", cmd_list
},
878 { "register", cmd_register
},
879 { "unregister", cmd_unregister
},
881 { "reconfigure", cmd_reconfigure
},
882 { "delete", cmd_delete
},
883 { "help", cmd_help
},
884 { "version", cmd_version
},
885 { "diagnose", cmd_diagnose
},
889 int cmd_main(int argc
, const char **argv
)
891 struct strbuf scalar_usage
= STRBUF_INIT
;
894 while (argc
> 1 && *argv
[1] == '-') {
895 if (!strcmp(argv
[1], "-C")) {
897 die(_("-C requires a <directory>"));
898 if (chdir(argv
[2]) < 0)
899 die_errno(_("could not change to '%s'"),
903 } else if (!strcmp(argv
[1], "-c")) {
905 die(_("-c requires a <key>=<value> argument"));
906 git_config_push_parameter(argv
[2]);
917 for (i
= 0; builtins
[i
].name
; i
++)
918 if (!strcmp(builtins
[i
].name
, argv
[0]))
919 return !!builtins
[i
].fn(argc
, argv
);
922 strbuf_addstr(&scalar_usage
,
923 N_("scalar [-C <directory>] [-c <key>=<value>] "
924 "<command> [<options>]\n\nCommands:\n"));
925 for (i
= 0; builtins
[i
].name
; i
++)
926 strbuf_addf(&scalar_usage
, "\t%s\n", builtins
[i
].name
);
928 usage(scalar_usage
.buf
);