blob: fb2940c2a00c94d806319dc8d45e8ffed1231c17 [file] [log] [blame]
Johannes Schindelin0a43fb22021-12-03 13:34:16 +00001/*
2 * The Scalar command-line interface.
3 */
4
Elijah Newren5579f442023-04-11 00:41:48 -07005#include "git-compat-util.h"
Elijah Newren0b027f62023-03-21 06:25:58 +00006#include "abspath.h"
Johannes Schindelin0a43fb22021-12-03 13:34:16 +00007#include "gettext.h"
8#include "parse-options.h"
Derrick Stoleed0feac42021-12-03 13:34:19 +00009#include "config.h"
10#include "run-command.h"
Matthew John Cheetham3f1917d2022-08-18 21:40:51 +000011#include "simple-ipc.h"
12#include "fsmonitor-ipc.h"
13#include "fsmonitor-settings.h"
Johannes Schindelin546f8222021-12-03 13:34:23 +000014#include "refs.h"
Matthew John Cheethamd85ada72021-12-03 13:34:28 +000015#include "dir.h"
16#include "packfile.h"
Johannes Schindelinddc35d82021-12-03 13:34:29 +000017#include "help.h"
Elijah Newrene38da482023-03-21 06:26:05 +000018#include "setup.h"
Elijah Newren74ea5c92023-04-11 03:00:38 +000019#include "trace2.h"
Derrick Stoleed0feac42021-12-03 13:34:19 +000020
Derrick Stoleed0feac42021-12-03 13:34:19 +000021static 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;
Victoria Dye65f6a9e2022-08-18 21:40:46 +000027 int enlistment_is_repo_parent = 0;
28 size_t len;
Derrick Stoleed0feac42021-12-03 13:34:19 +000029
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 */
Johannes Schindelinb4485572022-05-28 16:11:14 -070037 if (argc == 1) {
Derrick Stoleed0feac42021-12-03 13:34:19 +000038 strbuf_add_absolute_path(&path, argv[0]);
Johannes Schindelinb4485572022-05-28 16:11:14 -070039 if (!is_directory(path.buf))
40 die(_("'%s' does not exist"), path.buf);
Victoria Dye65f6a9e2022-08-18 21:40:46 +000041 if (chdir(path.buf) < 0)
42 die_errno(_("could not switch to '%s'"), path.buf);
Johannes Schindelinb4485572022-05-28 16:11:14 -070043 } else if (strbuf_getcwd(&path) < 0)
Derrick Stoleed0feac42021-12-03 13:34:19 +000044 die(_("need a working directory"));
45
46 strbuf_trim_trailing_dir_sep(&path);
Derrick Stoleed0feac42021-12-03 13:34:19 +000047
Victoria Dye65f6a9e2022-08-18 21:40:46 +000048 /* 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);
Derrick Stoleed0feac42021-12-03 13:34:19 +000057
Victoria Dye65f6a9e2022-08-18 21:40:46 +000058 setup_git_directory();
Derrick Stoleed0feac42021-12-03 13:34:19 +000059
Victoria Dye65f6a9e2022-08-18 21:40:46 +000060 if (!the_repository->worktree)
61 die(_("Scalar enlistments require a worktree"));
Derrick Stoleed0feac42021-12-03 13:34:19 +000062
Victoria Dye65f6a9e2022-08-18 21:40:46 +000063 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 }
Derrick Stoleed0feac42021-12-03 13:34:19 +000069
70 strbuf_release(&path);
Derrick Stoleed0feac42021-12-03 13:34:19 +000071}
72
73static int run_git(const char *arg, ...)
74{
René Scharfe0e906732022-10-30 12:51:14 +010075 struct child_process cmd = CHILD_PROCESS_INIT;
Derrick Stoleed0feac42021-12-03 13:34:19 +000076 va_list args;
77 const char *p;
Derrick Stoleed0feac42021-12-03 13:34:19 +000078
79 va_start(args, arg);
René Scharfe0e906732022-10-30 12:51:14 +010080 strvec_push(&cmd.args, arg);
Derrick Stoleed0feac42021-12-03 13:34:19 +000081 while ((p = va_arg(args, const char *)))
René Scharfe0e906732022-10-30 12:51:14 +010082 strvec_push(&cmd.args, p);
Derrick Stoleed0feac42021-12-03 13:34:19 +000083 va_end(args);
84
René Scharfe0e906732022-10-30 12:51:14 +010085 cmd.git_cmd = 1;
86 return run_command(&cmd);
Derrick Stoleed0feac42021-12-03 13:34:19 +000087}
88
Victoria Dyed934a112022-08-18 21:40:50 +000089struct scalar_config {
90 const char *key;
91 const char *value;
92 int overwrite_on_reconfigure;
93};
94
95static 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
Matthew John Cheetham3f1917d2022-08-18 21:40:51 +0000113static int have_fsmonitor_support(void)
114{
115 return fsmonitor_ipc__is_supported() &&
116 fsm_settings__get_reason(the_repository) == FSMONITOR_REASON_OK;
117}
118
Johannes Schindelincb59d552021-12-03 13:34:26 +0000119static int set_recommended_config(int reconfigure)
Derrick Stoleed0feac42021-12-03 13:34:19 +0000120{
Victoria Dyed934a112022-08-18 21:40:50 +0000121 struct scalar_config config[] = {
Johannes Schindelincb59d552021-12-03 13:34:26 +0000122 /* Required */
123 { "am.keepCR", "true", 1 },
124 { "core.FSCache", "true", 1 },
125 { "core.multiPackIndex", "true", 1 },
126 { "core.preloadIndex", "true", 1 },
Derrick Stoleed0feac42021-12-03 13:34:19 +0000127#ifndef WIN32
Johannes Schindelincb59d552021-12-03 13:34:26 +0000128 { "core.untrackedCache", "true", 1 },
Derrick Stoleed0feac42021-12-03 13:34:19 +0000129#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 */
Johannes Schindelincb59d552021-12-03 13:34:26 +0000142 { "core.untrackedCache", "false", 1 },
Derrick Stoleed0feac42021-12-03 13:34:19 +0000143#endif
Johannes Schindelincb59d552021-12-03 13:34:26 +0000144 { "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 },
Derrick Stolee17194b12023-01-06 16:31:56 +0000149 { "index.skipHash", "false", 1 },
Johannes Schindelincb59d552021-12-03 13:34:26 +0000150 { "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 },
Johannes Schindelincb59d552021-12-03 13:34:26 +0000157 { "feature.manyFiles", "false", 1 },
158 { "feature.experimental", "false", 1 },
159 { "fetch.unpackLimit", "1", 1 },
160 { "fetch.writeCommitGraph", "false", 1 },
Derrick Stoleed0feac42021-12-03 13:34:19 +0000161#ifdef WIN32
Johannes Schindelincb59d552021-12-03 13:34:26 +0000162 { "http.sslBackend", "schannel", 1 },
Derrick Stoleed0feac42021-12-03 13:34:19 +0000163#endif
Johannes Schindelincb59d552021-12-03 13:34:26 +0000164 /* Optional */
Derrick Stoleed0feac42021-12-03 13:34:19 +0000165 { "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++) {
Victoria Dyed934a112022-08-18 21:40:50 +0000176 if (set_scalar_config(config + i, reconfigure))
177 return error(_("could not configure %s=%s"),
178 config[i].key, config[i].value);
Derrick Stoleed0feac42021-12-03 13:34:19 +0000179 }
180
Matthew John Cheetham3f1917d2022-08-18 21:40:51 +0000181 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);
Derrick Stoleed0feac42021-12-03 13:34:19 +0000186 }
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
Derrick Stoleec76a53e2021-12-03 13:34:20 +0000209static int toggle_maintenance(int enable)
Derrick Stoleed0feac42021-12-03 13:34:19 +0000210{
Derrick Stoleed871b6c2022-09-27 13:56:59 +0000211 return run_git("maintenance",
212 enable ? "start" : "unregister",
213 enable ? NULL : "--force",
214 NULL);
Derrick Stoleed0feac42021-12-03 13:34:19 +0000215}
216
Derrick Stoleec76a53e2021-12-03 13:34:20 +0000217static int add_or_remove_enlistment(int add)
Derrick Stoleed0feac42021-12-03 13:34:19 +0000218{
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 /*
Derrick Stoleec76a53e2021-12-03 13:34:20 +0000228 * 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.
Derrick Stoleed0feac42021-12-03 13:34:19 +0000230 */
Derrick Stoleec76a53e2021-12-03 13:34:20 +0000231 if ((add && !res) || (!add && res))
Derrick Stoleed0feac42021-12-03 13:34:19 +0000232 return 0;
233
Derrick Stoleec76a53e2021-12-03 13:34:20 +0000234 return run_git("config", "--global", add ? "--add" : "--unset",
235 add ? "--no-fixed-value" : "--fixed-value",
Derrick Stoleed0feac42021-12-03 13:34:19 +0000236 "scalar.repo", the_repository->worktree, NULL);
237}
238
Matthew John Cheetham3f1917d2022-08-18 21:40:51 +0000239static 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
Johannes Schindelinec4c2312022-08-18 21:40:52 +0000249static 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
Derrick Stoleed0feac42021-12-03 13:34:19 +0000259static int register_dir(void)
260{
Victoria Dyed2a79bc2022-08-18 21:40:48 +0000261 if (add_or_remove_enlistment(1))
262 return error(_("could not add enlistment"));
Derrick Stoleed0feac42021-12-03 13:34:19 +0000263
Victoria Dyed2a79bc2022-08-18 21:40:48 +0000264 if (set_recommended_config(0))
265 return error(_("could not set recommended config"));
Derrick Stoleed0feac42021-12-03 13:34:19 +0000266
Victoria Dyed2a79bc2022-08-18 21:40:48 +0000267 if (toggle_maintenance(1))
Derrick Stoleedea63082023-01-27 20:06:03 +0000268 warning(_("could not turn on maintenance"));
Derrick Stoleec76a53e2021-12-03 13:34:20 +0000269
Matthew John Cheetham3f1917d2022-08-18 21:40:51 +0000270 if (have_fsmonitor_support() && start_fsmonitor_daemon()) {
271 return error(_("could not start the FSMonitor daemon"));
272 }
273
Victoria Dyed2a79bc2022-08-18 21:40:48 +0000274 return 0;
Derrick Stoleec76a53e2021-12-03 13:34:20 +0000275}
276
277static int unregister_dir(void)
278{
279 int res = 0;
280
Victoria Dyeadedcee2022-08-18 21:40:47 +0000281 if (toggle_maintenance(0))
Victoria Dyed2a79bc2022-08-18 21:40:48 +0000282 res = error(_("could not turn off maintenance"));
Derrick Stoleec76a53e2021-12-03 13:34:20 +0000283
Victoria Dyeadedcee2022-08-18 21:40:47 +0000284 if (add_or_remove_enlistment(0))
Victoria Dyed2a79bc2022-08-18 21:40:48 +0000285 res = error(_("could not remove enlistment"));
Derrick Stoleed0feac42021-12-03 13:34:19 +0000286
287 return res;
288}
289
Johannes Schindelin546f8222021-12-03 13:34:23 +0000290/* printf-style interface, expects `<key>=<value>` argument */
291static 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
311static 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
Matthew John Cheethamd85ada72021-12-03 13:34:28 +0000362static int delete_enlistment(struct strbuf *enlistment)
363{
364#ifdef WIN32
365 struct strbuf parent = STRBUF_INIT;
Victoria Dye65f6a9e2022-08-18 21:40:46 +0000366 size_t offset;
367 char *path_sep;
Matthew John Cheethamd85ada72021-12-03 13:34:28 +0000368#endif
369
370 if (unregister_dir())
Victoria Dye9b24bb92022-08-18 21:40:49 +0000371 return error(_("failed to unregister repository"));
Matthew John Cheethamd85ada72021-12-03 13:34:28 +0000372
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 */
Victoria Dye65f6a9e2022-08-18 21:40:46 +0000378 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);
Victoria Dye9b24bb92022-08-18 21:40:49 +0000382 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 }
Matthew John Cheethamd85ada72021-12-03 13:34:28 +0000387 strbuf_release(&parent);
388#endif
389
Johannes Schindelinec4c2312022-08-18 21:40:52 +0000390 if (have_fsmonitor_support() && stop_fsmonitor_daemon())
391 return error(_("failed to stop the FSMonitor daemon"));
392
Matthew John Cheethamd85ada72021-12-03 13:34:28 +0000393 if (remove_dir_recursively(enlistment, 0))
Victoria Dye9b24bb92022-08-18 21:40:49 +0000394 return error(_("failed to delete enlistment directory"));
Matthew John Cheethamd85ada72021-12-03 13:34:28 +0000395
396 return 0;
397}
398
Johannes Schindelinddc35d82021-12-03 13:34:29 +0000399/*
400 * Dummy implementation; Using `get_version_info()` would cause a link error
401 * without this.
402 */
403void load_builtin_commands(const char *prefix, struct cmdnames *cmds)
404{
405 die("not implemented");
406}
407
Johannes Schindelin546f8222021-12-03 13:34:23 +0000408static int cmd_clone(int argc, const char **argv)
409{
410 const char *branch = NULL;
ZheNing Hu4433bd22023-01-11 13:14:20 +0000411 int full_clone = 0, single_branch = 0, show_progress = isatty(2);
Derrick Stolee4527db82023-08-28 13:52:24 +0000412 int src = 1;
Johannes Schindelin546f8222021-12-03 13:34:23 +0000413 struct option clone_options[] = {
414 OPT_STRING('b', "branch", &branch, N_("<branch>"),
415 N_("branch to checkout after clone")),
416 OPT_BOOL(0, "full-clone", &full_clone,
417 N_("when cloning, create full working directory")),
Johannes Schindelin4368e402021-12-03 13:34:24 +0000418 OPT_BOOL(0, "single-branch", &single_branch,
419 N_("only download metadata for the branch that will "
420 "be checked out")),
Derrick Stolee4527db82023-08-28 13:52:24 +0000421 OPT_BOOL(0, "src", &src,
422 N_("create repository within 'src' directory")),
Johannes Schindelin546f8222021-12-03 13:34:23 +0000423 OPT_END(),
424 };
425 const char * const clone_usage[] = {
Derrick Stolee4527db82023-08-28 13:52:24 +0000426 N_("scalar clone [--single-branch] [--branch <main-branch>] [--full-clone]\n"
427 "\t[--[no-]src] <url> [<enlistment>]"),
Johannes Schindelin546f8222021-12-03 13:34:23 +0000428 NULL
429 };
430 const char *url;
431 char *enlistment = NULL, *dir = NULL;
432 struct strbuf buf = STRBUF_INIT;
433 int res;
434
435 argc = parse_options(argc, argv, NULL, clone_options, clone_usage, 0);
436
437 if (argc == 2) {
438 url = argv[0];
439 enlistment = xstrdup(argv[1]);
440 } else if (argc == 1) {
441 url = argv[0];
442
443 strbuf_addstr(&buf, url);
444 /* Strip trailing slashes, if any */
445 while (buf.len > 0 && is_dir_sep(buf.buf[buf.len - 1]))
446 strbuf_setlen(&buf, buf.len - 1);
447 /* Strip suffix `.git`, if any */
448 strbuf_strip_suffix(&buf, ".git");
449
450 enlistment = find_last_dir_sep(buf.buf);
451 if (!enlistment) {
452 die(_("cannot deduce worktree name from '%s'"), url);
453 }
454 enlistment = xstrdup(enlistment + 1);
455 } else {
456 usage_msg_opt(_("You must specify a repository to clone."),
457 clone_usage, clone_options);
458 }
459
460 if (is_directory(enlistment))
461 die(_("directory '%s' exists already"), enlistment);
462
Derrick Stolee4527db82023-08-28 13:52:24 +0000463 if (src)
464 dir = xstrfmt("%s/src", enlistment);
465 else
466 dir = xstrdup(enlistment);
Johannes Schindelin546f8222021-12-03 13:34:23 +0000467
468 strbuf_reset(&buf);
469 if (branch)
470 strbuf_addf(&buf, "init.defaultBranch=%s", branch);
471 else {
472 char *b = repo_default_branch_name(the_repository, 1);
473 strbuf_addf(&buf, "init.defaultBranch=%s", b);
474 free(b);
475 }
476
477 if ((res = run_git("-c", buf.buf, "init", "--", dir, NULL)))
478 goto cleanup;
479
480 if (chdir(dir) < 0) {
481 res = error_errno(_("could not switch to '%s'"), dir);
482 goto cleanup;
483 }
484
485 setup_git_directory();
486
487 /* common-main already logs `argv` */
488 trace2_def_repo(the_repository);
489
490 if (!branch && !(branch = remote_default_branch(url))) {
491 res = error(_("failed to get default branch for '%s'"), url);
492 goto cleanup;
493 }
494
495 if (set_config("remote.origin.url=%s", url) ||
496 set_config("remote.origin.fetch="
Johannes Schindelin4368e402021-12-03 13:34:24 +0000497 "+refs/heads/%s:refs/remotes/origin/%s",
498 single_branch ? branch : "*",
499 single_branch ? branch : "*") ||
Johannes Schindelin546f8222021-12-03 13:34:23 +0000500 set_config("remote.origin.promisor=true") ||
501 set_config("remote.origin.partialCloneFilter=blob:none")) {
502 res = error(_("could not configure remote in '%s'"), dir);
503 goto cleanup;
504 }
505
506 if (!full_clone &&
507 (res = run_git("sparse-checkout", "init", "--cone", NULL)))
508 goto cleanup;
509
Johannes Schindelincb59d552021-12-03 13:34:26 +0000510 if (set_recommended_config(0))
Johannes Schindelin546f8222021-12-03 13:34:23 +0000511 return error(_("could not configure '%s'"), dir);
512
ZheNing Hu4433bd22023-01-11 13:14:20 +0000513 if ((res = run_git("fetch", "--quiet",
514 show_progress ? "--progress" : "--no-progress",
515 "origin", NULL))) {
Johannes Schindelin546f8222021-12-03 13:34:23 +0000516 warning(_("partial clone failed; attempting full clone"));
517
518 if (set_config("remote.origin.promisor") ||
519 set_config("remote.origin.partialCloneFilter")) {
520 res = error(_("could not configure for full clone"));
521 goto cleanup;
522 }
523
ZheNing Hu4433bd22023-01-11 13:14:20 +0000524 if ((res = run_git("fetch", "--quiet",
525 show_progress ? "--progress" : "--no-progress",
526 "origin", NULL)))
Johannes Schindelin546f8222021-12-03 13:34:23 +0000527 goto cleanup;
528 }
529
530 if ((res = set_config("branch.%s.remote=origin", branch)))
531 goto cleanup;
532 if ((res = set_config("branch.%s.merge=refs/heads/%s",
533 branch, branch)))
534 goto cleanup;
535
536 strbuf_reset(&buf);
537 strbuf_addf(&buf, "origin/%s", branch);
538 res = run_git("checkout", "-f", "-t", buf.buf, NULL);
539 if (res)
540 goto cleanup;
541
542 res = register_dir();
543
544cleanup:
545 free(enlistment);
546 free(dir);
547 strbuf_release(&buf);
548 return res;
549}
550
Johannes Schindelinaa5c79a2022-05-28 16:11:15 -0700551static int cmd_diagnose(int argc, const char **argv)
552{
553 struct option options[] = {
554 OPT_END(),
555 };
556 const char * const usage[] = {
557 N_("scalar diagnose [<enlistment>]"),
558 NULL
559 };
Victoria Dye672196a2022-08-12 20:10:18 +0000560 struct strbuf diagnostics_root = STRBUF_INIT;
Johannes Schindelinaa5c79a2022-05-28 16:11:15 -0700561 int res = 0;
562
563 argc = parse_options(argc, argv, NULL, options,
564 usage, 0);
565
Victoria Dye672196a2022-08-12 20:10:18 +0000566 setup_enlistment_directory(argc, argv, usage, options, &diagnostics_root);
567 strbuf_addstr(&diagnostics_root, "/.scalarDiagnostics");
Johannes Schindelinaa5c79a2022-05-28 16:11:15 -0700568
Victoria Dye672196a2022-08-12 20:10:18 +0000569 res = run_git("diagnose", "--mode=all", "-s", "%Y%m%d_%H%M%S",
570 "-o", diagnostics_root.buf, NULL);
Johannes Schindelinaa5c79a2022-05-28 16:11:15 -0700571
Victoria Dye672196a2022-08-12 20:10:18 +0000572 strbuf_release(&diagnostics_root);
Johannes Schindelinaa5c79a2022-05-28 16:11:15 -0700573 return res;
574}
575
Jeff King6ba21fa2023-03-28 16:57:04 -0400576static int cmd_list(int argc, const char **argv UNUSED)
Derrick Stolee2b710452021-12-03 13:34:22 +0000577{
578 if (argc != 1)
579 die(_("`scalar list` does not take arguments"));
580
581 if (run_git("config", "--global", "--get-all", "scalar.repo", NULL) < 0)
582 return -1;
583 return 0;
584}
585
Derrick Stoleed0feac42021-12-03 13:34:19 +0000586static int cmd_register(int argc, const char **argv)
587{
588 struct option options[] = {
589 OPT_END(),
590 };
591 const char * const usage[] = {
592 N_("scalar register [<enlistment>]"),
593 NULL
594 };
595
596 argc = parse_options(argc, argv, NULL, options,
597 usage, 0);
598
599 setup_enlistment_directory(argc, argv, usage, options, NULL);
600
601 return register_dir();
602}
Johannes Schindelin0a43fb22021-12-03 13:34:16 +0000603
Glen Chooa4e7e312023-06-28 19:26:22 +0000604static int get_scalar_repos(const char *key, const char *value,
605 const struct config_context *ctx UNUSED,
606 void *data)
Johannes Schindelin45826762021-12-03 13:34:27 +0000607{
608 struct string_list *list = data;
609
610 if (!strcmp(key, "scalar.repo"))
611 string_list_append(list, value);
612
613 return 0;
614}
615
Johannes Schindelinc90db532022-11-07 18:25:01 +0000616static int remove_deleted_enlistment(struct strbuf *path)
617{
618 int res = 0;
619 strbuf_realpath_forgiving(path, path->buf, 1);
620
621 if (run_git("config", "--global",
622 "--unset", "--fixed-value",
623 "scalar.repo", path->buf, NULL) < 0)
624 res = -1;
625
626 if (run_git("config", "--global",
627 "--unset", "--fixed-value",
628 "maintenance.repo", path->buf, NULL) < 0)
629 res = -1;
630
631 return res;
632}
633
Johannes Schindelincb59d552021-12-03 13:34:26 +0000634static int cmd_reconfigure(int argc, const char **argv)
635{
Johannes Schindelin45826762021-12-03 13:34:27 +0000636 int all = 0;
Johannes Schindelincb59d552021-12-03 13:34:26 +0000637 struct option options[] = {
Johannes Schindelin45826762021-12-03 13:34:27 +0000638 OPT_BOOL('a', "all", &all,
639 N_("reconfigure all registered enlistments")),
Johannes Schindelincb59d552021-12-03 13:34:26 +0000640 OPT_END(),
641 };
642 const char * const usage[] = {
Johannes Schindelin45826762021-12-03 13:34:27 +0000643 N_("scalar reconfigure [--all | <enlistment>]"),
Johannes Schindelincb59d552021-12-03 13:34:26 +0000644 NULL
645 };
Johannes Schindelin45826762021-12-03 13:34:27 +0000646 struct string_list scalar_repos = STRING_LIST_INIT_DUP;
647 int i, res = 0;
648 struct repository r = { NULL };
649 struct strbuf commondir = STRBUF_INIT, gitdir = STRBUF_INIT;
Johannes Schindelincb59d552021-12-03 13:34:26 +0000650
651 argc = parse_options(argc, argv, NULL, options,
652 usage, 0);
653
Johannes Schindelin45826762021-12-03 13:34:27 +0000654 if (!all) {
655 setup_enlistment_directory(argc, argv, usage, options, NULL);
Johannes Schindelincb59d552021-12-03 13:34:26 +0000656
Johannes Schindelin45826762021-12-03 13:34:27 +0000657 return set_recommended_config(1);
658 }
659
660 if (argc > 0)
661 usage_msg_opt(_("--all or <enlistment>, but not both"),
662 usage, options);
663
664 git_config(get_scalar_repos, &scalar_repos);
665
666 for (i = 0; i < scalar_repos.nr; i++) {
Derrick Stoleef9a547d2023-08-28 13:52:26 +0000667 int succeeded = 0;
Johannes Schindelin45826762021-12-03 13:34:27 +0000668 const char *dir = scalar_repos.items[i].string;
669
670 strbuf_reset(&commondir);
671 strbuf_reset(&gitdir);
672
673 if (chdir(dir) < 0) {
Johannes Schindelinc90db532022-11-07 18:25:01 +0000674 struct strbuf buf = STRBUF_INIT;
675
676 if (errno != ENOENT) {
677 warning_errno(_("could not switch to '%s'"), dir);
Derrick Stoleef9a547d2023-08-28 13:52:26 +0000678 goto loop_end;
Johannes Schindelinc90db532022-11-07 18:25:01 +0000679 }
680
681 strbuf_addstr(&buf, dir);
682 if (remove_deleted_enlistment(&buf))
Derrick Stoleef9a547d2023-08-28 13:52:26 +0000683 error(_("could not remove stale "
684 "scalar.repo '%s'"), dir);
685 else {
686 warning(_("removed stale scalar.repo '%s'"),
Johannes Schindelinc90db532022-11-07 18:25:01 +0000687 dir);
Derrick Stoleef9a547d2023-08-28 13:52:26 +0000688 succeeded = 1;
689 }
Johannes Schindelinc90db532022-11-07 18:25:01 +0000690 strbuf_release(&buf);
Derrick Stoleef9a547d2023-08-28 13:52:26 +0000691 goto loop_end;
692 }
693
694 switch (discover_git_directory_reason(&commondir, &gitdir)) {
695 case GIT_DIR_INVALID_OWNERSHIP:
696 warning(_("repository at '%s' has different owner"), dir);
697 goto loop_end;
698
699 case GIT_DIR_INVALID_GITFILE:
700 case GIT_DIR_INVALID_FORMAT:
701 warning(_("repository at '%s' has a format issue"), dir);
702 goto loop_end;
703
704 case GIT_DIR_DISCOVERED:
705 succeeded = 1;
706 break;
707
708 default:
709 warning(_("repository not found in '%s'"), dir);
710 break;
711 }
712
713 git_config_clear();
714
715 the_repository = &r;
716 r.commondir = commondir.buf;
717 r.gitdir = gitdir.buf;
718
719 if (set_recommended_config(1) >= 0)
720 succeeded = 1;
721
722loop_end:
723 if (!succeeded) {
Johannes Schindelin45826762021-12-03 13:34:27 +0000724 res = -1;
Derrick Stoleef9a547d2023-08-28 13:52:26 +0000725 warning(_("to unregister this repository from Scalar, run\n"
726 "\tgit config --global --unset --fixed-value scalar.repo \"%s\""),
727 dir);
Johannes Schindelin45826762021-12-03 13:34:27 +0000728 }
729 }
730
731 string_list_clear(&scalar_repos, 1);
732 strbuf_release(&commondir);
733 strbuf_release(&gitdir);
734
735 return res;
Johannes Schindelincb59d552021-12-03 13:34:26 +0000736}
737
Derrick Stolee7020c882021-12-03 13:34:25 +0000738static int cmd_run(int argc, const char **argv)
739{
740 struct option options[] = {
741 OPT_END(),
742 };
743 struct {
744 const char *arg, *task;
745 } tasks[] = {
746 { "config", NULL },
747 { "commit-graph", "commit-graph" },
748 { "fetch", "prefetch" },
749 { "loose-objects", "loose-objects" },
750 { "pack-files", "incremental-repack" },
751 { NULL, NULL }
752 };
753 struct strbuf buf = STRBUF_INIT;
754 const char *usagestr[] = { NULL, NULL };
755 int i;
756
757 strbuf_addstr(&buf, N_("scalar run <task> [<enlistment>]\nTasks:\n"));
758 for (i = 0; tasks[i].arg; i++)
759 strbuf_addf(&buf, "\t%s\n", tasks[i].arg);
760 usagestr[0] = buf.buf;
761
762 argc = parse_options(argc, argv, NULL, options,
763 usagestr, 0);
764
765 if (!argc)
766 usage_with_options(usagestr, options);
767
768 if (!strcmp("all", argv[0])) {
769 i = -1;
770 } else {
771 for (i = 0; tasks[i].arg && strcmp(tasks[i].arg, argv[0]); i++)
772 ; /* keep looking for the task */
773
774 if (i > 0 && !tasks[i].arg) {
775 error(_("no such task: '%s'"), argv[0]);
776 usage_with_options(usagestr, options);
777 }
778 }
779
780 argc--;
781 argv++;
782 setup_enlistment_directory(argc, argv, usagestr, options, NULL);
783 strbuf_release(&buf);
784
785 if (i == 0)
786 return register_dir();
787
788 if (i > 0)
789 return run_git("maintenance", "run",
790 "--task", tasks[i].task, NULL);
791
792 if (register_dir())
793 return -1;
794 for (i = 1; tasks[i].arg; i++)
795 if (run_git("maintenance", "run",
796 "--task", tasks[i].task, NULL))
797 return -1;
798 return 0;
799}
800
Derrick Stoleec76a53e2021-12-03 13:34:20 +0000801static int cmd_unregister(int argc, const char **argv)
802{
803 struct option options[] = {
804 OPT_END(),
805 };
806 const char * const usage[] = {
807 N_("scalar unregister [<enlistment>]"),
808 NULL
809 };
810
811 argc = parse_options(argc, argv, NULL, options,
812 usage, 0);
813
Johannes Schindelinf5f08422021-12-03 13:34:21 +0000814 /*
815 * Be forgiving when the enlistment or worktree does not even exist any
816 * longer; This can be the case if a user deleted the worktree by
817 * mistake and _still_ wants to unregister the thing.
818 */
819 if (argc == 1) {
820 struct strbuf src_path = STRBUF_INIT, workdir_path = STRBUF_INIT;
821
822 strbuf_addf(&src_path, "%s/src/.git", argv[0]);
823 strbuf_addf(&workdir_path, "%s/.git", argv[0]);
824 if (!is_directory(src_path.buf) && !is_directory(workdir_path.buf)) {
825 /* remove possible matching registrations */
826 int res = -1;
827
828 strbuf_strip_suffix(&src_path, "/.git");
829 res = remove_deleted_enlistment(&src_path) && res;
830
831 strbuf_strip_suffix(&workdir_path, "/.git");
832 res = remove_deleted_enlistment(&workdir_path) && res;
833
834 strbuf_release(&src_path);
835 strbuf_release(&workdir_path);
836 return res;
837 }
838 strbuf_release(&src_path);
839 strbuf_release(&workdir_path);
840 }
841
Derrick Stoleec76a53e2021-12-03 13:34:20 +0000842 setup_enlistment_directory(argc, argv, usage, options, NULL);
843
844 return unregister_dir();
845}
846
Matthew John Cheethamd85ada72021-12-03 13:34:28 +0000847static int cmd_delete(int argc, const char **argv)
848{
849 char *cwd = xgetcwd();
850 struct option options[] = {
851 OPT_END(),
852 };
853 const char * const usage[] = {
854 N_("scalar delete <enlistment>"),
855 NULL
856 };
857 struct strbuf enlistment = STRBUF_INIT;
858 int res = 0;
859
860 argc = parse_options(argc, argv, NULL, options,
861 usage, 0);
862
863 if (argc != 1)
864 usage_with_options(usage, options);
865
866 setup_enlistment_directory(argc, argv, usage, options, &enlistment);
867
868 if (dir_inside_of(cwd, enlistment.buf) >= 0)
869 res = error(_("refusing to delete current working directory"));
870 else {
871 close_object_store(the_repository->objects);
872 res = delete_enlistment(&enlistment);
873 }
874 strbuf_release(&enlistment);
875 free(cwd);
876
877 return res;
878}
879
Johannes Schindelin951759d2022-09-02 15:56:45 +0000880static int cmd_help(int argc, const char **argv)
881{
882 struct option options[] = {
883 OPT_END(),
884 };
885 const char * const usage[] = {
886 "scalar help",
887 NULL
888 };
889
890 argc = parse_options(argc, argv, NULL, options,
891 usage, 0);
892
893 if (argc != 0)
894 usage_with_options(usage, options);
895
896 return run_git("help", "scalar", NULL);
897}
898
Johannes Schindelinddc35d82021-12-03 13:34:29 +0000899static int cmd_version(int argc, const char **argv)
900{
901 int verbose = 0, build_options = 0;
902 struct option options[] = {
903 OPT__VERBOSE(&verbose, N_("include Git version")),
904 OPT_BOOL(0, "build-options", &build_options,
905 N_("include Git's build options")),
906 OPT_END(),
907 };
908 const char * const usage[] = {
909 N_("scalar verbose [-v | --verbose] [--build-options]"),
910 NULL
911 };
912 struct strbuf buf = STRBUF_INIT;
913
914 argc = parse_options(argc, argv, NULL, options,
915 usage, 0);
916
917 if (argc != 0)
918 usage_with_options(usage, options);
919
920 get_version_info(&buf, build_options);
921 fprintf(stderr, "%s\n", buf.buf);
922 strbuf_release(&buf);
923
924 return 0;
925}
926
Johannes Schindelin0a43fb22021-12-03 13:34:16 +0000927static struct {
928 const char *name;
929 int (*fn)(int, const char **);
930} builtins[] = {
Johannes Schindelin546f8222021-12-03 13:34:23 +0000931 { "clone", cmd_clone },
Derrick Stolee2b710452021-12-03 13:34:22 +0000932 { "list", cmd_list },
Derrick Stoleed0feac42021-12-03 13:34:19 +0000933 { "register", cmd_register },
Derrick Stoleec76a53e2021-12-03 13:34:20 +0000934 { "unregister", cmd_unregister },
Derrick Stolee7020c882021-12-03 13:34:25 +0000935 { "run", cmd_run },
Johannes Schindelincb59d552021-12-03 13:34:26 +0000936 { "reconfigure", cmd_reconfigure },
Matthew John Cheethamd85ada72021-12-03 13:34:28 +0000937 { "delete", cmd_delete },
Johannes Schindelin951759d2022-09-02 15:56:45 +0000938 { "help", cmd_help },
Johannes Schindelinddc35d82021-12-03 13:34:29 +0000939 { "version", cmd_version },
Johannes Schindelinaa5c79a2022-05-28 16:11:15 -0700940 { "diagnose", cmd_diagnose },
Johannes Schindelin0a43fb22021-12-03 13:34:16 +0000941 { NULL, NULL},
942};
943
944int cmd_main(int argc, const char **argv)
945{
946 struct strbuf scalar_usage = STRBUF_INIT;
947 int i;
948
Johannes Schindelin2ae8eb52022-01-28 14:31:57 +0000949 while (argc > 1 && *argv[1] == '-') {
950 if (!strcmp(argv[1], "-C")) {
951 if (argc < 3)
952 die(_("-C requires a <directory>"));
953 if (chdir(argv[2]) < 0)
954 die_errno(_("could not change to '%s'"),
955 argv[2]);
956 argc -= 2;
957 argv += 2;
958 } else if (!strcmp(argv[1], "-c")) {
959 if (argc < 3)
960 die(_("-c requires a <key>=<value> argument"));
961 git_config_push_parameter(argv[2]);
962 argc -= 2;
963 argv += 2;
964 } else
965 break;
966 }
967
Johannes Schindelin0a43fb22021-12-03 13:34:16 +0000968 if (argc > 1) {
969 argv++;
970 argc--;
971
972 for (i = 0; builtins[i].name; i++)
973 if (!strcmp(builtins[i].name, argv[0]))
974 return !!builtins[i].fn(argc, argv);
975 }
976
977 strbuf_addstr(&scalar_usage,
Johannes Schindelin2ae8eb52022-01-28 14:31:57 +0000978 N_("scalar [-C <directory>] [-c <key>=<value>] "
979 "<command> [<options>]\n\nCommands:\n"));
Johannes Schindelin0a43fb22021-12-03 13:34:16 +0000980 for (i = 0; builtins[i].name; i++)
981 strbuf_addf(&scalar_usage, "\t%s\n", builtins[i].name);
982
983 usage(scalar_usage.buf);
984}