Commit 43db9288 by Edward Thomson

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.
parent 19ccab00
...@@ -44,7 +44,7 @@ int git_grafts_new(git_grafts **out, git_oid_t oid_type) ...@@ -44,7 +44,7 @@ int git_grafts_new(git_grafts **out, git_oid_t oid_type)
return 0; return 0;
} }
int git_grafts_from_file( int git_grafts_open(
git_grafts **out, git_grafts **out,
const char *path, const char *path,
git_oid_t oid_type) git_oid_t oid_type)
...@@ -52,10 +52,7 @@ int git_grafts_from_file( ...@@ -52,10 +52,7 @@ int git_grafts_from_file(
git_grafts *grafts = NULL; git_grafts *grafts = NULL;
int error; int error;
GIT_ASSERT_ARG(path && oid_type); GIT_ASSERT_ARG(out && path && oid_type);
if (*out)
return git_grafts_refresh(*out);
if ((error = git_grafts_new(&grafts, oid_type)) < 0) if ((error = git_grafts_new(&grafts, oid_type)) < 0)
goto error; goto error;
...@@ -67,12 +64,24 @@ int git_grafts_from_file( ...@@ -67,12 +64,24 @@ int git_grafts_from_file(
goto error; goto error;
*out = grafts; *out = grafts;
error: error:
if (error < 0) if (error < 0)
git_grafts_free(grafts); git_grafts_free(grafts);
return error; 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) void git_grafts_free(git_grafts *grafts)
{ {
if (!grafts) if (!grafts)
......
...@@ -20,7 +20,8 @@ typedef struct { ...@@ -20,7 +20,8 @@ typedef struct {
typedef struct git_grafts git_grafts; typedef struct git_grafts git_grafts;
int git_grafts_new(git_grafts **out, git_oid_t oid_type); 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_free(git_grafts *grafts);
void git_grafts_clear(git_grafts *grafts); void git_grafts_clear(git_grafts *grafts);
......
...@@ -852,13 +852,13 @@ static int load_grafts(git_repository *repo) ...@@ -852,13 +852,13 @@ static int load_grafts(git_repository *repo)
if ((error = git_repository__item_path(&path, repo, GIT_REPOSITORY_ITEM_INFO)) < 0 || if ((error = git_repository__item_path(&path, repo, GIT_REPOSITORY_ITEM_INFO)) < 0 ||
(error = git_str_joinpath(&path, path.ptr, "grafts")) < 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; goto error;
git_str_clear(&path); git_str_clear(&path);
if ((error = git_str_joinpath(&path, repo->gitdir, "shallow")) < 0 || 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; goto error;
error: error:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment