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