diff options
| author | yuangli <yuangli@mathworks.com> | 2022-07-26 16:26:32 +0100 |
|---|---|---|
| committer | yuangli <yuangli@mathworks.com> | 2022-07-26 16:26:32 +0100 |
| commit | 52ba17f3d6f9e3490f537a7c6dd243e9dbf9d760 (patch) | |
| tree | 29244004ab34fc757f3bf5c7f2b9ffbb531f042e /src/libgit2 | |
| parent | fbea439d4b6fc91c6b619d01b85ab3b7746e4c19 (diff) | |
| parent | 70a332a51b6364507d19bd1821b22fabbd1f2e08 (diff) | |
| download | libgit2-52ba17f3d6f9e3490f537a7c6dd243e9dbf9d760.tar.gz | |
Merge branch 'pr/pks-t/5254' into shallow-clone-local
Diffstat (limited to 'src/libgit2')
| -rw-r--r-- | src/libgit2/commit.c | 34 | ||||
| -rw-r--r-- | src/libgit2/libgit2.c | 3 | ||||
| -rw-r--r-- | src/libgit2/repository.c | 41 | ||||
| -rw-r--r-- | src/libgit2/repository.h | 7 |
4 files changed, 80 insertions, 5 deletions
diff --git a/src/libgit2/commit.c b/src/libgit2/commit.c index b137463f3..7432ef4ec 100644 --- a/src/libgit2/commit.c +++ b/src/libgit2/commit.c @@ -22,6 +22,7 @@ #include "object.h" #include "array.h" #include "oidarray.h" +#include "grafts.h" void git_commit__free(void *_commit) { @@ -417,10 +418,6 @@ static int commit_parse(git_commit *commit, const char *data, size_t size, unsig buffer += tree_len; } - /* - * TODO: commit grafts! - */ - while (git_oid__parse(&parent_id, &buffer, buffer_end, "parent ") == 0) { git_oid *new_id = git_array_alloc(commit->parent_ids); GIT_ERROR_CHECK_ALLOC(new_id); @@ -504,7 +501,34 @@ int git_commit__parse_raw(void *commit, const char *data, size_t size) int git_commit__parse_ext(git_commit *commit, git_odb_object *odb_obj, unsigned int flags) { - return commit_parse(commit, git_odb_object_data(odb_obj), git_odb_object_size(odb_obj), flags); + + int error; + if ((error = commit_parse(commit, git_odb_object_data(odb_obj), + git_odb_object_size(odb_obj), flags)) < 0) + return error; + + if (!git_shallow__enabled) + return 0; + + git_repository *repo = git_object_owner((git_object *)commit); + git_commit_graft *graft; + + /* Perform necessary grafts */ + if (git_grafts_get(&graft, repo->grafts, git_odb_object_id(odb_obj)) == 0 || + git_grafts_get(&graft, repo->shallow_grafts, git_odb_object_id(odb_obj)) == 0) { + size_t idx; + git_oid *oid; + git_array_clear(commit->parent_ids); + git_array_init_to_size(commit->parent_ids, git_array_size(graft->parents)); + git_array_foreach(graft->parents, idx, oid) { + git_oid *id = git_array_alloc(commit->parent_ids); + GIT_ERROR_CHECK_ALLOC(id); + + git_oid_cpy(id, oid); + } + } + + return 0; } int git_commit__parse(void *_commit, git_odb_object *odb_obj) diff --git a/src/libgit2/libgit2.c b/src/libgit2/libgit2.c index 2fda0722e..b76728e0c 100644 --- a/src/libgit2/libgit2.c +++ b/src/libgit2/libgit2.c @@ -13,6 +13,7 @@ #include "cache.h" #include "common.h" #include "filter.h" +#include "grafts.h" #include "hash.h" #include "index.h" #include "merge_driver.h" @@ -412,6 +413,8 @@ int git_libgit2_opts(int key, ...) case GIT_OPT_SET_OWNER_VALIDATION: git_repository__validate_ownership = (va_arg(ap, int) != 0); + case GIT_OPT_ENABLE_SHALLOW: + git_shallow__enabled = (va_arg(ap, int) != 0); break; default: diff --git a/src/libgit2/repository.c b/src/libgit2/repository.c index f761b5f32..2ecc510b2 100644 --- a/src/libgit2/repository.c +++ b/src/libgit2/repository.c @@ -15,6 +15,7 @@ #include "buf.h" #include "common.h" #include "commit.h" +#include "grafts.h" #include "tag.h" #include "blob.h" #include "futils.h" @@ -150,6 +151,8 @@ int git_repository__cleanup(git_repository *repo) git_repository_submodule_cache_clear(repo); git_cache_clear(&repo->objects); git_attr_cache_flush(repo); + git_grafts_free(repo->grafts); + git_grafts_free(repo->shallow_grafts); set_config(repo, NULL); set_index(repo, NULL); @@ -727,6 +730,27 @@ out: return error; } +static int load_grafts(git_repository *repo) +{ + git_buf path = GIT_BUF_INIT; + int error; + + if ((error = git_repository_item_path(&path, repo, GIT_REPOSITORY_ITEM_INFO)) < 0 || + (error = git_buf_joinpath(&path, path.ptr, "grafts")) < 0 || + (error = git_grafts_from_file(&repo->grafts, path.ptr)) < 0) + goto error; + + git_buf_clear(&path); + + if ((error = git_buf_joinpath(&path, repo->gitdir, "shallow")) < 0 || + (error = git_grafts_from_file(&repo->shallow_grafts, path.ptr)) < 0) + goto error; + +error: + git_buf_dispose(&path); + return error; +} + int git_repository_open_bare( git_repository **repo_ptr, const char *bare_path) @@ -1016,6 +1040,9 @@ int git_repository_open_ext( if ((error = check_extensions(config, version)) < 0) goto cleanup; + if (git_shallow__enabled && (error = load_grafts(repo)) < 0) + goto cleanup; + if ((flags & GIT_REPOSITORY_OPEN_BARE) != 0) { repo->is_bare = 1; } else { @@ -1402,6 +1429,20 @@ int git_repository_set_index(git_repository *repo, git_index *index) return 0; } +int git_repository_grafts__weakptr(git_grafts **out, git_repository *repo) +{ + assert(out && repo && repo->grafts); + *out = repo->grafts; + return 0; +} + +int git_repository_shallow_grafts__weakptr(git_grafts **out, git_repository *repo) +{ + assert(out && repo && repo->shallow_grafts); + *out = repo->shallow_grafts; + return 0; +} + int git_repository_set_namespace(git_repository *repo, const char *namespace) { git__free(repo->namespace); diff --git a/src/libgit2/repository.h b/src/libgit2/repository.h index a488f2bf2..6d39bb92a 100644 --- a/src/libgit2/repository.h +++ b/src/libgit2/repository.h @@ -24,6 +24,8 @@ #include "attrcache.h" #include "submodule.h" #include "diff_driver.h" +#include "grafts.h" +#include "oidarray.h" #define DOT_GIT ".git" #define GIT_DIR DOT_GIT "/" @@ -156,6 +158,9 @@ struct git_repository { unsigned int lru_counter; + git_grafts *grafts; + git_grafts *shallow_grafts; + git_atomic32 attr_session_key; intptr_t configmap_cache[GIT_CONFIGMAP_CACHE_MAX]; @@ -187,6 +192,8 @@ int git_repository_config__weakptr(git_config **out, git_repository *repo); int git_repository_odb__weakptr(git_odb **out, git_repository *repo); int git_repository_refdb__weakptr(git_refdb **out, git_repository *repo); int git_repository_index__weakptr(git_index **out, git_repository *repo); +int git_repository_grafts__weakptr(git_grafts **out, git_repository *repo); +int git_repository_shallow_grafts__weakptr(git_grafts **out, git_repository *repo); /* * Configuration map cache |
