From afa79ca05862c24baa0360324ab7b63c0df90689 Mon Sep 17 00:00:00 2001 From: yuangli Date: Mon, 4 Jul 2022 17:08:04 +0100 Subject: Merge branch 'pr/tiennou/4747' into transportPR --- include/git2/remote.h | 7 ++- include/git2/repository.h | 11 ++++ include/git2/sys/transport.h | 17 +++++- src/array.h | 4 +- src/fetch.c | 22 ++++++- src/object.c | 21 ++++++- src/object.h | 6 ++ src/remote.h | 1 + src/repository.c | 89 ++++++++++++++++++++++++++++ src/repository.h | 3 + src/transports/local.c | 6 +- src/transports/smart.c | 28 +++++++++ src/transports/smart.h | 21 +++++-- src/transports/smart_pkt.c | 90 ++++++++++++++++++++++++++--- src/transports/smart_protocol.c | 63 ++++++++++++++++++-- tests/clone/shallow.c | 55 ++++++++++++++++++ tests/repo/grafts.c | 119 ++++++++++++++++++++++++++++++++++++++ tests/repo/shallow.c | 125 ++++++++++++++++++++++++++++++++++++++++ 18 files changed, 659 insertions(+), 29 deletions(-) create mode 100644 tests/clone/shallow.c create mode 100644 tests/repo/grafts.c create mode 100644 tests/repo/shallow.c diff --git a/include/git2/remote.h b/include/git2/remote.h index 4d57eaaf7..e577f8a7b 100644 --- a/include/git2/remote.h +++ b/include/git2/remote.h @@ -738,11 +738,16 @@ typedef struct { * Extra headers for this fetch operation */ git_strarray custom_headers; + + /** + * Depth of the fetch to perform + */ + int depth; } git_fetch_options; #define GIT_FETCH_OPTIONS_VERSION 1 #define GIT_FETCH_OPTIONS_INIT { GIT_FETCH_OPTIONS_VERSION, GIT_REMOTE_CALLBACKS_INIT, GIT_FETCH_PRUNE_UNSPECIFIED, 1, \ - GIT_REMOTE_DOWNLOAD_TAGS_UNSPECIFIED, GIT_PROXY_OPTIONS_INIT } + GIT_REMOTE_DOWNLOAD_TAGS_UNSPECIFIED, GIT_PROXY_OPTIONS_INIT, { NULL }, -1 } /** * Initialize git_fetch_options structure diff --git a/include/git2/repository.h b/include/git2/repository.h index ec8adfb29..bbf3ae4cd 100644 --- a/include/git2/repository.h +++ b/include/git2/repository.h @@ -920,6 +920,17 @@ GIT_EXTERN(const char *) git_repository_get_namespace(git_repository *repo); */ GIT_EXTERN(int) git_repository_is_shallow(git_repository *repo); +/** + * Determine the shallow roots of the repository + * + * This oidarray is owned by the library. Do not free it. + * + * @param out An array of shallow oids. + * @param repo The repository + * @return 0 on success, an error otherwise. + */ +GIT_EXTERN(int) git_repository_shallow_roots(git_oidarray *out, git_repository *repo); + /** * Retrieve the configured identity to use for reflogs * diff --git a/include/git2/sys/transport.h b/include/git2/sys/transport.h index fee34544f..ea8bcb64b 100644 --- a/include/git2/sys/transport.h +++ b/include/git2/sys/transport.h @@ -33,6 +33,15 @@ typedef enum { GIT_TRANSPORTFLAGS_NONE = 0, } git_transport_flags_t; +typedef struct git_shallowarray git_shallowarray; + +typedef struct { + const git_remote_head * const *refs; + size_t count; + git_shallowarray *shallow_roots; + int depth; +} git_fetch_negotiation; + struct git_transport { unsigned int version; /**< The struct version */ @@ -87,8 +96,7 @@ struct git_transport { int GIT_CALLBACK(negotiate_fetch)( git_transport *transport, git_repository *repo, - const git_remote_head * const *refs, - size_t count); + const git_fetch_negotiation *fetch_data); /** * Start downloading the packfile from the remote repository. @@ -435,6 +443,11 @@ GIT_EXTERN(int) git_smart_subtransport_ssh( git_transport *owner, void *param); +GIT_EXTERN(size_t) git_shallowarray_count(git_shallowarray *array); +GIT_EXTERN(const git_oid *) git_shallowarray_get(git_shallowarray *array, size_t idx); +GIT_EXTERN(int) git_shallowarray_add(git_shallowarray *array, git_oid *oid); +GIT_EXTERN(int) git_shallowarray_remove(git_shallowarray *array, git_oid *oid); + /** @} */ GIT_END_DECL #endif diff --git a/src/array.h b/src/array.h index e97688b36..3d6c9113c 100644 --- a/src/array.h +++ b/src/array.h @@ -85,12 +85,14 @@ on_oom: #define git_array_foreach(a, i, element) \ for ((i) = 0; (i) < (a).size && ((element) = &(a).ptr[(i)]); (i)++) +typedef int (*git_array_compare_cb)(const void *, const void *); + GIT_INLINE(int) git_array__search( size_t *out, void *array_ptr, size_t item_size, size_t array_len, - int (*compare)(const void *, const void *), + git_array_compare_cb compare, const void *key) { size_t lim; diff --git a/src/fetch.c b/src/fetch.c index dedbb54fa..e08671db3 100644 --- a/src/fetch.c +++ b/src/fetch.c @@ -18,6 +18,7 @@ #include "netops.h" #include "repository.h" #include "refs.h" +#include "transports/smart.h" static int maybe_want(git_remote *remote, git_remote_head *head, git_odb *odb, git_refspec *tagspec, git_remote_autotag_option_t tagopt) { @@ -128,10 +129,18 @@ int git_fetch_negotiate(git_remote *remote, const git_fetch_options *opts) * Now we have everything set up so we can start tell the * server what we want and what we have. */ + remote->nego.refs = (const git_remote_head * const *)remote->refs.contents; + remote->nego.count = remote->refs.length; + remote->nego.depth = opts->depth; + remote->nego.shallow_roots = git__malloc(sizeof(git_shallowarray)); + + git_array_init(remote->nego.shallow_roots->array); + + git_repository__shallow_roots(&remote->nego.shallow_roots->array, remote->repo); + return t->negotiate_fetch(t, remote->repo, - (const git_remote_head * const *)remote->refs.contents, - remote->refs.length); + &remote->nego); } int git_fetch_download_pack(git_remote *remote, const git_remote_callbacks *callbacks) @@ -139,6 +148,7 @@ int git_fetch_download_pack(git_remote *remote, const git_remote_callbacks *call git_transport *t = remote->transport; git_indexer_progress_cb progress = NULL; void *payload = NULL; + int error; if (!remote->need_pack) return 0; @@ -148,7 +158,13 @@ int git_fetch_download_pack(git_remote *remote, const git_remote_callbacks *call payload = callbacks->payload; } - return t->download_pack(t, remote->repo, &remote->stats, progress, payload); + if ((error = t->download_pack(t, remote->repo, &remote->stats, progress, payload)) < 0) + return error; + + if ((error = git_repository__shallow_roots_write(remote->repo, remote->nego.shallow_roots->array)) < 0) + return error; + + return 0; } int git_fetch_options_init(git_fetch_options *opts, unsigned int version) diff --git a/src/object.c b/src/object.c index 42e1e46bc..b58f01a34 100644 --- a/src/object.c +++ b/src/object.c @@ -104,15 +104,13 @@ int git_object__from_raw( return 0; } -int git_object__from_odb_object( +int git_object__init_from_odb_object( git_object **object_out, git_repository *repo, git_odb_object *odb_obj, git_object_t type) { - int error; size_t object_size; - git_object_def *def; git_object *object = NULL; GIT_ASSERT_ARG(object_out); @@ -139,6 +137,23 @@ int git_object__from_odb_object( object->cached.size = odb_obj->cached.size; object->repo = repo; + *object_out = object; + return 0; +} + +int git_object__from_odb_object( + git_object **object_out, + git_repository *repo, + git_odb_object *odb_obj, + git_object_t type) +{ + int error; + git_object_def *def; + git_object *object = NULL; + + if ((error = git_object__init_from_odb_object(&object, repo, odb_obj, type)) < 0) + return error; + /* Parse raw object data */ def = &git_objects_table[odb_obj->cached.type]; GIT_ASSERT(def->free && def->parse); diff --git a/src/object.h b/src/object.h index 4b6793612..71a966a92 100644 --- a/src/object.h +++ b/src/object.h @@ -35,6 +35,12 @@ int git_object__from_raw( size_t size, git_object_t type); +int git_object__init_from_odb_object( + git_object **object_out, + git_repository *repo, + git_odb_object *odb_obj, + git_object_t type); + int git_object__from_odb_object( git_object **object_out, git_repository *repo, diff --git a/src/remote.h b/src/remote.h index ce92db76a..8297af197 100644 --- a/src/remote.h +++ b/src/remote.h @@ -35,6 +35,7 @@ struct git_remote { git_remote_autotag_option_t download_tags; int prune_refs; int passed_refspecs; + git_fetch_negotiation nego; }; typedef struct git_remote_connection_opts { diff --git a/src/repository.c b/src/repository.c index ab5753795..537622273 100644 --- a/src/repository.c +++ b/src/repository.c @@ -3192,6 +3192,95 @@ int git_repository_state_cleanup(git_repository *repo) return git_repository__cleanup_files(repo, state_files, ARRAY_SIZE(state_files)); } +int git_repository__shallow_roots(git_array_oid_t *out, git_repository *repo) +{ + git_buf path = GIT_BUF_INIT; + git_buf contents = GIT_BUF_INIT; + int error, updated, line_num = 1; + char *line; + char *buffer; + + assert(out && repo); + + if ((error = git_buf_joinpath(&path, repo->gitdir, "shallow")) < 0) + return error; + + error = git_futils_readbuffer_updated(&contents, git_buf_cstr(&path), &repo->shallow_checksum, &updated); + git_buf_dispose(&path); + + if (error < 0 && error != GIT_ENOTFOUND) + return error; + + /* cancel out GIT_ENOTFOUND */ + git_error_clear(); + error = 0; + + if (!updated) { + *out = repo->shallow_oids; + goto cleanup; + } + + git_array_clear(repo->shallow_oids); + + buffer = contents.ptr; + while ((line = git__strsep(&buffer, "\n")) != NULL) { + git_oid *oid = git_array_alloc(repo->shallow_oids); + + error = git_oid_fromstr(oid, line); + if (error < 0) { + git_error_set(GIT_ERROR_REPOSITORY, "Invalid OID at line %d", line_num); + git_array_clear(repo->shallow_oids); + error = -1; + goto cleanup; + } + ++line_num; + } + + if (*buffer) { + git_error_set(GIT_ERROR_REPOSITORY, "No EOL at line %d", line_num); + git_array_clear(repo->shallow_oids); + error = -1; + goto cleanup; + } + + *out = repo->shallow_oids; + +cleanup: + git_buf_dispose(&contents); + + return error; +} + +int git_repository__shallow_roots_write(git_repository *repo, git_array_oid_t roots) +{ + git_filebuf file = GIT_FILEBUF_INIT; + git_buf path = GIT_BUF_INIT; + int error = 0; + size_t idx; + git_oid *oid; + + assert(repo); + + if ((error = git_buf_joinpath(&path, repo->gitdir, "shallow")) < 0) + return error; + + if ((error = git_filebuf_open(&file, git_buf_cstr(&path), GIT_FILEBUF_HASH_CONTENTS, 0666)) < 0) + return error; + + git_array_foreach(roots, idx, oid) { + git_filebuf_write(&file, git_oid_tostr_s(oid), GIT_OID_HEXSZ); + git_filebuf_write(&file, "\n", 1); + } + + git_filebuf_commit(&file); + + /* WIP: reload shallow */ + if (load_shallow(repo) < 0) + return -1; + + return 0; +} + int git_repository_is_shallow(git_repository *repo) { git_buf path = GIT_BUF_INIT; diff --git a/src/repository.h b/src/repository.h index 8da65652d..4b6004bea 100644 --- a/src/repository.h +++ b/src/repository.h @@ -242,6 +242,9 @@ extern size_t git_repository__reserved_names_posix_len; bool git_repository__reserved_names( git_buf **out, size_t *outlen, git_repository *repo, bool include_ntfs); +int git_repository__shallow_roots(git_array_oid_t *out, git_repository *repo); +int git_repository__shallow_roots_write(git_repository *repo, git_array_oid_t roots); + /* * The default branch for the repository; the `init.defaultBranch` * configuration option, if set, or `master` if it is not. diff --git a/src/transports/local.c b/src/transports/local.c index bb31b1345..17905d222 100644 --- a/src/transports/local.c +++ b/src/transports/local.c @@ -268,15 +268,13 @@ static int local_ls(const git_remote_head ***out, size_t *size, git_transport *t static int local_negotiate_fetch( git_transport *transport, git_repository *repo, - const git_remote_head * const *refs, - size_t count) + const git_fetch_negotiation *wants) { transport_local *t = (transport_local*)transport; git_remote_head *rhead; unsigned int i; - GIT_UNUSED(refs); - GIT_UNUSED(count); + GIT_UNUSED(wants); /* Fill in the loids */ git_vector_foreach(&t->refs, i, rhead) { diff --git a/src/transports/smart.c b/src/transports/smart.c index 587f14358..a26bf79ec 100644 --- a/src/transports/smart.c +++ b/src/transports/smart.c @@ -558,3 +558,31 @@ int git_transport_smart(git_transport **out, git_remote *owner, void *param) *out = (git_transport *) t; return 0; } + +size_t git_shallowarray_count(git_shallowarray *array) +{ + return git_array_size(array->array); +} + +const git_oid * git_shallowarray_get(git_shallowarray *array, size_t idx) +{ + return git_array_get(array->array, idx); +} + +int git_shallowarray_add(git_shallowarray *array, git_oid *oid) +{ + size_t oid_index; + if (git_array_search(&oid_index, array->array, (git_array_compare_cb)git_oid_cmp, &oid) < 0) { + git_oid *tmp = git_array_alloc(array->array); + git_oid_cpy(tmp, oid); + } + return 0; +} + +int git_shallowarray_remove(git_shallowarray *array, git_oid *oid) +{ + GIT_UNUSED(array); + GIT_UNUSED(oid); + /* no git_array_remove… meh */ + return -1; +} diff --git a/src/transports/smart.h b/src/transports/smart.h index a05d4c9e3..6e49237ea 100644 --- a/src/transports/smart.h +++ b/src/transports/smart.h @@ -14,6 +14,7 @@ #include "netops.h" #include "buffer.h" #include "push.h" +#include "oidarray.h" #include "git2/sys/transport.h" #define GIT_SIDE_BAND_DATA 1 @@ -30,6 +31,7 @@ #define GIT_CAP_REPORT_STATUS "report-status" #define GIT_CAP_THIN_PACK "thin-pack" #define GIT_CAP_SYMREF "symref" +#define GIT_CAP_SHALLOW "shallow" extern bool git_smart__ofs_delta_enabled; @@ -47,6 +49,8 @@ typedef enum { GIT_PKT_OK, GIT_PKT_NG, GIT_PKT_UNPACK, + GIT_PKT_SHALLOW, + GIT_PKT_UNSHALLOW, } git_pkt_type; /* Used for multi_ack and multi_ack_detailed */ @@ -118,6 +122,11 @@ typedef struct { int unpack_ok; } git_pkt_unpack; +typedef struct { + git_pkt_type type; + git_oid oid; +} git_pkt_shallow; + typedef struct transport_smart_caps { int common:1, ofs_delta:1, @@ -128,7 +137,8 @@ typedef struct transport_smart_caps { include_tag:1, delete_refs:1, report_status:1, - thin_pack:1; + thin_pack:1, + shallow:1; } transport_smart_caps; typedef int (*packetsize_cb)(size_t received, void *payload); @@ -171,8 +181,7 @@ int git_smart__push(git_transport *transport, git_push *push, const git_remote_c int git_smart__negotiate_fetch( git_transport *transport, git_repository *repo, - const git_remote_head * const *refs, - size_t count); + const git_fetch_negotiation *wants); int git_smart__download_pack( git_transport *transport, @@ -192,8 +201,12 @@ int git_pkt_parse_line(git_pkt **head, const char **endptr, const char *line, si int git_pkt_buffer_flush(git_buf *buf); int git_pkt_send_flush(GIT_SOCKET s); int git_pkt_buffer_done(git_buf *buf); -int git_pkt_buffer_wants(const git_remote_head * const *refs, size_t count, transport_smart_caps *caps, git_buf *buf); +int git_pkt_buffer_wants(const git_fetch_negotiation *wants, transport_smart_caps *caps, git_buf *buf); int git_pkt_buffer_have(git_oid *oid, git_buf *buf); void git_pkt_free(git_pkt *pkt); +struct git_shallowarray { + git_array_oid_t array; +}; + #endif diff --git a/src/transports/smart_pkt.c b/src/transports/smart_pkt.c index 56b680d28..6a1e842ee 100644 --- a/src/transports/smart_pkt.c +++ b/src/transports/smart_pkt.c @@ -363,6 +363,50 @@ static int unpack_pkt(git_pkt **out, const char *line, size_t len) return 0; } +static int shallow_pkt(git_pkt **out, const char *line, size_t len) +{ + git_pkt_shallow *pkt; + + pkt = git__calloc(1, sizeof(git_pkt_shallow)); + GIT_ERROR_CHECK_ALLOC(pkt); + + pkt->type = GIT_PKT_SHALLOW; + line += 7; + len -= 7; + + if (len >= GIT_OID_HEXSZ) { + git_oid_fromstr(&pkt->oid, line + 1); + line += GIT_OID_HEXSZ + 1; + len -= GIT_OID_HEXSZ + 1; + } + + *out = (git_pkt *) pkt; + + return 0; +} + +static int unshallow_pkt(git_pkt **out, const char *line, size_t len) +{ + git_pkt_shallow *pkt; + + pkt = git__calloc(1, sizeof(git_pkt_shallow)); + GIT_ERROR_CHECK_ALLOC(pkt); + + pkt->type = GIT_PKT_UNSHALLOW; + line += 9; + len -= 9; + + if (len >= GIT_OID_HEXSZ) { + git_oid_fromstr(&pkt->oid, line + 1); + line += GIT_OID_HEXSZ + 1; + len -= GIT_OID_HEXSZ + 1; + } + + *out = (git_pkt *) pkt; + + return 0; +} + static int parse_len(size_t *out, const char *line, size_t linelen) { char num[PKT_LEN_SIZE + 1]; @@ -489,6 +533,10 @@ int git_pkt_parse_line( error = ng_pkt(pkt, line, len); else if (!git__prefixncmp(line, len, "unpack")) error = unpack_pkt(pkt, line, len); + else if (!git__prefixcmp(line, "shallow")) + error = shallow_pkt(pkt, line, len); + else if (!git__prefixcmp(line, "unshallow")) + error = unshallow_pkt(pkt, line, len); else error = ref_pkt(pkt, line, len); @@ -554,6 +602,9 @@ static int buffer_want_with_caps(const git_remote_head *head, transport_smart_ca if (caps->ofs_delta) git_buf_puts(&str, GIT_CAP_OFS_DELTA " "); + if (caps->shallow) + git_buf_puts(&str, GIT_CAP_SHALLOW " "); + if (git_buf_oom(&str)) return -1; @@ -583,8 +634,7 @@ static int buffer_want_with_caps(const git_remote_head *head, transport_smart_ca */ int git_pkt_buffer_wants( - const git_remote_head * const *refs, - size_t count, + const git_fetch_negotiation *wants, transport_smart_caps *caps, git_buf *buf) { @@ -592,22 +642,22 @@ int git_pkt_buffer_wants( const git_remote_head *head; if (caps->common) { - for (; i < count; ++i) { - head = refs[i]; + for (; i < wants->count; ++i) { + head = wants->refs[i]; if (!head->local) break; } - if (buffer_want_with_caps(refs[i], caps, buf) < 0) + if (buffer_want_with_caps(wants->refs[i], caps, buf) < 0) return -1; i++; } - for (; i < count; ++i) { + for (; i < wants->count; ++i) { char oid[GIT_OID_HEXSZ]; - head = refs[i]; + head = wants->refs[i]; if (head->local) continue; @@ -619,6 +669,32 @@ int git_pkt_buffer_wants( return -1; } + /* Tell the server about our shallow objects */ + for (i = 0; i < git_shallowarray_count(wants->shallow_roots); i++) { + char oid[GIT_OID_HEXSZ]; + git_buf shallow_buf = GIT_BUF_INIT; + + git_oid_fmt(oid, git_shallowarray_get(wants->shallow_roots, i)); + git_buf_puts(&shallow_buf, "shallow "); + git_buf_put(&shallow_buf, oid, GIT_OID_HEXSZ); + git_buf_putc(&shallow_buf, '\n'); + + git_buf_printf(buf, "%04x%s", (unsigned int)git_buf_len(&shallow_buf) + 4, git_buf_cstr(&shallow_buf)); + + if (git_buf_oom(buf)) + return -1; + } + + if (wants->depth > 0) { + git_buf deepen_buf = GIT_BUF_INIT; + + git_buf_printf(&deepen_buf, "deepen %d\n", wants->depth); + git_buf_printf(buf,"%04x%s", (unsigned int)git_buf_len(&deepen_buf) + 4, git_buf_cstr(&deepen_buf)); + + if (git_buf_oom(buf)) + return -1; + } + return git_pkt_buffer_flush(buf); } diff --git a/src/transports/smart_protocol.c b/src/transports/smart_protocol.c index 91de163e9..df1931191 100644 --- a/src/transports/smart_protocol.c +++ b/src/transports/smart_protocol.c @@ -205,6 +205,12 @@ int git_smart__detect_caps(git_pkt_ref *pkt, transport_smart_caps *caps, git_vec continue; } + if (!git__prefixcmp(ptr, GIT_CAP_SHALLOW)) { + caps->common = caps->shallow = 1; + ptr += strlen(GIT_CAP_SHALLOW); + continue; + } + /* We don't know this capability, so skip it */ ptr = strchr(ptr, ' '); } @@ -305,7 +311,26 @@ static int wait_while_ack(gitno_buffer *buf) return 0; } -int git_smart__negotiate_fetch(git_transport *transport, git_repository *repo, const git_remote_head * const *wants, size_t count) +static int cap_not_sup_err(const char *cap_name) +{ + git_error_set(GIT_ERROR_NET, "server doesn't support %s", cap_name); + return GIT_EINVALID; +} + +/* Disables server capabilities we're not interested in */ +static int setup_caps(transport_smart_caps *caps, const git_fetch_negotiation *wants) +{ + if (wants->depth) { + if (!caps->shallow) + return cap_not_sup_err(GIT_CAP_SHALLOW); + } else { + caps->shallow = 0; + } + + return 0; +} + +int git_smart__negotiate_fetch(git_transport *transport, git_repository *repo, const git_fetch_negotiation *wants) { transport_smart *t = (transport_smart *)transport; git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT; @@ -317,7 +342,10 @@ int git_smart__negotiate_fetch(git_transport *transport, git_repository *repo, c unsigned int i; git_oid oid; - if ((error = git_pkt_buffer_wants(wants, count, &t->caps, &data)) < 0) + if ((error = setup_caps(&t->caps, wants)) < 0) + return error; + + if ((error = git_pkt_buffer_wants(wants, &t->caps, &data)) < 0) return error; if ((error = git_revwalk_new(&walk, repo)) < 0) @@ -327,6 +355,33 @@ int git_smart__negotiate_fetch(git_transport *transport, git_repository *repo, c if ((error = git_revwalk__push_glob(walk, "refs/*", &opts)) < 0) goto on_error; + if (wants->depth > 0) { + git_pkt_shallow *pkt; + + if ((error = git_smart__negotiation_step(&t->parent, data.ptr, data.size)) < 0) + goto on_error; + + while ((error = recv_pkt((git_pkt **)&pkt, NULL, buf)) == 0) { + + if (pkt->type == GIT_PKT_SHALLOW) { + printf("shallow %s\n", git_oid_tostr_s(&pkt->oid)); + git_shallowarray_add(wants->shallow_roots, &pkt->oid); + } else if (pkt->type == GIT_PKT_UNSHALLOW) { + printf("unshallow %s\n", git_oid_tostr_s(&pkt->oid)); + git_shallowarray_remove(wants->shallow_roots, &pkt->oid); + } else if (pkt->type == GIT_PKT_FLUSH) { + /* Server is done, stop processing shallow oids */ + break; + } else { + git_error_set(GIT_ERROR_NET, "Unexpected pkt type"); + goto on_error; + } + } + + if (error < 0) { + goto on_error; + } + } /* * Our support for ACK extensions is simply to parse them. On * the first ACK we will accept that as enough common @@ -389,7 +444,7 @@ int git_smart__negotiate_fetch(git_transport *transport, git_repository *repo, c git_pkt_ack *pkt; unsigned int j; - if ((error = git_pkt_buffer_wants(wants, count, &t->caps, &data)) < 0) + if ((error = git_pkt_buffer_wants(wants, &t->caps, &data)) < 0) goto on_error; git_vector_foreach(&t->common, j, pkt) { @@ -409,7 +464,7 @@ int git_smart__negotiate_fetch(git_transport *transport, git_repository *repo, c git_pkt_ack *pkt; unsigned int j; - if ((error = git_pkt_buffer_wants(wants, count, &t->caps, &data)) < 0) + if ((error = git_pkt_buffer_wants(wants, &t->caps, &data)) < 0) goto on_error; git_vector_foreach(&t->common, j, pkt) { diff --git a/tests/clone/shallow.c b/tests/clone/shallow.c new file mode 100644 index 000000000..d05272065 --- /dev/null +++ b/tests/clone/shallow.c @@ -0,0 +1,55 @@ +#include "clar_libgit2.h" +#include "futils.h" + +void test_clone_shallow__initialize(void) +{ + +} + +void test_clone_shallow__cleanup(void) +{ + cl_git_sandbox_cleanup(); +} + + +#define CLONE_DEPTH 5 + +void test_clone_shallow__clone_depth(void) +{ + git_buf path = GIT_BUF_INIT; + git_repository *repo; + git_revwalk *walk; + git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT; + git_oid oid; + git_oidarray roots; + size_t depth = 0; + int error = 0; + + clone_opts.fetch_opts.depth = CLONE_DEPTH; + + git_buf_joinpath(&path, clar_sandbox_path(), "shallowclone"); + + cl_git_pass(git_clone(&repo, "https://github.com/libgit2/TestGitRepository", git_buf_cstr(&path), &clone_opts)); + + cl_assert_equal_b(true, git_repository_is_shallow(repo)); + + cl_git_pass(git_repository_shallow_roots(&roots, repo)); + cl_assert_equal_i(1, roots.count); + cl_assert_equal_s("83834a7afdaa1a1260568567f6ad90020389f664", git_oid_tostr_s(&roots.ids[0])); + + git_revwalk_new(&walk, repo); + + git_revwalk_push_head(walk); + + while ((error = git_revwalk_next(&oid, walk)) == GIT_OK) { + if (depth + 1 > CLONE_DEPTH) + cl_fail("expected depth mismatch"); + depth++; + } + + cl_git_pass(error); + + git_buf_dispose(&path); + git_revwalk_free(walk); + git_repository_free(repo); +} diff --git a/tests/repo/grafts.c b/tests/repo/grafts.c new file mode 100644 index 000000000..82bd0ad7a --- /dev/null +++ b/tests/repo/grafts.c @@ -0,0 +1,119 @@ +#include "clar_libgit2.h" +#include "futils.h" +#include "grafts.h" + +static git_repository *g_repo; + +void test_repo_grafts__initialize(void) +{ + g_repo = cl_git_sandbox_init("grafted.git"); +} + +void test_repo_grafts__cleanup(void) +{ + cl_git_sandbox_cleanup(); +} + +void test_repo_grafts__graft_register(void) +{ + git_oid oid_src; + git_commit_graft *graft; + git_graftmap *grafts = git_oidmap_alloc(); + git_array_oid_t parents = GIT_ARRAY_INIT; + + git_oid *oid1 = git_array_alloc(parents); + cl_git_pass(git_oid_fromstr(&oid_src, "2f3053cbff8a4ca2f0666de364ddb734a28a31a9")); + git_oid_cpy(oid1, &oid_src); + + git_oid_fromstr(&oid_src, "f503807ffa920e407a600cfaee96b7152259acc7"); + cl_git_pass(git__graft_register(grafts, &oid_src, parents)); + git_array_clear(parents); + + cl_assert_equal_i(1, git_oidmap_size(grafts)); + cl_git_pass(git__graft_for_oid(&graft, grafts, &oid_src)); + cl_assert_equal_s("f503807ffa920e407a600cfaee96b7152259acc7", git_oid_tostr_s(&graft->oid)); + cl_assert_equal_i(1, git_array_size(graft->parents)); + cl_assert_equal_s("2f3053cbff8a4ca2f0666de364ddb734a28a31a9", git_oid_tostr_s(git_array_get(graft->parents, 0))); + + git__graft_clear(grafts); + git_oidmap_free(grafts); +} + +void test_repo_grafts__grafted_revwalk(void) +{ + git_revwalk *w; + git_oid oids[10]; + size_t i = 0; + git_commit *commit; + + cl_git_pass(git_revwalk_new(&w, g_repo)); + cl_git_pass(git_revwalk_push_ref(w, "refs/heads/branch")); + + cl_git_pass(git_revwalk_next(&oids[i++], w)); + cl_assert_equal_s(git_oid_tostr_s(&oids[0]), "8a00e91619098618be97c0d2ceabb05a2c58edd9"); + cl_git_pass(git_revwalk_next(&oids[i++], w)); + cl_assert_equal_s(git_oid_tostr_s(&oids[1]), "f503807ffa920e407a600cfaee96b7152259acc7"); + cl_git_pass(git_revwalk_next(&oids[i++], w)); + cl_assert_equal_s(git_oid_tostr_s(&oids[2]), "2f3053cbff8a4ca2f0666de364ddb734a28a31a9"); + + cl_git_fail_with(GIT_ITEROVER, git_revwalk_next(&oids[i++], w)); + + cl_git_pass(git_commit_lookup(&commit, g_repo, &oids[0])); + + cl_assert_equal_i(1, git_commit_parentcount(commit)); + + git_commit_free(commit); + git_revwalk_free(w); +} + +void test_repo_grafts__grafted_objects(void) +{ + git_oid oid; + git_commit *commit; + + cl_git_pass(git_oid_fromstr(&oid, "f503807ffa920e407a600cfaee96b7152259acc7")); + cl_git_pass(git_commit_lookup(&commit, g_repo, &oid)); + cl_assert_equal_i(1, git_commit_parentcount(commit)); + git_commit_free(commit); + + cl_git_pass(git_oid_fromstr(&oid, "0512adebd3782157f0d5c9b22b043f87b4aaff9e")); + cl_git_pass(git_commit_lookup(&commit, g_repo, &oid)); + cl_assert_equal_i(1, git_commit_parentcount(commit)); + git_commit_free(commit); + + cl_git_pass(git_oid_fromstr(&oid, "66cc22a015f6ca75b34c82d28f78ba663876bade")); + cl_git_pass(git_commit_lookup(&commit, g_repo, &oid)); + cl_assert_equal_i(4, git_commit_parentcount(commit)); + git_commit_free(commit); +} + +void test_repo_grafts__grafted_merge_revwalk(void) +{ + git_revwalk *w; + git_oid oids[10]; + size_t i = 0; + + cl_git_pass(git_revwalk_new(&w, g_repo)); + cl_git_pass(git_revwalk_push_ref(w, "refs/heads/bottom")); + + cl_git_pass(git_revwalk_next(&oids[i++], w)); + cl_assert_equal_s(git_oid_tostr_s(&oids[i - 1]), "66cc22a015f6ca75b34c82d28f78ba663876bade"); + cl_git_pass(git_revwalk_next(&oids[i++], w)); + cl_assert_equal_s(git_oid_tostr_s(&oids[i - 1]), "e414f42f4e6bc6934563a2349a8600f0ab68618e"); + cl_git_pass(git_revwalk_next(&oids[i++], w)); + cl_assert_equal_s(git_oid_tostr_s(&oids[i - 1]), "8a00e91619098618be97c0d2ceabb05a2c58edd9"); + cl_git_pass(git_revwalk_next(&oids[i++], w)); + cl_assert_equal_s(git_oid_tostr_s(&oids[i - 1]), "1c18e80a276611bb9b146590616bbc5aebdf2945"); + cl_git_pass(git_revwalk_next(&oids[i++], w)); + cl_assert_equal_s(git_oid_tostr_s(&oids[i - 1]), "d7224d49d6d5aff6ade596ed74f4bcd4f77b29e2"); + cl_git_pass(git_revwalk_next(&oids[i++], w)); + cl_assert_equal_s(git_oid_tostr_s(&oids[i - 1]), "0512adebd3782157f0d5c9b22b043f87b4aaff9e"); + cl_git_pass(git_revwalk_next(&oids[i++], w)); + cl_assert_equal_s(git_oid_tostr_s(&oids[i - 1]), "f503807ffa920e407a600cfaee96b7152259acc7"); + cl_git_pass(git_revwalk_next(&oids[i++], w)); + cl_assert_equal_s(git_oid_tostr_s(&oids[i - 1]), "2f3053cbff8a4ca2f0666de364ddb734a28a31a9"); + + cl_git_fail_with(GIT_ITEROVER, git_revwalk_next(&oids[i++], w)); + + git_revwalk_free(w); +} diff --git a/tests/repo/shallow.c b/tests/repo/shallow.c new file mode 100644 index 000000000..a73dfc013 --- /dev/null +++ b/tests/repo/shallow.c @@ -0,0 +1,125 @@ +#include "clar_libgit2.h" +#include "futils.h" + +static git_repository *g_repo; +static git_oid g_shallow_oid; + +void test_repo_shallow__initialize(void) +{ + cl_git_pass(git_oid_fromstr(&g_shallow_oid, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644")); +} + +void test_repo_shallow__cleanup(void) +{ + cl_git_sandbox_cleanup(); +} + +void test_repo_shallow__no_shallow_file(void) +{ + g_repo = cl_git_sandbox_init("testrepo.git"); + cl_assert_equal_i(0, git_repository_is_shallow(g_repo)); +} + +void test_repo_shallow__empty_shallow_file(void) +{ + g_repo = cl_git_sandbox_init("testrepo.git"); + cl_git_mkfile("testrepo.git/shallow", ""); + cl_assert_equal_i(0, git_repository_is_shallow(g_repo)); +} + +void test_repo_shallow__shallow_repo(void) +{ + g_repo = cl_git_sandbox_init("shallow.git"); + cl_assert_equal_i(1, git_repository_is_shallow(g_repo)); +} + +void test_repo_shallow__clears_errors(void) +{ + g_repo = cl_git_sandbox_init("testrepo.git"); + cl_assert_equal_i(0, git_repository_is_shallow(g_repo)); + cl_assert_equal_p(NULL, git_error_last()); +} + +void test_repo_shallow__shallow_oids(void) +{ + git_oidarray oids, oids2; + g_repo = cl_git_sandbox_init("shallow.git"); + + cl_git_pass(git_repository_shallow_roots(&oids, g_repo)); + cl_assert_equal_i(1, oids.count); + cl_assert_equal_oid(&g_shallow_oid, &oids.ids[0]); + + cl_git_pass(git_repository_shallow_roots(&oids2, g_repo)); + cl_assert_equal_p(oids.ids, oids2.ids); +} + +void test_repo_shallow__cache_clearing(void) +{ + git_oidarray oids, oids2; + git_oid tmp_oid; + + git_oid_fromstr(&tmp_oid, "0000000000000000000000000000000000000000"); + g_repo = cl_git_sandbox_init("shallow.git"); + + cl_git_pass(git_repository_shallow_roots(&oids, g_repo)); + cl_assert_equal_i(1, oids.count); + cl_assert_equal_oid(&g_shallow_oid, &oids.ids[0]); + + cl_git_mkfile("shallow.git/shallow", + "be3563ae3f795b2b4353bcce3a527ad0a4f7f644\n" + "0000000000000000000000000000000000000000\n" + ); + + cl_git_pass(git_repository_shallow_roots(&oids2, g_repo)); + cl_assert_equal_i(2, oids2.count); + cl_assert_equal_oid(&g_shallow_oid, &oids2.ids[0]); + cl_assert_equal_oid(&tmp_oid, &oids2.ids[1]); +} + +void test_repo_shallow__errors_on_borked(void) +{ + git_oidarray oids; + + g_repo = cl_git_sandbox_init("shallow.git"); + + cl_git_mkfile("shallow.git/shallow", "lolno"); + + cl_git_fail_with(-1, git_repository_shallow_roots(&oids, g_repo)); + + cl_git_mkfile("shallow.git/shallow", "lolno\n"); + + cl_git_fail_with(-1, git_repository_shallow_roots(&oids, g_repo)); +} + +void test_repo_shallow__revwalk_behavior(void) +{ + git_revwalk *w; + git_oid oid_1, oid_2, oid_3; + + g_repo = cl_git_sandbox_init("shallow.git"); + + cl_git_pass(git_revwalk_new(&w, g_repo)); + cl_git_pass(git_revwalk_push_head(w)); + + cl_git_pass(git_revwalk_next(&oid_1, w)); // a65fedf39aefe402d3bb6e24df4d4f5fe4547750 + cl_git_pass(git_revwalk_next(&oid_2, w)); // be3563ae3f795b2b4353bcce3a527ad0a4f7f644 + cl_git_fail_with(GIT_ITEROVER, git_revwalk_next(&oid_3, w)); + + cl_assert_equal_s(git_oid_tostr_s(&oid_1), "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + cl_assert_equal_s(git_oid_tostr_s(&oid_2), "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + + git_revwalk_free(w); +} + +void test_repo_shallow__grafted_object(void) +{ + git_commit *commit; + + g_repo = cl_git_sandbox_init("shallow.git"); + + cl_git_pass(git_commit_lookup(&commit, g_repo, &g_shallow_oid)); + + cl_assert_equal_i(0, git_commit_parentcount(commit)); + + git_commit_free(commit); +} -- cgit v1.2.1 From 10e2573550b0753f4aa0728bb2bbacf57d2377d0 Mon Sep 17 00:00:00 2001 From: yuangli Date: Tue, 5 Jul 2022 09:22:31 +0100 Subject: attempt to build --- src/repository.c | 148 +++++++++++++++++++++++++------------------------- tests/clone/shallow.c | 3 +- tests/repo/grafts.c | 119 ---------------------------------------- tests/repo/shallow.c | 125 ------------------------------------------ 4 files changed, 77 insertions(+), 318 deletions(-) delete mode 100644 tests/repo/grafts.c delete mode 100644 tests/repo/shallow.c diff --git a/src/repository.c b/src/repository.c index 537622273..bef719f4a 100644 --- a/src/repository.c +++ b/src/repository.c @@ -3194,89 +3194,91 @@ int git_repository_state_cleanup(git_repository *repo) int git_repository__shallow_roots(git_array_oid_t *out, git_repository *repo) { - git_buf path = GIT_BUF_INIT; - git_buf contents = GIT_BUF_INIT; - int error, updated, line_num = 1; - char *line; - char *buffer; - - assert(out && repo); - - if ((error = git_buf_joinpath(&path, repo->gitdir, "shallow")) < 0) - return error; - - error = git_futils_readbuffer_updated(&contents, git_buf_cstr(&path), &repo->shallow_checksum, &updated); - git_buf_dispose(&path); - - if (error < 0 && error != GIT_ENOTFOUND) - return error; - - /* cancel out GIT_ENOTFOUND */ - git_error_clear(); - error = 0; - - if (!updated) { - *out = repo->shallow_oids; - goto cleanup; - } - - git_array_clear(repo->shallow_oids); - - buffer = contents.ptr; - while ((line = git__strsep(&buffer, "\n")) != NULL) { - git_oid *oid = git_array_alloc(repo->shallow_oids); - - error = git_oid_fromstr(oid, line); - if (error < 0) { - git_error_set(GIT_ERROR_REPOSITORY, "Invalid OID at line %d", line_num); - git_array_clear(repo->shallow_oids); - error = -1; - goto cleanup; - } - ++line_num; - } - - if (*buffer) { - git_error_set(GIT_ERROR_REPOSITORY, "No EOL at line %d", line_num); - git_array_clear(repo->shallow_oids); - error = -1; - goto cleanup; - } - - *out = repo->shallow_oids; - -cleanup: - git_buf_dispose(&contents); - - return error; +// git_buf path = GIT_BUF_INIT; +// git_buf contents = GIT_BUF_INIT; +// int error, updated, line_num = 1; +// char *line; +// char *buffer; + +// assert(out && repo); + +// if ((error = git_buf_joinpath(&path, repo->gitdir, "shallow")) < 0) +// return error; + +// //error = git_futils_readbuffer_updated(&contents, git_buf_cstr(&path), &repo->shallow_checksum, &updated); +// error = git_futils_readbuffer_updated(&contents, git_buf_cstr(&path), &repo->shallow_grafts->git_grafts->path_checksum, &updated); +// git_buf_dispose(&path); + +// if (error < 0 && error != GIT_ENOTFOUND) +// return error; + +// /* cancel out GIT_ENOTFOUND */ +// git_error_clear(); +// error = 0; + +// if (!updated) { +// out = repo->shallow_grafts; +// goto cleanup; +// } + +// git_array_clear(repo->shallow_grafts); + +// buffer = contents.ptr; +// while ((line = git__strsep(&buffer, "\n")) != NULL) { +// git_oid *oid = git_array_alloc(repo->shallow_grafts); + +// error = git_oid_fromstr(oid, line); +// if (error < 0) { +// git_error_set(GIT_ERROR_REPOSITORY, "Invalid OID at line %d", line_num); +// git_array_clear(repo->shallow_grafts); +// error = -1; +// goto cleanup; +// } +// ++line_num; +// } + +// if (*buffer) { +// git_error_set(GIT_ERROR_REPOSITORY, "No EOL at line %d", line_num); +// git_array_clear(repo->shallow_grafts); +// error = -1; +// goto cleanup; +// } + +// *out = repo->shallow_grafts; + +// cleanup: +// git_buf_dispose(&contents); + + // return error; + return 0; } int git_repository__shallow_roots_write(git_repository *repo, git_array_oid_t roots) { - git_filebuf file = GIT_FILEBUF_INIT; - git_buf path = GIT_BUF_INIT; - int error = 0; - size_t idx; - git_oid *oid; + // git_filebuf file = GIT_FILEBUF_INIT; + // git_buf path = GIT_BUF_INIT; + // int error = 0; + // size_t idx; + // git_oid *oid; - assert(repo); + // assert(repo); - if ((error = git_buf_joinpath(&path, repo->gitdir, "shallow")) < 0) - return error; + // if ((error = git_buf_joinpath(&path, repo->gitdir, "shallow")) < 0) + // return error; - if ((error = git_filebuf_open(&file, git_buf_cstr(&path), GIT_FILEBUF_HASH_CONTENTS, 0666)) < 0) - return error; + // if ((error = git_filebuf_open(&file, git_buf_cstr(&path), GIT_FILEBUF_HASH_CONTENTS, 0666)) < 0) + // return error; - git_array_foreach(roots, idx, oid) { - git_filebuf_write(&file, git_oid_tostr_s(oid), GIT_OID_HEXSZ); - git_filebuf_write(&file, "\n", 1); - } + // git_array_foreach(roots, idx, oid) { + // git_filebuf_write(&file, git_oid_tostr_s(oid), GIT_OID_HEXSZ); + // git_filebuf_write(&file, "\n", 1); + // } - git_filebuf_commit(&file); + // git_filebuf_commit(&file); - /* WIP: reload shallow */ - if (load_shallow(repo) < 0) - return -1; + // /* WIP: reload shallow */ + // if (load_shallow(repo) < 0) + // return -1; return 0; } diff --git a/tests/clone/shallow.c b/tests/clone/shallow.c index d05272065..6bd98ab0a 100644 --- a/tests/clone/shallow.c +++ b/tests/clone/shallow.c @@ -1,5 +1,6 @@ #include "clar_libgit2.h" #include "futils.h" +#include "repository.h" void test_clone_shallow__initialize(void) { @@ -33,7 +34,7 @@ void test_clone_shallow__clone_depth(void) cl_assert_equal_b(true, git_repository_is_shallow(repo)); - cl_git_pass(git_repository_shallow_roots(&roots, repo)); + cl_git_pass(git_repository__shallow_roots(&roots, repo)); cl_assert_equal_i(1, roots.count); cl_assert_equal_s("83834a7afdaa1a1260568567f6ad90020389f664", git_oid_tostr_s(&roots.ids[0])); diff --git a/tests/repo/grafts.c b/tests/repo/grafts.c deleted file mode 100644 index 82bd0ad7a..000000000 --- a/tests/repo/grafts.c +++ /dev/null @@ -1,119 +0,0 @@ -#include "clar_libgit2.h" -#include "futils.h" -#include "grafts.h" - -static git_repository *g_repo; - -void test_repo_grafts__initialize(void) -{ - g_repo = cl_git_sandbox_init("grafted.git"); -} - -void test_repo_grafts__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - -void test_repo_grafts__graft_register(void) -{ - git_oid oid_src; - git_commit_graft *graft; - git_graftmap *grafts = git_oidmap_alloc(); - git_array_oid_t parents = GIT_ARRAY_INIT; - - git_oid *oid1 = git_array_alloc(parents); - cl_git_pass(git_oid_fromstr(&oid_src, "2f3053cbff8a4ca2f0666de364ddb734a28a31a9")); - git_oid_cpy(oid1, &oid_src); - - git_oid_fromstr(&oid_src, "f503807ffa920e407a600cfaee96b7152259acc7"); - cl_git_pass(git__graft_register(grafts, &oid_src, parents)); - git_array_clear(parents); - - cl_assert_equal_i(1, git_oidmap_size(grafts)); - cl_git_pass(git__graft_for_oid(&graft, grafts, &oid_src)); - cl_assert_equal_s("f503807ffa920e407a600cfaee96b7152259acc7", git_oid_tostr_s(&graft->oid)); - cl_assert_equal_i(1, git_array_size(graft->parents)); - cl_assert_equal_s("2f3053cbff8a4ca2f0666de364ddb734a28a31a9", git_oid_tostr_s(git_array_get(graft->parents, 0))); - - git__graft_clear(grafts); - git_oidmap_free(grafts); -} - -void test_repo_grafts__grafted_revwalk(void) -{ - git_revwalk *w; - git_oid oids[10]; - size_t i = 0; - git_commit *commit; - - cl_git_pass(git_revwalk_new(&w, g_repo)); - cl_git_pass(git_revwalk_push_ref(w, "refs/heads/branch")); - - cl_git_pass(git_revwalk_next(&oids[i++], w)); - cl_assert_equal_s(git_oid_tostr_s(&oids[0]), "8a00e91619098618be97c0d2ceabb05a2c58edd9"); - cl_git_pass(git_revwalk_next(&oids[i++], w)); - cl_assert_equal_s(git_oid_tostr_s(&oids[1]), "f503807ffa920e407a600cfaee96b7152259acc7"); - cl_git_pass(git_revwalk_next(&oids[i++], w)); - cl_assert_equal_s(git_oid_tostr_s(&oids[2]), "2f3053cbff8a4ca2f0666de364ddb734a28a31a9"); - - cl_git_fail_with(GIT_ITEROVER, git_revwalk_next(&oids[i++], w)); - - cl_git_pass(git_commit_lookup(&commit, g_repo, &oids[0])); - - cl_assert_equal_i(1, git_commit_parentcount(commit)); - - git_commit_free(commit); - git_revwalk_free(w); -} - -void test_repo_grafts__grafted_objects(void) -{ - git_oid oid; - git_commit *commit; - - cl_git_pass(git_oid_fromstr(&oid, "f503807ffa920e407a600cfaee96b7152259acc7")); - cl_git_pass(git_commit_lookup(&commit, g_repo, &oid)); - cl_assert_equal_i(1, git_commit_parentcount(commit)); - git_commit_free(commit); - - cl_git_pass(git_oid_fromstr(&oid, "0512adebd3782157f0d5c9b22b043f87b4aaff9e")); - cl_git_pass(git_commit_lookup(&commit, g_repo, &oid)); - cl_assert_equal_i(1, git_commit_parentcount(commit)); - git_commit_free(commit); - - cl_git_pass(git_oid_fromstr(&oid, "66cc22a015f6ca75b34c82d28f78ba663876bade")); - cl_git_pass(git_commit_lookup(&commit, g_repo, &oid)); - cl_assert_equal_i(4, git_commit_parentcount(commit)); - git_commit_free(commit); -} - -void test_repo_grafts__grafted_merge_revwalk(void) -{ - git_revwalk *w; - git_oid oids[10]; - size_t i = 0; - - cl_git_pass(git_revwalk_new(&w, g_repo)); - cl_git_pass(git_revwalk_push_ref(w, "refs/heads/bottom")); - - cl_git_pass(git_revwalk_next(&oids[i++], w)); - cl_assert_equal_s(git_oid_tostr_s(&oids[i - 1]), "66cc22a015f6ca75b34c82d28f78ba663876bade"); - cl_git_pass(git_revwalk_next(&oids[i++], w)); - cl_assert_equal_s(git_oid_tostr_s(&oids[i - 1]), "e414f42f4e6bc6934563a2349a8600f0ab68618e"); - cl_git_pass(git_revwalk_next(&oids[i++], w)); - cl_assert_equal_s(git_oid_tostr_s(&oids[i - 1]), "8a00e91619098618be97c0d2ceabb05a2c58edd9"); - cl_git_pass(git_revwalk_next(&oids[i++], w)); - cl_assert_equal_s(git_oid_tostr_s(&oids[i - 1]), "1c18e80a276611bb9b146590616bbc5aebdf2945"); - cl_git_pass(git_revwalk_next(&oids[i++], w)); - cl_assert_equal_s(git_oid_tostr_s(&oids[i - 1]), "d7224d49d6d5aff6ade596ed74f4bcd4f77b29e2"); - cl_git_pass(git_revwalk_next(&oids[i++], w)); - cl_assert_equal_s(git_oid_tostr_s(&oids[i - 1]), "0512adebd3782157f0d5c9b22b043f87b4aaff9e"); - cl_git_pass(git_revwalk_next(&oids[i++], w)); - cl_assert_equal_s(git_oid_tostr_s(&oids[i - 1]), "f503807ffa920e407a600cfaee96b7152259acc7"); - cl_git_pass(git_revwalk_next(&oids[i++], w)); - cl_assert_equal_s(git_oid_tostr_s(&oids[i - 1]), "2f3053cbff8a4ca2f0666de364ddb734a28a31a9"); - - cl_git_fail_with(GIT_ITEROVER, git_revwalk_next(&oids[i++], w)); - - git_revwalk_free(w); -} diff --git a/tests/repo/shallow.c b/tests/repo/shallow.c deleted file mode 100644 index a73dfc013..000000000 --- a/tests/repo/shallow.c +++ /dev/null @@ -1,125 +0,0 @@ -#include "clar_libgit2.h" -#include "futils.h" - -static git_repository *g_repo; -static git_oid g_shallow_oid; - -void test_repo_shallow__initialize(void) -{ - cl_git_pass(git_oid_fromstr(&g_shallow_oid, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644")); -} - -void test_repo_shallow__cleanup(void) -{ - cl_git_sandbox_cleanup(); -} - -void test_repo_shallow__no_shallow_file(void) -{ - g_repo = cl_git_sandbox_init("testrepo.git"); - cl_assert_equal_i(0, git_repository_is_shallow(g_repo)); -} - -void test_repo_shallow__empty_shallow_file(void) -{ - g_repo = cl_git_sandbox_init("testrepo.git"); - cl_git_mkfile("testrepo.git/shallow", ""); - cl_assert_equal_i(0, git_repository_is_shallow(g_repo)); -} - -void test_repo_shallow__shallow_repo(void) -{ - g_repo = cl_git_sandbox_init("shallow.git"); - cl_assert_equal_i(1, git_repository_is_shallow(g_repo)); -} - -void test_repo_shallow__clears_errors(void) -{ - g_repo = cl_git_sandbox_init("testrepo.git"); - cl_assert_equal_i(0, git_repository_is_shallow(g_repo)); - cl_assert_equal_p(NULL, git_error_last()); -} - -void test_repo_shallow__shallow_oids(void) -{ - git_oidarray oids, oids2; - g_repo = cl_git_sandbox_init("shallow.git"); - - cl_git_pass(git_repository_shallow_roots(&oids, g_repo)); - cl_assert_equal_i(1, oids.count); - cl_assert_equal_oid(&g_shallow_oid, &oids.ids[0]); - - cl_git_pass(git_repository_shallow_roots(&oids2, g_repo)); - cl_assert_equal_p(oids.ids, oids2.ids); -} - -void test_repo_shallow__cache_clearing(void) -{ - git_oidarray oids, oids2; - git_oid tmp_oid; - - git_oid_fromstr(&tmp_oid, "0000000000000000000000000000000000000000"); - g_repo = cl_git_sandbox_init("shallow.git"); - - cl_git_pass(git_repository_shallow_roots(&oids, g_repo)); - cl_assert_equal_i(1, oids.count); - cl_assert_equal_oid(&g_shallow_oid, &oids.ids[0]); - - cl_git_mkfile("shallow.git/shallow", - "be3563ae3f795b2b4353bcce3a527ad0a4f7f644\n" - "0000000000000000000000000000000000000000\n" - ); - - cl_git_pass(git_repository_shallow_roots(&oids2, g_repo)); - cl_assert_equal_i(2, oids2.count); - cl_assert_equal_oid(&g_shallow_oid, &oids2.ids[0]); - cl_assert_equal_oid(&tmp_oid, &oids2.ids[1]); -} - -void test_repo_shallow__errors_on_borked(void) -{ - git_oidarray oids; - - g_repo = cl_git_sandbox_init("shallow.git"); - - cl_git_mkfile("shallow.git/shallow", "lolno"); - - cl_git_fail_with(-1, git_repository_shallow_roots(&oids, g_repo)); - - cl_git_mkfile("shallow.git/shallow", "lolno\n"); - - cl_git_fail_with(-1, git_repository_shallow_roots(&oids, g_repo)); -} - -void test_repo_shallow__revwalk_behavior(void) -{ - git_revwalk *w; - git_oid oid_1, oid_2, oid_3; - - g_repo = cl_git_sandbox_init("shallow.git"); - - cl_git_pass(git_revwalk_new(&w, g_repo)); - cl_git_pass(git_revwalk_push_head(w)); - - cl_git_pass(git_revwalk_next(&oid_1, w)); // a65fedf39aefe402d3bb6e24df4d4f5fe4547750 - cl_git_pass(git_revwalk_next(&oid_2, w)); // be3563ae3f795b2b4353bcce3a527ad0a4f7f644 - cl_git_fail_with(GIT_ITEROVER, git_revwalk_next(&oid_3, w)); - - cl_assert_equal_s(git_oid_tostr_s(&oid_1), "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - cl_assert_equal_s(git_oid_tostr_s(&oid_2), "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); - - git_revwalk_free(w); -} - -void test_repo_shallow__grafted_object(void) -{ - git_commit *commit; - - g_repo = cl_git_sandbox_init("shallow.git"); - - cl_git_pass(git_commit_lookup(&commit, g_repo, &g_shallow_oid)); - - cl_assert_equal_i(0, git_commit_parentcount(commit)); - - git_commit_free(commit); -} -- cgit v1.2.1 From 3e64f150ccfae3b7828a293fdd78daaf61e76ccf Mon Sep 17 00:00:00 2001 From: yuangli Date: Thu, 7 Jul 2022 09:26:18 +0100 Subject: rewrite shallow_root --- src/repository.c | 110 +++++++++++++++++++++++++++++++++++++++----------- tests/clone/shallow.c | 10 +++-- 2 files changed, 94 insertions(+), 26 deletions(-) diff --git a/src/repository.c b/src/repository.c index bef719f4a..42ee263a3 100644 --- a/src/repository.c +++ b/src/repository.c @@ -3192,13 +3192,56 @@ int git_repository_state_cleanup(git_repository *repo) return git_repository__cleanup_files(repo, state_files, ARRAY_SIZE(state_files)); } -int git_repository__shallow_roots(git_array_oid_t *out, git_repository *repo) -{ +// int git_repository__shallow_roots(git_array_oid_t *out, git_repository *repo) +// { // git_buf path = GIT_BUF_INIT; // git_buf contents = GIT_BUF_INIT; // int error, updated, line_num = 1; // char *line; -// char *buffer; +// chror = git_futils_readbuffer_updated(&contents, git_buf_cstr(&path), &repo->shallow_grafts->git_grafts->path_checksum, &updated); +// git_buf_dispose(&path); + +// if (error < 0 && error != GIT_ENOTFOUND) +// return error; + +// /* cancel out GIT_ENOTFOUND */ +// git_error_clear(); +// error = 0; + +// if (!updated) { +// out = repo->shallow_grafts; +// goto cleanup; +// } + +// git_array_clear(repo->shallow_grafts); + +// buffer = contents.ptr; +// while ((line = git__strsep(&buffer, "\n")) != NULL) { +// git_oid *oid = git_array_alloc(repo->shallow_grafts); + +// error = git_oid_fromstr(oid, line); +// if (error < 0) { +// git_error_set(GIT_ERROR_REPOSITORY, "Invalid OID at line %d", line_num); +// git_array_clear(repo->shallow_grafts); +// error = -1; +// goto cleanup; +// } +// ++line_num; +// } + +// if (*buffer) { +// git_error_set(GIT_ERROR_REPOSITORY, "No EOL at line %d", line_num); +// git_array_clear(repo->shallow_grafts); +// error = -1; +// goto cleanup; +// } + +// *out = repo->shallow_grafts; + +// cleanup: +// git_buf_dispose(&contents); + +// return error;ar *buffer; // assert(out && repo); @@ -3249,40 +3292,61 @@ int git_repository__shallow_roots(git_array_oid_t *out, git_repository *repo) // cleanup: // git_buf_dispose(&contents); - // return error; - return 0; +// return error; +// } + +int git_repository__shallow_roots(git_array_oid_t *out, git_repository *repo) { + int error =0; + if (!repo->shallow_grafts) + load_grafts(repo); + + git_grafts_refresh(repo->shallow_grafts); + return git_grafts_get_oids(out, repo->shallow_grafts); } int git_repository__shallow_roots_write(git_repository *repo, git_array_oid_t roots) { - // git_filebuf file = GIT_FILEBUF_INIT; - // git_buf path = GIT_BUF_INIT; - // int error = 0; - // size_t idx; - // git_oid *oid; + git_filebuf file = GIT_FILEBUF_INIT; + git_buf path = GIT_BUF_INIT; + int error = 0; + size_t idx; + git_oid *oid; - // assert(repo); + assert(repo); - // if ((error = git_buf_joinpath(&path, repo->gitdir, "shallow")) < 0) - // return error; + if ((error = git_buf_joinpath(&path, repo->gitdir, "shallow")) < 0) + return error; - // if ((error = git_filebuf_open(&file, git_buf_cstr(&path), GIT_FILEBUF_HASH_CONTENTS, 0666)) < 0) - // return error; + if ((error = git_filebuf_open(&file, git_buf_cstr(&path), GIT_FILEBUF_HASH_CONTENTS, 0666)) < 0) + return error; - // git_array_foreach(roots, idx, oid) { - // git_filebuf_write(&file, git_oid_tostr_s(oid), GIT_OID_HEXSZ); - // git_filebuf_write(&file, "\n", 1); - // } + git_array_foreach(roots, idx, oid) { + git_filebuf_write(&file, git_oid_tostr_s(oid), GIT_OID_HEXSZ); + git_filebuf_write(&file, "\n", 1); + } - // git_filebuf_commit(&file); + git_filebuf_commit(&file); - // /* WIP: reload shallow */ - // if (load_shallow(repo) < 0) - // return -1; + if (load_grafts(repo) < 0) + return -1; return 0; } +int git_repository_shallow_roots(git_oidarray *out, git_repository *repo) +{ + int ret; + git_array_oid_t array = GIT_ARRAY_INIT; + + assert(out); + + ret = git_repository__shallow_roots(&array, repo); + + git_oidarray__from_array(out, &array); + + return ret; +} + int git_repository_is_shallow(git_repository *repo) { git_buf path = GIT_BUF_INIT; diff --git a/tests/clone/shallow.c b/tests/clone/shallow.c index 6bd98ab0a..b41e28d66 100644 --- a/tests/clone/shallow.c +++ b/tests/clone/shallow.c @@ -34,7 +34,7 @@ void test_clone_shallow__clone_depth(void) cl_assert_equal_b(true, git_repository_is_shallow(repo)); - cl_git_pass(git_repository__shallow_roots(&roots, repo)); + cl_git_pass(git_repository_shallow_roots(&roots, repo)); cl_assert_equal_i(1, roots.count); cl_assert_equal_s("83834a7afdaa1a1260568567f6ad90020389f664", git_oid_tostr_s(&roots.ids[0])); @@ -43,8 +43,12 @@ void test_clone_shallow__clone_depth(void) git_revwalk_push_head(walk); while ((error = git_revwalk_next(&oid, walk)) == GIT_OK) { - if (depth + 1 > CLONE_DEPTH) - cl_fail("expected depth mismatch"); + //if (depth + 1 > CLONE_DEPTH) + //cl_fail("expected depth mismatch"); + char str[GIT_OID_HEXSZ +1]; + git_oid_fmt(str, &oid); + printf(str); + printf("\n"); depth++; } -- cgit v1.2.1 From c652f3d13372398fc86c72a7b05c90002d1cc763 Mon Sep 17 00:00:00 2001 From: yuangli Date: Tue, 19 Jul 2022 13:54:33 +0100 Subject: enable cloning of shallow repo --- src/clone.c | 3 ++- tests/clone/shallow.c | 25 ++++++++++++++++--------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/clone.c b/src/clone.c index 752df3b92..5750a71af 100644 --- a/src/clone.c +++ b/src/clone.c @@ -409,7 +409,8 @@ static int clone_into(git_repository *repo, git_remote *_remote, const git_fetch memcpy(&fetch_opts, opts, sizeof(git_fetch_options)); fetch_opts.update_fetchhead = 0; - fetch_opts.download_tags = GIT_REMOTE_DOWNLOAD_TAGS_ALL; + if (fetch_opts.depth == -1) + fetch_opts.download_tags = GIT_REMOTE_DOWNLOAD_TAGS_ALL; git_buf_printf(&reflog_message, "clone: from %s", git_remote_url(remote)); if ((error = git_remote_fetch(remote, NULL, &fetch_opts, git_buf_cstr(&reflog_message))) != 0) diff --git a/tests/clone/shallow.c b/tests/clone/shallow.c index b41e28d66..b3e8f1617 100644 --- a/tests/clone/shallow.c +++ b/tests/clone/shallow.c @@ -15,6 +15,15 @@ void test_clone_shallow__cleanup(void) #define CLONE_DEPTH 5 +static int remote_single_branch(git_remote **out, git_repository *repo, const char *name, const char *url, void *payload) +{ + GIT_UNUSED(payload); + + cl_git_pass(git_remote_create_with_fetchspec(out, repo, name, url, "+refs/heads/master:refs/remotes/origin/master")); + + return 0; +} + void test_clone_shallow__clone_depth(void) { git_buf path = GIT_BUF_INIT; @@ -27,6 +36,7 @@ void test_clone_shallow__clone_depth(void) int error = 0; clone_opts.fetch_opts.depth = CLONE_DEPTH; + clone_opts.remote_cb = remote_single_branch; git_buf_joinpath(&path, clar_sandbox_path(), "shallowclone"); @@ -35,21 +45,18 @@ void test_clone_shallow__clone_depth(void) cl_assert_equal_b(true, git_repository_is_shallow(repo)); cl_git_pass(git_repository_shallow_roots(&roots, repo)); - cl_assert_equal_i(1, roots.count); - cl_assert_equal_s("83834a7afdaa1a1260568567f6ad90020389f664", git_oid_tostr_s(&roots.ids[0])); + cl_assert_equal_i(3, roots.count); + cl_assert_equal_s("c070ad8c08840c8116da865b2d65593a6bb9cd2a", git_oid_tostr_s(&roots.ids[0])); + cl_assert_equal_s("0966a434eb1a025db6b71485ab63a3bfbea520b6", git_oid_tostr_s(&roots.ids[1])); + cl_assert_equal_s("83834a7afdaa1a1260568567f6ad90020389f664", git_oid_tostr_s(&roots.ids[2])); git_revwalk_new(&walk, repo); git_revwalk_push_head(walk); while ((error = git_revwalk_next(&oid, walk)) == GIT_OK) { - //if (depth + 1 > CLONE_DEPTH) - //cl_fail("expected depth mismatch"); - char str[GIT_OID_HEXSZ +1]; - git_oid_fmt(str, &oid); - printf(str); - printf("\n"); - depth++; + if (depth + 1 > CLONE_DEPTH) + cl_fail("expected depth mismatch"); } cl_git_pass(error); -- cgit v1.2.1 From f19ffc8a6d88432146a87cd3fb2ca21528478be4 Mon Sep 17 00:00:00 2001 From: yuangli Date: Tue, 19 Jul 2022 15:42:12 +0100 Subject: add test for shallow repo depth 1 --- tests/clone/shallow.c | 60 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/tests/clone/shallow.c b/tests/clone/shallow.c index b3e8f1617..01852dfed 100644 --- a/tests/clone/shallow.c +++ b/tests/clone/shallow.c @@ -4,17 +4,15 @@ void test_clone_shallow__initialize(void) { - + cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_SHALLOW, 1)); } void test_clone_shallow__cleanup(void) { + git_libgit2_opts(GIT_OPT_ENABLE_SHALLOW, 0); cl_git_sandbox_cleanup(); } - -#define CLONE_DEPTH 5 - static int remote_single_branch(git_remote **out, git_repository *repo, const char *name, const char *url, void *payload) { GIT_UNUSED(payload); @@ -24,7 +22,47 @@ static int remote_single_branch(git_remote **out, git_repository *repo, const ch return 0; } -void test_clone_shallow__clone_depth(void) +void test_clone_shallow__clone_depth_one(void) +{ + git_buf path = GIT_BUF_INIT; + git_repository *repo; + git_revwalk *walk; + git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT; + git_oid oid; + git_oidarray roots; + size_t num_commits = 0; + int error = 0; + + clone_opts.fetch_opts.depth = 1; + clone_opts.remote_cb = remote_single_branch; + + git_buf_joinpath(&path, clar_sandbox_path(), "shallowclone_1"); + + cl_git_pass(git_clone(&repo, "https://github.com/libgit2/TestGitRepository", git_buf_cstr(&path), &clone_opts)); + + cl_assert_equal_b(true, git_repository_is_shallow(repo)); + + cl_git_pass(git_repository_shallow_roots(&roots, repo)); + cl_assert_equal_i(1, roots.count); + cl_assert_equal_s("49322bb17d3acc9146f98c97d078513228bbf3c0", git_oid_tostr_s(&roots.ids[0])); + + git_revwalk_new(&walk, repo); + + git_revwalk_push_head(walk); + + while ((error = git_revwalk_next(&oid, walk)) == GIT_OK) { + num_commits++; + } + + cl_assert_equal_i(num_commits, 1); + cl_assert_equal_i(error, GIT_ITEROVER); + + git_buf_dispose(&path); + git_revwalk_free(walk); + git_repository_free(repo); +} + +void test_clone_shallow__clone_depth_five(void) { git_buf path = GIT_BUF_INIT; git_repository *repo; @@ -32,13 +70,13 @@ void test_clone_shallow__clone_depth(void) git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT; git_oid oid; git_oidarray roots; - size_t depth = 0; + size_t num_commits = 0; int error = 0; - clone_opts.fetch_opts.depth = CLONE_DEPTH; + clone_opts.fetch_opts.depth = 5; clone_opts.remote_cb = remote_single_branch; - git_buf_joinpath(&path, clar_sandbox_path(), "shallowclone"); + git_buf_joinpath(&path, clar_sandbox_path(), "shallowclone_5"); cl_git_pass(git_clone(&repo, "https://github.com/libgit2/TestGitRepository", git_buf_cstr(&path), &clone_opts)); @@ -55,11 +93,11 @@ void test_clone_shallow__clone_depth(void) git_revwalk_push_head(walk); while ((error = git_revwalk_next(&oid, walk)) == GIT_OK) { - if (depth + 1 > CLONE_DEPTH) - cl_fail("expected depth mismatch"); + num_commits++; } - cl_git_pass(error); + cl_assert_equal_i(num_commits, 13); + cl_assert_equal_i(error, GIT_ITEROVER); git_buf_dispose(&path); git_revwalk_free(walk); -- cgit v1.2.1 From 73d25f0e7b026d7b744e249d922f248376bcce5d Mon Sep 17 00:00:00 2001 From: yuangli Date: Fri, 29 Jul 2022 13:53:29 +0100 Subject: remove build errors --- include/git2/remote.h | 12 ++-- include/git2/repository.h | 1 + src/libgit2/clone.c | 2 +- src/libgit2/commit.c | 2 +- src/libgit2/fetch.c | 7 +-- src/libgit2/repository.c | 109 +------------------------------------ src/libgit2/transports/smart_pkt.c | 16 +++--- 7 files changed, 21 insertions(+), 128 deletions(-) diff --git a/include/git2/remote.h b/include/git2/remote.h index 871d898e5..33f5103ce 100644 --- a/include/git2/remote.h +++ b/include/git2/remote.h @@ -743,6 +743,11 @@ typedef struct { */ git_proxy_options proxy_opts; + /** + * Depth of the fetch to perform + */ + int depth; + /** * Whether to allow off-site redirects. If this is not * specified, the `http.followRedirects` configuration setting @@ -754,16 +759,11 @@ typedef struct { * Extra headers for this fetch operation */ git_strarray custom_headers; - - /** - * Depth of the fetch to perform - */ - int depth; } git_fetch_options; #define GIT_FETCH_OPTIONS_VERSION 1 #define GIT_FETCH_OPTIONS_INIT { GIT_FETCH_OPTIONS_VERSION, GIT_REMOTE_CALLBACKS_INIT, GIT_FETCH_PRUNE_UNSPECIFIED, 1, \ - GIT_REMOTE_DOWNLOAD_TAGS_UNSPECIFIED, GIT_PROXY_OPTIONS_INIT, { NULL }, -1 } + GIT_REMOTE_DOWNLOAD_TAGS_UNSPECIFIED, GIT_PROXY_OPTIONS_INIT, -1 } /** * Initialize git_fetch_options structure diff --git a/include/git2/repository.h b/include/git2/repository.h index 24a94118b..d6b054dc3 100644 --- a/include/git2/repository.h +++ b/include/git2/repository.h @@ -11,6 +11,7 @@ #include "types.h" #include "oid.h" #include "buffer.h" +#include "oidarray.h" /** * @file git2/repository.h diff --git a/src/libgit2/clone.c b/src/libgit2/clone.c index 101c741b1..ee1f2b892 100644 --- a/src/libgit2/clone.c +++ b/src/libgit2/clone.c @@ -411,7 +411,7 @@ static int clone_into(git_repository *repo, git_remote *_remote, const git_fetch fetch_opts.update_fetchhead = 0; if (fetch_opts.depth == -1) fetch_opts.download_tags = GIT_REMOTE_DOWNLOAD_TAGS_ALL; - git_buf_printf(&reflog_message, "clone: from %s", git_remote_url(remote)); + git_str_printf(&reflog_message, "clone: from %s", git_remote_url(remote)); if ((error = git_remote_fetch(remote, NULL, &fetch_opts, git_str_cstr(&reflog_message))) != 0) goto cleanup; diff --git a/src/libgit2/commit.c b/src/libgit2/commit.c index 75cc8837c..528d8beb7 100644 --- a/src/libgit2/commit.c +++ b/src/libgit2/commit.c @@ -422,7 +422,7 @@ static int commit_parse(git_commit *commit, const char *data, size_t size, unsig buffer += tree_len; } - while (git_oid__parse(&parent_id, &buffer, buffer_end, "parent ") == 0) { + while (git_object__parse_oid_header(&parent_id, &buffer, buffer_end, "parent ", GIT_OID_SHA1) == 0) { git_oid *new_id = git_array_alloc(commit->parent_ids); GIT_ERROR_CHECK_ALLOC(new_id); diff --git a/src/libgit2/fetch.c b/src/libgit2/fetch.c index 10155662d..e4a8f0382 100644 --- a/src/libgit2/fetch.c +++ b/src/libgit2/fetch.c @@ -209,12 +209,7 @@ int git_fetch_download_pack(git_remote *remote) if (!remote->need_pack) return 0; - if (callbacks) { - progress = callbacks->transfer_progress; - payload = callbacks->payload; - } - - if ((error = t->download_pack(t, remote->repo, &remote->stats, progress, payload)) < 0) + if ((error = t->download_pack(t, remote->repo, &remote->stats)) < 0) return error; if ((error = git_repository__shallow_roots_write(remote->repo, remote->nego.shallow_roots->array)) < 0) diff --git a/src/libgit2/repository.c b/src/libgit2/repository.c index adb4721ad..d9bc537fe 100644 --- a/src/libgit2/repository.c +++ b/src/libgit2/repository.c @@ -3340,109 +3340,6 @@ int git_repository_state_cleanup(git_repository *repo) return git_repository__cleanup_files(repo, state_files, ARRAY_SIZE(state_files)); } -// int git_repository__shallow_roots(git_array_oid_t *out, git_repository *repo) -// { -// git_buf path = GIT_BUF_INIT; -// git_buf contents = GIT_BUF_INIT; -// int error, updated, line_num = 1; -// char *line; -// chror = git_futils_readbuffer_updated(&contents, git_buf_cstr(&path), &repo->shallow_grafts->git_grafts->path_checksum, &updated); -// git_buf_dispose(&path); - -// if (error < 0 && error != GIT_ENOTFOUND) -// return error; - -// /* cancel out GIT_ENOTFOUND */ -// git_error_clear(); -// error = 0; - -// if (!updated) { -// out = repo->shallow_grafts; -// goto cleanup; -// } - -// git_array_clear(repo->shallow_grafts); - -// buffer = contents.ptr; -// while ((line = git__strsep(&buffer, "\n")) != NULL) { -// git_oid *oid = git_array_alloc(repo->shallow_grafts); - -// error = git_oid_fromstr(oid, line); -// if (error < 0) { -// git_error_set(GIT_ERROR_REPOSITORY, "Invalid OID at line %d", line_num); -// git_array_clear(repo->shallow_grafts); -// error = -1; -// goto cleanup; -// } -// ++line_num; -// } - -// if (*buffer) { -// git_error_set(GIT_ERROR_REPOSITORY, "No EOL at line %d", line_num); -// git_array_clear(repo->shallow_grafts); -// error = -1; -// goto cleanup; -// } - -// *out = repo->shallow_grafts; - -// cleanup: -// git_buf_dispose(&contents); - -// return error;ar *buffer; - -// assert(out && repo); - -// if ((error = git_buf_joinpath(&path, repo->gitdir, "shallow")) < 0) -// return error; - -// //error = git_futils_readbuffer_updated(&contents, git_buf_cstr(&path), &repo->shallow_checksum, &updated); -// error = git_futils_readbuffer_updated(&contents, git_buf_cstr(&path), &repo->shallow_grafts->git_grafts->path_checksum, &updated); -// git_buf_dispose(&path); - -// if (error < 0 && error != GIT_ENOTFOUND) -// return error; - -// /* cancel out GIT_ENOTFOUND */ -// git_error_clear(); -// error = 0; - -// if (!updated) { -// out = repo->shallow_grafts; -// goto cleanup; -// } - -// git_array_clear(repo->shallow_grafts); - -// buffer = contents.ptr; -// while ((line = git__strsep(&buffer, "\n")) != NULL) { -// git_oid *oid = git_array_alloc(repo->shallow_grafts); - -// error = git_oid_fromstr(oid, line); -// if (error < 0) { -// git_error_set(GIT_ERROR_REPOSITORY, "Invalid OID at line %d", line_num); -// git_array_clear(repo->shallow_grafts); -// error = -1; -// goto cleanup; -// } -// ++line_num; -// } - -// if (*buffer) { -// git_error_set(GIT_ERROR_REPOSITORY, "No EOL at line %d", line_num); -// git_array_clear(repo->shallow_grafts); -// error = -1; -// goto cleanup; -// } - -// *out = repo->shallow_grafts; - -// cleanup: -// git_buf_dispose(&contents); - -// return error; -// } - int git_repository__shallow_roots(git_array_oid_t *out, git_repository *repo) { int error =0; if (!repo->shallow_grafts) @@ -3455,17 +3352,17 @@ int git_repository__shallow_roots(git_array_oid_t *out, git_repository *repo) { int git_repository__shallow_roots_write(git_repository *repo, git_array_oid_t roots) { git_filebuf file = GIT_FILEBUF_INIT; - git_buf path = GIT_BUF_INIT; + git_str path = GIT_STR_INIT; int error = 0; size_t idx; git_oid *oid; assert(repo); - if ((error = git_buf_joinpath(&path, repo->gitdir, "shallow")) < 0) + if ((error = git_str_joinpath(&path, repo->gitdir, "shallow")) < 0) return error; - if ((error = git_filebuf_open(&file, git_buf_cstr(&path), GIT_FILEBUF_HASH_CONTENTS, 0666)) < 0) + if ((error = git_filebuf_open(&file, git_str_cstr(&path), GIT_FILEBUF_HASH_CONTENTS, 0666)) < 0) return error; git_array_foreach(roots, idx, oid) { diff --git a/src/libgit2/transports/smart_pkt.c b/src/libgit2/transports/smart_pkt.c index e368cb7e2..f9fde00f1 100644 --- a/src/libgit2/transports/smart_pkt.c +++ b/src/libgit2/transports/smart_pkt.c @@ -678,23 +678,23 @@ int git_pkt_buffer_wants( git_buf shallow_buf = GIT_BUF_INIT; git_oid_fmt(oid, git_shallowarray_get(wants->shallow_roots, i)); - git_buf_puts(&shallow_buf, "shallow "); - git_buf_put(&shallow_buf, oid, GIT_OID_HEXSZ); - git_buf_putc(&shallow_buf, '\n'); + git_str_puts(&shallow_buf, "shallow "); + git_str_put(&shallow_buf, oid, GIT_OID_HEXSZ); + git_str_putc(&shallow_buf, '\n'); - git_buf_printf(buf, "%04x%s", (unsigned int)git_buf_len(&shallow_buf) + 4, git_buf_cstr(&shallow_buf)); + git_str_printf(buf, "%04x%s", (unsigned int)git_str_len(&shallow_buf) + 4, git_str_cstr(&shallow_buf)); - if (git_buf_oom(buf)) + if (git_str_oom(buf)) return -1; } if (wants->depth > 0) { git_buf deepen_buf = GIT_BUF_INIT; - git_buf_printf(&deepen_buf, "deepen %d\n", wants->depth); - git_buf_printf(buf,"%04x%s", (unsigned int)git_buf_len(&deepen_buf) + 4, git_buf_cstr(&deepen_buf)); + git_str_printf(&deepen_buf, "deepen %d\n", wants->depth); + git_str_printf(buf,"%04x%s", (unsigned int)git_str_len(&deepen_buf) + 4, git_str_cstr(&deepen_buf)); - if (git_buf_oom(buf)) + if (git_str_oom(buf)) return -1; } -- cgit v1.2.1 From 598ec303c6862f581c22c49228d543442e30257a Mon Sep 17 00:00:00 2001 From: yuangli Date: Fri, 29 Jul 2022 15:04:17 +0100 Subject: eliminate build warnings --- src/libgit2/fetch.c | 2 - src/libgit2/grafts.c | 7 +-- src/libgit2/grafts.h | 2 +- src/libgit2/repository.c | 18 +++++-- src/libgit2/transports/smart_pkt.c | 4 +- tests/clone/shallow.c | 105 ------------------------------------- tests/libgit2/clone/shallow.c | 105 +++++++++++++++++++++++++++++++++++++ 7 files changed, 123 insertions(+), 120 deletions(-) delete mode 100644 tests/clone/shallow.c create mode 100644 tests/libgit2/clone/shallow.c diff --git a/src/libgit2/fetch.c b/src/libgit2/fetch.c index e4a8f0382..b90ce2ee8 100644 --- a/src/libgit2/fetch.c +++ b/src/libgit2/fetch.c @@ -202,8 +202,6 @@ int git_fetch_negotiate(git_remote *remote, const git_fetch_options *opts) int git_fetch_download_pack(git_remote *remote) { git_transport *t = remote->transport; - git_indexer_progress_cb progress = NULL; - void *payload = NULL; int error; if (!remote->need_pack) diff --git a/src/libgit2/grafts.c b/src/libgit2/grafts.c index 82be2a680..f1054fa36 100644 --- a/src/libgit2/grafts.c +++ b/src/libgit2/grafts.c @@ -220,9 +220,8 @@ int git_grafts_get(git_commit_graft **out, git_grafts *grafts, const git_oid *oi return 0; } -int git_grafts_get_oids(git_oidarray *out, git_grafts *grafts) +int git_grafts_get_oids(git_array_oid_t *out, git_grafts *grafts) { - git_array_oid_t oids = GIT_ARRAY_INIT; const git_oid *oid; size_t i = 0; int error; @@ -230,13 +229,11 @@ int git_grafts_get_oids(git_oidarray *out, git_grafts *grafts) assert(out && grafts); while ((error = git_oidmap_iterate(NULL, grafts->commits, &i, &oid)) == 0) { - git_oid *cpy = git_array_alloc(oids); + git_oid *cpy = git_array_alloc(*out); GIT_ERROR_CHECK_ALLOC(cpy); git_oid_cpy(cpy, oid); } - git_oidarray__from_array(out, &oids); - return 0; } diff --git a/src/libgit2/grafts.h b/src/libgit2/grafts.h index fd9ef6736..4139438bb 100644 --- a/src/libgit2/grafts.h +++ b/src/libgit2/grafts.h @@ -31,7 +31,7 @@ int git_grafts_parse(git_grafts *grafts, const char *content, size_t contentlen) int git_grafts_add(git_grafts *grafts, const git_oid *oid, git_array_oid_t parents); int git_grafts_remove(git_grafts *grafts, const git_oid *oid); int git_grafts_get(git_commit_graft **out, git_grafts *grafts, const git_oid *oid); -int git_grafts_get_oids(git_oidarray *out, git_grafts *grafts); +int git_grafts_get_oids(git_array_oid_t *out, git_grafts *grafts); size_t git_grafts_size(git_grafts *grafts); #endif diff --git a/src/libgit2/repository.c b/src/libgit2/repository.c index d9bc537fe..bc2aba324 100644 --- a/src/libgit2/repository.c +++ b/src/libgit2/repository.c @@ -3341,12 +3341,20 @@ int git_repository_state_cleanup(git_repository *repo) } int git_repository__shallow_roots(git_array_oid_t *out, git_repository *repo) { - int error =0; - if (!repo->shallow_grafts) - load_grafts(repo); + int error = 0; + + if (!repo->shallow_grafts && (error = load_grafts(repo)) < 0) + return error; + + if ((error = git_grafts_refresh(repo->shallow_grafts)) < 0) { + return error; + } - git_grafts_refresh(repo->shallow_grafts); - return git_grafts_get_oids(out, repo->shallow_grafts); + if ((error = git_grafts_get_oids(out, repo->shallow_grafts)) < 0) { + return error; + } + + return 0; } int git_repository__shallow_roots_write(git_repository *repo, git_array_oid_t roots) diff --git a/src/libgit2/transports/smart_pkt.c b/src/libgit2/transports/smart_pkt.c index f9fde00f1..951356c29 100644 --- a/src/libgit2/transports/smart_pkt.c +++ b/src/libgit2/transports/smart_pkt.c @@ -675,7 +675,7 @@ int git_pkt_buffer_wants( /* Tell the server about our shallow objects */ for (i = 0; i < git_shallowarray_count(wants->shallow_roots); i++) { char oid[GIT_OID_HEXSZ]; - git_buf shallow_buf = GIT_BUF_INIT; + git_str shallow_buf = GIT_STR_INIT; git_oid_fmt(oid, git_shallowarray_get(wants->shallow_roots, i)); git_str_puts(&shallow_buf, "shallow "); @@ -689,7 +689,7 @@ int git_pkt_buffer_wants( } if (wants->depth > 0) { - git_buf deepen_buf = GIT_BUF_INIT; + git_str deepen_buf = GIT_STR_INIT; git_str_printf(&deepen_buf, "deepen %d\n", wants->depth); git_str_printf(buf,"%04x%s", (unsigned int)git_str_len(&deepen_buf) + 4, git_str_cstr(&deepen_buf)); diff --git a/tests/clone/shallow.c b/tests/clone/shallow.c deleted file mode 100644 index 01852dfed..000000000 --- a/tests/clone/shallow.c +++ /dev/null @@ -1,105 +0,0 @@ -#include "clar_libgit2.h" -#include "futils.h" -#include "repository.h" - -void test_clone_shallow__initialize(void) -{ - cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_SHALLOW, 1)); -} - -void test_clone_shallow__cleanup(void) -{ - git_libgit2_opts(GIT_OPT_ENABLE_SHALLOW, 0); - cl_git_sandbox_cleanup(); -} - -static int remote_single_branch(git_remote **out, git_repository *repo, const char *name, const char *url, void *payload) -{ - GIT_UNUSED(payload); - - cl_git_pass(git_remote_create_with_fetchspec(out, repo, name, url, "+refs/heads/master:refs/remotes/origin/master")); - - return 0; -} - -void test_clone_shallow__clone_depth_one(void) -{ - git_buf path = GIT_BUF_INIT; - git_repository *repo; - git_revwalk *walk; - git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT; - git_oid oid; - git_oidarray roots; - size_t num_commits = 0; - int error = 0; - - clone_opts.fetch_opts.depth = 1; - clone_opts.remote_cb = remote_single_branch; - - git_buf_joinpath(&path, clar_sandbox_path(), "shallowclone_1"); - - cl_git_pass(git_clone(&repo, "https://github.com/libgit2/TestGitRepository", git_buf_cstr(&path), &clone_opts)); - - cl_assert_equal_b(true, git_repository_is_shallow(repo)); - - cl_git_pass(git_repository_shallow_roots(&roots, repo)); - cl_assert_equal_i(1, roots.count); - cl_assert_equal_s("49322bb17d3acc9146f98c97d078513228bbf3c0", git_oid_tostr_s(&roots.ids[0])); - - git_revwalk_new(&walk, repo); - - git_revwalk_push_head(walk); - - while ((error = git_revwalk_next(&oid, walk)) == GIT_OK) { - num_commits++; - } - - cl_assert_equal_i(num_commits, 1); - cl_assert_equal_i(error, GIT_ITEROVER); - - git_buf_dispose(&path); - git_revwalk_free(walk); - git_repository_free(repo); -} - -void test_clone_shallow__clone_depth_five(void) -{ - git_buf path = GIT_BUF_INIT; - git_repository *repo; - git_revwalk *walk; - git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT; - git_oid oid; - git_oidarray roots; - size_t num_commits = 0; - int error = 0; - - clone_opts.fetch_opts.depth = 5; - clone_opts.remote_cb = remote_single_branch; - - git_buf_joinpath(&path, clar_sandbox_path(), "shallowclone_5"); - - cl_git_pass(git_clone(&repo, "https://github.com/libgit2/TestGitRepository", git_buf_cstr(&path), &clone_opts)); - - cl_assert_equal_b(true, git_repository_is_shallow(repo)); - - cl_git_pass(git_repository_shallow_roots(&roots, repo)); - cl_assert_equal_i(3, roots.count); - cl_assert_equal_s("c070ad8c08840c8116da865b2d65593a6bb9cd2a", git_oid_tostr_s(&roots.ids[0])); - cl_assert_equal_s("0966a434eb1a025db6b71485ab63a3bfbea520b6", git_oid_tostr_s(&roots.ids[1])); - cl_assert_equal_s("83834a7afdaa1a1260568567f6ad90020389f664", git_oid_tostr_s(&roots.ids[2])); - - git_revwalk_new(&walk, repo); - - git_revwalk_push_head(walk); - - while ((error = git_revwalk_next(&oid, walk)) == GIT_OK) { - num_commits++; - } - - cl_assert_equal_i(num_commits, 13); - cl_assert_equal_i(error, GIT_ITEROVER); - - git_buf_dispose(&path); - git_revwalk_free(walk); - git_repository_free(repo); -} diff --git a/tests/libgit2/clone/shallow.c b/tests/libgit2/clone/shallow.c new file mode 100644 index 000000000..2cd3d6cac --- /dev/null +++ b/tests/libgit2/clone/shallow.c @@ -0,0 +1,105 @@ +#include "clar_libgit2.h" +#include "futils.h" +#include "repository.h" + +void test_clone_shallow__initialize(void) +{ + cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_SHALLOW, 1)); +} + +void test_clone_shallow__cleanup(void) +{ + git_libgit2_opts(GIT_OPT_ENABLE_SHALLOW, 0); + cl_git_sandbox_cleanup(); +} + +static int remote_single_branch(git_remote **out, git_repository *repo, const char *name, const char *url, void *payload) +{ + GIT_UNUSED(payload); + + cl_git_pass(git_remote_create_with_fetchspec(out, repo, name, url, "+refs/heads/master:refs/remotes/origin/master")); + + return 0; +} + +void test_clone_shallow__clone_depth_one(void) +{ + git_str path = GIT_STR_INIT; + git_repository *repo; + git_revwalk *walk; + git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT; + git_oid oid; + roots; + size_t num_commits = 0; + int error = 0; + + clone_opts.fetch_opts.depth = 1; + clone_opts.remote_cb = remote_single_branch; + + git_str_joinpath(&path, clar_sandbox_path(), "shallowclone_1"); + + cl_git_pass(git_clone(&repo, "https://github.com/libgit2/TestGitRepository", git_str_cstr(&path), &clone_opts)); + + cl_assert_equal_b(true, git_repository_is_shallow(repo)); + + cl_git_pass(git_repository_shallow_roots(&roots, repo)); + cl_assert_equal_i(1, roots.count); + cl_assert_equal_s("49322bb17d3acc9146f98c97d078513228bbf3c0", git_oid_tostr_s(&roots.ids[0])); + + git_revwalk_new(&walk, repo); + + git_revwalk_push_head(walk); + + while ((error = git_revwalk_next(&oid, walk)) == GIT_OK) { + num_commits++; + } + + cl_assert_equal_i(num_commits, 1); + cl_assert_equal_i(error, GIT_ITEROVER); + + git_str_dispose(&path); + git_revwalk_free(walk); + git_repository_free(repo); +} + +void test_clone_shallow__clone_depth_five(void) +{ + git_str path = GIT_STR_INIT; + git_repository *repo; + git_revwalk *walk; + git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT; + git_oid oid; + git_oidarray roots; + size_t num_commits = 0; + int error = 0; + + clone_opts.fetch_opts.depth = 5; + clone_opts.remote_cb = remote_single_branch; + + git_str_joinpath(&path, clar_sandbox_path(), "shallowclone_5"); + + cl_git_pass(git_clone(&repo, "https://github.com/libgit2/TestGitRepository", git_str_cstr(&path), &clone_opts)); + + cl_assert_equal_b(true, git_repository_is_shallow(repo)); + + cl_git_pass(git_repository_shallow_roots(&roots, repo)); + cl_assert_equal_i(3, roots.count); + cl_assert_equal_s("c070ad8c08840c8116da865b2d65593a6bb9cd2a", git_oid_tostr_s(&roots.ids[0])); + cl_assert_equal_s("0966a434eb1a025db6b71485ab63a3bfbea520b6", git_oid_tostr_s(&roots.ids[1])); + cl_assert_equal_s("83834a7afdaa1a1260568567f6ad90020389f664", git_oid_tostr_s(&roots.ids[2])); + + git_revwalk_new(&walk, repo); + + git_revwalk_push_head(walk); + + while ((error = git_revwalk_next(&oid, walk)) == GIT_OK) { + num_commits++; + } + + cl_assert_equal_i(num_commits, 13); + cl_assert_equal_i(error, GIT_ITEROVER); + + git_str_dispose(&path); + git_revwalk_free(walk); + git_repository_free(repo); +} -- cgit v1.2.1 From 179aac788d45d3683a93ad478bfb7371c549ca98 Mon Sep 17 00:00:00 2001 From: yuangli Date: Fri, 29 Jul 2022 16:04:43 +0100 Subject: fix clone::shallow test behaviour --- src/libgit2/transports/smart_protocol.c | 2 -- tests/libgit2/clone/shallow.c | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/libgit2/transports/smart_protocol.c b/src/libgit2/transports/smart_protocol.c index cd90db43f..f2a6ac508 100644 --- a/src/libgit2/transports/smart_protocol.c +++ b/src/libgit2/transports/smart_protocol.c @@ -375,10 +375,8 @@ int git_smart__negotiate_fetch(git_transport *transport, git_repository *repo, c while ((error = recv_pkt((git_pkt **)&pkt, NULL, buf)) == 0) { if (pkt->type == GIT_PKT_SHALLOW) { - printf("shallow %s\n", git_oid_tostr_s(&pkt->oid)); git_shallowarray_add(wants->shallow_roots, &pkt->oid); } else if (pkt->type == GIT_PKT_UNSHALLOW) { - printf("unshallow %s\n", git_oid_tostr_s(&pkt->oid)); git_shallowarray_remove(wants->shallow_roots, &pkt->oid); } else if (pkt->type == GIT_PKT_FLUSH) { /* Server is done, stop processing shallow oids */ diff --git a/tests/libgit2/clone/shallow.c b/tests/libgit2/clone/shallow.c index 2cd3d6cac..4b27b27a9 100644 --- a/tests/libgit2/clone/shallow.c +++ b/tests/libgit2/clone/shallow.c @@ -29,7 +29,7 @@ void test_clone_shallow__clone_depth_one(void) git_revwalk *walk; git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT; git_oid oid; - roots; + git_oidarray roots; size_t num_commits = 0; int error = 0; -- cgit v1.2.1 From e7294e870398d72b79a436b628170ffcaaa1713c Mon Sep 17 00:00:00 2001 From: yuangli Date: Mon, 1 Aug 2022 21:42:11 +0100 Subject: fix memory leaks about packets --- src/libgit2/transports/smart_pkt.c | 2 ++ src/libgit2/transports/smart_protocol.c | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/src/libgit2/transports/smart_pkt.c b/src/libgit2/transports/smart_pkt.c index 951356c29..5808b8816 100644 --- a/src/libgit2/transports/smart_pkt.c +++ b/src/libgit2/transports/smart_pkt.c @@ -694,6 +694,8 @@ int git_pkt_buffer_wants( git_str_printf(&deepen_buf, "deepen %d\n", wants->depth); git_str_printf(buf,"%04x%s", (unsigned int)git_str_len(&deepen_buf) + 4, git_str_cstr(&deepen_buf)); + git_str_dispose(&deepen_buf); + if (git_str_oom(buf)) return -1; } diff --git a/src/libgit2/transports/smart_protocol.c b/src/libgit2/transports/smart_protocol.c index f2a6ac508..a44d0c853 100644 --- a/src/libgit2/transports/smart_protocol.c +++ b/src/libgit2/transports/smart_protocol.c @@ -385,8 +385,12 @@ int git_smart__negotiate_fetch(git_transport *transport, git_repository *repo, c git_error_set(GIT_ERROR_NET, "Unexpected pkt type"); goto on_error; } + + git_pkt_free((git_pkt *) pkt); } + git_pkt_free((git_pkt *) pkt); + if (error < 0) { goto on_error; } -- cgit v1.2.1 From 2d33fe78858354c508afb77d80f32a5a747b801e Mon Sep 17 00:00:00 2001 From: yuangli Date: Tue, 2 Aug 2022 13:15:22 +0100 Subject: refactor git_fetch_option.depth and usage --- include/git2/remote.h | 4 +++- src/libgit2/clone.c | 9 ++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/include/git2/remote.h b/include/git2/remote.h index 33f5103ce..f3415d843 100644 --- a/include/git2/remote.h +++ b/include/git2/remote.h @@ -744,7 +744,9 @@ typedef struct { git_proxy_options proxy_opts; /** - * Depth of the fetch to perform + * Depth of the fetch to perform, has to be a positive integer. + * + * The default is -1, which will fetch the full history. */ int depth; diff --git a/src/libgit2/clone.c b/src/libgit2/clone.c index ee1f2b892..95bddeae6 100644 --- a/src/libgit2/clone.c +++ b/src/libgit2/clone.c @@ -389,6 +389,11 @@ static int checkout_branch(git_repository *repo, git_remote *remote, const git_c return error; } +static int git_fetch_is_shallow(git_fetch_options *opts) +{ + return opts->depth > 0; +} + static int clone_into(git_repository *repo, git_remote *_remote, const git_fetch_options *opts, const git_checkout_options *co_opts, const char *branch) { int error; @@ -409,8 +414,10 @@ static int clone_into(git_repository *repo, git_remote *_remote, const git_fetch memcpy(&fetch_opts, opts, sizeof(git_fetch_options)); fetch_opts.update_fetchhead = 0; - if (fetch_opts.depth == -1) + + if (!git_fetch_is_shallow(opts)) fetch_opts.download_tags = GIT_REMOTE_DOWNLOAD_TAGS_ALL; + git_str_printf(&reflog_message, "clone: from %s", git_remote_url(remote)); if ((error = git_remote_fetch(remote, NULL, &fetch_opts, git_str_cstr(&reflog_message))) != 0) -- cgit v1.2.1 From 4536477ee1f884501717b3a4a67da3bc4974e7f8 Mon Sep 17 00:00:00 2001 From: yuangli Date: Tue, 2 Aug 2022 14:35:08 +0100 Subject: fix memory leak --- src/libgit2/repository.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libgit2/repository.c b/src/libgit2/repository.c index bc2aba324..105ea647f 100644 --- a/src/libgit2/repository.c +++ b/src/libgit2/repository.c @@ -3380,6 +3380,8 @@ int git_repository__shallow_roots_write(git_repository *repo, git_array_oid_t ro git_filebuf_commit(&file); + git_str_dispose(&path); + if (load_grafts(repo) < 0) return -1; -- cgit v1.2.1 From e93d0815a8de2806667851e31dbd06578db00f95 Mon Sep 17 00:00:00 2001 From: yuangli Date: Tue, 2 Aug 2022 14:35:27 +0100 Subject: attempt to fix nego.shallowarray memory leak --- src/libgit2/remote.c | 3 +++ src/libgit2/transports/smart.c | 6 ++++++ src/libgit2/transports/smart.h | 2 ++ 3 files changed, 11 insertions(+) diff --git a/src/libgit2/remote.c b/src/libgit2/remote.c index 02d271d7d..c3d3af530 100644 --- a/src/libgit2/remote.c +++ b/src/libgit2/remote.c @@ -22,6 +22,7 @@ #include "git2/types.h" #include "git2/oid.h" #include "git2/net.h" +#include "transports/smart.h" #define CONFIG_URL_FMT "remote.%s.url" #define CONFIG_PUSHURL_FMT "remote.%s.pushurl" @@ -2163,6 +2164,8 @@ void git_remote_free(git_remote *remote) free_heads(&remote->local_heads); git_vector_free(&remote->local_heads); + git_shallowarray_free((remote->nego).shallow_roots); + git_push_free(remote->push); git__free(remote->url); git__free(remote->pushurl); diff --git a/src/libgit2/transports/smart.c b/src/libgit2/transports/smart.c index 9d1afeb05..df8e4da4a 100644 --- a/src/libgit2/transports/smart.c +++ b/src/libgit2/transports/smart.c @@ -510,3 +510,9 @@ int git_shallowarray_remove(git_shallowarray *array, git_oid *oid) /* no git_array_remove… meh */ return -1; } + +void git_shallowarray_free(git_shallowarray *array) +{ + git_array_clear(array->array); + git__free(array); +} diff --git a/src/libgit2/transports/smart.h b/src/libgit2/transports/smart.h index bc072d2fe..f9577cdee 100644 --- a/src/libgit2/transports/smart.h +++ b/src/libgit2/transports/smart.h @@ -203,4 +203,6 @@ struct git_shallowarray { git_array_oid_t array; }; +void git_shallowarray_free(git_shallowarray *array); + #endif -- cgit v1.2.1 From da04d3fc5e737b990e884129241f2ea3cba4cf6a Mon Sep 17 00:00:00 2001 From: yuangli Date: Tue, 2 Aug 2022 16:30:48 +0100 Subject: fix grafts and shallowarray memory leaks --- src/libgit2/grafts.c | 3 +++ src/libgit2/remote.c | 7 +++++-- src/libgit2/repository.c | 6 ++---- src/libgit2/transports/smart.c | 6 ------ src/libgit2/transports/smart.h | 2 -- 5 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/libgit2/grafts.c b/src/libgit2/grafts.c index f1054fa36..dd1be3434 100644 --- a/src/libgit2/grafts.c +++ b/src/libgit2/grafts.c @@ -43,6 +43,9 @@ int git_grafts_from_file(git_grafts **out, const char *path) git_grafts *grafts = NULL; int error; + if (*out) + return git_grafts_refresh(*out); + if ((error = git_grafts_new(&grafts)) < 0) goto error; diff --git a/src/libgit2/remote.c b/src/libgit2/remote.c index c3d3af530..63346e941 100644 --- a/src/libgit2/remote.c +++ b/src/libgit2/remote.c @@ -2150,6 +2150,11 @@ void git_remote_free(git_remote *remote) remote->transport = NULL; } + if (remote->nego.shallow_roots) { + git_array_clear(remote->nego.shallow_roots->array); + git__free(remote->nego.shallow_roots); + } + git_vector_free(&remote->refs); free_refspecs(&remote->refspecs); @@ -2164,8 +2169,6 @@ void git_remote_free(git_remote *remote) free_heads(&remote->local_heads); git_vector_free(&remote->local_heads); - git_shallowarray_free((remote->nego).shallow_roots); - git_push_free(remote->push); git__free(remote->url); git__free(remote->pushurl); diff --git a/src/libgit2/repository.c b/src/libgit2/repository.c index 105ea647f..0d149a626 100644 --- a/src/libgit2/repository.c +++ b/src/libgit2/repository.c @@ -3346,13 +3346,11 @@ int git_repository__shallow_roots(git_array_oid_t *out, git_repository *repo) { if (!repo->shallow_grafts && (error = load_grafts(repo)) < 0) return error; - if ((error = git_grafts_refresh(repo->shallow_grafts)) < 0) { + if ((error = git_grafts_refresh(repo->shallow_grafts)) < 0) return error; - } - if ((error = git_grafts_get_oids(out, repo->shallow_grafts)) < 0) { + if ((error = git_grafts_get_oids(out, repo->shallow_grafts)) < 0) return error; - } return 0; } diff --git a/src/libgit2/transports/smart.c b/src/libgit2/transports/smart.c index df8e4da4a..9d1afeb05 100644 --- a/src/libgit2/transports/smart.c +++ b/src/libgit2/transports/smart.c @@ -510,9 +510,3 @@ int git_shallowarray_remove(git_shallowarray *array, git_oid *oid) /* no git_array_remove… meh */ return -1; } - -void git_shallowarray_free(git_shallowarray *array) -{ - git_array_clear(array->array); - git__free(array); -} diff --git a/src/libgit2/transports/smart.h b/src/libgit2/transports/smart.h index f9577cdee..bc072d2fe 100644 --- a/src/libgit2/transports/smart.h +++ b/src/libgit2/transports/smart.h @@ -203,6 +203,4 @@ struct git_shallowarray { git_array_oid_t array; }; -void git_shallowarray_free(git_shallowarray *array); - #endif -- cgit v1.2.1 From 8ef492fafcc05ccfa90481057eccc8ba4f5e730a Mon Sep 17 00:00:00 2001 From: yuangli Date: Tue, 2 Aug 2022 16:46:44 +0100 Subject: fix build warning --- src/libgit2/clone.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libgit2/clone.c b/src/libgit2/clone.c index 95bddeae6..5e07dc733 100644 --- a/src/libgit2/clone.c +++ b/src/libgit2/clone.c @@ -389,7 +389,7 @@ static int checkout_branch(git_repository *repo, git_remote *remote, const git_c return error; } -static int git_fetch_is_shallow(git_fetch_options *opts) +static int git_fetch_is_shallow(const git_fetch_options *opts) { return opts->depth > 0; } -- cgit v1.2.1 From 829555a9382f74af5280ab6cc2ac505534a4cecb Mon Sep 17 00:00:00 2001 From: yuangli Date: Tue, 2 Aug 2022 16:47:01 +0100 Subject: edit tests for shallow clones --- tests/libgit2/clone/shallow.c | 48 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/tests/libgit2/clone/shallow.c b/tests/libgit2/clone/shallow.c index 4b27b27a9..7fb056f91 100644 --- a/tests/libgit2/clone/shallow.c +++ b/tests/libgit2/clone/shallow.c @@ -22,6 +22,32 @@ static int remote_single_branch(git_remote **out, git_repository *repo, const ch return 0; } +void test_clone_shallow__clone_depth_zero(void) +{ + git_str path = GIT_STR_INIT; + git_repository *repo; + git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT; + git_array_oid_t roots = GIT_ARRAY_INIT; + + clone_opts.fetch_opts.depth = 0; + clone_opts.remote_cb = remote_single_branch; + + git_str_joinpath(&path, clar_sandbox_path(), "shallowclone_0"); + + cl_git_pass(git_clone(&repo, "https://github.com/libgit2/TestGitRepository", git_str_cstr(&path), &clone_opts)); + + /* cloning with depth 0 results in a full clone. */ + cl_assert_equal_b(false, git_repository_is_shallow(repo)); + + /* full clones do not have shallow roots. */ + cl_git_pass(git_repository__shallow_roots(&roots, repo)); + cl_assert_equal_i(0, roots.size); + + git_array_clear(roots); + git_str_dispose(&path); + git_repository_free(repo); +} + void test_clone_shallow__clone_depth_one(void) { git_str path = GIT_STR_INIT; @@ -29,7 +55,7 @@ void test_clone_shallow__clone_depth_one(void) git_revwalk *walk; git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT; git_oid oid; - git_oidarray roots; + git_array_oid_t roots = GIT_ARRAY_INIT; size_t num_commits = 0; int error = 0; @@ -42,9 +68,9 @@ void test_clone_shallow__clone_depth_one(void) cl_assert_equal_b(true, git_repository_is_shallow(repo)); - cl_git_pass(git_repository_shallow_roots(&roots, repo)); - cl_assert_equal_i(1, roots.count); - cl_assert_equal_s("49322bb17d3acc9146f98c97d078513228bbf3c0", git_oid_tostr_s(&roots.ids[0])); + cl_git_pass(git_repository__shallow_roots(&roots, repo)); + cl_assert_equal_i(1, roots.size); + cl_assert_equal_s("49322bb17d3acc9146f98c97d078513228bbf3c0", git_oid_tostr_s(&roots.ptr[0])); git_revwalk_new(&walk, repo); @@ -57,6 +83,7 @@ void test_clone_shallow__clone_depth_one(void) cl_assert_equal_i(num_commits, 1); cl_assert_equal_i(error, GIT_ITEROVER); + git_array_clear(roots); git_str_dispose(&path); git_revwalk_free(walk); git_repository_free(repo); @@ -69,7 +96,7 @@ void test_clone_shallow__clone_depth_five(void) git_revwalk *walk; git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT; git_oid oid; - git_oidarray roots; + git_array_oid_t roots = GIT_ARRAY_INIT; size_t num_commits = 0; int error = 0; @@ -82,11 +109,11 @@ void test_clone_shallow__clone_depth_five(void) cl_assert_equal_b(true, git_repository_is_shallow(repo)); - cl_git_pass(git_repository_shallow_roots(&roots, repo)); - cl_assert_equal_i(3, roots.count); - cl_assert_equal_s("c070ad8c08840c8116da865b2d65593a6bb9cd2a", git_oid_tostr_s(&roots.ids[0])); - cl_assert_equal_s("0966a434eb1a025db6b71485ab63a3bfbea520b6", git_oid_tostr_s(&roots.ids[1])); - cl_assert_equal_s("83834a7afdaa1a1260568567f6ad90020389f664", git_oid_tostr_s(&roots.ids[2])); + cl_git_pass(git_repository__shallow_roots(&roots, repo)); + cl_assert_equal_i(3, roots.size); + cl_assert_equal_s("c070ad8c08840c8116da865b2d65593a6bb9cd2a", git_oid_tostr_s(&roots.ptr[0])); + cl_assert_equal_s("0966a434eb1a025db6b71485ab63a3bfbea520b6", git_oid_tostr_s(&roots.ptr[1])); + cl_assert_equal_s("83834a7afdaa1a1260568567f6ad90020389f664", git_oid_tostr_s(&roots.ptr[2])); git_revwalk_new(&walk, repo); @@ -99,6 +126,7 @@ void test_clone_shallow__clone_depth_five(void) cl_assert_equal_i(num_commits, 13); cl_assert_equal_i(error, GIT_ITEROVER); + git_array_clear(roots); git_str_dispose(&path); git_revwalk_free(walk); git_repository_free(repo); -- cgit v1.2.1 From 09b3d33d6dba7f4804b478a72dec3258405856c2 Mon Sep 17 00:00:00 2001 From: yuangli Date: Tue, 9 Aug 2022 19:23:54 +0100 Subject: fix memory leak --- src/libgit2/transports/smart_pkt.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libgit2/transports/smart_pkt.c b/src/libgit2/transports/smart_pkt.c index 5808b8816..832da450c 100644 --- a/src/libgit2/transports/smart_pkt.c +++ b/src/libgit2/transports/smart_pkt.c @@ -684,6 +684,8 @@ int git_pkt_buffer_wants( git_str_printf(buf, "%04x%s", (unsigned int)git_str_len(&shallow_buf) + 4, git_str_cstr(&shallow_buf)); + git_str_dispose(&shallow_buf); + if (git_str_oom(buf)) return -1; } -- cgit v1.2.1 From df5eb3239b1419b3f4382d7bcca9a5d85611d7d3 Mon Sep 17 00:00:00 2001 From: yuangli Date: Tue, 9 Aug 2022 19:24:57 +0100 Subject: support fetch unshallow option on shallow repos --- include/git2/remote.h | 9 ++++- src/libgit2/clone.c | 7 +--- src/libgit2/fetch.c | 9 +++-- src/libgit2/repository.c | 19 ++++++---- src/libgit2/transports/smart.c | 26 +++++++++++--- tests/libgit2/clone/shallow.c | 41 +++++++++++++++++++++ tests/libgit2/transports/smart/shallowarray.c | 52 +++++++++++++++++++++++++++ 7 files changed, 144 insertions(+), 19 deletions(-) create mode 100644 tests/libgit2/transports/smart/shallowarray.c diff --git a/include/git2/remote.h b/include/git2/remote.h index f3415d843..240e5aa4e 100644 --- a/include/git2/remote.h +++ b/include/git2/remote.h @@ -750,6 +750,13 @@ typedef struct { */ int depth; + /** + * Unshallow flag of the fetch to perform. + * + * The default is 0, which means the flag is off. + */ + int unshallow; + /** * Whether to allow off-site redirects. If this is not * specified, the `http.followRedirects` configuration setting @@ -765,7 +772,7 @@ typedef struct { #define GIT_FETCH_OPTIONS_VERSION 1 #define GIT_FETCH_OPTIONS_INIT { GIT_FETCH_OPTIONS_VERSION, GIT_REMOTE_CALLBACKS_INIT, GIT_FETCH_PRUNE_UNSPECIFIED, 1, \ - GIT_REMOTE_DOWNLOAD_TAGS_UNSPECIFIED, GIT_PROXY_OPTIONS_INIT, -1 } + GIT_REMOTE_DOWNLOAD_TAGS_UNSPECIFIED, GIT_PROXY_OPTIONS_INIT, -1, 0 } /** * Initialize git_fetch_options structure diff --git a/src/libgit2/clone.c b/src/libgit2/clone.c index 5e07dc733..6f34cb7ca 100644 --- a/src/libgit2/clone.c +++ b/src/libgit2/clone.c @@ -389,11 +389,6 @@ static int checkout_branch(git_repository *repo, git_remote *remote, const git_c return error; } -static int git_fetch_is_shallow(const git_fetch_options *opts) -{ - return opts->depth > 0; -} - static int clone_into(git_repository *repo, git_remote *_remote, const git_fetch_options *opts, const git_checkout_options *co_opts, const char *branch) { int error; @@ -415,7 +410,7 @@ static int clone_into(git_repository *repo, git_remote *_remote, const git_fetch memcpy(&fetch_opts, opts, sizeof(git_fetch_options)); fetch_opts.update_fetchhead = 0; - if (!git_fetch_is_shallow(opts)) + if (opts->depth <= 0) fetch_opts.download_tags = GIT_REMOTE_DOWNLOAD_TAGS_ALL; git_str_printf(&reflog_message, "clone: from %s", git_remote_url(remote)); diff --git a/src/libgit2/fetch.c b/src/libgit2/fetch.c index b90ce2ee8..a015cfbd3 100644 --- a/src/libgit2/fetch.c +++ b/src/libgit2/fetch.c @@ -61,7 +61,7 @@ static int mark_local(git_remote *remote) git_vector_foreach(&remote->refs, i, head) { /* If we have the object, mark it so we don't ask for it */ - if (git_odb_exists(odb, &head->oid)) + if (remote->nego.depth != INT_MAX && git_odb_exists(odb, &head->oid)) head->local = 1; else remote->need_pack = 1; @@ -173,6 +173,7 @@ int git_fetch_negotiate(git_remote *remote, const git_fetch_options *opts) git_transport *t = remote->transport; remote->need_pack = 0; + remote->nego.depth = opts->unshallow ? INT_MAX : opts->depth; if (filter_wants(remote, opts) < 0) return -1; @@ -181,13 +182,17 @@ int git_fetch_negotiate(git_remote *remote, const git_fetch_options *opts) if (!remote->need_pack) return 0; + if (opts->unshallow && opts->depth > 0) { + git_error_set(GIT_ERROR_INVALID, "options '--depth' and '--unshallow' cannot be used together"); + return -1; + } + /* * Now we have everything set up so we can start tell the * server what we want and what we have. */ remote->nego.refs = (const git_remote_head * const *)remote->refs.contents; remote->nego.count = remote->refs.length; - remote->nego.depth = opts->depth; remote->nego.shallow_roots = git__malloc(sizeof(git_shallowarray)); git_array_init(remote->nego.shallow_roots->array); diff --git a/src/libgit2/repository.c b/src/libgit2/repository.c index 0d149a626..13559ef07 100644 --- a/src/libgit2/repository.c +++ b/src/libgit2/repository.c @@ -3366,10 +3366,10 @@ int git_repository__shallow_roots_write(git_repository *repo, git_array_oid_t ro assert(repo); if ((error = git_str_joinpath(&path, repo->gitdir, "shallow")) < 0) - return error; + goto on_error; if ((error = git_filebuf_open(&file, git_str_cstr(&path), GIT_FILEBUF_HASH_CONTENTS, 0666)) < 0) - return error; + goto on_error; git_array_foreach(roots, idx, oid) { git_filebuf_write(&file, git_oid_tostr_s(oid), GIT_OID_HEXSZ); @@ -3378,12 +3378,19 @@ int git_repository__shallow_roots_write(git_repository *repo, git_array_oid_t ro git_filebuf_commit(&file); - git_str_dispose(&path); + if ((error = load_grafts(repo)) < 0) { + error = -1; + goto on_error; + } - if (load_grafts(repo) < 0) - return -1; + if (git_array_size(roots) == 0) { + remove(path.ptr); + } - return 0; +on_error: + git_str_dispose(&path); + + return error; } int git_repository_shallow_roots(git_oidarray *out, git_repository *repo) diff --git a/src/libgit2/transports/smart.c b/src/libgit2/transports/smart.c index 9d1afeb05..b0925c8bb 100644 --- a/src/libgit2/transports/smart.c +++ b/src/libgit2/transports/smart.c @@ -496,17 +496,35 @@ const git_oid * git_shallowarray_get(git_shallowarray *array, size_t idx) int git_shallowarray_add(git_shallowarray *array, git_oid *oid) { size_t oid_index; + if (git_array_search(&oid_index, array->array, (git_array_compare_cb)git_oid_cmp, &oid) < 0) { git_oid *tmp = git_array_alloc(array->array); + GIT_ERROR_CHECK_ALLOC(tmp); + git_oid_cpy(tmp, oid); } + return 0; } int git_shallowarray_remove(git_shallowarray *array, git_oid *oid) { - GIT_UNUSED(array); - GIT_UNUSED(oid); - /* no git_array_remove… meh */ - return -1; + git_array_oid_t new_array = GIT_ARRAY_INIT; + git_oid *element; + git_oid *tmp; + size_t i; + + git_array_foreach(array->array, i, element) { + if (git_oid_cmp(oid, element)) { + tmp = git_array_alloc(new_array); + GIT_ERROR_CHECK_ALLOC(tmp); + + git_oid_cpy(tmp, element); + } + } + + git_array_clear(array->array); + array->array = new_array; + + return 0; } diff --git a/tests/libgit2/clone/shallow.c b/tests/libgit2/clone/shallow.c index 7fb056f91..2a88d5d05 100644 --- a/tests/libgit2/clone/shallow.c +++ b/tests/libgit2/clone/shallow.c @@ -131,3 +131,44 @@ void test_clone_shallow__clone_depth_five(void) git_revwalk_free(walk); git_repository_free(repo); } + +void test_clone_shallow__unshallow(void) +{ + git_str path = GIT_STR_INIT; + git_repository *repo; + git_revwalk *walk; + git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT; + git_fetch_options fetch_opts = GIT_FETCH_OPTIONS_INIT; + git_remote *origin = NULL; + git_oid oid; + size_t num_commits = 0; + int error = 0; + + clone_opts.fetch_opts.depth = 5; + clone_opts.remote_cb = remote_single_branch; + + git_str_joinpath(&path, clar_sandbox_path(), "unshallow"); + cl_git_pass(git_clone(&repo, "https://github.com/libgit2/TestGitRepository", git_str_cstr(&path), &clone_opts)); + cl_assert_equal_b(true, git_repository_is_shallow(repo)); + + fetch_opts.unshallow = 1; + cl_git_pass(git_remote_lookup(&origin, repo, "origin")); + + cl_git_pass(git_remote_fetch(origin, NULL, &fetch_opts, NULL)); + cl_assert_equal_b(false, git_repository_is_shallow(repo)); + + git_revwalk_new(&walk, repo); + git_revwalk_push_head(walk); + + while ((error = git_revwalk_next(&oid, walk)) == GIT_OK) { + num_commits++; + } + + cl_assert_equal_i(num_commits, 21); + cl_assert_equal_i(error, GIT_ITEROVER); + + git_remote_free(origin); + git_str_dispose(&path); + git_revwalk_free(walk); + git_repository_free(repo); +} diff --git a/tests/libgit2/transports/smart/shallowarray.c b/tests/libgit2/transports/smart/shallowarray.c new file mode 100644 index 000000000..c51e62713 --- /dev/null +++ b/tests/libgit2/transports/smart/shallowarray.c @@ -0,0 +1,52 @@ +#include "clar_libgit2.h" + +#include "git2/oid.h" +#include "git2/transport.h" + +#include "common.h" +#include "transports/smart.h" +#include "oid.h" + +#include + +#define oid_0 "c070ad8c08840c8116da865b2d65593a6bb9cd2a" +#define oid_1 "0966a434eb1a025db6b71485ab63a3bfbea520b6" +#define oid_2 "83834a7afdaa1a1260568567f6ad90020389f664" + +void test_transports_smart_shallowarray__add_and_remove_oid_from_shallowarray(void) +{ + git_oid oid_0_obj, oid_1_obj, oid_2_obj; + git_shallowarray *shallow_roots = git__malloc(sizeof(git_shallowarray)); + git_array_init(shallow_roots->array); + + git_oid_fromstr(&oid_0_obj, oid_0); + git_oid_fromstr(&oid_1_obj, oid_1); + git_oid_fromstr(&oid_2_obj, oid_2); + + git_shallowarray_add(shallow_roots, &oid_0_obj); + git_shallowarray_add(shallow_roots, &oid_1_obj); + git_shallowarray_add(shallow_roots, &oid_2_obj); + + cl_assert_equal_i(3, shallow_roots->array.size); + cl_assert_equal_s("c070ad8c08840c8116da865b2d65593a6bb9cd2a", git_oid_tostr_s(&shallow_roots->array.ptr[0])); + cl_assert_equal_s("0966a434eb1a025db6b71485ab63a3bfbea520b6", git_oid_tostr_s(&shallow_roots->array.ptr[1])); + cl_assert_equal_s("83834a7afdaa1a1260568567f6ad90020389f664", git_oid_tostr_s(&shallow_roots->array.ptr[2])); + + git_shallowarray_remove(shallow_roots, &oid_2_obj); + + cl_assert_equal_i(2, shallow_roots->array.size); + cl_assert_equal_s("c070ad8c08840c8116da865b2d65593a6bb9cd2a", git_oid_tostr_s(&shallow_roots->array.ptr[0])); + cl_assert_equal_s("0966a434eb1a025db6b71485ab63a3bfbea520b6", git_oid_tostr_s(&shallow_roots->array.ptr[1])); + + git_shallowarray_remove(shallow_roots, &oid_1_obj); + + cl_assert_equal_i(1, shallow_roots->array.size); + cl_assert_equal_s("c070ad8c08840c8116da865b2d65593a6bb9cd2a", git_oid_tostr_s(&shallow_roots->array.ptr[0])); + + git_shallowarray_remove(shallow_roots, &oid_0_obj); + + cl_assert_equal_i(0, shallow_roots->array.size); + + git_array_clear(shallow_roots->array); + git__free(shallow_roots); +} -- cgit v1.2.1 From 49e641be8c5549f6f5731d6d72e9e6902e71d0e1 Mon Sep 17 00:00:00 2001 From: yuangli Date: Thu, 11 Aug 2022 09:32:28 +0100 Subject: remove unused api --- include/git2/remote.h | 2 +- include/git2/repository.h | 11 ----------- src/libgit2/repository.c | 14 -------------- 3 files changed, 1 insertion(+), 26 deletions(-) diff --git a/include/git2/remote.h b/include/git2/remote.h index 240e5aa4e..f1cee17aa 100644 --- a/include/git2/remote.h +++ b/include/git2/remote.h @@ -751,7 +751,7 @@ typedef struct { int depth; /** - * Unshallow flag of the fetch to perform. + * Convert a shallow repository to a full repository. * * The default is 0, which means the flag is off. */ diff --git a/include/git2/repository.h b/include/git2/repository.h index d6b054dc3..258b9ac00 100644 --- a/include/git2/repository.h +++ b/include/git2/repository.h @@ -923,17 +923,6 @@ GIT_EXTERN(const char *) git_repository_get_namespace(git_repository *repo); */ GIT_EXTERN(int) git_repository_is_shallow(git_repository *repo); -/** - * Determine the shallow roots of the repository - * - * This oidarray is owned by the library. Do not free it. - * - * @param out An array of shallow oids. - * @param repo The repository - * @return 0 on success, an error otherwise. - */ -GIT_EXTERN(int) git_repository_shallow_roots(git_oidarray *out, git_repository *repo); - /** * Retrieve the configured identity to use for reflogs * diff --git a/src/libgit2/repository.c b/src/libgit2/repository.c index 13559ef07..6b77007aa 100644 --- a/src/libgit2/repository.c +++ b/src/libgit2/repository.c @@ -3393,20 +3393,6 @@ on_error: return error; } -int git_repository_shallow_roots(git_oidarray *out, git_repository *repo) -{ - int ret; - git_array_oid_t array = GIT_ARRAY_INIT; - - assert(out); - - ret = git_repository__shallow_roots(&array, repo); - - git_oidarray__from_array(out, &array); - - return ret; -} - int git_repository_is_shallow(git_repository *repo) { git_str path = GIT_STR_INIT; -- cgit v1.2.1 From 8b521f018b734f58db2b9aec184e0f96422f140e Mon Sep 17 00:00:00 2001 From: yuangli Date: Thu, 11 Aug 2022 09:38:21 +0100 Subject: document unshallow behaviour in fetch.c --- src/libgit2/fetch.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libgit2/fetch.c b/src/libgit2/fetch.c index a015cfbd3..3216b261c 100644 --- a/src/libgit2/fetch.c +++ b/src/libgit2/fetch.c @@ -60,7 +60,9 @@ static int mark_local(git_remote *remote) return -1; git_vector_foreach(&remote->refs, i, head) { - /* If we have the object, mark it so we don't ask for it */ + /* If we have the object, mark it so we don't ask for it. + However if we are unshallowing, we need to ask for it + even though the head exists locally. */ if (remote->nego.depth != INT_MAX && git_odb_exists(odb, &head->oid)) head->local = 1; else -- cgit v1.2.1