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