summaryrefslogtreecommitdiff
path: root/include/git2
diff options
context:
space:
mode:
Diffstat (limited to 'include/git2')
-rw-r--r--include/git2/blob.h32
-rw-r--r--include/git2/buffer.h86
-rw-r--r--include/git2/filter.h54
3 files changed, 172 insertions, 0 deletions
diff --git a/include/git2/blob.h b/include/git2/blob.h
index 8fca48966..dcb815b2f 100644
--- a/include/git2/blob.h
+++ b/include/git2/blob.h
@@ -11,6 +11,7 @@
#include "types.h"
#include "oid.h"
#include "object.h"
+#include "buffer.h"
/**
* @file git2/blob.h
@@ -96,6 +97,37 @@ GIT_EXTERN(const void *) git_blob_rawcontent(const git_blob *blob);
GIT_EXTERN(git_off_t) git_blob_rawsize(const git_blob *blob);
/**
+ * Get a buffer with the filtered content of a blob.
+ *
+ * This applies filters as if the blob was being checked out to the
+ * working directory under the specified filename. This may apply
+ * 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`).
+ *
+ * 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
+ * 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 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
+ * NUL bytes / looks like binary data before applying filters?
+ * @return 0 on success or an error code
+ */
+GIT_EXTERN(int) git_blob_filtered_content(
+ git_buffer *out,
+ git_blob *blob,
+ const char *as_path,
+ int check_for_binary_data);
+
+/**
* Read a file from the working folder of a repository
* and write it to the Object Database as a loose blob
*
diff --git a/include/git2/buffer.h b/include/git2/buffer.h
new file mode 100644
index 000000000..454a1faa5
--- /dev/null
+++ b/include/git2/buffer.h
@@ -0,0 +1,86 @@
+/*
+ * 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_git_buffer_h__
+#define INCLUDE_git_buffer_h__
+
+#include "common.h"
+
+/**
+ * @file git2/buffer.h
+ * @brief Buffer export structure
+ *
+ * @ingroup Git
+ * @{
+ */
+GIT_BEGIN_DECL
+
+/**
+ * A data buffer for exporting data from libgit2
+ *
+ * There are a number of places where libgit2 wants to return an allocated
+ * data buffer to the caller and have the caller take ownership of that
+ * allocated 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.
+ *
+ * * `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 in
+ * cases where it is larger than the `size` actually in use.
+ *
+ * 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.
+ */
+typedef struct git_buffer {
+ char *ptr;
+ size_t size;
+ size_t available;
+} git_buffer;
+
+/**
+ * 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.
+ *
+ * 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.
+ *
+ * @param buffer The buffer with allocated memory
+ */
+GIT_EXTERN(void) git_buffer_free(git_buffer *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.
+ *
+ * Currently, this will never shrink the buffer, only expand it.
+ *
+ * @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
+ */
+GIT_EXTERN(int) git_buffer_resize(git_buffer *buffer, size_t want_size);
+
+GIT_END_DECL
+
+/** @} */
+
+#endif
diff --git a/include/git2/filter.h b/include/git2/filter.h
new file mode 100644
index 000000000..3bc4a9037
--- /dev/null
+++ b/include/git2/filter.h
@@ -0,0 +1,54 @@
+/*
+ * 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_git_filter_h__
+#define INCLUDE_git_filter_h__
+
+#include "common.h"
+#include "types.h"
+#include "oid.h"
+#include "buffer.h"
+
+/**
+ * @file git2/filter.h
+ * @brief Git filter APIs
+ *
+ * @ingroup Git
+ * @{
+ */
+GIT_BEGIN_DECL
+
+/**
+ * Filters are applied in one of two directions: smudging - which is
+ * exporting a file from the Git object database to the working directory,
+ * and cleaning - which is importing a file from the working directory to
+ * the Git object database. These values control which direction of
+ * change is being applied.
+ */
+typedef enum {
+ GIT_FILTER_SMUDGE = 0,
+ GIT_FILTER_TO_WORKTREE = GIT_FILTER_SMUDGE,
+ GIT_FILTER_CLEAN = 1,
+ GIT_FILTER_TO_ODB = GIT_FILTER_CLEAN,
+} git_filter_mode_t;
+
+/**
+ * A filter that can transform file data
+ *
+ * This represents a filter that can be used to transform or even replace
+ * file data. Libgit2 currently includes one built in filter:
+ *
+ * * "crlf" which uses the complex rules with the "text", "eol", and
+ * "crlf" file attributes to decide how to convert between LF and CRLF
+ * line endings
+ */
+typedef struct git_filter git_filter;
+
+GIT_END_DECL
+
+/** @} */
+
+#endif