| 1 | #define USE_THE_REPOSITORY_VARIABLE |
| 2 | |
| 3 | #include "test-tool.h" |
| 4 | #include "hex.h" |
| 5 | #include "refs.h" |
| 6 | #include "setup.h" |
| 7 | #include "worktree.h" |
| 8 | #include "odb.h" |
| 9 | #include "path.h" |
| 10 | #include "repository.h" |
| 11 | #include "strbuf.h" |
| 12 | #include "revision.h" |
| 13 | |
| 14 | struct flag_definition { |
| 15 | const char *name; |
| 16 | uint64_t mask; |
| 17 | }; |
| 18 | |
| 19 | #define FLAG_DEF(x) \ |
| 20 | { \ |
| 21 | #x, (x) \ |
| 22 | } |
| 23 | |
| 24 | static unsigned int parse_flags(const char *str, struct flag_definition *defs) |
| 25 | { |
| 26 | struct string_list masks = STRING_LIST_INIT_DUP; |
| 27 | unsigned int result = 0; |
| 28 | |
| 29 | if (!strcmp(str, "0")) |
| 30 | return 0; |
| 31 | |
| 32 | string_list_split(&masks, str, ",", 64); |
| 33 | for (size_t i = 0; i < masks.nr; i++) { |
| 34 | const char *name = masks.items[i].string; |
| 35 | struct flag_definition *def = defs; |
| 36 | int found = 0; |
| 37 | while (def->name) { |
| 38 | if (!strcmp(def->name, name)) { |
| 39 | result |= def->mask; |
| 40 | found = 1; |
| 41 | break; |
| 42 | } |
| 43 | def++; |
| 44 | } |
| 45 | if (!found) |
| 46 | die("unknown flag \"%s\"", name); |
| 47 | } |
| 48 | |
| 49 | string_list_clear(&masks, 0); |
| 50 | return result; |
| 51 | } |
| 52 | |
| 53 | static struct flag_definition empty_flags[] = { { NULL, 0 } }; |
| 54 | |
| 55 | static const char *notnull(const char *arg, const char *name) |
| 56 | { |
| 57 | if (!arg) |
| 58 | die("%s required", name); |
| 59 | return arg; |
| 60 | } |
| 61 | |
| 62 | static unsigned int arg_flags(const char *arg, const char *name, |
| 63 | struct flag_definition *defs) |
| 64 | { |
| 65 | return parse_flags(notnull(arg, name), defs); |
| 66 | } |
| 67 | |
| 68 | static const char **get_store(const char **argv, struct ref_store **refs) |
| 69 | { |
| 70 | const char *gitdir; |
| 71 | |
| 72 | if (!argv[0]) { |
| 73 | die("ref store required"); |
| 74 | } else if (!strcmp(argv[0], "main")) { |
| 75 | *refs = get_main_ref_store(the_repository); |
| 76 | } else if (skip_prefix(argv[0], "submodule:", &gitdir)) { |
| 77 | struct strbuf sb = STRBUF_INIT; |
| 78 | |
| 79 | if (!repo_submodule_path_append(the_repository, |
| 80 | &sb, gitdir, "objects/")) |
| 81 | die("computing submodule path failed"); |
| 82 | odb_add_to_alternates_memory(the_repository->objects, sb.buf); |
| 83 | strbuf_release(&sb); |
| 84 | |
| 85 | *refs = repo_get_submodule_ref_store(the_repository, gitdir); |
| 86 | } else if (skip_prefix(argv[0], "worktree:", &gitdir)) { |
| 87 | struct worktree **p, **worktrees = get_worktrees(); |
| 88 | |
| 89 | for (p = worktrees; *p; p++) { |
| 90 | struct worktree *wt = *p; |
| 91 | |
| 92 | if (!wt->id) { |
| 93 | /* special case for main worktree */ |
| 94 | if (!strcmp(gitdir, "main")) |
| 95 | break; |
| 96 | } else if (!strcmp(gitdir, wt->id)) |
| 97 | break; |
| 98 | } |
| 99 | if (!*p) |
| 100 | die("no such worktree: %s", gitdir); |
| 101 | |
| 102 | *refs = get_worktree_ref_store(*p); |
| 103 | free_worktrees(worktrees); |
| 104 | } else |
| 105 | die("unknown backend %s", argv[0]); |
| 106 | |
| 107 | if (!*refs) |
| 108 | die("no ref store"); |
| 109 | |
| 110 | /* consume store-specific optional arguments if needed */ |
| 111 | |
| 112 | return argv + 1; |
| 113 | } |
| 114 | |
| 115 | static int cmd_create_symref(struct ref_store *refs, const char **argv) |
| 116 | { |
| 117 | const char *refname = notnull(*argv++, "refname"); |
| 118 | const char *target = notnull(*argv++, "target"); |
| 119 | const char *logmsg = *argv++; |
| 120 | |
| 121 | return refs_update_symref(refs, refname, target, logmsg); |
| 122 | } |
| 123 | |
| 124 | static struct flag_definition transaction_flags[] = { |
| 125 | FLAG_DEF(REF_NO_DEREF), |
| 126 | FLAG_DEF(REF_FORCE_CREATE_REFLOG), |
| 127 | FLAG_DEF(REF_SKIP_OID_VERIFICATION), |
| 128 | FLAG_DEF(REF_SKIP_REFNAME_VERIFICATION), |
| 129 | FLAG_DEF(REF_SKIP_CREATE_REFLOG), |
| 130 | { NULL, 0 } |
| 131 | }; |
| 132 | |
| 133 | static int cmd_delete_refs(struct ref_store *refs, const char **argv) |
| 134 | { |
| 135 | unsigned int flags = arg_flags(*argv++, "flags", transaction_flags); |
| 136 | const char *msg = *argv++; |
| 137 | struct string_list refnames = STRING_LIST_INIT_NODUP; |
| 138 | int result; |
| 139 | |
| 140 | while (*argv) |
| 141 | string_list_append(&refnames, *argv++); |
| 142 | |
| 143 | result = refs_delete_refs(refs, msg, &refnames, flags); |
| 144 | string_list_clear(&refnames, 0); |
| 145 | return result; |
| 146 | } |
| 147 | |
| 148 | static int cmd_rename_ref(struct ref_store *refs, const char **argv) |
| 149 | { |
| 150 | const char *oldref = notnull(*argv++, "oldref"); |
| 151 | const char *newref = notnull(*argv++, "newref"); |
| 152 | const char *logmsg = *argv++; |
| 153 | |
| 154 | return refs_rename_ref(refs, oldref, newref, logmsg); |
| 155 | } |
| 156 | |
| 157 | static int each_ref(const struct reference *ref, void *cb_data UNUSED) |
| 158 | { |
| 159 | printf("%s %s 0x%x\n", oid_to_hex(ref->oid), ref->name, ref->flags); |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | static int cmd_for_each_ref(struct ref_store *refs, const char **argv) |
| 164 | { |
| 165 | const char *prefix = notnull(*argv++, "prefix"); |
| 166 | struct refs_for_each_ref_options opts = { |
| 167 | .prefix = prefix, |
| 168 | .trim_prefix = strlen(prefix), |
| 169 | }; |
| 170 | return refs_for_each_ref_ext(refs, each_ref, NULL, &opts); |
| 171 | } |
| 172 | |
| 173 | static int cmd_for_each_ref__exclude(struct ref_store *refs, const char **argv) |
| 174 | { |
| 175 | const char *prefix = notnull(*argv++, "prefix"); |
| 176 | struct refs_for_each_ref_options opts = { |
| 177 | .prefix = prefix, |
| 178 | .exclude_patterns = argv, |
| 179 | }; |
| 180 | |
| 181 | return refs_for_each_ref_ext(refs, each_ref, NULL, &opts); |
| 182 | } |
| 183 | |
| 184 | static int cmd_resolve_ref(struct ref_store *refs, const char **argv) |
| 185 | { |
| 186 | struct object_id oid = *null_oid(the_hash_algo); |
| 187 | const char *refname = notnull(*argv++, "refname"); |
| 188 | int resolve_flags = arg_flags(*argv++, "resolve-flags", empty_flags); |
| 189 | int flags; |
| 190 | const char *ref; |
| 191 | |
| 192 | ref = refs_resolve_ref_unsafe(refs, refname, resolve_flags, |
| 193 | &oid, &flags); |
| 194 | printf("%s %s 0x%x\n", oid_to_hex(&oid), ref ? ref : "(null)", flags); |
| 195 | return ref ? 0 : 1; |
| 196 | } |
| 197 | |
| 198 | static int cmd_verify_ref(struct ref_store *refs, const char **argv) |
| 199 | { |
| 200 | const char *refname = notnull(*argv++, "refname"); |
| 201 | struct strbuf err = STRBUF_INIT; |
| 202 | int ret; |
| 203 | |
| 204 | ret = refs_verify_refname_available(refs, refname, NULL, NULL, 0, &err); |
| 205 | if (err.len) |
| 206 | puts(err.buf); |
| 207 | return ret; |
| 208 | } |
| 209 | |
| 210 | static int each_reflog(const char *refname, void *cb_data UNUSED) |
| 211 | { |
| 212 | printf("%s\n", refname); |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | static int cmd_for_each_reflog(struct ref_store *refs, |
| 217 | const char **argv UNUSED) |
| 218 | { |
| 219 | return refs_for_each_reflog(refs, each_reflog, NULL); |
| 220 | } |
| 221 | |
| 222 | static int each_reflog_ent(const char *refname UNUSED, |
| 223 | struct object_id *old_oid, struct object_id *new_oid, |
| 224 | const char *committer, timestamp_t timestamp, |
| 225 | int tz, const char *msg, void *cb_data UNUSED) |
| 226 | { |
| 227 | printf("%s %s %s %" PRItime " %+05d%s%s", oid_to_hex(old_oid), |
| 228 | oid_to_hex(new_oid), committer, timestamp, tz, |
| 229 | *msg == '\n' ? "" : "\t", msg); |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | static int cmd_for_each_reflog_ent(struct ref_store *refs, const char **argv) |
| 234 | { |
| 235 | const char *refname = notnull(*argv++, "refname"); |
| 236 | |
| 237 | return refs_for_each_reflog_ent(refs, refname, each_reflog_ent, refs); |
| 238 | } |
| 239 | |
| 240 | static int cmd_for_each_reflog_ent_reverse(struct ref_store *refs, const char **argv) |
| 241 | { |
| 242 | const char *refname = notnull(*argv++, "refname"); |
| 243 | |
| 244 | return refs_for_each_reflog_ent_reverse(refs, refname, each_reflog_ent, refs); |
| 245 | } |
| 246 | |
| 247 | static int cmd_reflog_exists(struct ref_store *refs, const char **argv) |
| 248 | { |
| 249 | const char *refname = notnull(*argv++, "refname"); |
| 250 | |
| 251 | return !refs_reflog_exists(refs, refname); |
| 252 | } |
| 253 | |
| 254 | static int cmd_create_reflog(struct ref_store *refs, const char **argv) |
| 255 | { |
| 256 | const char *refname = notnull(*argv++, "refname"); |
| 257 | struct strbuf err = STRBUF_INIT; |
| 258 | int ret; |
| 259 | |
| 260 | ret = refs_create_reflog(refs, refname, &err); |
| 261 | if (err.len) |
| 262 | puts(err.buf); |
| 263 | return ret; |
| 264 | } |
| 265 | |
| 266 | static int cmd_delete_reflog(struct ref_store *refs, const char **argv) |
| 267 | { |
| 268 | const char *refname = notnull(*argv++, "refname"); |
| 269 | |
| 270 | return refs_delete_reflog(refs, refname); |
| 271 | } |
| 272 | |
| 273 | static int cmd_delete_ref(struct ref_store *refs, const char **argv) |
| 274 | { |
| 275 | const char *msg = notnull(*argv++, "msg"); |
| 276 | const char *refname = notnull(*argv++, "refname"); |
| 277 | const char *sha1_buf = notnull(*argv++, "old-sha1"); |
| 278 | unsigned int flags = arg_flags(*argv++, "flags", transaction_flags); |
| 279 | struct object_id old_oid; |
| 280 | |
| 281 | if (get_oid_hex(sha1_buf, &old_oid)) |
| 282 | die("cannot parse %s as %s", sha1_buf, the_hash_algo->name); |
| 283 | |
| 284 | return refs_delete_ref(refs, msg, refname, &old_oid, flags); |
| 285 | } |
| 286 | |
| 287 | static int cmd_update_ref(struct ref_store *refs, const char **argv) |
| 288 | { |
| 289 | const char *msg = notnull(*argv++, "msg"); |
| 290 | const char *refname = notnull(*argv++, "refname"); |
| 291 | const char *new_sha1_buf = notnull(*argv++, "new-sha1"); |
| 292 | const char *old_sha1_buf = notnull(*argv++, "old-sha1"); |
| 293 | unsigned int flags = arg_flags(*argv++, "flags", transaction_flags); |
| 294 | struct object_id old_oid, *old_oid_ptr = NULL; |
| 295 | struct object_id new_oid; |
| 296 | |
| 297 | if (*old_sha1_buf) { |
| 298 | if (get_oid_hex(old_sha1_buf, &old_oid)) |
| 299 | die("cannot parse %s as %s", old_sha1_buf, the_hash_algo->name); |
| 300 | old_oid_ptr = &old_oid; |
| 301 | } |
| 302 | if (get_oid_hex(new_sha1_buf, &new_oid)) |
| 303 | die("cannot parse %s as %s", new_sha1_buf, the_hash_algo->name); |
| 304 | |
| 305 | return refs_update_ref(refs, msg, refname, |
| 306 | &new_oid, old_oid_ptr, |
| 307 | flags, UPDATE_REFS_DIE_ON_ERR); |
| 308 | } |
| 309 | |
| 310 | struct command { |
| 311 | const char *name; |
| 312 | int (*func)(struct ref_store *refs, const char **argv); |
| 313 | }; |
| 314 | |
| 315 | static struct command commands[] = { |
| 316 | { "create-symref", cmd_create_symref }, |
| 317 | { "delete-refs", cmd_delete_refs }, |
| 318 | { "rename-ref", cmd_rename_ref }, |
| 319 | { "for-each-ref", cmd_for_each_ref }, |
| 320 | { "for-each-ref--exclude", cmd_for_each_ref__exclude }, |
| 321 | { "resolve-ref", cmd_resolve_ref }, |
| 322 | { "verify-ref", cmd_verify_ref }, |
| 323 | { "for-each-reflog", cmd_for_each_reflog }, |
| 324 | { "for-each-reflog-ent", cmd_for_each_reflog_ent }, |
| 325 | { "for-each-reflog-ent-reverse", cmd_for_each_reflog_ent_reverse }, |
| 326 | { "reflog-exists", cmd_reflog_exists }, |
| 327 | { "create-reflog", cmd_create_reflog }, |
| 328 | { "delete-reflog", cmd_delete_reflog }, |
| 329 | /* |
| 330 | * backend transaction functions can't be tested separately |
| 331 | */ |
| 332 | { "delete-ref", cmd_delete_ref }, |
| 333 | { "update-ref", cmd_update_ref }, |
| 334 | { NULL, NULL } |
| 335 | }; |
| 336 | |
| 337 | int cmd__ref_store(int argc UNUSED, const char **argv) |
| 338 | { |
| 339 | struct ref_store *refs; |
| 340 | const char *func; |
| 341 | struct command *cmd; |
| 342 | |
| 343 | setup_git_directory(the_repository); |
| 344 | |
| 345 | argv = get_store(argv + 1, &refs); |
| 346 | |
| 347 | func = *argv++; |
| 348 | if (!func) |
| 349 | die("ref function required"); |
| 350 | for (cmd = commands; cmd->name; cmd++) { |
| 351 | if (!strcmp(func, cmd->name)) |
| 352 | return cmd->func(refs, argv); |
| 353 | } |
| 354 | die("unknown function %s", func); |
| 355 | return 0; |
| 356 | } |