blob: 91f0225931858b83765d5339cc242bb1c5b1dbc3 [file] [log] [blame]
Daniel Barkalow95fc7512005-06-06 16:31:29 -04001#include "cache.h"
Junio C Hamano85023572006-12-19 14:34:12 -08002#include "refs.h"
Junio C Hamanocf0adba2006-11-19 13:22:44 -08003#include "object.h"
4#include "tag.h"
Johannes Schindelin7155b722007-09-28 16:28:54 +01005#include "dir.h"
Daniel Barkalow95fc7512005-06-06 16:31:29 -04006
Michael Haggertybc5fd6d2012-04-10 07:30:13 +02007/*
8 * Make sure "ref" is something reasonable to have under ".git/refs/";
9 * We do not like it if:
10 *
11 * - any path component of it begins with ".", or
12 * - it has double dots "..", or
13 * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
14 * - it ends with a "/".
15 * - it ends with ".lock"
16 * - it contains a "\" (backslash)
17 */
Junio C Hamanof4204ab2006-11-21 23:36:35 -080018
Michael Haggertybc5fd6d2012-04-10 07:30:13 +020019/* Return true iff ch is not allowed in reference names. */
20static inline int bad_ref_char(int ch)
21{
22 if (((unsigned) ch) <= ' ' || ch == 0x7f ||
23 ch == '~' || ch == '^' || ch == ':' || ch == '\\')
24 return 1;
25 /* 2.13 Pattern Matching Notation */
26 if (ch == '*' || ch == '?' || ch == '[') /* Unsupported */
27 return 1;
28 return 0;
29}
30
31/*
32 * Try to read one refname component from the front of refname. Return
33 * the length of the component found, or -1 if the component is not
34 * legal.
35 */
36static int check_refname_component(const char *refname, int flags)
37{
38 const char *cp;
39 char last = '\0';
40
41 for (cp = refname; ; cp++) {
42 char ch = *cp;
43 if (ch == '\0' || ch == '/')
44 break;
45 if (bad_ref_char(ch))
46 return -1; /* Illegal character in refname. */
47 if (last == '.' && ch == '.')
48 return -1; /* Refname contains "..". */
49 if (last == '@' && ch == '{')
50 return -1; /* Refname contains "@{". */
51 last = ch;
52 }
53 if (cp == refname)
Michael Haggertydac529e2012-04-10 07:30:22 +020054 return 0; /* Component has zero length. */
Michael Haggertybc5fd6d2012-04-10 07:30:13 +020055 if (refname[0] == '.') {
56 if (!(flags & REFNAME_DOT_COMPONENT))
57 return -1; /* Component starts with '.'. */
58 /*
59 * Even if leading dots are allowed, don't allow "."
60 * as a component (".." is prevented by a rule above).
61 */
62 if (refname[1] == '\0')
63 return -1; /* Component equals ".". */
64 }
65 if (cp - refname >= 5 && !memcmp(cp - 5, ".lock", 5))
66 return -1; /* Refname ends with ".lock". */
67 return cp - refname;
68}
69
70int check_refname_format(const char *refname, int flags)
71{
72 int component_len, component_count = 0;
73
74 while (1) {
75 /* We are at the start of a path component. */
76 component_len = check_refname_component(refname, flags);
Michael Haggertydac529e2012-04-10 07:30:22 +020077 if (component_len <= 0) {
Michael Haggertybc5fd6d2012-04-10 07:30:13 +020078 if ((flags & REFNAME_REFSPEC_PATTERN) &&
79 refname[0] == '*' &&
80 (refname[1] == '\0' || refname[1] == '/')) {
81 /* Accept one wildcard as a full refname component. */
82 flags &= ~REFNAME_REFSPEC_PATTERN;
83 component_len = 1;
84 } else {
85 return -1;
86 }
87 }
88 component_count++;
89 if (refname[component_len] == '\0')
90 break;
91 /* Skip to next component. */
92 refname += component_len + 1;
93 }
94
95 if (refname[component_len - 1] == '.')
96 return -1; /* Refname ends with '.'. */
97 if (!(flags & REFNAME_ALLOW_ONELEVEL) && component_count < 2)
98 return -1; /* Refname has only one component. */
99 return 0;
100}
101
102struct ref_entry;
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700103
Michael Haggerty593f1bb2012-04-10 07:30:23 +0200104struct ref_value {
105 unsigned char sha1[20];
106 unsigned char peeled[20];
107};
108
Michael Haggertyd3177272012-04-10 07:30:24 +0200109struct ref_dir {
Julian Phillipse9c4c112011-09-29 23:11:42 +0100110 int nr, alloc;
Michael Haggertye6ed3ca2012-01-17 06:50:32 +0100111
112 /*
113 * Entries with index 0 <= i < sorted are sorted by name. New
114 * entries are appended to the list unsorted, and are sorted
115 * only when required; thus we avoid the need to sort the list
116 * after the addition of every reference.
117 */
118 int sorted;
119
Michael Haggertyd3177272012-04-10 07:30:24 +0200120 struct ref_entry **entries;
Julian Phillipse9c4c112011-09-29 23:11:42 +0100121};
122
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200123/* ISSYMREF=0x01, ISPACKED=0x02 and ISBROKEN=0x04 are public interfaces */
124#define REF_KNOWS_PEELED 0x10
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700125
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200126struct ref_entry {
127 unsigned char flag; /* ISSYMREF? ISPACKED? */
Michael Haggerty593f1bb2012-04-10 07:30:23 +0200128 union {
129 struct ref_value value;
130 } u;
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200131 /* The full name of the reference (e.g., "refs/heads/master"): */
132 char name[FLEX_ARRAY];
133};
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700134
Michael Haggertycddc4252011-12-12 06:38:22 +0100135static struct ref_entry *create_ref_entry(const char *refname,
136 const unsigned char *sha1, int flag,
137 int check_name)
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700138{
139 int len;
Michael Haggertycddc4252011-12-12 06:38:22 +0100140 struct ref_entry *ref;
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700141
Junio C Hamano09116a12011-11-16 16:54:32 -0800142 if (check_name &&
Michael Haggertydfefa932011-12-12 06:38:09 +0100143 check_refname_format(refname, REFNAME_ALLOW_ONELEVEL|REFNAME_DOT_COMPONENT))
144 die("Reference has invalid format: '%s'", refname);
Michael Haggertycddc4252011-12-12 06:38:22 +0100145 len = strlen(refname) + 1;
146 ref = xmalloc(sizeof(struct ref_entry) + len);
Michael Haggerty593f1bb2012-04-10 07:30:23 +0200147 hashcpy(ref->u.value.sha1, sha1);
148 hashclr(ref->u.value.peeled);
Michael Haggertycddc4252011-12-12 06:38:22 +0100149 memcpy(ref->name, refname, len);
150 ref->flag = flag;
151 return ref;
152}
153
Michael Haggerty732134e2012-04-10 07:30:21 +0200154static void free_ref_entry(struct ref_entry *entry)
155{
156 free(entry);
157}
158
Michael Haggertyd3177272012-04-10 07:30:24 +0200159/* Add a ref_entry to the end of the ref_dir (unsorted). */
160static void add_ref(struct ref_dir *refs, struct ref_entry *ref)
Michael Haggertycddc4252011-12-12 06:38:22 +0100161{
Michael Haggertyd3177272012-04-10 07:30:24 +0200162 ALLOC_GROW(refs->entries, refs->nr + 1, refs->alloc);
163 refs->entries[refs->nr++] = ref;
Julian Phillipsc774aab2007-04-17 02:42:50 +0100164}
165
Michael Haggertyd3177272012-04-10 07:30:24 +0200166static void clear_ref_dir(struct ref_dir *dir)
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200167{
168 int i;
Michael Haggertyd3177272012-04-10 07:30:24 +0200169 for (i = 0; i < dir->nr; i++)
170 free_ref_entry(dir->entries[i]);
171 free(dir->entries);
172 dir->sorted = dir->nr = dir->alloc = 0;
173 dir->entries = NULL;
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200174}
175
Julian Phillipse9c4c112011-09-29 23:11:42 +0100176static int ref_entry_cmp(const void *a, const void *b)
Julian Phillipsc774aab2007-04-17 02:42:50 +0100177{
Julian Phillipse9c4c112011-09-29 23:11:42 +0100178 struct ref_entry *one = *(struct ref_entry **)a;
179 struct ref_entry *two = *(struct ref_entry **)b;
180 return strcmp(one->name, two->name);
181}
Julian Phillipsc774aab2007-04-17 02:42:50 +0100182
Michael Haggertyd3177272012-04-10 07:30:24 +0200183static void sort_ref_dir(struct ref_dir *dir);
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200184
Michael Haggertyd3177272012-04-10 07:30:24 +0200185static struct ref_entry *search_ref_dir(struct ref_dir *dir, const char *refname)
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200186{
187 struct ref_entry *e, **r;
188 int len;
189
190 if (refname == NULL)
191 return NULL;
192
Michael Haggertyd3177272012-04-10 07:30:24 +0200193 if (!dir->nr)
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200194 return NULL;
Michael Haggertyd3177272012-04-10 07:30:24 +0200195 sort_ref_dir(dir);
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200196 len = strlen(refname) + 1;
197 e = xmalloc(sizeof(struct ref_entry) + len);
198 memcpy(e->name, refname, len);
199
Michael Haggertyd3177272012-04-10 07:30:24 +0200200 r = bsearch(&e, dir->entries, dir->nr, sizeof(*dir->entries), ref_entry_cmp);
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200201
202 free(e);
203
204 if (r == NULL)
205 return NULL;
206
207 return *r;
208}
209
Michael Haggerty202a56a2011-12-12 06:38:15 +0100210/*
211 * Emit a warning and return true iff ref1 and ref2 have the same name
212 * and the same sha1. Die if they have the same name but different
213 * sha1s.
214 */
215static int is_dup_ref(const struct ref_entry *ref1, const struct ref_entry *ref2)
216{
217 if (!strcmp(ref1->name, ref2->name)) {
218 /* Duplicate name; make sure that the SHA1s match: */
Michael Haggerty593f1bb2012-04-10 07:30:23 +0200219 if (hashcmp(ref1->u.value.sha1, ref2->u.value.sha1))
Michael Haggerty202a56a2011-12-12 06:38:15 +0100220 die("Duplicated ref, and SHA1s don't match: %s",
221 ref1->name);
222 warning("Duplicated ref: %s", ref1->name);
223 return 1;
224 } else {
225 return 0;
226 }
227}
228
Michael Haggertye6ed3ca2012-01-17 06:50:32 +0100229/*
Michael Haggerty81a79d82012-04-10 07:30:25 +0200230 * Sort the entries in dir (if they are not already sorted)
231 * and remove any duplicate entries.
Michael Haggertye6ed3ca2012-01-17 06:50:32 +0100232 */
Michael Haggertyd3177272012-04-10 07:30:24 +0200233static void sort_ref_dir(struct ref_dir *dir)
Julian Phillipse9c4c112011-09-29 23:11:42 +0100234{
Michael Haggerty202a56a2011-12-12 06:38:15 +0100235 int i, j;
Michael Haggerty81a79d82012-04-10 07:30:25 +0200236 struct ref_entry *last = NULL;
Julian Phillipsc774aab2007-04-17 02:42:50 +0100237
Michael Haggertye6ed3ca2012-01-17 06:50:32 +0100238 /*
239 * This check also prevents passing a zero-length array to qsort(),
240 * which is a problem on some platforms.
241 */
Michael Haggertyd3177272012-04-10 07:30:24 +0200242 if (dir->sorted == dir->nr)
Julian Phillipse9c4c112011-09-29 23:11:42 +0100243 return;
Julian Phillipsc774aab2007-04-17 02:42:50 +0100244
Michael Haggertyd3177272012-04-10 07:30:24 +0200245 qsort(dir->entries, dir->nr, sizeof(*dir->entries), ref_entry_cmp);
Julian Phillipsc774aab2007-04-17 02:42:50 +0100246
Michael Haggerty81a79d82012-04-10 07:30:25 +0200247 /* Remove any duplicates: */
248 for (i = 0, j = 0; j < dir->nr; j++) {
249 struct ref_entry *entry = dir->entries[j];
250 if (last && is_dup_ref(last, entry))
251 free_ref_entry(entry);
252 else
253 last = dir->entries[i++] = entry;
Julian Phillipse9c4c112011-09-29 23:11:42 +0100254 }
Michael Haggerty81a79d82012-04-10 07:30:25 +0200255 dir->sorted = dir->nr = i;
Julian Phillipse9c4c112011-09-29 23:11:42 +0100256}
Julian Phillipsc774aab2007-04-17 02:42:50 +0100257
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200258#define DO_FOR_EACH_INCLUDE_BROKEN 01
259
260static struct ref_entry *current_ref;
261
262static int do_one_ref(const char *base, each_ref_fn fn, int trim,
263 int flags, void *cb_data, struct ref_entry *entry)
Julian Phillipse9c4c112011-09-29 23:11:42 +0100264{
Michael Haggerty429213e2012-04-10 07:30:14 +0200265 int retval;
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200266 if (prefixcmp(entry->name, base))
267 return 0;
Julian Phillipsc774aab2007-04-17 02:42:50 +0100268
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200269 if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN)) {
270 if (entry->flag & REF_ISBROKEN)
271 return 0; /* ignore broken refs e.g. dangling symref */
Michael Haggerty593f1bb2012-04-10 07:30:23 +0200272 if (!has_sha1_file(entry->u.value.sha1)) {
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200273 error("%s does not point to a valid object!", entry->name);
274 return 0;
275 }
276 }
277 current_ref = entry;
Michael Haggerty593f1bb2012-04-10 07:30:23 +0200278 retval = fn(entry->name + trim, entry->u.value.sha1, entry->flag, cb_data);
Michael Haggerty429213e2012-04-10 07:30:14 +0200279 current_ref = NULL;
280 return retval;
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200281}
Julian Phillipsc774aab2007-04-17 02:42:50 +0100282
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200283/*
Michael Haggertyd3177272012-04-10 07:30:24 +0200284 * Call fn for each reference in dir that has index in the range
285 * offset <= index < dir->nr. This function does not sort the dir;
286 * sorting should be done by the caller.
Michael Haggertyc36b5bc2012-04-10 07:30:15 +0200287 */
Michael Haggertyd3177272012-04-10 07:30:24 +0200288static int do_for_each_ref_in_dir(struct ref_dir *dir, int offset,
289 const char *base,
290 each_ref_fn fn, int trim, int flags, void *cb_data)
Michael Haggertyc36b5bc2012-04-10 07:30:15 +0200291{
292 int i;
Michael Haggertyd3177272012-04-10 07:30:24 +0200293 assert(dir->sorted == dir->nr);
294 for (i = offset; i < dir->nr; i++) {
295 int retval = do_one_ref(base, fn, trim, flags, cb_data, dir->entries[i]);
Michael Haggertyc36b5bc2012-04-10 07:30:15 +0200296 if (retval)
297 return retval;
298 }
299 return 0;
300}
301
302/*
Michael Haggertyd3177272012-04-10 07:30:24 +0200303 * Call fn for each reference in the union of dir1 and dir2, in order
304 * by refname. If an entry appears in both dir1 and dir2, then only
305 * process the version that is in dir2. The input dirs must already
306 * be sorted.
Michael Haggertyb3fd0602012-04-10 07:30:16 +0200307 */
Michael Haggertyd3177272012-04-10 07:30:24 +0200308static int do_for_each_ref_in_dirs(struct ref_dir *dir1,
309 struct ref_dir *dir2,
310 const char *base, each_ref_fn fn, int trim,
311 int flags, void *cb_data)
Michael Haggertyb3fd0602012-04-10 07:30:16 +0200312{
313 int retval;
314 int i1 = 0, i2 = 0;
315
Michael Haggertyd3177272012-04-10 07:30:24 +0200316 assert(dir1->sorted == dir1->nr);
317 assert(dir2->sorted == dir2->nr);
318 while (i1 < dir1->nr && i2 < dir2->nr) {
319 struct ref_entry *e1 = dir1->entries[i1];
320 struct ref_entry *e2 = dir2->entries[i2];
Michael Haggertyb3fd0602012-04-10 07:30:16 +0200321 int cmp = strcmp(e1->name, e2->name);
322 if (cmp < 0) {
323 retval = do_one_ref(base, fn, trim, flags, cb_data, e1);
324 i1++;
325 } else {
326 retval = do_one_ref(base, fn, trim, flags, cb_data, e2);
327 i2++;
328 if (cmp == 0) {
329 /*
330 * There was a ref in array1 with the
331 * same name; ignore it.
332 */
333 i1++;
334 }
335 }
336 if (retval)
337 return retval;
338 }
Michael Haggertyd3177272012-04-10 07:30:24 +0200339 if (i1 < dir1->nr)
340 return do_for_each_ref_in_dir(dir1, i1,
341 base, fn, trim, flags, cb_data);
342 if (i2 < dir2->nr)
343 return do_for_each_ref_in_dir(dir2, i2,
344 base, fn, trim, flags, cb_data);
Michael Haggertyb3fd0602012-04-10 07:30:16 +0200345 return 0;
346}
347
348/*
Michael Haggertyd66da472012-04-10 07:30:17 +0200349 * Return true iff refname1 and refname2 conflict with each other.
350 * Two reference names conflict if one of them exactly matches the
351 * leading components of the other; e.g., "foo/bar" conflicts with
352 * both "foo" and with "foo/bar/baz" but not with "foo/bar" or
353 * "foo/barbados".
354 */
355static int names_conflict(const char *refname1, const char *refname2)
356{
Michael Haggerty5a4d4942012-04-10 07:30:19 +0200357 for (; *refname1 && *refname1 == *refname2; refname1++, refname2++)
358 ;
359 return (*refname1 == '\0' && *refname2 == '/')
360 || (*refname1 == '/' && *refname2 == '\0');
361}
Michael Haggertyd66da472012-04-10 07:30:17 +0200362
Michael Haggerty5a4d4942012-04-10 07:30:19 +0200363struct name_conflict_cb {
364 const char *refname;
365 const char *oldrefname;
366 const char *conflicting_refname;
367};
368
369static int name_conflict_fn(const char *existingrefname, const unsigned char *sha1,
370 int flags, void *cb_data)
371{
372 struct name_conflict_cb *data = (struct name_conflict_cb *)cb_data;
373 if (data->oldrefname && !strcmp(data->oldrefname, existingrefname))
374 return 0;
375 if (names_conflict(data->refname, existingrefname)) {
376 data->conflicting_refname = existingrefname;
377 return 1;
Michael Haggertyd66da472012-04-10 07:30:17 +0200378 }
Michael Haggerty5a4d4942012-04-10 07:30:19 +0200379 return 0;
Michael Haggertyd66da472012-04-10 07:30:17 +0200380}
381
382/*
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200383 * Return true iff a reference named refname could be created without
Michael Haggerty5a4d4942012-04-10 07:30:19 +0200384 * conflicting with the name of an existing reference in array. If
385 * oldrefname is non-NULL, ignore potential conflicts with oldrefname
386 * (e.g., because oldrefname is scheduled for deletion in the same
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200387 * operation).
388 */
389static int is_refname_available(const char *refname, const char *oldrefname,
Michael Haggertyd3177272012-04-10 07:30:24 +0200390 struct ref_dir *dir)
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200391{
Michael Haggerty5a4d4942012-04-10 07:30:19 +0200392 struct name_conflict_cb data;
393 data.refname = refname;
394 data.oldrefname = oldrefname;
395 data.conflicting_refname = NULL;
396
Michael Haggertyd3177272012-04-10 07:30:24 +0200397 sort_ref_dir(dir);
398 if (do_for_each_ref_in_dir(dir, 0, "", name_conflict_fn,
399 0, DO_FOR_EACH_INCLUDE_BROKEN,
400 &data)) {
Michael Haggerty5a4d4942012-04-10 07:30:19 +0200401 error("'%s' exists; cannot create '%s'",
402 data.conflicting_refname, refname);
403 return 0;
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200404 }
405 return 1;
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700406}
407
Junio C Hamano5e290ff2006-09-30 12:37:37 -0700408/*
409 * Future: need to be in "struct repository"
410 * when doing a full libification.
411 */
Michael Haggerty79c7ca52011-10-17 04:38:05 +0200412static struct ref_cache {
413 struct ref_cache *next;
Junio C Hamano5e290ff2006-09-30 12:37:37 -0700414 char did_loose;
415 char did_packed;
Michael Haggertyd3177272012-04-10 07:30:24 +0200416 struct ref_dir loose;
417 struct ref_dir packed;
Michael Haggertyce409792011-08-13 00:36:28 +0200418 /* The submodule name, or "" for the main repo. */
419 char name[FLEX_ARRAY];
Michael Haggerty79c7ca52011-10-17 04:38:05 +0200420} *ref_cache;
Michael Haggerty0e88c132011-08-13 00:36:29 +0200421
Michael Haggerty760c4512011-10-17 04:38:09 +0200422static void clear_packed_ref_cache(struct ref_cache *refs)
Junio C Hamano5e290ff2006-09-30 12:37:37 -0700423{
Michael Haggerty760c4512011-10-17 04:38:09 +0200424 if (refs->did_packed)
Michael Haggertyd3177272012-04-10 07:30:24 +0200425 clear_ref_dir(&refs->packed);
Michael Haggerty760c4512011-10-17 04:38:09 +0200426 refs->did_packed = 0;
Junio C Hamano5e290ff2006-09-30 12:37:37 -0700427}
428
Michael Haggerty760c4512011-10-17 04:38:09 +0200429static void clear_loose_ref_cache(struct ref_cache *refs)
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700430{
Michael Haggerty1b7edaf2011-10-17 04:38:08 +0200431 if (refs->did_loose)
Michael Haggertyd3177272012-04-10 07:30:24 +0200432 clear_ref_dir(&refs->loose);
Michael Haggerty760c4512011-10-17 04:38:09 +0200433 refs->did_loose = 0;
434}
435
Michael Haggerty79c7ca52011-10-17 04:38:05 +0200436static struct ref_cache *create_ref_cache(const char *submodule)
Michael Haggertye5dbf602011-08-13 00:36:27 +0200437{
Michael Haggertyce409792011-08-13 00:36:28 +0200438 int len;
Michael Haggerty79c7ca52011-10-17 04:38:05 +0200439 struct ref_cache *refs;
Michael Haggertyce409792011-08-13 00:36:28 +0200440 if (!submodule)
441 submodule = "";
442 len = strlen(submodule) + 1;
Michael Haggerty79c7ca52011-10-17 04:38:05 +0200443 refs = xcalloc(1, sizeof(struct ref_cache) + len);
Michael Haggertyce409792011-08-13 00:36:28 +0200444 memcpy(refs->name, submodule, len);
Michael Haggertye5dbf602011-08-13 00:36:27 +0200445 return refs;
446}
447
Michael Haggerty4349a662011-08-13 00:36:25 +0200448/*
Michael Haggerty79c7ca52011-10-17 04:38:05 +0200449 * Return a pointer to a ref_cache for the specified submodule. For
Michael Haggerty4349a662011-08-13 00:36:25 +0200450 * the main repository, use submodule==NULL. The returned structure
451 * will be allocated and initialized but not necessarily populated; it
452 * should not be freed.
453 */
Michael Haggerty79c7ca52011-10-17 04:38:05 +0200454static struct ref_cache *get_ref_cache(const char *submodule)
Michael Haggerty4349a662011-08-13 00:36:25 +0200455{
Michael Haggerty79c7ca52011-10-17 04:38:05 +0200456 struct ref_cache *refs = ref_cache;
Michael Haggerty0e88c132011-08-13 00:36:29 +0200457 if (!submodule)
458 submodule = "";
459 while (refs) {
460 if (!strcmp(submodule, refs->name))
461 return refs;
462 refs = refs->next;
Michael Haggerty4349a662011-08-13 00:36:25 +0200463 }
Michael Haggerty0e88c132011-08-13 00:36:29 +0200464
Michael Haggerty79c7ca52011-10-17 04:38:05 +0200465 refs = create_ref_cache(submodule);
466 refs->next = ref_cache;
467 ref_cache = refs;
Michael Haggerty0e88c132011-08-13 00:36:29 +0200468 return refs;
Michael Haggerty4349a662011-08-13 00:36:25 +0200469}
470
Michael Haggerty8be8bde2011-10-17 04:38:07 +0200471void invalidate_ref_cache(const char *submodule)
Michael Haggertyf130b112011-08-13 00:36:24 +0200472{
Michael Haggertyc5f29ab2011-10-17 04:38:11 +0200473 struct ref_cache *refs = get_ref_cache(submodule);
474 clear_packed_ref_cache(refs);
475 clear_loose_ref_cache(refs);
Michael Haggertyf130b112011-08-13 00:36:24 +0200476}
477
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200478/*
479 * Parse one line from a packed-refs file. Write the SHA1 to sha1.
480 * Return a pointer to the refname within the line (null-terminated),
481 * or NULL if there was a problem.
482 */
483static const char *parse_ref_line(char *line, unsigned char *sha1)
484{
485 /*
486 * 42: the answer to everything.
487 *
488 * In this case, it happens to be the answer to
489 * 40 (length of sha1 hex representation)
490 * +1 (space in between hex and name)
491 * +1 (newline at the end of the line)
492 */
493 int len = strlen(line) - 42;
494
495 if (len <= 0)
496 return NULL;
497 if (get_sha1_hex(line, sha1) < 0)
498 return NULL;
499 if (!isspace(line[40]))
500 return NULL;
501 line += 41;
502 if (isspace(*line))
503 return NULL;
504 if (line[len] != '\n')
505 return NULL;
506 line[len] = 0;
507
508 return line;
509}
510
Michael Haggertyd3177272012-04-10 07:30:24 +0200511static void read_packed_refs(FILE *f, struct ref_dir *dir)
Junio C Hamanof4204ab2006-11-21 23:36:35 -0800512{
Julian Phillipse9c4c112011-09-29 23:11:42 +0100513 struct ref_entry *last = NULL;
Junio C Hamanof4204ab2006-11-21 23:36:35 -0800514 char refline[PATH_MAX];
515 int flag = REF_ISPACKED;
516
517 while (fgets(refline, sizeof(refline), f)) {
518 unsigned char sha1[20];
Michael Haggertydfefa932011-12-12 06:38:09 +0100519 const char *refname;
Junio C Hamanof4204ab2006-11-21 23:36:35 -0800520 static const char header[] = "# pack-refs with:";
521
522 if (!strncmp(refline, header, sizeof(header)-1)) {
523 const char *traits = refline + sizeof(header) - 1;
524 if (strstr(traits, " peeled "))
525 flag |= REF_KNOWS_PEELED;
526 /* perhaps other traits later as well */
527 continue;
528 }
529
Michael Haggertydfefa932011-12-12 06:38:09 +0100530 refname = parse_ref_line(refline, sha1);
531 if (refname) {
Michael Haggertydd73ecd2011-12-12 06:38:23 +0100532 last = create_ref_entry(refname, sha1, flag, 1);
Michael Haggertyd3177272012-04-10 07:30:24 +0200533 add_ref(dir, last);
Junio C Hamanof4204ab2006-11-21 23:36:35 -0800534 continue;
535 }
536 if (last &&
537 refline[0] == '^' &&
538 strlen(refline) == 42 &&
539 refline[41] == '\n' &&
540 !get_sha1_hex(refline + 1, sha1))
Michael Haggerty593f1bb2012-04-10 07:30:23 +0200541 hashcpy(last->u.value.peeled, sha1);
Junio C Hamanof4204ab2006-11-21 23:36:35 -0800542 }
Junio C Hamanof4204ab2006-11-21 23:36:35 -0800543}
544
Michael Haggertyd3177272012-04-10 07:30:24 +0200545static struct ref_dir *get_packed_refs(struct ref_cache *refs)
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700546{
Michael Haggerty4349a662011-08-13 00:36:25 +0200547 if (!refs->did_packed) {
548 const char *packed_refs_file;
549 FILE *f;
Heiko Voigt0bad6112010-07-07 15:39:11 +0200550
Michael Haggerty316b0972011-12-12 06:38:16 +0100551 if (*refs->name)
552 packed_refs_file = git_path_submodule(refs->name, "packed-refs");
Michael Haggerty4349a662011-08-13 00:36:25 +0200553 else
554 packed_refs_file = git_path("packed-refs");
555 f = fopen(packed_refs_file, "r");
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700556 if (f) {
Junio C Hamano2c5c66b2011-10-10 15:56:19 -0700557 read_packed_refs(f, &refs->packed);
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700558 fclose(f);
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700559 }
Heiko Voigt0bad6112010-07-07 15:39:11 +0200560 refs->did_packed = 1;
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700561 }
Julian Phillipse9c4c112011-09-29 23:11:42 +0100562 return &refs->packed;
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700563}
564
Michael Haggerty30249ee2012-01-17 06:50:33 +0100565void add_packed_ref(const char *refname, const unsigned char *sha1)
566{
567 add_ref(get_packed_refs(get_ref_cache(NULL)),
568 create_ref_entry(refname, sha1, REF_ISPACKED, 1));
569}
570
Michael Haggerty3b124822011-12-12 06:38:17 +0100571static void get_ref_dir(struct ref_cache *refs, const char *base,
Michael Haggertyd3177272012-04-10 07:30:24 +0200572 struct ref_dir *dir)
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700573{
Michael Haggertyd3177272012-04-10 07:30:24 +0200574 DIR *d;
Heiko Voigt0bad6112010-07-07 15:39:11 +0200575 const char *path;
576
Michael Haggerty3b124822011-12-12 06:38:17 +0100577 if (*refs->name)
578 path = git_path_submodule(refs->name, "%s", base);
Heiko Voigt0bad6112010-07-07 15:39:11 +0200579 else
580 path = git_path("%s", base);
581
Michael Haggertyd3177272012-04-10 07:30:24 +0200582 d = opendir(path);
583 if (d) {
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700584 struct dirent *de;
585 int baselen = strlen(base);
Michael Haggertydfefa932011-12-12 06:38:09 +0100586 char *refname = xmalloc(baselen + 257);
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700587
Michael Haggertydfefa932011-12-12 06:38:09 +0100588 memcpy(refname, base, baselen);
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700589 if (baselen && base[baselen-1] != '/')
Michael Haggertydfefa932011-12-12 06:38:09 +0100590 refname[baselen++] = '/';
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700591
Michael Haggertyd3177272012-04-10 07:30:24 +0200592 while ((de = readdir(d)) != NULL) {
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700593 unsigned char sha1[20];
594 struct stat st;
Junio C Hamano8da19772006-09-20 22:02:01 -0700595 int flag;
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700596 int namelen;
Heiko Voigt0bad6112010-07-07 15:39:11 +0200597 const char *refdir;
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700598
599 if (de->d_name[0] == '.')
600 continue;
601 namelen = strlen(de->d_name);
602 if (namelen > 255)
603 continue;
604 if (has_extension(de->d_name, ".lock"))
605 continue;
Michael Haggertydfefa932011-12-12 06:38:09 +0100606 memcpy(refname + baselen, de->d_name, namelen+1);
Michael Haggerty3b124822011-12-12 06:38:17 +0100607 refdir = *refs->name
608 ? git_path_submodule(refs->name, "%s", refname)
Michael Haggertydfefa932011-12-12 06:38:09 +0100609 : git_path("%s", refname);
Heiko Voigt0bad6112010-07-07 15:39:11 +0200610 if (stat(refdir, &st) < 0)
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700611 continue;
612 if (S_ISDIR(st.st_mode)) {
Michael Haggertyd3177272012-04-10 07:30:24 +0200613 get_ref_dir(refs, refname, dir);
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700614 continue;
615 }
Michael Haggerty3b124822011-12-12 06:38:17 +0100616 if (*refs->name) {
Junio C Hamanof8948e22009-02-08 23:27:10 -0800617 hashclr(sha1);
Heiko Voigt0bad6112010-07-07 15:39:11 +0200618 flag = 0;
Michael Haggerty3b124822011-12-12 06:38:17 +0100619 if (resolve_gitlink_ref(refs->name, refname, sha1) < 0) {
Heiko Voigt0bad6112010-07-07 15:39:11 +0200620 hashclr(sha1);
Junio C Hamano98ac34b2011-10-19 13:45:50 -0700621 flag |= REF_ISBROKEN;
Heiko Voigt0bad6112010-07-07 15:39:11 +0200622 }
Michael Haggertydfefa932011-12-12 06:38:09 +0100623 } else if (read_ref_full(refname, sha1, 1, &flag)) {
Junio C Hamano09116a12011-11-16 16:54:32 -0800624 hashclr(sha1);
625 flag |= REF_ISBROKEN;
626 }
Michael Haggertyd3177272012-04-10 07:30:24 +0200627 add_ref(dir, create_ref_entry(refname, sha1, flag, 1));
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700628 }
Michael Haggertydfefa932011-12-12 06:38:09 +0100629 free(refname);
Michael Haggertyd3177272012-04-10 07:30:24 +0200630 closedir(d);
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700631 }
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700632}
633
Michael Haggertyd3177272012-04-10 07:30:24 +0200634static struct ref_dir *get_loose_refs(struct ref_cache *refs)
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700635{
Michael Haggerty4349a662011-08-13 00:36:25 +0200636 if (!refs->did_loose) {
Michael Haggerty3b124822011-12-12 06:38:17 +0100637 get_ref_dir(refs, "refs", &refs->loose);
Michael Haggerty4349a662011-08-13 00:36:25 +0200638 refs->did_loose = 1;
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700639 }
Junio C Hamano2c5c66b2011-10-10 15:56:19 -0700640 return &refs->loose;
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700641}
642
Linus Torvaldsca8db142005-09-25 09:59:37 -0700643/* We allow "recursive" symbolic refs. Only within reason, though */
644#define MAXDEPTH 5
Linus Torvalds0ebde322007-04-09 21:14:26 -0700645#define MAXREFLEN (1024)
646
Junio C Hamanoe5fa45c2011-10-17 11:43:30 -0700647/*
648 * Called by resolve_gitlink_ref_recursive() after it failed to read
Michael Haggertyb0626602011-12-12 06:38:19 +0100649 * from the loose refs in ref_cache refs. Find <refname> in the
650 * packed-refs file for the submodule.
Junio C Hamanoe5fa45c2011-10-17 11:43:30 -0700651 */
Michael Haggertyb0626602011-12-12 06:38:19 +0100652static int resolve_gitlink_packed_ref(struct ref_cache *refs,
Michael Haggerty85be1fe2011-12-12 06:38:10 +0100653 const char *refname, unsigned char *sha1)
Linus Torvalds0ebde322007-04-09 21:14:26 -0700654{
Junio C Hamano2c5c66b2011-10-10 15:56:19 -0700655 struct ref_entry *ref;
Michael Haggertyd3177272012-04-10 07:30:24 +0200656 struct ref_dir *dir = get_packed_refs(refs);
Linus Torvalds0ebde322007-04-09 21:14:26 -0700657
Michael Haggertyd3177272012-04-10 07:30:24 +0200658 ref = search_ref_dir(dir, refname);
Michael Haggertyb0626602011-12-12 06:38:19 +0100659 if (ref == NULL)
660 return -1;
661
Michael Haggerty593f1bb2012-04-10 07:30:23 +0200662 memcpy(sha1, ref->u.value.sha1, 20);
Michael Haggertyb0626602011-12-12 06:38:19 +0100663 return 0;
Linus Torvalds0ebde322007-04-09 21:14:26 -0700664}
665
Michael Haggertyb0626602011-12-12 06:38:19 +0100666static int resolve_gitlink_ref_recursive(struct ref_cache *refs,
Michael Haggerty85be1fe2011-12-12 06:38:10 +0100667 const char *refname, unsigned char *sha1,
Michael Haggertydfefa932011-12-12 06:38:09 +0100668 int recursion)
Linus Torvalds0ebde322007-04-09 21:14:26 -0700669{
Michael Haggerty064d51d2011-12-12 06:38:20 +0100670 int fd, len;
Linus Torvalds0ebde322007-04-09 21:14:26 -0700671 char buffer[128], *p;
Michael Haggerty064d51d2011-12-12 06:38:20 +0100672 char *path;
Linus Torvalds0ebde322007-04-09 21:14:26 -0700673
Michael Haggerty064d51d2011-12-12 06:38:20 +0100674 if (recursion > MAXDEPTH || strlen(refname) > MAXREFLEN)
Linus Torvalds0ebde322007-04-09 21:14:26 -0700675 return -1;
Michael Haggerty064d51d2011-12-12 06:38:20 +0100676 path = *refs->name
677 ? git_path_submodule(refs->name, "%s", refname)
678 : git_path("%s", refname);
679 fd = open(path, O_RDONLY);
Linus Torvalds0ebde322007-04-09 21:14:26 -0700680 if (fd < 0)
Michael Haggertyb0626602011-12-12 06:38:19 +0100681 return resolve_gitlink_packed_ref(refs, refname, sha1);
Linus Torvalds0ebde322007-04-09 21:14:26 -0700682
683 len = read(fd, buffer, sizeof(buffer)-1);
684 close(fd);
685 if (len < 0)
686 return -1;
687 while (len && isspace(buffer[len-1]))
688 len--;
689 buffer[len] = 0;
690
691 /* Was it a detached head or an old-fashioned symlink? */
Michael Haggerty85be1fe2011-12-12 06:38:10 +0100692 if (!get_sha1_hex(buffer, sha1))
Linus Torvalds0ebde322007-04-09 21:14:26 -0700693 return 0;
694
695 /* Symref? */
696 if (strncmp(buffer, "ref:", 4))
697 return -1;
698 p = buffer + 4;
699 while (isspace(*p))
700 p++;
701
Michael Haggerty064d51d2011-12-12 06:38:20 +0100702 return resolve_gitlink_ref_recursive(refs, p, sha1, recursion+1);
Linus Torvalds0ebde322007-04-09 21:14:26 -0700703}
704
Michael Haggerty85be1fe2011-12-12 06:38:10 +0100705int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1)
Linus Torvalds0ebde322007-04-09 21:14:26 -0700706{
707 int len = strlen(path), retval;
Michael Haggerty064d51d2011-12-12 06:38:20 +0100708 char *submodule;
Michael Haggertyb0626602011-12-12 06:38:19 +0100709 struct ref_cache *refs;
Linus Torvalds0ebde322007-04-09 21:14:26 -0700710
711 while (len && path[len-1] == '/')
712 len--;
713 if (!len)
714 return -1;
Michael Haggertyb0626602011-12-12 06:38:19 +0100715 submodule = xstrndup(path, len);
716 refs = get_ref_cache(submodule);
717 free(submodule);
Linus Torvalds0ebde322007-04-09 21:14:26 -0700718
Michael Haggerty064d51d2011-12-12 06:38:20 +0100719 retval = resolve_gitlink_ref_recursive(refs, refname, sha1, 0);
Linus Torvalds0ebde322007-04-09 21:14:26 -0700720 return retval;
721}
Linus Torvaldsca8db142005-09-25 09:59:37 -0700722
Christian Couder4886b892008-09-09 07:10:56 +0200723/*
Michael Haggertyc224ca72011-09-15 23:10:35 +0200724 * Try to read ref from the packed references. On success, set sha1
725 * and return 0; otherwise, return -1.
Christian Couder4886b892008-09-09 07:10:56 +0200726 */
Michael Haggertydfefa932011-12-12 06:38:09 +0100727static int get_packed_ref(const char *refname, unsigned char *sha1)
Michael Haggertyc224ca72011-09-15 23:10:35 +0200728{
Michael Haggertyd3177272012-04-10 07:30:24 +0200729 struct ref_dir *packed = get_packed_refs(get_ref_cache(NULL));
730 struct ref_entry *entry = search_ref_dir(packed, refname);
Junio C Hamano2c5c66b2011-10-10 15:56:19 -0700731 if (entry) {
Michael Haggerty593f1bb2012-04-10 07:30:23 +0200732 hashcpy(sha1, entry->u.value.sha1);
Junio C Hamano2c5c66b2011-10-10 15:56:19 -0700733 return 0;
Michael Haggertyc224ca72011-09-15 23:10:35 +0200734 }
735 return -1;
736}
737
Junio C Hamano8d684932011-12-20 13:25:53 -0800738const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int reading, int *flag)
Junio C Hamanoa876ed82005-09-30 14:08:25 -0700739{
Heikki Orsila0104ca02008-04-27 21:21:58 +0300740 int depth = MAXDEPTH;
741 ssize_t len;
Junio C Hamanoa876ed82005-09-30 14:08:25 -0700742 char buffer[256];
Michael Haggertydfefa932011-12-12 06:38:09 +0100743 static char refname_buffer[256];
Junio C Hamanoa876ed82005-09-30 14:08:25 -0700744
Junio C Hamano8da19772006-09-20 22:02:01 -0700745 if (flag)
746 *flag = 0;
747
Michael Haggertydfefa932011-12-12 06:38:09 +0100748 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))
Michael Haggerty8384d782011-09-15 23:10:39 +0200749 return NULL;
750
Junio C Hamanoa876ed82005-09-30 14:08:25 -0700751 for (;;) {
Junio C Hamano55956352011-10-19 13:55:49 -0700752 char path[PATH_MAX];
Junio C Hamanoa876ed82005-09-30 14:08:25 -0700753 struct stat st;
754 char *buf;
755 int fd;
756
757 if (--depth < 0)
758 return NULL;
759
Michael Haggertydfefa932011-12-12 06:38:09 +0100760 git_snpath(path, sizeof(path), "%s", refname);
Michael Haggertyc224ca72011-09-15 23:10:35 +0200761
Junio C Hamanoa876ed82005-09-30 14:08:25 -0700762 if (lstat(path, &st) < 0) {
Michael Haggertyc224ca72011-09-15 23:10:35 +0200763 if (errno != ENOENT)
Junio C Hamanoa876ed82005-09-30 14:08:25 -0700764 return NULL;
Michael Haggertyc224ca72011-09-15 23:10:35 +0200765 /*
766 * The loose reference file does not exist;
767 * check for a packed reference.
768 */
Michael Haggertydfefa932011-12-12 06:38:09 +0100769 if (!get_packed_ref(refname, sha1)) {
Michael Haggertyc224ca72011-09-15 23:10:35 +0200770 if (flag)
771 *flag |= REF_ISPACKED;
Michael Haggertydfefa932011-12-12 06:38:09 +0100772 return refname;
Michael Haggertyc224ca72011-09-15 23:10:35 +0200773 }
774 /* The reference is not a packed reference, either. */
775 if (reading) {
776 return NULL;
777 } else {
778 hashclr(sha1);
Michael Haggertydfefa932011-12-12 06:38:09 +0100779 return refname;
Michael Haggertyc224ca72011-09-15 23:10:35 +0200780 }
Junio C Hamanoa876ed82005-09-30 14:08:25 -0700781 }
782
783 /* Follow "normalized" - ie "refs/.." symlinks by hand */
784 if (S_ISLNK(st.st_mode)) {
785 len = readlink(path, buffer, sizeof(buffer)-1);
Michael Haggerty7bb2bf82011-09-15 23:10:31 +0200786 if (len < 0)
787 return NULL;
Michael Haggertyb54cb792011-09-15 23:10:32 +0200788 buffer[len] = 0;
Michael Haggerty1f58a032011-09-15 23:10:33 +0200789 if (!prefixcmp(buffer, "refs/") &&
790 !check_refname_format(buffer, 0)) {
Michael Haggertydfefa932011-12-12 06:38:09 +0100791 strcpy(refname_buffer, buffer);
792 refname = refname_buffer;
Junio C Hamano8da19772006-09-20 22:02:01 -0700793 if (flag)
794 *flag |= REF_ISSYMREF;
Junio C Hamanoa876ed82005-09-30 14:08:25 -0700795 continue;
796 }
797 }
798
Dennis Stosberg7a216322006-10-02 19:23:53 +0200799 /* Is it a directory? */
800 if (S_ISDIR(st.st_mode)) {
801 errno = EISDIR;
802 return NULL;
803 }
804
Junio C Hamanoa876ed82005-09-30 14:08:25 -0700805 /*
806 * Anything else, just open it and try to use it as
807 * a ref
808 */
809 fd = open(path, O_RDONLY);
810 if (fd < 0)
811 return NULL;
Andy Whitcroft93d26e42007-01-08 15:58:08 +0000812 len = read_in_full(fd, buffer, sizeof(buffer)-1);
Junio C Hamanoa876ed82005-09-30 14:08:25 -0700813 close(fd);
Michael Haggerty28775052011-09-15 23:10:34 +0200814 if (len < 0)
815 return NULL;
816 while (len && isspace(buffer[len-1]))
817 len--;
818 buffer[len] = '\0';
Junio C Hamanoa876ed82005-09-30 14:08:25 -0700819
820 /*
821 * Is it a symbolic ref?
822 */
Michael Haggerty28775052011-09-15 23:10:34 +0200823 if (prefixcmp(buffer, "ref:"))
Junio C Hamanoa876ed82005-09-30 14:08:25 -0700824 break;
Junio C Hamano55956352011-10-19 13:55:49 -0700825 if (flag)
826 *flag |= REF_ISSYMREF;
Junio C Hamanoa876ed82005-09-30 14:08:25 -0700827 buf = buffer + 4;
Michael Haggerty28775052011-09-15 23:10:34 +0200828 while (isspace(*buf))
829 buf++;
Michael Haggerty313fb012011-09-15 23:10:36 +0200830 if (check_refname_format(buf, REFNAME_ALLOW_ONELEVEL)) {
Junio C Hamano55956352011-10-19 13:55:49 -0700831 if (flag)
832 *flag |= REF_ISBROKEN;
Michael Haggerty313fb012011-09-15 23:10:36 +0200833 return NULL;
834 }
Michael Haggertydfefa932011-12-12 06:38:09 +0100835 refname = strcpy(refname_buffer, buf);
Junio C Hamanoa876ed82005-09-30 14:08:25 -0700836 }
Michael Haggertyf989fea2011-09-15 23:10:41 +0200837 /* Please note that FETCH_HEAD has a second line containing other data. */
838 if (get_sha1_hex(buffer, sha1) || (buffer[40] != '\0' && !isspace(buffer[40]))) {
Junio C Hamano55956352011-10-19 13:55:49 -0700839 if (flag)
840 *flag |= REF_ISBROKEN;
Junio C Hamanoa876ed82005-09-30 14:08:25 -0700841 return NULL;
Michael Haggerty629cd3a2011-09-15 23:10:40 +0200842 }
Michael Haggertydfefa932011-12-12 06:38:09 +0100843 return refname;
Junio C Hamanoa876ed82005-09-30 14:08:25 -0700844}
845
Nguyễn Thái Ngọc Duy96ec7b12011-12-13 21:17:48 +0700846char *resolve_refdup(const char *ref, unsigned char *sha1, int reading, int *flag)
847{
Nguyễn Thái Ngọc Duy8cad4742011-12-12 18:20:32 +0700848 const char *ret = resolve_ref_unsafe(ref, sha1, reading, flag);
Nguyễn Thái Ngọc Duy96ec7b12011-12-13 21:17:48 +0700849 return ret ? xstrdup(ret) : NULL;
850}
851
Ilari Liusvaarad08bae72010-01-20 11:48:25 +0200852/* The argument to filter_refs */
853struct ref_filter {
854 const char *pattern;
855 each_ref_fn *fn;
856 void *cb_data;
857};
858
Michael Haggertydfefa932011-12-12 06:38:09 +0100859int read_ref_full(const char *refname, unsigned char *sha1, int reading, int *flags)
Linus Torvalds8a65ff72005-07-02 20:23:36 -0700860{
Junio C Hamano8d684932011-12-20 13:25:53 -0800861 if (resolve_ref_unsafe(refname, sha1, reading, flags))
Junio C Hamanoa876ed82005-09-30 14:08:25 -0700862 return 0;
863 return -1;
Linus Torvalds8a65ff72005-07-02 20:23:36 -0700864}
865
Michael Haggertydfefa932011-12-12 06:38:09 +0100866int read_ref(const char *refname, unsigned char *sha1)
Nguyễn Thái Ngọc Duyc6893322011-11-13 17:22:14 +0700867{
Michael Haggertydfefa932011-12-12 06:38:09 +0100868 return read_ref_full(refname, sha1, 1, NULL);
Nguyễn Thái Ngọc Duyc6893322011-11-13 17:22:14 +0700869}
870
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200871int ref_exists(const char *refname)
Junio C Hamanoef06b912006-11-18 22:13:33 -0800872{
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200873 unsigned char sha1[20];
874 return !!resolve_ref_unsafe(refname, sha1, 1, NULL);
Junio C Hamanoef06b912006-11-18 22:13:33 -0800875}
876
Michael Haggerty85be1fe2011-12-12 06:38:10 +0100877static int filter_refs(const char *refname, const unsigned char *sha1, int flags,
Michael Haggertydfefa932011-12-12 06:38:09 +0100878 void *data)
Ilari Liusvaarad08bae72010-01-20 11:48:25 +0200879{
880 struct ref_filter *filter = (struct ref_filter *)data;
Michael Haggertydfefa932011-12-12 06:38:09 +0100881 if (fnmatch(filter->pattern, refname, 0))
Ilari Liusvaarad08bae72010-01-20 11:48:25 +0200882 return 0;
Michael Haggerty85be1fe2011-12-12 06:38:10 +0100883 return filter->fn(refname, sha1, flags, filter->cb_data);
Ilari Liusvaarad08bae72010-01-20 11:48:25 +0200884}
885
Michael Haggertydfefa932011-12-12 06:38:09 +0100886int peel_ref(const char *refname, unsigned char *sha1)
Junio C Hamanocf0adba2006-11-19 13:22:44 -0800887{
888 int flag;
889 unsigned char base[20];
890 struct object *o;
891
Michael Haggertydfefa932011-12-12 06:38:09 +0100892 if (current_ref && (current_ref->name == refname
893 || !strcmp(current_ref->name, refname))) {
Shawn O. Pearce0ae91be2008-02-24 03:07:22 -0500894 if (current_ref->flag & REF_KNOWS_PEELED) {
Michael Haggerty593f1bb2012-04-10 07:30:23 +0200895 hashcpy(sha1, current_ref->u.value.peeled);
Shawn O. Pearce0ae91be2008-02-24 03:07:22 -0500896 return 0;
897 }
Michael Haggerty593f1bb2012-04-10 07:30:23 +0200898 hashcpy(base, current_ref->u.value.sha1);
Shawn O. Pearce0ae91be2008-02-24 03:07:22 -0500899 goto fallback;
900 }
901
Michael Haggertydfefa932011-12-12 06:38:09 +0100902 if (read_ref_full(refname, base, 1, &flag))
Junio C Hamanocf0adba2006-11-19 13:22:44 -0800903 return -1;
904
905 if ((flag & REF_ISPACKED)) {
Michael Haggertyd3177272012-04-10 07:30:24 +0200906 struct ref_dir *dir = get_packed_refs(get_ref_cache(NULL));
907 struct ref_entry *r = search_ref_dir(dir, refname);
Junio C Hamanocf0adba2006-11-19 13:22:44 -0800908
Julian Phillipse9c4c112011-09-29 23:11:42 +0100909 if (r != NULL && r->flag & REF_KNOWS_PEELED) {
Michael Haggerty593f1bb2012-04-10 07:30:23 +0200910 hashcpy(sha1, r->u.value.peeled);
Julian Phillipse9c4c112011-09-29 23:11:42 +0100911 return 0;
Junio C Hamanocf0adba2006-11-19 13:22:44 -0800912 }
Junio C Hamanocf0adba2006-11-19 13:22:44 -0800913 }
914
Shawn O. Pearce0ae91be2008-02-24 03:07:22 -0500915fallback:
Junio C Hamanocf0adba2006-11-19 13:22:44 -0800916 o = parse_object(base);
Shawn O. Pearce8c87dc72008-02-24 03:07:19 -0500917 if (o && o->type == OBJ_TAG) {
Michael Haggertydfefa932011-12-12 06:38:09 +0100918 o = deref_tag(o, refname, 0);
Junio C Hamanocf0adba2006-11-19 13:22:44 -0800919 if (o) {
920 hashcpy(sha1, o->sha1);
921 return 0;
922 }
923 }
924 return -1;
925}
926
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200927struct warn_if_dangling_data {
928 FILE *fp;
929 const char *refname;
930 const char *msg_fmt;
931};
932
933static int warn_if_dangling_symref(const char *refname, const unsigned char *sha1,
934 int flags, void *cb_data)
935{
936 struct warn_if_dangling_data *d = cb_data;
937 const char *resolves_to;
938 unsigned char junk[20];
939
940 if (!(flags & REF_ISSYMREF))
941 return 0;
942
943 resolves_to = resolve_ref_unsafe(refname, junk, 0, NULL);
944 if (!resolves_to || strcmp(resolves_to, d->refname))
945 return 0;
946
947 fprintf(d->fp, d->msg_fmt, refname);
948 return 0;
949}
950
951void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname)
952{
953 struct warn_if_dangling_data data;
954
955 data.fp = fp;
956 data.refname = refname;
957 data.msg_fmt = msg_fmt;
958 for_each_rawref(warn_if_dangling_symref, &data);
959}
960
Heiko Voigt0bad6112010-07-07 15:39:11 +0200961static int do_for_each_ref(const char *submodule, const char *base, each_ref_fn fn,
962 int trim, int flags, void *cb_data)
Linus Torvalds8a65ff72005-07-02 20:23:36 -0700963{
Michael Haggerty316b0972011-12-12 06:38:16 +0100964 struct ref_cache *refs = get_ref_cache(submodule);
Michael Haggertyd3177272012-04-10 07:30:24 +0200965 struct ref_dir *packed_refs = get_packed_refs(refs);
966 struct ref_dir *loose_refs = get_loose_refs(refs);
967 sort_ref_dir(packed_refs);
968 sort_ref_dir(loose_refs);
969 return do_for_each_ref_in_dirs(packed_refs,
970 loose_refs,
971 base, fn, trim, flags, cb_data);
Linus Torvalds8a65ff72005-07-02 20:23:36 -0700972}
973
Heiko Voigt0bad6112010-07-07 15:39:11 +0200974static int do_head_ref(const char *submodule, each_ref_fn fn, void *cb_data)
Linus Torvalds723c31f2005-07-05 11:31:32 -0700975{
976 unsigned char sha1[20];
Junio C Hamano8da19772006-09-20 22:02:01 -0700977 int flag;
978
Heiko Voigt0bad6112010-07-07 15:39:11 +0200979 if (submodule) {
980 if (resolve_gitlink_ref(submodule, "HEAD", sha1) == 0)
981 return fn("HEAD", sha1, 0, cb_data);
982
983 return 0;
984 }
985
Nguyễn Thái Ngọc Duyc6893322011-11-13 17:22:14 +0700986 if (!read_ref_full("HEAD", sha1, 1, &flag))
Junio C Hamano8da19772006-09-20 22:02:01 -0700987 return fn("HEAD", sha1, flag, cb_data);
Heiko Voigt0bad6112010-07-07 15:39:11 +0200988
Linus Torvalds2f34ba32005-07-05 15:45:00 -0700989 return 0;
Linus Torvalds723c31f2005-07-05 11:31:32 -0700990}
991
Heiko Voigt0bad6112010-07-07 15:39:11 +0200992int head_ref(each_ref_fn fn, void *cb_data)
993{
994 return do_head_ref(NULL, fn, cb_data);
995}
996
Heiko Voigt9ef6aeb2010-07-07 15:39:12 +0200997int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
998{
999 return do_head_ref(submodule, fn, cb_data);
1000}
1001
Junio C Hamanocb5d7092006-09-20 21:47:42 -07001002int for_each_ref(each_ref_fn fn, void *cb_data)
Linus Torvalds8a65ff72005-07-02 20:23:36 -07001003{
Josh Triplettb3cfc402011-07-05 10:54:21 -07001004 return do_for_each_ref(NULL, "", fn, 0, 0, cb_data);
Seana62be772006-05-13 21:43:00 -04001005}
1006
Heiko Voigt9ef6aeb2010-07-07 15:39:12 +02001007int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
1008{
Josh Triplettb3cfc402011-07-05 10:54:21 -07001009 return do_for_each_ref(submodule, "", fn, 0, 0, cb_data);
Seana62be772006-05-13 21:43:00 -04001010}
1011
Christian Couder2a8177b2009-03-30 05:07:15 +02001012int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)
1013{
Heiko Voigt0bad6112010-07-07 15:39:11 +02001014 return do_for_each_ref(NULL, prefix, fn, strlen(prefix), 0, cb_data);
Christian Couder2a8177b2009-03-30 05:07:15 +02001015}
1016
Heiko Voigt9ef6aeb2010-07-07 15:39:12 +02001017int for_each_ref_in_submodule(const char *submodule, const char *prefix,
1018 each_ref_fn fn, void *cb_data)
1019{
1020 return do_for_each_ref(submodule, prefix, fn, strlen(prefix), 0, cb_data);
Seana62be772006-05-13 21:43:00 -04001021}
1022
Junio C Hamanocb5d7092006-09-20 21:47:42 -07001023int for_each_tag_ref(each_ref_fn fn, void *cb_data)
Seana62be772006-05-13 21:43:00 -04001024{
Christian Couder2a8177b2009-03-30 05:07:15 +02001025 return for_each_ref_in("refs/tags/", fn, cb_data);
Seana62be772006-05-13 21:43:00 -04001026}
1027
Heiko Voigt9ef6aeb2010-07-07 15:39:12 +02001028int for_each_tag_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
1029{
1030 return for_each_ref_in_submodule(submodule, "refs/tags/", fn, cb_data);
1031}
1032
Junio C Hamanocb5d7092006-09-20 21:47:42 -07001033int for_each_branch_ref(each_ref_fn fn, void *cb_data)
Seana62be772006-05-13 21:43:00 -04001034{
Christian Couder2a8177b2009-03-30 05:07:15 +02001035 return for_each_ref_in("refs/heads/", fn, cb_data);
Seana62be772006-05-13 21:43:00 -04001036}
1037
Heiko Voigt9ef6aeb2010-07-07 15:39:12 +02001038int for_each_branch_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
1039{
1040 return for_each_ref_in_submodule(submodule, "refs/heads/", fn, cb_data);
1041}
1042
Junio C Hamanocb5d7092006-09-20 21:47:42 -07001043int for_each_remote_ref(each_ref_fn fn, void *cb_data)
Seana62be772006-05-13 21:43:00 -04001044{
Christian Couder2a8177b2009-03-30 05:07:15 +02001045 return for_each_ref_in("refs/remotes/", fn, cb_data);
Junio C Hamanof8948e22009-02-08 23:27:10 -08001046}
1047
Heiko Voigt9ef6aeb2010-07-07 15:39:12 +02001048int for_each_remote_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
1049{
1050 return for_each_ref_in_submodule(submodule, "refs/remotes/", fn, cb_data);
1051}
1052
Christian Couder29268702009-01-23 10:06:38 +01001053int for_each_replace_ref(each_ref_fn fn, void *cb_data)
1054{
Heiko Voigt0bad6112010-07-07 15:39:11 +02001055 return do_for_each_ref(NULL, "refs/replace/", fn, 13, 0, cb_data);
Christian Couder29268702009-01-23 10:06:38 +01001056}
1057
Josh Tripletta1bea2c2011-07-05 10:54:44 -07001058int head_ref_namespaced(each_ref_fn fn, void *cb_data)
1059{
1060 struct strbuf buf = STRBUF_INIT;
1061 int ret = 0;
1062 unsigned char sha1[20];
1063 int flag;
1064
1065 strbuf_addf(&buf, "%sHEAD", get_git_namespace());
Nguyễn Thái Ngọc Duyc6893322011-11-13 17:22:14 +07001066 if (!read_ref_full(buf.buf, sha1, 1, &flag))
Josh Tripletta1bea2c2011-07-05 10:54:44 -07001067 ret = fn(buf.buf, sha1, flag, cb_data);
1068 strbuf_release(&buf);
1069
1070 return ret;
1071}
1072
1073int for_each_namespaced_ref(each_ref_fn fn, void *cb_data)
1074{
1075 struct strbuf buf = STRBUF_INIT;
1076 int ret;
1077 strbuf_addf(&buf, "%srefs/", get_git_namespace());
1078 ret = do_for_each_ref(NULL, buf.buf, fn, 0, 0, cb_data);
1079 strbuf_release(&buf);
1080 return ret;
1081}
1082
Ilari Liusvaarab09fe972010-01-20 11:48:26 +02001083int for_each_glob_ref_in(each_ref_fn fn, const char *pattern,
1084 const char *prefix, void *cb_data)
Ilari Liusvaarad08bae72010-01-20 11:48:25 +02001085{
1086 struct strbuf real_pattern = STRBUF_INIT;
1087 struct ref_filter filter;
Ilari Liusvaarad08bae72010-01-20 11:48:25 +02001088 int ret;
1089
Ilari Liusvaarab09fe972010-01-20 11:48:26 +02001090 if (!prefix && prefixcmp(pattern, "refs/"))
Ilari Liusvaarad08bae72010-01-20 11:48:25 +02001091 strbuf_addstr(&real_pattern, "refs/");
Ilari Liusvaarab09fe972010-01-20 11:48:26 +02001092 else if (prefix)
1093 strbuf_addstr(&real_pattern, prefix);
Ilari Liusvaarad08bae72010-01-20 11:48:25 +02001094 strbuf_addstr(&real_pattern, pattern);
1095
Thomas Rast894a9d32010-03-12 18:04:26 +01001096 if (!has_glob_specials(pattern)) {
Junio C Hamano9517e6b2010-02-03 21:23:18 -08001097 /* Append implied '/' '*' if not present. */
Ilari Liusvaarad08bae72010-01-20 11:48:25 +02001098 if (real_pattern.buf[real_pattern.len - 1] != '/')
1099 strbuf_addch(&real_pattern, '/');
1100 /* No need to check for '*', there is none. */
1101 strbuf_addch(&real_pattern, '*');
1102 }
1103
1104 filter.pattern = real_pattern.buf;
1105 filter.fn = fn;
1106 filter.cb_data = cb_data;
1107 ret = for_each_ref(filter_refs, &filter);
1108
1109 strbuf_release(&real_pattern);
1110 return ret;
1111}
1112
Ilari Liusvaarab09fe972010-01-20 11:48:26 +02001113int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data)
1114{
1115 return for_each_glob_ref_in(fn, pattern, NULL, cb_data);
1116}
1117
Junio C Hamanof8948e22009-02-08 23:27:10 -08001118int for_each_rawref(each_ref_fn fn, void *cb_data)
1119{
Josh Triplettb3cfc402011-07-05 10:54:21 -07001120 return do_for_each_ref(NULL, "", fn, 0,
Junio C Hamanof8948e22009-02-08 23:27:10 -08001121 DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
Linus Torvalds8a65ff72005-07-02 20:23:36 -07001122}
1123
Felipe Contreras4577e482009-05-14 00:22:04 +03001124const char *prettify_refname(const char *name)
Daniel Barkalowa9c37a72009-03-08 21:06:05 -04001125{
Daniel Barkalowa9c37a72009-03-08 21:06:05 -04001126 return name + (
1127 !prefixcmp(name, "refs/heads/") ? 11 :
1128 !prefixcmp(name, "refs/tags/") ? 10 :
1129 !prefixcmp(name, "refs/remotes/") ? 13 :
1130 0);
1131}
1132
Steffen Prohaska79803322007-11-11 15:01:46 +01001133const char *ref_rev_parse_rules[] = {
1134 "%.*s",
1135 "refs/%.*s",
1136 "refs/tags/%.*s",
1137 "refs/heads/%.*s",
1138 "refs/remotes/%.*s",
1139 "refs/remotes/%.*s/HEAD",
1140 NULL
1141};
1142
1143int refname_match(const char *abbrev_name, const char *full_name, const char **rules)
1144{
1145 const char **p;
1146 const int abbrev_name_len = strlen(abbrev_name);
1147
1148 for (p = rules; *p; p++) {
1149 if (!strcmp(full_name, mkpath(*p, abbrev_name_len, abbrev_name))) {
1150 return 1;
1151 }
1152 }
1153
1154 return 0;
1155}
1156
Junio C Hamanoe5f38ec2006-06-06 14:04:17 -07001157static struct ref_lock *verify_lock(struct ref_lock *lock,
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001158 const unsigned char *old_sha1, int mustexist)
Daniel Barkalow95fc7512005-06-06 16:31:29 -04001159{
Nguyễn Thái Ngọc Duyc6893322011-11-13 17:22:14 +07001160 if (read_ref_full(lock->ref_name, lock->old_sha1, mustexist, NULL)) {
Linus Torvalds434cd0c2006-09-14 10:14:47 -07001161 error("Can't verify ref %s", lock->ref_name);
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001162 unlock_ref(lock);
1163 return NULL;
1164 }
David Rientjesa89fccd2006-08-17 11:54:57 -07001165 if (hashcmp(lock->old_sha1, old_sha1)) {
Linus Torvalds434cd0c2006-09-14 10:14:47 -07001166 error("Ref %s is at %s but expected %s", lock->ref_name,
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001167 sha1_to_hex(lock->old_sha1), sha1_to_hex(old_sha1));
1168 unlock_ref(lock);
1169 return NULL;
1170 }
1171 return lock;
1172}
1173
Johannes Schindelin7155b722007-09-28 16:28:54 +01001174static int remove_empty_directories(const char *file)
Junio C Hamanobc7127e2006-09-30 02:25:30 -07001175{
1176 /* we want to create a file but there is a directory there;
1177 * if that is an empty directory (or a directory that contains
1178 * only empty directories), remove them.
1179 */
Johannes Schindelin7155b722007-09-28 16:28:54 +01001180 struct strbuf path;
1181 int result;
Junio C Hamanobc7127e2006-09-30 02:25:30 -07001182
Johannes Schindelin7155b722007-09-28 16:28:54 +01001183 strbuf_init(&path, 20);
1184 strbuf_addstr(&path, file);
1185
Junio C Hamanoa0f4afb2009-06-30 15:33:45 -07001186 result = remove_dir_recursively(&path, REMOVE_DIR_EMPTY_ONLY);
Johannes Schindelin7155b722007-09-28 16:28:54 +01001187
1188 strbuf_release(&path);
1189
1190 return result;
Junio C Hamanobc7127e2006-09-30 02:25:30 -07001191}
1192
Michael Haggerty19b68b12011-12-12 06:38:12 +01001193/*
Junio C Hamanoff74f7f2011-10-12 10:35:38 -07001194 * *string and *len will only be substituted, and *string returned (for
1195 * later free()ing) if the string passed in is a magic short-hand form
1196 * to name a branch.
1197 */
1198static char *substitute_branch_name(const char **string, int *len)
1199{
1200 struct strbuf buf = STRBUF_INIT;
1201 int ret = interpret_branch_name(*string, &buf);
1202
1203 if (ret == *len) {
1204 size_t size;
1205 *string = strbuf_detach(&buf, &size);
1206 *len = size;
1207 return (char *)*string;
1208 }
1209
1210 return NULL;
1211}
1212
1213int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref)
1214{
1215 char *last_branch = substitute_branch_name(&str, &len);
1216 const char **p, *r;
1217 int refs_found = 0;
1218
1219 *ref = NULL;
1220 for (p = ref_rev_parse_rules; *p; p++) {
1221 char fullref[PATH_MAX];
1222 unsigned char sha1_from_ref[20];
1223 unsigned char *this_result;
1224 int flag;
1225
1226 this_result = refs_found ? sha1_from_ref : sha1;
1227 mksnpath(fullref, sizeof(fullref), *p, len, str);
Nguyễn Thái Ngọc Duy8cad4742011-12-12 18:20:32 +07001228 r = resolve_ref_unsafe(fullref, this_result, 1, &flag);
Junio C Hamanoff74f7f2011-10-12 10:35:38 -07001229 if (r) {
1230 if (!refs_found++)
1231 *ref = xstrdup(r);
1232 if (!warn_ambiguous_refs)
1233 break;
Junio C Hamano55956352011-10-19 13:55:49 -07001234 } else if ((flag & REF_ISSYMREF) && strcmp(fullref, "HEAD")) {
Junio C Hamanoff74f7f2011-10-12 10:35:38 -07001235 warning("ignoring dangling symref %s.", fullref);
Junio C Hamano55956352011-10-19 13:55:49 -07001236 } else if ((flag & REF_ISBROKEN) && strchr(fullref, '/')) {
1237 warning("ignoring broken ref %s.", fullref);
1238 }
Junio C Hamanoff74f7f2011-10-12 10:35:38 -07001239 }
1240 free(last_branch);
1241 return refs_found;
1242}
1243
1244int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
1245{
1246 char *last_branch = substitute_branch_name(&str, &len);
1247 const char **p;
1248 int logs_found = 0;
1249
1250 *log = NULL;
1251 for (p = ref_rev_parse_rules; *p; p++) {
1252 struct stat st;
1253 unsigned char hash[20];
1254 char path[PATH_MAX];
1255 const char *ref, *it;
1256
1257 mksnpath(path, sizeof(path), *p, len, str);
Nguyễn Thái Ngọc Duy8cad4742011-12-12 18:20:32 +07001258 ref = resolve_ref_unsafe(path, hash, 1, NULL);
Junio C Hamanoff74f7f2011-10-12 10:35:38 -07001259 if (!ref)
1260 continue;
1261 if (!stat(git_path("logs/%s", path), &st) &&
1262 S_ISREG(st.st_mode))
1263 it = path;
1264 else if (strcmp(ref, path) &&
1265 !stat(git_path("logs/%s", ref), &st) &&
1266 S_ISREG(st.st_mode))
1267 it = ref;
1268 else
1269 continue;
1270 if (!logs_found++) {
1271 *log = xstrdup(it);
1272 hashcpy(sha1, hash);
1273 }
1274 if (!warn_ambiguous_refs)
1275 break;
1276 }
1277 free(last_branch);
1278 return logs_found;
1279}
1280
Michael Haggertydfefa932011-12-12 06:38:09 +01001281static struct ref_lock *lock_ref_sha1_basic(const char *refname,
1282 const unsigned char *old_sha1,
1283 int flags, int *type_p)
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001284{
Linus Torvalds434cd0c2006-09-14 10:14:47 -07001285 char *ref_file;
Michael Haggertydfefa932011-12-12 06:38:09 +01001286 const char *orig_refname = refname;
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001287 struct ref_lock *lock;
Junio C Hamano5cc3cef2006-09-30 14:14:31 -07001288 int last_errno = 0;
Junio C Hamanoacd3b9e2008-10-17 15:44:39 -07001289 int type, lflags;
Junio C Hamano4431fcc2006-09-27 01:09:18 -07001290 int mustexist = (old_sha1 && !is_null_sha1(old_sha1));
Clemens Buchacher5bdd8d42008-11-05 21:55:53 +01001291 int missing = 0;
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001292
1293 lock = xcalloc(1, sizeof(struct ref_lock));
1294 lock->lock_fd = -1;
1295
Junio C Hamano8d684932011-12-20 13:25:53 -08001296 refname = resolve_ref_unsafe(refname, lock->old_sha1, mustexist, &type);
Michael Haggertydfefa932011-12-12 06:38:09 +01001297 if (!refname && errno == EISDIR) {
Junio C Hamanobc7127e2006-09-30 02:25:30 -07001298 /* we are trying to lock foo but we used to
1299 * have foo/bar which now does not exist;
1300 * it is normal for the empty directory 'foo'
1301 * to remain.
1302 */
Michael Haggertydfefa932011-12-12 06:38:09 +01001303 ref_file = git_path("%s", orig_refname);
Junio C Hamano5cc3cef2006-09-30 14:14:31 -07001304 if (remove_empty_directories(ref_file)) {
1305 last_errno = errno;
Michael Haggertydfefa932011-12-12 06:38:09 +01001306 error("there are still refs under '%s'", orig_refname);
Junio C Hamano5cc3cef2006-09-30 14:14:31 -07001307 goto error_return;
1308 }
Junio C Hamano8d684932011-12-20 13:25:53 -08001309 refname = resolve_ref_unsafe(orig_refname, lock->old_sha1, mustexist, &type);
Junio C Hamanobc7127e2006-09-30 02:25:30 -07001310 }
Sven Verdoolaege68db31c2007-05-09 12:33:20 +02001311 if (type_p)
1312 *type_p = type;
Michael Haggertydfefa932011-12-12 06:38:09 +01001313 if (!refname) {
Junio C Hamano5cc3cef2006-09-30 14:14:31 -07001314 last_errno = errno;
Shawn Pearce818f4772006-07-28 23:44:51 -04001315 error("unable to resolve reference %s: %s",
Michael Haggertydfefa932011-12-12 06:38:09 +01001316 orig_refname, strerror(errno));
Junio C Hamano5cc3cef2006-09-30 14:14:31 -07001317 goto error_return;
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001318 }
Clemens Buchacher5bdd8d42008-11-05 21:55:53 +01001319 missing = is_null_sha1(lock->old_sha1);
Lars Hjemlic976d412006-11-28 15:47:40 +01001320 /* When the ref did not exist and we are creating it,
1321 * make sure there is no existing ref that is packed
1322 * whose name begins with our refname, nor a ref whose
1323 * name is a proper prefix of our refname.
1324 */
Clemens Buchacher5bdd8d42008-11-05 21:55:53 +01001325 if (missing &&
Michael Haggerty316b0972011-12-12 06:38:16 +01001326 !is_refname_available(refname, NULL, get_packed_refs(get_ref_cache(NULL)))) {
Jeff Kingf475e082009-05-25 06:37:15 -04001327 last_errno = ENOTDIR;
Lars Hjemlic976d412006-11-28 15:47:40 +01001328 goto error_return;
Jeff Kingf475e082009-05-25 06:37:15 -04001329 }
Junio C Hamano22a38442006-09-30 14:19:25 -07001330
Junio C Hamanoc33d5172006-06-06 13:54:14 -07001331 lock->lk = xcalloc(1, sizeof(struct lock_file));
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001332
Junio C Hamanoacd3b9e2008-10-17 15:44:39 -07001333 lflags = LOCK_DIE_ON_ERROR;
1334 if (flags & REF_NODEREF) {
Michael Haggertydfefa932011-12-12 06:38:09 +01001335 refname = orig_refname;
Junio C Hamanoacd3b9e2008-10-17 15:44:39 -07001336 lflags |= LOCK_NODEREF;
1337 }
Michael Haggertydfefa932011-12-12 06:38:09 +01001338 lock->ref_name = xstrdup(refname);
1339 lock->orig_ref_name = xstrdup(orig_refname);
1340 ref_file = git_path("%s", refname);
Clemens Buchacher5bdd8d42008-11-05 21:55:53 +01001341 if (missing)
Sven Verdoolaege68db31c2007-05-09 12:33:20 +02001342 lock->force_write = 1;
1343 if ((flags & REF_NODEREF) && (type & REF_ISSYMREF))
1344 lock->force_write = 1;
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001345
Junio C Hamano5cc3cef2006-09-30 14:14:31 -07001346 if (safe_create_leading_directories(ref_file)) {
1347 last_errno = errno;
1348 error("unable to create directory for %s", ref_file);
1349 goto error_return;
1350 }
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001351
Junio C Hamanoacd3b9e2008-10-17 15:44:39 -07001352 lock->lock_fd = hold_lock_file_for_update(lock->lk, ref_file, lflags);
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001353 return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock;
Junio C Hamano5cc3cef2006-09-30 14:14:31 -07001354
1355 error_return:
1356 unlock_ref(lock);
1357 errno = last_errno;
1358 return NULL;
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001359}
1360
Michael Haggertydfefa932011-12-12 06:38:09 +01001361struct ref_lock *lock_ref_sha1(const char *refname, const unsigned char *old_sha1)
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001362{
Petr Baudis53cce842006-09-19 22:58:23 +02001363 char refpath[PATH_MAX];
Michael Haggertydfefa932011-12-12 06:38:09 +01001364 if (check_refname_format(refname, 0))
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001365 return NULL;
Michael Haggertydfefa932011-12-12 06:38:09 +01001366 strcpy(refpath, mkpath("refs/%s", refname));
Sven Verdoolaege68db31c2007-05-09 12:33:20 +02001367 return lock_ref_sha1_basic(refpath, old_sha1, 0, NULL);
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001368}
1369
Michael Haggertydfefa932011-12-12 06:38:09 +01001370struct ref_lock *lock_any_ref_for_update(const char *refname,
1371 const unsigned char *old_sha1, int flags)
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001372{
Michael Haggertydfefa932011-12-12 06:38:09 +01001373 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))
Junio C Hamano257f3022008-01-02 11:14:40 -08001374 return NULL;
Michael Haggertydfefa932011-12-12 06:38:09 +01001375 return lock_ref_sha1_basic(refname, old_sha1, flags, NULL);
Junio C Hamanoc0277d12006-09-30 15:02:00 -07001376}
1377
Michael Haggertyd66da472012-04-10 07:30:17 +02001378struct repack_without_ref_sb {
1379 const char *refname;
1380 int fd;
1381};
1382
1383static int repack_without_ref_fn(const char *refname, const unsigned char *sha1,
1384 int flags, void *cb_data)
1385{
1386 struct repack_without_ref_sb *data = cb_data;
1387 char line[PATH_MAX + 100];
1388 int len;
1389
1390 if (!strcmp(data->refname, refname))
1391 return 0;
1392 len = snprintf(line, sizeof(line), "%s %s\n",
1393 sha1_to_hex(sha1), refname);
1394 /* this should not happen but just being defensive */
1395 if (len > sizeof(line))
1396 die("too long a refname '%s'", refname);
1397 write_or_die(data->fd, line, len);
1398 return 0;
1399}
1400
Junio C Hamano26a063a2006-10-01 11:41:00 -07001401static struct lock_file packlock;
1402
Junio C Hamanoc0277d12006-09-30 15:02:00 -07001403static int repack_without_ref(const char *refname)
1404{
Michael Haggertyd66da472012-04-10 07:30:17 +02001405 struct repack_without_ref_sb data;
Michael Haggertyd3177272012-04-10 07:30:24 +02001406 struct ref_dir *packed = get_packed_refs(get_ref_cache(NULL));
1407 sort_ref_dir(packed);
1408 if (search_ref_dir(packed, refname) == NULL)
Junio C Hamanoc0277d12006-09-30 15:02:00 -07001409 return 0;
Michael Haggertyd66da472012-04-10 07:30:17 +02001410 data.refname = refname;
1411 data.fd = hold_lock_file_for_update(&packlock, git_path("packed-refs"), 0);
1412 if (data.fd < 0) {
Miklos Vajna1b018fd2009-09-27 01:15:09 +02001413 unable_to_lock_error(git_path("packed-refs"), errno);
Junio C Hamanoc0277d12006-09-30 15:02:00 -07001414 return error("cannot delete '%s' from packed refs", refname);
Miklos Vajna1b018fd2009-09-27 01:15:09 +02001415 }
Michael Haggertyd3177272012-04-10 07:30:24 +02001416 do_for_each_ref_in_dir(packed, 0, "", repack_without_ref_fn, 0, 0, &data);
Junio C Hamanoc0277d12006-09-30 15:02:00 -07001417 return commit_lock_file(&packlock);
1418}
1419
Miklos Vajnaeca35a22008-10-26 03:33:56 +01001420int delete_ref(const char *refname, const unsigned char *sha1, int delopt)
Junio C Hamanoc0277d12006-09-30 15:02:00 -07001421{
1422 struct ref_lock *lock;
Miklos Vajnaeca35a22008-10-26 03:33:56 +01001423 int err, i = 0, ret = 0, flag = 0;
Junio C Hamanoc0277d12006-09-30 15:02:00 -07001424
Sven Verdoolaege68db31c2007-05-09 12:33:20 +02001425 lock = lock_ref_sha1_basic(refname, sha1, 0, &flag);
Junio C Hamanoc0277d12006-09-30 15:02:00 -07001426 if (!lock)
1427 return 1;
Miklos Vajna045a4762008-11-01 00:25:44 +01001428 if (!(flag & REF_ISPACKED) || flag & REF_ISSYMREF) {
Junio C Hamanoc0277d12006-09-30 15:02:00 -07001429 /* loose */
Miklos Vajnaeca35a22008-10-26 03:33:56 +01001430 const char *path;
1431
1432 if (!(delopt & REF_NODEREF)) {
1433 i = strlen(lock->lk->filename) - 5; /* .lock */
1434 lock->lk->filename[i] = 0;
1435 path = lock->lk->filename;
1436 } else {
Daniel Lowe9db56f72008-11-10 16:07:52 -05001437 path = git_path("%s", refname);
Miklos Vajnaeca35a22008-10-26 03:33:56 +01001438 }
Alex Riesen691f1a22009-04-29 23:22:56 +02001439 err = unlink_or_warn(path);
1440 if (err && errno != ENOENT)
Junio C Hamanoc0277d12006-09-30 15:02:00 -07001441 ret = 1;
Alex Riesen691f1a22009-04-29 23:22:56 +02001442
Miklos Vajnaeca35a22008-10-26 03:33:56 +01001443 if (!(delopt & REF_NODEREF))
1444 lock->lk->filename[i] = '.';
Junio C Hamanoc0277d12006-09-30 15:02:00 -07001445 }
1446 /* removing the loose one could have resurrected an earlier
1447 * packed one. Also, if it was not loose we need to repack
1448 * without it.
1449 */
1450 ret |= repack_without_ref(refname);
1451
Alex Riesen691f1a22009-04-29 23:22:56 +02001452 unlink_or_warn(git_path("logs/%s", lock->ref_name));
Michael Haggerty3870a0d2011-10-17 04:38:06 +02001453 invalidate_ref_cache(NULL);
Junio C Hamanoc0277d12006-09-30 15:02:00 -07001454 unlock_ref(lock);
1455 return ret;
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001456}
1457
Pierre Habouzit765c2252010-07-07 09:47:20 +02001458/*
1459 * People using contrib's git-new-workdir have .git/logs/refs ->
1460 * /some/other/path/.git/logs/refs, and that may live on another device.
1461 *
1462 * IOW, to avoid cross device rename errors, the temporary renamed log must
1463 * live into logs/refs.
1464 */
1465#define TMP_RENAMED_LOG "logs/refs/.tmp-renamed-log"
1466
Michael Haggertydfefa932011-12-12 06:38:09 +01001467int rename_ref(const char *oldrefname, const char *newrefname, const char *logmsg)
Lars Hjemlic976d412006-11-28 15:47:40 +01001468{
Lars Hjemlic976d412006-11-28 15:47:40 +01001469 unsigned char sha1[20], orig_sha1[20];
1470 int flag = 0, logmoved = 0;
1471 struct ref_lock *lock;
Lars Hjemlic976d412006-11-28 15:47:40 +01001472 struct stat loginfo;
Michael Haggertydfefa932011-12-12 06:38:09 +01001473 int log = !lstat(git_path("logs/%s", oldrefname), &loginfo);
Miklos Vajnaeca35a22008-10-26 03:33:56 +01001474 const char *symref = NULL;
Michael Haggerty316b0972011-12-12 06:38:16 +01001475 struct ref_cache *refs = get_ref_cache(NULL);
Lars Hjemlic976d412006-11-28 15:47:40 +01001476
Miklos Vajna450d4c02008-10-26 03:33:57 +01001477 if (log && S_ISLNK(loginfo.st_mode))
Michael Haggertydfefa932011-12-12 06:38:09 +01001478 return error("reflog for %s is a symlink", oldrefname);
Lars Hjemlic976d412006-11-28 15:47:40 +01001479
Junio C Hamano8d684932011-12-20 13:25:53 -08001480 symref = resolve_ref_unsafe(oldrefname, orig_sha1, 1, &flag);
Miklos Vajnaeca35a22008-10-26 03:33:56 +01001481 if (flag & REF_ISSYMREF)
Miklos Vajnafa581862008-10-29 01:05:27 +01001482 return error("refname %s is a symbolic ref, renaming it is not supported",
Michael Haggertydfefa932011-12-12 06:38:09 +01001483 oldrefname);
Miklos Vajnaeca35a22008-10-26 03:33:56 +01001484 if (!symref)
Michael Haggertydfefa932011-12-12 06:38:09 +01001485 return error("refname %s not found", oldrefname);
Lars Hjemlic976d412006-11-28 15:47:40 +01001486
Michael Haggerty316b0972011-12-12 06:38:16 +01001487 if (!is_refname_available(newrefname, oldrefname, get_packed_refs(refs)))
Lars Hjemlic976d412006-11-28 15:47:40 +01001488 return 1;
1489
Michael Haggerty316b0972011-12-12 06:38:16 +01001490 if (!is_refname_available(newrefname, oldrefname, get_loose_refs(refs)))
Lars Hjemlic976d412006-11-28 15:47:40 +01001491 return 1;
1492
Michael Haggertydfefa932011-12-12 06:38:09 +01001493 if (log && rename(git_path("logs/%s", oldrefname), git_path(TMP_RENAMED_LOG)))
Pierre Habouzit765c2252010-07-07 09:47:20 +02001494 return error("unable to move logfile logs/%s to "TMP_RENAMED_LOG": %s",
Michael Haggertydfefa932011-12-12 06:38:09 +01001495 oldrefname, strerror(errno));
Lars Hjemlic976d412006-11-28 15:47:40 +01001496
Michael Haggertydfefa932011-12-12 06:38:09 +01001497 if (delete_ref(oldrefname, orig_sha1, REF_NODEREF)) {
1498 error("unable to delete old %s", oldrefname);
Lars Hjemlic976d412006-11-28 15:47:40 +01001499 goto rollback;
1500 }
1501
Michael Haggertydfefa932011-12-12 06:38:09 +01001502 if (!read_ref_full(newrefname, sha1, 1, &flag) &&
1503 delete_ref(newrefname, sha1, REF_NODEREF)) {
Lars Hjemlic976d412006-11-28 15:47:40 +01001504 if (errno==EISDIR) {
Michael Haggertydfefa932011-12-12 06:38:09 +01001505 if (remove_empty_directories(git_path("%s", newrefname))) {
1506 error("Directory not empty: %s", newrefname);
Lars Hjemlic976d412006-11-28 15:47:40 +01001507 goto rollback;
1508 }
1509 } else {
Michael Haggertydfefa932011-12-12 06:38:09 +01001510 error("unable to delete existing %s", newrefname);
Lars Hjemlic976d412006-11-28 15:47:40 +01001511 goto rollback;
1512 }
1513 }
1514
Michael Haggertydfefa932011-12-12 06:38:09 +01001515 if (log && safe_create_leading_directories(git_path("logs/%s", newrefname))) {
1516 error("unable to create directory for %s", newrefname);
Lars Hjemlic976d412006-11-28 15:47:40 +01001517 goto rollback;
1518 }
1519
1520 retry:
Michael Haggertydfefa932011-12-12 06:38:09 +01001521 if (log && rename(git_path(TMP_RENAMED_LOG), git_path("logs/%s", newrefname))) {
Jason Riedyd9e74d52007-01-15 17:30:59 -08001522 if (errno==EISDIR || errno==ENOTDIR) {
1523 /*
1524 * rename(a, b) when b is an existing
1525 * directory ought to result in ISDIR, but
1526 * Solaris 5.8 gives ENOTDIR. Sheesh.
1527 */
Michael Haggertydfefa932011-12-12 06:38:09 +01001528 if (remove_empty_directories(git_path("logs/%s", newrefname))) {
1529 error("Directory not empty: logs/%s", newrefname);
Lars Hjemlic976d412006-11-28 15:47:40 +01001530 goto rollback;
1531 }
1532 goto retry;
1533 } else {
Pierre Habouzit765c2252010-07-07 09:47:20 +02001534 error("unable to move logfile "TMP_RENAMED_LOG" to logs/%s: %s",
Michael Haggertydfefa932011-12-12 06:38:09 +01001535 newrefname, strerror(errno));
Lars Hjemlic976d412006-11-28 15:47:40 +01001536 goto rollback;
1537 }
1538 }
1539 logmoved = log;
1540
Michael Haggertydfefa932011-12-12 06:38:09 +01001541 lock = lock_ref_sha1_basic(newrefname, NULL, 0, NULL);
Lars Hjemlic976d412006-11-28 15:47:40 +01001542 if (!lock) {
Michael Haggertydfefa932011-12-12 06:38:09 +01001543 error("unable to lock %s for update", newrefname);
Lars Hjemlic976d412006-11-28 15:47:40 +01001544 goto rollback;
1545 }
Lars Hjemlic976d412006-11-28 15:47:40 +01001546 lock->force_write = 1;
1547 hashcpy(lock->old_sha1, orig_sha1);
Lars Hjemli678d0f42006-11-30 03:16:56 +01001548 if (write_ref_sha1(lock, orig_sha1, logmsg)) {
Michael Haggertydfefa932011-12-12 06:38:09 +01001549 error("unable to write current sha1 into %s", newrefname);
Lars Hjemlic976d412006-11-28 15:47:40 +01001550 goto rollback;
1551 }
1552
1553 return 0;
1554
1555 rollback:
Michael Haggertydfefa932011-12-12 06:38:09 +01001556 lock = lock_ref_sha1_basic(oldrefname, NULL, 0, NULL);
Lars Hjemlic976d412006-11-28 15:47:40 +01001557 if (!lock) {
Michael Haggertydfefa932011-12-12 06:38:09 +01001558 error("unable to lock %s for rollback", oldrefname);
Lars Hjemlic976d412006-11-28 15:47:40 +01001559 goto rollbacklog;
1560 }
1561
1562 lock->force_write = 1;
1563 flag = log_all_ref_updates;
1564 log_all_ref_updates = 0;
1565 if (write_ref_sha1(lock, orig_sha1, NULL))
Michael Haggertydfefa932011-12-12 06:38:09 +01001566 error("unable to write current sha1 into %s", oldrefname);
Lars Hjemlic976d412006-11-28 15:47:40 +01001567 log_all_ref_updates = flag;
1568
1569 rollbacklog:
Michael Haggertydfefa932011-12-12 06:38:09 +01001570 if (logmoved && rename(git_path("logs/%s", newrefname), git_path("logs/%s", oldrefname)))
Lars Hjemlic976d412006-11-28 15:47:40 +01001571 error("unable to restore logfile %s from %s: %s",
Michael Haggertydfefa932011-12-12 06:38:09 +01001572 oldrefname, newrefname, strerror(errno));
Lars Hjemlic976d412006-11-28 15:47:40 +01001573 if (!logmoved && log &&
Michael Haggertydfefa932011-12-12 06:38:09 +01001574 rename(git_path(TMP_RENAMED_LOG), git_path("logs/%s", oldrefname)))
Pierre Habouzit765c2252010-07-07 09:47:20 +02001575 error("unable to restore logfile %s from "TMP_RENAMED_LOG": %s",
Michael Haggertydfefa932011-12-12 06:38:09 +01001576 oldrefname, strerror(errno));
Lars Hjemlic976d412006-11-28 15:47:40 +01001577
1578 return 1;
1579}
1580
Brandon Casey435fc852008-02-22 12:57:30 -06001581int close_ref(struct ref_lock *lock)
Brandon Caseyb5313942008-01-16 13:14:30 -06001582{
1583 if (close_lock_file(lock->lk))
1584 return -1;
1585 lock->lock_fd = -1;
1586 return 0;
1587}
1588
Brandon Casey435fc852008-02-22 12:57:30 -06001589int commit_ref(struct ref_lock *lock)
Brandon Caseyb5313942008-01-16 13:14:30 -06001590{
1591 if (commit_lock_file(lock->lk))
1592 return -1;
1593 lock->lock_fd = -1;
1594 return 0;
1595}
1596
Junio C Hamanoe5f38ec2006-06-06 14:04:17 -07001597void unlock_ref(struct ref_lock *lock)
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001598{
Brandon Casey4ed7cd32008-01-16 13:12:46 -06001599 /* Do not free lock->lk -- atexit() still looks at them */
1600 if (lock->lk)
1601 rollback_lock_file(lock->lk);
Linus Torvalds434cd0c2006-09-14 10:14:47 -07001602 free(lock->ref_name);
Nicolas Pitre16557072007-01-26 17:26:06 -05001603 free(lock->orig_ref_name);
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001604 free(lock);
1605}
1606
Junio C Hamano0ec29a42007-07-28 17:17:17 -07001607/*
1608 * copy the reflog message msg to buf, which has been allocated sufficiently
1609 * large, while cleaning up the whitespaces. Especially, convert LF to space,
1610 * because reflog file is one line per entry.
1611 */
1612static int copy_msg(char *buf, const char *msg)
1613{
1614 char *cp = buf;
1615 char c;
1616 int wasspace = 1;
1617
1618 *cp++ = '\t';
1619 while ((c = *msg++)) {
1620 if (wasspace && isspace(c))
1621 continue;
1622 wasspace = isspace(c);
1623 if (wasspace)
1624 c = ' ';
1625 *cp++ = c;
1626 }
1627 while (buf < cp && isspace(cp[-1]))
1628 cp--;
1629 *cp++ = '\n';
1630 return cp - buf;
1631}
1632
Michael Haggertydfefa932011-12-12 06:38:09 +01001633int log_ref_setup(const char *refname, char *logfile, int bufsize)
Erick Mattos859c3012010-05-21 21:28:36 -03001634{
1635 int logfd, oflags = O_APPEND | O_WRONLY;
Erick Mattos859c3012010-05-21 21:28:36 -03001636
Michael Haggertydfefa932011-12-12 06:38:09 +01001637 git_snpath(logfile, bufsize, "logs/%s", refname);
Erick Mattos859c3012010-05-21 21:28:36 -03001638 if (log_all_ref_updates &&
Michael Haggertydfefa932011-12-12 06:38:09 +01001639 (!prefixcmp(refname, "refs/heads/") ||
1640 !prefixcmp(refname, "refs/remotes/") ||
1641 !prefixcmp(refname, "refs/notes/") ||
1642 !strcmp(refname, "HEAD"))) {
Thomas Rast157aaea2010-06-10 14:54:03 +02001643 if (safe_create_leading_directories(logfile) < 0)
Erick Mattos859c3012010-05-21 21:28:36 -03001644 return error("unable to create directory for %s",
Thomas Rast157aaea2010-06-10 14:54:03 +02001645 logfile);
Erick Mattos859c3012010-05-21 21:28:36 -03001646 oflags |= O_CREAT;
1647 }
1648
Thomas Rast157aaea2010-06-10 14:54:03 +02001649 logfd = open(logfile, oflags, 0666);
Erick Mattos859c3012010-05-21 21:28:36 -03001650 if (logfd < 0) {
1651 if (!(oflags & O_CREAT) && errno == ENOENT)
1652 return 0;
1653
1654 if ((oflags & O_CREAT) && errno == EISDIR) {
Thomas Rast157aaea2010-06-10 14:54:03 +02001655 if (remove_empty_directories(logfile)) {
Erick Mattos859c3012010-05-21 21:28:36 -03001656 return error("There are still logs under '%s'",
Thomas Rast157aaea2010-06-10 14:54:03 +02001657 logfile);
Erick Mattos859c3012010-05-21 21:28:36 -03001658 }
Thomas Rast157aaea2010-06-10 14:54:03 +02001659 logfd = open(logfile, oflags, 0666);
Erick Mattos859c3012010-05-21 21:28:36 -03001660 }
1661
1662 if (logfd < 0)
1663 return error("Unable to append to %s: %s",
Thomas Rast157aaea2010-06-10 14:54:03 +02001664 logfile, strerror(errno));
Erick Mattos859c3012010-05-21 21:28:36 -03001665 }
1666
Thomas Rast157aaea2010-06-10 14:54:03 +02001667 adjust_shared_perm(logfile);
Erick Mattos859c3012010-05-21 21:28:36 -03001668 close(logfd);
1669 return 0;
1670}
1671
Michael Haggertydfefa932011-12-12 06:38:09 +01001672static int log_ref_write(const char *refname, const unsigned char *old_sha1,
Nicolas Pitre9a13f0b2007-01-26 17:26:05 -05001673 const unsigned char *new_sha1, const char *msg)
Shawn Pearce6de08ae2006-05-17 05:55:40 -04001674{
Erick Mattos859c3012010-05-21 21:28:36 -03001675 int logfd, result, written, oflags = O_APPEND | O_WRONLY;
Shawn Pearce6de08ae2006-05-17 05:55:40 -04001676 unsigned maxlen, len;
Junio C Hamano8ac65932007-01-26 02:26:04 -08001677 int msglen;
Thomas Rast157aaea2010-06-10 14:54:03 +02001678 char log_file[PATH_MAX];
Alex Riesen958a4782008-10-27 11:11:40 +01001679 char *logrec;
Alp Tokerff4c8482006-07-09 10:36:24 +01001680 const char *committer;
Shawn Pearce6de08ae2006-05-17 05:55:40 -04001681
Junio C Hamano510c5a82007-01-07 01:35:34 -08001682 if (log_all_ref_updates < 0)
Junio C Hamano7d1864c2007-01-07 02:00:28 -08001683 log_all_ref_updates = !is_bare_repository();
Junio C Hamano510c5a82007-01-07 01:35:34 -08001684
Michael Haggertydfefa932011-12-12 06:38:09 +01001685 result = log_ref_setup(refname, log_file, sizeof(log_file));
Erick Mattos859c3012010-05-21 21:28:36 -03001686 if (result)
1687 return result;
Nicolas Pitre9a13f0b2007-01-26 17:26:05 -05001688
Erick Mattos859c3012010-05-21 21:28:36 -03001689 logfd = open(log_file, oflags);
1690 if (logfd < 0)
1691 return 0;
Junio C Hamano0ec29a42007-07-28 17:17:17 -07001692 msglen = msg ? strlen(msg) : 0;
Junio C Hamano774751a2007-12-08 17:32:08 -08001693 committer = git_committer_info(0);
Junio C Hamano8ac65932007-01-26 02:26:04 -08001694 maxlen = strlen(committer) + msglen + 100;
1695 logrec = xmalloc(maxlen);
1696 len = sprintf(logrec, "%s %s %s\n",
Nicolas Pitre9a13f0b2007-01-26 17:26:05 -05001697 sha1_to_hex(old_sha1),
1698 sha1_to_hex(new_sha1),
Junio C Hamano8ac65932007-01-26 02:26:04 -08001699 committer);
1700 if (msglen)
Junio C Hamano0ec29a42007-07-28 17:17:17 -07001701 len += copy_msg(logrec + len - 1, msg) - 1;
Andy Whitcroft93822c22007-01-08 15:58:23 +00001702 written = len <= maxlen ? write_in_full(logfd, logrec, len) : -1;
Shawn Pearce6de08ae2006-05-17 05:55:40 -04001703 free(logrec);
Jim Meyering91c8d592007-06-24 21:20:41 +02001704 if (close(logfd) != 0 || written != len)
Nicolas Pitre9a13f0b2007-01-26 17:26:05 -05001705 return error("Unable to append to %s", log_file);
Shawn Pearce6de08ae2006-05-17 05:55:40 -04001706 return 0;
1707}
1708
Linus Torvaldsc3b0dec2008-01-15 15:50:17 -08001709static int is_branch(const char *refname)
1710{
1711 return !strcmp(refname, "HEAD") || !prefixcmp(refname, "refs/heads/");
1712}
1713
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001714int write_ref_sha1(struct ref_lock *lock,
1715 const unsigned char *sha1, const char *logmsg)
1716{
1717 static char term = '\n';
Linus Torvaldsc3b0dec2008-01-15 15:50:17 -08001718 struct object *o;
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001719
1720 if (!lock)
1721 return -1;
David Rientjesa89fccd2006-08-17 11:54:57 -07001722 if (!lock->force_write && !hashcmp(lock->old_sha1, sha1)) {
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001723 unlock_ref(lock);
1724 return 0;
1725 }
Linus Torvaldsc3b0dec2008-01-15 15:50:17 -08001726 o = parse_object(sha1);
1727 if (!o) {
Dmitry Ivankov7be8b3b2011-06-16 19:42:48 +06001728 error("Trying to write ref %s with nonexistent object %s",
Linus Torvaldsc3b0dec2008-01-15 15:50:17 -08001729 lock->ref_name, sha1_to_hex(sha1));
1730 unlock_ref(lock);
1731 return -1;
1732 }
1733 if (o->type != OBJ_COMMIT && is_branch(lock->ref_name)) {
1734 error("Trying to write non-commit object %s to branch %s",
1735 sha1_to_hex(sha1), lock->ref_name);
1736 unlock_ref(lock);
1737 return -1;
1738 }
Andy Whitcroft93822c22007-01-08 15:58:23 +00001739 if (write_in_full(lock->lock_fd, sha1_to_hex(sha1), 40) != 40 ||
1740 write_in_full(lock->lock_fd, &term, 1) != 1
Brandon Caseyb5313942008-01-16 13:14:30 -06001741 || close_ref(lock) < 0) {
Junio C Hamanoc33d5172006-06-06 13:54:14 -07001742 error("Couldn't write %s", lock->lk->filename);
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001743 unlock_ref(lock);
1744 return -1;
1745 }
Michael Haggerty8bf90dc2011-10-17 04:38:10 +02001746 clear_loose_ref_cache(get_ref_cache(NULL));
Nicolas Pitrebd104db2007-01-26 17:26:07 -05001747 if (log_ref_write(lock->ref_name, lock->old_sha1, sha1, logmsg) < 0 ||
1748 (strcmp(lock->ref_name, lock->orig_ref_name) &&
1749 log_ref_write(lock->orig_ref_name, lock->old_sha1, sha1, logmsg) < 0)) {
Shawn Pearce6de08ae2006-05-17 05:55:40 -04001750 unlock_ref(lock);
1751 return -1;
1752 }
Nicolas Pitre605fac82007-03-21 17:11:44 -04001753 if (strcmp(lock->orig_ref_name, "HEAD") != 0) {
1754 /*
1755 * Special hack: If a branch is updated directly and HEAD
1756 * points to it (may happen on the remote side of a push
1757 * for example) then logically the HEAD reflog should be
1758 * updated too.
1759 * A generic solution implies reverse symref information,
1760 * but finding all symrefs pointing to the given branch
1761 * would be rather costly for this rare event (the direct
1762 * update of a branch) to be worth it. So let's cheat and
1763 * check with HEAD only which should cover 99% of all usage
1764 * scenarios (even 100% of the default ones).
1765 */
1766 unsigned char head_sha1[20];
1767 int head_flag;
1768 const char *head_ref;
Nguyễn Thái Ngọc Duy8cad4742011-12-12 18:20:32 +07001769 head_ref = resolve_ref_unsafe("HEAD", head_sha1, 1, &head_flag);
Nicolas Pitre605fac82007-03-21 17:11:44 -04001770 if (head_ref && (head_flag & REF_ISSYMREF) &&
1771 !strcmp(head_ref, lock->ref_name))
1772 log_ref_write("HEAD", lock->old_sha1, sha1, logmsg);
1773 }
Brandon Caseyb5313942008-01-16 13:14:30 -06001774 if (commit_ref(lock)) {
Linus Torvalds434cd0c2006-09-14 10:14:47 -07001775 error("Couldn't set %s", lock->ref_name);
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001776 unlock_ref(lock);
1777 return -1;
1778 }
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001779 unlock_ref(lock);
1780 return 0;
Daniel Barkalow95fc7512005-06-06 16:31:29 -04001781}
Shawn Pearced556fae2006-05-17 05:56:09 -04001782
Nicolas Pitre8b5157e2007-01-26 17:26:10 -05001783int create_symref(const char *ref_target, const char *refs_heads_master,
1784 const char *logmsg)
Nicolas Pitre41b625b2007-01-26 17:26:09 -05001785{
1786 const char *lockpath;
1787 char ref[1000];
1788 int fd, len, written;
Alex Riesena4f34cb2008-10-27 11:22:09 +01001789 char *git_HEAD = git_pathdup("%s", ref_target);
Nicolas Pitre8b5157e2007-01-26 17:26:10 -05001790 unsigned char old_sha1[20], new_sha1[20];
1791
1792 if (logmsg && read_ref(ref_target, old_sha1))
1793 hashclr(old_sha1);
Nicolas Pitre41b625b2007-01-26 17:26:09 -05001794
Junio C Hamanod48744d2007-02-07 23:41:43 -08001795 if (safe_create_leading_directories(git_HEAD) < 0)
1796 return error("unable to create directory for %s", git_HEAD);
1797
Nicolas Pitre41b625b2007-01-26 17:26:09 -05001798#ifndef NO_SYMLINK_HEAD
1799 if (prefer_symlink_refs) {
1800 unlink(git_HEAD);
1801 if (!symlink(refs_heads_master, git_HEAD))
Nicolas Pitre8b5157e2007-01-26 17:26:10 -05001802 goto done;
Nicolas Pitre41b625b2007-01-26 17:26:09 -05001803 fprintf(stderr, "no symlink - falling back to symbolic ref\n");
1804 }
1805#endif
1806
1807 len = snprintf(ref, sizeof(ref), "ref: %s\n", refs_heads_master);
1808 if (sizeof(ref) <= len) {
1809 error("refname too long: %s", refs_heads_master);
Junio C Hamano47fc52e2007-01-26 17:49:00 -08001810 goto error_free_return;
Nicolas Pitre41b625b2007-01-26 17:26:09 -05001811 }
1812 lockpath = mkpath("%s.lock", git_HEAD);
1813 fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666);
1814 if (fd < 0) {
1815 error("Unable to open %s for writing", lockpath);
Junio C Hamano47fc52e2007-01-26 17:49:00 -08001816 goto error_free_return;
Nicolas Pitre41b625b2007-01-26 17:26:09 -05001817 }
1818 written = write_in_full(fd, ref, len);
Jim Meyering91c8d592007-06-24 21:20:41 +02001819 if (close(fd) != 0 || written != len) {
Nicolas Pitre41b625b2007-01-26 17:26:09 -05001820 error("Unable to write to %s", lockpath);
Junio C Hamano47fc52e2007-01-26 17:49:00 -08001821 goto error_unlink_return;
Nicolas Pitre41b625b2007-01-26 17:26:09 -05001822 }
1823 if (rename(lockpath, git_HEAD) < 0) {
Nicolas Pitre41b625b2007-01-26 17:26:09 -05001824 error("Unable to create %s", git_HEAD);
Junio C Hamano47fc52e2007-01-26 17:49:00 -08001825 goto error_unlink_return;
Nicolas Pitre41b625b2007-01-26 17:26:09 -05001826 }
1827 if (adjust_shared_perm(git_HEAD)) {
Nicolas Pitre41b625b2007-01-26 17:26:09 -05001828 error("Unable to fix permissions on %s", lockpath);
Junio C Hamano47fc52e2007-01-26 17:49:00 -08001829 error_unlink_return:
Alex Riesen691f1a22009-04-29 23:22:56 +02001830 unlink_or_warn(lockpath);
Junio C Hamano47fc52e2007-01-26 17:49:00 -08001831 error_free_return:
1832 free(git_HEAD);
1833 return -1;
Nicolas Pitre41b625b2007-01-26 17:26:09 -05001834 }
Nicolas Pitre8b5157e2007-01-26 17:26:10 -05001835
Ramsay Jonesee96d112007-03-03 18:28:46 +00001836#ifndef NO_SYMLINK_HEAD
Nicolas Pitre8b5157e2007-01-26 17:26:10 -05001837 done:
Ramsay Jonesee96d112007-03-03 18:28:46 +00001838#endif
Nicolas Pitre8b5157e2007-01-26 17:26:10 -05001839 if (logmsg && !read_ref(refs_heads_master, new_sha1))
1840 log_ref_write(ref_target, old_sha1, new_sha1, logmsg);
1841
Junio C Hamano47fc52e2007-01-26 17:49:00 -08001842 free(git_HEAD);
Nicolas Pitre41b625b2007-01-26 17:26:09 -05001843 return 0;
1844}
1845
Junio C Hamano16d7cc92007-01-19 01:19:05 -08001846static char *ref_msg(const char *line, const char *endp)
1847{
1848 const char *ep;
Junio C Hamano16d7cc92007-01-19 01:19:05 -08001849 line += 82;
Pierre Habouzit182af832007-09-16 00:32:36 +02001850 ep = memchr(line, '\n', endp - line);
1851 if (!ep)
1852 ep = endp;
1853 return xmemdupz(line, ep - line);
Junio C Hamano16d7cc92007-01-19 01:19:05 -08001854}
1855
Michael Haggertydfefa932011-12-12 06:38:09 +01001856int read_ref_at(const char *refname, unsigned long at_time, int cnt,
1857 unsigned char *sha1, char **msg,
1858 unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt)
Shawn Pearced556fae2006-05-17 05:56:09 -04001859{
Shawn Pearcee5229042006-05-19 03:28:19 -04001860 const char *logfile, *logdata, *logend, *rec, *lastgt, *lastrec;
Shawn Pearced556fae2006-05-17 05:56:09 -04001861 char *tz_c;
Junio C Hamanoe29cb532006-12-18 22:07:45 -08001862 int logfd, tz, reccnt = 0;
Shawn Pearced556fae2006-05-17 05:56:09 -04001863 struct stat st;
1864 unsigned long date;
Shawn Pearcee5229042006-05-19 03:28:19 -04001865 unsigned char logged_sha1[20];
Junio C Hamanocb48cb52007-01-19 00:39:32 -08001866 void *log_mapped;
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05001867 size_t mapsz;
Shawn Pearced556fae2006-05-17 05:56:09 -04001868
Michael Haggertydfefa932011-12-12 06:38:09 +01001869 logfile = git_path("logs/%s", refname);
Shawn Pearced556fae2006-05-17 05:56:09 -04001870 logfd = open(logfile, O_RDONLY, 0);
1871 if (logfd < 0)
Thomas Rastd824cbb2009-06-27 17:58:46 +02001872 die_errno("Unable to read log '%s'", logfile);
Shawn Pearced556fae2006-05-17 05:56:09 -04001873 fstat(logfd, &st);
1874 if (!st.st_size)
1875 die("Log %s is empty.", logfile);
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05001876 mapsz = xsize_t(st.st_size);
1877 log_mapped = xmmap(NULL, mapsz, PROT_READ, MAP_PRIVATE, logfd, 0);
Junio C Hamanocb48cb52007-01-19 00:39:32 -08001878 logdata = log_mapped;
Shawn Pearced556fae2006-05-17 05:56:09 -04001879 close(logfd);
1880
Shawn Pearcee5229042006-05-19 03:28:19 -04001881 lastrec = NULL;
Shawn Pearced556fae2006-05-17 05:56:09 -04001882 rec = logend = logdata + st.st_size;
1883 while (logdata < rec) {
Junio C Hamanoe29cb532006-12-18 22:07:45 -08001884 reccnt++;
Shawn Pearced556fae2006-05-17 05:56:09 -04001885 if (logdata < rec && *(rec-1) == '\n')
1886 rec--;
Shawn Pearcee5229042006-05-19 03:28:19 -04001887 lastgt = NULL;
1888 while (logdata < rec && *(rec-1) != '\n') {
Shawn Pearced556fae2006-05-17 05:56:09 -04001889 rec--;
Shawn Pearcee5229042006-05-19 03:28:19 -04001890 if (*rec == '>')
1891 lastgt = rec;
1892 }
1893 if (!lastgt)
Shawn Pearced556fae2006-05-17 05:56:09 -04001894 die("Log %s is corrupt.", logfile);
Shawn Pearcee5229042006-05-19 03:28:19 -04001895 date = strtoul(lastgt + 1, &tz_c, 10);
Junio C Hamanoab2a1a32006-10-05 23:16:15 -07001896 if (date <= at_time || cnt == 0) {
Junio C Hamano76a44c52007-01-19 01:20:23 -08001897 tz = strtoul(tz_c, NULL, 10);
Junio C Hamano16d7cc92007-01-19 01:19:05 -08001898 if (msg)
1899 *msg = ref_msg(rec, logend);
1900 if (cutoff_time)
1901 *cutoff_time = date;
1902 if (cutoff_tz)
1903 *cutoff_tz = tz;
1904 if (cutoff_cnt)
Junio C Hamano76a44c52007-01-19 01:20:23 -08001905 *cutoff_cnt = reccnt - 1;
Shawn Pearcee5229042006-05-19 03:28:19 -04001906 if (lastrec) {
1907 if (get_sha1_hex(lastrec, logged_sha1))
1908 die("Log %s is corrupt.", logfile);
1909 if (get_sha1_hex(rec + 41, sha1))
1910 die("Log %s is corrupt.", logfile);
David Rientjesa89fccd2006-08-17 11:54:57 -07001911 if (hashcmp(logged_sha1, sha1)) {
Miklos Vajnaedbc25c2009-03-24 02:09:17 +01001912 warning("Log %s has gap after %s.",
Junio C Hamano73013af2007-07-13 23:14:52 -07001913 logfile, show_date(date, tz, DATE_RFC2822));
Shawn Pearcee5229042006-05-19 03:28:19 -04001914 }
Junio C Hamanoe5f38ec2006-06-06 14:04:17 -07001915 }
1916 else if (date == at_time) {
Shawn Pearcee5229042006-05-19 03:28:19 -04001917 if (get_sha1_hex(rec + 41, sha1))
1918 die("Log %s is corrupt.", logfile);
Junio C Hamanoe5f38ec2006-06-06 14:04:17 -07001919 }
1920 else {
Shawn Pearcee5229042006-05-19 03:28:19 -04001921 if (get_sha1_hex(rec + 41, logged_sha1))
1922 die("Log %s is corrupt.", logfile);
David Rientjesa89fccd2006-08-17 11:54:57 -07001923 if (hashcmp(logged_sha1, sha1)) {
Miklos Vajnaedbc25c2009-03-24 02:09:17 +01001924 warning("Log %s unexpectedly ended on %s.",
Junio C Hamano73013af2007-07-13 23:14:52 -07001925 logfile, show_date(date, tz, DATE_RFC2822));
Shawn Pearcee5229042006-05-19 03:28:19 -04001926 }
1927 }
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05001928 munmap(log_mapped, mapsz);
Shawn Pearced556fae2006-05-17 05:56:09 -04001929 return 0;
1930 }
Shawn Pearcee5229042006-05-19 03:28:19 -04001931 lastrec = rec;
Junio C Hamanoab2a1a32006-10-05 23:16:15 -07001932 if (cnt > 0)
1933 cnt--;
Shawn Pearced556fae2006-05-17 05:56:09 -04001934 }
1935
Shawn Pearcee5229042006-05-19 03:28:19 -04001936 rec = logdata;
1937 while (rec < logend && *rec != '>' && *rec != '\n')
1938 rec++;
1939 if (rec == logend || *rec == '\n')
Shawn Pearced556fae2006-05-17 05:56:09 -04001940 die("Log %s is corrupt.", logfile);
Shawn Pearcee5229042006-05-19 03:28:19 -04001941 date = strtoul(rec + 1, &tz_c, 10);
Shawn Pearced556fae2006-05-17 05:56:09 -04001942 tz = strtoul(tz_c, NULL, 10);
1943 if (get_sha1_hex(logdata, sha1))
1944 die("Log %s is corrupt.", logfile);
Jeff Kingd1a44892008-07-08 00:38:54 -04001945 if (is_null_sha1(sha1)) {
1946 if (get_sha1_hex(logdata + 41, sha1))
1947 die("Log %s is corrupt.", logfile);
1948 }
Junio C Hamano16d7cc92007-01-19 01:19:05 -08001949 if (msg)
1950 *msg = ref_msg(logdata, logend);
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05001951 munmap(log_mapped, mapsz);
Junio C Hamano16d7cc92007-01-19 01:19:05 -08001952
1953 if (cutoff_time)
1954 *cutoff_time = date;
1955 if (cutoff_tz)
1956 *cutoff_tz = tz;
1957 if (cutoff_cnt)
1958 *cutoff_cnt = reccnt;
1959 return 1;
Shawn Pearced556fae2006-05-17 05:56:09 -04001960}
Junio C Hamano2ff81662006-12-18 01:18:16 -08001961
Michael Haggertydfefa932011-12-12 06:38:09 +01001962int for_each_recent_reflog_ent(const char *refname, each_reflog_ent_fn fn, long ofs, void *cb_data)
Junio C Hamano2ff81662006-12-18 01:18:16 -08001963{
1964 const char *logfile;
1965 FILE *logfp;
René Scharfe8ca78802010-03-13 18:37:50 +01001966 struct strbuf sb = STRBUF_INIT;
Junio C Hamano2266bf22007-01-18 23:25:54 -08001967 int ret = 0;
Junio C Hamano2ff81662006-12-18 01:18:16 -08001968
Michael Haggertydfefa932011-12-12 06:38:09 +01001969 logfile = git_path("logs/%s", refname);
Junio C Hamano2ff81662006-12-18 01:18:16 -08001970 logfp = fopen(logfile, "r");
1971 if (!logfp)
Johannes Schindelin883d60f2007-01-08 01:59:54 +01001972 return -1;
Junio C Hamano101d15e2009-01-19 22:18:29 -08001973
1974 if (ofs) {
1975 struct stat statbuf;
1976 if (fstat(fileno(logfp), &statbuf) ||
1977 statbuf.st_size < ofs ||
1978 fseek(logfp, -ofs, SEEK_END) ||
René Scharfe8ca78802010-03-13 18:37:50 +01001979 strbuf_getwholeline(&sb, logfp, '\n')) {
Brandon Casey9d33f7c2009-07-16 16:25:18 -05001980 fclose(logfp);
René Scharfe8ca78802010-03-13 18:37:50 +01001981 strbuf_release(&sb);
Junio C Hamano101d15e2009-01-19 22:18:29 -08001982 return -1;
Brandon Casey9d33f7c2009-07-16 16:25:18 -05001983 }
Junio C Hamano101d15e2009-01-19 22:18:29 -08001984 }
1985
René Scharfe8ca78802010-03-13 18:37:50 +01001986 while (!strbuf_getwholeline(&sb, logfp, '\n')) {
Junio C Hamano2ff81662006-12-18 01:18:16 -08001987 unsigned char osha1[20], nsha1[20];
Johannes Schindelin883d60f2007-01-08 01:59:54 +01001988 char *email_end, *message;
1989 unsigned long timestamp;
René Scharfe8ca78802010-03-13 18:37:50 +01001990 int tz;
Junio C Hamano2ff81662006-12-18 01:18:16 -08001991
1992 /* old SP new SP name <email> SP time TAB msg LF */
René Scharfe8ca78802010-03-13 18:37:50 +01001993 if (sb.len < 83 || sb.buf[sb.len - 1] != '\n' ||
1994 get_sha1_hex(sb.buf, osha1) || sb.buf[40] != ' ' ||
1995 get_sha1_hex(sb.buf + 41, nsha1) || sb.buf[81] != ' ' ||
1996 !(email_end = strchr(sb.buf + 82, '>')) ||
Johannes Schindelin883d60f2007-01-08 01:59:54 +01001997 email_end[1] != ' ' ||
1998 !(timestamp = strtoul(email_end + 2, &message, 10)) ||
1999 !message || message[0] != ' ' ||
2000 (message[1] != '+' && message[1] != '-') ||
2001 !isdigit(message[2]) || !isdigit(message[3]) ||
Johannes Schindelinb4dd4852007-02-09 00:59:47 +01002002 !isdigit(message[4]) || !isdigit(message[5]))
Junio C Hamano2ff81662006-12-18 01:18:16 -08002003 continue; /* corrupt? */
Johannes Schindelin883d60f2007-01-08 01:59:54 +01002004 email_end[1] = '\0';
2005 tz = strtol(message + 1, NULL, 10);
Johannes Schindelinb4dd4852007-02-09 00:59:47 +01002006 if (message[6] != '\t')
2007 message += 6;
2008 else
2009 message += 7;
René Scharfe8ca78802010-03-13 18:37:50 +01002010 ret = fn(osha1, nsha1, sb.buf + 82, timestamp, tz, message,
2011 cb_data);
Johannes Schindelin883d60f2007-01-08 01:59:54 +01002012 if (ret)
Junio C Hamano2266bf22007-01-18 23:25:54 -08002013 break;
Junio C Hamano2ff81662006-12-18 01:18:16 -08002014 }
2015 fclose(logfp);
René Scharfe8ca78802010-03-13 18:37:50 +01002016 strbuf_release(&sb);
Junio C Hamano2266bf22007-01-18 23:25:54 -08002017 return ret;
Junio C Hamano2ff81662006-12-18 01:18:16 -08002018}
Junio C Hamanoe29cb532006-12-18 22:07:45 -08002019
Michael Haggertydfefa932011-12-12 06:38:09 +01002020int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn, void *cb_data)
Junio C Hamano101d15e2009-01-19 22:18:29 -08002021{
Michael Haggertydfefa932011-12-12 06:38:09 +01002022 return for_each_recent_reflog_ent(refname, fn, 0, cb_data);
Junio C Hamano101d15e2009-01-19 22:18:29 -08002023}
2024
Nicolas Pitreeb8381c2007-02-03 13:25:43 -05002025static int do_for_each_reflog(const char *base, each_ref_fn fn, void *cb_data)
2026{
Michael Haggertyd3177272012-04-10 07:30:24 +02002027 DIR *d = opendir(git_path("logs/%s", base));
Junio C Hamanofcee5a12007-02-07 09:18:57 -08002028 int retval = 0;
Nicolas Pitreeb8381c2007-02-03 13:25:43 -05002029
Michael Haggertyd3177272012-04-10 07:30:24 +02002030 if (d) {
Nicolas Pitreeb8381c2007-02-03 13:25:43 -05002031 struct dirent *de;
2032 int baselen = strlen(base);
2033 char *log = xmalloc(baselen + 257);
2034
2035 memcpy(log, base, baselen);
2036 if (baselen && base[baselen-1] != '/')
2037 log[baselen++] = '/';
2038
Michael Haggertyd3177272012-04-10 07:30:24 +02002039 while ((de = readdir(d)) != NULL) {
Nicolas Pitreeb8381c2007-02-03 13:25:43 -05002040 struct stat st;
2041 int namelen;
2042
2043 if (de->d_name[0] == '.')
2044 continue;
2045 namelen = strlen(de->d_name);
2046 if (namelen > 255)
2047 continue;
2048 if (has_extension(de->d_name, ".lock"))
2049 continue;
2050 memcpy(log + baselen, de->d_name, namelen+1);
2051 if (stat(git_path("logs/%s", log), &st) < 0)
2052 continue;
2053 if (S_ISDIR(st.st_mode)) {
2054 retval = do_for_each_reflog(log, fn, cb_data);
2055 } else {
2056 unsigned char sha1[20];
Nguyễn Thái Ngọc Duyc6893322011-11-13 17:22:14 +07002057 if (read_ref_full(log, sha1, 0, NULL))
Nicolas Pitreeb8381c2007-02-03 13:25:43 -05002058 retval = error("bad ref for %s", log);
2059 else
2060 retval = fn(log, sha1, 0, cb_data);
2061 }
2062 if (retval)
2063 break;
2064 }
2065 free(log);
Michael Haggertyd3177272012-04-10 07:30:24 +02002066 closedir(d);
Nicolas Pitreeb8381c2007-02-03 13:25:43 -05002067 }
Junio C Hamanoacb39f62007-02-12 23:21:34 -08002068 else if (*base)
Junio C Hamanofcee5a12007-02-07 09:18:57 -08002069 return errno;
Nicolas Pitreeb8381c2007-02-03 13:25:43 -05002070 return retval;
2071}
2072
2073int for_each_reflog(each_ref_fn fn, void *cb_data)
2074{
2075 return do_for_each_reflog("", fn, cb_data);
2076}
Carlos Rica3d9f0372007-09-05 03:38:24 +02002077
2078int update_ref(const char *action, const char *refname,
2079 const unsigned char *sha1, const unsigned char *oldval,
2080 int flags, enum action_on_err onerr)
2081{
2082 static struct ref_lock *lock;
2083 lock = lock_any_ref_for_update(refname, oldval, flags);
2084 if (!lock) {
2085 const char *str = "Cannot lock the ref '%s'.";
2086 switch (onerr) {
2087 case MSG_ON_ERR: error(str, refname); break;
2088 case DIE_ON_ERR: die(str, refname); break;
2089 case QUIET_ON_ERR: break;
2090 }
2091 return 1;
2092 }
2093 if (write_ref_sha1(lock, sha1, action) < 0) {
2094 const char *str = "Cannot update the ref '%s'.";
2095 switch (onerr) {
2096 case MSG_ON_ERR: error(str, refname); break;
2097 case DIE_ON_ERR: die(str, refname); break;
2098 case QUIET_ON_ERR: break;
2099 }
2100 return 1;
2101 }
2102 return 0;
2103}
Jeff Kingcda69f42007-11-18 02:13:10 -05002104
Jeff King5483f792009-02-25 03:32:10 -05002105struct ref *find_ref_by_name(const struct ref *list, const char *name)
Jeff Kingcda69f42007-11-18 02:13:10 -05002106{
2107 for ( ; list; list = list->next)
2108 if (!strcmp(list->name, name))
Jeff King5483f792009-02-25 03:32:10 -05002109 return (struct ref *)list;
Jeff Kingcda69f42007-11-18 02:13:10 -05002110 return NULL;
2111}
Jeff King7c2b3022009-04-07 03:14:20 -04002112
2113/*
2114 * generate a format suitable for scanf from a ref_rev_parse_rules
2115 * rule, that is replace the "%.*s" spec with a "%s" spec
2116 */
2117static void gen_scanf_fmt(char *scanf_fmt, const char *rule)
2118{
2119 char *spec;
2120
2121 spec = strstr(rule, "%.*s");
2122 if (!spec || strstr(spec + 4, "%.*s"))
2123 die("invalid rule in ref_rev_parse_rules: %s", rule);
2124
2125 /* copy all until spec */
2126 strncpy(scanf_fmt, rule, spec - rule);
2127 scanf_fmt[spec - rule] = '\0';
2128 /* copy new spec */
2129 strcat(scanf_fmt, "%s");
2130 /* copy remaining rule */
2131 strcat(scanf_fmt, spec + 4);
2132
2133 return;
2134}
2135
Michael Haggertydfefa932011-12-12 06:38:09 +01002136char *shorten_unambiguous_ref(const char *refname, int strict)
Jeff King7c2b3022009-04-07 03:14:20 -04002137{
2138 int i;
2139 static char **scanf_fmts;
2140 static int nr_rules;
2141 char *short_name;
2142
2143 /* pre generate scanf formats from ref_rev_parse_rules[] */
2144 if (!nr_rules) {
2145 size_t total_len = 0;
2146
2147 /* the rule list is NULL terminated, count them first */
2148 for (; ref_rev_parse_rules[nr_rules]; nr_rules++)
2149 /* no +1 because strlen("%s") < strlen("%.*s") */
2150 total_len += strlen(ref_rev_parse_rules[nr_rules]);
2151
2152 scanf_fmts = xmalloc(nr_rules * sizeof(char *) + total_len);
2153
2154 total_len = 0;
2155 for (i = 0; i < nr_rules; i++) {
2156 scanf_fmts[i] = (char *)&scanf_fmts[nr_rules]
2157 + total_len;
2158 gen_scanf_fmt(scanf_fmts[i], ref_rev_parse_rules[i]);
2159 total_len += strlen(ref_rev_parse_rules[i]);
2160 }
2161 }
2162
2163 /* bail out if there are no rules */
2164 if (!nr_rules)
Michael Haggertydfefa932011-12-12 06:38:09 +01002165 return xstrdup(refname);
Jeff King7c2b3022009-04-07 03:14:20 -04002166
Michael Haggertydfefa932011-12-12 06:38:09 +01002167 /* buffer for scanf result, at most refname must fit */
2168 short_name = xstrdup(refname);
Jeff King7c2b3022009-04-07 03:14:20 -04002169
2170 /* skip first rule, it will always match */
2171 for (i = nr_rules - 1; i > 0 ; --i) {
2172 int j;
Bert Wesarg6e7b3302009-04-13 12:25:46 +02002173 int rules_to_fail = i;
Jeff King7c2b3022009-04-07 03:14:20 -04002174 int short_name_len;
2175
Michael Haggertydfefa932011-12-12 06:38:09 +01002176 if (1 != sscanf(refname, scanf_fmts[i], short_name))
Jeff King7c2b3022009-04-07 03:14:20 -04002177 continue;
2178
2179 short_name_len = strlen(short_name);
2180
2181 /*
Bert Wesarg6e7b3302009-04-13 12:25:46 +02002182 * in strict mode, all (except the matched one) rules
2183 * must fail to resolve to a valid non-ambiguous ref
2184 */
2185 if (strict)
2186 rules_to_fail = nr_rules;
2187
2188 /*
Jeff King7c2b3022009-04-07 03:14:20 -04002189 * check if the short name resolves to a valid ref,
2190 * but use only rules prior to the matched one
2191 */
Bert Wesarg6e7b3302009-04-13 12:25:46 +02002192 for (j = 0; j < rules_to_fail; j++) {
Jeff King7c2b3022009-04-07 03:14:20 -04002193 const char *rule = ref_rev_parse_rules[j];
Jeff King7c2b3022009-04-07 03:14:20 -04002194 char refname[PATH_MAX];
2195
Bert Wesarg6e7b3302009-04-13 12:25:46 +02002196 /* skip matched rule */
2197 if (i == j)
2198 continue;
2199
Jeff King7c2b3022009-04-07 03:14:20 -04002200 /*
2201 * the short name is ambiguous, if it resolves
2202 * (with this previous rule) to a valid ref
2203 * read_ref() returns 0 on success
2204 */
2205 mksnpath(refname, sizeof(refname),
2206 rule, short_name_len, short_name);
Nguyễn Thái Ngọc Duyc6893322011-11-13 17:22:14 +07002207 if (ref_exists(refname))
Jeff King7c2b3022009-04-07 03:14:20 -04002208 break;
2209 }
2210
2211 /*
2212 * short name is non-ambiguous if all previous rules
2213 * haven't resolved to a valid ref
2214 */
Bert Wesarg6e7b3302009-04-13 12:25:46 +02002215 if (j == rules_to_fail)
Jeff King7c2b3022009-04-07 03:14:20 -04002216 return short_name;
2217 }
2218
2219 free(short_name);
Michael Haggertydfefa932011-12-12 06:38:09 +01002220 return xstrdup(refname);
Jeff King7c2b3022009-04-07 03:14:20 -04002221}