]>
Commit | Line | Data |
---|---|---|
0a43fb22 JS |
1 | /* |
2 | * The Scalar command-line interface. | |
3 | */ | |
4 | ||
5 | #include "cache.h" | |
6 | #include "gettext.h" | |
7 | #include "parse-options.h" | |
d0feac4e DS |
8 | #include "config.h" |
9 | #include "run-command.h" | |
3f1917dc MJC |
10 | #include "simple-ipc.h" |
11 | #include "fsmonitor-ipc.h" | |
12 | #include "fsmonitor-settings.h" | |
546f822d | 13 | #include "refs.h" |
d85ada7c MJC |
14 | #include "dir.h" |
15 | #include "packfile.h" | |
ddc35d83 | 16 | #include "help.h" |
d0feac4e | 17 | |
d0feac4e DS |
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) | |
22 | { | |
23 | struct strbuf path = STRBUF_INIT; | |
65f6a9eb VD |
24 | int enlistment_is_repo_parent = 0; |
25 | size_t len; | |
d0feac4e DS |
26 | |
27 | if (startup_info->have_repository) | |
28 | BUG("gitdir already set up?!?"); | |
29 | ||
30 | if (argc > 1) | |
31 | usage_with_options(usagestr, options); | |
32 | ||
33 | /* find the worktree, determine its corresponding root */ | |
b4485574 | 34 | if (argc == 1) { |
d0feac4e | 35 | strbuf_add_absolute_path(&path, argv[0]); |
b4485574 JS |
36 | if (!is_directory(path.buf)) |
37 | die(_("'%s' does not exist"), path.buf); | |
65f6a9eb VD |
38 | if (chdir(path.buf) < 0) |
39 | die_errno(_("could not switch to '%s'"), path.buf); | |
b4485574 | 40 | } else if (strbuf_getcwd(&path) < 0) |
d0feac4e DS |
41 | die(_("need a working directory")); |
42 | ||
43 | strbuf_trim_trailing_dir_sep(&path); | |
d0feac4e | 44 | |
65f6a9eb VD |
45 | /* check if currently in enlistment root with src/ workdir */ |
46 | len = path.len; | |
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); | |
52 | } | |
53 | strbuf_setlen(&path, len); | |
d0feac4e | 54 | |
65f6a9eb | 55 | setup_git_directory(); |
d0feac4e | 56 | |
65f6a9eb VD |
57 | if (!the_repository->worktree) |
58 | die(_("Scalar enlistments require a worktree")); | |
d0feac4e | 59 | |
65f6a9eb VD |
60 | if (enlistment_root) { |
61 | if (enlistment_is_repo_parent) | |
62 | strbuf_addbuf(enlistment_root, &path); | |
63 | else | |
64 | strbuf_addstr(enlistment_root, the_repository->worktree); | |
65 | } | |
d0feac4e DS |
66 | |
67 | strbuf_release(&path); | |
d0feac4e DS |
68 | } |
69 | ||
70 | static int run_git(const char *arg, ...) | |
71 | { | |
72 | struct strvec argv = STRVEC_INIT; | |
73 | va_list args; | |
74 | const char *p; | |
75 | int res; | |
76 | ||
77 | va_start(args, arg); | |
78 | strvec_push(&argv, arg); | |
79 | while ((p = va_arg(args, const char *))) | |
80 | strvec_push(&argv, p); | |
81 | va_end(args); | |
82 | ||
83 | res = run_command_v_opt(argv.v, RUN_GIT_CMD); | |
84 | ||
85 | strvec_clear(&argv); | |
86 | return res; | |
87 | } | |
88 | ||
d934a11c VD |
89 | struct scalar_config { |
90 | const char *key; | |
91 | const char *value; | |
92 | int overwrite_on_reconfigure; | |
93 | }; | |
94 | ||
95 | static int set_scalar_config(const struct scalar_config *config, int reconfigure) | |
96 | { | |
97 | char *value = NULL; | |
98 | int res; | |
99 | ||
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); | |
104 | } else { | |
105 | trace2_data_string("scalar", the_repository, config->key, "exists"); | |
106 | res = 0; | |
107 | } | |
108 | ||
109 | free(value); | |
110 | return res; | |
111 | } | |
112 | ||
3f1917dc MJC |
113 | static int have_fsmonitor_support(void) |
114 | { | |
115 | return fsmonitor_ipc__is_supported() && | |
116 | fsm_settings__get_reason(the_repository) == FSMONITOR_REASON_OK; | |
117 | } | |
118 | ||
cb59d55e | 119 | static int set_recommended_config(int reconfigure) |
d0feac4e | 120 | { |
d934a11c | 121 | struct scalar_config config[] = { |
cb59d55e JS |
122 | /* Required */ |
123 | { "am.keepCR", "true", 1 }, | |
124 | { "core.FSCache", "true", 1 }, | |
125 | { "core.multiPackIndex", "true", 1 }, | |
126 | { "core.preloadIndex", "true", 1 }, | |
d0feac4e | 127 | #ifndef WIN32 |
cb59d55e | 128 | { "core.untrackedCache", "true", 1 }, |
d0feac4e DS |
129 | #else |
130 | /* | |
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 | |
137 | * that. | |
138 | * | |
139 | * Therefore, with a sad heart, we disable this very useful | |
140 | * feature on Windows. | |
141 | */ | |
cb59d55e | 142 | { "core.untrackedCache", "false", 1 }, |
d0feac4e | 143 | #endif |
cb59d55e JS |
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 }, | |
cb59d55e JS |
156 | { "feature.manyFiles", "false", 1 }, |
157 | { "feature.experimental", "false", 1 }, | |
158 | { "fetch.unpackLimit", "1", 1 }, | |
159 | { "fetch.writeCommitGraph", "false", 1 }, | |
d0feac4e | 160 | #ifdef WIN32 |
cb59d55e | 161 | { "http.sslBackend", "schannel", 1 }, |
d0feac4e | 162 | #endif |
cb59d55e | 163 | /* Optional */ |
d0feac4e DS |
164 | { "status.aheadBehind", "false" }, |
165 | { "commitGraph.generationVersion", "1" }, | |
166 | { "core.autoCRLF", "false" }, | |
167 | { "core.safeCRLF", "false" }, | |
168 | { "fetch.showForcedUpdates", "false" }, | |
169 | { NULL, NULL }, | |
170 | }; | |
171 | int i; | |
172 | char *value; | |
173 | ||
174 | for (i = 0; config[i].key; i++) { | |
d934a11c VD |
175 | if (set_scalar_config(config + i, reconfigure)) |
176 | return error(_("could not configure %s=%s"), | |
177 | config[i].key, config[i].value); | |
d0feac4e DS |
178 | } |
179 | ||
3f1917dc MJC |
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); | |
d0feac4e DS |
185 | } |
186 | ||
187 | /* | |
188 | * The `log.excludeDecoration` setting is special because it allows | |
189 | * for multiple values. | |
190 | */ | |
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", | |
195 | "refs/prefetch/*", | |
196 | CONFIG_REGEX_NONE, 0)) | |
197 | return error(_("could not configure " | |
198 | "log.excludeDecoration")); | |
199 | } else { | |
200 | trace2_data_string("scalar", the_repository, | |
201 | "log.excludeDecoration", "exists"); | |
202 | free(value); | |
203 | } | |
204 | ||
205 | return 0; | |
206 | } | |
207 | ||
c76a53eb | 208 | static int toggle_maintenance(int enable) |
d0feac4e | 209 | { |
d871b6c6 DS |
210 | return run_git("maintenance", |
211 | enable ? "start" : "unregister", | |
212 | enable ? NULL : "--force", | |
213 | NULL); | |
d0feac4e DS |
214 | } |
215 | ||
c76a53eb | 216 | static int add_or_remove_enlistment(int add) |
d0feac4e DS |
217 | { |
218 | int res; | |
219 | ||
220 | if (!the_repository->worktree) | |
221 | die(_("Scalar enlistments require a worktree")); | |
222 | ||
223 | res = run_git("config", "--global", "--get", "--fixed-value", | |
224 | "scalar.repo", the_repository->worktree, NULL); | |
225 | ||
226 | /* | |
c76a53eb DS |
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. | |
d0feac4e | 229 | */ |
c76a53eb | 230 | if ((add && !res) || (!add && res)) |
d0feac4e DS |
231 | return 0; |
232 | ||
c76a53eb DS |
233 | return run_git("config", "--global", add ? "--add" : "--unset", |
234 | add ? "--no-fixed-value" : "--fixed-value", | |
d0feac4e DS |
235 | "scalar.repo", the_repository->worktree, NULL); |
236 | } | |
237 | ||
3f1917dc MJC |
238 | static int start_fsmonitor_daemon(void) |
239 | { | |
240 | assert(have_fsmonitor_support()); | |
241 | ||
242 | if (fsmonitor_ipc__get_state() != IPC_STATE__LISTENING) | |
243 | return run_git("fsmonitor--daemon", "start", NULL); | |
244 | ||
245 | return 0; | |
246 | } | |
247 | ||
ec4c2311 JS |
248 | static int stop_fsmonitor_daemon(void) |
249 | { | |
250 | assert(have_fsmonitor_support()); | |
251 | ||
252 | if (fsmonitor_ipc__get_state() == IPC_STATE__LISTENING) | |
253 | return run_git("fsmonitor--daemon", "stop", NULL); | |
254 | ||
255 | return 0; | |
256 | } | |
257 | ||
d0feac4e DS |
258 | static int register_dir(void) |
259 | { | |
d2a79bc9 VD |
260 | if (add_or_remove_enlistment(1)) |
261 | return error(_("could not add enlistment")); | |
d0feac4e | 262 | |
d2a79bc9 VD |
263 | if (set_recommended_config(0)) |
264 | return error(_("could not set recommended config")); | |
d0feac4e | 265 | |
d2a79bc9 VD |
266 | if (toggle_maintenance(1)) |
267 | return error(_("could not turn on maintenance")); | |
c76a53eb | 268 | |
3f1917dc MJC |
269 | if (have_fsmonitor_support() && start_fsmonitor_daemon()) { |
270 | return error(_("could not start the FSMonitor daemon")); | |
271 | } | |
272 | ||
d2a79bc9 | 273 | return 0; |
c76a53eb DS |
274 | } |
275 | ||
276 | static int unregister_dir(void) | |
277 | { | |
278 | int res = 0; | |
279 | ||
adedcee8 | 280 | if (toggle_maintenance(0)) |
d2a79bc9 | 281 | res = error(_("could not turn off maintenance")); |
c76a53eb | 282 | |
adedcee8 | 283 | if (add_or_remove_enlistment(0)) |
d2a79bc9 | 284 | res = error(_("could not remove enlistment")); |
d0feac4e DS |
285 | |
286 | return res; | |
287 | } | |
288 | ||
546f822d JS |
289 | /* printf-style interface, expects `<key>=<value>` argument */ |
290 | static int set_config(const char *fmt, ...) | |
291 | { | |
292 | struct strbuf buf = STRBUF_INIT; | |
293 | char *value; | |
294 | int res; | |
295 | va_list args; | |
296 | ||
297 | va_start(args, fmt); | |
298 | strbuf_vaddf(&buf, fmt, args); | |
299 | va_end(args); | |
300 | ||
301 | value = strchr(buf.buf, '='); | |
302 | if (value) | |
303 | *(value++) = '\0'; | |
304 | res = git_config_set_gently(buf.buf, value); | |
305 | strbuf_release(&buf); | |
306 | ||
307 | return res; | |
308 | } | |
309 | ||
310 | static char *remote_default_branch(const char *url) | |
311 | { | |
312 | struct child_process cp = CHILD_PROCESS_INIT; | |
313 | struct strbuf out = STRBUF_INIT; | |
314 | ||
315 | cp.git_cmd = 1; | |
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; | |
319 | ||
320 | while (*line) { | |
321 | const char *eol = strchrnul(line, '\n'), *p; | |
322 | size_t len = eol - line; | |
323 | char *branch; | |
324 | ||
325 | if (!skip_prefix(line, "ref: ", &p) || | |
326 | !strip_suffix_mem(line, &len, "\tHEAD")) { | |
327 | line = eol + (*eol == '\n'); | |
328 | continue; | |
329 | } | |
330 | ||
331 | eol = line + len; | |
332 | if (skip_prefix(p, "refs/heads/", &p)) { | |
333 | branch = xstrndup(p, eol - p); | |
334 | strbuf_release(&out); | |
335 | return branch; | |
336 | } | |
337 | ||
338 | error(_("remote HEAD is not a branch: '%.*s'"), | |
339 | (int)(eol - p), p); | |
340 | strbuf_release(&out); | |
341 | return NULL; | |
342 | } | |
343 | } | |
344 | warning(_("failed to get default branch name from remote; " | |
345 | "using local default")); | |
346 | strbuf_reset(&out); | |
347 | ||
348 | child_process_init(&cp); | |
349 | cp.git_cmd = 1; | |
350 | strvec_pushl(&cp.args, "symbolic-ref", "--short", "HEAD", NULL); | |
351 | if (!pipe_command(&cp, NULL, 0, &out, 0, NULL, 0)) { | |
352 | strbuf_trim(&out); | |
353 | return strbuf_detach(&out, NULL); | |
354 | } | |
355 | ||
356 | strbuf_release(&out); | |
357 | error(_("failed to get default branch name")); | |
358 | return NULL; | |
359 | } | |
360 | ||
d85ada7c MJC |
361 | static int delete_enlistment(struct strbuf *enlistment) |
362 | { | |
363 | #ifdef WIN32 | |
364 | struct strbuf parent = STRBUF_INIT; | |
65f6a9eb VD |
365 | size_t offset; |
366 | char *path_sep; | |
d85ada7c MJC |
367 | #endif |
368 | ||
369 | if (unregister_dir()) | |
9b24bb92 | 370 | return error(_("failed to unregister repository")); |
d85ada7c MJC |
371 | |
372 | #ifdef WIN32 | |
373 | /* | |
374 | * Change the current directory to one outside of the enlistment so | |
375 | * that we may delete everything underneath it. | |
376 | */ | |
65f6a9eb VD |
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); | |
9b24bb92 VD |
381 | if (chdir(parent.buf) < 0) { |
382 | int res = error_errno(_("could not switch to '%s'"), parent.buf); | |
383 | strbuf_release(&parent); | |
384 | return res; | |
385 | } | |
d85ada7c MJC |
386 | strbuf_release(&parent); |
387 | #endif | |
388 | ||
ec4c2311 JS |
389 | if (have_fsmonitor_support() && stop_fsmonitor_daemon()) |
390 | return error(_("failed to stop the FSMonitor daemon")); | |
391 | ||
d85ada7c | 392 | if (remove_dir_recursively(enlistment, 0)) |
9b24bb92 | 393 | return error(_("failed to delete enlistment directory")); |
d85ada7c MJC |
394 | |
395 | return 0; | |
396 | } | |
397 | ||
ddc35d83 JS |
398 | /* |
399 | * Dummy implementation; Using `get_version_info()` would cause a link error | |
400 | * without this. | |
401 | */ | |
402 | void load_builtin_commands(const char *prefix, struct cmdnames *cmds) | |
403 | { | |
404 | die("not implemented"); | |
405 | } | |
406 | ||
546f822d JS |
407 | static int cmd_clone(int argc, const char **argv) |
408 | { | |
409 | const char *branch = NULL; | |
4368e40b | 410 | int full_clone = 0, single_branch = 0; |
546f822d JS |
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")), | |
4368e40b JS |
416 | OPT_BOOL(0, "single-branch", &single_branch, |
417 | N_("only download metadata for the branch that will " | |
418 | "be checked out")), | |
546f822d JS |
419 | OPT_END(), |
420 | }; | |
421 | const char * const clone_usage[] = { | |
422 | N_("scalar clone [<options>] [--] <repo> [<dir>]"), | |
423 | NULL | |
424 | }; | |
425 | const char *url; | |
426 | char *enlistment = NULL, *dir = NULL; | |
427 | struct strbuf buf = STRBUF_INIT; | |
428 | int res; | |
429 | ||
430 | argc = parse_options(argc, argv, NULL, clone_options, clone_usage, 0); | |
431 | ||
432 | if (argc == 2) { | |
433 | url = argv[0]; | |
434 | enlistment = xstrdup(argv[1]); | |
435 | } else if (argc == 1) { | |
436 | url = argv[0]; | |
437 | ||
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"); | |
444 | ||
445 | enlistment = find_last_dir_sep(buf.buf); | |
446 | if (!enlistment) { | |
447 | die(_("cannot deduce worktree name from '%s'"), url); | |
448 | } | |
449 | enlistment = xstrdup(enlistment + 1); | |
450 | } else { | |
451 | usage_msg_opt(_("You must specify a repository to clone."), | |
452 | clone_usage, clone_options); | |
453 | } | |
454 | ||
455 | if (is_directory(enlistment)) | |
456 | die(_("directory '%s' exists already"), enlistment); | |
457 | ||
458 | dir = xstrfmt("%s/src", enlistment); | |
459 | ||
460 | strbuf_reset(&buf); | |
461 | if (branch) | |
462 | strbuf_addf(&buf, "init.defaultBranch=%s", branch); | |
463 | else { | |
464 | char *b = repo_default_branch_name(the_repository, 1); | |
465 | strbuf_addf(&buf, "init.defaultBranch=%s", b); | |
466 | free(b); | |
467 | } | |
468 | ||
469 | if ((res = run_git("-c", buf.buf, "init", "--", dir, NULL))) | |
470 | goto cleanup; | |
471 | ||
472 | if (chdir(dir) < 0) { | |
473 | res = error_errno(_("could not switch to '%s'"), dir); | |
474 | goto cleanup; | |
475 | } | |
476 | ||
477 | setup_git_directory(); | |
478 | ||
479 | /* common-main already logs `argv` */ | |
480 | trace2_def_repo(the_repository); | |
481 | ||
482 | if (!branch && !(branch = remote_default_branch(url))) { | |
483 | res = error(_("failed to get default branch for '%s'"), url); | |
484 | goto cleanup; | |
485 | } | |
486 | ||
487 | if (set_config("remote.origin.url=%s", url) || | |
488 | set_config("remote.origin.fetch=" | |
4368e40b JS |
489 | "+refs/heads/%s:refs/remotes/origin/%s", |
490 | single_branch ? branch : "*", | |
491 | single_branch ? branch : "*") || | |
546f822d JS |
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); | |
495 | goto cleanup; | |
496 | } | |
497 | ||
498 | if (!full_clone && | |
499 | (res = run_git("sparse-checkout", "init", "--cone", NULL))) | |
500 | goto cleanup; | |
501 | ||
cb59d55e | 502 | if (set_recommended_config(0)) |
546f822d JS |
503 | return error(_("could not configure '%s'"), dir); |
504 | ||
505 | if ((res = run_git("fetch", "--quiet", "origin", NULL))) { | |
506 | warning(_("partial clone failed; attempting full clone")); | |
507 | ||
508 | if (set_config("remote.origin.promisor") || | |
509 | set_config("remote.origin.partialCloneFilter")) { | |
510 | res = error(_("could not configure for full clone")); | |
511 | goto cleanup; | |
512 | } | |
513 | ||
514 | if ((res = run_git("fetch", "--quiet", "origin", NULL))) | |
515 | goto cleanup; | |
516 | } | |
517 | ||
518 | if ((res = set_config("branch.%s.remote=origin", branch))) | |
519 | goto cleanup; | |
520 | if ((res = set_config("branch.%s.merge=refs/heads/%s", | |
521 | branch, branch))) | |
522 | goto cleanup; | |
523 | ||
524 | strbuf_reset(&buf); | |
525 | strbuf_addf(&buf, "origin/%s", branch); | |
526 | res = run_git("checkout", "-f", "-t", buf.buf, NULL); | |
527 | if (res) | |
528 | goto cleanup; | |
529 | ||
530 | res = register_dir(); | |
531 | ||
532 | cleanup: | |
533 | free(enlistment); | |
534 | free(dir); | |
535 | strbuf_release(&buf); | |
536 | return res; | |
537 | } | |
538 | ||
aa5c79a3 JS |
539 | static int cmd_diagnose(int argc, const char **argv) |
540 | { | |
541 | struct option options[] = { | |
542 | OPT_END(), | |
543 | }; | |
544 | const char * const usage[] = { | |
545 | N_("scalar diagnose [<enlistment>]"), | |
546 | NULL | |
547 | }; | |
672196a3 | 548 | struct strbuf diagnostics_root = STRBUF_INIT; |
aa5c79a3 JS |
549 | int res = 0; |
550 | ||
551 | argc = parse_options(argc, argv, NULL, options, | |
552 | usage, 0); | |
553 | ||
672196a3 VD |
554 | setup_enlistment_directory(argc, argv, usage, options, &diagnostics_root); |
555 | strbuf_addstr(&diagnostics_root, "/.scalarDiagnostics"); | |
aa5c79a3 | 556 | |
672196a3 VD |
557 | res = run_git("diagnose", "--mode=all", "-s", "%Y%m%d_%H%M%S", |
558 | "-o", diagnostics_root.buf, NULL); | |
aa5c79a3 | 559 | |
672196a3 | 560 | strbuf_release(&diagnostics_root); |
aa5c79a3 JS |
561 | return res; |
562 | } | |
563 | ||
2b710457 DS |
564 | static int cmd_list(int argc, const char **argv) |
565 | { | |
566 | if (argc != 1) | |
567 | die(_("`scalar list` does not take arguments")); | |
568 | ||
569 | if (run_git("config", "--global", "--get-all", "scalar.repo", NULL) < 0) | |
570 | return -1; | |
571 | return 0; | |
572 | } | |
573 | ||
d0feac4e DS |
574 | static int cmd_register(int argc, const char **argv) |
575 | { | |
576 | struct option options[] = { | |
577 | OPT_END(), | |
578 | }; | |
579 | const char * const usage[] = { | |
580 | N_("scalar register [<enlistment>]"), | |
581 | NULL | |
582 | }; | |
583 | ||
584 | argc = parse_options(argc, argv, NULL, options, | |
585 | usage, 0); | |
586 | ||
587 | setup_enlistment_directory(argc, argv, usage, options, NULL); | |
588 | ||
589 | return register_dir(); | |
590 | } | |
0a43fb22 | 591 | |
45826760 JS |
592 | static int get_scalar_repos(const char *key, const char *value, void *data) |
593 | { | |
594 | struct string_list *list = data; | |
595 | ||
596 | if (!strcmp(key, "scalar.repo")) | |
597 | string_list_append(list, value); | |
598 | ||
599 | return 0; | |
600 | } | |
601 | ||
cb59d55e JS |
602 | static int cmd_reconfigure(int argc, const char **argv) |
603 | { | |
45826760 | 604 | int all = 0; |
cb59d55e | 605 | struct option options[] = { |
45826760 JS |
606 | OPT_BOOL('a', "all", &all, |
607 | N_("reconfigure all registered enlistments")), | |
cb59d55e JS |
608 | OPT_END(), |
609 | }; | |
610 | const char * const usage[] = { | |
45826760 | 611 | N_("scalar reconfigure [--all | <enlistment>]"), |
cb59d55e JS |
612 | NULL |
613 | }; | |
45826760 JS |
614 | struct string_list scalar_repos = STRING_LIST_INIT_DUP; |
615 | int i, res = 0; | |
616 | struct repository r = { NULL }; | |
617 | struct strbuf commondir = STRBUF_INIT, gitdir = STRBUF_INIT; | |
cb59d55e JS |
618 | |
619 | argc = parse_options(argc, argv, NULL, options, | |
620 | usage, 0); | |
621 | ||
45826760 JS |
622 | if (!all) { |
623 | setup_enlistment_directory(argc, argv, usage, options, NULL); | |
624 | ||
625 | return set_recommended_config(1); | |
626 | } | |
627 | ||
628 | if (argc > 0) | |
629 | usage_msg_opt(_("--all or <enlistment>, but not both"), | |
630 | usage, options); | |
631 | ||
632 | git_config(get_scalar_repos, &scalar_repos); | |
cb59d55e | 633 | |
45826760 JS |
634 | for (i = 0; i < scalar_repos.nr; i++) { |
635 | const char *dir = scalar_repos.items[i].string; | |
636 | ||
637 | strbuf_reset(&commondir); | |
638 | strbuf_reset(&gitdir); | |
639 | ||
640 | if (chdir(dir) < 0) { | |
641 | warning_errno(_("could not switch to '%s'"), dir); | |
642 | res = -1; | |
643 | } else if (discover_git_directory(&commondir, &gitdir) < 0) { | |
644 | warning_errno(_("git repository gone in '%s'"), dir); | |
645 | res = -1; | |
646 | } else { | |
647 | git_config_clear(); | |
648 | ||
649 | the_repository = &r; | |
650 | r.commondir = commondir.buf; | |
651 | r.gitdir = gitdir.buf; | |
652 | ||
653 | if (set_recommended_config(1) < 0) | |
654 | res = -1; | |
655 | } | |
656 | } | |
657 | ||
658 | string_list_clear(&scalar_repos, 1); | |
659 | strbuf_release(&commondir); | |
660 | strbuf_release(&gitdir); | |
661 | ||
662 | return res; | |
cb59d55e JS |
663 | } |
664 | ||
7020c88c DS |
665 | static int cmd_run(int argc, const char **argv) |
666 | { | |
667 | struct option options[] = { | |
668 | OPT_END(), | |
669 | }; | |
670 | struct { | |
671 | const char *arg, *task; | |
672 | } tasks[] = { | |
673 | { "config", NULL }, | |
674 | { "commit-graph", "commit-graph" }, | |
675 | { "fetch", "prefetch" }, | |
676 | { "loose-objects", "loose-objects" }, | |
677 | { "pack-files", "incremental-repack" }, | |
678 | { NULL, NULL } | |
679 | }; | |
680 | struct strbuf buf = STRBUF_INIT; | |
681 | const char *usagestr[] = { NULL, NULL }; | |
682 | int i; | |
683 | ||
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; | |
688 | ||
689 | argc = parse_options(argc, argv, NULL, options, | |
690 | usagestr, 0); | |
691 | ||
692 | if (!argc) | |
693 | usage_with_options(usagestr, options); | |
694 | ||
695 | if (!strcmp("all", argv[0])) { | |
696 | i = -1; | |
697 | } else { | |
698 | for (i = 0; tasks[i].arg && strcmp(tasks[i].arg, argv[0]); i++) | |
699 | ; /* keep looking for the task */ | |
700 | ||
701 | if (i > 0 && !tasks[i].arg) { | |
702 | error(_("no such task: '%s'"), argv[0]); | |
703 | usage_with_options(usagestr, options); | |
704 | } | |
705 | } | |
706 | ||
707 | argc--; | |
708 | argv++; | |
709 | setup_enlistment_directory(argc, argv, usagestr, options, NULL); | |
710 | strbuf_release(&buf); | |
711 | ||
712 | if (i == 0) | |
713 | return register_dir(); | |
714 | ||
715 | if (i > 0) | |
716 | return run_git("maintenance", "run", | |
717 | "--task", tasks[i].task, NULL); | |
718 | ||
719 | if (register_dir()) | |
720 | return -1; | |
721 | for (i = 1; tasks[i].arg; i++) | |
722 | if (run_git("maintenance", "run", | |
723 | "--task", tasks[i].task, NULL)) | |
724 | return -1; | |
725 | return 0; | |
726 | } | |
727 | ||
f5f0842d JS |
728 | static int remove_deleted_enlistment(struct strbuf *path) |
729 | { | |
730 | int res = 0; | |
731 | strbuf_realpath_forgiving(path, path->buf, 1); | |
732 | ||
733 | if (run_git("config", "--global", | |
734 | "--unset", "--fixed-value", | |
735 | "scalar.repo", path->buf, NULL) < 0) | |
736 | res = -1; | |
737 | ||
738 | if (run_git("config", "--global", | |
739 | "--unset", "--fixed-value", | |
740 | "maintenance.repo", path->buf, NULL) < 0) | |
741 | res = -1; | |
742 | ||
743 | return res; | |
744 | } | |
745 | ||
c76a53eb DS |
746 | static int cmd_unregister(int argc, const char **argv) |
747 | { | |
748 | struct option options[] = { | |
749 | OPT_END(), | |
750 | }; | |
751 | const char * const usage[] = { | |
752 | N_("scalar unregister [<enlistment>]"), | |
753 | NULL | |
754 | }; | |
755 | ||
756 | argc = parse_options(argc, argv, NULL, options, | |
757 | usage, 0); | |
758 | ||
f5f0842d JS |
759 | /* |
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. | |
763 | */ | |
764 | if (argc == 1) { | |
765 | struct strbuf src_path = STRBUF_INIT, workdir_path = STRBUF_INIT; | |
766 | ||
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 */ | |
771 | int res = -1; | |
772 | ||
773 | strbuf_strip_suffix(&src_path, "/.git"); | |
774 | res = remove_deleted_enlistment(&src_path) && res; | |
775 | ||
776 | strbuf_strip_suffix(&workdir_path, "/.git"); | |
777 | res = remove_deleted_enlistment(&workdir_path) && res; | |
778 | ||
779 | strbuf_release(&src_path); | |
780 | strbuf_release(&workdir_path); | |
781 | return res; | |
782 | } | |
783 | strbuf_release(&src_path); | |
784 | strbuf_release(&workdir_path); | |
785 | } | |
786 | ||
c76a53eb DS |
787 | setup_enlistment_directory(argc, argv, usage, options, NULL); |
788 | ||
789 | return unregister_dir(); | |
790 | } | |
791 | ||
d85ada7c MJC |
792 | static int cmd_delete(int argc, const char **argv) |
793 | { | |
794 | char *cwd = xgetcwd(); | |
795 | struct option options[] = { | |
796 | OPT_END(), | |
797 | }; | |
798 | const char * const usage[] = { | |
799 | N_("scalar delete <enlistment>"), | |
800 | NULL | |
801 | }; | |
802 | struct strbuf enlistment = STRBUF_INIT; | |
803 | int res = 0; | |
804 | ||
805 | argc = parse_options(argc, argv, NULL, options, | |
806 | usage, 0); | |
807 | ||
808 | if (argc != 1) | |
809 | usage_with_options(usage, options); | |
810 | ||
811 | setup_enlistment_directory(argc, argv, usage, options, &enlistment); | |
812 | ||
813 | if (dir_inside_of(cwd, enlistment.buf) >= 0) | |
814 | res = error(_("refusing to delete current working directory")); | |
815 | else { | |
816 | close_object_store(the_repository->objects); | |
817 | res = delete_enlistment(&enlistment); | |
818 | } | |
819 | strbuf_release(&enlistment); | |
820 | free(cwd); | |
821 | ||
822 | return res; | |
823 | } | |
824 | ||
951759d3 JS |
825 | static int cmd_help(int argc, const char **argv) |
826 | { | |
827 | struct option options[] = { | |
828 | OPT_END(), | |
829 | }; | |
830 | const char * const usage[] = { | |
831 | "scalar help", | |
832 | NULL | |
833 | }; | |
834 | ||
835 | argc = parse_options(argc, argv, NULL, options, | |
836 | usage, 0); | |
837 | ||
838 | if (argc != 0) | |
839 | usage_with_options(usage, options); | |
840 | ||
841 | return run_git("help", "scalar", NULL); | |
842 | } | |
843 | ||
ddc35d83 JS |
844 | static int cmd_version(int argc, const char **argv) |
845 | { | |
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")), | |
851 | OPT_END(), | |
852 | }; | |
853 | const char * const usage[] = { | |
854 | N_("scalar verbose [-v | --verbose] [--build-options]"), | |
855 | NULL | |
856 | }; | |
857 | struct strbuf buf = STRBUF_INIT; | |
858 | ||
859 | argc = parse_options(argc, argv, NULL, options, | |
860 | usage, 0); | |
861 | ||
862 | if (argc != 0) | |
863 | usage_with_options(usage, options); | |
864 | ||
865 | get_version_info(&buf, build_options); | |
866 | fprintf(stderr, "%s\n", buf.buf); | |
867 | strbuf_release(&buf); | |
868 | ||
869 | return 0; | |
870 | } | |
871 | ||
0a43fb22 JS |
872 | static struct { |
873 | const char *name; | |
874 | int (*fn)(int, const char **); | |
875 | } builtins[] = { | |
546f822d | 876 | { "clone", cmd_clone }, |
2b710457 | 877 | { "list", cmd_list }, |
d0feac4e | 878 | { "register", cmd_register }, |
c76a53eb | 879 | { "unregister", cmd_unregister }, |
7020c88c | 880 | { "run", cmd_run }, |
cb59d55e | 881 | { "reconfigure", cmd_reconfigure }, |
d85ada7c | 882 | { "delete", cmd_delete }, |
951759d3 | 883 | { "help", cmd_help }, |
ddc35d83 | 884 | { "version", cmd_version }, |
aa5c79a3 | 885 | { "diagnose", cmd_diagnose }, |
0a43fb22 JS |
886 | { NULL, NULL}, |
887 | }; | |
888 | ||
889 | int cmd_main(int argc, const char **argv) | |
890 | { | |
891 | struct strbuf scalar_usage = STRBUF_INIT; | |
892 | int i; | |
893 | ||
2ae8eb5d JS |
894 | while (argc > 1 && *argv[1] == '-') { |
895 | if (!strcmp(argv[1], "-C")) { | |
896 | if (argc < 3) | |
897 | die(_("-C requires a <directory>")); | |
898 | if (chdir(argv[2]) < 0) | |
899 | die_errno(_("could not change to '%s'"), | |
900 | argv[2]); | |
901 | argc -= 2; | |
902 | argv += 2; | |
903 | } else if (!strcmp(argv[1], "-c")) { | |
904 | if (argc < 3) | |
905 | die(_("-c requires a <key>=<value> argument")); | |
906 | git_config_push_parameter(argv[2]); | |
907 | argc -= 2; | |
908 | argv += 2; | |
909 | } else | |
910 | break; | |
911 | } | |
912 | ||
0a43fb22 JS |
913 | if (argc > 1) { |
914 | argv++; | |
915 | argc--; | |
916 | ||
917 | for (i = 0; builtins[i].name; i++) | |
918 | if (!strcmp(builtins[i].name, argv[0])) | |
919 | return !!builtins[i].fn(argc, argv); | |
920 | } | |
921 | ||
922 | strbuf_addstr(&scalar_usage, | |
2ae8eb5d JS |
923 | N_("scalar [-C <directory>] [-c <key>=<value>] " |
924 | "<command> [<options>]\n\nCommands:\n")); | |
0a43fb22 JS |
925 | for (i = 0; builtins[i].name; i++) |
926 | strbuf_addf(&scalar_usage, "\t%s\n", builtins[i].name); | |
927 | ||
928 | usage(scalar_usage.buf); | |
929 | } |