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