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