Skip to content

Commit 985f5cd

Browse files
committed
config_file: split out function that reads entries from a buffer
The `config_read` function currently performs both reading the on-disk config file as well as parsing the retrieved buffer contents. To optimize how we refresh our config entries from an in-memory buffer, we need to be able to directly parse buffers, though, without involving any on-disk files at all. Extract a new function `config_read_buffer` that sets up the parsing logic and then parses config entries from a buffer, only. Have `config_read` use it to avoid duplicated logic.
1 parent 3e1c137 commit 985f5cd

File tree

1 file changed

+40
-20
lines changed

1 file changed

+40
-20
lines changed

src/config_file.c

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ typedef struct {
6262
} diskfile_parse_state;
6363

6464
static int config_read(git_config_entries *entries, const git_repository *repo, git_config_file *file, git_config_level_t level, int depth);
65+
static int config_read_buffer(git_config_entries *entries, const git_repository *repo, git_config_file *file, git_config_level_t level, int depth, const char *buf, size_t buflen);
6566
static int config_write(diskfile_backend *cfg, const char *orig_key, const char *key, const p_regex_t *preg, const char *value);
6667
static char *escape_value(const char *ptr);
6768

@@ -845,45 +846,33 @@ static int read_on_variable(
845846
return result;
846847
}
847848

848-
static int config_read(
849+
static int config_read_buffer(
849850
git_config_entries *entries,
850851
const git_repository *repo,
851852
git_config_file *file,
852853
git_config_level_t level,
853-
int depth)
854+
int depth,
855+
const char *buf,
856+
size_t buflen)
854857
{
855858
diskfile_parse_state parse_data;
856859
git_config_parser reader;
857-
git_buf contents = GIT_BUF_INIT;
858-
struct stat st;
859860
int error;
860861

861862
if (depth >= MAX_INCLUDE_DEPTH) {
862863
git_error_set(GIT_ERROR_CONFIG, "maximum config include depth reached");
863864
return -1;
864865
}
865866

866-
if (p_stat(file->path, &st) < 0) {
867-
error = git_path_set_error(errno, file->path, "stat");
868-
goto out;
869-
}
870-
871-
if ((error = git_futils_readbuffer(&contents, file->path)) < 0)
872-
goto out;
873-
874-
git_parse_ctx_init(&reader.ctx, contents.ptr, contents.size);
875-
876-
git_futils_filestamp_set_from_stat(&file->stamp, &st);
877-
if ((error = git_hash_buf(&file->checksum, contents.ptr, contents.size)) < 0)
878-
goto out;
879-
880867
/* Initialize the reading position */
881868
reader.file = file;
882-
git_parse_ctx_init(&reader.ctx, contents.ptr, contents.size);
869+
git_parse_ctx_init(&reader.ctx, buf, buflen);
883870

884871
/* If the file is empty, there's nothing for us to do */
885-
if (!reader.ctx.content || *reader.ctx.content == '\0')
872+
if (!reader.ctx.content || *reader.ctx.content == '\0') {
873+
error = 0;
886874
goto out;
875+
}
887876

888877
parse_data.repo = repo;
889878
parse_data.file_path = file->path;
@@ -893,6 +882,37 @@ static int config_read(
893882

894883
error = git_config_parse(&reader, NULL, read_on_variable, NULL, NULL, &parse_data);
895884

885+
out:
886+
return error;
887+
}
888+
889+
static int config_read(
890+
git_config_entries *entries,
891+
const git_repository *repo,
892+
git_config_file *file,
893+
git_config_level_t level,
894+
int depth)
895+
{
896+
git_buf contents = GIT_BUF_INIT;
897+
struct stat st;
898+
int error;
899+
900+
if (p_stat(file->path, &st) < 0) {
901+
error = git_path_set_error(errno, file->path, "stat");
902+
goto out;
903+
}
904+
905+
if ((error = git_futils_readbuffer(&contents, file->path)) < 0)
906+
goto out;
907+
908+
git_futils_filestamp_set_from_stat(&file->stamp, &st);
909+
if ((error = git_hash_buf(&file->checksum, contents.ptr, contents.size)) < 0)
910+
goto out;
911+
912+
if ((error = config_read_buffer(entries, repo, file, level, depth,
913+
contents.ptr, contents.size)) < 0)
914+
goto out;
915+
896916
out:
897917
git_buf_dispose(&contents);
898918
return error;

0 commit comments

Comments
 (0)