Commit f0e693b1 by Edward Thomson

str: introduce `git_str` for internal, `git_buf` is external

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.
parent 5346be3d
......@@ -344,7 +344,7 @@ static void parse_opts(struct diff_options *o, int argc, char *argv[])
static void diff_print_stats(git_diff *diff, struct diff_options *o)
{
git_diff_stats *stats;
git_buf b = GIT_BUF_INIT_CONST(NULL, 0);
git_buf b = GIT_BUF_INIT;
git_diff_stats_format_t format = 0;
check_lg2(
......
......@@ -11,8 +11,8 @@
#include "git2.h"
#include "buffer.h"
#include "common.h"
#include "str.h"
#include "futils.h"
#include "hash.h"
#include "commit_graph.h"
......@@ -33,7 +33,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
git_commit_graph_file file = {{0}};
git_commit_graph_entry e;
git_buf commit_graph_buf = GIT_BUF_INIT;
git_str commit_graph_buf = GIT_STR_INIT;
unsigned char hash[GIT_HASH_SHA1_SIZE];
git_oid oid = {{0}};
bool append_hash = false;
......@@ -51,7 +51,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
size -= 4;
if (append_hash) {
if (git_buf_init(&commit_graph_buf, size + GIT_HASH_SHA1_SIZE) < 0)
if (git_str_init(&commit_graph_buf, size + GIT_HASH_SHA1_SIZE) < 0)
goto cleanup;
if (git_hash_buf(hash, data, size, GIT_HASH_ALGORITHM_SHA1) < 0) {
fprintf(stderr, "Failed to compute the SHA1 hash\n");
......@@ -62,13 +62,13 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
memcpy(oid.id, hash, GIT_OID_RAWSZ);
} else {
git_buf_attach_notowned(&commit_graph_buf, (char *)data, size);
git_str_attach_notowned(&commit_graph_buf, (char *)data, size);
}
if (git_commit_graph_file_parse(
&file,
(const unsigned char *)git_buf_cstr(&commit_graph_buf),
git_buf_len(&commit_graph_buf))
(const unsigned char *)git_str_cstr(&commit_graph_buf),
git_str_len(&commit_graph_buf))
< 0)
goto cleanup;
......@@ -78,6 +78,6 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
cleanup:
git_commit_graph_file_close(&file);
git_buf_dispose(&commit_graph_buf);
git_str_dispose(&commit_graph_buf);
return 0;
}
......@@ -11,7 +11,6 @@
#include "git2.h"
#include "buffer.h"
#include "common.h"
#include "futils.h"
#include "hash.h"
......@@ -33,7 +32,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
git_midx_file idx = {{0}};
git_midx_entry e;
git_buf midx_buf = GIT_BUF_INIT;
git_str midx_buf = GIT_STR_INIT;
unsigned char hash[GIT_HASH_SHA1_SIZE];
git_oid oid = {{0}};
bool append_hash = false;
......@@ -51,7 +50,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
size -= 4;
if (append_hash) {
if (git_buf_init(&midx_buf, size + GIT_HASH_SHA1_SIZE) < 0)
if (git_str_init(&midx_buf, size + GIT_HASH_SHA1_SIZE) < 0)
goto cleanup;
if (git_hash_buf(hash, data, size, GIT_HASH_ALGORITHM_SHA1) < 0) {
fprintf(stderr, "Failed to compute the SHA1 hash\n");
......@@ -62,10 +61,10 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
memcpy(oid.id, hash, GIT_OID_RAWSZ);
} else {
git_buf_attach_notowned(&midx_buf, (char *)data, size);
git_str_attach_notowned(&midx_buf, (char *)data, size);
}
if (git_midx_parse(&idx, (const unsigned char *)git_buf_cstr(&midx_buf), git_buf_len(&midx_buf)) < 0)
if (git_midx_parse(&idx, (const unsigned char *)git_str_cstr(&midx_buf), git_str_len(&midx_buf)) < 0)
goto cleanup;
/* Search for any oid, just to exercise that codepath. */
......@@ -74,6 +73,6 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
cleanup:
git_midx_close(&idx);
git_buf_dispose(&midx_buf);
git_str_dispose(&midx_buf);
return 0;
}
......@@ -12,7 +12,7 @@
#include "git2.h"
#include "git2/sys/mempack.h"
#include "common.h"
#include "buffer.h"
#include "str.h"
static git_odb *odb = NULL;
static git_odb_backend *mempack = NULL;
......@@ -53,7 +53,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
git_indexer_progress stats = {0, 0};
git_indexer *indexer = NULL;
git_buf path = GIT_BUF_INIT;
git_str path = GIT_STR_INIT;
git_oid oid;
bool append_hash = false;
......@@ -99,19 +99,19 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
if (git_indexer_commit(indexer, &stats) < 0)
goto cleanup;
if (git_buf_printf(&path, "pack-%s.idx", git_oid_tostr_s(git_indexer_hash(indexer))) < 0)
if (git_str_printf(&path, "pack-%s.idx", git_oid_tostr_s(git_indexer_hash(indexer))) < 0)
goto cleanup;
p_unlink(git_buf_cstr(&path));
p_unlink(git_str_cstr(&path));
git_buf_clear(&path);
git_str_clear(&path);
if (git_buf_printf(&path, "pack-%s.pack", git_oid_tostr_s(git_indexer_hash(indexer))) < 0)
if (git_str_printf(&path, "pack-%s.pack", git_oid_tostr_s(git_indexer_hash(indexer))) < 0)
goto cleanup;
p_unlink(git_buf_cstr(&path));
p_unlink(git_str_cstr(&path));
cleanup:
git_mempack_reset(mempack);
git_indexer_free(indexer);
git_buf_dispose(&path);
git_str_dispose(&path);
return 0;
}
......@@ -16,7 +16,7 @@ extern int LLVMFuzzerInitialize(int *argc, char ***argv);
static int run_one_file(const char *filename)
{
git_buf buf = GIT_BUF_INIT;
git_str buf = GIT_STR_INIT;
int error = 0;
if (git_futils_readbuffer(&buf, filename) < 0) {
......@@ -27,7 +27,7 @@ static int run_one_file(const char *filename)
LLVMFuzzerTestOneInput((const unsigned char *)buf.ptr, buf.size);
exit:
git_buf_dispose(&buf);
git_str_dispose(&buf);
return error;
}
......
......@@ -23,110 +23,50 @@ GIT_BEGIN_DECL
*
* Sometimes libgit2 wants to return an allocated data buffer to the
* caller and have the caller take responsibility for freeing that memory.
* This can be awkward if the caller does not have easy access to the same
* allocation functions that libgit2 is using. In those cases, libgit2
* will fill in a `git_buf` and the caller can use `git_buf_dispose()` to
* release it when they are done.
* To make ownership clear in these cases, libgit2 uses `git_buf` to
* return this data. Callers should use `git_buf_dispose()` to release
* the memory when they are done.
*
* A `git_buf` may also be used for the caller to pass in a reference to
* a block of memory they hold. In this case, libgit2 will not resize or
* free the memory, but will read from it as needed.
*
* Some APIs may occasionally do something slightly unusual with a buffer,
* such as setting `ptr` to a value that was passed in by the user. In
* those cases, the behavior will be clearly documented by the API.
* A `git_buf` contains a pointer to a NUL-terminated C string, and
* the length of the string (not including the NUL terminator).
*/
typedef struct {
/**
* The buffer contents.
*
* `ptr` points to the start of the allocated memory. If it is NULL,
* then the `git_buf` is considered empty and libgit2 will feel free
* to overwrite it with new data.
* The buffer contents. `ptr` points to the start of the buffer
* being returned. The buffer's length (in bytes) is specified
* by the `size` member of the structure, and contains a NUL
* terminator at position `(size + 1)`.
*/
char *ptr;
char *ptr;
/**
* `asize` holds the known total amount of allocated memory if the `ptr`
* was allocated by libgit2. It may be larger than `size`. If `ptr`
* was not allocated by libgit2 and should not be resized and/or freed,
* then `asize` will be set to zero.
* This field is reserved and unused.
*/
size_t asize;
size_t reserved;
/**
* `size` holds the size (in bytes) of the data that is actually used.
* The length (in bytes) of the buffer pointed to by `ptr`,
* not including a NUL terminator.
*/
size_t size;
} git_buf;
/**
* Static initializer for git_buf from static buffer
* Use to initialize a `git_buf` before passing it to a function that
* will populate it.
*/
#define GIT_BUF_INIT_CONST(STR,LEN) { (char *)(STR), 0, (size_t)(LEN) }
#define GIT_BUF_INIT { NULL, 0, 0 }
/**
* Free the memory referred to by the git_buf.
*
* Note that this does not free the `git_buf` itself, just the memory
* pointed to by `buffer->ptr`. This will not free the memory if it looks
* like it was not allocated internally, but it will clear the buffer back
* to the empty state.
* pointed to by `buffer->ptr`.
*
* @param buffer The buffer to deallocate
*/
GIT_EXTERN(void) git_buf_dispose(git_buf *buffer);
/**
* Resize the buffer allocation to make more space.
*
* This will attempt to grow the buffer to accommodate the target size.
*
* If the buffer refers to memory that was not allocated by libgit2 (i.e.
* the `asize` field is zero), then `ptr` will be replaced with a newly
* allocated block of data. Be careful so that memory allocated by the
* caller is not lost. As a special variant, if you pass `target_size` as
* 0 and the memory is not allocated by libgit2, this will allocate a new
* buffer of size `size` and copy the external data into it.
*
* Currently, this will never shrink a buffer, only expand it.
*
* If the allocation fails, this will return an error and the buffer will be
* marked as invalid for future operations, invaliding the contents.
*
* @param buffer The buffer to be resized; may or may not be allocated yet
* @param target_size The desired available size
* @return 0 on success, -1 on allocation failure
*/
GIT_EXTERN(int) git_buf_grow(git_buf *buffer, size_t target_size);
/**
* Set buffer to a copy of some raw data.
*
* @param buffer The buffer to set
* @param data The data to copy into the buffer
* @param datalen The length of the data to copy into the buffer
* @return 0 on success, -1 on allocation failure
*/
GIT_EXTERN(int) git_buf_set(
git_buf *buffer, const void *data, size_t datalen);
/**
* Check quickly if buffer looks like it contains binary data
*
* @param buf Buffer to check
* @return 1 if buffer looks like non-text data
*/
GIT_EXTERN(int) git_buf_is_binary(const git_buf *buf);
/**
* Check quickly if buffer contains a NUL byte
*
* @param buf Buffer to check
* @return 1 if buffer contains a NUL byte
*/
GIT_EXTERN(int) git_buf_contains_nul(const git_buf *buf);
GIT_END_DECL
/** @} */
......
......@@ -265,7 +265,7 @@ done:
}
static int apply_hunks(
git_buf *out,
git_str *out,
const char *source,
size_t source_len,
git_patch *patch,
......@@ -286,7 +286,7 @@ static int apply_hunks(
}
git_vector_foreach(&image.lines, i, line)
git_buf_put(out, line->content, line->content_len);
git_str_put(out, line->content, line->content_len);
done:
patch_image_free(&image);
......@@ -295,24 +295,24 @@ done:
}
static int apply_binary_delta(
git_buf *out,
git_str *out,
const char *source,
size_t source_len,
git_diff_binary_file *binary_file)
{
git_buf inflated = GIT_BUF_INIT;
git_str inflated = GIT_STR_INIT;
int error = 0;
/* no diff means identical contents */
if (binary_file->datalen == 0)
return git_buf_put(out, source, source_len);
return git_str_put(out, source, source_len);
error = git_zstream_inflatebuf(&inflated,
binary_file->data, binary_file->datalen);
if (!error && inflated.size != binary_file->inflatedlen) {
error = apply_err("inflated delta does not match expected length");
git_buf_dispose(out);
git_str_dispose(out);
}
if (error < 0)
......@@ -330,7 +330,7 @@ static int apply_binary_delta(
out->asize = data_len;
}
else if (binary_file->type == GIT_DIFF_BINARY_LITERAL) {
git_buf_swap(out, &inflated);
git_str_swap(out, &inflated);
}
else {
error = apply_err("unknown binary delta type");
......@@ -338,17 +338,17 @@ static int apply_binary_delta(
}
done:
git_buf_dispose(&inflated);
git_str_dispose(&inflated);
return error;
}
static int apply_binary(
git_buf *out,
git_str *out,
const char *source,
size_t source_len,
git_patch *patch)
{
git_buf reverse = GIT_BUF_INIT;
git_str reverse = GIT_STR_INIT;
int error = 0;
if (!patch->binary.contains_data) {
......@@ -378,14 +378,14 @@ static int apply_binary(
done:
if (error < 0)
git_buf_dispose(out);
git_str_dispose(out);
git_buf_dispose(&reverse);
git_str_dispose(&reverse);
return error;
}
int git_apply__patch(
git_buf *contents_out,
git_str *contents_out,
char **filename_out,
unsigned int *mode_out,
const char *source,
......@@ -423,13 +423,13 @@ int git_apply__patch(
else if (patch->hunks.size)
error = apply_hunks(contents_out, source, source_len, patch, &ctx);
else
error = git_buf_put(contents_out, source, source_len);
error = git_str_put(contents_out, source, source_len);
if (error)
goto done;
if (patch->delta->status == GIT_DELTA_DELETED &&
git_buf_len(contents_out) > 0) {
git_str_len(contents_out) > 0) {
error = apply_err("removal patch leaves file contents");
goto done;
}
......@@ -456,7 +456,7 @@ static int apply_one(
const git_apply_options *opts)
{
git_patch *patch = NULL;
git_buf pre_contents = GIT_BUF_INIT, post_contents = GIT_BUF_INIT;
git_str pre_contents = GIT_STR_INIT, post_contents = GIT_STR_INIT;
const git_diff_delta *delta;
char *filename = NULL;
unsigned int mode;
......@@ -579,8 +579,8 @@ static int apply_one(
git_strmap_delete(removed_paths, delta->new_file.path);
done:
git_buf_dispose(&pre_contents);
git_buf_dispose(&post_contents);
git_str_dispose(&pre_contents);
git_str_dispose(&post_contents);
git__free(filename);
git_patch_free(patch);
......
......@@ -11,10 +11,10 @@
#include "git2/patch.h"
#include "git2/apply.h"
#include "buffer.h"
#include "str.h"
extern int git_apply__patch(
git_buf *out,
git_str *out,
char **filename,
unsigned int *mode,
const char *source,
......
......@@ -338,7 +338,7 @@ GIT_INLINE(int) preload_attr_file(
}
static int system_attr_file(
git_buf *out,
git_str *out,
git_attr_session *attr_session)
{
int error;
......@@ -366,11 +366,11 @@ static int system_attr_file(
if (attr_session->sysdir.size == 0)
return GIT_ENOTFOUND;
/* We can safely provide a git_buf with no allocation (asize == 0) to
* a consumer. This allows them to treat this as a regular `git_buf`,
* but their call to `git_buf_dispose` will not attempt to free it.
/* We can safely provide a git_str with no allocation (asize == 0) to
* a consumer. This allows them to treat this as a regular `git_str`,
* but their call to `git_str_dispose` will not attempt to free it.
*/
git_buf_attach_notowned(
git_str_attach_notowned(
out, attr_session->sysdir.ptr, attr_session->sysdir.size);
return 0;
}
......@@ -380,7 +380,7 @@ static int attr_setup(
git_attr_session *attr_session,
git_attr_options *opts)
{
git_buf system = GIT_BUF_INIT, info = GIT_BUF_INIT;
git_str system = GIT_STR_INIT, info = GIT_STR_INIT;
git_attr_file_source index_source = { GIT_ATTR_FILE_SOURCE_INDEX, NULL, GIT_ATTR_FILE, NULL };
git_attr_file_source head_source = { GIT_ATTR_FILE_SOURCE_HEAD, NULL, GIT_ATTR_FILE, NULL };
git_attr_file_source commit_source = { GIT_ATTR_FILE_SOURCE_COMMIT, NULL, GIT_ATTR_FILE, NULL };
......@@ -411,7 +411,7 @@ static int attr_setup(
git_repository_attr_cache(repo)->cfg_attr_file)) < 0)
goto out;
if ((error = git_repository_item_path(&info, repo, GIT_REPOSITORY_ITEM_INFO)) < 0 ||
if ((error = git_repository__item_path(&info, repo, GIT_REPOSITORY_ITEM_INFO)) < 0 ||
(error = preload_attr_file(repo, attr_session, info.ptr, GIT_ATTR_FILE_INREPO)) < 0) {
if (error != GIT_ENOTFOUND)
goto out;
......@@ -447,8 +447,8 @@ static int attr_setup(
attr_session->init_setup = 1;
out:
git_buf_dispose(&system);
git_buf_dispose(&info);
git_str_dispose(&system);
git_str_dispose(&info);
return error;
}
......@@ -625,7 +625,7 @@ static int collect_attr_files(
git_vector *files)
{
int error = 0;
git_buf dir = GIT_BUF_INIT, attrfile = GIT_BUF_INIT;
git_str dir = GIT_STR_INIT, attrfile = GIT_STR_INIT;
const char *workdir = git_repository_workdir(repo);
attr_walk_up_info info = { NULL };
......@@ -653,7 +653,7 @@ static int collect_attr_files(
* - $GIT_PREFIX/etc/gitattributes
*/
if ((error = git_repository_item_path(&attrfile, repo, GIT_REPOSITORY_ITEM_INFO)) < 0 ||
if ((error = git_repository__item_path(&attrfile, repo, GIT_REPOSITORY_ITEM_INFO)) < 0 ||
(error = push_attr_file(repo, attr_session, files, attrfile.ptr, GIT_ATTR_FILE_INREPO)) < 0) {
if (error != GIT_ENOTFOUND)
goto cleanup;
......@@ -693,8 +693,8 @@ static int collect_attr_files(
cleanup:
if (error < 0)
release_attr_files(files);
git_buf_dispose(&attrfile);
git_buf_dispose(&dir);
git_str_dispose(&attrfile);
git_str_dispose(&dir);
return error;
}
......@@ -117,13 +117,13 @@ int git_attr_file__load(
git_tree *tree = NULL;
git_tree_entry *tree_entry = NULL;
git_blob *blob = NULL;
git_buf content = GIT_BUF_INIT;
git_str content = GIT_STR_INIT;
const char *content_str;
git_attr_file *file;
struct stat st;
bool nonexistent = false;
int bom_offset;
git_buf_bom_t bom;
git_str_bom_t bom;
git_oid id;
git_object_size_t blobsize;
......@@ -143,7 +143,7 @@ int git_attr_file__load(
blobsize = git_blob_rawsize(blob);
GIT_ERROR_CHECK_BLOBSIZE(blobsize);
git_buf_put(&content, git_blob_rawcontent(blob), (size_t)blobsize);
git_str_put(&content, git_blob_rawcontent(blob), (size_t)blobsize);
break;
}
case GIT_ATTR_FILE_SOURCE_FILE: {
......@@ -198,7 +198,7 @@ int git_attr_file__load(
blobsize = git_blob_rawsize(blob);
GIT_ERROR_CHECK_BLOBSIZE(blobsize);
if ((error = git_buf_put(&content,
if ((error = git_str_put(&content,
git_blob_rawcontent(blob), (size_t)blobsize)) < 0)
goto cleanup;
......@@ -213,10 +213,10 @@ int git_attr_file__load(
goto cleanup;
/* advance over a UTF8 BOM */
content_str = git_buf_cstr(&content);
bom_offset = git_buf_detect_bom(&bom, &content);
content_str = git_str_cstr(&content);
bom_offset = git_str_detect_bom(&bom, &content);
if (bom == GIT_BUF_BOM_UTF8)
if (bom == GIT_STR_BOM_UTF8)
content_str += bom_offset;
/* store the key of the attr_reader; don't bother with cache
......@@ -250,7 +250,7 @@ cleanup:
git_tree_entry_free(tree_entry);
git_tree_free(tree);
git_commit_free(commit);
git_buf_dispose(&content);
git_str_dispose(&content);
return error;
}
......@@ -435,7 +435,7 @@ int git_attr_file__lookup_one(
int git_attr_file__load_standalone(git_attr_file **out, const char *path)
{
git_buf content = GIT_BUF_INIT;
git_str content = GIT_STR_INIT;
git_attr_file_source source = { GIT_ATTR_FILE_SOURCE_FILE };
git_attr_file *file = NULL;
int error;
......@@ -457,7 +457,7 @@ int git_attr_file__load_standalone(git_attr_file **out, const char *path)
out:
if (error < 0)
git_attr_file__free(file);
git_buf_dispose(&content);
git_str_dispose(&content);
return error;
}
......@@ -558,7 +558,7 @@ int git_attr_path__init(
ssize_t root;
/* build full path as best we can */
git_buf_init(&info->full, 0);
git_str_init(&info->full, 0);
if (git_path_join_unrooted(&info->full, path, base, &root) < 0)
return -1;
......@@ -605,7 +605,7 @@ int git_attr_path__init(
void git_attr_path__free(git_attr_path *info)
{
git_buf_dispose(&info->full);
git_str_dispose(&info->full);
info->path = NULL;
info->basename = NULL;
}
......@@ -1020,8 +1020,8 @@ void git_attr_session__free(git_attr_session *session)
if (!session)
return;
git_buf_dispose(&session->sysdir);
git_buf_dispose(&session->tmp);
git_str_dispose(&session->sysdir);
git_str_dispose(&session->tmp);
memset(session, 0, sizeof(git_attr_session));
}
......@@ -13,7 +13,7 @@
#include "git2/attr.h"
#include "vector.h"
#include "pool.h"
#include "buffer.h"
#include "str.h"
#include "futils.h"
#define GIT_ATTR_FILE ".gitattributes"
......@@ -118,7 +118,7 @@ struct git_attr_file_entry {
};
typedef struct {
git_buf full;
git_str full;
char *path;
char *basename;
int is_dir;
......@@ -132,8 +132,8 @@ typedef struct {
int key;
unsigned int init_setup:1,
init_sysdir:1;
git_buf sysdir;
git_buf tmp;
git_str sysdir;
git_str tmp;
} git_attr_session;
extern int git_attr_session__init(git_attr_session *attr_session, git_repository *repo);
......
......@@ -161,7 +161,7 @@ static int attr_cache_lookup(
git_attr_file_source *source)
{
int error = 0;
git_buf path = GIT_BUF_INIT;
git_str path = GIT_STR_INIT;
const char *wd = git_repository_workdir(repo);
const char *filename;
git_attr_cache *cache = git_repository_attr_cache(repo);
......@@ -170,9 +170,9 @@ static int attr_cache_lookup(
/* join base and path as needed */
if (source->base != NULL && git_path_root(source->filename) < 0) {
git_buf *p = attr_session ? &attr_session->tmp : &path;
git_str *p = attr_session ? &attr_session->tmp : &path;
if (git_buf_joinpath(p, source->base, source->filename) < 0 ||
if (git_str_joinpath(p, source->base, source->filename) < 0 ||
git_path_validate_workdir_buf(repo, p) < 0)
return -1;
......@@ -203,7 +203,7 @@ cleanup:
*out_file = file;
*out_entry = entry;
git_buf_dispose(&path);
git_str_dispose(&path);
return error;
}
......@@ -281,7 +281,7 @@ bool git_attr_cache__is_cached(
static int attr_cache__lookup_path(
char **out, git_config *cfg, const char *key, const char *fallback)
{
git_buf buf = GIT_BUF_INIT;
git_str buf = GIT_STR_INIT;
int error;
git_config_entry *entry = NULL;
......@@ -296,17 +296,17 @@ static int attr_cache__lookup_path(
/* expand leading ~/ as needed */
if (cfgval && cfgval[0] == '~' && cfgval[1] == '/') {
if (! (error = git_sysdir_expand_global_file(&buf, &cfgval[2])))
*out = git_buf_detach(&buf);
*out = git_str_detach(&buf);
} else if (cfgval) {
*out = git__strdup(cfgval);
}
}
else if (!git_sysdir_find_xdg_file(&buf, fallback)) {
*out = git_buf_detach(&buf);
*out = git_str_detach(&buf);
}
git_config_entry_free(entry);
git_buf_dispose(&buf);
git_str_dispose(&buf);
return error;
}
......
......@@ -12,6 +12,7 @@
#include "git2/repository.h"
#include "git2/odb_backend.h"
#include "buf.h"
#include "filebuf.h"
#include "filter.h"
......@@ -35,12 +36,12 @@ git_object_size_t git_blob_rawsize(const git_blob *blob)
return (git_object_size_t)git_odb_object_size(blob->data.odb);
}
int git_blob__getbuf(git_buf *buffer, git_blob *blob)
int git_blob__getbuf(git_str *buffer, git_blob *blob)
{
git_object_size_t size = git_blob_rawsize(blob);
GIT_ERROR_CHECK_BLOBSIZE(size);
return git_buf_set(buffer, git_blob_rawcontent(blob), (size_t)size);
return git_str_set(buffer, git_blob_rawcontent(blob), (size_t)size);
}
void git_blob__free(void *_blob)
......@@ -142,9 +143,9 @@ static int write_file_filtered(
git_repository* repo)
{
int error;
git_buf tgt = GIT_BUF_INIT;
git_str tgt = GIT_STR_INIT;
error = git_filter_list_apply_to_file(&tgt, fl, repo, full_path);
error = git_filter_list__apply_to_file(&tgt, fl, repo, full_path);
/* Write the file to disk if it was properly filtered */
if (!error) {
......@@ -153,7 +154,7 @@ static int write_file_filtered(
error = git_odb_write(id, odb, tgt.ptr, tgt.size, GIT_OBJECT_BLOB);
}
git_buf_dispose(&tgt);
git_str_dispose(&tgt);
return error;
}
......@@ -193,7 +194,7 @@ int git_blob__create_from_paths(
git_odb *odb = NULL;
git_object_size_t size;
mode_t mode;
git_buf path = GIT_BUF_INIT;
git_str path = GIT_STR_INIT;
GIT_ASSERT_ARG(hint_path || !try_load_filters);
......@@ -261,7 +262,7 @@ int git_blob__create_from_paths(
done:
git_odb_free(odb);
git_buf_dispose(&path);
git_str_dispose(&path);
return error;
}
......@@ -276,11 +277,11 @@ int git_blob_create_from_disk(
git_oid *id, git_repository *repo, const char *path)
{
int error;
git_buf full_path = GIT_BUF_INIT;
git_str full_path = GIT_STR_INIT;
const char *workdir, *hintpath = NULL;
if ((error = git_path_prettify(&full_path, path, NULL)) < 0) {
git_buf_dispose(&full_path);
git_str_dispose(&full_path);
return error;
}
......@@ -290,9 +291,9 @@ int git_blob_create_from_disk(
hintpath = full_path.ptr + strlen(workdir);
error = git_blob__create_from_paths(
id, NULL, repo, git_buf_cstr(&full_path), hintpath, 0, !!hintpath);
id, NULL, repo, git_str_cstr(&full_path), hintpath, 0, !!hintpath);
git_buf_dispose(&full_path);
git_str_dispose(&full_path);
return error;
}
......@@ -330,7 +331,7 @@ static int blob_writestream_write(git_writestream *_stream, const char *buffer,
int git_blob_create_from_stream(git_writestream **out, git_repository *repo, const char *hintpath)
{
int error;
git_buf path = GIT_BUF_INIT;
git_str path = GIT_STR_INIT;
blob_writestream *stream;
GIT_ASSERT_ARG(out);
......@@ -349,11 +350,11 @@ int git_blob_create_from_stream(git_writestream **out, git_repository *repo, con
stream->parent.close = blob_writestream_close;
stream->parent.free = blob_writestream_free;
if ((error = git_repository_item_path(&path, repo, GIT_REPOSITORY_ITEM_OBJECTS)) < 0
|| (error = git_buf_joinpath(&path, path.ptr, "streamed")) < 0)
if ((error = git_repository__item_path(&path, repo, GIT_REPOSITORY_ITEM_OBJECTS)) < 0
|| (error = git_str_joinpath(&path, path.ptr, "streamed")) < 0)
goto cleanup;
if ((error = git_filebuf_open_withsize(&stream->fbuf, git_buf_cstr(&path), GIT_FILEBUF_TEMPORARY,
if ((error = git_filebuf_open_withsize(&stream->fbuf, git_str_cstr(&path), GIT_FILEBUF_TEMPORARY,
0666, 2 * 1024 * 1024)) < 0)
goto cleanup;
......@@ -363,7 +364,7 @@ cleanup:
if (error < 0)
blob_writestream_free((git_writestream *) stream);
git_buf_dispose(&path);
git_str_dispose(&path);
return error;
}
......@@ -391,16 +392,16 @@ cleanup:
int git_blob_is_binary(const git_blob *blob)
{
git_buf content = GIT_BUF_INIT;
git_str content = GIT_STR_INIT;
git_object_size_t size;
GIT_ASSERT_ARG(blob);
size = git_blob_rawsize(blob);
git_buf_attach_notowned(&content, git_blob_rawcontent(blob),
git_str_attach_notowned(&content, git_blob_rawcontent(blob),
(size_t)min(size, GIT_FILTER_BYTES_TO_CHECK_NUL));
return git_buf_is_binary(&content);
return git_str_is_binary(&content);
}
int git_blob_filter_options_init(
......@@ -418,10 +419,10 @@ int git_blob_filter(
const char *path,
git_blob_filter_options *given_opts)
{
int error = 0;
git_filter_list *fl = NULL;
git_blob_filter_options opts = GIT_BLOB_FILTER_OPTIONS_INIT;
git_filter_options filter_opts = GIT_FILTER_OPTIONS_INIT;
git_filter_list *fl = NULL;
int error = 0;
GIT_ASSERT_ARG(blob);
GIT_ASSERT_ARG(path);
......@@ -430,9 +431,6 @@ int git_blob_filter(
GIT_ERROR_CHECK_VERSION(
given_opts, GIT_BLOB_FILTER_OPTIONS_VERSION, "git_blob_filter_options");
if (git_buf_sanitize(out) < 0)
return -1;
if (given_opts != NULL)
memcpy(&opts, given_opts, sizeof(git_blob_filter_options));
......
......@@ -38,7 +38,7 @@ struct git_blob {
void git_blob__free(void *blob);
int git_blob__parse(void *blob, git_odb_object *obj);
int git_blob__parse_raw(void *blob, const char *data, size_t size);
int git_blob__getbuf(git_buf *buffer, git_blob *blob);
int git_blob__getbuf(git_str *buffer, git_blob *blob);
extern int git_blob__create_from_paths(
git_oid *out_oid,
......
......@@ -9,10 +9,22 @@
#include "common.h"
#include "buffer.h"
#include "str.h"
int git_branch_upstream__name(
git_buf *tracking_name,
int git_branch__remote_name(
git_str *out,
git_repository *repo,
const char *refname);
int git_branch__upstream_remote(
git_str *out,
git_repository *repo,
const char *refname);
int git_branch__upstream_merge(
git_str *out,
git_repository *repo,
const char *refname);
int git_branch__upstream_name(
git_str *tracking_name,
git_repository *repo,
const char *canonical_branch_name);
......
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "buf.h"
#include "common.h"
int git_buf_sanitize(git_buf *buf)
{
GIT_ASSERT_ARG(buf);
if (buf->reserved > 0)
buf->ptr[0] = '\0';
else
buf->ptr = git_str__initstr;
buf->size = 0;
return 0;
}
int git_buf_tostr(git_str *out, git_buf *buf)
{
GIT_ASSERT_ARG(out);
GIT_ASSERT_ARG(buf);
if (git_buf_sanitize(buf) < 0)
return -1;
out->ptr = buf->ptr;
out->asize = buf->reserved;
out->size = buf->size;
buf->ptr = git_str__initstr;
buf->reserved = 0;
buf->size = 0;
return 0;
}
int git_buf_fromstr(git_buf *out, git_str *str)
{
GIT_ASSERT_ARG(out);
GIT_ASSERT_ARG(str);
out->ptr = str->ptr;
out->reserved = str->asize;
out->size = str->size;
str->ptr = git_str__initstr;
str->asize = 0;
str->size = 0;
return 0;
}
void git_buf_dispose(git_buf *buf)
{
if (!buf)
return;
if (buf->ptr != git_str__initstr)
git__free(buf->ptr);
buf->ptr = git_str__initstr;
buf->reserved = 0;
buf->size = 0;
}
#ifndef GIT_DEPRECATE_HARD
int git_buf_grow(git_buf *buffer, size_t target_size)
{
char *newptr;
if (buffer->reserved >= target_size)
return 0;
if (buffer->ptr == git_str__initstr)
newptr = git__malloc(target_size);
else
newptr = git__realloc(buffer->ptr, target_size);
if (!newptr)
return -1;
buffer->ptr = newptr;
buffer->reserved = target_size;
return 0;
}
int git_buf_set(git_buf *buffer, const void *data, size_t datalen)
{
size_t alloclen;
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, datalen, 1);
if (git_buf_grow(buffer, alloclen) < 0)
return -1;
memmove(buffer->ptr, data, datalen);
buffer->size = datalen;
buffer->ptr[buffer->size] = '\0';
return 0;
}
int git_buf_is_binary(const git_buf *buf)
{
git_str str = GIT_STR_INIT_CONST(buf->ptr, buf->size);
return git_str_is_binary(&str);
}
int git_buf_contains_nul(const git_buf *buf)
{
git_str str = GIT_STR_INIT_CONST(buf->ptr, buf->size);
return git_str_contains_nul(&str);
}
void git_buf_free(git_buf *buffer)
{
git_buf_dispose(buffer);
}
#endif
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#ifndef INCLUDE_buf_h__
#define INCLUDE_buf_h__
#include "git2/buffer.h"
#include "common.h"
/*
* Adapts a private API that takes a `git_str` into a public API that
* takes a `git_buf`.
*/
#define GIT_BUF_WRAP_PRIVATE(buf, fn, ...) \
{ \
git_str str = GIT_STR_INIT; \
int error; \
if ((error = git_buf_tostr(&str, buf)) == 0 && \
(error = fn(&str, __VA_ARGS__)) == 0) \
error = git_buf_fromstr(buf, &str); \
git_str_dispose(&str); \
return error; \
}
/**
* "Sanitizes" a buffer from user input. This simply ensures that the
* `git_buf` has nice defaults if the user didn't set the members to
* anything, so that if we return early we don't leave it populated
* with nonsense.
*/
extern int git_buf_sanitize(git_buf *from_user);
/**
* Populate a `git_str` from a `git_buf` for passing to libgit2 internal
* functions. Sanitizes the given `git_buf` before proceeding. The
* `git_buf` will no longer point to this memory.
*/
extern int git_buf_tostr(git_str *out, git_buf *buf);
/**
* Populate a `git_buf` from a `git_str` for returning to a user.
* The `git_str` will no longer point to this memory.
*/
extern int git_buf_fromstr(git_buf *out, git_str *str);
#endif
......@@ -61,9 +61,9 @@ typedef struct {
git_vector update_conflicts;
git_vector *update_reuc;
git_vector *update_names;
git_buf target_path;
git_str target_path;
size_t target_len;
git_buf tmp;
git_str tmp;
unsigned int strategy;
int can_symlink;
int respect_filemode;
......@@ -321,11 +321,11 @@ static int checkout_action_no_wd(
}
static int checkout_target_fullpath(
git_buf **out, checkout_data *data, const char *path)
git_str **out, checkout_data *data, const char *path)
{
git_buf_truncate(&data->target_path, data->target_len);
git_str_truncate(&data->target_path, data->target_len);
if (path && git_buf_puts(&data->target_path, path) < 0)
if (path && git_str_puts(&data->target_path, path) < 0)
return -1;
if (git_path_validate_workdir_buf(data->repo, &data->target_path) < 0)
......@@ -339,7 +339,7 @@ static int checkout_target_fullpath(
static bool wd_item_is_removable(
checkout_data *data, const git_index_entry *wd)
{
git_buf *full;
git_str *full;
if (wd->mode != GIT_FILEMODE_TREE)
return true;
......@@ -423,7 +423,7 @@ static int checkout_action_wd_only(
/* copy the entry for issuing notification callback later */
git_index_entry saved_wd = *wd;
git_buf_sets(&data->tmp, wd->path);
git_str_sets(&data->tmp, wd->path);
saved_wd.path = data->tmp.ptr;
error = git_iterator_advance_over(
......@@ -476,7 +476,7 @@ static bool submodule_is_config_only(
static bool checkout_is_empty_dir(checkout_data *data, const char *path)
{
git_buf *fullpath;
git_str *fullpath;
if (checkout_target_fullpath(&fullpath, data, path) < 0)
return false;
......@@ -1584,7 +1584,7 @@ static int blob_content_to_link(
git_blob *blob,
const char *path)
{
git_buf linktarget = GIT_BUF_INIT;
git_str linktarget = GIT_STR_INIT;
int error;
if ((error = mkpath2file(data, path, data->opts.dir_mode)) < 0)
......@@ -1594,10 +1594,10 @@ static int blob_content_to_link(
return error;
if (data->can_symlink) {
if ((error = p_symlink(git_buf_cstr(&linktarget), path)) < 0)
if ((error = p_symlink(git_str_cstr(&linktarget), path)) < 0)
git_error_set(GIT_ERROR_OS, "could not create symlink %s", path);
} else {
error = git_futils_fake_symlink(git_buf_cstr(&linktarget), path);
error = git_futils_fake_symlink(git_str_cstr(&linktarget), path);
}
if (!error) {
......@@ -1609,7 +1609,7 @@ static int blob_content_to_link(
st->st_mode = GIT_FILEMODE_LINK;
}
git_buf_dispose(&linktarget);
git_str_dispose(&linktarget);
return error;
}
......@@ -1636,7 +1636,7 @@ static int checkout_submodule_update_index(
checkout_data *data,
const git_diff_file *file)
{
git_buf *fullpath;
git_str *fullpath;
struct stat st;
/* update the index unless prevented */
......@@ -1772,7 +1772,7 @@ static int checkout_blob(
checkout_data *data,
const git_diff_file *file)
{
git_buf *fullpath;
git_str *fullpath;
struct stat st;
int error = 0;
......@@ -1809,7 +1809,7 @@ static int checkout_remove_the_old(
git_diff_delta *delta;
const char *str;
size_t i;
git_buf *fullpath;
git_str *fullpath;
uint32_t flg = GIT_RMDIR_EMPTY_PARENTS |
GIT_RMDIR_REMOVE_FILES | GIT_RMDIR_REMOVE_BLOCKERS;
......@@ -1927,40 +1927,40 @@ static int checkout_lookup_head_tree(git_tree **out, git_repository *repo)
static int conflict_entry_name(
git_buf *out,
git_str *out,
const char *side_name,
const char *filename)
{
if (git_buf_puts(out, side_name) < 0 ||
git_buf_putc(out, ':') < 0 ||
git_buf_puts(out, filename) < 0)
if (git_str_puts(out, side_name) < 0 ||
git_str_putc(out, ':') < 0 ||
git_str_puts(out, filename) < 0)
return -1;
return 0;
}
static int checkout_path_suffixed(git_buf *path, const char *suffix)
static int checkout_path_suffixed(git_str *path, const char *suffix)
{
size_t path_len;
int i = 0, error = 0;
if ((error = git_buf_putc(path, '~')) < 0 || (error = git_buf_puts(path, suffix)) < 0)
if ((error = git_str_putc(path, '~')) < 0 || (error = git_str_puts(path, suffix)) < 0)
return -1;
path_len = git_buf_len(path);
path_len = git_str_len(path);
while (git_path_exists(git_buf_cstr(path)) && i < INT_MAX) {
git_buf_truncate(path, path_len);
while (git_path_exists(git_str_cstr(path)) && i < INT_MAX) {
git_str_truncate(path, path_len);
if ((error = git_buf_putc(path, '_')) < 0 ||
(error = git_buf_printf(path, "%d", i)) < 0)
if ((error = git_str_putc(path, '_')) < 0 ||
(error = git_str_printf(path, "%d", i)) < 0)
return error;
i++;
}
if (i == INT_MAX) {
git_buf_truncate(path, path_len);
git_str_truncate(path, path_len);
git_error_set(GIT_ERROR_CHECKOUT, "could not write '%s': working directory file exists", path->ptr);
return GIT_EEXISTS;
......@@ -1974,8 +1974,8 @@ static int checkout_write_entry(
checkout_conflictdata *conflict,
const git_index_entry *side)
{
const char *hint_path, *suffix;
git_buf *fullpath;
const char *hint_path = NULL, *suffix;
git_str *fullpath;
struct stat st;
int error;
......@@ -2025,7 +2025,7 @@ static int checkout_write_entries(
}
static int checkout_merge_path(
git_buf *out,
git_str *out,
checkout_data *data,
checkout_conflictdata *conflict,
git_merge_file_result *result)
......@@ -2033,7 +2033,7 @@ static int checkout_merge_path(
const char *our_label_raw, *their_label_raw, *suffix;
int error = 0;
if ((error = git_buf_joinpath(out, data->opts.target_directory, result->path)) < 0 ||
if ((error = git_str_joinpath(out, data->opts.target_directory, result->path)) < 0 ||
(error = git_path_validate_workdir_buf(data->repo, out)) < 0)
return error;
......@@ -2056,9 +2056,9 @@ static int checkout_write_merge(
checkout_data *data,
checkout_conflictdata *conflict)
{
git_buf our_label = GIT_BUF_INIT, their_label = GIT_BUF_INIT,
path_suffixed = GIT_BUF_INIT, path_workdir = GIT_BUF_INIT,
in_data = GIT_BUF_INIT, out_data = GIT_BUF_INIT;
git_str our_label = GIT_STR_INIT, their_label = GIT_STR_INIT,
path_suffixed = GIT_STR_INIT, path_workdir = GIT_STR_INIT,
in_data = GIT_STR_INIT, out_data = GIT_STR_INIT;
git_merge_file_options opts = GIT_MERGE_FILE_OPTIONS_INIT;
git_merge_file_result result = {0};
git_filebuf output = GIT_FILEBUF_INIT;
......@@ -2088,8 +2088,8 @@ static int checkout_write_merge(
&their_label, opts.their_label, conflict->theirs->path)) < 0)
goto done;
opts.our_label = git_buf_cstr(&our_label);
opts.their_label = git_buf_cstr(&their_label);
opts.our_label = git_str_cstr(&our_label);
opts.their_label = git_str_cstr(&their_label);
}
if ((error = git_merge_file_from_index(&result, data->repo,
......@@ -2106,7 +2106,7 @@ static int checkout_write_merge(
goto done;
if ((data->strategy & GIT_CHECKOUT_UPDATE_ONLY) != 0 &&
(error = checkout_safe_for_update_only(data, git_buf_cstr(&path_workdir), result.mode)) <= 0)
(error = checkout_safe_for_update_only(data, git_str_cstr(&path_workdir), result.mode)) <= 0)
goto done;
if (!data->opts.disable_filters) {
......@@ -2127,7 +2127,7 @@ static int checkout_write_merge(
}
if ((error = mkpath2file(data, path_workdir.ptr, data->opts.dir_mode)) < 0 ||
(error = git_filebuf_open(&output, git_buf_cstr(&path_workdir), GIT_FILEBUF_DO_NOT_BUFFER, result.mode)) < 0 ||
(error = git_filebuf_open(&output, git_str_cstr(&path_workdir), GIT_FILEBUF_DO_NOT_BUFFER, result.mode)) < 0 ||
(error = git_filebuf_write(&output, out_data.ptr, out_data.size)) < 0 ||
(error = git_filebuf_commit(&output)) < 0)
goto done;
......@@ -2135,13 +2135,13 @@ static int checkout_write_merge(
done:
git_filter_list_free(fl);
git_buf_dispose(&out_data);
git_buf_dispose(&our_label);
git_buf_dispose(&their_label);
git_str_dispose(&out_data);
git_str_dispose(&our_label);
git_str_dispose(&their_label);
git_merge_file_result_free(&result);
git_buf_dispose(&path_workdir);
git_buf_dispose(&path_suffixed);
git_str_dispose(&path_workdir);
git_str_dispose(&path_suffixed);
return error;
}
......@@ -2321,8 +2321,8 @@ static void checkout_data_clear(checkout_data *data)
git__free(data->pfx);
data->pfx = NULL;
git_buf_dispose(&data->target_path);
git_buf_dispose(&data->tmp);
git_str_dispose(&data->target_path);
git_str_dispose(&data->tmp);
git_index_free(data->index);
data->index = NULL;
......@@ -2506,12 +2506,12 @@ static int checkout_data_init(
(error = git_vector_init(&data->removes, 0, git__strcmp_cb)) < 0 ||
(error = git_vector_init(&data->remove_conflicts, 0, NULL)) < 0 ||
(error = git_vector_init(&data->update_conflicts, 0, NULL)) < 0 ||
(error = git_buf_puts(&data->target_path, data->opts.target_directory)) < 0 ||
(error = git_str_puts(&data->target_path, data->opts.target_directory)) < 0 ||
(error = git_path_to_dir(&data->target_path)) < 0 ||
(error = git_strmap_new(&data->mkdir_map)) < 0)
goto cleanup;
data->target_len = git_buf_len(&data->target_path);
data->target_len = git_str_len(&data->target_path);
git_attr_session__init(&data->attr_session, data->repo);
......@@ -2623,7 +2623,7 @@ int git_checkout_iterator(
if (data.strategy & GIT_CHECKOUT_DRY_RUN)
goto cleanup;
data.total_steps = counts[CHECKOUT_ACTION__REMOVE] +
counts[CHECKOUT_ACTION__REMOVE_CONFLICT] +
counts[CHECKOUT_ACTION__UPDATE_BLOB] +
......
......@@ -26,10 +26,10 @@ static int write_cherrypick_head(
const char *commit_oidstr)
{
git_filebuf file = GIT_FILEBUF_INIT;
git_buf file_path = GIT_BUF_INIT;
git_str file_path = GIT_STR_INIT;
int error = 0;
if ((error = git_buf_joinpath(&file_path, repo->gitdir, GIT_CHERRYPICK_HEAD_FILE)) >= 0 &&
if ((error = git_str_joinpath(&file_path, repo->gitdir, GIT_CHERRYPICK_HEAD_FILE)) >= 0 &&
(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_CREATE_LEADING_DIRS, GIT_CHERRYPICK_FILE_MODE)) >= 0 &&
(error = git_filebuf_printf(&file, "%s\n", commit_oidstr)) >= 0)
error = git_filebuf_commit(&file);
......@@ -37,7 +37,7 @@ static int write_cherrypick_head(
if (error < 0)
git_filebuf_cleanup(&file);
git_buf_dispose(&file_path);
git_str_dispose(&file_path);
return error;
}
......@@ -47,10 +47,10 @@ static int write_merge_msg(
const char *commit_msg)
{
git_filebuf file = GIT_FILEBUF_INIT;
git_buf file_path = GIT_BUF_INIT;
git_str file_path = GIT_STR_INIT;
int error = 0;
if ((error = git_buf_joinpath(&file_path, repo->gitdir, GIT_MERGE_MSG_FILE)) < 0 ||
if ((error = git_str_joinpath(&file_path, repo->gitdir, GIT_MERGE_MSG_FILE)) < 0 ||
(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_CREATE_LEADING_DIRS, GIT_CHERRYPICK_FILE_MODE)) < 0 ||
(error = git_filebuf_printf(&file, "%s", commit_msg)) < 0)
goto cleanup;
......@@ -61,7 +61,7 @@ cleanup:
if (error < 0)
git_filebuf_cleanup(&file);
git_buf_dispose(&file_path);
git_str_dispose(&file_path);
return error;
}
......@@ -175,7 +175,7 @@ int git_cherrypick(
git_commit *our_commit = NULL;
char commit_oidstr[GIT_OID_HEXSZ + 1];
const char *commit_msg, *commit_summary;
git_buf their_label = GIT_BUF_INIT;
git_str their_label = GIT_STR_INIT;
git_index *index = NULL;
git_indexwriter indexwriter = GIT_INDEXWRITER_INIT;
int error = 0;
......@@ -197,8 +197,8 @@ int git_cherrypick(
git_oid_nfmt(commit_oidstr, sizeof(commit_oidstr), git_commit_id(commit));
if ((error = write_merge_msg(repo, commit_msg)) < 0 ||
(error = git_buf_printf(&their_label, "%.7s... %s", commit_oidstr, commit_summary)) < 0 ||
(error = cherrypick_normalize_opts(repo, &opts, given_opts, git_buf_cstr(&their_label))) < 0 ||
(error = git_str_printf(&their_label, "%.7s... %s", commit_oidstr, commit_summary)) < 0 ||
(error = cherrypick_normalize_opts(repo, &opts, given_opts, git_str_cstr(&their_label))) < 0 ||
(error = git_indexwriter_init_for_operation(&indexwriter, repo, &opts.checkout_opts.checkout_strategy)) < 0 ||
(error = write_cherrypick_head(repo, commit_oidstr)) < 0 ||
(error = git_repository_head(&our_ref, repo)) < 0 ||
......@@ -220,7 +220,7 @@ done:
git_index_free(index);
git_commit_free(our_commit);
git_reference_free(our_ref);
git_buf_dispose(&their_label);
git_str_dispose(&their_label);
return error;
}
......
......@@ -14,10 +14,10 @@
#include "git2/mailmap.h"
#include "git2/sys/commit.h"
#include "buf.h"
#include "odb.h"
#include "commit.h"
#include "signature.h"
#include "message.h"
#include "refs.h"
#include "object.h"
#include "array.h"
......@@ -42,7 +42,7 @@ void git_commit__free(void *_commit)
}
static int git_commit__create_buffer_internal(
git_buf *out,
git_str *out,
const git_signature *author,
const git_signature *committer,
const char *message_encoding,
......@@ -67,17 +67,17 @@ static int git_commit__create_buffer_internal(
git_signature__writebuf(out, "committer ", committer);
if (message_encoding != NULL)
git_buf_printf(out, "encoding %s\n", message_encoding);
git_str_printf(out, "encoding %s\n", message_encoding);
git_buf_putc(out, '\n');
git_str_putc(out, '\n');
if (git_buf_puts(out, message) < 0)
if (git_str_puts(out, message) < 0)
goto on_error;
return 0;
on_error:
git_buf_dispose(out);
git_str_dispose(out);
return -1;
}
......@@ -136,7 +136,7 @@ static int git_commit__create_internal(
int error;
git_odb *odb;
git_reference *ref = NULL;
git_buf buf = GIT_BUF_INIT;
git_str buf = GIT_STR_INIT;
const git_oid *current_id = NULL;
git_array_oid_t parents = GIT_ARRAY_INIT;
......@@ -179,7 +179,7 @@ static int git_commit__create_internal(
cleanup:
git_array_clear(parents);
git_reference_free(ref);
git_buf_dispose(&buf);
git_str_dispose(&buf);
return error;
}
......@@ -545,7 +545,7 @@ const char *git_commit_message(const git_commit *commit)
const char *git_commit_summary(git_commit *commit)
{
git_buf summary = GIT_BUF_INIT;
git_str summary = GIT_STR_INIT;
const char *msg, *space;
bool space_contains_newline = false;
......@@ -570,17 +570,17 @@ const char *git_commit_summary(git_commit *commit)
/* process any recorded whitespace */
if (space) {
if(space_contains_newline)
git_buf_putc(&summary, ' '); /* if the space contains a newline, collapse to ' ' */
git_str_putc(&summary, ' '); /* if the space contains a newline, collapse to ' ' */
else
git_buf_put(&summary, space, (msg - space)); /* otherwise copy it */
git_str_put(&summary, space, (msg - space)); /* otherwise copy it */
space = NULL;
}
/* copy the next character */
git_buf_putc(&summary, next_character);
git_str_putc(&summary, next_character);
}
}
commit->summary = git_buf_detach(&summary);
commit->summary = git_str_detach(&summary);
if (!commit->summary)
commit->summary = git__strdup("");
}
......@@ -678,11 +678,22 @@ int git_commit_nth_gen_ancestor(
return 0;
}
int git_commit_header_field(git_buf *out, const git_commit *commit, const char *field)
int git_commit_header_field(
git_buf *out,
const git_commit *commit,
const char *field)
{
GIT_BUF_WRAP_PRIVATE(out, git_commit__header_field, commit, field);
}
int git_commit__header_field(
git_str *out,
const git_commit *commit,
const char *field)
{
const char *eol, *buf = commit->raw_header;
git_buf_clear(out);
git_str_clear(out);
while ((eol = strchr(buf, '\n'))) {
/* We can skip continuations here */
......@@ -706,22 +717,22 @@ int git_commit_header_field(git_buf *out, const git_commit *commit, const char *
buf++; /* skip the SP */
git_buf_put(out, buf, eol - buf);
if (git_buf_oom(out))
git_str_put(out, buf, eol - buf);
if (git_str_oom(out))
goto oom;
/* If the next line starts with SP, it's multi-line, we must continue */
while (eol[1] == ' ') {
git_buf_putc(out, '\n');
git_str_putc(out, '\n');
buf = eol + 2;
eol = strchr(buf, '\n');
if (!eol)
goto malformed;
git_buf_put(out, buf, eol - buf);
git_str_put(out, buf, eol - buf);
}
if (git_buf_oom(out))
if (git_str_oom(out))
goto oom;
return 0;
......@@ -738,7 +749,35 @@ oom:
return -1;
}
int git_commit_extract_signature(git_buf *signature, git_buf *signed_data, git_repository *repo, git_oid *commit_id, const char *field)
int git_commit_extract_signature(
git_buf *signature_out,
git_buf *signed_data_out,
git_repository *repo,
git_oid *commit_id,
const char *field)
{
git_str signature = GIT_STR_INIT, signed_data = GIT_STR_INIT;
int error;
if ((error = git_buf_tostr(&signature, signature_out)) < 0 ||
(error = git_buf_tostr(&signed_data, signed_data_out)) < 0 ||
(error = git_commit__extract_signature(&signature, &signed_data, repo, commit_id, field)) < 0 ||
(error = git_buf_fromstr(signature_out, &signature)) < 0 ||
(error = git_buf_fromstr(signed_data_out, &signed_data)) < 0)
goto done;
done:
git_str_dispose(&signature);
git_str_dispose(&signed_data);
return error;
}
int git_commit__extract_signature(
git_str *signature,
git_str *signed_data,
git_repository *repo,
git_oid *commit_id,
const char *field)
{
git_odb_object *obj;
git_odb *odb;
......@@ -746,8 +785,8 @@ int git_commit_extract_signature(git_buf *signature, git_buf *signed_data, git_r
const char *h, *eol;
int error;
git_buf_clear(signature);
git_buf_clear(signed_data);
git_str_clear(signature);
git_str_clear(signed_data);
if (!field)
field = "gpgsig";
......@@ -769,7 +808,7 @@ int git_commit_extract_signature(git_buf *signature, git_buf *signed_data, git_r
while ((h = strchr(buf, '\n')) && h[1] != '\0') {
h++;
if (git__prefixcmp(buf, field)) {
if (git_buf_put(signed_data, buf, h - buf) < 0)
if (git_str_put(signed_data, buf, h - buf) < 0)
return -1;
buf = h;
......@@ -788,25 +827,25 @@ int git_commit_extract_signature(git_buf *signature, git_buf *signed_data, git_r
h++; /* skip the SP */
git_buf_put(signature, h, eol - h);
if (git_buf_oom(signature))
git_str_put(signature, h, eol - h);
if (git_str_oom(signature))
goto oom;
/* If the next line starts with SP, it's multi-line, we must continue */
while (eol[1] == ' ') {
git_buf_putc(signature, '\n');
git_str_putc(signature, '\n');
h = eol + 2;
eol = strchr(h, '\n');
if (!eol)
goto malformed;
git_buf_put(signature, h, eol - h);
git_str_put(signature, h, eol - h);
}
if (git_buf_oom(signature))
if (git_str_oom(signature))
goto oom;
error = git_buf_puts(signed_data, eol+1);
error = git_str_puts(signed_data, eol+1);
git_odb_object_free(obj);
return error;
}
......@@ -826,12 +865,29 @@ oom:
cleanup:
git_odb_object_free(obj);
git_buf_clear(signature);
git_buf_clear(signed_data);
git_str_clear(signature);
git_str_clear(signed_data);
return error;
}
int git_commit_create_buffer(git_buf *out,
int git_commit_create_buffer(
git_buf *out,
git_repository *repo,
const git_signature *author,
const git_signature *committer,
const char *message_encoding,
const char *message,
const git_tree *tree,
size_t parent_count,
const git_commit *parents[])
{
GIT_BUF_WRAP_PRIVATE(out, git_commit__create_buffer, repo,
author, committer, message_encoding, message,
tree, parent_count, parents);
}
int git_commit__create_buffer(
git_str *out,
git_repository *repo,
const git_signature *author,
const git_signature *committer,
......@@ -866,7 +922,7 @@ int git_commit_create_buffer(git_buf *out,
/**
* Append to 'out' properly marking continuations when there's a newline in 'content'
*/
static int format_header_field(git_buf *out, const char *field, const char *content)
static int format_header_field(git_str *out, const char *field, const char *content)
{
const char *lf;
......@@ -874,19 +930,19 @@ static int format_header_field(git_buf *out, const char *field, const char *cont
GIT_ASSERT_ARG(field);
GIT_ASSERT_ARG(content);
git_buf_puts(out, field);
git_buf_putc(out, ' ');
git_str_puts(out, field);
git_str_putc(out, ' ');
while ((lf = strchr(content, '\n')) != NULL) {
git_buf_put(out, content, lf - content);
git_buf_puts(out, "\n ");
git_str_put(out, content, lf - content);
git_str_puts(out, "\n ");
content = lf + 1;
}
git_buf_puts(out, content);
git_buf_putc(out, '\n');
git_str_puts(out, content);
git_str_putc(out, '\n');
return git_buf_oom(out) ? -1 : 0;
return git_str_oom(out) ? -1 : 0;
}
static const git_oid *commit_parent_from_commit(size_t n, void *payload)
......@@ -908,7 +964,7 @@ int git_commit_create_with_signature(
int error = 0;
const char *field;
const char *header_end;
git_buf commit = GIT_BUF_INIT;
git_str commit = GIT_STR_INIT;
git_commit *parsed;
git_array_oid_t parents = GIT_ARRAY_INIT;
......@@ -933,7 +989,7 @@ int git_commit_create_with_signature(
/* The header ends after the first LF */
header_end++;
git_buf_put(&commit, commit_content, header_end - commit_content);
git_str_put(&commit, commit_content, header_end - commit_content);
if (signature != NULL) {
field = signature_field ? signature_field : "gpgsig";
......@@ -942,9 +998,9 @@ int git_commit_create_with_signature(
goto cleanup;
}
git_buf_puts(&commit, header_end);
git_str_puts(&commit, header_end);
if (git_buf_oom(&commit))
if (git_str_oom(&commit))
return -1;
if ((error = git_repository_odb__weakptr(&odb, repo)) < 0)
......@@ -955,7 +1011,7 @@ int git_commit_create_with_signature(
cleanup:
git_commit__free(parsed);
git_buf_dispose(&commit);
git_str_dispose(&commit);
return error;
}
......
......@@ -33,6 +33,29 @@ struct git_commit {
char *body;
};
int git_commit__header_field(
git_str *out,
const git_commit *commit,
const char *field);
int git_commit__extract_signature(
git_str *signature,
git_str *signed_data,
git_repository *repo,
git_oid *commit_id,
const char *field);
int git_commit__create_buffer(
git_str *out,
git_repository *repo,
const git_signature *author,
const git_signature *committer,
const char *message_encoding,
const char *message,
const git_tree *tree,
size_t parent_count,
const git_commit *parents[]);
void git_commit__free(void *commit);
int git_commit__parse(void *commit, git_odb_object *obj);
int git_commit__parse_raw(void *commit, const char *data, size_t size);
......
......@@ -8,6 +8,7 @@
#include "commit_graph.h"
#include "array.h"
#include "buf.h"
#include "filebuf.h"
#include "futils.h"
#include "hash.h"
......@@ -308,12 +309,12 @@ int git_commit_graph_new(git_commit_graph **cgraph_out, const char *objects_dir,
cgraph = git__calloc(1, sizeof(git_commit_graph));
GIT_ERROR_CHECK_ALLOC(cgraph);
error = git_buf_joinpath(&cgraph->filename, objects_dir, "info/commit-graph");
error = git_str_joinpath(&cgraph->filename, objects_dir, "info/commit-graph");
if (error < 0)
goto error;
if (open_file) {
error = git_commit_graph_file_open(&cgraph->file, git_buf_cstr(&cgraph->filename));
error = git_commit_graph_file_open(&cgraph->file, git_str_cstr(&cgraph->filename));
if (error < 0)
goto error;
cgraph->checked = 1;
......@@ -387,7 +388,7 @@ int git_commit_graph_get_file(git_commit_graph_file **file_out, git_commit_graph
cgraph->checked = 1;
/* Best effort */
error = git_commit_graph_file_open(&result, git_buf_cstr(&cgraph->filename));
error = git_commit_graph_file_open(&result, git_str_cstr(&cgraph->filename));
if (error < 0)
return error;
......@@ -407,7 +408,7 @@ void git_commit_graph_refresh(git_commit_graph *cgraph)
return;
if (cgraph->file
&& git_commit_graph_file_needs_refresh(cgraph->file, git_buf_cstr(&cgraph->filename))) {
&& git_commit_graph_file_needs_refresh(cgraph->file, git_str_cstr(&cgraph->filename))) {
/* We just free the commit graph. The next time it is requested, it will be
* re-loaded. */
git_commit_graph_file_free(cgraph->file);
......@@ -597,7 +598,7 @@ void git_commit_graph_free(git_commit_graph *cgraph)
if (!cgraph)
return;
git_buf_dispose(&cgraph->filename);
git_str_dispose(&cgraph->filename);
git_commit_graph_file_free(cgraph->file);
git__free(cgraph);
}
......@@ -623,13 +624,13 @@ int git_commit_graph_writer_new(git_commit_graph_writer **out, const char *objec
git_commit_graph_writer *w = git__calloc(1, sizeof(git_commit_graph_writer));
GIT_ERROR_CHECK_ALLOC(w);
if (git_buf_sets(&w->objects_info_dir, objects_info_dir) < 0) {
if (git_str_sets(&w->objects_info_dir, objects_info_dir) < 0) {
git__free(w);
return -1;
}
if (git_vector_init(&w->commits, 0, packed_commit__cmp) < 0) {
git_buf_dispose(&w->objects_info_dir);
git_str_dispose(&w->objects_info_dir);
git__free(w);
return -1;
}
......@@ -649,7 +650,7 @@ void git_commit_graph_writer_free(git_commit_graph_writer *w)
git_vector_foreach (&w->commits, i, packed_commit)
packed_commit_free(packed_commit);
git_vector_free(&w->commits);
git_buf_dispose(&w->objects_info_dir);
git_str_dispose(&w->objects_info_dir);
git__free(w);
}
......@@ -931,8 +932,8 @@ static int write_chunk_header(
static int commit_graph_write_buf(const char *buf, size_t size, void *data)
{
git_buf *b = (git_buf *)data;
return git_buf_put(b, buf, size);
git_str *b = (git_str *)data;
return git_str_put(b, buf, size);
}
struct commit_graph_write_hash_context {
......@@ -971,8 +972,8 @@ static int commit_graph_write(
uint32_t extra_edge_list_count;
uint32_t oid_fanout[256];
off64_t offset;
git_buf oid_lookup = GIT_BUF_INIT, commit_data = GIT_BUF_INIT,
extra_edge_list = GIT_BUF_INIT;
git_str oid_lookup = GIT_STR_INIT, commit_data = GIT_STR_INIT,
extra_edge_list = GIT_STR_INIT;
git_oid cgraph_checksum = {{0}};
git_hash_ctx ctx;
struct commit_graph_write_hash_context hash_cb_data = {0};
......@@ -1011,7 +1012,7 @@ static int commit_graph_write(
/* Fill the OID Lookup table. */
git_vector_foreach (&w->commits, i, packed_commit) {
error = git_buf_put(&oid_lookup,
error = git_str_put(&oid_lookup,
(const char *)&packed_commit->sha1, sizeof(git_oid));
if (error < 0)
goto cleanup;
......@@ -1026,7 +1027,7 @@ static int commit_graph_write(
size_t *packed_index;
unsigned int parentcount = (unsigned int)git_array_size(packed_commit->parents);
error = git_buf_put(&commit_data,
error = git_str_put(&commit_data,
(const char *)&packed_commit->tree_oid,
sizeof(git_oid));
if (error < 0)
......@@ -1038,7 +1039,7 @@ static int commit_graph_write(
packed_index = git_array_get(packed_commit->parent_indices, 0);
word = htonl((uint32_t)*packed_index);
}
error = git_buf_put(&commit_data, (const char *)&word, sizeof(word));
error = git_str_put(&commit_data, (const char *)&word, sizeof(word));
if (error < 0)
goto cleanup;
......@@ -1050,7 +1051,7 @@ static int commit_graph_write(
} else {
word = htonl(0x80000000u | extra_edge_list_count);
}
error = git_buf_put(&commit_data, (const char *)&word, sizeof(word));
error = git_str_put(&commit_data, (const char *)&word, sizeof(word));
if (error < 0)
goto cleanup;
......@@ -1061,7 +1062,7 @@ static int commit_graph_write(
packed_commit->parent_indices, parent_i);
word = htonl((uint32_t)(*packed_index | (parent_i + 1 == parentcount ? 0x80000000u : 0)));
error = git_buf_put(&extra_edge_list,
error = git_str_put(&extra_edge_list,
(const char *)&word,
sizeof(word));
if (error < 0)
......@@ -1075,18 +1076,18 @@ static int commit_graph_write(
if (generation > GIT_COMMIT_GRAPH_GENERATION_NUMBER_MAX)
generation = GIT_COMMIT_GRAPH_GENERATION_NUMBER_MAX;
word = ntohl((uint32_t)((generation << 2) | ((commit_time >> 32ull) & 0x3ull)));
error = git_buf_put(&commit_data, (const char *)&word, sizeof(word));
error = git_str_put(&commit_data, (const char *)&word, sizeof(word));
if (error < 0)
goto cleanup;
word = ntohl((uint32_t)(commit_time & 0xffffffffull));
error = git_buf_put(&commit_data, (const char *)&word, sizeof(word));
error = git_str_put(&commit_data, (const char *)&word, sizeof(word));
if (error < 0)
goto cleanup;
}
/* Write the header. */
hdr.chunks = 3;
if (git_buf_len(&extra_edge_list) > 0)
if (git_str_len(&extra_edge_list) > 0)
hdr.chunks++;
error = write_cb((const char *)&hdr, sizeof(hdr), cb_data);
if (error < 0)
......@@ -1101,17 +1102,17 @@ static int commit_graph_write(
error = write_chunk_header(COMMIT_GRAPH_OID_LOOKUP_ID, offset, write_cb, cb_data);
if (error < 0)
goto cleanup;
offset += git_buf_len(&oid_lookup);
offset += git_str_len(&oid_lookup);
error = write_chunk_header(COMMIT_GRAPH_COMMIT_DATA_ID, offset, write_cb, cb_data);
if (error < 0)
goto cleanup;
offset += git_buf_len(&commit_data);
if (git_buf_len(&extra_edge_list) > 0) {
offset += git_str_len(&commit_data);
if (git_str_len(&extra_edge_list) > 0) {
error = write_chunk_header(
COMMIT_GRAPH_EXTRA_EDGE_LIST_ID, offset, write_cb, cb_data);
if (error < 0)
goto cleanup;
offset += git_buf_len(&extra_edge_list);
offset += git_str_len(&extra_edge_list);
}
error = write_chunk_header(0, offset, write_cb, cb_data);
if (error < 0)
......@@ -1121,13 +1122,13 @@ static int commit_graph_write(
error = write_cb((const char *)oid_fanout, sizeof(oid_fanout), cb_data);
if (error < 0)
goto cleanup;
error = write_cb(git_buf_cstr(&oid_lookup), git_buf_len(&oid_lookup), cb_data);
error = write_cb(git_str_cstr(&oid_lookup), git_str_len(&oid_lookup), cb_data);
if (error < 0)
goto cleanup;
error = write_cb(git_buf_cstr(&commit_data), git_buf_len(&commit_data), cb_data);
error = write_cb(git_str_cstr(&commit_data), git_str_len(&commit_data), cb_data);
if (error < 0)
goto cleanup;
error = write_cb(git_buf_cstr(&extra_edge_list), git_buf_len(&extra_edge_list), cb_data);
error = write_cb(git_str_cstr(&extra_edge_list), git_str_len(&extra_edge_list), cb_data);
if (error < 0)
goto cleanup;
......@@ -1140,9 +1141,9 @@ static int commit_graph_write(
goto cleanup;
cleanup:
git_buf_dispose(&oid_lookup);
git_buf_dispose(&commit_data);
git_buf_dispose(&extra_edge_list);
git_str_dispose(&oid_lookup);
git_str_dispose(&commit_data);
git_str_dispose(&extra_edge_list);
git_hash_ctx_cleanup(&ctx);
return error;
}
......@@ -1171,21 +1172,21 @@ int git_commit_graph_writer_commit(
{
int error;
int filebuf_flags = GIT_FILEBUF_DO_NOT_BUFFER;
git_buf commit_graph_path = GIT_BUF_INIT;
git_str commit_graph_path = GIT_STR_INIT;
git_filebuf output = GIT_FILEBUF_INIT;
/* TODO: support options and fill in defaults. */
GIT_UNUSED(opts);
error = git_buf_joinpath(
&commit_graph_path, git_buf_cstr(&w->objects_info_dir), "commit-graph");
error = git_str_joinpath(
&commit_graph_path, git_str_cstr(&w->objects_info_dir), "commit-graph");
if (error < 0)
return error;
if (git_repository__fsync_gitdir)
filebuf_flags |= GIT_FILEBUF_FSYNC;
error = git_filebuf_open(&output, git_buf_cstr(&commit_graph_path), filebuf_flags, 0644);
git_buf_dispose(&commit_graph_path);
error = git_filebuf_open(&output, git_str_cstr(&commit_graph_path), filebuf_flags, 0644);
git_str_dispose(&commit_graph_path);
if (error < 0)
return error;
......@@ -1199,9 +1200,17 @@ int git_commit_graph_writer_commit(
}
int git_commit_graph_writer_dump(
git_buf *cgraph,
git_commit_graph_writer *w,
git_commit_graph_writer_options *opts)
git_buf *cgraph,
git_commit_graph_writer *w,
git_commit_graph_writer_options *opts)
{
GIT_BUF_WRAP_PRIVATE(cgraph, git_commit_graph__writer_dump, w, opts);
}
int git_commit_graph__writer_dump(
git_str *cgraph,
git_commit_graph_writer *w,
git_commit_graph_writer_options *opts)
{
/* TODO: support options. */
GIT_UNUSED(opts);
......
......@@ -92,7 +92,7 @@ typedef struct git_commit_graph_entry {
/* A wrapper for git_commit_graph_file to enable lazy loading in the ODB. */
struct git_commit_graph {
/* The path to the commit-graph file. Something like ".git/objects/info/commit-graph". */
git_buf filename;
git_str filename;
/* The underlying commit-graph file. */
git_commit_graph_file *file;
......@@ -127,12 +127,17 @@ struct git_commit_graph_writer {
* The path of the `objects/info` directory where the `commit-graph` will be
* stored.
*/
git_buf objects_info_dir;
git_str objects_info_dir;
/* The list of packed commits. */
git_vector commits;
};
int git_commit_graph__writer_dump(
git_str *cgraph,
git_commit_graph_writer *w,
git_commit_graph_writer_options *opts);
/*
* Returns whether the git_commit_graph_file needs to be reloaded since the
* contents of the commit-graph file have changed on disk.
......
......@@ -124,9 +124,9 @@
#define GIT_ERROR_CHECK_ALLOC(ptr) if (ptr == NULL) { return -1; }
/**
* Check a buffer allocation result, returning -1 if it failed.
* Check a string buffer allocation result, returning -1 if it failed.
*/
#define GIT_ERROR_CHECK_ALLOC_BUF(buf) if ((void *)(buf) == NULL || git_buf_oom(buf)) { return -1; }
#define GIT_ERROR_CHECK_ALLOC_STR(buf) if ((void *)(buf) == NULL || git_str_oom(buf)) { return -1; }
/**
* Check a return value and propagate result if non-zero.
......@@ -202,6 +202,9 @@ GIT_INLINE(void) git__init_structure(void *structure, size_t len, unsigned int v
/* NOTE: other git_error functions are in the public errors.h header file */
/* Forward declare git_str */
typedef struct git_str git_str;
#include "util.h"
#endif
......@@ -10,6 +10,7 @@
#include "git2/config.h"
#include "git2/sys/config.h"
#include "buf.h"
#include "config_backend.h"
#include "regexp.h"
#include "sysdir.h"
......@@ -848,7 +849,40 @@ static int is_readonly(const git_config *cfg)
return 1;
}
int git_config_get_path(git_buf *out, const git_config *cfg, const char *name)
static int git_config__parse_path(git_str *out, const char *value)
{
GIT_ASSERT_ARG(out);
GIT_ASSERT_ARG(value);
if (value[0] == '~') {
if (value[1] != '\0' && value[1] != '/') {
git_error_set(GIT_ERROR_CONFIG, "retrieving a homedir by name is not supported");
return -1;
}
return git_sysdir_expand_global_file(out, value[1] ? &value[2] : NULL);
}
return git_str_sets(out, value);
}
int git_config_parse_path(git_buf *out, const char *value)
{
GIT_BUF_WRAP_PRIVATE(out, git_config__parse_path, value);
}
int git_config_get_path(
git_buf *out,
const git_config *cfg,
const char *name)
{
GIT_BUF_WRAP_PRIVATE(out, git_config__get_path, cfg, name);
}
int git_config__get_path(
git_str *out,
const git_config *cfg,
const char *name)
{
git_config_entry *entry;
int error;
......@@ -856,7 +890,7 @@ int git_config_get_path(git_buf *out, const git_config *cfg, const char *name)
if ((error = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS)) < 0)
return error;
error = git_config_parse_path(out, entry->value);
error = git_config__parse_path(out, entry->value);
git_config_entry_free(entry);
return error;
......@@ -884,18 +918,24 @@ int git_config_get_string(
int git_config_get_string_buf(
git_buf *out, const git_config *cfg, const char *name)
{
GIT_BUF_WRAP_PRIVATE(out, git_config__get_string_buf, cfg, name);
}
int git_config__get_string_buf(
git_str *out, const git_config *cfg, const char *name)
{
git_config_entry *entry;
int ret;
const char *str;
if ((ret = git_buf_sanitize(out)) < 0)
return ret;
GIT_ASSERT_ARG(out);
GIT_ASSERT_ARG(cfg);
ret = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS);
str = !ret ? (entry->value ? entry->value : "") : NULL;
if (str)
ret = git_buf_puts(out, str);
ret = git_str_puts(out, str);
git_config_entry_free(entry);
......@@ -1087,101 +1127,112 @@ void git_config_iterator_free(git_config_iterator *iter)
int git_config_find_global(git_buf *path)
{
int error;
if ((error = git_buf_sanitize(path)) < 0)
return error;
GIT_BUF_WRAP_PRIVATE(path, git_sysdir_find_global_file, GIT_CONFIG_FILENAME_GLOBAL);
}
int git_config__find_global(git_str *path)
{
return git_sysdir_find_global_file(path, GIT_CONFIG_FILENAME_GLOBAL);
}
int git_config_find_xdg(git_buf *path)
{
int error;
if ((error = git_buf_sanitize(path)) < 0)
return error;
GIT_BUF_WRAP_PRIVATE(path, git_sysdir_find_global_file, GIT_CONFIG_FILENAME_XDG);
}
int git_config__find_xdg(git_str *path)
{
return git_sysdir_find_xdg_file(path, GIT_CONFIG_FILENAME_XDG);
}
int git_config_find_system(git_buf *path)
{
int error;
if ((error = git_buf_sanitize(path)) < 0)
return error;
GIT_BUF_WRAP_PRIVATE(path, git_sysdir_find_global_file, GIT_CONFIG_FILENAME_SYSTEM);
}
int git_config__find_system(git_str *path)
{
return git_sysdir_find_system_file(path, GIT_CONFIG_FILENAME_SYSTEM);
}
int git_config_find_programdata(git_buf *path)
{
git_str str = GIT_STR_INIT;
int error;
if ((error = git_buf_tostr(&str, path)) == 0 &&
(error = git_config__find_programdata(&str)) == 0)
error = git_buf_fromstr(path, &str);
git_str_dispose(&str);
return error;
}
int git_config__find_programdata(git_str *path)
{
int ret;
if ((ret = git_buf_sanitize(path)) < 0)
return ret;
ret = git_sysdir_find_programdata_file(path, GIT_CONFIG_FILENAME_PROGRAMDATA);
ret = git_sysdir_find_programdata_file(path,
GIT_CONFIG_FILENAME_PROGRAMDATA);
if (ret != GIT_OK)
return ret;
return git_path_validate_system_file_ownership(path->ptr);
}
int git_config__global_location(git_buf *buf)
int git_config__global_location(git_str *buf)
{
const git_buf *paths;
const git_str *paths;
const char *sep, *start;
if (git_sysdir_get(&paths, GIT_SYSDIR_GLOBAL) < 0)
return -1;
/* no paths, so give up */
if (!paths || !git_buf_len(paths))
if (!paths || !git_str_len(paths))
return -1;
/* find unescaped separator or end of string */
for (sep = start = git_buf_cstr(paths); *sep; ++sep) {
for (sep = start = git_str_cstr(paths); *sep; ++sep) {
if (*sep == GIT_PATH_LIST_SEPARATOR &&
(sep <= start || sep[-1] != '\\'))
break;
}
if (git_buf_set(buf, start, (size_t)(sep - start)) < 0)
if (git_str_set(buf, start, (size_t)(sep - start)) < 0)
return -1;
return git_buf_joinpath(buf, buf->ptr, GIT_CONFIG_FILENAME_GLOBAL);
return git_str_joinpath(buf, buf->ptr, GIT_CONFIG_FILENAME_GLOBAL);
}
int git_config_open_default(git_config **out)
{
int error;
git_config *cfg = NULL;
git_buf buf = GIT_BUF_INIT;
git_str buf = GIT_STR_INIT;
if ((error = git_config_new(&cfg)) < 0)
return error;
if (!git_config_find_global(&buf) || !git_config__global_location(&buf)) {
if (!git_config__find_global(&buf) ||
!git_config__global_location(&buf)) {
error = git_config_add_file_ondisk(cfg, buf.ptr,
GIT_CONFIG_LEVEL_GLOBAL, NULL, 0);
}
if (!error && !git_config_find_xdg(&buf))
if (!error && !git_config__find_xdg(&buf))
error = git_config_add_file_ondisk(cfg, buf.ptr,
GIT_CONFIG_LEVEL_XDG, NULL, 0);
if (!error && !git_config_find_system(&buf))
if (!error && !git_config__find_system(&buf))
error = git_config_add_file_ondisk(cfg, buf.ptr,
GIT_CONFIG_LEVEL_SYSTEM, NULL, 0);
if (!error && !git_config_find_programdata(&buf))
if (!error && !git_config__find_programdata(&buf))
error = git_config_add_file_ondisk(cfg, buf.ptr,
GIT_CONFIG_LEVEL_PROGRAMDATA, NULL, 0);
git_buf_dispose(&buf);
git_str_dispose(&buf);
if (error) {
git_config_free(cfg);
......@@ -1375,28 +1426,6 @@ fail_parse:
return -1;
}
int git_config_parse_path(git_buf *out, const char *value)
{
int error;
GIT_ASSERT_ARG(out);
GIT_ASSERT_ARG(value);
if ((error = git_buf_sanitize(out)) < 0)
return error;
if (value[0] == '~') {
if (value[1] != '\0' && value[1] != '/') {
git_error_set(GIT_ERROR_CONFIG, "retrieving a homedir by name is not supported");
return -1;
}
return git_sysdir_expand_global_file(out, value[1] ? &value[2] : NULL);
}
return git_buf_sets(out, value);
}
static int normalize_section(char *start, char *end)
{
char *scan;
......@@ -1459,7 +1488,7 @@ invalid:
struct rename_data {
git_config *config;
git_buf *name;
git_str *name;
size_t old_len;
};
......@@ -1469,15 +1498,15 @@ static int rename_config_entries_cb(
{
int error = 0;
struct rename_data *data = (struct rename_data *)payload;
size_t base_len = git_buf_len(data->name);
size_t base_len = git_str_len(data->name);
if (base_len > 0 &&
!(error = git_buf_puts(data->name, entry->name + data->old_len)))
!(error = git_str_puts(data->name, entry->name + data->old_len)))
{
error = git_config_set_string(
data->config, git_buf_cstr(data->name), entry->value);
data->config, git_str_cstr(data->name), entry->value);
git_buf_truncate(data->name, base_len);
git_str_truncate(data->name, base_len);
}
if (!error)
......@@ -1492,13 +1521,13 @@ int git_config_rename_section(
const char *new_section_name)
{
git_config *config;
git_buf pattern = GIT_BUF_INIT, replace = GIT_BUF_INIT;
git_str pattern = GIT_STR_INIT, replace = GIT_STR_INIT;
int error = 0;
struct rename_data data;
git_buf_puts_escape_regex(&pattern, old_section_name);
git_str_puts_escape_regex(&pattern, old_section_name);
if ((error = git_buf_puts(&pattern, "\\..+")) < 0)
if ((error = git_str_puts(&pattern, "\\..+")) < 0)
goto cleanup;
if ((error = git_repository_config__weakptr(&config, repo)) < 0)
......@@ -1508,7 +1537,7 @@ int git_config_rename_section(
data.name = &replace;
data.old_len = strlen(old_section_name) + 1;
if ((error = git_buf_join(&replace, '.', new_section_name, "")) < 0)
if ((error = git_str_join(&replace, '.', new_section_name, "")) < 0)
goto cleanup;
if (new_section_name != NULL &&
......@@ -1520,11 +1549,11 @@ int git_config_rename_section(
}
error = git_config_foreach_match(
config, git_buf_cstr(&pattern), rename_config_entries_cb, &data);
config, git_str_cstr(&pattern), rename_config_entries_cb, &data);
cleanup:
git_buf_dispose(&pattern);
git_buf_dispose(&replace);
git_str_dispose(&pattern);
git_str_dispose(&replace);
return error;
}
......
......@@ -27,7 +27,12 @@ struct git_config {
git_vector backends;
};
extern int git_config__global_location(git_buf *buf);
extern int git_config__global_location(git_str *buf);
extern int git_config__find_global(git_str *path);
extern int git_config__find_xdg(git_str *path);
extern int git_config__find_system(git_str *path);
extern int git_config__find_programdata(git_str *path);
extern int git_config_rename_section(
git_repository *repo,
......@@ -51,6 +56,14 @@ extern int git_config__update_entry(
bool overwrite_existing,
bool only_if_existing);
int git_config__get_path(
git_str *out,
const git_config *cfg,
const char *name);
int git_config__get_string_buf(
git_str *out, const git_config *cfg, const char *name);
/*
* Lookup functions that cannot fail. These functions look up a config
* value and return a fallback value if the value is missing or if any
......
......@@ -14,7 +14,7 @@
typedef struct {
git_config_backend parent;
git_config_entries *entries;
git_buf cfg;
git_str cfg;
} config_memory_backend;
typedef struct {
......@@ -38,7 +38,7 @@ static int read_variable_cb(
void *payload)
{
config_memory_parse_data *parse_data = (config_memory_parse_data *) payload;
git_buf buf = GIT_BUF_INIT;
git_str buf = GIT_STR_INIT;
git_config_entry *entry;
const char *c;
int result;
......@@ -52,19 +52,19 @@ static int read_variable_cb(
* 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 = 0;
......@@ -178,7 +178,7 @@ static void config_memory_free(git_config_backend *_backend)
return;
git_config_entries_free(backend->entries);
git_buf_dispose(&backend->cfg);
git_str_dispose(&backend->cfg);
git__free(backend);
}
......@@ -194,7 +194,7 @@ int git_config_backend_from_string(git_config_backend **out, const char *cfg, si
return -1;
}
if (git_buf_set(&backend->cfg, cfg, len) < 0) {
if (git_str_set(&backend->cfg, cfg, len) < 0) {
git_config_entries_free(backend->entries);
git__free(backend);
return -1;
......
......@@ -67,7 +67,7 @@ static int parse_subsection_header(git_config_parser *reader, const char *line,
int c, rpos;
const char *first_quote, *last_quote;
const char *line_start = line;
git_buf buf = GIT_BUF_INIT;
git_str buf = GIT_STR_INIT;
size_t quoted_len, alloc_len, base_name_len = strlen(base_name);
/* Skip any additional whitespace before our section name */
......@@ -97,8 +97,8 @@ static int parse_subsection_header(git_config_parser *reader, const char *line,
GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, base_name_len, quoted_len);
GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 2);
if (git_buf_grow(&buf, alloc_len) < 0 ||
git_buf_printf(&buf, "%s.", base_name) < 0)
if (git_str_grow(&buf, alloc_len) < 0 ||
git_str_printf(&buf, "%s.", base_name) < 0)
goto end_error;
rpos = 0;
......@@ -132,25 +132,25 @@ static int parse_subsection_header(git_config_parser *reader, const char *line,
break;
}
git_buf_putc(&buf, (char)c);
git_str_putc(&buf, (char)c);
c = line[++rpos];
} while (line + rpos < last_quote);
end_parse:
if (git_buf_oom(&buf))
if (git_str_oom(&buf))
goto end_error;
if (line[rpos] != '"' || line[rpos + 1] != ']') {
set_parse_error(reader, rpos, "unexpected text after closing quotes");
git_buf_dispose(&buf);
git_str_dispose(&buf);
return -1;
}
*section_name = git_buf_detach(&buf);
*section_name = git_str_detach(&buf);
return (int)(&line[rpos + 2] - line_start); /* rpos is at the closing quote */
end_error:
git_buf_dispose(&buf);
git_str_dispose(&buf);
return -1;
}
......@@ -227,11 +227,11 @@ fail_parse:
static int skip_bom(git_parse_ctx *parser)
{
git_buf buf = GIT_BUF_INIT_CONST(parser->content, parser->content_len);
git_buf_bom_t bom;
int bom_offset = git_buf_detect_bom(&bom, &buf);
git_str buf = GIT_STR_INIT_CONST(parser->content, parser->content_len);
git_str_bom_t bom;
int bom_offset = git_str_detect_bom(&bom, &buf);
if (bom == GIT_BUF_BOM_UTF8)
if (bom == GIT_STR_BOM_UTF8)
git_parse_advance_chars(parser, bom_offset);
/* TODO: reference implementation is pretty stupid with BoM */
......@@ -325,7 +325,7 @@ done:
return 0;
}
static int parse_multiline_variable(git_config_parser *reader, git_buf *value, int in_quotes)
static int parse_multiline_variable(git_config_parser *reader, git_str *value, int in_quotes)
{
int quote_count;
bool multiline = true;
......@@ -358,7 +358,7 @@ static int parse_multiline_variable(git_config_parser *reader, git_buf *value, i
goto out;
/* Add this line to the multiline var */
if ((error = git_buf_puts(value, proc_line)) < 0)
if ((error = git_str_puts(value, proc_line)) < 0)
goto out;
next:
......@@ -445,18 +445,18 @@ static int parse_variable(git_config_parser *reader, char **var_name, char **var
goto out;
if (multiline) {
git_buf multi_value = GIT_BUF_INIT;
git_buf_attach(&multi_value, value, 0);
git_str multi_value = GIT_STR_INIT;
git_str_attach(&multi_value, value, 0);
value = NULL;
if (parse_multiline_variable(reader, &multi_value, quote_count % 2) < 0 ||
git_buf_oom(&multi_value)) {
git_str_oom(&multi_value)) {
error = -1;
git_buf_dispose(&multi_value);
git_str_dispose(&multi_value);
goto out;
}
value = git_buf_detach(&multi_value);
value = git_str_detach(&multi_value);
}
}
......
......@@ -12,6 +12,7 @@
#include "git2/index.h"
#include "git2/sys/filter.h"
#include "buf.h"
#include "futils.h"
#include "hash.h"
#include "filter.h"
......@@ -152,7 +153,7 @@ static git_configmap_value output_eol(struct crlf_attrs *ca)
GIT_INLINE(int) check_safecrlf(
struct crlf_attrs *ca,
const git_filter_source *src,
git_buf_text_stats *stats)
git_str_text_stats *stats)
{
const char *filename = git_filter_source_path(src);
......@@ -206,19 +207,19 @@ GIT_INLINE(int) check_safecrlf(
static int crlf_apply_to_odb(
struct crlf_attrs *ca,
git_buf *to,
const git_buf *from,
git_str *to,
const git_str *from,
const git_filter_source *src)
{
git_buf_text_stats stats;
git_str_text_stats stats;
bool is_binary;
int error;
/* Binary attribute? Empty file? Nothing to do */
if (ca->crlf_action == GIT_CRLF_BINARY || !git_buf_len(from))
if (ca->crlf_action == GIT_CRLF_BINARY || from->size == 0)
return GIT_PASSTHROUGH;
is_binary = git_buf_gather_text_stats(&stats, from, false);
is_binary = git_str_gather_text_stats(&stats, from, false);
/* Heuristics to see if we can skip the conversion.
* Straight from Core Git.
......@@ -246,22 +247,22 @@ static int crlf_apply_to_odb(
return GIT_PASSTHROUGH;
/* Actually drop the carriage returns */
return git_buf_crlf_to_lf(to, from);
return git_str_crlf_to_lf(to, from);
}
static int crlf_apply_to_workdir(
struct crlf_attrs *ca,
git_buf *to,
const git_buf *from)
git_str *to,
const git_str *from)
{
git_buf_text_stats stats;
git_str_text_stats stats;
bool is_binary;
/* Empty file? Nothing to do. */
if (git_buf_len(from) == 0 || output_eol(ca) != GIT_EOL_CRLF)
if (git_str_len(from) == 0 || output_eol(ca) != GIT_EOL_CRLF)
return GIT_PASSTHROUGH;
is_binary = git_buf_gather_text_stats(&stats, from, false);
is_binary = git_str_gather_text_stats(&stats, from, false);
/* If there are no LFs, or all LFs are part of a CRLF, nothing to do */
if (stats.lf == 0 || stats.lf == stats.crlf)
......@@ -280,7 +281,7 @@ static int crlf_apply_to_workdir(
return GIT_PASSTHROUGH;
}
return git_buf_lf_to_crlf(to, from);
return git_str_lf_to_crlf(to, from);
}
static int convert_attrs(
......@@ -368,22 +369,24 @@ static int crlf_check(
static int crlf_apply(
git_filter *self,
void **payload, /* may be read and/or set */
git_buf *to,
const git_buf *from,
git_str *to,
const git_str *from,
const git_filter_source *src)
{
int error = 0;
/* initialize payload in case `check` was bypassed */
if (!*payload) {
int error = crlf_check(self, payload, src, NULL);
if (error < 0)
if ((error = crlf_check(self, payload, src, NULL)) < 0)
return error;
}
if (git_filter_source_mode(src) == GIT_FILTER_SMUDGE)
return crlf_apply_to_workdir(*payload, to, from);
error = crlf_apply_to_workdir(*payload, to, from);
else
return crlf_apply_to_odb(*payload, to, from, src);
error = crlf_apply_to_odb(*payload, to, from, src);
return error;
}
static int crlf_stream(
......
......@@ -12,6 +12,7 @@
#include "git2/diff.h"
#include "git2/status.h"
#include "buf.h"
#include "commit.h"
#include "commit_list.h"
#include "oidmap.h"
......@@ -322,7 +323,7 @@ static unsigned long finish_depth_computation(
return seen_commits;
}
static int display_name(git_buf *buf, git_repository *repo, struct commit_name *n)
static int display_name(git_str *buf, git_repository *repo, struct commit_name *n)
{
if (n->prio == 2 && !n->tag) {
if (git_tag_lookup(&n->tag, repo, &n->sha1) < 0) {
......@@ -346,9 +347,9 @@ static int display_name(git_buf *buf, git_repository *repo, struct commit_name *
}
if (n->tag)
git_buf_printf(buf, "%s", git_tag_name(n->tag));
git_str_printf(buf, "%s", git_tag_name(n->tag));
else
git_buf_printf(buf, "%s", n->path);
git_str_printf(buf, "%s", n->path);
return 0;
}
......@@ -388,7 +389,7 @@ static int find_unique_abbrev_size(
}
static int show_suffix(
git_buf *buf,
git_str *buf,
int depth,
git_repository *repo,
const git_oid *id,
......@@ -403,11 +404,11 @@ static int show_suffix(
git_oid_fmt(hex_oid, id);
git_buf_printf(buf, "-%d-g", depth);
git_str_printf(buf, "-%d-g", depth);
git_buf_put(buf, hex_oid, size);
git_str_put(buf, hex_oid, size);
return git_buf_oom(buf) ? -1 : 0;
return git_str_oom(buf) ? -1 : 0;
}
#define MAX_CANDIDATES_TAGS FLAG_BITS - 1
......@@ -769,7 +770,10 @@ static int normalize_format_options(
return 0;
}
int git_describe_format(git_buf *out, const git_describe_result *result, const git_describe_format_options *given)
static int git_describe__format(
git_str *out,
const git_describe_result *result,
const git_describe_format_options *given)
{
int error;
git_repository *repo;
......@@ -782,10 +786,6 @@ int git_describe_format(git_buf *out, const git_describe_result *result, const g
GIT_ERROR_CHECK_VERSION(given, GIT_DESCRIBE_FORMAT_OPTIONS_VERSION, "git_describe_format_options");
normalize_format_options(&opts, given);
if ((error = git_buf_sanitize(out)) < 0)
return error;
if (opts.always_use_long_format && opts.abbreviated_size == 0) {
git_error_set(GIT_ERROR_DESCRIBE, "cannot describe - "
"'always_use_long_format' is incompatible with a zero"
......@@ -809,9 +809,9 @@ int git_describe_format(git_buf *out, const git_describe_result *result, const g
}
if (result->dirty && opts.dirty_suffix)
git_buf_puts(out, opts.dirty_suffix);
git_str_puts(out, opts.dirty_suffix);
return git_buf_oom(out) ? -1 : 0;
return git_str_oom(out) ? -1 : 0;
}
/* If we didn't find *any* tags, we fall back to the commit's id */
......@@ -824,12 +824,12 @@ int git_describe_format(git_buf *out, const git_describe_result *result, const g
return -1;
git_oid_fmt(hex_oid, &result->commit_id);
git_buf_put(out, hex_oid, size);
git_str_put(out, hex_oid, size);
if (result->dirty && opts.dirty_suffix)
git_buf_puts(out, opts.dirty_suffix);
git_str_puts(out, opts.dirty_suffix);
return git_buf_oom(out) ? -1 : 0;
return git_str_oom(out) ? -1 : 0;
}
/* Lastly, if we found a matching tag, we show that */
......@@ -845,10 +845,18 @@ int git_describe_format(git_buf *out, const git_describe_result *result, const g
}
if (result->dirty && opts.dirty_suffix) {
git_buf_puts(out, opts.dirty_suffix);
git_str_puts(out, opts.dirty_suffix);
}
return git_buf_oom(out) ? -1 : 0;
return git_str_oom(out) ? -1 : 0;
}
int git_describe_format(
git_buf *out,
const git_describe_result *result,
const git_describe_format_options *given)
{
GIT_BUF_WRAP_PRIVATE(out, git_describe__format, result, given);
}
void git_describe_result_free(git_describe_result *result)
......
......@@ -8,6 +8,7 @@
#include "diff.h"
#include "common.h"
#include "buf.h"
#include "patch.h"
#include "email.h"
#include "commit.h"
......@@ -162,6 +163,7 @@ int git_diff_format_email(
const git_diff_format_email_options *opts)
{
git_email_create_options email_create_opts = GIT_EMAIL_CREATE_OPTIONS_INIT;
git_str email = GIT_STR_INIT;
int error;
GIT_ASSERT_ARG(out);
......@@ -172,14 +174,29 @@ int git_diff_format_email(
GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION,
"git_format_email_options");
/* This is a `git_buf` special case; subsequent calls append. */
email.ptr = out->ptr;
email.asize = out->reserved;
email.size = out->size;
out->ptr = git_str__initstr;
out->reserved = 0;
out->size = 0;
if ((opts->flags & GIT_DIFF_FORMAT_EMAIL_EXCLUDE_SUBJECT_PATCH_MARKER) != 0)
email_create_opts.subject_prefix = "";
error = git_email__append_from_diff(out, diff, opts->patch_no,
error = git_email__append_from_diff(&email, diff, opts->patch_no,
opts->total_patches, opts->id, opts->summary, opts->body,
opts->author, &email_create_opts);
if (error < 0)
goto done;
error = git_buf_fromstr(out, &email);
done:
git_str_dispose(&email);
return error;
}
......@@ -282,7 +299,7 @@ static int flush_hunk(git_oid *result, git_hash_ctx *ctx)
return 0;
}
static void strip_spaces(git_buf *buf)
static void strip_spaces(git_str *buf)
{
char *src = buf->ptr, *dst = buf->ptr;
char c;
......@@ -295,7 +312,7 @@ static void strip_spaces(git_buf *buf)
}
}
git_buf_truncate(buf, len);
git_str_truncate(buf, len);
}
static int diff_patchid_print_callback_to_buf(
......@@ -305,7 +322,7 @@ static int diff_patchid_print_callback_to_buf(
void *payload)
{
struct patch_id_args *args = (struct patch_id_args *) payload;
git_buf buf = GIT_BUF_INIT;
git_str buf = GIT_STR_INIT;
int error = 0;
if (line->origin == GIT_DIFF_LINE_CONTEXT_EOFNL ||
......@@ -331,7 +348,7 @@ static int diff_patchid_print_callback_to_buf(
args->first_file = 0;
out:
git_buf_dispose(&buf);
git_str_dispose(&buf);
return error;
}
......
......@@ -14,9 +14,7 @@
#include "git2/sys/diff.h"
#include "git2/oid.h"
#include <stdio.h>
#include "vector.h"
#include "buffer.h"
#include "iterator.h"
#include "repository.h"
#include "pool.h"
......@@ -53,7 +51,7 @@ struct git_diff {
};
extern int git_diff_delta__format_file_header(
git_buf *out,
git_str *out,
const git_diff_delta *delta,
const char *oldpfx,
const char *newpfx,
......
......@@ -90,7 +90,7 @@ static int diff_driver_add_patterns(
int error = 0;
const char *scan, *end;
git_diff_driver_pattern *pat = NULL;
git_buf buf = GIT_BUF_INIT;
git_str buf = GIT_STR_INIT;
for (scan = regex_str; scan; scan = end) {
/* get pattern to fill in */
......@@ -105,10 +105,10 @@ static int diff_driver_add_patterns(
}
if ((end = strchr(scan, '\n')) != NULL) {
error = git_buf_set(&buf, scan, end - scan);
error = git_str_set(&buf, scan, end - scan);
end++;
} else {
error = git_buf_sets(&buf, scan);
error = git_str_sets(&buf, scan);
}
if (error < 0)
break;
......@@ -122,7 +122,7 @@ static int diff_driver_add_patterns(
if (error && pat != NULL)
(void)git_array_pop(drv->fn_patterns); /* release last item */
git_buf_dispose(&buf);
git_str_dispose(&buf);
/* We want to ignore bad patterns, so return success regardless */
return 0;
......@@ -237,7 +237,7 @@ static int git_diff_driver_load(
git_diff_driver *drv;
size_t namelen;
git_config *cfg = NULL;
git_buf name = GIT_BUF_INIT;
git_str name = GIT_STR_INIT;
git_config_entry *ce = NULL;
bool found_driver = false;
......@@ -260,7 +260,7 @@ static int git_diff_driver_load(
goto done;
}
if ((error = git_buf_printf(&name, "diff.%s.binary", driver_name)) < 0)
if ((error = git_str_printf(&name, "diff.%s.binary", driver_name)) < 0)
goto done;
switch (git_config__get_bool_force(cfg, name.ptr, -1)) {
......@@ -281,8 +281,8 @@ static int git_diff_driver_load(
/* TODO: warn if diff.<name>.command or diff.<name>.textconv are set */
git_buf_truncate(&name, namelen + strlen("diff.."));
if ((error = git_buf_PUTS(&name, "xfuncname")) < 0)
git_str_truncate(&name, namelen + strlen("diff.."));
if ((error = git_str_PUTS(&name, "xfuncname")) < 0)
goto done;
if ((error = git_config_get_multivar_foreach(
......@@ -292,8 +292,8 @@ static int git_diff_driver_load(
git_error_clear(); /* no diff.<driver>.xfuncname, so just continue */
}
git_buf_truncate(&name, namelen + strlen("diff.."));
if ((error = git_buf_PUTS(&name, "funcname")) < 0)
git_str_truncate(&name, namelen + strlen("diff.."));
if ((error = git_str_PUTS(&name, "funcname")) < 0)
goto done;
if ((error = git_config_get_multivar_foreach(
......@@ -309,8 +309,8 @@ static int git_diff_driver_load(
found_driver = true;
}
git_buf_truncate(&name, namelen + strlen("diff.."));
if ((error = git_buf_PUTS(&name, "wordregex")) < 0)
git_str_truncate(&name, namelen + strlen("diff.."));
if ((error = git_str_PUTS(&name, "wordregex")) < 0)
goto done;
if ((error = git_config__lookup_entry(&ce, cfg, name.ptr, false)) < 0)
......@@ -340,7 +340,7 @@ static int git_diff_driver_load(
done:
git_config_entry_free(ce);
git_buf_dispose(&name);
git_str_dispose(&name);
git_config_free(cfg);
if (!*out) {
......@@ -420,11 +420,11 @@ void git_diff_driver_update_options(
int git_diff_driver_content_is_binary(
git_diff_driver *driver, const char *content, size_t content_len)
{
git_buf search = GIT_BUF_INIT;
git_str search = GIT_STR_INIT;
GIT_UNUSED(driver);
git_buf_attach_notowned(&search, content,
git_str_attach_notowned(&search, content,
min(content_len, GIT_FILTER_BYTES_TO_CHECK_NUL));
/* TODO: provide encoding / binary detection callbacks that can
......@@ -432,15 +432,15 @@ int git_diff_driver_content_is_binary(
* let's just use the simple NUL-byte detection that core git uses.
*/
/* previously was: if (git_buf_is_binary(&search)) */
if (git_buf_contains_nul(&search))
/* previously was: if (git_str_is_binary(&search)) */
if (git_str_contains_nul(&search))
return 1;
return 0;
}
static int diff_context_line__simple(
git_diff_driver *driver, git_buf *line)
git_diff_driver *driver, git_str *line)
{
char firstch = line->ptr[0];
GIT_UNUSED(driver);
......@@ -448,7 +448,7 @@ static int diff_context_line__simple(
}
static int diff_context_line__pattern_match(
git_diff_driver *driver, git_buf *line)
git_diff_driver *driver, git_str *line)
{
size_t i, maxi = git_array_size(driver->fn_patterns);
git_regmatch pmatch[2];
......@@ -462,9 +462,9 @@ static int diff_context_line__pattern_match(
/* use pmatch data to trim line data */
i = (pmatch[1].start >= 0) ? 1 : 0;
git_buf_consume(line, git_buf_cstr(line) + pmatch[i].start);
git_buf_truncate(line, pmatch[i].end - pmatch[i].start);
git_buf_rtrim(line);
git_str_consume(line, git_str_cstr(line) + pmatch[i].start);
git_str_truncate(line, pmatch[i].end - pmatch[i].start);
git_str_rtrim(line);
return true;
}
......@@ -482,9 +482,9 @@ static long diff_context_find(
{
git_diff_find_context_payload *ctxt = payload;
if (git_buf_set(&ctxt->line, line, (size_t)line_len) < 0)
if (git_str_set(&ctxt->line, line, (size_t)line_len) < 0)
return -1;
git_buf_rtrim(&ctxt->line);
git_str_rtrim(&ctxt->line);
if (!ctxt->line.size)
return -1;
......@@ -511,14 +511,14 @@ void git_diff_find_context_init(
payload_out->driver = driver;
payload_out->match_line = (driver->type == DIFF_DRIVER_PATTERNLIST) ?
diff_context_line__pattern_match : diff_context_line__simple;
git_buf_init(&payload_out->line, 0);
git_str_init(&payload_out->line, 0);
}
}
void git_diff_find_context_clear(git_diff_find_context_payload *payload)
{
if (payload) {
git_buf_dispose(&payload->line);
git_str_dispose(&payload->line);
payload->driver = NULL;
}
}
......@@ -10,7 +10,7 @@
#include "common.h"
#include "attr_file.h"
#include "buffer.h"
#include "str.h"
typedef struct git_diff_driver_registry git_diff_driver_registry;
......@@ -34,12 +34,12 @@ typedef long (*git_diff_find_context_fn)(
const char *, long, char *, long, void *);
typedef int (*git_diff_find_context_line)(
git_diff_driver *, git_buf *);
git_diff_driver *, git_str *);
typedef struct {
git_diff_driver *driver;
git_diff_find_context_line match_line;
git_buf line;
git_str line;
} git_diff_find_context_payload;
void git_diff_find_context_init(
......
......@@ -178,7 +178,7 @@ static int diff_file_content_commit_to_str(
git_diff_file_content *fc, bool check_status)
{
char oid[GIT_OID_HEXSZ+1];
git_buf content = GIT_BUF_INIT;
git_str content = GIT_STR_INIT;
const char *status = "";
if (check_status) {
......@@ -217,11 +217,11 @@ static int diff_file_content_commit_to_str(
}
git_oid_tostr(oid, sizeof(oid), &fc->file->id);
if (git_buf_printf(&content, "Subproject commit %s%s\n", oid, status) < 0)
if (git_str_printf(&content, "Subproject commit %s%s\n", oid, status) < 0)
return -1;
fc->map.len = git_buf_len(&content);
fc->map.data = git_buf_detach(&content);
fc->map.len = git_str_len(&content);
fc->map.data = git_str_detach(&content);
fc->flags |= GIT_DIFF_FLAG__FREE_DATA;
return 0;
......@@ -270,24 +270,24 @@ static int diff_file_content_load_blob(
}
static int diff_file_content_load_workdir_symlink_fake(
git_diff_file_content *fc, git_buf *path)
git_diff_file_content *fc, git_str *path)
{
git_buf target = GIT_BUF_INIT;
git_str target = GIT_STR_INIT;
int error;
if ((error = git_futils_readbuffer(&target, path->ptr)) < 0)
return error;
fc->map.len = git_buf_len(&target);
fc->map.data = git_buf_detach(&target);
fc->map.len = git_str_len(&target);
fc->map.data = git_str_detach(&target);
fc->flags |= GIT_DIFF_FLAG__FREE_DATA;
git_buf_dispose(&target);
git_str_dispose(&target);
return error;
}
static int diff_file_content_load_workdir_symlink(
git_diff_file_content *fc, git_buf *path)
git_diff_file_content *fc, git_str *path)
{
ssize_t alloc_len, read_len;
int symlink_supported, error;
......@@ -309,7 +309,7 @@ static int diff_file_content_load_workdir_symlink(
fc->flags |= GIT_DIFF_FLAG__FREE_DATA;
read_len = p_readlink(git_buf_cstr(path), fc->map.data, alloc_len);
read_len = p_readlink(git_str_cstr(path), fc->map.data, alloc_len);
if (read_len < 0) {
git_error_set(GIT_ERROR_OS, "failed to read symlink '%s'", fc->file->path);
return -1;
......@@ -321,13 +321,13 @@ static int diff_file_content_load_workdir_symlink(
static int diff_file_content_load_workdir_file(
git_diff_file_content *fc,
git_buf *path,
git_str *path,
git_diff_options *diff_opts)
{
int error = 0;
git_filter_list *fl = NULL;
git_file fd = git_futils_open_ro(git_buf_cstr(path));
git_buf raw = GIT_BUF_INIT;
git_file fd = git_futils_open_ro(git_str_cstr(path));
git_str raw = GIT_STR_INIT;
if (fd < 0)
return fd;
......@@ -360,7 +360,7 @@ static int diff_file_content_load_workdir_file(
}
if (!(error = git_futils_readbuffer_fd(&raw, fd, (size_t)fc->file->size))) {
git_buf out = GIT_BUF_INIT;
git_str out = GIT_STR_INIT;
error = git_filter_list__convert_buf(&out, fl, &raw);
......@@ -383,7 +383,7 @@ static int diff_file_content_load_workdir(
git_diff_options *diff_opts)
{
int error = 0;
git_buf path = GIT_BUF_INIT;
git_str path = GIT_STR_INIT;
if (fc->file->mode == GIT_FILEMODE_COMMIT)
return diff_file_content_commit_to_str(fc, true);
......@@ -406,7 +406,7 @@ static int diff_file_content_load_workdir(
fc->file->flags |= GIT_DIFF_FLAG_VALID_ID;
}
git_buf_dispose(&path);
git_str_dispose(&path);
return error;
}
......
......@@ -586,7 +586,7 @@ int git_diff__oid_for_entry(
const git_oid *update_match)
{
git_diff_generated *diff;
git_buf full_path = GIT_BUF_INIT;
git_str full_path = GIT_STR_INIT;
git_index_entry entry = *src;
git_filter_list *fl = NULL;
int error = 0;
......@@ -606,7 +606,7 @@ int git_diff__oid_for_entry(
if (p_stat(full_path.ptr, &st) < 0) {
error = git_path_set_error(errno, entry.path, "stat");
git_buf_dispose(&full_path);
git_str_dispose(&full_path);
return error;
}
......@@ -669,7 +669,7 @@ int git_diff__oid_for_entry(
}
}
git_buf_dispose(&full_path);
git_str_dispose(&full_path);
return error;
}
......@@ -1023,7 +1023,7 @@ static int handle_unmatched_new_item(
/* do not advance into directories that contain a .git file */
if (recurse_into_dir && !contains_oitem) {
git_buf *full = NULL;
git_str *full = NULL;
if (git_iterator_current_workdir_path(&full, info->new_iter) < 0)
return -1;
if (full && git_path_contains(full, DOT_GIT)) {
......
......@@ -5,8 +5,10 @@
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "common.h"
#include "diff_stats.h"
#include "buf.h"
#include "common.h"
#include "vector.h"
#include "diff.h"
#include "patch_generate.h"
......@@ -47,7 +49,7 @@ static int digits_for_value(size_t val)
}
static int diff_file_stats_full_to_buf(
git_buf *out,
git_str *out,
const git_diff_delta *delta,
const diff_file_stats *filestat,
const git_diff_stats *stats,
......@@ -70,12 +72,12 @@ static int diff_file_stats_full_to_buf(
if ((common_dirlen = git_path_common_dirlen(old_path, new_path)) &&
common_dirlen <= INT_MAX) {
error = git_buf_printf(out, " %.*s{%s"DIFF_RENAME_FILE_SEPARATOR"%s}",
error = git_str_printf(out, " %.*s{%s"DIFF_RENAME_FILE_SEPARATOR"%s}",
(int) common_dirlen, old_path,
old_path + common_dirlen,
new_path + common_dirlen);
} else {
error = git_buf_printf(out, " %s" DIFF_RENAME_FILE_SEPARATOR "%s",
error = git_str_printf(out, " %s" DIFF_RENAME_FILE_SEPARATOR "%s",
old_path, new_path);
}
......@@ -83,7 +85,7 @@ static int diff_file_stats_full_to_buf(
goto on_error;
} else {
adddel_path = new_path ? new_path : old_path;
if (git_buf_printf(out, " %s", adddel_path) < 0)
if (git_str_printf(out, " %s", adddel_path) < 0)
goto on_error;
padding = stats->max_name - strlen(adddel_path);
......@@ -92,28 +94,28 @@ static int diff_file_stats_full_to_buf(
padding += strlen(DIFF_RENAME_FILE_SEPARATOR);
}
if (git_buf_putcn(out, ' ', padding) < 0 ||
git_buf_puts(out, " | ") < 0)
if (git_str_putcn(out, ' ', padding) < 0 ||
git_str_puts(out, " | ") < 0)
goto on_error;
if (delta->flags & GIT_DIFF_FLAG_BINARY) {
if (git_buf_printf(out,
if (git_str_printf(out,
"Bin %" PRId64 " -> %" PRId64 " bytes", old_size, new_size) < 0)
goto on_error;
}
else {
if (git_buf_printf(out,
if (git_str_printf(out,
"%*" PRIuZ, stats->max_digits,
filestat->insertions + filestat->deletions) < 0)
goto on_error;
if (filestat->insertions || filestat->deletions) {
if (git_buf_putc(out, ' ') < 0)
if (git_str_putc(out, ' ') < 0)
goto on_error;
if (!width) {
if (git_buf_putcn(out, '+', filestat->insertions) < 0 ||
git_buf_putcn(out, '-', filestat->deletions) < 0)
if (git_str_putcn(out, '+', filestat->insertions) < 0 ||
git_str_putcn(out, '-', filestat->deletions) < 0)
goto on_error;
} else {
size_t total = filestat->insertions + filestat->deletions;
......@@ -122,21 +124,21 @@ static int diff_file_stats_full_to_buf(
size_t plus = full * filestat->insertions / total;
size_t minus = full - plus;
if (git_buf_putcn(out, '+', max(plus, 1)) < 0 ||
git_buf_putcn(out, '-', max(minus, 1)) < 0)
if (git_str_putcn(out, '+', max(plus, 1)) < 0 ||
git_str_putcn(out, '-', max(minus, 1)) < 0)
goto on_error;
}
}
}
git_buf_putc(out, '\n');
git_str_putc(out, '\n');
on_error:
return (git_buf_oom(out) ? -1 : 0);
return (git_str_oom(out) ? -1 : 0);
}
static int diff_file_stats_number_to_buf(
git_buf *out,
git_str *out,
const git_diff_delta *delta,
const diff_file_stats *filestats)
{
......@@ -144,29 +146,29 @@ static int diff_file_stats_number_to_buf(
const char *path = delta->new_file.path;
if (delta->flags & GIT_DIFF_FLAG_BINARY)
error = git_buf_printf(out, "%-8c" "%-8c" "%s\n", '-', '-', path);
error = git_str_printf(out, "%-8c" "%-8c" "%s\n", '-', '-', path);
else
error = git_buf_printf(out, "%-8" PRIuZ "%-8" PRIuZ "%s\n",
error = git_str_printf(out, "%-8" PRIuZ "%-8" PRIuZ "%s\n",
filestats->insertions, filestats->deletions, path);
return error;
}
static int diff_file_stats_summary_to_buf(
git_buf *out,
git_str *out,
const git_diff_delta *delta)
{
if (delta->old_file.mode != delta->new_file.mode) {
if (delta->old_file.mode == 0) {
git_buf_printf(out, " create mode %06o %s\n",
git_str_printf(out, " create mode %06o %s\n",
delta->new_file.mode, delta->new_file.path);
}
else if (delta->new_file.mode == 0) {
git_buf_printf(out, " delete mode %06o %s\n",
git_str_printf(out, " delete mode %06o %s\n",
delta->old_file.mode, delta->old_file.path);
}
else {
git_buf_printf(out, " mode change %06o => %06o %s\n",
git_str_printf(out, " mode change %06o => %06o %s\n",
delta->old_file.mode, delta->new_file.mode, delta->new_file.path);
}
}
......@@ -279,6 +281,15 @@ int git_diff_stats_to_buf(
git_diff_stats_format_t format,
size_t width)
{
GIT_BUF_WRAP_PRIVATE(out, git_diff__stats_to_buf, stats, format, width);
}
int git_diff__stats_to_buf(
git_str *out,
const git_diff_stats *stats,
git_diff_stats_format_t format,
size_t width)
{
int error = 0;
size_t i;
const git_diff_delta *delta;
......@@ -320,23 +331,23 @@ int git_diff_stats_to_buf(
}
if (format & GIT_DIFF_STATS_FULL || format & GIT_DIFF_STATS_SHORT) {
git_buf_printf(
git_str_printf(
out, " %" PRIuZ " file%s changed",
stats->files_changed, stats->files_changed != 1 ? "s" : "");
if (stats->insertions || stats->deletions == 0)
git_buf_printf(
git_str_printf(
out, ", %" PRIuZ " insertion%s(+)",
stats->insertions, stats->insertions != 1 ? "s" : "");
if (stats->deletions || stats->insertions == 0)
git_buf_printf(
git_str_printf(
out, ", %" PRIuZ " deletion%s(-)",
stats->deletions, stats->deletions != 1 ? "s" : "");
git_buf_putc(out, '\n');
git_str_putc(out, '\n');
if (git_buf_oom(out))
if (git_str_oom(out))
return -1;
}
......
......@@ -4,14 +4,15 @@
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#ifndef INCLUDE_message_h__
#define INCLUDE_message_h__
#ifndef INCLUDE_diff_stats_h__
#define INCLUDE_diff_stats_h__
#include "common.h"
#include "git2/message.h"
#include "buffer.h"
int git_message__prettify(git_buf *message_out, const char *message, int strip_comments);
int git_diff__stats_to_buf(
git_str *out,
const git_diff_stats *stats,
git_diff_stats_format_t format,
size_t width);
#endif
......@@ -444,7 +444,7 @@ typedef struct {
git_iterator_t src;
git_repository *repo;
git_diff_file *file;
git_buf data;
git_str data;
git_odb_object *odb_obj;
git_blob *blob;
} similarity_info;
......@@ -458,7 +458,7 @@ static int similarity_init(
info->file = similarity_get_file(diff, file_idx);
info->odb_obj = NULL;
info->blob = NULL;
git_buf_init(&info->data, 0);
git_str_init(&info->data, 0);
if (info->file->size > 0 || info->src == GIT_ITERATOR_WORKDIR)
return 0;
......@@ -529,7 +529,7 @@ static void similarity_unload(similarity_info *info)
if (info->blob)
git_blob_free(info->blob);
else
git_buf_dispose(&info->data);
git_str_dispose(&info->data);
}
#define FLAG_SET(opts,flag_name) (((opts)->flags & flag_name) != 0)
......
......@@ -7,9 +7,11 @@
#include "email.h"
#include "buffer.h"
#include "common.h"
#include "buf.h"
#include "diff_generate.h"
#include "diff_stats.h"
#include "patch.h"
#include "git2/email.h"
#include "git2/patch.h"
......@@ -32,7 +34,7 @@ GIT_INLINE(int) include_prefix(
}
static int append_prefix(
git_buf *out,
git_str *out,
size_t patch_idx,
size_t patch_count,
git_email_create_options *opts)
......@@ -40,16 +42,16 @@ static int append_prefix(
const char *subject_prefix = opts->subject_prefix ?
opts->subject_prefix : "PATCH";
git_buf_putc(out, '[');
git_str_putc(out, '[');
if (*subject_prefix)
git_buf_puts(out, subject_prefix);
git_str_puts(out, subject_prefix);
if (opts->reroll_number) {
if (*subject_prefix)
git_buf_putc(out, ' ');
git_str_putc(out, ' ');
git_buf_printf(out, "v%" PRIuZ, opts->reroll_number);
git_str_printf(out, "v%" PRIuZ, opts->reroll_number);
}
if ((opts->flags & GIT_EMAIL_CREATE_ALWAYS_NUMBER) != 0 ||
......@@ -58,20 +60,20 @@ static int append_prefix(
opts->start_number : 1;
if (*subject_prefix || opts->reroll_number)
git_buf_putc(out, ' ');
git_str_putc(out, ' ');
git_buf_printf(out, "%" PRIuZ "/%" PRIuZ,
git_str_printf(out, "%" PRIuZ "/%" PRIuZ,
patch_idx + (start_number - 1),
patch_count + (start_number - 1));
}
git_buf_puts(out, "]");
git_str_puts(out, "]");
return git_buf_oom(out) ? -1 : 0;
return git_str_oom(out) ? -1 : 0;
}
static int append_subject(
git_buf *out,
git_str *out,
size_t patch_idx,
size_t patch_count,
const char *summary,
......@@ -88,25 +90,25 @@ static int append_subject(
summary_len = (nl - summary);
}
if ((error = git_buf_puts(out, "Subject: ")) < 0)
if ((error = git_str_puts(out, "Subject: ")) < 0)
return error;
if (prefix &&
(error = append_prefix(out, patch_idx, patch_count, opts)) < 0)
return error;
if (prefix && summary_len && (error = git_buf_putc(out, ' ')) < 0)
if (prefix && summary_len && (error = git_str_putc(out, ' ')) < 0)
return error;
if (summary_len &&
(error = git_buf_put(out, summary, summary_len)) < 0)
(error = git_str_put(out, summary, summary_len)) < 0)
return error;
return git_buf_putc(out, '\n');
return git_str_putc(out, '\n');
}
static int append_header(
git_buf *out,
git_str *out,
size_t patch_idx,
size_t patch_count,
const git_oid *commit_id,
......@@ -119,20 +121,20 @@ static int append_header(
int error;
if ((error = git_oid_fmt(id, commit_id)) < 0 ||
(error = git_buf_printf(out, "From %.*s %s\n", GIT_OID_HEXSZ, id, EMAIL_TIMESTAMP)) < 0 ||
(error = git_buf_printf(out, "From: %s <%s>\n", author->name, author->email)) < 0 ||
(error = git_str_printf(out, "From %.*s %s\n", GIT_OID_HEXSZ, id, EMAIL_TIMESTAMP)) < 0 ||
(error = git_str_printf(out, "From: %s <%s>\n", author->name, author->email)) < 0 ||
(error = git__date_rfc2822_fmt(date, sizeof(date), &author->when)) < 0 ||
(error = git_buf_printf(out, "Date: %s\n", date)) < 0 ||
(error = git_str_printf(out, "Date: %s\n", date)) < 0 ||
(error = append_subject(out, patch_idx, patch_count, summary, opts)) < 0)
return error;
if ((error = git_buf_putc(out, '\n')) < 0)
if ((error = git_str_putc(out, '\n')) < 0)
return error;
return 0;
}
static int append_body(git_buf *out, const char *body)
static int append_body(git_str *out, const char *body)
{
size_t body_len;
int error;
......@@ -142,16 +144,16 @@ static int append_body(git_buf *out, const char *body)
body_len = strlen(body);
if ((error = git_buf_puts(out, body)) < 0)
if ((error = git_str_puts(out, body)) < 0)
return error;
if (body_len && body[body_len - 1] != '\n')
error = git_buf_putc(out, '\n');
error = git_str_putc(out, '\n');
return error;
}
static int append_diffstat(git_buf *out, git_diff *diff)
static int append_diffstat(git_str *out, git_diff *diff)
{
git_diff_stats *stats = NULL;
unsigned int format_flags;
......@@ -160,14 +162,14 @@ static int append_diffstat(git_buf *out, git_diff *diff)
format_flags = GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_INCLUDE_SUMMARY;
if ((error = git_diff_get_stats(&stats, diff)) == 0 &&
(error = git_diff_stats_to_buf(out, stats, format_flags, 0)) == 0)
error = git_buf_putc(out, '\n');
(error = git_diff__stats_to_buf(out, stats, format_flags, 0)) == 0)
error = git_str_putc(out, '\n');
git_diff_stats_free(stats);
return error;
}
static int append_patches(git_buf *out, git_diff *diff)
static int append_patches(git_str *out, git_diff *diff)
{
size_t i, deltas;
int error = 0;
......@@ -178,7 +180,7 @@ static int append_patches(git_buf *out, git_diff *diff)
git_patch *patch = NULL;
if ((error = git_patch_from_diff(&patch, diff, i)) >= 0)
error = git_patch_to_buf(out, patch);
error = git_patch__to_buf(out, patch);
git_patch_free(patch);
......@@ -190,7 +192,7 @@ static int append_patches(git_buf *out, git_diff *diff)
}
int git_email__append_from_diff(
git_buf *out,
git_str *out,
git_diff *diff,
size_t patch_idx,
size_t patch_count,
......@@ -216,14 +218,12 @@ int git_email__append_from_diff(
if (given_opts)
memcpy(&opts, given_opts, sizeof(git_email_create_options));
git_buf_sanitize(out);
if ((error = append_header(out, patch_idx, patch_count, commit_id, summary, author, &opts)) == 0 &&
(error = append_body(out, body)) == 0 &&
(error = git_buf_puts(out, "---\n")) == 0 &&
(error = git_str_puts(out, "---\n")) == 0 &&
(error = append_diffstat(out, diff)) == 0 &&
(error = append_patches(out, diff)) == 0)
error = git_buf_puts(out, "--\nlibgit2 " LIBGIT2_VERSION "\n\n");
error = git_str_puts(out, "--\nlibgit2 " LIBGIT2_VERSION "\n\n");
return error;
}
......@@ -239,15 +239,19 @@ int git_email_create_from_diff(
const git_signature *author,
const git_email_create_options *given_opts)
{
git_str email = GIT_STR_INIT;
int error;
git_buf_sanitize(out);
git_buf_clear(out);
git_buf_tostr(&email, out);
error = git_email__append_from_diff(out, diff, patch_idx,
error = git_email__append_from_diff(&email, diff, patch_idx,
patch_count, commit_id, summary, body, author,
given_opts);
if (error == 0)
error = git_buf_fromstr(out, &email);
git_str_dispose(&email);
return error;
}
......
......@@ -12,7 +12,7 @@
#include "git2/email.h"
extern int git_email__append_from_diff(
git_buf *out,
git_str *out,
git_diff *diff,
size_t patch_idx,
size_t patch_count,
......
......@@ -9,7 +9,7 @@
#include "threadstate.h"
#include "posix.h"
#include "buffer.h"
#include "str.h"
#include "libgit2.h"
/********************************************
......@@ -29,7 +29,7 @@ static git_error g_git_uninitialized_error = {
static void set_error_from_buffer(int error_class)
{
git_error *error = &GIT_THREADSTATE->error_t;
git_buf *buf = &GIT_THREADSTATE->error_buf;
git_str *buf = &GIT_THREADSTATE->error_buf;
error->message = buf->ptr;
error->klass = error_class;
......@@ -39,11 +39,11 @@ static void set_error_from_buffer(int error_class)
static void set_error(int error_class, char *string)
{
git_buf *buf = &GIT_THREADSTATE->error_buf;
git_str *buf = &GIT_THREADSTATE->error_buf;
git_buf_clear(buf);
git_str_clear(buf);
if (string) {
git_buf_puts(buf, string);
git_str_puts(buf, string);
git__free(string);
}
......@@ -70,20 +70,20 @@ void git_error_vset(int error_class, const char *fmt, va_list ap)
DWORD win32_error_code = (error_class == GIT_ERROR_OS) ? GetLastError() : 0;
#endif
int error_code = (error_class == GIT_ERROR_OS) ? errno : 0;
git_buf *buf = &GIT_THREADSTATE->error_buf;
git_str *buf = &GIT_THREADSTATE->error_buf;
git_buf_clear(buf);
git_str_clear(buf);
if (fmt) {
git_buf_vprintf(buf, fmt, ap);
git_str_vprintf(buf, fmt, ap);
if (error_class == GIT_ERROR_OS)
git_buf_PUTS(buf, ": ");
git_str_PUTS(buf, ": ");
}
if (error_class == GIT_ERROR_OS) {
#ifdef GIT_WIN32
char * win32_error = git_win32_get_error_message(win32_error_code);
if (win32_error) {
git_buf_puts(buf, win32_error);
git_str_puts(buf, win32_error);
git__free(win32_error);
SetLastError(0);
......@@ -91,26 +91,26 @@ void git_error_vset(int error_class, const char *fmt, va_list ap)
else
#endif
if (error_code)
git_buf_puts(buf, strerror(error_code));
git_str_puts(buf, strerror(error_code));
if (error_code)
errno = 0;
}
if (!git_buf_oom(buf))
if (!git_str_oom(buf))
set_error_from_buffer(error_class);
}
int git_error_set_str(int error_class, const char *string)
{
git_buf *buf = &GIT_THREADSTATE->error_buf;
git_str *buf = &GIT_THREADSTATE->error_buf;
GIT_ASSERT_ARG(string);
git_buf_clear(buf);
git_buf_puts(buf, string);
git_str_clear(buf);
git_str_puts(buf, string);
if (git_buf_oom(buf))
if (git_str_oom(buf))
return -1;
set_error_from_buffer(error_class);
......@@ -142,7 +142,7 @@ const git_error *git_error_last(void)
int git_error_state_capture(git_error_state *state, int error_code)
{
git_error *error = GIT_THREADSTATE->last_error;
git_buf *error_buf = &GIT_THREADSTATE->error_buf;
git_str *error_buf = &GIT_THREADSTATE->error_buf;
memset(state, 0, sizeof(git_error_state));
......@@ -158,7 +158,7 @@ int git_error_state_capture(git_error_state *state, int error_code)
if (state->oom)
state->error_msg.message = g_git_oom_error.message;
else
state->error_msg.message = git_buf_detach(error_buf);
state->error_msg.message = git_str_detach(error_buf);
}
git_error_clear();
......
......@@ -10,7 +10,7 @@
#include "git2/types.h"
#include "git2/oid.h"
#include "buffer.h"
#include "str.h"
#include "futils.h"
#include "filebuf.h"
#include "refs.h"
......@@ -44,7 +44,7 @@ static char *sanitized_remote_url(const char *remote_url)
int error;
if (git_net_url_parse(&url, remote_url) == 0) {
git_buf buf = GIT_BUF_INIT;
git_str buf = GIT_STR_INIT;
git__free(url.username);
git__free(url.password);
......@@ -53,7 +53,7 @@ static char *sanitized_remote_url(const char *remote_url)
if ((error = git_net_url_fmt(&buf, &url)) < 0)
goto fallback;
sanitized = git_buf_detach(&buf);
sanitized = git_str_detach(&buf);
}
fallback:
......@@ -143,22 +143,22 @@ static int fetchhead_ref_write(
int git_fetchhead_write(git_repository *repo, git_vector *fetchhead_refs)
{
git_filebuf file = GIT_FILEBUF_INIT;
git_buf path = GIT_BUF_INIT;
git_str path = GIT_STR_INIT;
unsigned int i;
git_fetchhead_ref *fetchhead_ref;
GIT_ASSERT_ARG(repo);
GIT_ASSERT_ARG(fetchhead_refs);
if (git_buf_joinpath(&path, repo->gitdir, GIT_FETCH_HEAD_FILE) < 0)
if (git_str_joinpath(&path, repo->gitdir, GIT_FETCH_HEAD_FILE) < 0)
return -1;
if (git_filebuf_open(&file, path.ptr, GIT_FILEBUF_APPEND, GIT_REFS_FILE_MODE) < 0) {
git_buf_dispose(&path);
git_str_dispose(&path);
return -1;
}
git_buf_dispose(&path);
git_str_dispose(&path);
git_vector_sort(fetchhead_refs);
......@@ -171,7 +171,7 @@ int git_fetchhead_write(git_repository *repo, git_vector *fetchhead_refs)
static int fetchhead_ref_parse(
git_oid *oid,
unsigned int *is_merge,
git_buf *ref_name,
git_str *ref_name,
const char **remote_url,
char *line,
size_t line_num)
......@@ -259,12 +259,12 @@ static int fetchhead_ref_parse(
*remote_url = desc;
}
git_buf_clear(ref_name);
git_str_clear(ref_name);
if (type)
git_buf_join(ref_name, '/', type, name);
git_str_join(ref_name, '/', type, name);
else if(name)
git_buf_puts(ref_name, name);
git_str_puts(ref_name, name);
return error;
}
......@@ -273,7 +273,7 @@ int git_repository_fetchhead_foreach(git_repository *repo,
git_repository_fetchhead_foreach_cb cb,
void *payload)
{
git_buf path = GIT_BUF_INIT, file = GIT_BUF_INIT, name = GIT_BUF_INIT;
git_str path = GIT_STR_INIT, file = GIT_STR_INIT, name = GIT_STR_INIT;
const char *ref_name;
git_oid oid;
const char *remote_url;
......@@ -285,10 +285,10 @@ int git_repository_fetchhead_foreach(git_repository *repo,
GIT_ASSERT_ARG(repo);
GIT_ASSERT_ARG(cb);
if (git_buf_joinpath(&path, repo->gitdir, GIT_FETCH_HEAD_FILE) < 0)
if (git_str_joinpath(&path, repo->gitdir, GIT_FETCH_HEAD_FILE) < 0)
return -1;
if ((error = git_futils_readbuffer(&file, git_buf_cstr(&path))) < 0)
if ((error = git_futils_readbuffer(&file, git_str_cstr(&path))) < 0)
goto done;
buffer = file.ptr;
......@@ -300,8 +300,8 @@ int git_repository_fetchhead_foreach(git_repository *repo,
&oid, &is_merge, &name, &remote_url, line, line_num)) < 0)
goto done;
if (git_buf_len(&name) > 0)
ref_name = git_buf_cstr(&name);
if (git_str_len(&name) > 0)
ref_name = git_str_cstr(&name);
else
ref_name = NULL;
......@@ -319,9 +319,9 @@ int git_repository_fetchhead_foreach(git_repository *repo,
}
done:
git_buf_dispose(&file);
git_buf_dispose(&path);
git_buf_dispose(&name);
git_str_dispose(&file);
git_str_dispose(&path);
git_str_dispose(&name);
return error;
}
......
......@@ -195,21 +195,21 @@ static int write_deflate(git_filebuf *file, void *source, size_t len)
#define MAX_SYMLINK_DEPTH 5
static int resolve_symlink(git_buf *out, const char *path)
static int resolve_symlink(git_str *out, const char *path)
{
int i, error, root;
ssize_t ret;
struct stat st;
git_buf curpath = GIT_BUF_INIT, target = GIT_BUF_INIT;
git_str curpath = GIT_STR_INIT, target = GIT_STR_INIT;
if ((error = git_buf_grow(&target, GIT_PATH_MAX + 1)) < 0 ||
(error = git_buf_puts(&curpath, path)) < 0)
if ((error = git_str_grow(&target, GIT_PATH_MAX + 1)) < 0 ||
(error = git_str_puts(&curpath, path)) < 0)
return error;
for (i = 0; i < MAX_SYMLINK_DEPTH; i++) {
error = p_lstat(curpath.ptr, &st);
if (error < 0 && errno == ENOENT) {
error = git_buf_puts(out, curpath.ptr);
error = git_str_puts(out, curpath.ptr);
goto cleanup;
}
......@@ -220,7 +220,7 @@ static int resolve_symlink(git_buf *out, const char *path)
}
if (!S_ISLNK(st.st_mode)) {
error = git_buf_puts(out, curpath.ptr);
error = git_str_puts(out, curpath.ptr);
goto cleanup;
}
......@@ -243,16 +243,16 @@ static int resolve_symlink(git_buf *out, const char *path)
root = git_path_root(target.ptr);
if (root >= 0) {
if ((error = git_buf_sets(&curpath, target.ptr)) < 0)
if ((error = git_str_sets(&curpath, target.ptr)) < 0)
goto cleanup;
} else {
git_buf dir = GIT_BUF_INIT;
git_str dir = GIT_STR_INIT;
if ((error = git_path_dirname_r(&dir, curpath.ptr)) < 0)
goto cleanup;
git_buf_swap(&curpath, &dir);
git_buf_dispose(&dir);
git_str_swap(&curpath, &dir);
git_str_dispose(&dir);
if ((error = git_path_apply_relative(&curpath, target.ptr)) < 0)
goto cleanup;
......@@ -263,8 +263,8 @@ static int resolve_symlink(git_buf *out, const char *path)
error = -1;
cleanup:
git_buf_dispose(&curpath);
git_buf_dispose(&target);
git_str_dispose(&curpath);
git_str_dispose(&target);
return error;
}
......@@ -332,13 +332,13 @@ int git_filebuf_open_withsize(git_filebuf *file, const char *path, int flags, mo
/* If we are writing to a temp file */
if (flags & GIT_FILEBUF_TEMPORARY) {
git_buf tmp_path = GIT_BUF_INIT;
git_str tmp_path = GIT_STR_INIT;
/* Open the file as temporary for locking */
file->fd = git_futils_mktmp(&tmp_path, path, mode);
if (file->fd < 0) {
git_buf_dispose(&tmp_path);
git_str_dispose(&tmp_path);
goto cleanup;
}
file->fd_is_open = true;
......@@ -346,17 +346,17 @@ int git_filebuf_open_withsize(git_filebuf *file, const char *path, int flags, mo
/* No original path */
file->path_original = NULL;
file->path_lock = git_buf_detach(&tmp_path);
file->path_lock = git_str_detach(&tmp_path);
GIT_ERROR_CHECK_ALLOC(file->path_lock);
} else {
git_buf resolved_path = GIT_BUF_INIT;
git_str resolved_path = GIT_STR_INIT;
if ((error = resolve_symlink(&resolved_path, path)) < 0)
goto cleanup;
/* Save the original path of the file */
path_len = resolved_path.size;
file->path_original = git_buf_detach(&resolved_path);
file->path_original = git_str_detach(&resolved_path);
/* create the locking path by appending ".lock" to the original */
GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, path_len, GIT_FILELOCK_EXTLENGTH);
......
......@@ -7,6 +7,7 @@
#include "filter.h"
#include "buf.h"
#include "common.h"
#include "futils.h"
#include "hash.h"
......@@ -36,7 +37,7 @@ typedef struct {
struct git_filter_list {
git_array_t(git_filter_entry) filters;
git_filter_source source;
git_buf *temp_buf;
git_str *temp_buf;
char path[GIT_FLEX_ARRAY];
};
......@@ -68,7 +69,7 @@ static void git_filter_global_shutdown(void);
static int filter_def_scan_attrs(
git_buf *attrs, size_t *nattr, size_t *nmatch, const char *attr_str)
git_str *attrs, size_t *nattr, size_t *nmatch, const char *attr_str)
{
const char *start, *scan = attr_str;
int has_eq;
......@@ -92,9 +93,9 @@ static int filter_def_scan_attrs(
(*nmatch)++;
if (has_eq)
git_buf_putc(attrs, '=');
git_buf_put(attrs, start, scan - start);
git_buf_putc(attrs, '\0');
git_str_putc(attrs, '=');
git_str_put(attrs, start, scan - start);
git_str_putc(attrs, '\0');
}
}
......@@ -152,7 +153,7 @@ static int filter_registry_insert(
{
git_filter_def *fdef;
size_t nattr = 0, nmatch = 0, alloc_len;
git_buf attrs = GIT_BUF_INIT;
git_str attrs = GIT_STR_INIT;
if (filter_def_scan_attrs(&attrs, &nattr, &nmatch, filter->attributes) < 0)
return -1;
......@@ -171,7 +172,7 @@ static int filter_registry_insert(
fdef->priority = priority;
fdef->nattrs = nattr;
fdef->nmatches = nmatch;
fdef->attrdata = git_buf_detach(&attrs);
fdef->attrdata = git_str_detach(&attrs);
filter_def_set_attrs(fdef);
......@@ -710,7 +711,7 @@ size_t git_filter_list_length(const git_filter_list *fl)
struct buf_stream {
git_writestream parent;
git_buf *target;
git_str *target;
bool complete;
};
......@@ -721,7 +722,7 @@ static int buf_stream_write(
GIT_ASSERT_ARG(buf_stream);
GIT_ASSERT(buf_stream->complete == 0);
return git_buf_put(buf_stream->target, buffer, len);
return git_str_put(buf_stream->target, buffer, len);
}
static int buf_stream_close(git_writestream *s)
......@@ -740,7 +741,7 @@ static void buf_stream_free(git_writestream *s)
GIT_UNUSED(s);
}
static void buf_stream_init(struct buf_stream *writer, git_buf *target)
static void buf_stream_init(struct buf_stream *writer, git_str *target)
{
memset(writer, 0, sizeof(struct buf_stream));
......@@ -749,7 +750,7 @@ static void buf_stream_init(struct buf_stream *writer, git_buf *target)
writer->parent.free = buf_stream_free;
writer->target = target;
git_buf_clear(target);
git_str_clear(target);
}
int git_filter_list_apply_to_buffer(
......@@ -758,12 +759,18 @@ int git_filter_list_apply_to_buffer(
const char *in,
size_t in_len)
{
GIT_BUF_WRAP_PRIVATE(out, git_filter_list__apply_to_buffer, filters, in, in_len);
}
int git_filter_list__apply_to_buffer(
git_str *out,
git_filter_list *filters,
const char *in,
size_t in_len)
{
struct buf_stream writer;
int error;
if ((error = git_buf_sanitize(out)) < 0)
return error;
buf_stream_init(&writer, out);
if ((error = git_filter_list_stream_buffer(filters,
......@@ -775,23 +782,23 @@ int git_filter_list_apply_to_buffer(
}
int git_filter_list__convert_buf(
git_buf *out,
git_str *out,
git_filter_list *filters,
git_buf *in)
git_str *in)
{
int error;
if (!filters || git_filter_list_length(filters) == 0) {
git_buf_swap(out, in);
git_buf_dispose(in);
git_str_swap(out, in);
git_str_dispose(in);
return 0;
}
error = git_filter_list_apply_to_buffer(out, filters,
error = git_filter_list__apply_to_buffer(out, filters,
in->ptr, in->size);
if (!error)
git_buf_dispose(in);
git_str_dispose(in);
return error;
}
......@@ -802,6 +809,15 @@ int git_filter_list_apply_to_file(
git_repository *repo,
const char *path)
{
GIT_BUF_WRAP_PRIVATE(out, git_filter_list__apply_to_file, filters, repo, path);
}
int git_filter_list__apply_to_file(
git_str *out,
git_filter_list *filters,
git_repository *repo,
const char *path)
{
struct buf_stream writer;
int error;
......@@ -815,7 +831,7 @@ int git_filter_list_apply_to_file(
return error;
}
static int buf_from_blob(git_buf *out, git_blob *blob)
static int buf_from_blob(git_str *out, git_blob *blob)
{
git_object_size_t rawsize = git_blob_rawsize(blob);
......@@ -824,7 +840,7 @@ static int buf_from_blob(git_buf *out, git_blob *blob)
return -1;
}
git_buf_attach_notowned(out, git_blob_rawcontent(blob), (size_t)rawsize);
git_str_attach_notowned(out, git_blob_rawcontent(blob), (size_t)rawsize);
return 0;
}
......@@ -833,6 +849,14 @@ int git_filter_list_apply_to_blob(
git_filter_list *filters,
git_blob *blob)
{
GIT_BUF_WRAP_PRIVATE(out, git_filter_list__apply_to_blob, filters, blob);
}
int git_filter_list__apply_to_blob(
git_str *out,
git_filter_list *filters,
git_blob *blob)
{
struct buf_stream writer;
int error;
......@@ -849,12 +873,13 @@ int git_filter_list_apply_to_blob(
struct buffered_stream {
git_writestream parent;
git_filter *filter;
int (*write_fn)(git_filter *, void **, git_buf *, const git_buf *, const git_filter_source *);
int (*write_fn)(git_filter *, void **, git_str *, const git_str *, const git_filter_source *);
int (*legacy_write_fn)(git_filter *, void **, git_buf *, const git_buf *, const git_filter_source *);
const git_filter_source *source;
void **payload;
git_buf input;
git_buf temp_buf;
git_buf *output;
git_str input;
git_str temp_buf;
git_str *output;
git_writestream *target;
};
......@@ -864,13 +889,13 @@ static int buffered_stream_write(
struct buffered_stream *buffered_stream = (struct buffered_stream *)s;
GIT_ASSERT_ARG(buffered_stream);
return git_buf_put(&buffered_stream->input, buffer, len);
return git_str_put(&buffered_stream->input, buffer, len);
}
static int buffered_stream_close(git_writestream *s)
{
struct buffered_stream *buffered_stream = (struct buffered_stream *)s;
git_buf *writebuf;
git_str *writebuf;
git_error_state error_state = {0};
int error;
......@@ -886,9 +911,6 @@ static int buffered_stream_close(git_writestream *s)
if (error == GIT_PASSTHROUGH) {
writebuf = &buffered_stream->input;
} else if (error == 0) {
if ((error = git_buf_sanitize(buffered_stream->output)) < 0)
return error;
writebuf = buffered_stream->output;
} else {
/* close stream before erroring out taking care
......@@ -911,8 +933,8 @@ static void buffered_stream_free(git_writestream *s)
struct buffered_stream *buffered_stream = (struct buffered_stream *)s;
if (buffered_stream) {
git_buf_dispose(&buffered_stream->input);
git_buf_dispose(&buffered_stream->temp_buf);
git_str_dispose(&buffered_stream->input);
git_str_dispose(&buffered_stream->temp_buf);
git__free(buffered_stream);
}
}
......@@ -920,8 +942,8 @@ static void buffered_stream_free(git_writestream *s)
int git_filter_buffered_stream_new(
git_writestream **out,
git_filter *filter,
int (*write_fn)(git_filter *, void **, git_buf *, const git_buf *, const git_filter_source *),
git_buf *temp_buf,
int (*write_fn)(git_filter *, void **, git_str *, const git_str *, const git_filter_source *),
git_str *temp_buf,
void **payload,
const git_filter_source *source,
git_writestream *target)
......@@ -940,12 +962,43 @@ int git_filter_buffered_stream_new(
buffered_stream->target = target;
if (temp_buf)
git_buf_clear(temp_buf);
git_str_clear(temp_buf);
*out = (git_writestream *)buffered_stream;
return 0;
}
#ifndef GIT_DEPRECATE_HARD
static int buffered_legacy_stream_new(
git_writestream **out,
git_filter *filter,
int (*legacy_write_fn)(git_filter *, void **, git_buf *, const git_buf *, const git_filter_source *),
git_str *temp_buf,
void **payload,
const git_filter_source *source,
git_writestream *target)
{
struct buffered_stream *buffered_stream = git__calloc(1, sizeof(struct buffered_stream));
GIT_ERROR_CHECK_ALLOC(buffered_stream);
buffered_stream->parent.write = buffered_stream_write;
buffered_stream->parent.close = buffered_stream_close;
buffered_stream->parent.free = buffered_stream_free;
buffered_stream->filter = filter;
buffered_stream->legacy_write_fn = legacy_write_fn;
buffered_stream->output = temp_buf ? temp_buf : &buffered_stream->temp_buf;
buffered_stream->payload = payload;
buffered_stream->source = source;
buffered_stream->target = target;
if (temp_buf)
git_str_clear(temp_buf);
*out = (git_writestream *)buffered_stream;
return 0;
}
#endif
static int setup_stream(
git_writestream **out,
git_filter_entry *fe,
......@@ -961,7 +1014,7 @@ static int setup_stream(
*/
if (!fe->filter->stream) {
/* Create a stream that proxies the one-shot apply */
return git_filter_buffered_stream_new(out,
return buffered_legacy_stream_new(out,
fe->filter, fe->filter->apply, filters->temp_buf,
&fe->payload, &filters->source, last_stream);
}
......@@ -1032,7 +1085,7 @@ int git_filter_list_stream_file(
git_writestream *target)
{
char buf[FILTERIO_BUFSIZE];
git_buf abspath = GIT_BUF_INIT;
git_str abspath = GIT_STR_INIT;
const char *base = repo ? git_repository_workdir(repo) : NULL;
git_vector filter_streams = GIT_VECTOR_INIT;
git_writestream *stream_start;
......@@ -1067,7 +1120,7 @@ done:
if (fd >= 0)
p_close(fd);
filter_streams_free(&filter_streams);
git_buf_dispose(&abspath);
git_str_dispose(&abspath);
return error;
}
......@@ -1101,7 +1154,7 @@ int git_filter_list_stream_blob(
git_blob *blob,
git_writestream *target)
{
git_buf in = GIT_BUF_INIT;
git_str in = GIT_STR_INIT;
if (buf_from_blob(&in, blob) < 0)
return -1;
......@@ -1125,22 +1178,12 @@ int git_filter_list_stream_data(
git_buf *data,
git_writestream *target)
{
int error;
if ((error = git_buf_sanitize(data)) < 0)
return error;
return git_filter_list_stream_buffer(filters, data->ptr, data->size, target);
}
int git_filter_list_apply_to_data(
git_buf *tgt, git_filter_list *filters, git_buf *src)
{
int error;
if ((error = git_buf_sanitize(src)) < 0)
return error;
return git_filter_list_apply_to_buffer(tgt, filters, src->ptr, src->size);
}
......
......@@ -19,7 +19,7 @@
typedef struct {
git_filter_options options;
git_attr_session *attr_session;
git_buf *temp_buf;
git_str *temp_buf;
} git_filter_session;
#define GIT_FILTER_SESSION_INIT {GIT_FILTER_OPTIONS_INIT, 0}
......@@ -36,14 +36,35 @@ extern int git_filter_list__load(
git_filter_mode_t mode,
git_filter_session *filter_session);
int git_filter_list__apply_to_buffer(
git_str *out,
git_filter_list *filters,
const char *in,
size_t in_len);
int git_filter_list__apply_to_file(
git_str *out,
git_filter_list *filters,
git_repository *repo,
const char *path);
int git_filter_list__apply_to_blob(
git_str *out,
git_filter_list *filters,
git_blob *blob);
/*
* The given input buffer will be converted to the given output buffer.
* The input buffer will be freed (_if_ it was allocated).
*/
extern int git_filter_list__convert_buf(
git_buf *out,
git_str *out,
git_filter_list *filters,
git_str *in);
extern int git_filter_list__apply_to_file(
git_str *out,
git_filter_list *filters,
git_buf *in);
git_repository *repo,
const char *path);
/*
* Available filters
......@@ -55,8 +76,8 @@ extern git_filter *git_ident_filter_new(void);
extern int git_filter_buffered_stream_new(
git_writestream **out,
git_filter *filter,
int (*write_fn)(git_filter *, void **, git_buf *, const git_buf *, const git_filter_source *),
git_buf *temp_buf,
int (*write_fn)(git_filter *, void **, git_str *, const git_str *, const git_filter_source *),
git_str *temp_buf,
void **payload,
const git_filter_source *source,
git_writestream *target);
......
This diff is collapsed. Click to expand it.
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