🌐 AI搜索 & 代理 主页
blob: 4e15f60d98ea8affdef226bce199935fa694b195 [file] [log] [blame]
Daniel Barkalow95fc7512005-06-06 20:31:291#include "cache.h"
Michael Haggerty697cc8e2014-10-01 10:28:422#include "lockfile.h"
Junio C Hamano85023572006-12-19 22:34:123#include "refs.h"
Junio C Hamanocf0adba2006-11-19 21:22:444#include "object.h"
5#include "tag.h"
Johannes Schindelin7155b722007-09-28 15:28:546#include "dir.h"
Junio C Hamanodaebaa72013-01-19 00:08:307#include "string-list.h"
Daniel Barkalow95fc7512005-06-06 20:31:298
Stefan Beller3581d792014-12-12 08:57:029struct ref_lock {
10 char *ref_name;
11 char *orig_ref_name;
12 struct lock_file *lk;
Michael Haggerty5cb901a2015-05-25 18:39:2213 struct object_id old_oid;
Stefan Beller3581d792014-12-12 08:57:0214};
15
Michael Haggertybc5fd6d2012-04-10 05:30:1316/*
David Turnerdde8a902014-06-04 03:38:1017 * How to handle various characters in refnames:
18 * 0: An acceptable character for refs
Junio C Hamano5e650222014-07-28 17:41:5319 * 1: End-of-component
20 * 2: ., look for a preceding . to reject .. in refs
21 * 3: {, look for a preceding @ to reject @{ in refs
Jacob Keller53a85552015-07-22 21:05:3222 * 4: A bad character: ASCII control characters, and
Jacob Kellercd377f42015-07-22 21:05:3323 * ":", "?", "[", "\", "^", "~", SP, or TAB
24 * 5: *, reject unless REFNAME_REFSPEC_PATTERN is set
David Turnerdde8a902014-06-04 03:38:1025 */
26static unsigned char refname_disposition[256] = {
Junio C Hamano5e650222014-07-28 17:41:5327 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
28 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
Jacob Kellercd377f42015-07-22 21:05:3329 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 2, 1,
Junio C Hamano5e650222014-07-28 17:41:5330 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 4,
David Turnerdde8a902014-06-04 03:38:1031 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Junio C Hamano5e650222014-07-28 17:41:5332 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 4, 0,
33 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
34 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 4, 4
David Turnerdde8a902014-06-04 03:38:1035};
36
37/*
Michael Haggerty581d4e02015-02-12 11:12:1238 * Flag passed to lock_ref_sha1_basic() telling it to tolerate broken
39 * refs (i.e., because the reference is about to be deleted anyway).
40 */
41#define REF_DELETING 0x02
42
43/*
Michael Haggerty8df4e512015-02-17 17:00:1444 * Used as a flag in ref_update::flags when a loose ref is being
Ronnie Sahlberg029cdb42014-04-30 16:03:3645 * pruned.
46 */
Michael Haggerty31e79f02015-02-12 11:12:1347#define REF_ISPRUNING 0x04
48
Ronnie Sahlberg029cdb42014-04-30 16:03:3649/*
Michael Haggerty16180332015-02-17 17:00:2150 * Used as a flag in ref_update::flags when the reference should be
51 * updated to new_sha1.
52 */
53#define REF_HAVE_NEW 0x08
54
55/*
Michael Haggerty8df4e512015-02-17 17:00:1456 * Used as a flag in ref_update::flags when old_sha1 should be
57 * checked.
58 */
Michael Haggerty16180332015-02-17 17:00:2159#define REF_HAVE_OLD 0x10
Michael Haggerty8df4e512015-02-17 17:00:1460
61/*
Michael Haggertycf018ee2015-04-24 11:35:4962 * Used as a flag in ref_update::flags when the lockfile needs to be
63 * committed.
64 */
65#define REF_NEEDS_COMMIT 0x20
66
67/*
David Turner0f2a71d2015-07-21 21:04:5468 * 0x40 is REF_FORCE_CREATE_REFLOG, so skip it if you're adding a
69 * value to ref_update::flags
70 */
71
72/*
David Turnerdde8a902014-06-04 03:38:1073 * Try to read one refname component from the front of refname.
74 * Return the length of the component found, or -1 if the component is
75 * not legal. It is legal if it is something reasonable to have under
76 * ".git/refs/"; We do not like it if:
Michael Haggertybc5fd6d2012-04-10 05:30:1377 *
78 * - any path component of it begins with ".", or
79 * - it has double dots "..", or
Jacob Keller53a85552015-07-22 21:05:3280 * - it has ASCII control characters, or
Jacob Kellercd377f42015-07-22 21:05:3381 * - it has ":", "?", "[", "\", "^", "~", SP, or TAB anywhere, or
82 * - it has "*" anywhere unless REFNAME_REFSPEC_PATTERN is set, or
Jacob Keller53a85552015-07-22 21:05:3283 * - it ends with a "/", or
84 * - it ends with ".lock", or
85 * - it contains a "@{" portion
Michael Haggertybc5fd6d2012-04-10 05:30:1386 */
Jacob Kellercd377f42015-07-22 21:05:3387static int check_refname_component(const char *refname, int *flags)
Michael Haggertybc5fd6d2012-04-10 05:30:1388{
89 const char *cp;
90 char last = '\0';
91
92 for (cp = refname; ; cp++) {
David Turnerdde8a902014-06-04 03:38:1093 int ch = *cp & 255;
94 unsigned char disp = refname_disposition[ch];
95 switch (disp) {
Junio C Hamano5e650222014-07-28 17:41:5396 case 1:
David Turnerdde8a902014-06-04 03:38:1097 goto out;
Junio C Hamano5e650222014-07-28 17:41:5398 case 2:
David Turnerdde8a902014-06-04 03:38:1099 if (last == '.')
100 return -1; /* Refname contains "..". */
Michael Haggertybc5fd6d2012-04-10 05:30:13101 break;
Junio C Hamano5e650222014-07-28 17:41:53102 case 3:
David Turnerdde8a902014-06-04 03:38:10103 if (last == '@')
104 return -1; /* Refname contains "@{". */
105 break;
Junio C Hamano5e650222014-07-28 17:41:53106 case 4:
David Turnerdde8a902014-06-04 03:38:10107 return -1;
Jacob Kellercd377f42015-07-22 21:05:33108 case 5:
109 if (!(*flags & REFNAME_REFSPEC_PATTERN))
110 return -1; /* refspec can't be a pattern */
111
112 /*
113 * Unset the pattern flag so that we only accept
114 * a single asterisk for one side of refspec.
115 */
116 *flags &= ~ REFNAME_REFSPEC_PATTERN;
117 break;
David Turnerdde8a902014-06-04 03:38:10118 }
Michael Haggertybc5fd6d2012-04-10 05:30:13119 last = ch;
120 }
David Turnerdde8a902014-06-04 03:38:10121out:
Michael Haggertybc5fd6d2012-04-10 05:30:13122 if (cp == refname)
Michael Haggertydac529e2012-04-10 05:30:22123 return 0; /* Component has zero length. */
Jonathan Niederf3cc52d2014-09-26 19:22:22124 if (refname[0] == '.')
125 return -1; /* Component starts with '.'. */
Michael Haggerty7108ad22014-10-01 10:28:15126 if (cp - refname >= LOCK_SUFFIX_LEN &&
127 !memcmp(cp - LOCK_SUFFIX_LEN, LOCK_SUFFIX, LOCK_SUFFIX_LEN))
Michael Haggertybc5fd6d2012-04-10 05:30:13128 return -1; /* Refname ends with ".lock". */
129 return cp - refname;
130}
131
Junio C Hamano5e650222014-07-28 17:41:53132int check_refname_format(const char *refname, int flags)
Michael Haggertybc5fd6d2012-04-10 05:30:13133{
134 int component_len, component_count = 0;
135
Felipe Contreras9ba89f42013-09-02 06:34:30136 if (!strcmp(refname, "@"))
137 /* Refname is a single character '@'. */
138 return -1;
139
Michael Haggertybc5fd6d2012-04-10 05:30:13140 while (1) {
141 /* We are at the start of a path component. */
Jacob Kellercd377f42015-07-22 21:05:33142 component_len = check_refname_component(refname, &flags);
143 if (component_len <= 0)
144 return -1;
145
Michael Haggertybc5fd6d2012-04-10 05:30:13146 component_count++;
147 if (refname[component_len] == '\0')
148 break;
149 /* Skip to next component. */
150 refname += component_len + 1;
151 }
152
153 if (refname[component_len - 1] == '.')
154 return -1; /* Refname ends with '.'. */
155 if (!(flags & REFNAME_ALLOW_ONELEVEL) && component_count < 2)
156 return -1; /* Refname has only one component. */
157 return 0;
158}
159
160struct ref_entry;
Linus Torvaldse1e22e32006-09-11 23:37:32161
Michael Haggerty28e6a342012-04-26 22:27:07162/*
163 * Information used (along with the information in ref_entry) to
164 * describe a single cached reference. This data structure only
165 * occurs embedded in a union in struct ref_entry, and only when
166 * (ref_entry->flag & REF_DIR) is zero.
167 */
Michael Haggerty593f1bb2012-04-10 05:30:23168struct ref_value {
Michael Haggerty6c6f58d2013-04-14 12:54:17169 /*
170 * The name of the object to which this reference resolves
171 * (which may be a tag object). If REF_ISBROKEN, this is
172 * null. If REF_ISSYMREF, then this is the name of the object
173 * referred to by the last reference in the symlink chain.
174 */
brian m. carlson83538472015-05-25 18:38:27175 struct object_id oid;
Michael Haggerty6c6f58d2013-04-14 12:54:17176
177 /*
178 * If REF_KNOWS_PEELED, then this field holds the peeled value
179 * of this reference, or null if the reference is known not to
Michael Haggerty2312a792013-04-22 19:52:21180 * be peelable. See the documentation for peel_ref() for an
181 * exact definition of "peelable".
Michael Haggerty6c6f58d2013-04-14 12:54:17182 */
brian m. carlson83538472015-05-25 18:38:27183 struct object_id peeled;
Michael Haggerty593f1bb2012-04-10 05:30:23184};
185
Michael Haggertyf006c422012-04-26 22:27:05186struct ref_cache;
187
Michael Haggerty28e6a342012-04-26 22:27:07188/*
189 * Information used (along with the information in ref_entry) to
190 * describe a level in the hierarchy of references. This data
191 * structure only occurs embedded in a union in struct ref_entry, and
192 * only when (ref_entry.flag & REF_DIR) is set. In that case,
193 * (ref_entry.flag & REF_INCOMPLETE) determines whether the references
194 * in the directory have already been read:
195 *
196 * (ref_entry.flag & REF_INCOMPLETE) unset -- a directory of loose
197 * or packed references, already read.
198 *
199 * (ref_entry.flag & REF_INCOMPLETE) set -- a directory of loose
200 * references that hasn't been read yet (nor has any of its
201 * subdirectories).
202 *
203 * Entries within a directory are stored within a growable array of
204 * pointers to ref_entries (entries, nr, alloc). Entries 0 <= i <
205 * sorted are sorted by their component name in strcmp() order and the
206 * remaining entries are unsorted.
207 *
208 * Loose references are read lazily, one directory at a time. When a
209 * directory of loose references is read, then all of the references
210 * in that directory are stored, and REF_INCOMPLETE stubs are created
211 * for any subdirectories, but the subdirectories themselves are not
212 * read. The reading is triggered by get_ref_dir().
213 */
Michael Haggertyd3177272012-04-10 05:30:24214struct ref_dir {
Julian Phillipse9c4c112011-09-29 22:11:42215 int nr, alloc;
Michael Haggertye6ed3ca2012-01-17 05:50:32216
217 /*
218 * Entries with index 0 <= i < sorted are sorted by name. New
219 * entries are appended to the list unsorted, and are sorted
220 * only when required; thus we avoid the need to sort the list
221 * after the addition of every reference.
222 */
223 int sorted;
224
Michael Haggertyf006c422012-04-26 22:27:05225 /* A pointer to the ref_cache that contains this ref_dir. */
226 struct ref_cache *ref_cache;
227
Michael Haggertyd3177272012-04-10 05:30:24228 struct ref_entry **entries;
Julian Phillipse9c4c112011-09-29 22:11:42229};
230
Michael Haggerty89df9c82013-04-14 12:54:16231/*
232 * Bit values for ref_entry::flag. REF_ISSYMREF=0x01,
Ronnie Sahlbergd0f810f2014-09-03 18:45:43233 * REF_ISPACKED=0x02, REF_ISBROKEN=0x04 and REF_BAD_NAME=0x08 are
234 * public values; see refs.h.
Michael Haggerty89df9c82013-04-14 12:54:16235 */
236
237/*
238 * The field ref_entry->u.value.peeled of this value entry contains
239 * the correct peeled value for the reference, which might be
240 * null_sha1 if the reference is not a tag or if it is broken.
241 */
Ronnie Sahlbergd0f810f2014-09-03 18:45:43242#define REF_KNOWS_PEELED 0x10
Michael Haggerty28e6a342012-04-26 22:27:07243
244/* ref_entry represents a directory of references */
Ronnie Sahlbergd0f810f2014-09-03 18:45:43245#define REF_DIR 0x20
Linus Torvaldse1e22e32006-09-11 23:37:32246
Michael Haggerty432ad412012-04-10 05:30:26247/*
Michael Haggerty28e6a342012-04-26 22:27:07248 * Entry has not yet been read from disk (used only for REF_DIR
249 * entries representing loose references)
250 */
Ronnie Sahlbergd0f810f2014-09-03 18:45:43251#define REF_INCOMPLETE 0x40
Michael Haggerty28e6a342012-04-26 22:27:07252
253/*
Michael Haggerty432ad412012-04-10 05:30:26254 * A ref_entry represents either a reference or a "subdirectory" of
Michael Haggerty28e6a342012-04-26 22:27:07255 * references.
256 *
257 * Each directory in the reference namespace is represented by a
258 * ref_entry with (flags & REF_DIR) set and containing a subdir member
259 * that holds the entries in that directory that have been read so
260 * far. If (flags & REF_INCOMPLETE) is set, then the directory and
261 * its subdirectories haven't been read yet. REF_INCOMPLETE is only
262 * used for loose reference directories.
263 *
264 * References are represented by a ref_entry with (flags & REF_DIR)
265 * unset and a value member that describes the reference's value. The
266 * flag member is at the ref_entry level, but it is also needed to
267 * interpret the contents of the value field (in other words, a
268 * ref_value object is not very much use without the enclosing
269 * ref_entry).
Michael Haggerty432ad412012-04-10 05:30:26270 *
271 * Reference names cannot end with slash and directories' names are
272 * always stored with a trailing slash (except for the top-level
273 * directory, which is always denoted by ""). This has two nice
274 * consequences: (1) when the entries in each subdir are sorted
275 * lexicographically by name (as they usually are), the references in
276 * a whole tree can be generated in lexicographic order by traversing
277 * the tree in left-to-right, depth-first order; (2) the names of
278 * references and subdirectories cannot conflict, and therefore the
279 * presence of an empty subdirectory does not block the creation of a
280 * similarly-named reference. (The fact that reference names with the
281 * same leading components can conflict *with each other* is a
Michael Haggerty5baf37d2015-05-11 15:25:13282 * separate issue that is regulated by verify_refname_available().)
Michael Haggerty432ad412012-04-10 05:30:26283 *
284 * Please note that the name field contains the fully-qualified
285 * reference (or subdirectory) name. Space could be saved by only
286 * storing the relative names. But that would require the full names
287 * to be generated on the fly when iterating in do_for_each_ref(), and
288 * would break callback functions, who have always been able to assume
289 * that the name strings that they are passed will not be freed during
290 * the iteration.
291 */
Michael Haggertybc5fd6d2012-04-10 05:30:13292struct ref_entry {
293 unsigned char flag; /* ISSYMREF? ISPACKED? */
Michael Haggerty593f1bb2012-04-10 05:30:23294 union {
Michael Haggerty432ad412012-04-10 05:30:26295 struct ref_value value; /* if not (flags&REF_DIR) */
296 struct ref_dir subdir; /* if (flags&REF_DIR) */
Michael Haggerty593f1bb2012-04-10 05:30:23297 } u;
Michael Haggerty432ad412012-04-10 05:30:26298 /*
299 * The full name of the reference (e.g., "refs/heads/master")
300 * or the full name of the directory with a trailing slash
301 * (e.g., "refs/heads/"):
302 */
Michael Haggertybc5fd6d2012-04-10 05:30:13303 char name[FLEX_ARRAY];
304};
Linus Torvaldse1e22e32006-09-11 23:37:32305
Michael Haggerty28e6a342012-04-26 22:27:07306static void read_loose_refs(const char *dirname, struct ref_dir *dir);
307
Michael Haggertyd7826d52012-04-26 22:27:03308static struct ref_dir *get_ref_dir(struct ref_entry *entry)
309{
Michael Haggerty28e6a342012-04-26 22:27:07310 struct ref_dir *dir;
Michael Haggertyd7826d52012-04-26 22:27:03311 assert(entry->flag & REF_DIR);
Michael Haggerty28e6a342012-04-26 22:27:07312 dir = &entry->u.subdir;
313 if (entry->flag & REF_INCOMPLETE) {
314 read_loose_refs(entry->name, dir);
315 entry->flag &= ~REF_INCOMPLETE;
316 }
317 return dir;
Michael Haggertyd7826d52012-04-26 22:27:03318}
319
Ronnie Sahlbergd0f810f2014-09-03 18:45:43320/*
321 * Check if a refname is safe.
322 * For refs that start with "refs/" we consider it safe as long they do
323 * not try to resolve to outside of refs/.
324 *
325 * For all other refs we only consider them safe iff they only contain
326 * upper case characters and '_' (like "HEAD" AND "MERGE_HEAD", and not like
327 * "config").
328 */
329static int refname_is_safe(const char *refname)
330{
331 if (starts_with(refname, "refs/")) {
332 char *buf;
333 int result;
334
335 buf = xmalloc(strlen(refname) + 1);
336 /*
337 * Does the refname try to escape refs/?
338 * For example: refs/foo/../bar is safe but refs/foo/../../bar
339 * is not.
340 */
341 result = !normalize_path_copy(buf, refname + strlen("refs/"));
342 free(buf);
343 return result;
344 }
345 while (*refname) {
346 if (!isupper(*refname) && *refname != '_')
347 return 0;
348 refname++;
349 }
350 return 1;
351}
352
Michael Haggertycddc4252011-12-12 05:38:22353static struct ref_entry *create_ref_entry(const char *refname,
354 const unsigned char *sha1, int flag,
355 int check_name)
Linus Torvaldse1e22e32006-09-11 23:37:32356{
357 int len;
Michael Haggertycddc4252011-12-12 05:38:22358 struct ref_entry *ref;
Linus Torvaldse1e22e32006-09-11 23:37:32359
Junio C Hamano09116a12011-11-17 00:54:32360 if (check_name &&
Jonathan Niederf3cc52d2014-09-26 19:22:22361 check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))
Michael Haggertydfefa932011-12-12 05:38:09362 die("Reference has invalid format: '%s'", refname);
Michael Haggertycddc4252011-12-12 05:38:22363 len = strlen(refname) + 1;
364 ref = xmalloc(sizeof(struct ref_entry) + len);
brian m. carlson83538472015-05-25 18:38:27365 hashcpy(ref->u.value.oid.hash, sha1);
366 oidclr(&ref->u.value.peeled);
Michael Haggertycddc4252011-12-12 05:38:22367 memcpy(ref->name, refname, len);
368 ref->flag = flag;
369 return ref;
370}
371
Michael Haggerty432ad412012-04-10 05:30:26372static void clear_ref_dir(struct ref_dir *dir);
373
Michael Haggerty732134e2012-04-10 05:30:21374static void free_ref_entry(struct ref_entry *entry)
375{
Michael Haggerty27b55872012-05-20 06:49:32376 if (entry->flag & REF_DIR) {
377 /*
378 * Do not use get_ref_dir() here, as that might
379 * trigger the reading of loose refs.
380 */
381 clear_ref_dir(&entry->u.subdir);
382 }
Michael Haggerty732134e2012-04-10 05:30:21383 free(entry);
384}
385
Michael Haggerty432ad412012-04-10 05:30:26386/*
387 * Add a ref_entry to the end of dir (unsorted). Entry is always
388 * stored directly in dir; no recursion into subdirectories is
389 * done.
390 */
391static void add_entry_to_dir(struct ref_dir *dir, struct ref_entry *entry)
Michael Haggertycddc4252011-12-12 05:38:22392{
Michael Haggerty432ad412012-04-10 05:30:26393 ALLOC_GROW(dir->entries, dir->nr + 1, dir->alloc);
394 dir->entries[dir->nr++] = entry;
Michael Haggerty654ad402012-05-24 12:16:50395 /* optimize for the case that entries are added in order */
396 if (dir->nr == 1 ||
397 (dir->nr == dir->sorted + 1 &&
398 strcmp(dir->entries[dir->nr - 2]->name,
399 dir->entries[dir->nr - 1]->name) < 0))
400 dir->sorted = dir->nr;
Julian Phillipsc774aab2007-04-17 01:42:50401}
402
Michael Haggerty432ad412012-04-10 05:30:26403/*
404 * Clear and free all entries in dir, recursively.
405 */
Michael Haggertyd3177272012-04-10 05:30:24406static void clear_ref_dir(struct ref_dir *dir)
Michael Haggertybc5fd6d2012-04-10 05:30:13407{
408 int i;
Michael Haggertyd3177272012-04-10 05:30:24409 for (i = 0; i < dir->nr; i++)
410 free_ref_entry(dir->entries[i]);
411 free(dir->entries);
412 dir->sorted = dir->nr = dir->alloc = 0;
413 dir->entries = NULL;
Michael Haggertybc5fd6d2012-04-10 05:30:13414}
415
Michael Haggerty432ad412012-04-10 05:30:26416/*
417 * Create a struct ref_entry object for the specified dirname.
418 * dirname is the name of the directory with a trailing slash (e.g.,
419 * "refs/heads/") or "" for the top-level directory.
420 */
Michael Haggertyf006c422012-04-26 22:27:05421static struct ref_entry *create_dir_entry(struct ref_cache *ref_cache,
René Scharfeb9146f52012-05-22 18:50:52422 const char *dirname, size_t len,
423 int incomplete)
Michael Haggerty432ad412012-04-10 05:30:26424{
425 struct ref_entry *direntry;
Michael Haggerty432ad412012-04-10 05:30:26426 direntry = xcalloc(1, sizeof(struct ref_entry) + len + 1);
René Scharfeb9146f52012-05-22 18:50:52427 memcpy(direntry->name, dirname, len);
428 direntry->name[len] = '\0';
Michael Haggertyf006c422012-04-26 22:27:05429 direntry->u.subdir.ref_cache = ref_cache;
Michael Haggerty28e6a342012-04-26 22:27:07430 direntry->flag = REF_DIR | (incomplete ? REF_INCOMPLETE : 0);
Michael Haggerty432ad412012-04-10 05:30:26431 return direntry;
432}
433
Julian Phillipse9c4c112011-09-29 22:11:42434static int ref_entry_cmp(const void *a, const void *b)
Julian Phillipsc774aab2007-04-17 01:42:50435{
Julian Phillipse9c4c112011-09-29 22:11:42436 struct ref_entry *one = *(struct ref_entry **)a;
437 struct ref_entry *two = *(struct ref_entry **)b;
438 return strcmp(one->name, two->name);
439}
Julian Phillipsc774aab2007-04-17 01:42:50440
Michael Haggertyd3177272012-04-10 05:30:24441static void sort_ref_dir(struct ref_dir *dir);
Michael Haggertybc5fd6d2012-04-10 05:30:13442
Junio C Hamanoe1980c92012-05-22 21:03:29443struct string_slice {
444 size_t len;
445 const char *str;
446};
447
448static int ref_entry_cmp_sslice(const void *key_, const void *ent_)
449{
René Scharfec971ddf2013-01-16 01:08:16450 const struct string_slice *key = key_;
451 const struct ref_entry *ent = *(const struct ref_entry * const *)ent_;
452 int cmp = strncmp(key->str, ent->name, key->len);
Junio C Hamanoe1980c92012-05-22 21:03:29453 if (cmp)
454 return cmp;
René Scharfec971ddf2013-01-16 01:08:16455 return '\0' - (unsigned char)ent->name[key->len];
Junio C Hamanoe1980c92012-05-22 21:03:29456}
457
Michael Haggerty432ad412012-04-10 05:30:26458/*
Michael Haggerty9fc0a642013-04-22 19:52:26459 * Return the index of the entry with the given refname from the
460 * ref_dir (non-recursively), sorting dir if necessary. Return -1 if
461 * no such entry is found. dir must already be complete.
Michael Haggerty432ad412012-04-10 05:30:26462 */
Michael Haggerty9fc0a642013-04-22 19:52:26463static int search_ref_dir(struct ref_dir *dir, const char *refname, size_t len)
Michael Haggertybc5fd6d2012-04-10 05:30:13464{
Junio C Hamanoe1980c92012-05-22 21:03:29465 struct ref_entry **r;
466 struct string_slice key;
Michael Haggertybc5fd6d2012-04-10 05:30:13467
Michael Haggerty432ad412012-04-10 05:30:26468 if (refname == NULL || !dir->nr)
Michael Haggerty9fc0a642013-04-22 19:52:26469 return -1;
Michael Haggertybc5fd6d2012-04-10 05:30:13470
Michael Haggertyd3177272012-04-10 05:30:24471 sort_ref_dir(dir);
Junio C Hamanoe1980c92012-05-22 21:03:29472 key.len = len;
473 key.str = refname;
474 r = bsearch(&key, dir->entries, dir->nr, sizeof(*dir->entries),
475 ref_entry_cmp_sslice);
Michael Haggertybc5fd6d2012-04-10 05:30:13476
477 if (r == NULL)
Michael Haggerty9fc0a642013-04-22 19:52:26478 return -1;
Michael Haggertybc5fd6d2012-04-10 05:30:13479
Michael Haggerty9fc0a642013-04-22 19:52:26480 return r - dir->entries;
Michael Haggertybc5fd6d2012-04-10 05:30:13481}
482
Michael Haggerty202a56a2011-12-12 05:38:15483/*
Michael Haggertyf348ac92012-04-24 22:45:11484 * Search for a directory entry directly within dir (without
485 * recursing). Sort dir if necessary. subdirname must be a directory
486 * name (i.e., end in '/'). If mkdir is set, then create the
487 * directory if it is missing; otherwise, return NULL if the desired
Michael Haggerty28e6a342012-04-26 22:27:07488 * directory cannot be found. dir must already be complete.
Michael Haggertyf348ac92012-04-24 22:45:11489 */
Michael Haggerty3f3aa1b2012-04-26 22:27:04490static struct ref_dir *search_for_subdir(struct ref_dir *dir,
René Scharfedd02e722012-05-22 18:50:58491 const char *subdirname, size_t len,
492 int mkdir)
Michael Haggertyf348ac92012-04-24 22:45:11493{
Michael Haggerty9fc0a642013-04-22 19:52:26494 int entry_index = search_ref_dir(dir, subdirname, len);
495 struct ref_entry *entry;
496 if (entry_index == -1) {
Michael Haggertyf348ac92012-04-24 22:45:11497 if (!mkdir)
498 return NULL;
Michael Haggerty28e6a342012-04-26 22:27:07499 /*
500 * Since dir is complete, the absence of a subdir
501 * means that the subdir really doesn't exist;
502 * therefore, create an empty record for it but mark
503 * the record complete.
504 */
René Scharfeb9146f52012-05-22 18:50:52505 entry = create_dir_entry(dir->ref_cache, subdirname, len, 0);
Michael Haggertyf348ac92012-04-24 22:45:11506 add_entry_to_dir(dir, entry);
Michael Haggerty9fc0a642013-04-22 19:52:26507 } else {
508 entry = dir->entries[entry_index];
Michael Haggertyf348ac92012-04-24 22:45:11509 }
Michael Haggerty3f3aa1b2012-04-26 22:27:04510 return get_ref_dir(entry);
Michael Haggertyf348ac92012-04-24 22:45:11511}
512
513/*
Michael Haggerty432ad412012-04-10 05:30:26514 * If refname is a reference name, find the ref_dir within the dir
515 * tree that should hold refname. If refname is a directory name
516 * (i.e., ends in '/'), then return that ref_dir itself. dir must
Michael Haggerty28e6a342012-04-26 22:27:07517 * represent the top-level directory and must already be complete.
518 * Sort ref_dirs and recurse into subdirectories as necessary. If
519 * mkdir is set, then create any missing directories; otherwise,
520 * return NULL if the desired directory cannot be found.
Michael Haggerty432ad412012-04-10 05:30:26521 */
522static struct ref_dir *find_containing_dir(struct ref_dir *dir,
523 const char *refname, int mkdir)
524{
Michael Haggerty5fa04412012-04-26 22:27:00525 const char *slash;
Michael Haggerty5fa04412012-04-26 22:27:00526 for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) {
René Scharfedd02e722012-05-22 18:50:58527 size_t dirnamelen = slash - refname + 1;
Michael Haggerty3f3aa1b2012-04-26 22:27:04528 struct ref_dir *subdir;
René Scharfedd02e722012-05-22 18:50:58529 subdir = search_for_subdir(dir, refname, dirnamelen, mkdir);
Junio C Hamano663c1292012-05-03 22:12:54530 if (!subdir) {
531 dir = NULL;
Michael Haggertyf348ac92012-04-24 22:45:11532 break;
Michael Haggerty432ad412012-04-10 05:30:26533 }
Michael Haggerty3f3aa1b2012-04-26 22:27:04534 dir = subdir;
Michael Haggerty432ad412012-04-10 05:30:26535 }
536
Michael Haggerty432ad412012-04-10 05:30:26537 return dir;
538}
539
540/*
541 * Find the value entry with the given name in dir, sorting ref_dirs
542 * and recursing into subdirectories as necessary. If the name is not
543 * found or it corresponds to a directory entry, return NULL.
544 */
545static struct ref_entry *find_ref(struct ref_dir *dir, const char *refname)
546{
Michael Haggerty9fc0a642013-04-22 19:52:26547 int entry_index;
Michael Haggerty432ad412012-04-10 05:30:26548 struct ref_entry *entry;
549 dir = find_containing_dir(dir, refname, 0);
550 if (!dir)
551 return NULL;
Michael Haggerty9fc0a642013-04-22 19:52:26552 entry_index = search_ref_dir(dir, refname, strlen(refname));
553 if (entry_index == -1)
554 return NULL;
555 entry = dir->entries[entry_index];
556 return (entry->flag & REF_DIR) ? NULL : entry;
Michael Haggerty432ad412012-04-10 05:30:26557}
558
559/*
Michael Haggerty506a7602013-04-22 19:52:27560 * Remove the entry with the given name from dir, recursing into
561 * subdirectories as necessary. If refname is the name of a directory
562 * (i.e., ends with '/'), then remove the directory and its contents.
563 * If the removal was successful, return the number of entries
564 * remaining in the directory entry that contained the deleted entry.
565 * If the name was not found, return -1. Please note that this
566 * function only deletes the entry from the cache; it does not delete
567 * it from the filesystem or ensure that other cache entries (which
568 * might be symbolic references to the removed entry) are updated.
569 * Nor does it remove any containing dir entries that might be made
570 * empty by the removal. dir must represent the top-level directory
571 * and must already be complete.
572 */
573static int remove_entry(struct ref_dir *dir, const char *refname)
574{
575 int refname_len = strlen(refname);
576 int entry_index;
577 struct ref_entry *entry;
578 int is_dir = refname[refname_len - 1] == '/';
579 if (is_dir) {
580 /*
581 * refname represents a reference directory. Remove
582 * the trailing slash; otherwise we will get the
583 * directory *representing* refname rather than the
584 * one *containing* it.
585 */
586 char *dirname = xmemdupz(refname, refname_len - 1);
587 dir = find_containing_dir(dir, dirname, 0);
588 free(dirname);
589 } else {
590 dir = find_containing_dir(dir, refname, 0);
591 }
592 if (!dir)
593 return -1;
594 entry_index = search_ref_dir(dir, refname, refname_len);
595 if (entry_index == -1)
596 return -1;
597 entry = dir->entries[entry_index];
598
599 memmove(&dir->entries[entry_index],
600 &dir->entries[entry_index + 1],
601 (dir->nr - entry_index - 1) * sizeof(*dir->entries)
602 );
603 dir->nr--;
604 if (dir->sorted > entry_index)
605 dir->sorted--;
606 free_ref_entry(entry);
607 return dir->nr;
Michael Haggerty432ad412012-04-10 05:30:26608}
609
610/*
611 * Add a ref_entry to the ref_dir (unsorted), recursing into
612 * subdirectories as necessary. dir must represent the top-level
613 * directory. Return 0 on success.
614 */
615static int add_ref(struct ref_dir *dir, struct ref_entry *ref)
616{
617 dir = find_containing_dir(dir, ref->name, 1);
618 if (!dir)
619 return -1;
620 add_entry_to_dir(dir, ref);
621 return 0;
622}
623
624/*
Michael Haggerty202a56a2011-12-12 05:38:15625 * Emit a warning and return true iff ref1 and ref2 have the same name
626 * and the same sha1. Die if they have the same name but different
627 * sha1s.
628 */
629static int is_dup_ref(const struct ref_entry *ref1, const struct ref_entry *ref2)
630{
Michael Haggerty432ad412012-04-10 05:30:26631 if (strcmp(ref1->name, ref2->name))
Michael Haggerty202a56a2011-12-12 05:38:15632 return 0;
Michael Haggerty432ad412012-04-10 05:30:26633
634 /* Duplicate name; make sure that they don't conflict: */
635
636 if ((ref1->flag & REF_DIR) || (ref2->flag & REF_DIR))
637 /* This is impossible by construction */
638 die("Reference directory conflict: %s", ref1->name);
639
brian m. carlson83538472015-05-25 18:38:27640 if (oidcmp(&ref1->u.value.oid, &ref2->u.value.oid))
Michael Haggerty432ad412012-04-10 05:30:26641 die("Duplicated ref, and SHA1s don't match: %s", ref1->name);
642
643 warning("Duplicated ref: %s", ref1->name);
644 return 1;
Michael Haggerty202a56a2011-12-12 05:38:15645}
646
Michael Haggertye6ed3ca2012-01-17 05:50:32647/*
Michael Haggerty432ad412012-04-10 05:30:26648 * Sort the entries in dir non-recursively (if they are not already
649 * sorted) and remove any duplicate entries.
Michael Haggertye6ed3ca2012-01-17 05:50:32650 */
Michael Haggertyd3177272012-04-10 05:30:24651static void sort_ref_dir(struct ref_dir *dir)
Julian Phillipse9c4c112011-09-29 22:11:42652{
Michael Haggerty202a56a2011-12-12 05:38:15653 int i, j;
Michael Haggerty81a79d82012-04-10 05:30:25654 struct ref_entry *last = NULL;
Julian Phillipsc774aab2007-04-17 01:42:50655
Michael Haggertye6ed3ca2012-01-17 05:50:32656 /*
657 * This check also prevents passing a zero-length array to qsort(),
658 * which is a problem on some platforms.
659 */
Michael Haggertyd3177272012-04-10 05:30:24660 if (dir->sorted == dir->nr)
Julian Phillipse9c4c112011-09-29 22:11:42661 return;
Julian Phillipsc774aab2007-04-17 01:42:50662
Michael Haggertyd3177272012-04-10 05:30:24663 qsort(dir->entries, dir->nr, sizeof(*dir->entries), ref_entry_cmp);
Julian Phillipsc774aab2007-04-17 01:42:50664
Michael Haggerty81a79d82012-04-10 05:30:25665 /* Remove any duplicates: */
666 for (i = 0, j = 0; j < dir->nr; j++) {
667 struct ref_entry *entry = dir->entries[j];
668 if (last && is_dup_ref(last, entry))
669 free_ref_entry(entry);
670 else
671 last = dir->entries[i++] = entry;
Julian Phillipse9c4c112011-09-29 22:11:42672 }
Michael Haggerty81a79d82012-04-10 05:30:25673 dir->sorted = dir->nr = i;
Julian Phillipse9c4c112011-09-29 22:11:42674}
Julian Phillipsc774aab2007-04-17 01:42:50675
Michael Haggertyfcce1702013-04-22 19:52:11676/* Include broken references in a do_for_each_ref*() iteration: */
677#define DO_FOR_EACH_INCLUDE_BROKEN 0x01
Michael Haggertybc5fd6d2012-04-10 05:30:13678
Michael Haggerty7d76fdc2013-04-22 19:52:12679/*
Michael Haggerty662428f2013-04-22 19:52:18680 * Return true iff the reference described by entry can be resolved to
681 * an object in the database. Emit a warning if the referred-to
682 * object does not exist.
683 */
684static int ref_resolves_to_object(struct ref_entry *entry)
685{
686 if (entry->flag & REF_ISBROKEN)
687 return 0;
brian m. carlson83538472015-05-25 18:38:27688 if (!has_sha1_file(entry->u.value.oid.hash)) {
Michael Haggerty662428f2013-04-22 19:52:18689 error("%s does not point to a valid object!", entry->name);
690 return 0;
691 }
692 return 1;
693}
694
695/*
Michael Haggerty7d76fdc2013-04-22 19:52:12696 * current_ref is a performance hack: when iterating over references
697 * using the for_each_ref*() functions, current_ref is set to the
698 * current reference's entry before calling the callback function. If
699 * the callback function calls peel_ref(), then peel_ref() first
700 * checks whether the reference to be peeled is the current reference
701 * (it usually is) and if so, returns that reference's peeled version
702 * if it is available. This avoids a refname lookup in a common case.
703 */
Michael Haggertybc5fd6d2012-04-10 05:30:13704static struct ref_entry *current_ref;
705
Michael Haggerty624cac32013-04-22 19:52:23706typedef int each_ref_entry_fn(struct ref_entry *entry, void *cb_data);
707
708struct ref_entry_cb {
709 const char *base;
710 int trim;
711 int flags;
712 each_ref_fn *fn;
713 void *cb_data;
714};
715
Michael Haggertyfcce1702013-04-22 19:52:11716/*
Michael Haggerty624cac32013-04-22 19:52:23717 * Handle one reference in a do_for_each_ref*()-style iteration,
718 * calling an each_ref_fn for each entry.
Michael Haggertyfcce1702013-04-22 19:52:11719 */
Michael Haggerty624cac32013-04-22 19:52:23720static int do_one_ref(struct ref_entry *entry, void *cb_data)
Julian Phillipse9c4c112011-09-29 22:11:42721{
Michael Haggerty624cac32013-04-22 19:52:23722 struct ref_entry_cb *data = cb_data;
Michael Haggertyd0cf51e2013-07-15 15:24:17723 struct ref_entry *old_current_ref;
Michael Haggerty429213e2012-04-10 05:30:14724 int retval;
Michael Haggertyd0cf51e2013-07-15 15:24:17725
Christian Couder59556542013-11-30 20:55:40726 if (!starts_with(entry->name, data->base))
Michael Haggertybc5fd6d2012-04-10 05:30:13727 return 0;
Julian Phillipsc774aab2007-04-17 01:42:50728
Michael Haggerty624cac32013-04-22 19:52:23729 if (!(data->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
Michael Haggerty662428f2013-04-22 19:52:18730 !ref_resolves_to_object(entry))
731 return 0;
732
Michael Haggertyd0cf51e2013-07-15 15:24:17733 /* Store the old value, in case this is a recursive call: */
734 old_current_ref = current_ref;
Michael Haggertybc5fd6d2012-04-10 05:30:13735 current_ref = entry;
Michael Haggerty2b2a5be2015-05-25 18:38:28736 retval = data->fn(entry->name + data->trim, &entry->u.value.oid,
Michael Haggerty624cac32013-04-22 19:52:23737 entry->flag, data->cb_data);
Michael Haggertyd0cf51e2013-07-15 15:24:17738 current_ref = old_current_ref;
Michael Haggerty429213e2012-04-10 05:30:14739 return retval;
Michael Haggertybc5fd6d2012-04-10 05:30:13740}
Julian Phillipsc774aab2007-04-17 01:42:50741
Michael Haggertybc5fd6d2012-04-10 05:30:13742/*
Michael Haggertyd3177272012-04-10 05:30:24743 * Call fn for each reference in dir that has index in the range
Michael Haggerty432ad412012-04-10 05:30:26744 * offset <= index < dir->nr. Recurse into subdirectories that are in
745 * that index range, sorting them before iterating. This function
Michael Haggerty624cac32013-04-22 19:52:23746 * does not sort dir itself; it should be sorted beforehand. fn is
747 * called for all references, including broken ones.
Michael Haggertyc36b5bc2012-04-10 05:30:15748 */
Michael Haggerty624cac32013-04-22 19:52:23749static int do_for_each_entry_in_dir(struct ref_dir *dir, int offset,
750 each_ref_entry_fn fn, void *cb_data)
Michael Haggertyc36b5bc2012-04-10 05:30:15751{
752 int i;
Michael Haggertyd3177272012-04-10 05:30:24753 assert(dir->sorted == dir->nr);
754 for (i = offset; i < dir->nr; i++) {
Michael Haggerty432ad412012-04-10 05:30:26755 struct ref_entry *entry = dir->entries[i];
756 int retval;
757 if (entry->flag & REF_DIR) {
Michael Haggertyd7826d52012-04-26 22:27:03758 struct ref_dir *subdir = get_ref_dir(entry);
759 sort_ref_dir(subdir);
Michael Haggerty624cac32013-04-22 19:52:23760 retval = do_for_each_entry_in_dir(subdir, 0, fn, cb_data);
Michael Haggerty432ad412012-04-10 05:30:26761 } else {
Michael Haggerty624cac32013-04-22 19:52:23762 retval = fn(entry, cb_data);
Michael Haggerty432ad412012-04-10 05:30:26763 }
Michael Haggertyc36b5bc2012-04-10 05:30:15764 if (retval)
765 return retval;
766 }
767 return 0;
768}
769
770/*
Michael Haggertyd3177272012-04-10 05:30:24771 * Call fn for each reference in the union of dir1 and dir2, in order
Michael Haggerty432ad412012-04-10 05:30:26772 * by refname. Recurse into subdirectories. If a value entry appears
773 * in both dir1 and dir2, then only process the version that is in
774 * dir2. The input dirs must already be sorted, but subdirs will be
Michael Haggerty624cac32013-04-22 19:52:23775 * sorted as needed. fn is called for all references, including
776 * broken ones.
Michael Haggertyb3fd0602012-04-10 05:30:16777 */
Michael Haggerty624cac32013-04-22 19:52:23778static int do_for_each_entry_in_dirs(struct ref_dir *dir1,
779 struct ref_dir *dir2,
780 each_ref_entry_fn fn, void *cb_data)
Michael Haggertyb3fd0602012-04-10 05:30:16781{
782 int retval;
783 int i1 = 0, i2 = 0;
784
Michael Haggertyd3177272012-04-10 05:30:24785 assert(dir1->sorted == dir1->nr);
786 assert(dir2->sorted == dir2->nr);
Michael Haggerty432ad412012-04-10 05:30:26787 while (1) {
788 struct ref_entry *e1, *e2;
789 int cmp;
790 if (i1 == dir1->nr) {
Michael Haggerty624cac32013-04-22 19:52:23791 return do_for_each_entry_in_dir(dir2, i2, fn, cb_data);
Michael Haggerty432ad412012-04-10 05:30:26792 }
793 if (i2 == dir2->nr) {
Michael Haggerty624cac32013-04-22 19:52:23794 return do_for_each_entry_in_dir(dir1, i1, fn, cb_data);
Michael Haggerty432ad412012-04-10 05:30:26795 }
796 e1 = dir1->entries[i1];
797 e2 = dir2->entries[i2];
798 cmp = strcmp(e1->name, e2->name);
799 if (cmp == 0) {
800 if ((e1->flag & REF_DIR) && (e2->flag & REF_DIR)) {
801 /* Both are directories; descend them in parallel. */
Michael Haggertyd7826d52012-04-26 22:27:03802 struct ref_dir *subdir1 = get_ref_dir(e1);
803 struct ref_dir *subdir2 = get_ref_dir(e2);
804 sort_ref_dir(subdir1);
805 sort_ref_dir(subdir2);
Michael Haggerty624cac32013-04-22 19:52:23806 retval = do_for_each_entry_in_dirs(
807 subdir1, subdir2, fn, cb_data);
Michael Haggertyb3fd0602012-04-10 05:30:16808 i1++;
Michael Haggerty432ad412012-04-10 05:30:26809 i2++;
810 } else if (!(e1->flag & REF_DIR) && !(e2->flag & REF_DIR)) {
811 /* Both are references; ignore the one from dir1. */
Michael Haggerty624cac32013-04-22 19:52:23812 retval = fn(e2, cb_data);
Michael Haggerty432ad412012-04-10 05:30:26813 i1++;
814 i2++;
815 } else {
816 die("conflict between reference and directory: %s",
817 e1->name);
818 }
819 } else {
820 struct ref_entry *e;
821 if (cmp < 0) {
822 e = e1;
823 i1++;
824 } else {
825 e = e2;
826 i2++;
827 }
828 if (e->flag & REF_DIR) {
Michael Haggertyd7826d52012-04-26 22:27:03829 struct ref_dir *subdir = get_ref_dir(e);
830 sort_ref_dir(subdir);
Michael Haggerty624cac32013-04-22 19:52:23831 retval = do_for_each_entry_in_dir(
832 subdir, 0, fn, cb_data);
Michael Haggerty432ad412012-04-10 05:30:26833 } else {
Michael Haggerty624cac32013-04-22 19:52:23834 retval = fn(e, cb_data);
Michael Haggertyb3fd0602012-04-10 05:30:16835 }
836 }
837 if (retval)
838 return retval;
839 }
Michael Haggertyb3fd0602012-04-10 05:30:16840}
841
842/*
Jeff King98eeb092013-06-20 08:37:53843 * Load all of the refs from the dir into our in-memory cache. The hard work
844 * of loading loose refs is done by get_ref_dir(), so we just need to recurse
845 * through all of the sub-directories. We do not even need to care about
846 * sorting, as traversal order does not matter to us.
847 */
848static void prime_ref_dir(struct ref_dir *dir)
849{
850 int i;
851 for (i = 0; i < dir->nr; i++) {
852 struct ref_entry *entry = dir->entries[i];
853 if (entry->flag & REF_DIR)
854 prime_ref_dir(get_ref_dir(entry));
855 }
856}
Jeff Kingcbe73332014-09-10 11:11:55857
Jeff Kingcbe73332014-09-10 11:11:55858struct nonmatching_ref_data {
Ronnie Sahlberg5fe7d822014-05-01 18:16:07859 const struct string_list *skip;
Michael Haggerty521331c2015-05-11 15:25:09860 const char *conflicting_refname;
Michael Haggerty5a4d4942012-04-10 05:30:19861};
862
Jeff Kingcbe73332014-09-10 11:11:55863static int nonmatching_ref_fn(struct ref_entry *entry, void *vdata)
Michael Haggerty5a4d4942012-04-10 05:30:19864{
Jeff Kingcbe73332014-09-10 11:11:55865 struct nonmatching_ref_data *data = vdata;
866
Michael Haggerty8bfac192015-05-11 15:25:07867 if (data->skip && string_list_has_string(data->skip, entry->name))
Michael Haggerty5a4d4942012-04-10 05:30:19868 return 0;
Jeff Kingcbe73332014-09-10 11:11:55869
Michael Haggerty521331c2015-05-11 15:25:09870 data->conflicting_refname = entry->name;
Jeff Kingcbe73332014-09-10 11:11:55871 return 1;
872}
873
Michael Haggertyd66da472012-04-10 05:30:17874/*
Michael Haggerty5baf37d2015-05-11 15:25:13875 * Return 0 if a reference named refname could be created without
876 * conflicting with the name of an existing reference in dir.
Michael Haggerty1146f172015-05-11 15:25:14877 * Otherwise, return a negative value and write an explanation to err.
878 * If extras is non-NULL, it is a list of additional refnames with
879 * which refname is not allowed to conflict. If skip is non-NULL,
880 * ignore potential conflicts with refs in skip (e.g., because they
881 * are scheduled for deletion in the same operation). Behavior is
882 * undefined if the same name is listed in both extras and skip.
Jeff Kingcbe73332014-09-10 11:11:55883 *
884 * Two reference names conflict if one of them exactly matches the
Michael Haggerty49e81872015-05-11 15:25:04885 * leading components of the other; e.g., "refs/foo/bar" conflicts
886 * with both "refs/foo" and with "refs/foo/bar/baz" but not with
887 * "refs/foo/bar" or "refs/foo/barbados".
Ronnie Sahlberg5fe7d822014-05-01 18:16:07888 *
Michael Haggertye9111042015-05-11 15:25:12889 * extras and skip must be sorted.
Michael Haggertybc5fd6d2012-04-10 05:30:13890 */
Michael Haggerty5baf37d2015-05-11 15:25:13891static int verify_refname_available(const char *refname,
892 const struct string_list *extras,
893 const struct string_list *skip,
Michael Haggerty1146f172015-05-11 15:25:14894 struct ref_dir *dir,
895 struct strbuf *err)
Michael Haggertybc5fd6d2012-04-10 05:30:13896{
Jeff Kingcbe73332014-09-10 11:11:55897 const char *slash;
Jeff Kingcbe73332014-09-10 11:11:55898 int pos;
Michael Haggerty6075f302015-05-11 15:25:06899 struct strbuf dirname = STRBUF_INIT;
Michael Haggerty5baf37d2015-05-11 15:25:13900 int ret = -1;
Michael Haggerty5a4d4942012-04-10 05:30:19901
Michael Haggerty49e81872015-05-11 15:25:04902 /*
903 * For the sake of comments in this function, suppose that
904 * refname is "refs/foo/bar".
905 */
906
Michael Haggerty1146f172015-05-11 15:25:14907 assert(err);
908
Michael Haggerty61da5962015-05-11 15:25:10909 strbuf_grow(&dirname, strlen(refname) + 1);
Jeff Kingcbe73332014-09-10 11:11:55910 for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) {
Michael Haggerty61da5962015-05-11 15:25:10911 /* Expand dirname to the new prefix, not including the trailing slash: */
912 strbuf_add(&dirname, refname + dirname.len, slash - refname - dirname.len);
913
Jeff Kingcbe73332014-09-10 11:11:55914 /*
Michael Haggerty49e81872015-05-11 15:25:04915 * We are still at a leading dir of the refname (e.g.,
916 * "refs/foo"; if there is a reference with that name,
917 * it is a conflict, *unless* it is in skip.
Jeff Kingcbe73332014-09-10 11:11:55918 */
Michael Haggertye9111042015-05-11 15:25:12919 if (dir) {
920 pos = search_ref_dir(dir, dirname.buf, dirname.len);
921 if (pos >= 0 &&
922 (!skip || !string_list_has_string(skip, dirname.buf))) {
Michael Haggerty49e81872015-05-11 15:25:04923 /*
Michael Haggertye9111042015-05-11 15:25:12924 * We found a reference whose name is
925 * a proper prefix of refname; e.g.,
926 * "refs/foo", and is not in skip.
Michael Haggerty49e81872015-05-11 15:25:04927 */
Michael Haggerty1146f172015-05-11 15:25:14928 strbuf_addf(err, "'%s' exists; cannot create '%s'",
929 dirname.buf, refname);
Michael Haggerty61da5962015-05-11 15:25:10930 goto cleanup;
Michael Haggerty49e81872015-05-11 15:25:04931 }
Jeff Kingcbe73332014-09-10 11:11:55932 }
933
Michael Haggertye9111042015-05-11 15:25:12934 if (extras && string_list_has_string(extras, dirname.buf) &&
935 (!skip || !string_list_has_string(skip, dirname.buf))) {
Michael Haggerty1146f172015-05-11 15:25:14936 strbuf_addf(err, "cannot process '%s' and '%s' at the same time",
937 refname, dirname.buf);
Michael Haggertye9111042015-05-11 15:25:12938 goto cleanup;
939 }
Jeff Kingcbe73332014-09-10 11:11:55940
941 /*
942 * Otherwise, we can try to continue our search with
Michael Haggerty49e81872015-05-11 15:25:04943 * the next component. So try to look up the
Michael Haggertye9111042015-05-11 15:25:12944 * directory, e.g., "refs/foo/". If we come up empty,
945 * we know there is nothing under this whole prefix,
946 * but even in that case we still have to continue the
947 * search for conflicts with extras.
Jeff Kingcbe73332014-09-10 11:11:55948 */
Michael Haggerty61da5962015-05-11 15:25:10949 strbuf_addch(&dirname, '/');
Michael Haggertye9111042015-05-11 15:25:12950 if (dir) {
951 pos = search_ref_dir(dir, dirname.buf, dirname.len);
952 if (pos < 0) {
953 /*
954 * There was no directory "refs/foo/",
955 * so there is nothing under this
956 * whole prefix. So there is no need
957 * to continue looking for conflicting
958 * references. But we need to continue
959 * looking for conflicting extras.
960 */
961 dir = NULL;
962 } else {
963 dir = get_ref_dir(dir->entries[pos]);
964 }
Michael Haggerty49e81872015-05-11 15:25:04965 }
Jeff Kingcbe73332014-09-10 11:11:55966 }
967
968 /*
Michael Haggerty49e81872015-05-11 15:25:04969 * We are at the leaf of our refname (e.g., "refs/foo/bar").
970 * There is no point in searching for a reference with that
971 * name, because a refname isn't considered to conflict with
972 * itself. But we still need to check for references whose
973 * names are in the "refs/foo/bar/" namespace, because they
974 * *do* conflict.
Jeff Kingcbe73332014-09-10 11:11:55975 */
Michael Haggerty61da5962015-05-11 15:25:10976 strbuf_addstr(&dirname, refname + dirname.len);
Michael Haggerty6075f302015-05-11 15:25:06977 strbuf_addch(&dirname, '/');
Jeff Kingcbe73332014-09-10 11:11:55978
Michael Haggertye9111042015-05-11 15:25:12979 if (dir) {
980 pos = search_ref_dir(dir, dirname.buf, dirname.len);
Jeff Kingcbe73332014-09-10 11:11:55981
Michael Haggertye9111042015-05-11 15:25:12982 if (pos >= 0) {
983 /*
984 * We found a directory named "$refname/"
985 * (e.g., "refs/foo/bar/"). It is a problem
986 * iff it contains any ref that is not in
987 * "skip".
988 */
989 struct nonmatching_ref_data data;
990
991 data.skip = skip;
992 data.conflicting_refname = NULL;
993 dir = get_ref_dir(dir->entries[pos]);
994 sort_ref_dir(dir);
995 if (do_for_each_entry_in_dir(dir, 0, nonmatching_ref_fn, &data)) {
Michael Haggerty1146f172015-05-11 15:25:14996 strbuf_addf(err, "'%s' exists; cannot create '%s'",
997 data.conflicting_refname, refname);
Michael Haggertye9111042015-05-11 15:25:12998 goto cleanup;
999 }
Michael Haggerty61da5962015-05-11 15:25:101000 }
Michael Haggertybc5fd6d2012-04-10 05:30:131001 }
Jeff Kingcbe73332014-09-10 11:11:551002
Michael Haggertye9111042015-05-11 15:25:121003 if (extras) {
Jeff Kingcbe73332014-09-10 11:11:551004 /*
Michael Haggertye9111042015-05-11 15:25:121005 * Check for entries in extras that start with
1006 * "$refname/". We do that by looking for the place
1007 * where "$refname/" would be inserted in extras. If
1008 * there is an entry at that position that starts with
1009 * "$refname/" and is not in skip, then we have a
1010 * conflict.
Jeff Kingcbe73332014-09-10 11:11:551011 */
Michael Haggertye9111042015-05-11 15:25:121012 for (pos = string_list_find_insert_index(extras, dirname.buf, 0);
1013 pos < extras->nr; pos++) {
1014 const char *extra_refname = extras->items[pos].string;
Jeff Kingcbe73332014-09-10 11:11:551015
Michael Haggertye9111042015-05-11 15:25:121016 if (!starts_with(extra_refname, dirname.buf))
1017 break;
Jeff Kingcbe73332014-09-10 11:11:551018
Michael Haggertye9111042015-05-11 15:25:121019 if (!skip || !string_list_has_string(skip, extra_refname)) {
Michael Haggerty1146f172015-05-11 15:25:141020 strbuf_addf(err, "cannot process '%s' and '%s' at the same time",
1021 refname, extra_refname);
Michael Haggertye9111042015-05-11 15:25:121022 goto cleanup;
1023 }
1024 }
Michael Haggertybc5fd6d2012-04-10 05:30:131025 }
Jeff Kingcbe73332014-09-10 11:11:551026
Michael Haggertye9111042015-05-11 15:25:121027 /* No conflicts were found */
Michael Haggerty5baf37d2015-05-11 15:25:131028 ret = 0;
Michael Haggerty61da5962015-05-11 15:25:101029
1030cleanup:
1031 strbuf_release(&dirname);
1032 return ret;
Linus Torvaldse1e22e32006-09-11 23:37:321033}
1034
Michael Haggerty2fff7812013-06-20 08:37:451035struct packed_ref_cache {
1036 struct ref_entry *root;
Michael Haggerty9f69d292013-06-20 08:37:461037
1038 /*
Michael Haggerty5f5e2a82013-06-20 08:37:471039 * Count of references to the data structure in this instance,
1040 * including the pointer from ref_cache::packed if any. The
1041 * data will not be freed as long as the reference count is
1042 * nonzero.
1043 */
1044 unsigned int referrers;
1045
1046 /*
Michael Haggerty9f69d292013-06-20 08:37:461047 * Iff the packed-refs file associated with this instance is
1048 * currently locked for writing, this points at the associated
Michael Haggerty4f6b83e2013-06-20 08:37:491049 * lock (which is owned by somebody else). The referrer count
1050 * is also incremented when the file is locked and decremented
1051 * when it is unlocked.
Michael Haggerty9f69d292013-06-20 08:37:461052 */
1053 struct lock_file *lock;
Jeff Kingca919932013-06-20 08:37:521054
1055 /* The metadata from when this packed-refs cache was read */
1056 struct stat_validity validity;
Michael Haggerty2fff7812013-06-20 08:37:451057};
1058
Junio C Hamano5e290ff2006-09-30 19:37:371059/*
1060 * Future: need to be in "struct repository"
1061 * when doing a full libification.
1062 */
Michael Haggerty79c7ca52011-10-17 02:38:051063static struct ref_cache {
1064 struct ref_cache *next;
Michael Haggertyd12229f2012-04-26 22:27:011065 struct ref_entry *loose;
Michael Haggerty2fff7812013-06-20 08:37:451066 struct packed_ref_cache *packed;
Michael Haggerty9da31cb2013-04-22 19:52:411067 /*
1068 * The submodule name, or "" for the main repo. We allocate
1069 * length 1 rather than FLEX_ARRAY so that the main ref_cache
1070 * is initialized correctly.
1071 */
1072 char name[1];
1073} ref_cache, *submodule_ref_caches;
Michael Haggerty0e88c132011-08-12 22:36:291074
Michael Haggerty9f69d292013-06-20 08:37:461075/* Lock used for the main packed-refs file: */
1076static struct lock_file packlock;
1077
Michael Haggerty5f5e2a82013-06-20 08:37:471078/*
1079 * Increment the reference count of *packed_refs.
1080 */
1081static void acquire_packed_ref_cache(struct packed_ref_cache *packed_refs)
1082{
1083 packed_refs->referrers++;
1084}
1085
1086/*
1087 * Decrease the reference count of *packed_refs. If it goes to zero,
1088 * free *packed_refs and return true; otherwise return false.
1089 */
1090static int release_packed_ref_cache(struct packed_ref_cache *packed_refs)
1091{
1092 if (!--packed_refs->referrers) {
1093 free_ref_entry(packed_refs->root);
Jeff Kingca919932013-06-20 08:37:521094 stat_validity_clear(&packed_refs->validity);
Michael Haggerty5f5e2a82013-06-20 08:37:471095 free(packed_refs);
1096 return 1;
1097 } else {
1098 return 0;
1099 }
1100}
1101
Michael Haggerty760c4512011-10-17 02:38:091102static void clear_packed_ref_cache(struct ref_cache *refs)
Junio C Hamano5e290ff2006-09-30 19:37:371103{
Michael Haggertyd12229f2012-04-26 22:27:011104 if (refs->packed) {
Michael Haggerty5f5e2a82013-06-20 08:37:471105 struct packed_ref_cache *packed_refs = refs->packed;
1106
1107 if (packed_refs->lock)
Michael Haggerty9f69d292013-06-20 08:37:461108 die("internal error: packed-ref cache cleared while locked");
Michael Haggertyd12229f2012-04-26 22:27:011109 refs->packed = NULL;
Michael Haggerty5f5e2a82013-06-20 08:37:471110 release_packed_ref_cache(packed_refs);
Michael Haggertyd12229f2012-04-26 22:27:011111 }
Junio C Hamano5e290ff2006-09-30 19:37:371112}
1113
Michael Haggerty760c4512011-10-17 02:38:091114static void clear_loose_ref_cache(struct ref_cache *refs)
Junio C Hamano5e290ff2006-09-30 19:37:371115{
Michael Haggertyd12229f2012-04-26 22:27:011116 if (refs->loose) {
1117 free_ref_entry(refs->loose);
1118 refs->loose = NULL;
1119 }
Michael Haggerty760c4512011-10-17 02:38:091120}
1121
Michael Haggerty79c7ca52011-10-17 02:38:051122static struct ref_cache *create_ref_cache(const char *submodule)
Michael Haggertye5dbf602011-08-12 22:36:271123{
Michael Haggertyce409792011-08-12 22:36:281124 int len;
Michael Haggerty79c7ca52011-10-17 02:38:051125 struct ref_cache *refs;
Michael Haggertyce409792011-08-12 22:36:281126 if (!submodule)
1127 submodule = "";
1128 len = strlen(submodule) + 1;
Michael Haggerty79c7ca52011-10-17 02:38:051129 refs = xcalloc(1, sizeof(struct ref_cache) + len);
Michael Haggertyce409792011-08-12 22:36:281130 memcpy(refs->name, submodule, len);
Michael Haggertye5dbf602011-08-12 22:36:271131 return refs;
1132}
1133
Michael Haggerty4349a662011-08-12 22:36:251134/*
Michael Haggerty79c7ca52011-10-17 02:38:051135 * Return a pointer to a ref_cache for the specified submodule. For
Michael Haggerty4349a662011-08-12 22:36:251136 * the main repository, use submodule==NULL. The returned structure
1137 * will be allocated and initialized but not necessarily populated; it
1138 * should not be freed.
1139 */
Michael Haggerty79c7ca52011-10-17 02:38:051140static struct ref_cache *get_ref_cache(const char *submodule)
Michael Haggerty4349a662011-08-12 22:36:251141{
Michael Haggerty9da31cb2013-04-22 19:52:411142 struct ref_cache *refs;
1143
1144 if (!submodule || !*submodule)
1145 return &ref_cache;
1146
1147 for (refs = submodule_ref_caches; refs; refs = refs->next)
Michael Haggerty0e88c132011-08-12 22:36:291148 if (!strcmp(submodule, refs->name))
1149 return refs;
Michael Haggerty0e88c132011-08-12 22:36:291150
Michael Haggerty79c7ca52011-10-17 02:38:051151 refs = create_ref_cache(submodule);
Michael Haggerty9da31cb2013-04-22 19:52:411152 refs->next = submodule_ref_caches;
1153 submodule_ref_caches = refs;
Michael Haggerty0e88c132011-08-12 22:36:291154 return refs;
Michael Haggerty4349a662011-08-12 22:36:251155}
1156
Michael Haggerty3feb4f02013-04-22 19:52:131157/* The length of a peeled reference line in packed-refs, including EOL: */
1158#define PEELED_LINE_LENGTH 42
1159
Michael Haggertybc5fd6d2012-04-10 05:30:131160/*
Michael Haggerty694b7a12013-04-22 19:52:291161 * The packed-refs header line that we write out. Perhaps other
1162 * traits will be added later. The trailing space is required.
1163 */
1164static const char PACKED_REFS_HEADER[] =
1165 "# pack-refs with: peeled fully-peeled \n";
1166
Michael Haggertybc5fd6d2012-04-10 05:30:131167/*
1168 * Parse one line from a packed-refs file. Write the SHA1 to sha1.
1169 * Return a pointer to the refname within the line (null-terminated),
1170 * or NULL if there was a problem.
1171 */
Jeff King6a498702014-12-10 10:40:191172static const char *parse_ref_line(struct strbuf *line, unsigned char *sha1)
Michael Haggertybc5fd6d2012-04-10 05:30:131173{
Jeff King6a498702014-12-10 10:40:191174 const char *ref;
1175
Michael Haggertybc5fd6d2012-04-10 05:30:131176 /*
1177 * 42: the answer to everything.
1178 *
1179 * In this case, it happens to be the answer to
1180 * 40 (length of sha1 hex representation)
1181 * +1 (space in between hex and name)
1182 * +1 (newline at the end of the line)
1183 */
Jeff King6a498702014-12-10 10:40:191184 if (line->len <= 42)
1185 return NULL;
Michael Haggertybc5fd6d2012-04-10 05:30:131186
Jeff King6a498702014-12-10 10:40:191187 if (get_sha1_hex(line->buf, sha1) < 0)
Michael Haggertybc5fd6d2012-04-10 05:30:131188 return NULL;
Jeff King6a498702014-12-10 10:40:191189 if (!isspace(line->buf[40]))
Michael Haggertybc5fd6d2012-04-10 05:30:131190 return NULL;
Michael Haggertybc5fd6d2012-04-10 05:30:131191
Jeff King6a498702014-12-10 10:40:191192 ref = line->buf + 41;
1193 if (isspace(*ref))
1194 return NULL;
1195
1196 if (line->buf[line->len - 1] != '\n')
1197 return NULL;
1198 line->buf[--line->len] = 0;
1199
1200 return ref;
Michael Haggertybc5fd6d2012-04-10 05:30:131201}
1202
Michael Haggertyc29c46f2013-03-18 11:37:321203/*
1204 * Read f, which is a packed-refs file, into dir.
1205 *
1206 * A comment line of the form "# pack-refs with: " may contain zero or
1207 * more traits. We interpret the traits as follows:
1208 *
1209 * No traits:
1210 *
1211 * Probably no references are peeled. But if the file contains a
1212 * peeled value for a reference, we will use it.
1213 *
1214 * peeled:
1215 *
1216 * References under "refs/tags/", if they *can* be peeled, *are*
1217 * peeled in this file. References outside of "refs/tags/" are
1218 * probably not peeled even if they could have been, but if we find
1219 * a peeled value for such a reference we will use it.
1220 *
1221 * fully-peeled:
1222 *
1223 * All references in the file that can be peeled are peeled.
1224 * Inversely (and this is more important), any references in the
1225 * file for which no peeled value is recorded is not peelable. This
1226 * trait should typically be written alongside "peeled" for
1227 * compatibility with older clients, but we do not require it
1228 * (i.e., "peeled" is a no-op if "fully-peeled" is set).
1229 */
Michael Haggertyd3177272012-04-10 05:30:241230static void read_packed_refs(FILE *f, struct ref_dir *dir)
Junio C Hamanof4204ab2006-11-22 07:36:351231{
Julian Phillipse9c4c112011-09-29 22:11:421232 struct ref_entry *last = NULL;
Jeff King10c497a2014-12-10 10:40:071233 struct strbuf line = STRBUF_INIT;
Michael Haggertyc29c46f2013-03-18 11:37:321234 enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE;
Junio C Hamanof4204ab2006-11-22 07:36:351235
Jeff King10c497a2014-12-10 10:40:071236 while (strbuf_getwholeline(&line, f, '\n') != EOF) {
Junio C Hamanof4204ab2006-11-22 07:36:351237 unsigned char sha1[20];
Michael Haggertydfefa932011-12-12 05:38:091238 const char *refname;
Jeff Kingea417832014-12-10 10:40:361239 const char *traits;
Junio C Hamanof4204ab2006-11-22 07:36:351240
Jeff Kingea417832014-12-10 10:40:361241 if (skip_prefix(line.buf, "# pack-refs with:", &traits)) {
Michael Haggertyc29c46f2013-03-18 11:37:321242 if (strstr(traits, " fully-peeled "))
1243 peeled = PEELED_FULLY;
1244 else if (strstr(traits, " peeled "))
1245 peeled = PEELED_TAGS;
Junio C Hamanof4204ab2006-11-22 07:36:351246 /* perhaps other traits later as well */
1247 continue;
1248 }
1249
Jeff King6a498702014-12-10 10:40:191250 refname = parse_ref_line(&line, sha1);
Michael Haggertydfefa932011-12-12 05:38:091251 if (refname) {
Ronnie Sahlbergd0f810f2014-09-03 18:45:431252 int flag = REF_ISPACKED;
1253
1254 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
Jeff King03afcbe2015-04-16 09:03:261255 if (!refname_is_safe(refname))
1256 die("packed refname is dangerous: %s", refname);
Ronnie Sahlbergd0f810f2014-09-03 18:45:431257 hashclr(sha1);
1258 flag |= REF_BAD_NAME | REF_ISBROKEN;
1259 }
1260 last = create_ref_entry(refname, sha1, flag, 0);
Michael Haggertyc29c46f2013-03-18 11:37:321261 if (peeled == PEELED_FULLY ||
Christian Couder59556542013-11-30 20:55:401262 (peeled == PEELED_TAGS && starts_with(refname, "refs/tags/")))
Michael Haggertyc29c46f2013-03-18 11:37:321263 last->flag |= REF_KNOWS_PEELED;
Michael Haggertyd3177272012-04-10 05:30:241264 add_ref(dir, last);
Junio C Hamanof4204ab2006-11-22 07:36:351265 continue;
1266 }
1267 if (last &&
Jeff King10c497a2014-12-10 10:40:071268 line.buf[0] == '^' &&
1269 line.len == PEELED_LINE_LENGTH &&
1270 line.buf[PEELED_LINE_LENGTH - 1] == '\n' &&
1271 !get_sha1_hex(line.buf + 1, sha1)) {
brian m. carlson83538472015-05-25 18:38:271272 hashcpy(last->u.value.peeled.hash, sha1);
Michael Haggertyc29c46f2013-03-18 11:37:321273 /*
1274 * Regardless of what the file header said,
1275 * we definitely know the value of *this*
1276 * reference:
1277 */
1278 last->flag |= REF_KNOWS_PEELED;
1279 }
Junio C Hamanof4204ab2006-11-22 07:36:351280 }
Jeff King10c497a2014-12-10 10:40:071281
1282 strbuf_release(&line);
Junio C Hamanof4204ab2006-11-22 07:36:351283}
1284
Michael Haggerty2fff7812013-06-20 08:37:451285/*
1286 * Get the packed_ref_cache for the specified ref_cache, creating it
1287 * if necessary.
1288 */
1289static struct packed_ref_cache *get_packed_ref_cache(struct ref_cache *refs)
Linus Torvaldse1e22e32006-09-11 23:37:321290{
Jeff Kingfcd12db2015-08-10 09:35:311291 char *packed_refs_file;
Jeff Kingca919932013-06-20 08:37:521292
1293 if (*refs->name)
Jeff Kingfcd12db2015-08-10 09:35:311294 packed_refs_file = git_pathdup_submodule(refs->name, "packed-refs");
Jeff Kingca919932013-06-20 08:37:521295 else
Jeff Kingfcd12db2015-08-10 09:35:311296 packed_refs_file = git_pathdup("packed-refs");
Jeff Kingca919932013-06-20 08:37:521297
1298 if (refs->packed &&
1299 !stat_validity_check(&refs->packed->validity, packed_refs_file))
1300 clear_packed_ref_cache(refs);
1301
Michael Haggertyd12229f2012-04-26 22:27:011302 if (!refs->packed) {
Michael Haggerty4349a662011-08-12 22:36:251303 FILE *f;
Heiko Voigt0bad6112010-07-07 13:39:111304
Michael Haggerty2fff7812013-06-20 08:37:451305 refs->packed = xcalloc(1, sizeof(*refs->packed));
Michael Haggerty5f5e2a82013-06-20 08:37:471306 acquire_packed_ref_cache(refs->packed);
Michael Haggerty2fff7812013-06-20 08:37:451307 refs->packed->root = create_dir_entry(refs, "", 0, 0);
Michael Haggerty4349a662011-08-12 22:36:251308 f = fopen(packed_refs_file, "r");
Linus Torvaldse1e22e32006-09-11 23:37:321309 if (f) {
Jeff Kingca919932013-06-20 08:37:521310 stat_validity_update(&refs->packed->validity, fileno(f));
Michael Haggerty2fff7812013-06-20 08:37:451311 read_packed_refs(f, get_ref_dir(refs->packed->root));
Linus Torvaldse1e22e32006-09-11 23:37:321312 fclose(f);
Linus Torvaldse1e22e32006-09-11 23:37:321313 }
Linus Torvaldse1e22e32006-09-11 23:37:321314 }
Jeff Kingfcd12db2015-08-10 09:35:311315 free(packed_refs_file);
Michael Haggerty2fff7812013-06-20 08:37:451316 return refs->packed;
1317}
1318
1319static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_cache)
1320{
1321 return get_ref_dir(packed_ref_cache->root);
1322}
1323
1324static struct ref_dir *get_packed_refs(struct ref_cache *refs)
1325{
1326 return get_packed_ref_dir(get_packed_ref_cache(refs));
Linus Torvaldse1e22e32006-09-11 23:37:321327}
1328
Michael Haggerty0a4b24f2015-06-22 14:03:021329/*
1330 * Add a reference to the in-memory packed reference cache. This may
1331 * only be called while the packed-refs file is locked (see
1332 * lock_packed_refs()). To actually write the packed-refs file, call
1333 * commit_packed_refs().
1334 */
1335static void add_packed_ref(const char *refname, const unsigned char *sha1)
Michael Haggerty30249ee2012-01-17 05:50:331336{
Michael Haggerty9f69d292013-06-20 08:37:461337 struct packed_ref_cache *packed_ref_cache =
1338 get_packed_ref_cache(&ref_cache);
1339
1340 if (!packed_ref_cache->lock)
1341 die("internal error: packed refs not locked");
1342 add_ref(get_packed_ref_dir(packed_ref_cache),
Michael Haggerty9da31cb2013-04-22 19:52:411343 create_ref_entry(refname, sha1, REF_ISPACKED, 1));
Michael Haggerty30249ee2012-01-17 05:50:331344}
1345
Michael Haggertyabc39092012-04-24 22:45:101346/*
Michael Haggerty28e6a342012-04-26 22:27:071347 * Read the loose references from the namespace dirname into dir
1348 * (without recursing). dirname must end with '/'. dir must be the
1349 * directory entry corresponding to dirname.
Michael Haggertyabc39092012-04-24 22:45:101350 */
Michael Haggerty423a1af2012-04-26 22:27:061351static void read_loose_refs(const char *dirname, struct ref_dir *dir)
Linus Torvaldse1e22e32006-09-11 23:37:321352{
Michael Haggerty423a1af2012-04-26 22:27:061353 struct ref_cache *refs = dir->ref_cache;
Michael Haggertyd3177272012-04-10 05:30:241354 DIR *d;
Michael Haggertyd5fdae62012-04-24 22:45:071355 struct dirent *de;
Michael Haggertyabc39092012-04-24 22:45:101356 int dirnamelen = strlen(dirname);
Michael Haggerty72b64b42012-04-24 22:45:081357 struct strbuf refname;
Jeff Kingf5b2dec2015-08-10 09:36:191358 struct strbuf path = STRBUF_INIT;
1359 size_t path_baselen;
Heiko Voigt0bad6112010-07-07 13:39:111360
Michael Haggerty3b124822011-12-12 05:38:171361 if (*refs->name)
Jeff Kingf5b2dec2015-08-10 09:36:191362 strbuf_git_path_submodule(&path, refs->name, "%s", dirname);
Heiko Voigt0bad6112010-07-07 13:39:111363 else
Jeff Kingf5b2dec2015-08-10 09:36:191364 strbuf_git_path(&path, "%s", dirname);
1365 path_baselen = path.len;
Heiko Voigt0bad6112010-07-07 13:39:111366
Jeff Kingf5b2dec2015-08-10 09:36:191367 d = opendir(path.buf);
1368 if (!d) {
1369 strbuf_release(&path);
Michael Haggertyd5fdae62012-04-24 22:45:071370 return;
Jeff Kingf5b2dec2015-08-10 09:36:191371 }
Linus Torvaldse1e22e32006-09-11 23:37:321372
Michael Haggerty66a3d202012-04-24 22:45:091373 strbuf_init(&refname, dirnamelen + 257);
1374 strbuf_add(&refname, dirname, dirnamelen);
Linus Torvaldse1e22e32006-09-11 23:37:321375
Michael Haggertyd5fdae62012-04-24 22:45:071376 while ((de = readdir(d)) != NULL) {
1377 unsigned char sha1[20];
1378 struct stat st;
1379 int flag;
Linus Torvaldse1e22e32006-09-11 23:37:321380
Michael Haggertyd5fdae62012-04-24 22:45:071381 if (de->d_name[0] == '.')
1382 continue;
Jeff King2975c772014-06-30 16:58:251383 if (ends_with(de->d_name, ".lock"))
Michael Haggertyd5fdae62012-04-24 22:45:071384 continue;
Michael Haggerty72b64b42012-04-24 22:45:081385 strbuf_addstr(&refname, de->d_name);
Jeff Kingf5b2dec2015-08-10 09:36:191386 strbuf_addstr(&path, de->d_name);
1387 if (stat(path.buf, &st) < 0) {
Michael Haggerty72b64b42012-04-24 22:45:081388 ; /* silently ignore */
1389 } else if (S_ISDIR(st.st_mode)) {
Michael Haggertyabc39092012-04-24 22:45:101390 strbuf_addch(&refname, '/');
Michael Haggerty28e6a342012-04-26 22:27:071391 add_entry_to_dir(dir,
René Scharfeb9146f52012-05-22 18:50:521392 create_dir_entry(refs, refname.buf,
1393 refname.len, 1));
Michael Haggerty72b64b42012-04-24 22:45:081394 } else {
Michael Haggertyf5517072015-06-03 13:51:581395 int read_ok;
1396
Michael Haggerty3b124822011-12-12 05:38:171397 if (*refs->name) {
Junio C Hamanof8948e22009-02-09 07:27:101398 hashclr(sha1);
Heiko Voigt0bad6112010-07-07 13:39:111399 flag = 0;
Michael Haggertyf5517072015-06-03 13:51:581400 read_ok = !resolve_gitlink_ref(refs->name,
1401 refname.buf, sha1);
1402 } else {
1403 read_ok = !read_ref_full(refname.buf,
1404 RESOLVE_REF_READING,
1405 sha1, &flag);
1406 }
1407
1408 if (!read_ok) {
Junio C Hamano09116a12011-11-17 00:54:321409 hashclr(sha1);
1410 flag |= REF_ISBROKEN;
Michael Haggerty501cf472015-06-03 13:51:591411 } else if (is_null_sha1(sha1)) {
1412 /*
1413 * It is so astronomically unlikely
1414 * that NULL_SHA1 is the SHA-1 of an
1415 * actual object that we consider its
1416 * appearance in a loose reference
1417 * file to be repo corruption
1418 * (probably due to a software bug).
1419 */
1420 flag |= REF_ISBROKEN;
Junio C Hamano09116a12011-11-17 00:54:321421 }
Michael Haggertyf5517072015-06-03 13:51:581422
Ronnie Sahlbergd0f810f2014-09-03 18:45:431423 if (check_refname_format(refname.buf,
1424 REFNAME_ALLOW_ONELEVEL)) {
Jeff King03afcbe2015-04-16 09:03:261425 if (!refname_is_safe(refname.buf))
1426 die("loose refname is dangerous: %s", refname.buf);
Ronnie Sahlbergd0f810f2014-09-03 18:45:431427 hashclr(sha1);
1428 flag |= REF_BAD_NAME | REF_ISBROKEN;
1429 }
Michael Haggerty9f2fb4a2012-04-24 22:45:121430 add_entry_to_dir(dir,
Ronnie Sahlbergd0f810f2014-09-03 18:45:431431 create_ref_entry(refname.buf, sha1, flag, 0));
Linus Torvaldse1e22e32006-09-11 23:37:321432 }
Michael Haggerty66a3d202012-04-24 22:45:091433 strbuf_setlen(&refname, dirnamelen);
Jeff Kingf5b2dec2015-08-10 09:36:191434 strbuf_setlen(&path, path_baselen);
Linus Torvaldse1e22e32006-09-11 23:37:321435 }
Michael Haggerty72b64b42012-04-24 22:45:081436 strbuf_release(&refname);
Jeff Kingf5b2dec2015-08-10 09:36:191437 strbuf_release(&path);
Michael Haggertyd5fdae62012-04-24 22:45:071438 closedir(d);
Linus Torvaldse1e22e32006-09-11 23:37:321439}
1440
Michael Haggertyd3177272012-04-10 05:30:241441static struct ref_dir *get_loose_refs(struct ref_cache *refs)
Linus Torvaldse1e22e32006-09-11 23:37:321442{
Michael Haggertyd12229f2012-04-26 22:27:011443 if (!refs->loose) {
Michael Haggerty28e6a342012-04-26 22:27:071444 /*
1445 * Mark the top-level directory complete because we
1446 * are about to read the only subdirectory that can
1447 * hold references:
1448 */
René Scharfeb9146f52012-05-22 18:50:521449 refs->loose = create_dir_entry(refs, "", 0, 0);
Michael Haggerty28e6a342012-04-26 22:27:071450 /*
1451 * Create an incomplete entry for "refs/":
1452 */
1453 add_entry_to_dir(get_ref_dir(refs->loose),
René Scharfeb9146f52012-05-22 18:50:521454 create_dir_entry(refs, "refs/", 5, 1));
Linus Torvaldse1e22e32006-09-11 23:37:321455 }
Michael Haggertyd7826d52012-04-26 22:27:031456 return get_ref_dir(refs->loose);
Linus Torvaldse1e22e32006-09-11 23:37:321457}
1458
Linus Torvaldsca8db142005-09-25 16:59:371459/* We allow "recursive" symbolic refs. Only within reason, though */
1460#define MAXDEPTH 5
Linus Torvalds0ebde322007-04-10 04:14:261461#define MAXREFLEN (1024)
1462
Junio C Hamanoe5fa45c2011-10-17 18:43:301463/*
1464 * Called by resolve_gitlink_ref_recursive() after it failed to read
Michael Haggertyb0626602011-12-12 05:38:191465 * from the loose refs in ref_cache refs. Find <refname> in the
1466 * packed-refs file for the submodule.
Junio C Hamanoe5fa45c2011-10-17 18:43:301467 */
Michael Haggertyb0626602011-12-12 05:38:191468static int resolve_gitlink_packed_ref(struct ref_cache *refs,
Michael Haggerty85be1fe2011-12-12 05:38:101469 const char *refname, unsigned char *sha1)
Linus Torvalds0ebde322007-04-10 04:14:261470{
Junio C Hamano2c5c66b2011-10-10 22:56:191471 struct ref_entry *ref;
Michael Haggertyd3177272012-04-10 05:30:241472 struct ref_dir *dir = get_packed_refs(refs);
Linus Torvalds0ebde322007-04-10 04:14:261473
Michael Haggerty432ad412012-04-10 05:30:261474 ref = find_ref(dir, refname);
Michael Haggertyb0626602011-12-12 05:38:191475 if (ref == NULL)
1476 return -1;
1477
brian m. carlson83538472015-05-25 18:38:271478 hashcpy(sha1, ref->u.value.oid.hash);
Michael Haggertyb0626602011-12-12 05:38:191479 return 0;
Linus Torvalds0ebde322007-04-10 04:14:261480}
1481
Michael Haggertyb0626602011-12-12 05:38:191482static int resolve_gitlink_ref_recursive(struct ref_cache *refs,
Michael Haggerty85be1fe2011-12-12 05:38:101483 const char *refname, unsigned char *sha1,
Michael Haggertydfefa932011-12-12 05:38:091484 int recursion)
Linus Torvalds0ebde322007-04-10 04:14:261485{
Michael Haggerty064d51d2011-12-12 05:38:201486 int fd, len;
Linus Torvalds0ebde322007-04-10 04:14:261487 char buffer[128], *p;
Jeff Kingfcd12db2015-08-10 09:35:311488 char *path;
Linus Torvalds0ebde322007-04-10 04:14:261489
Michael Haggerty064d51d2011-12-12 05:38:201490 if (recursion > MAXDEPTH || strlen(refname) > MAXREFLEN)
Linus Torvalds0ebde322007-04-10 04:14:261491 return -1;
Michael Haggerty064d51d2011-12-12 05:38:201492 path = *refs->name
Jeff Kingfcd12db2015-08-10 09:35:311493 ? git_pathdup_submodule(refs->name, "%s", refname)
1494 : git_pathdup("%s", refname);
Michael Haggerty064d51d2011-12-12 05:38:201495 fd = open(path, O_RDONLY);
Jeff Kingfcd12db2015-08-10 09:35:311496 free(path);
Linus Torvalds0ebde322007-04-10 04:14:261497 if (fd < 0)
Michael Haggertyb0626602011-12-12 05:38:191498 return resolve_gitlink_packed_ref(refs, refname, sha1);
Linus Torvalds0ebde322007-04-10 04:14:261499
1500 len = read(fd, buffer, sizeof(buffer)-1);
1501 close(fd);
1502 if (len < 0)
1503 return -1;
1504 while (len && isspace(buffer[len-1]))
1505 len--;
1506 buffer[len] = 0;
1507
1508 /* Was it a detached head or an old-fashioned symlink? */
Michael Haggerty85be1fe2011-12-12 05:38:101509 if (!get_sha1_hex(buffer, sha1))
Linus Torvalds0ebde322007-04-10 04:14:261510 return 0;
1511
1512 /* Symref? */
1513 if (strncmp(buffer, "ref:", 4))
1514 return -1;
1515 p = buffer + 4;
1516 while (isspace(*p))
1517 p++;
1518
Michael Haggerty064d51d2011-12-12 05:38:201519 return resolve_gitlink_ref_recursive(refs, p, sha1, recursion+1);
Linus Torvalds0ebde322007-04-10 04:14:261520}
1521
Michael Haggerty85be1fe2011-12-12 05:38:101522int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1)
Linus Torvalds0ebde322007-04-10 04:14:261523{
1524 int len = strlen(path), retval;
Michael Haggerty064d51d2011-12-12 05:38:201525 char *submodule;
Michael Haggertyb0626602011-12-12 05:38:191526 struct ref_cache *refs;
Linus Torvalds0ebde322007-04-10 04:14:261527
1528 while (len && path[len-1] == '/')
1529 len--;
1530 if (!len)
1531 return -1;
Michael Haggertyb0626602011-12-12 05:38:191532 submodule = xstrndup(path, len);
1533 refs = get_ref_cache(submodule);
1534 free(submodule);
Linus Torvalds0ebde322007-04-10 04:14:261535
Michael Haggerty064d51d2011-12-12 05:38:201536 retval = resolve_gitlink_ref_recursive(refs, refname, sha1, 0);
Linus Torvalds0ebde322007-04-10 04:14:261537 return retval;
1538}
Linus Torvaldsca8db142005-09-25 16:59:371539
Christian Couder4886b892008-09-09 05:10:561540/*
Michael Haggerty63331582013-04-22 19:52:151541 * Return the ref_entry for the given refname from the packed
1542 * references. If it does not exist, return NULL.
Christian Couder4886b892008-09-09 05:10:561543 */
Michael Haggerty63331582013-04-22 19:52:151544static struct ref_entry *get_packed_ref(const char *refname)
Michael Haggertyc224ca72011-09-15 21:10:351545{
Michael Haggerty9da31cb2013-04-22 19:52:411546 return find_ref(get_packed_refs(&ref_cache), refname);
Michael Haggertyc224ca72011-09-15 21:10:351547}
1548
Michael Haggerty47f534b2013-06-19 06:36:261549/*
1550 * A loose ref file doesn't exist; check for a packed ref. The
1551 * options are forwarded from resolve_safe_unsafe().
1552 */
Ronnie Sahlbergd0f810f2014-09-03 18:45:431553static int resolve_missing_loose_ref(const char *refname,
1554 int resolve_flags,
1555 unsigned char *sha1,
1556 int *flags)
Michael Haggerty47f534b2013-06-19 06:36:261557{
1558 struct ref_entry *entry;
1559
1560 /*
1561 * The loose reference file does not exist; check for a packed
1562 * reference.
1563 */
1564 entry = get_packed_ref(refname);
1565 if (entry) {
brian m. carlson83538472015-05-25 18:38:271566 hashcpy(sha1, entry->u.value.oid.hash);
Ronnie Sahlberg7695d112014-07-15 19:59:361567 if (flags)
1568 *flags |= REF_ISPACKED;
Ronnie Sahlbergd0f810f2014-09-03 18:45:431569 return 0;
Michael Haggerty47f534b2013-06-19 06:36:261570 }
1571 /* The reference is not a packed reference, either. */
Ronnie Sahlberg7695d112014-07-15 19:59:361572 if (resolve_flags & RESOLVE_REF_READING) {
Ronnie Sahlbergd0f810f2014-09-03 18:45:431573 errno = ENOENT;
1574 return -1;
Michael Haggerty47f534b2013-06-19 06:36:261575 } else {
1576 hashclr(sha1);
Ronnie Sahlbergd0f810f2014-09-03 18:45:431577 return 0;
Michael Haggerty47f534b2013-06-19 06:36:261578 }
1579}
1580
Ronnie Sahlberg76d70dc2014-06-20 14:42:541581/* This function needs to return a meaningful errno on failure */
Nguyễn Thái Ngọc Duy1a83c242014-11-30 08:24:281582static const char *resolve_ref_unsafe_1(const char *refname,
1583 int resolve_flags,
1584 unsigned char *sha1,
1585 int *flags,
1586 struct strbuf *sb_path)
Junio C Hamanoa876ed82005-09-30 21:08:251587{
Heikki Orsila0104ca02008-04-27 18:21:581588 int depth = MAXDEPTH;
1589 ssize_t len;
Junio C Hamanoa876ed82005-09-30 21:08:251590 char buffer[256];
Michael Haggertydfefa932011-12-12 05:38:091591 static char refname_buffer[256];
Ronnie Sahlbergd0f810f2014-09-03 18:45:431592 int bad_name = 0;
Junio C Hamanoa876ed82005-09-30 21:08:251593
Ronnie Sahlberg7695d112014-07-15 19:59:361594 if (flags)
1595 *flags = 0;
Junio C Hamano8da19772006-09-21 05:02:011596
Ronnie Sahlberg76d70dc2014-06-20 14:42:541597 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
Ronnie Sahlbergd0f810f2014-09-03 18:45:431598 if (flags)
1599 *flags |= REF_BAD_NAME;
1600
1601 if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
1602 !refname_is_safe(refname)) {
1603 errno = EINVAL;
1604 return NULL;
1605 }
1606 /*
1607 * dwim_ref() uses REF_ISBROKEN to distinguish between
1608 * missing refs and refs that were present but invalid,
1609 * to complain about the latter to stderr.
1610 *
1611 * We don't know whether the ref exists, so don't set
1612 * REF_ISBROKEN yet.
1613 */
1614 bad_name = 1;
Ronnie Sahlberg76d70dc2014-06-20 14:42:541615 }
Junio C Hamanoa876ed82005-09-30 21:08:251616 for (;;) {
Nguyễn Thái Ngọc Duy1a83c242014-11-30 08:24:281617 const char *path;
Junio C Hamanoa876ed82005-09-30 21:08:251618 struct stat st;
1619 char *buf;
1620 int fd;
1621
Ronnie Sahlberg76d70dc2014-06-20 14:42:541622 if (--depth < 0) {
1623 errno = ELOOP;
Junio C Hamanoa876ed82005-09-30 21:08:251624 return NULL;
Ronnie Sahlberg76d70dc2014-06-20 14:42:541625 }
Junio C Hamanoa876ed82005-09-30 21:08:251626
Nguyễn Thái Ngọc Duy1a83c242014-11-30 08:24:281627 strbuf_reset(sb_path);
1628 strbuf_git_path(sb_path, "%s", refname);
1629 path = sb_path->buf;
Michael Haggertyc224ca72011-09-15 21:10:351630
Michael Haggertyfcb7c762013-06-19 06:36:281631 /*
1632 * We might have to loop back here to avoid a race
1633 * condition: first we lstat() the file, then we try
1634 * to read it as a link or as a file. But if somebody
1635 * changes the type of the file (file <-> directory
1636 * <-> symlink) between the lstat() and reading, then
1637 * we don't want to report that as an error but rather
1638 * try again starting with the lstat().
1639 */
1640 stat_ref:
Junio C Hamanoa876ed82005-09-30 21:08:251641 if (lstat(path, &st) < 0) {
Ronnie Sahlbergd0f810f2014-09-03 18:45:431642 if (errno != ENOENT)
Junio C Hamanoa876ed82005-09-30 21:08:251643 return NULL;
Ronnie Sahlbergd0f810f2014-09-03 18:45:431644 if (resolve_missing_loose_ref(refname, resolve_flags,
1645 sha1, flags))
1646 return NULL;
1647 if (bad_name) {
1648 hashclr(sha1);
1649 if (flags)
1650 *flags |= REF_ISBROKEN;
1651 }
1652 return refname;
Junio C Hamanoa876ed82005-09-30 21:08:251653 }
1654
1655 /* Follow "normalized" - ie "refs/.." symlinks by hand */
1656 if (S_ISLNK(st.st_mode)) {
1657 len = readlink(path, buffer, sizeof(buffer)-1);
Michael Haggertyfcb7c762013-06-19 06:36:281658 if (len < 0) {
1659 if (errno == ENOENT || errno == EINVAL)
1660 /* inconsistent with lstat; retry */
1661 goto stat_ref;
1662 else
1663 return NULL;
1664 }
Michael Haggertyb54cb792011-09-15 21:10:321665 buffer[len] = 0;
Christian Couder59556542013-11-30 20:55:401666 if (starts_with(buffer, "refs/") &&
Michael Haggerty1f58a032011-09-15 21:10:331667 !check_refname_format(buffer, 0)) {
Michael Haggertydfefa932011-12-12 05:38:091668 strcpy(refname_buffer, buffer);
1669 refname = refname_buffer;
Ronnie Sahlberg7695d112014-07-15 19:59:361670 if (flags)
1671 *flags |= REF_ISSYMREF;
Jonathan Nieder62a2d522014-09-11 01:22:481672 if (resolve_flags & RESOLVE_REF_NO_RECURSE) {
1673 hashclr(sha1);
1674 return refname;
1675 }
Junio C Hamanoa876ed82005-09-30 21:08:251676 continue;
1677 }
1678 }
1679
Dennis Stosberg7a216322006-10-02 17:23:531680 /* Is it a directory? */
1681 if (S_ISDIR(st.st_mode)) {
1682 errno = EISDIR;
1683 return NULL;
1684 }
1685
Junio C Hamanoa876ed82005-09-30 21:08:251686 /*
1687 * Anything else, just open it and try to use it as
1688 * a ref
1689 */
1690 fd = open(path, O_RDONLY);
Michael Haggertyfcb7c762013-06-19 06:36:281691 if (fd < 0) {
1692 if (errno == ENOENT)
1693 /* inconsistent with lstat; retry */
1694 goto stat_ref;
1695 else
1696 return NULL;
1697 }
Andy Whitcroft93d26e42007-01-08 15:58:081698 len = read_in_full(fd, buffer, sizeof(buffer)-1);
Ronnie Sahlberg76d70dc2014-06-20 14:42:541699 if (len < 0) {
1700 int save_errno = errno;
1701 close(fd);
1702 errno = save_errno;
Michael Haggerty28775052011-09-15 21:10:341703 return NULL;
Ronnie Sahlberg76d70dc2014-06-20 14:42:541704 }
1705 close(fd);
Michael Haggerty28775052011-09-15 21:10:341706 while (len && isspace(buffer[len-1]))
1707 len--;
1708 buffer[len] = '\0';
Junio C Hamanoa876ed82005-09-30 21:08:251709
1710 /*
1711 * Is it a symbolic ref?
1712 */
Christian Couder59556542013-11-30 20:55:401713 if (!starts_with(buffer, "ref:")) {
Michael Haggerty2884c062013-06-19 06:36:271714 /*
1715 * Please note that FETCH_HEAD has a second
1716 * line containing other data.
1717 */
1718 if (get_sha1_hex(buffer, sha1) ||
1719 (buffer[40] != '\0' && !isspace(buffer[40]))) {
Ronnie Sahlberg7695d112014-07-15 19:59:361720 if (flags)
1721 *flags |= REF_ISBROKEN;
Ronnie Sahlberg76d70dc2014-06-20 14:42:541722 errno = EINVAL;
Michael Haggerty2884c062013-06-19 06:36:271723 return NULL;
1724 }
Ronnie Sahlbergd0f810f2014-09-03 18:45:431725 if (bad_name) {
1726 hashclr(sha1);
1727 if (flags)
1728 *flags |= REF_ISBROKEN;
1729 }
Michael Haggerty2884c062013-06-19 06:36:271730 return refname;
1731 }
Ronnie Sahlberg7695d112014-07-15 19:59:361732 if (flags)
1733 *flags |= REF_ISSYMREF;
Junio C Hamanoa876ed82005-09-30 21:08:251734 buf = buffer + 4;
Michael Haggerty28775052011-09-15 21:10:341735 while (isspace(*buf))
1736 buf++;
Jonathan Nieder62a2d522014-09-11 01:22:481737 refname = strcpy(refname_buffer, buf);
1738 if (resolve_flags & RESOLVE_REF_NO_RECURSE) {
1739 hashclr(sha1);
1740 return refname;
1741 }
Michael Haggerty313fb012011-09-15 21:10:361742 if (check_refname_format(buf, REFNAME_ALLOW_ONELEVEL)) {
Ronnie Sahlberg7695d112014-07-15 19:59:361743 if (flags)
1744 *flags |= REF_ISBROKEN;
Ronnie Sahlbergd0f810f2014-09-03 18:45:431745
1746 if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
1747 !refname_is_safe(buf)) {
1748 errno = EINVAL;
1749 return NULL;
1750 }
1751 bad_name = 1;
Michael Haggerty313fb012011-09-15 21:10:361752 }
Junio C Hamanoa876ed82005-09-30 21:08:251753 }
Junio C Hamanoa876ed82005-09-30 21:08:251754}
1755
Nguyễn Thái Ngọc Duy1a83c242014-11-30 08:24:281756const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
1757 unsigned char *sha1, int *flags)
1758{
1759 struct strbuf sb_path = STRBUF_INIT;
1760 const char *ret = resolve_ref_unsafe_1(refname, resolve_flags,
1761 sha1, flags, &sb_path);
1762 strbuf_release(&sb_path);
1763 return ret;
1764}
1765
Michael Haggertyfb58c8d2015-06-22 14:03:051766char *resolve_refdup(const char *refname, int resolve_flags,
1767 unsigned char *sha1, int *flags)
Nguyễn Thái Ngọc Duy96ec7b12011-12-13 14:17:481768{
Michael Haggertyfb58c8d2015-06-22 14:03:051769 return xstrdup_or_null(resolve_ref_unsafe(refname, resolve_flags,
1770 sha1, flags));
Nguyễn Thái Ngọc Duy96ec7b12011-12-13 14:17:481771}
1772
Ilari Liusvaarad08bae72010-01-20 09:48:251773/* The argument to filter_refs */
1774struct ref_filter {
1775 const char *pattern;
1776 each_ref_fn *fn;
1777 void *cb_data;
1778};
1779
Ronnie Sahlberg7695d112014-07-15 19:59:361780int read_ref_full(const char *refname, int resolve_flags, unsigned char *sha1, int *flags)
Linus Torvalds8a65ff72005-07-03 03:23:361781{
Ronnie Sahlberg7695d112014-07-15 19:59:361782 if (resolve_ref_unsafe(refname, resolve_flags, sha1, flags))
Junio C Hamanoa876ed82005-09-30 21:08:251783 return 0;
1784 return -1;
Linus Torvalds8a65ff72005-07-03 03:23:361785}
1786
Michael Haggertydfefa932011-12-12 05:38:091787int read_ref(const char *refname, unsigned char *sha1)
Nguyễn Thái Ngọc Duyc6893322011-11-13 10:22:141788{
Ronnie Sahlberg7695d112014-07-15 19:59:361789 return read_ref_full(refname, RESOLVE_REF_READING, sha1, NULL);
Nguyễn Thái Ngọc Duyc6893322011-11-13 10:22:141790}
1791
Michael Haggertybc5fd6d2012-04-10 05:30:131792int ref_exists(const char *refname)
Junio C Hamanoef06b912006-11-19 06:13:331793{
Michael Haggertybc5fd6d2012-04-10 05:30:131794 unsigned char sha1[20];
Ronnie Sahlberg7695d112014-07-15 19:59:361795 return !!resolve_ref_unsafe(refname, RESOLVE_REF_READING, sha1, NULL);
Junio C Hamanoef06b912006-11-19 06:13:331796}
1797
Michael Haggerty2b2a5be2015-05-25 18:38:281798static int filter_refs(const char *refname, const struct object_id *oid,
Michael Haggerty4e675d12015-05-25 18:39:211799 int flags, void *data)
Ilari Liusvaarad08bae72010-01-20 09:48:251800{
1801 struct ref_filter *filter = (struct ref_filter *)data;
Michael Haggerty2b2a5be2015-05-25 18:38:281802
Nguyễn Thái Ngọc Duyeb078942014-02-15 02:01:461803 if (wildmatch(filter->pattern, refname, 0, NULL))
Ilari Liusvaarad08bae72010-01-20 09:48:251804 return 0;
Michael Haggerty2b2a5be2015-05-25 18:38:281805 return filter->fn(refname, oid, flags, filter->cb_data);
Ilari Liusvaarad08bae72010-01-20 09:48:251806}
1807
Michael Haggerty68cf8702013-04-22 19:52:201808enum peel_status {
1809 /* object was peeled successfully: */
1810 PEEL_PEELED = 0,
1811
1812 /*
1813 * object cannot be peeled because the named object (or an
1814 * object referred to by a tag in the peel chain), does not
1815 * exist.
1816 */
1817 PEEL_INVALID = -1,
1818
1819 /* object cannot be peeled because it is not a tag: */
Michael Haggerty9a489f32013-04-22 19:52:221820 PEEL_NON_TAG = -2,
1821
1822 /* ref_entry contains no peeled value because it is a symref: */
1823 PEEL_IS_SYMREF = -3,
1824
1825 /*
1826 * ref_entry cannot be peeled because it is broken (i.e., the
1827 * symbolic reference cannot even be resolved to an object
1828 * name):
1829 */
1830 PEEL_BROKEN = -4
Michael Haggerty68cf8702013-04-22 19:52:201831};
1832
Michael Haggertycb2ae1c2013-04-22 19:52:191833/*
1834 * Peel the named object; i.e., if the object is a tag, resolve the
Michael Haggerty68cf8702013-04-22 19:52:201835 * tag recursively until a non-tag is found. If successful, store the
1836 * result to sha1 and return PEEL_PEELED. If the object is not a tag
1837 * or is not valid, return PEEL_NON_TAG or PEEL_INVALID, respectively,
1838 * and leave sha1 unchanged.
Michael Haggertycb2ae1c2013-04-22 19:52:191839 */
Michael Haggerty68cf8702013-04-22 19:52:201840static enum peel_status peel_object(const unsigned char *name, unsigned char *sha1)
Michael Haggertycb2ae1c2013-04-22 19:52:191841{
1842 struct object *o = lookup_unknown_object(name);
1843
1844 if (o->type == OBJ_NONE) {
1845 int type = sha1_object_info(name, NULL);
Jeff King8ff226a2014-07-13 06:42:031846 if (type < 0 || !object_as_type(o, type, 0))
Michael Haggerty68cf8702013-04-22 19:52:201847 return PEEL_INVALID;
Michael Haggertycb2ae1c2013-04-22 19:52:191848 }
1849
1850 if (o->type != OBJ_TAG)
Michael Haggerty68cf8702013-04-22 19:52:201851 return PEEL_NON_TAG;
Michael Haggertycb2ae1c2013-04-22 19:52:191852
1853 o = deref_tag_noverify(o);
1854 if (!o)
Michael Haggerty68cf8702013-04-22 19:52:201855 return PEEL_INVALID;
Michael Haggertycb2ae1c2013-04-22 19:52:191856
1857 hashcpy(sha1, o->sha1);
Michael Haggerty68cf8702013-04-22 19:52:201858 return PEEL_PEELED;
Michael Haggertycb2ae1c2013-04-22 19:52:191859}
1860
Michael Haggerty9a489f32013-04-22 19:52:221861/*
Michael Haggertyf85354b2013-04-22 19:52:371862 * Peel the entry (if possible) and return its new peel_status. If
1863 * repeel is true, re-peel the entry even if there is an old peeled
1864 * value that is already stored in it.
Michael Haggerty694b7a12013-04-22 19:52:291865 *
1866 * It is OK to call this function with a packed reference entry that
1867 * might be stale and might even refer to an object that has since
1868 * been garbage-collected. In such a case, if the entry has
1869 * REF_KNOWS_PEELED then leave the status unchanged and return
1870 * PEEL_PEELED or PEEL_NON_TAG; otherwise, return PEEL_INVALID.
Michael Haggerty9a489f32013-04-22 19:52:221871 */
Michael Haggertyf85354b2013-04-22 19:52:371872static enum peel_status peel_entry(struct ref_entry *entry, int repeel)
Michael Haggerty9a489f32013-04-22 19:52:221873{
1874 enum peel_status status;
1875
Michael Haggertyf85354b2013-04-22 19:52:371876 if (entry->flag & REF_KNOWS_PEELED) {
1877 if (repeel) {
1878 entry->flag &= ~REF_KNOWS_PEELED;
brian m. carlson83538472015-05-25 18:38:271879 oidclr(&entry->u.value.peeled);
Michael Haggertyf85354b2013-04-22 19:52:371880 } else {
brian m. carlson83538472015-05-25 18:38:271881 return is_null_oid(&entry->u.value.peeled) ?
Michael Haggertyf85354b2013-04-22 19:52:371882 PEEL_NON_TAG : PEEL_PEELED;
1883 }
1884 }
Michael Haggerty9a489f32013-04-22 19:52:221885 if (entry->flag & REF_ISBROKEN)
1886 return PEEL_BROKEN;
1887 if (entry->flag & REF_ISSYMREF)
1888 return PEEL_IS_SYMREF;
1889
brian m. carlson83538472015-05-25 18:38:271890 status = peel_object(entry->u.value.oid.hash, entry->u.value.peeled.hash);
Michael Haggerty9a489f32013-04-22 19:52:221891 if (status == PEEL_PEELED || status == PEEL_NON_TAG)
1892 entry->flag |= REF_KNOWS_PEELED;
1893 return status;
1894}
1895
Michael Haggertydfefa932011-12-12 05:38:091896int peel_ref(const char *refname, unsigned char *sha1)
Junio C Hamanocf0adba2006-11-19 21:22:441897{
1898 int flag;
1899 unsigned char base[20];
Junio C Hamanocf0adba2006-11-19 21:22:441900
Michael Haggertydfefa932011-12-12 05:38:091901 if (current_ref && (current_ref->name == refname
Michael Haggerty9a489f32013-04-22 19:52:221902 || !strcmp(current_ref->name, refname))) {
Michael Haggertyf85354b2013-04-22 19:52:371903 if (peel_entry(current_ref, 0))
Michael Haggerty9a489f32013-04-22 19:52:221904 return -1;
brian m. carlson83538472015-05-25 18:38:271905 hashcpy(sha1, current_ref->u.value.peeled.hash);
Michael Haggerty9a489f32013-04-22 19:52:221906 return 0;
Shawn O. Pearce0ae91be2008-02-24 08:07:221907 }
1908
Ronnie Sahlberg7695d112014-07-15 19:59:361909 if (read_ref_full(refname, RESOLVE_REF_READING, base, &flag))
Junio C Hamanocf0adba2006-11-19 21:22:441910 return -1;
1911
Michael Haggerty9a489f32013-04-22 19:52:221912 /*
1913 * If the reference is packed, read its ref_entry from the
1914 * cache in the hope that we already know its peeled value.
1915 * We only try this optimization on packed references because
1916 * (a) forcing the filling of the loose reference cache could
1917 * be expensive and (b) loose references anyway usually do not
1918 * have REF_KNOWS_PEELED.
1919 */
1920 if (flag & REF_ISPACKED) {
Michael Haggertyf361bae2013-04-22 19:52:161921 struct ref_entry *r = get_packed_ref(refname);
Michael Haggerty9a489f32013-04-22 19:52:221922 if (r) {
Michael Haggertyf85354b2013-04-22 19:52:371923 if (peel_entry(r, 0))
Michael Haggerty9a489f32013-04-22 19:52:221924 return -1;
brian m. carlson83538472015-05-25 18:38:271925 hashcpy(sha1, r->u.value.peeled.hash);
Julian Phillipse9c4c112011-09-29 22:11:421926 return 0;
Junio C Hamanocf0adba2006-11-19 21:22:441927 }
Junio C Hamanocf0adba2006-11-19 21:22:441928 }
1929
Michael Haggertycb2ae1c2013-04-22 19:52:191930 return peel_object(base, sha1);
Junio C Hamanocf0adba2006-11-19 21:22:441931}
1932
Michael Haggertybc5fd6d2012-04-10 05:30:131933struct warn_if_dangling_data {
1934 FILE *fp;
1935 const char *refname;
Jens Lindströme6bea662014-05-23 10:30:251936 const struct string_list *refnames;
Michael Haggertybc5fd6d2012-04-10 05:30:131937 const char *msg_fmt;
1938};
1939
Michael Haggerty2b2a5be2015-05-25 18:38:281940static int warn_if_dangling_symref(const char *refname, const struct object_id *oid,
Michael Haggertybc5fd6d2012-04-10 05:30:131941 int flags, void *cb_data)
1942{
1943 struct warn_if_dangling_data *d = cb_data;
1944 const char *resolves_to;
Michael Haggerty4e675d12015-05-25 18:39:211945 struct object_id junk;
Michael Haggertybc5fd6d2012-04-10 05:30:131946
1947 if (!(flags & REF_ISSYMREF))
1948 return 0;
1949
Michael Haggerty4e675d12015-05-25 18:39:211950 resolves_to = resolve_ref_unsafe(refname, 0, junk.hash, NULL);
Jens Lindströme6bea662014-05-23 10:30:251951 if (!resolves_to
1952 || (d->refname
1953 ? strcmp(resolves_to, d->refname)
1954 : !string_list_has_string(d->refnames, resolves_to))) {
Michael Haggertybc5fd6d2012-04-10 05:30:131955 return 0;
Jens Lindströme6bea662014-05-23 10:30:251956 }
Michael Haggertybc5fd6d2012-04-10 05:30:131957
1958 fprintf(d->fp, d->msg_fmt, refname);
Junio C Hamano1be65ed2012-05-02 20:51:351959 fputc('\n', d->fp);
Michael Haggertybc5fd6d2012-04-10 05:30:131960 return 0;
1961}
1962
1963void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname)
1964{
1965 struct warn_if_dangling_data data;
1966
1967 data.fp = fp;
1968 data.refname = refname;
Jens Lindströme6bea662014-05-23 10:30:251969 data.refnames = NULL;
1970 data.msg_fmt = msg_fmt;
1971 for_each_rawref(warn_if_dangling_symref, &data);
1972}
1973
1974void warn_dangling_symrefs(FILE *fp, const char *msg_fmt, const struct string_list *refnames)
1975{
1976 struct warn_if_dangling_data data;
1977
1978 data.fp = fp;
1979 data.refname = NULL;
1980 data.refnames = refnames;
Michael Haggertybc5fd6d2012-04-10 05:30:131981 data.msg_fmt = msg_fmt;
1982 for_each_rawref(warn_if_dangling_symref, &data);
1983}
1984
Michael Haggertyfcce1702013-04-22 19:52:111985/*
Michael Haggerty65cf1022013-04-22 19:52:401986 * Call fn for each reference in the specified ref_cache, omitting
Michael Haggerty624cac32013-04-22 19:52:231987 * references not in the containing_dir of base. fn is called for all
1988 * references, including broken ones. If fn ever returns a non-zero
Michael Haggertyfcce1702013-04-22 19:52:111989 * value, stop the iteration and return that value; otherwise, return
1990 * 0.
1991 */
Michael Haggerty65cf1022013-04-22 19:52:401992static int do_for_each_entry(struct ref_cache *refs, const char *base,
Michael Haggerty624cac32013-04-22 19:52:231993 each_ref_entry_fn fn, void *cb_data)
Linus Torvalds8a65ff72005-07-03 03:23:361994{
Jeff King98eeb092013-06-20 08:37:531995 struct packed_ref_cache *packed_ref_cache;
1996 struct ref_dir *loose_dir;
1997 struct ref_dir *packed_dir;
Michael Haggerty933ac032012-04-10 05:30:271998 int retval = 0;
1999
Jeff King98eeb092013-06-20 08:37:532000 /*
2001 * We must make sure that all loose refs are read before accessing the
2002 * packed-refs file; this avoids a race condition in which loose refs
2003 * are migrated to the packed-refs file by a simultaneous process, but
2004 * our in-memory view is from before the migration. get_packed_ref_cache()
2005 * takes care of making sure our view is up to date with what is on
2006 * disk.
2007 */
2008 loose_dir = get_loose_refs(refs);
2009 if (base && *base) {
2010 loose_dir = find_containing_dir(loose_dir, base, 0);
2011 }
2012 if (loose_dir)
2013 prime_ref_dir(loose_dir);
2014
2015 packed_ref_cache = get_packed_ref_cache(refs);
Michael Haggerty8baf2bb2013-06-20 08:37:482016 acquire_packed_ref_cache(packed_ref_cache);
Jeff King98eeb092013-06-20 08:37:532017 packed_dir = get_packed_ref_dir(packed_ref_cache);
Michael Haggerty933ac032012-04-10 05:30:272018 if (base && *base) {
2019 packed_dir = find_containing_dir(packed_dir, base, 0);
Michael Haggerty933ac032012-04-10 05:30:272020 }
2021
2022 if (packed_dir && loose_dir) {
2023 sort_ref_dir(packed_dir);
2024 sort_ref_dir(loose_dir);
Michael Haggerty624cac32013-04-22 19:52:232025 retval = do_for_each_entry_in_dirs(
2026 packed_dir, loose_dir, fn, cb_data);
Michael Haggerty933ac032012-04-10 05:30:272027 } else if (packed_dir) {
2028 sort_ref_dir(packed_dir);
Michael Haggerty624cac32013-04-22 19:52:232029 retval = do_for_each_entry_in_dir(
2030 packed_dir, 0, fn, cb_data);
Michael Haggerty933ac032012-04-10 05:30:272031 } else if (loose_dir) {
2032 sort_ref_dir(loose_dir);
Michael Haggerty624cac32013-04-22 19:52:232033 retval = do_for_each_entry_in_dir(
2034 loose_dir, 0, fn, cb_data);
Michael Haggerty933ac032012-04-10 05:30:272035 }
2036
Michael Haggerty8baf2bb2013-06-20 08:37:482037 release_packed_ref_cache(packed_ref_cache);
Michael Haggerty933ac032012-04-10 05:30:272038 return retval;
Linus Torvalds8a65ff72005-07-03 03:23:362039}
2040
Michael Haggerty624cac32013-04-22 19:52:232041/*
Michael Haggerty65cf1022013-04-22 19:52:402042 * Call fn for each reference in the specified ref_cache for which the
Michael Haggerty624cac32013-04-22 19:52:232043 * refname begins with base. If trim is non-zero, then trim that many
2044 * characters off the beginning of each refname before passing the
2045 * refname to fn. flags can be DO_FOR_EACH_INCLUDE_BROKEN to include
2046 * broken references in the iteration. If fn ever returns a non-zero
2047 * value, stop the iteration and return that value; otherwise, return
2048 * 0.
2049 */
Michael Haggerty65cf1022013-04-22 19:52:402050static int do_for_each_ref(struct ref_cache *refs, const char *base,
2051 each_ref_fn fn, int trim, int flags, void *cb_data)
Michael Haggerty624cac32013-04-22 19:52:232052{
2053 struct ref_entry_cb data;
2054 data.base = base;
2055 data.trim = trim;
2056 data.flags = flags;
2057 data.fn = fn;
2058 data.cb_data = cb_data;
2059
Jeff King49672f22015-03-20 18:43:062060 if (ref_paranoia < 0)
2061 ref_paranoia = git_env_bool("GIT_REF_PARANOIA", 0);
2062 if (ref_paranoia)
2063 data.flags |= DO_FOR_EACH_INCLUDE_BROKEN;
2064
Michael Haggerty65cf1022013-04-22 19:52:402065 return do_for_each_entry(refs, base, do_one_ref, &data);
Michael Haggerty624cac32013-04-22 19:52:232066}
2067
Heiko Voigt0bad6112010-07-07 13:39:112068static int do_head_ref(const char *submodule, each_ref_fn fn, void *cb_data)
Linus Torvalds723c31f2005-07-05 18:31:322069{
Michael Haggerty2b2a5be2015-05-25 18:38:282070 struct object_id oid;
Junio C Hamano8da19772006-09-21 05:02:012071 int flag;
2072
Heiko Voigt0bad6112010-07-07 13:39:112073 if (submodule) {
Michael Haggerty2b2a5be2015-05-25 18:38:282074 if (resolve_gitlink_ref(submodule, "HEAD", oid.hash) == 0)
2075 return fn("HEAD", &oid, 0, cb_data);
Heiko Voigt0bad6112010-07-07 13:39:112076
2077 return 0;
2078 }
2079
Michael Haggerty2b2a5be2015-05-25 18:38:282080 if (!read_ref_full("HEAD", RESOLVE_REF_READING, oid.hash, &flag))
2081 return fn("HEAD", &oid, flag, cb_data);
Heiko Voigt0bad6112010-07-07 13:39:112082
Linus Torvalds2f34ba32005-07-05 22:45:002083 return 0;
Linus Torvalds723c31f2005-07-05 18:31:322084}
2085
Heiko Voigt0bad6112010-07-07 13:39:112086int head_ref(each_ref_fn fn, void *cb_data)
2087{
2088 return do_head_ref(NULL, fn, cb_data);
2089}
2090
Heiko Voigt9ef6aeb2010-07-07 13:39:122091int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
2092{
2093 return do_head_ref(submodule, fn, cb_data);
2094}
2095
Junio C Hamanocb5d7092006-09-21 04:47:422096int for_each_ref(each_ref_fn fn, void *cb_data)
Linus Torvalds8a65ff72005-07-03 03:23:362097{
Michael Haggerty9da31cb2013-04-22 19:52:412098 return do_for_each_ref(&ref_cache, "", fn, 0, 0, cb_data);
Seana62be772006-05-14 01:43:002099}
2100
Heiko Voigt9ef6aeb2010-07-07 13:39:122101int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
2102{
Michael Haggerty65cf1022013-04-22 19:52:402103 return do_for_each_ref(get_ref_cache(submodule), "", fn, 0, 0, cb_data);
Seana62be772006-05-14 01:43:002104}
2105
Christian Couder2a8177b2009-03-30 03:07:152106int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)
2107{
Michael Haggerty9da31cb2013-04-22 19:52:412108 return do_for_each_ref(&ref_cache, prefix, fn, strlen(prefix), 0, cb_data);
Christian Couder2a8177b2009-03-30 03:07:152109}
2110
Heiko Voigt9ef6aeb2010-07-07 13:39:122111int for_each_ref_in_submodule(const char *submodule, const char *prefix,
2112 each_ref_fn fn, void *cb_data)
2113{
Michael Haggerty65cf1022013-04-22 19:52:402114 return do_for_each_ref(get_ref_cache(submodule), prefix, fn, strlen(prefix), 0, cb_data);
Christian Couder2a8177b2009-03-30 03:07:152115}
2116
Junio C Hamanocb5d7092006-09-21 04:47:422117int for_each_tag_ref(each_ref_fn fn, void *cb_data)
Seana62be772006-05-14 01:43:002118{
Christian Couder2a8177b2009-03-30 03:07:152119 return for_each_ref_in("refs/tags/", fn, cb_data);
Seana62be772006-05-14 01:43:002120}
2121
Heiko Voigt9ef6aeb2010-07-07 13:39:122122int for_each_tag_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
2123{
2124 return for_each_ref_in_submodule(submodule, "refs/tags/", fn, cb_data);
2125}
2126
Junio C Hamanocb5d7092006-09-21 04:47:422127int for_each_branch_ref(each_ref_fn fn, void *cb_data)
Seana62be772006-05-14 01:43:002128{
Christian Couder2a8177b2009-03-30 03:07:152129 return for_each_ref_in("refs/heads/", fn, cb_data);
Seana62be772006-05-14 01:43:002130}
2131
Heiko Voigt9ef6aeb2010-07-07 13:39:122132int for_each_branch_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
2133{
2134 return for_each_ref_in_submodule(submodule, "refs/heads/", fn, cb_data);
2135}
2136
Junio C Hamanocb5d7092006-09-21 04:47:422137int for_each_remote_ref(each_ref_fn fn, void *cb_data)
Seana62be772006-05-14 01:43:002138{
Christian Couder2a8177b2009-03-30 03:07:152139 return for_each_ref_in("refs/remotes/", fn, cb_data);
Junio C Hamanof8948e22009-02-09 07:27:102140}
2141
Heiko Voigt9ef6aeb2010-07-07 13:39:122142int for_each_remote_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
2143{
2144 return for_each_ref_in_submodule(submodule, "refs/remotes/", fn, cb_data);
2145}
2146
Christian Couder29268702009-01-23 09:06:382147int for_each_replace_ref(each_ref_fn fn, void *cb_data)
2148{
Mike Hommey58d121b2015-06-11 21:34:592149 return do_for_each_ref(&ref_cache, git_replace_ref_base, fn,
2150 strlen(git_replace_ref_base), 0, cb_data);
Christian Couder29268702009-01-23 09:06:382151}
2152
Josh Tripletta1bea2c2011-07-05 17:54:442153int head_ref_namespaced(each_ref_fn fn, void *cb_data)
2154{
2155 struct strbuf buf = STRBUF_INIT;
2156 int ret = 0;
Michael Haggerty2b2a5be2015-05-25 18:38:282157 struct object_id oid;
Josh Tripletta1bea2c2011-07-05 17:54:442158 int flag;
2159
2160 strbuf_addf(&buf, "%sHEAD", get_git_namespace());
Michael Haggerty2b2a5be2015-05-25 18:38:282161 if (!read_ref_full(buf.buf, RESOLVE_REF_READING, oid.hash, &flag))
2162 ret = fn(buf.buf, &oid, flag, cb_data);
Josh Tripletta1bea2c2011-07-05 17:54:442163 strbuf_release(&buf);
2164
2165 return ret;
2166}
2167
2168int for_each_namespaced_ref(each_ref_fn fn, void *cb_data)
2169{
2170 struct strbuf buf = STRBUF_INIT;
2171 int ret;
2172 strbuf_addf(&buf, "%srefs/", get_git_namespace());
Michael Haggerty9da31cb2013-04-22 19:52:412173 ret = do_for_each_ref(&ref_cache, buf.buf, fn, 0, 0, cb_data);
Josh Tripletta1bea2c2011-07-05 17:54:442174 strbuf_release(&buf);
2175 return ret;
2176}
2177
Ilari Liusvaarab09fe972010-01-20 09:48:262178int for_each_glob_ref_in(each_ref_fn fn, const char *pattern,
2179 const char *prefix, void *cb_data)
Ilari Liusvaarad08bae72010-01-20 09:48:252180{
2181 struct strbuf real_pattern = STRBUF_INIT;
2182 struct ref_filter filter;
Ilari Liusvaarad08bae72010-01-20 09:48:252183 int ret;
2184
Christian Couder59556542013-11-30 20:55:402185 if (!prefix && !starts_with(pattern, "refs/"))
Ilari Liusvaarad08bae72010-01-20 09:48:252186 strbuf_addstr(&real_pattern, "refs/");
Ilari Liusvaarab09fe972010-01-20 09:48:262187 else if (prefix)
2188 strbuf_addstr(&real_pattern, prefix);
Ilari Liusvaarad08bae72010-01-20 09:48:252189 strbuf_addstr(&real_pattern, pattern);
2190
Thomas Rast894a9d32010-03-12 17:04:262191 if (!has_glob_specials(pattern)) {
Junio C Hamano9517e6b2010-02-04 05:23:182192 /* Append implied '/' '*' if not present. */
Ilari Liusvaarad08bae72010-01-20 09:48:252193 if (real_pattern.buf[real_pattern.len - 1] != '/')
2194 strbuf_addch(&real_pattern, '/');
2195 /* No need to check for '*', there is none. */
2196 strbuf_addch(&real_pattern, '*');
2197 }
2198
2199 filter.pattern = real_pattern.buf;
2200 filter.fn = fn;
2201 filter.cb_data = cb_data;
2202 ret = for_each_ref(filter_refs, &filter);
2203
2204 strbuf_release(&real_pattern);
2205 return ret;
2206}
2207
Ilari Liusvaarab09fe972010-01-20 09:48:262208int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data)
2209{
2210 return for_each_glob_ref_in(fn, pattern, NULL, cb_data);
2211}
2212
Junio C Hamanof8948e22009-02-09 07:27:102213int for_each_rawref(each_ref_fn fn, void *cb_data)
2214{
Michael Haggerty9da31cb2013-04-22 19:52:412215 return do_for_each_ref(&ref_cache, "", fn, 0,
Junio C Hamanof8948e22009-02-09 07:27:102216 DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
Linus Torvalds8a65ff72005-07-03 03:23:362217}
2218
Felipe Contreras4577e482009-05-13 21:22:042219const char *prettify_refname(const char *name)
Daniel Barkalowa9c37a72009-03-09 01:06:052220{
Daniel Barkalowa9c37a72009-03-09 01:06:052221 return name + (
Christian Couder59556542013-11-30 20:55:402222 starts_with(name, "refs/heads/") ? 11 :
2223 starts_with(name, "refs/tags/") ? 10 :
2224 starts_with(name, "refs/remotes/") ? 13 :
Daniel Barkalowa9c37a72009-03-09 01:06:052225 0);
2226}
2227
Michael Haggerty54457fe2014-01-14 03:16:072228static const char *ref_rev_parse_rules[] = {
Steffen Prohaska79803322007-11-11 14:01:462229 "%.*s",
2230 "refs/%.*s",
2231 "refs/tags/%.*s",
2232 "refs/heads/%.*s",
2233 "refs/remotes/%.*s",
2234 "refs/remotes/%.*s/HEAD",
2235 NULL
2236};
2237
Michael Haggerty54457fe2014-01-14 03:16:072238int refname_match(const char *abbrev_name, const char *full_name)
Steffen Prohaska79803322007-11-11 14:01:462239{
2240 const char **p;
2241 const int abbrev_name_len = strlen(abbrev_name);
2242
Michael Haggerty54457fe2014-01-14 03:16:072243 for (p = ref_rev_parse_rules; *p; p++) {
Steffen Prohaska79803322007-11-11 14:01:462244 if (!strcmp(full_name, mkpath(*p, abbrev_name_len, abbrev_name))) {
2245 return 1;
2246 }
2247 }
2248
2249 return 0;
2250}
2251
Ronnie Sahlberg0b1e6542014-12-12 08:57:002252static void unlock_ref(struct ref_lock *lock)
2253{
2254 /* Do not free lock->lk -- atexit() still looks at them */
2255 if (lock->lk)
2256 rollback_lock_file(lock->lk);
2257 free(lock->ref_name);
2258 free(lock->orig_ref_name);
2259 free(lock);
2260}
2261
Michael Haggertya5e24992015-05-22 23:34:532262/*
2263 * Verify that the reference locked by lock has the value old_sha1.
2264 * Fail if the reference doesn't exist and mustexist is set. Return 0
Michael Haggerty33ffc172015-05-22 23:34:552265 * on success. On error, write an error message to err, set errno, and
2266 * return a negative value.
Michael Haggertya5e24992015-05-22 23:34:532267 */
2268static int verify_lock(struct ref_lock *lock,
Michael Haggerty33ffc172015-05-22 23:34:552269 const unsigned char *old_sha1, int mustexist,
2270 struct strbuf *err)
Daniel Barkalow95fc7512005-06-06 20:31:292271{
Michael Haggerty33ffc172015-05-22 23:34:552272 assert(err);
2273
Ronnie Sahlberg7695d112014-07-15 19:59:362274 if (read_ref_full(lock->ref_name,
2275 mustexist ? RESOLVE_REF_READING : 0,
Michael Haggerty5cb901a2015-05-25 18:39:222276 lock->old_oid.hash, NULL)) {
Ronnie Sahlberg835e3c92014-06-20 14:42:512277 int save_errno = errno;
Michael Haggerty000f0da2015-05-22 23:34:562278 strbuf_addf(err, "can't verify ref %s", lock->ref_name);
Ronnie Sahlberg835e3c92014-06-20 14:42:512279 errno = save_errno;
Michael Haggertya5e24992015-05-22 23:34:532280 return -1;
Shawn Pearce4bd18c42006-05-17 09:55:022281 }
Michael Haggerty5cb901a2015-05-25 18:39:222282 if (hashcmp(lock->old_oid.hash, old_sha1)) {
Michael Haggerty000f0da2015-05-22 23:34:562283 strbuf_addf(err, "ref %s is at %s but expected %s",
Michael Haggerty33ffc172015-05-22 23:34:552284 lock->ref_name,
Junio C Hamano829f03e2015-06-11 16:29:542285 sha1_to_hex(lock->old_oid.hash),
Michael Haggerty33ffc172015-05-22 23:34:552286 sha1_to_hex(old_sha1));
Ronnie Sahlberg835e3c92014-06-20 14:42:512287 errno = EBUSY;
Michael Haggertya5e24992015-05-22 23:34:532288 return -1;
Shawn Pearce4bd18c42006-05-17 09:55:022289 }
Michael Haggertya5e24992015-05-22 23:34:532290 return 0;
Shawn Pearce4bd18c42006-05-17 09:55:022291}
2292
Jeff King470e28d2015-08-10 09:37:272293static int remove_empty_directories(struct strbuf *path)
Junio C Hamanobc7127e2006-09-30 09:25:302294{
Jeff King470e28d2015-08-10 09:37:272295 /*
2296 * we want to create a file but there is a directory there;
Junio C Hamanobc7127e2006-09-30 09:25:302297 * if that is an empty directory (or a directory that contains
2298 * only empty directories), remove them.
2299 */
Jeff King470e28d2015-08-10 09:37:272300 return remove_dir_recursively(path, REMOVE_DIR_EMPTY_ONLY);
Junio C Hamanobc7127e2006-09-30 09:25:302301}
2302
Michael Haggerty19b68b12011-12-12 05:38:122303/*
Junio C Hamanoff74f7f2011-10-12 17:35:382304 * *string and *len will only be substituted, and *string returned (for
2305 * later free()ing) if the string passed in is a magic short-hand form
2306 * to name a branch.
2307 */
2308static char *substitute_branch_name(const char **string, int *len)
2309{
2310 struct strbuf buf = STRBUF_INIT;
Felipe Contrerascf99a762013-09-02 06:34:292311 int ret = interpret_branch_name(*string, *len, &buf);
Junio C Hamanoff74f7f2011-10-12 17:35:382312
2313 if (ret == *len) {
2314 size_t size;
2315 *string = strbuf_detach(&buf, &size);
2316 *len = size;
2317 return (char *)*string;
2318 }
2319
2320 return NULL;
2321}
2322
2323int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref)
2324{
2325 char *last_branch = substitute_branch_name(&str, &len);
2326 const char **p, *r;
2327 int refs_found = 0;
2328
2329 *ref = NULL;
2330 for (p = ref_rev_parse_rules; *p; p++) {
2331 char fullref[PATH_MAX];
2332 unsigned char sha1_from_ref[20];
2333 unsigned char *this_result;
2334 int flag;
2335
2336 this_result = refs_found ? sha1_from_ref : sha1;
2337 mksnpath(fullref, sizeof(fullref), *p, len, str);
Ronnie Sahlberg7695d112014-07-15 19:59:362338 r = resolve_ref_unsafe(fullref, RESOLVE_REF_READING,
2339 this_result, &flag);
Junio C Hamanoff74f7f2011-10-12 17:35:382340 if (r) {
2341 if (!refs_found++)
2342 *ref = xstrdup(r);
2343 if (!warn_ambiguous_refs)
2344 break;
Junio C Hamano55956352011-10-19 20:55:492345 } else if ((flag & REF_ISSYMREF) && strcmp(fullref, "HEAD")) {
Junio C Hamanoff74f7f2011-10-12 17:35:382346 warning("ignoring dangling symref %s.", fullref);
Junio C Hamano55956352011-10-19 20:55:492347 } else if ((flag & REF_ISBROKEN) && strchr(fullref, '/')) {
2348 warning("ignoring broken ref %s.", fullref);
2349 }
Junio C Hamanoff74f7f2011-10-12 17:35:382350 }
2351 free(last_branch);
2352 return refs_found;
2353}
2354
2355int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
2356{
2357 char *last_branch = substitute_branch_name(&str, &len);
2358 const char **p;
2359 int logs_found = 0;
2360
2361 *log = NULL;
2362 for (p = ref_rev_parse_rules; *p; p++) {
Junio C Hamanoff74f7f2011-10-12 17:35:382363 unsigned char hash[20];
2364 char path[PATH_MAX];
2365 const char *ref, *it;
2366
2367 mksnpath(path, sizeof(path), *p, len, str);
Ronnie Sahlberg7695d112014-07-15 19:59:362368 ref = resolve_ref_unsafe(path, RESOLVE_REF_READING,
2369 hash, NULL);
Junio C Hamanoff74f7f2011-10-12 17:35:382370 if (!ref)
2371 continue;
Ronnie Sahlberg4da58832014-05-06 22:45:522372 if (reflog_exists(path))
Junio C Hamanoff74f7f2011-10-12 17:35:382373 it = path;
Ronnie Sahlberg4da58832014-05-06 22:45:522374 else if (strcmp(ref, path) && reflog_exists(ref))
Junio C Hamanoff74f7f2011-10-12 17:35:382375 it = ref;
2376 else
2377 continue;
2378 if (!logs_found++) {
2379 *log = xstrdup(it);
2380 hashcpy(sha1, hash);
2381 }
2382 if (!warn_ambiguous_refs)
2383 break;
2384 }
2385 free(last_branch);
2386 return logs_found;
2387}
2388
Ronnie Sahlberg88b680a2014-04-28 22:38:472389/*
Ronnie Sahlberg3c93c842014-10-02 14:59:022390 * Locks a ref returning the lock on success and NULL on failure.
Ronnie Sahlberg88b680a2014-04-28 22:38:472391 * On failure errno is set to something meaningful.
2392 */
Michael Haggertydfefa932011-12-12 05:38:092393static struct ref_lock *lock_ref_sha1_basic(const char *refname,
2394 const unsigned char *old_sha1,
Michael Haggertye9111042015-05-11 15:25:122395 const struct string_list *extras,
Ronnie Sahlberg5fe7d822014-05-01 18:16:072396 const struct string_list *skip,
Michael Haggerty4a32b2e2015-05-11 15:25:152397 unsigned int flags, int *type_p,
2398 struct strbuf *err)
Shawn Pearce4bd18c42006-05-17 09:55:022399{
Jeff King5f8ef5b2015-08-10 09:37:122400 struct strbuf ref_file = STRBUF_INIT;
2401 struct strbuf orig_ref_file = STRBUF_INIT;
Michael Haggertydfefa932011-12-12 05:38:092402 const char *orig_refname = refname;
Shawn Pearce4bd18c42006-05-17 09:55:022403 struct ref_lock *lock;
Junio C Hamano5cc3cef2006-09-30 21:14:312404 int last_errno = 0;
Junio C Hamanoacd3b9e2008-10-17 22:44:392405 int type, lflags;
Junio C Hamano4431fcc2006-09-27 08:09:182406 int mustexist = (old_sha1 && !is_null_sha1(old_sha1));
Ronnie Sahlberg7695d112014-07-15 19:59:362407 int resolve_flags = 0;
Michael Haggertyc4c61c72014-01-18 22:48:542408 int attempts_remaining = 3;
Shawn Pearce4bd18c42006-05-17 09:55:022409
Michael Haggerty4a32b2e2015-05-11 15:25:152410 assert(err);
Shawn Pearce4bd18c42006-05-17 09:55:022411
2412 lock = xcalloc(1, sizeof(struct ref_lock));
Shawn Pearce4bd18c42006-05-17 09:55:022413
Ronnie Sahlberg7695d112014-07-15 19:59:362414 if (mustexist)
2415 resolve_flags |= RESOLVE_REF_READING;
Ronnie Sahlbergd0f810f2014-09-03 18:45:432416 if (flags & REF_DELETING) {
2417 resolve_flags |= RESOLVE_REF_ALLOW_BAD_NAME;
2418 if (flags & REF_NODEREF)
2419 resolve_flags |= RESOLVE_REF_NO_RECURSE;
2420 }
Ronnie Sahlberg7695d112014-07-15 19:59:362421
2422 refname = resolve_ref_unsafe(refname, resolve_flags,
Michael Haggerty5cb901a2015-05-25 18:39:222423 lock->old_oid.hash, &type);
Michael Haggertydfefa932011-12-12 05:38:092424 if (!refname && errno == EISDIR) {
Jeff King5f8ef5b2015-08-10 09:37:122425 /*
2426 * we are trying to lock foo but we used to
Junio C Hamanobc7127e2006-09-30 09:25:302427 * have foo/bar which now does not exist;
2428 * it is normal for the empty directory 'foo'
2429 * to remain.
2430 */
Jeff King5f8ef5b2015-08-10 09:37:122431 strbuf_git_path(&orig_ref_file, "%s", orig_refname);
Jeff King470e28d2015-08-10 09:37:272432 if (remove_empty_directories(&orig_ref_file)) {
Junio C Hamano5cc3cef2006-09-30 21:14:312433 last_errno = errno;
Michael Haggerty5b2d8d62015-05-11 15:25:162434 if (!verify_refname_available(orig_refname, extras, skip,
2435 get_loose_refs(&ref_cache), err))
2436 strbuf_addf(err, "there are still refs under '%s'",
2437 orig_refname);
Junio C Hamano5cc3cef2006-09-30 21:14:312438 goto error_return;
2439 }
Ronnie Sahlberg7695d112014-07-15 19:59:362440 refname = resolve_ref_unsafe(orig_refname, resolve_flags,
Michael Haggerty5cb901a2015-05-25 18:39:222441 lock->old_oid.hash, &type);
Junio C Hamanobc7127e2006-09-30 09:25:302442 }
Sven Verdoolaege68db31c2007-05-09 10:33:202443 if (type_p)
2444 *type_p = type;
Michael Haggertydfefa932011-12-12 05:38:092445 if (!refname) {
Junio C Hamano5cc3cef2006-09-30 21:14:312446 last_errno = errno;
Michael Haggerty5b2d8d62015-05-11 15:25:162447 if (last_errno != ENOTDIR ||
2448 !verify_refname_available(orig_refname, extras, skip,
2449 get_loose_refs(&ref_cache), err))
2450 strbuf_addf(err, "unable to resolve reference %s: %s",
2451 orig_refname, strerror(last_errno));
2452
Junio C Hamano5cc3cef2006-09-30 21:14:312453 goto error_return;
Shawn Pearce4bd18c42006-05-17 09:55:022454 }
Michael Haggerty074336e2015-03-02 09:29:532455 /*
2456 * If the ref did not exist and we are creating it, make sure
2457 * there is no existing packed ref whose name begins with our
2458 * refname, nor a packed ref whose name is a proper prefix of
2459 * our refname.
Lars Hjemlic976d412006-11-28 14:47:402460 */
Michael Haggerty5cb901a2015-05-25 18:39:222461 if (is_null_oid(&lock->old_oid) &&
Michael Haggerty1146f172015-05-11 15:25:142462 verify_refname_available(refname, extras, skip,
Michael Haggerty4a32b2e2015-05-11 15:25:152463 get_packed_refs(&ref_cache), err)) {
Jeff Kingf475e082009-05-25 10:37:152464 last_errno = ENOTDIR;
Lars Hjemlic976d412006-11-28 14:47:402465 goto error_return;
Jeff Kingf475e082009-05-25 10:37:152466 }
Junio C Hamano22a38442006-09-30 21:19:252467
Junio C Hamanoc33d5172006-06-06 20:54:142468 lock->lk = xcalloc(1, sizeof(struct lock_file));
Shawn Pearce4bd18c42006-05-17 09:55:022469
Michael Haggertye5c223e2014-01-18 22:48:552470 lflags = 0;
Junio C Hamanoacd3b9e2008-10-17 22:44:392471 if (flags & REF_NODEREF) {
Michael Haggertydfefa932011-12-12 05:38:092472 refname = orig_refname;
Michael Haggerty47ba4662014-10-01 10:28:372473 lflags |= LOCK_NO_DEREF;
Junio C Hamanoacd3b9e2008-10-17 22:44:392474 }
Michael Haggertydfefa932011-12-12 05:38:092475 lock->ref_name = xstrdup(refname);
2476 lock->orig_ref_name = xstrdup(orig_refname);
Jeff King5f8ef5b2015-08-10 09:37:122477 strbuf_git_path(&ref_file, "%s", refname);
Shawn Pearce4bd18c42006-05-17 09:55:022478
Michael Haggertyc4c61c72014-01-18 22:48:542479 retry:
Jeff King5f8ef5b2015-08-10 09:37:122480 switch (safe_create_leading_directories_const(ref_file.buf)) {
Michael Haggertyc4c61c72014-01-18 22:48:542481 case SCLD_OK:
2482 break; /* success */
2483 case SCLD_VANISHED:
2484 if (--attempts_remaining > 0)
2485 goto retry;
2486 /* fall through */
2487 default:
Junio C Hamano5cc3cef2006-09-30 21:14:312488 last_errno = errno;
Jeff King5f8ef5b2015-08-10 09:37:122489 strbuf_addf(err, "unable to create directory for %s",
2490 ref_file.buf);
Junio C Hamano5cc3cef2006-09-30 21:14:312491 goto error_return;
2492 }
Shawn Pearce4bd18c42006-05-17 09:55:022493
Jeff King5f8ef5b2015-08-10 09:37:122494 if (hold_lock_file_for_update(lock->lk, ref_file.buf, lflags) < 0) {
Ronnie Sahlberg06839512014-11-19 22:28:522495 last_errno = errno;
Michael Haggertye5c223e2014-01-18 22:48:552496 if (errno == ENOENT && --attempts_remaining > 0)
2497 /*
2498 * Maybe somebody just deleted one of the
2499 * directories leading to ref_file. Try
2500 * again:
2501 */
2502 goto retry;
Ronnie Sahlberg06839512014-11-19 22:28:522503 else {
Jeff King5f8ef5b2015-08-10 09:37:122504 unable_to_lock_message(ref_file.buf, errno, err);
Ronnie Sahlberg06839512014-11-19 22:28:522505 goto error_return;
2506 }
Michael Haggertye5c223e2014-01-18 22:48:552507 }
Michael Haggerty33ffc172015-05-22 23:34:552508 if (old_sha1 && verify_lock(lock, old_sha1, mustexist, err)) {
Michael Haggertyf41d6322015-05-22 23:34:542509 last_errno = errno;
2510 goto error_return;
2511 }
Jeff King5f8ef5b2015-08-10 09:37:122512 goto out;
Junio C Hamano5cc3cef2006-09-30 21:14:312513
2514 error_return:
2515 unlock_ref(lock);
Jeff King5f8ef5b2015-08-10 09:37:122516 lock = NULL;
2517
2518 out:
2519 strbuf_release(&ref_file);
2520 strbuf_release(&orig_ref_file);
Junio C Hamano5cc3cef2006-09-30 21:14:312521 errno = last_errno;
Jeff King5f8ef5b2015-08-10 09:37:122522 return lock;
Shawn Pearce4bd18c42006-05-17 09:55:022523}
2524
Michael Haggertyfec31372013-04-22 19:52:302525/*
2526 * Write an entry to the packed-refs file for the specified refname.
2527 * If peeled is non-NULL, write it as the entry's peeled value.
2528 */
Jeff King9540ce52014-09-10 10:03:522529static void write_packed_entry(FILE *fh, char *refname, unsigned char *sha1,
Michael Haggertyfec31372013-04-22 19:52:302530 unsigned char *peeled)
Michael Haggertyd66da472012-04-10 05:30:172531{
Jeff King9540ce52014-09-10 10:03:522532 fprintf_or_die(fh, "%s %s\n", sha1_to_hex(sha1), refname);
2533 if (peeled)
2534 fprintf_or_die(fh, "^%s\n", sha1_to_hex(peeled));
Michael Haggertyfec31372013-04-22 19:52:302535}
2536
Michael Haggerty7b40d392013-06-20 08:37:432537/*
2538 * An each_ref_entry_fn that writes the entry to a packed-refs file.
2539 */
2540static int write_packed_entry_fn(struct ref_entry *entry, void *cb_data)
2541{
Michael Haggerty7b40d392013-06-20 08:37:432542 enum peel_status peel_status = peel_entry(entry, 0);
2543
2544 if (peel_status != PEEL_PEELED && peel_status != PEEL_NON_TAG)
2545 error("internal error: %s is not a valid packed reference!",
2546 entry->name);
brian m. carlson83538472015-05-25 18:38:272547 write_packed_entry(cb_data, entry->name, entry->u.value.oid.hash,
Michael Haggerty7b40d392013-06-20 08:37:432548 peel_status == PEEL_PEELED ?
brian m. carlson83538472015-05-25 18:38:272549 entry->u.value.peeled.hash : NULL);
Michael Haggerty7b40d392013-06-20 08:37:432550 return 0;
2551}
2552
Michael Haggerty0a4b24f2015-06-22 14:03:022553/*
2554 * Lock the packed-refs file for writing. Flags is passed to
2555 * hold_lock_file_for_update(). Return 0 on success. On errors, set
2556 * errno appropriately and return a nonzero value.
2557 */
2558static int lock_packed_refs(int flags)
Michael Haggerty9f69d292013-06-20 08:37:462559{
Michael Haggertyf4ab4f32015-05-11 10:35:262560 static int timeout_configured = 0;
2561 static int timeout_value = 1000;
2562
Michael Haggerty9f69d292013-06-20 08:37:462563 struct packed_ref_cache *packed_ref_cache;
2564
Michael Haggertyf4ab4f32015-05-11 10:35:262565 if (!timeout_configured) {
2566 git_config_get_int("core.packedrefstimeout", &timeout_value);
2567 timeout_configured = 1;
2568 }
2569
2570 if (hold_lock_file_for_update_timeout(
2571 &packlock, git_path("packed-refs"),
2572 flags, timeout_value) < 0)
Michael Haggerty9f69d292013-06-20 08:37:462573 return -1;
Michael Haggerty5d478f52013-06-20 08:37:542574 /*
2575 * Get the current packed-refs while holding the lock. If the
2576 * packed-refs file has been modified since we last read it,
2577 * this will automatically invalidate the cache and re-read
2578 * the packed-refs file.
2579 */
Michael Haggerty9f69d292013-06-20 08:37:462580 packed_ref_cache = get_packed_ref_cache(&ref_cache);
2581 packed_ref_cache->lock = &packlock;
Michael Haggerty4f6b83e2013-06-20 08:37:492582 /* Increment the reference count to prevent it from being freed: */
2583 acquire_packed_ref_cache(packed_ref_cache);
Michael Haggerty9f69d292013-06-20 08:37:462584 return 0;
2585}
2586
Ronnie Sahlbergd3f66552014-06-20 14:42:532587/*
Michael Haggerty0a4b24f2015-06-22 14:03:022588 * Write the current version of the packed refs cache from memory to
2589 * disk. The packed-refs file must already be locked for writing (see
2590 * lock_packed_refs()). Return zero on success. On errors, set errno
2591 * and return a nonzero value
Ronnie Sahlbergd3f66552014-06-20 14:42:532592 */
Michael Haggerty0a4b24f2015-06-22 14:03:022593static int commit_packed_refs(void)
Michael Haggerty9f69d292013-06-20 08:37:462594{
2595 struct packed_ref_cache *packed_ref_cache =
2596 get_packed_ref_cache(&ref_cache);
2597 int error = 0;
Ronnie Sahlbergd3f66552014-06-20 14:42:532598 int save_errno = 0;
Jeff King9540ce52014-09-10 10:03:522599 FILE *out;
Michael Haggerty9f69d292013-06-20 08:37:462600
2601 if (!packed_ref_cache->lock)
2602 die("internal error: packed-refs not locked");
Michael Haggerty9f69d292013-06-20 08:37:462603
Michael Haggerty6e578a32014-10-01 11:14:492604 out = fdopen_lock_file(packed_ref_cache->lock, "w");
Jeff King9540ce52014-09-10 10:03:522605 if (!out)
2606 die_errno("unable to fdopen packed-refs descriptor");
2607
2608 fprintf_or_die(out, "%s", PACKED_REFS_HEADER);
Michael Haggerty9f69d292013-06-20 08:37:462609 do_for_each_entry_in_dir(get_packed_ref_dir(packed_ref_cache),
Jeff King9540ce52014-09-10 10:03:522610 0, write_packed_entry_fn, out);
Jeff King9540ce52014-09-10 10:03:522611
Ronnie Sahlbergd3f66552014-06-20 14:42:532612 if (commit_lock_file(packed_ref_cache->lock)) {
2613 save_errno = errno;
Michael Haggerty9f69d292013-06-20 08:37:462614 error = -1;
Ronnie Sahlbergd3f66552014-06-20 14:42:532615 }
Michael Haggerty9f69d292013-06-20 08:37:462616 packed_ref_cache->lock = NULL;
Michael Haggerty4f6b83e2013-06-20 08:37:492617 release_packed_ref_cache(packed_ref_cache);
Ronnie Sahlbergd3f66552014-06-20 14:42:532618 errno = save_errno;
Michael Haggerty9f69d292013-06-20 08:37:462619 return error;
2620}
2621
Michael Haggerty0a4b24f2015-06-22 14:03:022622/*
2623 * Rollback the lockfile for the packed-refs file, and discard the
2624 * in-memory packed reference cache. (The packed-refs file will be
2625 * read anew if it is needed again after this function is called.)
2626 */
2627static void rollback_packed_refs(void)
Michael Haggerty9f69d292013-06-20 08:37:462628{
2629 struct packed_ref_cache *packed_ref_cache =
2630 get_packed_ref_cache(&ref_cache);
2631
2632 if (!packed_ref_cache->lock)
2633 die("internal error: packed-refs not locked");
2634 rollback_lock_file(packed_ref_cache->lock);
2635 packed_ref_cache->lock = NULL;
Michael Haggerty4f6b83e2013-06-20 08:37:492636 release_packed_ref_cache(packed_ref_cache);
Michael Haggerty9f69d292013-06-20 08:37:462637 clear_packed_ref_cache(&ref_cache);
2638}
2639
Michael Haggerty32d462c2013-04-22 19:52:322640struct ref_to_prune {
2641 struct ref_to_prune *next;
2642 unsigned char sha1[20];
2643 char name[FLEX_ARRAY];
2644};
2645
2646struct pack_refs_cb_data {
2647 unsigned int flags;
Michael Haggerty267f9a82013-06-20 08:37:442648 struct ref_dir *packed_refs;
Michael Haggerty32d462c2013-04-22 19:52:322649 struct ref_to_prune *ref_to_prune;
Michael Haggerty32d462c2013-04-22 19:52:322650};
2651
Michael Haggerty267f9a82013-06-20 08:37:442652/*
2653 * An each_ref_entry_fn that is run over loose references only. If
2654 * the loose reference can be packed, add an entry in the packed ref
2655 * cache. If the reference should be pruned, also add it to
2656 * ref_to_prune in the pack_refs_cb_data.
2657 */
2658static int pack_if_possible_fn(struct ref_entry *entry, void *cb_data)
Michael Haggerty32d462c2013-04-22 19:52:322659{
2660 struct pack_refs_cb_data *cb = cb_data;
Michael Haggertyf85354b2013-04-22 19:52:372661 enum peel_status peel_status;
Michael Haggerty267f9a82013-06-20 08:37:442662 struct ref_entry *packed_entry;
Christian Couder59556542013-11-30 20:55:402663 int is_tag_ref = starts_with(entry->name, "refs/tags/");
Michael Haggerty32d462c2013-04-22 19:52:322664
Michael Haggerty267f9a82013-06-20 08:37:442665 /* ALWAYS pack tags */
2666 if (!(cb->flags & PACK_REFS_ALL) && !is_tag_ref)
Michael Haggerty32d462c2013-04-22 19:52:322667 return 0;
2668
Michael Haggertyb2a82262013-04-22 19:52:392669 /* Do not pack symbolic or broken refs: */
2670 if ((entry->flag & REF_ISSYMREF) || !ref_resolves_to_object(entry))
2671 return 0;
2672
Michael Haggerty267f9a82013-06-20 08:37:442673 /* Add a packed ref cache entry equivalent to the loose entry. */
Michael Haggertyf85354b2013-04-22 19:52:372674 peel_status = peel_entry(entry, 1);
Michael Haggerty0f299202013-04-22 19:52:382675 if (peel_status != PEEL_PEELED && peel_status != PEEL_NON_TAG)
Michael Haggertyf85354b2013-04-22 19:52:372676 die("internal error peeling reference %s (%s)",
brian m. carlson83538472015-05-25 18:38:272677 entry->name, oid_to_hex(&entry->u.value.oid));
Michael Haggerty267f9a82013-06-20 08:37:442678 packed_entry = find_ref(cb->packed_refs, entry->name);
2679 if (packed_entry) {
2680 /* Overwrite existing packed entry with info from loose entry */
2681 packed_entry->flag = REF_ISPACKED | REF_KNOWS_PEELED;
brian m. carlson83538472015-05-25 18:38:272682 oidcpy(&packed_entry->u.value.oid, &entry->u.value.oid);
Michael Haggerty267f9a82013-06-20 08:37:442683 } else {
brian m. carlson83538472015-05-25 18:38:272684 packed_entry = create_ref_entry(entry->name, entry->u.value.oid.hash,
Michael Haggerty267f9a82013-06-20 08:37:442685 REF_ISPACKED | REF_KNOWS_PEELED, 0);
2686 add_ref(cb->packed_refs, packed_entry);
2687 }
brian m. carlson83538472015-05-25 18:38:272688 oidcpy(&packed_entry->u.value.peeled, &entry->u.value.peeled);
Michael Haggerty32d462c2013-04-22 19:52:322689
Michael Haggerty267f9a82013-06-20 08:37:442690 /* Schedule the loose reference for pruning if requested. */
2691 if ((cb->flags & PACK_REFS_PRUNE)) {
Michael Haggerty12e77552013-04-22 19:52:352692 int namelen = strlen(entry->name) + 1;
Michael Haggerty32d462c2013-04-22 19:52:322693 struct ref_to_prune *n = xcalloc(1, sizeof(*n) + namelen);
brian m. carlson83538472015-05-25 18:38:272694 hashcpy(n->sha1, entry->u.value.oid.hash);
Michael Haggerty12e77552013-04-22 19:52:352695 strcpy(n->name, entry->name);
Michael Haggerty32d462c2013-04-22 19:52:322696 n->next = cb->ref_to_prune;
2697 cb->ref_to_prune = n;
2698 }
Michael Haggertyd66da472012-04-10 05:30:172699 return 0;
2700}
2701
Michael Haggerty32d462c2013-04-22 19:52:322702/*
2703 * Remove empty parents, but spare refs/ and immediate subdirs.
2704 * Note: munges *name.
2705 */
2706static void try_remove_empty_parents(char *name)
2707{
2708 char *p, *q;
2709 int i;
2710 p = name;
2711 for (i = 0; i < 2; i++) { /* refs/{heads,tags,...}/ */
2712 while (*p && *p != '/')
2713 p++;
2714 /* tolerate duplicate slashes; see check_refname_format() */
2715 while (*p == '/')
2716 p++;
2717 }
2718 for (q = p; *q; q++)
2719 ;
2720 while (1) {
2721 while (q > p && *q != '/')
2722 q--;
2723 while (q > p && *(q-1) == '/')
2724 q--;
2725 if (q == p)
2726 break;
2727 *q = '\0';
2728 if (rmdir(git_path("%s", name)))
2729 break;
2730 }
2731}
2732
2733/* make sure nobody touched the ref, and unlink */
2734static void prune_ref(struct ref_to_prune *r)
2735{
Ronnie Sahlberg029cdb42014-04-30 16:03:362736 struct ref_transaction *transaction;
2737 struct strbuf err = STRBUF_INIT;
Michael Haggerty32d462c2013-04-22 19:52:322738
Junio C Hamano88e7dff2014-09-11 17:33:332739 if (check_refname_format(r->name, 0))
Ronnie Sahlbergcba12022014-04-29 22:45:522740 return;
2741
Ronnie Sahlberg029cdb42014-04-30 16:03:362742 transaction = ref_transaction_begin(&err);
2743 if (!transaction ||
2744 ref_transaction_delete(transaction, r->name, r->sha1,
Michael Haggertyfb5a6bb2015-02-17 17:00:162745 REF_ISPRUNING, NULL, &err) ||
Ronnie Sahlbergdb7516a2014-04-30 19:22:422746 ref_transaction_commit(transaction, &err)) {
Ronnie Sahlberg029cdb42014-04-30 16:03:362747 ref_transaction_free(transaction);
2748 error("%s", err.buf);
2749 strbuf_release(&err);
2750 return;
Michael Haggerty32d462c2013-04-22 19:52:322751 }
Ronnie Sahlberg029cdb42014-04-30 16:03:362752 ref_transaction_free(transaction);
2753 strbuf_release(&err);
2754 try_remove_empty_parents(r->name);
Michael Haggerty32d462c2013-04-22 19:52:322755}
2756
2757static void prune_refs(struct ref_to_prune *r)
2758{
2759 while (r) {
2760 prune_ref(r);
2761 r = r->next;
2762 }
2763}
2764
Michael Haggerty32d462c2013-04-22 19:52:322765int pack_refs(unsigned int flags)
2766{
Michael Haggerty32d462c2013-04-22 19:52:322767 struct pack_refs_cb_data cbdata;
2768
2769 memset(&cbdata, 0, sizeof(cbdata));
2770 cbdata.flags = flags;
2771
Michael Haggerty9f69d292013-06-20 08:37:462772 lock_packed_refs(LOCK_DIE_ON_ERROR);
Michael Haggerty267f9a82013-06-20 08:37:442773 cbdata.packed_refs = get_packed_refs(&ref_cache);
Michael Haggerty32d462c2013-04-22 19:52:322774
Michael Haggerty267f9a82013-06-20 08:37:442775 do_for_each_entry_in_dir(get_loose_refs(&ref_cache), 0,
2776 pack_if_possible_fn, &cbdata);
Michael Haggerty32d462c2013-04-22 19:52:322777
Michael Haggerty9f69d292013-06-20 08:37:462778 if (commit_packed_refs())
Michael Haggerty32d462c2013-04-22 19:52:322779 die_errno("unable to overwrite old ref-pack file");
Michael Haggerty9f69d292013-06-20 08:37:462780
Michael Haggerty32d462c2013-04-22 19:52:322781 prune_refs(cbdata.ref_to_prune);
2782 return 0;
2783}
2784
Michael Haggerty79e4d8a2015-06-22 14:03:002785/*
2786 * Rewrite the packed-refs file, omitting any refs listed in
2787 * 'refnames'. On error, leave packed-refs unchanged, write an error
2788 * message to 'err', and return a nonzero value.
2789 *
2790 * The refs in 'refnames' needn't be sorted. `err` must not be NULL.
2791 */
2792static int repack_without_refs(struct string_list *refnames, struct strbuf *err)
Junio C Hamanoc0277d12006-09-30 22:02:002793{
Michael Haggerty7618fd82013-04-22 19:52:172794 struct ref_dir *packed;
Jeff Kingea56c4e2015-03-20 18:43:172795 struct string_list_item *refname;
Michael Haggerty4a45b2f2014-11-25 08:02:322796 int ret, needs_repacking = 0, removed = 0;
Michael Haggerty7618fd82013-04-22 19:52:172797
Jonathan Nieder5a603b02014-08-28 23:42:372798 assert(err);
2799
Brad King61cee0d2013-09-04 15:22:422800 /* Look for a packed ref */
Michael Haggerty4a45b2f2014-11-25 08:02:322801 for_each_string_list_item(refname, refnames) {
2802 if (get_packed_ref(refname->string)) {
2803 needs_repacking = 1;
Brad King61cee0d2013-09-04 15:22:422804 break;
Michael Haggerty4a45b2f2014-11-25 08:02:322805 }
2806 }
Brad King61cee0d2013-09-04 15:22:422807
2808 /* Avoid locking if we have nothing to do */
Michael Haggerty4a45b2f2014-11-25 08:02:322809 if (!needs_repacking)
Brad King61cee0d2013-09-04 15:22:422810 return 0; /* no refname exists in packed refs */
Michael Haggerty7618fd82013-04-22 19:52:172811
Michael Haggerty9f69d292013-06-20 08:37:462812 if (lock_packed_refs(0)) {
Jonathan Nieder5a603b02014-08-28 23:42:372813 unable_to_lock_message(git_path("packed-refs"), errno, err);
2814 return -1;
Miklos Vajna1b018fd2009-09-26 23:15:092815 }
Michael Haggerty9da31cb2013-04-22 19:52:412816 packed = get_packed_refs(&ref_cache);
Michael Haggerty7b40d392013-06-20 08:37:432817
Brad King61cee0d2013-09-04 15:22:422818 /* Remove refnames from the cache */
Michael Haggerty4a45b2f2014-11-25 08:02:322819 for_each_string_list_item(refname, refnames)
2820 if (remove_entry(packed, refname->string) != -1)
Brad King61cee0d2013-09-04 15:22:422821 removed = 1;
2822 if (!removed) {
Michael Haggerty506a7602013-04-22 19:52:272823 /*
Brad King61cee0d2013-09-04 15:22:422824 * All packed entries disappeared while we were
Michael Haggerty506a7602013-04-22 19:52:272825 * acquiring the lock.
2826 */
Michael Haggerty9f69d292013-06-20 08:37:462827 rollback_packed_refs();
Michael Haggerty506a7602013-04-22 19:52:272828 return 0;
2829 }
Michael Haggerty7b40d392013-06-20 08:37:432830
Brad King61cee0d2013-09-04 15:22:422831 /* Write what remains */
Ronnie Sahlberg60bca082014-06-20 14:42:492832 ret = commit_packed_refs();
Jonathan Nieder5a603b02014-08-28 23:42:372833 if (ret)
Ronnie Sahlberg60bca082014-06-20 14:42:492834 strbuf_addf(err, "unable to overwrite old ref-pack file: %s",
2835 strerror(errno));
2836 return ret;
Junio C Hamanoc0277d12006-09-30 22:02:002837}
2838
Ronnie Sahlbergdbdcac72014-05-15 15:25:232839static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err)
Brad King2ddb5d12013-09-04 15:22:412840{
Jonathan Nieder5a603b02014-08-28 23:42:372841 assert(err);
2842
Brad King2ddb5d12013-09-04 15:22:412843 if (!(flag & REF_ISPACKED) || flag & REF_ISSYMREF) {
Michael Haggerty91f1f192014-10-01 10:28:162844 /*
2845 * loose. The loose file name is the same as the
2846 * lockfile name, minus ".lock":
2847 */
Michael Haggertyec38b4e2014-10-01 10:28:392848 char *loose_filename = get_locked_file_path(lock->lk);
Ronnie Sahlbergdbdcac72014-05-15 15:25:232849 int res = unlink_or_msg(loose_filename, err);
Michael Haggerty91f1f192014-10-01 10:28:162850 free(loose_filename);
Ronnie Sahlbergdbdcac72014-05-15 15:25:232851 if (res)
Brad King2ddb5d12013-09-04 15:22:412852 return 1;
2853 }
2854 return 0;
2855}
2856
David Turner266b1822015-07-31 06:06:182857static int is_per_worktree_ref(const char *refname)
2858{
2859 return !strcmp(refname, "HEAD");
2860}
2861
2862static int is_pseudoref_syntax(const char *refname)
2863{
2864 const char *c;
2865
2866 for (c = refname; *c; c++) {
2867 if (!isupper(*c) && *c != '-' && *c != '_')
2868 return 0;
2869 }
2870
2871 return 1;
2872}
2873
2874enum ref_type ref_type(const char *refname)
2875{
2876 if (is_per_worktree_ref(refname))
2877 return REF_TYPE_PER_WORKTREE;
2878 if (is_pseudoref_syntax(refname))
2879 return REF_TYPE_PSEUDOREF;
2880 return REF_TYPE_NORMAL;
2881}
2882
David Turner74ec19d2015-07-31 06:06:192883static int write_pseudoref(const char *pseudoref, const unsigned char *sha1,
2884 const unsigned char *old_sha1, struct strbuf *err)
2885{
2886 const char *filename;
2887 int fd;
2888 static struct lock_file lock;
2889 struct strbuf buf = STRBUF_INIT;
2890 int ret = -1;
2891
2892 strbuf_addf(&buf, "%s\n", sha1_to_hex(sha1));
2893
2894 filename = git_path("%s", pseudoref);
2895 fd = hold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR);
2896 if (fd < 0) {
2897 strbuf_addf(err, "Could not open '%s' for writing: %s",
2898 filename, strerror(errno));
2899 return -1;
2900 }
2901
2902 if (old_sha1) {
2903 unsigned char actual_old_sha1[20];
David Turner2c3aed12015-07-15 22:05:282904
2905 if (read_ref(pseudoref, actual_old_sha1))
2906 die("could not read ref '%s'", pseudoref);
David Turner74ec19d2015-07-31 06:06:192907 if (hashcmp(actual_old_sha1, old_sha1)) {
2908 strbuf_addf(err, "Unexpected sha1 when writing %s", pseudoref);
2909 rollback_lock_file(&lock);
2910 goto done;
2911 }
2912 }
2913
2914 if (write_in_full(fd, buf.buf, buf.len) != buf.len) {
2915 strbuf_addf(err, "Could not write to '%s'", filename);
2916 rollback_lock_file(&lock);
2917 goto done;
2918 }
2919
2920 commit_lock_file(&lock);
2921 ret = 0;
2922done:
2923 strbuf_release(&buf);
2924 return ret;
2925}
2926
2927static int delete_pseudoref(const char *pseudoref, const unsigned char *old_sha1)
2928{
2929 static struct lock_file lock;
2930 const char *filename;
2931
2932 filename = git_path("%s", pseudoref);
2933
2934 if (old_sha1 && !is_null_sha1(old_sha1)) {
2935 int fd;
2936 unsigned char actual_old_sha1[20];
2937
2938 fd = hold_lock_file_for_update(&lock, filename,
2939 LOCK_DIE_ON_ERROR);
2940 if (fd < 0)
2941 die_errno(_("Could not open '%s' for writing"), filename);
David Turner2c3aed12015-07-15 22:05:282942 if (read_ref(pseudoref, actual_old_sha1))
2943 die("could not read ref '%s'", pseudoref);
David Turner74ec19d2015-07-31 06:06:192944 if (hashcmp(actual_old_sha1, old_sha1)) {
2945 warning("Unexpected sha1 when deleting %s", pseudoref);
2946 rollback_lock_file(&lock);
2947 return -1;
2948 }
2949
2950 unlink(filename);
2951 rollback_lock_file(&lock);
2952 } else {
2953 unlink(filename);
2954 }
2955
2956 return 0;
2957}
2958
Michael Haggertyfc1c2162015-06-22 14:02:522959int delete_ref(const char *refname, const unsigned char *old_sha1,
2960 unsigned int flags)
Junio C Hamanoc0277d12006-09-30 22:02:002961{
Ronnie Sahlberg7521cc42014-04-30 16:22:452962 struct ref_transaction *transaction;
2963 struct strbuf err = STRBUF_INIT;
Junio C Hamanoc0277d12006-09-30 22:02:002964
David Turner74ec19d2015-07-31 06:06:192965 if (ref_type(refname) == REF_TYPE_PSEUDOREF)
Junio C Hamano080cc642015-08-25 21:57:082966 return delete_pseudoref(refname, old_sha1);
David Turner74ec19d2015-07-31 06:06:192967
Ronnie Sahlberg7521cc42014-04-30 16:22:452968 transaction = ref_transaction_begin(&err);
2969 if (!transaction ||
Michael Haggertyfc67a082015-06-22 14:02:542970 ref_transaction_delete(transaction, refname, old_sha1,
Michael Haggertyfb5a6bb2015-02-17 17:00:162971 flags, NULL, &err) ||
Ronnie Sahlbergdb7516a2014-04-30 19:22:422972 ref_transaction_commit(transaction, &err)) {
Ronnie Sahlberg7521cc42014-04-30 16:22:452973 error("%s", err.buf);
2974 ref_transaction_free(transaction);
2975 strbuf_release(&err);
Junio C Hamanoc0277d12006-09-30 22:02:002976 return 1;
Ronnie Sahlberg7521cc42014-04-30 16:22:452977 }
2978 ref_transaction_free(transaction);
2979 strbuf_release(&err);
2980 return 0;
Shawn Pearce4bd18c42006-05-17 09:55:022981}
2982
Michael Haggerty98ffd5f2015-06-22 14:02:552983int delete_refs(struct string_list *refnames)
2984{
2985 struct strbuf err = STRBUF_INIT;
2986 int i, result = 0;
2987
Michael Haggerty7fa7dc82015-06-22 14:02:572988 if (!refnames->nr)
2989 return 0;
2990
2991 result = repack_without_refs(refnames, &err);
2992 if (result) {
2993 /*
2994 * If we failed to rewrite the packed-refs file, then
2995 * it is unsafe to try to remove loose refs, because
2996 * doing so might expose an obsolete packed value for
2997 * a reference that might even point at an object that
2998 * has been garbage collected.
2999 */
3000 if (refnames->nr == 1)
3001 error(_("could not delete reference %s: %s"),
3002 refnames->items[0].string, err.buf);
3003 else
3004 error(_("could not delete references: %s"), err.buf);
3005
3006 goto out;
3007 }
Michael Haggerty98ffd5f2015-06-22 14:02:553008
3009 for (i = 0; i < refnames->nr; i++) {
3010 const char *refname = refnames->items[i].string;
3011
3012 if (delete_ref(refname, NULL, 0))
Michael Haggerty5d978612015-06-22 14:02:563013 result |= error(_("could not remove reference %s"), refname);
Michael Haggerty98ffd5f2015-06-22 14:02:553014 }
3015
Michael Haggerty7fa7dc82015-06-22 14:02:573016out:
3017 strbuf_release(&err);
Michael Haggerty98ffd5f2015-06-22 14:02:553018 return result;
3019}
3020
Pierre Habouzit765c2252010-07-07 07:47:203021/*
3022 * People using contrib's git-new-workdir have .git/logs/refs ->
3023 * /some/other/path/.git/logs/refs, and that may live on another device.
3024 *
3025 * IOW, to avoid cross device rename errors, the temporary renamed log must
3026 * live into logs/refs.
3027 */
3028#define TMP_RENAMED_LOG "logs/refs/.tmp-renamed-log"
3029
Michael Haggertyfa59ae72014-01-18 22:48:583030static int rename_tmp_log(const char *newrefname)
3031{
Michael Haggertyf1e9e9a2014-01-18 22:49:003032 int attempts_remaining = 4;
Jeff Kingd6549f32015-08-10 09:36:533033 struct strbuf path = STRBUF_INIT;
3034 int ret = -1;
Michael Haggertyae4a2832014-01-18 22:48:593035
3036 retry:
Jeff Kingd6549f32015-08-10 09:36:533037 strbuf_reset(&path);
3038 strbuf_git_path(&path, "logs/%s", newrefname);
3039 switch (safe_create_leading_directories_const(path.buf)) {
Michael Haggerty08f555c2014-01-18 22:49:013040 case SCLD_OK:
3041 break; /* success */
3042 case SCLD_VANISHED:
3043 if (--attempts_remaining > 0)
3044 goto retry;
3045 /* fall through */
3046 default:
Michael Haggertyfa59ae72014-01-18 22:48:583047 error("unable to create directory for %s", newrefname);
Jeff Kingd6549f32015-08-10 09:36:533048 goto out;
Michael Haggertyfa59ae72014-01-18 22:48:583049 }
3050
Jeff Kingd6549f32015-08-10 09:36:533051 if (rename(git_path(TMP_RENAMED_LOG), path.buf)) {
Michael Haggertyf1e9e9a2014-01-18 22:49:003052 if ((errno==EISDIR || errno==ENOTDIR) && --attempts_remaining > 0) {
Michael Haggertyfa59ae72014-01-18 22:48:583053 /*
3054 * rename(a, b) when b is an existing
3055 * directory ought to result in ISDIR, but
3056 * Solaris 5.8 gives ENOTDIR. Sheesh.
3057 */
Jeff King470e28d2015-08-10 09:37:273058 if (remove_empty_directories(&path)) {
Michael Haggertyfa59ae72014-01-18 22:48:583059 error("Directory not empty: logs/%s", newrefname);
Jeff Kingd6549f32015-08-10 09:36:533060 goto out;
Michael Haggertyfa59ae72014-01-18 22:48:583061 }
3062 goto retry;
Michael Haggertyae4a2832014-01-18 22:48:593063 } else if (errno == ENOENT && --attempts_remaining > 0) {
3064 /*
3065 * Maybe another process just deleted one of
3066 * the directories in the path to newrefname.
3067 * Try again from the beginning.
3068 */
3069 goto retry;
Michael Haggertyfa59ae72014-01-18 22:48:583070 } else {
3071 error("unable to move logfile "TMP_RENAMED_LOG" to logs/%s: %s",
3072 newrefname, strerror(errno));
Jeff Kingd6549f32015-08-10 09:36:533073 goto out;
Michael Haggertyfa59ae72014-01-18 22:48:583074 }
3075 }
Jeff Kingd6549f32015-08-10 09:36:533076 ret = 0;
3077out:
3078 strbuf_release(&path);
3079 return ret;
Michael Haggertyfa59ae72014-01-18 22:48:583080}
3081
Ronnie Sahlberg5fe7d822014-05-01 18:16:073082static int rename_ref_available(const char *oldname, const char *newname)
3083{
3084 struct string_list skip = STRING_LIST_INIT_NODUP;
Michael Haggerty1146f172015-05-11 15:25:143085 struct strbuf err = STRBUF_INIT;
Ronnie Sahlberg5fe7d822014-05-01 18:16:073086 int ret;
3087
3088 string_list_insert(&skip, oldname);
Michael Haggerty5baf37d2015-05-11 15:25:133089 ret = !verify_refname_available(newname, NULL, &skip,
Michael Haggerty1146f172015-05-11 15:25:143090 get_packed_refs(&ref_cache), &err)
Michael Haggerty5baf37d2015-05-11 15:25:133091 && !verify_refname_available(newname, NULL, &skip,
Michael Haggerty1146f172015-05-11 15:25:143092 get_loose_refs(&ref_cache), &err);
3093 if (!ret)
3094 error("%s", err.buf);
3095
Ronnie Sahlberg5fe7d822014-05-01 18:16:073096 string_list_clear(&skip, 0);
Michael Haggerty1146f172015-05-11 15:25:143097 strbuf_release(&err);
Ronnie Sahlberg5fe7d822014-05-01 18:16:073098 return ret;
3099}
3100
David Turnera4c653d2015-07-21 21:04:503101static int write_ref_to_lockfile(struct ref_lock *lock,
3102 const unsigned char *sha1, struct strbuf *err);
Michael Haggertyba43b7f2015-05-09 15:20:393103static int commit_ref_update(struct ref_lock *lock,
David Turnera4c653d2015-07-21 21:04:503104 const unsigned char *sha1, const char *logmsg,
David Turner0f2a71d2015-07-21 21:04:543105 int flags, struct strbuf *err);
Ronnie Sahlbergaae383d2014-04-28 22:36:583106
Michael Haggertydfefa932011-12-12 05:38:093107int rename_ref(const char *oldrefname, const char *newrefname, const char *logmsg)
Lars Hjemlic976d412006-11-28 14:47:403108{
Lars Hjemlic976d412006-11-28 14:47:403109 unsigned char sha1[20], orig_sha1[20];
3110 int flag = 0, logmoved = 0;
3111 struct ref_lock *lock;
Lars Hjemlic976d412006-11-28 14:47:403112 struct stat loginfo;
Michael Haggertydfefa932011-12-12 05:38:093113 int log = !lstat(git_path("logs/%s", oldrefname), &loginfo);
Miklos Vajnaeca35a22008-10-26 02:33:563114 const char *symref = NULL;
Michael Haggerty4a32b2e2015-05-11 15:25:153115 struct strbuf err = STRBUF_INIT;
Lars Hjemlic976d412006-11-28 14:47:403116
Miklos Vajna450d4c02008-10-26 02:33:573117 if (log && S_ISLNK(loginfo.st_mode))
Michael Haggertydfefa932011-12-12 05:38:093118 return error("reflog for %s is a symlink", oldrefname);
Lars Hjemlic976d412006-11-28 14:47:403119
Ronnie Sahlberg7695d112014-07-15 19:59:363120 symref = resolve_ref_unsafe(oldrefname, RESOLVE_REF_READING,
3121 orig_sha1, &flag);
Miklos Vajnaeca35a22008-10-26 02:33:563122 if (flag & REF_ISSYMREF)
Miklos Vajnafa58186c2008-10-29 00:05:273123 return error("refname %s is a symbolic ref, renaming it is not supported",
Michael Haggertydfefa932011-12-12 05:38:093124 oldrefname);
Miklos Vajnaeca35a22008-10-26 02:33:563125 if (!symref)
Michael Haggertydfefa932011-12-12 05:38:093126 return error("refname %s not found", oldrefname);
Lars Hjemlic976d412006-11-28 14:47:403127
Ronnie Sahlberg5fe7d822014-05-01 18:16:073128 if (!rename_ref_available(oldrefname, newrefname))
Lars Hjemlic976d412006-11-28 14:47:403129 return 1;
3130
Michael Haggertydfefa932011-12-12 05:38:093131 if (log && rename(git_path("logs/%s", oldrefname), git_path(TMP_RENAMED_LOG)))
Pierre Habouzit765c2252010-07-07 07:47:203132 return error("unable to move logfile logs/%s to "TMP_RENAMED_LOG": %s",
Michael Haggertydfefa932011-12-12 05:38:093133 oldrefname, strerror(errno));
Lars Hjemlic976d412006-11-28 14:47:403134
Michael Haggertydfefa932011-12-12 05:38:093135 if (delete_ref(oldrefname, orig_sha1, REF_NODEREF)) {
3136 error("unable to delete old %s", oldrefname);
Lars Hjemlic976d412006-11-28 14:47:403137 goto rollback;
3138 }
3139
Ronnie Sahlberg7695d112014-07-15 19:59:363140 if (!read_ref_full(newrefname, RESOLVE_REF_READING, sha1, NULL) &&
Michael Haggertydfefa932011-12-12 05:38:093141 delete_ref(newrefname, sha1, REF_NODEREF)) {
Lars Hjemlic976d412006-11-28 14:47:403142 if (errno==EISDIR) {
Jeff King470e28d2015-08-10 09:37:273143 struct strbuf path = STRBUF_INIT;
3144 int result;
3145
3146 strbuf_git_path(&path, "%s", newrefname);
3147 result = remove_empty_directories(&path);
3148 strbuf_release(&path);
3149
3150 if (result) {
Michael Haggertydfefa932011-12-12 05:38:093151 error("Directory not empty: %s", newrefname);
Lars Hjemlic976d412006-11-28 14:47:403152 goto rollback;
3153 }
3154 } else {
Michael Haggertydfefa932011-12-12 05:38:093155 error("unable to delete existing %s", newrefname);
Lars Hjemlic976d412006-11-28 14:47:403156 goto rollback;
3157 }
3158 }
3159
Michael Haggertyfa59ae72014-01-18 22:48:583160 if (log && rename_tmp_log(newrefname))
Lars Hjemlic976d412006-11-28 14:47:403161 goto rollback;
Lars Hjemlic976d412006-11-28 14:47:403162
Lars Hjemlic976d412006-11-28 14:47:403163 logmoved = log;
3164
Michael Haggerty4a32b2e2015-05-11 15:25:153165 lock = lock_ref_sha1_basic(newrefname, NULL, NULL, NULL, 0, NULL, &err);
Lars Hjemlic976d412006-11-28 14:47:403166 if (!lock) {
Michael Haggertyabeef9c2015-05-11 15:25:173167 error("unable to rename '%s' to '%s': %s", oldrefname, newrefname, err.buf);
Michael Haggerty4a32b2e2015-05-11 15:25:153168 strbuf_release(&err);
Lars Hjemlic976d412006-11-28 14:47:403169 goto rollback;
3170 }
Michael Haggerty5cb901a2015-05-25 18:39:223171 hashcpy(lock->old_oid.hash, orig_sha1);
Michael Haggertyba43b7f2015-05-09 15:20:393172
David Turnera4c653d2015-07-21 21:04:503173 if (write_ref_to_lockfile(lock, orig_sha1, &err) ||
David Turner0f2a71d2015-07-21 21:04:543174 commit_ref_update(lock, orig_sha1, logmsg, 0, &err)) {
David Turnera4c653d2015-07-21 21:04:503175 error("unable to write current sha1 into %s: %s", newrefname, err.buf);
3176 strbuf_release(&err);
Lars Hjemlic976d412006-11-28 14:47:403177 goto rollback;
3178 }
3179
3180 return 0;
3181
3182 rollback:
Michael Haggerty4a32b2e2015-05-11 15:25:153183 lock = lock_ref_sha1_basic(oldrefname, NULL, NULL, NULL, 0, NULL, &err);
Lars Hjemlic976d412006-11-28 14:47:403184 if (!lock) {
Michael Haggertyabeef9c2015-05-11 15:25:173185 error("unable to lock %s for rollback: %s", oldrefname, err.buf);
Michael Haggerty4a32b2e2015-05-11 15:25:153186 strbuf_release(&err);
Lars Hjemlic976d412006-11-28 14:47:403187 goto rollbacklog;
3188 }
3189
Lars Hjemlic976d412006-11-28 14:47:403190 flag = log_all_ref_updates;
3191 log_all_ref_updates = 0;
David Turnera4c653d2015-07-21 21:04:503192 if (write_ref_to_lockfile(lock, orig_sha1, &err) ||
David Turner0f2a71d2015-07-21 21:04:543193 commit_ref_update(lock, orig_sha1, NULL, 0, &err)) {
David Turnera4c653d2015-07-21 21:04:503194 error("unable to write current sha1 into %s: %s", oldrefname, err.buf);
3195 strbuf_release(&err);
3196 }
Lars Hjemlic976d412006-11-28 14:47:403197 log_all_ref_updates = flag;
3198
3199 rollbacklog:
Michael Haggertydfefa932011-12-12 05:38:093200 if (logmoved && rename(git_path("logs/%s", newrefname), git_path("logs/%s", oldrefname)))
Lars Hjemlic976d412006-11-28 14:47:403201 error("unable to restore logfile %s from %s: %s",
Michael Haggertydfefa932011-12-12 05:38:093202 oldrefname, newrefname, strerror(errno));
Lars Hjemlic976d412006-11-28 14:47:403203 if (!logmoved && log &&
Michael Haggertydfefa932011-12-12 05:38:093204 rename(git_path(TMP_RENAMED_LOG), git_path("logs/%s", oldrefname)))
Pierre Habouzit765c2252010-07-07 07:47:203205 error("unable to restore logfile %s from "TMP_RENAMED_LOG": %s",
Michael Haggertydfefa932011-12-12 05:38:093206 oldrefname, strerror(errno));
Lars Hjemlic976d412006-11-28 14:47:403207
3208 return 1;
3209}
3210
Ronnie Sahlberg0b1e6542014-12-12 08:57:003211static int close_ref(struct ref_lock *lock)
Brandon Caseyb5313942008-01-16 19:14:303212{
3213 if (close_lock_file(lock->lk))
3214 return -1;
Brandon Caseyb5313942008-01-16 19:14:303215 return 0;
3216}
3217
Ronnie Sahlberg0b1e6542014-12-12 08:57:003218static int commit_ref(struct ref_lock *lock)
Brandon Caseyb5313942008-01-16 19:14:303219{
3220 if (commit_lock_file(lock->lk))
3221 return -1;
Brandon Caseyb5313942008-01-16 19:14:303222 return 0;
3223}
3224
Junio C Hamano0ec29a42007-07-29 00:17:173225/*
3226 * copy the reflog message msg to buf, which has been allocated sufficiently
3227 * large, while cleaning up the whitespaces. Especially, convert LF to space,
3228 * because reflog file is one line per entry.
3229 */
3230static int copy_msg(char *buf, const char *msg)
3231{
3232 char *cp = buf;
3233 char c;
3234 int wasspace = 1;
3235
3236 *cp++ = '\t';
3237 while ((c = *msg++)) {
3238 if (wasspace && isspace(c))
3239 continue;
3240 wasspace = isspace(c);
3241 if (wasspace)
3242 c = ' ';
3243 *cp++ = c;
3244 }
3245 while (buf < cp && isspace(cp[-1]))
3246 cp--;
3247 *cp++ = '\n';
3248 return cp - buf;
3249}
3250
David Turner4e2bef52015-07-21 21:04:513251static int should_autocreate_reflog(const char *refname)
3252{
3253 if (!log_all_ref_updates)
3254 return 0;
3255 return starts_with(refname, "refs/heads/") ||
3256 starts_with(refname, "refs/remotes/") ||
3257 starts_with(refname, "refs/notes/") ||
3258 !strcmp(refname, "HEAD");
3259}
3260
David Turnerabd0cd32015-07-21 21:04:523261/*
3262 * Create a reflog for a ref. If force_create = 0, the reflog will
3263 * only be created for certain refs (those for which
3264 * should_autocreate_reflog returns non-zero. Otherwise, create it
3265 * regardless of the ref name. Fill in *err and return -1 on failure.
3266 */
Jeff King54b418f2015-08-10 12:26:383267static int log_ref_setup(const char *refname, struct strbuf *logfile, struct strbuf *err, int force_create)
Erick Mattos859c3012010-05-22 00:28:363268{
3269 int logfd, oflags = O_APPEND | O_WRONLY;
Erick Mattos859c3012010-05-22 00:28:363270
Jeff King54b418f2015-08-10 12:26:383271 strbuf_git_path(logfile, "logs/%s", refname);
David Turnerabd0cd32015-07-21 21:04:523272 if (force_create || should_autocreate_reflog(refname)) {
Jeff King54b418f2015-08-10 12:26:383273 if (safe_create_leading_directories(logfile->buf) < 0) {
David Turnera4c653d2015-07-21 21:04:503274 strbuf_addf(err, "unable to create directory for %s: "
Jeff King54b418f2015-08-10 12:26:383275 "%s", logfile->buf, strerror(errno));
Ronnie Sahlbergbd3b02d2014-06-20 14:42:503276 return -1;
3277 }
Erick Mattos859c3012010-05-22 00:28:363278 oflags |= O_CREAT;
3279 }
3280
Jeff King54b418f2015-08-10 12:26:383281 logfd = open(logfile->buf, oflags, 0666);
Erick Mattos859c3012010-05-22 00:28:363282 if (logfd < 0) {
Jeff King92338872014-11-04 13:24:533283 if (!(oflags & O_CREAT) && (errno == ENOENT || errno == EISDIR))
Erick Mattos859c3012010-05-22 00:28:363284 return 0;
3285
Jeff King92338872014-11-04 13:24:533286 if (errno == EISDIR) {
Thomas Rast157aaea2010-06-10 12:54:033287 if (remove_empty_directories(logfile)) {
David Turnera4c653d2015-07-21 21:04:503288 strbuf_addf(err, "There are still logs under "
Jeff King54b418f2015-08-10 12:26:383289 "'%s'", logfile->buf);
Ronnie Sahlbergbd3b02d2014-06-20 14:42:503290 return -1;
Erick Mattos859c3012010-05-22 00:28:363291 }
Jeff King54b418f2015-08-10 12:26:383292 logfd = open(logfile->buf, oflags, 0666);
Erick Mattos859c3012010-05-22 00:28:363293 }
3294
Ronnie Sahlbergbd3b02d2014-06-20 14:42:503295 if (logfd < 0) {
David Turnera4c653d2015-07-21 21:04:503296 strbuf_addf(err, "unable to append to %s: %s",
Jeff King54b418f2015-08-10 12:26:383297 logfile->buf, strerror(errno));
Ronnie Sahlbergbd3b02d2014-06-20 14:42:503298 return -1;
3299 }
Erick Mattos859c3012010-05-22 00:28:363300 }
3301
Jeff King54b418f2015-08-10 12:26:383302 adjust_shared_perm(logfile->buf);
Erick Mattos859c3012010-05-22 00:28:363303 close(logfd);
3304 return 0;
3305}
3306
David Turnerabd0cd32015-07-21 21:04:523307
3308int safe_create_reflog(const char *refname, int force_create, struct strbuf *err)
3309{
3310 int ret;
3311 struct strbuf sb = STRBUF_INIT;
3312
3313 ret = log_ref_setup(refname, &sb, err, force_create);
3314 strbuf_release(&sb);
3315 return ret;
3316}
3317
Ronnie Sahlberg2c6207a2014-12-12 08:56:423318static int log_ref_write_fd(int fd, const unsigned char *old_sha1,
3319 const unsigned char *new_sha1,
3320 const char *committer, const char *msg)
3321{
3322 int msglen, written;
3323 unsigned maxlen, len;
3324 char *logrec;
3325
3326 msglen = msg ? strlen(msg) : 0;
3327 maxlen = strlen(committer) + msglen + 100;
3328 logrec = xmalloc(maxlen);
3329 len = sprintf(logrec, "%s %s %s\n",
3330 sha1_to_hex(old_sha1),
3331 sha1_to_hex(new_sha1),
3332 committer);
3333 if (msglen)
3334 len += copy_msg(logrec + len - 1, msg) - 1;
3335
3336 written = len <= maxlen ? write_in_full(fd, logrec, len) : -1;
3337 free(logrec);
3338 if (written != len)
3339 return -1;
3340
3341 return 0;
3342}
3343
Nguyễn Thái Ngọc Duy1a83c242014-11-30 08:24:283344static int log_ref_write_1(const char *refname, const unsigned char *old_sha1,
3345 const unsigned char *new_sha1, const char *msg,
Jeff King54b418f2015-08-10 12:26:383346 struct strbuf *logfile, int flags,
David Turner0f2a71d2015-07-21 21:04:543347 struct strbuf *err)
Shawn Pearce6de08ae2006-05-17 09:55:403348{
Ronnie Sahlberg2c6207a2014-12-12 08:56:423349 int logfd, result, oflags = O_APPEND | O_WRONLY;
Shawn Pearce6de08ae2006-05-17 09:55:403350
Junio C Hamano510c5a82007-01-07 09:35:343351 if (log_all_ref_updates < 0)
Junio C Hamano7d1864c2007-01-07 10:00:283352 log_all_ref_updates = !is_bare_repository();
Junio C Hamano510c5a82007-01-07 09:35:343353
Jeff King54b418f2015-08-10 12:26:383354 result = log_ref_setup(refname, logfile, err, flags & REF_FORCE_CREATE_REFLOG);
David Turnera4c653d2015-07-21 21:04:503355
Erick Mattos859c3012010-05-22 00:28:363356 if (result)
3357 return result;
Nicolas Pitre9a13f0b2007-01-26 22:26:053358
Jeff King54b418f2015-08-10 12:26:383359 logfd = open(logfile->buf, oflags);
Erick Mattos859c3012010-05-22 00:28:363360 if (logfd < 0)
3361 return 0;
Ronnie Sahlberg2c6207a2014-12-12 08:56:423362 result = log_ref_write_fd(logfd, old_sha1, new_sha1,
3363 git_committer_info(0), msg);
3364 if (result) {
Jeff King54b418f2015-08-10 12:26:383365 strbuf_addf(err, "unable to append to %s: %s", logfile->buf,
David Turnera4c653d2015-07-21 21:04:503366 strerror(errno));
Ronnie Sahlbergdc615de2014-06-20 14:42:553367 close(logfd);
Ronnie Sahlbergdc615de2014-06-20 14:42:553368 return -1;
3369 }
3370 if (close(logfd)) {
Jeff King54b418f2015-08-10 12:26:383371 strbuf_addf(err, "unable to append to %s: %s", logfile->buf,
David Turnera4c653d2015-07-21 21:04:503372 strerror(errno));
Ronnie Sahlbergdc615de2014-06-20 14:42:553373 return -1;
3374 }
Shawn Pearce6de08ae2006-05-17 09:55:403375 return 0;
3376}
3377
Nguyễn Thái Ngọc Duy1a83c242014-11-30 08:24:283378static int log_ref_write(const char *refname, const unsigned char *old_sha1,
David Turnera4c653d2015-07-21 21:04:503379 const unsigned char *new_sha1, const char *msg,
David Turner0f2a71d2015-07-21 21:04:543380 int flags, struct strbuf *err)
Nguyễn Thái Ngọc Duy1a83c242014-11-30 08:24:283381{
3382 struct strbuf sb = STRBUF_INIT;
David Turner0f2a71d2015-07-21 21:04:543383 int ret = log_ref_write_1(refname, old_sha1, new_sha1, msg, &sb, flags,
3384 err);
Nguyễn Thái Ngọc Duy1a83c242014-11-30 08:24:283385 strbuf_release(&sb);
3386 return ret;
3387}
3388
Ronnie Sahlberge7e0f262014-07-15 23:02:383389int is_branch(const char *refname)
Linus Torvaldsc3b0dec2008-01-15 23:50:173390{
Christian Couder59556542013-11-30 20:55:403391 return !strcmp(refname, "HEAD") || starts_with(refname, "refs/heads/");
Linus Torvaldsc3b0dec2008-01-15 23:50:173392}
3393
Ronnie Sahlbergaae383d2014-04-28 22:36:583394/*
Michael Haggertye6fd3c62015-04-24 11:35:453395 * Write sha1 into the open lockfile, then close the lockfile. On
David Turnera4c653d2015-07-21 21:04:503396 * errors, rollback the lockfile, fill in *err and
3397 * return -1.
Ronnie Sahlbergaae383d2014-04-28 22:36:583398 */
Michael Haggertye6fd3c62015-04-24 11:35:453399static int write_ref_to_lockfile(struct ref_lock *lock,
David Turnera4c653d2015-07-21 21:04:503400 const unsigned char *sha1, struct strbuf *err)
Shawn Pearce4bd18c42006-05-17 09:55:023401{
3402 static char term = '\n';
Linus Torvaldsc3b0dec2008-01-15 23:50:173403 struct object *o;
Michael Haggertyc99a4c22015-08-10 09:47:383404 int fd;
Shawn Pearce4bd18c42006-05-17 09:55:023405
Linus Torvaldsc3b0dec2008-01-15 23:50:173406 o = parse_object(sha1);
3407 if (!o) {
David Turnera4c653d2015-07-21 21:04:503408 strbuf_addf(err,
3409 "Trying to write ref %s with nonexistent object %s",
3410 lock->ref_name, sha1_to_hex(sha1));
Linus Torvaldsc3b0dec2008-01-15 23:50:173411 unlock_ref(lock);
Linus Torvaldsc3b0dec2008-01-15 23:50:173412 return -1;
3413 }
3414 if (o->type != OBJ_COMMIT && is_branch(lock->ref_name)) {
David Turnera4c653d2015-07-21 21:04:503415 strbuf_addf(err,
3416 "Trying to write non-commit object %s to branch %s",
3417 sha1_to_hex(sha1), lock->ref_name);
Linus Torvaldsc3b0dec2008-01-15 23:50:173418 unlock_ref(lock);
Linus Torvaldsc3b0dec2008-01-15 23:50:173419 return -1;
3420 }
Michael Haggertyc99a4c22015-08-10 09:47:383421 fd = get_lock_file_fd(lock->lk);
3422 if (write_in_full(fd, sha1_to_hex(sha1), 40) != 40 ||
3423 write_in_full(fd, &term, 1) != 1 ||
Ronnie Sahlbergdc615de2014-06-20 14:42:553424 close_ref(lock) < 0) {
David Turnera4c653d2015-07-21 21:04:503425 strbuf_addf(err,
Junio C Hamanodb86e612015-08-25 21:57:093426 "Couldn't write %s", get_lock_file_path(lock->lk));
Shawn Pearce4bd18c42006-05-17 09:55:023427 unlock_ref(lock);
Shawn Pearce4bd18c42006-05-17 09:55:023428 return -1;
3429 }
Michael Haggertye6fd3c62015-04-24 11:35:453430 return 0;
3431}
3432
3433/*
Michael Haggertyad4cd6c2015-05-09 15:18:363434 * Commit a change to a loose reference that has already been written
3435 * to the loose reference lockfile. Also update the reflogs if
3436 * necessary, using the specified lockmsg (which can be NULL).
Michael Haggertye6fd3c62015-04-24 11:35:453437 */
Michael Haggertyad4cd6c2015-05-09 15:18:363438static int commit_ref_update(struct ref_lock *lock,
David Turnera4c653d2015-07-21 21:04:503439 const unsigned char *sha1, const char *logmsg,
David Turner0f2a71d2015-07-21 21:04:543440 int flags, struct strbuf *err)
Michael Haggertye6fd3c62015-04-24 11:35:453441{
Michael Haggerty9da31cb2013-04-22 19:52:413442 clear_loose_ref_cache(&ref_cache);
David Turner0f2a71d2015-07-21 21:04:543443 if (log_ref_write(lock->ref_name, lock->old_oid.hash, sha1, logmsg, flags, err) < 0 ||
Nicolas Pitrebd104db2007-01-26 22:26:073444 (strcmp(lock->ref_name, lock->orig_ref_name) &&
David Turner0f2a71d2015-07-21 21:04:543445 log_ref_write(lock->orig_ref_name, lock->old_oid.hash, sha1, logmsg, flags, err) < 0)) {
David Turnera4c653d2015-07-21 21:04:503446 char *old_msg = strbuf_detach(err, NULL);
3447 strbuf_addf(err, "Cannot update the ref '%s': %s",
3448 lock->ref_name, old_msg);
3449 free(old_msg);
Shawn Pearce6de08ae2006-05-17 09:55:403450 unlock_ref(lock);
3451 return -1;
3452 }
Nicolas Pitre605fac82007-03-21 21:11:443453 if (strcmp(lock->orig_ref_name, "HEAD") != 0) {
3454 /*
3455 * Special hack: If a branch is updated directly and HEAD
3456 * points to it (may happen on the remote side of a push
3457 * for example) then logically the HEAD reflog should be
3458 * updated too.
3459 * A generic solution implies reverse symref information,
3460 * but finding all symrefs pointing to the given branch
3461 * would be rather costly for this rare event (the direct
3462 * update of a branch) to be worth it. So let's cheat and
3463 * check with HEAD only which should cover 99% of all usage
3464 * scenarios (even 100% of the default ones).
3465 */
3466 unsigned char head_sha1[20];
3467 int head_flag;
3468 const char *head_ref;
Ronnie Sahlberg7695d112014-07-15 19:59:363469 head_ref = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
3470 head_sha1, &head_flag);
Nicolas Pitre605fac82007-03-21 21:11:443471 if (head_ref && (head_flag & REF_ISSYMREF) &&
David Turnera4c653d2015-07-21 21:04:503472 !strcmp(head_ref, lock->ref_name)) {
3473 struct strbuf log_err = STRBUF_INIT;
3474 if (log_ref_write("HEAD", lock->old_oid.hash, sha1,
David Turner0f2a71d2015-07-21 21:04:543475 logmsg, 0, &log_err)) {
David Turnera4c653d2015-07-21 21:04:503476 error("%s", log_err.buf);
3477 strbuf_release(&log_err);
3478 }
3479 }
Nicolas Pitre605fac82007-03-21 21:11:443480 }
Brandon Caseyb5313942008-01-16 19:14:303481 if (commit_ref(lock)) {
Linus Torvalds434cd0c2006-09-14 17:14:473482 error("Couldn't set %s", lock->ref_name);
Shawn Pearce4bd18c42006-05-17 09:55:023483 unlock_ref(lock);
3484 return -1;
3485 }
David Turnera4c653d2015-07-21 21:04:503486
Shawn Pearce4bd18c42006-05-17 09:55:023487 unlock_ref(lock);
3488 return 0;
Daniel Barkalow95fc7512005-06-06 20:31:293489}
Shawn Pearced556fae2006-05-17 09:56:093490
Nicolas Pitre8b5157e2007-01-26 22:26:103491int create_symref(const char *ref_target, const char *refs_heads_master,
3492 const char *logmsg)
Nicolas Pitre41b625b2007-01-26 22:26:093493{
Jeff Kinge3cf2302015-08-10 09:35:383494 char *lockpath = NULL;
Nicolas Pitre41b625b2007-01-26 22:26:093495 char ref[1000];
3496 int fd, len, written;
Alex Riesena4f34cb2008-10-27 10:22:093497 char *git_HEAD = git_pathdup("%s", ref_target);
Nicolas Pitre8b5157e2007-01-26 22:26:103498 unsigned char old_sha1[20], new_sha1[20];
David Turnera4c653d2015-07-21 21:04:503499 struct strbuf err = STRBUF_INIT;
Nicolas Pitre8b5157e2007-01-26 22:26:103500
3501 if (logmsg && read_ref(ref_target, old_sha1))
3502 hashclr(old_sha1);
Nicolas Pitre41b625b2007-01-26 22:26:093503
Junio C Hamanod48744d2007-02-08 07:41:433504 if (safe_create_leading_directories(git_HEAD) < 0)
3505 return error("unable to create directory for %s", git_HEAD);
3506
Nicolas Pitre41b625b2007-01-26 22:26:093507#ifndef NO_SYMLINK_HEAD
3508 if (prefer_symlink_refs) {
3509 unlink(git_HEAD);
3510 if (!symlink(refs_heads_master, git_HEAD))
Nicolas Pitre8b5157e2007-01-26 22:26:103511 goto done;
Nicolas Pitre41b625b2007-01-26 22:26:093512 fprintf(stderr, "no symlink - falling back to symbolic ref\n");
3513 }
3514#endif
3515
3516 len = snprintf(ref, sizeof(ref), "ref: %s\n", refs_heads_master);
3517 if (sizeof(ref) <= len) {
3518 error("refname too long: %s", refs_heads_master);
Junio C Hamano47fc52e2007-01-27 01:49:003519 goto error_free_return;
Nicolas Pitre41b625b2007-01-26 22:26:093520 }
Jeff Kinge3cf2302015-08-10 09:35:383521 lockpath = mkpathdup("%s.lock", git_HEAD);
Nicolas Pitre41b625b2007-01-26 22:26:093522 fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666);
3523 if (fd < 0) {
3524 error("Unable to open %s for writing", lockpath);
Junio C Hamano47fc52e2007-01-27 01:49:003525 goto error_free_return;
Nicolas Pitre41b625b2007-01-26 22:26:093526 }
3527 written = write_in_full(fd, ref, len);
Jim Meyering91c8d592007-06-24 19:20:413528 if (close(fd) != 0 || written != len) {
Nicolas Pitre41b625b2007-01-26 22:26:093529 error("Unable to write to %s", lockpath);
Junio C Hamano47fc52e2007-01-27 01:49:003530 goto error_unlink_return;
Nicolas Pitre41b625b2007-01-26 22:26:093531 }
3532 if (rename(lockpath, git_HEAD) < 0) {
Nicolas Pitre41b625b2007-01-26 22:26:093533 error("Unable to create %s", git_HEAD);
Junio C Hamano47fc52e2007-01-27 01:49:003534 goto error_unlink_return;
Nicolas Pitre41b625b2007-01-26 22:26:093535 }
3536 if (adjust_shared_perm(git_HEAD)) {
Nicolas Pitre41b625b2007-01-26 22:26:093537 error("Unable to fix permissions on %s", lockpath);
Junio C Hamano47fc52e2007-01-27 01:49:003538 error_unlink_return:
Alex Riesen691f1a22009-04-29 21:22:563539 unlink_or_warn(lockpath);
Junio C Hamano47fc52e2007-01-27 01:49:003540 error_free_return:
Jeff Kinge3cf2302015-08-10 09:35:383541 free(lockpath);
Junio C Hamano47fc52e2007-01-27 01:49:003542 free(git_HEAD);
3543 return -1;
Nicolas Pitre41b625b2007-01-26 22:26:093544 }
Jeff Kinge3cf2302015-08-10 09:35:383545 free(lockpath);
Nicolas Pitre8b5157e2007-01-26 22:26:103546
Ramsay Jonesee96d112007-03-03 18:28:463547#ifndef NO_SYMLINK_HEAD
Nicolas Pitre8b5157e2007-01-26 22:26:103548 done:
Ramsay Jonesee96d112007-03-03 18:28:463549#endif
David Turnera4c653d2015-07-21 21:04:503550 if (logmsg && !read_ref(refs_heads_master, new_sha1) &&
David Turner0f2a71d2015-07-21 21:04:543551 log_ref_write(ref_target, old_sha1, new_sha1, logmsg, 0, &err)) {
David Turnera4c653d2015-07-21 21:04:503552 error("%s", err.buf);
3553 strbuf_release(&err);
3554 }
Nicolas Pitre8b5157e2007-01-26 22:26:103555
Junio C Hamano47fc52e2007-01-27 01:49:003556 free(git_HEAD);
Nicolas Pitre41b625b2007-01-26 22:26:093557 return 0;
3558}
3559
Ronnie Sahlberg4207ed22014-06-03 16:09:593560struct read_ref_at_cb {
3561 const char *refname;
3562 unsigned long at_time;
3563 int cnt;
3564 int reccnt;
3565 unsigned char *sha1;
3566 int found_it;
3567
3568 unsigned char osha1[20];
3569 unsigned char nsha1[20];
3570 int tz;
3571 unsigned long date;
3572 char **msg;
3573 unsigned long *cutoff_time;
3574 int *cutoff_tz;
3575 int *cutoff_cnt;
3576};
3577
3578static int read_ref_at_ent(unsigned char *osha1, unsigned char *nsha1,
3579 const char *email, unsigned long timestamp, int tz,
3580 const char *message, void *cb_data)
Junio C Hamano16d7cc92007-01-19 09:19:053581{
Ronnie Sahlberg4207ed22014-06-03 16:09:593582 struct read_ref_at_cb *cb = cb_data;
3583
3584 cb->reccnt++;
3585 cb->tz = tz;
3586 cb->date = timestamp;
3587
3588 if (timestamp <= cb->at_time || cb->cnt == 0) {
3589 if (cb->msg)
3590 *cb->msg = xstrdup(message);
3591 if (cb->cutoff_time)
3592 *cb->cutoff_time = timestamp;
3593 if (cb->cutoff_tz)
3594 *cb->cutoff_tz = tz;
3595 if (cb->cutoff_cnt)
3596 *cb->cutoff_cnt = cb->reccnt - 1;
3597 /*
3598 * we have not yet updated cb->[n|o]sha1 so they still
3599 * hold the values for the previous record.
3600 */
3601 if (!is_null_sha1(cb->osha1)) {
3602 hashcpy(cb->sha1, nsha1);
3603 if (hashcmp(cb->osha1, nsha1))
3604 warning("Log for ref %s has gap after %s.",
Jeff Kinga5481a62015-06-25 16:55:023605 cb->refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822)));
Ronnie Sahlberg4207ed22014-06-03 16:09:593606 }
3607 else if (cb->date == cb->at_time)
3608 hashcpy(cb->sha1, nsha1);
3609 else if (hashcmp(nsha1, cb->sha1))
3610 warning("Log for ref %s unexpectedly ended on %s.",
3611 cb->refname, show_date(cb->date, cb->tz,
Jeff Kinga5481a62015-06-25 16:55:023612 DATE_MODE(RFC2822)));
Ronnie Sahlberg4207ed22014-06-03 16:09:593613 hashcpy(cb->osha1, osha1);
3614 hashcpy(cb->nsha1, nsha1);
3615 cb->found_it = 1;
3616 return 1;
3617 }
3618 hashcpy(cb->osha1, osha1);
3619 hashcpy(cb->nsha1, nsha1);
3620 if (cb->cnt > 0)
3621 cb->cnt--;
3622 return 0;
3623}
3624
3625static int read_ref_at_ent_oldest(unsigned char *osha1, unsigned char *nsha1,
3626 const char *email, unsigned long timestamp,
3627 int tz, const char *message, void *cb_data)
3628{
3629 struct read_ref_at_cb *cb = cb_data;
3630
3631 if (cb->msg)
3632 *cb->msg = xstrdup(message);
3633 if (cb->cutoff_time)
3634 *cb->cutoff_time = timestamp;
3635 if (cb->cutoff_tz)
3636 *cb->cutoff_tz = tz;
3637 if (cb->cutoff_cnt)
3638 *cb->cutoff_cnt = cb->reccnt;
3639 hashcpy(cb->sha1, osha1);
3640 if (is_null_sha1(cb->sha1))
3641 hashcpy(cb->sha1, nsha1);
3642 /* We just want the first entry */
3643 return 1;
Junio C Hamano16d7cc92007-01-19 09:19:053644}
3645
David Aguilarc41a87d2014-09-19 03:45:373646int read_ref_at(const char *refname, unsigned int flags, unsigned long at_time, int cnt,
Michael Haggertydfefa932011-12-12 05:38:093647 unsigned char *sha1, char **msg,
3648 unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt)
Shawn Pearced556fae2006-05-17 09:56:093649{
Ronnie Sahlberg4207ed22014-06-03 16:09:593650 struct read_ref_at_cb cb;
Shawn Pearced556fae2006-05-17 09:56:093651
Ronnie Sahlberg4207ed22014-06-03 16:09:593652 memset(&cb, 0, sizeof(cb));
3653 cb.refname = refname;
3654 cb.at_time = at_time;
3655 cb.cnt = cnt;
3656 cb.msg = msg;
3657 cb.cutoff_time = cutoff_time;
3658 cb.cutoff_tz = cutoff_tz;
3659 cb.cutoff_cnt = cutoff_cnt;
3660 cb.sha1 = sha1;
Shawn Pearced556fae2006-05-17 09:56:093661
Ronnie Sahlberg4207ed22014-06-03 16:09:593662 for_each_reflog_ent_reverse(refname, read_ref_at_ent, &cb);
Shawn Pearced556fae2006-05-17 09:56:093663
David Aguilarc41a87d2014-09-19 03:45:373664 if (!cb.reccnt) {
3665 if (flags & GET_SHA1_QUIETLY)
3666 exit(128);
3667 else
3668 die("Log for %s is empty.", refname);
3669 }
Ronnie Sahlberg4207ed22014-06-03 16:09:593670 if (cb.found_it)
3671 return 0;
Junio C Hamano16d7cc92007-01-19 09:19:053672
Ronnie Sahlberg4207ed22014-06-03 16:09:593673 for_each_reflog_ent(refname, read_ref_at_ent_oldest, &cb);
3674
Junio C Hamano16d7cc92007-01-19 09:19:053675 return 1;
Shawn Pearced556fae2006-05-17 09:56:093676}
Junio C Hamano2ff81662006-12-18 09:18:163677
Ronnie Sahlberg4da58832014-05-06 22:45:523678int reflog_exists(const char *refname)
3679{
3680 struct stat st;
3681
3682 return !lstat(git_path("logs/%s", refname), &st) &&
3683 S_ISREG(st.st_mode);
3684}
3685
3686int delete_reflog(const char *refname)
3687{
3688 return remove_path(git_path("logs/%s", refname));
3689}
3690
Junio C Hamano9a7a1832013-03-08 18:36:433691static int show_one_reflog_ent(struct strbuf *sb, each_reflog_ent_fn fn, void *cb_data)
Junio C Hamano2ff81662006-12-18 09:18:163692{
Junio C Hamano9a7a1832013-03-08 18:36:433693 unsigned char osha1[20], nsha1[20];
3694 char *email_end, *message;
3695 unsigned long timestamp;
3696 int tz;
Junio C Hamano2ff81662006-12-18 09:18:163697
Junio C Hamano9a7a1832013-03-08 18:36:433698 /* old SP new SP name <email> SP time TAB msg LF */
3699 if (sb->len < 83 || sb->buf[sb->len - 1] != '\n' ||
3700 get_sha1_hex(sb->buf, osha1) || sb->buf[40] != ' ' ||
3701 get_sha1_hex(sb->buf + 41, nsha1) || sb->buf[81] != ' ' ||
3702 !(email_end = strchr(sb->buf + 82, '>')) ||
3703 email_end[1] != ' ' ||
3704 !(timestamp = strtoul(email_end + 2, &message, 10)) ||
3705 !message || message[0] != ' ' ||
3706 (message[1] != '+' && message[1] != '-') ||
3707 !isdigit(message[2]) || !isdigit(message[3]) ||
3708 !isdigit(message[4]) || !isdigit(message[5]))
3709 return 0; /* corrupt? */
3710 email_end[1] = '\0';
3711 tz = strtol(message + 1, NULL, 10);
3712 if (message[6] != '\t')
3713 message += 6;
3714 else
3715 message += 7;
3716 return fn(osha1, nsha1, sb->buf + 82, timestamp, tz, message, cb_data);
3717}
3718
Junio C Hamano98f85ff2013-03-08 21:27:373719static char *find_beginning_of_line(char *bob, char *scan)
3720{
3721 while (bob < scan && *(--scan) != '\n')
3722 ; /* keep scanning backwards */
3723 /*
3724 * Return either beginning of the buffer, or LF at the end of
3725 * the previous line.
3726 */
3727 return scan;
3728}
3729
3730int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn, void *cb_data)
3731{
3732 struct strbuf sb = STRBUF_INIT;
3733 FILE *logfp;
3734 long pos;
3735 int ret = 0, at_tail = 1;
3736
3737 logfp = fopen(git_path("logs/%s", refname), "r");
Junio C Hamano2ff81662006-12-18 09:18:163738 if (!logfp)
Johannes Schindelin883d60f2007-01-08 00:59:543739 return -1;
Junio C Hamano101d15e2009-01-20 06:18:293740
Junio C Hamano98f85ff2013-03-08 21:27:373741 /* Jump to the end */
3742 if (fseek(logfp, 0, SEEK_END) < 0)
3743 return error("cannot seek back reflog for %s: %s",
3744 refname, strerror(errno));
3745 pos = ftell(logfp);
3746 while (!ret && 0 < pos) {
3747 int cnt;
3748 size_t nread;
3749 char buf[BUFSIZ];
3750 char *endp, *scanp;
3751
3752 /* Fill next block from the end */
3753 cnt = (sizeof(buf) < pos) ? sizeof(buf) : pos;
3754 if (fseek(logfp, pos - cnt, SEEK_SET))
3755 return error("cannot seek back reflog for %s: %s",
3756 refname, strerror(errno));
3757 nread = fread(buf, cnt, 1, logfp);
John Keepinge4ca8192013-03-23 17:16:463758 if (nread != 1)
Junio C Hamano98f85ff2013-03-08 21:27:373759 return error("cannot read %d bytes from reflog for %s: %s",
3760 cnt, refname, strerror(errno));
3761 pos -= cnt;
3762
3763 scanp = endp = buf + cnt;
3764 if (at_tail && scanp[-1] == '\n')
3765 /* Looking at the final LF at the end of the file */
3766 scanp--;
3767 at_tail = 0;
3768
3769 while (buf < scanp) {
3770 /*
3771 * terminating LF of the previous line, or the beginning
3772 * of the buffer.
3773 */
3774 char *bp;
3775
3776 bp = find_beginning_of_line(buf, scanp);
3777
Jeff Kinge5e73ff2014-12-05 01:28:543778 if (*bp == '\n') {
Junio C Hamano98f85ff2013-03-08 21:27:373779 /*
Jeff Kinge5e73ff2014-12-05 01:28:543780 * The newline is the end of the previous line,
3781 * so we know we have complete line starting
3782 * at (bp + 1). Prefix it onto any prior data
3783 * we collected for the line and process it.
Junio C Hamano98f85ff2013-03-08 21:27:373784 */
3785 strbuf_splice(&sb, 0, 0, bp + 1, endp - (bp + 1));
3786 scanp = bp;
3787 endp = bp + 1;
Jeff Kinge5e73ff2014-12-05 01:28:543788 ret = show_one_reflog_ent(&sb, fn, cb_data);
3789 strbuf_reset(&sb);
3790 if (ret)
3791 break;
3792 } else if (!pos) {
3793 /*
3794 * We are at the start of the buffer, and the
3795 * start of the file; there is no previous
3796 * line, and we have everything for this one.
3797 * Process it, and we can end the loop.
3798 */
3799 strbuf_splice(&sb, 0, 0, buf, endp - buf);
3800 ret = show_one_reflog_ent(&sb, fn, cb_data);
3801 strbuf_reset(&sb);
Junio C Hamano98f85ff2013-03-08 21:27:373802 break;
Jeff Kinge5e73ff2014-12-05 01:28:543803 }
3804
3805 if (bp == buf) {
3806 /*
3807 * We are at the start of the buffer, and there
3808 * is more file to read backwards. Which means
3809 * we are in the middle of a line. Note that we
3810 * may get here even if *bp was a newline; that
3811 * just means we are at the exact end of the
3812 * previous line, rather than some spot in the
3813 * middle.
3814 *
3815 * Save away what we have to be combined with
3816 * the data from the next read.
3817 */
3818 strbuf_splice(&sb, 0, 0, buf, endp - buf);
3819 break;
3820 }
Brandon Casey9d33f7c2009-07-16 21:25:183821 }
Junio C Hamano101d15e2009-01-20 06:18:293822
Junio C Hamano2ff81662006-12-18 09:18:163823 }
Junio C Hamano98f85ff2013-03-08 21:27:373824 if (!ret && sb.len)
Jeff King69216bf2014-12-05 01:32:443825 die("BUG: reverse reflog parser had leftover data");
Junio C Hamano98f85ff2013-03-08 21:27:373826
Junio C Hamano2ff81662006-12-18 09:18:163827 fclose(logfp);
René Scharfe8ca78802010-03-13 17:37:503828 strbuf_release(&sb);
Junio C Hamano2266bf22007-01-19 07:25:543829 return ret;
Junio C Hamano2ff81662006-12-18 09:18:163830}
Junio C Hamanoe29cb532006-12-19 06:07:453831
Michael Haggertydfefa932011-12-12 05:38:093832int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn, void *cb_data)
Junio C Hamano101d15e2009-01-20 06:18:293833{
Junio C Hamano2ff81662006-12-18 09:18:163834 FILE *logfp;
René Scharfe8ca78802010-03-13 17:37:503835 struct strbuf sb = STRBUF_INIT;
Junio C Hamano2266bf22007-01-19 07:25:543836 int ret = 0;
Junio C Hamano101d15e2009-01-20 06:18:293837
Junio C Hamano7ae07c12013-03-08 18:45:253838 logfp = fopen(git_path("logs/%s", refname), "r");
Junio C Hamano2ff81662006-12-18 09:18:163839 if (!logfp)
Johannes Schindelin883d60f2007-01-08 00:59:543840 return -1;
Junio C Hamano101d15e2009-01-20 06:18:293841
Junio C Hamano9a7a1832013-03-08 18:36:433842 while (!ret && !strbuf_getwholeline(&sb, logfp, '\n'))
3843 ret = show_one_reflog_ent(&sb, fn, cb_data);
Junio C Hamano2ff81662006-12-18 09:18:163844 fclose(logfp);
René Scharfe8ca78802010-03-13 17:37:503845 strbuf_release(&sb);
Junio C Hamano2266bf22007-01-19 07:25:543846 return ret;
Junio C Hamano2ff81662006-12-18 09:18:163847}
Michael Haggerty989c0e52012-04-24 22:45:143848/*
3849 * Call fn for each reflog in the namespace indicated by name. name
3850 * must be empty or end with '/'. Name will be used as a scratch
3851 * space, but its contents will be restored before return.
3852 */
3853static int do_for_each_reflog(struct strbuf *name, each_ref_fn fn, void *cb_data)
Nicolas Pitreeb8381c2007-02-03 18:25:433854{
Michael Haggerty989c0e52012-04-24 22:45:143855 DIR *d = opendir(git_path("logs/%s", name->buf));
Junio C Hamanofcee5a12007-02-07 17:18:573856 int retval = 0;
Michael Haggerty93c603f2012-04-24 22:45:133857 struct dirent *de;
Michael Haggerty989c0e52012-04-24 22:45:143858 int oldlen = name->len;
Nicolas Pitreeb8381c2007-02-03 18:25:433859
Michael Haggerty93c603f2012-04-24 22:45:133860 if (!d)
Michael Haggerty989c0e52012-04-24 22:45:143861 return name->len ? errno : 0;
Nicolas Pitreeb8381c2007-02-03 18:25:433862
Michael Haggerty93c603f2012-04-24 22:45:133863 while ((de = readdir(d)) != NULL) {
3864 struct stat st;
Nicolas Pitreeb8381c2007-02-03 18:25:433865
Michael Haggerty93c603f2012-04-24 22:45:133866 if (de->d_name[0] == '.')
3867 continue;
Jeff King2975c772014-06-30 16:58:253868 if (ends_with(de->d_name, ".lock"))
Michael Haggerty93c603f2012-04-24 22:45:133869 continue;
Michael Haggerty989c0e52012-04-24 22:45:143870 strbuf_addstr(name, de->d_name);
3871 if (stat(git_path("logs/%s", name->buf), &st) < 0) {
3872 ; /* silently ignore */
Michael Haggerty93c603f2012-04-24 22:45:133873 } else {
Nicolas Pitreeb8381c2007-02-03 18:25:433874 if (S_ISDIR(st.st_mode)) {
Michael Haggerty989c0e52012-04-24 22:45:143875 strbuf_addch(name, '/');
3876 retval = do_for_each_reflog(name, fn, cb_data);
Nicolas Pitreeb8381c2007-02-03 18:25:433877 } else {
Michael Haggerty2b2a5be2015-05-25 18:38:283878 struct object_id oid;
3879
3880 if (read_ref_full(name->buf, 0, oid.hash, NULL))
Michael Haggerty989c0e52012-04-24 22:45:143881 retval = error("bad ref for %s", name->buf);
Nicolas Pitreeb8381c2007-02-03 18:25:433882 else
Michael Haggerty2b2a5be2015-05-25 18:38:283883 retval = fn(name->buf, &oid, 0, cb_data);
Nicolas Pitreeb8381c2007-02-03 18:25:433884 }
3885 if (retval)
3886 break;
3887 }
Michael Haggerty989c0e52012-04-24 22:45:143888 strbuf_setlen(name, oldlen);
Nicolas Pitreeb8381c2007-02-03 18:25:433889 }
Michael Haggerty93c603f2012-04-24 22:45:133890 closedir(d);
Nicolas Pitreeb8381c2007-02-03 18:25:433891 return retval;
3892}
3893
3894int for_each_reflog(each_ref_fn fn, void *cb_data)
3895{
Michael Haggerty989c0e52012-04-24 22:45:143896 int retval;
3897 struct strbuf name;
3898 strbuf_init(&name, PATH_MAX);
3899 retval = do_for_each_reflog(&name, fn, cb_data);
3900 strbuf_release(&name);
3901 return retval;
Nicolas Pitreeb8381c2007-02-03 18:25:433902}
Carlos Rica3d9f0372007-09-05 01:38:243903
Michael Haggertyb5c8ea22014-04-07 13:48:123904/**
Michael Haggerty8df4e512015-02-17 17:00:143905 * Information needed for a single ref update. Set new_sha1 to the new
3906 * value or to null_sha1 to delete the ref. To check the old value
3907 * while the ref is locked, set (flags & REF_HAVE_OLD) and set
3908 * old_sha1 to the old value, or to null_sha1 to ensure the ref does
3909 * not exist before update.
Michael Haggertyb5c8ea22014-04-07 13:48:123910 */
3911struct ref_update {
Michael Haggerty16180332015-02-17 17:00:213912 /*
3913 * If (flags & REF_HAVE_NEW), set the reference to this value:
3914 */
Michael Haggertyb5c8ea22014-04-07 13:48:123915 unsigned char new_sha1[20];
Michael Haggerty16180332015-02-17 17:00:213916 /*
3917 * If (flags & REF_HAVE_OLD), check that the reference
3918 * previously had this value:
3919 */
Michael Haggertyb5c8ea22014-04-07 13:48:123920 unsigned char old_sha1[20];
Michael Haggerty8df4e512015-02-17 17:00:143921 /*
Michael Haggerty16180332015-02-17 17:00:213922 * One or more of REF_HAVE_NEW, REF_HAVE_OLD, REF_NODEREF,
Michael Haggerty8df4e512015-02-17 17:00:143923 * REF_DELETING, and REF_ISPRUNING:
3924 */
3925 unsigned int flags;
Michael Haggerty81c960e2014-04-07 13:48:163926 struct ref_lock *lock;
Michael Haggerty84178db2014-04-07 13:48:173927 int type;
Ronnie Sahlbergdb7516a2014-04-30 19:22:423928 char *msg;
Michael Haggerty88615912014-04-07 13:48:143929 const char refname[FLEX_ARRAY];
Michael Haggertyb5c8ea22014-04-07 13:48:123930};
3931
Michael Haggertycaa40462014-04-07 13:48:103932/*
Ronnie Sahlberg2bdc7852014-04-29 19:06:193933 * Transaction states.
3934 * OPEN: The transaction is in a valid state and can accept new updates.
3935 * An OPEN transaction can be committed.
3936 * CLOSED: A closed transaction is no longer active and no other operations
3937 * than free can be used on it in this state.
3938 * A transaction can either become closed by successfully committing
3939 * an active transaction or if there is a failure while building
3940 * the transaction thus rendering it failed/inactive.
3941 */
3942enum ref_transaction_state {
3943 REF_TRANSACTION_OPEN = 0,
3944 REF_TRANSACTION_CLOSED = 1
3945};
3946
3947/*
Michael Haggertycaa40462014-04-07 13:48:103948 * Data structure for holding a reference transaction, which can
3949 * consist of checks and updates to multiple references, carried out
3950 * as atomically as possible. This structure is opaque to callers.
3951 */
3952struct ref_transaction {
3953 struct ref_update **updates;
3954 size_t alloc;
3955 size_t nr;
Ronnie Sahlberg2bdc7852014-04-29 19:06:193956 enum ref_transaction_state state;
Michael Haggertycaa40462014-04-07 13:48:103957};
3958
Ronnie Sahlberg93a644e2014-05-19 17:42:343959struct ref_transaction *ref_transaction_begin(struct strbuf *err)
Michael Haggertycaa40462014-04-07 13:48:103960{
Jonathan Nieder5a603b02014-08-28 23:42:373961 assert(err);
3962
Michael Haggertycaa40462014-04-07 13:48:103963 return xcalloc(1, sizeof(struct ref_transaction));
3964}
3965
Ronnie Sahlberg026bd1d2014-06-20 14:42:423966void ref_transaction_free(struct ref_transaction *transaction)
Michael Haggertycaa40462014-04-07 13:48:103967{
3968 int i;
3969
Ronnie Sahlberg1b072552014-06-20 14:42:453970 if (!transaction)
3971 return;
3972
Ronnie Sahlbergdb7516a2014-04-30 19:22:423973 for (i = 0; i < transaction->nr; i++) {
3974 free(transaction->updates[i]->msg);
Michael Haggerty88615912014-04-07 13:48:143975 free(transaction->updates[i]);
Ronnie Sahlbergdb7516a2014-04-30 19:22:423976 }
Michael Haggertycaa40462014-04-07 13:48:103977 free(transaction->updates);
3978 free(transaction);
3979}
3980
Michael Haggertycaa40462014-04-07 13:48:103981static struct ref_update *add_update(struct ref_transaction *transaction,
3982 const char *refname)
3983{
Michael Haggerty88615912014-04-07 13:48:143984 size_t len = strlen(refname);
3985 struct ref_update *update = xcalloc(1, sizeof(*update) + len + 1);
Michael Haggertycaa40462014-04-07 13:48:103986
Michael Haggerty88615912014-04-07 13:48:143987 strcpy((char *)update->refname, refname);
Michael Haggertycaa40462014-04-07 13:48:103988 ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc);
3989 transaction->updates[transaction->nr++] = update;
3990 return update;
3991}
3992
Ronnie Sahlberg8e348002014-06-20 14:43:003993int ref_transaction_update(struct ref_transaction *transaction,
3994 const char *refname,
3995 const unsigned char *new_sha1,
3996 const unsigned char *old_sha1,
Michael Haggerty1d147bd2015-02-17 17:00:153997 unsigned int flags, const char *msg,
Ronnie Sahlberg8e348002014-06-20 14:43:003998 struct strbuf *err)
Michael Haggertycaa40462014-04-07 13:48:103999{
Ronnie Sahlberg8e348002014-06-20 14:43:004000 struct ref_update *update;
Michael Haggertycaa40462014-04-07 13:48:104001
Jonathan Nieder5a603b02014-08-28 23:42:374002 assert(err);
4003
Ronnie Sahlberg2bdc7852014-04-29 19:06:194004 if (transaction->state != REF_TRANSACTION_OPEN)
4005 die("BUG: update called for transaction that is not open");
4006
Michael Haggerty16180332015-02-17 17:00:214007 if (new_sha1 && !is_null_sha1(new_sha1) &&
Ronnie Sahlbergd0f810f2014-09-03 18:45:434008 check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
4009 strbuf_addf(err, "refusing to update ref with bad name %s",
4010 refname);
4011 return -1;
4012 }
4013
Ronnie Sahlberg8e348002014-06-20 14:43:004014 update = add_update(transaction, refname);
Michael Haggerty16180332015-02-17 17:00:214015 if (new_sha1) {
4016 hashcpy(update->new_sha1, new_sha1);
4017 flags |= REF_HAVE_NEW;
4018 }
Michael Haggerty1d147bd2015-02-17 17:00:154019 if (old_sha1) {
Michael Haggertycaa40462014-04-07 13:48:104020 hashcpy(update->old_sha1, old_sha1);
Michael Haggerty8df4e512015-02-17 17:00:144021 flags |= REF_HAVE_OLD;
4022 }
4023 update->flags = flags;
Ronnie Sahlbergdb7516a2014-04-30 19:22:424024 if (msg)
4025 update->msg = xstrdup(msg);
Ronnie Sahlberg8e348002014-06-20 14:43:004026 return 0;
Michael Haggertycaa40462014-04-07 13:48:104027}
4028
Ronnie Sahlbergb416af52014-04-16 22:26:444029int ref_transaction_create(struct ref_transaction *transaction,
4030 const char *refname,
4031 const unsigned char *new_sha1,
Michael Haggertyfec14ec2015-02-17 17:00:134032 unsigned int flags, const char *msg,
Ronnie Sahlbergb416af52014-04-16 22:26:444033 struct strbuf *err)
Michael Haggertycaa40462014-04-07 13:48:104034{
Michael Haggertyf04c5b52015-02-17 17:00:194035 if (!new_sha1 || is_null_sha1(new_sha1))
4036 die("BUG: create called without valid new_sha1");
Ronnie Sahlbergbc9f2922014-12-04 23:08:134037 return ref_transaction_update(transaction, refname, new_sha1,
Michael Haggerty1d147bd2015-02-17 17:00:154038 null_sha1, flags, msg, err);
Michael Haggertycaa40462014-04-07 13:48:104039}
4040
Ronnie Sahlberg8c8bdc02014-04-16 22:27:454041int ref_transaction_delete(struct ref_transaction *transaction,
4042 const char *refname,
4043 const unsigned char *old_sha1,
Michael Haggertyfb5a6bb2015-02-17 17:00:164044 unsigned int flags, const char *msg,
Ronnie Sahlberg8c8bdc02014-04-16 22:27:454045 struct strbuf *err)
Michael Haggertycaa40462014-04-07 13:48:104046{
Michael Haggerty60294592015-02-17 17:00:204047 if (old_sha1 && is_null_sha1(old_sha1))
4048 die("BUG: delete called with old_sha1 set to zeros");
Michael Haggerty1d147bd2015-02-17 17:00:154049 return ref_transaction_update(transaction, refname,
Michael Haggertyfb5a6bb2015-02-17 17:00:164050 null_sha1, old_sha1,
Michael Haggerty1d147bd2015-02-17 17:00:154051 flags, msg, err);
Michael Haggertycaa40462014-04-07 13:48:104052}
4053
Michael Haggerty16180332015-02-17 17:00:214054int ref_transaction_verify(struct ref_transaction *transaction,
4055 const char *refname,
4056 const unsigned char *old_sha1,
4057 unsigned int flags,
4058 struct strbuf *err)
4059{
4060 if (!old_sha1)
4061 die("BUG: verify called with old_sha1 set to NULL");
4062 return ref_transaction_update(transaction, refname,
4063 NULL, old_sha1,
4064 flags, NULL, err);
4065}
4066
Michael Haggerty4b7b5202015-02-17 17:00:224067int update_ref(const char *msg, const char *refname,
4068 const unsigned char *new_sha1, const unsigned char *old_sha1,
Michael Haggertyfec14ec2015-02-17 17:00:134069 unsigned int flags, enum action_on_err onerr)
Brad King4738a332013-09-04 15:22:404070{
David Turner74ec19d2015-07-31 06:06:194071 struct ref_transaction *t = NULL;
Ronnie Sahlbergb4d75ac2014-04-24 23:36:554072 struct strbuf err = STRBUF_INIT;
David Turner74ec19d2015-07-31 06:06:194073 int ret = 0;
Ronnie Sahlbergb4d75ac2014-04-24 23:36:554074
David Turner74ec19d2015-07-31 06:06:194075 if (ref_type(refname) == REF_TYPE_PSEUDOREF) {
4076 ret = write_pseudoref(refname, new_sha1, old_sha1, &err);
4077 } else {
4078 t = ref_transaction_begin(&err);
4079 if (!t ||
4080 ref_transaction_update(t, refname, new_sha1, old_sha1,
4081 flags, msg, &err) ||
4082 ref_transaction_commit(t, &err)) {
4083 ret = 1;
4084 ref_transaction_free(t);
4085 }
4086 }
4087 if (ret) {
Ronnie Sahlbergb4d75ac2014-04-24 23:36:554088 const char *str = "update_ref failed for ref '%s': %s";
4089
Ronnie Sahlbergb4d75ac2014-04-24 23:36:554090 switch (onerr) {
4091 case UPDATE_REFS_MSG_ON_ERR:
4092 error(str, refname, err.buf);
4093 break;
4094 case UPDATE_REFS_DIE_ON_ERR:
4095 die(str, refname, err.buf);
4096 break;
4097 case UPDATE_REFS_QUIET_ON_ERR:
4098 break;
4099 }
4100 strbuf_release(&err);
Brad King4738a332013-09-04 15:22:404101 return 1;
Ronnie Sahlbergb4d75ac2014-04-24 23:36:554102 }
4103 strbuf_release(&err);
David Turner74ec19d2015-07-31 06:06:194104 if (t)
4105 ref_transaction_free(t);
Ronnie Sahlbergb4d75ac2014-04-24 23:36:554106 return 0;
Brad King4738a332013-09-04 15:22:404107}
4108
Michael Haggerty07f9c882015-05-11 15:25:114109static int ref_update_reject_duplicates(struct string_list *refnames,
Ronnie Sahlberg013198372014-06-20 14:42:594110 struct strbuf *err)
Brad King98aee922013-09-04 15:22:434111{
Michael Haggerty07f9c882015-05-11 15:25:114112 int i, n = refnames->nr;
Jonathan Nieder5a603b02014-08-28 23:42:374113
4114 assert(err);
4115
Brad King98aee922013-09-04 15:22:434116 for (i = 1; i < n; i++)
Michael Haggerty07f9c882015-05-11 15:25:114117 if (!strcmp(refnames->items[i - 1].string, refnames->items[i].string)) {
Jonathan Nieder5a603b02014-08-28 23:42:374118 strbuf_addf(err,
4119 "Multiple updates for ref '%s' not allowed.",
Michael Haggerty07f9c882015-05-11 15:25:114120 refnames->items[i].string);
Brad King98aee922013-09-04 15:22:434121 return 1;
4122 }
4123 return 0;
4124}
4125
Michael Haggertyb5c8ea22014-04-07 13:48:124126int ref_transaction_commit(struct ref_transaction *transaction,
Ronnie Sahlbergdb7516a2014-04-30 19:22:424127 struct strbuf *err)
Brad King98aee922013-09-04 15:22:434128{
Michael Haggerty4a45b2f2014-11-25 08:02:324129 int ret = 0, i;
Michael Haggertyb5c8ea22014-04-07 13:48:124130 int n = transaction->nr;
Michael Haggerty6a402332014-04-07 13:48:184131 struct ref_update **updates = transaction->updates;
Michael Haggerty4a45b2f2014-11-25 08:02:324132 struct string_list refs_to_delete = STRING_LIST_INIT_NODUP;
4133 struct string_list_item *ref_to_delete;
Michael Haggerty07f9c882015-05-11 15:25:114134 struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
Brad King98aee922013-09-04 15:22:434135
Jonathan Nieder5a603b02014-08-28 23:42:374136 assert(err);
4137
Ronnie Sahlberg2bdc7852014-04-29 19:06:194138 if (transaction->state != REF_TRANSACTION_OPEN)
4139 die("BUG: commit called for transaction that is not open");
4140
4141 if (!n) {
4142 transaction->state = REF_TRANSACTION_CLOSED;
Brad King98aee922013-09-04 15:22:434143 return 0;
Ronnie Sahlberg2bdc7852014-04-29 19:06:194144 }
Brad King98aee922013-09-04 15:22:434145
Michael Haggerty07f9c882015-05-11 15:25:114146 /* Fail if a refname appears more than once in the transaction: */
4147 for (i = 0; i < n; i++)
4148 string_list_append(&affected_refnames, updates[i]->refname);
4149 string_list_sort(&affected_refnames);
4150 if (ref_update_reject_duplicates(&affected_refnames, err)) {
Ronnie Sahlberg28e6a972014-05-16 21:14:384151 ret = TRANSACTION_GENERIC_ERROR;
Brad King98aee922013-09-04 15:22:434152 goto cleanup;
Ronnie Sahlberg28e6a972014-05-16 21:14:384153 }
Brad King98aee922013-09-04 15:22:434154
Michael Haggertycf018ee2015-04-24 11:35:494155 /*
4156 * Acquire all locks, verify old values if provided, check
4157 * that new values are valid, and write new values to the
4158 * lockfiles, ready to be activated. Only keep one lockfile
4159 * open at a time to avoid running out of file descriptors.
4160 */
Brad King98aee922013-09-04 15:22:434161 for (i = 0; i < n; i++) {
Michael Haggertycb198d22014-04-07 13:48:154162 struct ref_update *update = updates[i];
Michael Haggertycb198d22014-04-07 13:48:154163
Michael Haggertycbf50f92015-04-24 11:35:484164 if ((update->flags & REF_HAVE_NEW) &&
4165 is_null_sha1(update->new_sha1))
4166 update->flags |= REF_DELETING;
Michael Haggerty8df4e512015-02-17 17:00:144167 update->lock = lock_ref_sha1_basic(
4168 update->refname,
4169 ((update->flags & REF_HAVE_OLD) ?
4170 update->old_sha1 : NULL),
Michael Haggertye9111042015-05-11 15:25:124171 &affected_refnames, NULL,
Michael Haggertycbf50f92015-04-24 11:35:484172 update->flags,
Michael Haggerty4a32b2e2015-05-11 15:25:154173 &update->type,
4174 err);
Michael Haggerty81c960e2014-04-07 13:48:164175 if (!update->lock) {
Michael Haggertycbaabcb2015-05-11 15:25:184176 char *reason;
4177
Ronnie Sahlberg28e6a972014-05-16 21:14:384178 ret = (errno == ENOTDIR)
4179 ? TRANSACTION_NAME_CONFLICT
4180 : TRANSACTION_GENERIC_ERROR;
Michael Haggertycbaabcb2015-05-11 15:25:184181 reason = strbuf_detach(err, NULL);
Michael Haggertyc2e0a712015-05-22 23:34:574182 strbuf_addf(err, "cannot lock ref '%s': %s",
Michael Haggertycbaabcb2015-05-11 15:25:184183 update->refname, reason);
4184 free(reason);
Brad King98aee922013-09-04 15:22:434185 goto cleanup;
4186 }
Michael Haggertycf018ee2015-04-24 11:35:494187 if ((update->flags & REF_HAVE_NEW) &&
4188 !(update->flags & REF_DELETING)) {
Stefan Beller5a6f4702015-03-03 11:43:144189 int overwriting_symref = ((update->type & REF_ISSYMREF) &&
4190 (update->flags & REF_NODEREF));
4191
Michael Haggertycf018ee2015-04-24 11:35:494192 if (!overwriting_symref &&
Michael Haggerty5cb901a2015-05-25 18:39:224193 !hashcmp(update->lock->old_oid.hash, update->new_sha1)) {
Stefan Beller5a6f4702015-03-03 11:43:144194 /*
4195 * The reference already has the desired
4196 * value, so we don't need to write it.
4197 */
Michael Haggerty61e51e02015-05-09 15:29:204198 } else if (write_ref_to_lockfile(update->lock,
David Turnera4c653d2015-07-21 21:04:504199 update->new_sha1,
4200 err)) {
4201 char *write_err = strbuf_detach(err, NULL);
4202
Michael Haggertycf018ee2015-04-24 11:35:494203 /*
4204 * The lock was freed upon failure of
4205 * write_ref_to_lockfile():
4206 */
Michael Haggerty706d5f82015-03-02 09:29:524207 update->lock = NULL;
David Turnera4c653d2015-07-21 21:04:504208 strbuf_addf(err,
4209 "cannot update the ref '%s': %s",
4210 update->refname, write_err);
4211 free(write_err);
Ronnie Sahlberg28e6a972014-05-16 21:14:384212 ret = TRANSACTION_GENERIC_ERROR;
Brad King98aee922013-09-04 15:22:434213 goto cleanup;
Michael Haggerty706d5f82015-03-02 09:29:524214 } else {
Michael Haggertycf018ee2015-04-24 11:35:494215 update->flags |= REF_NEEDS_COMMIT;
4216 }
4217 }
4218 if (!(update->flags & REF_NEEDS_COMMIT)) {
4219 /*
4220 * We didn't have to write anything to the lockfile.
4221 * Close it to free up the file descriptor:
4222 */
4223 if (close_ref(update->lock)) {
4224 strbuf_addf(err, "Couldn't close %s.lock",
4225 update->refname);
4226 goto cleanup;
4227 }
4228 }
4229 }
4230
4231 /* Perform updates first so live commits remain referenced */
4232 for (i = 0; i < n; i++) {
4233 struct ref_update *update = updates[i];
4234
4235 if (update->flags & REF_NEEDS_COMMIT) {
4236 if (commit_ref_update(update->lock,
David Turner0f2a71d2015-07-21 21:04:544237 update->new_sha1, update->msg,
4238 update->flags, err)) {
Michael Haggertycf018ee2015-04-24 11:35:494239 /* freed by commit_ref_update(): */
4240 update->lock = NULL;
Michael Haggertycf018ee2015-04-24 11:35:494241 ret = TRANSACTION_GENERIC_ERROR;
4242 goto cleanup;
4243 } else {
4244 /* freed by commit_ref_update(): */
Michael Haggerty706d5f82015-03-02 09:29:524245 update->lock = NULL;
Ronnie Sahlberg04ad6222014-04-29 20:42:074246 }
Brad King98aee922013-09-04 15:22:434247 }
Michael Haggertycb198d22014-04-07 13:48:154248 }
Brad King98aee922013-09-04 15:22:434249
4250 /* Perform deletes now that updates are safely completed */
Michael Haggerty81c960e2014-04-07 13:48:164251 for (i = 0; i < n; i++) {
4252 struct ref_update *update = updates[i];
4253
Michael Haggertycf018ee2015-04-24 11:35:494254 if (update->flags & REF_DELETING) {
Jonathan Nieder65732842014-08-29 00:01:354255 if (delete_ref_loose(update->lock, update->type, err)) {
Ronnie Sahlberg28e6a972014-05-16 21:14:384256 ret = TRANSACTION_GENERIC_ERROR;
Jonathan Nieder65732842014-08-29 00:01:354257 goto cleanup;
4258 }
Ronnie Sahlberg28e6a972014-05-16 21:14:384259
Michael Haggertycbf50f92015-04-24 11:35:484260 if (!(update->flags & REF_ISPRUNING))
Michael Haggerty4a45b2f2014-11-25 08:02:324261 string_list_append(&refs_to_delete,
4262 update->lock->ref_name);
Brad King98aee922013-09-04 15:22:434263 }
Michael Haggerty81c960e2014-04-07 13:48:164264 }
4265
Michael Haggerty4a45b2f2014-11-25 08:02:324266 if (repack_without_refs(&refs_to_delete, err)) {
Ronnie Sahlberg28e6a972014-05-16 21:14:384267 ret = TRANSACTION_GENERIC_ERROR;
Jonathan Nieder65732842014-08-29 00:01:354268 goto cleanup;
4269 }
Michael Haggerty4a45b2f2014-11-25 08:02:324270 for_each_string_list_item(ref_to_delete, &refs_to_delete)
4271 unlink_or_warn(git_path("logs/%s", ref_to_delete->string));
Brad King98aee922013-09-04 15:22:434272 clear_loose_ref_cache(&ref_cache);
4273
4274cleanup:
Ronnie Sahlberg2bdc7852014-04-29 19:06:194275 transaction->state = REF_TRANSACTION_CLOSED;
4276
Brad King98aee922013-09-04 15:22:434277 for (i = 0; i < n; i++)
Michael Haggerty81c960e2014-04-07 13:48:164278 if (updates[i]->lock)
4279 unlock_ref(updates[i]->lock);
Michael Haggerty4a45b2f2014-11-25 08:02:324280 string_list_clear(&refs_to_delete, 0);
Michael Haggerty07f9c882015-05-11 15:25:114281 string_list_clear(&affected_refnames, 0);
Michael Haggertycaa40462014-04-07 13:48:104282 return ret;
4283}
4284
Michael Haggertye426ff42015-06-22 14:03:044285static int ref_present(const char *refname,
4286 const struct object_id *oid, int flags, void *cb_data)
4287{
4288 struct string_list *affected_refnames = cb_data;
4289
4290 return string_list_has_string(affected_refnames, refname);
4291}
4292
Michael Haggerty58f233c2015-06-22 14:03:014293int initial_ref_transaction_commit(struct ref_transaction *transaction,
4294 struct strbuf *err)
4295{
Michael Haggertye426ff42015-06-22 14:03:044296 struct ref_dir *loose_refs = get_loose_refs(&ref_cache);
4297 struct ref_dir *packed_refs = get_packed_refs(&ref_cache);
Michael Haggerty58f233c2015-06-22 14:03:014298 int ret = 0, i;
4299 int n = transaction->nr;
4300 struct ref_update **updates = transaction->updates;
Michael Haggertyfb802b32015-06-22 14:03:034301 struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
Michael Haggerty58f233c2015-06-22 14:03:014302
4303 assert(err);
4304
4305 if (transaction->state != REF_TRANSACTION_OPEN)
4306 die("BUG: commit called for transaction that is not open");
4307
Michael Haggertyfb802b32015-06-22 14:03:034308 /* Fail if a refname appears more than once in the transaction: */
4309 for (i = 0; i < n; i++)
4310 string_list_append(&affected_refnames, updates[i]->refname);
4311 string_list_sort(&affected_refnames);
4312 if (ref_update_reject_duplicates(&affected_refnames, err)) {
4313 ret = TRANSACTION_GENERIC_ERROR;
4314 goto cleanup;
4315 }
4316
Michael Haggertye426ff42015-06-22 14:03:044317 /*
4318 * It's really undefined to call this function in an active
4319 * repository or when there are existing references: we are
4320 * only locking and changing packed-refs, so (1) any
4321 * simultaneous processes might try to change a reference at
4322 * the same time we do, and (2) any existing loose versions of
4323 * the references that we are setting would have precedence
4324 * over our values. But some remote helpers create the remote
4325 * "HEAD" and "master" branches before calling this function,
4326 * so here we really only check that none of the references
4327 * that we are creating already exists.
4328 */
4329 if (for_each_rawref(ref_present, &affected_refnames))
4330 die("BUG: initial ref transaction called with existing refs");
4331
Michael Haggerty58f233c2015-06-22 14:03:014332 for (i = 0; i < n; i++) {
4333 struct ref_update *update = updates[i];
4334
4335 if ((update->flags & REF_HAVE_OLD) &&
4336 !is_null_sha1(update->old_sha1))
4337 die("BUG: initial ref transaction with old_sha1 set");
Michael Haggertye426ff42015-06-22 14:03:044338 if (verify_refname_available(update->refname,
4339 &affected_refnames, NULL,
4340 loose_refs, err) ||
4341 verify_refname_available(update->refname,
4342 &affected_refnames, NULL,
4343 packed_refs, err)) {
4344 ret = TRANSACTION_NAME_CONFLICT;
4345 goto cleanup;
4346 }
Michael Haggerty58f233c2015-06-22 14:03:014347 }
4348
4349 if (lock_packed_refs(0)) {
4350 strbuf_addf(err, "unable to lock packed-refs file: %s",
4351 strerror(errno));
4352 ret = TRANSACTION_GENERIC_ERROR;
4353 goto cleanup;
4354 }
4355
4356 for (i = 0; i < n; i++) {
4357 struct ref_update *update = updates[i];
4358
4359 if ((update->flags & REF_HAVE_NEW) &&
4360 !is_null_sha1(update->new_sha1))
4361 add_packed_ref(update->refname, update->new_sha1);
4362 }
4363
4364 if (commit_packed_refs()) {
4365 strbuf_addf(err, "unable to commit packed-refs file: %s",
4366 strerror(errno));
4367 ret = TRANSACTION_GENERIC_ERROR;
4368 goto cleanup;
4369 }
4370
4371cleanup:
4372 transaction->state = REF_TRANSACTION_CLOSED;
Michael Haggertyfb802b32015-06-22 14:03:034373 string_list_clear(&affected_refnames, 0);
Michael Haggerty58f233c2015-06-22 14:03:014374 return ret;
4375}
4376
Michael Haggertydfefa932011-12-12 05:38:094377char *shorten_unambiguous_ref(const char *refname, int strict)
Jeff King7c2b3022009-04-07 07:14:204378{
4379 int i;
4380 static char **scanf_fmts;
4381 static int nr_rules;
4382 char *short_name;
4383
Jeff King7c2b3022009-04-07 07:14:204384 if (!nr_rules) {
Michael Haggerty43466632014-01-08 14:43:394385 /*
4386 * Pre-generate scanf formats from ref_rev_parse_rules[].
4387 * Generate a format suitable for scanf from a
4388 * ref_rev_parse_rules rule by interpolating "%s" at the
4389 * location of the "%.*s".
4390 */
Jeff King7c2b3022009-04-07 07:14:204391 size_t total_len = 0;
Michael Haggerty84d56332014-01-08 14:43:384392 size_t offset = 0;
Jeff King7c2b3022009-04-07 07:14:204393
4394 /* the rule list is NULL terminated, count them first */
Jeff Kinga4165852013-10-24 08:45:134395 for (nr_rules = 0; ref_rev_parse_rules[nr_rules]; nr_rules++)
Michael Haggerty7902fe02014-01-08 14:43:404396 /* -2 for strlen("%.*s") - strlen("%s"); +1 for NUL */
4397 total_len += strlen(ref_rev_parse_rules[nr_rules]) - 2 + 1;
Jeff King7c2b3022009-04-07 07:14:204398
4399 scanf_fmts = xmalloc(nr_rules * sizeof(char *) + total_len);
4400
Michael Haggerty84d56332014-01-08 14:43:384401 offset = 0;
Jeff King7c2b3022009-04-07 07:14:204402 for (i = 0; i < nr_rules; i++) {
Michael Haggerty43466632014-01-08 14:43:394403 assert(offset < total_len);
Michael Haggerty84d56332014-01-08 14:43:384404 scanf_fmts[i] = (char *)&scanf_fmts[nr_rules] + offset;
Michael Haggerty43466632014-01-08 14:43:394405 offset += snprintf(scanf_fmts[i], total_len - offset,
4406 ref_rev_parse_rules[i], 2, "%s") + 1;
Jeff King7c2b3022009-04-07 07:14:204407 }
4408 }
4409
4410 /* bail out if there are no rules */
4411 if (!nr_rules)
Michael Haggertydfefa932011-12-12 05:38:094412 return xstrdup(refname);
Jeff King7c2b3022009-04-07 07:14:204413
Michael Haggertydfefa932011-12-12 05:38:094414 /* buffer for scanf result, at most refname must fit */
4415 short_name = xstrdup(refname);
Jeff King7c2b3022009-04-07 07:14:204416
4417 /* skip first rule, it will always match */
4418 for (i = nr_rules - 1; i > 0 ; --i) {
4419 int j;
Bert Wesarg6e7b3302009-04-13 10:25:464420 int rules_to_fail = i;
Jeff King7c2b3022009-04-07 07:14:204421 int short_name_len;
4422
Michael Haggertydfefa932011-12-12 05:38:094423 if (1 != sscanf(refname, scanf_fmts[i], short_name))
Jeff King7c2b3022009-04-07 07:14:204424 continue;
4425
4426 short_name_len = strlen(short_name);
4427
4428 /*
Bert Wesarg6e7b3302009-04-13 10:25:464429 * in strict mode, all (except the matched one) rules
4430 * must fail to resolve to a valid non-ambiguous ref
4431 */
4432 if (strict)
4433 rules_to_fail = nr_rules;
4434
4435 /*
Jeff King7c2b3022009-04-07 07:14:204436 * check if the short name resolves to a valid ref,
4437 * but use only rules prior to the matched one
4438 */
Bert Wesarg6e7b3302009-04-13 10:25:464439 for (j = 0; j < rules_to_fail; j++) {
Jeff King7c2b3022009-04-07 07:14:204440 const char *rule = ref_rev_parse_rules[j];
Jeff King7c2b3022009-04-07 07:14:204441 char refname[PATH_MAX];
4442
Bert Wesarg6e7b3302009-04-13 10:25:464443 /* skip matched rule */
4444 if (i == j)
4445 continue;
4446
Jeff King7c2b3022009-04-07 07:14:204447 /*
4448 * the short name is ambiguous, if it resolves
4449 * (with this previous rule) to a valid ref
4450 * read_ref() returns 0 on success
4451 */
4452 mksnpath(refname, sizeof(refname),
4453 rule, short_name_len, short_name);
Nguyễn Thái Ngọc Duyc6893322011-11-13 10:22:144454 if (ref_exists(refname))
Jeff King7c2b3022009-04-07 07:14:204455 break;
4456 }
4457
4458 /*
4459 * short name is non-ambiguous if all previous rules
4460 * haven't resolved to a valid ref
4461 */
Bert Wesarg6e7b3302009-04-13 10:25:464462 if (j == rules_to_fail)
Jeff King7c2b3022009-04-07 07:14:204463 return short_name;
4464 }
4465
4466 free(short_name);
Michael Haggertydfefa932011-12-12 05:38:094467 return xstrdup(refname);
Jeff King7c2b3022009-04-07 07:14:204468}
Junio C Hamanodaebaa72013-01-19 00:08:304469
4470static struct string_list *hide_refs;
4471
4472int parse_hide_refs_config(const char *var, const char *value, const char *section)
4473{
4474 if (!strcmp("transfer.hiderefs", var) ||
4475 /* NEEDSWORK: use parse_config_key() once both are merged */
Christian Couder59556542013-11-30 20:55:404476 (starts_with(var, section) && var[strlen(section)] == '.' &&
Junio C Hamanodaebaa72013-01-19 00:08:304477 !strcmp(var + strlen(section), ".hiderefs"))) {
4478 char *ref;
4479 int len;
4480
4481 if (!value)
4482 return config_error_nonbool(var);
4483 ref = xstrdup(value);
4484 len = strlen(ref);
4485 while (len && ref[len - 1] == '/')
4486 ref[--len] = '\0';
4487 if (!hide_refs) {
4488 hide_refs = xcalloc(1, sizeof(*hide_refs));
4489 hide_refs->strdup_strings = 1;
4490 }
4491 string_list_append(hide_refs, ref);
4492 }
4493 return 0;
4494}
4495
4496int ref_is_hidden(const char *refname)
4497{
Jeff King2bc31d12015-07-28 20:23:264498 int i;
Junio C Hamanodaebaa72013-01-19 00:08:304499
4500 if (!hide_refs)
4501 return 0;
Jeff King2bc31d12015-07-28 20:23:264502 for (i = hide_refs->nr - 1; i >= 0; i--) {
4503 const char *match = hide_refs->items[i].string;
4504 int neg = 0;
Junio C Hamanodaebaa72013-01-19 00:08:304505 int len;
Jeff King2bc31d12015-07-28 20:23:264506
4507 if (*match == '!') {
4508 neg = 1;
4509 match++;
4510 }
4511
4512 if (!starts_with(refname, match))
Junio C Hamanodaebaa72013-01-19 00:08:304513 continue;
Jeff King2bc31d12015-07-28 20:23:264514 len = strlen(match);
Junio C Hamanodaebaa72013-01-19 00:08:304515 if (!refname[len] || refname[len] == '/')
Jeff King2bc31d12015-07-28 20:23:264516 return !neg;
Junio C Hamanodaebaa72013-01-19 00:08:304517 }
4518 return 0;
4519}
Michael Haggertyfa5b1832014-12-12 08:56:594520
4521struct expire_reflog_cb {
4522 unsigned int flags;
4523 reflog_expiry_should_prune_fn *should_prune_fn;
4524 void *policy_cb;
4525 FILE *newlog;
4526 unsigned char last_kept_sha1[20];
4527};
4528
4529static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
4530 const char *email, unsigned long timestamp, int tz,
4531 const char *message, void *cb_data)
4532{
4533 struct expire_reflog_cb *cb = cb_data;
4534 struct expire_reflog_policy_cb *policy_cb = cb->policy_cb;
4535
4536 if (cb->flags & EXPIRE_REFLOGS_REWRITE)
4537 osha1 = cb->last_kept_sha1;
4538
4539 if ((*cb->should_prune_fn)(osha1, nsha1, email, timestamp, tz,
4540 message, policy_cb)) {
4541 if (!cb->newlog)
4542 printf("would prune %s", message);
4543 else if (cb->flags & EXPIRE_REFLOGS_VERBOSE)
4544 printf("prune %s", message);
4545 } else {
4546 if (cb->newlog) {
Stefan Bellerc653e032014-12-12 08:57:034547 fprintf(cb->newlog, "%s %s %s %lu %+05d\t%s",
Michael Haggertyfa5b1832014-12-12 08:56:594548 sha1_to_hex(osha1), sha1_to_hex(nsha1),
Stefan Bellerc653e032014-12-12 08:57:034549 email, timestamp, tz, message);
Michael Haggertyfa5b1832014-12-12 08:56:594550 hashcpy(cb->last_kept_sha1, nsha1);
4551 }
4552 if (cb->flags & EXPIRE_REFLOGS_VERBOSE)
4553 printf("keep %s", message);
4554 }
4555 return 0;
4556}
4557
4558int reflog_expire(const char *refname, const unsigned char *sha1,
4559 unsigned int flags,
4560 reflog_expiry_prepare_fn prepare_fn,
4561 reflog_expiry_should_prune_fn should_prune_fn,
4562 reflog_expiry_cleanup_fn cleanup_fn,
4563 void *policy_cb_data)
4564{
4565 static struct lock_file reflog_lock;
4566 struct expire_reflog_cb cb;
4567 struct ref_lock *lock;
4568 char *log_file;
4569 int status = 0;
Michael Haggerty5e6f0032015-03-03 11:43:164570 int type;
Michael Haggerty4a32b2e2015-05-11 15:25:154571 struct strbuf err = STRBUF_INIT;
Michael Haggertyfa5b1832014-12-12 08:56:594572
4573 memset(&cb, 0, sizeof(cb));
4574 cb.flags = flags;
4575 cb.policy_cb = policy_cb_data;
4576 cb.should_prune_fn = should_prune_fn;
4577
4578 /*
4579 * The reflog file is locked by holding the lock on the
4580 * reference itself, plus we might need to update the
4581 * reference if --updateref was specified:
4582 */
Michael Haggerty4a32b2e2015-05-11 15:25:154583 lock = lock_ref_sha1_basic(refname, sha1, NULL, NULL, 0, &type, &err);
4584 if (!lock) {
Michael Haggertyc628edf2015-05-11 15:25:204585 error("cannot lock ref '%s': %s", refname, err.buf);
Michael Haggerty4a32b2e2015-05-11 15:25:154586 strbuf_release(&err);
Michael Haggertyc628edf2015-05-11 15:25:204587 return -1;
Michael Haggerty4a32b2e2015-05-11 15:25:154588 }
Michael Haggertyfa5b1832014-12-12 08:56:594589 if (!reflog_exists(refname)) {
4590 unlock_ref(lock);
4591 return 0;
4592 }
4593
4594 log_file = git_pathdup("logs/%s", refname);
4595 if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {
4596 /*
4597 * Even though holding $GIT_DIR/logs/$reflog.lock has
4598 * no locking implications, we use the lock_file
4599 * machinery here anyway because it does a lot of the
4600 * work we need, including cleaning up if the program
4601 * exits unexpectedly.
4602 */
4603 if (hold_lock_file_for_update(&reflog_lock, log_file, 0) < 0) {
4604 struct strbuf err = STRBUF_INIT;
4605 unable_to_lock_message(log_file, errno, &err);
4606 error("%s", err.buf);
4607 strbuf_release(&err);
4608 goto failure;
4609 }
4610 cb.newlog = fdopen_lock_file(&reflog_lock, "w");
4611 if (!cb.newlog) {
4612 error("cannot fdopen %s (%s)",
Michael Haggertyb4fb09e2015-08-10 09:47:394613 get_lock_file_path(&reflog_lock), strerror(errno));
Michael Haggertyfa5b1832014-12-12 08:56:594614 goto failure;
4615 }
4616 }
4617
4618 (*prepare_fn)(refname, sha1, cb.policy_cb);
4619 for_each_reflog_ent(refname, expire_reflog_ent, &cb);
4620 (*cleanup_fn)(cb.policy_cb);
4621
4622 if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {
Michael Haggerty5e6f0032015-03-03 11:43:164623 /*
4624 * It doesn't make sense to adjust a reference pointed
4625 * to by a symbolic ref based on expiring entries in
Michael Haggerty423c6882015-03-03 11:43:174626 * the symbolic reference's reflog. Nor can we update
4627 * a reference if there are no remaining reflog
4628 * entries.
Michael Haggerty5e6f0032015-03-03 11:43:164629 */
4630 int update = (flags & EXPIRE_REFLOGS_UPDATE_REF) &&
Michael Haggerty423c6882015-03-03 11:43:174631 !(type & REF_ISSYMREF) &&
4632 !is_null_sha1(cb.last_kept_sha1);
Michael Haggerty5e6f0032015-03-03 11:43:164633
Michael Haggertyfa5b1832014-12-12 08:56:594634 if (close_lock_file(&reflog_lock)) {
4635 status |= error("couldn't write %s: %s", log_file,
4636 strerror(errno));
Michael Haggerty5e6f0032015-03-03 11:43:164637 } else if (update &&
Michael Haggertyc99a4c22015-08-10 09:47:384638 (write_in_full(get_lock_file_fd(lock->lk),
Michael Haggertyfa5b1832014-12-12 08:56:594639 sha1_to_hex(cb.last_kept_sha1), 40) != 40 ||
Michael Haggertyc99a4c22015-08-10 09:47:384640 write_str_in_full(get_lock_file_fd(lock->lk), "\n") != 1 ||
4641 close_ref(lock) < 0)) {
Michael Haggertyfa5b1832014-12-12 08:56:594642 status |= error("couldn't write %s",
Michael Haggertyb4fb09e2015-08-10 09:47:394643 get_lock_file_path(lock->lk));
Michael Haggertyfa5b1832014-12-12 08:56:594644 rollback_lock_file(&reflog_lock);
4645 } else if (commit_lock_file(&reflog_lock)) {
4646 status |= error("unable to commit reflog '%s' (%s)",
4647 log_file, strerror(errno));
Michael Haggerty5e6f0032015-03-03 11:43:164648 } else if (update && commit_ref(lock)) {
Michael Haggertyfa5b1832014-12-12 08:56:594649 status |= error("couldn't set %s", lock->ref_name);
4650 }
4651 }
4652 free(log_file);
4653 unlock_ref(lock);
4654 return status;
4655
4656 failure:
4657 rollback_lock_file(&reflog_lock);
4658 free(log_file);
4659 unlock_ref(lock);
4660 return -1;
4661}