2 * The Scalar command-line interface.
7 #include "parse-options.h"
9 #include "run-command.h"
15 #include "object-store.h"
18 * Remove the deepest subdirectory in the provided path string. Path must not
19 * include a trailing path separator. Returns 1 if parent directory found,
22 static int strbuf_parent_directory(struct strbuf
*buf
)
24 size_t len
= buf
->len
;
25 size_t offset
= offset_1st_component(buf
->buf
);
26 char *path_sep
= find_last_dir_sep(buf
->buf
+ offset
);
27 strbuf_setlen(buf
, path_sep
? path_sep
- buf
->buf
: offset
);
29 return buf
->len
< len
;
32 static void setup_enlistment_directory(int argc
, const char **argv
,
33 const char * const *usagestr
,
34 const struct option
*options
,
35 struct strbuf
*enlistment_root
)
37 struct strbuf path
= STRBUF_INIT
;
39 int enlistment_found
= 0;
41 if (startup_info
->have_repository
)
42 BUG("gitdir already set up?!?");
45 usage_with_options(usagestr
, options
);
47 /* find the worktree, determine its corresponding root */
49 strbuf_add_absolute_path(&path
, argv
[0]);
50 if (!is_directory(path
.buf
))
51 die(_("'%s' does not exist"), path
.buf
);
52 } else if (strbuf_getcwd(&path
) < 0)
53 die(_("need a working directory"));
55 strbuf_trim_trailing_dir_sep(&path
);
57 const size_t len
= path
.len
;
59 /* check if currently in enlistment root with src/ workdir */
60 strbuf_addstr(&path
, "/src");
61 if (is_nonbare_repository_dir(&path
)) {
63 strbuf_add(enlistment_root
, path
.buf
, len
);
69 /* reset to original path */
70 strbuf_setlen(&path
, len
);
72 /* check if currently in workdir */
73 if (is_nonbare_repository_dir(&path
)) {
74 if (enlistment_root
) {
76 * If the worktree's directory's name is `src`, the enlistment is the
77 * parent directory, otherwise it is identical to the worktree.
79 root
= strip_path_suffix(path
.buf
, "src");
80 strbuf_addstr(enlistment_root
, root
? root
: path
.buf
);
87 } while (strbuf_parent_directory(&path
));
89 if (!enlistment_found
)
90 die(_("could not find enlistment root"));
92 if (chdir(path
.buf
) < 0)
93 die_errno(_("could not switch to '%s'"), path
.buf
);
95 strbuf_release(&path
);
96 setup_git_directory();
99 static int run_git(const char *arg
, ...)
101 struct strvec argv
= STRVEC_INIT
;
107 strvec_push(&argv
, arg
);
108 while ((p
= va_arg(args
, const char *)))
109 strvec_push(&argv
, p
);
112 res
= run_command_v_opt(argv
.v
, RUN_GIT_CMD
);
118 static int set_recommended_config(int reconfigure
)
123 int overwrite_on_reconfigure
;
126 { "am.keepCR", "true", 1 },
127 { "core.FSCache", "true", 1 },
128 { "core.multiPackIndex", "true", 1 },
129 { "core.preloadIndex", "true", 1 },
131 { "core.untrackedCache", "true", 1 },
134 * Unfortunately, Scalar's Functional Tests demonstrated
135 * that the untracked cache feature is unreliable on Windows
136 * (which is a bummer because that platform would benefit the
137 * most from it). For some reason, freshly created files seem
138 * not to update the directory's `lastModified` time
139 * immediately, but the untracked cache would need to rely on
142 * Therefore, with a sad heart, we disable this very useful
143 * feature on Windows.
145 { "core.untrackedCache", "false", 1 },
147 { "core.logAllRefUpdates", "true", 1 },
148 { "credential.https://dev.azure.com.useHttpPath", "true", 1 },
149 { "credential.validate", "false", 1 }, /* GCM4W-only */
150 { "gc.auto", "0", 1 },
151 { "gui.GCWarning", "false", 1 },
152 { "index.threads", "true", 1 },
153 { "index.version", "4", 1 },
154 { "merge.stat", "false", 1 },
155 { "merge.renames", "true", 1 },
156 { "pack.useBitmaps", "false", 1 },
157 { "pack.useSparse", "true", 1 },
158 { "receive.autoGC", "false", 1 },
159 { "feature.manyFiles", "false", 1 },
160 { "feature.experimental", "false", 1 },
161 { "fetch.unpackLimit", "1", 1 },
162 { "fetch.writeCommitGraph", "false", 1 },
164 { "http.sslBackend", "schannel", 1 },
167 { "status.aheadBehind", "false" },
168 { "commitGraph.generationVersion", "1" },
169 { "core.autoCRLF", "false" },
170 { "core.safeCRLF", "false" },
171 { "fetch.showForcedUpdates", "false" },
177 for (i
= 0; config
[i
].key
; i
++) {
178 if ((reconfigure
&& config
[i
].overwrite_on_reconfigure
) ||
179 git_config_get_string(config
[i
].key
, &value
)) {
180 trace2_data_string("scalar", the_repository
, config
[i
].key
, "created");
181 if (git_config_set_gently(config
[i
].key
,
182 config
[i
].value
) < 0)
183 return error(_("could not configure %s=%s"),
184 config
[i
].key
, config
[i
].value
);
186 trace2_data_string("scalar", the_repository
, config
[i
].key
, "exists");
192 * The `log.excludeDecoration` setting is special because it allows
193 * for multiple values.
195 if (git_config_get_string("log.excludeDecoration", &value
)) {
196 trace2_data_string("scalar", the_repository
,
197 "log.excludeDecoration", "created");
198 if (git_config_set_multivar_gently("log.excludeDecoration",
200 CONFIG_REGEX_NONE
, 0))
201 return error(_("could not configure "
202 "log.excludeDecoration"));
204 trace2_data_string("scalar", the_repository
,
205 "log.excludeDecoration", "exists");
212 static int toggle_maintenance(int enable
)
214 return run_git("maintenance", enable
? "start" : "unregister", NULL
);
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 register_dir(void)
241 int res
= add_or_remove_enlistment(1);
244 res
= set_recommended_config(0);
247 res
= toggle_maintenance(1);
252 static int unregister_dir(void)
256 if (toggle_maintenance(0) < 0)
259 if (add_or_remove_enlistment(0) < 0)
265 static int add_directory_to_archiver(struct strvec
*archiver_args
,
266 const char *path
, int recurse
)
268 int at_root
= !*path
;
269 DIR *dir
= opendir(at_root
? "." : path
);
271 struct strbuf buf
= STRBUF_INIT
;
276 return error_errno(_("could not open directory '%s'"), path
);
279 strbuf_addf(&buf
, "%s/", path
);
281 strvec_pushf(archiver_args
, "--prefix=%s", buf
.buf
);
283 while (!res
&& (e
= readdir(dir
))) {
284 if (!strcmp(".", e
->d_name
) || !strcmp("..", e
->d_name
))
287 strbuf_setlen(&buf
, len
);
288 strbuf_addstr(&buf
, e
->d_name
);
290 if (e
->d_type
== DT_REG
)
291 strvec_pushf(archiver_args
, "--add-file=%s", buf
.buf
);
292 else if (e
->d_type
!= DT_DIR
)
293 warning(_("skipping '%s', which is neither file nor "
294 "directory"), buf
.buf
);
296 add_directory_to_archiver(archiver_args
,
297 buf
.buf
, recurse
) < 0)
302 strbuf_release(&buf
);
307 #include <sys/statvfs.h>
310 static int get_disk_info(struct strbuf
*out
)
313 struct strbuf buf
= STRBUF_INIT
;
314 char volume_name
[MAX_PATH
], fs_name
[MAX_PATH
];
315 DWORD serial_number
, component_length
, flags
;
316 ULARGE_INTEGER avail2caller
, total
, avail
;
318 strbuf_realpath(&buf
, ".", 1);
319 if (!GetDiskFreeSpaceExA(buf
.buf
, &avail2caller
, &total
, &avail
)) {
320 error(_("could not determine free disk size for '%s'"),
322 strbuf_release(&buf
);
326 strbuf_setlen(&buf
, offset_1st_component(buf
.buf
));
327 if (!GetVolumeInformationA(buf
.buf
, volume_name
, sizeof(volume_name
),
328 &serial_number
, &component_length
, &flags
,
329 fs_name
, sizeof(fs_name
))) {
330 error(_("could not get info for '%s'"), buf
.buf
);
331 strbuf_release(&buf
);
334 strbuf_addf(out
, "Available space on '%s': ", buf
.buf
);
335 strbuf_humanise_bytes(out
, avail2caller
.QuadPart
);
336 strbuf_addch(out
, '\n');
337 strbuf_release(&buf
);
339 struct strbuf buf
= STRBUF_INIT
;
342 strbuf_realpath(&buf
, ".", 1);
343 if (statvfs(buf
.buf
, &stat
) < 0) {
344 error_errno(_("could not determine free disk size for '%s'"),
346 strbuf_release(&buf
);
350 strbuf_addf(out
, "Available space on '%s': ", buf
.buf
);
351 strbuf_humanise_bytes(out
, st_mult(stat
.f_bsize
, stat
.f_bavail
));
352 strbuf_addf(out
, " (mount flags 0x%lx)\n", stat
.f_flag
);
353 strbuf_release(&buf
);
358 /* printf-style interface, expects `<key>=<value>` argument */
359 static int set_config(const char *fmt
, ...)
361 struct strbuf buf
= STRBUF_INIT
;
367 strbuf_vaddf(&buf
, fmt
, args
);
370 value
= strchr(buf
.buf
, '=');
373 res
= git_config_set_gently(buf
.buf
, value
);
374 strbuf_release(&buf
);
379 static char *remote_default_branch(const char *url
)
381 struct child_process cp
= CHILD_PROCESS_INIT
;
382 struct strbuf out
= STRBUF_INIT
;
385 strvec_pushl(&cp
.args
, "ls-remote", "--symref", url
, "HEAD", NULL
);
386 if (!pipe_command(&cp
, NULL
, 0, &out
, 0, NULL
, 0)) {
387 const char *line
= out
.buf
;
390 const char *eol
= strchrnul(line
, '\n'), *p
;
391 size_t len
= eol
- line
;
394 if (!skip_prefix(line
, "ref: ", &p
) ||
395 !strip_suffix_mem(line
, &len
, "\tHEAD")) {
396 line
= eol
+ (*eol
== '\n');
401 if (skip_prefix(p
, "refs/heads/", &p
)) {
402 branch
= xstrndup(p
, eol
- p
);
403 strbuf_release(&out
);
407 error(_("remote HEAD is not a branch: '%.*s'"),
409 strbuf_release(&out
);
413 warning(_("failed to get default branch name from remote; "
414 "using local default"));
417 child_process_init(&cp
);
419 strvec_pushl(&cp
.args
, "symbolic-ref", "--short", "HEAD", NULL
);
420 if (!pipe_command(&cp
, NULL
, 0, &out
, 0, NULL
, 0)) {
422 return strbuf_detach(&out
, NULL
);
425 strbuf_release(&out
);
426 error(_("failed to get default branch name"));
430 static int delete_enlistment(struct strbuf
*enlistment
)
433 struct strbuf parent
= STRBUF_INIT
;
436 if (unregister_dir())
437 die(_("failed to unregister repository"));
441 * Change the current directory to one outside of the enlistment so
442 * that we may delete everything underneath it.
444 strbuf_addbuf(&parent
, enlistment
);
445 strbuf_parent_directory(&parent
);
446 if (chdir(parent
.buf
) < 0)
447 die_errno(_("could not switch to '%s'"), parent
.buf
);
448 strbuf_release(&parent
);
451 if (remove_dir_recursively(enlistment
, 0))
452 die(_("failed to delete enlistment directory"));
458 * Dummy implementation; Using `get_version_info()` would cause a link error
461 void load_builtin_commands(const char *prefix
, struct cmdnames
*cmds
)
463 die("not implemented");
466 static int cmd_clone(int argc
, const char **argv
)
468 const char *branch
= NULL
;
469 int full_clone
= 0, single_branch
= 0;
470 struct option clone_options
[] = {
471 OPT_STRING('b', "branch", &branch
, N_("<branch>"),
472 N_("branch to checkout after clone")),
473 OPT_BOOL(0, "full-clone", &full_clone
,
474 N_("when cloning, create full working directory")),
475 OPT_BOOL(0, "single-branch", &single_branch
,
476 N_("only download metadata for the branch that will "
480 const char * const clone_usage
[] = {
481 N_("scalar clone [<options>] [--] <repo> [<dir>]"),
485 char *enlistment
= NULL
, *dir
= NULL
;
486 struct strbuf buf
= STRBUF_INIT
;
489 argc
= parse_options(argc
, argv
, NULL
, clone_options
, clone_usage
, 0);
493 enlistment
= xstrdup(argv
[1]);
494 } else if (argc
== 1) {
497 strbuf_addstr(&buf
, url
);
498 /* Strip trailing slashes, if any */
499 while (buf
.len
> 0 && is_dir_sep(buf
.buf
[buf
.len
- 1]))
500 strbuf_setlen(&buf
, buf
.len
- 1);
501 /* Strip suffix `.git`, if any */
502 strbuf_strip_suffix(&buf
, ".git");
504 enlistment
= find_last_dir_sep(buf
.buf
);
506 die(_("cannot deduce worktree name from '%s'"), url
);
508 enlistment
= xstrdup(enlistment
+ 1);
510 usage_msg_opt(_("You must specify a repository to clone."),
511 clone_usage
, clone_options
);
514 if (is_directory(enlistment
))
515 die(_("directory '%s' exists already"), enlistment
);
517 dir
= xstrfmt("%s/src", enlistment
);
521 strbuf_addf(&buf
, "init.defaultBranch=%s", branch
);
523 char *b
= repo_default_branch_name(the_repository
, 1);
524 strbuf_addf(&buf
, "init.defaultBranch=%s", b
);
528 if ((res
= run_git("-c", buf
.buf
, "init", "--", dir
, NULL
)))
531 if (chdir(dir
) < 0) {
532 res
= error_errno(_("could not switch to '%s'"), dir
);
536 setup_git_directory();
538 /* common-main already logs `argv` */
539 trace2_def_repo(the_repository
);
541 if (!branch
&& !(branch
= remote_default_branch(url
))) {
542 res
= error(_("failed to get default branch for '%s'"), url
);
546 if (set_config("remote.origin.url=%s", url
) ||
547 set_config("remote.origin.fetch="
548 "+refs/heads/%s:refs/remotes/origin/%s",
549 single_branch
? branch
: "*",
550 single_branch
? branch
: "*") ||
551 set_config("remote.origin.promisor=true") ||
552 set_config("remote.origin.partialCloneFilter=blob:none")) {
553 res
= error(_("could not configure remote in '%s'"), dir
);
558 (res
= run_git("sparse-checkout", "init", "--cone", NULL
)))
561 if (set_recommended_config(0))
562 return error(_("could not configure '%s'"), dir
);
564 if ((res
= run_git("fetch", "--quiet", "origin", NULL
))) {
565 warning(_("partial clone failed; attempting full clone"));
567 if (set_config("remote.origin.promisor") ||
568 set_config("remote.origin.partialCloneFilter")) {
569 res
= error(_("could not configure for full clone"));
573 if ((res
= run_git("fetch", "--quiet", "origin", NULL
)))
577 if ((res
= set_config("branch.%s.remote=origin", branch
)))
579 if ((res
= set_config("branch.%s.merge=refs/heads/%s",
584 strbuf_addf(&buf
, "origin/%s", branch
);
585 res
= run_git("checkout", "-f", "-t", buf
.buf
, NULL
);
589 res
= register_dir();
594 strbuf_release(&buf
);
598 static void dir_file_stats_objects(const char *full_path
, size_t full_path_len
,
599 const char *file_name
, void *data
)
601 struct strbuf
*buf
= data
;
604 if (!stat(full_path
, &st
))
605 strbuf_addf(buf
, "%-70s %16" PRIuMAX
"\n", file_name
,
606 (uintmax_t)st
.st_size
);
609 static int dir_file_stats(struct object_directory
*object_dir
, void *data
)
611 struct strbuf
*buf
= data
;
613 strbuf_addf(buf
, "Contents of %s:\n", object_dir
->path
);
615 for_each_file_in_pack_dir(object_dir
->path
, dir_file_stats_objects
,
621 static int cmd_diagnose(int argc
, const char **argv
)
623 struct option options
[] = {
626 const char * const usage
[] = {
627 N_("scalar diagnose [<enlistment>]"),
630 struct strbuf zip_path
= STRBUF_INIT
;
631 struct strvec archiver_args
= STRVEC_INIT
;
632 char **argv_copy
= NULL
;
633 int stdout_fd
= -1, archiver_fd
= -1;
634 time_t now
= time(NULL
);
636 struct strbuf path
= STRBUF_INIT
, buf
= STRBUF_INIT
;
639 argc
= parse_options(argc
, argv
, NULL
, options
,
642 setup_enlistment_directory(argc
, argv
, usage
, options
, &zip_path
);
644 strbuf_addstr(&zip_path
, "/.scalarDiagnostics/scalar_");
645 strbuf_addftime(&zip_path
,
646 "%Y%m%d_%H%M%S", localtime_r(&now
, &tm
), 0, 0);
647 strbuf_addstr(&zip_path
, ".zip");
648 switch (safe_create_leading_directories(zip_path
.buf
)) {
653 error_errno(_("could not create directory for '%s'"),
655 goto diagnose_cleanup
;
659 res
= error_errno(_("could not duplicate stdout"));
660 goto diagnose_cleanup
;
663 archiver_fd
= xopen(zip_path
.buf
, O_CREAT
| O_WRONLY
| O_TRUNC
, 0666);
664 if (archiver_fd
< 0 || dup2(archiver_fd
, 1) < 0) {
665 res
= error_errno(_("could not redirect output"));
666 goto diagnose_cleanup
;
670 strvec_pushl(&archiver_args
, "scalar-diagnose", "--format=zip", NULL
);
673 strbuf_addstr(&buf
, "Collecting diagnostic info\n\n");
674 get_version_info(&buf
, 1);
676 strbuf_addf(&buf
, "Enlistment root: %s\n", the_repository
->worktree
);
678 write_or_die(stdout_fd
, buf
.buf
, buf
.len
);
679 strvec_pushf(&archiver_args
,
680 "--add-virtual-file=diagnostics.log:%.*s",
681 (int)buf
.len
, buf
.buf
);
684 strbuf_addstr(&buf
, "--add-virtual-file=packs-local.txt:");
685 dir_file_stats(the_repository
->objects
->odb
, &buf
);
686 foreach_alt_odb(dir_file_stats
, &buf
);
687 strvec_push(&archiver_args
, buf
.buf
);
689 if ((res
= add_directory_to_archiver(&archiver_args
, ".git", 0)) ||
690 (res
= add_directory_to_archiver(&archiver_args
, ".git/hooks", 0)) ||
691 (res
= add_directory_to_archiver(&archiver_args
, ".git/info", 0)) ||
692 (res
= add_directory_to_archiver(&archiver_args
, ".git/logs", 1)) ||
693 (res
= add_directory_to_archiver(&archiver_args
, ".git/objects/info", 0)))
694 goto diagnose_cleanup
;
696 strvec_pushl(&archiver_args
, "--prefix=",
697 oid_to_hex(the_hash_algo
->empty_tree
), "--", NULL
);
699 /* `write_archive()` modifies the `argv` passed to it. Let it. */
700 argv_copy
= xmemdupz(archiver_args
.v
,
701 sizeof(char *) * archiver_args
.nr
);
702 res
= write_archive(archiver_args
.nr
, (const char **)argv_copy
, NULL
,
703 the_repository
, NULL
, 0);
705 error(_("failed to write archive"));
706 goto diagnose_cleanup
;
711 "Diagnostics complete.\n"
712 "All of the gathered info is captured in '%s'\n",
716 if (archiver_fd
>= 0) {
721 strvec_clear(&archiver_args
);
722 strbuf_release(&zip_path
);
723 strbuf_release(&path
);
724 strbuf_release(&buf
);
729 static int cmd_list(int argc
, const char **argv
)
732 die(_("`scalar list` does not take arguments"));
734 if (run_git("config", "--global", "--get-all", "scalar.repo", NULL
) < 0)
739 static int cmd_register(int argc
, const char **argv
)
741 struct option options
[] = {
744 const char * const usage
[] = {
745 N_("scalar register [<enlistment>]"),
749 argc
= parse_options(argc
, argv
, NULL
, options
,
752 setup_enlistment_directory(argc
, argv
, usage
, options
, NULL
);
754 return register_dir();
757 static int get_scalar_repos(const char *key
, const char *value
, void *data
)
759 struct string_list
*list
= data
;
761 if (!strcmp(key
, "scalar.repo"))
762 string_list_append(list
, value
);
767 static int cmd_reconfigure(int argc
, const char **argv
)
770 struct option options
[] = {
771 OPT_BOOL('a', "all", &all
,
772 N_("reconfigure all registered enlistments")),
775 const char * const usage
[] = {
776 N_("scalar reconfigure [--all | <enlistment>]"),
779 struct string_list scalar_repos
= STRING_LIST_INIT_DUP
;
781 struct repository r
= { NULL
};
782 struct strbuf commondir
= STRBUF_INIT
, gitdir
= STRBUF_INIT
;
784 argc
= parse_options(argc
, argv
, NULL
, options
,
788 setup_enlistment_directory(argc
, argv
, usage
, options
, NULL
);
790 return set_recommended_config(1);
794 usage_msg_opt(_("--all or <enlistment>, but not both"),
797 git_config(get_scalar_repos
, &scalar_repos
);
799 for (i
= 0; i
< scalar_repos
.nr
; i
++) {
800 const char *dir
= scalar_repos
.items
[i
].string
;
802 strbuf_reset(&commondir
);
803 strbuf_reset(&gitdir
);
805 if (chdir(dir
) < 0) {
806 warning_errno(_("could not switch to '%s'"), dir
);
808 } else if (discover_git_directory(&commondir
, &gitdir
) < 0) {
809 warning_errno(_("git repository gone in '%s'"), dir
);
815 r
.commondir
= commondir
.buf
;
816 r
.gitdir
= gitdir
.buf
;
818 if (set_recommended_config(1) < 0)
823 string_list_clear(&scalar_repos
, 1);
824 strbuf_release(&commondir
);
825 strbuf_release(&gitdir
);
830 static int cmd_run(int argc
, const char **argv
)
832 struct option options
[] = {
836 const char *arg
, *task
;
839 { "commit-graph", "commit-graph" },
840 { "fetch", "prefetch" },
841 { "loose-objects", "loose-objects" },
842 { "pack-files", "incremental-repack" },
845 struct strbuf buf
= STRBUF_INIT
;
846 const char *usagestr
[] = { NULL
, NULL
};
849 strbuf_addstr(&buf
, N_("scalar run <task> [<enlistment>]\nTasks:\n"));
850 for (i
= 0; tasks
[i
].arg
; i
++)
851 strbuf_addf(&buf
, "\t%s\n", tasks
[i
].arg
);
852 usagestr
[0] = buf
.buf
;
854 argc
= parse_options(argc
, argv
, NULL
, options
,
858 usage_with_options(usagestr
, options
);
860 if (!strcmp("all", argv
[0])) {
863 for (i
= 0; tasks
[i
].arg
&& strcmp(tasks
[i
].arg
, argv
[0]); i
++)
864 ; /* keep looking for the task */
866 if (i
> 0 && !tasks
[i
].arg
) {
867 error(_("no such task: '%s'"), argv
[0]);
868 usage_with_options(usagestr
, options
);
874 setup_enlistment_directory(argc
, argv
, usagestr
, options
, NULL
);
875 strbuf_release(&buf
);
878 return register_dir();
881 return run_git("maintenance", "run",
882 "--task", tasks
[i
].task
, NULL
);
886 for (i
= 1; tasks
[i
].arg
; i
++)
887 if (run_git("maintenance", "run",
888 "--task", tasks
[i
].task
, NULL
))
893 static int remove_deleted_enlistment(struct strbuf
*path
)
896 strbuf_realpath_forgiving(path
, path
->buf
, 1);
898 if (run_git("config", "--global",
899 "--unset", "--fixed-value",
900 "scalar.repo", path
->buf
, NULL
) < 0)
903 if (run_git("config", "--global",
904 "--unset", "--fixed-value",
905 "maintenance.repo", path
->buf
, NULL
) < 0)
911 static int cmd_unregister(int argc
, const char **argv
)
913 struct option options
[] = {
916 const char * const usage
[] = {
917 N_("scalar unregister [<enlistment>]"),
921 argc
= parse_options(argc
, argv
, NULL
, options
,
925 * Be forgiving when the enlistment or worktree does not even exist any
926 * longer; This can be the case if a user deleted the worktree by
927 * mistake and _still_ wants to unregister the thing.
930 struct strbuf src_path
= STRBUF_INIT
, workdir_path
= STRBUF_INIT
;
932 strbuf_addf(&src_path
, "%s/src/.git", argv
[0]);
933 strbuf_addf(&workdir_path
, "%s/.git", argv
[0]);
934 if (!is_directory(src_path
.buf
) && !is_directory(workdir_path
.buf
)) {
935 /* remove possible matching registrations */
938 strbuf_strip_suffix(&src_path
, "/.git");
939 res
= remove_deleted_enlistment(&src_path
) && res
;
941 strbuf_strip_suffix(&workdir_path
, "/.git");
942 res
= remove_deleted_enlistment(&workdir_path
) && res
;
944 strbuf_release(&src_path
);
945 strbuf_release(&workdir_path
);
948 strbuf_release(&src_path
);
949 strbuf_release(&workdir_path
);
952 setup_enlistment_directory(argc
, argv
, usage
, options
, NULL
);
954 return unregister_dir();
957 static int cmd_delete(int argc
, const char **argv
)
959 char *cwd
= xgetcwd();
960 struct option options
[] = {
963 const char * const usage
[] = {
964 N_("scalar delete <enlistment>"),
967 struct strbuf enlistment
= STRBUF_INIT
;
970 argc
= parse_options(argc
, argv
, NULL
, options
,
974 usage_with_options(usage
, options
);
976 setup_enlistment_directory(argc
, argv
, usage
, options
, &enlistment
);
978 if (dir_inside_of(cwd
, enlistment
.buf
) >= 0)
979 res
= error(_("refusing to delete current working directory"));
981 close_object_store(the_repository
->objects
);
982 res
= delete_enlistment(&enlistment
);
984 strbuf_release(&enlistment
);
990 static int cmd_version(int argc
, const char **argv
)
992 int verbose
= 0, build_options
= 0;
993 struct option options
[] = {
994 OPT__VERBOSE(&verbose
, N_("include Git version")),
995 OPT_BOOL(0, "build-options", &build_options
,
996 N_("include Git's build options")),
999 const char * const usage
[] = {
1000 N_("scalar verbose [-v | --verbose] [--build-options]"),
1003 struct strbuf buf
= STRBUF_INIT
;
1005 argc
= parse_options(argc
, argv
, NULL
, options
,
1009 usage_with_options(usage
, options
);
1011 get_version_info(&buf
, build_options
);
1012 fprintf(stderr
, "%s\n", buf
.buf
);
1013 strbuf_release(&buf
);
1020 int (*fn
)(int, const char **);
1022 { "clone", cmd_clone
},
1023 { "list", cmd_list
},
1024 { "register", cmd_register
},
1025 { "unregister", cmd_unregister
},
1027 { "reconfigure", cmd_reconfigure
},
1028 { "delete", cmd_delete
},
1029 { "version", cmd_version
},
1030 { "diagnose", cmd_diagnose
},
1034 int cmd_main(int argc
, const char **argv
)
1036 struct strbuf scalar_usage
= STRBUF_INIT
;
1039 while (argc
> 1 && *argv
[1] == '-') {
1040 if (!strcmp(argv
[1], "-C")) {
1042 die(_("-C requires a <directory>"));
1043 if (chdir(argv
[2]) < 0)
1044 die_errno(_("could not change to '%s'"),
1048 } else if (!strcmp(argv
[1], "-c")) {
1050 die(_("-c requires a <key>=<value> argument"));
1051 git_config_push_parameter(argv
[2]);
1062 for (i
= 0; builtins
[i
].name
; i
++)
1063 if (!strcmp(builtins
[i
].name
, argv
[0]))
1064 return !!builtins
[i
].fn(argc
, argv
);
1067 strbuf_addstr(&scalar_usage
,
1068 N_("scalar [-C <directory>] [-c <key>=<value>] "
1069 "<command> [<options>]\n\nCommands:\n"));
1070 for (i
= 0; builtins
[i
].name
; i
++)
1071 strbuf_addf(&scalar_usage
, "\t%s\n", builtins
[i
].name
);
1073 usage(scalar_usage
.buf
);