diff options
Diffstat (limited to 'src/repository.c')
-rw-r--r-- | src/repository.c | 89 |
1 files changed, 89 insertions, 0 deletions
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; |