From: Junio C Hamano Date: Thu, 2 Oct 2025 19:26:12 +0000 (-0700) Subject: Merge branch 'ms/refs-optimize' X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=db0babf9b2f807e6913b3591d04cb752b8219e9d;p=thirdparty%2Fgit.git Merge branch 'ms/refs-optimize' "git refs optimize" is added for not very well explained reason despite it does the same thing as "git pack-refs"... * ms/refs-optimize: t: add test for git refs optimize subcommand t0601: refactor tests to be shareable builtin/refs: add optimize subcommand doc: pack-refs: factor out common options builtin/pack-refs: factor out core logic into a shared library builtin/pack-refs: convert to use the generic refs_optimize() API reftable-backend: implement 'optimize' action files-backend: implement 'optimize' action refs: add a generic 'optimize' API --- db0babf9b2f807e6913b3591d04cb752b8219e9d diff --cc Documentation/git-refs.adoc index bfa9b3ea2d,e233f21eeb..fa33680cc7 --- a/Documentation/git-refs.adoc +++ b/Documentation/git-refs.adoc @@@ -18,7 -18,7 +18,8 @@@ git refs list [--count=] [--shel [--contains[=]] [--no-contains[=]] [(--exclude=)...] [--start-after=] [ --stdin | (...)] +git refs exists + git refs optimize [--all] [--no-prune] [--auto] [--include ] [--exclude ] DESCRIPTION ----------- @@@ -39,12 -39,11 +40,17 @@@ list: formatting, and sorting. This subcommand is an alias for linkgit:git-for-each-ref[1] and offers identical functionality. +exists:: + Check whether the given reference exists. Returns an exit code of 0 if + it does, 2 if it is missing, and 1 in case looking up the reference + failed with an error other than the reference being missing. This does + not verify whether the reference resolves to an actual object. + + optimize:: + Optimizes references to improve repository performance and reduce disk + usage. This subcommand is an alias for linkgit:git-pack-refs[1] and + offers identical functionality. + OPTIONS ------- diff --cc builtin/refs.c index 91548783b7,785f476e4b..3064f888b2 --- a/builtin/refs.c +++ b/builtin/refs.c @@@ -15,9 -15,9 +16,12 @@@ #define REFS_VERIFY_USAGE \ N_("git refs verify [--strict] [--verbose]") +#define REFS_EXISTS_USAGE \ + N_("git refs exists ") + + #define REFS_OPTIMIZE_USAGE \ + N_("git refs optimize " PACK_REFS_OPTS) + static int cmd_refs_migrate(int argc, const char **argv, const char *prefix, struct repository *repo UNUSED) { @@@ -117,48 -117,17 +121,59 @@@ static int cmd_refs_list(int argc, cons return for_each_ref_core(argc, argv, prefix, repo, refs_list_usage); } +static int cmd_refs_exists(int argc, const char **argv, const char *prefix, + struct repository *repo UNUSED) +{ + struct strbuf unused_referent = STRBUF_INIT; + struct object_id unused_oid; + unsigned int unused_type; + int failure_errno = 0; + const char *ref; + int ret = 0; + const char * const exists_usage[] = { + REFS_EXISTS_USAGE, + NULL, + }; + struct option options[] = { + OPT_END(), + }; + + argc = parse_options(argc, argv, prefix, options, exists_usage, 0); + if (argc != 1) + die(_("'git refs exists' requires a reference")); + + ref = *argv++; + if (refs_read_raw_ref(get_main_ref_store(the_repository), ref, + &unused_oid, &unused_referent, &unused_type, + &failure_errno)) { + if (failure_errno == ENOENT || failure_errno == EISDIR) { + error(_("reference does not exist")); + ret = 2; + } else { + errno = failure_errno; + error_errno(_("failed to look up reference")); + ret = 1; + } + + goto out; + } + +out: + strbuf_release(&unused_referent); + return ret; +} + + static int cmd_refs_optimize(int argc, const char **argv, const char *prefix, + struct repository *repo) + { + static char const * const refs_optimize_usage[] = { + REFS_OPTIMIZE_USAGE, + NULL + }; + + return pack_refs_core(argc, argv, prefix, repo, refs_optimize_usage); + } + int cmd_refs(int argc, const char **argv, const char *prefix, @@@ -168,7 -137,7 +183,8 @@@ REFS_MIGRATE_USAGE, REFS_VERIFY_USAGE, "git refs list " COMMON_USAGE_FOR_EACH_REF, + REFS_EXISTS_USAGE, + REFS_OPTIMIZE_USAGE, NULL, }; parse_opt_subcommand_fn *fn = NULL; @@@ -176,7 -145,7 +192,8 @@@ OPT_SUBCOMMAND("migrate", &fn, cmd_refs_migrate), OPT_SUBCOMMAND("verify", &fn, cmd_refs_verify), OPT_SUBCOMMAND("list", &fn, cmd_refs_list), + OPT_SUBCOMMAND("exists", &fn, cmd_refs_exists), + OPT_SUBCOMMAND("optimize", &fn, cmd_refs_optimize), OPT_END(), }; diff --cc t/meson.build index 7974795fe4,92327aabdf..d87166c0db --- a/t/meson.build +++ b/t/meson.build @@@ -212,7 -211,7 +212,8 @@@ integration_tests = 't1451-fsck-buffer.sh', 't1460-refs-migrate.sh', 't1461-refs-list.sh', + 't1462-refs-exists.sh', + 't1463-refs-optimize.sh', 't1500-rev-parse.sh', 't1501-work-tree.sh', 't1502-rev-parse-parseopt.sh',