refs.c: rename ref_array -> ref_dir
This purely textual change is in preparation for storing references hierarchically, when the old ref_array structure will represent one "directory" of references. Rename functions that deal with this structure analogously, and also rename the structure's "refs" member to "entries". Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Michael Haggerty committed
Apr 10, 2012 at 07:30 UTC
d3177275ed8e7dcb258a4496bf7b9317edfb9700
1 file changed
+97
-98
refs.c
+97
-98
@@ -106,7 +106,7 @@ struct ref_value {
106
unsigned char peeled[20];
107
};
108
109
-struct ref_array {
109
+struct ref_dir {
110
int nr, alloc;
111
112
/*
@@ -117,7 +117,7 @@ struct ref_array {
117
*/
118
int sorted;
119
120
- struct ref_entry **refs;
120
+ struct ref_entry **entries;
121
};
122
123
/* ISSYMREF=0x01, ISPACKED=0x02 and ISBROKEN=0x04 are public interfaces */
@@ -156,21 +156,21 @@ static void free_ref_entry(struct ref_entry *entry)
156
free(entry);
157
}
158
159
-/* Add a ref_entry to the end of the ref_array (unsorted). */
160
-static void add_ref(struct ref_array *refs, struct ref_entry *ref)
159
+/* Add a ref_entry to the end of the ref_dir (unsorted). */
160
+static void add_ref(struct ref_dir *refs, struct ref_entry *ref)
161
{
162
- ALLOC_GROW(refs->refs, refs->nr + 1, refs->alloc);
163
- refs->refs[refs->nr++] = ref;
162
+ ALLOC_GROW(refs->entries, refs->nr + 1, refs->alloc);
163
+ refs->entries[refs->nr++] = ref;
164
}
165
166
-static void clear_ref_array(struct ref_array *array)
166
+static void clear_ref_dir(struct ref_dir *dir)
167
{
168
int i;
169
- for (i = 0; i < array->nr; i++)
170
- free_ref_entry(array->refs[i]);
171
- free(array->refs);
172
- array->sorted = array->nr = array->alloc = 0;
173
- array->refs = NULL;
169
+ for (i = 0; i < dir->nr; i++)
170
+ free_ref_entry(dir->entries[i]);
171
+ free(dir->entries);
172
+ dir->sorted = dir->nr = dir->alloc = 0;
173
+ dir->entries = NULL;
174
}
175
176
static int ref_entry_cmp(const void *a, const void *b)
@@ -180,9 +180,9 @@ static int ref_entry_cmp(const void *a, const void *b)
180
return strcmp(one->name, two->name);
181
}
182
183
-static void sort_ref_array(struct ref_array *array);
183
+static void sort_ref_dir(struct ref_dir *dir);
184
185
-static struct ref_entry *search_ref_array(struct ref_array *array, const char *refname)
185
+static struct ref_entry *search_ref_dir(struct ref_dir *dir, const char *refname)
186
{
187
struct ref_entry *e, **r;
188
int len;
@@ -190,14 +190,14 @@ static struct ref_entry *search_ref_array(struct ref_array *array, const char *r
190
if (refname == NULL)
191
return NULL;
192
193
- if (!array->nr)
193
+ if (!dir->nr)
194
return NULL;
195
- sort_ref_array(array);
195
+ sort_ref_dir(dir);
196
len = strlen(refname) + 1;
197
e = xmalloc(sizeof(struct ref_entry) + len);
198
memcpy(e->name, refname, len);
199
200
- r = bsearch(&e, array->refs, array->nr, sizeof(*array->refs), ref_entry_cmp);
200
+ r = bsearch(&e, dir->entries, dir->nr, sizeof(*dir->entries), ref_entry_cmp);
201
202
free(e);
203
@@ -227,9 +227,9 @@ static int is_dup_ref(const struct ref_entry *ref1, const struct ref_entry *ref2
227
}
228
229
/*
230
- * Sort the entries in array (if they are not already sorted).
230
+ * Sort the entries in dir (if they are not already sorted).
231
*/
232
-static void sort_ref_array(struct ref_array *array)
232
+static void sort_ref_dir(struct ref_dir *dir)
233
{
234
int i, j;
235
@@ -237,21 +237,21 @@ static void sort_ref_array(struct ref_array *array)
237
* This check also prevents passing a zero-length array to qsort(),
238
* which is a problem on some platforms.
239
*/
240
- if (array->sorted == array->nr)
240
+ if (dir->sorted == dir->nr)
241
return;
242
243
- qsort(array->refs, array->nr, sizeof(*array->refs), ref_entry_cmp);
243
+ qsort(dir->entries, dir->nr, sizeof(*dir->entries), ref_entry_cmp);
244
245
- /* Remove any duplicates from the ref_array */
245
+ /* Remove any duplicates from the ref_dir */
246
i = 0;
247
- for (j = 1; j < array->nr; j++) {
248
- if (is_dup_ref(array->refs[i], array->refs[j])) {
249
- free_ref_entry(array->refs[j]);
247
+ for (j = 1; j < dir->nr; j++) {
248
+ if (is_dup_ref(dir->entries[i], dir->entries[j])) {
249
+ free_ref_entry(dir->entries[j]);
250
continue;
251
}
252
- array->refs[++i] = array->refs[j];
252
+ dir->entries[++i] = dir->entries[j];
253
}
254
- array->sorted = array->nr = i + 1;
254
+ dir->sorted = dir->nr = i + 1;
255
}
256
257
#define DO_FOR_EACH_INCLUDE_BROKEN 01
@@ -280,18 +280,18 @@ static int do_one_ref(const char *base, each_ref_fn fn, int trim,
280
}
281
282
/*
283
- * Call fn for each reference in array that has index in the range
284
- * offset <= index < array->nr. This function does not sort the
285
- * array; sorting should be done by the caller.
283
+ * Call fn for each reference in dir that has index in the range
284
+ * offset <= index < dir->nr. This function does not sort the dir;
285
+ * sorting should be done by the caller.
286
*/
287
-static int do_for_each_ref_in_array(struct ref_array *array, int offset,
288
- const char *base,
289
- each_ref_fn fn, int trim, int flags, void *cb_data)
287
+static int do_for_each_ref_in_dir(struct ref_dir *dir, int offset,
288
+ const char *base,
289
+ each_ref_fn fn, int trim, int flags, void *cb_data)
290
{
291
int i;
292
- assert(array->sorted == array->nr);
293
- for (i = offset; i < array->nr; i++) {
294
- int retval = do_one_ref(base, fn, trim, flags, cb_data, array->refs[i]);
292
+ assert(dir->sorted == dir->nr);
293
+ for (i = offset; i < dir->nr; i++) {
294
+ int retval = do_one_ref(base, fn, trim, flags, cb_data, dir->entries[i]);
295
if (retval)
296
return retval;
297
}
@@ -299,24 +299,24 @@ static int do_for_each_ref_in_array(struct ref_array *array, int offset,
299
}
300
301
/*
302
- * Call fn for each reference in the union of array1 and array2, in
303
- * order by refname. If an entry appears in both array1 and array2,
304
- * then only process the version that is in array2. The input arrays
305
- * must already be sorted.
302
+ * Call fn for each reference in the union of dir1 and dir2, in order
303
+ * by refname. If an entry appears in both dir1 and dir2, then only
304
+ * process the version that is in dir2. The input dirs must already
305
+ * be sorted.
306
*/
307
-static int do_for_each_ref_in_arrays(struct ref_array *array1,
308
- struct ref_array *array2,
309
- const char *base, each_ref_fn fn, int trim,
310
- int flags, void *cb_data)
307
+static int do_for_each_ref_in_dirs(struct ref_dir *dir1,
308
+ struct ref_dir *dir2,
309
+ const char *base, each_ref_fn fn, int trim,
310
+ int flags, void *cb_data)
311
{
312
int retval;
313
int i1 = 0, i2 = 0;
314
315
- assert(array1->sorted == array1->nr);
316
- assert(array2->sorted == array2->nr);
317
- while (i1 < array1->nr && i2 < array2->nr) {
318
- struct ref_entry *e1 = array1->refs[i1];
319
- struct ref_entry *e2 = array2->refs[i2];
315
+ assert(dir1->sorted == dir1->nr);
316
+ assert(dir2->sorted == dir2->nr);
317
+ while (i1 < dir1->nr && i2 < dir2->nr) {
318
+ struct ref_entry *e1 = dir1->entries[i1];
319
+ struct ref_entry *e2 = dir2->entries[i2];
320
int cmp = strcmp(e1->name, e2->name);
321
if (cmp < 0) {
322
retval = do_one_ref(base, fn, trim, flags, cb_data, e1);
@@ -335,12 +335,12 @@ static int do_for_each_ref_in_arrays(struct ref_array *array1,
335
if (retval)
336
return retval;
337
}
338
- if (i1 < array1->nr)
339
- return do_for_each_ref_in_array(array1, i1,
340
- base, fn, trim, flags, cb_data);
341
- if (i2 < array2->nr)
342
- return do_for_each_ref_in_array(array2, i2,
343
- base, fn, trim, flags, cb_data);
338
+ if (i1 < dir1->nr)
339
+ return do_for_each_ref_in_dir(dir1, i1,
340
+ base, fn, trim, flags, cb_data);
341
+ if (i2 < dir2->nr)
342
+ return do_for_each_ref_in_dir(dir2, i2,
343
+ base, fn, trim, flags, cb_data);
344
return 0;
345
}
346
@@ -386,17 +386,17 @@ static int name_conflict_fn(const char *existingrefname, const unsigned char *sh
386
* operation).
387
*/
388
static int is_refname_available(const char *refname, const char *oldrefname,
389
- struct ref_array *array)
389
+ struct ref_dir *dir)
390
{
391
struct name_conflict_cb data;
392
data.refname = refname;
393
data.oldrefname = oldrefname;
394
data.conflicting_refname = NULL;
395
396
- sort_ref_array(array);
397
- if (do_for_each_ref_in_array(array, 0, "", name_conflict_fn,
398
- 0, DO_FOR_EACH_INCLUDE_BROKEN,
399
- &data)) {
396
+ sort_ref_dir(dir);
397
+ if (do_for_each_ref_in_dir(dir, 0, "", name_conflict_fn,
398
+ 0, DO_FOR_EACH_INCLUDE_BROKEN,
399
+ &data)) {
400
error("'%s' exists; cannot create '%s'",
401
data.conflicting_refname, refname);
402
return 0;
@@ -412,8 +412,8 @@ static struct ref_cache {
412
struct ref_cache *next;
413
char did_loose;
414
char did_packed;
415
- struct ref_array loose;
416
- struct ref_array packed;
415
+ struct ref_dir loose;
416
+ struct ref_dir packed;
417
/* The submodule name, or "" for the main repo. */
418
char name[FLEX_ARRAY];
419
} *ref_cache;
@@ -421,14 +421,14 @@ static struct ref_cache {
421
static void clear_packed_ref_cache(struct ref_cache *refs)
422
{
423
if (refs->did_packed)
424
- clear_ref_array(&refs->packed);
424
+ clear_ref_dir(&refs->packed);
425
refs->did_packed = 0;
426
}
427
428
static void clear_loose_ref_cache(struct ref_cache *refs)
429
{
430
if (refs->did_loose)
431
- clear_ref_array(&refs->loose);
431
+ clear_ref_dir(&refs->loose);
432
refs->did_loose = 0;
433
}
434
@@ -507,7 +507,7 @@ static const char *parse_ref_line(char *line, unsigned char *sha1)
507
return line;
508
}
509
510
-static void read_packed_refs(FILE *f, struct ref_array *array)
510
+static void read_packed_refs(FILE *f, struct ref_dir *dir)
511
{
512
struct ref_entry *last = NULL;
513
char refline[PATH_MAX];
@@ -529,7 +529,7 @@ static void read_packed_refs(FILE *f, struct ref_array *array)
529
refname = parse_ref_line(refline, sha1);
530
if (refname) {
531
last = create_ref_entry(refname, sha1, flag, 1);
532
- add_ref(array, last);
532
+ add_ref(dir, last);
533
continue;
534
}
535
if (last &&
@@ -541,7 +541,7 @@ static void read_packed_refs(FILE *f, struct ref_array *array)
541
}
542
}
543
544
-static struct ref_array *get_packed_refs(struct ref_cache *refs)
544
+static struct ref_dir *get_packed_refs(struct ref_cache *refs)
545
{
546
if (!refs->did_packed) {
547
const char *packed_refs_file;
@@ -568,9 +568,9 @@ void add_packed_ref(const char *refname, const unsigned char *sha1)
568
}
569
570
static void get_ref_dir(struct ref_cache *refs, const char *base,
571
- struct ref_array *array)
571
+ struct ref_dir *dir)
572
{
573
- DIR *dir;
573
+ DIR *d;
574
const char *path;
575
576
if (*refs->name)
@@ -578,9 +578,8 @@ static void get_ref_dir(struct ref_cache *refs, const char *base,
578
else
579
path = git_path("%s", base);
580
581
- dir = opendir(path);
582
-
583
- if (dir) {
581
+ d = opendir(path);
582
+ if (d) {
583
struct dirent *de;
584
int baselen = strlen(base);
585
char *refname = xmalloc(baselen + 257);
@@ -589,7 +588,7 @@ static void get_ref_dir(struct ref_cache *refs, const char *base,
588
if (baselen && base[baselen-1] != '/')
589
refname[baselen++] = '/';
590
592
- while ((de = readdir(dir)) != NULL) {
591
+ while ((de = readdir(d)) != NULL) {
592
unsigned char sha1[20];
593
struct stat st;
594
int flag;
@@ -610,7 +609,7 @@ static void get_ref_dir(struct ref_cache *refs, const char *base,
609
if (stat(refdir, &st) < 0)
610
continue;
611
if (S_ISDIR(st.st_mode)) {
613
- get_ref_dir(refs, refname, array);
612
+ get_ref_dir(refs, refname, dir);
613
continue;
614
}
615
if (*refs->name) {
@@ -624,14 +623,14 @@ static void get_ref_dir(struct ref_cache *refs, const char *base,
623
hashclr(sha1);
624
flag |= REF_ISBROKEN;
625
}
627
- add_ref(array, create_ref_entry(refname, sha1, flag, 1));
626
+ add_ref(dir, create_ref_entry(refname, sha1, flag, 1));
627
}
628
free(refname);
630
- closedir(dir);
629
+ closedir(d);
630
}
631
}
632
634
-static struct ref_array *get_loose_refs(struct ref_cache *refs)
633
+static struct ref_dir *get_loose_refs(struct ref_cache *refs)
634
{
635
if (!refs->did_loose) {
636
get_ref_dir(refs, "refs", &refs->loose);
@@ -653,9 +652,9 @@ static int resolve_gitlink_packed_ref(struct ref_cache *refs,
652
const char *refname, unsigned char *sha1)
653
{
654
struct ref_entry *ref;
656
- struct ref_array *array = get_packed_refs(refs);
655
+ struct ref_dir *dir = get_packed_refs(refs);
656
658
- ref = search_ref_array(array, refname);
657
+ ref = search_ref_dir(dir, refname);
658
if (ref == NULL)
659
return -1;
660
@@ -726,8 +725,8 @@ int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sh
725
*/
726
static int get_packed_ref(const char *refname, unsigned char *sha1)
727
{
729
- struct ref_array *packed = get_packed_refs(get_ref_cache(NULL));
730
- struct ref_entry *entry = search_ref_array(packed, refname);
728
+ struct ref_dir *packed = get_packed_refs(get_ref_cache(NULL));
729
+ struct ref_entry *entry = search_ref_dir(packed, refname);
730
if (entry) {
731
hashcpy(sha1, entry->u.value.sha1);
732
return 0;
@@ -903,8 +902,8 @@ int peel_ref(const char *refname, unsigned char *sha1)
902
return -1;
903
904
if ((flag & REF_ISPACKED)) {
906
- struct ref_array *array = get_packed_refs(get_ref_cache(NULL));
907
- struct ref_entry *r = search_ref_array(array, refname);
905
+ struct ref_dir *dir = get_packed_refs(get_ref_cache(NULL));
906
+ struct ref_entry *r = search_ref_dir(dir, refname);
907
908
if (r != NULL && r->flag & REF_KNOWS_PEELED) {
909
hashcpy(sha1, r->u.value.peeled);
@@ -962,13 +961,13 @@ static int do_for_each_ref(const char *submodule, const char *base, each_ref_fn
961
int trim, int flags, void *cb_data)
962
{
963
struct ref_cache *refs = get_ref_cache(submodule);
965
- struct ref_array *packed_refs = get_packed_refs(refs);
966
- struct ref_array *loose_refs = get_loose_refs(refs);
967
- sort_ref_array(packed_refs);
968
- sort_ref_array(loose_refs);
969
- return do_for_each_ref_in_arrays(packed_refs,
970
- loose_refs,
971
- base, fn, trim, flags, cb_data);
964
+ struct ref_dir *packed_refs = get_packed_refs(refs);
965
+ struct ref_dir *loose_refs = get_loose_refs(refs);
966
+ sort_ref_dir(packed_refs);
967
+ sort_ref_dir(loose_refs);
968
+ return do_for_each_ref_in_dirs(packed_refs,
969
+ loose_refs,
970
+ base, fn, trim, flags, cb_data);
971
}
972
973
static int do_head_ref(const char *submodule, each_ref_fn fn, void *cb_data)
@@ -1403,9 +1402,9 @@ static struct lock_file packlock;
1402
static int repack_without_ref(const char *refname)
1403
{
1404
struct repack_without_ref_sb data;
1406
- struct ref_array *packed = get_packed_refs(get_ref_cache(NULL));
1407
- sort_ref_array(packed);
1408
- if (search_ref_array(packed, refname) == NULL)
1405
+ struct ref_dir *packed = get_packed_refs(get_ref_cache(NULL));
1406
+ sort_ref_dir(packed);
1407
+ if (search_ref_dir(packed, refname) == NULL)
1408
return 0;
1409
data.refname = refname;
1410
data.fd = hold_lock_file_for_update(&packlock, git_path("packed-refs"), 0);
@@ -1413,7 +1412,7 @@ static int repack_without_ref(const char *refname)
1412
unable_to_lock_error(git_path("packed-refs"), errno);
1413
return error("cannot delete '%s' from packed refs", refname);
1414
}
1416
- do_for_each_ref_in_array(packed, 0, "", repack_without_ref_fn, 0, 0, &data);
1415
+ do_for_each_ref_in_dir(packed, 0, "", repack_without_ref_fn, 0, 0, &data);
1416
return commit_lock_file(&packlock);
1417
}
1418
@@ -2024,10 +2023,10 @@ int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn, void *cb_dat
2023
2024
static int do_for_each_reflog(const char *base, each_ref_fn fn, void *cb_data)
2025
{
2027
- DIR *dir = opendir(git_path("logs/%s", base));
2026
+ DIR *d = opendir(git_path("logs/%s", base));
2027
int retval = 0;
2028
2030
- if (dir) {
2029
+ if (d) {
2030
struct dirent *de;
2031
int baselen = strlen(base);
2032
char *log = xmalloc(baselen + 257);
@@ -2036,7 +2035,7 @@ static int do_for_each_reflog(const char *base, each_ref_fn fn, void *cb_data)
2035
if (baselen && base[baselen-1] != '/')
2036
log[baselen++] = '/';
2037
2039
- while ((de = readdir(dir)) != NULL) {
2038
+ while ((de = readdir(d)) != NULL) {
2039
struct stat st;
2040
int namelen;
2041
@@ -2063,7 +2062,7 @@ static int do_for_each_reflog(const char *base, each_ref_fn fn, void *cb_data)
2062
break;
2063
}
2064
free(log);
2066
- closedir(dir);
2065
+ closedir(d);
2066
}
2067
else if (*base)
2068
return errno;