]> git.ipfire.org Git - thirdparty/git.git/blame - refs.c
refs: make verify_refname_available() virtual
[thirdparty/git.git] / refs.c
CommitLineData
7bd9bcf3
MH
1/*
2 * The backend-independent part of the reference module.
3 */
4
95fc7512 5#include "cache.h"
697cc8ef 6#include "lockfile.h"
85023577 7#include "refs.h"
4cb77009 8#include "refs/refs-internal.h"
cf0adba7
JH
9#include "object.h"
10#include "tag.h"
3581d793 11
3dce444f
RS
12/*
13 * List of all available backends
14 */
15static struct ref_storage_be *refs_backends = &refs_be_files;
16
17static struct ref_storage_be *find_ref_storage_backend(const char *name)
18{
19 struct ref_storage_be *be;
20 for (be = refs_backends; be; be = be->next)
21 if (!strcmp(be->name, name))
22 return be;
23 return NULL;
24}
25
26int ref_storage_backend_exists(const char *name)
27{
28 return find_ref_storage_backend(name) != NULL;
29}
30
bc5fd6d3 31/*
dde8a902
DT
32 * How to handle various characters in refnames:
33 * 0: An acceptable character for refs
5e650228
JH
34 * 1: End-of-component
35 * 2: ., look for a preceding . to reject .. in refs
36 * 3: {, look for a preceding @ to reject @{ in refs
53a8555e 37 * 4: A bad character: ASCII control characters, and
cd377f45
JK
38 * ":", "?", "[", "\", "^", "~", SP, or TAB
39 * 5: *, reject unless REFNAME_REFSPEC_PATTERN is set
dde8a902
DT
40 */
41static unsigned char refname_disposition[256] = {
5e650228
JH
42 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
43 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
cd377f45 44 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 2, 1,
5e650228 45 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 4,
dde8a902 46 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5e650228
JH
47 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 4, 0,
48 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
49 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 4, 4
dde8a902
DT
50};
51
52/*
53 * Try to read one refname component from the front of refname.
54 * Return the length of the component found, or -1 if the component is
55 * not legal. It is legal if it is something reasonable to have under
56 * ".git/refs/"; We do not like it if:
bc5fd6d3
MH
57 *
58 * - any path component of it begins with ".", or
59 * - it has double dots "..", or
53a8555e 60 * - it has ASCII control characters, or
cd377f45
JK
61 * - it has ":", "?", "[", "\", "^", "~", SP, or TAB anywhere, or
62 * - it has "*" anywhere unless REFNAME_REFSPEC_PATTERN is set, or
53a8555e
JK
63 * - it ends with a "/", or
64 * - it ends with ".lock", or
65 * - it contains a "@{" portion
bc5fd6d3 66 */
cd377f45 67static int check_refname_component(const char *refname, int *flags)
bc5fd6d3
MH
68{
69 const char *cp;
70 char last = '\0';
71
72 for (cp = refname; ; cp++) {
dde8a902
DT
73 int ch = *cp & 255;
74 unsigned char disp = refname_disposition[ch];
75 switch (disp) {
5e650228 76 case 1:
dde8a902 77 goto out;
5e650228 78 case 2:
dde8a902
DT
79 if (last == '.')
80 return -1; /* Refname contains "..". */
81 break;
5e650228 82 case 3:
dde8a902
DT
83 if (last == '@')
84 return -1; /* Refname contains "@{". */
bc5fd6d3 85 break;
5e650228 86 case 4:
dde8a902 87 return -1;
cd377f45
JK
88 case 5:
89 if (!(*flags & REFNAME_REFSPEC_PATTERN))
90 return -1; /* refspec can't be a pattern */
91
92 /*
93 * Unset the pattern flag so that we only accept
94 * a single asterisk for one side of refspec.
95 */
96 *flags &= ~ REFNAME_REFSPEC_PATTERN;
97 break;
dde8a902 98 }
bc5fd6d3
MH
99 last = ch;
100 }
dde8a902 101out:
bc5fd6d3 102 if (cp == refname)
dac529e4 103 return 0; /* Component has zero length. */
f3cc52d8
JN
104 if (refname[0] == '.')
105 return -1; /* Component starts with '.'. */
7108ad23
MH
106 if (cp - refname >= LOCK_SUFFIX_LEN &&
107 !memcmp(cp - LOCK_SUFFIX_LEN, LOCK_SUFFIX, LOCK_SUFFIX_LEN))
bc5fd6d3
MH
108 return -1; /* Refname ends with ".lock". */
109 return cp - refname;
110}
111
5e650228 112int check_refname_format(const char *refname, int flags)
bc5fd6d3
MH
113{
114 int component_len, component_count = 0;
115
9ba89f48
FC
116 if (!strcmp(refname, "@"))
117 /* Refname is a single character '@'. */
118 return -1;
119
bc5fd6d3
MH
120 while (1) {
121 /* We are at the start of a path component. */
cd377f45
JK
122 component_len = check_refname_component(refname, &flags);
123 if (component_len <= 0)
124 return -1;
125
bc5fd6d3
MH
126 component_count++;
127 if (refname[component_len] == '\0')
128 break;
129 /* Skip to next component. */
130 refname += component_len + 1;
131 }
132
133 if (refname[component_len - 1] == '.')
134 return -1; /* Refname ends with '.'. */
135 if (!(flags & REFNAME_ALLOW_ONELEVEL) && component_count < 2)
136 return -1; /* Refname has only one component. */
137 return 0;
138}
139
4cb77009 140int refname_is_safe(const char *refname)
d0f810f0 141{
39950fef
MH
142 const char *rest;
143
144 if (skip_prefix(refname, "refs/", &rest)) {
d0f810f0
RS
145 char *buf;
146 int result;
e40f3557
MH
147 size_t restlen = strlen(rest);
148
149 /* rest must not be empty, or start or end with "/" */
150 if (!restlen || *rest == '/' || rest[restlen - 1] == '/')
151 return 0;
d0f810f0 152
d0f810f0
RS
153 /*
154 * Does the refname try to escape refs/?
155 * For example: refs/foo/../bar is safe but refs/foo/../../bar
156 * is not.
157 */
e40f3557
MH
158 buf = xmallocz(restlen);
159 result = !normalize_path_copy(buf, rest) && !strcmp(buf, rest);
d0f810f0
RS
160 free(buf);
161 return result;
162 }
35db25c6
MH
163
164 do {
d0f810f0
RS
165 if (!isupper(*refname) && *refname != '_')
166 return 0;
167 refname++;
35db25c6 168 } while (*refname);
d0f810f0
RS
169 return 1;
170}
171
7bd9bcf3
MH
172char *resolve_refdup(const char *refname, int resolve_flags,
173 unsigned char *sha1, int *flags)
e1e22e37 174{
7bd9bcf3
MH
175 return xstrdup_or_null(resolve_ref_unsafe(refname, resolve_flags,
176 sha1, flags));
cddc4258
MH
177}
178
7bd9bcf3
MH
179/* The argument to filter_refs */
180struct ref_filter {
181 const char *pattern;
182 each_ref_fn *fn;
183 void *cb_data;
184};
432ad41e 185
7bd9bcf3 186int read_ref_full(const char *refname, int resolve_flags, unsigned char *sha1, int *flags)
732134ed 187{
7bd9bcf3
MH
188 if (resolve_ref_unsafe(refname, resolve_flags, sha1, flags))
189 return 0;
190 return -1;
732134ed
MH
191}
192
7bd9bcf3 193int read_ref(const char *refname, unsigned char *sha1)
cddc4258 194{
7bd9bcf3 195 return read_ref_full(refname, RESOLVE_REF_READING, sha1, NULL);
c774aab9
JP
196}
197
7bd9bcf3 198int ref_exists(const char *refname)
bc5fd6d3 199{
7bd9bcf3
MH
200 unsigned char sha1[20];
201 return !!resolve_ref_unsafe(refname, RESOLVE_REF_READING, sha1, NULL);
bc5fd6d3
MH
202}
203
7bd9bcf3
MH
204static int filter_refs(const char *refname, const struct object_id *oid,
205 int flags, void *data)
432ad41e 206{
7bd9bcf3
MH
207 struct ref_filter *filter = (struct ref_filter *)data;
208
209 if (wildmatch(filter->pattern, refname, 0, NULL))
210 return 0;
211 return filter->fn(refname, oid, flags, filter->cb_data);
432ad41e
MH
212}
213
7bd9bcf3 214enum peel_status peel_object(const unsigned char *name, unsigned char *sha1)
c774aab9 215{
7bd9bcf3 216 struct object *o = lookup_unknown_object(name);
c774aab9 217
7bd9bcf3
MH
218 if (o->type == OBJ_NONE) {
219 int type = sha1_object_info(name, NULL);
220 if (type < 0 || !object_as_type(o, type, 0))
221 return PEEL_INVALID;
222 }
bc5fd6d3 223
7bd9bcf3
MH
224 if (o->type != OBJ_TAG)
225 return PEEL_NON_TAG;
e1980c9d 226
7bd9bcf3
MH
227 o = deref_tag_noverify(o);
228 if (!o)
229 return PEEL_INVALID;
230
844a9ce4 231 hashcpy(sha1, o->oid.hash);
7bd9bcf3 232 return PEEL_PEELED;
e1980c9d
JH
233}
234
7bd9bcf3
MH
235struct warn_if_dangling_data {
236 FILE *fp;
237 const char *refname;
238 const struct string_list *refnames;
239 const char *msg_fmt;
240};
bc5fd6d3 241
7bd9bcf3
MH
242static int warn_if_dangling_symref(const char *refname, const struct object_id *oid,
243 int flags, void *cb_data)
244{
245 struct warn_if_dangling_data *d = cb_data;
246 const char *resolves_to;
247 struct object_id junk;
bc5fd6d3 248
7bd9bcf3
MH
249 if (!(flags & REF_ISSYMREF))
250 return 0;
bc5fd6d3 251
7bd9bcf3
MH
252 resolves_to = resolve_ref_unsafe(refname, 0, junk.hash, NULL);
253 if (!resolves_to
254 || (d->refname
255 ? strcmp(resolves_to, d->refname)
256 : !string_list_has_string(d->refnames, resolves_to))) {
257 return 0;
258 }
bc5fd6d3 259
7bd9bcf3
MH
260 fprintf(d->fp, d->msg_fmt, refname);
261 fputc('\n', d->fp);
262 return 0;
bc5fd6d3
MH
263}
264
7bd9bcf3 265void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname)
f348ac92 266{
7bd9bcf3
MH
267 struct warn_if_dangling_data data;
268
269 data.fp = fp;
270 data.refname = refname;
271 data.refnames = NULL;
272 data.msg_fmt = msg_fmt;
273 for_each_rawref(warn_if_dangling_symref, &data);
f348ac92
MH
274}
275
7bd9bcf3 276void warn_dangling_symrefs(FILE *fp, const char *msg_fmt, const struct string_list *refnames)
432ad41e 277{
7bd9bcf3 278 struct warn_if_dangling_data data;
432ad41e 279
7bd9bcf3
MH
280 data.fp = fp;
281 data.refname = NULL;
282 data.refnames = refnames;
283 data.msg_fmt = msg_fmt;
284 for_each_rawref(warn_if_dangling_symref, &data);
432ad41e
MH
285}
286
7bd9bcf3 287int for_each_tag_ref(each_ref_fn fn, void *cb_data)
432ad41e 288{
7bd9bcf3 289 return for_each_ref_in("refs/tags/", fn, cb_data);
432ad41e
MH
290}
291
7bd9bcf3 292int for_each_tag_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
506a760d 293{
7bd9bcf3 294 return for_each_ref_in_submodule(submodule, "refs/tags/", fn, cb_data);
432ad41e
MH
295}
296
7bd9bcf3 297int for_each_branch_ref(each_ref_fn fn, void *cb_data)
432ad41e 298{
7bd9bcf3 299 return for_each_ref_in("refs/heads/", fn, cb_data);
432ad41e
MH
300}
301
7bd9bcf3 302int for_each_branch_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
202a56a9 303{
7bd9bcf3 304 return for_each_ref_in_submodule(submodule, "refs/heads/", fn, cb_data);
202a56a9 305}
432ad41e 306
7bd9bcf3 307int for_each_remote_ref(each_ref_fn fn, void *cb_data)
e9c4c111 308{
7bd9bcf3 309 return for_each_ref_in("refs/remotes/", fn, cb_data);
202a56a9
MH
310}
311
7bd9bcf3 312int for_each_remote_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
e9c4c111 313{
7bd9bcf3 314 return for_each_ref_in_submodule(submodule, "refs/remotes/", fn, cb_data);
662428f4 315}
c774aab9 316
7bd9bcf3
MH
317int head_ref_namespaced(each_ref_fn fn, void *cb_data)
318{
319 struct strbuf buf = STRBUF_INIT;
320 int ret = 0;
321 struct object_id oid;
322 int flag;
c774aab9 323
7bd9bcf3
MH
324 strbuf_addf(&buf, "%sHEAD", get_git_namespace());
325 if (!read_ref_full(buf.buf, RESOLVE_REF_READING, oid.hash, &flag))
326 ret = fn(buf.buf, &oid, flag, cb_data);
327 strbuf_release(&buf);
c774aab9 328
7bd9bcf3 329 return ret;
e9c4c111 330}
c774aab9 331
7bd9bcf3
MH
332int for_each_glob_ref_in(each_ref_fn fn, const char *pattern,
333 const char *prefix, void *cb_data)
662428f4 334{
7bd9bcf3
MH
335 struct strbuf real_pattern = STRBUF_INIT;
336 struct ref_filter filter;
337 int ret;
d08bae7e 338
59556548 339 if (!prefix && !starts_with(pattern, "refs/"))
d08bae7e 340 strbuf_addstr(&real_pattern, "refs/");
b09fe971
IL
341 else if (prefix)
342 strbuf_addstr(&real_pattern, prefix);
d08bae7e
IL
343 strbuf_addstr(&real_pattern, pattern);
344
894a9d33 345 if (!has_glob_specials(pattern)) {
9517e6b8 346 /* Append implied '/' '*' if not present. */
00b6c178 347 strbuf_complete(&real_pattern, '/');
d08bae7e
IL
348 /* No need to check for '*', there is none. */
349 strbuf_addch(&real_pattern, '*');
350 }
351
352 filter.pattern = real_pattern.buf;
353 filter.fn = fn;
354 filter.cb_data = cb_data;
355 ret = for_each_ref(filter_refs, &filter);
356
357 strbuf_release(&real_pattern);
358 return ret;
359}
360
b09fe971
IL
361int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data)
362{
363 return for_each_glob_ref_in(fn, pattern, NULL, cb_data);
364}
365
4577e483 366const char *prettify_refname(const char *name)
a9c37a72 367{
a9c37a72 368 return name + (
59556548
CC
369 starts_with(name, "refs/heads/") ? 11 :
370 starts_with(name, "refs/tags/") ? 10 :
371 starts_with(name, "refs/remotes/") ? 13 :
a9c37a72
DB
372 0);
373}
374
54457fe5 375static const char *ref_rev_parse_rules[] = {
79803322
SP
376 "%.*s",
377 "refs/%.*s",
378 "refs/tags/%.*s",
379 "refs/heads/%.*s",
380 "refs/remotes/%.*s",
381 "refs/remotes/%.*s/HEAD",
382 NULL
383};
384
54457fe5 385int refname_match(const char *abbrev_name, const char *full_name)
79803322
SP
386{
387 const char **p;
388 const int abbrev_name_len = strlen(abbrev_name);
389
54457fe5 390 for (p = ref_rev_parse_rules; *p; p++) {
79803322
SP
391 if (!strcmp(full_name, mkpath(*p, abbrev_name_len, abbrev_name))) {
392 return 1;
393 }
394 }
395
396 return 0;
397}
398
ff74f7f1
JH
399/*
400 * *string and *len will only be substituted, and *string returned (for
401 * later free()ing) if the string passed in is a magic short-hand form
402 * to name a branch.
403 */
404static char *substitute_branch_name(const char **string, int *len)
405{
406 struct strbuf buf = STRBUF_INIT;
cf99a761 407 int ret = interpret_branch_name(*string, *len, &buf);
ff74f7f1
JH
408
409 if (ret == *len) {
410 size_t size;
411 *string = strbuf_detach(&buf, &size);
412 *len = size;
413 return (char *)*string;
414 }
415
416 return NULL;
417}
418
419int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref)
420{
421 char *last_branch = substitute_branch_name(&str, &len);
422 const char **p, *r;
423 int refs_found = 0;
424
425 *ref = NULL;
426 for (p = ref_rev_parse_rules; *p; p++) {
427 char fullref[PATH_MAX];
428 unsigned char sha1_from_ref[20];
429 unsigned char *this_result;
430 int flag;
431
432 this_result = refs_found ? sha1_from_ref : sha1;
433 mksnpath(fullref, sizeof(fullref), *p, len, str);
7695d118
RS
434 r = resolve_ref_unsafe(fullref, RESOLVE_REF_READING,
435 this_result, &flag);
ff74f7f1
JH
436 if (r) {
437 if (!refs_found++)
438 *ref = xstrdup(r);
439 if (!warn_ambiguous_refs)
440 break;
55956350 441 } else if ((flag & REF_ISSYMREF) && strcmp(fullref, "HEAD")) {
ff74f7f1 442 warning("ignoring dangling symref %s.", fullref);
55956350
JH
443 } else if ((flag & REF_ISBROKEN) && strchr(fullref, '/')) {
444 warning("ignoring broken ref %s.", fullref);
445 }
ff74f7f1
JH
446 }
447 free(last_branch);
448 return refs_found;
449}
450
451int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
452{
453 char *last_branch = substitute_branch_name(&str, &len);
454 const char **p;
455 int logs_found = 0;
456
457 *log = NULL;
458 for (p = ref_rev_parse_rules; *p; p++) {
ff74f7f1
JH
459 unsigned char hash[20];
460 char path[PATH_MAX];
461 const char *ref, *it;
462
463 mksnpath(path, sizeof(path), *p, len, str);
7695d118
RS
464 ref = resolve_ref_unsafe(path, RESOLVE_REF_READING,
465 hash, NULL);
ff74f7f1
JH
466 if (!ref)
467 continue;
4da58835 468 if (reflog_exists(path))
ff74f7f1 469 it = path;
4da58835 470 else if (strcmp(ref, path) && reflog_exists(ref))
ff74f7f1
JH
471 it = ref;
472 else
473 continue;
474 if (!logs_found++) {
475 *log = xstrdup(it);
476 hashcpy(sha1, hash);
477 }
7bd9bcf3
MH
478 if (!warn_ambiguous_refs)
479 break;
c0277d15 480 }
7bd9bcf3
MH
481 free(last_branch);
482 return logs_found;
2ddb5d17
BK
483}
484
266b1827
DT
485static int is_per_worktree_ref(const char *refname)
486{
ce414b33
DT
487 return !strcmp(refname, "HEAD") ||
488 starts_with(refname, "refs/bisect/");
266b1827
DT
489}
490
491static int is_pseudoref_syntax(const char *refname)
492{
493 const char *c;
494
495 for (c = refname; *c; c++) {
496 if (!isupper(*c) && *c != '-' && *c != '_')
497 return 0;
498 }
499
500 return 1;
501}
502
503enum ref_type ref_type(const char *refname)
504{
505 if (is_per_worktree_ref(refname))
506 return REF_TYPE_PER_WORKTREE;
507 if (is_pseudoref_syntax(refname))
508 return REF_TYPE_PSEUDOREF;
509 return REF_TYPE_NORMAL;
510}
511
74ec19d4
DT
512static int write_pseudoref(const char *pseudoref, const unsigned char *sha1,
513 const unsigned char *old_sha1, struct strbuf *err)
514{
515 const char *filename;
516 int fd;
517 static struct lock_file lock;
518 struct strbuf buf = STRBUF_INIT;
519 int ret = -1;
520
521 strbuf_addf(&buf, "%s\n", sha1_to_hex(sha1));
522
523 filename = git_path("%s", pseudoref);
524 fd = hold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR);
525 if (fd < 0) {
0568c8e9 526 strbuf_addf(err, "could not open '%s' for writing: %s",
74ec19d4
DT
527 filename, strerror(errno));
528 return -1;
529 }
530
531 if (old_sha1) {
532 unsigned char actual_old_sha1[20];
2c3aed13
DT
533
534 if (read_ref(pseudoref, actual_old_sha1))
535 die("could not read ref '%s'", pseudoref);
74ec19d4 536 if (hashcmp(actual_old_sha1, old_sha1)) {
0568c8e9 537 strbuf_addf(err, "unexpected sha1 when writing '%s'", pseudoref);
74ec19d4
DT
538 rollback_lock_file(&lock);
539 goto done;
540 }
541 }
542
543 if (write_in_full(fd, buf.buf, buf.len) != buf.len) {
0568c8e9 544 strbuf_addf(err, "could not write to '%s'", filename);
74ec19d4
DT
545 rollback_lock_file(&lock);
546 goto done;
547 }
548
549 commit_lock_file(&lock);
550 ret = 0;
551done:
552 strbuf_release(&buf);
553 return ret;
554}
555
556static int delete_pseudoref(const char *pseudoref, const unsigned char *old_sha1)
557{
558 static struct lock_file lock;
559 const char *filename;
560
561 filename = git_path("%s", pseudoref);
562
563 if (old_sha1 && !is_null_sha1(old_sha1)) {
564 int fd;
565 unsigned char actual_old_sha1[20];
566
567 fd = hold_lock_file_for_update(&lock, filename,
568 LOCK_DIE_ON_ERROR);
569 if (fd < 0)
570 die_errno(_("Could not open '%s' for writing"), filename);
2c3aed13
DT
571 if (read_ref(pseudoref, actual_old_sha1))
572 die("could not read ref '%s'", pseudoref);
74ec19d4
DT
573 if (hashcmp(actual_old_sha1, old_sha1)) {
574 warning("Unexpected sha1 when deleting %s", pseudoref);
575 rollback_lock_file(&lock);
576 return -1;
a4c653df 577 }
74ec19d4
DT
578
579 unlink(filename);
580 rollback_lock_file(&lock);
581 } else {
582 unlink(filename);
4bd18c43 583 }
a4c653df 584
4bd18c43 585 return 0;
95fc7512 586}
d556fae2 587
fc1c2168
MH
588int delete_ref(const char *refname, const unsigned char *old_sha1,
589 unsigned int flags)
41b625b0 590{
7521cc46 591 struct ref_transaction *transaction;
a4c653df 592 struct strbuf err = STRBUF_INIT;
8b5157e4 593
74ec19d4 594 if (ref_type(refname) == REF_TYPE_PSEUDOREF)
080cc646 595 return delete_pseudoref(refname, old_sha1);
d48744d1 596
7521cc46
RS
597 transaction = ref_transaction_begin(&err);
598 if (!transaction ||
fc67a082 599 ref_transaction_delete(transaction, refname, old_sha1,
fb5a6bb6 600 flags, NULL, &err) ||
db7516ab 601 ref_transaction_commit(transaction, &err)) {
7521cc46
RS
602 error("%s", err.buf);
603 ref_transaction_free(transaction);
604 strbuf_release(&err);
c0277d15 605 return 1;
41b625b0 606 }
7bd9bcf3
MH
607 ref_transaction_free(transaction);
608 strbuf_release(&err);
b531394d
BC
609 return 0;
610}
41b625b0 611
4cb77009 612int copy_reflog_msg(char *buf, const char *msg)
0ec29a47
JH
613{
614 char *cp = buf;
615 char c;
616 int wasspace = 1;
8b5157e4 617
0ec29a47
JH
618 *cp++ = '\t';
619 while ((c = *msg++)) {
620 if (wasspace && isspace(c))
621 continue;
622 wasspace = isspace(c);
623 if (wasspace)
624 c = ' ';
625 *cp++ = c;
a4c653df 626 }
0ec29a47
JH
627 while (buf < cp && isspace(cp[-1]))
628 cp--;
629 *cp++ = '\n';
630 return cp - buf;
631}
8b5157e4 632
4cb77009 633int should_autocreate_reflog(const char *refname)
4e2bef57
DT
634{
635 if (!log_all_ref_updates)
636 return 0;
637 return starts_with(refname, "refs/heads/") ||
638 starts_with(refname, "refs/remotes/") ||
639 starts_with(refname, "refs/notes/") ||
640 !strcmp(refname, "HEAD");
641}
642
e7e0f26e 643int is_branch(const char *refname)
c3b0dec5 644{
59556548 645 return !strcmp(refname, "HEAD") || starts_with(refname, "refs/heads/");
41b625b0
NP
646}
647
4207ed28
RS
648struct read_ref_at_cb {
649 const char *refname;
650 unsigned long at_time;
651 int cnt;
652 int reccnt;
653 unsigned char *sha1;
654 int found_it;
655
656 unsigned char osha1[20];
657 unsigned char nsha1[20];
658 int tz;
659 unsigned long date;
660 char **msg;
661 unsigned long *cutoff_time;
662 int *cutoff_tz;
663 int *cutoff_cnt;
664};
665
666static int read_ref_at_ent(unsigned char *osha1, unsigned char *nsha1,
667 const char *email, unsigned long timestamp, int tz,
668 const char *message, void *cb_data)
669{
670 struct read_ref_at_cb *cb = cb_data;
671
672 cb->reccnt++;
673 cb->tz = tz;
674 cb->date = timestamp;
675
676 if (timestamp <= cb->at_time || cb->cnt == 0) {
677 if (cb->msg)
678 *cb->msg = xstrdup(message);
679 if (cb->cutoff_time)
680 *cb->cutoff_time = timestamp;
681 if (cb->cutoff_tz)
682 *cb->cutoff_tz = tz;
683 if (cb->cutoff_cnt)
684 *cb->cutoff_cnt = cb->reccnt - 1;
685 /*
686 * we have not yet updated cb->[n|o]sha1 so they still
687 * hold the values for the previous record.
688 */
689 if (!is_null_sha1(cb->osha1)) {
690 hashcpy(cb->sha1, nsha1);
691 if (hashcmp(cb->osha1, nsha1))
692 warning("Log for ref %s has gap after %s.",
a5481a6c 693 cb->refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822)));
4207ed28
RS
694 }
695 else if (cb->date == cb->at_time)
696 hashcpy(cb->sha1, nsha1);
697 else if (hashcmp(nsha1, cb->sha1))
698 warning("Log for ref %s unexpectedly ended on %s.",
699 cb->refname, show_date(cb->date, cb->tz,
a5481a6c 700 DATE_MODE(RFC2822)));
4207ed28
RS
701 hashcpy(cb->osha1, osha1);
702 hashcpy(cb->nsha1, nsha1);
703 cb->found_it = 1;
704 return 1;
705 }
706 hashcpy(cb->osha1, osha1);
707 hashcpy(cb->nsha1, nsha1);
708 if (cb->cnt > 0)
709 cb->cnt--;
710 return 0;
711}
712
713static int read_ref_at_ent_oldest(unsigned char *osha1, unsigned char *nsha1,
714 const char *email, unsigned long timestamp,
715 int tz, const char *message, void *cb_data)
716{
717 struct read_ref_at_cb *cb = cb_data;
718
719 if (cb->msg)
720 *cb->msg = xstrdup(message);
721 if (cb->cutoff_time)
722 *cb->cutoff_time = timestamp;
723 if (cb->cutoff_tz)
724 *cb->cutoff_tz = tz;
725 if (cb->cutoff_cnt)
726 *cb->cutoff_cnt = cb->reccnt;
727 hashcpy(cb->sha1, osha1);
728 if (is_null_sha1(cb->sha1))
729 hashcpy(cb->sha1, nsha1);
730 /* We just want the first entry */
731 return 1;
16d7cc90
JH
732}
733
c41a87dd 734int read_ref_at(const char *refname, unsigned int flags, unsigned long at_time, int cnt,
dfefa935
MH
735 unsigned char *sha1, char **msg,
736 unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt)
d556fae2 737{
4207ed28 738 struct read_ref_at_cb cb;
d556fae2 739
4207ed28
RS
740 memset(&cb, 0, sizeof(cb));
741 cb.refname = refname;
742 cb.at_time = at_time;
743 cb.cnt = cnt;
744 cb.msg = msg;
745 cb.cutoff_time = cutoff_time;
746 cb.cutoff_tz = cutoff_tz;
747 cb.cutoff_cnt = cutoff_cnt;
748 cb.sha1 = sha1;
749
750 for_each_reflog_ent_reverse(refname, read_ref_at_ent, &cb);
751
c41a87dd
DA
752 if (!cb.reccnt) {
753 if (flags & GET_SHA1_QUIETLY)
754 exit(128);
755 else
756 die("Log for %s is empty.", refname);
757 }
4207ed28
RS
758 if (cb.found_it)
759 return 0;
760
761 for_each_reflog_ent(refname, read_ref_at_ent_oldest, &cb);
d556fae2 762
16d7cc90 763 return 1;
d556fae2 764}
2ff81662 765
93a644ea 766struct ref_transaction *ref_transaction_begin(struct strbuf *err)
caa4046c 767{
5a603b04
JN
768 assert(err);
769
caa4046c
MH
770 return xcalloc(1, sizeof(struct ref_transaction));
771}
772
026bd1d3 773void ref_transaction_free(struct ref_transaction *transaction)
caa4046c
MH
774{
775 int i;
776
1b07255c
RS
777 if (!transaction)
778 return;
779
db7516ab
RS
780 for (i = 0; i < transaction->nr; i++) {
781 free(transaction->updates[i]->msg);
88615910 782 free(transaction->updates[i]);
db7516ab 783 }
caa4046c
MH
784 free(transaction->updates);
785 free(transaction);
786}
787
71564516
MH
788struct ref_update *ref_transaction_add_update(
789 struct ref_transaction *transaction,
790 const char *refname, unsigned int flags,
791 const unsigned char *new_sha1,
792 const unsigned char *old_sha1,
793 const char *msg)
caa4046c 794{
96ffc06f 795 struct ref_update *update;
71564516
MH
796
797 if (transaction->state != REF_TRANSACTION_OPEN)
798 die("BUG: update called for transaction that is not open");
799
800 if ((flags & REF_ISPRUNING) && !(flags & REF_NODEREF))
801 die("BUG: REF_ISPRUNING set without REF_NODEREF");
802
96ffc06f 803 FLEX_ALLOC_STR(update, refname, refname);
caa4046c
MH
804 ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc);
805 transaction->updates[transaction->nr++] = update;
71564516
MH
806
807 update->flags = flags;
808
809 if (flags & REF_HAVE_NEW)
810 hashcpy(update->new_sha1, new_sha1);
811 if (flags & REF_HAVE_OLD)
812 hashcpy(update->old_sha1, old_sha1);
813 if (msg)
814 update->msg = xstrdup(msg);
caa4046c
MH
815 return update;
816}
817
8e34800e
RS
818int ref_transaction_update(struct ref_transaction *transaction,
819 const char *refname,
820 const unsigned char *new_sha1,
821 const unsigned char *old_sha1,
1d147bdf 822 unsigned int flags, const char *msg,
8e34800e 823 struct strbuf *err)
caa4046c 824{
5a603b04
JN
825 assert(err);
826
8a679de6
MH
827 if ((new_sha1 && !is_null_sha1(new_sha1)) ?
828 check_refname_format(refname, REFNAME_ALLOW_ONELEVEL) :
829 !refname_is_safe(refname)) {
0568c8e9 830 strbuf_addf(err, "refusing to update ref with bad name '%s'",
d0f810f0
RS
831 refname);
832 return -1;
833 }
834
71564516
MH
835 flags |= (new_sha1 ? REF_HAVE_NEW : 0) | (old_sha1 ? REF_HAVE_OLD : 0);
836
837 ref_transaction_add_update(transaction, refname, flags,
838 new_sha1, old_sha1, msg);
8e34800e 839 return 0;
caa4046c
MH
840}
841
b416af5b
RS
842int ref_transaction_create(struct ref_transaction *transaction,
843 const char *refname,
844 const unsigned char *new_sha1,
fec14ec3 845 unsigned int flags, const char *msg,
b416af5b 846 struct strbuf *err)
caa4046c 847{
f04c5b55
MH
848 if (!new_sha1 || is_null_sha1(new_sha1))
849 die("BUG: create called without valid new_sha1");
bc9f2925 850 return ref_transaction_update(transaction, refname, new_sha1,
1d147bdf 851 null_sha1, flags, msg, err);
caa4046c
MH
852}
853
8c8bdc0d
RS
854int ref_transaction_delete(struct ref_transaction *transaction,
855 const char *refname,
856 const unsigned char *old_sha1,
fb5a6bb6 857 unsigned int flags, const char *msg,
8c8bdc0d 858 struct strbuf *err)
caa4046c 859{
60294596
MH
860 if (old_sha1 && is_null_sha1(old_sha1))
861 die("BUG: delete called with old_sha1 set to zeros");
1d147bdf 862 return ref_transaction_update(transaction, refname,
fb5a6bb6 863 null_sha1, old_sha1,
1d147bdf 864 flags, msg, err);
caa4046c
MH
865}
866
16180334
MH
867int ref_transaction_verify(struct ref_transaction *transaction,
868 const char *refname,
869 const unsigned char *old_sha1,
870 unsigned int flags,
871 struct strbuf *err)
872{
873 if (!old_sha1)
874 die("BUG: verify called with old_sha1 set to NULL");
875 return ref_transaction_update(transaction, refname,
876 NULL, old_sha1,
877 flags, NULL, err);
878}
879
4b7b520b
MH
880int update_ref(const char *msg, const char *refname,
881 const unsigned char *new_sha1, const unsigned char *old_sha1,
fec14ec3 882 unsigned int flags, enum action_on_err onerr)
4738a333 883{
74ec19d4 884 struct ref_transaction *t = NULL;
b4d75ac1 885 struct strbuf err = STRBUF_INIT;
74ec19d4 886 int ret = 0;
b4d75ac1 887
74ec19d4
DT
888 if (ref_type(refname) == REF_TYPE_PSEUDOREF) {
889 ret = write_pseudoref(refname, new_sha1, old_sha1, &err);
890 } else {
891 t = ref_transaction_begin(&err);
892 if (!t ||
893 ref_transaction_update(t, refname, new_sha1, old_sha1,
894 flags, msg, &err) ||
895 ref_transaction_commit(t, &err)) {
896 ret = 1;
897 ref_transaction_free(t);
898 }
899 }
900 if (ret) {
b4d75ac1
RS
901 const char *str = "update_ref failed for ref '%s': %s";
902
b4d75ac1
RS
903 switch (onerr) {
904 case UPDATE_REFS_MSG_ON_ERR:
905 error(str, refname, err.buf);
906 break;
907 case UPDATE_REFS_DIE_ON_ERR:
908 die(str, refname, err.buf);
909 break;
910 case UPDATE_REFS_QUIET_ON_ERR:
911 break;
912 }
913 strbuf_release(&err);
4738a333 914 return 1;
b4d75ac1
RS
915 }
916 strbuf_release(&err);
74ec19d4
DT
917 if (t)
918 ref_transaction_free(t);
b4d75ac1 919 return 0;
4738a333
BK
920}
921
dfefa935 922char *shorten_unambiguous_ref(const char *refname, int strict)
7c2b3029
JK
923{
924 int i;
925 static char **scanf_fmts;
926 static int nr_rules;
927 char *short_name;
928
7c2b3029 929 if (!nr_rules) {
4346663a
MH
930 /*
931 * Pre-generate scanf formats from ref_rev_parse_rules[].
932 * Generate a format suitable for scanf from a
933 * ref_rev_parse_rules rule by interpolating "%s" at the
934 * location of the "%.*s".
935 */
7c2b3029 936 size_t total_len = 0;
84d5633f 937 size_t offset = 0;
7c2b3029
JK
938
939 /* the rule list is NULL terminated, count them first */
a4165851 940 for (nr_rules = 0; ref_rev_parse_rules[nr_rules]; nr_rules++)
7902fe03
MH
941 /* -2 for strlen("%.*s") - strlen("%s"); +1 for NUL */
942 total_len += strlen(ref_rev_parse_rules[nr_rules]) - 2 + 1;
7c2b3029 943
50a6c8ef 944 scanf_fmts = xmalloc(st_add(st_mult(nr_rules, sizeof(char *)), total_len));
7c2b3029 945
84d5633f 946 offset = 0;
7c2b3029 947 for (i = 0; i < nr_rules; i++) {
4346663a 948 assert(offset < total_len);
84d5633f 949 scanf_fmts[i] = (char *)&scanf_fmts[nr_rules] + offset;
4346663a
MH
950 offset += snprintf(scanf_fmts[i], total_len - offset,
951 ref_rev_parse_rules[i], 2, "%s") + 1;
7c2b3029
JK
952 }
953 }
954
955 /* bail out if there are no rules */
956 if (!nr_rules)
dfefa935 957 return xstrdup(refname);
7c2b3029 958
dfefa935
MH
959 /* buffer for scanf result, at most refname must fit */
960 short_name = xstrdup(refname);
7c2b3029
JK
961
962 /* skip first rule, it will always match */
963 for (i = nr_rules - 1; i > 0 ; --i) {
964 int j;
6e7b3309 965 int rules_to_fail = i;
7c2b3029
JK
966 int short_name_len;
967
dfefa935 968 if (1 != sscanf(refname, scanf_fmts[i], short_name))
7c2b3029
JK
969 continue;
970
971 short_name_len = strlen(short_name);
972
6e7b3309
BW
973 /*
974 * in strict mode, all (except the matched one) rules
975 * must fail to resolve to a valid non-ambiguous ref
976 */
977 if (strict)
978 rules_to_fail = nr_rules;
979
7c2b3029
JK
980 /*
981 * check if the short name resolves to a valid ref,
982 * but use only rules prior to the matched one
983 */
6e7b3309 984 for (j = 0; j < rules_to_fail; j++) {
7c2b3029 985 const char *rule = ref_rev_parse_rules[j];
7c2b3029
JK
986 char refname[PATH_MAX];
987
6e7b3309
BW
988 /* skip matched rule */
989 if (i == j)
990 continue;
991
7c2b3029
JK
992 /*
993 * the short name is ambiguous, if it resolves
994 * (with this previous rule) to a valid ref
995 * read_ref() returns 0 on success
996 */
997 mksnpath(refname, sizeof(refname),
998 rule, short_name_len, short_name);
c6893323 999 if (ref_exists(refname))
7c2b3029
JK
1000 break;
1001 }
1002
1003 /*
1004 * short name is non-ambiguous if all previous rules
1005 * haven't resolved to a valid ref
1006 */
6e7b3309 1007 if (j == rules_to_fail)
7c2b3029
JK
1008 return short_name;
1009 }
1010
1011 free(short_name);
dfefa935 1012 return xstrdup(refname);
7c2b3029 1013}
daebaa78
JH
1014
1015static struct string_list *hide_refs;
1016
1017int parse_hide_refs_config(const char *var, const char *value, const char *section)
1018{
1019 if (!strcmp("transfer.hiderefs", var) ||
1020 /* NEEDSWORK: use parse_config_key() once both are merged */
59556548 1021 (starts_with(var, section) && var[strlen(section)] == '.' &&
daebaa78
JH
1022 !strcmp(var + strlen(section), ".hiderefs"))) {
1023 char *ref;
1024 int len;
1025
1026 if (!value)
1027 return config_error_nonbool(var);
1028 ref = xstrdup(value);
1029 len = strlen(ref);
1030 while (len && ref[len - 1] == '/')
1031 ref[--len] = '\0';
1032 if (!hide_refs) {
1033 hide_refs = xcalloc(1, sizeof(*hide_refs));
1034 hide_refs->strdup_strings = 1;
1035 }
1036 string_list_append(hide_refs, ref);
1037 }
1038 return 0;
1039}
1040
78a766ab 1041int ref_is_hidden(const char *refname, const char *refname_full)
daebaa78 1042{
2bc31d16 1043 int i;
daebaa78
JH
1044
1045 if (!hide_refs)
1046 return 0;
2bc31d16
JK
1047 for (i = hide_refs->nr - 1; i >= 0; i--) {
1048 const char *match = hide_refs->items[i].string;
78a766ab 1049 const char *subject;
2bc31d16 1050 int neg = 0;
daebaa78 1051 int len;
2bc31d16
JK
1052
1053 if (*match == '!') {
1054 neg = 1;
1055 match++;
1056 }
1057
78a766ab
LF
1058 if (*match == '^') {
1059 subject = refname_full;
1060 match++;
1061 } else {
1062 subject = refname;
1063 }
1064
1065 /* refname can be NULL when namespaces are used. */
1066 if (!subject || !starts_with(subject, match))
daebaa78 1067 continue;
2bc31d16 1068 len = strlen(match);
78a766ab 1069 if (!subject[len] || subject[len] == '/')
2bc31d16 1070 return !neg;
daebaa78
JH
1071 }
1072 return 0;
1073}
fa5b1830 1074
0845122c
DT
1075const char *find_descendant_ref(const char *dirname,
1076 const struct string_list *extras,
1077 const struct string_list *skip)
fa5b1830 1078{
0845122c 1079 int pos;
fa5b1830 1080
0845122c
DT
1081 if (!extras)
1082 return NULL;
fa5b1830
MH
1083
1084 /*
0845122c
DT
1085 * Look at the place where dirname would be inserted into
1086 * extras. If there is an entry at that position that starts
1087 * with dirname (remember, dirname includes the trailing
1088 * slash) and is not in skip, then we have a conflict.
fa5b1830 1089 */
0845122c
DT
1090 for (pos = string_list_find_insert_index(extras, dirname, 0);
1091 pos < extras->nr; pos++) {
1092 const char *extra_refname = extras->items[pos].string;
fa5b1830 1093
0845122c
DT
1094 if (!starts_with(extra_refname, dirname))
1095 break;
1096
1097 if (!skip || !string_list_has_string(skip, extra_refname))
1098 return extra_refname;
fa5b1830 1099 }
0845122c
DT
1100 return NULL;
1101}
fa5b1830 1102
ff3a299c 1103int rename_ref_available(const char *old_refname, const char *new_refname)
0845122c
DT
1104{
1105 struct string_list skip = STRING_LIST_INIT_NODUP;
1106 struct strbuf err = STRBUF_INIT;
ff3a299c 1107 int ok;
fa5b1830 1108
ff3a299c
DT
1109 string_list_insert(&skip, old_refname);
1110 ok = !verify_refname_available(new_refname, NULL, &skip, &err);
1111 if (!ok)
0845122c
DT
1112 error("%s", err.buf);
1113
1114 string_list_clear(&skip, 0);
1115 strbuf_release(&err);
ff3a299c 1116 return ok;
fa5b1830 1117}
2bf68ed5
DT
1118
1119int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
1120{
1121 struct object_id oid;
1122 int flag;
1123
1124 if (submodule) {
1125 if (resolve_gitlink_ref(submodule, "HEAD", oid.hash) == 0)
1126 return fn("HEAD", &oid, 0, cb_data);
1127
1128 return 0;
1129 }
1130
1131 if (!read_ref_full("HEAD", RESOLVE_REF_READING, oid.hash, &flag))
1132 return fn("HEAD", &oid, flag, cb_data);
1133
1134 return 0;
1135}
1136
1137int head_ref(each_ref_fn fn, void *cb_data)
1138{
1139 return head_ref_submodule(NULL, fn, cb_data);
1140}
93770590 1141
4c4de895
MH
1142/*
1143 * Call fn for each reference in the specified submodule for which the
1144 * refname begins with prefix. If trim is non-zero, then trim that
1145 * many characters off the beginning of each refname before passing
1146 * the refname to fn. flags can be DO_FOR_EACH_INCLUDE_BROKEN to
1147 * include broken references in the iteration. If fn ever returns a
1148 * non-zero value, stop the iteration and return that value;
1149 * otherwise, return 0.
1150 */
1151static int do_for_each_ref(const char *submodule, const char *prefix,
1152 each_ref_fn fn, int trim, int flags, void *cb_data)
1153{
00eebe35 1154 struct ref_store *refs = get_ref_store(submodule);
4c4de895
MH
1155 struct ref_iterator *iter;
1156
00eebe35
MH
1157 if (!refs)
1158 return 0;
1159
4c4de895
MH
1160 iter = files_ref_iterator_begin(submodule, prefix, flags);
1161 iter = prefix_ref_iterator_begin(iter, prefix, trim);
1162
1163 return do_for_each_ref_iterator(iter, fn, cb_data);
1164}
1165
93770590
DT
1166int for_each_ref(each_ref_fn fn, void *cb_data)
1167{
1168 return do_for_each_ref(NULL, "", fn, 0, 0, cb_data);
1169}
1170
1171int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
1172{
1173 return do_for_each_ref(submodule, "", fn, 0, 0, cb_data);
1174}
1175
1176int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)
1177{
1178 return do_for_each_ref(NULL, prefix, fn, strlen(prefix), 0, cb_data);
1179}
1180
1181int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsigned int broken)
1182{
1183 unsigned int flag = 0;
1184
1185 if (broken)
1186 flag = DO_FOR_EACH_INCLUDE_BROKEN;
1187 return do_for_each_ref(NULL, prefix, fn, 0, flag, cb_data);
1188}
1189
1190int for_each_ref_in_submodule(const char *submodule, const char *prefix,
1191 each_ref_fn fn, void *cb_data)
1192{
1193 return do_for_each_ref(submodule, prefix, fn, strlen(prefix), 0, cb_data);
1194}
1195
1196int for_each_replace_ref(each_ref_fn fn, void *cb_data)
1197{
1198 return do_for_each_ref(NULL, git_replace_ref_base, fn,
1199 strlen(git_replace_ref_base), 0, cb_data);
1200}
1201
1202int for_each_namespaced_ref(each_ref_fn fn, void *cb_data)
1203{
1204 struct strbuf buf = STRBUF_INIT;
1205 int ret;
1206 strbuf_addf(&buf, "%srefs/", get_git_namespace());
1207 ret = do_for_each_ref(NULL, buf.buf, fn, 0, 0, cb_data);
1208 strbuf_release(&buf);
1209 return ret;
1210}
1211
1212int for_each_rawref(each_ref_fn fn, void *cb_data)
1213{
1214 return do_for_each_ref(NULL, "", fn, 0,
1215 DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
1216}
2d0663b2
DT
1217
1218/* This function needs to return a meaningful errno on failure */
bd40dcda
MH
1219static const char *resolve_ref_recursively(struct ref_store *refs,
1220 const char *refname,
1221 int resolve_flags,
1222 unsigned char *sha1, int *flags)
2d0663b2
DT
1223{
1224 static struct strbuf sb_refname = STRBUF_INIT;
1225 int unused_flags;
1226 int symref_count;
1227
1228 if (!flags)
1229 flags = &unused_flags;
1230
1231 *flags = 0;
1232
1233 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
1234 if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
1235 !refname_is_safe(refname)) {
1236 errno = EINVAL;
1237 return NULL;
1238 }
1239
1240 /*
1241 * dwim_ref() uses REF_ISBROKEN to distinguish between
1242 * missing refs and refs that were present but invalid,
1243 * to complain about the latter to stderr.
1244 *
1245 * We don't know whether the ref exists, so don't set
1246 * REF_ISBROKEN yet.
1247 */
1248 *flags |= REF_BAD_NAME;
1249 }
1250
1251 for (symref_count = 0; symref_count < SYMREF_MAXDEPTH; symref_count++) {
1252 unsigned int read_flags = 0;
1253
e1e33b72
MH
1254 if (refs->be->read_raw_ref(refs, refname,
1255 sha1, &sb_refname, &read_flags)) {
2d0663b2
DT
1256 *flags |= read_flags;
1257 if (errno != ENOENT || (resolve_flags & RESOLVE_REF_READING))
1258 return NULL;
1259 hashclr(sha1);
1260 if (*flags & REF_BAD_NAME)
1261 *flags |= REF_ISBROKEN;
1262 return refname;
1263 }
1264
1265 *flags |= read_flags;
1266
1267 if (!(read_flags & REF_ISSYMREF)) {
1268 if (*flags & REF_BAD_NAME) {
1269 hashclr(sha1);
1270 *flags |= REF_ISBROKEN;
1271 }
1272 return refname;
1273 }
1274
1275 refname = sb_refname.buf;
1276 if (resolve_flags & RESOLVE_REF_NO_RECURSE) {
1277 hashclr(sha1);
1278 return refname;
1279 }
1280 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
1281 if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
1282 !refname_is_safe(refname)) {
1283 errno = EINVAL;
1284 return NULL;
1285 }
1286
1287 *flags |= REF_ISBROKEN | REF_BAD_NAME;
1288 }
1289 }
1290
1291 errno = ELOOP;
1292 return NULL;
1293}
00eebe35 1294
bd40dcda
MH
1295const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
1296 unsigned char *sha1, int *flags)
1297{
1298 return resolve_ref_recursively(get_ref_store(NULL), refname,
1299 resolve_flags, sha1, flags);
1300}
1301
a8355bb7
MH
1302int resolve_gitlink_ref(const char *submodule, const char *refname,
1303 unsigned char *sha1)
424dcc76 1304{
a8355bb7 1305 size_t len = strlen(submodule);
424dcc76
MH
1306 struct ref_store *refs;
1307 int flags;
1308
a8355bb7 1309 while (len && submodule[len - 1] == '/')
424dcc76 1310 len--;
48a8475f 1311
424dcc76
MH
1312 if (!len)
1313 return -1;
1314
a8355bb7 1315 if (submodule[len]) {
48a8475f 1316 /* We need to strip off one or more trailing slashes */
a8355bb7 1317 char *stripped = xmemdupz(submodule, len);
48a8475f
MH
1318
1319 refs = get_ref_store(stripped);
1320 free(stripped);
1321 } else {
a8355bb7 1322 refs = get_ref_store(submodule);
48a8475f
MH
1323 }
1324
424dcc76
MH
1325 if (!refs)
1326 return -1;
1327
1328 if (!resolve_ref_recursively(refs, refname, 0, sha1, &flags) ||
1329 is_null_sha1(sha1))
1330 return -1;
1331 return 0;
1332}
1333
00eebe35
MH
1334/* A pointer to the ref_store for the main repository: */
1335static struct ref_store *main_ref_store;
1336
1337/* A linked list of ref_stores for submodules: */
1338static struct ref_store *submodule_ref_stores;
1339
1340void base_ref_store_init(struct ref_store *refs,
1341 const struct ref_storage_be *be,
1342 const char *submodule)
1343{
1344 refs->be = be;
1345 if (!submodule) {
1346 if (main_ref_store)
1347 die("BUG: main_ref_store initialized twice");
1348
1349 refs->submodule = "";
1350 refs->next = NULL;
1351 main_ref_store = refs;
1352 } else {
1353 if (lookup_ref_store(submodule))
1354 die("BUG: ref_store for submodule '%s' initialized twice",
1355 submodule);
1356
1357 refs->submodule = xstrdup(submodule);
1358 refs->next = submodule_ref_stores;
1359 submodule_ref_stores = refs;
1360 }
1361}
1362
1363struct ref_store *ref_store_init(const char *submodule)
1364{
1365 const char *be_name = "files";
1366 struct ref_storage_be *be = find_ref_storage_backend(be_name);
1367
1368 if (!be)
1369 die("BUG: reference backend %s is unknown", be_name);
1370
1371 if (!submodule || !*submodule)
1372 return be->init(NULL);
1373 else
1374 return be->init(submodule);
1375}
1376
1377struct ref_store *lookup_ref_store(const char *submodule)
1378{
1379 struct ref_store *refs;
1380
1381 if (!submodule || !*submodule)
1382 return main_ref_store;
1383
1384 for (refs = submodule_ref_stores; refs; refs = refs->next) {
1385 if (!strcmp(submodule, refs->submodule))
1386 return refs;
1387 }
1388
1389 return NULL;
1390}
1391
1392struct ref_store *get_ref_store(const char *submodule)
1393{
1394 struct ref_store *refs;
1395
1396 if (!submodule || !*submodule) {
1397 refs = lookup_ref_store(NULL);
1398
1399 if (!refs)
1400 refs = ref_store_init(NULL);
1401 } else {
1402 refs = lookup_ref_store(submodule);
1403
1404 if (!refs) {
1405 struct strbuf submodule_sb = STRBUF_INIT;
1406
1407 strbuf_addstr(&submodule_sb, submodule);
1408 if (is_nonbare_repository_dir(&submodule_sb))
1409 refs = ref_store_init(submodule);
1410 strbuf_release(&submodule_sb);
1411 }
1412 }
1413
1414 return refs;
1415}
1416
1417void assert_main_repository(struct ref_store *refs, const char *caller)
1418{
1419 if (*refs->submodule)
1420 die("BUG: %s called for a submodule", caller);
1421}
127b42a1
RS
1422
1423/* backend functions */
1424int ref_transaction_commit(struct ref_transaction *transaction,
1425 struct strbuf *err)
1426{
1427 struct ref_store *refs = get_ref_store(NULL);
1428
1429 return refs->be->transaction_commit(refs, transaction, err);
1430}
62665823
MH
1431
1432int verify_refname_available(const char *refname,
1433 const struct string_list *extra,
1434 const struct string_list *skip,
1435 struct strbuf *err)
1436{
1437 struct ref_store *refs = get_ref_store(NULL);
1438
1439 return refs->be->verify_refname_available(refs, refname, extra, skip, err);
1440}