Blame include/git2/attr.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_attr_h__
Packit Service 20376f
#define INCLUDE_git_attr_h__
Packit Service 20376f
Packit Service 20376f
#include "common.h"
Packit Service 20376f
#include "types.h"
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * @file git2/attr.h
Packit Service 20376f
 * @brief Git attribute management routines
Packit Service 20376f
 * @defgroup git_attr Git attribute management routines
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
 * GIT_ATTR_TRUE checks if an attribute is set on.  In core git
Packit Service 20376f
 * parlance, this the value for "Set" attributes.
Packit Service 20376f
 *
Packit Service 20376f
 * For example, if the attribute file contains:
Packit Service 20376f
 *
Packit Service 20376f
 *    *.c foo
Packit Service 20376f
 *
Packit Service 20376f
 * Then for file `xyz.c` looking up attribute "foo" gives a value for
Packit Service 20376f
 * which `GIT_ATTR_TRUE(value)` is true.
Packit Service 20376f
 */
Packit Service 20376f
#define GIT_ATTR_TRUE(attr)	(git_attr_value(attr) == GIT_ATTR_TRUE_T)
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * GIT_ATTR_FALSE checks if an attribute is set off.  In core git
Packit Service 20376f
 * parlance, this is the value for attributes that are "Unset" (not to
Packit Service 20376f
 * be confused with values that a "Unspecified").
Packit Service 20376f
 *
Packit Service 20376f
 * For example, if the attribute file contains:
Packit Service 20376f
 *
Packit Service 20376f
 *    *.h -foo
Packit Service 20376f
 *
Packit Service 20376f
 * Then for file `zyx.h` looking up attribute "foo" gives a value for
Packit Service 20376f
 * which `GIT_ATTR_FALSE(value)` is true.
Packit Service 20376f
 */
Packit Service 20376f
#define GIT_ATTR_FALSE(attr) (git_attr_value(attr) == GIT_ATTR_FALSE_T)
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * GIT_ATTR_UNSPECIFIED checks if an attribute is unspecified.  This
Packit Service 20376f
 * may be due to the attribute not being mentioned at all or because
Packit Service 20376f
 * the attribute was explicitly set unspecified via the `!` operator.
Packit Service 20376f
 *
Packit Service 20376f
 * For example, if the attribute file contains:
Packit Service 20376f
 *
Packit Service 20376f
 *    *.c foo
Packit Service 20376f
 *    *.h -foo
Packit Service 20376f
 *    onefile.c !foo
Packit Service 20376f
 *
Packit Service 20376f
 * Then for `onefile.c` looking up attribute "foo" yields a value with
Packit Service 20376f
 * `GIT_ATTR_UNSPECIFIED(value)` of true.  Also, looking up "foo" on
Packit Service 20376f
 * file `onefile.rb` or looking up "bar" on any file will all give
Packit Service 20376f
 * `GIT_ATTR_UNSPECIFIED(value)` of true.
Packit Service 20376f
 */
Packit Service 20376f
#define GIT_ATTR_UNSPECIFIED(attr) (git_attr_value(attr) == GIT_ATTR_UNSPECIFIED_T)
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * GIT_ATTR_HAS_VALUE checks if an attribute is set to a value (as
Packit Service 20376f
 * opposed to TRUE, FALSE or UNSPECIFIED).  This would be the case if
Packit Service 20376f
 * for a file with something like:
Packit Service 20376f
 *
Packit Service 20376f
 *    *.txt eol=lf
Packit Service 20376f
 *
Packit Service 20376f
 * Given this, looking up "eol" for `onefile.txt` will give back the
Packit Service 20376f
 * string "lf" and `GIT_ATTR_SET_TO_VALUE(attr)` will return true.
Packit Service 20376f
 */
Packit Service 20376f
#define GIT_ATTR_HAS_VALUE(attr) (git_attr_value(attr) == GIT_ATTR_VALUE_T)
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Possible states for an attribute
Packit Service 20376f
 */
Packit Service 20376f
typedef enum {
Packit Service 20376f
	GIT_ATTR_UNSPECIFIED_T = 0, /**< The attribute has been left unspecified */
Packit Service 20376f
	GIT_ATTR_TRUE_T,  /**< The attribute has been set */
Packit Service 20376f
	GIT_ATTR_FALSE_T, /**< The attribute has been unset */
Packit Service 20376f
	GIT_ATTR_VALUE_T, /**< This attribute has a value */
Packit Service 20376f
} git_attr_t;
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Return the value type for a given attribute.
Packit Service 20376f
 *
Packit Service 20376f
 * This can be either `TRUE`, `FALSE`, `UNSPECIFIED` (if the attribute
Packit Service 20376f
 * was not set at all), or `VALUE`, if the attribute was set to an
Packit Service 20376f
 * actual string.
Packit Service 20376f
 *
Packit Service 20376f
 * If the attribute has a `VALUE` string, it can be accessed normally
Packit Service 20376f
 * as a NULL-terminated C string.
Packit Service 20376f
 *
Packit Service 20376f
 * @param attr The attribute
Packit Service 20376f
 * @return the value type for the attribute
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(git_attr_t) git_attr_value(const char *attr);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Check attribute flags: Reading values from index and working directory.
Packit Service 20376f
 *
Packit Service 20376f
 * When checking attributes, it is possible to check attribute files
Packit Service 20376f
 * in both the working directory (if there is one) and the index (if
Packit Service 20376f
 * there is one).  You can explicitly choose where to check and in
Packit Service 20376f
 * which order using the following flags.
Packit Service 20376f
 *
Packit Service 20376f
 * Core git usually checks the working directory then the index,
Packit Service 20376f
 * except during a checkout when it checks the index first.  It will
Packit Service 20376f
 * use index only for creating archives or for a bare repo (if an
Packit Service 20376f
 * index has been specified for the bare repo).
Packit Service 20376f
 */
Packit Service 20376f
#define GIT_ATTR_CHECK_FILE_THEN_INDEX	0
Packit Service 20376f
#define GIT_ATTR_CHECK_INDEX_THEN_FILE	1
Packit Service 20376f
#define GIT_ATTR_CHECK_INDEX_ONLY		2
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Check attribute flags: Using the system attributes file.
Packit Service 20376f
 *
Packit Service 20376f
 * Normally, attribute checks include looking in the /etc (or system
Packit Service 20376f
 * equivalent) directory for a `gitattributes` file.  Passing this
Packit Service 20376f
 * flag will cause attribute checks to ignore that file.
Packit Service 20376f
 */
Packit Service 20376f
#define GIT_ATTR_CHECK_NO_SYSTEM		(1 << 2)
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Look up the value of one git attribute for path.
Packit Service 20376f
 *
Packit Service 20376f
 * @param value_out Output of the value of the attribute.  Use the GIT_ATTR_...
Packit Service 20376f
 *             macros to test for TRUE, FALSE, UNSPECIFIED, etc. or just
Packit Service 20376f
 *             use the string value for attributes set to a value.  You
Packit Service 20376f
 *             should NOT modify or free this value.
Packit Service 20376f
 * @param repo The repository containing the path.
Packit Service 20376f
 * @param flags A combination of GIT_ATTR_CHECK... flags.
Packit Service 20376f
 * @param path The path to check for attributes.  Relative paths are
Packit Service 20376f
 *             interpreted relative to the repo root.  The file does
Packit Service 20376f
 *             not have to exist, but if it does not, then it will be
Packit Service 20376f
 *             treated as a plain file (not a directory).
Packit Service 20376f
 * @param name The name of the attribute to look up.
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(int) git_attr_get(
Packit Service 20376f
	const char **value_out,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	uint32_t flags,
Packit Service 20376f
	const char *path,
Packit Service 20376f
	const char *name);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Look up a list of git attributes for path.
Packit Service 20376f
 *
Packit Service 20376f
 * Use this if you have a known list of attributes that you want to
Packit Service 20376f
 * look up in a single call.  This is somewhat more efficient than
Packit Service 20376f
 * calling `git_attr_get()` multiple times.
Packit Service 20376f
 *
Packit Service 20376f
 * For example, you might write:
Packit Service 20376f
 *
Packit Service 20376f
 *     const char *attrs[] = { "crlf", "diff", "foo" };
Packit Service 20376f
 *     const char **values[3];
Packit Service 20376f
 *     git_attr_get_many(values, repo, 0, "my/fun/file.c", 3, attrs);
Packit Service 20376f
 *
Packit Service 20376f
 * Then you could loop through the 3 values to get the settings for
Packit Service 20376f
 * the three attributes you asked about.
Packit Service 20376f
 *
Packit Service 20376f
 * @param values_out An array of num_attr entries that will have string
Packit Service 20376f
 *             pointers written into it for the values of the attributes.
Packit Service 20376f
 *             You should not modify or free the values that are written
Packit Service 20376f
 *             into this array (although of course, you should free the
Packit Service 20376f
 *             array itself if you allocated it).
Packit Service 20376f
 * @param repo The repository containing the path.
Packit Service 20376f
 * @param flags A combination of GIT_ATTR_CHECK... flags.
Packit Service 20376f
 * @param path The path inside the repo to check attributes.  This
Packit Service 20376f
 *             does not have to exist, but if it does not, then
Packit Service 20376f
 *             it will be treated as a plain file (i.e. not a directory).
Packit Service 20376f
 * @param num_attr The number of attributes being looked up
Packit Service 20376f
 * @param names An array of num_attr strings containing attribute names.
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(int) git_attr_get_many(
Packit Service 20376f
	const char **values_out,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	uint32_t flags,
Packit Service 20376f
	const char *path,
Packit Service 20376f
	size_t num_attr,
Packit Service 20376f
	const char **names);
Packit Service 20376f
Packit Service 20376f
typedef int (*git_attr_foreach_cb)(const char *name, const char *value, void *payload);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Loop over all the git attributes for a path.
Packit Service 20376f
 *
Packit Service 20376f
 * @param repo The repository containing the path.
Packit Service 20376f
 * @param flags A combination of GIT_ATTR_CHECK... flags.
Packit Service 20376f
 * @param path Path inside the repo to check attributes.  This does not have
Packit Service 20376f
 *             to exist, but if it does not, then it will be treated as a
Packit Service 20376f
 *             plain file (i.e. not a directory).
Packit Service 20376f
 * @param callback Function to invoke on each attribute name and value.  The
Packit Service 20376f
 *             value may be NULL is the attribute is explicitly set to
Packit Service 20376f
 *             UNSPECIFIED using the '!' sign.  Callback will be invoked
Packit Service 20376f
 *             only once per attribute name, even if there are multiple
Packit Service 20376f
 *             rules for a given file.  The highest priority rule will be
Packit Service 20376f
 *             used.  Return a non-zero value from this to stop looping.
Packit Service 20376f
 *             The value will be returned from `git_attr_foreach`.
Packit Service 20376f
 * @param payload Passed on as extra parameter to callback function.
Packit Service 20376f
 * @return 0 on success, non-zero callback return value, or error code
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(int) git_attr_foreach(
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	uint32_t flags,
Packit Service 20376f
	const char *path,
Packit Service 20376f
	git_attr_foreach_cb callback,
Packit Service 20376f
	void *payload);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Flush the gitattributes cache.
Packit Service 20376f
 *
Packit Service 20376f
 * Call this if you have reason to believe that the attributes files on
Packit Service 20376f
 * disk no longer match the cached contents of memory.  This will cause
Packit Service 20376f
 * the attributes files to be reloaded the next time that an attribute
Packit Service 20376f
 * access function is called.
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(void) git_attr_cache_flush(
Packit Service 20376f
	git_repository *repo);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Add a macro definition.
Packit Service 20376f
 *
Packit Service 20376f
 * Macros will automatically be loaded from the top level `.gitattributes`
Packit Service 20376f
 * file of the repository (plus the build-in "binary" macro).  This
Packit Service 20376f
 * function allows you to add others.  For example, to add the default
Packit Service 20376f
 * macro, you would call:
Packit Service 20376f
 *
Packit Service 20376f
 *     git_attr_add_macro(repo, "binary", "-diff -crlf");
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(int) git_attr_add_macro(
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	const char *name,
Packit Service 20376f
	const char *values);
Packit Service 20376f
Packit Service 20376f
/** @} */
Packit Service 20376f
GIT_END_DECL
Packit Service 20376f
#endif
Packit Service 20376f