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-format=<format> [--no-reflog] [--dry-run]")
15
#define REFS_VERIFY_USAGE \
16
N_("git refs verify [--strict] [--verbose]")
17
18
+#define REFS_EXISTS_USAGE \
19
+ N_("git refs exists <ref>")
20
+
21
static int cmd_refs_migrate(int argc, const char **argv, const char *prefix,
22
struct repository *repo UNUSED)
23
{
117
return for_each_ref_core(argc, argv, prefix, repo, refs_list_usage);
118
}
119
120
+static int cmd_refs_exists(int argc, const char **argv, const char *prefix,
121
+ struct repository *repo UNUSED)
122
+{
123
+ struct strbuf unused_referent = STRBUF_INIT;
124
+ struct object_id unused_oid;
125
+ unsigned int unused_type;
126
+ int failure_errno = 0;
127
+ const char *ref;
128
+ int ret = 0;
129
+ const char * const exists_usage[] = {
130
+ REFS_EXISTS_USAGE,
131
+ NULL,
132
+ };
133
+ struct option options[] = {
134
+ OPT_END(),
135
+ };
136
+
137
+ argc = parse_options(argc, argv, prefix, options, exists_usage, 0);
138
+ if (argc != 1)
139
+ die(_("'git refs exists' requires a reference"));
140
+
141
+ ref = *argv++;
142
+ if (refs_read_raw_ref(get_main_ref_store(the_repository), ref,
143
+ &unused_oid, &unused_referent, &unused_type,
144
+ &failure_errno)) {
145
+ if (failure_errno == ENOENT || failure_errno == EISDIR) {
146
+ error(_("reference does not exist"));
147
+ ret = 2;
148
+ } else {
149
+ errno = failure_errno;
150
+ error_errno(_("failed to look up reference"));
151
+ ret = 1;
152
+ }
153
+
154
+ goto out;
155
+ }
156
+
157
+out:
158
+ strbuf_release(&unused_referent);
159
+ return ret;
160
+}
161
+
162
int cmd_refs(int argc,
163
const char **argv,
164
const char *prefix,
168
REFS_MIGRATE_USAGE,
169
REFS_VERIFY_USAGE,
170
"git refs list " COMMON_USAGE_FOR_EACH_REF,
171
+ REFS_EXISTS_USAGE,
172
NULL,
173
};
174
parse_opt_subcommand_fn *fn = NULL;
176
OPT_SUBCOMMAND("migrate", &fn, cmd_refs_migrate),
177
OPT_SUBCOMMAND("verify", &fn, cmd_refs_verify),
178
OPT_SUBCOMMAND("list", &fn, cmd_refs_list),
179
+ OPT_SUBCOMMAND("exists", &fn, cmd_refs_exists),
180
OPT_END(),
181
};
182