summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2019-10-03 13:24:25 +0200
committerPatrick Steinhardt <ps@pks.im>2020-06-27 14:33:58 +0200
commitfd2398b2fc09e3ae292fb33f2ec2a45b6bf8e3e9 (patch)
treef72899fb609370aadc60a9467dc4502a9cea4d23
parent70867f7c594ea6960c07d10fef32c932f7fa3bbb (diff)
downloadlibgit2-fd2398b2fc09e3ae292fb33f2ec2a45b6bf8e3e9.tar.gz
grafts: move refresh logic into grafts code
The refresh logic for both "normal" and shallow grafts are currently part of the repository code and implemented twice. Unify them into the grafts code by introducing two new functions to create grafts from a file and to refresh a grafts structure.
-rw-r--r--src/grafts.c55
-rw-r--r--src/grafts.h2
-rw-r--r--src/repository.c73
-rw-r--r--src/repository.h3
4 files changed, 72 insertions, 61 deletions
diff --git a/src/grafts.c b/src/grafts.c
index 23b012ce8..da5bd1050 100644
--- a/src/grafts.c
+++ b/src/grafts.c
@@ -7,12 +7,17 @@
#include "grafts.h"
+#include "futils.h"
#include "oidarray.h"
#include "parse.h"
struct git_grafts {
/* Map of `git_commit_graft`s */
git_oidmap *commits;
+
+ /* File backing the graft. NULL if it's an in-memory graft */
+ char *path;
+ git_oid path_checksum;
};
int git_grafts_new(git_grafts **out)
@@ -31,10 +36,32 @@ int git_grafts_new(git_grafts **out)
return 0;
}
+int git_grafts_from_file(git_grafts **out, const char *path)
+{
+ git_grafts *grafts = NULL;
+ int error;
+
+ if ((error = git_grafts_new(&grafts)) < 0)
+ goto error;
+
+ grafts->path = git__strdup(path);
+ GIT_ERROR_CHECK_ALLOC(grafts->path);
+
+ if ((error = git_grafts_refresh(grafts)) < 0)
+ goto error;
+
+ *out = grafts;
+error:
+ if (error < 0)
+ git_grafts_free(grafts);
+ return error;
+}
+
void git_grafts_free(git_grafts *grafts)
{
if (!grafts)
return;
+ git__free(grafts->path);
git_grafts_clear(grafts);
git_oidmap_free(grafts->commits);
git__free(grafts);
@@ -54,6 +81,34 @@ void git_grafts_clear(git_grafts *grafts)
git_oidmap_clear(grafts->commits);
}
+int git_grafts_refresh(git_grafts *grafts)
+{
+ git_buf contents = GIT_BUF_INIT;
+ int error, updated = 0;
+
+ assert(grafts);
+
+ if (!grafts->path)
+ return 0;
+
+ error = git_futils_readbuffer_updated(&contents, grafts->path,
+ &grafts->path_checksum, &updated);
+ if (error < 0 || error == GIT_ENOTFOUND || !updated) {
+ if (error == GIT_ENOTFOUND) {
+ git_grafts_clear(grafts);
+ error = 0;
+ }
+ goto cleanup;
+ }
+
+ if ((error = git_grafts_parse(grafts, contents.ptr, contents.size)) < 0)
+ goto cleanup;
+
+cleanup:
+ git_buf_dispose(&contents);
+ return error;
+}
+
int git_grafts_parse(git_grafts *grafts, const char *content, size_t contentlen)
{
git_array_oid_t parents = GIT_ARRAY_INIT;
diff --git a/src/grafts.h b/src/grafts.h
index 305b0d61a..30062725e 100644
--- a/src/grafts.h
+++ b/src/grafts.h
@@ -20,9 +20,11 @@ typedef struct {
typedef struct git_grafts git_grafts;
int git_grafts_new(git_grafts **out);
+int git_grafts_from_file(git_grafts **out, const char *path);
void git_grafts_free(git_grafts *grafts);
void git_grafts_clear(git_grafts *grafts);
+int git_grafts_refresh(git_grafts *grafts);
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);
diff --git a/src/repository.c b/src/repository.c
index cad80845d..730f19c20 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -256,9 +256,6 @@ static git_repository *repository_alloc(void)
/* set all the entries in the configmap cache to `unset` */
git_repository__configmap_lookup_cache_clear(repo);
- if (git_grafts_new(&repo->grafts) < 0)
- goto on_error;
-
return repo;
on_error:
@@ -581,49 +578,25 @@ out:
static int load_grafts(git_repository *repo)
{
- git_buf graft_path = GIT_BUF_INIT;
- git_buf contents = GIT_BUF_INIT;
- int error, updated;
-
- if ((error = git_repository_item_path(&graft_path, repo, GIT_REPOSITORY_ITEM_INFO)) < 0)
- return error;
-
- if (git_buf_joinpath(&graft_path, graft_path.ptr, "grafts")) {
- git_buf_dispose(&graft_path);
- return error;
- }
+ git_buf path = GIT_BUF_INIT;
+ int error;
- error = git_futils_readbuffer_updated(&contents, git_buf_cstr(&graft_path),
- &repo->graft_checksum, &updated);
- if (error < 0 || error == GIT_ENOTFOUND || !updated) {
- if (error == GIT_ENOTFOUND)
- error = 0;
- goto cleanup;
- }
+ 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;
- if ((error = git_grafts_parse(repo->grafts, contents.ptr, contents.size)) < 0)
- goto cleanup;
+ git_buf_clear(&path);
-cleanup:
- git_buf_dispose(&contents);
- git_buf_dispose(&graft_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;
}
-static int load_shallow(git_repository *repo)
-{
- git_oidarray roots;
- int error;
-
- /* Graft shallow roots */
- if ((error = git_repository_shallow_roots(&roots, repo)) < 0)
- return error;
-
- git_oidarray_free(&roots);
- return 0;
-}
-
int git_repository_open_bare(
git_repository **repo_ptr,
const char *bare_path)
@@ -916,9 +889,6 @@ int git_repository_open_ext(
if ((error = load_grafts(repo)) < 0)
goto cleanup;
- if ((error = load_shallow(repo)) < 0)
- goto cleanup;
-
if ((flags & GIT_REPOSITORY_OPEN_BARE) != 0)
repo->is_bare = 1;
else {
@@ -2956,27 +2926,14 @@ int git_repository_state_cleanup(git_repository *repo)
int git_repository_shallow_roots(git_oidarray *out, git_repository *repo)
{
git_buf path = GIT_BUF_INIT, contents = GIT_BUF_INIT;
- int error, updated = 0;
+ int error;
assert(out && repo);
memset(out, 0, sizeof(*out));
- if (!repo->shallow_grafts && (error = git_grafts_new(&repo->shallow_grafts)) < 0)
- goto error;
-
- if ((error = git_buf_joinpath(&path, repo->gitdir, "shallow")) < 0 ||
- (error = git_futils_readbuffer_updated(&contents, git_buf_cstr(&path),
- &repo->shallow_checksum, &updated)) < 0) {
- if (error == GIT_ENOTFOUND)
- error = 0;
- goto error;
- }
-
- if (updated && (error = git_grafts_parse(repo->shallow_grafts, contents.ptr, contents.size)) < 0)
- goto error;
-
- if ((error = git_grafts_get_oids(out, repo->shallow_grafts)) < 0)
+ if ((error = git_grafts_refresh(repo->shallow_grafts)) < 0 ||
+ (error = git_grafts_get_oids(out, repo->shallow_grafts)) < 0)
goto error;
error:
diff --git a/src/repository.h b/src/repository.h
index 14b266567..81f2bc101 100644
--- a/src/repository.h
+++ b/src/repository.h
@@ -155,10 +155,7 @@ struct git_repository {
unsigned int lru_counter;
git_grafts *grafts;
- git_oid graft_checksum;
-
git_grafts *shallow_grafts;
- git_oid shallow_checksum;
git_atomic attr_session_key;