2 * The Scalar command-line interface.
5 #include "git-compat-util.h"
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"
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;
30 if (startup_info
->have_repository
)
31 BUG("gitdir already set up?!?");
34 usage_with_options(usagestr
, options
);
36 /* find the worktree, determine its corresponding root */
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 */
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
);
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
;
80 strvec_push(&cmd
.args
, arg
);
81 while ((p
= va_arg(args
, const char *)))
82 strvec_push(&cmd
.args
, p
);
86 return run_command(&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.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 },
162 { "http.sslBackend", "schannel", 1 },
165 { "status.aheadBehind", "false" },
166 { "commitGraph.generationVersion", "1" },
167 { "core.autoCRLF", "false" },
168 { "core.safeCRLF", "false" },
169 { "fetch.showForcedUpdates", "false" },
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",
197 CONFIG_REGEX_NONE
, 0))
198 return error(_("could not configure "
199 "log.excludeDecoration"));
201 trace2_data_string("scalar", the_repository
,
202 "log.excludeDecoration", "exists");
209 static int toggle_maintenance(int enable
)
211 return run_git("maintenance",
212 enable
? "start" : "unregister",
213 enable
? NULL
: "--force",
217 static int add_or_remove_enlistment(int add
)
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
))
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
);
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
);
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"));
277 static int unregister_dir(void)
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"));
290 /* printf-style interface, expects `<key>=<value>` argument */
291 static int set_config(const char *fmt
, ...)
293 struct strbuf buf
= STRBUF_INIT
;
299 strbuf_vaddf(&buf
, fmt
, args
);
302 value
= strchr(buf
.buf
, '=');
305 res
= git_config_set_gently(buf
.buf
, value
);
306 strbuf_release(&buf
);
311 static char *remote_default_branch(const char *url
)
313 struct child_process cp
= CHILD_PROCESS_INIT
;
314 struct strbuf out
= STRBUF_INIT
;
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
;
322 const char *eol
= strchrnul(line
, '\n'), *p
;
323 size_t len
= eol
- line
;
326 if (!skip_prefix(line
, "ref: ", &p
) ||
327 !strip_suffix_mem(line
, &len
, "\tHEAD")) {
328 line
= eol
+ (*eol
== '\n');
333 if (skip_prefix(p
, "refs/heads/", &p
)) {
334 branch
= xstrndup(p
, eol
- p
);
335 strbuf_release(&out
);
339 error(_("remote HEAD is not a branch: '%.*s'"),
341 strbuf_release(&out
);
345 warning(_("failed to get default branch name from remote; "
346 "using local default"));
349 child_process_init(&cp
);
351 strvec_pushl(&cp
.args
, "symbolic-ref", "--short", "HEAD", NULL
);
352 if (!pipe_command(&cp
, NULL
, 0, &out
, 0, NULL
, 0)) {
354 return strbuf_detach(&out
, NULL
);
357 strbuf_release(&out
);
358 error(_("failed to get default branch name"));
362 static int delete_enlistment(struct strbuf
*enlistment
)
365 struct strbuf parent
= STRBUF_INIT
;
370 if (unregister_dir())
371 return error(_("failed to unregister repository"));
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
);
387 strbuf_release(&parent
);
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"));
400 * Dummy implementation; Using `get_version_info()` would cause a link error
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 struct option clone_options
[] = {
413 OPT_STRING('b', "branch", &branch
, N_("<branch>"),
414 N_("branch to checkout after clone")),
415 OPT_BOOL(0, "full-clone", &full_clone
,
416 N_("when cloning, create full working directory")),
417 OPT_BOOL(0, "single-branch", &single_branch
,
418 N_("only download metadata for the branch that will "
422 const char * const clone_usage
[] = {
423 N_("scalar clone [<options>] [--] <repo> [<dir>]"),
427 char *enlistment
= NULL
, *dir
= NULL
;
428 struct strbuf buf
= STRBUF_INIT
;
431 argc
= parse_options(argc
, argv
, NULL
, clone_options
, clone_usage
, 0);
435 enlistment
= xstrdup(argv
[1]);
436 } else if (argc
== 1) {
439 strbuf_addstr(&buf
, url
);
440 /* Strip trailing slashes, if any */
441 while (buf
.len
> 0 && is_dir_sep(buf
.buf
[buf
.len
- 1]))
442 strbuf_setlen(&buf
, buf
.len
- 1);
443 /* Strip suffix `.git`, if any */
444 strbuf_strip_suffix(&buf
, ".git");
446 enlistment
= find_last_dir_sep(buf
.buf
);
448 die(_("cannot deduce worktree name from '%s'"), url
);
450 enlistment
= xstrdup(enlistment
+ 1);
452 usage_msg_opt(_("You must specify a repository to clone."),
453 clone_usage
, clone_options
);
456 if (is_directory(enlistment
))
457 die(_("directory '%s' exists already"), enlistment
);
459 dir
= xstrfmt("%s/src", enlistment
);
463 strbuf_addf(&buf
, "init.defaultBranch=%s", branch
);
465 char *b
= repo_default_branch_name(the_repository
, 1);
466 strbuf_addf(&buf
, "init.defaultBranch=%s", b
);
470 if ((res
= run_git("-c", buf
.buf
, "init", "--", dir
, NULL
)))
473 if (chdir(dir
) < 0) {
474 res
= error_errno(_("could not switch to '%s'"), dir
);
478 setup_git_directory();
480 /* common-main already logs `argv` */
481 trace2_def_repo(the_repository
);
483 if (!branch
&& !(branch
= remote_default_branch(url
))) {
484 res
= error(_("failed to get default branch for '%s'"), url
);
488 if (set_config("remote.origin.url=%s", url
) ||
489 set_config("remote.origin.fetch="
490 "+refs/heads/%s:refs/remotes/origin/%s",
491 single_branch
? branch
: "*",
492 single_branch
? branch
: "*") ||
493 set_config("remote.origin.promisor=true") ||
494 set_config("remote.origin.partialCloneFilter=blob:none")) {
495 res
= error(_("could not configure remote in '%s'"), dir
);
500 (res
= run_git("sparse-checkout", "init", "--cone", NULL
)))
503 if (set_recommended_config(0))
504 return error(_("could not configure '%s'"), dir
);
506 if ((res
= run_git("fetch", "--quiet",
507 show_progress
? "--progress" : "--no-progress",
509 warning(_("partial clone failed; attempting full clone"));
511 if (set_config("remote.origin.promisor") ||
512 set_config("remote.origin.partialCloneFilter")) {
513 res
= error(_("could not configure for full clone"));
517 if ((res
= run_git("fetch", "--quiet",
518 show_progress
? "--progress" : "--no-progress",
523 if ((res
= set_config("branch.%s.remote=origin", branch
)))
525 if ((res
= set_config("branch.%s.merge=refs/heads/%s",
530 strbuf_addf(&buf
, "origin/%s", branch
);
531 res
= run_git("checkout", "-f", "-t", buf
.buf
, NULL
);
535 res
= register_dir();
540 strbuf_release(&buf
);
544 static int cmd_diagnose(int argc
, const char **argv
)
546 struct option options
[] = {
549 const char * const usage
[] = {
550 N_("scalar diagnose [<enlistment>]"),
553 struct strbuf diagnostics_root
= STRBUF_INIT
;
556 argc
= parse_options(argc
, argv
, NULL
, options
,
559 setup_enlistment_directory(argc
, argv
, usage
, options
, &diagnostics_root
);
560 strbuf_addstr(&diagnostics_root
, "/.scalarDiagnostics");
562 res
= run_git("diagnose", "--mode=all", "-s", "%Y%m%d_%H%M%S",
563 "-o", diagnostics_root
.buf
, NULL
);
565 strbuf_release(&diagnostics_root
);
569 static int cmd_list(int argc
, const char **argv UNUSED
)
572 die(_("`scalar list` does not take arguments"));
574 if (run_git("config", "--global", "--get-all", "scalar.repo", NULL
) < 0)
579 static int cmd_register(int argc
, const char **argv
)
581 struct option options
[] = {
584 const char * const usage
[] = {
585 N_("scalar register [<enlistment>]"),
589 argc
= parse_options(argc
, argv
, NULL
, options
,
592 setup_enlistment_directory(argc
, argv
, usage
, options
, NULL
);
594 return register_dir();
597 static int get_scalar_repos(const char *key
, const char *value
,
598 const struct config_context
*ctx UNUSED
,
601 struct string_list
*list
= data
;
603 if (!strcmp(key
, "scalar.repo"))
604 string_list_append(list
, value
);
609 static int remove_deleted_enlistment(struct strbuf
*path
)
612 strbuf_realpath_forgiving(path
, path
->buf
, 1);
614 if (run_git("config", "--global",
615 "--unset", "--fixed-value",
616 "scalar.repo", path
->buf
, NULL
) < 0)
619 if (run_git("config", "--global",
620 "--unset", "--fixed-value",
621 "maintenance.repo", path
->buf
, NULL
) < 0)
627 static int cmd_reconfigure(int argc
, const char **argv
)
630 struct option options
[] = {
631 OPT_BOOL('a', "all", &all
,
632 N_("reconfigure all registered enlistments")),
635 const char * const usage
[] = {
636 N_("scalar reconfigure [--all | <enlistment>]"),
639 struct string_list scalar_repos
= STRING_LIST_INIT_DUP
;
641 struct repository r
= { NULL
};
642 struct strbuf commondir
= STRBUF_INIT
, gitdir
= STRBUF_INIT
;
644 argc
= parse_options(argc
, argv
, NULL
, options
,
648 setup_enlistment_directory(argc
, argv
, usage
, options
, NULL
);
650 return set_recommended_config(1);
654 usage_msg_opt(_("--all or <enlistment>, but not both"),
657 git_config(get_scalar_repos
, &scalar_repos
);
659 for (i
= 0; i
< scalar_repos
.nr
; i
++) {
660 const char *dir
= scalar_repos
.items
[i
].string
;
662 strbuf_reset(&commondir
);
663 strbuf_reset(&gitdir
);
665 if (chdir(dir
) < 0) {
666 struct strbuf buf
= STRBUF_INIT
;
668 if (errno
!= ENOENT
) {
669 warning_errno(_("could not switch to '%s'"), dir
);
674 strbuf_addstr(&buf
, dir
);
675 if (remove_deleted_enlistment(&buf
))
676 res
= error(_("could not remove stale "
677 "scalar.repo '%s'"), dir
);
679 warning(_("removing stale scalar.repo '%s'"),
681 strbuf_release(&buf
);
682 } else if (discover_git_directory(&commondir
, &gitdir
) < 0) {
683 warning_errno(_("git repository gone in '%s'"), dir
);
689 r
.commondir
= commondir
.buf
;
690 r
.gitdir
= gitdir
.buf
;
692 if (set_recommended_config(1) < 0)
697 string_list_clear(&scalar_repos
, 1);
698 strbuf_release(&commondir
);
699 strbuf_release(&gitdir
);
704 static int cmd_run(int argc
, const char **argv
)
706 struct option options
[] = {
710 const char *arg
, *task
;
713 { "commit-graph", "commit-graph" },
714 { "fetch", "prefetch" },
715 { "loose-objects", "loose-objects" },
716 { "pack-files", "incremental-repack" },
719 struct strbuf buf
= STRBUF_INIT
;
720 const char *usagestr
[] = { NULL
, NULL
};
723 strbuf_addstr(&buf
, N_("scalar run <task> [<enlistment>]\nTasks:\n"));
724 for (i
= 0; tasks
[i
].arg
; i
++)
725 strbuf_addf(&buf
, "\t%s\n", tasks
[i
].arg
);
726 usagestr
[0] = buf
.buf
;
728 argc
= parse_options(argc
, argv
, NULL
, options
,
732 usage_with_options(usagestr
, options
);
734 if (!strcmp("all", argv
[0])) {
737 for (i
= 0; tasks
[i
].arg
&& strcmp(tasks
[i
].arg
, argv
[0]); i
++)
738 ; /* keep looking for the task */
740 if (i
> 0 && !tasks
[i
].arg
) {
741 error(_("no such task: '%s'"), argv
[0]);
742 usage_with_options(usagestr
, options
);
748 setup_enlistment_directory(argc
, argv
, usagestr
, options
, NULL
);
749 strbuf_release(&buf
);
752 return register_dir();
755 return run_git("maintenance", "run",
756 "--task", tasks
[i
].task
, NULL
);
760 for (i
= 1; tasks
[i
].arg
; i
++)
761 if (run_git("maintenance", "run",
762 "--task", tasks
[i
].task
, NULL
))
767 static int cmd_unregister(int argc
, const char **argv
)
769 struct option options
[] = {
772 const char * const usage
[] = {
773 N_("scalar unregister [<enlistment>]"),
777 argc
= parse_options(argc
, argv
, NULL
, options
,
781 * Be forgiving when the enlistment or worktree does not even exist any
782 * longer; This can be the case if a user deleted the worktree by
783 * mistake and _still_ wants to unregister the thing.
786 struct strbuf src_path
= STRBUF_INIT
, workdir_path
= STRBUF_INIT
;
788 strbuf_addf(&src_path
, "%s/src/.git", argv
[0]);
789 strbuf_addf(&workdir_path
, "%s/.git", argv
[0]);
790 if (!is_directory(src_path
.buf
) && !is_directory(workdir_path
.buf
)) {
791 /* remove possible matching registrations */
794 strbuf_strip_suffix(&src_path
, "/.git");
795 res
= remove_deleted_enlistment(&src_path
) && res
;
797 strbuf_strip_suffix(&workdir_path
, "/.git");
798 res
= remove_deleted_enlistment(&workdir_path
) && res
;
800 strbuf_release(&src_path
);
801 strbuf_release(&workdir_path
);
804 strbuf_release(&src_path
);
805 strbuf_release(&workdir_path
);
808 setup_enlistment_directory(argc
, argv
, usage
, options
, NULL
);
810 return unregister_dir();
813 static int cmd_delete(int argc
, const char **argv
)
815 char *cwd
= xgetcwd();
816 struct option options
[] = {
819 const char * const usage
[] = {
820 N_("scalar delete <enlistment>"),
823 struct strbuf enlistment
= STRBUF_INIT
;
826 argc
= parse_options(argc
, argv
, NULL
, options
,
830 usage_with_options(usage
, options
);
832 setup_enlistment_directory(argc
, argv
, usage
, options
, &enlistment
);
834 if (dir_inside_of(cwd
, enlistment
.buf
) >= 0)
835 res
= error(_("refusing to delete current working directory"));
837 close_object_store(the_repository
->objects
);
838 res
= delete_enlistment(&enlistment
);
840 strbuf_release(&enlistment
);
846 static int cmd_help(int argc
, const char **argv
)
848 struct option options
[] = {
851 const char * const usage
[] = {
856 argc
= parse_options(argc
, argv
, NULL
, options
,
860 usage_with_options(usage
, options
);
862 return run_git("help", "scalar", NULL
);
865 static int cmd_version(int argc
, const char **argv
)
867 int verbose
= 0, build_options
= 0;
868 struct option options
[] = {
869 OPT__VERBOSE(&verbose
, N_("include Git version")),
870 OPT_BOOL(0, "build-options", &build_options
,
871 N_("include Git's build options")),
874 const char * const usage
[] = {
875 N_("scalar verbose [-v | --verbose] [--build-options]"),
878 struct strbuf buf
= STRBUF_INIT
;
880 argc
= parse_options(argc
, argv
, NULL
, options
,
884 usage_with_options(usage
, options
);
886 get_version_info(&buf
, build_options
);
887 fprintf(stderr
, "%s\n", buf
.buf
);
888 strbuf_release(&buf
);
895 int (*fn
)(int, const char **);
897 { "clone", cmd_clone
},
898 { "list", cmd_list
},
899 { "register", cmd_register
},
900 { "unregister", cmd_unregister
},
902 { "reconfigure", cmd_reconfigure
},
903 { "delete", cmd_delete
},
904 { "help", cmd_help
},
905 { "version", cmd_version
},
906 { "diagnose", cmd_diagnose
},
910 int cmd_main(int argc
, const char **argv
)
912 struct strbuf scalar_usage
= STRBUF_INIT
;
915 while (argc
> 1 && *argv
[1] == '-') {
916 if (!strcmp(argv
[1], "-C")) {
918 die(_("-C requires a <directory>"));
919 if (chdir(argv
[2]) < 0)
920 die_errno(_("could not change to '%s'"),
924 } else if (!strcmp(argv
[1], "-c")) {
926 die(_("-c requires a <key>=<value> argument"));
927 git_config_push_parameter(argv
[2]);
938 for (i
= 0; builtins
[i
].name
; i
++)
939 if (!strcmp(builtins
[i
].name
, argv
[0]))
940 return !!builtins
[i
].fn(argc
, argv
);
943 strbuf_addstr(&scalar_usage
,
944 N_("scalar [-C <directory>] [-c <key>=<value>] "
945 "<command> [<options>]\n\nCommands:\n"));
946 for (i
= 0; builtins
[i
].name
; i
++)
947 strbuf_addf(&scalar_usage
, "\t%s\n", builtins
[i
].name
);
949 usage(scalar_usage
.buf
);