|
| 1 | +#include "builtin.h" |
| 2 | +#include "parse-options.h" |
| 3 | +#include "refs.h" |
| 4 | +#include "repository.h" |
| 5 | +#include "strbuf.h" |
| 6 | + |
| 7 | +#define REFS_MIGRATE_USAGE \ |
| 8 | + N_("git refs migrate --ref-format=<format> [--dry-run]") |
| 9 | + |
| 10 | +static int cmd_refs_migrate(int argc, const char **argv, const char *prefix) |
| 11 | +{ |
| 12 | + const char * const migrate_usage[] = { |
| 13 | + REFS_MIGRATE_USAGE, |
| 14 | + NULL, |
| 15 | + }; |
| 16 | + const char *format_str = NULL; |
| 17 | + enum ref_storage_format format; |
| 18 | + unsigned int flags = 0; |
| 19 | + struct option options[] = { |
| 20 | + OPT_STRING_F(0, "ref-format", &format_str, N_("format"), |
| 21 | + N_("specify the reference format to convert to"), |
| 22 | + PARSE_OPT_NONEG), |
| 23 | + OPT_BIT(0, "dry-run", &flags, |
| 24 | + N_("perform a non-destructive dry-run"), |
| 25 | + REPO_MIGRATE_REF_STORAGE_FORMAT_DRYRUN), |
| 26 | + OPT_END(), |
| 27 | + }; |
| 28 | + struct strbuf errbuf = STRBUF_INIT; |
| 29 | + int err; |
| 30 | + |
| 31 | + argc = parse_options(argc, argv, prefix, options, migrate_usage, 0); |
| 32 | + if (argc) |
| 33 | + usage(_("too many arguments")); |
| 34 | + if (!format_str) |
| 35 | + usage(_("missing --ref-format=<format>")); |
| 36 | + |
| 37 | + format = ref_storage_format_by_name(format_str); |
| 38 | + if (format == REF_STORAGE_FORMAT_UNKNOWN) { |
| 39 | + err = error(_("unknown ref storage format '%s'"), format_str); |
| 40 | + goto out; |
| 41 | + } |
| 42 | + |
| 43 | + if (the_repository->ref_storage_format == format) { |
| 44 | + err = error(_("repository already uses '%s' format"), |
| 45 | + ref_storage_format_to_name(format)); |
| 46 | + goto out; |
| 47 | + } |
| 48 | + |
| 49 | + if (repo_migrate_ref_storage_format(the_repository, format, flags, &errbuf) < 0) { |
| 50 | + err = error("%s", errbuf.buf); |
| 51 | + goto out; |
| 52 | + } |
| 53 | + |
| 54 | + err = 0; |
| 55 | + |
| 56 | +out: |
| 57 | + strbuf_release(&errbuf); |
| 58 | + return err; |
| 59 | +} |
| 60 | + |
| 61 | +int cmd_refs(int argc, const char **argv, const char *prefix) |
| 62 | +{ |
| 63 | + const char * const refs_usage[] = { |
| 64 | + REFS_MIGRATE_USAGE, |
| 65 | + NULL, |
| 66 | + }; |
| 67 | + parse_opt_subcommand_fn *fn = NULL; |
| 68 | + struct option opts[] = { |
| 69 | + OPT_SUBCOMMAND("migrate", &fn, cmd_refs_migrate), |
| 70 | + OPT_END(), |
| 71 | + }; |
| 72 | + |
| 73 | + argc = parse_options(argc, argv, prefix, opts, refs_usage, 0); |
| 74 | + return fn(argc, argv, prefix); |
| 75 | +} |
0 commit comments