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