Coverage Report

Created: 2024-09-08 06:23

/src/git/builtin/show-ref.c
Line
Count
Source (jump to first uncovered line)
1
#include "builtin.h"
2
#include "config.h"
3
#include "gettext.h"
4
#include "hex.h"
5
#include "refs/refs-internal.h"
6
#include "object-name.h"
7
#include "object-store-ll.h"
8
#include "object.h"
9
#include "string-list.h"
10
#include "parse-options.h"
11
12
static const char * const show_ref_usage[] = {
13
  N_("git show-ref [--head] [-d | --dereference]\n"
14
     "             [-s | --hash[=<n>]] [--abbrev[=<n>]] [--branches] [--tags]\n"
15
     "             [--] [<pattern>...]"),
16
  N_("git show-ref --verify [-q | --quiet] [-d | --dereference]\n"
17
     "             [-s | --hash[=<n>]] [--abbrev[=<n>]]\n"
18
     "             [--] [<ref>...]"),
19
  N_("git show-ref --exclude-existing[=<pattern>]"),
20
  N_("git show-ref --exists <ref>"),
21
  NULL
22
};
23
24
struct show_one_options {
25
  int quiet;
26
  int hash_only;
27
  int abbrev;
28
  int deref_tags;
29
};
30
31
static void show_one(const struct show_one_options *opts,
32
         const char *refname, const struct object_id *oid)
33
0
{
34
0
  const char *hex;
35
0
  struct object_id peeled;
36
37
0
  if (!repo_has_object_file(the_repository, oid))
38
0
    die("git show-ref: bad ref %s (%s)", refname,
39
0
        oid_to_hex(oid));
40
41
0
  if (opts->quiet)
42
0
    return;
43
44
0
  hex = repo_find_unique_abbrev(the_repository, oid, opts->abbrev);
45
0
  if (opts->hash_only)
46
0
    printf("%s\n", hex);
47
0
  else
48
0
    printf("%s %s\n", hex, refname);
49
50
0
  if (!opts->deref_tags)
51
0
    return;
52
53
0
  if (!peel_iterated_oid(the_repository, oid, &peeled)) {
54
0
    hex = repo_find_unique_abbrev(the_repository, &peeled, opts->abbrev);
55
0
    printf("%s %s^{}\n", hex, refname);
56
0
  }
57
0
}
58
59
struct show_ref_data {
60
  const struct show_one_options *show_one_opts;
61
  const char **patterns;
62
  int found_match;
63
  int show_head;
64
};
65
66
static int show_ref(const char *refname, const char *referent UNUSED, const struct object_id *oid,
67
        int flag UNUSED, void *cbdata)
68
0
{
69
0
  struct show_ref_data *data = cbdata;
70
71
0
  if (data->show_head && !strcmp(refname, "HEAD"))
72
0
    goto match;
73
74
0
  if (data->patterns) {
75
0
    int reflen = strlen(refname);
76
0
    const char **p = data->patterns, *m;
77
0
    while ((m = *p++) != NULL) {
78
0
      int len = strlen(m);
79
0
      if (len > reflen)
80
0
        continue;
81
0
      if (memcmp(m, refname + reflen - len, len))
82
0
        continue;
83
0
      if (len == reflen)
84
0
        goto match;
85
0
      if (refname[reflen - len - 1] == '/')
86
0
        goto match;
87
0
    }
88
0
    return 0;
89
0
  }
90
91
0
match:
92
0
  data->found_match++;
93
94
0
  show_one(data->show_one_opts, refname, oid);
95
96
0
  return 0;
97
0
}
98
99
static int add_existing(const char *refname,
100
      const char *referent UNUSED,
101
      const struct object_id *oid UNUSED,
102
      int flag UNUSED, void *cbdata)
103
0
{
104
0
  struct string_list *list = (struct string_list *)cbdata;
105
0
  string_list_insert(list, refname);
106
0
  return 0;
107
0
}
108
109
struct exclude_existing_options {
110
  /*
111
   * We need an explicit `enabled` field because it is perfectly valid
112
   * for `pattern` to be `NULL` even if `--exclude-existing` was given.
113
   */
114
  int enabled;
115
  const char *pattern;
116
};
117
118
/*
119
 * read "^(?:<anything>\s)?<refname>(?:\^\{\})?$" from the standard input,
120
 * and
121
 * (1) strip "^{}" at the end of line if any;
122
 * (2) ignore if match is provided and does not head-match refname;
123
 * (3) warn if refname is not a well-formed refname and skip;
124
 * (4) ignore if refname is a ref that exists in the local repository;
125
 * (5) otherwise output the line.
126
 */
127
static int cmd_show_ref__exclude_existing(const struct exclude_existing_options *opts)
128
0
{
129
0
  struct string_list existing_refs = STRING_LIST_INIT_DUP;
130
0
  char buf[1024];
131
0
  int patternlen = opts->pattern ? strlen(opts->pattern) : 0;
132
133
0
  refs_for_each_ref(get_main_ref_store(the_repository), add_existing,
134
0
        &existing_refs);
135
0
  while (fgets(buf, sizeof(buf), stdin)) {
136
0
    char *ref;
137
0
    int len = strlen(buf);
138
139
0
    if (len > 0 && buf[len - 1] == '\n')
140
0
      buf[--len] = '\0';
141
0
    if (3 <= len && !strcmp(buf + len - 3, "^{}")) {
142
0
      len -= 3;
143
0
      buf[len] = '\0';
144
0
    }
145
0
    for (ref = buf + len; buf < ref; ref--)
146
0
      if (isspace(ref[-1]))
147
0
        break;
148
0
    if (opts->pattern) {
149
0
      int reflen = buf + len - ref;
150
0
      if (reflen < patternlen)
151
0
        continue;
152
0
      if (strncmp(ref, opts->pattern, patternlen))
153
0
        continue;
154
0
    }
155
0
    if (check_refname_format(ref, 0)) {
156
0
      warning("ref '%s' ignored", ref);
157
0
      continue;
158
0
    }
159
0
    if (!string_list_has_string(&existing_refs, ref)) {
160
0
      printf("%s\n", buf);
161
0
    }
162
0
  }
163
164
0
  string_list_clear(&existing_refs, 0);
165
0
  return 0;
166
0
}
167
168
static int cmd_show_ref__verify(const struct show_one_options *show_one_opts,
169
        const char **refs)
170
0
{
171
0
  if (!refs || !*refs)
172
0
    die("--verify requires a reference");
173
174
0
  while (*refs) {
175
0
    struct object_id oid;
176
177
0
    if ((starts_with(*refs, "refs/") || refname_is_safe(*refs)) &&
178
0
        !refs_read_ref(get_main_ref_store(the_repository), *refs, &oid)) {
179
0
      show_one(show_one_opts, *refs, &oid);
180
0
    }
181
0
    else if (!show_one_opts->quiet)
182
0
      die("'%s' - not a valid ref", *refs);
183
0
    else
184
0
      return 1;
185
0
    refs++;
186
0
  }
187
188
0
  return 0;
189
0
}
190
191
struct patterns_options {
192
  int show_head;
193
  int branches_only;
194
  int tags_only;
195
};
196
197
static int cmd_show_ref__patterns(const struct patterns_options *opts,
198
          const struct show_one_options *show_one_opts,
199
          const char **patterns)
200
0
{
201
0
  struct show_ref_data show_ref_data = {
202
0
    .show_one_opts = show_one_opts,
203
0
    .show_head = opts->show_head,
204
0
  };
205
206
0
  if (patterns && *patterns)
207
0
    show_ref_data.patterns = patterns;
208
209
0
  if (opts->show_head)
210
0
    refs_head_ref(get_main_ref_store(the_repository), show_ref,
211
0
            &show_ref_data);
212
0
  if (opts->branches_only || opts->tags_only) {
213
0
    if (opts->branches_only)
214
0
      refs_for_each_fullref_in(get_main_ref_store(the_repository),
215
0
             "refs/heads/", NULL,
216
0
             show_ref, &show_ref_data);
217
0
    if (opts->tags_only)
218
0
      refs_for_each_fullref_in(get_main_ref_store(the_repository),
219
0
             "refs/tags/", NULL, show_ref,
220
0
             &show_ref_data);
221
0
  } else {
222
0
    refs_for_each_ref(get_main_ref_store(the_repository),
223
0
          show_ref, &show_ref_data);
224
0
  }
225
0
  if (!show_ref_data.found_match)
226
0
    return 1;
227
228
0
  return 0;
229
0
}
230
231
static int cmd_show_ref__exists(const char **refs)
232
0
{
233
0
  struct strbuf unused_referent = STRBUF_INIT;
234
0
  struct object_id unused_oid;
235
0
  unsigned int unused_type;
236
0
  int failure_errno = 0;
237
0
  const char *ref;
238
0
  int ret = 0;
239
240
0
  if (!refs || !*refs)
241
0
    die("--exists requires a reference");
242
0
  ref = *refs++;
243
0
  if (*refs)
244
0
    die("--exists requires exactly one reference");
245
246
0
  if (refs_read_raw_ref(get_main_ref_store(the_repository), ref,
247
0
            &unused_oid, &unused_referent, &unused_type,
248
0
            &failure_errno)) {
249
0
    if (failure_errno == ENOENT || failure_errno == EISDIR) {
250
0
      error(_("reference does not exist"));
251
0
      ret = 2;
252
0
    } else {
253
0
      errno = failure_errno;
254
0
      error_errno(_("failed to look up reference"));
255
0
      ret = 1;
256
0
    }
257
258
0
    goto out;
259
0
  }
260
261
0
out:
262
0
  strbuf_release(&unused_referent);
263
0
  return ret;
264
0
}
265
266
static int hash_callback(const struct option *opt, const char *arg, int unset)
267
0
{
268
0
  struct show_one_options *opts = opt->value;
269
0
  struct option abbrev_opt = *opt;
270
271
0
  opts->hash_only = 1;
272
  /* Use full length SHA1 if no argument */
273
0
  if (!arg)
274
0
    return 0;
275
276
0
  abbrev_opt.value = &opts->abbrev;
277
0
  return parse_opt_abbrev_cb(&abbrev_opt, arg, unset);
278
0
}
279
280
static int exclude_existing_callback(const struct option *opt, const char *arg,
281
             int unset)
282
0
{
283
0
  struct exclude_existing_options *opts = opt->value;
284
0
  BUG_ON_OPT_NEG(unset);
285
0
  opts->enabled = 1;
286
0
  opts->pattern = arg;
287
0
  return 0;
288
0
}
289
290
int cmd_show_ref(int argc, const char **argv, const char *prefix)
291
0
{
292
0
  struct exclude_existing_options exclude_existing_opts = {0};
293
0
  struct patterns_options patterns_opts = {0};
294
0
  struct show_one_options show_one_opts = {0};
295
0
  int verify = 0, exists = 0;
296
0
  const struct option show_ref_options[] = {
297
0
    OPT_BOOL(0, "tags", &patterns_opts.tags_only, N_("only show tags (can be combined with --branches)")),
298
0
    OPT_BOOL(0, "branches", &patterns_opts.branches_only, N_("only show branches (can be combined with --tags)")),
299
0
    OPT_HIDDEN_BOOL(0, "heads", &patterns_opts.branches_only,
300
0
        N_("deprecated synonym for --branches")),
301
0
    OPT_BOOL(0, "exists", &exists, N_("check for reference existence without resolving")),
302
0
    OPT_BOOL(0, "verify", &verify, N_("stricter reference checking, "
303
0
          "requires exact ref path")),
304
0
    OPT_HIDDEN_BOOL('h', NULL, &patterns_opts.show_head,
305
0
        N_("show the HEAD reference, even if it would be filtered out")),
306
0
    OPT_BOOL(0, "head", &patterns_opts.show_head,
307
0
      N_("show the HEAD reference, even if it would be filtered out")),
308
0
    OPT_BOOL('d', "dereference", &show_one_opts.deref_tags,
309
0
          N_("dereference tags into object IDs")),
310
0
    OPT_CALLBACK_F('s', "hash", &show_one_opts, N_("n"),
311
0
             N_("only show SHA1 hash using <n> digits"),
312
0
             PARSE_OPT_OPTARG, &hash_callback),
313
0
    OPT__ABBREV(&show_one_opts.abbrev),
314
0
    OPT__QUIET(&show_one_opts.quiet,
315
0
         N_("do not print results to stdout (useful with --verify)")),
316
0
    OPT_CALLBACK_F(0, "exclude-existing", &exclude_existing_opts,
317
0
             N_("pattern"), N_("show refs from stdin that aren't in local repository"),
318
0
             PARSE_OPT_OPTARG | PARSE_OPT_NONEG, exclude_existing_callback),
319
0
    OPT_END()
320
0
  };
321
322
0
  git_config(git_default_config, NULL);
323
324
0
  argc = parse_options(argc, argv, prefix, show_ref_options,
325
0
           show_ref_usage, 0);
326
327
0
  die_for_incompatible_opt3(exclude_existing_opts.enabled, "--exclude-existing",
328
0
          verify, "--verify",
329
0
          exists, "--exists");
330
331
0
  if (exclude_existing_opts.enabled)
332
0
    return cmd_show_ref__exclude_existing(&exclude_existing_opts);
333
0
  else if (verify)
334
0
    return cmd_show_ref__verify(&show_one_opts, argv);
335
0
  else if (exists)
336
0
    return cmd_show_ref__exists(argv);
337
0
  else
338
0
    return cmd_show_ref__patterns(&patterns_opts, &show_one_opts, argv);
339
0
}