]> git.proxmox.com Git - libgit2.git/blob - src/refs.c
Ignore ref oid terminator
[libgit2.git] / src / refs.c
1 /*
2 * Copyright (C) 2009-2012 the libgit2 contributors
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
8 #include "refs.h"
9 #include "hash.h"
10 #include "repository.h"
11 #include "fileops.h"
12 #include "pack.h"
13 #include "reflog.h"
14
15 #include <git2/tag.h>
16 #include <git2/object.h>
17
18 GIT__USE_STRMAP;
19
20 #define DEFAULT_NESTING_LEVEL 5
21 #define MAX_NESTING_LEVEL 10
22
23 enum {
24 GIT_PACKREF_HAS_PEEL = 1,
25 GIT_PACKREF_WAS_LOOSE = 2
26 };
27
28 struct packref {
29 git_oid oid;
30 git_oid peel;
31 char flags;
32 char name[GIT_FLEX_ARRAY];
33 };
34
35 static int reference_read(
36 git_buf *file_content,
37 time_t *mtime,
38 const char *repo_path,
39 const char *ref_name,
40 int *updated);
41
42 /* loose refs */
43 static int loose_parse_symbolic(git_reference *ref, git_buf *file_content);
44 static int loose_parse_oid(git_oid *ref, git_buf *file_content);
45 static int loose_lookup(git_reference *ref);
46 static int loose_lookup_to_packfile(struct packref **ref_out,
47 git_repository *repo, const char *name);
48 static int loose_write(git_reference *ref);
49
50 /* packed refs */
51 static int packed_parse_peel(struct packref *tag_ref,
52 const char **buffer_out, const char *buffer_end);
53 static int packed_parse_oid(struct packref **ref_out,
54 const char **buffer_out, const char *buffer_end);
55 static int packed_load(git_repository *repo);
56 static int packed_loadloose(git_repository *repository);
57 static int packed_write_ref(struct packref *ref, git_filebuf *file);
58 static int packed_find_peel(git_repository *repo, struct packref *ref);
59 static int packed_remove_loose(git_repository *repo, git_vector *packing_list);
60 static int packed_sort(const void *a, const void *b);
61 static int packed_lookup(git_reference *ref);
62 static int packed_write(git_repository *repo);
63
64 /* internal helpers */
65 static int reference_path_available(git_repository *repo,
66 const char *ref, const char *old_ref);
67 static int reference_delete(git_reference *ref);
68 static int reference_lookup(git_reference *ref);
69
70 /* name normalization */
71 static int normalize_name(char *buffer_out, size_t out_size,
72 const char *name, int is_oid_ref);
73
74
75 void git_reference_free(git_reference *reference)
76 {
77 if (reference == NULL)
78 return;
79
80 git__free(reference->name);
81 reference->name = NULL;
82
83 if (reference->flags & GIT_REF_SYMBOLIC) {
84 git__free(reference->target.symbolic);
85 reference->target.symbolic = NULL;
86 }
87
88 git__free(reference);
89 }
90
91 static int reference_alloc(
92 git_reference **ref_out,
93 git_repository *repo,
94 const char *name)
95 {
96 git_reference *reference = NULL;
97
98 assert(ref_out && repo && name);
99
100 reference = git__malloc(sizeof(git_reference));
101 GITERR_CHECK_ALLOC(reference);
102
103 memset(reference, 0x0, sizeof(git_reference));
104 reference->owner = repo;
105
106 reference->name = git__strdup(name);
107 GITERR_CHECK_ALLOC(reference->name);
108
109 *ref_out = reference;
110 return 0;
111 }
112
113 static int reference_read(
114 git_buf *file_content,
115 time_t *mtime,
116 const char *repo_path,
117 const char *ref_name,
118 int *updated)
119 {
120 git_buf path = GIT_BUF_INIT;
121 int result;
122
123 assert(file_content && repo_path && ref_name);
124
125 /* Determine the full path of the file */
126 if (git_buf_joinpath(&path, repo_path, ref_name) < 0)
127 return -1;
128
129 result = git_futils_readbuffer_updated(file_content, path.ptr, mtime, updated);
130 git_buf_free(&path);
131 return result;
132 }
133
134 static int loose_parse_symbolic(git_reference *ref, git_buf *file_content)
135 {
136 const unsigned int header_len = (unsigned int)strlen(GIT_SYMREF);
137 const char *refname_start;
138 char *eol;
139
140 refname_start = (const char *)file_content->ptr;
141
142 if (git_buf_len(file_content) < header_len + 1)
143 goto corrupt;
144
145 /*
146 * Assume we have already checked for the header
147 * before calling this function
148 */
149 refname_start += header_len;
150
151 ref->target.symbolic = git__strdup(refname_start);
152 GITERR_CHECK_ALLOC(ref->target.symbolic);
153
154 /* remove newline at the end of file */
155 eol = strchr(ref->target.symbolic, '\n');
156 if (eol == NULL)
157 goto corrupt;
158
159 *eol = '\0';
160 if (eol[-1] == '\r')
161 eol[-1] = '\0';
162
163 return 0;
164
165 corrupt:
166 giterr_set(GITERR_REFERENCE, "Corrupted loose reference file");
167 return -1;
168 }
169
170 static int loose_parse_oid(git_oid *oid, git_buf *file_content)
171 {
172 char *buffer;
173
174 buffer = (char *)file_content->ptr;
175
176 /* File format: 40 chars (OID) */
177 if (git_buf_len(file_content) < GIT_OID_HEXSZ)
178 goto corrupt;
179
180 if (git_oid_fromstr(oid, buffer) < 0)
181 goto corrupt;
182
183 return 0;
184
185 corrupt:
186 giterr_set(GITERR_REFERENCE, "Corrupted loose reference file");
187 return -1;
188 }
189
190 static git_ref_t loose_guess_rtype(const git_buf *full_path)
191 {
192 git_buf ref_file = GIT_BUF_INIT;
193 git_ref_t type;
194
195 type = GIT_REF_INVALID;
196
197 if (git_futils_readbuffer(&ref_file, full_path->ptr) == 0) {
198 if (git__prefixcmp((const char *)(ref_file.ptr), GIT_SYMREF) == 0)
199 type = GIT_REF_SYMBOLIC;
200 else
201 type = GIT_REF_OID;
202 }
203
204 git_buf_free(&ref_file);
205 return type;
206 }
207
208 static int loose_lookup(git_reference *ref)
209 {
210 int result, updated;
211 git_buf ref_file = GIT_BUF_INIT;
212
213 result = reference_read(&ref_file, &ref->mtime,
214 ref->owner->path_repository, ref->name, &updated);
215
216 if (result < 0)
217 return result;
218
219 if (!updated)
220 return 0;
221
222 if (ref->flags & GIT_REF_SYMBOLIC) {
223 git__free(ref->target.symbolic);
224 ref->target.symbolic = NULL;
225 }
226
227 ref->flags = 0;
228
229 if (git__prefixcmp((const char *)(ref_file.ptr), GIT_SYMREF) == 0) {
230 ref->flags |= GIT_REF_SYMBOLIC;
231 result = loose_parse_symbolic(ref, &ref_file);
232 } else {
233 ref->flags |= GIT_REF_OID;
234 result = loose_parse_oid(&ref->target.oid, &ref_file);
235 }
236
237 git_buf_free(&ref_file);
238 return result;
239 }
240
241 static int loose_lookup_to_packfile(
242 struct packref **ref_out,
243 git_repository *repo,
244 const char *name)
245 {
246 git_buf ref_file = GIT_BUF_INIT;
247 struct packref *ref = NULL;
248 size_t name_len;
249
250 *ref_out = NULL;
251
252 if (reference_read(&ref_file, NULL, repo->path_repository, name, NULL) < 0)
253 return -1;
254
255 name_len = strlen(name);
256 ref = git__malloc(sizeof(struct packref) + name_len + 1);
257 GITERR_CHECK_ALLOC(ref);
258
259 memcpy(ref->name, name, name_len);
260 ref->name[name_len] = 0;
261
262 if (loose_parse_oid(&ref->oid, &ref_file) < 0) {
263 git_buf_free(&ref_file);
264 git__free(ref);
265 return -1;
266 }
267
268 ref->flags = GIT_PACKREF_WAS_LOOSE;
269
270 *ref_out = ref;
271 git_buf_free(&ref_file);
272 return 0;
273 }
274
275 static int loose_write(git_reference *ref)
276 {
277 git_filebuf file = GIT_FILEBUF_INIT;
278 git_buf ref_path = GIT_BUF_INIT;
279 struct stat st;
280
281 if (git_buf_joinpath(&ref_path, ref->owner->path_repository, ref->name) < 0)
282 return -1;
283
284 /* Remove a possibly existing empty directory hierarchy
285 * which name would collide with the reference name
286 */
287 if (git_path_isdir(git_buf_cstr(&ref_path)) &&
288 (git_futils_rmdir_r(git_buf_cstr(&ref_path), GIT_DIRREMOVAL_ONLY_EMPTY_DIRS) < 0)) {
289 git_buf_free(&ref_path);
290 return -1;
291 }
292
293 if (git_filebuf_open(&file, ref_path.ptr, GIT_FILEBUF_FORCE) < 0) {
294 git_buf_free(&ref_path);
295 return -1;
296 }
297
298 git_buf_free(&ref_path);
299
300 if (ref->flags & GIT_REF_OID) {
301 char oid[GIT_OID_HEXSZ + 1];
302
303 git_oid_fmt(oid, &ref->target.oid);
304 oid[GIT_OID_HEXSZ] = '\0';
305
306 git_filebuf_printf(&file, "%s\n", oid);
307
308 } else if (ref->flags & GIT_REF_SYMBOLIC) {
309 git_filebuf_printf(&file, GIT_SYMREF "%s\n", ref->target.symbolic);
310 } else {
311 assert(0); /* don't let this happen */
312 }
313
314 if (p_stat(ref_path.ptr, &st) == 0)
315 ref->mtime = st.st_mtime;
316
317 return git_filebuf_commit(&file, GIT_REFS_FILE_MODE);
318 }
319
320 static int packed_parse_peel(
321 struct packref *tag_ref,
322 const char **buffer_out,
323 const char *buffer_end)
324 {
325 const char *buffer = *buffer_out + 1;
326
327 assert(buffer[-1] == '^');
328
329 /* Ensure it's not the first entry of the file */
330 if (tag_ref == NULL)
331 goto corrupt;
332
333 /* Ensure reference is a tag */
334 if (git__prefixcmp(tag_ref->name, GIT_REFS_TAGS_DIR) != 0)
335 goto corrupt;
336
337 if (buffer + GIT_OID_HEXSZ >= buffer_end)
338 goto corrupt;
339
340 /* Is this a valid object id? */
341 if (git_oid_fromstr(&tag_ref->peel, buffer) < 0)
342 goto corrupt;
343
344 buffer = buffer + GIT_OID_HEXSZ;
345 if (*buffer == '\r')
346 buffer++;
347
348 if (*buffer != '\n')
349 goto corrupt;
350
351 *buffer_out = buffer + 1;
352 return 0;
353
354 corrupt:
355 giterr_set(GITERR_REFERENCE, "The packed references file is corrupted");
356 return -1;
357 }
358
359 static int packed_parse_oid(
360 struct packref **ref_out,
361 const char **buffer_out,
362 const char *buffer_end)
363 {
364 struct packref *ref = NULL;
365
366 const char *buffer = *buffer_out;
367 const char *refname_begin, *refname_end;
368
369 size_t refname_len;
370 git_oid id;
371
372 refname_begin = (buffer + GIT_OID_HEXSZ + 1);
373 if (refname_begin >= buffer_end || refname_begin[-1] != ' ')
374 goto corrupt;
375
376 /* Is this a valid object id? */
377 if (git_oid_fromstr(&id, buffer) < 0)
378 goto corrupt;
379
380 refname_end = memchr(refname_begin, '\n', buffer_end - refname_begin);
381 if (refname_end == NULL)
382 goto corrupt;
383
384 if (refname_end[-1] == '\r')
385 refname_end--;
386
387 refname_len = refname_end - refname_begin;
388
389 ref = git__malloc(sizeof(struct packref) + refname_len + 1);
390 GITERR_CHECK_ALLOC(ref);
391
392 memcpy(ref->name, refname_begin, refname_len);
393 ref->name[refname_len] = 0;
394
395 git_oid_cpy(&ref->oid, &id);
396
397 ref->flags = 0;
398
399 *ref_out = ref;
400 *buffer_out = refname_end + 1;
401
402 return 0;
403
404 corrupt:
405 git__free(ref);
406 giterr_set(GITERR_REFERENCE, "The packed references file is corrupted");
407 return -1;
408 }
409
410 static int packed_load(git_repository *repo)
411 {
412 int result, updated;
413 git_buf packfile = GIT_BUF_INIT;
414 const char *buffer_start, *buffer_end;
415 git_refcache *ref_cache = &repo->references;
416
417 /* First we make sure we have allocated the hash table */
418 if (ref_cache->packfile == NULL) {
419 ref_cache->packfile = git_strmap_alloc();
420 GITERR_CHECK_ALLOC(ref_cache->packfile);
421 }
422
423 result = reference_read(&packfile, &ref_cache->packfile_time,
424 repo->path_repository, GIT_PACKEDREFS_FILE, &updated);
425
426 /*
427 * If we couldn't find the file, we need to clear the table and
428 * return. On any other error, we return that error. If everything
429 * went fine and the file wasn't updated, then there's nothing new
430 * for us here, so just return. Anything else means we need to
431 * refresh the packed refs.
432 */
433 if (result == GIT_ENOTFOUND) {
434 git_strmap_clear(ref_cache->packfile);
435 return 0;
436 }
437
438 if (result < 0)
439 return -1;
440
441 if (!updated)
442 return 0;
443
444 /*
445 * At this point, we want to refresh the packed refs. We already
446 * have the contents in our buffer.
447 */
448 git_strmap_clear(ref_cache->packfile);
449
450 buffer_start = (const char *)packfile.ptr;
451 buffer_end = (const char *)(buffer_start) + packfile.size;
452
453 while (buffer_start < buffer_end && buffer_start[0] == '#') {
454 buffer_start = strchr(buffer_start, '\n');
455 if (buffer_start == NULL)
456 goto parse_failed;
457
458 buffer_start++;
459 }
460
461 while (buffer_start < buffer_end) {
462 int err;
463 struct packref *ref = NULL;
464
465 if (packed_parse_oid(&ref, &buffer_start, buffer_end) < 0)
466 goto parse_failed;
467
468 if (buffer_start[0] == '^') {
469 if (packed_parse_peel(ref, &buffer_start, buffer_end) < 0)
470 goto parse_failed;
471 }
472
473 git_strmap_insert(ref_cache->packfile, ref->name, ref, err);
474 if (err < 0)
475 goto parse_failed;
476 }
477
478 git_buf_free(&packfile);
479 return 0;
480
481 parse_failed:
482 git_strmap_free(ref_cache->packfile);
483 ref_cache->packfile = NULL;
484 git_buf_free(&packfile);
485 return -1;
486 }
487
488
489 struct dirent_list_data {
490 git_repository *repo;
491 size_t repo_path_len;
492 unsigned int list_flags;
493
494 int (*callback)(const char *, void *);
495 void *callback_payload;
496 int callback_error;
497 };
498
499 static int _dirent_loose_listall(void *_data, git_buf *full_path)
500 {
501 struct dirent_list_data *data = (struct dirent_list_data *)_data;
502 const char *file_path = full_path->ptr + data->repo_path_len;
503
504 if (git_path_isdir(full_path->ptr) == true)
505 return git_path_direach(full_path, _dirent_loose_listall, _data);
506
507 /* do not add twice a reference that exists already in the packfile */
508 if ((data->list_flags & GIT_REF_PACKED) != 0 &&
509 git_strmap_exists(data->repo->references.packfile, file_path))
510 return 0;
511
512 if (data->list_flags != GIT_REF_LISTALL) {
513 if ((data->list_flags & loose_guess_rtype(full_path)) == 0)
514 return 0; /* we are filtering out this reference */
515 }
516
517 if (data->callback(file_path, data->callback_payload))
518 data->callback_error = GIT_EUSER;
519
520 return data->callback_error;
521 }
522
523 static int _dirent_loose_load(void *data, git_buf *full_path)
524 {
525 git_repository *repository = (git_repository *)data;
526 void *old_ref = NULL;
527 struct packref *ref;
528 const char *file_path;
529 int err;
530
531 if (git_path_isdir(full_path->ptr) == true)
532 return git_path_direach(full_path, _dirent_loose_load, repository);
533
534 file_path = full_path->ptr + strlen(repository->path_repository);
535
536 if (loose_lookup_to_packfile(&ref, repository, file_path) < 0)
537 return -1;
538
539 git_strmap_insert2(
540 repository->references.packfile, ref->name, ref, old_ref, err);
541 if (err < 0) {
542 git__free(ref);
543 return -1;
544 }
545
546 git__free(old_ref);
547 return 0;
548 }
549
550 /*
551 * Load all the loose references from the repository
552 * into the in-memory Packfile, and build a vector with
553 * all the references so it can be written back to
554 * disk.
555 */
556 static int packed_loadloose(git_repository *repository)
557 {
558 git_buf refs_path = GIT_BUF_INIT;
559 int result;
560
561 /* the packfile must have been previously loaded! */
562 assert(repository->references.packfile);
563
564 if (git_buf_joinpath(&refs_path, repository->path_repository, GIT_REFS_DIR) < 0)
565 return -1;
566
567 /*
568 * Load all the loose files from disk into the Packfile table.
569 * This will overwrite any old packed entries with their
570 * updated loose versions
571 */
572 result = git_path_direach(&refs_path, _dirent_loose_load, repository);
573 git_buf_free(&refs_path);
574
575 return result;
576 }
577
578 /*
579 * Write a single reference into a packfile
580 */
581 static int packed_write_ref(struct packref *ref, git_filebuf *file)
582 {
583 char oid[GIT_OID_HEXSZ + 1];
584
585 git_oid_fmt(oid, &ref->oid);
586 oid[GIT_OID_HEXSZ] = 0;
587
588 /*
589 * For references that peel to an object in the repo, we must
590 * write the resulting peel on a separate line, e.g.
591 *
592 * 6fa8a902cc1d18527e1355773c86721945475d37 refs/tags/libgit2-0.4
593 * ^2ec0cb7959b0bf965d54f95453f5b4b34e8d3100
594 *
595 * This obviously only applies to tags.
596 * The required peels have already been loaded into `ref->peel_target`.
597 */
598 if (ref->flags & GIT_PACKREF_HAS_PEEL) {
599 char peel[GIT_OID_HEXSZ + 1];
600 git_oid_fmt(peel, &ref->peel);
601 peel[GIT_OID_HEXSZ] = 0;
602
603 if (git_filebuf_printf(file, "%s %s\n^%s\n", oid, ref->name, peel) < 0)
604 return -1;
605 } else {
606 if (git_filebuf_printf(file, "%s %s\n", oid, ref->name) < 0)
607 return -1;
608 }
609
610 return 0;
611 }
612
613 /*
614 * Find out what object this reference resolves to.
615 *
616 * For references that point to a 'big' tag (e.g. an
617 * actual tag object on the repository), we need to
618 * cache on the packfile the OID of the object to
619 * which that 'big tag' is pointing to.
620 */
621 static int packed_find_peel(git_repository *repo, struct packref *ref)
622 {
623 git_object *object;
624
625 if (ref->flags & GIT_PACKREF_HAS_PEEL)
626 return 0;
627
628 /*
629 * Only applies to tags, i.e. references
630 * in the /refs/tags folder
631 */
632 if (git__prefixcmp(ref->name, GIT_REFS_TAGS_DIR) != 0)
633 return 0;
634
635 /*
636 * Find the tagged object in the repository
637 */
638 if (git_object_lookup(&object, repo, &ref->oid, GIT_OBJ_ANY) < 0)
639 return -1;
640
641 /*
642 * If the tagged object is a Tag object, we need to resolve it;
643 * if the ref is actually a 'weak' ref, we don't need to resolve
644 * anything.
645 */
646 if (git_object_type(object) == GIT_OBJ_TAG) {
647 git_tag *tag = (git_tag *)object;
648
649 /*
650 * Find the object pointed at by this tag
651 */
652 git_oid_cpy(&ref->peel, git_tag_target_oid(tag));
653 ref->flags |= GIT_PACKREF_HAS_PEEL;
654
655 /*
656 * The reference has now cached the resolved OID, and is
657 * marked at such. When written to the packfile, it'll be
658 * accompanied by this resolved oid
659 */
660 }
661
662 git_object_free(object);
663 return 0;
664 }
665
666 /*
667 * Remove all loose references
668 *
669 * Once we have successfully written a packfile,
670 * all the loose references that were packed must be
671 * removed from disk.
672 *
673 * This is a dangerous method; make sure the packfile
674 * is well-written, because we are destructing references
675 * here otherwise.
676 */
677 static int packed_remove_loose(git_repository *repo, git_vector *packing_list)
678 {
679 unsigned int i;
680 git_buf full_path = GIT_BUF_INIT;
681 int failed = 0;
682
683 for (i = 0; i < packing_list->length; ++i) {
684 struct packref *ref = git_vector_get(packing_list, i);
685
686 if ((ref->flags & GIT_PACKREF_WAS_LOOSE) == 0)
687 continue;
688
689 if (git_buf_joinpath(&full_path, repo->path_repository, ref->name) < 0)
690 return -1; /* critical; do not try to recover on oom */
691
692 if (git_path_exists(full_path.ptr) == true && p_unlink(full_path.ptr) < 0) {
693 if (failed)
694 continue;
695
696 giterr_set(GITERR_REFERENCE,
697 "Failed to remove loose reference '%s' after packing: %s",
698 full_path.ptr, strerror(errno));
699
700 failed = 1;
701 }
702
703 /*
704 * if we fail to remove a single file, this is *not* good,
705 * but we should keep going and remove as many as possible.
706 * After we've removed as many files as possible, we return
707 * the error code anyway.
708 */
709 }
710
711 git_buf_free(&full_path);
712 return failed ? -1 : 0;
713 }
714
715 static int packed_sort(const void *a, const void *b)
716 {
717 const struct packref *ref_a = (const struct packref *)a;
718 const struct packref *ref_b = (const struct packref *)b;
719
720 return strcmp(ref_a->name, ref_b->name);
721 }
722
723 /*
724 * Write all the contents in the in-memory packfile to disk.
725 */
726 static int packed_write(git_repository *repo)
727 {
728 git_filebuf pack_file = GIT_FILEBUF_INIT;
729 unsigned int i;
730 git_buf pack_file_path = GIT_BUF_INIT;
731 git_vector packing_list;
732 unsigned int total_refs;
733
734 assert(repo && repo->references.packfile);
735
736 total_refs =
737 (unsigned int)git_strmap_num_entries(repo->references.packfile);
738
739 if (git_vector_init(&packing_list, total_refs, packed_sort) < 0)
740 return -1;
741
742 /* Load all the packfile into a vector */
743 {
744 struct packref *reference;
745
746 /* cannot fail: vector already has the right size */
747 git_strmap_foreach_value(repo->references.packfile, reference, {
748 git_vector_insert(&packing_list, reference);
749 });
750 }
751
752 /* sort the vector so the entries appear sorted on the packfile */
753 git_vector_sort(&packing_list);
754
755 /* Now we can open the file! */
756 if (git_buf_joinpath(&pack_file_path, repo->path_repository, GIT_PACKEDREFS_FILE) < 0)
757 goto cleanup_memory;
758
759 if (git_filebuf_open(&pack_file, pack_file_path.ptr, 0) < 0)
760 goto cleanup_packfile;
761
762 /* Packfiles have a header... apparently
763 * This is in fact not required, but we might as well print it
764 * just for kicks */
765 if (git_filebuf_printf(&pack_file, "%s\n", GIT_PACKEDREFS_HEADER) < 0)
766 goto cleanup_packfile;
767
768 for (i = 0; i < packing_list.length; ++i) {
769 struct packref *ref = (struct packref *)git_vector_get(&packing_list, i);
770
771 if (packed_find_peel(repo, ref) < 0)
772 goto cleanup_packfile;
773
774 if (packed_write_ref(ref, &pack_file) < 0)
775 goto cleanup_packfile;
776 }
777
778 /* if we've written all the references properly, we can commit
779 * the packfile to make the changes effective */
780 if (git_filebuf_commit(&pack_file, GIT_PACKEDREFS_FILE_MODE) < 0)
781 goto cleanup_memory;
782
783 /* when and only when the packfile has been properly written,
784 * we can go ahead and remove the loose refs */
785 if (packed_remove_loose(repo, &packing_list) < 0)
786 goto cleanup_memory;
787
788 {
789 struct stat st;
790 if (p_stat(pack_file_path.ptr, &st) == 0)
791 repo->references.packfile_time = st.st_mtime;
792 }
793
794 git_vector_free(&packing_list);
795 git_buf_free(&pack_file_path);
796
797 /* we're good now */
798 return 0;
799
800 cleanup_packfile:
801 git_filebuf_cleanup(&pack_file);
802
803 cleanup_memory:
804 git_vector_free(&packing_list);
805 git_buf_free(&pack_file_path);
806
807 return -1;
808 }
809
810 struct reference_available_t {
811 const char *new_ref;
812 const char *old_ref;
813 int available;
814 };
815
816 static int _reference_available_cb(const char *ref, void *data)
817 {
818 struct reference_available_t *d;
819
820 assert(ref && data);
821 d = (struct reference_available_t *)data;
822
823 if (!d->old_ref || strcmp(d->old_ref, ref)) {
824 size_t reflen = strlen(ref);
825 size_t newlen = strlen(d->new_ref);
826 size_t cmplen = reflen < newlen ? reflen : newlen;
827 const char *lead = reflen < newlen ? d->new_ref : ref;
828
829 if (!strncmp(d->new_ref, ref, cmplen) && lead[cmplen] == '/') {
830 d->available = 0;
831 return -1;
832 }
833 }
834
835 return 0;
836 }
837
838 static int reference_path_available(
839 git_repository *repo,
840 const char *ref,
841 const char* old_ref)
842 {
843 int error;
844 struct reference_available_t data;
845
846 data.new_ref = ref;
847 data.old_ref = old_ref;
848 data.available = 1;
849
850 error = git_reference_foreach(
851 repo, GIT_REF_LISTALL, _reference_available_cb, (void *)&data);
852 if (error < 0)
853 return error;
854
855 if (!data.available) {
856 giterr_set(GITERR_REFERENCE,
857 "The path to reference '%s' collides with an existing one", ref);
858 return -1;
859 }
860
861 return 0;
862 }
863
864 static int reference_exists(int *exists, git_repository *repo, const char *ref_name)
865 {
866 git_buf ref_path = GIT_BUF_INIT;
867
868 if (packed_load(repo) < 0)
869 return -1;
870
871 if (git_buf_joinpath(&ref_path, repo->path_repository, ref_name) < 0)
872 return -1;
873
874 if (git_path_isfile(ref_path.ptr) == true ||
875 git_strmap_exists(repo->references.packfile, ref_path.ptr))
876 {
877 *exists = 1;
878 } else {
879 *exists = 0;
880 }
881
882 git_buf_free(&ref_path);
883 return 0;
884 }
885
886 /*
887 * Check if a reference could be written to disk, based on:
888 *
889 * - Whether a reference with the same name already exists,
890 * and we are allowing or disallowing overwrites
891 *
892 * - Whether the name of the reference would collide with
893 * an existing path
894 */
895 static int reference_can_write(
896 git_repository *repo,
897 const char *refname,
898 const char *previous_name,
899 int force)
900 {
901 /* see if the reference shares a path with an existing reference;
902 * if a path is shared, we cannot create the reference, even when forcing */
903 if (reference_path_available(repo, refname, previous_name) < 0)
904 return -1;
905
906 /* check if the reference actually exists, but only if we are not forcing
907 * the rename. If we are forcing, it's OK to overwrite */
908 if (!force) {
909 int exists;
910
911 if (reference_exists(&exists, repo, refname) < 0)
912 return -1;
913
914 /* We cannot proceed if the reference already exists and we're not forcing
915 * the rename; the existing one would be overwritten */
916 if (exists) {
917 giterr_set(GITERR_REFERENCE,
918 "A reference with that name (%s) already exists", refname);
919 return GIT_EEXISTS;
920 }
921 }
922
923 /* FIXME: if the reference exists and we are forcing, do we really need to
924 * remove the reference first?
925 *
926 * Two cases:
927 *
928 * - the reference already exists and is loose: not a problem, the file
929 * gets overwritten on disk
930 *
931 * - the reference already exists and is packed: we write a new one as
932 * loose, which by all means renders the packed one useless
933 */
934
935 return 0;
936 }
937
938
939 static int packed_lookup(git_reference *ref)
940 {
941 struct packref *pack_ref = NULL;
942 git_strmap *packfile_refs;
943 khiter_t pos;
944
945 if (packed_load(ref->owner) < 0)
946 return -1;
947
948 /* maybe the packfile hasn't changed at all, so we don't
949 * have to re-lookup the reference */
950 if ((ref->flags & GIT_REF_PACKED) &&
951 ref->mtime == ref->owner->references.packfile_time)
952 return 0;
953
954 if (ref->flags & GIT_REF_SYMBOLIC) {
955 git__free(ref->target.symbolic);
956 ref->target.symbolic = NULL;
957 }
958
959 /* Look up on the packfile */
960 packfile_refs = ref->owner->references.packfile;
961 pos = git_strmap_lookup_index(packfile_refs, ref->name);
962 if (!git_strmap_valid_index(packfile_refs, pos)) {
963 giterr_set(GITERR_REFERENCE, "Reference '%s' not found", ref->name);
964 return GIT_ENOTFOUND;
965 }
966
967 pack_ref = git_strmap_value_at(packfile_refs, pos);
968
969 ref->flags = GIT_REF_OID | GIT_REF_PACKED;
970 ref->mtime = ref->owner->references.packfile_time;
971 git_oid_cpy(&ref->target.oid, &pack_ref->oid);
972
973 return 0;
974 }
975
976 static int reference_lookup(git_reference *ref)
977 {
978 int result;
979
980 result = loose_lookup(ref);
981 if (result == 0)
982 return 0;
983
984 /* only try to lookup this reference on the packfile if it
985 * wasn't found on the loose refs; not if there was a critical error */
986 if (result == GIT_ENOTFOUND) {
987 giterr_clear();
988 result = packed_lookup(ref);
989 if (result == 0)
990 return 0;
991 }
992
993 /* unexpected error; free the reference */
994 git_reference_free(ref);
995 return result;
996 }
997
998 /*
999 * Delete a reference.
1000 * This is an internal method; the reference is removed
1001 * from disk or the packfile, but the pointer is not freed
1002 */
1003 static int reference_delete(git_reference *ref)
1004 {
1005 int result;
1006
1007 assert(ref);
1008
1009 /* If the reference is packed, this is an expensive operation.
1010 * We need to reload the packfile, remove the reference from the
1011 * packing list, and repack */
1012 if (ref->flags & GIT_REF_PACKED) {
1013 git_strmap *packfile_refs;
1014 struct packref *packref;
1015 khiter_t pos;
1016
1017 /* load the existing packfile */
1018 if (packed_load(ref->owner) < 0)
1019 return -1;
1020
1021 packfile_refs = ref->owner->references.packfile;
1022 pos = git_strmap_lookup_index(packfile_refs, ref->name);
1023 if (!git_strmap_valid_index(packfile_refs, pos)) {
1024 giterr_set(GITERR_REFERENCE,
1025 "Reference %s stopped existing in the packfile", ref->name);
1026 return -1;
1027 }
1028
1029 packref = git_strmap_value_at(packfile_refs, pos);
1030 git_strmap_delete_at(packfile_refs, pos);
1031
1032 git__free(packref);
1033 if (packed_write(ref->owner) < 0)
1034 return -1;
1035
1036 /* If the reference is loose, we can just remove the reference
1037 * from the filesystem */
1038 } else {
1039 git_reference *ref_in_pack;
1040 git_buf full_path = GIT_BUF_INIT;
1041
1042 if (git_buf_joinpath(&full_path, ref->owner->path_repository, ref->name) < 0)
1043 return -1;
1044
1045 result = p_unlink(full_path.ptr);
1046 git_buf_free(&full_path); /* done with path at this point */
1047
1048 if (result < 0) {
1049 giterr_set(GITERR_OS, "Failed to unlink '%s'", full_path.ptr);
1050 return -1;
1051 }
1052
1053 /* When deleting a loose reference, we have to ensure that an older
1054 * packed version of it doesn't exist */
1055 if (git_reference_lookup(&ref_in_pack, ref->owner, ref->name) == 0) {
1056 assert((ref_in_pack->flags & GIT_REF_PACKED) != 0);
1057 return git_reference_delete(ref_in_pack);
1058 }
1059
1060 giterr_clear();
1061 }
1062
1063 return 0;
1064 }
1065
1066 int git_reference_delete(git_reference *ref)
1067 {
1068 int result = reference_delete(ref);
1069 git_reference_free(ref);
1070 return result;
1071 }
1072
1073 int git_reference_lookup(git_reference **ref_out,
1074 git_repository *repo, const char *name)
1075 {
1076 return git_reference_lookup_resolved(ref_out, repo, name, 0);
1077 }
1078
1079 int git_reference_name_to_oid(
1080 git_oid *out, git_repository *repo, const char *name)
1081 {
1082 int error;
1083 git_reference *ref;
1084
1085 if ((error = git_reference_lookup_resolved(&ref, repo, name, -1)) < 0)
1086 return error;
1087
1088 git_oid_cpy(out, git_reference_oid(ref));
1089 git_reference_free(ref);
1090 return 0;
1091 }
1092
1093 int git_reference_lookup_resolved(
1094 git_reference **ref_out,
1095 git_repository *repo,
1096 const char *name,
1097 int max_nesting)
1098 {
1099 git_reference *scan;
1100 int result, nesting;
1101
1102 assert(ref_out && repo && name);
1103
1104 *ref_out = NULL;
1105
1106 if (max_nesting > MAX_NESTING_LEVEL)
1107 max_nesting = MAX_NESTING_LEVEL;
1108 else if (max_nesting < 0)
1109 max_nesting = DEFAULT_NESTING_LEVEL;
1110
1111 scan = git__calloc(1, sizeof(git_reference));
1112 GITERR_CHECK_ALLOC(scan);
1113
1114 scan->name = git__calloc(GIT_REFNAME_MAX + 1, sizeof(char));
1115 GITERR_CHECK_ALLOC(scan->name);
1116
1117 if ((result = normalize_name(scan->name, GIT_REFNAME_MAX, name, 0)) < 0) {
1118 git_reference_free(scan);
1119 return result;
1120 }
1121
1122 scan->target.symbolic = git__strdup(scan->name);
1123 GITERR_CHECK_ALLOC(scan->target.symbolic);
1124
1125 scan->owner = repo;
1126 scan->flags = GIT_REF_SYMBOLIC;
1127
1128 for (nesting = max_nesting;
1129 nesting >= 0 && (scan->flags & GIT_REF_SYMBOLIC) != 0;
1130 nesting--)
1131 {
1132 if (nesting != max_nesting)
1133 strncpy(scan->name, scan->target.symbolic, GIT_REFNAME_MAX);
1134
1135 scan->mtime = 0;
1136
1137 if ((result = reference_lookup(scan)) < 0)
1138 return result; /* lookup git_reference_free on scan already */
1139 }
1140
1141 if ((scan->flags & GIT_REF_OID) == 0 && max_nesting != 0) {
1142 giterr_set(GITERR_REFERENCE,
1143 "Cannot resolve reference (>%u levels deep)", max_nesting);
1144 git_reference_free(scan);
1145 return -1;
1146 }
1147
1148 *ref_out = scan;
1149 return 0;
1150 }
1151
1152 /**
1153 * Getters
1154 */
1155 git_ref_t git_reference_type(git_reference *ref)
1156 {
1157 assert(ref);
1158
1159 if (ref->flags & GIT_REF_OID)
1160 return GIT_REF_OID;
1161
1162 if (ref->flags & GIT_REF_SYMBOLIC)
1163 return GIT_REF_SYMBOLIC;
1164
1165 return GIT_REF_INVALID;
1166 }
1167
1168 int git_reference_is_packed(git_reference *ref)
1169 {
1170 assert(ref);
1171 return !!(ref->flags & GIT_REF_PACKED);
1172 }
1173
1174 const char *git_reference_name(git_reference *ref)
1175 {
1176 assert(ref);
1177 return ref->name;
1178 }
1179
1180 git_repository *git_reference_owner(git_reference *ref)
1181 {
1182 assert(ref);
1183 return ref->owner;
1184 }
1185
1186 const git_oid *git_reference_oid(git_reference *ref)
1187 {
1188 assert(ref);
1189
1190 if ((ref->flags & GIT_REF_OID) == 0)
1191 return NULL;
1192
1193 return &ref->target.oid;
1194 }
1195
1196 const char *git_reference_target(git_reference *ref)
1197 {
1198 assert(ref);
1199
1200 if ((ref->flags & GIT_REF_SYMBOLIC) == 0)
1201 return NULL;
1202
1203 return ref->target.symbolic;
1204 }
1205
1206 int git_reference_create_symbolic(
1207 git_reference **ref_out,
1208 git_repository *repo,
1209 const char *name,
1210 const char *target,
1211 int force)
1212 {
1213 char normalized[GIT_REFNAME_MAX];
1214 git_reference *ref = NULL;
1215
1216 if (normalize_name(normalized, sizeof(normalized), name, 0) < 0)
1217 return -1;
1218
1219 if (reference_can_write(repo, normalized, NULL, force) < 0)
1220 return -1;
1221
1222 if (reference_alloc(&ref, repo, normalized) < 0)
1223 return -1;
1224
1225 ref->flags |= GIT_REF_SYMBOLIC;
1226
1227 /* set the target; this will normalize the name automatically
1228 * and write the reference on disk */
1229 if (git_reference_set_target(ref, target) < 0) {
1230 git_reference_free(ref);
1231 return -1;
1232 }
1233 if (ref_out == NULL) {
1234 git_reference_free(ref);
1235 } else {
1236 *ref_out = ref;
1237 }
1238
1239 return 0;
1240 }
1241
1242 int git_reference_create_oid(
1243 git_reference **ref_out,
1244 git_repository *repo,
1245 const char *name,
1246 const git_oid *id,
1247 int force)
1248 {
1249 git_reference *ref = NULL;
1250 char normalized[GIT_REFNAME_MAX];
1251
1252 if (normalize_name(normalized, sizeof(normalized), name, 1) < 0)
1253 return -1;
1254
1255 if (reference_can_write(repo, normalized, NULL, force) < 0)
1256 return -1;
1257
1258 if (reference_alloc(&ref, repo, name) < 0)
1259 return -1;
1260
1261 ref->flags |= GIT_REF_OID;
1262
1263 /* set the oid; this will write the reference on disk */
1264 if (git_reference_set_oid(ref, id) < 0) {
1265 git_reference_free(ref);
1266 return -1;
1267 }
1268
1269 if (ref_out == NULL) {
1270 git_reference_free(ref);
1271 } else {
1272 *ref_out = ref;
1273 }
1274
1275 return 0;
1276 }
1277 /*
1278 * Change the OID target of a reference.
1279 *
1280 * For both loose and packed references, just change
1281 * the oid in memory and (over)write the file in disk.
1282 *
1283 * We do not repack packed references because of performance
1284 * reasons.
1285 */
1286 int git_reference_set_oid(git_reference *ref, const git_oid *id)
1287 {
1288 git_odb *odb = NULL;
1289
1290 if ((ref->flags & GIT_REF_OID) == 0) {
1291 giterr_set(GITERR_REFERENCE, "Cannot set OID on symbolic reference");
1292 return -1;
1293 }
1294
1295 assert(ref->owner);
1296
1297 if (git_repository_odb__weakptr(&odb, ref->owner) < 0)
1298 return -1;
1299
1300 /* Don't let the user create references to OIDs that
1301 * don't exist in the ODB */
1302 if (!git_odb_exists(odb, id)) {
1303 giterr_set(GITERR_REFERENCE,
1304 "Target OID for the reference doesn't exist on the repository");
1305 return -1;
1306 }
1307
1308 /* Update the OID value on `ref` */
1309 git_oid_cpy(&ref->target.oid, id);
1310
1311 /* Write back to disk */
1312 return loose_write(ref);
1313 }
1314
1315 /*
1316 * Change the target of a symbolic reference.
1317 *
1318 * This is easy because symrefs cannot be inside
1319 * a pack. We just change the target in memory
1320 * and overwrite the file on disk.
1321 */
1322 int git_reference_set_target(git_reference *ref, const char *target)
1323 {
1324 char normalized[GIT_REFNAME_MAX];
1325
1326 if ((ref->flags & GIT_REF_SYMBOLIC) == 0) {
1327 giterr_set(GITERR_REFERENCE,
1328 "Cannot set symbolic target on a direct reference");
1329 return -1;
1330 }
1331
1332 if (normalize_name(normalized, sizeof(normalized), target, 0))
1333 return -1;
1334
1335 git__free(ref->target.symbolic);
1336 ref->target.symbolic = git__strdup(normalized);
1337 GITERR_CHECK_ALLOC(ref->target.symbolic);
1338
1339 return loose_write(ref);
1340 }
1341
1342 int git_reference_rename(git_reference *ref, const char *new_name, int force)
1343 {
1344 int result;
1345 git_buf aux_path = GIT_BUF_INIT;
1346 char normalized[GIT_REFNAME_MAX];
1347
1348 const char *head_target = NULL;
1349 git_reference *head = NULL;
1350
1351 if (normalize_name(normalized, sizeof(normalized),
1352 new_name, ref->flags & GIT_REF_OID) < 0)
1353 return -1;
1354
1355 if (reference_can_write(ref->owner, normalized, ref->name, force) < 0)
1356 return -1;
1357
1358 /* Initialize path now so we won't get an allocation failure once
1359 * we actually start removing things. */
1360 if (git_buf_joinpath(&aux_path, ref->owner->path_repository, new_name) < 0)
1361 return -1;
1362
1363 /*
1364 * Now delete the old ref and remove an possibly existing directory
1365 * named `new_name`. Note that using the internal `reference_delete`
1366 * method deletes the ref from disk but doesn't free the pointer, so
1367 * we can still access the ref's attributes for creating the new one
1368 */
1369 if (reference_delete(ref) < 0)
1370 goto cleanup;
1371
1372 /*
1373 * Finally we can create the new reference.
1374 */
1375 if (ref->flags & GIT_REF_SYMBOLIC) {
1376 result = git_reference_create_symbolic(
1377 NULL, ref->owner, new_name, ref->target.symbolic, force);
1378 } else {
1379 result = git_reference_create_oid(
1380 NULL, ref->owner, new_name, &ref->target.oid, force);
1381 }
1382
1383 if (result < 0)
1384 goto rollback;
1385
1386 /*
1387 * Check if we have to update HEAD.
1388 */
1389 if (git_reference_lookup(&head, ref->owner, GIT_HEAD_FILE) < 0) {
1390 giterr_set(GITERR_REFERENCE,
1391 "Failed to update HEAD after renaming reference");
1392 goto cleanup;
1393 }
1394
1395 head_target = git_reference_target(head);
1396
1397 if (head_target && !strcmp(head_target, ref->name)) {
1398 git_reference_free(head);
1399 head = NULL;
1400
1401 if (git_reference_create_symbolic(&head, ref->owner, "HEAD", new_name, 1) < 0) {
1402 giterr_set(GITERR_REFERENCE,
1403 "Failed to update HEAD after renaming reference");
1404 goto cleanup;
1405 }
1406 }
1407
1408 /*
1409 * Rename the reflog file, if it exists.
1410 */
1411 if ((git_reference_has_log(ref)) && (git_reflog_rename(ref, new_name) < 0))
1412 goto cleanup;
1413
1414 /*
1415 * Change the name of the reference given by the user.
1416 */
1417 git__free(ref->name);
1418 ref->name = git__strdup(new_name);
1419
1420 /* The reference is no longer packed */
1421 ref->flags &= ~GIT_REF_PACKED;
1422
1423 git_reference_free(head);
1424 git_buf_free(&aux_path);
1425 return 0;
1426
1427 cleanup:
1428 git_reference_free(head);
1429 git_buf_free(&aux_path);
1430 return -1;
1431
1432 rollback:
1433 /*
1434 * Try to create the old reference again, ignore failures
1435 */
1436 if (ref->flags & GIT_REF_SYMBOLIC)
1437 git_reference_create_symbolic(
1438 NULL, ref->owner, ref->name, ref->target.symbolic, 0);
1439 else
1440 git_reference_create_oid(
1441 NULL, ref->owner, ref->name, &ref->target.oid, 0);
1442
1443 /* The reference is no longer packed */
1444 ref->flags &= ~GIT_REF_PACKED;
1445
1446 git_buf_free(&aux_path);
1447 return -1;
1448 }
1449
1450 int git_reference_resolve(git_reference **ref_out, git_reference *ref)
1451 {
1452 if (ref->flags & GIT_REF_OID)
1453 return git_reference_lookup(ref_out, ref->owner, ref->name);
1454 else
1455 return git_reference_lookup_resolved(ref_out, ref->owner, ref->target.symbolic, -1);
1456 }
1457
1458 int git_reference_packall(git_repository *repo)
1459 {
1460 if (packed_load(repo) < 0 || /* load the existing packfile */
1461 packed_loadloose(repo) < 0 || /* add all the loose refs */
1462 packed_write(repo) < 0) /* write back to disk */
1463 return -1;
1464
1465 return 0;
1466 }
1467
1468 int git_reference_foreach(
1469 git_repository *repo,
1470 unsigned int list_flags,
1471 int (*callback)(const char *, void *),
1472 void *payload)
1473 {
1474 int result;
1475 struct dirent_list_data data;
1476 git_buf refs_path = GIT_BUF_INIT;
1477
1478 /* list all the packed references first */
1479 if (list_flags & GIT_REF_PACKED) {
1480 const char *ref_name;
1481 void *ref;
1482 GIT_UNUSED(ref);
1483
1484 if (packed_load(repo) < 0)
1485 return -1;
1486
1487 git_strmap_foreach(repo->references.packfile, ref_name, ref, {
1488 if (callback(ref_name, payload))
1489 return GIT_EUSER;
1490 });
1491 }
1492
1493 /* now list the loose references, trying not to
1494 * duplicate the ref names already in the packed-refs file */
1495
1496 data.repo_path_len = strlen(repo->path_repository);
1497 data.list_flags = list_flags;
1498 data.repo = repo;
1499 data.callback = callback;
1500 data.callback_payload = payload;
1501 data.callback_error = 0;
1502
1503 if (git_buf_joinpath(&refs_path, repo->path_repository, GIT_REFS_DIR) < 0)
1504 return -1;
1505
1506 result = git_path_direach(&refs_path, _dirent_loose_listall, &data);
1507
1508 git_buf_free(&refs_path);
1509
1510 return data.callback_error ? GIT_EUSER : result;
1511 }
1512
1513 static int cb__reflist_add(const char *ref, void *data)
1514 {
1515 return git_vector_insert((git_vector *)data, git__strdup(ref));
1516 }
1517
1518 int git_reference_list(
1519 git_strarray *array,
1520 git_repository *repo,
1521 unsigned int list_flags)
1522 {
1523 git_vector ref_list;
1524
1525 assert(array && repo);
1526
1527 array->strings = NULL;
1528 array->count = 0;
1529
1530 if (git_vector_init(&ref_list, 8, NULL) < 0)
1531 return -1;
1532
1533 if (git_reference_foreach(
1534 repo, list_flags, &cb__reflist_add, (void *)&ref_list) < 0) {
1535 git_vector_free(&ref_list);
1536 return -1;
1537 }
1538
1539 array->strings = (char **)ref_list.contents;
1540 array->count = ref_list.length;
1541 return 0;
1542 }
1543
1544 int git_reference_reload(git_reference *ref)
1545 {
1546 return reference_lookup(ref);
1547 }
1548
1549 void git_repository__refcache_free(git_refcache *refs)
1550 {
1551 assert(refs);
1552
1553 if (refs->packfile) {
1554 struct packref *reference;
1555
1556 git_strmap_foreach_value(refs->packfile, reference, {
1557 git__free(reference);
1558 });
1559
1560 git_strmap_free(refs->packfile);
1561 }
1562 }
1563
1564 static int is_valid_ref_char(char ch)
1565 {
1566 if ((unsigned) ch <= ' ')
1567 return 0;
1568
1569 switch (ch) {
1570 case '~':
1571 case '^':
1572 case ':':
1573 case '\\':
1574 case '?':
1575 case '[':
1576 case '*':
1577 return 0;
1578 default:
1579 return 1;
1580 }
1581 }
1582
1583 static int normalize_name(
1584 char *buffer_out,
1585 size_t out_size,
1586 const char *name,
1587 int is_oid_ref)
1588 {
1589 const char *name_end, *buffer_out_start;
1590 const char *current;
1591 int contains_a_slash = 0;
1592
1593 assert(name && buffer_out);
1594
1595 buffer_out_start = buffer_out;
1596 current = name;
1597 name_end = name + strlen(name);
1598
1599 /* Terminating null byte */
1600 out_size--;
1601
1602 /* A refname can not be empty */
1603 if (name_end == name)
1604 goto invalid_name;
1605
1606 /* A refname can not end with a dot or a slash */
1607 if (*(name_end - 1) == '.' || *(name_end - 1) == '/')
1608 goto invalid_name;
1609
1610 while (current < name_end && out_size) {
1611 if (!is_valid_ref_char(*current))
1612 goto invalid_name;
1613
1614 if (buffer_out > buffer_out_start) {
1615 char prev = *(buffer_out - 1);
1616
1617 /* A refname can not start with a dot nor contain a double dot */
1618 if (*current == '.' && ((prev == '.') || (prev == '/')))
1619 goto invalid_name;
1620
1621 /* '@{' is forbidden within a refname */
1622 if (*current == '{' && prev == '@')
1623 goto invalid_name;
1624
1625 /* Prevent multiple slashes from being added to the output */
1626 if (*current == '/' && prev == '/') {
1627 current++;
1628 continue;
1629 }
1630 }
1631
1632 if (*current == '/')
1633 contains_a_slash = 1;
1634
1635 *buffer_out++ = *current++;
1636 out_size--;
1637 }
1638
1639 if (!out_size)
1640 goto invalid_name;
1641
1642 /* Object id refname have to contain at least one slash, except
1643 * for HEAD in a detached state or MERGE_HEAD if we're in the
1644 * middle of a merge */
1645 if (is_oid_ref &&
1646 !contains_a_slash &&
1647 strcmp(name, GIT_HEAD_FILE) != 0 &&
1648 strcmp(name, GIT_MERGE_HEAD_FILE) != 0 &&
1649 strcmp(name, GIT_FETCH_HEAD_FILE) != 0)
1650 goto invalid_name;
1651
1652 /* A refname can not end with ".lock" */
1653 if (!git__suffixcmp(name, GIT_FILELOCK_EXTENSION))
1654 goto invalid_name;
1655
1656 *buffer_out = '\0';
1657
1658 /*
1659 * For object id references, name has to start with refs/. Again,
1660 * we need to allow HEAD to be in a detached state.
1661 */
1662 if (is_oid_ref && !(git__prefixcmp(buffer_out_start, GIT_REFS_DIR) ||
1663 strcmp(buffer_out_start, GIT_HEAD_FILE)))
1664 goto invalid_name;
1665
1666 return 0;
1667
1668 invalid_name:
1669 giterr_set(GITERR_REFERENCE, "The given reference name is not valid");
1670 return -1;
1671 }
1672
1673 int git_reference__normalize_name(
1674 char *buffer_out,
1675 size_t out_size,
1676 const char *name)
1677 {
1678 return normalize_name(buffer_out, out_size, name, 0);
1679 }
1680
1681 int git_reference__normalize_name_oid(
1682 char *buffer_out,
1683 size_t out_size,
1684 const char *name)
1685 {
1686 return normalize_name(buffer_out, out_size, name, 1);
1687 }
1688
1689 #define GIT_REF_TYPEMASK (GIT_REF_OID | GIT_REF_SYMBOLIC)
1690
1691 int git_reference_cmp(git_reference *ref1, git_reference *ref2)
1692 {
1693 assert(ref1 && ref2);
1694
1695 /* let's put symbolic refs before OIDs */
1696 if ((ref1->flags & GIT_REF_TYPEMASK) != (ref2->flags & GIT_REF_TYPEMASK))
1697 return (ref1->flags & GIT_REF_SYMBOLIC) ? -1 : 1;
1698
1699 if (ref1->flags & GIT_REF_SYMBOLIC)
1700 return strcmp(ref1->target.symbolic, ref2->target.symbolic);
1701
1702 return git_oid_cmp(&ref1->target.oid, &ref2->target.oid);
1703 }
1704
1705 /* Update the reference named `ref_name` so it points to `oid` */
1706 int git_reference__update(git_repository *repo, const git_oid *oid, const char *ref_name)
1707 {
1708 git_reference *ref;
1709 int res;
1710
1711 res = git_reference_lookup(&ref, repo, ref_name);
1712
1713 /* If we haven't found the reference at all, we assume we need to create
1714 * a new reference and that's it */
1715 if (res == GIT_ENOTFOUND) {
1716 giterr_clear();
1717 return git_reference_create_oid(NULL, repo, ref_name, oid, 1);
1718 }
1719
1720 if (res < 0)
1721 return -1;
1722
1723 /* If we have found a reference, but it's symbolic, we need to update
1724 * the direct reference it points to */
1725 if (git_reference_type(ref) == GIT_REF_SYMBOLIC) {
1726 git_reference *aux;
1727 const char *sym_target;
1728
1729 /* The target pointed at by this reference */
1730 sym_target = git_reference_target(ref);
1731
1732 /* resolve the reference to the target it points to */
1733 res = git_reference_resolve(&aux, ref);
1734
1735 /*
1736 * if the symbolic reference pointed to an inexisting ref,
1737 * this is means we're creating a new branch, for example.
1738 * We need to create a new direct reference with that name
1739 */
1740 if (res == GIT_ENOTFOUND) {
1741 giterr_clear();
1742 res = git_reference_create_oid(NULL, repo, sym_target, oid, 1);
1743 git_reference_free(ref);
1744 return res;
1745 }
1746
1747 /* free the original symbolic reference now; not before because
1748 * we're using the `sym_target` pointer */
1749 git_reference_free(ref);
1750
1751 if (res < 0)
1752 return -1;
1753
1754 /* store the newly found direct reference in its place */
1755 ref = aux;
1756 }
1757
1758 /* ref is made to point to `oid`: ref is either the original reference,
1759 * or the target of the symbolic reference we've looked up */
1760 res = git_reference_set_oid(ref, oid);
1761 git_reference_free(ref);
1762 return res;
1763 }
1764
1765 struct glob_cb_data {
1766 const char *glob;
1767 int (*callback)(const char *, void *);
1768 void *payload;
1769 };
1770
1771 static int fromglob_cb(const char *reference_name, void *payload)
1772 {
1773 struct glob_cb_data *data = (struct glob_cb_data *)payload;
1774
1775 if (!p_fnmatch(data->glob, reference_name, 0))
1776 return data->callback(reference_name, data->payload);
1777
1778 return 0;
1779 }
1780
1781 int git_reference_foreach_glob(
1782 git_repository *repo,
1783 const char *glob,
1784 unsigned int list_flags,
1785 int (*callback)(
1786 const char *reference_name,
1787 void *payload),
1788 void *payload)
1789 {
1790 struct glob_cb_data data;
1791
1792 assert(repo && glob && callback);
1793
1794 data.glob = glob;
1795 data.callback = callback;
1796 data.payload = payload;
1797
1798 return git_reference_foreach(
1799 repo, list_flags, fromglob_cb, &data);
1800 }
1801
1802 int git_reference_has_log(
1803 git_reference *ref)
1804 {
1805 git_buf path = GIT_BUF_INIT;
1806 int result;
1807
1808 assert(ref);
1809
1810 if (git_buf_join_n(&path, '/', 3, ref->owner->path_repository, GIT_REFLOG_DIR, ref->name) < 0)
1811 return -1;
1812
1813 result = git_path_isfile(git_buf_cstr(&path));
1814 git_buf_free(&path);
1815
1816 return result;
1817 }
1818
1819 int git_reference_is_branch(git_reference *ref)
1820 {
1821 assert(ref);
1822
1823 return git__prefixcmp(ref->name, GIT_REFS_HEADS_DIR) == 0;
1824 }