2 * Copyright (C) 2009-2012 the libgit2 contributors
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.
10 #include "repository.h"
16 #include <git2/object.h>
21 #define DEFAULT_NESTING_LEVEL 5
22 #define MAX_NESTING_LEVEL 10
25 GIT_PACKREF_HAS_PEEL
= 1,
26 GIT_PACKREF_WAS_LOOSE
= 2
33 char name
[GIT_FLEX_ARRAY
];
36 static int reference_read(
37 git_buf
*file_content
,
39 const char *repo_path
,
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
);
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
);
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
);
71 void git_reference_free(git_reference
*reference
)
73 if (reference
== NULL
)
76 git__free(reference
->name
);
77 reference
->name
= NULL
;
79 if (reference
->flags
& GIT_REF_SYMBOLIC
) {
80 git__free(reference
->target
.symbolic
);
81 reference
->target
.symbolic
= NULL
;
87 static int reference_alloc(
88 git_reference
**ref_out
,
92 git_reference
*reference
= NULL
;
94 assert(ref_out
&& repo
&& name
);
96 reference
= git__malloc(sizeof(git_reference
));
97 GITERR_CHECK_ALLOC(reference
);
99 memset(reference
, 0x0, sizeof(git_reference
));
100 reference
->owner
= repo
;
102 reference
->name
= git__strdup(name
);
103 GITERR_CHECK_ALLOC(reference
->name
);
105 *ref_out
= reference
;
109 static int reference_read(
110 git_buf
*file_content
,
112 const char *repo_path
,
113 const char *ref_name
,
116 git_buf path
= GIT_BUF_INIT
;
119 assert(file_content
&& repo_path
&& ref_name
);
121 /* Determine the full path of the file */
122 if (git_buf_joinpath(&path
, repo_path
, ref_name
) < 0)
125 result
= git_futils_readbuffer_updated(file_content
, path
.ptr
, mtime
, updated
);
131 static int loose_parse_symbolic(git_reference
*ref
, git_buf
*file_content
)
133 const unsigned int header_len
= (unsigned int)strlen(GIT_SYMREF
);
134 const char *refname_start
;
136 refname_start
= (const char *)file_content
->ptr
;
138 if (git_buf_len(file_content
) < header_len
+ 1) {
139 giterr_set(GITERR_REFERENCE
, "Corrupted loose reference file");
144 * Assume we have already checked for the header
145 * before calling this function
147 refname_start
+= header_len
;
149 ref
->target
.symbolic
= git__strdup(refname_start
);
150 GITERR_CHECK_ALLOC(ref
->target
.symbolic
);
155 static int loose_parse_oid(git_oid
*oid
, git_buf
*file_content
)
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)
162 giterr_set(GITERR_REFERENCE
, "Corrupted loose reference file");
166 static git_ref_t
loose_guess_rtype(const git_buf
*full_path
)
168 git_buf ref_file
= GIT_BUF_INIT
;
171 type
= GIT_REF_INVALID
;
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
;
180 git_buf_free(&ref_file
);
184 static int loose_lookup(git_reference
*ref
)
187 git_buf ref_file
= GIT_BUF_INIT
;
189 result
= reference_read(&ref_file
, &ref
->mtime
,
190 ref
->owner
->path_repository
, ref
->name
, &updated
);
198 git_buf_rtrim(&ref_file
);
200 if (ref
->flags
& GIT_REF_SYMBOLIC
) {
201 git__free(ref
->target
.symbolic
);
202 ref
->target
.symbolic
= NULL
;
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
);
211 ref
->flags
|= GIT_REF_OID
;
212 result
= loose_parse_oid(&ref
->target
.oid
, &ref_file
);
215 git_buf_free(&ref_file
);
219 static int loose_lookup_to_packfile(
220 struct packref
**ref_out
,
221 git_repository
*repo
,
224 git_buf ref_file
= GIT_BUF_INIT
;
225 struct packref
*ref
= NULL
;
230 if (reference_read(&ref_file
, NULL
, repo
->path_repository
, name
, NULL
) < 0)
233 git_buf_rtrim(&ref_file
);
235 name_len
= strlen(name
);
236 ref
= git__malloc(sizeof(struct packref
) + name_len
+ 1);
237 GITERR_CHECK_ALLOC(ref
);
239 memcpy(ref
->name
, name
, name_len
);
240 ref
->name
[name_len
] = 0;
242 if (loose_parse_oid(&ref
->oid
, &ref_file
) < 0) {
243 git_buf_free(&ref_file
);
248 ref
->flags
= GIT_PACKREF_WAS_LOOSE
;
251 git_buf_free(&ref_file
);
255 static int loose_write(git_reference
*ref
)
257 git_filebuf file
= GIT_FILEBUF_INIT
;
258 git_buf ref_path
= GIT_BUF_INIT
;
261 if (git_buf_joinpath(&ref_path
, ref
->owner
->path_repository
, ref
->name
) < 0)
264 /* Remove a possibly existing empty directory hierarchy
265 * which name would collide with the reference name
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
);
273 if (git_filebuf_open(&file
, ref_path
.ptr
, GIT_FILEBUF_FORCE
) < 0) {
274 git_buf_free(&ref_path
);
278 git_buf_free(&ref_path
);
280 if (ref
->flags
& GIT_REF_OID
) {
281 char oid
[GIT_OID_HEXSZ
+ 1];
283 git_oid_fmt(oid
, &ref
->target
.oid
);
284 oid
[GIT_OID_HEXSZ
] = '\0';
286 git_filebuf_printf(&file
, "%s\n", oid
);
288 } else if (ref
->flags
& GIT_REF_SYMBOLIC
) {
289 git_filebuf_printf(&file
, GIT_SYMREF
"%s\n", ref
->target
.symbolic
);
291 assert(0); /* don't let this happen */
294 if (p_stat(ref_path
.ptr
, &st
) == 0)
295 ref
->mtime
= st
.st_mtime
;
297 return git_filebuf_commit(&file
, GIT_REFS_FILE_MODE
);
300 static int packed_parse_peel(
301 struct packref
*tag_ref
,
302 const char **buffer_out
,
303 const char *buffer_end
)
305 const char *buffer
= *buffer_out
+ 1;
307 assert(buffer
[-1] == '^');
309 /* Ensure it's not the first entry of the file */
313 /* Ensure reference is a tag */
314 if (git__prefixcmp(tag_ref
->name
, GIT_REFS_TAGS_DIR
) != 0)
317 if (buffer
+ GIT_OID_HEXSZ
>= buffer_end
)
320 /* Is this a valid object id? */
321 if (git_oid_fromstr(&tag_ref
->peel
, buffer
) < 0)
324 buffer
= buffer
+ GIT_OID_HEXSZ
;
331 *buffer_out
= buffer
+ 1;
335 giterr_set(GITERR_REFERENCE
, "The packed references file is corrupted");
339 static int packed_parse_oid(
340 struct packref
**ref_out
,
341 const char **buffer_out
,
342 const char *buffer_end
)
344 struct packref
*ref
= NULL
;
346 const char *buffer
= *buffer_out
;
347 const char *refname_begin
, *refname_end
;
352 refname_begin
= (buffer
+ GIT_OID_HEXSZ
+ 1);
353 if (refname_begin
>= buffer_end
|| refname_begin
[-1] != ' ')
356 /* Is this a valid object id? */
357 if (git_oid_fromstr(&id
, buffer
) < 0)
360 refname_end
= memchr(refname_begin
, '\n', buffer_end
- refname_begin
);
361 if (refname_end
== NULL
)
364 if (refname_end
[-1] == '\r')
367 refname_len
= refname_end
- refname_begin
;
369 ref
= git__malloc(sizeof(struct packref
) + refname_len
+ 1);
370 GITERR_CHECK_ALLOC(ref
);
372 memcpy(ref
->name
, refname_begin
, refname_len
);
373 ref
->name
[refname_len
] = 0;
375 git_oid_cpy(&ref
->oid
, &id
);
380 *buffer_out
= refname_end
+ 1;
386 giterr_set(GITERR_REFERENCE
, "The packed references file is corrupted");
390 static int packed_load(git_repository
*repo
)
393 git_buf packfile
= GIT_BUF_INIT
;
394 const char *buffer_start
, *buffer_end
;
395 git_refcache
*ref_cache
= &repo
->references
;
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
);
403 result
= reference_read(&packfile
, &ref_cache
->packfile_time
,
404 repo
->path_repository
, GIT_PACKEDREFS_FILE
, &updated
);
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.
413 if (result
== GIT_ENOTFOUND
) {
414 git_strmap_clear(ref_cache
->packfile
);
425 * At this point, we want to refresh the packed refs. We already
426 * have the contents in our buffer.
428 git_strmap_clear(ref_cache
->packfile
);
430 buffer_start
= (const char *)packfile
.ptr
;
431 buffer_end
= (const char *)(buffer_start
) + packfile
.size
;
433 while (buffer_start
< buffer_end
&& buffer_start
[0] == '#') {
434 buffer_start
= strchr(buffer_start
, '\n');
435 if (buffer_start
== NULL
)
441 while (buffer_start
< buffer_end
) {
443 struct packref
*ref
= NULL
;
445 if (packed_parse_oid(&ref
, &buffer_start
, buffer_end
) < 0)
448 if (buffer_start
[0] == '^') {
449 if (packed_parse_peel(ref
, &buffer_start
, buffer_end
) < 0)
453 git_strmap_insert(ref_cache
->packfile
, ref
->name
, ref
, err
);
458 git_buf_free(&packfile
);
462 git_strmap_free(ref_cache
->packfile
);
463 ref_cache
->packfile
= NULL
;
464 git_buf_free(&packfile
);
469 struct dirent_list_data
{
470 git_repository
*repo
;
471 size_t repo_path_len
;
472 unsigned int list_flags
;
474 int (*callback
)(const char *, void *);
475 void *callback_payload
;
479 static int _dirent_loose_listall(void *_data
, git_buf
*full_path
)
481 struct dirent_list_data
*data
= (struct dirent_list_data
*)_data
;
482 const char *file_path
= full_path
->ptr
+ data
->repo_path_len
;
484 if (git_path_isdir(full_path
->ptr
) == true)
485 return git_path_direach(full_path
, _dirent_loose_listall
, _data
);
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
))
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 */
497 /* Locked references aren't returned */
498 if (!git__suffixcmp(file_path
, GIT_FILELOCK_EXTENSION
))
501 if (data
->callback(file_path
, data
->callback_payload
))
502 data
->callback_error
= GIT_EUSER
;
504 return data
->callback_error
;
507 static int _dirent_loose_load(void *data
, git_buf
*full_path
)
509 git_repository
*repository
= (git_repository
*)data
;
510 void *old_ref
= NULL
;
512 const char *file_path
;
515 if (git_path_isdir(full_path
->ptr
) == true)
516 return git_path_direach(full_path
, _dirent_loose_load
, repository
);
518 file_path
= full_path
->ptr
+ strlen(repository
->path_repository
);
520 if (loose_lookup_to_packfile(&ref
, repository
, file_path
) < 0)
524 repository
->references
.packfile
, ref
->name
, ref
, old_ref
, err
);
535 * Load all the loose references from the repository
536 * into the in-memory Packfile, and build a vector with
537 * all the references so it can be written back to
540 static int packed_loadloose(git_repository
*repository
)
542 git_buf refs_path
= GIT_BUF_INIT
;
545 /* the packfile must have been previously loaded! */
546 assert(repository
->references
.packfile
);
548 if (git_buf_joinpath(&refs_path
, repository
->path_repository
, GIT_REFS_DIR
) < 0)
552 * Load all the loose files from disk into the Packfile table.
553 * This will overwrite any old packed entries with their
554 * updated loose versions
556 result
= git_path_direach(&refs_path
, _dirent_loose_load
, repository
);
557 git_buf_free(&refs_path
);
563 * Write a single reference into a packfile
565 static int packed_write_ref(struct packref
*ref
, git_filebuf
*file
)
567 char oid
[GIT_OID_HEXSZ
+ 1];
569 git_oid_fmt(oid
, &ref
->oid
);
570 oid
[GIT_OID_HEXSZ
] = 0;
573 * For references that peel to an object in the repo, we must
574 * write the resulting peel on a separate line, e.g.
576 * 6fa8a902cc1d18527e1355773c86721945475d37 refs/tags/libgit2-0.4
577 * ^2ec0cb7959b0bf965d54f95453f5b4b34e8d3100
579 * This obviously only applies to tags.
580 * The required peels have already been loaded into `ref->peel_target`.
582 if (ref
->flags
& GIT_PACKREF_HAS_PEEL
) {
583 char peel
[GIT_OID_HEXSZ
+ 1];
584 git_oid_fmt(peel
, &ref
->peel
);
585 peel
[GIT_OID_HEXSZ
] = 0;
587 if (git_filebuf_printf(file
, "%s %s\n^%s\n", oid
, ref
->name
, peel
) < 0)
590 if (git_filebuf_printf(file
, "%s %s\n", oid
, ref
->name
) < 0)
598 * Find out what object this reference resolves to.
600 * For references that point to a 'big' tag (e.g. an
601 * actual tag object on the repository), we need to
602 * cache on the packfile the OID of the object to
603 * which that 'big tag' is pointing to.
605 static int packed_find_peel(git_repository
*repo
, struct packref
*ref
)
609 if (ref
->flags
& GIT_PACKREF_HAS_PEEL
)
613 * Only applies to tags, i.e. references
614 * in the /refs/tags folder
616 if (git__prefixcmp(ref
->name
, GIT_REFS_TAGS_DIR
) != 0)
620 * Find the tagged object in the repository
622 if (git_object_lookup(&object
, repo
, &ref
->oid
, GIT_OBJ_ANY
) < 0)
626 * If the tagged object is a Tag object, we need to resolve it;
627 * if the ref is actually a 'weak' ref, we don't need to resolve
630 if (git_object_type(object
) == GIT_OBJ_TAG
) {
631 git_tag
*tag
= (git_tag
*)object
;
634 * Find the object pointed at by this tag
636 git_oid_cpy(&ref
->peel
, git_tag_target_oid(tag
));
637 ref
->flags
|= GIT_PACKREF_HAS_PEEL
;
640 * The reference has now cached the resolved OID, and is
641 * marked at such. When written to the packfile, it'll be
642 * accompanied by this resolved oid
646 git_object_free(object
);
651 * Remove all loose references
653 * Once we have successfully written a packfile,
654 * all the loose references that were packed must be
657 * This is a dangerous method; make sure the packfile
658 * is well-written, because we are destructing references
661 static int packed_remove_loose(git_repository
*repo
, git_vector
*packing_list
)
664 git_buf full_path
= GIT_BUF_INIT
;
667 for (i
= 0; i
< packing_list
->length
; ++i
) {
668 struct packref
*ref
= git_vector_get(packing_list
, i
);
670 if ((ref
->flags
& GIT_PACKREF_WAS_LOOSE
) == 0)
673 if (git_buf_joinpath(&full_path
, repo
->path_repository
, ref
->name
) < 0)
674 return -1; /* critical; do not try to recover on oom */
676 if (git_path_exists(full_path
.ptr
) == true && p_unlink(full_path
.ptr
) < 0) {
680 giterr_set(GITERR_REFERENCE
,
681 "Failed to remove loose reference '%s' after packing: %s",
682 full_path
.ptr
, strerror(errno
));
688 * if we fail to remove a single file, this is *not* good,
689 * but we should keep going and remove as many as possible.
690 * After we've removed as many files as possible, we return
691 * the error code anyway.
695 git_buf_free(&full_path
);
696 return failed
? -1 : 0;
699 static int packed_sort(const void *a
, const void *b
)
701 const struct packref
*ref_a
= (const struct packref
*)a
;
702 const struct packref
*ref_b
= (const struct packref
*)b
;
704 return strcmp(ref_a
->name
, ref_b
->name
);
708 * Write all the contents in the in-memory packfile to disk.
710 static int packed_write(git_repository
*repo
)
712 git_filebuf pack_file
= GIT_FILEBUF_INIT
;
714 git_buf pack_file_path
= GIT_BUF_INIT
;
715 git_vector packing_list
;
716 unsigned int total_refs
;
718 assert(repo
&& repo
->references
.packfile
);
721 (unsigned int)git_strmap_num_entries(repo
->references
.packfile
);
723 if (git_vector_init(&packing_list
, total_refs
, packed_sort
) < 0)
726 /* Load all the packfile into a vector */
728 struct packref
*reference
;
730 /* cannot fail: vector already has the right size */
731 git_strmap_foreach_value(repo
->references
.packfile
, reference
, {
732 git_vector_insert(&packing_list
, reference
);
736 /* sort the vector so the entries appear sorted on the packfile */
737 git_vector_sort(&packing_list
);
739 /* Now we can open the file! */
740 if (git_buf_joinpath(&pack_file_path
, repo
->path_repository
, GIT_PACKEDREFS_FILE
) < 0)
743 if (git_filebuf_open(&pack_file
, pack_file_path
.ptr
, 0) < 0)
744 goto cleanup_packfile
;
746 /* Packfiles have a header... apparently
747 * This is in fact not required, but we might as well print it
749 if (git_filebuf_printf(&pack_file
, "%s\n", GIT_PACKEDREFS_HEADER
) < 0)
750 goto cleanup_packfile
;
752 for (i
= 0; i
< packing_list
.length
; ++i
) {
753 struct packref
*ref
= (struct packref
*)git_vector_get(&packing_list
, i
);
755 if (packed_find_peel(repo
, ref
) < 0)
756 goto cleanup_packfile
;
758 if (packed_write_ref(ref
, &pack_file
) < 0)
759 goto cleanup_packfile
;
762 /* if we've written all the references properly, we can commit
763 * the packfile to make the changes effective */
764 if (git_filebuf_commit(&pack_file
, GIT_PACKEDREFS_FILE_MODE
) < 0)
767 /* when and only when the packfile has been properly written,
768 * we can go ahead and remove the loose refs */
769 if (packed_remove_loose(repo
, &packing_list
) < 0)
774 if (p_stat(pack_file_path
.ptr
, &st
) == 0)
775 repo
->references
.packfile_time
= st
.st_mtime
;
778 git_vector_free(&packing_list
);
779 git_buf_free(&pack_file_path
);
785 git_filebuf_cleanup(&pack_file
);
788 git_vector_free(&packing_list
);
789 git_buf_free(&pack_file_path
);
794 struct reference_available_t
{
800 static int _reference_available_cb(const char *ref
, void *data
)
802 struct reference_available_t
*d
;
805 d
= (struct reference_available_t
*)data
;
807 if (!d
->old_ref
|| strcmp(d
->old_ref
, ref
)) {
808 size_t reflen
= strlen(ref
);
809 size_t newlen
= strlen(d
->new_ref
);
810 size_t cmplen
= reflen
< newlen
? reflen
: newlen
;
811 const char *lead
= reflen
< newlen
? d
->new_ref
: ref
;
813 if (!strncmp(d
->new_ref
, ref
, cmplen
) && lead
[cmplen
] == '/') {
822 static int reference_path_available(
823 git_repository
*repo
,
828 struct reference_available_t data
;
831 data
.old_ref
= old_ref
;
834 error
= git_reference_foreach(
835 repo
, GIT_REF_LISTALL
, _reference_available_cb
, (void *)&data
);
839 if (!data
.available
) {
840 giterr_set(GITERR_REFERENCE
,
841 "The path to reference '%s' collides with an existing one", ref
);
848 static int reference_exists(int *exists
, git_repository
*repo
, const char *ref_name
)
850 git_buf ref_path
= GIT_BUF_INIT
;
852 if (packed_load(repo
) < 0)
855 if (git_buf_joinpath(&ref_path
, repo
->path_repository
, ref_name
) < 0)
858 if (git_path_isfile(ref_path
.ptr
) == true ||
859 git_strmap_exists(repo
->references
.packfile
, ref_path
.ptr
))
866 git_buf_free(&ref_path
);
871 * Check if a reference could be written to disk, based on:
873 * - Whether a reference with the same name already exists,
874 * and we are allowing or disallowing overwrites
876 * - Whether the name of the reference would collide with
879 static int reference_can_write(
880 git_repository
*repo
,
882 const char *previous_name
,
885 /* see if the reference shares a path with an existing reference;
886 * if a path is shared, we cannot create the reference, even when forcing */
887 if (reference_path_available(repo
, refname
, previous_name
) < 0)
890 /* check if the reference actually exists, but only if we are not forcing
891 * the rename. If we are forcing, it's OK to overwrite */
895 if (reference_exists(&exists
, repo
, refname
) < 0)
898 /* We cannot proceed if the reference already exists and we're not forcing
899 * the rename; the existing one would be overwritten */
901 giterr_set(GITERR_REFERENCE
,
902 "A reference with that name (%s) already exists", refname
);
907 /* FIXME: if the reference exists and we are forcing, do we really need to
908 * remove the reference first?
912 * - the reference already exists and is loose: not a problem, the file
913 * gets overwritten on disk
915 * - the reference already exists and is packed: we write a new one as
916 * loose, which by all means renders the packed one useless
923 static int packed_lookup(git_reference
*ref
)
925 struct packref
*pack_ref
= NULL
;
926 git_strmap
*packfile_refs
;
929 if (packed_load(ref
->owner
) < 0)
932 /* maybe the packfile hasn't changed at all, so we don't
933 * have to re-lookup the reference */
934 if ((ref
->flags
& GIT_REF_PACKED
) &&
935 ref
->mtime
== ref
->owner
->references
.packfile_time
)
938 if (ref
->flags
& GIT_REF_SYMBOLIC
) {
939 git__free(ref
->target
.symbolic
);
940 ref
->target
.symbolic
= NULL
;
943 /* Look up on the packfile */
944 packfile_refs
= ref
->owner
->references
.packfile
;
945 pos
= git_strmap_lookup_index(packfile_refs
, ref
->name
);
946 if (!git_strmap_valid_index(packfile_refs
, pos
)) {
947 giterr_set(GITERR_REFERENCE
, "Reference '%s' not found", ref
->name
);
948 return GIT_ENOTFOUND
;
951 pack_ref
= git_strmap_value_at(packfile_refs
, pos
);
953 ref
->flags
= GIT_REF_OID
| GIT_REF_PACKED
;
954 ref
->mtime
= ref
->owner
->references
.packfile_time
;
955 git_oid_cpy(&ref
->target
.oid
, &pack_ref
->oid
);
960 static int reference_lookup(git_reference
*ref
)
964 result
= loose_lookup(ref
);
968 /* only try to lookup this reference on the packfile if it
969 * wasn't found on the loose refs; not if there was a critical error */
970 if (result
== GIT_ENOTFOUND
) {
972 result
= packed_lookup(ref
);
977 /* unexpected error; free the reference */
978 git_reference_free(ref
);
983 * Delete a reference.
984 * This is an internal method; the reference is removed
985 * from disk or the packfile, but the pointer is not freed
987 static int reference_delete(git_reference
*ref
)
993 /* If the reference is packed, this is an expensive operation.
994 * We need to reload the packfile, remove the reference from the
995 * packing list, and repack */
996 if (ref
->flags
& GIT_REF_PACKED
) {
997 git_strmap
*packfile_refs
;
998 struct packref
*packref
;
1001 /* load the existing packfile */
1002 if (packed_load(ref
->owner
) < 0)
1005 packfile_refs
= ref
->owner
->references
.packfile
;
1006 pos
= git_strmap_lookup_index(packfile_refs
, ref
->name
);
1007 if (!git_strmap_valid_index(packfile_refs
, pos
)) {
1008 giterr_set(GITERR_REFERENCE
,
1009 "Reference %s stopped existing in the packfile", ref
->name
);
1013 packref
= git_strmap_value_at(packfile_refs
, pos
);
1014 git_strmap_delete_at(packfile_refs
, pos
);
1017 if (packed_write(ref
->owner
) < 0)
1020 /* If the reference is loose, we can just remove the reference
1021 * from the filesystem */
1023 git_reference
*ref_in_pack
;
1024 git_buf full_path
= GIT_BUF_INIT
;
1026 if (git_buf_joinpath(&full_path
, ref
->owner
->path_repository
, ref
->name
) < 0)
1029 result
= p_unlink(full_path
.ptr
);
1030 git_buf_free(&full_path
); /* done with path at this point */
1033 giterr_set(GITERR_OS
, "Failed to unlink '%s'", full_path
.ptr
);
1037 /* When deleting a loose reference, we have to ensure that an older
1038 * packed version of it doesn't exist */
1039 if (git_reference_lookup(&ref_in_pack
, ref
->owner
, ref
->name
) == 0) {
1040 assert((ref_in_pack
->flags
& GIT_REF_PACKED
) != 0);
1041 return git_reference_delete(ref_in_pack
);
1050 int git_reference_delete(git_reference
*ref
)
1052 int result
= reference_delete(ref
);
1053 git_reference_free(ref
);
1057 int git_reference_lookup(git_reference
**ref_out
,
1058 git_repository
*repo
, const char *name
)
1060 return git_reference_lookup_resolved(ref_out
, repo
, name
, 0);
1063 int git_reference_name_to_oid(
1064 git_oid
*out
, git_repository
*repo
, const char *name
)
1069 if ((error
= git_reference_lookup_resolved(&ref
, repo
, name
, -1)) < 0)
1072 git_oid_cpy(out
, git_reference_oid(ref
));
1073 git_reference_free(ref
);
1077 int git_reference_lookup_resolved(
1078 git_reference
**ref_out
,
1079 git_repository
*repo
,
1083 git_reference
*scan
;
1084 int result
, nesting
;
1086 assert(ref_out
&& repo
&& name
);
1090 if (max_nesting
> MAX_NESTING_LEVEL
)
1091 max_nesting
= MAX_NESTING_LEVEL
;
1092 else if (max_nesting
< 0)
1093 max_nesting
= DEFAULT_NESTING_LEVEL
;
1095 scan
= git__calloc(1, sizeof(git_reference
));
1096 GITERR_CHECK_ALLOC(scan
);
1098 scan
->name
= git__calloc(GIT_REFNAME_MAX
+ 1, sizeof(char));
1099 GITERR_CHECK_ALLOC(scan
->name
);
1101 if ((result
= git_reference__normalize_name_lax(
1105 git_reference_free(scan
);
1109 scan
->target
.symbolic
= git__strdup(scan
->name
);
1110 GITERR_CHECK_ALLOC(scan
->target
.symbolic
);
1113 scan
->flags
= GIT_REF_SYMBOLIC
;
1115 for (nesting
= max_nesting
;
1116 nesting
>= 0 && (scan
->flags
& GIT_REF_SYMBOLIC
) != 0;
1119 if (nesting
!= max_nesting
)
1120 strncpy(scan
->name
, scan
->target
.symbolic
, GIT_REFNAME_MAX
);
1124 if ((result
= reference_lookup(scan
)) < 0)
1125 return result
; /* lookup git_reference_free on scan already */
1128 if ((scan
->flags
& GIT_REF_OID
) == 0 && max_nesting
!= 0) {
1129 giterr_set(GITERR_REFERENCE
,
1130 "Cannot resolve reference (>%u levels deep)", max_nesting
);
1131 git_reference_free(scan
);
1142 git_ref_t
git_reference_type(git_reference
*ref
)
1146 if (ref
->flags
& GIT_REF_OID
)
1149 if (ref
->flags
& GIT_REF_SYMBOLIC
)
1150 return GIT_REF_SYMBOLIC
;
1152 return GIT_REF_INVALID
;
1155 int git_reference_is_packed(git_reference
*ref
)
1158 return !!(ref
->flags
& GIT_REF_PACKED
);
1161 const char *git_reference_name(git_reference
*ref
)
1167 git_repository
*git_reference_owner(git_reference
*ref
)
1173 const git_oid
*git_reference_oid(git_reference
*ref
)
1177 if ((ref
->flags
& GIT_REF_OID
) == 0)
1180 return &ref
->target
.oid
;
1183 const char *git_reference_target(git_reference
*ref
)
1187 if ((ref
->flags
& GIT_REF_SYMBOLIC
) == 0)
1190 return ref
->target
.symbolic
;
1193 int git_reference_create_symbolic(
1194 git_reference
**ref_out
,
1195 git_repository
*repo
,
1200 char normalized
[GIT_REFNAME_MAX
];
1201 git_reference
*ref
= NULL
;
1203 if (git_reference__normalize_name_lax(
1209 if (reference_can_write(repo
, normalized
, NULL
, force
) < 0)
1212 if (reference_alloc(&ref
, repo
, normalized
) < 0)
1215 ref
->flags
|= GIT_REF_SYMBOLIC
;
1217 /* set the target; this will normalize the name automatically
1218 * and write the reference on disk */
1219 if (git_reference_set_target(ref
, target
) < 0) {
1220 git_reference_free(ref
);
1223 if (ref_out
== NULL
) {
1224 git_reference_free(ref
);
1232 int git_reference_create_oid(
1233 git_reference
**ref_out
,
1234 git_repository
*repo
,
1239 git_reference
*ref
= NULL
;
1240 char normalized
[GIT_REFNAME_MAX
];
1242 if (git_reference__normalize_name_lax(
1248 if (reference_can_write(repo
, normalized
, NULL
, force
) < 0)
1251 if (reference_alloc(&ref
, repo
, name
) < 0)
1254 ref
->flags
|= GIT_REF_OID
;
1256 /* set the oid; this will write the reference on disk */
1257 if (git_reference_set_oid(ref
, id
) < 0) {
1258 git_reference_free(ref
);
1262 if (ref_out
== NULL
) {
1263 git_reference_free(ref
);
1271 * Change the OID target of a reference.
1273 * For both loose and packed references, just change
1274 * the oid in memory and (over)write the file in disk.
1276 * We do not repack packed references because of performance
1279 int git_reference_set_oid(git_reference
*ref
, const git_oid
*id
)
1281 git_odb
*odb
= NULL
;
1283 if ((ref
->flags
& GIT_REF_OID
) == 0) {
1284 giterr_set(GITERR_REFERENCE
, "Cannot set OID on symbolic reference");
1290 if (git_repository_odb__weakptr(&odb
, ref
->owner
) < 0)
1293 /* Don't let the user create references to OIDs that
1294 * don't exist in the ODB */
1295 if (!git_odb_exists(odb
, id
)) {
1296 giterr_set(GITERR_REFERENCE
,
1297 "Target OID for the reference doesn't exist on the repository");
1301 /* Update the OID value on `ref` */
1302 git_oid_cpy(&ref
->target
.oid
, id
);
1304 /* Write back to disk */
1305 return loose_write(ref
);
1309 * Change the target of a symbolic reference.
1311 * This is easy because symrefs cannot be inside
1312 * a pack. We just change the target in memory
1313 * and overwrite the file on disk.
1315 int git_reference_set_target(git_reference
*ref
, const char *target
)
1317 char normalized
[GIT_REFNAME_MAX
];
1319 if ((ref
->flags
& GIT_REF_SYMBOLIC
) == 0) {
1320 giterr_set(GITERR_REFERENCE
,
1321 "Cannot set symbolic target on a direct reference");
1325 if (git_reference__normalize_name_lax(
1331 git__free(ref
->target
.symbolic
);
1332 ref
->target
.symbolic
= git__strdup(normalized
);
1333 GITERR_CHECK_ALLOC(ref
->target
.symbolic
);
1335 return loose_write(ref
);
1338 int git_reference_rename(git_reference
*ref
, const char *new_name
, int force
)
1341 unsigned int normalization_flags
;
1342 git_buf aux_path
= GIT_BUF_INIT
;
1343 char normalized
[GIT_REFNAME_MAX
];
1345 const char *head_target
= NULL
;
1346 git_reference
*head
= NULL
;
1348 normalization_flags
= ref
->flags
& GIT_REF_SYMBOLIC
?
1349 GIT_REF_FORMAT_ALLOW_ONELEVEL
1350 : GIT_REF_FORMAT_NORMAL
;
1352 if (git_reference_normalize_name(
1356 normalization_flags
) < 0)
1359 if (reference_can_write(ref
->owner
, normalized
, ref
->name
, force
) < 0)
1362 /* Initialize path now so we won't get an allocation failure once
1363 * we actually start removing things. */
1364 if (git_buf_joinpath(&aux_path
, ref
->owner
->path_repository
, new_name
) < 0)
1368 * Now delete the old ref and remove an possibly existing directory
1369 * named `new_name`. Note that using the internal `reference_delete`
1370 * method deletes the ref from disk but doesn't free the pointer, so
1371 * we can still access the ref's attributes for creating the new one
1373 if (reference_delete(ref
) < 0)
1377 * Finally we can create the new reference.
1379 if (ref
->flags
& GIT_REF_SYMBOLIC
) {
1380 result
= git_reference_create_symbolic(
1381 NULL
, ref
->owner
, new_name
, ref
->target
.symbolic
, force
);
1383 result
= git_reference_create_oid(
1384 NULL
, ref
->owner
, new_name
, &ref
->target
.oid
, force
);
1391 * Check if we have to update HEAD.
1393 if (git_reference_lookup(&head
, ref
->owner
, GIT_HEAD_FILE
) < 0) {
1394 giterr_set(GITERR_REFERENCE
,
1395 "Failed to update HEAD after renaming reference");
1399 head_target
= git_reference_target(head
);
1401 if (head_target
&& !strcmp(head_target
, ref
->name
)) {
1402 git_reference_free(head
);
1405 if (git_reference_create_symbolic(&head
, ref
->owner
, "HEAD", new_name
, 1) < 0) {
1406 giterr_set(GITERR_REFERENCE
,
1407 "Failed to update HEAD after renaming reference");
1413 * Rename the reflog file, if it exists.
1415 if ((git_reference_has_log(ref
)) && (git_reflog_rename(ref
, new_name
) < 0))
1419 * Change the name of the reference given by the user.
1421 git__free(ref
->name
);
1422 ref
->name
= git__strdup(new_name
);
1424 /* The reference is no longer packed */
1425 ref
->flags
&= ~GIT_REF_PACKED
;
1427 git_reference_free(head
);
1428 git_buf_free(&aux_path
);
1432 git_reference_free(head
);
1433 git_buf_free(&aux_path
);
1438 * Try to create the old reference again, ignore failures
1440 if (ref
->flags
& GIT_REF_SYMBOLIC
)
1441 git_reference_create_symbolic(
1442 NULL
, ref
->owner
, ref
->name
, ref
->target
.symbolic
, 0);
1444 git_reference_create_oid(
1445 NULL
, ref
->owner
, ref
->name
, &ref
->target
.oid
, 0);
1447 /* The reference is no longer packed */
1448 ref
->flags
&= ~GIT_REF_PACKED
;
1450 git_buf_free(&aux_path
);
1454 int git_reference_resolve(git_reference
**ref_out
, git_reference
*ref
)
1456 if (ref
->flags
& GIT_REF_OID
)
1457 return git_reference_lookup(ref_out
, ref
->owner
, ref
->name
);
1459 return git_reference_lookup_resolved(ref_out
, ref
->owner
, ref
->target
.symbolic
, -1);
1462 int git_reference_packall(git_repository
*repo
)
1464 if (packed_load(repo
) < 0 || /* load the existing packfile */
1465 packed_loadloose(repo
) < 0 || /* add all the loose refs */
1466 packed_write(repo
) < 0) /* write back to disk */
1472 int git_reference_foreach(
1473 git_repository
*repo
,
1474 unsigned int list_flags
,
1475 int (*callback
)(const char *, void *),
1479 struct dirent_list_data data
;
1480 git_buf refs_path
= GIT_BUF_INIT
;
1482 /* list all the packed references first */
1483 if (list_flags
& GIT_REF_PACKED
) {
1484 const char *ref_name
;
1488 if (packed_load(repo
) < 0)
1491 git_strmap_foreach(repo
->references
.packfile
, ref_name
, ref
, {
1492 if (callback(ref_name
, payload
))
1497 /* now list the loose references, trying not to
1498 * duplicate the ref names already in the packed-refs file */
1500 data
.repo_path_len
= strlen(repo
->path_repository
);
1501 data
.list_flags
= list_flags
;
1503 data
.callback
= callback
;
1504 data
.callback_payload
= payload
;
1505 data
.callback_error
= 0;
1507 if (git_buf_joinpath(&refs_path
, repo
->path_repository
, GIT_REFS_DIR
) < 0)
1510 result
= git_path_direach(&refs_path
, _dirent_loose_listall
, &data
);
1512 git_buf_free(&refs_path
);
1514 return data
.callback_error
? GIT_EUSER
: result
;
1517 static int cb__reflist_add(const char *ref
, void *data
)
1519 return git_vector_insert((git_vector
*)data
, git__strdup(ref
));
1522 int git_reference_list(
1523 git_strarray
*array
,
1524 git_repository
*repo
,
1525 unsigned int list_flags
)
1527 git_vector ref_list
;
1529 assert(array
&& repo
);
1531 array
->strings
= NULL
;
1534 if (git_vector_init(&ref_list
, 8, NULL
) < 0)
1537 if (git_reference_foreach(
1538 repo
, list_flags
, &cb__reflist_add
, (void *)&ref_list
) < 0) {
1539 git_vector_free(&ref_list
);
1543 array
->strings
= (char **)ref_list
.contents
;
1544 array
->count
= ref_list
.length
;
1548 int git_reference_reload(git_reference
*ref
)
1550 return reference_lookup(ref
);
1553 void git_repository__refcache_free(git_refcache
*refs
)
1557 if (refs
->packfile
) {
1558 struct packref
*reference
;
1560 git_strmap_foreach_value(refs
->packfile
, reference
, {
1561 git__free(reference
);
1564 git_strmap_free(refs
->packfile
);
1568 static int is_valid_ref_char(char ch
)
1570 if ((unsigned) ch
<= ' ')
1587 static int ensure_segment_validity(const char *name
)
1589 const char *current
= name
;
1592 if (*current
== '.')
1593 return -1; /* Refname starts with "." */
1595 for (current
= name
; ; current
++) {
1596 if (*current
== '\0' || *current
== '/')
1599 if (!is_valid_ref_char(*current
))
1600 return -1; /* Illegal character in refname */
1602 if (prev
== '.' && *current
== '.')
1603 return -1; /* Refname contains ".." */
1605 if (prev
== '@' && *current
== '{')
1606 return -1; /* Refname contains "@{" */
1611 return current
- name
;
1614 static bool is_all_caps_and_underscore(const char *name
, int len
)
1619 assert(name
&& len
> 0);
1621 for (i
= 0; i
< len
; i
++)
1624 if ((c
< 'A' || c
> 'Z') && c
!= '_')
1628 if (*name
== '_' || name
[len
- 1] == '_')
1634 int git_reference__normalize_name(
1639 // Inspired from https://github.com/git/git/blob/f06d47e7e0d9db709ee204ed13a8a7486149f494/refs.c#L36-100
1642 int segment_len
, segments_count
= 0, error
= -1;
1643 unsigned int process_flags
;
1644 bool normalize
= (buf
!= NULL
);
1647 process_flags
= flags
;
1648 current
= (char *)name
;
1654 segment_len
= ensure_segment_validity(current
);
1655 if (segment_len
< 0) {
1656 if ((process_flags
& GIT_REF_FORMAT_REFSPEC_PATTERN
) &&
1657 current
[0] == '*' &&
1658 (current
[1] == '\0' || current
[1] == '/')) {
1659 /* Accept one wildcard as a full refname component. */
1660 process_flags
&= ~GIT_REF_FORMAT_REFSPEC_PATTERN
;
1666 if (segment_len
> 0) {
1668 int cur_len
= git_buf_len(buf
);
1670 git_buf_joinpath(buf
, git_buf_cstr(buf
), current
);
1671 git_buf_truncate(buf
,
1672 cur_len
+ segment_len
+ (segments_count
? 1 : 0));
1674 if (git_buf_oom(buf
))
1681 if (current
[segment_len
] == '\0')
1684 current
+= segment_len
+ 1;
1687 /* A refname can not be empty */
1688 if (segment_len
== 0 && segments_count
== 0)
1691 /* A refname can not end with "." */
1692 if (current
[segment_len
- 1] == '.')
1695 /* A refname can not end with "/" */
1696 if (current
[segment_len
- 1] == '/')
1699 /* A refname can not end with ".lock" */
1700 if (!git__suffixcmp(name
, GIT_FILELOCK_EXTENSION
))
1703 if ((segments_count
== 1 ) && !(flags
& GIT_REF_FORMAT_ALLOW_ONELEVEL
))
1706 if ((segments_count
== 1 ) &&
1707 !(is_all_caps_and_underscore(name
, segment_len
) ||
1708 ((flags
& GIT_REF_FORMAT_REFSPEC_PATTERN
) && !strcmp("*", name
))))
1711 if ((segments_count
> 1)
1712 && (is_all_caps_and_underscore(name
, strchr(name
, '/') - name
)))
1721 "The given reference name '%s' is not valid", name
);
1726 int git_reference_normalize_name(
1732 git_buf buf
= GIT_BUF_INIT
;
1735 if ((error
= git_reference__normalize_name(&buf
, name
, flags
)) < 0)
1738 if (git_buf_len(&buf
) > buffer_size
- 1) {
1741 "The provided buffer is too short to hold the normalization of '%s'", name
);
1746 git_buf_copy_cstr(buffer_out
, buffer_size
, &buf
);
1755 int git_reference__normalize_name_lax(
1760 return git_reference_normalize_name(
1764 GIT_REF_FORMAT_ALLOW_ONELEVEL
);
1766 #define GIT_REF_TYPEMASK (GIT_REF_OID | GIT_REF_SYMBOLIC)
1768 int git_reference_cmp(git_reference
*ref1
, git_reference
*ref2
)
1770 assert(ref1
&& ref2
);
1772 /* let's put symbolic refs before OIDs */
1773 if ((ref1
->flags
& GIT_REF_TYPEMASK
) != (ref2
->flags
& GIT_REF_TYPEMASK
))
1774 return (ref1
->flags
& GIT_REF_SYMBOLIC
) ? -1 : 1;
1776 if (ref1
->flags
& GIT_REF_SYMBOLIC
)
1777 return strcmp(ref1
->target
.symbolic
, ref2
->target
.symbolic
);
1779 return git_oid_cmp(&ref1
->target
.oid
, &ref2
->target
.oid
);
1782 /* Update the reference named `ref_name` so it points to `oid` */
1783 int git_reference__update(git_repository
*repo
, const git_oid
*oid
, const char *ref_name
)
1788 res
= git_reference_lookup(&ref
, repo
, ref_name
);
1790 /* If we haven't found the reference at all, we assume we need to create
1791 * a new reference and that's it */
1792 if (res
== GIT_ENOTFOUND
) {
1794 return git_reference_create_oid(NULL
, repo
, ref_name
, oid
, 1);
1800 /* If we have found a reference, but it's symbolic, we need to update
1801 * the direct reference it points to */
1802 if (git_reference_type(ref
) == GIT_REF_SYMBOLIC
) {
1804 const char *sym_target
;
1806 /* The target pointed at by this reference */
1807 sym_target
= git_reference_target(ref
);
1809 /* resolve the reference to the target it points to */
1810 res
= git_reference_resolve(&aux
, ref
);
1813 * if the symbolic reference pointed to an inexisting ref,
1814 * this is means we're creating a new branch, for example.
1815 * We need to create a new direct reference with that name
1817 if (res
== GIT_ENOTFOUND
) {
1819 res
= git_reference_create_oid(NULL
, repo
, sym_target
, oid
, 1);
1820 git_reference_free(ref
);
1824 /* free the original symbolic reference now; not before because
1825 * we're using the `sym_target` pointer */
1826 git_reference_free(ref
);
1831 /* store the newly found direct reference in its place */
1835 /* ref is made to point to `oid`: ref is either the original reference,
1836 * or the target of the symbolic reference we've looked up */
1837 res
= git_reference_set_oid(ref
, oid
);
1838 git_reference_free(ref
);
1842 struct glob_cb_data
{
1844 int (*callback
)(const char *, void *);
1848 static int fromglob_cb(const char *reference_name
, void *payload
)
1850 struct glob_cb_data
*data
= (struct glob_cb_data
*)payload
;
1852 if (!p_fnmatch(data
->glob
, reference_name
, 0))
1853 return data
->callback(reference_name
, data
->payload
);
1858 int git_reference_foreach_glob(
1859 git_repository
*repo
,
1861 unsigned int list_flags
,
1863 const char *reference_name
,
1867 struct glob_cb_data data
;
1869 assert(repo
&& glob
&& callback
);
1872 data
.callback
= callback
;
1873 data
.payload
= payload
;
1875 return git_reference_foreach(
1876 repo
, list_flags
, fromglob_cb
, &data
);
1879 int git_reference_has_log(
1882 git_buf path
= GIT_BUF_INIT
;
1887 if (git_buf_join_n(&path
, '/', 3, ref
->owner
->path_repository
, GIT_REFLOG_DIR
, ref
->name
) < 0)
1890 result
= git_path_isfile(git_buf_cstr(&path
));
1891 git_buf_free(&path
);
1896 int git_reference_is_branch(git_reference
*ref
)
1899 return git__prefixcmp(ref
->name
, GIT_REFS_HEADS_DIR
) == 0;
1902 int git_reference_is_remote(git_reference
*ref
)
1905 return git__prefixcmp(ref
->name
, GIT_REFS_REMOTES_DIR
) == 0;
1908 static int peel_error(int error
, git_reference
*ref
, const char* msg
)
1912 "The reference '%s' cannot be peeled - %s", git_reference_name(ref
), msg
);
1916 static int reference_target(git_object
**object
, git_reference
*ref
)
1920 oid
= git_reference_oid(ref
);
1922 return git_object_lookup(object
, git_reference_owner(ref
), oid
, GIT_OBJ_ANY
);
1925 int git_reference_peel(
1926 git_object
**peeled
,
1928 git_otype target_type
)
1930 git_reference
*resolved
= NULL
;
1931 git_object
*target
= NULL
;
1936 if ((error
= git_reference_resolve(&resolved
, ref
)) < 0)
1937 return peel_error(error
, ref
, "Cannot resolve reference");
1939 if ((error
= reference_target(&target
, resolved
)) < 0) {
1940 peel_error(error
, ref
, "Cannot retrieve reference target");
1944 if (target_type
== GIT_OBJ_ANY
&& git_object_type(target
) != GIT_OBJ_TAG
)
1945 error
= git_object__dup(peeled
, target
);
1947 error
= git_object_peel(peeled
, target
, target_type
);
1950 git_object_free(target
);
1951 git_reference_free(resolved
);
1955 int git_reference__is_valid_name(
1956 const char *refname
,
1960 return git_reference__normalize_name(NULL
, refname
, flags
) == 0;
1963 int git_reference_is_valid_name(
1964 const char *refname
)
1966 return git_reference__is_valid_name(
1968 GIT_REF_FORMAT_ALLOW_ONELEVEL
);