mirror of
https://git.proxmox.com/git/libgit2
synced 2025-06-18 21:35:20 +00:00
Merge git_buf and git_buffer
This makes the git_buf struct that was used internally into an externally available structure and eliminates the git_buffer. As part of that, some of the special cases that arose with the externally used git_buffer were blended into the git_buf, such as being careful about git_buf objects that may have a NULL ptr and allowing for bufs with a valid ptr and size but zero asize as a way of referring to externally owned data.
This commit is contained in:
parent
4b11f25a4f
commit
a9f51e430f
@ -104,17 +104,17 @@ GIT_EXTERN(git_off_t) git_blob_rawsize(const git_blob *blob);
|
||||
* CRLF filtering or other types of changes depending on the file
|
||||
* attributes set for the blob and the content detected in it.
|
||||
*
|
||||
* The output is written into a `git_buffer` which the caller must free
|
||||
* when done (via `git_buffer_free`).
|
||||
* The output is written into a `git_buf` which the caller must free
|
||||
* when done (via `git_buf_free`).
|
||||
*
|
||||
* If no filters need to be applied, then the `out` buffer will just be
|
||||
* populated with a pointer to the raw content of the blob. In that case,
|
||||
* be careful to *not* free the blob until done with the buffer. To keep
|
||||
* the data detached from the blob, call `git_buffer_resize` on the buffer
|
||||
* the data detached from the blob, call `git_buf_grow` on the buffer
|
||||
* with a `want_size` of 0 and the buffer will be reallocated to be
|
||||
* detached from the blob.
|
||||
*
|
||||
* @param out The git_buffer to be filled in
|
||||
* @param out The git_buf to be filled in
|
||||
* @param blob Pointer to the blob
|
||||
* @param as_path Path used for file attribute lookups, etc.
|
||||
* @param check_for_binary_data Should this test if blob content contains
|
||||
@ -122,7 +122,7 @@ GIT_EXTERN(git_off_t) git_blob_rawsize(const git_blob *blob);
|
||||
* @return 0 on success or an error code
|
||||
*/
|
||||
GIT_EXTERN(int) git_blob_filtered_content(
|
||||
git_buffer *out,
|
||||
git_buf *out,
|
||||
git_blob *blob,
|
||||
const char *as_path,
|
||||
int check_for_binary_data);
|
||||
|
@ -4,8 +4,8 @@
|
||||
* 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_git_buffer_h__
|
||||
#define INCLUDE_git_buffer_h__
|
||||
#ifndef INCLUDE_git_buf_h__
|
||||
#define INCLUDE_git_buf_h__
|
||||
|
||||
#include "common.h"
|
||||
|
||||
@ -25,59 +25,69 @@ GIT_BEGIN_DECL
|
||||
* 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 instead fill in a `git_buffer` and the caller can use
|
||||
* `git_buffer_free()` to release it when they are done.
|
||||
* will fill in a `git_buf` and the caller can use `git_buf_free()` to
|
||||
* release it when they are done.
|
||||
*
|
||||
* * `ptr` refers to the start of the allocated memory.
|
||||
* * `size` contains the size of the data in `ptr` that is actually used.
|
||||
* * `available` refers to the known total amount of allocated memory. It
|
||||
* may be larger than the `size` actually in use.
|
||||
* 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.
|
||||
*
|
||||
* In a few cases, for uniformity and simplicity, an API may populate a
|
||||
* `git_buffer` with data that should *not* be freed (i.e. the lifetime of
|
||||
* the data buffer is actually tied to another libgit2 object). These
|
||||
* cases will be clearly documented in the APIs that use the `git_buffer`.
|
||||
* In those cases, the `available` field will be set to zero even though
|
||||
* the `ptr` and `size` will be valid.
|
||||
* A `git_buf` is a public structure with three fields:
|
||||
*
|
||||
* - `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.
|
||||
*
|
||||
* - `size` holds the size (in bytes) of the data that is actually used.
|
||||
*
|
||||
* - `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.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
typedef struct git_buffer {
|
||||
typedef struct {
|
||||
char *ptr;
|
||||
size_t size;
|
||||
size_t available;
|
||||
} git_buffer;
|
||||
size_t asize, size;
|
||||
} git_buf;
|
||||
|
||||
/**
|
||||
* Use to initialize buffer structure when git_buffer is on stack
|
||||
*/
|
||||
#define GIT_BUFFER_INIT { NULL, 0, 0 }
|
||||
|
||||
/**
|
||||
* Free the memory referred to by the git_buffer.
|
||||
* Free the memory referred to by the git_buf.
|
||||
*
|
||||
* Note that this does not free the `git_buffer` itself, just the memory
|
||||
* pointed to by `buffer->ptr`. If that memory was not allocated by
|
||||
* libgit2 itself, be careful with using this function because it could
|
||||
* cause problems.
|
||||
* 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.
|
||||
*
|
||||
* @param buffer The buffer with allocated memory
|
||||
* @param buffer The buffer to deallocate
|
||||
*/
|
||||
GIT_EXTERN(void) git_buffer_free(git_buffer *buffer);
|
||||
GIT_EXTERN(void) git_buf_free(git_buf *buffer);
|
||||
|
||||
/**
|
||||
* Resize the buffer allocation to make more space.
|
||||
*
|
||||
* This will update `buffer->available` with the new size (which will be
|
||||
* at least `want_size` and may be larger). This may or may not change
|
||||
* `buffer->ptr` depending on whether there is an existing allocation and
|
||||
* whether that allocation can be increased in place.
|
||||
* This will attempt to grow the buffer to accomodate the target size.
|
||||
*
|
||||
* Currently, this will never shrink the buffer, only expand it.
|
||||
* 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 want_size The desired available size
|
||||
* @return 0 on success, negative error code on allocation failure
|
||||
* @param target_size The desired available size
|
||||
* @return 0 on success, -1 on allocation failure
|
||||
*/
|
||||
GIT_EXTERN(int) git_buffer_resize(git_buffer *buffer, size_t want_size);
|
||||
GIT_EXTERN(int) git_buf_grow(git_buf *buffer, size_t target_size);
|
||||
|
||||
/**
|
||||
* Set buffer to a copy of some raw data.
|
||||
@ -85,10 +95,10 @@ GIT_EXTERN(int) git_buffer_resize(git_buffer *buffer, size_t want_size);
|
||||
* @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, negative error code on allocation failure
|
||||
* @return 0 on success, -1 on allocation failure
|
||||
*/
|
||||
GIT_EXTERN(int) git_buffer_copy(
|
||||
git_buffer *buffer, const void *data, size_t datalen);
|
||||
GIT_EXTERN(int) git_buf_set(
|
||||
git_buf *buffer, const void *data, size_t datalen);
|
||||
|
||||
GIT_END_DECL
|
||||
|
||||
|
@ -88,16 +88,17 @@ GIT_EXTERN(int) git_filter_list_load(
|
||||
/**
|
||||
* Apply filter list to a data buffer.
|
||||
*
|
||||
* See `git2/buffer.h` for background on `git_buffer` objects.
|
||||
* See `git2/buffer.h` for background on `git_buf` objects.
|
||||
*
|
||||
* If the `in` buffer refers to data managed by libgit2
|
||||
* (i.e. `in->available` is not zero), then it will be overwritten when
|
||||
* applying the filters. If not, then it will be left untouched.
|
||||
* If the `in` buffer holds data allocated by libgit2 (i.e. `in->asize` is
|
||||
* not zero), then it will be overwritten when applying the filters. If
|
||||
* not, then it will be left untouched.
|
||||
*
|
||||
* If there are no filters to apply (or `filters` is NULL), then the `out`
|
||||
* buffer will reference the `in` buffer data (with `available` set to
|
||||
* zero) instead of allocating data. This keeps allocations to a minimum,
|
||||
* but it means you have to be careful about freeing the `in` data.
|
||||
* buffer will reference the `in` buffer data (with `asize` set to zero)
|
||||
* instead of allocating data. This keeps allocations to a minimum, but
|
||||
* it means you have to be careful about freeing the `in` data since `out`
|
||||
* may be pointing to it!
|
||||
*
|
||||
* @param out Buffer to store the result of the filtering
|
||||
* @param filters A loaded git_filter_list (or NULL)
|
||||
@ -105,15 +106,15 @@ GIT_EXTERN(int) git_filter_list_load(
|
||||
* @return 0 on success, an error code otherwise
|
||||
*/
|
||||
GIT_EXTERN(int) git_filter_list_apply_to_data(
|
||||
git_buffer *out,
|
||||
git_buf *out,
|
||||
git_filter_list *filters,
|
||||
git_buffer *in);
|
||||
git_buf *in);
|
||||
|
||||
/**
|
||||
* Apply filter list to the contents of a file on disk
|
||||
*/
|
||||
GIT_EXTERN(int) git_filter_list_apply_to_file(
|
||||
git_buffer *out,
|
||||
git_buf *out,
|
||||
git_filter_list *filters,
|
||||
git_repository *repo,
|
||||
const char *path);
|
||||
@ -122,7 +123,7 @@ GIT_EXTERN(int) git_filter_list_apply_to_file(
|
||||
* Apply filter list to the contents of a blob
|
||||
*/
|
||||
GIT_EXTERN(int) git_filter_list_apply_to_blob(
|
||||
git_buffer *out,
|
||||
git_buf *out,
|
||||
git_filter_list *filters,
|
||||
git_blob *blob);
|
||||
|
||||
|
@ -4,8 +4,8 @@
|
||||
* 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_sys_git_config_backend_h__
|
||||
#define INCLUDE_sys_git_config_backend_h__
|
||||
#ifndef INCLUDE_sys_git_filter_h__
|
||||
#define INCLUDE_sys_git_filter_h__
|
||||
|
||||
#include "git2/filter.h"
|
||||
|
||||
@ -117,19 +117,19 @@ typedef void (*git_filter_shutdown_fn)(git_filter *self);
|
||||
* Callback to decide if a given source needs this filter
|
||||
*/
|
||||
typedef int (*git_filter_check_fn)(
|
||||
git_filter *self,
|
||||
void **payload, /* points to NULL ptr on entry, may be set */
|
||||
git_filter *self,
|
||||
void **payload, /* points to NULL ptr on entry, may be set */
|
||||
const git_filter_source *src,
|
||||
const char **attr_values);
|
||||
const char **attr_values);
|
||||
|
||||
/**
|
||||
* Callback to actually perform the data filtering
|
||||
*/
|
||||
typedef int (*git_filter_apply_fn)(
|
||||
git_filter *self,
|
||||
void **payload, /* may be read and/or set */
|
||||
git_buffer *to,
|
||||
const git_buffer *from,
|
||||
git_filter *self,
|
||||
void **payload, /* may be read and/or set */
|
||||
git_buf *to,
|
||||
const git_buf *from,
|
||||
const git_filter_source *src);
|
||||
|
||||
/**
|
||||
|
@ -111,7 +111,7 @@ static int write_file_filtered(
|
||||
git_filter_list *fl)
|
||||
{
|
||||
int error;
|
||||
git_buffer tgt = GIT_BUFFER_INIT;
|
||||
git_buf tgt = GIT_BUF_INIT;
|
||||
|
||||
error = git_filter_list_apply_to_file(&tgt, fl, NULL, full_path);
|
||||
|
||||
@ -122,7 +122,7 @@ static int write_file_filtered(
|
||||
error = git_odb_write(oid, odb, tgt.ptr, tgt.size, GIT_OBJ_BLOB);
|
||||
}
|
||||
|
||||
git_buffer_free(&tgt);
|
||||
git_buf_free(&tgt);
|
||||
return error;
|
||||
}
|
||||
|
||||
@ -329,7 +329,7 @@ int git_blob_is_binary(git_blob *blob)
|
||||
}
|
||||
|
||||
int git_blob_filtered_content(
|
||||
git_buffer *out,
|
||||
git_buf *out,
|
||||
git_blob *blob,
|
||||
const char *path,
|
||||
int check_for_binary_data)
|
||||
|
93
src/buffer.c
93
src/buffer.c
@ -32,7 +32,8 @@ void git_buf_init(git_buf *buf, size_t initial_size)
|
||||
git_buf_grow(buf, initial_size);
|
||||
}
|
||||
|
||||
int git_buf_try_grow(git_buf *buf, size_t target_size, bool mark_oom)
|
||||
int git_buf_try_grow(
|
||||
git_buf *buf, size_t target_size, bool mark_oom, bool preserve_external)
|
||||
{
|
||||
char *new_ptr;
|
||||
size_t new_size;
|
||||
@ -40,6 +41,9 @@ int git_buf_try_grow(git_buf *buf, size_t target_size, bool mark_oom)
|
||||
if (buf->ptr == git_buf__oom)
|
||||
return -1;
|
||||
|
||||
if (!target_size)
|
||||
target_size = buf->size;
|
||||
|
||||
if (target_size <= buf->asize)
|
||||
return 0;
|
||||
|
||||
@ -67,6 +71,9 @@ int git_buf_try_grow(git_buf *buf, size_t target_size, bool mark_oom)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (preserve_external && !buf->asize && buf->ptr != NULL && buf->size > 0)
|
||||
memcpy(new_ptr, buf->ptr, min(buf->size, new_size));
|
||||
|
||||
buf->asize = new_size;
|
||||
buf->ptr = new_ptr;
|
||||
|
||||
@ -78,11 +85,16 @@ int git_buf_try_grow(git_buf *buf, size_t target_size, bool mark_oom)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int git_buf_grow(git_buf *buffer, size_t target_size)
|
||||
{
|
||||
return git_buf_try_grow(buffer, target_size, true, true);
|
||||
}
|
||||
|
||||
void git_buf_free(git_buf *buf)
|
||||
{
|
||||
if (!buf) return;
|
||||
|
||||
if (buf->ptr != git_buf__initbuf && buf->ptr != git_buf__oom)
|
||||
if (buf->asize > 0 && buf->ptr != NULL && buf->ptr != git_buf__oom)
|
||||
git__free(buf->ptr);
|
||||
|
||||
git_buf_init(buf, 0);
|
||||
@ -91,11 +103,15 @@ void git_buf_free(git_buf *buf)
|
||||
void git_buf_clear(git_buf *buf)
|
||||
{
|
||||
buf->size = 0;
|
||||
|
||||
if (!buf->ptr)
|
||||
buf->ptr = git_buf__initbuf;
|
||||
|
||||
if (buf->asize > 0)
|
||||
buf->ptr[0] = '\0';
|
||||
}
|
||||
|
||||
int git_buf_set(git_buf *buf, const char *data, size_t len)
|
||||
int git_buf_set(git_buf *buf, const void *data, size_t len)
|
||||
{
|
||||
if (len == 0 || data == NULL) {
|
||||
git_buf_clear(buf);
|
||||
@ -485,74 +501,3 @@ int git_buf_splice(
|
||||
buf->ptr[buf->size] = '\0';
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Public buffers API
|
||||
*/
|
||||
|
||||
void git_buffer_free(git_buffer *buffer)
|
||||
{
|
||||
if (!buffer)
|
||||
return;
|
||||
|
||||
if (buffer->ptr != NULL && buffer->available > 0)
|
||||
git__free(buffer->ptr);
|
||||
|
||||
git__memzero(buffer, sizeof(*buffer));
|
||||
}
|
||||
|
||||
static int git_buffer__resize(
|
||||
git_buffer *buffer, size_t want_size, int preserve_data)
|
||||
{
|
||||
int non_allocated_buffer = 0;
|
||||
char *new_ptr;
|
||||
|
||||
assert(buffer);
|
||||
|
||||
/* check if buffer->ptr points to memory owned elsewhere */
|
||||
non_allocated_buffer = (buffer->ptr != NULL && buffer->available == 0);
|
||||
|
||||
if (non_allocated_buffer && !want_size)
|
||||
want_size = buffer->size;
|
||||
|
||||
if (buffer->available >= want_size)
|
||||
return 0;
|
||||
|
||||
if (non_allocated_buffer) {
|
||||
new_ptr = NULL;
|
||||
if (want_size < buffer->size)
|
||||
want_size = buffer->size;
|
||||
} else {
|
||||
new_ptr = buffer->ptr;
|
||||
}
|
||||
|
||||
want_size = (want_size + 7) & ~7; /* round up to multiple of 8 */
|
||||
|
||||
new_ptr = git__realloc(new_ptr, want_size);
|
||||
GITERR_CHECK_ALLOC(new_ptr);
|
||||
|
||||
if (non_allocated_buffer && preserve_data)
|
||||
memcpy(new_ptr, buffer->ptr, buffer->size);
|
||||
|
||||
buffer->ptr = new_ptr;
|
||||
buffer->available = want_size;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int git_buffer_resize(git_buffer *buffer, size_t want_size)
|
||||
{
|
||||
return git_buffer__resize(buffer, want_size, true);
|
||||
}
|
||||
|
||||
int git_buffer_copy(
|
||||
git_buffer *buffer, const void *data, size_t datalen)
|
||||
{
|
||||
if (git_buffer__resize(buffer, datalen + 1, false) < 0)
|
||||
return -1;
|
||||
memcpy(buffer->ptr, data, datalen);
|
||||
buffer->ptr[datalen] = '\0';
|
||||
buffer->size = datalen;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
65
src/buffer.h
65
src/buffer.h
@ -12,16 +12,23 @@
|
||||
#include "git2/buffer.h"
|
||||
#include <stdarg.h>
|
||||
|
||||
typedef struct {
|
||||
char *ptr;
|
||||
size_t asize, size;
|
||||
} git_buf;
|
||||
/* typedef struct {
|
||||
* char *ptr;
|
||||
* size_t asize, size;
|
||||
* } git_buf;
|
||||
*/
|
||||
|
||||
extern char git_buf__initbuf[];
|
||||
extern char git_buf__oom[];
|
||||
|
||||
/* Use to initialize buffer structure when git_buf is on stack */
|
||||
#define GIT_BUF_INIT { git_buf__initbuf, 0, 0 }
|
||||
|
||||
GIT_INLINE(bool) git_buf_is_allocated(const git_buf *buf)
|
||||
{
|
||||
return (buf->ptr != NULL && buf->asize > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize a git_buf structure.
|
||||
*
|
||||
@ -33,27 +40,16 @@ extern void git_buf_init(git_buf *buf, size_t initial_size);
|
||||
/**
|
||||
* Attempt to grow the buffer to hold at least `target_size` bytes.
|
||||
*
|
||||
* If the allocation fails, this will return an error. If mark_oom is true,
|
||||
* If the allocation fails, this will return an error. If `mark_oom` is true,
|
||||
* this will mark the buffer as invalid for future operations; if false,
|
||||
* existing buffer content will be preserved, but calling code must handle
|
||||
* that buffer was not expanded.
|
||||
* that buffer was not expanded. If `preserve_external` is true, then any
|
||||
* existing data pointed to be `ptr` even if `asize` is zero will be copied
|
||||
* into the newly allocated buffer.
|
||||
*/
|
||||
extern int git_buf_try_grow(git_buf *buf, size_t target_size, bool mark_oom);
|
||||
extern int git_buf_try_grow(
|
||||
git_buf *buf, size_t target_size, bool mark_oom, bool preserve_external);
|
||||
|
||||
/**
|
||||
* Grow the buffer to hold at least `target_size` bytes.
|
||||
*
|
||||
* If the allocation fails, this will return an error and the buffer will be
|
||||
* marked as invalid for future operations, invaliding contents.
|
||||
*
|
||||
* @return 0 on success or -1 on failure
|
||||
*/
|
||||
GIT_INLINE(int) git_buf_grow(git_buf *buf, size_t target_size)
|
||||
{
|
||||
return git_buf_try_grow(buf, target_size, true);
|
||||
}
|
||||
|
||||
extern void git_buf_free(git_buf *buf);
|
||||
extern void git_buf_swap(git_buf *buf_a, git_buf *buf_b);
|
||||
extern char *git_buf_detach(git_buf *buf);
|
||||
extern void git_buf_attach(git_buf *buf, char *ptr, size_t asize);
|
||||
@ -82,7 +78,6 @@ GIT_INLINE(bool) git_buf_oom(const git_buf *buf)
|
||||
* return code of these functions and call them in a series then just call
|
||||
* git_buf_oom at the end.
|
||||
*/
|
||||
int git_buf_set(git_buf *buf, const char *data, size_t len);
|
||||
int git_buf_sets(git_buf *buf, const char *string);
|
||||
int git_buf_putc(git_buf *buf, char c);
|
||||
int git_buf_put(git_buf *buf, const char *data, size_t len);
|
||||
@ -175,30 +170,4 @@ int git_buf_splice(
|
||||
const char *data,
|
||||
size_t nb_to_insert);
|
||||
|
||||
|
||||
GIT_INLINE(bool) git_buffer_is_allocated(const git_buffer *buffer)
|
||||
{
|
||||
return (buffer->ptr != NULL && buffer->available > 0);
|
||||
}
|
||||
|
||||
#define GIT_BUF_FROM_BUFFER(buffer) \
|
||||
{ (buffer)->ptr, (buffer)->available, (buffer)->size }
|
||||
|
||||
GIT_INLINE(void) git_buf_from_buffer(git_buf *buf, const git_buffer *buffer)
|
||||
{
|
||||
buf->ptr = buffer->ptr;
|
||||
buf->size = buffer->size;
|
||||
buf->asize = buffer->available;
|
||||
}
|
||||
|
||||
#define GIT_BUFFER_FROM_BUF(buf) \
|
||||
{ (buf)->ptr, (buf)->size, (buf)->asize }
|
||||
|
||||
GIT_INLINE(void) git_buffer_from_buf(git_buffer *buffer, const git_buf *buf)
|
||||
{
|
||||
buffer->ptr = buf->ptr;
|
||||
buffer->size = buf->size;
|
||||
buffer->available = buf->asize;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -678,20 +678,19 @@ fail:
|
||||
|
||||
static int buffer_to_file(
|
||||
struct stat *st,
|
||||
git_buffer *buffer,
|
||||
git_buf *buf,
|
||||
const char *path,
|
||||
mode_t dir_mode,
|
||||
int file_open_flags,
|
||||
mode_t file_mode)
|
||||
{
|
||||
int error;
|
||||
git_buf buf = GIT_BUF_FROM_BUFFER(buffer);
|
||||
|
||||
if ((error = git_futils_mkpath2file(path, dir_mode)) < 0)
|
||||
return error;
|
||||
|
||||
if ((error = git_futils_writebuffer(
|
||||
&buf, path, file_open_flags, file_mode)) < 0)
|
||||
buf, path, file_open_flags, file_mode)) < 0)
|
||||
return error;
|
||||
|
||||
if (st != NULL && (error = p_stat(path, st)) < 0)
|
||||
@ -713,7 +712,7 @@ static int blob_content_to_file(
|
||||
{
|
||||
int error = 0;
|
||||
mode_t file_mode = opts->file_mode ? opts->file_mode : entry_filemode;
|
||||
git_buffer out = GIT_BUFFER_INIT;
|
||||
git_buf out = GIT_BUF_INIT;
|
||||
git_filter_list *fl = NULL;
|
||||
|
||||
if (!opts->disable_filters && !git_blob_is_binary(blob))
|
||||
@ -731,7 +730,7 @@ static int blob_content_to_file(
|
||||
|
||||
st->st_mode = entry_filemode;
|
||||
|
||||
git_buffer_free(&out);
|
||||
git_buf_free(&out);
|
||||
}
|
||||
|
||||
return error;
|
||||
|
@ -293,6 +293,8 @@ static int config_iterator_new(
|
||||
diskfile_backend *b = (diskfile_backend *)backend;
|
||||
git_config_file_iter *it = git__calloc(1, sizeof(git_config_file_iter));
|
||||
|
||||
GIT_UNUSED(b);
|
||||
|
||||
GITERR_CHECK_ALLOC(it);
|
||||
|
||||
it->parent.backend = backend;
|
||||
|
42
src/crlf.c
42
src/crlf.c
@ -119,15 +119,12 @@ static int has_cr_in_index(const git_filter_source *src)
|
||||
|
||||
static int crlf_apply_to_odb(
|
||||
struct crlf_attrs *ca,
|
||||
git_buffer *to,
|
||||
const git_buffer *from,
|
||||
git_buf *to,
|
||||
const git_buf *from,
|
||||
const git_filter_source *src)
|
||||
{
|
||||
const git_buf from_buf = GIT_BUF_FROM_BUFFER(from);
|
||||
git_buf to_buf = GIT_BUF_FROM_BUFFER(to);
|
||||
|
||||
/* Empty file? Nothing to do */
|
||||
if (!git_buf_len(&from_buf))
|
||||
if (!git_buf_len(from))
|
||||
return 0;
|
||||
|
||||
/* Heuristics to see if we can skip the conversion.
|
||||
@ -137,7 +134,7 @@ static int crlf_apply_to_odb(
|
||||
git_buf_text_stats stats;
|
||||
|
||||
/* Check heuristics for binary vs text... */
|
||||
if (git_buf_text_gather_stats(&stats, &from_buf, false))
|
||||
if (git_buf_text_gather_stats(&stats, from, false))
|
||||
return -1;
|
||||
|
||||
/*
|
||||
@ -162,13 +159,7 @@ static int crlf_apply_to_odb(
|
||||
}
|
||||
|
||||
/* Actually drop the carriage returns */
|
||||
if (git_buf_text_crlf_to_lf(&to_buf, &from_buf) < 0)
|
||||
return -1;
|
||||
|
||||
/* Overwrite "to" buffer in case data was resized */
|
||||
git_buffer_from_buf(to, &to_buf);
|
||||
|
||||
return 0;
|
||||
return git_buf_text_crlf_to_lf(to, from);
|
||||
}
|
||||
|
||||
static const char *line_ending(struct crlf_attrs *ca)
|
||||
@ -210,14 +201,12 @@ line_ending_error:
|
||||
}
|
||||
|
||||
static int crlf_apply_to_workdir(
|
||||
struct crlf_attrs *ca, git_buffer *to, const git_buffer *from)
|
||||
struct crlf_attrs *ca, git_buf *to, const git_buf *from)
|
||||
{
|
||||
const git_buf from_buf = GIT_BUF_FROM_BUFFER(from);
|
||||
git_buf to_buf = GIT_BUF_FROM_BUFFER(to);
|
||||
const char *workdir_ending = NULL;
|
||||
|
||||
/* Empty file? Nothing to do. */
|
||||
if (git_buf_len(&from_buf) == 0)
|
||||
if (git_buf_len(from) == 0)
|
||||
return 0;
|
||||
|
||||
/* Determine proper line ending */
|
||||
@ -229,22 +218,19 @@ static int crlf_apply_to_workdir(
|
||||
if (ca->crlf_action == GIT_CRLF_GUESS && ca->auto_crlf)
|
||||
return GIT_ENOTFOUND;
|
||||
|
||||
if (git_buf_find(&from_buf, '\r') < 0)
|
||||
if (git_buf_find(from, '\r') < 0)
|
||||
return GIT_ENOTFOUND;
|
||||
|
||||
if (git_buf_text_crlf_to_lf(&to_buf, &from_buf) < 0)
|
||||
if (git_buf_text_crlf_to_lf(to, from) < 0)
|
||||
return -1;
|
||||
} else {
|
||||
/* only other supported option is lf->crlf conversion */
|
||||
assert(!strcmp("\r\n", workdir_ending));
|
||||
|
||||
if (git_buf_text_lf_to_crlf(&to_buf, &from_buf) < 0)
|
||||
if (git_buf_text_lf_to_crlf(to, from) < 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Overwrite "to" buffer in case data was resized */
|
||||
git_buffer_from_buf(to, &to_buf);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -297,10 +283,10 @@ static int crlf_check(
|
||||
}
|
||||
|
||||
static int crlf_apply(
|
||||
git_filter *self,
|
||||
void **payload, /* may be read and/or set */
|
||||
git_buffer *to,
|
||||
const git_buffer *from,
|
||||
git_filter *self,
|
||||
void **payload, /* may be read and/or set */
|
||||
git_buf *to,
|
||||
const git_buf *from,
|
||||
const git_filter_source *src)
|
||||
{
|
||||
/* initialize payload in case `check` was bypassed */
|
||||
|
@ -327,11 +327,11 @@ static int diff_file_content_load_workdir_file(
|
||||
}
|
||||
|
||||
if (!(error = git_futils_readbuffer_fd(&raw, fd, (size_t)fc->file->size))) {
|
||||
git_buffer in = GIT_BUFFER_FROM_BUF(&raw), out = GIT_BUFFER_INIT;
|
||||
git_buf out = GIT_BUF_INIT;
|
||||
|
||||
error = git_filter_list_apply_to_data(&out, fl, &in);
|
||||
error = git_filter_list_apply_to_data(&out, fl, &raw);
|
||||
|
||||
git_buffer_free(&in);
|
||||
git_buf_free(&raw);
|
||||
|
||||
if (!error) {
|
||||
fc->map.len = out.size;
|
||||
|
48
src/filter.c
48
src/filter.c
@ -549,23 +549,28 @@ int git_filter_list_push(
|
||||
}
|
||||
|
||||
static int filter_list_out_buffer_from_raw(
|
||||
git_buffer *out, const void *ptr, size_t size)
|
||||
git_buf *out, const void *ptr, size_t size)
|
||||
{
|
||||
if (git_buffer_is_allocated(out))
|
||||
git_buffer_free(out);
|
||||
if (git_buf_is_allocated(out))
|
||||
git_buf_free(out);
|
||||
|
||||
if (!size) {
|
||||
git_buf_init(out, 0);
|
||||
} else {
|
||||
out->ptr = (char *)ptr;
|
||||
out->asize = 0;
|
||||
out->size = size;
|
||||
}
|
||||
|
||||
out->ptr = (char *)ptr;
|
||||
out->size = size;
|
||||
out->available = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int git_filter_list_apply_to_data(
|
||||
git_buffer *tgt, git_filter_list *fl, git_buffer *src)
|
||||
git_buf *tgt, git_filter_list *fl, git_buf *src)
|
||||
{
|
||||
int error = 0;
|
||||
uint32_t i;
|
||||
git_buffer *dbuffer[2], local = GIT_BUFFER_INIT;
|
||||
git_buf *dbuffer[2], local = GIT_BUF_INIT;
|
||||
unsigned int si = 0;
|
||||
|
||||
if (!fl)
|
||||
@ -575,8 +580,8 @@ int git_filter_list_apply_to_data(
|
||||
dbuffer[1] = tgt;
|
||||
|
||||
/* if `src` buffer is reallocable, then use it, otherwise copy it */
|
||||
if (!git_buffer_is_allocated(src)) {
|
||||
if (git_buffer_copy(&local, src->ptr, src->size) < 0)
|
||||
if (!git_buf_is_allocated(src)) {
|
||||
if (git_buf_set(&local, src->ptr, src->size) < 0)
|
||||
return -1;
|
||||
dbuffer[0] = &local;
|
||||
}
|
||||
@ -610,19 +615,16 @@ int git_filter_list_apply_to_data(
|
||||
}
|
||||
|
||||
/* Ensure that the output ends up in dbuffer[1] (i.e. the dest) */
|
||||
if (si != 1) {
|
||||
git_buffer sw = *dbuffer[1];
|
||||
*dbuffer[1] = *dbuffer[0];
|
||||
*dbuffer[0] = sw;
|
||||
}
|
||||
if (si != 1)
|
||||
git_buf_swap(dbuffer[0], dbuffer[1]);
|
||||
|
||||
git_buffer_free(&local); /* don't leak if we allocated locally */
|
||||
git_buf_free(&local); /* don't leak if we allocated locally */
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int git_filter_list_apply_to_file(
|
||||
git_buffer *out,
|
||||
git_buf *out,
|
||||
git_filter_list *filters,
|
||||
git_repository *repo,
|
||||
const char *path)
|
||||
@ -634,11 +636,9 @@ int git_filter_list_apply_to_file(
|
||||
if (!(error = git_path_join_unrooted(&abspath, path, base, NULL)) &&
|
||||
!(error = git_futils_readbuffer(&raw, abspath.ptr)))
|
||||
{
|
||||
git_buffer in = GIT_BUFFER_FROM_BUF(&raw);
|
||||
error = git_filter_list_apply_to_data(out, filters, &raw);
|
||||
|
||||
error = git_filter_list_apply_to_data(out, filters, &in);
|
||||
|
||||
git_buffer_free(&in);
|
||||
git_buf_free(&raw);
|
||||
}
|
||||
|
||||
git_buf_free(&abspath);
|
||||
@ -646,12 +646,12 @@ int git_filter_list_apply_to_file(
|
||||
}
|
||||
|
||||
int git_filter_list_apply_to_blob(
|
||||
git_buffer *out,
|
||||
git_buf *out,
|
||||
git_filter_list *filters,
|
||||
git_blob *blob)
|
||||
{
|
||||
git_buffer in = {
|
||||
(char *)git_blob_rawcontent(blob), git_blob_rawsize(blob), 0
|
||||
git_buf in = {
|
||||
(char *)git_blob_rawcontent(blob), 0, git_blob_rawsize(blob)
|
||||
};
|
||||
|
||||
if (filters)
|
||||
|
48
src/ident.c
48
src/ident.c
@ -9,7 +9,7 @@
|
||||
#include "filter.h"
|
||||
#include "buffer.h"
|
||||
|
||||
int ident_find_id(
|
||||
static int ident_find_id(
|
||||
const char **id_start, const char **id_end, const char *start, size_t len)
|
||||
{
|
||||
const char *found;
|
||||
@ -36,12 +36,11 @@ int ident_find_id(
|
||||
}
|
||||
|
||||
static int ident_insert_id(
|
||||
git_buffer *to, const git_buffer *from, const git_filter_source *src)
|
||||
git_buf *to, const git_buf *from, const git_filter_source *src)
|
||||
{
|
||||
char oid[GIT_OID_HEXSZ+1];
|
||||
const char *id_start, *id_end, *from_end = from->ptr + from->size;
|
||||
size_t need_size;
|
||||
git_buf to_buf = GIT_BUF_FROM_BUFFER(to);
|
||||
|
||||
/* replace $Id$ with blob id */
|
||||
|
||||
@ -57,28 +56,23 @@ static int ident_insert_id(
|
||||
5 /* "$Id: " */ + GIT_OID_HEXSZ + 1 /* "$" */ +
|
||||
(size_t)(from_end - id_end);
|
||||
|
||||
if (git_buf_grow(&to_buf, need_size) < 0)
|
||||
if (git_buf_grow(to, need_size) < 0)
|
||||
return -1;
|
||||
|
||||
git_buf_set(&to_buf, from->ptr, (size_t)(id_start - from->ptr));
|
||||
git_buf_put(&to_buf, "$Id: ", 5);
|
||||
git_buf_put(&to_buf, oid, GIT_OID_HEXSZ);
|
||||
git_buf_putc(&to_buf, '$');
|
||||
git_buf_put(&to_buf, id_end, (size_t)(from_end - id_end));
|
||||
git_buf_set(to, from->ptr, (size_t)(id_start - from->ptr));
|
||||
git_buf_put(to, "$Id: ", 5);
|
||||
git_buf_put(to, oid, GIT_OID_HEXSZ);
|
||||
git_buf_putc(to, '$');
|
||||
git_buf_put(to, id_end, (size_t)(from_end - id_end));
|
||||
|
||||
if (git_buf_oom(&to_buf))
|
||||
return -1;
|
||||
|
||||
git_buffer_from_buf(to, &to_buf);
|
||||
return 0;
|
||||
return git_buf_oom(to) ? -1 : 0;
|
||||
}
|
||||
|
||||
static int ident_remove_id(
|
||||
git_buffer *to, const git_buffer *from)
|
||||
git_buf *to, const git_buf *from)
|
||||
{
|
||||
const char *id_start, *id_end, *from_end = from->ptr + from->size;
|
||||
size_t need_size;
|
||||
git_buf to_buf = GIT_BUF_FROM_BUFFER(to);
|
||||
|
||||
if (ident_find_id(&id_start, &id_end, from->ptr, from->size) < 0)
|
||||
return GIT_ENOTFOUND;
|
||||
@ -86,25 +80,21 @@ static int ident_remove_id(
|
||||
need_size = (size_t)(id_start - from->ptr) +
|
||||
4 /* "$Id$" */ + (size_t)(from_end - id_end);
|
||||
|
||||
if (git_buf_grow(&to_buf, need_size) < 0)
|
||||
if (git_buf_grow(to, need_size) < 0)
|
||||
return -1;
|
||||
|
||||
git_buf_set(&to_buf, from->ptr, (size_t)(id_start - from->ptr));
|
||||
git_buf_put(&to_buf, "$Id$", 4);
|
||||
git_buf_put(&to_buf, id_end, (size_t)(from_end - id_end));
|
||||
git_buf_set(to, from->ptr, (size_t)(id_start - from->ptr));
|
||||
git_buf_put(to, "$Id$", 4);
|
||||
git_buf_put(to, id_end, (size_t)(from_end - id_end));
|
||||
|
||||
if (git_buf_oom(&to_buf))
|
||||
return -1;
|
||||
|
||||
git_buffer_from_buf(to, &to_buf);
|
||||
return 0;
|
||||
return git_buf_oom(to) ? -1 : 0;
|
||||
}
|
||||
|
||||
static int ident_apply(
|
||||
git_filter *self,
|
||||
void **payload,
|
||||
git_buffer *to,
|
||||
const git_buffer *from,
|
||||
git_filter *self,
|
||||
void **payload,
|
||||
git_buf *to,
|
||||
const git_buf *from,
|
||||
const git_filter_source *src)
|
||||
{
|
||||
GIT_UNUSED(self); GIT_UNUSED(payload);
|
||||
|
@ -192,16 +192,16 @@ int git_odb__hashfd_filtered(
|
||||
*/
|
||||
|
||||
if (!(error = git_futils_readbuffer_fd(&raw, fd, size))) {
|
||||
git_buffer pre = GIT_BUFFER_FROM_BUF(&raw), post = GIT_BUFFER_INIT;
|
||||
git_buf post = GIT_BUF_INIT;
|
||||
|
||||
error = git_filter_list_apply_to_data(&post, fl, &pre);
|
||||
error = git_filter_list_apply_to_data(&post, fl, &raw);
|
||||
|
||||
git_buffer_free(&pre);
|
||||
git_buf_free(&raw);
|
||||
|
||||
if (!error)
|
||||
error = git_odb_hash(out, post.ptr, post.size, type);
|
||||
|
||||
git_buffer_free(&post);
|
||||
git_buf_free(&post);
|
||||
}
|
||||
|
||||
return error;
|
||||
|
@ -565,7 +565,7 @@ static bool _check_dir_contents(
|
||||
size_t sub_size = strlen(sub);
|
||||
|
||||
/* leave base valid even if we could not make space for subdir */
|
||||
if (git_buf_try_grow(dir, dir_size + sub_size + 2, false) < 0)
|
||||
if (git_buf_try_grow(dir, dir_size + sub_size + 2, false, false) < 0)
|
||||
return false;
|
||||
|
||||
/* save excursion */
|
||||
|
@ -679,6 +679,9 @@ size_t git__unescape(char *str)
|
||||
{
|
||||
char *scan, *pos = str;
|
||||
|
||||
if (!str)
|
||||
return 0;
|
||||
|
||||
for (scan = str; *scan; pos++, scan++) {
|
||||
if (*scan == '\\' && *(scan + 1) != '\0')
|
||||
scan++; /* skip '\' but include next char */
|
||||
|
@ -23,7 +23,7 @@ void test_filter_blob__cleanup(void)
|
||||
void test_filter_blob__all_crlf(void)
|
||||
{
|
||||
git_blob *blob;
|
||||
git_buffer buf = GIT_BUFFER_INIT;
|
||||
git_buf buf = { 0 };
|
||||
|
||||
cl_git_pass(git_revparse_single(
|
||||
(git_object **)&blob, g_repo, "a9a2e891")); /* all-crlf */
|
||||
@ -43,7 +43,7 @@ void test_filter_blob__all_crlf(void)
|
||||
|
||||
cl_assert_equal_s(ALL_CRLF_TEXT_AS_LF, buf.ptr);
|
||||
|
||||
git_buffer_free(&buf);
|
||||
git_buf_free(&buf);
|
||||
git_blob_free(blob);
|
||||
}
|
||||
|
||||
@ -51,7 +51,7 @@ void test_filter_blob__ident(void)
|
||||
{
|
||||
git_oid id;
|
||||
git_blob *blob;
|
||||
git_buffer buf = GIT_BUFFER_INIT;
|
||||
git_buf buf = { 0 };
|
||||
|
||||
cl_git_mkfile("crlf/test.ident", "Some text\n$Id$\nGoes there\n");
|
||||
cl_git_pass(git_blob_create_fromworkdir(&id, g_repo, "test.ident"));
|
||||
@ -78,4 +78,7 @@ void test_filter_blob__ident(void)
|
||||
cl_assert_equal_s(
|
||||
"Some text\n$Id: 3164f585d548ac68027d22b104f2d8100b2b6845$\nGoes there\n", buf.ptr);
|
||||
|
||||
git_buf_free(&buf);
|
||||
git_blob_free(blob);
|
||||
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ void test_filter_crlf__to_worktree(void)
|
||||
{
|
||||
git_filter_list *fl;
|
||||
git_filter *crlf;
|
||||
git_buffer in = GIT_BUFFER_INIT, out = GIT_BUFFER_INIT;
|
||||
git_buf in = { 0 }, out = { 0 };
|
||||
|
||||
{
|
||||
git_config *cfg;
|
||||
@ -48,14 +48,14 @@ void test_filter_crlf__to_worktree(void)
|
||||
#endif
|
||||
|
||||
git_filter_list_free(fl);
|
||||
git_buffer_free(&out);
|
||||
git_buf_free(&out);
|
||||
}
|
||||
|
||||
void test_filter_crlf__to_odb(void)
|
||||
{
|
||||
git_filter_list *fl;
|
||||
git_filter *crlf;
|
||||
git_buffer in = GIT_BUFFER_INIT, out = GIT_BUFFER_INIT;
|
||||
git_buf in = { 0 }, out = { 0 };
|
||||
|
||||
{
|
||||
git_config *cfg;
|
||||
@ -79,5 +79,5 @@ void test_filter_crlf__to_odb(void)
|
||||
cl_assert_equal_s("Some text\nRight here\n", out.ptr);
|
||||
|
||||
git_filter_list_free(fl);
|
||||
git_buffer_free(&out);
|
||||
git_buf_free(&out);
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ static void add_blob_and_filter(
|
||||
{
|
||||
git_oid id;
|
||||
git_blob *blob;
|
||||
git_buffer out = GIT_BUFFER_INIT;
|
||||
git_buf out = { 0 };
|
||||
|
||||
cl_git_mkfile("crlf/identtest", data);
|
||||
cl_git_pass(git_blob_create_fromworkdir(&id, g_repo, "identtest"));
|
||||
@ -31,7 +31,7 @@ static void add_blob_and_filter(
|
||||
cl_assert_equal_s(expected, out.ptr);
|
||||
|
||||
git_blob_free(blob);
|
||||
git_buffer_free(&out);
|
||||
git_buf_free(&out);
|
||||
}
|
||||
|
||||
void test_filter_ident__to_worktree(void)
|
||||
|
@ -104,7 +104,7 @@ void test_object_blob_filter__to_odb(void)
|
||||
git_config *cfg;
|
||||
int i;
|
||||
git_blob *blob;
|
||||
git_buffer out = GIT_BUFFER_INIT;
|
||||
git_buf out = GIT_BUF_INIT;
|
||||
|
||||
cl_git_pass(git_repository_config(&cfg, g_repo));
|
||||
cl_assert(cfg);
|
||||
@ -129,7 +129,7 @@ void test_object_blob_filter__to_odb(void)
|
||||
}
|
||||
|
||||
git_filter_list_free(fl);
|
||||
git_buffer_free(&out);
|
||||
git_buf_free(&out);
|
||||
git_config_free(cfg);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user