10000 Shallow support v2 by pks-t · Pull Request #5254 · libgit2/libgit2 · GitHub
[go: up one dir, main page]

Skip to content

Shallow support v2 #5254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
pks-t committed Jun 27, 2020
commit fd2398b2fc09e3ae292fb33f2ec2a45b6bf8e3e9
55 changes: 55 additions & 0 deletions src/grafts.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
grafts->path = git__strdup(path);
grafts->path = git__strdup(path);
GIT_ERROR_CHECK_ALLOC(grafts->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);
Expand All @@ -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;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert(grafts);

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the kind of thing where I'm wondering "how would it work after I add the write-side of it" (ie. the actual part where the grafts are updated from the server's view). Swallowing ENOTFOUND would make an externally-triggered delete of the graft file (eg. an out-of-process unshallow takes place which just unlink() the file, stuff like that 😉). I haven't checked the semantics of readbuffer_updated in this case…

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's removed then it would return GIT_ENOTFOUND. But in fact, we should clear the grafts' contents in that case, which I missed out for now, so that definitely needs to be added

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;
Expand Down
2 changes: 2 additions & 0 deletions src/grafts.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
73 changes: 15 additions & 58 deletions src/repository.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -581,49 +578,25 @@ static int find_repo(

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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 0 additions & 3 deletions src/repository.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
0