| 1 | #include "builtin.h" |
| 2 | #include "config.h" |
| 3 | #include "fsck.h" |
| 4 | #include "pack-refs.h" |
| 5 | #include "parse-options.h" |
| 6 | #include "refs.h" |
| 7 | #include "strbuf.h" |
| 8 | #include "worktree.h" |
| 9 | #include "for-each-ref.h" |
| 10 | #include "refs/refs-internal.h" |
| 11 | |
| 12 | #define REFS_MIGRATE_USAGE \ |
| 13 | N_("git refs migrate --ref-format=<format> [--no-reflog] [--dry-run]") |
| 14 | |
| 15 | #define REFS_VERIFY_USAGE \ |
| 16 | N_("git refs verify [--strict] [--verbose]") |
| 17 | |
| 18 | #define REFS_EXISTS_USAGE \ |
| 19 | N_("git refs exists <ref>") |
| 20 | |
| 21 | #define REFS_OPTIMIZE_USAGE \ |
| 22 | N_("git refs optimize " PACK_REFS_OPTS) |
| 23 | |
| 24 | #define REFS_CREATE_USAGE \ |
| 25 | N_("git refs create [--message=<reason>] [--no-deref] [--create-reflog] <ref> <new-value>") |
| 26 | |
| 27 | #define REFS_DELETE_USAGE \ |
| 28 | N_("git refs delete [--message=<reason>] [--no-deref] <ref> [<old-value>]") |
| 29 | |
| 30 | #define REFS_UPDATE_USAGE \ |
| 31 | N_("git refs update [--message=<reason>] [--no-deref] [--create-reflog] <ref> <new-value> [<old-value>]") |
| 32 | |
| 33 | #define REFS_RENAME_USAGE \ |
| 34 | N_("git refs rename [--message=<reason>] <old-ref> <new-ref>") |
| 35 | |
| 36 | static int cmd_refs_migrate(int argc, const char **argv, const char *prefix, |
| 37 | struct repository *repo) |
| 38 | { |
| 39 | const char * const migrate_usage[] = { |
| 40 | REFS_MIGRATE_USAGE, |
| 41 | NULL, |
| 42 | }; |
| 43 | const char *format_str = NULL; |
| 44 | enum ref_storage_format format; |
| 45 | unsigned int flags = 0; |
| 46 | struct option options[] = { |
| 47 | OPT_STRING_F(0, "ref-format", &format_str, N_("format"), |
| 48 | N_("specify the reference format to convert to"), |
| 49 | PARSE_OPT_NONEG), |
| 50 | OPT_BIT(0, "dry-run", &flags, |
| 51 | N_("perform a non-destructive dry-run"), |
| 52 | REPO_MIGRATE_REF_STORAGE_FORMAT_DRYRUN), |
| 53 | OPT_BIT(0, "no-reflog", &flags, |
| 54 | N_("drop reflogs entirely during the migration"), |
| 55 | REPO_MIGRATE_REF_STORAGE_FORMAT_SKIP_REFLOG), |
| 56 | OPT_END(), |
| 57 | }; |
| 58 | struct strbuf errbuf = STRBUF_INIT; |
| 59 | int err; |
| 60 | |
| 61 | argc = parse_options(argc, argv, prefix, options, migrate_usage, 0); |
| 62 | if (argc) |
| 63 | usage(_("too many arguments")); |
| 64 | if (!format_str) |
| 65 | usage(_("missing --ref-format=<format>")); |
| 66 | |
| 67 | format = ref_storage_format_by_name(format_str); |
| 68 | if (format == REF_STORAGE_FORMAT_UNKNOWN) { |
| 69 | err = error(_("unknown ref storage format '%s'"), format_str); |
| 70 | goto out; |
| 71 | } |
| 72 | |
| 73 | if (repo->ref_storage_format == format) { |
| 74 | err = error(_("repository already uses '%s' format"), |
| 75 | ref_storage_format_to_name(format)); |
| 76 | goto out; |
| 77 | } |
| 78 | |
| 79 | if (repo_migrate_ref_storage_format(repo, format, flags, &errbuf) < 0) { |
| 80 | err = error("%s", errbuf.buf); |
| 81 | goto out; |
| 82 | } |
| 83 | |
| 84 | err = 0; |
| 85 | |
| 86 | out: |
| 87 | strbuf_release(&errbuf); |
| 88 | return err; |
| 89 | } |
| 90 | |
| 91 | static int cmd_refs_verify(int argc, const char **argv, const char *prefix, |
| 92 | struct repository *repo) |
| 93 | { |
| 94 | struct fsck_options fsck_refs_options; |
| 95 | struct worktree **worktrees; |
| 96 | const char * const verify_usage[] = { |
| 97 | REFS_VERIFY_USAGE, |
| 98 | NULL, |
| 99 | }; |
| 100 | struct option options[] = { |
| 101 | OPT_BOOL(0, "verbose", &fsck_refs_options.verbose, N_("be verbose")), |
| 102 | OPT_BOOL(0, "strict", &fsck_refs_options.strict, N_("enable strict checking")), |
| 103 | OPT_END(), |
| 104 | }; |
| 105 | int ret = 0; |
| 106 | |
| 107 | fsck_options_init(&fsck_refs_options, repo, FSCK_OPTIONS_REFS); |
| 108 | |
| 109 | argc = parse_options(argc, argv, prefix, options, verify_usage, 0); |
| 110 | if (argc) |
| 111 | usage(_("'git refs verify' takes no arguments")); |
| 112 | |
| 113 | repo_config(repo, git_fsck_config, &fsck_refs_options); |
| 114 | prepare_repo_settings(repo); |
| 115 | |
| 116 | worktrees = get_worktrees_without_reading_head(repo); |
| 117 | for (size_t i = 0; worktrees[i]; i++) |
| 118 | ret |= refs_fsck(get_worktree_ref_store(worktrees[i]), |
| 119 | &fsck_refs_options, worktrees[i]); |
| 120 | |
| 121 | fsck_options_clear(&fsck_refs_options); |
| 122 | free_worktrees(worktrees); |
| 123 | return ret; |
| 124 | } |
| 125 | |
| 126 | static int cmd_refs_list(int argc, const char **argv, const char *prefix, |
| 127 | struct repository *repo) |
| 128 | { |
| 129 | static char const * const refs_list_usage[] = { |
| 130 | N_("git refs list " COMMON_USAGE_FOR_EACH_REF), |
| 131 | NULL |
| 132 | }; |
| 133 | |
| 134 | return for_each_ref_core(argc, argv, prefix, repo, refs_list_usage); |
| 135 | } |
| 136 | |
| 137 | static int cmd_refs_exists(int argc, const char **argv, const char *prefix, |
| 138 | struct repository *repo) |
| 139 | { |
| 140 | struct strbuf unused_referent = STRBUF_INIT; |
| 141 | struct object_id unused_oid; |
| 142 | unsigned int unused_type; |
| 143 | int failure_errno = 0; |
| 144 | const char *ref; |
| 145 | int ret = 0; |
| 146 | const char * const exists_usage[] = { |
| 147 | REFS_EXISTS_USAGE, |
| 148 | NULL, |
| 149 | }; |
| 150 | struct option options[] = { |
| 151 | OPT_END(), |
| 152 | }; |
| 153 | |
| 154 | argc = parse_options(argc, argv, prefix, options, exists_usage, 0); |
| 155 | if (argc != 1) |
| 156 | die(_("'git refs exists' requires a reference")); |
| 157 | |
| 158 | ref = *argv++; |
| 159 | if (refs_read_raw_ref(get_main_ref_store(repo), ref, |
| 160 | &unused_oid, &unused_referent, &unused_type, |
| 161 | &failure_errno)) { |
| 162 | if (failure_errno == ENOENT || failure_errno == EISDIR) { |
| 163 | error(_("reference does not exist")); |
| 164 | ret = 2; |
| 165 | } else { |
| 166 | errno = failure_errno; |
| 167 | error_errno(_("failed to look up reference")); |
| 168 | ret = 1; |
| 169 | } |
| 170 | |
| 171 | goto out; |
| 172 | } |
| 173 | |
| 174 | out: |
| 175 | strbuf_release(&unused_referent); |
| 176 | return ret; |
| 177 | } |
| 178 | |
| 179 | static int cmd_refs_optimize(int argc, const char **argv, const char *prefix, |
| 180 | struct repository *repo) |
| 181 | { |
| 182 | static char const * const refs_optimize_usage[] = { |
| 183 | REFS_OPTIMIZE_USAGE, |
| 184 | NULL |
| 185 | }; |
| 186 | |
| 187 | return pack_refs_core(argc, argv, prefix, repo, refs_optimize_usage); |
| 188 | } |
| 189 | |
| 190 | static int cmd_refs_create(int argc, const char **argv, const char *prefix, |
| 191 | struct repository *repo) |
| 192 | { |
| 193 | static char const * const refs_create_usage[] = { |
| 194 | REFS_CREATE_USAGE, |
| 195 | NULL |
| 196 | }; |
| 197 | const char *message = NULL; |
| 198 | unsigned flags = 0; |
| 199 | struct option opts[] = { |
| 200 | OPT_STRING(0, "message", &message, N_("reason"), |
| 201 | N_("reason of the update")), |
| 202 | OPT_BIT(0 ,"no-deref", &flags, |
| 203 | N_("update <refname> not the one it points to"), |
| 204 | REF_NO_DEREF), |
| 205 | OPT_BIT(0, "create-reflog", &flags, N_("create a reflog"), |
| 206 | REF_FORCE_CREATE_REFLOG), |
| 207 | OPT_END(), |
| 208 | }; |
| 209 | struct object_id newoid; |
| 210 | const char *refname; |
| 211 | int ret; |
| 212 | |
| 213 | argc = parse_options(argc, argv, prefix, opts, refs_create_usage, 0); |
| 214 | if (argc != 2) |
| 215 | usage(_("create requires reference name and an object ID")); |
| 216 | |
| 217 | if (message && !*message) |
| 218 | die(_("refusing to perform update with empty message")); |
| 219 | |
| 220 | repo_config(repo, git_default_config, NULL); |
| 221 | |
| 222 | refname = argv[0]; |
| 223 | if (repo_get_oid_with_flags(repo, argv[1], &newoid, GET_OID_SKIP_AMBIGUITY_CHECK)) |
| 224 | die(_("invalid object ID: '%s'"), argv[1]); |
| 225 | if (is_null_oid(&newoid)) |
| 226 | die(_("cannot create reference with null new object ID")); |
| 227 | |
| 228 | ret = refs_update_ref(get_main_ref_store(repo), message, refname, |
| 229 | &newoid, null_oid(repo->hash_algo), flags, |
| 230 | UPDATE_REFS_MSG_ON_ERR); |
| 231 | |
| 232 | if (ret < 0) |
| 233 | ret = 1; |
| 234 | return ret; |
| 235 | } |
| 236 | |
| 237 | static int cmd_refs_delete(int argc, const char **argv, const char *prefix, |
| 238 | struct repository *repo) |
| 239 | { |
| 240 | static char const * const refs_delete_usage[] = { |
| 241 | REFS_DELETE_USAGE, |
| 242 | NULL |
| 243 | }; |
| 244 | const char *message = NULL; |
| 245 | unsigned flags = 0; |
| 246 | struct option opts[] = { |
| 247 | OPT_STRING(0, "message", &message, N_("reason"), |
| 248 | N_("reason of the update")), |
| 249 | OPT_BIT(0 ,"no-deref", &flags, |
| 250 | N_("update <refname> not the one it points to"), |
| 251 | REF_NO_DEREF), |
| 252 | OPT_END(), |
| 253 | }; |
| 254 | struct object_id oldoid; |
| 255 | const char *refname; |
| 256 | int ret; |
| 257 | |
| 258 | argc = parse_options(argc, argv, prefix, opts, refs_delete_usage, 0); |
| 259 | if (argc < 1 || argc > 2) |
| 260 | usage(_("delete requires reference name and an optional old object ID")); |
| 261 | |
| 262 | if (message && !*message) |
| 263 | die(_("refusing to perform update with empty message")); |
| 264 | |
| 265 | repo_config(repo, git_default_config, NULL); |
| 266 | |
| 267 | refname = argv[0]; |
| 268 | if (argc == 2) { |
| 269 | if (repo_get_oid_with_flags(repo, argv[1], &oldoid, GET_OID_SKIP_AMBIGUITY_CHECK)) |
| 270 | die(_("invalid old object ID: '%s'"), argv[1]); |
| 271 | if (is_null_oid(&oldoid)) |
| 272 | die(_("cannot delete reference with null old object ID")); |
| 273 | } |
| 274 | |
| 275 | ret = refs_delete_ref(get_main_ref_store(repo), message, refname, |
| 276 | argc == 2 ? &oldoid : NULL, flags); |
| 277 | |
| 278 | if (ret < 0) |
| 279 | ret = 1; |
| 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | static int cmd_refs_update(int argc, const char **argv, const char *prefix, |
| 284 | struct repository *repo) |
| 285 | { |
| 286 | static char const * const refs_update_usage[] = { |
| 287 | REFS_UPDATE_USAGE, |
| 288 | NULL |
| 289 | }; |
| 290 | const char *message = NULL; |
| 291 | unsigned flags = 0; |
| 292 | struct option opts[] = { |
| 293 | OPT_STRING(0, "message", &message, N_("reason"), |
| 294 | N_("reason of the update")), |
| 295 | OPT_BIT(0 ,"no-deref", &flags, |
| 296 | N_("update <refname> not the one it points to"), |
| 297 | REF_NO_DEREF), |
| 298 | OPT_BIT(0, "create-reflog", &flags, N_("create a reflog"), |
| 299 | REF_FORCE_CREATE_REFLOG), |
| 300 | OPT_END(), |
| 301 | }; |
| 302 | struct object_id newoid, oldoid; |
| 303 | const char *refname; |
| 304 | int ret; |
| 305 | |
| 306 | argc = parse_options(argc, argv, prefix, opts, refs_update_usage, 0); |
| 307 | if (argc < 2 || argc > 3) |
| 308 | usage(_("update requires reference name, new value and an optional old value")); |
| 309 | |
| 310 | if (message && !*message) |
| 311 | die(_("refusing to perform update with empty message")); |
| 312 | |
| 313 | repo_config(repo, git_default_config, NULL); |
| 314 | |
| 315 | refname = argv[0]; |
| 316 | if (repo_get_oid_with_flags(repo, argv[1], &newoid, |
| 317 | GET_OID_SKIP_AMBIGUITY_CHECK)) |
| 318 | die(_("invalid new object ID: '%s'"), argv[1]); |
| 319 | if (argc == 3 && |
| 320 | repo_get_oid_with_flags(repo, argv[2], &oldoid, |
| 321 | GET_OID_SKIP_AMBIGUITY_CHECK)) |
| 322 | die(_("invalid old object ID: '%s'"), argv[2]); |
| 323 | |
| 324 | ret = refs_update_ref(get_main_ref_store(repo), message, refname, |
| 325 | &newoid, argc == 3 ? &oldoid : NULL, flags, |
| 326 | UPDATE_REFS_MSG_ON_ERR); |
| 327 | |
| 328 | if (ret < 0) |
| 329 | ret = 1; |
| 330 | return ret; |
| 331 | } |
| 332 | |
| 333 | static int cmd_refs_rename(int argc, const char **argv, const char *prefix, |
| 334 | struct repository *repo) |
| 335 | { |
| 336 | static char const * const refs_rename_usage[] = { |
| 337 | REFS_RENAME_USAGE, |
| 338 | NULL |
| 339 | }; |
| 340 | const char *message = NULL; |
| 341 | struct option opts[] = { |
| 342 | OPT_STRING(0, "message", &message, N_("reason"), |
| 343 | N_("reason of the update")), |
| 344 | OPT_END(), |
| 345 | }; |
| 346 | const char *oldref, *newref; |
| 347 | int ret; |
| 348 | |
| 349 | argc = parse_options(argc, argv, prefix, opts, refs_rename_usage, 0); |
| 350 | if (argc != 2) |
| 351 | usage(_("rename requires old and new reference name")); |
| 352 | if (message && !*message) |
| 353 | die(_("refusing to perform update with empty message")); |
| 354 | |
| 355 | repo_config(repo, git_default_config, NULL); |
| 356 | |
| 357 | oldref = argv[0]; |
| 358 | newref = argv[1]; |
| 359 | |
| 360 | if (check_refname_format(oldref, 0)) |
| 361 | die(_("invalid ref format: '%s'"), oldref); |
| 362 | if (check_refname_format(newref, 0)) |
| 363 | die(_("invalid ref format: '%s'"), newref); |
| 364 | |
| 365 | if (!refs_ref_exists(get_main_ref_store(repo), oldref)) |
| 366 | die(_("reference does not exist: '%s'"), oldref); |
| 367 | if (refs_ref_exists(get_main_ref_store(repo), newref)) |
| 368 | die(_("reference already exists: '%s'"), newref); |
| 369 | |
| 370 | ret = refs_rename_ref(get_main_ref_store(repo), oldref, newref, message); |
| 371 | |
| 372 | if (ret < 0) |
| 373 | ret = 1; |
| 374 | return ret; |
| 375 | } |
| 376 | |
| 377 | int cmd_refs(int argc, |
| 378 | const char **argv, |
| 379 | const char *prefix, |
| 380 | struct repository *repo) |
| 381 | { |
| 382 | const char * const refs_usage[] = { |
| 383 | REFS_MIGRATE_USAGE, |
| 384 | REFS_VERIFY_USAGE, |
| 385 | "git refs list " COMMON_USAGE_FOR_EACH_REF, |
| 386 | REFS_EXISTS_USAGE, |
| 387 | REFS_OPTIMIZE_USAGE, |
| 388 | REFS_CREATE_USAGE, |
| 389 | REFS_DELETE_USAGE, |
| 390 | REFS_UPDATE_USAGE, |
| 391 | REFS_RENAME_USAGE, |
| 392 | NULL, |
| 393 | }; |
| 394 | parse_opt_subcommand_fn *fn = NULL; |
| 395 | struct option opts[] = { |
| 396 | OPT_SUBCOMMAND("migrate", &fn, cmd_refs_migrate), |
| 397 | OPT_SUBCOMMAND("verify", &fn, cmd_refs_verify), |
| 398 | OPT_SUBCOMMAND("list", &fn, cmd_refs_list), |
| 399 | OPT_SUBCOMMAND("exists", &fn, cmd_refs_exists), |
| 400 | OPT_SUBCOMMAND("optimize", &fn, cmd_refs_optimize), |
| 401 | OPT_SUBCOMMAND("create", &fn, cmd_refs_create), |
| 402 | OPT_SUBCOMMAND("delete", &fn, cmd_refs_delete), |
| 403 | OPT_SUBCOMMAND("update", &fn, cmd_refs_update), |
| 404 | OPT_SUBCOMMAND("rename", &fn, cmd_refs_rename), |
| 405 | OPT_END(), |
| 406 | }; |
| 407 | |
| 408 | argc = parse_options(argc, argv, prefix, opts, refs_usage, 0); |
| 409 | return fn(argc, argv, prefix, repo); |
| 410 | } |