Skip to content

Commit 96dc1ff

Browse files
committed
attr: the attr source is now a struct
We may want to extend the attribute source; use a structure instead of an enum.
1 parent 5ee5048 commit 96dc1ff

File tree

4 files changed

+26
-19
lines changed

4 files changed

+26
-19
lines changed

src/attr_file.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static void attr_file_free(git_attr_file *file)
3333
int git_attr_file__new(
3434
git_attr_file **out,
3535
git_attr_file_entry *entry,
36-
git_attr_file_source_t source_type)
36+
git_attr_file_source *source)
3737
{
3838
git_attr_file *attrs = git__calloc(1, sizeof(git_attr_file));
3939
GIT_ERROR_CHECK_ALLOC(attrs);
@@ -48,7 +48,7 @@ int git_attr_file__new(
4848

4949
GIT_REFCOUNT_INC(attrs);
5050
attrs->entry = entry;
51-
attrs->source_type = source_type;
51+
memcpy(&attrs->source, source, sizeof(git_attr_file_source));
5252
*out = attrs;
5353
return 0;
5454

@@ -108,7 +108,7 @@ int git_attr_file__load(
108108
git_repository *repo,
109109
git_attr_session *attr_session,
110110
git_attr_file_entry *entry,
111-
git_attr_file_source_t source_type,
111+
git_attr_file_source *source,
112112
git_attr_file_parser parser,
113113
bool allow_macros)
114114
{
@@ -128,7 +128,7 @@ int git_attr_file__load(
128128

129129
*out = NULL;
130130

131-
switch (source_type) {
131+
switch (source->type) {
132132
case GIT_ATTR_FILE_SOURCE_MEMORY:
133133
/* in-memory attribute file doesn't need data */
134134
break;
@@ -182,11 +182,11 @@ int git_attr_file__load(
182182
break;
183183
}
184184
default:
185-
git_error_set(GIT_ERROR_INVALID, "unknown file source %d", source_type);
185+
git_error_set(GIT_ERROR_INVALID, "unknown file source %d", source->type);
186186
return -1;
187187
}
188188

189-
if ((error = git_attr_file__new(&file, entry, source_type)) < 0)
189+
if ((error = git_attr_file__new(&file, entry, source)) < 0)
190190
goto cleanup;
191191

192192
/* advance over a UTF8 BOM */
@@ -210,11 +210,11 @@ int git_attr_file__load(
210210
/* write cache breakers */
211211
if (nonexistent)
212212
file->nonexistent = 1;
213-
else if (source_type == GIT_ATTR_FILE_SOURCE_INDEX)
213+
else if (source->type == GIT_ATTR_FILE_SOURCE_INDEX)
214214
git_oid_cpy(&file->cache_data.oid, git_blob_id(blob));
215-
else if (source_type == GIT_ATTR_FILE_SOURCE_HEAD)
215+
else if (source->type == GIT_ATTR_FILE_SOURCE_HEAD)
216216
git_oid_cpy(&file->cache_data.oid, git_tree_id(tree));
217-
else if (source_type == GIT_ATTR_FILE_SOURCE_FILE)
217+
else if (source->type == GIT_ATTR_FILE_SOURCE_FILE)
218218
git_futils_filestamp_set_from_stat(&file->cache_data.stamp, &st);
219219
/* else always cacheable */
220220

@@ -245,7 +245,7 @@ int git_attr_file__out_of_date(
245245
else if (file->nonexistent)
246246
return 1;
247247

248-
switch (file->source_type) {
248+
switch (file->source.type) {
249249
case GIT_ATTR_FILE_SOURCE_MEMORY:
250250
return 0;
251251

@@ -278,7 +278,7 @@ int git_attr_file__out_of_date(
278278
}
279279

280280
default:
281-
git_error_set(GIT_ERROR_INVALID, "invalid file type %d", file->source_type);
281+
git_error_set(GIT_ERROR_INVALID, "invalid file type %d", file->source.type);
282282
return -1;
283283
}
284284
}
@@ -389,6 +389,7 @@ int git_attr_file__lookup_one(
389389
int git_attr_file__load_standalone(git_attr_file **out, const char *path)
390390
{
391391
git_buf content = GIT_BUF_INIT;
392+
git_attr_file_source source = { GIT_ATTR_FILE_SOURCE_FILE };
392393
git_attr_file *file = NULL;
393394
int error;
394395

@@ -400,7 +401,7 @@ int git_attr_file__load_standalone(git_attr_file **out, const char *path)
400401
* don't have to free it - freeing file+pool will free cache entry, too.
401402
*/
402403

403-
if ((error = git_attr_file__new(&file, NULL, GIT_ATTR_FILE_SOURCE_FILE)) < 0 ||
404+
if ((error = git_attr_file__new(&file, NULL, &source)) < 0 ||
404405
(error = git_attr_file__parse_buffer(NULL, file, content.ptr, true)) < 0 ||
405406
(error = git_attr_cache__alloc_file_entry(&file->entry, NULL, NULL, path, &file->pool)) < 0)
406407
goto out;

src/attr_file.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ typedef enum {
4545
GIT_ATTR_FILE_NUM_SOURCES = 4
4646
} git_attr_file_source_t;
4747

48+
typedef struct {
49+
git_attr_file_source_t type;
50+
} git_attr_file_source;
51+
4852
extern const char *git_attr__true;
4953
extern const char *git_attr__false;
5054
extern const char *git_attr__unset;
@@ -81,7 +85,7 @@ typedef struct {
8185
git_refcount rc;
8286
git_mutex lock;
8387
git_attr_file_entry *entry;
84-
git_attr_file_source_t source_type;
88+
git_attr_file_source source;
8589
git_vector rules; /* vector of <rule*> or <fnmatch*> */
8690
git_pool pool;
8791
unsigned int nonexistent:1;
@@ -142,7 +146,7 @@ typedef int (*git_attr_file_parser)(
142146
int git_attr_file__new(
143147
git_attr_file **out,
144148
git_attr_file_entry *entry,
145-
git_attr_file_source_t source_type);
149+
git_attr_file_source *source);
146150

147151
void git_attr_file__free(git_attr_file *file);
148152

@@ -151,7 +155,7 @@ int git_attr_file__load(
151155
git_repository *repo,
152156
git_attr_session *attr_session,
153157
git_attr_file_entry *ce,
154-
git_attr_file_source_t source_type,
158+
git_attr_file_source *source,
155159
git_attr_file_parser parser,
156160
bool allow_macros);
157161

src/attrcache.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ static int attr_cache_upsert(git_attr_cache *cache, git_attr_file *file)
112112
* Replace the existing value if another thread has
113113
* created it in the meantime.
114114
*/
115-
old = git_atomic_swap(entry->file[file->source_type], file);
115+
old = git_atomic_swap(entry->file[file->source.type], file);
116116

117117
if (old) {
118118
GIT_REFCOUNT_OWN(old, NULL);
@@ -136,7 +136,7 @@ static int attr_cache_remove(git_attr_cache *cache, git_attr_file *file)
136136
return error;
137137

138138
if ((entry = attr_cache_lookup_entry(cache, file->entry->path)) != NULL)
139-
old = git_atomic_compare_and_swap(&entry->file[file->source_type], file, NULL);
139+
old = git_atomic_compare_and_swap(&entry->file[file->source.type], file, NULL);
140140

141141
attr_cache_unlock(cache);
142142

@@ -220,6 +220,7 @@ int git_attr_cache__get(
220220
git_attr_cache *cache = git_repository_attr_cache(repo);
221221
git_attr_file_entry *entry = NULL;
222222
git_attr_file *file = NULL, *updated = NULL;
223+
git_attr_file_source source = { source_type };
223224

224225
if ((error = attr_cache_lookup(&file, &entry, repo, attr_session,
225226
source_type, base, filename)) < 0)
@@ -228,7 +229,7 @@ int git_attr_cache__get(
228229
/* load file if we don't have one or if existing one is out of date */
229230
if (!file || (error = git_attr_file__out_of_date(repo, attr_session, file)) > 0)
230231
error = git_attr_file__load(&updated, repo, attr_session,
231-
entry, source_type, parser,
232+
entry, &source, parser,
232233
allow_macros);
233234

234235
/* if we loaded the file, insert into and/or update cache */

tests/attr/lookup.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ void test_attr_lookup__check_attr_examples(void)
236236
void test_attr_lookup__from_buffer(void)
237237
{
238238
git_attr_file *file;
239+
git_attr_file_source source = {0};
239240

240241
struct attr_expected cases[] = {
241242
{ "abc", "foo", EXPECT_TRUE, NULL },
@@ -250,7 +251,7 @@ void test_attr_lookup__from_buffer(void)
250251
{ NULL, NULL, 0, NULL }
251252
};
252253

253-
cl_git_pass(git_attr_file__new(&file, NULL, 0));
254+
cl_git_pass(git_attr_file__new(&file, NULL, &source));
254255

255256
cl_git_pass(git_attr_file__parse_buffer(NULL, file, "a* foo\nabc bar\n* baz", true));
256257

0 commit comments

Comments
 (0)