From 43db928895fa85e4ee104dc8757275400d5acc2a Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Tue, 25 Apr 2023 10:48:33 +0100 Subject: grafts: make `from_file` be `open_or_refresh` The semantics of `from_file` are weird - it looks like a function that just opens a file, but it actually inspects the pointer, which is unexpected and could make things very crashy. Make an `open` function that just does an open, and move the magic to `open_or_refresh` whose name better indicates that it may do weird stuff. --- src/libgit2/grafts.c | 19 ++++++++++++++----- src/libgit2/grafts.h | 3 ++- src/libgit2/repository.c | 4 ++-- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/libgit2/grafts.c b/src/libgit2/grafts.c index 00de03e8b..83f5b2ab4 100644 --- a/src/libgit2/grafts.c +++ b/src/libgit2/grafts.c @@ -44,7 +44,7 @@ int git_grafts_new(git_grafts **out, git_oid_t oid_type) return 0; } -int git_grafts_from_file( +int git_grafts_open( git_grafts **out, const char *path, git_oid_t oid_type) @@ -52,10 +52,7 @@ int git_grafts_from_file( git_grafts *grafts = NULL; int error; - GIT_ASSERT_ARG(path && oid_type); - - if (*out) - return git_grafts_refresh(*out); + GIT_ASSERT_ARG(out && path && oid_type); if ((error = git_grafts_new(&grafts, oid_type)) < 0) goto error; @@ -67,12 +64,24 @@ int git_grafts_from_file( goto error; *out = grafts; + error: if (error < 0) git_grafts_free(grafts); + return error; } +int git_grafts_open_or_refresh( + git_grafts **out, + const char *path, + git_oid_t oid_type) +{ + GIT_ASSERT_ARG(out && path && oid_type); + + return *out ? git_grafts_refresh(*out) : git_grafts_open(out, path, oid_type); +} + void git_grafts_free(git_grafts *grafts) { if (!grafts) diff --git a/src/libgit2/grafts.h b/src/libgit2/grafts.h index 0c0e9cecb..0d561fc25 100644 --- a/src/libgit2/grafts.h +++ b/src/libgit2/grafts.h @@ -20,7 +20,8 @@ typedef struct { typedef struct git_grafts git_grafts; int git_grafts_new(git_grafts **out, git_oid_t oid_type); -int git_grafts_from_file(git_grafts **out, const char *path, git_oid_t oid_type); +int git_grafts_open(git_grafts **out, const char *path, git_oid_t oid_type); +int git_grafts_open_or_refresh(git_grafts **out, const char *path, git_oid_t oid_type); void git_grafts_free(git_grafts *grafts); void git_grafts_clear(git_grafts *grafts); diff --git a/src/libgit2/repository.c b/src/libgit2/repository.c index 5778a86f7..763b62375 100644 --- a/src/libgit2/repository.c +++ b/src/libgit2/repository.c @@ -852,13 +852,13 @@ static int load_grafts(git_repository *repo) if ((error = git_repository__item_path(&path, repo, GIT_REPOSITORY_ITEM_INFO)) < 0 || (error = git_str_joinpath(&path, path.ptr, "grafts")) < 0 || - (error = git_grafts_from_file(&repo->grafts, path.ptr, repo->oid_type)) < 0) + (error = git_grafts_open_or_refresh(&repo->grafts, path.ptr, repo->oid_type)) < 0) goto error; git_str_clear(&path); if ((error = git_str_joinpath(&path, repo->gitdir, "shallow")) < 0 || - (error = git_grafts_from_file(&repo->shallow_grafts, path.ptr, repo->oid_type)) < 0) + (error = git_grafts_open_or_refresh(&repo->shallow_grafts, path.ptr, repo->oid_type)) < 0) goto error; error: -- cgit v1.2.1