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