Commit 14a309ab by Patrick Steinhardt

repository: convert grafts parsing to use parse context

Instead of using the newly introduced `git_buf_foreach_line`, which
modifies the buffer itself, we should try to use our existing parsing
infrastructure in "parse.h".

Convert the grafts parsing code to make use of `git_parse_ctx`. Remove
the `git_buf_foreach_line` macro, as grafts have been its sole user.
parent 22f201b1
...@@ -219,9 +219,4 @@ int git_buf_splice( ...@@ -219,9 +219,4 @@ int git_buf_splice(
const char *data, const char *data,
size_t nb_to_insert); size_t nb_to_insert);
/* warning: this will wreck your buf contents */
#define git_buf_foreach_line(line_start, line_end, line_num, buf) \
while (((line_start) = git__strsep(&(buf)->ptr, "\n")) != NULL && \
((line_end) = (line_start) + strlen((line_start))) != NULL && ++(line_num))
#endif #endif
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include "annotated_commit.h" #include "annotated_commit.h"
#include "submodule.h" #include "submodule.h"
#include "worktree.h" #include "worktree.h"
#include "parse.h"
#include "strmap.h" #include "strmap.h"
...@@ -581,14 +582,11 @@ out: ...@@ -581,14 +582,11 @@ out:
static int load_grafts(git_repository *repo) static int load_grafts(git_repository *repo)
{ {
git_array_oid_t parents = GIT_ARRAY_INIT;
git_buf graft_path = GIT_BUF_INIT; git_buf graft_path = GIT_BUF_INIT;
git_buf contents = GIT_BUF_INIT; git_buf contents = GIT_BUF_INIT;
git_buf dup_contents; git_parse_ctx parser;
const char *line_start;
const char *line_end;
int line_num = 0;
int error, updated; int error, updated;
git_array_oid_t parents = GIT_ARRAY_INIT;
if ((error = git_repository_item_path(&graft_path, repo, GIT_REPOSITORY_ITEM_INFO)) < 0) if ((error = git_repository_item_path(&graft_path, repo, GIT_REPOSITORY_ITEM_INFO)) < 0)
return error; return error;
...@@ -598,54 +596,52 @@ static int load_grafts(git_repository *repo) ...@@ -598,54 +596,52 @@ static int load_grafts(git_repository *repo)
return error; return error;
} }
error = git_futils_readbuffer_updated(&contents, git_buf_cstr(&graft_path), &repo->graft_checksum, &updated); error = git_futils_readbuffer_updated(&contents, git_buf_cstr(&graft_path),
git_buf_dispose(&graft_path); &repo->graft_checksum, &updated);
if (error < 0 || error == GIT_ENOTFOUND || !updated) {
if (error == GIT_ENOTFOUND || !updated) if (error == GIT_ENOTFOUND)
return 0; error = 0;
if (error < 0)
goto cleanup; goto cleanup;
}
if (updated)
git_grafts_clear(repo->grafts); git_grafts_clear(repo->grafts);
dup_contents.ptr = contents.ptr; if ((error = git_parse_ctx_init(&parser, contents.ptr, contents.size)) < 0)
git_buf_foreach_line(line_start, line_end, line_num, &dup_contents) { goto cleanup;
git_oid graft_oid, parent_oid;
error = git_oid_fromstrn(&graft_oid, line_start, GIT_OID_HEXSZ); for (; parser.remain_len; git_parse_advance_line(&parser)) {
if (error < 0) { const char *line_start = parser.line, *line_end = parser.line + parser.line_len;
git_error_set(GIT_ERROR_REPOSITORY, "Invalid OID at line %d", line_num); git_oid graft_oid;
error = -1;
} if (git_oid_fromstrn(&graft_oid, line_start, GIT_OID_HEXSZ) < 0)
goto invalid_oid;
line_start += GIT_OID_HEXSZ; line_start += GIT_OID_HEXSZ;
if (*(line_start++) == ' ') { while (line_start < line_end && *line_start == ' ') {
while (git_oid_fromstrn(&parent_oid, line_start, GIT_OID_HEXSZ) == 0) {
git_oid *id = git_array_alloc(parents); git_oid *id = git_array_alloc(parents);
GIT_ERROR_CHECK_ALLOC(id);
git_oid_cpy(id, &parent_oid); if (git_oid_fromstrn(id, ++line_start, GIT_OID_HEXSZ) < 0)
goto invalid_oid;
line_start += GIT_OID_HEXSZ; line_start += GIT_OID_HEXSZ;
if (line_start >= line_end) {
break;
}
line_start += 1;
}
} }
if (git_grafts_add(repo->grafts, &graft_oid, parents) < 0) { if ((error = git_grafts_add(repo->grafts, &graft_oid, parents)) < 0)
git_error_set(GIT_ERROR_REPOSITORY, "Invalid graft at line %d", line_num);
error = -1;
goto cleanup; goto cleanup;
}
git_array_clear(parents); git_array_clear(parents);
line_num++; continue;
invalid_oid:
git_error_set(GIT_ERROR_REPOSITORY, "invalid OID at line %" PRIuZ, parser.line_num);
error = -1;
goto cleanup;
} }
cleanup: cleanup:
git_array_clear(parents); git_array_clear(parents);
git_buf_dispose(&contents); git_buf_dispose(&contents);
git_buf_dispose(&graft_path);
return error; return 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