diff options
author | Yuang Li <yuangli88@hotmail.com> | 2022-08-11 10:29:57 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-11 10:29:57 +0100 |
commit | 7c2b1f45ff02ba12544b3f81d433ee698b44127b (patch) | |
tree | 967b7e0e30de827ceba74a8ef9b707723d7a1c59 /src/libgit2/repository.c | |
parent | a491917f8e37108b6180590736d833095626b1ec (diff) | |
parent | 8b521f018b734f58db2b9aec184e0f96422f140e (diff) | |
download | libgit2-7c2b1f45ff02ba12544b3f81d433ee698b44127b.tar.gz |
Merge pull request #6 from lya001/shallow-clone-network
Shallow clone network
Diffstat (limited to 'src/libgit2/repository.c')
-rw-r--r-- | src/libgit2/repository.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/libgit2/repository.c b/src/libgit2/repository.c index 761772d55..6b77007aa 100644 --- a/src/libgit2/repository.c +++ b/src/libgit2/repository.c @@ -3340,6 +3340,59 @@ 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 error = 0; + + if (!repo->shallow_grafts && (error = load_grafts(repo)) < 0) + return error; + + if ((error = git_grafts_refresh(repo->shallow_grafts)) < 0) + return error; + + 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) +{ + git_filebuf file = GIT_FILEBUF_INIT; + git_str path = GIT_STR_INIT; + int error = 0; + size_t idx; + git_oid *oid; + + assert(repo); + + if ((error = git_str_joinpath(&path, repo->gitdir, "shallow")) < 0) + goto on_error; + + if ((error = git_filebuf_open(&file, git_str_cstr(&path), GIT_FILEBUF_HASH_CONTENTS, 0666)) < 0) + goto on_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); + + if ((error = load_grafts(repo)) < 0) { + error = -1; + goto on_error; + } + + if (git_array_size(roots) == 0) { + remove(path.ptr); + } + +on_error: + git_str_dispose(&path); + + return error; +} + int git_repository_is_shallow(git_repository *repo) { git_str path = GIT_STR_INIT; |