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