]>
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" | |
546f822d | 10 | #include "refs.h" |
d85ada7c MJC |
11 | #include "dir.h" |
12 | #include "packfile.h" | |
ddc35d83 | 13 | #include "help.h" |
aa5c79a3 | 14 | #include "archive.h" |
93e804b2 | 15 | #include "object-store.h" |
d0feac4e | 16 | |
d0feac4e DS |
17 | static void setup_enlistment_directory(int argc, const char **argv, |
18 | const char * const *usagestr, | |
19 | const struct option *options, | |
20 | struct strbuf *enlistment_root) | |
21 | { | |
22 | struct strbuf path = STRBUF_INIT; | |
65f6a9eb VD |
23 | int enlistment_is_repo_parent = 0; |
24 | size_t len; | |
d0feac4e DS |
25 | |
26 | if (startup_info->have_repository) | |
27 | BUG("gitdir already set up?!?"); | |
28 | ||
29 | if (argc > 1) | |
30 | usage_with_options(usagestr, options); | |
31 | ||
32 | /* find the worktree, determine its corresponding root */ | |
b4485574 | 33 | if (argc == 1) { |
d0feac4e | 34 | strbuf_add_absolute_path(&path, argv[0]); |
b4485574 JS |
35 | if (!is_directory(path.buf)) |
36 | die(_("'%s' does not exist"), path.buf); | |
65f6a9eb VD |
37 | if (chdir(path.buf) < 0) |
38 | die_errno(_("could not switch to '%s'"), path.buf); | |
b4485574 | 39 | } else if (strbuf_getcwd(&path) < 0) |
d0feac4e DS |
40 | die(_("need a working directory")); |
41 | ||
42 | strbuf_trim_trailing_dir_sep(&path); | |
d0feac4e | 43 | |
65f6a9eb VD |
44 | /* check if currently in enlistment root with src/ workdir */ |
45 | len = path.len; | |
46 | strbuf_addstr(&path, "/src"); | |
47 | if (is_nonbare_repository_dir(&path)) { | |
48 | enlistment_is_repo_parent = 1; | |
49 | if (chdir(path.buf) < 0) | |
50 | die_errno(_("could not switch to '%s'"), path.buf); | |
51 | } | |
52 | strbuf_setlen(&path, len); | |
d0feac4e | 53 | |
65f6a9eb | 54 | setup_git_directory(); |
d0feac4e | 55 | |
65f6a9eb VD |
56 | if (!the_repository->worktree) |
57 | die(_("Scalar enlistments require a worktree")); | |
d0feac4e | 58 | |
65f6a9eb VD |
59 | if (enlistment_root) { |
60 | if (enlistment_is_repo_parent) | |
61 | strbuf_addbuf(enlistment_root, &path); | |
62 | else | |
63 | strbuf_addstr(enlistment_root, the_repository->worktree); | |
64 | } | |
d0feac4e DS |
65 | |
66 | strbuf_release(&path); | |
d0feac4e DS |
67 | } |
68 | ||
69 | static int run_git(const char *arg, ...) | |
70 | { | |
71 | struct strvec argv = STRVEC_INIT; | |
72 | va_list args; | |
73 | const char *p; | |
74 | int res; | |
75 | ||
76 | va_start(args, arg); | |
77 | strvec_push(&argv, arg); | |
78 | while ((p = va_arg(args, const char *))) | |
79 | strvec_push(&argv, p); | |
80 | va_end(args); | |
81 | ||
82 | res = run_command_v_opt(argv.v, RUN_GIT_CMD); | |
83 | ||
84 | strvec_clear(&argv); | |
85 | return res; | |
86 | } | |
87 | ||
cb59d55e | 88 | static int set_recommended_config(int reconfigure) |
d0feac4e DS |
89 | { |
90 | struct { | |
91 | const char *key; | |
92 | const char *value; | |
cb59d55e | 93 | int overwrite_on_reconfigure; |
d0feac4e | 94 | } config[] = { |
cb59d55e JS |
95 | /* Required */ |
96 | { "am.keepCR", "true", 1 }, | |
97 | { "core.FSCache", "true", 1 }, | |
98 | { "core.multiPackIndex", "true", 1 }, | |
99 | { "core.preloadIndex", "true", 1 }, | |
d0feac4e | 100 | #ifndef WIN32 |
cb59d55e | 101 | { "core.untrackedCache", "true", 1 }, |
d0feac4e DS |
102 | #else |
103 | /* | |
104 | * Unfortunately, Scalar's Functional Tests demonstrated | |
105 | * that the untracked cache feature is unreliable on Windows | |
106 | * (which is a bummer because that platform would benefit the | |
107 | * most from it). For some reason, freshly created files seem | |
108 | * not to update the directory's `lastModified` time | |
109 | * immediately, but the untracked cache would need to rely on | |
110 | * that. | |
111 | * | |
112 | * Therefore, with a sad heart, we disable this very useful | |
113 | * feature on Windows. | |
114 | */ | |
cb59d55e | 115 | { "core.untrackedCache", "false", 1 }, |
d0feac4e | 116 | #endif |
cb59d55e JS |
117 | { "core.logAllRefUpdates", "true", 1 }, |
118 | { "credential.https://dev.azure.com.useHttpPath", "true", 1 }, | |
119 | { "credential.validate", "false", 1 }, /* GCM4W-only */ | |
120 | { "gc.auto", "0", 1 }, | |
121 | { "gui.GCWarning", "false", 1 }, | |
122 | { "index.threads", "true", 1 }, | |
123 | { "index.version", "4", 1 }, | |
124 | { "merge.stat", "false", 1 }, | |
125 | { "merge.renames", "true", 1 }, | |
126 | { "pack.useBitmaps", "false", 1 }, | |
127 | { "pack.useSparse", "true", 1 }, | |
128 | { "receive.autoGC", "false", 1 }, | |
cb59d55e JS |
129 | { "feature.manyFiles", "false", 1 }, |
130 | { "feature.experimental", "false", 1 }, | |
131 | { "fetch.unpackLimit", "1", 1 }, | |
132 | { "fetch.writeCommitGraph", "false", 1 }, | |
d0feac4e | 133 | #ifdef WIN32 |
cb59d55e | 134 | { "http.sslBackend", "schannel", 1 }, |
d0feac4e | 135 | #endif |
cb59d55e | 136 | /* Optional */ |
d0feac4e DS |
137 | { "status.aheadBehind", "false" }, |
138 | { "commitGraph.generationVersion", "1" }, | |
139 | { "core.autoCRLF", "false" }, | |
140 | { "core.safeCRLF", "false" }, | |
141 | { "fetch.showForcedUpdates", "false" }, | |
142 | { NULL, NULL }, | |
143 | }; | |
144 | int i; | |
145 | char *value; | |
146 | ||
147 | for (i = 0; config[i].key; i++) { | |
cb59d55e JS |
148 | if ((reconfigure && config[i].overwrite_on_reconfigure) || |
149 | git_config_get_string(config[i].key, &value)) { | |
d0feac4e DS |
150 | trace2_data_string("scalar", the_repository, config[i].key, "created"); |
151 | if (git_config_set_gently(config[i].key, | |
152 | config[i].value) < 0) | |
153 | return error(_("could not configure %s=%s"), | |
154 | config[i].key, config[i].value); | |
155 | } else { | |
156 | trace2_data_string("scalar", the_repository, config[i].key, "exists"); | |
157 | free(value); | |
158 | } | |
159 | } | |
160 | ||
161 | /* | |
162 | * The `log.excludeDecoration` setting is special because it allows | |
163 | * for multiple values. | |
164 | */ | |
165 | if (git_config_get_string("log.excludeDecoration", &value)) { | |
166 | trace2_data_string("scalar", the_repository, | |
167 | "log.excludeDecoration", "created"); | |
168 | if (git_config_set_multivar_gently("log.excludeDecoration", | |
169 | "refs/prefetch/*", | |
170 | CONFIG_REGEX_NONE, 0)) | |
171 | return error(_("could not configure " | |
172 | "log.excludeDecoration")); | |
173 | } else { | |
174 | trace2_data_string("scalar", the_repository, | |
175 | "log.excludeDecoration", "exists"); | |
176 | free(value); | |
177 | } | |
178 | ||
179 | return 0; | |
180 | } | |
181 | ||
c76a53eb | 182 | static int toggle_maintenance(int enable) |
d0feac4e | 183 | { |
c76a53eb | 184 | return run_git("maintenance", enable ? "start" : "unregister", NULL); |
d0feac4e DS |
185 | } |
186 | ||
c76a53eb | 187 | static int add_or_remove_enlistment(int add) |
d0feac4e DS |
188 | { |
189 | int res; | |
190 | ||
191 | if (!the_repository->worktree) | |
192 | die(_("Scalar enlistments require a worktree")); | |
193 | ||
194 | res = run_git("config", "--global", "--get", "--fixed-value", | |
195 | "scalar.repo", the_repository->worktree, NULL); | |
196 | ||
197 | /* | |
c76a53eb DS |
198 | * If we want to add and the setting is already there, then do nothing. |
199 | * If we want to remove and the setting is not there, then do nothing. | |
d0feac4e | 200 | */ |
c76a53eb | 201 | if ((add && !res) || (!add && res)) |
d0feac4e DS |
202 | return 0; |
203 | ||
c76a53eb DS |
204 | return run_git("config", "--global", add ? "--add" : "--unset", |
205 | add ? "--no-fixed-value" : "--fixed-value", | |
d0feac4e DS |
206 | "scalar.repo", the_repository->worktree, NULL); |
207 | } | |
208 | ||
209 | static int register_dir(void) | |
210 | { | |
d2a79bc9 VD |
211 | if (add_or_remove_enlistment(1)) |
212 | return error(_("could not add enlistment")); | |
d0feac4e | 213 | |
d2a79bc9 VD |
214 | if (set_recommended_config(0)) |
215 | return error(_("could not set recommended config")); | |
d0feac4e | 216 | |
d2a79bc9 VD |
217 | if (toggle_maintenance(1)) |
218 | return error(_("could not turn on maintenance")); | |
c76a53eb | 219 | |
d2a79bc9 | 220 | return 0; |
c76a53eb DS |
221 | } |
222 | ||
223 | static int unregister_dir(void) | |
224 | { | |
225 | int res = 0; | |
226 | ||
adedcee8 | 227 | if (toggle_maintenance(0)) |
d2a79bc9 | 228 | res = error(_("could not turn off maintenance")); |
c76a53eb | 229 | |
adedcee8 | 230 | if (add_or_remove_enlistment(0)) |
d2a79bc9 | 231 | res = error(_("could not remove enlistment")); |
d0feac4e DS |
232 | |
233 | return res; | |
234 | } | |
235 | ||
aa5c79a3 JS |
236 | static int add_directory_to_archiver(struct strvec *archiver_args, |
237 | const char *path, int recurse) | |
238 | { | |
239 | int at_root = !*path; | |
240 | DIR *dir = opendir(at_root ? "." : path); | |
241 | struct dirent *e; | |
242 | struct strbuf buf = STRBUF_INIT; | |
243 | size_t len; | |
244 | int res = 0; | |
245 | ||
246 | if (!dir) | |
247 | return error_errno(_("could not open directory '%s'"), path); | |
248 | ||
249 | if (!at_root) | |
250 | strbuf_addf(&buf, "%s/", path); | |
251 | len = buf.len; | |
252 | strvec_pushf(archiver_args, "--prefix=%s", buf.buf); | |
253 | ||
254 | while (!res && (e = readdir(dir))) { | |
255 | if (!strcmp(".", e->d_name) || !strcmp("..", e->d_name)) | |
256 | continue; | |
257 | ||
258 | strbuf_setlen(&buf, len); | |
259 | strbuf_addstr(&buf, e->d_name); | |
260 | ||
261 | if (e->d_type == DT_REG) | |
262 | strvec_pushf(archiver_args, "--add-file=%s", buf.buf); | |
263 | else if (e->d_type != DT_DIR) | |
264 | warning(_("skipping '%s', which is neither file nor " | |
265 | "directory"), buf.buf); | |
266 | else if (recurse && | |
267 | add_directory_to_archiver(archiver_args, | |
268 | buf.buf, recurse) < 0) | |
269 | res = -1; | |
270 | } | |
271 | ||
272 | closedir(dir); | |
273 | strbuf_release(&buf); | |
274 | return res; | |
275 | } | |
276 | ||
0ed5b13f JS |
277 | #ifndef WIN32 |
278 | #include <sys/statvfs.h> | |
279 | #endif | |
280 | ||
281 | static int get_disk_info(struct strbuf *out) | |
282 | { | |
283 | #ifdef WIN32 | |
284 | struct strbuf buf = STRBUF_INIT; | |
285 | char volume_name[MAX_PATH], fs_name[MAX_PATH]; | |
286 | DWORD serial_number, component_length, flags; | |
287 | ULARGE_INTEGER avail2caller, total, avail; | |
288 | ||
289 | strbuf_realpath(&buf, ".", 1); | |
290 | if (!GetDiskFreeSpaceExA(buf.buf, &avail2caller, &total, &avail)) { | |
291 | error(_("could not determine free disk size for '%s'"), | |
292 | buf.buf); | |
293 | strbuf_release(&buf); | |
294 | return -1; | |
295 | } | |
296 | ||
297 | strbuf_setlen(&buf, offset_1st_component(buf.buf)); | |
298 | if (!GetVolumeInformationA(buf.buf, volume_name, sizeof(volume_name), | |
299 | &serial_number, &component_length, &flags, | |
300 | fs_name, sizeof(fs_name))) { | |
301 | error(_("could not get info for '%s'"), buf.buf); | |
302 | strbuf_release(&buf); | |
303 | return -1; | |
304 | } | |
305 | strbuf_addf(out, "Available space on '%s': ", buf.buf); | |
306 | strbuf_humanise_bytes(out, avail2caller.QuadPart); | |
307 | strbuf_addch(out, '\n'); | |
308 | strbuf_release(&buf); | |
309 | #else | |
310 | struct strbuf buf = STRBUF_INIT; | |
311 | struct statvfs stat; | |
312 | ||
313 | strbuf_realpath(&buf, ".", 1); | |
314 | if (statvfs(buf.buf, &stat) < 0) { | |
315 | error_errno(_("could not determine free disk size for '%s'"), | |
316 | buf.buf); | |
317 | strbuf_release(&buf); | |
318 | return -1; | |
319 | } | |
320 | ||
321 | strbuf_addf(out, "Available space on '%s': ", buf.buf); | |
322 | strbuf_humanise_bytes(out, st_mult(stat.f_bsize, stat.f_bavail)); | |
323 | strbuf_addf(out, " (mount flags 0x%lx)\n", stat.f_flag); | |
324 | strbuf_release(&buf); | |
325 | #endif | |
326 | return 0; | |
327 | } | |
328 | ||
546f822d JS |
329 | /* printf-style interface, expects `<key>=<value>` argument */ |
330 | static int set_config(const char *fmt, ...) | |
331 | { | |
332 | struct strbuf buf = STRBUF_INIT; | |
333 | char *value; | |
334 | int res; | |
335 | va_list args; | |
336 | ||
337 | va_start(args, fmt); | |
338 | strbuf_vaddf(&buf, fmt, args); | |
339 | va_end(args); | |
340 | ||
341 | value = strchr(buf.buf, '='); | |
342 | if (value) | |
343 | *(value++) = '\0'; | |
344 | res = git_config_set_gently(buf.buf, value); | |
345 | strbuf_release(&buf); | |
346 | ||
347 | return res; | |
348 | } | |
349 | ||
350 | static char *remote_default_branch(const char *url) | |
351 | { | |
352 | struct child_process cp = CHILD_PROCESS_INIT; | |
353 | struct strbuf out = STRBUF_INIT; | |
354 | ||
355 | cp.git_cmd = 1; | |
356 | strvec_pushl(&cp.args, "ls-remote", "--symref", url, "HEAD", NULL); | |
357 | if (!pipe_command(&cp, NULL, 0, &out, 0, NULL, 0)) { | |
358 | const char *line = out.buf; | |
359 | ||
360 | while (*line) { | |
361 | const char *eol = strchrnul(line, '\n'), *p; | |
362 | size_t len = eol - line; | |
363 | char *branch; | |
364 | ||
365 | if (!skip_prefix(line, "ref: ", &p) || | |
366 | !strip_suffix_mem(line, &len, "\tHEAD")) { | |
367 | line = eol + (*eol == '\n'); | |
368 | continue; | |
369 | } | |
370 | ||
371 | eol = line + len; | |
372 | if (skip_prefix(p, "refs/heads/", &p)) { | |
373 | branch = xstrndup(p, eol - p); | |
374 | strbuf_release(&out); | |
375 | return branch; | |
376 | } | |
377 | ||
378 | error(_("remote HEAD is not a branch: '%.*s'"), | |
379 | (int)(eol - p), p); | |
380 | strbuf_release(&out); | |
381 | return NULL; | |
382 | } | |
383 | } | |
384 | warning(_("failed to get default branch name from remote; " | |
385 | "using local default")); | |
386 | strbuf_reset(&out); | |
387 | ||
388 | child_process_init(&cp); | |
389 | cp.git_cmd = 1; | |
390 | strvec_pushl(&cp.args, "symbolic-ref", "--short", "HEAD", NULL); | |
391 | if (!pipe_command(&cp, NULL, 0, &out, 0, NULL, 0)) { | |
392 | strbuf_trim(&out); | |
393 | return strbuf_detach(&out, NULL); | |
394 | } | |
395 | ||
396 | strbuf_release(&out); | |
397 | error(_("failed to get default branch name")); | |
398 | return NULL; | |
399 | } | |
400 | ||
d85ada7c MJC |
401 | static int delete_enlistment(struct strbuf *enlistment) |
402 | { | |
403 | #ifdef WIN32 | |
404 | struct strbuf parent = STRBUF_INIT; | |
65f6a9eb VD |
405 | size_t offset; |
406 | char *path_sep; | |
d85ada7c MJC |
407 | #endif |
408 | ||
409 | if (unregister_dir()) | |
410 | die(_("failed to unregister repository")); | |
411 | ||
412 | #ifdef WIN32 | |
413 | /* | |
414 | * Change the current directory to one outside of the enlistment so | |
415 | * that we may delete everything underneath it. | |
416 | */ | |
65f6a9eb VD |
417 | offset = offset_1st_component(enlistment->buf); |
418 | path_sep = find_last_dir_sep(enlistment->buf + offset); | |
419 | strbuf_add(&parent, enlistment->buf, | |
420 | path_sep ? path_sep - enlistment->buf : offset); | |
d85ada7c MJC |
421 | if (chdir(parent.buf) < 0) |
422 | die_errno(_("could not switch to '%s'"), parent.buf); | |
423 | strbuf_release(&parent); | |
424 | #endif | |
425 | ||
426 | if (remove_dir_recursively(enlistment, 0)) | |
427 | die(_("failed to delete enlistment directory")); | |
428 | ||
429 | return 0; | |
430 | } | |
431 | ||
ddc35d83 JS |
432 | /* |
433 | * Dummy implementation; Using `get_version_info()` would cause a link error | |
434 | * without this. | |
435 | */ | |
436 | void load_builtin_commands(const char *prefix, struct cmdnames *cmds) | |
437 | { | |
438 | die("not implemented"); | |
439 | } | |
440 | ||
546f822d JS |
441 | static int cmd_clone(int argc, const char **argv) |
442 | { | |
443 | const char *branch = NULL; | |
4368e40b | 444 | int full_clone = 0, single_branch = 0; |
546f822d JS |
445 | struct option clone_options[] = { |
446 | OPT_STRING('b', "branch", &branch, N_("<branch>"), | |
447 | N_("branch to checkout after clone")), | |
448 | OPT_BOOL(0, "full-clone", &full_clone, | |
449 | N_("when cloning, create full working directory")), | |
4368e40b JS |
450 | OPT_BOOL(0, "single-branch", &single_branch, |
451 | N_("only download metadata for the branch that will " | |
452 | "be checked out")), | |
546f822d JS |
453 | OPT_END(), |
454 | }; | |
455 | const char * const clone_usage[] = { | |
456 | N_("scalar clone [<options>] [--] <repo> [<dir>]"), | |
457 | NULL | |
458 | }; | |
459 | const char *url; | |
460 | char *enlistment = NULL, *dir = NULL; | |
461 | struct strbuf buf = STRBUF_INIT; | |
462 | int res; | |
463 | ||
464 | argc = parse_options(argc, argv, NULL, clone_options, clone_usage, 0); | |
465 | ||
466 | if (argc == 2) { | |
467 | url = argv[0]; | |
468 | enlistment = xstrdup(argv[1]); | |
469 | } else if (argc == 1) { | |
470 | url = argv[0]; | |
471 | ||
472 | strbuf_addstr(&buf, url); | |
473 | /* Strip trailing slashes, if any */ | |
474 | while (buf.len > 0 && is_dir_sep(buf.buf[buf.len - 1])) | |
475 | strbuf_setlen(&buf, buf.len - 1); | |
476 | /* Strip suffix `.git`, if any */ | |
477 | strbuf_strip_suffix(&buf, ".git"); | |
478 | ||
479 | enlistment = find_last_dir_sep(buf.buf); | |
480 | if (!enlistment) { | |
481 | die(_("cannot deduce worktree name from '%s'"), url); | |
482 | } | |
483 | enlistment = xstrdup(enlistment + 1); | |
484 | } else { | |
485 | usage_msg_opt(_("You must specify a repository to clone."), | |
486 | clone_usage, clone_options); | |
487 | } | |
488 | ||
489 | if (is_directory(enlistment)) | |
490 | die(_("directory '%s' exists already"), enlistment); | |
491 | ||
492 | dir = xstrfmt("%s/src", enlistment); | |
493 | ||
494 | strbuf_reset(&buf); | |
495 | if (branch) | |
496 | strbuf_addf(&buf, "init.defaultBranch=%s", branch); | |
497 | else { | |
498 | char *b = repo_default_branch_name(the_repository, 1); | |
499 | strbuf_addf(&buf, "init.defaultBranch=%s", b); | |
500 | free(b); | |
501 | } | |
502 | ||
503 | if ((res = run_git("-c", buf.buf, "init", "--", dir, NULL))) | |
504 | goto cleanup; | |
505 | ||
506 | if (chdir(dir) < 0) { | |
507 | res = error_errno(_("could not switch to '%s'"), dir); | |
508 | goto cleanup; | |
509 | } | |
510 | ||
511 | setup_git_directory(); | |
512 | ||
513 | /* common-main already logs `argv` */ | |
514 | trace2_def_repo(the_repository); | |
515 | ||
516 | if (!branch && !(branch = remote_default_branch(url))) { | |
517 | res = error(_("failed to get default branch for '%s'"), url); | |
518 | goto cleanup; | |
519 | } | |
520 | ||
521 | if (set_config("remote.origin.url=%s", url) || | |
522 | set_config("remote.origin.fetch=" | |
4368e40b JS |
523 | "+refs/heads/%s:refs/remotes/origin/%s", |
524 | single_branch ? branch : "*", | |
525 | single_branch ? branch : "*") || | |
546f822d JS |
526 | set_config("remote.origin.promisor=true") || |
527 | set_config("remote.origin.partialCloneFilter=blob:none")) { | |
528 | res = error(_("could not configure remote in '%s'"), dir); | |
529 | goto cleanup; | |
530 | } | |
531 | ||
532 | if (!full_clone && | |
533 | (res = run_git("sparse-checkout", "init", "--cone", NULL))) | |
534 | goto cleanup; | |
535 | ||
cb59d55e | 536 | if (set_recommended_config(0)) |
546f822d JS |
537 | return error(_("could not configure '%s'"), dir); |
538 | ||
539 | if ((res = run_git("fetch", "--quiet", "origin", NULL))) { | |
540 | warning(_("partial clone failed; attempting full clone")); | |
541 | ||
542 | if (set_config("remote.origin.promisor") || | |
543 | set_config("remote.origin.partialCloneFilter")) { | |
544 | res = error(_("could not configure for full clone")); | |
545 | goto cleanup; | |
546 | } | |
547 | ||
548 | if ((res = run_git("fetch", "--quiet", "origin", NULL))) | |
549 | goto cleanup; | |
550 | } | |
551 | ||
552 | if ((res = set_config("branch.%s.remote=origin", branch))) | |
553 | goto cleanup; | |
554 | if ((res = set_config("branch.%s.merge=refs/heads/%s", | |
555 | branch, branch))) | |
556 | goto cleanup; | |
557 | ||
558 | strbuf_reset(&buf); | |
559 | strbuf_addf(&buf, "origin/%s", branch); | |
560 | res = run_git("checkout", "-f", "-t", buf.buf, NULL); | |
561 | if (res) | |
562 | goto cleanup; | |
563 | ||
564 | res = register_dir(); | |
565 | ||
566 | cleanup: | |
567 | free(enlistment); | |
568 | free(dir); | |
569 | strbuf_release(&buf); | |
570 | return res; | |
571 | } | |
572 | ||
93e804b2 MJC |
573 | static void dir_file_stats_objects(const char *full_path, size_t full_path_len, |
574 | const char *file_name, void *data) | |
575 | { | |
576 | struct strbuf *buf = data; | |
577 | struct stat st; | |
578 | ||
579 | if (!stat(full_path, &st)) | |
580 | strbuf_addf(buf, "%-70s %16" PRIuMAX "\n", file_name, | |
581 | (uintmax_t)st.st_size); | |
582 | } | |
583 | ||
584 | static int dir_file_stats(struct object_directory *object_dir, void *data) | |
585 | { | |
586 | struct strbuf *buf = data; | |
587 | ||
588 | strbuf_addf(buf, "Contents of %s:\n", object_dir->path); | |
589 | ||
590 | for_each_file_in_pack_dir(object_dir->path, dir_file_stats_objects, | |
591 | data); | |
592 | ||
593 | return 0; | |
594 | } | |
595 | ||
15d8adcc MJC |
596 | static int count_files(char *path) |
597 | { | |
598 | DIR *dir = opendir(path); | |
599 | struct dirent *e; | |
600 | int count = 0; | |
601 | ||
602 | if (!dir) | |
603 | return 0; | |
604 | ||
605 | while ((e = readdir(dir)) != NULL) | |
606 | if (!is_dot_or_dotdot(e->d_name) && e->d_type == DT_REG) | |
607 | count++; | |
608 | ||
609 | closedir(dir); | |
610 | return count; | |
611 | } | |
612 | ||
613 | static void loose_objs_stats(struct strbuf *buf, const char *path) | |
614 | { | |
615 | DIR *dir = opendir(path); | |
616 | struct dirent *e; | |
617 | int count; | |
618 | int total = 0; | |
619 | unsigned char c; | |
620 | struct strbuf count_path = STRBUF_INIT; | |
621 | size_t base_path_len; | |
622 | ||
623 | if (!dir) | |
624 | return; | |
625 | ||
626 | strbuf_addstr(buf, "Object directory stats for "); | |
627 | strbuf_add_absolute_path(buf, path); | |
628 | strbuf_addstr(buf, ":\n"); | |
629 | ||
630 | strbuf_add_absolute_path(&count_path, path); | |
631 | strbuf_addch(&count_path, '/'); | |
632 | base_path_len = count_path.len; | |
633 | ||
634 | while ((e = readdir(dir)) != NULL) | |
635 | if (!is_dot_or_dotdot(e->d_name) && | |
636 | e->d_type == DT_DIR && strlen(e->d_name) == 2 && | |
637 | !hex_to_bytes(&c, e->d_name, 1)) { | |
638 | strbuf_setlen(&count_path, base_path_len); | |
639 | strbuf_addstr(&count_path, e->d_name); | |
640 | total += (count = count_files(count_path.buf)); | |
641 | strbuf_addf(buf, "%s : %7d files\n", e->d_name, count); | |
642 | } | |
643 | ||
644 | strbuf_addf(buf, "Total: %d loose objects", total); | |
645 | ||
646 | strbuf_release(&count_path); | |
647 | closedir(dir); | |
648 | } | |
649 | ||
aa5c79a3 JS |
650 | static int cmd_diagnose(int argc, const char **argv) |
651 | { | |
652 | struct option options[] = { | |
653 | OPT_END(), | |
654 | }; | |
655 | const char * const usage[] = { | |
656 | N_("scalar diagnose [<enlistment>]"), | |
657 | NULL | |
658 | }; | |
659 | struct strbuf zip_path = STRBUF_INIT; | |
660 | struct strvec archiver_args = STRVEC_INIT; | |
661 | char **argv_copy = NULL; | |
662 | int stdout_fd = -1, archiver_fd = -1; | |
663 | time_t now = time(NULL); | |
664 | struct tm tm; | |
4f40f6cb | 665 | struct strbuf buf = STRBUF_INIT; |
aa5c79a3 JS |
666 | int res = 0; |
667 | ||
668 | argc = parse_options(argc, argv, NULL, options, | |
669 | usage, 0); | |
670 | ||
671 | setup_enlistment_directory(argc, argv, usage, options, &zip_path); | |
672 | ||
673 | strbuf_addstr(&zip_path, "/.scalarDiagnostics/scalar_"); | |
674 | strbuf_addftime(&zip_path, | |
675 | "%Y%m%d_%H%M%S", localtime_r(&now, &tm), 0, 0); | |
676 | strbuf_addstr(&zip_path, ".zip"); | |
677 | switch (safe_create_leading_directories(zip_path.buf)) { | |
678 | case SCLD_EXISTS: | |
679 | case SCLD_OK: | |
680 | break; | |
681 | default: | |
682 | error_errno(_("could not create directory for '%s'"), | |
683 | zip_path.buf); | |
684 | goto diagnose_cleanup; | |
685 | } | |
686 | stdout_fd = dup(1); | |
687 | if (stdout_fd < 0) { | |
688 | res = error_errno(_("could not duplicate stdout")); | |
689 | goto diagnose_cleanup; | |
690 | } | |
691 | ||
692 | archiver_fd = xopen(zip_path.buf, O_CREAT | O_WRONLY | O_TRUNC, 0666); | |
693 | if (archiver_fd < 0 || dup2(archiver_fd, 1) < 0) { | |
694 | res = error_errno(_("could not redirect output")); | |
695 | goto diagnose_cleanup; | |
696 | } | |
697 | ||
698 | init_zip_archiver(); | |
699 | strvec_pushl(&archiver_args, "scalar-diagnose", "--format=zip", NULL); | |
700 | ||
701 | strbuf_reset(&buf); | |
702 | strbuf_addstr(&buf, "Collecting diagnostic info\n\n"); | |
703 | get_version_info(&buf, 1); | |
704 | ||
705 | strbuf_addf(&buf, "Enlistment root: %s\n", the_repository->worktree); | |
0ed5b13f | 706 | get_disk_info(&buf); |
aa5c79a3 JS |
707 | write_or_die(stdout_fd, buf.buf, buf.len); |
708 | strvec_pushf(&archiver_args, | |
709 | "--add-virtual-file=diagnostics.log:%.*s", | |
710 | (int)buf.len, buf.buf); | |
711 | ||
93e804b2 MJC |
712 | strbuf_reset(&buf); |
713 | strbuf_addstr(&buf, "--add-virtual-file=packs-local.txt:"); | |
714 | dir_file_stats(the_repository->objects->odb, &buf); | |
715 | foreach_alt_odb(dir_file_stats, &buf); | |
716 | strvec_push(&archiver_args, buf.buf); | |
717 | ||
15d8adcc MJC |
718 | strbuf_reset(&buf); |
719 | strbuf_addstr(&buf, "--add-virtual-file=objects-local.txt:"); | |
720 | loose_objs_stats(&buf, ".git/objects"); | |
721 | strvec_push(&archiver_args, buf.buf); | |
722 | ||
aa5c79a3 JS |
723 | if ((res = add_directory_to_archiver(&archiver_args, ".git", 0)) || |
724 | (res = add_directory_to_archiver(&archiver_args, ".git/hooks", 0)) || | |
725 | (res = add_directory_to_archiver(&archiver_args, ".git/info", 0)) || | |
726 | (res = add_directory_to_archiver(&archiver_args, ".git/logs", 1)) || | |
727 | (res = add_directory_to_archiver(&archiver_args, ".git/objects/info", 0))) | |
728 | goto diagnose_cleanup; | |
729 | ||
730 | strvec_pushl(&archiver_args, "--prefix=", | |
731 | oid_to_hex(the_hash_algo->empty_tree), "--", NULL); | |
732 | ||
733 | /* `write_archive()` modifies the `argv` passed to it. Let it. */ | |
734 | argv_copy = xmemdupz(archiver_args.v, | |
735 | sizeof(char *) * archiver_args.nr); | |
736 | res = write_archive(archiver_args.nr, (const char **)argv_copy, NULL, | |
737 | the_repository, NULL, 0); | |
738 | if (res) { | |
739 | error(_("failed to write archive")); | |
740 | goto diagnose_cleanup; | |
741 | } | |
742 | ||
743 | if (!res) | |
744 | fprintf(stderr, "\n" | |
745 | "Diagnostics complete.\n" | |
746 | "All of the gathered info is captured in '%s'\n", | |
747 | zip_path.buf); | |
748 | ||
749 | diagnose_cleanup: | |
750 | if (archiver_fd >= 0) { | |
751 | close(1); | |
752 | dup2(stdout_fd, 1); | |
753 | } | |
754 | free(argv_copy); | |
755 | strvec_clear(&archiver_args); | |
756 | strbuf_release(&zip_path); | |
aa5c79a3 JS |
757 | strbuf_release(&buf); |
758 | ||
759 | return res; | |
760 | } | |
761 | ||
2b710457 DS |
762 | static int cmd_list(int argc, const char **argv) |
763 | { | |
764 | if (argc != 1) | |
765 | die(_("`scalar list` does not take arguments")); | |
766 | ||
767 | if (run_git("config", "--global", "--get-all", "scalar.repo", NULL) < 0) | |
768 | return -1; | |
769 | return 0; | |
770 | } | |
771 | ||
d0feac4e DS |
772 | static int cmd_register(int argc, const char **argv) |
773 | { | |
774 | struct option options[] = { | |
775 | OPT_END(), | |
776 | }; | |
777 | const char * const usage[] = { | |
778 | N_("scalar register [<enlistment>]"), | |
779 | NULL | |
780 | }; | |
781 | ||
782 | argc = parse_options(argc, argv, NULL, options, | |
783 | usage, 0); | |
784 | ||
785 | setup_enlistment_directory(argc, argv, usage, options, NULL); | |
786 | ||
787 | return register_dir(); | |
788 | } | |
0a43fb22 | 789 | |
45826760 JS |
790 | static int get_scalar_repos(const char *key, const char *value, void *data) |
791 | { | |
792 | struct string_list *list = data; | |
793 | ||
794 | if (!strcmp(key, "scalar.repo")) | |
795 | string_list_append(list, value); | |
796 | ||
797 | return 0; | |
798 | } | |
799 | ||
cb59d55e JS |
800 | static int cmd_reconfigure(int argc, const char **argv) |
801 | { | |
45826760 | 802 | int all = 0; |
cb59d55e | 803 | struct option options[] = { |
45826760 JS |
804 | OPT_BOOL('a', "all", &all, |
805 | N_("reconfigure all registered enlistments")), | |
cb59d55e JS |
806 | OPT_END(), |
807 | }; | |
808 | const char * const usage[] = { | |
45826760 | 809 | N_("scalar reconfigure [--all | <enlistment>]"), |
cb59d55e JS |
810 | NULL |
811 | }; | |
45826760 JS |
812 | struct string_list scalar_repos = STRING_LIST_INIT_DUP; |
813 | int i, res = 0; | |
814 | struct repository r = { NULL }; | |
815 | struct strbuf commondir = STRBUF_INIT, gitdir = STRBUF_INIT; | |
cb59d55e JS |
816 | |
817 | argc = parse_options(argc, argv, NULL, options, | |
818 | usage, 0); | |
819 | ||
45826760 JS |
820 | if (!all) { |
821 | setup_enlistment_directory(argc, argv, usage, options, NULL); | |
822 | ||
823 | return set_recommended_config(1); | |
824 | } | |
825 | ||
826 | if (argc > 0) | |
827 | usage_msg_opt(_("--all or <enlistment>, but not both"), | |
828 | usage, options); | |
829 | ||
830 | git_config(get_scalar_repos, &scalar_repos); | |
cb59d55e | 831 | |
45826760 JS |
832 | for (i = 0; i < scalar_repos.nr; i++) { |
833 | const char *dir = scalar_repos.items[i].string; | |
834 | ||
835 | strbuf_reset(&commondir); | |
836 | strbuf_reset(&gitdir); | |
837 | ||
838 | if (chdir(dir) < 0) { | |
839 | warning_errno(_("could not switch to '%s'"), dir); | |
840 | res = -1; | |
841 | } else if (discover_git_directory(&commondir, &gitdir) < 0) { | |
842 | warning_errno(_("git repository gone in '%s'"), dir); | |
843 | res = -1; | |
844 | } else { | |
845 | git_config_clear(); | |
846 | ||
847 | the_repository = &r; | |
848 | r.commondir = commondir.buf; | |
849 | r.gitdir = gitdir.buf; | |
850 | ||
851 | if (set_recommended_config(1) < 0) | |
852 | res = -1; | |
853 | } | |
854 | } | |
855 | ||
856 | string_list_clear(&scalar_repos, 1); | |
857 | strbuf_release(&commondir); | |
858 | strbuf_release(&gitdir); | |
859 | ||
860 | return res; | |
cb59d55e JS |
861 | } |
862 | ||
7020c88c DS |
863 | static int cmd_run(int argc, const char **argv) |
864 | { | |
865 | struct option options[] = { | |
866 | OPT_END(), | |
867 | }; | |
868 | struct { | |
869 | const char *arg, *task; | |
870 | } tasks[] = { | |
871 | { "config", NULL }, | |
872 | { "commit-graph", "commit-graph" }, | |
873 | { "fetch", "prefetch" }, | |
874 | { "loose-objects", "loose-objects" }, | |
875 | { "pack-files", "incremental-repack" }, | |
876 | { NULL, NULL } | |
877 | }; | |
878 | struct strbuf buf = STRBUF_INIT; | |
879 | const char *usagestr[] = { NULL, NULL }; | |
880 | int i; | |
881 | ||
882 | strbuf_addstr(&buf, N_("scalar run <task> [<enlistment>]\nTasks:\n")); | |
883 | for (i = 0; tasks[i].arg; i++) | |
884 | strbuf_addf(&buf, "\t%s\n", tasks[i].arg); | |
885 | usagestr[0] = buf.buf; | |
886 | ||
887 | argc = parse_options(argc, argv, NULL, options, | |
888 | usagestr, 0); | |
889 | ||
890 | if (!argc) | |
891 | usage_with_options(usagestr, options); | |
892 | ||
893 | if (!strcmp("all", argv[0])) { | |
894 | i = -1; | |
895 | } else { | |
896 | for (i = 0; tasks[i].arg && strcmp(tasks[i].arg, argv[0]); i++) | |
897 | ; /* keep looking for the task */ | |
898 | ||
899 | if (i > 0 && !tasks[i].arg) { | |
900 | error(_("no such task: '%s'"), argv[0]); | |
901 | usage_with_options(usagestr, options); | |
902 | } | |
903 | } | |
904 | ||
905 | argc--; | |
906 | argv++; | |
907 | setup_enlistment_directory(argc, argv, usagestr, options, NULL); | |
908 | strbuf_release(&buf); | |
909 | ||
910 | if (i == 0) | |
911 | return register_dir(); | |
912 | ||
913 | if (i > 0) | |
914 | return run_git("maintenance", "run", | |
915 | "--task", tasks[i].task, NULL); | |
916 | ||
917 | if (register_dir()) | |
918 | return -1; | |
919 | for (i = 1; tasks[i].arg; i++) | |
920 | if (run_git("maintenance", "run", | |
921 | "--task", tasks[i].task, NULL)) | |
922 | return -1; | |
923 | return 0; | |
924 | } | |
925 | ||
f5f0842d JS |
926 | static int remove_deleted_enlistment(struct strbuf *path) |
927 | { | |
928 | int res = 0; | |
929 | strbuf_realpath_forgiving(path, path->buf, 1); | |
930 | ||
931 | if (run_git("config", "--global", | |
932 | "--unset", "--fixed-value", | |
933 | "scalar.repo", path->buf, NULL) < 0) | |
934 | res = -1; | |
935 | ||
936 | if (run_git("config", "--global", | |
937 | "--unset", "--fixed-value", | |
938 | "maintenance.repo", path->buf, NULL) < 0) | |
939 | res = -1; | |
940 | ||
941 | return res; | |
942 | } | |
943 | ||
c76a53eb DS |
944 | static int cmd_unregister(int argc, const char **argv) |
945 | { | |
946 | struct option options[] = { | |
947 | OPT_END(), | |
948 | }; | |
949 | const char * const usage[] = { | |
950 | N_("scalar unregister [<enlistment>]"), | |
951 | NULL | |
952 | }; | |
953 | ||
954 | argc = parse_options(argc, argv, NULL, options, | |
955 | usage, 0); | |
956 | ||
f5f0842d JS |
957 | /* |
958 | * Be forgiving when the enlistment or worktree does not even exist any | |
959 | * longer; This can be the case if a user deleted the worktree by | |
960 | * mistake and _still_ wants to unregister the thing. | |
961 | */ | |
962 | if (argc == 1) { | |
963 | struct strbuf src_path = STRBUF_INIT, workdir_path = STRBUF_INIT; | |
964 | ||
965 | strbuf_addf(&src_path, "%s/src/.git", argv[0]); | |
966 | strbuf_addf(&workdir_path, "%s/.git", argv[0]); | |
967 | if (!is_directory(src_path.buf) && !is_directory(workdir_path.buf)) { | |
968 | /* remove possible matching registrations */ | |
969 | int res = -1; | |
970 | ||
971 | strbuf_strip_suffix(&src_path, "/.git"); | |
972 | res = remove_deleted_enlistment(&src_path) && res; | |
973 | ||
974 | strbuf_strip_suffix(&workdir_path, "/.git"); | |
975 | res = remove_deleted_enlistment(&workdir_path) && res; | |
976 | ||
977 | strbuf_release(&src_path); | |
978 | strbuf_release(&workdir_path); | |
979 | return res; | |
980 | } | |
981 | strbuf_release(&src_path); | |
982 | strbuf_release(&workdir_path); | |
983 | } | |
984 | ||
c76a53eb DS |
985 | setup_enlistment_directory(argc, argv, usage, options, NULL); |
986 | ||
987 | return unregister_dir(); | |
988 | } | |
989 | ||
d85ada7c MJC |
990 | static int cmd_delete(int argc, const char **argv) |
991 | { | |
992 | char *cwd = xgetcwd(); | |
993 | struct option options[] = { | |
994 | OPT_END(), | |
995 | }; | |
996 | const char * const usage[] = { | |
997 | N_("scalar delete <enlistment>"), | |
998 | NULL | |
999 | }; | |
1000 | struct strbuf enlistment = STRBUF_INIT; | |
1001 | int res = 0; | |
1002 | ||
1003 | argc = parse_options(argc, argv, NULL, options, | |
1004 | usage, 0); | |
1005 | ||
1006 | if (argc != 1) | |
1007 | usage_with_options(usage, options); | |
1008 | ||
1009 | setup_enlistment_directory(argc, argv, usage, options, &enlistment); | |
1010 | ||
1011 | if (dir_inside_of(cwd, enlistment.buf) >= 0) | |
1012 | res = error(_("refusing to delete current working directory")); | |
1013 | else { | |
1014 | close_object_store(the_repository->objects); | |
1015 | res = delete_enlistment(&enlistment); | |
1016 | } | |
1017 | strbuf_release(&enlistment); | |
1018 | free(cwd); | |
1019 | ||
1020 | return res; | |
1021 | } | |
1022 | ||
ddc35d83 JS |
1023 | static int cmd_version(int argc, const char **argv) |
1024 | { | |
1025 | int verbose = 0, build_options = 0; | |
1026 | struct option options[] = { | |
1027 | OPT__VERBOSE(&verbose, N_("include Git version")), | |
1028 | OPT_BOOL(0, "build-options", &build_options, | |
1029 | N_("include Git's build options")), | |
1030 | OPT_END(), | |
1031 | }; | |
1032 | const char * const usage[] = { | |
1033 | N_("scalar verbose [-v | --verbose] [--build-options]"), | |
1034 | NULL | |
1035 | }; | |
1036 | struct strbuf buf = STRBUF_INIT; | |
1037 | ||
1038 | argc = parse_options(argc, argv, NULL, options, | |
1039 | usage, 0); | |
1040 | ||
1041 | if (argc != 0) | |
1042 | usage_with_options(usage, options); | |
1043 | ||
1044 | get_version_info(&buf, build_options); | |
1045 | fprintf(stderr, "%s\n", buf.buf); | |
1046 | strbuf_release(&buf); | |
1047 | ||
1048 | return 0; | |
1049 | } | |
1050 | ||
0a43fb22 JS |
1051 | static struct { |
1052 | const char *name; | |
1053 | int (*fn)(int, const char **); | |
1054 | } builtins[] = { | |
546f822d | 1055 | { "clone", cmd_clone }, |
2b710457 | 1056 | { "list", cmd_list }, |
d0feac4e | 1057 | { "register", cmd_register }, |
c76a53eb | 1058 | { "unregister", cmd_unregister }, |
7020c88c | 1059 | { "run", cmd_run }, |
cb59d55e | 1060 | { "reconfigure", cmd_reconfigure }, |
d85ada7c | 1061 | { "delete", cmd_delete }, |
ddc35d83 | 1062 | { "version", cmd_version }, |
aa5c79a3 | 1063 | { "diagnose", cmd_diagnose }, |
0a43fb22 JS |
1064 | { NULL, NULL}, |
1065 | }; | |
1066 | ||
1067 | int cmd_main(int argc, const char **argv) | |
1068 | { | |
1069 | struct strbuf scalar_usage = STRBUF_INIT; | |
1070 | int i; | |
1071 | ||
2ae8eb5d JS |
1072 | while (argc > 1 && *argv[1] == '-') { |
1073 | if (!strcmp(argv[1], "-C")) { | |
1074 | if (argc < 3) | |
1075 | die(_("-C requires a <directory>")); | |
1076 | if (chdir(argv[2]) < 0) | |
1077 | die_errno(_("could not change to '%s'"), | |
1078 | argv[2]); | |
1079 | argc -= 2; | |
1080 | argv += 2; | |
1081 | } else if (!strcmp(argv[1], "-c")) { | |
1082 | if (argc < 3) | |
1083 | die(_("-c requires a <key>=<value> argument")); | |
1084 | git_config_push_parameter(argv[2]); | |
1085 | argc -= 2; | |
1086 | argv += 2; | |
1087 | } else | |
1088 | break; | |
1089 | } | |
1090 | ||
0a43fb22 JS |
1091 | if (argc > 1) { |
1092 | argv++; | |
1093 | argc--; | |
1094 | ||
1095 | for (i = 0; builtins[i].name; i++) | |
1096 | if (!strcmp(builtins[i].name, argv[0])) | |
1097 | return !!builtins[i].fn(argc, argv); | |
1098 | } | |
1099 | ||
1100 | strbuf_addstr(&scalar_usage, | |
2ae8eb5d JS |
1101 | N_("scalar [-C <directory>] [-c <key>=<value>] " |
1102 | "<command> [<options>]\n\nCommands:\n")); | |
0a43fb22 JS |
1103 | for (i = 0; builtins[i].name; i++) |
1104 | strbuf_addf(&scalar_usage, "\t%s\n", builtins[i].name); | |
1105 | ||
1106 | usage(scalar_usage.buf); | |
1107 | } |