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