struct ref_entry: nest the value part in a union
This change is obviously silly by itself, but it is a step towards adding a second member to the union. 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
593f1bb82fcf3a02b7984a21988596136917e5dc
1 file changed
+19
-13
refs.c
+19
-13
@@ -101,6 +101,11 @@ int check_refname_format(const char *refname, int flags)
101
102
struct ref_entry;
103
104
+struct ref_value {
105
+ unsigned char sha1[20];
106
+ unsigned char peeled[20];
107
+};
108
+
109
struct ref_array {
110
int nr, alloc;
111
@@ -120,8 +125,9 @@ struct ref_array {
125
126
struct ref_entry {
127
unsigned char flag; /* ISSYMREF? ISPACKED? */
123
- unsigned char sha1[20];
124
- unsigned char peeled[20];
128
+ union {
129
+ struct ref_value value;
130
+ } u;
131
/* The full name of the reference (e.g., "refs/heads/master"): */
132
char name[FLEX_ARRAY];
133
};
@@ -138,8 +144,8 @@ static struct ref_entry *create_ref_entry(const char *refname,
144
die("Reference has invalid format: '%s'", refname);
145
len = strlen(refname) + 1;
146
ref = xmalloc(sizeof(struct ref_entry) + len);
141
- hashcpy(ref->sha1, sha1);
142
- hashclr(ref->peeled);
147
+ hashcpy(ref->u.value.sha1, sha1);
148
+ hashclr(ref->u.value.peeled);
149
memcpy(ref->name, refname, len);
150
ref->flag = flag;
151
return ref;
@@ -210,7 +216,7 @@ static int is_dup_ref(const struct ref_entry *ref1, const struct ref_entry *ref2
216
{
217
if (!strcmp(ref1->name, ref2->name)) {
218
/* Duplicate name; make sure that the SHA1s match: */
213
- if (hashcmp(ref1->sha1, ref2->sha1))
219
+ if (hashcmp(ref1->u.value.sha1, ref2->u.value.sha1))
220
die("Duplicated ref, and SHA1s don't match: %s",
221
ref1->name);
222
warning("Duplicated ref: %s", ref1->name);
@@ -262,13 +268,13 @@ static int do_one_ref(const char *base, each_ref_fn fn, int trim,
268
if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN)) {
269
if (entry->flag & REF_ISBROKEN)
270
return 0; /* ignore broken refs e.g. dangling symref */
265
- if (!has_sha1_file(entry->sha1)) {
271
+ if (!has_sha1_file(entry->u.value.sha1)) {
272
error("%s does not point to a valid object!", entry->name);
273
return 0;
274
}
275
}
276
current_ref = entry;
271
- retval = fn(entry->name + trim, entry->sha1, entry->flag, cb_data);
277
+ retval = fn(entry->name + trim, entry->u.value.sha1, entry->flag, cb_data);
278
current_ref = NULL;
279
return retval;
280
}
@@ -531,7 +537,7 @@ static void read_packed_refs(FILE *f, struct ref_array *array)
537
strlen(refline) == 42 &&
538
refline[41] == '\n' &&
539
!get_sha1_hex(refline + 1, sha1))
534
- hashcpy(last->peeled, sha1);
540
+ hashcpy(last->u.value.peeled, sha1);
541
}
542
}
543
@@ -653,7 +659,7 @@ static int resolve_gitlink_packed_ref(struct ref_cache *refs,
659
if (ref == NULL)
660
return -1;
661
656
- memcpy(sha1, ref->sha1, 20);
662
+ memcpy(sha1, ref->u.value.sha1, 20);
663
return 0;
664
}
665
@@ -723,7 +729,7 @@ static int get_packed_ref(const char *refname, unsigned char *sha1)
729
struct ref_array *packed = get_packed_refs(get_ref_cache(NULL));
730
struct ref_entry *entry = search_ref_array(packed, refname);
731
if (entry) {
726
- hashcpy(sha1, entry->sha1);
732
+ hashcpy(sha1, entry->u.value.sha1);
733
return 0;
734
}
735
return -1;
@@ -886,10 +892,10 @@ int peel_ref(const char *refname, unsigned char *sha1)
892
if (current_ref && (current_ref->name == refname
893
|| !strcmp(current_ref->name, refname))) {
894
if (current_ref->flag & REF_KNOWS_PEELED) {
889
- hashcpy(sha1, current_ref->peeled);
895
+ hashcpy(sha1, current_ref->u.value.peeled);
896
return 0;
897
}
892
- hashcpy(base, current_ref->sha1);
898
+ hashcpy(base, current_ref->u.value.sha1);
899
goto fallback;
900
}
901
@@ -901,7 +907,7 @@ int peel_ref(const char *refname, unsigned char *sha1)
907
struct ref_entry *r = search_ref_array(array, refname);
908
909
if (r != NULL && r->flag & REF_KNOWS_PEELED) {
904
- hashcpy(sha1, r->peeled);
910
+ hashcpy(sha1, r->u.value.peeled);
911
return 0;
912
}
913
}