Blame include/git2/filter.h

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
#ifndef INCLUDE_git_filter_h__
Packit Service 20376f
#define INCLUDE_git_filter_h__
Packit Service 20376f
Packit Service 20376f
#include "common.h"
Packit Service 20376f
#include "types.h"
Packit Service 20376f
#include "oid.h"
Packit Service 20376f
#include "buffer.h"
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * @file git2/filter.h
Packit Service 20376f
 * @brief Git filter APIs
Packit Service 20376f
 *
Packit Service 20376f
 * @ingroup Git
Packit Service 20376f
 * @{
Packit Service 20376f
 */
Packit Service 20376f
GIT_BEGIN_DECL
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Filters are applied in one of two directions: smudging - which is
Packit Service 20376f
 * exporting a file from the Git object database to the working directory,
Packit Service 20376f
 * and cleaning - which is importing a file from the working directory to
Packit Service 20376f
 * the Git object database.  These values control which direction of
Packit Service 20376f
 * change is being applied.
Packit Service 20376f
 */
Packit Service 20376f
typedef enum {
Packit Service 20376f
	GIT_FILTER_TO_WORKTREE = 0,
Packit Service 20376f
	GIT_FILTER_SMUDGE = GIT_FILTER_TO_WORKTREE,
Packit Service 20376f
	GIT_FILTER_TO_ODB = 1,
Packit Service 20376f
	GIT_FILTER_CLEAN = GIT_FILTER_TO_ODB,
Packit Service 20376f
} git_filter_mode_t;
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Filter option flags.
Packit Service 20376f
 */
Packit Service 20376f
typedef enum {
Packit Service 20376f
	GIT_FILTER_DEFAULT = 0u,
Packit Service 20376f
	GIT_FILTER_ALLOW_UNSAFE = (1u << 0),
Packit Service 20376f
} git_filter_flag_t;
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * A filter that can transform file data
Packit Service 20376f
 *
Packit Service 20376f
 * This represents a filter that can be used to transform or even replace
Packit Service 20376f
 * file data.  Libgit2 includes one built in filter and it is possible to
Packit Service 20376f
 * write your own (see git2/sys/filter.h for information on that).
Packit Service 20376f
 *
Packit Service 20376f
 * The two builtin filters are:
Packit Service 20376f
 *
Packit Service 20376f
 * * "crlf" which uses the complex rules with the "text", "eol", and
Packit Service 20376f
 *   "crlf" file attributes to decide how to convert between LF and CRLF
Packit Service 20376f
 *   line endings
Packit Service 20376f
 * * "ident" which replaces "$Id$" in a blob with "$Id: <blob OID>$" upon
Packit Service 20376f
 *   checkout and replaced "$Id: <anything>$" with "$Id$" on checkin.
Packit Service 20376f
 */
Packit Service 20376f
typedef struct git_filter git_filter;
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * List of filters to be applied
Packit Service 20376f
 *
Packit Service 20376f
 * This represents a list of filters to be applied to a file / blob.  You
Packit Service 20376f
 * can build the list with one call, apply it with another, and dispose it
Packit Service 20376f
 * with a third.  In typical usage, there are not many occasions where a
Packit Service 20376f
 * git_filter_list is needed directly since the library will generally
Packit Service 20376f
 * handle conversions for you, but it can be convenient to be able to
Packit Service 20376f
 * build and apply the list sometimes.
Packit Service 20376f
 */
Packit Service 20376f
typedef struct git_filter_list git_filter_list;
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Load the filter list for a given path.
Packit Service 20376f
 *
Packit Service 20376f
 * This will return 0 (success) but set the output git_filter_list to NULL
Packit Service 20376f
 * if no filters are requested for the given file.
Packit Service 20376f
 *
Packit Service 20376f
 * @param filters Output newly created git_filter_list (or NULL)
Packit Service 20376f
 * @param repo Repository object that contains `path`
Packit Service 20376f
 * @param blob The blob to which the filter will be applied (if known)
Packit Service 20376f
 * @param path Relative path of the file to be filtered
Packit Service 20376f
 * @param mode Filtering direction (WT->ODB or ODB->WT)
Packit Service 20376f
 * @param flags Combination of `git_filter_flag_t` flags
Packit Service 20376f
 * @return 0 on success (which could still return NULL if no filters are
Packit Service 20376f
 *         needed for the requested file), <0 on error
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(int) git_filter_list_load(
Packit Service 20376f
	git_filter_list **filters,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	git_blob *blob, /* can be NULL */
Packit Service 20376f
	const char *path,
Packit Service 20376f
	git_filter_mode_t mode,
Packit Service 20376f
	uint32_t flags);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Query the filter list to see if a given filter (by name) will run.
Packit Service 20376f
 * The built-in filters "crlf" and "ident" can be queried, otherwise this
Packit Service 20376f
 * is the name of the filter specified by the filter attribute.
Packit Service 20376f
 *
Packit Service 20376f
 * This will return 0 if the given filter is not in the list, or 1 if
Packit Service 20376f
 * the filter will be applied.
Packit Service 20376f
 *
Packit Service 20376f
 * @param filters A loaded git_filter_list (or NULL)
Packit Service 20376f
 * @param name The name of the filter to query
Packit Service 20376f
 * @return 1 if the filter is in the list, 0 otherwise
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(int) git_filter_list_contains(
Packit Service 20376f
	git_filter_list *filters,
Packit Service 20376f
	const char *name);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Apply filter list to a data buffer.
Packit Service 20376f
 *
Packit Service 20376f
 * See `git2/buffer.h` for background on `git_buf` objects.
Packit Service 20376f
 *
Packit Service 20376f
 * If the `in` buffer holds data allocated by libgit2 (i.e. `in->asize` is
Packit Service 20376f
 * not zero), then it will be overwritten when applying the filters.  If
Packit Service 20376f
 * not, then it will be left untouched.
Packit Service 20376f
 *
Packit Service 20376f
 * If there are no filters to apply (or `filters` is NULL), then the `out`
Packit Service 20376f
 * buffer will reference the `in` buffer data (with `asize` set to zero)
Packit Service 20376f
 * instead of allocating data.  This keeps allocations to a minimum, but
Packit Service 20376f
 * it means you have to be careful about freeing the `in` data since `out`
Packit Service 20376f
 * may be pointing to it!
Packit Service 20376f
 *
Packit Service 20376f
 * @param out Buffer to store the result of the filtering
Packit Service 20376f
 * @param filters A loaded git_filter_list (or NULL)
Packit Service 20376f
 * @param in Buffer containing the data to filter
Packit Service 20376f
 * @return 0 on success, an error code otherwise
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(int) git_filter_list_apply_to_data(
Packit Service 20376f
	git_buf *out,
Packit Service 20376f
	git_filter_list *filters,
Packit Service 20376f
	git_buf *in);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Apply a filter list to the contents of a file on disk
Packit Service 20376f
 *
Packit Service 20376f
 * @param out buffer into which to store the filtered file
Packit Service 20376f
 * @param filters the list of filters to apply
Packit Service 20376f
 * @param repo the repository in which to perform the filtering
Packit Service 20376f
 * @param path the path of the file to filter, a relative path will be
Packit Service 20376f
 * taken as relative to the workdir
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(int) git_filter_list_apply_to_file(
Packit Service 20376f
	git_buf *out,
Packit Service 20376f
	git_filter_list *filters,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	const char *path);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Apply a filter list to the contents of a blob
Packit Service 20376f
 *
Packit Service 20376f
 * @param out buffer into which to store the filtered file
Packit Service 20376f
 * @param filters the list of filters to apply
Packit Service 20376f
 * @param blob the blob to filter
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(int) git_filter_list_apply_to_blob(
Packit Service 20376f
	git_buf *out,
Packit Service 20376f
	git_filter_list *filters,
Packit Service 20376f
	git_blob *blob);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Apply a filter list to an arbitrary buffer as a stream
Packit Service 20376f
 *
Packit Service 20376f
 * @param filters the list of filters to apply
Packit Service 20376f
 * @param data the buffer to filter
Packit Service 20376f
 * @param target the stream into which the data will be written
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(int) git_filter_list_stream_data(
Packit Service 20376f
	git_filter_list *filters,
Packit Service 20376f
	git_buf *data,
Packit Service 20376f
	git_writestream *target);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Apply a filter list to a file as a stream
Packit Service 20376f
 *
Packit Service 20376f
 * @param filters the list of filters to apply
Packit Service 20376f
 * @param repo the repository in which to perform the filtering
Packit Service 20376f
 * @param path the path of the file to filter, a relative path will be
Packit Service 20376f
 * taken as relative to the workdir
Packit Service 20376f
 * @param target the stream into which the data will be written
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(int) git_filter_list_stream_file(
Packit Service 20376f
	git_filter_list *filters,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	const char *path,
Packit Service 20376f
	git_writestream *target);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Apply a filter list to a blob as a stream
Packit Service 20376f
 *
Packit Service 20376f
 * @param filters the list of filters to apply
Packit Service 20376f
 * @param blob the blob to filter
Packit Service 20376f
 * @param target the stream into which the data will be written
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(int) git_filter_list_stream_blob(
Packit Service 20376f
	git_filter_list *filters,
Packit Service 20376f
	git_blob *blob,
Packit Service 20376f
	git_writestream *target);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Free a git_filter_list
Packit Service 20376f
 *
Packit Service 20376f
 * @param filters A git_filter_list created by `git_filter_list_load`
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(void) git_filter_list_free(git_filter_list *filters);
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
GIT_END_DECL
Packit Service 20376f
Packit Service 20376f
/** @} */
Packit Service 20376f
Packit Service 20376f
#endif