summaryrefslogtreecommitdiff
path: root/src/config_file.c
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2021-09-07 17:53:49 -0400
committerEdward Thomson <ethomson@edwardthomson.com>2021-10-17 09:49:01 -0400
commitf0e693b18afbe1de37d7da5b5a8967b6c87d8e53 (patch)
treebe5e1cdbfa218ba81ec06bf45e45cfeb7f79a2a5 /src/config_file.c
parent5346be3ddd3bcf19779c5d62e71f8442a0171133 (diff)
downloadlibgit2-ethomson/gitstr.tar.gz
str: introduce `git_str` for internal, `git_buf` is externalethomson/gitstr
libgit2 has two distinct requirements that were previously solved by `git_buf`. We require: 1. A general purpose string class that provides a number of utility APIs for manipulating data (eg, concatenating, truncating, etc). 2. A structure that we can use to return strings to callers that they can take ownership of. By using a single class (`git_buf`) for both of these purposes, we have confused the API to the point that refactorings are difficult and reasoning about correctness is also difficult. Move the utility class `git_buf` to be called `git_str`: this represents its general purpose, as an internal string buffer class. The name also is an homage to Junio Hamano ("gitstr"). The public API remains `git_buf`, and has a much smaller footprint. It is generally only used as an "out" param with strict requirements that follow the documentation. (Exceptions exist for some legacy APIs to avoid breaking callers unnecessarily.) Utility functions exist to convert a user-specified `git_buf` to a `git_str` so that we can call internal functions, then converting it back again.
Diffstat (limited to 'src/config_file.c')
-rw-r--r--src/config_file.c144
1 files changed, 72 insertions, 72 deletions
diff --git a/src/config_file.c b/src/config_file.c
index 2f83a4070..9c3d2ceb8 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -11,7 +11,7 @@
#include "git2/sys/config.h"
#include "array.h"
-#include "buffer.h"
+#include "str.h"
#include "config_backend.h"
#include "config_entries.h"
#include "config_parse.h"
@@ -41,7 +41,7 @@ typedef struct {
bool locked;
git_filebuf locked_buf;
- git_buf locked_content;
+ git_str locked_content;
config_file file;
} config_file_backend;
@@ -131,7 +131,7 @@ static int config_file_open(git_config_backend *cfg, git_config_level_t level, c
static int config_file_is_modified(int *modified, config_file *file)
{
config_file *include;
- git_buf buf = GIT_BUF_INIT;
+ git_str buf = GIT_STR_INIT;
git_oid hash;
uint32_t i;
int error = 0;
@@ -159,7 +159,7 @@ check_includes:
}
out:
- git_buf_dispose(&buf);
+ git_str_dispose(&buf);
return error;
}
@@ -486,7 +486,7 @@ static int config_file_unlock(git_config_backend *_cfg, int success)
}
git_filebuf_cleanup(&cfg->locked_buf);
- git_buf_dispose(&cfg->locked_content);
+ git_str_dispose(&cfg->locked_content);
cfg->locked = false;
return error;
@@ -523,7 +523,7 @@ int git_config_backend_from_file(git_config_backend **out, const char *path)
return 0;
}
-static int included_path(git_buf *out, const char *dir, const char *path)
+static int included_path(git_str *out, const char *dir, const char *path)
{
/* From the user's home */
if (path[0] == '~' && path[1] == '/')
@@ -535,7 +535,7 @@ static int included_path(git_buf *out, const char *dir, const char *path)
/* Escape the values to write them to the file */
static char *escape_value(const char *ptr)
{
- git_buf buf;
+ git_str buf;
size_t len;
const char *esc;
@@ -545,29 +545,29 @@ static char *escape_value(const char *ptr)
if (!len)
return git__calloc(1, sizeof(char));
- if (git_buf_init(&buf, len) < 0)
+ if (git_str_init(&buf, len) < 0)
return NULL;
while (*ptr != '\0') {
if ((esc = strchr(git_config_escaped, *ptr)) != NULL) {
- git_buf_putc(&buf, '\\');
- git_buf_putc(&buf, git_config_escapes[esc - git_config_escaped]);
+ git_str_putc(&buf, '\\');
+ git_str_putc(&buf, git_config_escapes[esc - git_config_escaped]);
} else {
- git_buf_putc(&buf, *ptr);
+ git_str_putc(&buf, *ptr);
}
ptr++;
}
- if (git_buf_oom(&buf))
+ if (git_str_oom(&buf))
return NULL;
- return git_buf_detach(&buf);
+ return git_str_detach(&buf);
}
static int parse_include(config_file_parse_data *parse_data, const char *file)
{
config_file *include;
- git_buf path = GIT_BUF_INIT;
+ git_str path = GIT_STR_INIT;
char *dir;
int result;
@@ -577,7 +577,7 @@ static int parse_include(config_file_parse_data *parse_data, const char *file)
if ((result = git_path_dirname_r(&path, parse_data->file->path)) < 0)
return result;
- dir = git_buf_detach(&path);
+ dir = git_str_detach(&path);
result = included_path(&path, dir, file);
git__free(dir);
@@ -588,7 +588,7 @@ static int parse_include(config_file_parse_data *parse_data, const char *file)
GIT_ERROR_CHECK_ALLOC(include);
memset(include, 0, sizeof(*include));
git_array_init(include->includes);
- include->path = git_buf_detach(&path);
+ include->path = git_str_detach(&path);
result = config_file_read(parse_data->entries, parse_data->repo, include,
parse_data->level, parse_data->depth+1);
@@ -608,38 +608,38 @@ static int do_match_gitdir(
const char *condition,
bool case_insensitive)
{
- git_buf pattern = GIT_BUF_INIT, gitdir = GIT_BUF_INIT;
+ git_str pattern = GIT_STR_INIT, gitdir = GIT_STR_INIT;
int error;
if (condition[0] == '.' && git_path_is_dirsep(condition[1])) {
git_path_dirname_r(&pattern, cfg_file);
- git_buf_joinpath(&pattern, pattern.ptr, condition + 2);
+ git_str_joinpath(&pattern, pattern.ptr, condition + 2);
} else if (condition[0] == '~' && git_path_is_dirsep(condition[1]))
git_sysdir_expand_global_file(&pattern, condition + 1);
else if (!git_path_is_absolute(condition))
- git_buf_joinpath(&pattern, "**", condition);
+ git_str_joinpath(&pattern, "**", condition);
else
- git_buf_sets(&pattern, condition);
+ git_str_sets(&pattern, condition);
if (git_path_is_dirsep(condition[strlen(condition) - 1]))
- git_buf_puts(&pattern, "**");
+ git_str_puts(&pattern, "**");
- if (git_buf_oom(&pattern)) {
+ if (git_str_oom(&pattern)) {
error = -1;
goto out;
}
- if ((error = git_repository_item_path(&gitdir, repo, GIT_REPOSITORY_ITEM_GITDIR)) < 0)
+ if ((error = git_repository__item_path(&gitdir, repo, GIT_REPOSITORY_ITEM_GITDIR)) < 0)
goto out;
if (git_path_is_dirsep(gitdir.ptr[gitdir.size - 1]))
- git_buf_truncate(&gitdir, gitdir.size - 1);
+ git_str_truncate(&gitdir, gitdir.size - 1);
*matches = wildmatch(pattern.ptr, gitdir.ptr,
WM_PATHNAME | (case_insensitive ? WM_CASEFOLD : 0)) == WM_MATCH;
out:
- git_buf_dispose(&pattern);
- git_buf_dispose(&gitdir);
+ git_str_dispose(&pattern);
+ git_str_dispose(&gitdir);
return error;
}
@@ -667,7 +667,7 @@ static int conditional_match_onbranch(
const char *cfg_file,
const char *condition)
{
- git_buf reference = GIT_BUF_INIT, buf = GIT_BUF_INIT;
+ git_str reference = GIT_STR_INIT, buf = GIT_STR_INIT;
int error;
GIT_UNUSED(cfg_file);
@@ -680,33 +680,33 @@ static int conditional_match_onbranch(
* an endless recursion.
*/
- if ((error = git_buf_joinpath(&buf, git_repository_path(repo), GIT_HEAD_FILE)) < 0 ||
+ if ((error = git_str_joinpath(&buf, git_repository_path(repo), GIT_HEAD_FILE)) < 0 ||
(error = git_futils_readbuffer(&reference, buf.ptr)) < 0)
goto out;
- git_buf_rtrim(&reference);
+ git_str_rtrim(&reference);
if (git__strncmp(reference.ptr, GIT_SYMREF, strlen(GIT_SYMREF)))
goto out;
- git_buf_consume(&reference, reference.ptr + strlen(GIT_SYMREF));
+ git_str_consume(&reference, reference.ptr + strlen(GIT_SYMREF));
if (git__strncmp(reference.ptr, GIT_REFS_HEADS_DIR, strlen(GIT_REFS_HEADS_DIR)))
goto out;
- git_buf_consume(&reference, reference.ptr + strlen(GIT_REFS_HEADS_DIR));
+ git_str_consume(&reference, reference.ptr + strlen(GIT_REFS_HEADS_DIR));
/*
* If the condition ends with a '/', then we should treat it as if
* it had '**' appended.
*/
- if ((error = git_buf_sets(&buf, condition)) < 0)
+ if ((error = git_str_sets(&buf, condition)) < 0)
goto out;
if (git_path_is_dirsep(condition[strlen(condition) - 1]) &&
- (error = git_buf_puts(&buf, "**")) < 0)
+ (error = git_str_puts(&buf, "**")) < 0)
goto out;
*matches = wildmatch(buf.ptr, reference.ptr, WM_PATHNAME) == WM_MATCH;
out:
- git_buf_dispose(&reference);
- git_buf_dispose(&buf);
+ git_str_dispose(&reference);
+ git_str_dispose(&buf);
return error;
@@ -763,7 +763,7 @@ static int read_on_variable(
void *data)
{
config_file_parse_data *parse_data = (config_file_parse_data *)data;
- git_buf buf = GIT_BUF_INIT;
+ git_str buf = GIT_STR_INIT;
git_config_entry *entry;
const char *c;
int result = 0;
@@ -777,19 +777,19 @@ static int read_on_variable(
* here. Git appears to warn in most cases if it sees
* un-namespaced config options.
*/
- git_buf_puts(&buf, current_section);
- git_buf_putc(&buf, '.');
+ git_str_puts(&buf, current_section);
+ git_str_putc(&buf, '.');
}
for (c = var_name; *c; c++)
- git_buf_putc(&buf, git__tolower(*c));
+ git_str_putc(&buf, git__tolower(*c));
- if (git_buf_oom(&buf))
+ if (git_str_oom(&buf))
return -1;
entry = git__calloc(1, sizeof(git_config_entry));
GIT_ERROR_CHECK_ALLOC(entry);
- entry->name = git_buf_detach(&buf);
+ entry->name = git_str_detach(&buf);
entry->value = var_value ? git__strdup(var_value) : NULL;
entry->level = parse_data->level;
entry->include_depth = parse_data->depth;
@@ -856,7 +856,7 @@ static int config_file_read(
git_config_level_t level,
int depth)
{
- git_buf contents = GIT_BUF_INIT;
+ git_str contents = GIT_STR_INIT;
struct stat st;
int error;
@@ -877,36 +877,36 @@ static int config_file_read(
goto out;
out:
- git_buf_dispose(&contents);
+ git_str_dispose(&contents);
return error;
}
-static int write_section(git_buf *fbuf, const char *key)
+static int write_section(git_str *fbuf, const char *key)
{
int result;
const char *dot;
- git_buf buf = GIT_BUF_INIT;
+ git_str buf = GIT_STR_INIT;
/* All of this just for [section "subsection"] */
dot = strchr(key, '.');
- git_buf_putc(&buf, '[');
+ git_str_putc(&buf, '[');
if (dot == NULL) {
- git_buf_puts(&buf, key);
+ git_str_puts(&buf, key);
} else {
char *escaped;
- git_buf_put(&buf, key, dot - key);
+ git_str_put(&buf, key, dot - key);
escaped = escape_value(dot + 1);
GIT_ERROR_CHECK_ALLOC(escaped);
- git_buf_printf(&buf, " \"%s\"", escaped);
+ git_str_printf(&buf, " \"%s\"", escaped);
git__free(escaped);
}
- git_buf_puts(&buf, "]\n");
+ git_str_puts(&buf, "]\n");
- if (git_buf_oom(&buf))
+ if (git_str_oom(&buf))
return -1;
- result = git_buf_put(fbuf, git_buf_cstr(&buf), buf.size);
- git_buf_dispose(&buf);
+ result = git_str_put(fbuf, git_str_cstr(&buf), buf.size);
+ git_str_dispose(&buf);
return result;
}
@@ -930,8 +930,8 @@ static const char *quotes_for_value(const char *value)
}
struct write_data {
- git_buf *buf;
- git_buf buffered_comment;
+ git_str *buf;
+ git_str buffered_comment;
unsigned int in_section : 1,
preg_replaced : 1;
const char *orig_section;
@@ -942,12 +942,12 @@ struct write_data {
const char *value;
};
-static int write_line_to(git_buf *buf, const char *line, size_t line_len)
+static int write_line_to(git_str *buf, const char *line, size_t line_len)
{
- int result = git_buf_put(buf, line, line_len);
+ int result = git_str_put(buf, line, line_len);
if (!result && line_len && line[line_len-1] != '\n')
- result = git_buf_printf(buf, "\n");
+ result = git_str_printf(buf, "\n");
return result;
}
@@ -963,7 +963,7 @@ static int write_value(struct write_data *write_data)
int result;
q = quotes_for_value(write_data->value);
- result = git_buf_printf(write_data->buf,
+ result = git_str_printf(write_data->buf,
"\t%s = %s%s%s\n", write_data->orig_name, q, write_data->value, q);
/* If we are updating a single name/value, we're done. Setting `value`
@@ -1002,8 +1002,8 @@ static int write_on_section(
* If there were comments just before this section, dump them as well.
*/
if (!result) {
- result = git_buf_put(write_data->buf, write_data->buffered_comment.ptr, write_data->buffered_comment.size);
- git_buf_clear(&write_data->buffered_comment);
+ result = git_str_put(write_data->buf, write_data->buffered_comment.ptr, write_data->buffered_comment.size);
+ git_str_clear(&write_data->buffered_comment);
}
if (!result)
@@ -1031,10 +1031,10 @@ static int write_on_variable(
/*
* If there were comments just before this variable, let's dump them as well.
*/
- if ((error = git_buf_put(write_data->buf, write_data->buffered_comment.ptr, write_data->buffered_comment.size)) < 0)
+ if ((error = git_str_put(write_data->buf, write_data->buffered_comment.ptr, write_data->buffered_comment.size)) < 0)
return error;
- git_buf_clear(&write_data->buffered_comment);
+ git_str_clear(&write_data->buffered_comment);
/* See if we are to update this name/value pair; first examine name */
if (write_data->in_section &&
@@ -1081,7 +1081,7 @@ static int write_on_eof(
/*
* If we've buffered comments when reaching EOF, make sure to dump them.
*/
- if ((result = git_buf_put(write_data->buf, write_data->buffered_comment.ptr, write_data->buffered_comment.size)) < 0)
+ if ((result = git_str_put(write_data->buf, write_data->buffered_comment.ptr, write_data->buffered_comment.size)) < 0)
return result;
/* If we are at the EOF and have not written our value (again, for a
@@ -1108,7 +1108,7 @@ static int config_file_write(config_file_backend *cfg, const char *orig_key, con
{
char *orig_section = NULL, *section = NULL, *orig_name, *name, *ldot;
- git_buf buf = GIT_BUF_INIT, contents = GIT_BUF_INIT;
+ git_str buf = GIT_STR_INIT, contents = GIT_STR_INIT;
git_config_parser parser = GIT_CONFIG_PARSER_INIT;
git_filebuf file = GIT_FILEBUF_INIT;
struct write_data write_data;
@@ -1117,7 +1117,7 @@ static int config_file_write(config_file_backend *cfg, const char *orig_key, con
memset(&write_data, 0, sizeof(write_data));
if (cfg->locked) {
- error = git_buf_puts(&contents, git_buf_cstr(&cfg->locked_content) == NULL ? "" : git_buf_cstr(&cfg->locked_content));
+ error = git_str_puts(&contents, git_str_cstr(&cfg->locked_content) == NULL ? "" : git_str_cstr(&cfg->locked_content));
} else {
if ((error = git_filebuf_open(&file, cfg->file.path, GIT_FILEBUF_HASH_CONTENTS,
GIT_CONFIG_FILE_MODE)) < 0)
@@ -1157,10 +1157,10 @@ static int config_file_write(config_file_backend *cfg, const char *orig_key, con
if (cfg->locked) {
size_t len = buf.asize;
/* Update our copy with the modified contents */
- git_buf_dispose(&cfg->locked_content);
- git_buf_attach(&cfg->locked_content, git_buf_detach(&buf), len);
+ git_str_dispose(&cfg->locked_content);
+ git_str_attach(&cfg->locked_content, git_str_detach(&buf), len);
} else {
- git_filebuf_write(&file, git_buf_cstr(&buf), git_buf_len(&buf));
+ git_filebuf_write(&file, git_str_cstr(&buf), git_str_len(&buf));
if ((error = git_filebuf_commit(&file)) < 0)
goto done;
@@ -1172,9 +1172,9 @@ static int config_file_write(config_file_backend *cfg, const char *orig_key, con
done:
git__free(section);
git__free(orig_section);
- git_buf_dispose(&write_data.buffered_comment);
- git_buf_dispose(&buf);
- git_buf_dispose(&contents);
+ git_str_dispose(&write_data.buffered_comment);
+ git_str_dispose(&buf);
+ git_str_dispose(&contents);
git_filebuf_cleanup(&file);
git_config_parser_dispose(&parser);