Michael Haggerty | 86ab7f0 | 2011-08-12 23:43:04 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Handle git attributes. See gitattributes(5) for a description of |
Heba Waly | 3a1b341 | 2019-11-17 21:04:47 +0000 | [diff] [blame] | 3 | * the file syntax, and attr.h for a description of the API. |
Michael Haggerty | 86ab7f0 | 2011-08-12 23:43:04 +0200 | [diff] [blame] | 4 | * |
| 5 | * One basic design decision here is that we are not going to support |
| 6 | * an insanely large number of attributes. |
| 7 | */ |
| 8 | |
Elijah Newren | bc5c5ec | 2023-05-16 06:33:57 +0000 | [diff] [blame] | 9 | #include "git-compat-util.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 10 | #include "config.h" |
Elijah Newren | 32a8f51 | 2023-03-21 06:26:03 +0000 | [diff] [blame] | 11 | #include "environment.h" |
Stefan Beller | d807c4a | 2018-04-10 14:26:18 -0700 | [diff] [blame] | 12 | #include "exec-cmd.h" |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 13 | #include "attr.h" |
Brandon Casey | 6eba621 | 2011-10-11 10:53:31 -0500 | [diff] [blame] | 14 | #include "dir.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 15 | #include "gettext.h" |
Elijah Newren | c339932 | 2023-05-16 06:33:59 +0000 | [diff] [blame] | 16 | #include "path.h" |
Junio C Hamano | 27547e5 | 2015-04-16 10:48:58 -0700 | [diff] [blame] | 17 | #include "utf8.h" |
Nguyễn Thái Ngọc Duy | 860a74d | 2017-01-27 18:01:50 -0800 | [diff] [blame] | 18 | #include "quote.h" |
Elijah Newren | 08c46a4 | 2023-05-16 06:33:56 +0000 | [diff] [blame] | 19 | #include "read-cache-ll.h" |
Joanna Wang | 2232a88 | 2023-11-16 05:44:37 +0000 | [diff] [blame] | 20 | #include "refs.h" |
Karthik Nayak | 47cfc9b | 2023-01-14 09:30:38 +0100 | [diff] [blame] | 21 | #include "revision.h" |
Elijah Newren | a034e91 | 2023-05-16 06:34:06 +0000 | [diff] [blame] | 22 | #include "object-store-ll.h" |
Elijah Newren | e38da48 | 2023-03-21 06:26:05 +0000 | [diff] [blame] | 23 | #include "setup.h" |
Brandon Williams | 1a600b7 | 2017-01-27 18:02:01 -0800 | [diff] [blame] | 24 | #include "thread-utils.h" |
Elijah Newren | 0e312ea | 2023-04-22 20:17:28 +0000 | [diff] [blame] | 25 | #include "tree-walk.h" |
John Cai | 44451a2 | 2023-05-06 04:15:29 +0000 | [diff] [blame] | 26 | #include "object-name.h" |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 27 | |
John Cai | 9f9c40c | 2023-10-13 17:39:30 +0000 | [diff] [blame] | 28 | const char *git_attr_tree; |
| 29 | |
Junio C Hamano | a5e92ab | 2007-04-18 16:16:37 -0700 | [diff] [blame] | 30 | const char git_attr__true[] = "(builtin)true"; |
| 31 | const char git_attr__false[] = "\0(builtin)false"; |
| 32 | static const char git_attr__unknown[] = "(builtin)unknown"; |
| 33 | #define ATTR__TRUE git_attr__true |
| 34 | #define ATTR__FALSE git_attr__false |
| 35 | #define ATTR__UNSET NULL |
| 36 | #define ATTR__UNKNOWN git_attr__unknown |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 37 | |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 38 | struct git_attr { |
Patrick Steinhardt | e1e12e9 | 2022-12-01 15:45:36 +0100 | [diff] [blame] | 39 | unsigned int attr_nr; /* unique attribute number */ |
Brandon Williams | 1a600b7 | 2017-01-27 18:02:01 -0800 | [diff] [blame] | 40 | char name[FLEX_ARRAY]; /* attribute name */ |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 41 | }; |
Junio C Hamano | 7d42ec5 | 2017-01-27 18:01:53 -0800 | [diff] [blame] | 42 | |
Junio C Hamano | ec4d77a | 2017-01-27 18:01:48 -0800 | [diff] [blame] | 43 | const char *git_attr_name(const struct git_attr *attr) |
Michael Haggerty | 352404a | 2011-08-04 06:36:17 +0200 | [diff] [blame] | 44 | { |
| 45 | return attr->name; |
| 46 | } |
| 47 | |
Brandon Williams | 1a600b7 | 2017-01-27 18:02:01 -0800 | [diff] [blame] | 48 | struct attr_hashmap { |
| 49 | struct hashmap map; |
Brandon Williams | 1a600b7 | 2017-01-27 18:02:01 -0800 | [diff] [blame] | 50 | pthread_mutex_t mutex; |
Brandon Williams | 1a600b7 | 2017-01-27 18:02:01 -0800 | [diff] [blame] | 51 | }; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 52 | |
Brandon Williams | 1a600b7 | 2017-01-27 18:02:01 -0800 | [diff] [blame] | 53 | static inline void hashmap_lock(struct attr_hashmap *map) |
| 54 | { |
Brandon Williams | 1a600b7 | 2017-01-27 18:02:01 -0800 | [diff] [blame] | 55 | pthread_mutex_lock(&map->mutex); |
Brandon Williams | 1a600b7 | 2017-01-27 18:02:01 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static inline void hashmap_unlock(struct attr_hashmap *map) |
| 59 | { |
Brandon Williams | 1a600b7 | 2017-01-27 18:02:01 -0800 | [diff] [blame] | 60 | pthread_mutex_unlock(&map->mutex); |
Brandon Williams | 1a600b7 | 2017-01-27 18:02:01 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Brandon Williams | 1a600b7 | 2017-01-27 18:02:01 -0800 | [diff] [blame] | 63 | /* The container for objects stored in "struct attr_hashmap" */ |
| 64 | struct attr_hash_entry { |
Eric Wong | e2b5038 | 2019-10-06 23:30:43 +0000 | [diff] [blame] | 65 | struct hashmap_entry ent; |
Brandon Williams | 1a600b7 | 2017-01-27 18:02:01 -0800 | [diff] [blame] | 66 | const char *key; /* the key; memory should be owned by value */ |
| 67 | size_t keylen; /* length of the key */ |
| 68 | void *value; /* the stored value */ |
| 69 | }; |
| 70 | |
| 71 | /* attr_hashmap comparison function */ |
Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 19:09:48 +0200 | [diff] [blame] | 72 | static int attr_hash_entry_cmp(const void *cmp_data UNUSED, |
Eric Wong | 939af16 | 2019-10-06 23:30:37 +0000 | [diff] [blame] | 73 | const struct hashmap_entry *eptr, |
| 74 | const struct hashmap_entry *entry_or_key, |
Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 19:09:48 +0200 | [diff] [blame] | 75 | const void *keydata UNUSED) |
Brandon Williams | 1a600b7 | 2017-01-27 18:02:01 -0800 | [diff] [blame] | 76 | { |
Eric Wong | 939af16 | 2019-10-06 23:30:37 +0000 | [diff] [blame] | 77 | const struct attr_hash_entry *a, *b; |
| 78 | |
| 79 | a = container_of(eptr, const struct attr_hash_entry, ent); |
| 80 | b = container_of(entry_or_key, const struct attr_hash_entry, ent); |
Brandon Williams | 1a600b7 | 2017-01-27 18:02:01 -0800 | [diff] [blame] | 81 | return (a->keylen != b->keylen) || strncmp(a->key, b->key, a->keylen); |
| 82 | } |
| 83 | |
Elijah Newren | b19315d | 2020-11-11 20:02:20 +0000 | [diff] [blame] | 84 | /* |
| 85 | * The global dictionary of all interned attributes. This |
| 86 | * is a singleton object which is shared between threads. |
| 87 | * Access to this dictionary must be surrounded with a mutex. |
| 88 | */ |
| 89 | static struct attr_hashmap g_attr_hashmap = { |
Ævar Arnfjörð Bjarmason | 5010364 | 2022-03-17 18:27:17 +0100 | [diff] [blame] | 90 | .map = HASHMAP_INIT(attr_hash_entry_cmp, NULL), |
Elijah Newren | b19315d | 2020-11-11 20:02:20 +0000 | [diff] [blame] | 91 | }; |
Brandon Williams | 1a600b7 | 2017-01-27 18:02:01 -0800 | [diff] [blame] | 92 | |
| 93 | /* |
| 94 | * Retrieve the 'value' stored in a hashmap given the provided 'key'. |
| 95 | * If there is no matching entry, return NULL. |
| 96 | */ |
| 97 | static void *attr_hashmap_get(struct attr_hashmap *map, |
| 98 | const char *key, size_t keylen) |
| 99 | { |
| 100 | struct attr_hash_entry k; |
| 101 | struct attr_hash_entry *e; |
| 102 | |
Eric Wong | d22245a | 2019-10-06 23:30:27 +0000 | [diff] [blame] | 103 | hashmap_entry_init(&k.ent, memhash(key, keylen)); |
Brandon Williams | 1a600b7 | 2017-01-27 18:02:01 -0800 | [diff] [blame] | 104 | k.key = key; |
| 105 | k.keylen = keylen; |
Eric Wong | 404ab78 | 2019-10-06 23:30:42 +0000 | [diff] [blame] | 106 | e = hashmap_get_entry(&map->map, &k, ent, NULL); |
Brandon Williams | 1a600b7 | 2017-01-27 18:02:01 -0800 | [diff] [blame] | 107 | |
| 108 | return e ? e->value : NULL; |
| 109 | } |
| 110 | |
| 111 | /* Add 'value' to a hashmap based on the provided 'key'. */ |
| 112 | static void attr_hashmap_add(struct attr_hashmap *map, |
| 113 | const char *key, size_t keylen, |
| 114 | void *value) |
| 115 | { |
| 116 | struct attr_hash_entry *e; |
| 117 | |
Brandon Williams | 1a600b7 | 2017-01-27 18:02:01 -0800 | [diff] [blame] | 118 | e = xmalloc(sizeof(struct attr_hash_entry)); |
Eric Wong | d22245a | 2019-10-06 23:30:27 +0000 | [diff] [blame] | 119 | hashmap_entry_init(&e->ent, memhash(key, keylen)); |
Brandon Williams | 1a600b7 | 2017-01-27 18:02:01 -0800 | [diff] [blame] | 120 | e->key = key; |
| 121 | e->keylen = keylen; |
| 122 | e->value = value; |
| 123 | |
Eric Wong | b94e5c1 | 2019-10-06 23:30:29 +0000 | [diff] [blame] | 124 | hashmap_add(&map->map, &e->ent); |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Brandon Williams | 685b292 | 2017-01-27 18:02:02 -0800 | [diff] [blame] | 127 | struct all_attrs_item { |
| 128 | const struct git_attr *attr; |
| 129 | const char *value; |
Brandon Williams | 60a1272 | 2017-01-27 18:02:03 -0800 | [diff] [blame] | 130 | /* |
| 131 | * If 'macro' is non-NULL, indicates that 'attr' is a macro based on |
| 132 | * the current attribute stack and contains a pointer to the match_attr |
| 133 | * definition of the macro |
| 134 | */ |
| 135 | const struct match_attr *macro; |
Brandon Williams | 685b292 | 2017-01-27 18:02:02 -0800 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | /* |
| 139 | * Reallocate and reinitialize the array of all attributes (which is used in |
| 140 | * the attribute collection process) in 'check' based on the global dictionary |
| 141 | * of attributes. |
| 142 | */ |
| 143 | static void all_attrs_init(struct attr_hashmap *map, struct attr_check *check) |
| 144 | { |
| 145 | int i; |
Jeff Hostetler | 8b604d1 | 2017-09-06 15:43:48 +0000 | [diff] [blame] | 146 | unsigned int size; |
Brandon Williams | 685b292 | 2017-01-27 18:02:02 -0800 | [diff] [blame] | 147 | |
| 148 | hashmap_lock(map); |
| 149 | |
Jeff Hostetler | 8b604d1 | 2017-09-06 15:43:48 +0000 | [diff] [blame] | 150 | size = hashmap_get_size(&map->map); |
| 151 | if (size < check->all_attrs_nr) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 152 | BUG("interned attributes shouldn't be deleted"); |
Brandon Williams | 685b292 | 2017-01-27 18:02:02 -0800 | [diff] [blame] | 153 | |
| 154 | /* |
| 155 | * If the number of attributes in the global dictionary has increased |
| 156 | * (or this attr_check instance doesn't have an initialized all_attrs |
| 157 | * field), reallocate the provided attr_check instance's all_attrs |
| 158 | * field and fill each entry with its corresponding git_attr. |
| 159 | */ |
Jeff Hostetler | 8b604d1 | 2017-09-06 15:43:48 +0000 | [diff] [blame] | 160 | if (size != check->all_attrs_nr) { |
Brandon Williams | 685b292 | 2017-01-27 18:02:02 -0800 | [diff] [blame] | 161 | struct attr_hash_entry *e; |
| 162 | struct hashmap_iter iter; |
Brandon Williams | 685b292 | 2017-01-27 18:02:02 -0800 | [diff] [blame] | 163 | |
Jeff Hostetler | 8b604d1 | 2017-09-06 15:43:48 +0000 | [diff] [blame] | 164 | REALLOC_ARRAY(check->all_attrs, size); |
| 165 | check->all_attrs_nr = size; |
Brandon Williams | 685b292 | 2017-01-27 18:02:02 -0800 | [diff] [blame] | 166 | |
Eric Wong | 87571c3 | 2019-10-06 23:30:38 +0000 | [diff] [blame] | 167 | hashmap_for_each_entry(&map->map, &iter, e, |
Eric Wong | 87571c3 | 2019-10-06 23:30:38 +0000 | [diff] [blame] | 168 | ent /* member name */) { |
Brandon Williams | 685b292 | 2017-01-27 18:02:02 -0800 | [diff] [blame] | 169 | const struct git_attr *a = e->value; |
| 170 | check->all_attrs[a->attr_nr].attr = a; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | hashmap_unlock(map); |
| 175 | |
| 176 | /* |
| 177 | * Re-initialize every entry in check->all_attrs. |
| 178 | * This re-initialization can live outside of the locked region since |
| 179 | * the attribute dictionary is no longer being accessed. |
| 180 | */ |
| 181 | for (i = 0; i < check->all_attrs_nr; i++) { |
| 182 | check->all_attrs[i].value = ATTR__UNKNOWN; |
Brandon Williams | 60a1272 | 2017-01-27 18:02:03 -0800 | [diff] [blame] | 183 | check->all_attrs[i].macro = NULL; |
Brandon Williams | 685b292 | 2017-01-27 18:02:02 -0800 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
Joanna Wang | 2232a88 | 2023-11-16 05:44:37 +0000 | [diff] [blame] | 187 | /* |
| 188 | * Atribute name cannot begin with "builtin_" which |
| 189 | * is a reserved namespace for built in attributes values. |
| 190 | */ |
| 191 | static int attr_name_reserved(const char *name) |
| 192 | { |
| 193 | return starts_with(name, "builtin_"); |
| 194 | } |
| 195 | |
Junio C Hamano | 428103c | 2017-01-27 18:02:00 -0800 | [diff] [blame] | 196 | static int attr_name_valid(const char *name, size_t namelen) |
Junio C Hamano | e4aee10 | 2007-04-15 14:56:09 -0700 | [diff] [blame] | 197 | { |
| 198 | /* |
Michael Haggerty | d42453a | 2011-08-04 06:36:13 +0200 | [diff] [blame] | 199 | * Attribute name cannot begin with '-' and must consist of |
| 200 | * characters from [-A-Za-z0-9_.]. |
Junio C Hamano | e4aee10 | 2007-04-15 14:56:09 -0700 | [diff] [blame] | 201 | */ |
Michael Haggerty | c0b13b2 | 2011-08-04 06:36:14 +0200 | [diff] [blame] | 202 | if (namelen <= 0 || *name == '-') |
Junio C Hamano | 428103c | 2017-01-27 18:02:00 -0800 | [diff] [blame] | 203 | return 0; |
Junio C Hamano | e4aee10 | 2007-04-15 14:56:09 -0700 | [diff] [blame] | 204 | while (namelen--) { |
| 205 | char ch = *name++; |
| 206 | if (! (ch == '-' || ch == '.' || ch == '_' || |
| 207 | ('0' <= ch && ch <= '9') || |
| 208 | ('a' <= ch && ch <= 'z') || |
| 209 | ('A' <= ch && ch <= 'Z')) ) |
Junio C Hamano | 428103c | 2017-01-27 18:02:00 -0800 | [diff] [blame] | 210 | return 0; |
Junio C Hamano | e4aee10 | 2007-04-15 14:56:09 -0700 | [diff] [blame] | 211 | } |
Junio C Hamano | 428103c | 2017-01-27 18:02:00 -0800 | [diff] [blame] | 212 | return 1; |
| 213 | } |
| 214 | |
| 215 | static void report_invalid_attr(const char *name, size_t len, |
| 216 | const char *src, int lineno) |
| 217 | { |
| 218 | struct strbuf err = STRBUF_INIT; |
| 219 | strbuf_addf(&err, _("%.*s is not a valid attribute name"), |
| 220 | (int) len, name); |
| 221 | fprintf(stderr, "%s: %s:%d\n", err.buf, src, lineno); |
| 222 | strbuf_release(&err); |
Junio C Hamano | e4aee10 | 2007-04-15 14:56:09 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Brandon Williams | 1a600b7 | 2017-01-27 18:02:01 -0800 | [diff] [blame] | 225 | /* |
| 226 | * Given a 'name', lookup and return the corresponding attribute in the global |
| 227 | * dictionary. If no entry is found, create a new attribute and store it in |
| 228 | * the dictionary. |
| 229 | */ |
Patrick Steinhardt | eb22e7d | 2022-12-01 15:45:15 +0100 | [diff] [blame] | 230 | static const struct git_attr *git_attr_internal(const char *name, size_t namelen) |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 231 | { |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 232 | struct git_attr *a; |
| 233 | |
Brandon Williams | 1a600b7 | 2017-01-27 18:02:01 -0800 | [diff] [blame] | 234 | if (!attr_name_valid(name, namelen)) |
Junio C Hamano | e4aee10 | 2007-04-15 14:56:09 -0700 | [diff] [blame] | 235 | return NULL; |
| 236 | |
Brandon Williams | 1a600b7 | 2017-01-27 18:02:01 -0800 | [diff] [blame] | 237 | hashmap_lock(&g_attr_hashmap); |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 238 | |
Brandon Williams | 1a600b7 | 2017-01-27 18:02:01 -0800 | [diff] [blame] | 239 | a = attr_hashmap_get(&g_attr_hashmap, name, namelen); |
| 240 | |
| 241 | if (!a) { |
| 242 | FLEX_ALLOC_MEM(a, name, name, namelen); |
Jeff Hostetler | 8b604d1 | 2017-09-06 15:43:48 +0000 | [diff] [blame] | 243 | a->attr_nr = hashmap_get_size(&g_attr_hashmap.map); |
Brandon Williams | 1a600b7 | 2017-01-27 18:02:01 -0800 | [diff] [blame] | 244 | |
| 245 | attr_hashmap_add(&g_attr_hashmap, a->name, namelen, a); |
Patrick Steinhardt | e1e12e9 | 2022-12-01 15:45:36 +0100 | [diff] [blame] | 246 | if (a->attr_nr != hashmap_get_size(&g_attr_hashmap.map) - 1) |
| 247 | die(_("unable to add additional attribute")); |
Brandon Williams | 1a600b7 | 2017-01-27 18:02:01 -0800 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | hashmap_unlock(&g_attr_hashmap); |
| 251 | |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 252 | return a; |
| 253 | } |
| 254 | |
Brandon Williams | e810e06 | 2017-01-27 18:02:04 -0800 | [diff] [blame] | 255 | const struct git_attr *git_attr(const char *name) |
Junio C Hamano | 7fb0eaa | 2010-01-16 20:39:59 -0800 | [diff] [blame] | 256 | { |
| 257 | return git_attr_internal(name, strlen(name)); |
| 258 | } |
| 259 | |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 260 | /* What does a matched pattern decide? */ |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 261 | struct attr_state { |
Brandon Williams | e810e06 | 2017-01-27 18:02:04 -0800 | [diff] [blame] | 262 | const struct git_attr *attr; |
Junio C Hamano | a5e92ab | 2007-04-18 16:16:37 -0700 | [diff] [blame] | 263 | const char *setto; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 264 | }; |
| 265 | |
Nguyễn Thái Ngọc Duy | 82dce99 | 2012-10-15 13:24:39 +0700 | [diff] [blame] | 266 | struct pattern { |
| 267 | const char *pattern; |
| 268 | int patternlen; |
| 269 | int nowildcardlen; |
Derrick Stolee | 4ff89ee | 2019-09-03 11:04:56 -0700 | [diff] [blame] | 270 | unsigned flags; /* PATTERN_FLAG_* */ |
Nguyễn Thái Ngọc Duy | 82dce99 | 2012-10-15 13:24:39 +0700 | [diff] [blame] | 271 | }; |
| 272 | |
Michael Haggerty | ba845b7 | 2011-08-12 23:43:05 +0200 | [diff] [blame] | 273 | /* |
| 274 | * One rule, as from a .gitattributes file. |
| 275 | * |
| 276 | * If is_macro is true, then u.attr is a pointer to the git_attr being |
| 277 | * defined. |
| 278 | * |
Junio C Hamano | 4894f4f | 2017-01-27 18:01:43 -0800 | [diff] [blame] | 279 | * If is_macro is false, then u.pat is the filename pattern to which the |
| 280 | * rule applies. |
Michael Haggerty | ba845b7 | 2011-08-12 23:43:05 +0200 | [diff] [blame] | 281 | * |
| 282 | * In either case, num_attr is the number of attributes affected by |
| 283 | * this rule, and state is an array listing them. The attributes are |
| 284 | * listed as they appear in the file (macros unexpanded). |
| 285 | */ |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 286 | struct match_attr { |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 287 | union { |
Nguyễn Thái Ngọc Duy | 82dce99 | 2012-10-15 13:24:39 +0700 | [diff] [blame] | 288 | struct pattern pat; |
Brandon Williams | e810e06 | 2017-01-27 18:02:04 -0800 | [diff] [blame] | 289 | const struct git_attr *attr; |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 290 | } u; |
| 291 | char is_macro; |
Patrick Steinhardt | 34ace8b | 2022-12-01 15:45:27 +0100 | [diff] [blame] | 292 | size_t num_attr; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 293 | struct attr_state state[FLEX_ARRAY]; |
| 294 | }; |
| 295 | |
| 296 | static const char blank[] = " \t\r\n"; |
| 297 | |
Jeff King | dbf387d | 2021-02-16 09:44:25 -0500 | [diff] [blame] | 298 | /* Flags usable in read_attr() and parse_attr_line() family of functions. */ |
| 299 | #define READ_ATTR_MACRO_OK (1<<0) |
Jeff King | 2ef579e2 | 2021-02-16 09:44:32 -0500 | [diff] [blame] | 300 | #define READ_ATTR_NOFOLLOW (1<<1) |
Jeff King | dbf387d | 2021-02-16 09:44:25 -0500 | [diff] [blame] | 301 | |
Michael Haggerty | d175129 | 2011-08-12 23:43:07 +0200 | [diff] [blame] | 302 | /* |
| 303 | * Parse a whitespace-delimited attribute state (i.e., "attr", |
| 304 | * "-attr", "!attr", or "attr=value") from the string starting at src. |
| 305 | * If e is not NULL, write the results to *e. Return a pointer to the |
| 306 | * remainder of the string (with leading whitespace removed), or NULL |
| 307 | * if there was an error. |
| 308 | */ |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 309 | static const char *parse_attr(const char *src, int lineno, const char *cp, |
Michael Haggerty | d175129 | 2011-08-12 23:43:07 +0200 | [diff] [blame] | 310 | struct attr_state *e) |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 311 | { |
| 312 | const char *ep, *equals; |
Patrick Steinhardt | 2455720 | 2022-12-01 15:45:23 +0100 | [diff] [blame] | 313 | size_t len; |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 314 | |
| 315 | ep = cp + strcspn(cp, blank); |
| 316 | equals = strchr(cp, '='); |
| 317 | if (equals && ep < equals) |
| 318 | equals = NULL; |
| 319 | if (equals) |
| 320 | len = equals - cp; |
| 321 | else |
| 322 | len = ep - cp; |
Michael Haggerty | d175129 | 2011-08-12 23:43:07 +0200 | [diff] [blame] | 323 | if (!e) { |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 324 | if (*cp == '-' || *cp == '!') { |
| 325 | cp++; |
| 326 | len--; |
| 327 | } |
Joanna Wang | 2232a88 | 2023-11-16 05:44:37 +0000 | [diff] [blame] | 328 | if (!attr_name_valid(cp, len) || attr_name_reserved(cp)) { |
Junio C Hamano | 428103c | 2017-01-27 18:02:00 -0800 | [diff] [blame] | 329 | report_invalid_attr(cp, len, src, lineno); |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 330 | return NULL; |
| 331 | } |
| 332 | } else { |
Junio C Hamano | 5a88401 | 2017-01-27 18:01:44 -0800 | [diff] [blame] | 333 | /* |
| 334 | * As this function is always called twice, once with |
| 335 | * e == NULL in the first pass and then e != NULL in |
Junio C Hamano | 428103c | 2017-01-27 18:02:00 -0800 | [diff] [blame] | 336 | * the second pass, no need for attr_name_valid() |
Junio C Hamano | 5a88401 | 2017-01-27 18:01:44 -0800 | [diff] [blame] | 337 | * check here. |
| 338 | */ |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 339 | if (*cp == '-' || *cp == '!') { |
| 340 | e->setto = (*cp == '-') ? ATTR__FALSE : ATTR__UNSET; |
| 341 | cp++; |
| 342 | len--; |
| 343 | } |
| 344 | else if (!equals) |
| 345 | e->setto = ATTR__TRUE; |
| 346 | else { |
Pierre Habouzit | 182af83 | 2007-09-16 00:32:36 +0200 | [diff] [blame] | 347 | e->setto = xmemdupz(equals + 1, ep - equals - 1); |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 348 | } |
Junio C Hamano | 7fb0eaa | 2010-01-16 20:39:59 -0800 | [diff] [blame] | 349 | e->attr = git_attr_internal(cp, len); |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 350 | } |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 351 | return ep + strspn(ep, blank); |
| 352 | } |
| 353 | |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 354 | static struct match_attr *parse_attr_line(const char *line, const char *src, |
Jeff King | dbf387d | 2021-02-16 09:44:25 -0500 | [diff] [blame] | 355 | int lineno, unsigned flags) |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 356 | { |
Patrick Steinhardt | 34ace8b | 2022-12-01 15:45:27 +0100 | [diff] [blame] | 357 | size_t namelen, num_attr, i; |
Michael Haggerty | 85c4a0d | 2011-08-12 23:43:08 +0200 | [diff] [blame] | 358 | const char *cp, *name, *states; |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 359 | struct match_attr *res = NULL; |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 360 | int is_macro; |
Nguyễn Thái Ngọc Duy | 860a74d | 2017-01-27 18:01:50 -0800 | [diff] [blame] | 361 | struct strbuf pattern = STRBUF_INIT; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 362 | |
| 363 | cp = line + strspn(line, blank); |
| 364 | if (!*cp || *cp == '#') |
| 365 | return NULL; |
| 366 | name = cp; |
Nguyễn Thái Ngọc Duy | 860a74d | 2017-01-27 18:01:50 -0800 | [diff] [blame] | 367 | |
Patrick Steinhardt | dfa6b32 | 2022-12-01 15:45:48 +0100 | [diff] [blame] | 368 | if (strlen(line) >= ATTR_MAX_LINE_LENGTH) { |
| 369 | warning(_("ignoring overly long attributes line %d"), lineno); |
| 370 | return NULL; |
| 371 | } |
| 372 | |
Nguyễn Thái Ngọc Duy | 860a74d | 2017-01-27 18:01:50 -0800 | [diff] [blame] | 373 | if (*cp == '"' && !unquote_c_style(&pattern, name, &states)) { |
| 374 | name = pattern.buf; |
| 375 | namelen = pattern.len; |
| 376 | } else { |
| 377 | namelen = strcspn(name, blank); |
| 378 | states = name + namelen; |
| 379 | } |
| 380 | |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 381 | if (strlen(ATTRIBUTE_MACRO_PREFIX) < namelen && |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 382 | starts_with(name, ATTRIBUTE_MACRO_PREFIX)) { |
Jeff King | dbf387d | 2021-02-16 09:44:25 -0500 | [diff] [blame] | 383 | if (!(flags & READ_ATTR_MACRO_OK)) { |
Nguyễn Thái Ngọc Duy | ad8f8f4 | 2018-11-10 06:16:03 +0100 | [diff] [blame] | 384 | fprintf_ln(stderr, _("%s not allowed: %s:%d"), |
| 385 | name, src, lineno); |
Junio C Hamano | 62af896 | 2017-01-27 18:01:49 -0800 | [diff] [blame] | 386 | goto fail_return; |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 387 | } |
| 388 | is_macro = 1; |
| 389 | name += strlen(ATTRIBUTE_MACRO_PREFIX); |
| 390 | name += strspn(name, blank); |
| 391 | namelen = strcspn(name, blank); |
Joanna Wang | 2232a88 | 2023-11-16 05:44:37 +0000 | [diff] [blame] | 392 | if (!attr_name_valid(name, namelen) || attr_name_reserved(name)) { |
Junio C Hamano | 428103c | 2017-01-27 18:02:00 -0800 | [diff] [blame] | 393 | report_invalid_attr(name, namelen, src, lineno); |
Junio C Hamano | 62af896 | 2017-01-27 18:01:49 -0800 | [diff] [blame] | 394 | goto fail_return; |
Junio C Hamano | e4aee10 | 2007-04-15 14:56:09 -0700 | [diff] [blame] | 395 | } |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 396 | } |
| 397 | else |
| 398 | is_macro = 0; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 399 | |
Michael Haggerty | 85c4a0d | 2011-08-12 23:43:08 +0200 | [diff] [blame] | 400 | states += strspn(states, blank); |
| 401 | |
Michael Haggerty | d68e1c1 | 2011-08-12 23:43:10 +0200 | [diff] [blame] | 402 | /* First pass to count the attr_states */ |
| 403 | for (cp = states, num_attr = 0; *cp; num_attr++) { |
| 404 | cp = parse_attr(src, lineno, cp, NULL); |
| 405 | if (!cp) |
Junio C Hamano | 62af896 | 2017-01-27 18:01:49 -0800 | [diff] [blame] | 406 | goto fail_return; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 407 | } |
Michael Haggerty | d68e1c1 | 2011-08-12 23:43:10 +0200 | [diff] [blame] | 408 | |
Patrick Steinhardt | a60a66e | 2022-12-01 15:45:40 +0100 | [diff] [blame] | 409 | res = xcalloc(1, st_add3(sizeof(*res), |
| 410 | st_mult(sizeof(struct attr_state), num_attr), |
| 411 | is_macro ? 0 : namelen + 1)); |
Nguyễn Thái Ngọc Duy | fad32bc | 2014-12-28 06:39:47 +0700 | [diff] [blame] | 412 | if (is_macro) { |
Michael Haggerty | d68e1c1 | 2011-08-12 23:43:10 +0200 | [diff] [blame] | 413 | res->u.attr = git_attr_internal(name, namelen); |
Nguyễn Thái Ngọc Duy | fad32bc | 2014-12-28 06:39:47 +0700 | [diff] [blame] | 414 | } else { |
Nguyễn Thái Ngọc Duy | 82dce99 | 2012-10-15 13:24:39 +0700 | [diff] [blame] | 415 | char *p = (char *)&(res->state[num_attr]); |
| 416 | memcpy(p, name, namelen); |
| 417 | res->u.pat.pattern = p; |
Derrick Stolee | 65edd96 | 2019-09-03 11:04:57 -0700 | [diff] [blame] | 418 | parse_path_pattern(&res->u.pat.pattern, |
Nguyễn Thái Ngọc Duy | 82dce99 | 2012-10-15 13:24:39 +0700 | [diff] [blame] | 419 | &res->u.pat.patternlen, |
| 420 | &res->u.pat.flags, |
| 421 | &res->u.pat.nowildcardlen); |
Derrick Stolee | 4ff89ee | 2019-09-03 11:04:56 -0700 | [diff] [blame] | 422 | if (res->u.pat.flags & PATTERN_FLAG_NEGATIVE) { |
Thomas Rast | 8b1bd02 | 2013-03-01 21:06:17 +0100 | [diff] [blame] | 423 | warning(_("Negative patterns are ignored in git attributes\n" |
| 424 | "Use '\\!' for literal leading exclamation.")); |
Junio C Hamano | 62af896 | 2017-01-27 18:01:49 -0800 | [diff] [blame] | 425 | goto fail_return; |
Thomas Rast | 8b1bd02 | 2013-03-01 21:06:17 +0100 | [diff] [blame] | 426 | } |
Michael Haggerty | d68e1c1 | 2011-08-12 23:43:10 +0200 | [diff] [blame] | 427 | } |
| 428 | res->is_macro = is_macro; |
| 429 | res->num_attr = num_attr; |
| 430 | |
| 431 | /* Second pass to fill the attr_states */ |
| 432 | for (cp = states, i = 0; *cp; i++) { |
| 433 | cp = parse_attr(src, lineno, cp, &(res->state[i])); |
| 434 | } |
| 435 | |
Nguyễn Thái Ngọc Duy | 860a74d | 2017-01-27 18:01:50 -0800 | [diff] [blame] | 436 | strbuf_release(&pattern); |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 437 | return res; |
Junio C Hamano | 62af896 | 2017-01-27 18:01:49 -0800 | [diff] [blame] | 438 | |
| 439 | fail_return: |
Nguyễn Thái Ngọc Duy | 860a74d | 2017-01-27 18:01:50 -0800 | [diff] [blame] | 440 | strbuf_release(&pattern); |
Junio C Hamano | 62af896 | 2017-01-27 18:01:49 -0800 | [diff] [blame] | 441 | free(res); |
| 442 | return NULL; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /* |
| 446 | * Like info/exclude and .gitignore, the attribute information can |
| 447 | * come from many places. |
| 448 | * |
Robert P. J. Day | bb101aa | 2019-03-06 04:14:44 -0500 | [diff] [blame] | 449 | * (1) .gitattributes file of the same directory; |
| 450 | * (2) .gitattributes file of the parent directory if (1) does not have |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 451 | * any match; this goes recursively upwards, just like .gitignore. |
| 452 | * (3) $GIT_DIR/info/attributes, which overrides both of the above. |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 453 | * |
| 454 | * In the same file, later entries override the earlier match, so in the |
| 455 | * global list, we would have entries from info/attributes the earliest |
Robert P. J. Day | bb101aa | 2019-03-06 04:14:44 -0500 | [diff] [blame] | 456 | * (reading the file from top to bottom), .gitattributes of the root |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 457 | * directory (again, reading the file from top to bottom) down to the |
| 458 | * current directory, and then scan the list backwards to find the first match. |
Adam Spiers | 6d24e7a | 2012-12-27 02:32:25 +0000 | [diff] [blame] | 459 | * This is exactly the same as what is_excluded() does in dir.c to deal with |
Junio C Hamano | 6f416cb | 2017-01-27 18:01:45 -0800 | [diff] [blame] | 460 | * .gitignore file and info/excludes file as a fallback. |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 461 | */ |
| 462 | |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 463 | struct attr_stack { |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 464 | struct attr_stack *prev; |
| 465 | char *origin; |
Nguyễn Thái Ngọc Duy | cd6a0b2 | 2012-10-05 11:41:01 +0700 | [diff] [blame] | 466 | size_t originlen; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 467 | unsigned num_matches; |
Junio C Hamano | a441311 | 2007-08-14 01:40:45 -0700 | [diff] [blame] | 468 | unsigned alloc; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 469 | struct match_attr **attrs; |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 470 | }; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 471 | |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 472 | static void attr_stack_free(struct attr_stack *e) |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 473 | { |
Patrick Steinhardt | 447ac90 | 2022-12-01 15:45:31 +0100 | [diff] [blame] | 474 | unsigned i; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 475 | free(e->origin); |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 476 | for (i = 0; i < e->num_matches; i++) { |
| 477 | struct match_attr *a = e->attrs[i]; |
Patrick Steinhardt | 34ace8b | 2022-12-01 15:45:27 +0100 | [diff] [blame] | 478 | size_t j; |
| 479 | |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 480 | for (j = 0; j < a->num_attr; j++) { |
Junio C Hamano | a5e92ab | 2007-04-18 16:16:37 -0700 | [diff] [blame] | 481 | const char *setto = a->state[j].setto; |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 482 | if (setto == ATTR__TRUE || |
| 483 | setto == ATTR__FALSE || |
| 484 | setto == ATTR__UNSET || |
| 485 | setto == ATTR__UNKNOWN) |
| 486 | ; |
| 487 | else |
Felipe Contreras | 4b25d09 | 2009-05-01 12:06:36 +0300 | [diff] [blame] | 488 | free((char *) setto); |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 489 | } |
| 490 | free(a); |
| 491 | } |
Jeff King | 37475f9 | 2012-01-11 22:05:03 -0500 | [diff] [blame] | 492 | free(e->attrs); |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 493 | free(e); |
| 494 | } |
| 495 | |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 496 | static void drop_attr_stack(struct attr_stack **stack) |
| 497 | { |
| 498 | while (*stack) { |
| 499 | struct attr_stack *elem = *stack; |
| 500 | *stack = elem->prev; |
| 501 | attr_stack_free(elem); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | /* List of all attr_check structs; access should be surrounded by mutex */ |
| 506 | static struct check_vector { |
| 507 | size_t nr; |
| 508 | size_t alloc; |
| 509 | struct attr_check **checks; |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 510 | pthread_mutex_t mutex; |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 511 | } check_vector; |
| 512 | |
| 513 | static inline void vector_lock(void) |
| 514 | { |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 515 | pthread_mutex_lock(&check_vector.mutex); |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | static inline void vector_unlock(void) |
| 519 | { |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 520 | pthread_mutex_unlock(&check_vector.mutex); |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | static void check_vector_add(struct attr_check *c) |
| 524 | { |
| 525 | vector_lock(); |
| 526 | |
| 527 | ALLOC_GROW(check_vector.checks, |
| 528 | check_vector.nr + 1, |
| 529 | check_vector.alloc); |
| 530 | check_vector.checks[check_vector.nr++] = c; |
| 531 | |
| 532 | vector_unlock(); |
| 533 | } |
| 534 | |
| 535 | static void check_vector_remove(struct attr_check *check) |
| 536 | { |
| 537 | int i; |
| 538 | |
| 539 | vector_lock(); |
| 540 | |
| 541 | /* Find entry */ |
| 542 | for (i = 0; i < check_vector.nr; i++) |
| 543 | if (check_vector.checks[i] == check) |
| 544 | break; |
| 545 | |
| 546 | if (i >= check_vector.nr) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 547 | BUG("no entry found"); |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 548 | |
| 549 | /* shift entries over */ |
| 550 | for (; i < check_vector.nr - 1; i++) |
| 551 | check_vector.checks[i] = check_vector.checks[i + 1]; |
| 552 | |
| 553 | check_vector.nr--; |
| 554 | |
| 555 | vector_unlock(); |
| 556 | } |
| 557 | |
| 558 | /* Iterate through all attr_check instances and drop their stacks */ |
| 559 | static void drop_all_attr_stacks(void) |
| 560 | { |
| 561 | int i; |
| 562 | |
| 563 | vector_lock(); |
| 564 | |
| 565 | for (i = 0; i < check_vector.nr; i++) { |
| 566 | drop_attr_stack(&check_vector.checks[i]->stack); |
| 567 | } |
| 568 | |
| 569 | vector_unlock(); |
| 570 | } |
| 571 | |
Junio C Hamano | 3729376 | 2017-01-30 10:05:20 -0800 | [diff] [blame] | 572 | struct attr_check *attr_check_alloc(void) |
| 573 | { |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 574 | struct attr_check *c = xcalloc(1, sizeof(struct attr_check)); |
| 575 | |
| 576 | /* save pointer to the check struct */ |
| 577 | check_vector_add(c); |
| 578 | |
| 579 | return c; |
Junio C Hamano | 3729376 | 2017-01-30 10:05:20 -0800 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | struct attr_check *attr_check_initl(const char *one, ...) |
| 583 | { |
| 584 | struct attr_check *check; |
| 585 | int cnt; |
| 586 | va_list params; |
| 587 | const char *param; |
| 588 | |
| 589 | va_start(params, one); |
| 590 | for (cnt = 1; (param = va_arg(params, const char *)) != NULL; cnt++) |
| 591 | ; |
| 592 | va_end(params); |
| 593 | |
| 594 | check = attr_check_alloc(); |
| 595 | check->nr = cnt; |
| 596 | check->alloc = cnt; |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 597 | CALLOC_ARRAY(check->items, cnt); |
Junio C Hamano | 3729376 | 2017-01-30 10:05:20 -0800 | [diff] [blame] | 598 | |
| 599 | check->items[0].attr = git_attr(one); |
| 600 | va_start(params, one); |
| 601 | for (cnt = 1; cnt < check->nr; cnt++) { |
| 602 | const struct git_attr *attr; |
| 603 | param = va_arg(params, const char *); |
| 604 | if (!param) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 605 | BUG("counted %d != ended at %d", |
Junio C Hamano | 3729376 | 2017-01-30 10:05:20 -0800 | [diff] [blame] | 606 | check->nr, cnt); |
| 607 | attr = git_attr(param); |
| 608 | if (!attr) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 609 | BUG("%s: not a valid attribute name", param); |
Junio C Hamano | 3729376 | 2017-01-30 10:05:20 -0800 | [diff] [blame] | 610 | check->items[cnt].attr = attr; |
| 611 | } |
| 612 | va_end(params); |
| 613 | return check; |
| 614 | } |
| 615 | |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 616 | struct attr_check *attr_check_dup(const struct attr_check *check) |
| 617 | { |
| 618 | struct attr_check *ret; |
| 619 | |
| 620 | if (!check) |
| 621 | return NULL; |
| 622 | |
| 623 | ret = attr_check_alloc(); |
| 624 | |
| 625 | ret->nr = check->nr; |
| 626 | ret->alloc = check->alloc; |
René Scharfe | 6e57841 | 2023-01-01 22:16:48 +0100 | [diff] [blame] | 627 | DUP_ARRAY(ret->items, check->items, ret->nr); |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 628 | |
| 629 | return ret; |
| 630 | } |
| 631 | |
Junio C Hamano | 3729376 | 2017-01-30 10:05:20 -0800 | [diff] [blame] | 632 | struct attr_check_item *attr_check_append(struct attr_check *check, |
| 633 | const struct git_attr *attr) |
| 634 | { |
| 635 | struct attr_check_item *item; |
| 636 | |
| 637 | ALLOC_GROW(check->items, check->nr + 1, check->alloc); |
| 638 | item = &check->items[check->nr++]; |
| 639 | item->attr = attr; |
| 640 | return item; |
| 641 | } |
| 642 | |
| 643 | void attr_check_reset(struct attr_check *check) |
| 644 | { |
| 645 | check->nr = 0; |
| 646 | } |
| 647 | |
| 648 | void attr_check_clear(struct attr_check *check) |
| 649 | { |
Ævar Arnfjörð Bjarmason | 6a83d90 | 2017-06-15 23:15:46 +0000 | [diff] [blame] | 650 | FREE_AND_NULL(check->items); |
Junio C Hamano | 3729376 | 2017-01-30 10:05:20 -0800 | [diff] [blame] | 651 | check->alloc = 0; |
| 652 | check->nr = 0; |
Brandon Williams | 685b292 | 2017-01-27 18:02:02 -0800 | [diff] [blame] | 653 | |
Ævar Arnfjörð Bjarmason | 6a83d90 | 2017-06-15 23:15:46 +0000 | [diff] [blame] | 654 | FREE_AND_NULL(check->all_attrs); |
Brandon Williams | 685b292 | 2017-01-27 18:02:02 -0800 | [diff] [blame] | 655 | check->all_attrs_nr = 0; |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 656 | |
| 657 | drop_attr_stack(&check->stack); |
Junio C Hamano | 3729376 | 2017-01-30 10:05:20 -0800 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | void attr_check_free(struct attr_check *check) |
| 661 | { |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 662 | if (check) { |
| 663 | /* Remove check from the check vector */ |
| 664 | check_vector_remove(check); |
| 665 | |
| 666 | attr_check_clear(check); |
| 667 | free(check); |
| 668 | } |
Junio C Hamano | 3729376 | 2017-01-30 10:05:20 -0800 | [diff] [blame] | 669 | } |
| 670 | |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 671 | static const char *builtin_attr[] = { |
Junio C Hamano | 155a4b7 | 2012-09-08 21:28:55 -0700 | [diff] [blame] | 672 | "[attr]binary -diff -merge -text", |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 673 | NULL, |
| 674 | }; |
| 675 | |
Junio C Hamano | a441311 | 2007-08-14 01:40:45 -0700 | [diff] [blame] | 676 | static void handle_attr_line(struct attr_stack *res, |
| 677 | const char *line, |
| 678 | const char *src, |
| 679 | int lineno, |
Jeff King | dbf387d | 2021-02-16 09:44:25 -0500 | [diff] [blame] | 680 | unsigned flags) |
Junio C Hamano | a441311 | 2007-08-14 01:40:45 -0700 | [diff] [blame] | 681 | { |
| 682 | struct match_attr *a; |
| 683 | |
Jeff King | dbf387d | 2021-02-16 09:44:25 -0500 | [diff] [blame] | 684 | a = parse_attr_line(line, src, lineno, flags); |
Junio C Hamano | a441311 | 2007-08-14 01:40:45 -0700 | [diff] [blame] | 685 | if (!a) |
| 686 | return; |
Patrick Steinhardt | 447ac90 | 2022-12-01 15:45:31 +0100 | [diff] [blame] | 687 | ALLOC_GROW_BY(res->attrs, res->num_matches, 1, res->alloc); |
| 688 | res->attrs[res->num_matches - 1] = a; |
Junio C Hamano | a441311 | 2007-08-14 01:40:45 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 691 | static struct attr_stack *read_attr_from_array(const char **list) |
| 692 | { |
| 693 | struct attr_stack *res; |
| 694 | const char *line; |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 695 | int lineno = 0; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 696 | |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 697 | CALLOC_ARRAY(res, 1); |
Junio C Hamano | a441311 | 2007-08-14 01:40:45 -0700 | [diff] [blame] | 698 | while ((line = *(list++)) != NULL) |
Jeff King | dbf387d | 2021-02-16 09:44:25 -0500 | [diff] [blame] | 699 | handle_attr_line(res, line, "[builtin]", ++lineno, |
| 700 | READ_ATTR_MACRO_OK); |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 701 | return res; |
| 702 | } |
| 703 | |
Junio C Hamano | 7d42ec5 | 2017-01-27 18:01:53 -0800 | [diff] [blame] | 704 | /* |
Brandon Williams | f0dd042 | 2017-01-27 18:02:07 -0800 | [diff] [blame] | 705 | * Callers into the attribute system assume there is a single, system-wide |
| 706 | * global state where attributes are read from and when the state is flipped by |
| 707 | * calling git_attr_set_direction(), the stack frames that have been |
Andrei Rybak | abcb66c | 2021-06-11 13:18:50 +0200 | [diff] [blame] | 708 | * constructed need to be discarded so that subsequent calls into the |
Brandon Williams | f0dd042 | 2017-01-27 18:02:07 -0800 | [diff] [blame] | 709 | * attribute system will lazily read from the right place. Since changing |
| 710 | * direction causes a global paradigm shift, it should not ever be called while |
| 711 | * another thread could potentially be calling into the attribute system. |
Junio C Hamano | 7d42ec5 | 2017-01-27 18:01:53 -0800 | [diff] [blame] | 712 | */ |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 713 | static enum git_attr_direction direction; |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 714 | |
Nguyễn Thái Ngọc Duy | c4500e2 | 2018-08-13 18:14:33 +0200 | [diff] [blame] | 715 | void git_attr_set_direction(enum git_attr_direction new_direction) |
Brandon Williams | f0dd042 | 2017-01-27 18:02:07 -0800 | [diff] [blame] | 716 | { |
| 717 | if (is_bare_repository() && new_direction != GIT_ATTR_INDEX) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 718 | BUG("non-INDEX attr direction in a bare repo"); |
Brandon Williams | f0dd042 | 2017-01-27 18:02:07 -0800 | [diff] [blame] | 719 | |
| 720 | if (new_direction != direction) |
| 721 | drop_all_attr_stacks(); |
| 722 | |
| 723 | direction = new_direction; |
Brandon Williams | f0dd042 | 2017-01-27 18:02:07 -0800 | [diff] [blame] | 724 | } |
| 725 | |
Jeff King | dbf387d | 2021-02-16 09:44:25 -0500 | [diff] [blame] | 726 | static struct attr_stack *read_attr_from_file(const char *path, unsigned flags) |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 727 | { |
Patrick Steinhardt | d74b1fd | 2022-12-01 15:45:44 +0100 | [diff] [blame] | 728 | struct strbuf buf = STRBUF_INIT; |
Jeff King | 2ef579e2 | 2021-02-16 09:44:32 -0500 | [diff] [blame] | 729 | int fd; |
| 730 | FILE *fp; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 731 | struct attr_stack *res; |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 732 | int lineno = 0; |
Patrick Steinhardt | 3c50032 | 2022-12-01 15:45:53 +0100 | [diff] [blame] | 733 | struct stat st; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 734 | |
Jeff King | 2ef579e2 | 2021-02-16 09:44:32 -0500 | [diff] [blame] | 735 | if (flags & READ_ATTR_NOFOLLOW) |
| 736 | fd = open_nofollow(path, O_RDONLY); |
| 737 | else |
| 738 | fd = open(path, O_RDONLY); |
| 739 | |
| 740 | if (fd < 0) { |
| 741 | warn_on_fopen_errors(path); |
Junio C Hamano | a441311 | 2007-08-14 01:40:45 -0700 | [diff] [blame] | 742 | return NULL; |
Jeff King | 2ef579e2 | 2021-02-16 09:44:32 -0500 | [diff] [blame] | 743 | } |
| 744 | fp = xfdopen(fd, "r"); |
Patrick Steinhardt | 3c50032 | 2022-12-01 15:45:53 +0100 | [diff] [blame] | 745 | if (fstat(fd, &st)) { |
| 746 | warning_errno(_("cannot fstat gitattributes file '%s'"), path); |
| 747 | fclose(fp); |
| 748 | return NULL; |
Junio C Hamano | 27547e5 | 2015-04-16 10:48:58 -0700 | [diff] [blame] | 749 | } |
Patrick Steinhardt | 3c50032 | 2022-12-01 15:45:53 +0100 | [diff] [blame] | 750 | if (st.st_size >= ATTR_MAX_FILE_SIZE) { |
| 751 | warning(_("ignoring overly large gitattributes file '%s'"), path); |
| 752 | fclose(fp); |
| 753 | return NULL; |
| 754 | } |
Jeff King | 2ef579e2 | 2021-02-16 09:44:32 -0500 | [diff] [blame] | 755 | |
Junio C Hamano | a441311 | 2007-08-14 01:40:45 -0700 | [diff] [blame] | 756 | CALLOC_ARRAY(res, 1); |
Patrick Steinhardt | d74b1fd | 2022-12-01 15:45:44 +0100 | [diff] [blame] | 757 | while (strbuf_getline(&buf, fp) != EOF) { |
| 758 | if (!lineno && starts_with(buf.buf, utf8_bom)) |
| 759 | strbuf_remove(&buf, 0, strlen(utf8_bom)); |
Junio C Hamano | 8a755ed | 2022-12-13 21:09:40 +0900 | [diff] [blame] | 760 | handle_attr_line(res, buf.buf, path, ++lineno, flags); |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 761 | } |
Patrick Steinhardt | d74b1fd | 2022-12-01 15:45:44 +0100 | [diff] [blame] | 762 | |
Junio C Hamano | 1a9d7e9 | 2007-08-14 01:41:02 -0700 | [diff] [blame] | 763 | fclose(fp); |
Patrick Steinhardt | d74b1fd | 2022-12-01 15:45:44 +0100 | [diff] [blame] | 764 | strbuf_release(&buf); |
Junio C Hamano | 1a9d7e9 | 2007-08-14 01:41:02 -0700 | [diff] [blame] | 765 | return res; |
| 766 | } |
| 767 | |
Karthik Nayak | 47cfc9b | 2023-01-14 09:30:38 +0100 | [diff] [blame] | 768 | static struct attr_stack *read_attr_from_buf(char *buf, const char *path, |
| 769 | unsigned flags) |
Junio C Hamano | a441311 | 2007-08-14 01:40:45 -0700 | [diff] [blame] | 770 | { |
| 771 | struct attr_stack *res; |
Karthik Nayak | 47cfc9b | 2023-01-14 09:30:38 +0100 | [diff] [blame] | 772 | char *sp; |
Junio C Hamano | 1a9d7e9 | 2007-08-14 01:41:02 -0700 | [diff] [blame] | 773 | int lineno = 0; |
Junio C Hamano | a441311 | 2007-08-14 01:40:45 -0700 | [diff] [blame] | 774 | |
Karthik Nayak | 47cfc9b | 2023-01-14 09:30:38 +0100 | [diff] [blame] | 775 | if (!buf) |
| 776 | return NULL; |
| 777 | |
| 778 | CALLOC_ARRAY(res, 1); |
| 779 | for (sp = buf; *sp;) { |
| 780 | char *ep; |
| 781 | int more; |
| 782 | |
| 783 | ep = strchrnul(sp, '\n'); |
| 784 | more = (*ep == '\n'); |
| 785 | *ep = '\0'; |
| 786 | handle_attr_line(res, sp, path, ++lineno, flags); |
| 787 | sp = ep + more; |
| 788 | } |
| 789 | free(buf); |
| 790 | |
| 791 | return res; |
| 792 | } |
| 793 | |
| 794 | static struct attr_stack *read_attr_from_blob(struct index_state *istate, |
| 795 | const struct object_id *tree_oid, |
| 796 | const char *path, unsigned flags) |
| 797 | { |
| 798 | struct object_id oid; |
| 799 | unsigned long sz; |
| 800 | enum object_type type; |
| 801 | void *buf; |
| 802 | unsigned short mode; |
| 803 | |
| 804 | if (!tree_oid) |
| 805 | return NULL; |
| 806 | |
| 807 | if (get_tree_entry(istate->repo, tree_oid, path, &oid, &mode)) |
| 808 | return NULL; |
| 809 | |
| 810 | buf = repo_read_object_file(istate->repo, &oid, &type, &sz); |
| 811 | if (!buf || type != OBJ_BLOB) { |
| 812 | free(buf); |
| 813 | return NULL; |
| 814 | } |
| 815 | |
| 816 | return read_attr_from_buf(buf, path, flags); |
| 817 | } |
| 818 | |
| 819 | static struct attr_stack *read_attr_from_index(struct index_state *istate, |
| 820 | const char *path, unsigned flags) |
| 821 | { |
Shuqi Liang | 4723ae1 | 2023-08-11 10:22:10 -0400 | [diff] [blame] | 822 | struct attr_stack *stack = NULL; |
Karthik Nayak | 47cfc9b | 2023-01-14 09:30:38 +0100 | [diff] [blame] | 823 | char *buf; |
Johannes Schindelin | 37537d6 | 2023-01-12 01:05:02 +0100 | [diff] [blame] | 824 | unsigned long size; |
Shuqi Liang | 4723ae1 | 2023-08-11 10:22:10 -0400 | [diff] [blame] | 825 | int sparse_dir_pos = -1; |
Junio C Hamano | a441311 | 2007-08-14 01:40:45 -0700 | [diff] [blame] | 826 | |
Nguyễn Thái Ngọc Duy | c4500e2 | 2018-08-13 18:14:33 +0200 | [diff] [blame] | 827 | if (!istate) |
Nguyễn Thái Ngọc Duy | 7a400a2 | 2018-08-13 18:14:20 +0200 | [diff] [blame] | 828 | return NULL; |
| 829 | |
Derrick Stolee | 77efbb3 | 2021-09-08 01:42:31 +0000 | [diff] [blame] | 830 | /* |
Shuqi Liang | 4723ae1 | 2023-08-11 10:22:10 -0400 | [diff] [blame] | 831 | * When handling sparse-checkouts, .gitattributes files |
| 832 | * may reside within a sparse directory. We distinguish |
| 833 | * whether a path exists directly in the index or not by |
| 834 | * evaluating if 'pos' is negative. |
| 835 | * If 'pos' is negative, the path is not directly present |
| 836 | * in the index and is likely within a sparse directory. |
| 837 | * For paths not in the index, The absolute value of 'pos' |
| 838 | * minus 1 gives us the position where the path would be |
| 839 | * inserted in lexicographic order within the index. |
| 840 | * We then subtract another 1 from this value |
| 841 | * (sparse_dir_pos = -pos - 2) to find the position of the |
| 842 | * last index entry which is lexicographically smaller than |
| 843 | * the path. This would be the sparse directory containing |
| 844 | * the path. By identifying the sparse directory containing |
| 845 | * the path, we can correctly read the attributes specified |
| 846 | * in the .gitattributes file from the tree object of the |
| 847 | * sparse directory. |
Derrick Stolee | 77efbb3 | 2021-09-08 01:42:31 +0000 | [diff] [blame] | 848 | */ |
Shuqi Liang | 4723ae1 | 2023-08-11 10:22:10 -0400 | [diff] [blame] | 849 | if (!path_in_cone_mode_sparse_checkout(path, istate)) { |
| 850 | int pos = index_name_pos_sparse(istate, path, strlen(path)); |
Derrick Stolee | 77efbb3 | 2021-09-08 01:42:31 +0000 | [diff] [blame] | 851 | |
Shuqi Liang | 4723ae1 | 2023-08-11 10:22:10 -0400 | [diff] [blame] | 852 | if (pos < 0) |
| 853 | sparse_dir_pos = -pos - 2; |
Patrick Steinhardt | 3c50032 | 2022-12-01 15:45:53 +0100 | [diff] [blame] | 854 | } |
Junio C Hamano | 1a9d7e9 | 2007-08-14 01:41:02 -0700 | [diff] [blame] | 855 | |
Shuqi Liang | 4723ae1 | 2023-08-11 10:22:10 -0400 | [diff] [blame] | 856 | if (sparse_dir_pos >= 0 && |
| 857 | S_ISSPARSEDIR(istate->cache[sparse_dir_pos]->ce_mode) && |
| 858 | !strncmp(istate->cache[sparse_dir_pos]->name, path, ce_namelen(istate->cache[sparse_dir_pos]))) { |
| 859 | const char *relative_path = path + ce_namelen(istate->cache[sparse_dir_pos]); |
| 860 | stack = read_attr_from_blob(istate, &istate->cache[sparse_dir_pos]->oid, relative_path, flags); |
| 861 | } else { |
| 862 | buf = read_blob_data_from_index(istate, path, &size); |
| 863 | if (!buf) |
| 864 | return NULL; |
| 865 | if (size >= ATTR_MAX_FILE_SIZE) { |
| 866 | warning(_("ignoring overly large gitattributes blob '%s'"), path); |
| 867 | return NULL; |
| 868 | } |
| 869 | stack = read_attr_from_buf(buf, path, flags); |
| 870 | } |
| 871 | return stack; |
Junio C Hamano | a441311 | 2007-08-14 01:40:45 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Derrick Stolee | 847a9e5 | 2021-04-01 01:49:39 +0000 | [diff] [blame] | 874 | static struct attr_stack *read_attr(struct index_state *istate, |
Karthik Nayak | 47cfc9b | 2023-01-14 09:30:38 +0100 | [diff] [blame] | 875 | const struct object_id *tree_oid, |
Jeff King | dbf387d | 2021-02-16 09:44:25 -0500 | [diff] [blame] | 876 | const char *path, unsigned flags) |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 877 | { |
Brandon Williams | 0787daf | 2017-01-27 18:02:06 -0800 | [diff] [blame] | 878 | struct attr_stack *res = NULL; |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 879 | |
Brandon Williams | 0787daf | 2017-01-27 18:02:06 -0800 | [diff] [blame] | 880 | if (direction == GIT_ATTR_INDEX) { |
Jeff King | dbf387d | 2021-02-16 09:44:25 -0500 | [diff] [blame] | 881 | res = read_attr_from_index(istate, path, flags); |
Karthik Nayak | 47cfc9b | 2023-01-14 09:30:38 +0100 | [diff] [blame] | 882 | } else if (tree_oid) { |
| 883 | res = read_attr_from_blob(istate, tree_oid, path, flags); |
Brandon Williams | 0787daf | 2017-01-27 18:02:06 -0800 | [diff] [blame] | 884 | } else if (!is_bare_repository()) { |
| 885 | if (direction == GIT_ATTR_CHECKOUT) { |
Jeff King | dbf387d | 2021-02-16 09:44:25 -0500 | [diff] [blame] | 886 | res = read_attr_from_index(istate, path, flags); |
Brandon Williams | 0787daf | 2017-01-27 18:02:06 -0800 | [diff] [blame] | 887 | if (!res) |
Jeff King | dbf387d | 2021-02-16 09:44:25 -0500 | [diff] [blame] | 888 | res = read_attr_from_file(path, flags); |
Brandon Williams | 0787daf | 2017-01-27 18:02:06 -0800 | [diff] [blame] | 889 | } else if (direction == GIT_ATTR_CHECKIN) { |
Jeff King | dbf387d | 2021-02-16 09:44:25 -0500 | [diff] [blame] | 890 | res = read_attr_from_file(path, flags); |
Brandon Williams | 0787daf | 2017-01-27 18:02:06 -0800 | [diff] [blame] | 891 | if (!res) |
| 892 | /* |
| 893 | * There is no checked out .gitattributes file |
| 894 | * there, but we might have it in the index. |
| 895 | * We allow operation in a sparsely checked out |
| 896 | * work tree, so read from it. |
| 897 | */ |
Jeff King | dbf387d | 2021-02-16 09:44:25 -0500 | [diff] [blame] | 898 | res = read_attr_from_index(istate, path, flags); |
Brandon Williams | 0787daf | 2017-01-27 18:02:06 -0800 | [diff] [blame] | 899 | } |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 900 | } |
Brandon Williams | 0787daf | 2017-01-27 18:02:06 -0800 | [diff] [blame] | 901 | |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 902 | if (!res) |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 903 | CALLOC_ARRAY(res, 1); |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 904 | return res; |
| 905 | } |
| 906 | |
brian m. carlson | 15780bb | 2023-06-27 16:19:00 +0000 | [diff] [blame] | 907 | const char *git_attr_system_file(void) |
Petr Onderka | 6df42ab | 2010-09-01 00:42:43 +0200 | [diff] [blame] | 908 | { |
| 909 | static const char *system_wide; |
| 910 | if (!system_wide) |
| 911 | system_wide = system_path(ETC_GITATTRIBUTES); |
| 912 | return system_wide; |
| 913 | } |
| 914 | |
brian m. carlson | 15780bb | 2023-06-27 16:19:00 +0000 | [diff] [blame] | 915 | const char *git_attr_global_file(void) |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 916 | { |
| 917 | if (!git_attributes_file) |
| 918 | git_attributes_file = xdg_config_home("attributes"); |
| 919 | |
| 920 | return git_attributes_file; |
| 921 | } |
| 922 | |
brian m. carlson | 15780bb | 2023-06-27 16:19:00 +0000 | [diff] [blame] | 923 | int git_attr_system_is_enabled(void) |
Petr Onderka | 6df42ab | 2010-09-01 00:42:43 +0200 | [diff] [blame] | 924 | { |
| 925 | return !git_env_bool("GIT_ATTR_NOSYSTEM", 0); |
| 926 | } |
| 927 | |
Jeff King | f932729 | 2015-08-10 05:38:57 -0400 | [diff] [blame] | 928 | static GIT_PATH_FUNC(git_path_info_attributes, INFOATTRIBUTES_FILE) |
| 929 | |
Junio C Hamano | 4c0ce07 | 2017-01-27 18:01:51 -0800 | [diff] [blame] | 930 | static void push_stack(struct attr_stack **attr_stack_p, |
| 931 | struct attr_stack *elem, char *origin, size_t originlen) |
| 932 | { |
| 933 | if (elem) { |
| 934 | elem->origin = origin; |
| 935 | if (origin) |
| 936 | elem->originlen = originlen; |
| 937 | elem->prev = *attr_stack_p; |
| 938 | *attr_stack_p = elem; |
| 939 | } |
| 940 | } |
| 941 | |
Derrick Stolee | 847a9e5 | 2021-04-01 01:49:39 +0000 | [diff] [blame] | 942 | static void bootstrap_attr_stack(struct index_state *istate, |
Karthik Nayak | 47cfc9b | 2023-01-14 09:30:38 +0100 | [diff] [blame] | 943 | const struct object_id *tree_oid, |
Nguyễn Thái Ngọc Duy | 7a400a2 | 2018-08-13 18:14:20 +0200 | [diff] [blame] | 944 | struct attr_stack **stack) |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 945 | { |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 946 | struct attr_stack *e; |
Jeff King | dbf387d | 2021-02-16 09:44:25 -0500 | [diff] [blame] | 947 | unsigned flags = READ_ATTR_MACRO_OK; |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 948 | |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 949 | if (*stack) |
Junio C Hamano | 909ca7b | 2012-01-10 12:27:37 -0800 | [diff] [blame] | 950 | return; |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 951 | |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 952 | /* builtin frame */ |
| 953 | e = read_attr_from_array(builtin_attr); |
| 954 | push_stack(stack, e, NULL, 0); |
Petr Onderka | 6df42ab | 2010-09-01 00:42:43 +0200 | [diff] [blame] | 955 | |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 956 | /* system-wide frame */ |
brian m. carlson | 15780bb | 2023-06-27 16:19:00 +0000 | [diff] [blame] | 957 | if (git_attr_system_is_enabled()) { |
| 958 | e = read_attr_from_file(git_attr_system_file(), flags); |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 959 | push_stack(stack, e, NULL, 0); |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 960 | } |
Junio C Hamano | 909ca7b | 2012-01-10 12:27:37 -0800 | [diff] [blame] | 961 | |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 962 | /* home directory */ |
brian m. carlson | 15780bb | 2023-06-27 16:19:00 +0000 | [diff] [blame] | 963 | if (git_attr_global_file()) { |
| 964 | e = read_attr_from_file(git_attr_global_file(), flags); |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 965 | push_stack(stack, e, NULL, 0); |
| 966 | } |
Jeff King | f0056f6 | 2016-10-20 02:16:41 -0400 | [diff] [blame] | 967 | |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 968 | /* root directory */ |
Karthik Nayak | 47cfc9b | 2023-01-14 09:30:38 +0100 | [diff] [blame] | 969 | e = read_attr(istate, tree_oid, GITATTRIBUTES_FILE, flags | READ_ATTR_NOFOLLOW); |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 970 | push_stack(stack, e, xstrdup(""), 0); |
| 971 | |
| 972 | /* info frame */ |
| 973 | if (startup_info->have_repository) |
Jeff King | dbf387d | 2021-02-16 09:44:25 -0500 | [diff] [blame] | 974 | e = read_attr_from_file(git_path_info_attributes(), flags); |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 975 | else |
| 976 | e = NULL; |
| 977 | if (!e) |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 978 | CALLOC_ARRAY(e, 1); |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 979 | push_stack(stack, e, NULL, 0); |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 980 | } |
| 981 | |
Derrick Stolee | 847a9e5 | 2021-04-01 01:49:39 +0000 | [diff] [blame] | 982 | static void prepare_attr_stack(struct index_state *istate, |
Karthik Nayak | 47cfc9b | 2023-01-14 09:30:38 +0100 | [diff] [blame] | 983 | const struct object_id *tree_oid, |
Nguyễn Thái Ngọc Duy | 7a400a2 | 2018-08-13 18:14:20 +0200 | [diff] [blame] | 984 | const char *path, int dirlen, |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 985 | struct attr_stack **stack) |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 986 | { |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 987 | struct attr_stack *info; |
Brandon Williams | 0787daf | 2017-01-27 18:02:06 -0800 | [diff] [blame] | 988 | struct strbuf pathbuf = STRBUF_INIT; |
Michael Haggerty | a872701 | 2011-08-04 06:36:19 +0200 | [diff] [blame] | 989 | |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 990 | /* |
| 991 | * At the bottom of the attribute stack is the built-in |
Petr Onderka | 6df42ab | 2010-09-01 00:42:43 +0200 | [diff] [blame] | 992 | * set of attribute definitions, followed by the contents |
| 993 | * of $(prefix)/etc/gitattributes and a file specified by |
| 994 | * core.attributesfile. Then, contents from |
Robert P. J. Day | bb101aa | 2019-03-06 04:14:44 -0500 | [diff] [blame] | 995 | * .gitattributes files from directories closer to the |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 996 | * root to the ones in deeper directories are pushed |
| 997 | * to the stack. Finally, at the very top of the stack |
| 998 | * we always keep the contents of $GIT_DIR/info/attributes. |
| 999 | * |
| 1000 | * When checking, we use entries from near the top of the |
| 1001 | * stack, preferring $GIT_DIR/info/attributes, then |
| 1002 | * .gitattributes in deeper directories to shallower ones, |
| 1003 | * and finally use the built-in set as the default. |
| 1004 | */ |
Karthik Nayak | 47cfc9b | 2023-01-14 09:30:38 +0100 | [diff] [blame] | 1005 | bootstrap_attr_stack(istate, tree_oid, stack); |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 1006 | |
| 1007 | /* |
| 1008 | * Pop the "info" one that is always at the top of the stack. |
| 1009 | */ |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 1010 | info = *stack; |
| 1011 | *stack = info->prev; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 1012 | |
| 1013 | /* |
| 1014 | * Pop the ones from directories that are not the prefix of |
Junio C Hamano | c432ef9 | 2012-01-10 12:28:38 -0800 | [diff] [blame] | 1015 | * the path we are checking. Break out of the loop when we see |
| 1016 | * the root one (whose origin is an empty string "") or the builtin |
| 1017 | * one (whose origin is NULL) without popping it. |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 1018 | */ |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 1019 | while ((*stack)->origin) { |
| 1020 | int namelen = (*stack)->originlen; |
| 1021 | struct attr_stack *elem; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 1022 | |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 1023 | elem = *stack; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 1024 | if (namelen <= dirlen && |
Jeff King | 1afca44 | 2012-01-10 13:08:21 -0500 | [diff] [blame] | 1025 | !strncmp(elem->origin, path, namelen) && |
| 1026 | (!namelen || path[namelen] == '/')) |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 1027 | break; |
| 1028 | |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 1029 | *stack = elem->prev; |
| 1030 | attr_stack_free(elem); |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | /* |
Brandon Williams | 0787daf | 2017-01-27 18:02:06 -0800 | [diff] [blame] | 1034 | * bootstrap_attr_stack() should have added, and the |
| 1035 | * above loop should have stopped before popping, the |
| 1036 | * root element whose attr_stack->origin is set to an |
| 1037 | * empty string. |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 1038 | */ |
Brandon Williams | 0787daf | 2017-01-27 18:02:06 -0800 | [diff] [blame] | 1039 | assert((*stack)->origin); |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 1040 | |
Brandon Williams | 0787daf | 2017-01-27 18:02:06 -0800 | [diff] [blame] | 1041 | strbuf_addstr(&pathbuf, (*stack)->origin); |
| 1042 | /* Build up to the directory 'path' is in */ |
| 1043 | while (pathbuf.len < dirlen) { |
| 1044 | size_t len = pathbuf.len; |
| 1045 | struct attr_stack *next; |
| 1046 | char *origin; |
Junio C Hamano | 4c0ce07 | 2017-01-27 18:01:51 -0800 | [diff] [blame] | 1047 | |
Brandon Williams | 0787daf | 2017-01-27 18:02:06 -0800 | [diff] [blame] | 1048 | /* Skip path-separator */ |
| 1049 | if (len < dirlen && is_dir_sep(path[len])) |
| 1050 | len++; |
| 1051 | /* Find the end of the next component */ |
| 1052 | while (len < dirlen && !is_dir_sep(path[len])) |
| 1053 | len++; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 1054 | |
Brandon Williams | 0787daf | 2017-01-27 18:02:06 -0800 | [diff] [blame] | 1055 | if (pathbuf.len > 0) |
| 1056 | strbuf_addch(&pathbuf, '/'); |
| 1057 | strbuf_add(&pathbuf, path + pathbuf.len, (len - pathbuf.len)); |
| 1058 | strbuf_addf(&pathbuf, "/%s", GITATTRIBUTES_FILE); |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 1059 | |
Karthik Nayak | 47cfc9b | 2023-01-14 09:30:38 +0100 | [diff] [blame] | 1060 | next = read_attr(istate, tree_oid, pathbuf.buf, READ_ATTR_NOFOLLOW); |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 1061 | |
Brandon Williams | 0787daf | 2017-01-27 18:02:06 -0800 | [diff] [blame] | 1062 | /* reset the pathbuf to not include "/.gitattributes" */ |
| 1063 | strbuf_setlen(&pathbuf, len); |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 1064 | |
Brandon Williams | 0787daf | 2017-01-27 18:02:06 -0800 | [diff] [blame] | 1065 | origin = xstrdup(pathbuf.buf); |
| 1066 | push_stack(stack, next, origin, len); |
Brandon Casey | 97410b2 | 2011-10-06 13:22:21 -0500 | [diff] [blame] | 1067 | } |
René Scharfe | d4c9856 | 2009-07-01 00:30:00 +0200 | [diff] [blame] | 1068 | |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 1069 | /* |
| 1070 | * Finally push the "info" one at the top of the stack. |
| 1071 | */ |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 1072 | push_stack(stack, info, NULL, 0); |
Brandon Williams | 0787daf | 2017-01-27 18:02:06 -0800 | [diff] [blame] | 1073 | |
| 1074 | strbuf_release(&pathbuf); |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | static int path_matches(const char *pathname, int pathlen, |
Junio C Hamano | bd2f371 | 2013-03-26 10:28:07 -0700 | [diff] [blame] | 1078 | int basename_offset, |
Nguyễn Thái Ngọc Duy | 82dce99 | 2012-10-15 13:24:39 +0700 | [diff] [blame] | 1079 | const struct pattern *pat, |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 1080 | const char *base, int baselen) |
| 1081 | { |
Nguyễn Thái Ngọc Duy | 82dce99 | 2012-10-15 13:24:39 +0700 | [diff] [blame] | 1082 | const char *pattern = pat->pattern; |
| 1083 | int prefix = pat->nowildcardlen; |
Junio C Hamano | dc09e9e | 2013-03-28 17:49:13 -0400 | [diff] [blame] | 1084 | int isdir = (pathlen && pathname[pathlen - 1] == '/'); |
Nguyễn Thái Ngọc Duy | 82dce99 | 2012-10-15 13:24:39 +0700 | [diff] [blame] | 1085 | |
Derrick Stolee | 4ff89ee | 2019-09-03 11:04:56 -0700 | [diff] [blame] | 1086 | if ((pat->flags & PATTERN_FLAG_MUSTBEDIR) && !isdir) |
Jean-Noël AVILA | 94bc671 | 2012-12-08 21:04:39 +0100 | [diff] [blame] | 1087 | return 0; |
| 1088 | |
Derrick Stolee | 4ff89ee | 2019-09-03 11:04:56 -0700 | [diff] [blame] | 1089 | if (pat->flags & PATTERN_FLAG_NODIR) { |
Junio C Hamano | bd2f371 | 2013-03-26 10:28:07 -0700 | [diff] [blame] | 1090 | return match_basename(pathname + basename_offset, |
Junio C Hamano | dc09e9e | 2013-03-28 17:49:13 -0400 | [diff] [blame] | 1091 | pathlen - basename_offset - isdir, |
Nguyễn Thái Ngọc Duy | 82dce99 | 2012-10-15 13:24:39 +0700 | [diff] [blame] | 1092 | pattern, prefix, |
| 1093 | pat->patternlen, pat->flags); |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 1094 | } |
Junio C Hamano | dc09e9e | 2013-03-28 17:49:13 -0400 | [diff] [blame] | 1095 | return match_pathname(pathname, pathlen - isdir, |
Nguyễn Thái Ngọc Duy | 82dce99 | 2012-10-15 13:24:39 +0700 | [diff] [blame] | 1096 | base, baselen, |
Jeff King | 77651c0 | 2022-08-19 04:50:54 -0400 | [diff] [blame] | 1097 | pattern, prefix, pat->patternlen); |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 1098 | } |
| 1099 | |
Brandon Williams | 685b292 | 2017-01-27 18:02:02 -0800 | [diff] [blame] | 1100 | static int macroexpand_one(struct all_attrs_item *all_attrs, int nr, int rem); |
Henrik Grubbström | ec775c4 | 2010-04-06 14:46:44 +0200 | [diff] [blame] | 1101 | |
Jeff King | 69c5f17 | 2022-10-06 09:23:19 -0400 | [diff] [blame] | 1102 | static int fill_one(struct all_attrs_item *all_attrs, |
Brandon Williams | 60a1272 | 2017-01-27 18:02:03 -0800 | [diff] [blame] | 1103 | const struct match_attr *a, int rem) |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 1104 | { |
Patrick Steinhardt | 34ace8b | 2022-12-01 15:45:27 +0100 | [diff] [blame] | 1105 | size_t i; |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 1106 | |
Patrick Steinhardt | 34ace8b | 2022-12-01 15:45:27 +0100 | [diff] [blame] | 1107 | for (i = a->num_attr; rem > 0 && i > 0; i--) { |
| 1108 | const struct git_attr *attr = a->state[i - 1].attr; |
Brandon Williams | 685b292 | 2017-01-27 18:02:02 -0800 | [diff] [blame] | 1109 | const char **n = &(all_attrs[attr->attr_nr].value); |
Patrick Steinhardt | 34ace8b | 2022-12-01 15:45:27 +0100 | [diff] [blame] | 1110 | const char *v = a->state[i - 1].setto; |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 1111 | |
| 1112 | if (*n == ATTR__UNKNOWN) { |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 1113 | *n = v; |
| 1114 | rem--; |
Brandon Williams | 685b292 | 2017-01-27 18:02:02 -0800 | [diff] [blame] | 1115 | rem = macroexpand_one(all_attrs, attr->attr_nr, rem); |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 1116 | } |
| 1117 | } |
| 1118 | return rem; |
| 1119 | } |
| 1120 | |
Junio C Hamano | bd2f371 | 2013-03-26 10:28:07 -0700 | [diff] [blame] | 1121 | static int fill(const char *path, int pathlen, int basename_offset, |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 1122 | const struct attr_stack *stack, |
| 1123 | struct all_attrs_item *all_attrs, int rem) |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 1124 | { |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 1125 | for (; rem > 0 && stack; stack = stack->prev) { |
Patrick Steinhardt | 447ac90 | 2022-12-01 15:45:31 +0100 | [diff] [blame] | 1126 | unsigned i; |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 1127 | const char *base = stack->origin ? stack->origin : ""; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 1128 | |
Patrick Steinhardt | 447ac90 | 2022-12-01 15:45:31 +0100 | [diff] [blame] | 1129 | for (i = stack->num_matches; 0 < rem && 0 < i; i--) { |
| 1130 | const struct match_attr *a = stack->attrs[i - 1]; |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 1131 | if (a->is_macro) |
| 1132 | continue; |
| 1133 | if (path_matches(path, pathlen, basename_offset, |
| 1134 | &a->u.pat, base, stack->originlen)) |
Jeff King | 69c5f17 | 2022-10-06 09:23:19 -0400 | [diff] [blame] | 1135 | rem = fill_one(all_attrs, a, rem); |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 1136 | } |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 1137 | } |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 1138 | |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 1139 | return rem; |
| 1140 | } |
| 1141 | |
Brandon Williams | 685b292 | 2017-01-27 18:02:02 -0800 | [diff] [blame] | 1142 | static int macroexpand_one(struct all_attrs_item *all_attrs, int nr, int rem) |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 1143 | { |
Brandon Williams | 60a1272 | 2017-01-27 18:02:03 -0800 | [diff] [blame] | 1144 | const struct all_attrs_item *item = &all_attrs[nr]; |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 1145 | |
Brandon Williams | 60a1272 | 2017-01-27 18:02:03 -0800 | [diff] [blame] | 1146 | if (item->macro && item->value == ATTR__TRUE) |
Jeff King | 69c5f17 | 2022-10-06 09:23:19 -0400 | [diff] [blame] | 1147 | return fill_one(all_attrs, item->macro, rem); |
Brandon Williams | 60a1272 | 2017-01-27 18:02:03 -0800 | [diff] [blame] | 1148 | else |
Henrik Grubbström | ec775c4 | 2010-04-06 14:46:44 +0200 | [diff] [blame] | 1149 | return rem; |
Brandon Williams | 60a1272 | 2017-01-27 18:02:03 -0800 | [diff] [blame] | 1150 | } |
Henrik Grubbström | ec775c4 | 2010-04-06 14:46:44 +0200 | [diff] [blame] | 1151 | |
Brandon Williams | 60a1272 | 2017-01-27 18:02:03 -0800 | [diff] [blame] | 1152 | /* |
| 1153 | * Marks the attributes which are macros based on the attribute stack. |
| 1154 | * This prevents having to search through the attribute stack each time |
| 1155 | * a macro needs to be expanded during the fill stage. |
| 1156 | */ |
| 1157 | static void determine_macros(struct all_attrs_item *all_attrs, |
| 1158 | const struct attr_stack *stack) |
| 1159 | { |
| 1160 | for (; stack; stack = stack->prev) { |
Patrick Steinhardt | 447ac90 | 2022-12-01 15:45:31 +0100 | [diff] [blame] | 1161 | unsigned i; |
| 1162 | for (i = stack->num_matches; i > 0; i--) { |
| 1163 | const struct match_attr *ma = stack->attrs[i - 1]; |
Brandon Williams | 60a1272 | 2017-01-27 18:02:03 -0800 | [diff] [blame] | 1164 | if (ma->is_macro) { |
Patrick Steinhardt | e1e12e9 | 2022-12-01 15:45:36 +0100 | [diff] [blame] | 1165 | unsigned int n = ma->u.attr->attr_nr; |
Brandon Williams | 60a1272 | 2017-01-27 18:02:03 -0800 | [diff] [blame] | 1166 | if (!all_attrs[n].macro) { |
| 1167 | all_attrs[n].macro = ma; |
| 1168 | } |
| 1169 | } |
Henrik Grubbström | ec775c4 | 2010-04-06 14:46:44 +0200 | [diff] [blame] | 1170 | } |
Junio C Hamano | 4b0c696 | 2017-01-27 18:01:47 -0800 | [diff] [blame] | 1171 | } |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 1172 | } |
| 1173 | |
Michael Haggerty | 2d72174 | 2011-08-04 06:36:20 +0200 | [diff] [blame] | 1174 | /* |
Brandon Williams | 685b292 | 2017-01-27 18:02:02 -0800 | [diff] [blame] | 1175 | * Collect attributes for path into the array pointed to by check->all_attrs. |
| 1176 | * If check->check_nr is non-zero, only attributes in check[] are collected. |
| 1177 | * Otherwise all attributes are collected. |
Michael Haggerty | 2d72174 | 2011-08-04 06:36:20 +0200 | [diff] [blame] | 1178 | */ |
Derrick Stolee | 847a9e5 | 2021-04-01 01:49:39 +0000 | [diff] [blame] | 1179 | static void collect_some_attrs(struct index_state *istate, |
Karthik Nayak | 47cfc9b | 2023-01-14 09:30:38 +0100 | [diff] [blame] | 1180 | const struct object_id *tree_oid, |
| 1181 | const char *path, struct attr_check *check) |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 1182 | { |
Jeff King | 7b95849 | 2019-01-18 16:34:58 -0500 | [diff] [blame] | 1183 | int pathlen, rem, dirlen; |
Junio C Hamano | bd2f371 | 2013-03-26 10:28:07 -0700 | [diff] [blame] | 1184 | const char *cp, *last_slash = NULL; |
| 1185 | int basename_offset; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 1186 | |
Duy Nguyen | 9db9eec | 2013-01-16 13:02:38 +0700 | [diff] [blame] | 1187 | for (cp = path; *cp; cp++) { |
| 1188 | if (*cp == '/' && cp[1]) |
| 1189 | last_slash = cp; |
| 1190 | } |
| 1191 | pathlen = cp - path; |
| 1192 | if (last_slash) { |
Junio C Hamano | bd2f371 | 2013-03-26 10:28:07 -0700 | [diff] [blame] | 1193 | basename_offset = last_slash + 1 - path; |
Duy Nguyen | 9db9eec | 2013-01-16 13:02:38 +0700 | [diff] [blame] | 1194 | dirlen = last_slash - path; |
| 1195 | } else { |
Junio C Hamano | bd2f371 | 2013-03-26 10:28:07 -0700 | [diff] [blame] | 1196 | basename_offset = 0; |
Duy Nguyen | 9db9eec | 2013-01-16 13:02:38 +0700 | [diff] [blame] | 1197 | dirlen = 0; |
| 1198 | } |
| 1199 | |
Karthik Nayak | 47cfc9b | 2023-01-14 09:30:38 +0100 | [diff] [blame] | 1200 | prepare_attr_stack(istate, tree_oid, path, dirlen, &check->stack); |
Brandon Williams | 685b292 | 2017-01-27 18:02:02 -0800 | [diff] [blame] | 1201 | all_attrs_init(&g_attr_hashmap, check); |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 1202 | determine_macros(check->all_attrs, check->stack); |
Brandon Williams | 685b292 | 2017-01-27 18:02:02 -0800 | [diff] [blame] | 1203 | |
Brandon Williams | 685b292 | 2017-01-27 18:02:02 -0800 | [diff] [blame] | 1204 | rem = check->all_attrs_nr; |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 1205 | fill(path, pathlen, basename_offset, check->stack, check->all_attrs, rem); |
Michael Haggerty | 2d72174 | 2011-08-04 06:36:20 +0200 | [diff] [blame] | 1206 | } |
| 1207 | |
John Cai | 44451a2 | 2023-05-06 04:15:29 +0000 | [diff] [blame] | 1208 | static const char *default_attr_source_tree_object_name; |
John Cai | 2386535 | 2023-10-13 17:39:29 +0000 | [diff] [blame] | 1209 | static int ignore_bad_attr_tree; |
John Cai | 44451a2 | 2023-05-06 04:15:29 +0000 | [diff] [blame] | 1210 | |
| 1211 | void set_git_attr_source(const char *tree_object_name) |
| 1212 | { |
| 1213 | default_attr_source_tree_object_name = xstrdup(tree_object_name); |
| 1214 | } |
| 1215 | |
| 1216 | static void compute_default_attr_source(struct object_id *attr_source) |
| 1217 | { |
| 1218 | if (!default_attr_source_tree_object_name) |
| 1219 | default_attr_source_tree_object_name = getenv(GIT_ATTR_SOURCE_ENVIRONMENT); |
| 1220 | |
John Cai | 9f9c40c | 2023-10-13 17:39:30 +0000 | [diff] [blame] | 1221 | if (!default_attr_source_tree_object_name && git_attr_tree) { |
| 1222 | default_attr_source_tree_object_name = git_attr_tree; |
| 1223 | ignore_bad_attr_tree = 1; |
| 1224 | } |
| 1225 | |
John Cai | 2386535 | 2023-10-13 17:39:29 +0000 | [diff] [blame] | 1226 | if (!default_attr_source_tree_object_name && |
| 1227 | startup_info->have_repository && |
| 1228 | is_bare_repository()) { |
| 1229 | default_attr_source_tree_object_name = "HEAD"; |
| 1230 | ignore_bad_attr_tree = 1; |
| 1231 | } |
| 1232 | |
John Cai | 44451a2 | 2023-05-06 04:15:29 +0000 | [diff] [blame] | 1233 | if (!default_attr_source_tree_object_name || !is_null_oid(attr_source)) |
| 1234 | return; |
| 1235 | |
John Cai | 2386535 | 2023-10-13 17:39:29 +0000 | [diff] [blame] | 1236 | if (repo_get_oid_treeish(the_repository, |
| 1237 | default_attr_source_tree_object_name, |
| 1238 | attr_source) && !ignore_bad_attr_tree) |
John Cai | 44451a2 | 2023-05-06 04:15:29 +0000 | [diff] [blame] | 1239 | die(_("bad --attr-source or GIT_ATTR_SOURCE")); |
| 1240 | } |
| 1241 | |
| 1242 | static struct object_id *default_attr_source(void) |
| 1243 | { |
| 1244 | static struct object_id attr_source; |
| 1245 | |
| 1246 | if (is_null_oid(&attr_source)) |
| 1247 | compute_default_attr_source(&attr_source); |
| 1248 | if (is_null_oid(&attr_source)) |
| 1249 | return NULL; |
| 1250 | return &attr_source; |
| 1251 | } |
| 1252 | |
Joanna Wang | 2232a88 | 2023-11-16 05:44:37 +0000 | [diff] [blame] | 1253 | static const char *interned_mode_string(unsigned int mode) |
| 1254 | { |
| 1255 | static struct { |
| 1256 | unsigned int val; |
| 1257 | char str[7]; |
| 1258 | } mode_string[] = { |
| 1259 | { .val = 0040000 }, |
| 1260 | { .val = 0100644 }, |
| 1261 | { .val = 0100755 }, |
| 1262 | { .val = 0120000 }, |
| 1263 | { .val = 0160000 }, |
| 1264 | }; |
| 1265 | int i; |
| 1266 | |
| 1267 | for (i = 0; i < ARRAY_SIZE(mode_string); i++) { |
| 1268 | if (mode_string[i].val != mode) |
| 1269 | continue; |
| 1270 | if (!*mode_string[i].str) |
| 1271 | snprintf(mode_string[i].str, sizeof(mode_string[i].str), |
| 1272 | "%06o", mode); |
| 1273 | return mode_string[i].str; |
| 1274 | } |
| 1275 | BUG("Unsupported mode 0%o", mode); |
| 1276 | } |
| 1277 | |
| 1278 | static const char *builtin_object_mode_attr(struct index_state *istate, const char *path) |
| 1279 | { |
| 1280 | unsigned int mode; |
| 1281 | |
| 1282 | if (direction == GIT_ATTR_CHECKIN) { |
| 1283 | struct object_id oid; |
| 1284 | struct stat st; |
| 1285 | if (lstat(path, &st)) |
| 1286 | die_errno(_("unable to stat '%s'"), path); |
| 1287 | mode = canon_mode(st.st_mode); |
| 1288 | if (S_ISDIR(mode)) { |
| 1289 | /* |
| 1290 | *`path` is either a directory or it is a submodule, |
| 1291 | * in which case it is already indexed as submodule |
| 1292 | * or it does not exist in the index yet and we need to |
| 1293 | * check if we can resolve to a ref. |
| 1294 | */ |
| 1295 | int pos = index_name_pos(istate, path, strlen(path)); |
| 1296 | if (pos >= 0) { |
| 1297 | if (S_ISGITLINK(istate->cache[pos]->ce_mode)) |
| 1298 | mode = istate->cache[pos]->ce_mode; |
| 1299 | } else if (resolve_gitlink_ref(path, "HEAD", &oid) == 0) { |
| 1300 | mode = S_IFGITLINK; |
| 1301 | } |
| 1302 | } |
| 1303 | } else { |
| 1304 | /* |
| 1305 | * For GIT_ATTR_CHECKOUT and GIT_ATTR_INDEX we only check |
| 1306 | * for mode in the index. |
| 1307 | */ |
| 1308 | int pos = index_name_pos(istate, path, strlen(path)); |
| 1309 | if (pos >= 0) |
| 1310 | mode = istate->cache[pos]->ce_mode; |
| 1311 | else |
| 1312 | return ATTR__UNSET; |
| 1313 | } |
| 1314 | |
| 1315 | return interned_mode_string(mode); |
| 1316 | } |
| 1317 | |
| 1318 | |
| 1319 | static const char *compute_builtin_attr(struct index_state *istate, |
| 1320 | const char *path, |
| 1321 | const struct git_attr *attr) { |
| 1322 | static const struct git_attr *object_mode_attr; |
| 1323 | |
| 1324 | if (!object_mode_attr) |
| 1325 | object_mode_attr = git_attr("builtin_objectmode"); |
| 1326 | |
| 1327 | if (attr == object_mode_attr) |
| 1328 | return builtin_object_mode_attr(istate, path); |
| 1329 | return ATTR__UNSET; |
| 1330 | } |
| 1331 | |
Derrick Stolee | 847a9e5 | 2021-04-01 01:49:39 +0000 | [diff] [blame] | 1332 | void git_check_attr(struct index_state *istate, |
John Cai | 44451a2 | 2023-05-06 04:15:29 +0000 | [diff] [blame] | 1333 | const char *path, |
Torsten Bögershausen | d64324c | 2018-09-12 21:32:02 +0200 | [diff] [blame] | 1334 | struct attr_check *check) |
Michael Haggerty | 2d72174 | 2011-08-04 06:36:20 +0200 | [diff] [blame] | 1335 | { |
| 1336 | int i; |
John Cai | 44451a2 | 2023-05-06 04:15:29 +0000 | [diff] [blame] | 1337 | const struct object_id *tree_oid = default_attr_source(); |
Michael Haggerty | 2d72174 | 2011-08-04 06:36:20 +0200 | [diff] [blame] | 1338 | |
Karthik Nayak | 47cfc9b | 2023-01-14 09:30:38 +0100 | [diff] [blame] | 1339 | collect_some_attrs(istate, tree_oid, path, check); |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 1340 | |
Brandon Williams | 6bc2e3f | 2017-01-27 18:01:59 -0800 | [diff] [blame] | 1341 | for (i = 0; i < check->nr; i++) { |
Patrick Steinhardt | e1e12e9 | 2022-12-01 15:45:36 +0100 | [diff] [blame] | 1342 | unsigned int n = check->items[i].attr->attr_nr; |
Brandon Williams | 685b292 | 2017-01-27 18:02:02 -0800 | [diff] [blame] | 1343 | const char *value = check->all_attrs[n].value; |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 1344 | if (value == ATTR__UNKNOWN) |
Joanna Wang | 2232a88 | 2023-11-16 05:44:37 +0000 | [diff] [blame] | 1345 | value = compute_builtin_attr(istate, path, check->all_attrs[n].attr); |
Brandon Williams | 6bc2e3f | 2017-01-27 18:01:59 -0800 | [diff] [blame] | 1346 | check->items[i].value = value; |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 1347 | } |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 1348 | } |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 1349 | |
John Cai | 44451a2 | 2023-05-06 04:15:29 +0000 | [diff] [blame] | 1350 | void git_all_attrs(struct index_state *istate, |
Nguyễn Thái Ngọc Duy | 7a400a2 | 2018-08-13 18:14:20 +0200 | [diff] [blame] | 1351 | const char *path, struct attr_check *check) |
Michael Haggerty | ee548df | 2011-08-04 06:36:23 +0200 | [diff] [blame] | 1352 | { |
Junio C Hamano | 7f86411 | 2017-01-30 10:06:08 -0800 | [diff] [blame] | 1353 | int i; |
John Cai | 44451a2 | 2023-05-06 04:15:29 +0000 | [diff] [blame] | 1354 | const struct object_id *tree_oid = default_attr_source(); |
Michael Haggerty | ee548df | 2011-08-04 06:36:23 +0200 | [diff] [blame] | 1355 | |
Junio C Hamano | 7f86411 | 2017-01-30 10:06:08 -0800 | [diff] [blame] | 1356 | attr_check_reset(check); |
Karthik Nayak | 47cfc9b | 2023-01-14 09:30:38 +0100 | [diff] [blame] | 1357 | collect_some_attrs(istate, tree_oid, path, check); |
Michael Haggerty | ee548df | 2011-08-04 06:36:23 +0200 | [diff] [blame] | 1358 | |
Brandon Williams | 685b292 | 2017-01-27 18:02:02 -0800 | [diff] [blame] | 1359 | for (i = 0; i < check->all_attrs_nr; i++) { |
| 1360 | const char *name = check->all_attrs[i].attr->name; |
| 1361 | const char *value = check->all_attrs[i].value; |
Junio C Hamano | 7f86411 | 2017-01-30 10:06:08 -0800 | [diff] [blame] | 1362 | struct attr_check_item *item; |
| 1363 | if (value == ATTR__UNSET || value == ATTR__UNKNOWN) |
| 1364 | continue; |
| 1365 | item = attr_check_append(check, git_attr(name)); |
| 1366 | item->value = value; |
Michael Haggerty | ee548df | 2011-08-04 06:36:23 +0200 | [diff] [blame] | 1367 | } |
Michael Haggerty | ee548df | 2011-08-04 06:36:23 +0200 | [diff] [blame] | 1368 | } |
| 1369 | |
Brandon Williams | 1a600b7 | 2017-01-27 18:02:01 -0800 | [diff] [blame] | 1370 | void attr_start(void) |
| 1371 | { |
Brandon Williams | 1a600b7 | 2017-01-27 18:02:01 -0800 | [diff] [blame] | 1372 | pthread_mutex_init(&g_attr_hashmap.mutex, NULL); |
Brandon Williams | dc81cf3 | 2017-01-27 18:02:05 -0800 | [diff] [blame] | 1373 | pthread_mutex_init(&check_vector.mutex, NULL); |
Brandon Williams | 1a600b7 | 2017-01-27 18:02:01 -0800 | [diff] [blame] | 1374 | } |