blob: db4082566634cbf5bdbb081d0807b0a82be7645e [file] [log] [blame]
Stephen Boydc2e86ad2011-03-22 00:51:05 -07001#include "builtin.h"
Elijah Newren8e712ef2019-04-25 07:58:54 -07002#include "config.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00003#include "gettext.h"
Pierre Habouzitb2565ae2007-10-15 23:06:02 +02004#include "parse-options.h"
Michael Haggerty32d462c2013-04-22 21:52:32 +02005#include "refs.h"
Stefan Beller23a3f0c2018-04-11 17:21:09 -07006#include "repository.h"
John Cai826ae792023-05-12 21:34:41 +00007#include "revision.h"
Linus Torvalds99b5a792007-05-26 09:25:31 -07008
Pierre Habouzitb2565ae2007-10-15 23:06:02 +02009static char const * const pack_refs_usage[] = {
Patrick Steinhardt6dcffc62024-03-25 11:03:20 +010010 N_("git pack-refs [--all] [--no-prune] [--auto] [--include <pattern>] [--exclude <pattern>]"),
Pierre Habouzitb2565ae2007-10-15 23:06:02 +020011 NULL
12};
13
Linus Torvalds99b5a792007-05-26 09:25:31 -070014int cmd_pack_refs(int argc, const char **argv, const char *prefix)
15{
Patrick Steinhardta75dc712024-03-25 11:03:15 +010016 struct ref_exclusions excludes = REF_EXCLUSIONS_INIT;
17 struct string_list included_refs = STRING_LIST_INIT_NODUP;
18 struct pack_refs_opts pack_refs_opts = {
19 .exclusions = &excludes,
20 .includes = &included_refs,
21 .flags = PACK_REFS_PRUNE,
22 };
23 struct string_list option_excluded_refs = STRING_LIST_INIT_NODUP;
John Cai826ae792023-05-12 21:34:41 +000024 struct string_list_item *item;
Patrick Steinhardt35aeabd2024-03-25 11:03:07 +010025 int pack_all = 0;
Patrick Steinhardta75dc712024-03-25 11:03:15 +010026 int ret;
John Cai826ae792023-05-12 21:34:41 +000027
Pierre Habouzitb2565ae2007-10-15 23:06:02 +020028 struct option opts[] = {
Patrick Steinhardt35aeabd2024-03-25 11:03:07 +010029 OPT_BOOL(0, "all", &pack_all, N_("pack everything")),
John Cai826ae792023-05-12 21:34:41 +000030 OPT_BIT(0, "prune", &pack_refs_opts.flags, N_("prune loose refs (default)"), PACK_REFS_PRUNE),
Patrick Steinhardt6dcffc62024-03-25 11:03:20 +010031 OPT_BIT(0, "auto", &pack_refs_opts.flags, N_("auto-pack refs as needed"), PACK_REFS_AUTO),
John Cai4fe42f32023-05-12 21:34:42 +000032 OPT_STRING_LIST(0, "include", pack_refs_opts.includes, N_("pattern"),
33 N_("references to include")),
John Cai826ae792023-05-12 21:34:41 +000034 OPT_STRING_LIST(0, "exclude", &option_excluded_refs, N_("pattern"),
35 N_("references to exclude")),
Pierre Habouzitb2565ae2007-10-15 23:06:02 +020036 OPT_END(),
37 };
Elijah Newren8e712ef2019-04-25 07:58:54 -070038 git_config(git_default_config, NULL);
Stephen Boyd37782922009-05-23 11:53:12 -070039 if (parse_options(argc, argv, prefix, opts, pack_refs_usage, 0))
Pierre Habouzitb2565ae2007-10-15 23:06:02 +020040 usage_with_options(pack_refs_usage, opts);
John Cai826ae792023-05-12 21:34:41 +000041
42 for_each_string_list_item(item, &option_excluded_refs)
43 add_ref_exclusion(pack_refs_opts.exclusions, item->string);
44
Patrick Steinhardt35aeabd2024-03-25 11:03:07 +010045 if (pack_all)
John Cai4fe42f32023-05-12 21:34:42 +000046 string_list_append(pack_refs_opts.includes, "*");
47
48 if (!pack_refs_opts.includes->nr)
49 string_list_append(pack_refs_opts.includes, "refs/tags/*");
50
Patrick Steinhardta75dc712024-03-25 11:03:15 +010051 ret = refs_pack_refs(get_main_ref_store(the_repository), &pack_refs_opts);
52
53 clear_ref_exclusions(&excludes);
54 string_list_clear(&included_refs, 0);
55 string_list_clear(&option_excluded_refs, 0);
56 return ret;
Linus Torvalds99b5a792007-05-26 09:25:31 -070057}