Commit 8a497ec9 by Vicent Martí

Merge pull request #1531 from libgit2/jk/peeled-refs

refdb_fs: do not require peeled packed refs to be tags
parents d966310c a591ed3e
...@@ -26,8 +26,15 @@ GIT__USE_STRMAP; ...@@ -26,8 +26,15 @@ GIT__USE_STRMAP;
#define MAX_NESTING_LEVEL 10 #define MAX_NESTING_LEVEL 10
enum { enum {
GIT_PACKREF_HAS_PEEL = 1, PACKREF_HAS_PEEL = 1,
GIT_PACKREF_WAS_LOOSE = 2 PACKREF_WAS_LOOSE = 2,
PACKREF_CANNOT_PEEL = 4
};
enum {
PEELING_NONE = 0,
PEELING_STANDARD,
PEELING_FULL
}; };
struct packref { struct packref {
...@@ -44,6 +51,7 @@ typedef struct refdb_fs_backend { ...@@ -44,6 +51,7 @@ typedef struct refdb_fs_backend {
char *path; char *path;
git_refcache refcache; git_refcache refcache;
int peeling_mode;
} refdb_fs_backend; } refdb_fs_backend;
static int reference_read( static int reference_read(
...@@ -132,10 +140,6 @@ static int packed_parse_peel( ...@@ -132,10 +140,6 @@ static int packed_parse_peel(
if (tag_ref == NULL) if (tag_ref == NULL)
goto corrupt; goto corrupt;
/* Ensure reference is a tag */
if (git__prefixcmp(tag_ref->name, GIT_REFS_TAGS_DIR) != 0)
goto corrupt;
if (buffer + GIT_OID_HEXSZ > buffer_end) if (buffer + GIT_OID_HEXSZ > buffer_end)
goto corrupt; goto corrupt;
...@@ -154,6 +158,7 @@ static int packed_parse_peel( ...@@ -154,6 +158,7 @@ static int packed_parse_peel(
goto corrupt; goto corrupt;
} }
tag_ref->flags |= PACKREF_HAS_PEEL;
*buffer_out = buffer; *buffer_out = buffer;
return 0; return 0;
...@@ -205,6 +210,30 @@ static int packed_load(refdb_fs_backend *backend) ...@@ -205,6 +210,30 @@ static int packed_load(refdb_fs_backend *backend)
buffer_start = (const char *)packfile.ptr; buffer_start = (const char *)packfile.ptr;
buffer_end = (const char *)(buffer_start) + packfile.size; buffer_end = (const char *)(buffer_start) + packfile.size;
backend->peeling_mode = PEELING_NONE;
if (buffer_start[0] == '#') {
static const char *traits_header = "# pack-refs with: ";
if (git__prefixcmp(buffer_start, traits_header) == 0) {
char *traits = (char *)buffer_start + strlen(traits_header);
char *traits_end = strchr(traits, '\n');
if (traits_end == NULL)
goto parse_failed;
*traits_end = '\0';
if (strstr(traits, " fully-peeled ") != NULL) {
backend->peeling_mode = PEELING_FULL;
} else if (strstr(traits, " peeled ") != NULL) {
backend->peeling_mode = PEELING_STANDARD;
}
buffer_start = traits_end + 1;
}
}
while (buffer_start < buffer_end && buffer_start[0] == '#') { while (buffer_start < buffer_end && buffer_start[0] == '#') {
buffer_start = strchr(buffer_start, '\n'); buffer_start = strchr(buffer_start, '\n');
if (buffer_start == NULL) if (buffer_start == NULL)
...@@ -223,6 +252,10 @@ static int packed_load(refdb_fs_backend *backend) ...@@ -223,6 +252,10 @@ static int packed_load(refdb_fs_backend *backend)
if (buffer_start[0] == '^') { if (buffer_start[0] == '^') {
if (packed_parse_peel(ref, &buffer_start, buffer_end) < 0) if (packed_parse_peel(ref, &buffer_start, buffer_end) < 0)
goto parse_failed; goto parse_failed;
} else if (backend->peeling_mode == PEELING_FULL ||
(backend->peeling_mode == PEELING_STANDARD &&
git__prefixcmp(ref->name, GIT_REFS_TAGS_DIR) == 0)) {
ref->flags |= PACKREF_CANNOT_PEEL;
} }
git_strmap_insert(ref_cache->packfile, ref->name, ref, err); git_strmap_insert(ref_cache->packfile, ref->name, ref, err);
...@@ -295,7 +328,7 @@ static int loose_lookup_to_packfile( ...@@ -295,7 +328,7 @@ static int loose_lookup_to_packfile(
return -1; return -1;
} }
ref->flags = GIT_PACKREF_WAS_LOOSE; ref->flags = PACKREF_WAS_LOOSE;
*ref_out = ref; *ref_out = ref;
git_buf_free(&ref_file); git_buf_free(&ref_file);
...@@ -678,14 +711,7 @@ static int packed_find_peel(refdb_fs_backend *backend, struct packref *ref) ...@@ -678,14 +711,7 @@ static int packed_find_peel(refdb_fs_backend *backend, struct packref *ref)
{ {
git_object *object; git_object *object;
if (ref->flags & GIT_PACKREF_HAS_PEEL) if (ref->flags & PACKREF_HAS_PEEL || ref->flags & PACKREF_CANNOT_PEEL)
return 0;
/*
* Only applies to tags, i.e. references
* in the /refs/tags folder
*/
if (git__prefixcmp(ref->name, GIT_REFS_TAGS_DIR) != 0)
return 0; return 0;
/* /*
...@@ -706,7 +732,7 @@ static int packed_find_peel(refdb_fs_backend *backend, struct packref *ref) ...@@ -706,7 +732,7 @@ static int packed_find_peel(refdb_fs_backend *backend, struct packref *ref)
* Find the object pointed at by this tag * Find the object pointed at by this tag
*/ */
git_oid_cpy(&ref->peel, git_tag_target_id(tag)); git_oid_cpy(&ref->peel, git_tag_target_id(tag));
ref->flags |= GIT_PACKREF_HAS_PEEL; ref->flags |= PACKREF_HAS_PEEL;
/* /*
* The reference has now cached the resolved OID, and is * The reference has now cached the resolved OID, and is
...@@ -739,7 +765,7 @@ static int packed_write_ref(struct packref *ref, git_filebuf *file) ...@@ -739,7 +765,7 @@ static int packed_write_ref(struct packref *ref, git_filebuf *file)
* This obviously only applies to tags. * This obviously only applies to tags.
* The required peels have already been loaded into `ref->peel_target`. * The required peels have already been loaded into `ref->peel_target`.
*/ */
if (ref->flags & GIT_PACKREF_HAS_PEEL) { if (ref->flags & PACKREF_HAS_PEEL) {
char peel[GIT_OID_HEXSZ + 1]; char peel[GIT_OID_HEXSZ + 1];
git_oid_fmt(peel, &ref->peel); git_oid_fmt(peel, &ref->peel);
peel[GIT_OID_HEXSZ] = 0; peel[GIT_OID_HEXSZ] = 0;
...@@ -776,7 +802,7 @@ static int packed_remove_loose( ...@@ -776,7 +802,7 @@ static int packed_remove_loose(
for (i = 0; i < packing_list->length; ++i) { for (i = 0; i < packing_list->length; ++i) {
struct packref *ref = git_vector_get(packing_list, i); struct packref *ref = git_vector_get(packing_list, i);
if ((ref->flags & GIT_PACKREF_WAS_LOOSE) == 0) if ((ref->flags & PACKREF_WAS_LOOSE) == 0)
continue; continue;
if (git_buf_joinpath(&full_path, backend->path, ref->name) < 0) if (git_buf_joinpath(&full_path, backend->path, ref->name) < 0)
......
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
#define GIT_SYMREF "ref: " #define GIT_SYMREF "ref: "
#define GIT_PACKEDREFS_FILE "packed-refs" #define GIT_PACKEDREFS_FILE "packed-refs"
#define GIT_PACKEDREFS_HEADER "# pack-refs with: peeled " #define GIT_PACKEDREFS_HEADER "# pack-refs with: peeled fully-peeled "
#define GIT_PACKEDREFS_FILE_MODE 0666 #define GIT_PACKEDREFS_FILE_MODE 0666
#define GIT_HEAD_FILE "HEAD" #define GIT_HEAD_FILE "HEAD"
......
#include "clar_libgit2.h" #include "clar_libgit2.h"
static git_repository *g_repo; static git_repository *g_repo;
static git_repository *g_peel_repo;
void test_refs_peel__initialize(void) void test_refs_peel__initialize(void)
{ {
cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git"))); cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
cl_git_pass(git_repository_open(&g_peel_repo, cl_fixture("peeled.git")));
} }
void test_refs_peel__cleanup(void) void test_refs_peel__cleanup(void)
{ {
git_repository_free(g_repo); git_repository_free(g_repo);
g_repo = NULL; g_repo = NULL;
git_repository_free(g_peel_repo);
g_peel_repo = NULL;
} }
static void assert_peel( static void assert_peel_generic(
git_repository *repo,
const char *ref_name, const char *ref_name,
git_otype requested_type, git_otype requested_type,
const char* expected_sha, const char* expected_sha,
...@@ -23,7 +28,7 @@ static void assert_peel( ...@@ -23,7 +28,7 @@ static void assert_peel(
git_reference *ref; git_reference *ref;
git_object *peeled; git_object *peeled;
cl_git_pass(git_reference_lookup(&ref, g_repo, ref_name)); cl_git_pass(git_reference_lookup(&ref, repo, ref_name));
cl_git_pass(git_reference_peel(&peeled, ref, requested_type)); cl_git_pass(git_reference_peel(&peeled, ref, requested_type));
...@@ -36,6 +41,16 @@ static void assert_peel( ...@@ -36,6 +41,16 @@ static void assert_peel(
git_reference_free(ref); git_reference_free(ref);
} }
static void assert_peel(
const char *ref_name,
git_otype requested_type,
const char* expected_sha,
git_otype expected_type)
{
assert_peel_generic(g_repo, ref_name, requested_type,
expected_sha, expected_type);
}
static void assert_peel_error(int error, const char *ref_name, git_otype requested_type) static void assert_peel_error(int error, const char *ref_name, git_otype requested_type)
{ {
git_reference *ref; git_reference *ref;
...@@ -90,3 +105,15 @@ void test_refs_peel__can_peel_into_any_non_tag_object(void) ...@@ -90,3 +105,15 @@ void test_refs_peel__can_peel_into_any_non_tag_object(void)
assert_peel("refs/tags/test", GIT_OBJ_ANY, assert_peel("refs/tags/test", GIT_OBJ_ANY,
"e90810b8df3e80c413d903f631643c716887138d", GIT_OBJ_COMMIT); "e90810b8df3e80c413d903f631643c716887138d", GIT_OBJ_COMMIT);
} }
void test_refs_peel__can_peel_fully_peeled_packed_refs(void)
{
assert_peel_generic(g_peel_repo,
"refs/tags/tag-inside-tags", GIT_OBJ_ANY,
"0df1a5865c8abfc09f1f2182e6a31be550e99f07",
GIT_OBJ_COMMIT);
assert_peel_generic(g_peel_repo,
"refs/foo/tag-outside-tags", GIT_OBJ_ANY,
"0df1a5865c8abfc09f1f2182e6a31be550e99f07",
GIT_OBJ_COMMIT);
}
[core]
repositoryformatversion = 0
filemode = true
bare = true
[remote "origin"]
url = /home/peff/compile/libgit2/tests-clar/resources/peeled
fetch = +refs/*:refs/*
mirror = true
P pack-e84773eaf3fce1774755580e3dbb8d9f3a1adc45.pack
# pack-refs with: peeled fully-peeled
c2596aa0151888587ec5c0187f261e63412d9e11 refs/foo/tag-outside-tags
^0df1a5865c8abfc09f1f2182e6a31be550e99f07
0df1a5865c8abfc09f1f2182e6a31be550e99f07 refs/heads/master
c2596aa0151888587ec5c0187f261e63412d9e11 refs/tags/tag-inside-tags
^0df1a5865c8abfc09f1f2182e6a31be550e99f07
0df1a5865c8abfc09f1f2182e6a31be550e99f07
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment