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 child_process cmd
= CHILD_PROCESS_INIT
;
77 strvec_push(&cmd
.args
, arg
);
78 while ((p
= va_arg(args
, const char *)))
79 strvec_push(&cmd
.args
, p
);
83 return run_command(&cmd
);
86 struct scalar_config
{
89 int overwrite_on_reconfigure
;
92 static int set_scalar_config(const struct scalar_config
*config
, int reconfigure
)
97 if ((reconfigure
&& config
->overwrite_on_reconfigure
) ||
98 git_config_get_string(config
->key
, &value
)) {
99 trace2_data_string("scalar", the_repository
, config
->key
, "created");
100 res
= git_config_set_gently(config
->key
, config
->value
);
102 trace2_data_string("scalar", the_repository
, config
->key
, "exists");
110 static int have_fsmonitor_support(void)
112 return fsmonitor_ipc__is_supported() &&
113 fsm_settings__get_reason(the_repository
) == FSMONITOR_REASON_OK
;
116 static int set_recommended_config(int reconfigure
)
118 struct scalar_config config
[] = {
120 { "am.keepCR", "true", 1 },
121 { "core.FSCache", "true", 1 },
122 { "core.multiPackIndex", "true", 1 },
123 { "core.preloadIndex", "true", 1 },
125 { "core.untrackedCache", "true", 1 },
128 * Unfortunately, Scalar's Functional Tests demonstrated
129 * that the untracked cache feature is unreliable on Windows
130 * (which is a bummer because that platform would benefit the
131 * most from it). For some reason, freshly created files seem
132 * not to update the directory's `lastModified` time
133 * immediately, but the untracked cache would need to rely on
136 * Therefore, with a sad heart, we disable this very useful
137 * feature on Windows.
139 { "core.untrackedCache", "false", 1 },
141 { "core.logAllRefUpdates", "true", 1 },
142 { "credential.https://dev.azure.com.useHttpPath", "true", 1 },
143 { "credential.validate", "false", 1 }, /* GCM4W-only */
144 { "gc.auto", "0", 1 },
145 { "gui.GCWarning", "false", 1 },
146 { "index.skipHash", "false", 1 },
147 { "index.threads", "true", 1 },
148 { "index.version", "4", 1 },
149 { "merge.stat", "false", 1 },
150 { "merge.renames", "true", 1 },
151 { "pack.useBitmaps", "false", 1 },
152 { "pack.useSparse", "true", 1 },
153 { "receive.autoGC", "false", 1 },
154 { "feature.manyFiles", "false", 1 },
155 { "feature.experimental", "false", 1 },
156 { "fetch.unpackLimit", "1", 1 },
157 { "fetch.writeCommitGraph", "false", 1 },
159 { "http.sslBackend", "schannel", 1 },
162 { "status.aheadBehind", "false" },
163 { "commitGraph.generationVersion", "1" },
164 { "core.autoCRLF", "false" },
165 { "core.safeCRLF", "false" },
166 { "fetch.showForcedUpdates", "false" },
172 for (i
= 0; config
[i
].key
; i
++) {
173 if (set_scalar_config(config
+ i
, reconfigure
))
174 return error(_("could not configure %s=%s"),
175 config
[i
].key
, config
[i
].value
);
178 if (have_fsmonitor_support()) {
179 struct scalar_config fsmonitor
= { "core.fsmonitor", "true" };
180 if (set_scalar_config(&fsmonitor
, reconfigure
))
181 return error(_("could not configure %s=%s"),
182 fsmonitor
.key
, fsmonitor
.value
);
186 * The `log.excludeDecoration` setting is special because it allows
187 * for multiple values.
189 if (git_config_get_string("log.excludeDecoration", &value
)) {
190 trace2_data_string("scalar", the_repository
,
191 "log.excludeDecoration", "created");
192 if (git_config_set_multivar_gently("log.excludeDecoration",
194 CONFIG_REGEX_NONE
, 0))
195 return error(_("could not configure "
196 "log.excludeDecoration"));
198 trace2_data_string("scalar", the_repository
,
199 "log.excludeDecoration", "exists");
206 static int toggle_maintenance(int enable
)
208 return run_git("maintenance",
209 enable
? "start" : "unregister",
210 enable
? NULL
: "--force",
214 static int add_or_remove_enlistment(int add
)
218 if (!the_repository
->worktree
)
219 die(_("Scalar enlistments require a worktree"));
221 res
= run_git("config", "--global", "--get", "--fixed-value",
222 "scalar.repo", the_repository
->worktree
, NULL
);
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.
228 if ((add
&& !res
) || (!add
&& res
))
231 return run_git("config", "--global", add
? "--add" : "--unset",
232 add
? "--no-fixed-value" : "--fixed-value",
233 "scalar.repo", the_repository
->worktree
, NULL
);
236 static int start_fsmonitor_daemon(void)
238 assert(have_fsmonitor_support());
240 if (fsmonitor_ipc__get_state() != IPC_STATE__LISTENING
)
241 return run_git("fsmonitor--daemon", "start", NULL
);
246 static int stop_fsmonitor_daemon(void)
248 assert(have_fsmonitor_support());
250 if (fsmonitor_ipc__get_state() == IPC_STATE__LISTENING
)
251 return run_git("fsmonitor--daemon", "stop", NULL
);
256 static int register_dir(void)
258 if (add_or_remove_enlistment(1))
259 return error(_("could not add enlistment"));
261 if (set_recommended_config(0))
262 return error(_("could not set recommended config"));
264 if (toggle_maintenance(1))
265 warning(_("could not turn on maintenance"));
267 if (have_fsmonitor_support() && start_fsmonitor_daemon()) {
268 return error(_("could not start the FSMonitor daemon"));
274 static int unregister_dir(void)
278 if (toggle_maintenance(0))
279 res
= error(_("could not turn off maintenance"));
281 if (add_or_remove_enlistment(0))
282 res
= error(_("could not remove enlistment"));
287 /* printf-style interface, expects `<key>=<value>` argument */
288 static int set_config(const char *fmt
, ...)
290 struct strbuf buf
= STRBUF_INIT
;
296 strbuf_vaddf(&buf
, fmt
, args
);
299 value
= strchr(buf
.buf
, '=');
302 res
= git_config_set_gently(buf
.buf
, value
);
303 strbuf_release(&buf
);
308 static char *remote_default_branch(const char *url
)
310 struct child_process cp
= CHILD_PROCESS_INIT
;
311 struct strbuf out
= STRBUF_INIT
;
314 strvec_pushl(&cp
.args
, "ls-remote", "--symref", url
, "HEAD", NULL
);
315 if (!pipe_command(&cp
, NULL
, 0, &out
, 0, NULL
, 0)) {
316 const char *line
= out
.buf
;
319 const char *eol
= strchrnul(line
, '\n'), *p
;
320 size_t len
= eol
- line
;
323 if (!skip_prefix(line
, "ref: ", &p
) ||
324 !strip_suffix_mem(line
, &len
, "\tHEAD")) {
325 line
= eol
+ (*eol
== '\n');
330 if (skip_prefix(p
, "refs/heads/", &p
)) {
331 branch
= xstrndup(p
, eol
- p
);
332 strbuf_release(&out
);
336 error(_("remote HEAD is not a branch: '%.*s'"),
338 strbuf_release(&out
);
342 warning(_("failed to get default branch name from remote; "
343 "using local default"));
346 child_process_init(&cp
);
348 strvec_pushl(&cp
.args
, "symbolic-ref", "--short", "HEAD", NULL
);
349 if (!pipe_command(&cp
, NULL
, 0, &out
, 0, NULL
, 0)) {
351 return strbuf_detach(&out
, NULL
);
354 strbuf_release(&out
);
355 error(_("failed to get default branch name"));
359 static int delete_enlistment(struct strbuf
*enlistment
)
362 struct strbuf parent
= STRBUF_INIT
;
367 if (unregister_dir())
368 return error(_("failed to unregister repository"));
372 * Change the current directory to one outside of the enlistment so
373 * that we may delete everything underneath it.
375 offset
= offset_1st_component(enlistment
->buf
);
376 path_sep
= find_last_dir_sep(enlistment
->buf
+ offset
);
377 strbuf_add(&parent
, enlistment
->buf
,
378 path_sep
? path_sep
- enlistment
->buf
: offset
);
379 if (chdir(parent
.buf
) < 0) {
380 int res
= error_errno(_("could not switch to '%s'"), parent
.buf
);
381 strbuf_release(&parent
);
384 strbuf_release(&parent
);
387 if (have_fsmonitor_support() && stop_fsmonitor_daemon())
388 return error(_("failed to stop the FSMonitor daemon"));
390 if (remove_dir_recursively(enlistment
, 0))
391 return error(_("failed to delete enlistment directory"));
397 * Dummy implementation; Using `get_version_info()` would cause a link error
400 void load_builtin_commands(const char *prefix
, struct cmdnames
*cmds
)
402 die("not implemented");
405 static int cmd_clone(int argc
, const char **argv
)
407 const char *branch
= NULL
;
408 int full_clone
= 0, single_branch
= 0, show_progress
= isatty(2);
409 struct option clone_options
[] = {
410 OPT_STRING('b', "branch", &branch
, N_("<branch>"),
411 N_("branch to checkout after clone")),
412 OPT_BOOL(0, "full-clone", &full_clone
,
413 N_("when cloning, create full working directory")),
414 OPT_BOOL(0, "single-branch", &single_branch
,
415 N_("only download metadata for the branch that will "
419 const char * const clone_usage
[] = {
420 N_("scalar clone [<options>] [--] <repo> [<dir>]"),
424 char *enlistment
= NULL
, *dir
= NULL
;
425 struct strbuf buf
= STRBUF_INIT
;
428 argc
= parse_options(argc
, argv
, NULL
, clone_options
, clone_usage
, 0);
432 enlistment
= xstrdup(argv
[1]);
433 } else if (argc
== 1) {
436 strbuf_addstr(&buf
, url
);
437 /* Strip trailing slashes, if any */
438 while (buf
.len
> 0 && is_dir_sep(buf
.buf
[buf
.len
- 1]))
439 strbuf_setlen(&buf
, buf
.len
- 1);
440 /* Strip suffix `.git`, if any */
441 strbuf_strip_suffix(&buf
, ".git");
443 enlistment
= find_last_dir_sep(buf
.buf
);
445 die(_("cannot deduce worktree name from '%s'"), url
);
447 enlistment
= xstrdup(enlistment
+ 1);
449 usage_msg_opt(_("You must specify a repository to clone."),
450 clone_usage
, clone_options
);
453 if (is_directory(enlistment
))
454 die(_("directory '%s' exists already"), enlistment
);
456 dir
= xstrfmt("%s/src", enlistment
);
460 strbuf_addf(&buf
, "init.defaultBranch=%s", branch
);
462 char *b
= repo_default_branch_name(the_repository
, 1);
463 strbuf_addf(&buf
, "init.defaultBranch=%s", b
);
467 if ((res
= run_git("-c", buf
.buf
, "init", "--", dir
, NULL
)))
470 if (chdir(dir
) < 0) {
471 res
= error_errno(_("could not switch to '%s'"), dir
);
475 setup_git_directory();
477 /* common-main already logs `argv` */
478 trace2_def_repo(the_repository
);
480 if (!branch
&& !(branch
= remote_default_branch(url
))) {
481 res
= error(_("failed to get default branch for '%s'"), url
);
485 if (set_config("remote.origin.url=%s", url
) ||
486 set_config("remote.origin.fetch="
487 "+refs/heads/%s:refs/remotes/origin/%s",
488 single_branch
? branch
: "*",
489 single_branch
? branch
: "*") ||
490 set_config("remote.origin.promisor=true") ||
491 set_config("remote.origin.partialCloneFilter=blob:none")) {
492 res
= error(_("could not configure remote in '%s'"), dir
);
497 (res
= run_git("sparse-checkout", "init", "--cone", NULL
)))
500 if (set_recommended_config(0))
501 return error(_("could not configure '%s'"), dir
);
503 if ((res
= run_git("fetch", "--quiet",
504 show_progress
? "--progress" : "--no-progress",
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",
515 show_progress
? "--progress" : "--no-progress",
520 if ((res
= set_config("branch.%s.remote=origin", branch
)))
522 if ((res
= set_config("branch.%s.merge=refs/heads/%s",
527 strbuf_addf(&buf
, "origin/%s", branch
);
528 res
= run_git("checkout", "-f", "-t", buf
.buf
, NULL
);
532 res
= register_dir();
537 strbuf_release(&buf
);
541 static int cmd_diagnose(int argc
, const char **argv
)
543 struct option options
[] = {
546 const char * const usage
[] = {
547 N_("scalar diagnose [<enlistment>]"),
550 struct strbuf diagnostics_root
= STRBUF_INIT
;
553 argc
= parse_options(argc
, argv
, NULL
, options
,
556 setup_enlistment_directory(argc
, argv
, usage
, options
, &diagnostics_root
);
557 strbuf_addstr(&diagnostics_root
, "/.scalarDiagnostics");
559 res
= run_git("diagnose", "--mode=all", "-s", "%Y%m%d_%H%M%S",
560 "-o", diagnostics_root
.buf
, NULL
);
562 strbuf_release(&diagnostics_root
);
566 static int cmd_list(int argc
, const char **argv
)
569 die(_("`scalar list` does not take arguments"));
571 if (run_git("config", "--global", "--get-all", "scalar.repo", NULL
) < 0)
576 static int cmd_register(int argc
, const char **argv
)
578 struct option options
[] = {
581 const char * const usage
[] = {
582 N_("scalar register [<enlistment>]"),
586 argc
= parse_options(argc
, argv
, NULL
, options
,
589 setup_enlistment_directory(argc
, argv
, usage
, options
, NULL
);
591 return register_dir();
594 static int get_scalar_repos(const char *key
, const char *value
, void *data
)
596 struct string_list
*list
= data
;
598 if (!strcmp(key
, "scalar.repo"))
599 string_list_append(list
, value
);
604 static int remove_deleted_enlistment(struct strbuf
*path
)
607 strbuf_realpath_forgiving(path
, path
->buf
, 1);
609 if (run_git("config", "--global",
610 "--unset", "--fixed-value",
611 "scalar.repo", path
->buf
, NULL
) < 0)
614 if (run_git("config", "--global",
615 "--unset", "--fixed-value",
616 "maintenance.repo", path
->buf
, NULL
) < 0)
622 static int cmd_reconfigure(int argc
, const char **argv
)
625 struct option options
[] = {
626 OPT_BOOL('a', "all", &all
,
627 N_("reconfigure all registered enlistments")),
630 const char * const usage
[] = {
631 N_("scalar reconfigure [--all | <enlistment>]"),
634 struct string_list scalar_repos
= STRING_LIST_INIT_DUP
;
636 struct repository r
= { NULL
};
637 struct strbuf commondir
= STRBUF_INIT
, gitdir
= STRBUF_INIT
;
639 argc
= parse_options(argc
, argv
, NULL
, options
,
643 setup_enlistment_directory(argc
, argv
, usage
, options
, NULL
);
645 return set_recommended_config(1);
649 usage_msg_opt(_("--all or <enlistment>, but not both"),
652 git_config(get_scalar_repos
, &scalar_repos
);
654 for (i
= 0; i
< scalar_repos
.nr
; i
++) {
655 const char *dir
= scalar_repos
.items
[i
].string
;
657 strbuf_reset(&commondir
);
658 strbuf_reset(&gitdir
);
660 if (chdir(dir
) < 0) {
661 struct strbuf buf
= STRBUF_INIT
;
663 if (errno
!= ENOENT
) {
664 warning_errno(_("could not switch to '%s'"), dir
);
669 strbuf_addstr(&buf
, dir
);
670 if (remove_deleted_enlistment(&buf
))
671 res
= error(_("could not remove stale "
672 "scalar.repo '%s'"), dir
);
674 warning(_("removing stale scalar.repo '%s'"),
676 strbuf_release(&buf
);
677 } else if (discover_git_directory(&commondir
, &gitdir
) < 0) {
678 warning_errno(_("git repository gone in '%s'"), dir
);
684 r
.commondir
= commondir
.buf
;
685 r
.gitdir
= gitdir
.buf
;
687 if (set_recommended_config(1) < 0)
692 string_list_clear(&scalar_repos
, 1);
693 strbuf_release(&commondir
);
694 strbuf_release(&gitdir
);
699 static int cmd_run(int argc
, const char **argv
)
701 struct option options
[] = {
705 const char *arg
, *task
;
708 { "commit-graph", "commit-graph" },
709 { "fetch", "prefetch" },
710 { "loose-objects", "loose-objects" },
711 { "pack-files", "incremental-repack" },
714 struct strbuf buf
= STRBUF_INIT
;
715 const char *usagestr
[] = { NULL
, NULL
};
718 strbuf_addstr(&buf
, N_("scalar run <task> [<enlistment>]\nTasks:\n"));
719 for (i
= 0; tasks
[i
].arg
; i
++)
720 strbuf_addf(&buf
, "\t%s\n", tasks
[i
].arg
);
721 usagestr
[0] = buf
.buf
;
723 argc
= parse_options(argc
, argv
, NULL
, options
,
727 usage_with_options(usagestr
, options
);
729 if (!strcmp("all", argv
[0])) {
732 for (i
= 0; tasks
[i
].arg
&& strcmp(tasks
[i
].arg
, argv
[0]); i
++)
733 ; /* keep looking for the task */
735 if (i
> 0 && !tasks
[i
].arg
) {
736 error(_("no such task: '%s'"), argv
[0]);
737 usage_with_options(usagestr
, options
);
743 setup_enlistment_directory(argc
, argv
, usagestr
, options
, NULL
);
744 strbuf_release(&buf
);
747 return register_dir();
750 return run_git("maintenance", "run",
751 "--task", tasks
[i
].task
, NULL
);
755 for (i
= 1; tasks
[i
].arg
; i
++)
756 if (run_git("maintenance", "run",
757 "--task", tasks
[i
].task
, NULL
))
762 static int cmd_unregister(int argc
, const char **argv
)
764 struct option options
[] = {
767 const char * const usage
[] = {
768 N_("scalar unregister [<enlistment>]"),
772 argc
= parse_options(argc
, argv
, NULL
, options
,
776 * Be forgiving when the enlistment or worktree does not even exist any
777 * longer; This can be the case if a user deleted the worktree by
778 * mistake and _still_ wants to unregister the thing.
781 struct strbuf src_path
= STRBUF_INIT
, workdir_path
= STRBUF_INIT
;
783 strbuf_addf(&src_path
, "%s/src/.git", argv
[0]);
784 strbuf_addf(&workdir_path
, "%s/.git", argv
[0]);
785 if (!is_directory(src_path
.buf
) && !is_directory(workdir_path
.buf
)) {
786 /* remove possible matching registrations */
789 strbuf_strip_suffix(&src_path
, "/.git");
790 res
= remove_deleted_enlistment(&src_path
) && res
;
792 strbuf_strip_suffix(&workdir_path
, "/.git");
793 res
= remove_deleted_enlistment(&workdir_path
) && res
;
795 strbuf_release(&src_path
);
796 strbuf_release(&workdir_path
);
799 strbuf_release(&src_path
);
800 strbuf_release(&workdir_path
);
803 setup_enlistment_directory(argc
, argv
, usage
, options
, NULL
);
805 return unregister_dir();
808 static int cmd_delete(int argc
, const char **argv
)
810 char *cwd
= xgetcwd();
811 struct option options
[] = {
814 const char * const usage
[] = {
815 N_("scalar delete <enlistment>"),
818 struct strbuf enlistment
= STRBUF_INIT
;
821 argc
= parse_options(argc
, argv
, NULL
, options
,
825 usage_with_options(usage
, options
);
827 setup_enlistment_directory(argc
, argv
, usage
, options
, &enlistment
);
829 if (dir_inside_of(cwd
, enlistment
.buf
) >= 0)
830 res
= error(_("refusing to delete current working directory"));
832 close_object_store(the_repository
->objects
);
833 res
= delete_enlistment(&enlistment
);
835 strbuf_release(&enlistment
);
841 static int cmd_help(int argc
, const char **argv
)
843 struct option options
[] = {
846 const char * const usage
[] = {
851 argc
= parse_options(argc
, argv
, NULL
, options
,
855 usage_with_options(usage
, options
);
857 return run_git("help", "scalar", NULL
);
860 static int cmd_version(int argc
, const char **argv
)
862 int verbose
= 0, build_options
= 0;
863 struct option options
[] = {
864 OPT__VERBOSE(&verbose
, N_("include Git version")),
865 OPT_BOOL(0, "build-options", &build_options
,
866 N_("include Git's build options")),
869 const char * const usage
[] = {
870 N_("scalar verbose [-v | --verbose] [--build-options]"),
873 struct strbuf buf
= STRBUF_INIT
;
875 argc
= parse_options(argc
, argv
, NULL
, options
,
879 usage_with_options(usage
, options
);
881 get_version_info(&buf
, build_options
);
882 fprintf(stderr
, "%s\n", buf
.buf
);
883 strbuf_release(&buf
);
890 int (*fn
)(int, const char **);
892 { "clone", cmd_clone
},
893 { "list", cmd_list
},
894 { "register", cmd_register
},
895 { "unregister", cmd_unregister
},
897 { "reconfigure", cmd_reconfigure
},
898 { "delete", cmd_delete
},
899 { "help", cmd_help
},
900 { "version", cmd_version
},
901 { "diagnose", cmd_diagnose
},
905 int cmd_main(int argc
, const char **argv
)
907 struct strbuf scalar_usage
= STRBUF_INIT
;
910 while (argc
> 1 && *argv
[1] == '-') {
911 if (!strcmp(argv
[1], "-C")) {
913 die(_("-C requires a <directory>"));
914 if (chdir(argv
[2]) < 0)
915 die_errno(_("could not change to '%s'"),
919 } else if (!strcmp(argv
[1], "-c")) {
921 die(_("-c requires a <key>=<value> argument"));
922 git_config_push_parameter(argv
[2]);
933 for (i
= 0; builtins
[i
].name
; i
++)
934 if (!strcmp(builtins
[i
].name
, argv
[0]))
935 return !!builtins
[i
].fn(argc
, argv
);
938 strbuf_addstr(&scalar_usage
,
939 N_("scalar [-C <directory>] [-c <key>=<value>] "
940 "<command> [<options>]\n\nCommands:\n"));
941 for (i
= 0; builtins
[i
].name
; i
++)
942 strbuf_addf(&scalar_usage
, "\t%s\n", builtins
[i
].name
);
944 usage(scalar_usage
.buf
);