|
||||
File indexing completed on 2025-01-18 09:59:36
0001 /* 0002 * Copyright (C) the libgit2 contributors. All rights reserved. 0003 * 0004 * This file is part of libgit2, distributed under the GNU GPL v2 with 0005 * a Linking Exception. For full terms see the included COPYING file. 0006 */ 0007 #ifndef INCLUDE_git_attr_h__ 0008 #define INCLUDE_git_attr_h__ 0009 0010 #include "common.h" 0011 #include "types.h" 0012 0013 /** 0014 * @file git2/attr.h 0015 * @brief Git attribute management routines 0016 * @defgroup git_attr Git attribute management routines 0017 * @ingroup Git 0018 * @{ 0019 */ 0020 GIT_BEGIN_DECL 0021 0022 /** 0023 * GIT_ATTR_TRUE checks if an attribute is set on. In core git 0024 * parlance, this the value for "Set" attributes. 0025 * 0026 * For example, if the attribute file contains: 0027 * 0028 * *.c foo 0029 * 0030 * Then for file `xyz.c` looking up attribute "foo" gives a value for 0031 * which `GIT_ATTR_TRUE(value)` is true. 0032 */ 0033 #define GIT_ATTR_IS_TRUE(attr) (git_attr_value(attr) == GIT_ATTR_VALUE_TRUE) 0034 0035 /** 0036 * GIT_ATTR_FALSE checks if an attribute is set off. In core git 0037 * parlance, this is the value for attributes that are "Unset" (not to 0038 * be confused with values that a "Unspecified"). 0039 * 0040 * For example, if the attribute file contains: 0041 * 0042 * *.h -foo 0043 * 0044 * Then for file `zyx.h` looking up attribute "foo" gives a value for 0045 * which `GIT_ATTR_FALSE(value)` is true. 0046 */ 0047 #define GIT_ATTR_IS_FALSE(attr) (git_attr_value(attr) == GIT_ATTR_VALUE_FALSE) 0048 0049 /** 0050 * GIT_ATTR_UNSPECIFIED checks if an attribute is unspecified. This 0051 * may be due to the attribute not being mentioned at all or because 0052 * the attribute was explicitly set unspecified via the `!` operator. 0053 * 0054 * For example, if the attribute file contains: 0055 * 0056 * *.c foo 0057 * *.h -foo 0058 * onefile.c !foo 0059 * 0060 * Then for `onefile.c` looking up attribute "foo" yields a value with 0061 * `GIT_ATTR_UNSPECIFIED(value)` of true. Also, looking up "foo" on 0062 * file `onefile.rb` or looking up "bar" on any file will all give 0063 * `GIT_ATTR_UNSPECIFIED(value)` of true. 0064 */ 0065 #define GIT_ATTR_IS_UNSPECIFIED(attr) (git_attr_value(attr) == GIT_ATTR_VALUE_UNSPECIFIED) 0066 0067 /** 0068 * GIT_ATTR_HAS_VALUE checks if an attribute is set to a value (as 0069 * opposed to TRUE, FALSE or UNSPECIFIED). This would be the case if 0070 * for a file with something like: 0071 * 0072 * *.txt eol=lf 0073 * 0074 * Given this, looking up "eol" for `onefile.txt` will give back the 0075 * string "lf" and `GIT_ATTR_SET_TO_VALUE(attr)` will return true. 0076 */ 0077 #define GIT_ATTR_HAS_VALUE(attr) (git_attr_value(attr) == GIT_ATTR_VALUE_STRING) 0078 0079 /** 0080 * Possible states for an attribute 0081 */ 0082 typedef enum { 0083 GIT_ATTR_VALUE_UNSPECIFIED = 0, /**< The attribute has been left unspecified */ 0084 GIT_ATTR_VALUE_TRUE, /**< The attribute has been set */ 0085 GIT_ATTR_VALUE_FALSE, /**< The attribute has been unset */ 0086 GIT_ATTR_VALUE_STRING /**< This attribute has a value */ 0087 } git_attr_value_t; 0088 0089 /** 0090 * Return the value type for a given attribute. 0091 * 0092 * This can be either `TRUE`, `FALSE`, `UNSPECIFIED` (if the attribute 0093 * was not set at all), or `VALUE`, if the attribute was set to an 0094 * actual string. 0095 * 0096 * If the attribute has a `VALUE` string, it can be accessed normally 0097 * as a NULL-terminated C string. 0098 * 0099 * @param attr The attribute 0100 * @return the value type for the attribute 0101 */ 0102 GIT_EXTERN(git_attr_value_t) git_attr_value(const char *attr); 0103 0104 /** 0105 * Check attribute flags: Reading values from index and working directory. 0106 * 0107 * When checking attributes, it is possible to check attribute files 0108 * in both the working directory (if there is one) and the index (if 0109 * there is one). You can explicitly choose where to check and in 0110 * which order using the following flags. 0111 * 0112 * Core git usually checks the working directory then the index, 0113 * except during a checkout when it checks the index first. It will 0114 * use index only for creating archives or for a bare repo (if an 0115 * index has been specified for the bare repo). 0116 */ 0117 #define GIT_ATTR_CHECK_FILE_THEN_INDEX 0 0118 #define GIT_ATTR_CHECK_INDEX_THEN_FILE 1 0119 #define GIT_ATTR_CHECK_INDEX_ONLY 2 0120 0121 /** 0122 * Check attribute flags: controlling extended attribute behavior. 0123 * 0124 * Normally, attribute checks include looking in the /etc (or system 0125 * equivalent) directory for a `gitattributes` file. Passing the 0126 * `GIT_ATTR_CHECK_NO_SYSTEM` flag will cause attribute checks to 0127 * ignore that file. 0128 * 0129 * Passing the `GIT_ATTR_CHECK_INCLUDE_HEAD` flag will use attributes 0130 * from a `.gitattributes` file in the repository at the HEAD revision. 0131 * 0132 * Passing the `GIT_ATTR_CHECK_INCLUDE_COMMIT` flag will use attributes 0133 * from a `.gitattributes` file in a specific commit. 0134 */ 0135 #define GIT_ATTR_CHECK_NO_SYSTEM (1 << 2) 0136 #define GIT_ATTR_CHECK_INCLUDE_HEAD (1 << 3) 0137 #define GIT_ATTR_CHECK_INCLUDE_COMMIT (1 << 4) 0138 0139 /** 0140 * An options structure for querying attributes. 0141 */ 0142 typedef struct { 0143 unsigned int version; 0144 0145 /** A combination of GIT_ATTR_CHECK flags */ 0146 unsigned int flags; 0147 0148 #ifdef GIT_DEPRECATE_HARD 0149 void *reserved; 0150 #else 0151 git_oid *commit_id; 0152 #endif 0153 0154 /** 0155 * The commit to load attributes from, when 0156 * `GIT_ATTR_CHECK_INCLUDE_COMMIT` is specified. 0157 */ 0158 git_oid attr_commit_id; 0159 } git_attr_options; 0160 0161 #define GIT_ATTR_OPTIONS_VERSION 1 0162 #define GIT_ATTR_OPTIONS_INIT {GIT_ATTR_OPTIONS_VERSION} 0163 0164 /** 0165 * Look up the value of one git attribute for path. 0166 * 0167 * @param value_out Output of the value of the attribute. Use the GIT_ATTR_... 0168 * macros to test for TRUE, FALSE, UNSPECIFIED, etc. or just 0169 * use the string value for attributes set to a value. You 0170 * should NOT modify or free this value. 0171 * @param repo The repository containing the path. 0172 * @param flags A combination of GIT_ATTR_CHECK... flags. 0173 * @param path The path to check for attributes. Relative paths are 0174 * interpreted relative to the repo root. The file does 0175 * not have to exist, but if it does not, then it will be 0176 * treated as a plain file (not a directory). 0177 * @param name The name of the attribute to look up. 0178 * @return 0 or an error code. 0179 */ 0180 GIT_EXTERN(int) git_attr_get( 0181 const char **value_out, 0182 git_repository *repo, 0183 uint32_t flags, 0184 const char *path, 0185 const char *name); 0186 0187 /** 0188 * Look up the value of one git attribute for path with extended options. 0189 * 0190 * @param value_out Output of the value of the attribute. Use the GIT_ATTR_... 0191 * macros to test for TRUE, FALSE, UNSPECIFIED, etc. or just 0192 * use the string value for attributes set to a value. You 0193 * should NOT modify or free this value. 0194 * @param repo The repository containing the path. 0195 * @param opts The `git_attr_options` to use when querying these attributes. 0196 * @param path The path to check for attributes. Relative paths are 0197 * interpreted relative to the repo root. The file does 0198 * not have to exist, but if it does not, then it will be 0199 * treated as a plain file (not a directory). 0200 * @param name The name of the attribute to look up. 0201 * @return 0 or an error code. 0202 */ 0203 GIT_EXTERN(int) git_attr_get_ext( 0204 const char **value_out, 0205 git_repository *repo, 0206 git_attr_options *opts, 0207 const char *path, 0208 const char *name); 0209 0210 /** 0211 * Look up a list of git attributes for path. 0212 * 0213 * Use this if you have a known list of attributes that you want to 0214 * look up in a single call. This is somewhat more efficient than 0215 * calling `git_attr_get()` multiple times. 0216 * 0217 * For example, you might write: 0218 * 0219 * const char *attrs[] = { "crlf", "diff", "foo" }; 0220 * const char **values[3]; 0221 * git_attr_get_many(values, repo, 0, "my/fun/file.c", 3, attrs); 0222 * 0223 * Then you could loop through the 3 values to get the settings for 0224 * the three attributes you asked about. 0225 * 0226 * @param values_out An array of num_attr entries that will have string 0227 * pointers written into it for the values of the attributes. 0228 * You should not modify or free the values that are written 0229 * into this array (although of course, you should free the 0230 * array itself if you allocated it). 0231 * @param repo The repository containing the path. 0232 * @param flags A combination of GIT_ATTR_CHECK... flags. 0233 * @param path The path inside the repo to check attributes. This 0234 * does not have to exist, but if it does not, then 0235 * it will be treated as a plain file (i.e. not a directory). 0236 * @param num_attr The number of attributes being looked up 0237 * @param names An array of num_attr strings containing attribute names. 0238 * @return 0 or an error code. 0239 */ 0240 GIT_EXTERN(int) git_attr_get_many( 0241 const char **values_out, 0242 git_repository *repo, 0243 uint32_t flags, 0244 const char *path, 0245 size_t num_attr, 0246 const char **names); 0247 0248 /** 0249 * Look up a list of git attributes for path with extended options. 0250 * 0251 * @param values_out An array of num_attr entries that will have string 0252 * pointers written into it for the values of the attributes. 0253 * You should not modify or free the values that are written 0254 * into this array (although of course, you should free the 0255 * array itself if you allocated it). 0256 * @param repo The repository containing the path. 0257 * @param opts The `git_attr_options` to use when querying these attributes. 0258 * @param path The path inside the repo to check attributes. This 0259 * does not have to exist, but if it does not, then 0260 * it will be treated as a plain file (i.e. not a directory). 0261 * @param num_attr The number of attributes being looked up 0262 * @param names An array of num_attr strings containing attribute names. 0263 * @return 0 or an error code. 0264 */ 0265 GIT_EXTERN(int) git_attr_get_many_ext( 0266 const char **values_out, 0267 git_repository *repo, 0268 git_attr_options *opts, 0269 const char *path, 0270 size_t num_attr, 0271 const char **names); 0272 0273 /** 0274 * The callback used with git_attr_foreach. 0275 * 0276 * This callback will be invoked only once per attribute name, even if there 0277 * are multiple rules for a given file. The highest priority rule will be 0278 * used. 0279 * 0280 * @see git_attr_foreach. 0281 * 0282 * @param name The attribute name. 0283 * @param value The attribute value. May be NULL if the attribute is explicitly 0284 * set to UNSPECIFIED using the '!' sign. 0285 * @param payload A user-specified pointer. 0286 * @return 0 to continue looping, non-zero to stop. This value will be returned 0287 * from git_attr_foreach. 0288 */ 0289 typedef int GIT_CALLBACK(git_attr_foreach_cb)(const char *name, const char *value, void *payload); 0290 0291 /** 0292 * Loop over all the git attributes for a path. 0293 * 0294 * @param repo The repository containing the path. 0295 * @param flags A combination of GIT_ATTR_CHECK... flags. 0296 * @param path Path inside the repo to check attributes. This does not have 0297 * to exist, but if it does not, then it will be treated as a 0298 * plain file (i.e. not a directory). 0299 * @param callback Function to invoke on each attribute name and value. 0300 * See git_attr_foreach_cb. 0301 * @param payload Passed on as extra parameter to callback function. 0302 * @return 0 on success, non-zero callback return value, or error code 0303 */ 0304 GIT_EXTERN(int) git_attr_foreach( 0305 git_repository *repo, 0306 uint32_t flags, 0307 const char *path, 0308 git_attr_foreach_cb callback, 0309 void *payload); 0310 0311 /** 0312 * Loop over all the git attributes for a path with extended options. 0313 * 0314 * @param repo The repository containing the path. 0315 * @param opts The `git_attr_options` to use when querying these attributes. 0316 * @param path Path inside the repo to check attributes. This does not have 0317 * to exist, but if it does not, then it will be treated as a 0318 * plain file (i.e. not a directory). 0319 * @param callback Function to invoke on each attribute name and value. 0320 * See git_attr_foreach_cb. 0321 * @param payload Passed on as extra parameter to callback function. 0322 * @return 0 on success, non-zero callback return value, or error code 0323 */ 0324 GIT_EXTERN(int) git_attr_foreach_ext( 0325 git_repository *repo, 0326 git_attr_options *opts, 0327 const char *path, 0328 git_attr_foreach_cb callback, 0329 void *payload); 0330 0331 /** 0332 * Flush the gitattributes cache. 0333 * 0334 * Call this if you have reason to believe that the attributes files on 0335 * disk no longer match the cached contents of memory. This will cause 0336 * the attributes files to be reloaded the next time that an attribute 0337 * access function is called. 0338 * 0339 * @param repo The repository containing the gitattributes cache 0340 * @return 0 on success, or an error code 0341 */ 0342 GIT_EXTERN(int) git_attr_cache_flush( 0343 git_repository *repo); 0344 0345 /** 0346 * Add a macro definition. 0347 * 0348 * Macros will automatically be loaded from the top level `.gitattributes` 0349 * file of the repository (plus the built-in "binary" macro). This 0350 * function allows you to add others. For example, to add the default 0351 * macro, you would call: 0352 * 0353 * git_attr_add_macro(repo, "binary", "-diff -crlf"); 0354 * 0355 * @param repo The repository to add the macro in. 0356 * @param name The name of the macro. 0357 * @param values The value for the macro. 0358 * @return 0 or an error code. 0359 */ 0360 GIT_EXTERN(int) git_attr_add_macro( 0361 git_repository *repo, 0362 const char *name, 0363 const char *values); 0364 0365 /** @} */ 0366 GIT_END_DECL 0367 #endif 0368
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |