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"
19 static void setup_enlistment_directory(int argc
, const char **argv
,
20 const char * const *usagestr
,
21 const struct option
*options
,
22 struct strbuf
*enlistment_root
)
24 struct strbuf path
= STRBUF_INIT
;
25 int enlistment_is_repo_parent
= 0;
28 if (startup_info
->have_repository
)
29 BUG("gitdir already set up?!?");
32 usage_with_options(usagestr
, options
);
34 /* find the worktree, determine its corresponding root */
36 strbuf_add_absolute_path(&path
, argv
[0]);
37 if (!is_directory(path
.buf
))
38 die(_("'%s' does not exist"), path
.buf
);
39 if (chdir(path
.buf
) < 0)
40 die_errno(_("could not switch to '%s'"), path
.buf
);
41 } else if (strbuf_getcwd(&path
) < 0)
42 die(_("need a working directory"));
44 strbuf_trim_trailing_dir_sep(&path
);
46 /* check if currently in enlistment root with src/ workdir */
48 strbuf_addstr(&path
, "/src");
49 if (is_nonbare_repository_dir(&path
)) {
50 enlistment_is_repo_parent
= 1;
51 if (chdir(path
.buf
) < 0)
52 die_errno(_("could not switch to '%s'"), path
.buf
);
54 strbuf_setlen(&path
, len
);
56 setup_git_directory();
58 if (!the_repository
->worktree
)
59 die(_("Scalar enlistments require a worktree"));
61 if (enlistment_root
) {
62 if (enlistment_is_repo_parent
)
63 strbuf_addbuf(enlistment_root
, &path
);
65 strbuf_addstr(enlistment_root
, the_repository
->worktree
);
68 strbuf_release(&path
);
71 static int run_git(const char *arg
, ...)
73 struct child_process cmd
= CHILD_PROCESS_INIT
;
78 strvec_push(&cmd
.args
, arg
);
79 while ((p
= va_arg(args
, const char *)))
80 strvec_push(&cmd
.args
, p
);
84 return run_command(&cmd
);
87 struct scalar_config
{
90 int overwrite_on_reconfigure
;
93 static int set_scalar_config(const struct scalar_config
*config
, int reconfigure
)
98 if ((reconfigure
&& config
->overwrite_on_reconfigure
) ||
99 git_config_get_string(config
->key
, &value
)) {
100 trace2_data_string("scalar", the_repository
, config
->key
, "created");
101 res
= git_config_set_gently(config
->key
, config
->value
);
103 trace2_data_string("scalar", the_repository
, config
->key
, "exists");
111 static int have_fsmonitor_support(void)
113 return fsmonitor_ipc__is_supported() &&
114 fsm_settings__get_reason(the_repository
) == FSMONITOR_REASON_OK
;
117 static int set_recommended_config(int reconfigure
)
119 struct scalar_config config
[] = {
121 { "am.keepCR", "true", 1 },
122 { "core.FSCache", "true", 1 },
123 { "core.multiPackIndex", "true", 1 },
124 { "core.preloadIndex", "true", 1 },
126 { "core.untrackedCache", "true", 1 },
129 * Unfortunately, Scalar's Functional Tests demonstrated
130 * that the untracked cache feature is unreliable on Windows
131 * (which is a bummer because that platform would benefit the
132 * most from it). For some reason, freshly created files seem
133 * not to update the directory's `lastModified` time
134 * immediately, but the untracked cache would need to rely on
137 * Therefore, with a sad heart, we disable this very useful
138 * feature on Windows.
140 { "core.untrackedCache", "false", 1 },
142 { "core.logAllRefUpdates", "true", 1 },
143 { "credential.https://dev.azure.com.useHttpPath", "true", 1 },
144 { "credential.validate", "false", 1 }, /* GCM4W-only */
145 { "gc.auto", "0", 1 },
146 { "gui.GCWarning", "false", 1 },
147 { "index.skipHash", "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 { "feature.manyFiles", "false", 1 },
156 { "feature.experimental", "false", 1 },
157 { "fetch.unpackLimit", "1", 1 },
158 { "fetch.writeCommitGraph", "false", 1 },
160 { "http.sslBackend", "schannel", 1 },
163 { "status.aheadBehind", "false" },
164 { "commitGraph.generationVersion", "1" },
165 { "core.autoCRLF", "false" },
166 { "core.safeCRLF", "false" },
167 { "fetch.showForcedUpdates", "false" },
173 for (i
= 0; config
[i
].key
; i
++) {
174 if (set_scalar_config(config
+ i
, reconfigure
))
175 return error(_("could not configure %s=%s"),
176 config
[i
].key
, config
[i
].value
);
179 if (have_fsmonitor_support()) {
180 struct scalar_config fsmonitor
= { "core.fsmonitor", "true" };
181 if (set_scalar_config(&fsmonitor
, reconfigure
))
182 return error(_("could not configure %s=%s"),
183 fsmonitor
.key
, fsmonitor
.value
);
187 * The `log.excludeDecoration` setting is special because it allows
188 * for multiple values.
190 if (git_config_get_string("log.excludeDecoration", &value
)) {
191 trace2_data_string("scalar", the_repository
,
192 "log.excludeDecoration", "created");
193 if (git_config_set_multivar_gently("log.excludeDecoration",
195 CONFIG_REGEX_NONE
, 0))
196 return error(_("could not configure "
197 "log.excludeDecoration"));
199 trace2_data_string("scalar", the_repository
,
200 "log.excludeDecoration", "exists");
207 static int toggle_maintenance(int enable
)
209 return run_git("maintenance",
210 enable
? "start" : "unregister",
211 enable
? NULL
: "--force",
215 static int add_or_remove_enlistment(int add
)
219 if (!the_repository
->worktree
)
220 die(_("Scalar enlistments require a worktree"));
222 res
= run_git("config", "--global", "--get", "--fixed-value",
223 "scalar.repo", the_repository
->worktree
, NULL
);
226 * If we want to add and the setting is already there, then do nothing.
227 * If we want to remove and the setting is not there, then do nothing.
229 if ((add
&& !res
) || (!add
&& res
))
232 return run_git("config", "--global", add
? "--add" : "--unset",
233 add
? "--no-fixed-value" : "--fixed-value",
234 "scalar.repo", the_repository
->worktree
, NULL
);
237 static int start_fsmonitor_daemon(void)
239 assert(have_fsmonitor_support());
241 if (fsmonitor_ipc__get_state() != IPC_STATE__LISTENING
)
242 return run_git("fsmonitor--daemon", "start", NULL
);
247 static int stop_fsmonitor_daemon(void)
249 assert(have_fsmonitor_support());
251 if (fsmonitor_ipc__get_state() == IPC_STATE__LISTENING
)
252 return run_git("fsmonitor--daemon", "stop", NULL
);
257 static int register_dir(void)
259 if (add_or_remove_enlistment(1))
260 return error(_("could not add enlistment"));
262 if (set_recommended_config(0))
263 return error(_("could not set recommended config"));
265 if (toggle_maintenance(1))
266 warning(_("could not turn on maintenance"));
268 if (have_fsmonitor_support() && start_fsmonitor_daemon()) {
269 return error(_("could not start the FSMonitor daemon"));
275 static int unregister_dir(void)
279 if (toggle_maintenance(0))
280 res
= error(_("could not turn off maintenance"));
282 if (add_or_remove_enlistment(0))
283 res
= error(_("could not remove enlistment"));
288 /* printf-style interface, expects `<key>=<value>` argument */
289 static int set_config(const char *fmt
, ...)
291 struct strbuf buf
= STRBUF_INIT
;
297 strbuf_vaddf(&buf
, fmt
, args
);
300 value
= strchr(buf
.buf
, '=');
303 res
= git_config_set_gently(buf
.buf
, value
);
304 strbuf_release(&buf
);
309 static char *remote_default_branch(const char *url
)
311 struct child_process cp
= CHILD_PROCESS_INIT
;
312 struct strbuf out
= STRBUF_INIT
;
315 strvec_pushl(&cp
.args
, "ls-remote", "--symref", url
, "HEAD", NULL
);
316 if (!pipe_command(&cp
, NULL
, 0, &out
, 0, NULL
, 0)) {
317 const char *line
= out
.buf
;
320 const char *eol
= strchrnul(line
, '\n'), *p
;
321 size_t len
= eol
- line
;
324 if (!skip_prefix(line
, "ref: ", &p
) ||
325 !strip_suffix_mem(line
, &len
, "\tHEAD")) {
326 line
= eol
+ (*eol
== '\n');
331 if (skip_prefix(p
, "refs/heads/", &p
)) {
332 branch
= xstrndup(p
, eol
- p
);
333 strbuf_release(&out
);
337 error(_("remote HEAD is not a branch: '%.*s'"),
339 strbuf_release(&out
);
343 warning(_("failed to get default branch name from remote; "
344 "using local default"));
347 child_process_init(&cp
);
349 strvec_pushl(&cp
.args
, "symbolic-ref", "--short", "HEAD", NULL
);
350 if (!pipe_command(&cp
, NULL
, 0, &out
, 0, NULL
, 0)) {
352 return strbuf_detach(&out
, NULL
);
355 strbuf_release(&out
);
356 error(_("failed to get default branch name"));
360 static int delete_enlistment(struct strbuf
*enlistment
)
363 struct strbuf parent
= STRBUF_INIT
;
368 if (unregister_dir())
369 return error(_("failed to unregister repository"));
373 * Change the current directory to one outside of the enlistment so
374 * that we may delete everything underneath it.
376 offset
= offset_1st_component(enlistment
->buf
);
377 path_sep
= find_last_dir_sep(enlistment
->buf
+ offset
);
378 strbuf_add(&parent
, enlistment
->buf
,
379 path_sep
? path_sep
- enlistment
->buf
: offset
);
380 if (chdir(parent
.buf
) < 0) {
381 int res
= error_errno(_("could not switch to '%s'"), parent
.buf
);
382 strbuf_release(&parent
);
385 strbuf_release(&parent
);
388 if (have_fsmonitor_support() && stop_fsmonitor_daemon())
389 return error(_("failed to stop the FSMonitor daemon"));
391 if (remove_dir_recursively(enlistment
, 0))
392 return error(_("failed to delete enlistment directory"));
398 * Dummy implementation; Using `get_version_info()` would cause a link error
401 void load_builtin_commands(const char *prefix
, struct cmdnames
*cmds
)
403 die("not implemented");
406 static int cmd_clone(int argc
, const char **argv
)
408 const char *branch
= NULL
;
409 int full_clone
= 0, single_branch
= 0, show_progress
= isatty(2);
410 struct option clone_options
[] = {
411 OPT_STRING('b', "branch", &branch
, N_("<branch>"),
412 N_("branch to checkout after clone")),
413 OPT_BOOL(0, "full-clone", &full_clone
,
414 N_("when cloning, create full working directory")),
415 OPT_BOOL(0, "single-branch", &single_branch
,
416 N_("only download metadata for the branch that will "
420 const char * const clone_usage
[] = {
421 N_("scalar clone [<options>] [--] <repo> [<dir>]"),
425 char *enlistment
= NULL
, *dir
= NULL
;
426 struct strbuf buf
= STRBUF_INIT
;
429 argc
= parse_options(argc
, argv
, NULL
, clone_options
, clone_usage
, 0);
433 enlistment
= xstrdup(argv
[1]);
434 } else if (argc
== 1) {
437 strbuf_addstr(&buf
, url
);
438 /* Strip trailing slashes, if any */
439 while (buf
.len
> 0 && is_dir_sep(buf
.buf
[buf
.len
- 1]))
440 strbuf_setlen(&buf
, buf
.len
- 1);
441 /* Strip suffix `.git`, if any */
442 strbuf_strip_suffix(&buf
, ".git");
444 enlistment
= find_last_dir_sep(buf
.buf
);
446 die(_("cannot deduce worktree name from '%s'"), url
);
448 enlistment
= xstrdup(enlistment
+ 1);
450 usage_msg_opt(_("You must specify a repository to clone."),
451 clone_usage
, clone_options
);
454 if (is_directory(enlistment
))
455 die(_("directory '%s' exists already"), enlistment
);
457 dir
= xstrfmt("%s/src", enlistment
);
461 strbuf_addf(&buf
, "init.defaultBranch=%s", branch
);
463 char *b
= repo_default_branch_name(the_repository
, 1);
464 strbuf_addf(&buf
, "init.defaultBranch=%s", b
);
468 if ((res
= run_git("-c", buf
.buf
, "init", "--", dir
, NULL
)))
471 if (chdir(dir
) < 0) {
472 res
= error_errno(_("could not switch to '%s'"), dir
);
476 setup_git_directory();
478 /* common-main already logs `argv` */
479 trace2_def_repo(the_repository
);
481 if (!branch
&& !(branch
= remote_default_branch(url
))) {
482 res
= error(_("failed to get default branch for '%s'"), url
);
486 if (set_config("remote.origin.url=%s", url
) ||
487 set_config("remote.origin.fetch="
488 "+refs/heads/%s:refs/remotes/origin/%s",
489 single_branch
? branch
: "*",
490 single_branch
? branch
: "*") ||
491 set_config("remote.origin.promisor=true") ||
492 set_config("remote.origin.partialCloneFilter=blob:none")) {
493 res
= error(_("could not configure remote in '%s'"), dir
);
498 (res
= run_git("sparse-checkout", "init", "--cone", NULL
)))
501 if (set_recommended_config(0))
502 return error(_("could not configure '%s'"), dir
);
504 if ((res
= run_git("fetch", "--quiet",
505 show_progress
? "--progress" : "--no-progress",
507 warning(_("partial clone failed; attempting full clone"));
509 if (set_config("remote.origin.promisor") ||
510 set_config("remote.origin.partialCloneFilter")) {
511 res
= error(_("could not configure for full clone"));
515 if ((res
= run_git("fetch", "--quiet",
516 show_progress
? "--progress" : "--no-progress",
521 if ((res
= set_config("branch.%s.remote=origin", branch
)))
523 if ((res
= set_config("branch.%s.merge=refs/heads/%s",
528 strbuf_addf(&buf
, "origin/%s", branch
);
529 res
= run_git("checkout", "-f", "-t", buf
.buf
, NULL
);
533 res
= register_dir();
538 strbuf_release(&buf
);
542 static int cmd_diagnose(int argc
, const char **argv
)
544 struct option options
[] = {
547 const char * const usage
[] = {
548 N_("scalar diagnose [<enlistment>]"),
551 struct strbuf diagnostics_root
= STRBUF_INIT
;
554 argc
= parse_options(argc
, argv
, NULL
, options
,
557 setup_enlistment_directory(argc
, argv
, usage
, options
, &diagnostics_root
);
558 strbuf_addstr(&diagnostics_root
, "/.scalarDiagnostics");
560 res
= run_git("diagnose", "--mode=all", "-s", "%Y%m%d_%H%M%S",
561 "-o", diagnostics_root
.buf
, NULL
);
563 strbuf_release(&diagnostics_root
);
567 static int cmd_list(int argc
, const char **argv
)
570 die(_("`scalar list` does not take arguments"));
572 if (run_git("config", "--global", "--get-all", "scalar.repo", NULL
) < 0)
577 static int cmd_register(int argc
, const char **argv
)
579 struct option options
[] = {
582 const char * const usage
[] = {
583 N_("scalar register [<enlistment>]"),
587 argc
= parse_options(argc
, argv
, NULL
, options
,
590 setup_enlistment_directory(argc
, argv
, usage
, options
, NULL
);
592 return register_dir();
595 static int get_scalar_repos(const char *key
, const char *value
, void *data
)
597 struct string_list
*list
= data
;
599 if (!strcmp(key
, "scalar.repo"))
600 string_list_append(list
, value
);
605 static int remove_deleted_enlistment(struct strbuf
*path
)
608 strbuf_realpath_forgiving(path
, path
->buf
, 1);
610 if (run_git("config", "--global",
611 "--unset", "--fixed-value",
612 "scalar.repo", path
->buf
, NULL
) < 0)
615 if (run_git("config", "--global",
616 "--unset", "--fixed-value",
617 "maintenance.repo", path
->buf
, NULL
) < 0)
623 static int cmd_reconfigure(int argc
, const char **argv
)
626 struct option options
[] = {
627 OPT_BOOL('a', "all", &all
,
628 N_("reconfigure all registered enlistments")),
631 const char * const usage
[] = {
632 N_("scalar reconfigure [--all | <enlistment>]"),
635 struct string_list scalar_repos
= STRING_LIST_INIT_DUP
;
637 struct repository r
= { NULL
};
638 struct strbuf commondir
= STRBUF_INIT
, gitdir
= STRBUF_INIT
;
640 argc
= parse_options(argc
, argv
, NULL
, options
,
644 setup_enlistment_directory(argc
, argv
, usage
, options
, NULL
);
646 return set_recommended_config(1);
650 usage_msg_opt(_("--all or <enlistment>, but not both"),
653 git_config(get_scalar_repos
, &scalar_repos
);
655 for (i
= 0; i
< scalar_repos
.nr
; i
++) {
656 const char *dir
= scalar_repos
.items
[i
].string
;
658 strbuf_reset(&commondir
);
659 strbuf_reset(&gitdir
);
661 if (chdir(dir
) < 0) {
662 struct strbuf buf
= STRBUF_INIT
;
664 if (errno
!= ENOENT
) {
665 warning_errno(_("could not switch to '%s'"), dir
);
670 strbuf_addstr(&buf
, dir
);
671 if (remove_deleted_enlistment(&buf
))
672 res
= error(_("could not remove stale "
673 "scalar.repo '%s'"), dir
);
675 warning(_("removing stale scalar.repo '%s'"),
677 strbuf_release(&buf
);
678 } else if (discover_git_directory(&commondir
, &gitdir
) < 0) {
679 warning_errno(_("git repository gone in '%s'"), dir
);
685 r
.commondir
= commondir
.buf
;
686 r
.gitdir
= gitdir
.buf
;
688 if (set_recommended_config(1) < 0)
693 string_list_clear(&scalar_repos
, 1);
694 strbuf_release(&commondir
);
695 strbuf_release(&gitdir
);
700 static int cmd_run(int argc
, const char **argv
)
702 struct option options
[] = {
706 const char *arg
, *task
;
709 { "commit-graph", "commit-graph" },
710 { "fetch", "prefetch" },
711 { "loose-objects", "loose-objects" },
712 { "pack-files", "incremental-repack" },
715 struct strbuf buf
= STRBUF_INIT
;
716 const char *usagestr
[] = { NULL
, NULL
};
719 strbuf_addstr(&buf
, N_("scalar run <task> [<enlistment>]\nTasks:\n"));
720 for (i
= 0; tasks
[i
].arg
; i
++)
721 strbuf_addf(&buf
, "\t%s\n", tasks
[i
].arg
);
722 usagestr
[0] = buf
.buf
;
724 argc
= parse_options(argc
, argv
, NULL
, options
,
728 usage_with_options(usagestr
, options
);
730 if (!strcmp("all", argv
[0])) {
733 for (i
= 0; tasks
[i
].arg
&& strcmp(tasks
[i
].arg
, argv
[0]); i
++)
734 ; /* keep looking for the task */
736 if (i
> 0 && !tasks
[i
].arg
) {
737 error(_("no such task: '%s'"), argv
[0]);
738 usage_with_options(usagestr
, options
);
744 setup_enlistment_directory(argc
, argv
, usagestr
, options
, NULL
);
745 strbuf_release(&buf
);
748 return register_dir();
751 return run_git("maintenance", "run",
752 "--task", tasks
[i
].task
, NULL
);
756 for (i
= 1; tasks
[i
].arg
; i
++)
757 if (run_git("maintenance", "run",
758 "--task", tasks
[i
].task
, NULL
))
763 static int cmd_unregister(int argc
, const char **argv
)
765 struct option options
[] = {
768 const char * const usage
[] = {
769 N_("scalar unregister [<enlistment>]"),
773 argc
= parse_options(argc
, argv
, NULL
, options
,
777 * Be forgiving when the enlistment or worktree does not even exist any
778 * longer; This can be the case if a user deleted the worktree by
779 * mistake and _still_ wants to unregister the thing.
782 struct strbuf src_path
= STRBUF_INIT
, workdir_path
= STRBUF_INIT
;
784 strbuf_addf(&src_path
, "%s/src/.git", argv
[0]);
785 strbuf_addf(&workdir_path
, "%s/.git", argv
[0]);
786 if (!is_directory(src_path
.buf
) && !is_directory(workdir_path
.buf
)) {
787 /* remove possible matching registrations */
790 strbuf_strip_suffix(&src_path
, "/.git");
791 res
= remove_deleted_enlistment(&src_path
) && res
;
793 strbuf_strip_suffix(&workdir_path
, "/.git");
794 res
= remove_deleted_enlistment(&workdir_path
) && res
;
796 strbuf_release(&src_path
);
797 strbuf_release(&workdir_path
);
800 strbuf_release(&src_path
);
801 strbuf_release(&workdir_path
);
804 setup_enlistment_directory(argc
, argv
, usage
, options
, NULL
);
806 return unregister_dir();
809 static int cmd_delete(int argc
, const char **argv
)
811 char *cwd
= xgetcwd();
812 struct option options
[] = {
815 const char * const usage
[] = {
816 N_("scalar delete <enlistment>"),
819 struct strbuf enlistment
= STRBUF_INIT
;
822 argc
= parse_options(argc
, argv
, NULL
, options
,
826 usage_with_options(usage
, options
);
828 setup_enlistment_directory(argc
, argv
, usage
, options
, &enlistment
);
830 if (dir_inside_of(cwd
, enlistment
.buf
) >= 0)
831 res
= error(_("refusing to delete current working directory"));
833 close_object_store(the_repository
->objects
);
834 res
= delete_enlistment(&enlistment
);
836 strbuf_release(&enlistment
);
842 static int cmd_help(int argc
, const char **argv
)
844 struct option options
[] = {
847 const char * const usage
[] = {
852 argc
= parse_options(argc
, argv
, NULL
, options
,
856 usage_with_options(usage
, options
);
858 return run_git("help", "scalar", NULL
);
861 static int cmd_version(int argc
, const char **argv
)
863 int verbose
= 0, build_options
= 0;
864 struct option options
[] = {
865 OPT__VERBOSE(&verbose
, N_("include Git version")),
866 OPT_BOOL(0, "build-options", &build_options
,
867 N_("include Git's build options")),
870 const char * const usage
[] = {
871 N_("scalar verbose [-v | --verbose] [--build-options]"),
874 struct strbuf buf
= STRBUF_INIT
;
876 argc
= parse_options(argc
, argv
, NULL
, options
,
880 usage_with_options(usage
, options
);
882 get_version_info(&buf
, build_options
);
883 fprintf(stderr
, "%s\n", buf
.buf
);
884 strbuf_release(&buf
);
891 int (*fn
)(int, const char **);
893 { "clone", cmd_clone
},
894 { "list", cmd_list
},
895 { "register", cmd_register
},
896 { "unregister", cmd_unregister
},
898 { "reconfigure", cmd_reconfigure
},
899 { "delete", cmd_delete
},
900 { "help", cmd_help
},
901 { "version", cmd_version
},
902 { "diagnose", cmd_diagnose
},
906 int cmd_main(int argc
, const char **argv
)
908 struct strbuf scalar_usage
= STRBUF_INIT
;
911 while (argc
> 1 && *argv
[1] == '-') {
912 if (!strcmp(argv
[1], "-C")) {
914 die(_("-C requires a <directory>"));
915 if (chdir(argv
[2]) < 0)
916 die_errno(_("could not change to '%s'"),
920 } else if (!strcmp(argv
[1], "-c")) {
922 die(_("-c requires a <key>=<value> argument"));
923 git_config_push_parameter(argv
[2]);
934 for (i
= 0; builtins
[i
].name
; i
++)
935 if (!strcmp(builtins
[i
].name
, argv
[0]))
936 return !!builtins
[i
].fn(argc
, argv
);
939 strbuf_addstr(&scalar_usage
,
940 N_("scalar [-C <directory>] [-c <key>=<value>] "
941 "<command> [<options>]\n\nCommands:\n"));
942 for (i
= 0; builtins
[i
].name
; i
++)
943 strbuf_addf(&scalar_usage
, "\t%s\n", builtins
[i
].name
);
945 usage(scalar_usage
.buf
);