Skip to content

Commit 43db928

Browse files
committed
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.
1 parent 19ccab0 commit 43db928

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

src/libgit2/grafts.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,15 @@ int git_grafts_new(git_grafts **out, git_oid_t oid_type)
4444
return 0;
4545
}
4646

47-
int git_grafts_from_file(
47+
int git_grafts_open(
4848
git_grafts **out,
4949
const char *path,
5050
git_oid_t oid_type)
5151
{
5252
git_grafts *grafts = NULL;
5353
int error;
5454

55-
GIT_ASSERT_ARG(path && oid_type);
56-
57-
if (*out)
58-
return git_grafts_refresh(*out);
55+
GIT_ASSERT_ARG(out && path && oid_type);
5956

6057
if ((error = git_grafts_new(&grafts, oid_type)) < 0)
6158
goto error;
@@ -67,12 +64,24 @@ int git_grafts_from_file(
6764
goto error;
6865

6966
*out = grafts;
67+
7068
error:
7169
if (error < 0)
7270
git_grafts_free(grafts);
71+
7372
return error;
7473
}
7574

75+
int git_grafts_open_or_refresh(
76+
git_grafts **out,
77+
const char *path,
78+
git_oid_t oid_type)
79+
{
80+
GIT_ASSERT_ARG(out && path && oid_type);
81+
82+
return *out ? git_grafts_refresh(*out) : git_grafts_open(out, path, oid_type);
83+
}
84+
7685
void git_grafts_free(git_grafts *grafts)
7786
{
7887
if (!grafts)

src/libgit2/grafts.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ typedef struct {
2020
typedef struct git_grafts git_grafts;
2121

2222
int git_grafts_new(git_grafts **out, git_oid_t oid_type);
23-
int git_grafts_from_file(git_grafts **out, const char *path, git_oid_t oid_type);
23+
int git_grafts_open(git_grafts **out, const char *path, git_oid_t oid_type);
24+
int git_grafts_open_or_refresh(git_grafts **out, const char *path, git_oid_t oid_type);
2425
void git_grafts_free(git_grafts *grafts);
2526
void git_grafts_clear(git_grafts *grafts);
2627

src/libgit2/repository.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -852,13 +852,13 @@ static int load_grafts(git_repository *repo)
852852

853853
if ((error = git_repository__item_path(&path, repo, GIT_REPOSITORY_ITEM_INFO)) < 0 ||
854854
(error = git_str_joinpath(&path, path.ptr, "grafts")) < 0 ||
855-
(error = git_grafts_from_file(&repo->grafts, path.ptr, repo->oid_type)) < 0)
855+
(error = git_grafts_open_or_refresh(&repo->grafts, path.ptr, repo->oid_type)) < 0)
856856
goto error;
857857

858858
git_str_clear(&path);
859859

860860
if ((error = git_str_joinpath(&path, repo->gitdir, "shallow")) < 0 ||
861-
(error = git_grafts_from_file(&repo->shallow_grafts, path.ptr, repo->oid_type)) < 0)
861+
(error = git_grafts_open_or_refresh(&repo->shallow_grafts, path.ptr, repo->oid_type)) < 0)
862862
goto error;
863863

864864
error:

0 commit comments

Comments
 (0)