Johannes Schindelin | 0a43fb2 | 2021-12-03 13:34:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * The Scalar command-line interface. |
| 3 | */ |
| 4 | |
Patrick Steinhardt | e7da938 | 2024-06-14 08:50:23 +0200 | [diff] [blame] | 5 | #define USE_THE_REPOSITORY_VARIABLE |
| 6 | |
Elijah Newren | 5579f44 | 2023-04-11 00:41:48 -0700 | [diff] [blame] | 7 | #include "git-compat-util.h" |
Elijah Newren | 0b027f6 | 2023-03-21 06:25:58 +0000 | [diff] [blame] | 8 | #include "abspath.h" |
Johannes Schindelin | 0a43fb2 | 2021-12-03 13:34:16 +0000 | [diff] [blame] | 9 | #include "gettext.h" |
| 10 | #include "parse-options.h" |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 11 | #include "config.h" |
| 12 | #include "run-command.h" |
Matthew John Cheetham | 3f1917d | 2022-08-18 21:40:51 +0000 | [diff] [blame] | 13 | #include "simple-ipc.h" |
| 14 | #include "fsmonitor-ipc.h" |
| 15 | #include "fsmonitor-settings.h" |
Johannes Schindelin | 546f822 | 2021-12-03 13:34:23 +0000 | [diff] [blame] | 16 | #include "refs.h" |
Matthew John Cheetham | d85ada7 | 2021-12-03 13:34:28 +0000 | [diff] [blame] | 17 | #include "dir.h" |
| 18 | #include "packfile.h" |
Johannes Schindelin | ddc35d8 | 2021-12-03 13:34:29 +0000 | [diff] [blame] | 19 | #include "help.h" |
Elijah Newren | e38da48 | 2023-03-21 06:26:05 +0000 | [diff] [blame] | 20 | #include "setup.h" |
Elijah Newren | 74ea5c9 | 2023-04-11 03:00:38 +0000 | [diff] [blame] | 21 | #include "trace2.h" |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 22 | |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 23 | static void setup_enlistment_directory(int argc, const char **argv, |
| 24 | const char * const *usagestr, |
| 25 | const struct option *options, |
| 26 | struct strbuf *enlistment_root) |
| 27 | { |
| 28 | struct strbuf path = STRBUF_INIT; |
Victoria Dye | 65f6a9e | 2022-08-18 21:40:46 +0000 | [diff] [blame] | 29 | int enlistment_is_repo_parent = 0; |
| 30 | size_t len; |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 31 | |
| 32 | if (startup_info->have_repository) |
| 33 | BUG("gitdir already set up?!?"); |
| 34 | |
| 35 | if (argc > 1) |
| 36 | usage_with_options(usagestr, options); |
| 37 | |
| 38 | /* find the worktree, determine its corresponding root */ |
Johannes Schindelin | b448557 | 2022-05-28 16:11:14 -0700 | [diff] [blame] | 39 | if (argc == 1) { |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 40 | strbuf_add_absolute_path(&path, argv[0]); |
Johannes Schindelin | b448557 | 2022-05-28 16:11:14 -0700 | [diff] [blame] | 41 | if (!is_directory(path.buf)) |
| 42 | die(_("'%s' does not exist"), path.buf); |
Victoria Dye | 65f6a9e | 2022-08-18 21:40:46 +0000 | [diff] [blame] | 43 | if (chdir(path.buf) < 0) |
| 44 | die_errno(_("could not switch to '%s'"), path.buf); |
Johannes Schindelin | b448557 | 2022-05-28 16:11:14 -0700 | [diff] [blame] | 45 | } else if (strbuf_getcwd(&path) < 0) |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 46 | die(_("need a working directory")); |
| 47 | |
| 48 | strbuf_trim_trailing_dir_sep(&path); |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 49 | |
Victoria Dye | 65f6a9e | 2022-08-18 21:40:46 +0000 | [diff] [blame] | 50 | /* check if currently in enlistment root with src/ workdir */ |
| 51 | len = path.len; |
| 52 | strbuf_addstr(&path, "/src"); |
| 53 | if (is_nonbare_repository_dir(&path)) { |
| 54 | enlistment_is_repo_parent = 1; |
| 55 | if (chdir(path.buf) < 0) |
| 56 | die_errno(_("could not switch to '%s'"), path.buf); |
| 57 | } |
| 58 | strbuf_setlen(&path, len); |
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 | setup_git_directory(); |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 61 | |
Victoria Dye | 65f6a9e | 2022-08-18 21:40:46 +0000 | [diff] [blame] | 62 | if (!the_repository->worktree) |
| 63 | die(_("Scalar enlistments require a worktree")); |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 64 | |
Victoria Dye | 65f6a9e | 2022-08-18 21:40:46 +0000 | [diff] [blame] | 65 | if (enlistment_root) { |
| 66 | if (enlistment_is_repo_parent) |
| 67 | strbuf_addbuf(enlistment_root, &path); |
| 68 | else |
| 69 | strbuf_addstr(enlistment_root, the_repository->worktree); |
| 70 | } |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 71 | |
| 72 | strbuf_release(&path); |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Junio C Hamano | ba74464 | 2024-06-08 11:37:46 -0700 | [diff] [blame] | 75 | LAST_ARG_MUST_BE_NULL |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 76 | static int run_git(const char *arg, ...) |
| 77 | { |
René Scharfe | 0e90673 | 2022-10-30 12:51:14 +0100 | [diff] [blame] | 78 | struct child_process cmd = CHILD_PROCESS_INIT; |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 79 | va_list args; |
| 80 | const char *p; |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 81 | |
| 82 | va_start(args, arg); |
René Scharfe | 0e90673 | 2022-10-30 12:51:14 +0100 | [diff] [blame] | 83 | strvec_push(&cmd.args, arg); |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 84 | while ((p = va_arg(args, const char *))) |
René Scharfe | 0e90673 | 2022-10-30 12:51:14 +0100 | [diff] [blame] | 85 | strvec_push(&cmd.args, p); |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 86 | va_end(args); |
| 87 | |
René Scharfe | 0e90673 | 2022-10-30 12:51:14 +0100 | [diff] [blame] | 88 | cmd.git_cmd = 1; |
| 89 | return run_command(&cmd); |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Victoria Dye | d934a11 | 2022-08-18 21:40:50 +0000 | [diff] [blame] | 92 | struct scalar_config { |
| 93 | const char *key; |
| 94 | const char *value; |
| 95 | int overwrite_on_reconfigure; |
| 96 | }; |
| 97 | |
| 98 | static int set_scalar_config(const struct scalar_config *config, int reconfigure) |
| 99 | { |
| 100 | char *value = NULL; |
| 101 | int res; |
| 102 | |
| 103 | if ((reconfigure && config->overwrite_on_reconfigure) || |
| 104 | git_config_get_string(config->key, &value)) { |
| 105 | trace2_data_string("scalar", the_repository, config->key, "created"); |
| 106 | res = git_config_set_gently(config->key, config->value); |
| 107 | } else { |
| 108 | trace2_data_string("scalar", the_repository, config->key, "exists"); |
| 109 | res = 0; |
| 110 | } |
| 111 | |
| 112 | free(value); |
| 113 | return res; |
| 114 | } |
| 115 | |
Matthew John Cheetham | 3f1917d | 2022-08-18 21:40:51 +0000 | [diff] [blame] | 116 | static int have_fsmonitor_support(void) |
| 117 | { |
| 118 | return fsmonitor_ipc__is_supported() && |
| 119 | fsm_settings__get_reason(the_repository) == FSMONITOR_REASON_OK; |
| 120 | } |
| 121 | |
Johannes Schindelin | cb59d55 | 2021-12-03 13:34:26 +0000 | [diff] [blame] | 122 | static int set_recommended_config(int reconfigure) |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 123 | { |
Victoria Dye | d934a11 | 2022-08-18 21:40:50 +0000 | [diff] [blame] | 124 | struct scalar_config config[] = { |
Johannes Schindelin | cb59d55 | 2021-12-03 13:34:26 +0000 | [diff] [blame] | 125 | /* Required */ |
| 126 | { "am.keepCR", "true", 1 }, |
| 127 | { "core.FSCache", "true", 1 }, |
| 128 | { "core.multiPackIndex", "true", 1 }, |
| 129 | { "core.preloadIndex", "true", 1 }, |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 130 | #ifndef WIN32 |
Johannes Schindelin | cb59d55 | 2021-12-03 13:34:26 +0000 | [diff] [blame] | 131 | { "core.untrackedCache", "true", 1 }, |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 132 | #else |
| 133 | /* |
| 134 | * Unfortunately, Scalar's Functional Tests demonstrated |
| 135 | * that the untracked cache feature is unreliable on Windows |
| 136 | * (which is a bummer because that platform would benefit the |
| 137 | * most from it). For some reason, freshly created files seem |
| 138 | * not to update the directory's `lastModified` time |
| 139 | * immediately, but the untracked cache would need to rely on |
| 140 | * that. |
| 141 | * |
| 142 | * Therefore, with a sad heart, we disable this very useful |
| 143 | * feature on Windows. |
| 144 | */ |
Johannes Schindelin | cb59d55 | 2021-12-03 13:34:26 +0000 | [diff] [blame] | 145 | { "core.untrackedCache", "false", 1 }, |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 146 | #endif |
Johannes Schindelin | cb59d55 | 2021-12-03 13:34:26 +0000 | [diff] [blame] | 147 | { "core.logAllRefUpdates", "true", 1 }, |
| 148 | { "credential.https://dev.azure.com.useHttpPath", "true", 1 }, |
| 149 | { "credential.validate", "false", 1 }, /* GCM4W-only */ |
| 150 | { "gc.auto", "0", 1 }, |
| 151 | { "gui.GCWarning", "false", 1 }, |
Derrick Stolee | 17194b1 | 2023-01-06 16:31:56 +0000 | [diff] [blame] | 152 | { "index.skipHash", "false", 1 }, |
Johannes Schindelin | cb59d55 | 2021-12-03 13:34:26 +0000 | [diff] [blame] | 153 | { "index.threads", "true", 1 }, |
| 154 | { "index.version", "4", 1 }, |
| 155 | { "merge.stat", "false", 1 }, |
| 156 | { "merge.renames", "true", 1 }, |
| 157 | { "pack.useBitmaps", "false", 1 }, |
| 158 | { "pack.useSparse", "true", 1 }, |
| 159 | { "receive.autoGC", "false", 1 }, |
Johannes Schindelin | cb59d55 | 2021-12-03 13:34:26 +0000 | [diff] [blame] | 160 | { "feature.manyFiles", "false", 1 }, |
| 161 | { "feature.experimental", "false", 1 }, |
| 162 | { "fetch.unpackLimit", "1", 1 }, |
| 163 | { "fetch.writeCommitGraph", "false", 1 }, |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 164 | #ifdef WIN32 |
Johannes Schindelin | cb59d55 | 2021-12-03 13:34:26 +0000 | [diff] [blame] | 165 | { "http.sslBackend", "schannel", 1 }, |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 166 | #endif |
Johannes Schindelin | cb59d55 | 2021-12-03 13:34:26 +0000 | [diff] [blame] | 167 | /* Optional */ |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 168 | { "status.aheadBehind", "false" }, |
| 169 | { "commitGraph.generationVersion", "1" }, |
| 170 | { "core.autoCRLF", "false" }, |
| 171 | { "core.safeCRLF", "false" }, |
| 172 | { "fetch.showForcedUpdates", "false" }, |
| 173 | { NULL, NULL }, |
| 174 | }; |
| 175 | int i; |
| 176 | char *value; |
| 177 | |
| 178 | for (i = 0; config[i].key; i++) { |
Victoria Dye | d934a11 | 2022-08-18 21:40:50 +0000 | [diff] [blame] | 179 | if (set_scalar_config(config + i, reconfigure)) |
| 180 | return error(_("could not configure %s=%s"), |
| 181 | config[i].key, config[i].value); |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Matthew John Cheetham | 3f1917d | 2022-08-18 21:40:51 +0000 | [diff] [blame] | 184 | if (have_fsmonitor_support()) { |
| 185 | struct scalar_config fsmonitor = { "core.fsmonitor", "true" }; |
| 186 | if (set_scalar_config(&fsmonitor, reconfigure)) |
| 187 | return error(_("could not configure %s=%s"), |
| 188 | fsmonitor.key, fsmonitor.value); |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /* |
| 192 | * The `log.excludeDecoration` setting is special because it allows |
| 193 | * for multiple values. |
| 194 | */ |
| 195 | if (git_config_get_string("log.excludeDecoration", &value)) { |
| 196 | trace2_data_string("scalar", the_repository, |
| 197 | "log.excludeDecoration", "created"); |
| 198 | if (git_config_set_multivar_gently("log.excludeDecoration", |
| 199 | "refs/prefetch/*", |
| 200 | CONFIG_REGEX_NONE, 0)) |
| 201 | return error(_("could not configure " |
| 202 | "log.excludeDecoration")); |
| 203 | } else { |
| 204 | trace2_data_string("scalar", the_repository, |
| 205 | "log.excludeDecoration", "exists"); |
| 206 | free(value); |
| 207 | } |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
Derrick Stolee | c76a53e | 2021-12-03 13:34:20 +0000 | [diff] [blame] | 212 | static int toggle_maintenance(int enable) |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 213 | { |
Derrick Stolee | d871b6c | 2022-09-27 13:56:59 +0000 | [diff] [blame] | 214 | return run_git("maintenance", |
| 215 | enable ? "start" : "unregister", |
| 216 | enable ? NULL : "--force", |
| 217 | NULL); |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Derrick Stolee | c76a53e | 2021-12-03 13:34:20 +0000 | [diff] [blame] | 220 | static int add_or_remove_enlistment(int add) |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 221 | { |
| 222 | int res; |
| 223 | |
| 224 | if (!the_repository->worktree) |
| 225 | die(_("Scalar enlistments require a worktree")); |
| 226 | |
| 227 | res = run_git("config", "--global", "--get", "--fixed-value", |
| 228 | "scalar.repo", the_repository->worktree, NULL); |
| 229 | |
| 230 | /* |
Derrick Stolee | c76a53e | 2021-12-03 13:34:20 +0000 | [diff] [blame] | 231 | * If we want to add and the setting is already there, then do nothing. |
| 232 | * 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] | 233 | */ |
Derrick Stolee | c76a53e | 2021-12-03 13:34:20 +0000 | [diff] [blame] | 234 | if ((add && !res) || (!add && res)) |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 235 | return 0; |
| 236 | |
Derrick Stolee | c76a53e | 2021-12-03 13:34:20 +0000 | [diff] [blame] | 237 | return run_git("config", "--global", add ? "--add" : "--unset", |
| 238 | add ? "--no-fixed-value" : "--fixed-value", |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 239 | "scalar.repo", the_repository->worktree, NULL); |
| 240 | } |
| 241 | |
Matthew John Cheetham | 3f1917d | 2022-08-18 21:40:51 +0000 | [diff] [blame] | 242 | static int start_fsmonitor_daemon(void) |
| 243 | { |
| 244 | assert(have_fsmonitor_support()); |
| 245 | |
| 246 | if (fsmonitor_ipc__get_state() != IPC_STATE__LISTENING) |
| 247 | return run_git("fsmonitor--daemon", "start", NULL); |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
Johannes Schindelin | ec4c231 | 2022-08-18 21:40:52 +0000 | [diff] [blame] | 252 | static int stop_fsmonitor_daemon(void) |
| 253 | { |
| 254 | assert(have_fsmonitor_support()); |
| 255 | |
| 256 | if (fsmonitor_ipc__get_state() == IPC_STATE__LISTENING) |
| 257 | return run_git("fsmonitor--daemon", "stop", NULL); |
| 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 262 | static int register_dir(void) |
| 263 | { |
Victoria Dye | d2a79bc | 2022-08-18 21:40:48 +0000 | [diff] [blame] | 264 | if (add_or_remove_enlistment(1)) |
| 265 | return error(_("could not add enlistment")); |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 266 | |
Victoria Dye | d2a79bc | 2022-08-18 21:40:48 +0000 | [diff] [blame] | 267 | if (set_recommended_config(0)) |
| 268 | return error(_("could not set recommended config")); |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 269 | |
Victoria Dye | d2a79bc | 2022-08-18 21:40:48 +0000 | [diff] [blame] | 270 | if (toggle_maintenance(1)) |
Derrick Stolee | dea6308 | 2023-01-27 20:06:03 +0000 | [diff] [blame] | 271 | warning(_("could not turn on maintenance")); |
Derrick Stolee | c76a53e | 2021-12-03 13:34:20 +0000 | [diff] [blame] | 272 | |
Matthew John Cheetham | 3f1917d | 2022-08-18 21:40:51 +0000 | [diff] [blame] | 273 | if (have_fsmonitor_support() && start_fsmonitor_daemon()) { |
| 274 | return error(_("could not start the FSMonitor daemon")); |
| 275 | } |
| 276 | |
Victoria Dye | d2a79bc | 2022-08-18 21:40:48 +0000 | [diff] [blame] | 277 | return 0; |
Derrick Stolee | c76a53e | 2021-12-03 13:34:20 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | static int unregister_dir(void) |
| 281 | { |
| 282 | int res = 0; |
| 283 | |
Victoria Dye | adedcee | 2022-08-18 21:40:47 +0000 | [diff] [blame] | 284 | if (toggle_maintenance(0)) |
Victoria Dye | d2a79bc | 2022-08-18 21:40:48 +0000 | [diff] [blame] | 285 | res = error(_("could not turn off maintenance")); |
Derrick Stolee | c76a53e | 2021-12-03 13:34:20 +0000 | [diff] [blame] | 286 | |
Victoria Dye | adedcee | 2022-08-18 21:40:47 +0000 | [diff] [blame] | 287 | if (add_or_remove_enlistment(0)) |
Victoria Dye | d2a79bc | 2022-08-18 21:40:48 +0000 | [diff] [blame] | 288 | res = error(_("could not remove enlistment")); |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 289 | |
| 290 | return res; |
| 291 | } |
| 292 | |
Johannes Schindelin | 546f822 | 2021-12-03 13:34:23 +0000 | [diff] [blame] | 293 | /* printf-style interface, expects `<key>=<value>` argument */ |
Junio C Hamano | 99c7de7 | 2024-06-08 11:37:47 -0700 | [diff] [blame] | 294 | __attribute__((format (printf, 1, 2))) |
Johannes Schindelin | 546f822 | 2021-12-03 13:34:23 +0000 | [diff] [blame] | 295 | static int set_config(const char *fmt, ...) |
| 296 | { |
| 297 | struct strbuf buf = STRBUF_INIT; |
| 298 | char *value; |
| 299 | int res; |
| 300 | va_list args; |
| 301 | |
| 302 | va_start(args, fmt); |
| 303 | strbuf_vaddf(&buf, fmt, args); |
| 304 | va_end(args); |
| 305 | |
| 306 | value = strchr(buf.buf, '='); |
| 307 | if (value) |
| 308 | *(value++) = '\0'; |
| 309 | res = git_config_set_gently(buf.buf, value); |
| 310 | strbuf_release(&buf); |
| 311 | |
| 312 | return res; |
| 313 | } |
| 314 | |
| 315 | static char *remote_default_branch(const char *url) |
| 316 | { |
| 317 | struct child_process cp = CHILD_PROCESS_INIT; |
| 318 | struct strbuf out = STRBUF_INIT; |
| 319 | |
| 320 | cp.git_cmd = 1; |
| 321 | strvec_pushl(&cp.args, "ls-remote", "--symref", url, "HEAD", NULL); |
| 322 | if (!pipe_command(&cp, NULL, 0, &out, 0, NULL, 0)) { |
| 323 | const char *line = out.buf; |
| 324 | |
| 325 | while (*line) { |
| 326 | const char *eol = strchrnul(line, '\n'), *p; |
| 327 | size_t len = eol - line; |
| 328 | char *branch; |
| 329 | |
| 330 | if (!skip_prefix(line, "ref: ", &p) || |
| 331 | !strip_suffix_mem(line, &len, "\tHEAD")) { |
| 332 | line = eol + (*eol == '\n'); |
| 333 | continue; |
| 334 | } |
| 335 | |
| 336 | eol = line + len; |
| 337 | if (skip_prefix(p, "refs/heads/", &p)) { |
| 338 | branch = xstrndup(p, eol - p); |
| 339 | strbuf_release(&out); |
| 340 | return branch; |
| 341 | } |
| 342 | |
| 343 | error(_("remote HEAD is not a branch: '%.*s'"), |
| 344 | (int)(eol - p), p); |
| 345 | strbuf_release(&out); |
| 346 | return NULL; |
| 347 | } |
| 348 | } |
| 349 | warning(_("failed to get default branch name from remote; " |
| 350 | "using local default")); |
| 351 | strbuf_reset(&out); |
| 352 | |
| 353 | child_process_init(&cp); |
| 354 | cp.git_cmd = 1; |
| 355 | strvec_pushl(&cp.args, "symbolic-ref", "--short", "HEAD", NULL); |
| 356 | if (!pipe_command(&cp, NULL, 0, &out, 0, NULL, 0)) { |
| 357 | strbuf_trim(&out); |
| 358 | return strbuf_detach(&out, NULL); |
| 359 | } |
| 360 | |
| 361 | strbuf_release(&out); |
| 362 | error(_("failed to get default branch name")); |
| 363 | return NULL; |
| 364 | } |
| 365 | |
Matthew John Cheetham | d85ada7 | 2021-12-03 13:34:28 +0000 | [diff] [blame] | 366 | static int delete_enlistment(struct strbuf *enlistment) |
| 367 | { |
Matthew John Cheetham | d85ada7 | 2021-12-03 13:34:28 +0000 | [diff] [blame] | 368 | struct strbuf parent = STRBUF_INIT; |
Victoria Dye | 65f6a9e | 2022-08-18 21:40:46 +0000 | [diff] [blame] | 369 | size_t offset; |
| 370 | char *path_sep; |
Matthew John Cheetham | d85ada7 | 2021-12-03 13:34:28 +0000 | [diff] [blame] | 371 | |
| 372 | if (unregister_dir()) |
Victoria Dye | 9b24bb9 | 2022-08-18 21:40:49 +0000 | [diff] [blame] | 373 | return error(_("failed to unregister repository")); |
Matthew John Cheetham | d85ada7 | 2021-12-03 13:34:28 +0000 | [diff] [blame] | 374 | |
Matthew John Cheetham | d85ada7 | 2021-12-03 13:34:28 +0000 | [diff] [blame] | 375 | /* |
| 376 | * Change the current directory to one outside of the enlistment so |
| 377 | * that we may delete everything underneath it. |
| 378 | */ |
Victoria Dye | 65f6a9e | 2022-08-18 21:40:46 +0000 | [diff] [blame] | 379 | offset = offset_1st_component(enlistment->buf); |
| 380 | path_sep = find_last_dir_sep(enlistment->buf + offset); |
| 381 | strbuf_add(&parent, enlistment->buf, |
| 382 | path_sep ? path_sep - enlistment->buf : offset); |
Victoria Dye | 9b24bb9 | 2022-08-18 21:40:49 +0000 | [diff] [blame] | 383 | if (chdir(parent.buf) < 0) { |
| 384 | int res = error_errno(_("could not switch to '%s'"), parent.buf); |
| 385 | strbuf_release(&parent); |
| 386 | return res; |
| 387 | } |
Matthew John Cheetham | d85ada7 | 2021-12-03 13:34:28 +0000 | [diff] [blame] | 388 | strbuf_release(&parent); |
Matthew John Cheetham | d85ada7 | 2021-12-03 13:34:28 +0000 | [diff] [blame] | 389 | |
Johannes Schindelin | ec4c231 | 2022-08-18 21:40:52 +0000 | [diff] [blame] | 390 | if (have_fsmonitor_support() && stop_fsmonitor_daemon()) |
| 391 | return error(_("failed to stop the FSMonitor daemon")); |
| 392 | |
Matthew John Cheetham | d85ada7 | 2021-12-03 13:34:28 +0000 | [diff] [blame] | 393 | if (remove_dir_recursively(enlistment, 0)) |
Victoria Dye | 9b24bb9 | 2022-08-18 21:40:49 +0000 | [diff] [blame] | 394 | return error(_("failed to delete enlistment directory")); |
Matthew John Cheetham | d85ada7 | 2021-12-03 13:34:28 +0000 | [diff] [blame] | 395 | |
| 396 | return 0; |
| 397 | } |
| 398 | |
Johannes Schindelin | ddc35d8 | 2021-12-03 13:34:29 +0000 | [diff] [blame] | 399 | /* |
| 400 | * Dummy implementation; Using `get_version_info()` would cause a link error |
| 401 | * without this. |
| 402 | */ |
| 403 | void load_builtin_commands(const char *prefix, struct cmdnames *cmds) |
| 404 | { |
| 405 | die("not implemented"); |
| 406 | } |
| 407 | |
Johannes Schindelin | 546f822 | 2021-12-03 13:34:23 +0000 | [diff] [blame] | 408 | static int cmd_clone(int argc, const char **argv) |
| 409 | { |
| 410 | const char *branch = NULL; |
ZheNing Hu | 4433bd2 | 2023-01-11 13:14:20 +0000 | [diff] [blame] | 411 | int full_clone = 0, single_branch = 0, show_progress = isatty(2); |
Derrick Stolee | 4527db8 | 2023-08-28 13:52:24 +0000 | [diff] [blame] | 412 | int src = 1; |
Johannes Schindelin | 546f822 | 2021-12-03 13:34:23 +0000 | [diff] [blame] | 413 | 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 Schindelin | 4368e40 | 2021-12-03 13:34:24 +0000 | [diff] [blame] | 418 | OPT_BOOL(0, "single-branch", &single_branch, |
| 419 | N_("only download metadata for the branch that will " |
| 420 | "be checked out")), |
Derrick Stolee | 4527db8 | 2023-08-28 13:52:24 +0000 | [diff] [blame] | 421 | OPT_BOOL(0, "src", &src, |
| 422 | N_("create repository within 'src' directory")), |
Johannes Schindelin | 546f822 | 2021-12-03 13:34:23 +0000 | [diff] [blame] | 423 | OPT_END(), |
| 424 | }; |
| 425 | const char * const clone_usage[] = { |
Derrick Stolee | 4527db8 | 2023-08-28 13:52:24 +0000 | [diff] [blame] | 426 | N_("scalar clone [--single-branch] [--branch <main-branch>] [--full-clone]\n" |
| 427 | "\t[--[no-]src] <url> [<enlistment>]"), |
Johannes Schindelin | 546f822 | 2021-12-03 13:34:23 +0000 | [diff] [blame] | 428 | 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 Stolee | 4527db8 | 2023-08-28 13:52:24 +0000 | [diff] [blame] | 463 | if (src) |
| 464 | dir = xstrfmt("%s/src", enlistment); |
| 465 | else |
| 466 | dir = xstrdup(enlistment); |
Johannes Schindelin | 546f822 | 2021-12-03 13:34:23 +0000 | [diff] [blame] | 467 | |
| 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 Schindelin | 4368e40 | 2021-12-03 13:34:24 +0000 | [diff] [blame] | 497 | "+refs/heads/%s:refs/remotes/origin/%s", |
| 498 | single_branch ? branch : "*", |
| 499 | single_branch ? branch : "*") || |
Johannes Schindelin | 546f822 | 2021-12-03 13:34:23 +0000 | [diff] [blame] | 500 | 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 Schindelin | cb59d55 | 2021-12-03 13:34:26 +0000 | [diff] [blame] | 510 | if (set_recommended_config(0)) |
Johannes Schindelin | 546f822 | 2021-12-03 13:34:23 +0000 | [diff] [blame] | 511 | return error(_("could not configure '%s'"), dir); |
| 512 | |
ZheNing Hu | 4433bd2 | 2023-01-11 13:14:20 +0000 | [diff] [blame] | 513 | if ((res = run_git("fetch", "--quiet", |
| 514 | show_progress ? "--progress" : "--no-progress", |
| 515 | "origin", NULL))) { |
Johannes Schindelin | 546f822 | 2021-12-03 13:34:23 +0000 | [diff] [blame] | 516 | 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 Hu | 4433bd2 | 2023-01-11 13:14:20 +0000 | [diff] [blame] | 524 | if ((res = run_git("fetch", "--quiet", |
| 525 | show_progress ? "--progress" : "--no-progress", |
| 526 | "origin", NULL))) |
Johannes Schindelin | 546f822 | 2021-12-03 13:34:23 +0000 | [diff] [blame] | 527 | 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 | |
| 544 | cleanup: |
| 545 | free(enlistment); |
| 546 | free(dir); |
| 547 | strbuf_release(&buf); |
| 548 | return res; |
| 549 | } |
| 550 | |
Johannes Schindelin | aa5c79a | 2022-05-28 16:11:15 -0700 | [diff] [blame] | 551 | static 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 Dye | 672196a | 2022-08-12 20:10:18 +0000 | [diff] [blame] | 560 | struct strbuf diagnostics_root = STRBUF_INIT; |
Johannes Schindelin | aa5c79a | 2022-05-28 16:11:15 -0700 | [diff] [blame] | 561 | int res = 0; |
| 562 | |
| 563 | argc = parse_options(argc, argv, NULL, options, |
| 564 | usage, 0); |
| 565 | |
Victoria Dye | 672196a | 2022-08-12 20:10:18 +0000 | [diff] [blame] | 566 | setup_enlistment_directory(argc, argv, usage, options, &diagnostics_root); |
| 567 | strbuf_addstr(&diagnostics_root, "/.scalarDiagnostics"); |
Johannes Schindelin | aa5c79a | 2022-05-28 16:11:15 -0700 | [diff] [blame] | 568 | |
Victoria Dye | 672196a | 2022-08-12 20:10:18 +0000 | [diff] [blame] | 569 | res = run_git("diagnose", "--mode=all", "-s", "%Y%m%d_%H%M%S", |
| 570 | "-o", diagnostics_root.buf, NULL); |
Johannes Schindelin | aa5c79a | 2022-05-28 16:11:15 -0700 | [diff] [blame] | 571 | |
Victoria Dye | 672196a | 2022-08-12 20:10:18 +0000 | [diff] [blame] | 572 | strbuf_release(&diagnostics_root); |
Johannes Schindelin | aa5c79a | 2022-05-28 16:11:15 -0700 | [diff] [blame] | 573 | return res; |
| 574 | } |
| 575 | |
Jeff King | 6ba21fa | 2023-03-28 16:57:04 -0400 | [diff] [blame] | 576 | static int cmd_list(int argc, const char **argv UNUSED) |
Derrick Stolee | 2b71045 | 2021-12-03 13:34:22 +0000 | [diff] [blame] | 577 | { |
| 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 Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 586 | static 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 Schindelin | 0a43fb2 | 2021-12-03 13:34:16 +0000 | [diff] [blame] | 603 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 604 | static int get_scalar_repos(const char *key, const char *value, |
| 605 | const struct config_context *ctx UNUSED, |
| 606 | void *data) |
Johannes Schindelin | 4582676 | 2021-12-03 13:34:27 +0000 | [diff] [blame] | 607 | { |
| 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 Schindelin | c90db53 | 2022-11-07 18:25:01 +0000 | [diff] [blame] | 616 | static 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 Schindelin | cb59d55 | 2021-12-03 13:34:26 +0000 | [diff] [blame] | 634 | static int cmd_reconfigure(int argc, const char **argv) |
| 635 | { |
Johannes Schindelin | 4582676 | 2021-12-03 13:34:27 +0000 | [diff] [blame] | 636 | int all = 0; |
Johannes Schindelin | cb59d55 | 2021-12-03 13:34:26 +0000 | [diff] [blame] | 637 | struct option options[] = { |
Johannes Schindelin | 4582676 | 2021-12-03 13:34:27 +0000 | [diff] [blame] | 638 | OPT_BOOL('a', "all", &all, |
| 639 | N_("reconfigure all registered enlistments")), |
Johannes Schindelin | cb59d55 | 2021-12-03 13:34:26 +0000 | [diff] [blame] | 640 | OPT_END(), |
| 641 | }; |
| 642 | const char * const usage[] = { |
Johannes Schindelin | 4582676 | 2021-12-03 13:34:27 +0000 | [diff] [blame] | 643 | N_("scalar reconfigure [--all | <enlistment>]"), |
Johannes Schindelin | cb59d55 | 2021-12-03 13:34:26 +0000 | [diff] [blame] | 644 | NULL |
| 645 | }; |
Johannes Schindelin | 4582676 | 2021-12-03 13:34:27 +0000 | [diff] [blame] | 646 | struct string_list scalar_repos = STRING_LIST_INIT_DUP; |
| 647 | int i, res = 0; |
Johannes Schindelin | 4582676 | 2021-12-03 13:34:27 +0000 | [diff] [blame] | 648 | struct strbuf commondir = STRBUF_INIT, gitdir = STRBUF_INIT; |
Johannes Schindelin | cb59d55 | 2021-12-03 13:34:26 +0000 | [diff] [blame] | 649 | |
| 650 | argc = parse_options(argc, argv, NULL, options, |
| 651 | usage, 0); |
| 652 | |
Johannes Schindelin | 4582676 | 2021-12-03 13:34:27 +0000 | [diff] [blame] | 653 | if (!all) { |
| 654 | setup_enlistment_directory(argc, argv, usage, options, NULL); |
Johannes Schindelin | cb59d55 | 2021-12-03 13:34:26 +0000 | [diff] [blame] | 655 | |
Johannes Schindelin | 4582676 | 2021-12-03 13:34:27 +0000 | [diff] [blame] | 656 | return set_recommended_config(1); |
| 657 | } |
| 658 | |
| 659 | if (argc > 0) |
| 660 | usage_msg_opt(_("--all or <enlistment>, but not both"), |
| 661 | usage, options); |
| 662 | |
| 663 | git_config(get_scalar_repos, &scalar_repos); |
| 664 | |
| 665 | for (i = 0; i < scalar_repos.nr; i++) { |
Derrick Stolee | f9a547d | 2023-08-28 13:52:26 +0000 | [diff] [blame] | 666 | int succeeded = 0; |
Derrick Stolee | b64b0df | 2024-05-08 00:05:49 +0000 | [diff] [blame] | 667 | struct repository *old_repo, r = { NULL }; |
Johannes Schindelin | 4582676 | 2021-12-03 13:34:27 +0000 | [diff] [blame] | 668 | const char *dir = scalar_repos.items[i].string; |
| 669 | |
| 670 | strbuf_reset(&commondir); |
| 671 | strbuf_reset(&gitdir); |
| 672 | |
| 673 | if (chdir(dir) < 0) { |
Johannes Schindelin | c90db53 | 2022-11-07 18:25:01 +0000 | [diff] [blame] | 674 | struct strbuf buf = STRBUF_INIT; |
| 675 | |
| 676 | if (errno != ENOENT) { |
| 677 | warning_errno(_("could not switch to '%s'"), dir); |
Derrick Stolee | f9a547d | 2023-08-28 13:52:26 +0000 | [diff] [blame] | 678 | goto loop_end; |
Johannes Schindelin | c90db53 | 2022-11-07 18:25:01 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | strbuf_addstr(&buf, dir); |
| 682 | if (remove_deleted_enlistment(&buf)) |
Derrick Stolee | f9a547d | 2023-08-28 13:52:26 +0000 | [diff] [blame] | 683 | error(_("could not remove stale " |
| 684 | "scalar.repo '%s'"), dir); |
| 685 | else { |
| 686 | warning(_("removed stale scalar.repo '%s'"), |
Johannes Schindelin | c90db53 | 2022-11-07 18:25:01 +0000 | [diff] [blame] | 687 | dir); |
Derrick Stolee | f9a547d | 2023-08-28 13:52:26 +0000 | [diff] [blame] | 688 | succeeded = 1; |
| 689 | } |
Johannes Schindelin | c90db53 | 2022-11-07 18:25:01 +0000 | [diff] [blame] | 690 | strbuf_release(&buf); |
Derrick Stolee | f9a547d | 2023-08-28 13:52:26 +0000 | [diff] [blame] | 691 | 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 | |
Derrick Stolee | b64b0df | 2024-05-08 00:05:49 +0000 | [diff] [blame] | 715 | if (repo_init(&r, gitdir.buf, commondir.buf)) |
| 716 | goto loop_end; |
| 717 | |
| 718 | old_repo = the_repository; |
Derrick Stolee | f9a547d | 2023-08-28 13:52:26 +0000 | [diff] [blame] | 719 | the_repository = &r; |
Derrick Stolee | f9a547d | 2023-08-28 13:52:26 +0000 | [diff] [blame] | 720 | |
| 721 | if (set_recommended_config(1) >= 0) |
| 722 | succeeded = 1; |
| 723 | |
Derrick Stolee | b64b0df | 2024-05-08 00:05:49 +0000 | [diff] [blame] | 724 | the_repository = old_repo; |
| 725 | |
Derrick Stolee | f9a547d | 2023-08-28 13:52:26 +0000 | [diff] [blame] | 726 | loop_end: |
| 727 | if (!succeeded) { |
Johannes Schindelin | 4582676 | 2021-12-03 13:34:27 +0000 | [diff] [blame] | 728 | res = -1; |
Derrick Stolee | f9a547d | 2023-08-28 13:52:26 +0000 | [diff] [blame] | 729 | warning(_("to unregister this repository from Scalar, run\n" |
| 730 | "\tgit config --global --unset --fixed-value scalar.repo \"%s\""), |
| 731 | dir); |
Johannes Schindelin | 4582676 | 2021-12-03 13:34:27 +0000 | [diff] [blame] | 732 | } |
| 733 | } |
| 734 | |
| 735 | string_list_clear(&scalar_repos, 1); |
| 736 | strbuf_release(&commondir); |
| 737 | strbuf_release(&gitdir); |
| 738 | |
| 739 | return res; |
Johannes Schindelin | cb59d55 | 2021-12-03 13:34:26 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Derrick Stolee | 7020c88 | 2021-12-03 13:34:25 +0000 | [diff] [blame] | 742 | static int cmd_run(int argc, const char **argv) |
| 743 | { |
| 744 | struct option options[] = { |
| 745 | OPT_END(), |
| 746 | }; |
| 747 | struct { |
| 748 | const char *arg, *task; |
| 749 | } tasks[] = { |
| 750 | { "config", NULL }, |
| 751 | { "commit-graph", "commit-graph" }, |
| 752 | { "fetch", "prefetch" }, |
| 753 | { "loose-objects", "loose-objects" }, |
| 754 | { "pack-files", "incremental-repack" }, |
| 755 | { NULL, NULL } |
| 756 | }; |
| 757 | struct strbuf buf = STRBUF_INIT; |
| 758 | const char *usagestr[] = { NULL, NULL }; |
| 759 | int i; |
| 760 | |
| 761 | strbuf_addstr(&buf, N_("scalar run <task> [<enlistment>]\nTasks:\n")); |
| 762 | for (i = 0; tasks[i].arg; i++) |
| 763 | strbuf_addf(&buf, "\t%s\n", tasks[i].arg); |
| 764 | usagestr[0] = buf.buf; |
| 765 | |
| 766 | argc = parse_options(argc, argv, NULL, options, |
| 767 | usagestr, 0); |
| 768 | |
| 769 | if (!argc) |
| 770 | usage_with_options(usagestr, options); |
| 771 | |
| 772 | if (!strcmp("all", argv[0])) { |
| 773 | i = -1; |
| 774 | } else { |
| 775 | for (i = 0; tasks[i].arg && strcmp(tasks[i].arg, argv[0]); i++) |
| 776 | ; /* keep looking for the task */ |
| 777 | |
| 778 | if (i > 0 && !tasks[i].arg) { |
| 779 | error(_("no such task: '%s'"), argv[0]); |
| 780 | usage_with_options(usagestr, options); |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | argc--; |
| 785 | argv++; |
| 786 | setup_enlistment_directory(argc, argv, usagestr, options, NULL); |
| 787 | strbuf_release(&buf); |
| 788 | |
| 789 | if (i == 0) |
| 790 | return register_dir(); |
| 791 | |
| 792 | if (i > 0) |
| 793 | return run_git("maintenance", "run", |
| 794 | "--task", tasks[i].task, NULL); |
| 795 | |
| 796 | if (register_dir()) |
| 797 | return -1; |
| 798 | for (i = 1; tasks[i].arg; i++) |
| 799 | if (run_git("maintenance", "run", |
| 800 | "--task", tasks[i].task, NULL)) |
| 801 | return -1; |
| 802 | return 0; |
| 803 | } |
| 804 | |
Derrick Stolee | c76a53e | 2021-12-03 13:34:20 +0000 | [diff] [blame] | 805 | static int cmd_unregister(int argc, const char **argv) |
| 806 | { |
| 807 | struct option options[] = { |
| 808 | OPT_END(), |
| 809 | }; |
| 810 | const char * const usage[] = { |
| 811 | N_("scalar unregister [<enlistment>]"), |
| 812 | NULL |
| 813 | }; |
| 814 | |
| 815 | argc = parse_options(argc, argv, NULL, options, |
| 816 | usage, 0); |
| 817 | |
Johannes Schindelin | f5f0842 | 2021-12-03 13:34:21 +0000 | [diff] [blame] | 818 | /* |
| 819 | * Be forgiving when the enlistment or worktree does not even exist any |
| 820 | * longer; This can be the case if a user deleted the worktree by |
| 821 | * mistake and _still_ wants to unregister the thing. |
| 822 | */ |
| 823 | if (argc == 1) { |
| 824 | struct strbuf src_path = STRBUF_INIT, workdir_path = STRBUF_INIT; |
| 825 | |
| 826 | strbuf_addf(&src_path, "%s/src/.git", argv[0]); |
| 827 | strbuf_addf(&workdir_path, "%s/.git", argv[0]); |
| 828 | if (!is_directory(src_path.buf) && !is_directory(workdir_path.buf)) { |
| 829 | /* remove possible matching registrations */ |
| 830 | int res = -1; |
| 831 | |
| 832 | strbuf_strip_suffix(&src_path, "/.git"); |
| 833 | res = remove_deleted_enlistment(&src_path) && res; |
| 834 | |
| 835 | strbuf_strip_suffix(&workdir_path, "/.git"); |
| 836 | res = remove_deleted_enlistment(&workdir_path) && res; |
| 837 | |
| 838 | strbuf_release(&src_path); |
| 839 | strbuf_release(&workdir_path); |
| 840 | return res; |
| 841 | } |
| 842 | strbuf_release(&src_path); |
| 843 | strbuf_release(&workdir_path); |
| 844 | } |
| 845 | |
Derrick Stolee | c76a53e | 2021-12-03 13:34:20 +0000 | [diff] [blame] | 846 | setup_enlistment_directory(argc, argv, usage, options, NULL); |
| 847 | |
| 848 | return unregister_dir(); |
| 849 | } |
| 850 | |
Matthew John Cheetham | d85ada7 | 2021-12-03 13:34:28 +0000 | [diff] [blame] | 851 | static int cmd_delete(int argc, const char **argv) |
| 852 | { |
| 853 | char *cwd = xgetcwd(); |
| 854 | struct option options[] = { |
| 855 | OPT_END(), |
| 856 | }; |
| 857 | const char * const usage[] = { |
| 858 | N_("scalar delete <enlistment>"), |
| 859 | NULL |
| 860 | }; |
| 861 | struct strbuf enlistment = STRBUF_INIT; |
| 862 | int res = 0; |
| 863 | |
| 864 | argc = parse_options(argc, argv, NULL, options, |
| 865 | usage, 0); |
| 866 | |
| 867 | if (argc != 1) |
| 868 | usage_with_options(usage, options); |
| 869 | |
| 870 | setup_enlistment_directory(argc, argv, usage, options, &enlistment); |
| 871 | |
| 872 | if (dir_inside_of(cwd, enlistment.buf) >= 0) |
| 873 | res = error(_("refusing to delete current working directory")); |
| 874 | else { |
| 875 | close_object_store(the_repository->objects); |
| 876 | res = delete_enlistment(&enlistment); |
| 877 | } |
| 878 | strbuf_release(&enlistment); |
| 879 | free(cwd); |
| 880 | |
| 881 | return res; |
| 882 | } |
| 883 | |
Johannes Schindelin | 951759d | 2022-09-02 15:56:45 +0000 | [diff] [blame] | 884 | static int cmd_help(int argc, const char **argv) |
| 885 | { |
| 886 | struct option options[] = { |
| 887 | OPT_END(), |
| 888 | }; |
| 889 | const char * const usage[] = { |
| 890 | "scalar help", |
| 891 | NULL |
| 892 | }; |
| 893 | |
| 894 | argc = parse_options(argc, argv, NULL, options, |
| 895 | usage, 0); |
| 896 | |
| 897 | if (argc != 0) |
| 898 | usage_with_options(usage, options); |
| 899 | |
| 900 | return run_git("help", "scalar", NULL); |
| 901 | } |
| 902 | |
Johannes Schindelin | ddc35d8 | 2021-12-03 13:34:29 +0000 | [diff] [blame] | 903 | static int cmd_version(int argc, const char **argv) |
| 904 | { |
| 905 | int verbose = 0, build_options = 0; |
| 906 | struct option options[] = { |
| 907 | OPT__VERBOSE(&verbose, N_("include Git version")), |
| 908 | OPT_BOOL(0, "build-options", &build_options, |
| 909 | N_("include Git's build options")), |
| 910 | OPT_END(), |
| 911 | }; |
| 912 | const char * const usage[] = { |
| 913 | N_("scalar verbose [-v | --verbose] [--build-options]"), |
| 914 | NULL |
| 915 | }; |
| 916 | struct strbuf buf = STRBUF_INIT; |
| 917 | |
| 918 | argc = parse_options(argc, argv, NULL, options, |
| 919 | usage, 0); |
| 920 | |
| 921 | if (argc != 0) |
| 922 | usage_with_options(usage, options); |
| 923 | |
| 924 | get_version_info(&buf, build_options); |
| 925 | fprintf(stderr, "%s\n", buf.buf); |
| 926 | strbuf_release(&buf); |
| 927 | |
| 928 | return 0; |
| 929 | } |
| 930 | |
Johannes Schindelin | 0a43fb2 | 2021-12-03 13:34:16 +0000 | [diff] [blame] | 931 | static struct { |
| 932 | const char *name; |
| 933 | int (*fn)(int, const char **); |
| 934 | } builtins[] = { |
Johannes Schindelin | 546f822 | 2021-12-03 13:34:23 +0000 | [diff] [blame] | 935 | { "clone", cmd_clone }, |
Derrick Stolee | 2b71045 | 2021-12-03 13:34:22 +0000 | [diff] [blame] | 936 | { "list", cmd_list }, |
Derrick Stolee | d0feac4 | 2021-12-03 13:34:19 +0000 | [diff] [blame] | 937 | { "register", cmd_register }, |
Derrick Stolee | c76a53e | 2021-12-03 13:34:20 +0000 | [diff] [blame] | 938 | { "unregister", cmd_unregister }, |
Derrick Stolee | 7020c88 | 2021-12-03 13:34:25 +0000 | [diff] [blame] | 939 | { "run", cmd_run }, |
Johannes Schindelin | cb59d55 | 2021-12-03 13:34:26 +0000 | [diff] [blame] | 940 | { "reconfigure", cmd_reconfigure }, |
Matthew John Cheetham | d85ada7 | 2021-12-03 13:34:28 +0000 | [diff] [blame] | 941 | { "delete", cmd_delete }, |
Johannes Schindelin | 951759d | 2022-09-02 15:56:45 +0000 | [diff] [blame] | 942 | { "help", cmd_help }, |
Johannes Schindelin | ddc35d8 | 2021-12-03 13:34:29 +0000 | [diff] [blame] | 943 | { "version", cmd_version }, |
Johannes Schindelin | aa5c79a | 2022-05-28 16:11:15 -0700 | [diff] [blame] | 944 | { "diagnose", cmd_diagnose }, |
Johannes Schindelin | 0a43fb2 | 2021-12-03 13:34:16 +0000 | [diff] [blame] | 945 | { NULL, NULL}, |
| 946 | }; |
| 947 | |
| 948 | int cmd_main(int argc, const char **argv) |
| 949 | { |
| 950 | struct strbuf scalar_usage = STRBUF_INIT; |
| 951 | int i; |
| 952 | |
Johannes Schindelin | 2ae8eb5 | 2022-01-28 14:31:57 +0000 | [diff] [blame] | 953 | while (argc > 1 && *argv[1] == '-') { |
| 954 | if (!strcmp(argv[1], "-C")) { |
| 955 | if (argc < 3) |
| 956 | die(_("-C requires a <directory>")); |
| 957 | if (chdir(argv[2]) < 0) |
| 958 | die_errno(_("could not change to '%s'"), |
| 959 | argv[2]); |
| 960 | argc -= 2; |
| 961 | argv += 2; |
| 962 | } else if (!strcmp(argv[1], "-c")) { |
| 963 | if (argc < 3) |
| 964 | die(_("-c requires a <key>=<value> argument")); |
| 965 | git_config_push_parameter(argv[2]); |
| 966 | argc -= 2; |
| 967 | argv += 2; |
| 968 | } else |
| 969 | break; |
| 970 | } |
| 971 | |
Johannes Schindelin | 0a43fb2 | 2021-12-03 13:34:16 +0000 | [diff] [blame] | 972 | if (argc > 1) { |
| 973 | argv++; |
| 974 | argc--; |
| 975 | |
| 976 | for (i = 0; builtins[i].name; i++) |
| 977 | if (!strcmp(builtins[i].name, argv[0])) |
| 978 | return !!builtins[i].fn(argc, argv); |
| 979 | } |
| 980 | |
| 981 | strbuf_addstr(&scalar_usage, |
Johannes Schindelin | 2ae8eb5 | 2022-01-28 14:31:57 +0000 | [diff] [blame] | 982 | N_("scalar [-C <directory>] [-c <key>=<value>] " |
| 983 | "<command> [<options>]\n\nCommands:\n")); |
Johannes Schindelin | 0a43fb2 | 2021-12-03 13:34:16 +0000 | [diff] [blame] | 984 | for (i = 0; builtins[i].name; i++) |
| 985 | strbuf_addf(&scalar_usage, "\t%s\n", builtins[i].name); |
| 986 | |
| 987 | usage(scalar_usage.buf); |
| 988 | } |