1 /* 2 * Copyright (C) the libgit2 contributors. All rights reserved. 3 * 4 * This file is part of libgit2, distributed under the GNU GPL v2 with 5 * a Linking Exception. For full terms see the included COPYING file. 6 */ 7 #ifndef INCLUDE_git_attr_h__ 8 #define INCLUDE_git_attr_h__ 9 10 #include "common.h" 11 #include "types.h" 12 13 /** 14 * @file git2/attr.h 15 * @brief Git attribute management routines 16 * @defgroup git_attr Git attribute management routines 17 * @ingroup Git 18 * @{ 19 */ 20 GIT_BEGIN_DECL 21 22 /** 23 * GIT_ATTR_TRUE checks if an attribute is set on. In core git 24 * parlance, this the value for "Set" attributes. 25 * 26 * For example, if the attribute file contains: 27 * 28 * *.c foo 29 * 30 * Then for file `xyz.c` looking up attribute "foo" gives a value for 31 * which `GIT_ATTR_TRUE(value)` is true. 32 */ 33 #define GIT_ATTR_IS_TRUE(attr) (git_attr_value(attr) == GIT_ATTR_VALUE_TRUE) 34 35 /** 36 * GIT_ATTR_FALSE checks if an attribute is set off. In core git 37 * parlance, this is the value for attributes that are "Unset" (not to 38 * be confused with values that a "Unspecified"). 39 * 40 * For example, if the attribute file contains: 41 * 42 * *.h -foo 43 * 44 * Then for file `zyx.h` looking up attribute "foo" gives a value for 45 * which `GIT_ATTR_FALSE(value)` is true. 46 */ 47 #define GIT_ATTR_IS_FALSE(attr) (git_attr_value(attr) == GIT_ATTR_VALUE_FALSE) 48 49 /** 50 * GIT_ATTR_UNSPECIFIED checks if an attribute is unspecified. This 51 * may be due to the attribute not being mentioned at all or because 52 * the attribute was explicitly set unspecified via the `!` operator. 53 * 54 * For example, if the attribute file contains: 55 * 56 * *.c foo 57 * *.h -foo 58 * onefile.c !foo 59 * 60 * Then for `onefile.c` looking up attribute "foo" yields a value with 61 * `GIT_ATTR_UNSPECIFIED(value)` of true. Also, looking up "foo" on 62 * file `onefile.rb` or looking up "bar" on any file will all give 63 * `GIT_ATTR_UNSPECIFIED(value)` of true. 64 */ 65 #define GIT_ATTR_IS_UNSPECIFIED(attr) (git_attr_value(attr) == GIT_ATTR_VALUE_UNSPECIFIED) 66 67 /** 68 * GIT_ATTR_HAS_VALUE checks if an attribute is set to a value (as 69 * opposed to TRUE, FALSE or UNSPECIFIED). This would be the case if 70 * for a file with something like: 71 * 72 * *.txt eol=lf 73 * 74 * Given this, looking up "eol" for `onefile.txt` will give back the 75 * string "lf" and `GIT_ATTR_SET_TO_VALUE(attr)` will return true. 76 */ 77 #define GIT_ATTR_HAS_VALUE(attr) (git_attr_value(attr) == GIT_ATTR_VALUE_STRING) 78 79 /** 80 * Possible states for an attribute 81 */ 82 typedef enum { 83 GIT_ATTR_VALUE_UNSPECIFIED = 0, /**< The attribute has been left unspecified */ 84 GIT_ATTR_VALUE_TRUE, /**< The attribute has been set */ 85 GIT_ATTR_VALUE_FALSE, /**< The attribute has been unset */ 86 GIT_ATTR_VALUE_STRING, /**< This attribute has a value */ 87 } git_attr_value_t; 88 89 /** 90 * Return the value type for a given attribute. 91 * 92 * This can be either `TRUE`, `FALSE`, `UNSPECIFIED` (if the attribute 93 * was not set at all), or `VALUE`, if the attribute was set to an 94 * actual string. 95 * 96 * If the attribute has a `VALUE` string, it can be accessed normally 97 * as a NULL-terminated C string. 98 * 99 * @param attr The attribute 100 * @return the value type for the attribute 101 */ 102 GIT_EXTERN(git_attr_value_t) git_attr_value(const char *attr); 103 104 /** 105 * Check attribute flags: Reading values from index and working directory. 106 * 107 * When checking attributes, it is possible to check attribute files 108 * in both the working directory (if there is one) and the index (if 109 * there is one). You can explicitly choose where to check and in 110 * which order using the following flags. 111 * 112 * Core git usually checks the working directory then the index, 113 * except during a checkout when it checks the index first. It will 114 * use index only for creating archives or for a bare repo (if an 115 * index has been specified for the bare repo). 116 */ 117 #define GIT_ATTR_CHECK_FILE_THEN_INDEX 0 118 #define GIT_ATTR_CHECK_INDEX_THEN_FILE 1 119 #define GIT_ATTR_CHECK_INDEX_ONLY 2 120 121 /** 122 * Check attribute flags: controlling extended attribute behavior. 123 * 124 * Normally, attribute checks include looking in the /etc (or system 125 * equivalent) directory for a `gitattributes` file. Passing this 126 * flag will cause attribute checks to ignore that file. 127 * equivalent) directory for a `gitattributes` file. Passing the 128 * `GIT_ATTR_CHECK_NO_SYSTEM` flag will cause attribute checks to 129 * ignore that file. 130 * 131 * Passing the `GIT_ATTR_CHECK_INCLUDE_HEAD` flag will use attributes 132 * from a `.gitattributes` file in the repository at the HEAD revision. 133 */ 134 #define GIT_ATTR_CHECK_NO_SYSTEM (1 << 2) 135 #define GIT_ATTR_CHECK_INCLUDE_HEAD (1 << 3) 136 137 /** 138 * Look up the value of one git attribute for path. 139 * 140 * @param value_out Output of the value of the attribute. Use the GIT_ATTR_... 141 * macros to test for TRUE, FALSE, UNSPECIFIED, etc. or just 142 * use the string value for attributes set to a value. You 143 * should NOT modify or free this value. 144 * @param repo The repository containing the path. 145 * @param flags A combination of GIT_ATTR_CHECK... flags. 146 * @param path The path to check for attributes. Relative paths are 147 * interpreted relative to the repo root. The file does 148 * not have to exist, but if it does not, then it will be 149 * treated as a plain file (not a directory). 150 * @param name The name of the attribute to look up. 151 */ 152 GIT_EXTERN(int) git_attr_get( 153 const char **value_out, 154 git_repository *repo, 155 uint32_t flags, 156 const char *path, 157 const char *name); 158 159 /** 160 * Look up a list of git attributes for path. 161 * 162 * Use this if you have a known list of attributes that you want to 163 * look up in a single call. This is somewhat more efficient than 164 * calling `git_attr_get()` multiple times. 165 * 166 * For example, you might write: 167 * 168 * const char *attrs[] = { "crlf", "diff", "foo" }; 169 * const char **values[3]; 170 * git_attr_get_many(values, repo, 0, "my/fun/file.c", 3, attrs); 171 * 172 * Then you could loop through the 3 values to get the settings for 173 * the three attributes you asked about. 174 * 175 * @param values_out An array of num_attr entries that will have string 176 * pointers written into it for the values of the attributes. 177 * You should not modify or free the values that are written 178 * into this array (although of course, you should free the 179 * array itself if you allocated it). 180 * @param repo The repository containing the path. 181 * @param flags A combination of GIT_ATTR_CHECK... flags. 182 * @param path The path inside the repo to check attributes. This 183 * does not have to exist, but if it does not, then 184 * it will be treated as a plain file (i.e. not a directory). 185 * @param num_attr The number of attributes being looked up 186 * @param names An array of num_attr strings containing attribute names. 187 */ 188 GIT_EXTERN(int) git_attr_get_many( 189 const char **values_out, 190 git_repository *repo, 191 uint32_t flags, 192 const char *path, 193 size_t num_attr, 194 const char **names); 195 196 /** 197 * The callback used with git_attr_foreach. 198 * 199 * This callback will be invoked only once per attribute name, even if there 200 * are multiple rules for a given file. The highest priority rule will be 201 * used. 202 * 203 * @see git_attr_foreach. 204 * 205 * @param name The attribute name. 206 * @param value The attribute value. May be NULL if the attribute is explicitly 207 * set to UNSPECIFIED using the '!' sign. 208 * @param payload A user-specified pointer. 209 * @return 0 to continue looping, non-zero to stop. This value will be returned 210 * from git_attr_foreach. 211 */ 212 typedef int GIT_CALLBACK(git_attr_foreach_cb)(const char *name, const char *value, void *payload); 213 214 /** 215 * Loop over all the git attributes for a path. 216 * 217 * @param repo The repository containing the path. 218 * @param flags A combination of GIT_ATTR_CHECK... flags. 219 * @param path Path inside the repo to check attributes. This does not have 220 * to exist, but if it does not, then it will be treated as a 221 * plain file (i.e. not a directory). 222 * @param callback Function to invoke on each attribute name and value. 223 * See git_attr_foreach_cb. 224 * @param payload Passed on as extra parameter to callback function. 225 * @return 0 on success, non-zero callback return value, or error code 226 */ 227 GIT_EXTERN(int) git_attr_foreach( 228 git_repository *repo, 229 uint32_t flags, 230 const char *path, 231 git_attr_foreach_cb callback, 232 void *payload); 233 234 /** 235 * Flush the gitattributes cache. 236 * 237 * Call this if you have reason to believe that the attributes files on 238 * disk no longer match the cached contents of memory. This will cause 239 * the attributes files to be reloaded the next time that an attribute 240 * access function is called. 241 * 242 * @param repo The repository containing the gitattributes cache 243 * @return 0 on success, or an error code 244 */ 245 GIT_EXTERN(int) git_attr_cache_flush( 246 git_repository *repo); 247 248 /** 249 * Add a macro definition. 250 * 251 * Macros will automatically be loaded from the top level `.gitattributes` 252 * file of the repository (plus the build-in "binary" macro). This 253 * function allows you to add others. For example, to add the default 254 * macro, you would call: 255 * 256 * git_attr_add_macro(repo, "binary", "-diff -crlf"); 257 */ 258 GIT_EXTERN(int) git_attr_add_macro( 259 git_repository *repo, 260 const char *name, 261 const char *values); 262 263 /** @} */ 264 GIT_END_DECL 265 #endif 266 267