Skip to content

Commit 593f1bb

Browse files
mhaggergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dac529e commit 593f1bb

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

refs.c

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ int check_refname_format(const char *refname, int flags)
101101

102102
struct ref_entry;
103103

104+
struct ref_value {
105+
unsigned char sha1[20];
106+
unsigned char peeled[20];
107+
};
108+
104109
struct ref_array {
105110
int nr, alloc;
106111

@@ -120,8 +125,9 @@ struct ref_array {
120125

121126
struct ref_entry {
122127
unsigned char flag; /* ISSYMREF? ISPACKED? */
123-
unsigned char sha1[20];
124-
unsigned char peeled[20];
128+
union {
129+
struct ref_value value;
130+
} u;
125131
/* The full name of the reference (e.g., "refs/heads/master"): */
126132
char name[FLEX_ARRAY];
127133
};
@@ -138,8 +144,8 @@ static struct ref_entry *create_ref_entry(const char *refname,
138144
die("Reference has invalid format: '%s'", refname);
139145
len = strlen(refname) + 1;
140146
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);
143149
memcpy(ref->name, refname, len);
144150
ref->flag = flag;
145151
return ref;
@@ -210,7 +216,7 @@ static int is_dup_ref(const struct ref_entry *ref1, const struct ref_entry *ref2
210216
{
211217
if (!strcmp(ref1->name, ref2->name)) {
212218
/* 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))
214220
die("Duplicated ref, and SHA1s don't match: %s",
215221
ref1->name);
216222
warning("Duplicated ref: %s", ref1->name);
@@ -262,13 +268,13 @@ static int do_one_ref(const char *base, each_ref_fn fn, int trim,
262268
if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN)) {
263269
if (entry->flag & REF_ISBROKEN)
264270
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)) {
266272
error("%s does not point to a valid object!", entry->name);
267273
return 0;
268274
}
269275
}
270276
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);
272278
current_ref = NULL;
273279
return retval;
274280
}
@@ -531,7 +537,7 @@ static void read_packed_refs(FILE *f, struct ref_array *array)
531537
strlen(refline) == 42 &&
532538
refline[41] == '\n' &&
533539
!get_sha1_hex(refline + 1, sha1))
534-
hashcpy(last->peeled, sha1);
540+
hashcpy(last->u.value.peeled, sha1);
535541
}
536542
}
537543

@@ -653,7 +659,7 @@ static int resolve_gitlink_packed_ref(struct ref_cache *refs,
653659
if (ref == NULL)
654660
return -1;
655661

656-
memcpy(sha1, ref->sha1, 20);
662+
memcpy(sha1, ref->u.value.sha1, 20);
657663
return 0;
658664
}
659665

@@ -723,7 +729,7 @@ static int get_packed_ref(const char *refname, unsigned char *sha1)
723729
struct ref_array *packed = get_packed_refs(get_ref_cache(NULL));
724730
struct ref_entry *entry = search_ref_array(packed, refname);
725731
if (entry) {
726-
hashcpy(sha1, entry->sha1);
732+
hashcpy(sha1, entry->u.value.sha1);
727733
return 0;
728734
}
729735
return -1;
@@ -886,10 +892,10 @@ int peel_ref(const char *refname, unsigned char *sha1)
886892
if (current_ref && (current_ref->name == refname
887893
|| !strcmp(current_ref->name, refname))) {
888894
if (current_ref->flag & REF_KNOWS_PEELED) {
889-
hashcpy(sha1, current_ref->peeled);
895+
hashcpy(sha1, current_ref->u.value.peeled);
890896
return 0;
891897
}
892-
hashcpy(base, current_ref->sha1);
898+
hashcpy(base, current_ref->u.value.sha1);
893899
goto fallback;
894900
}
895901

@@ -901,7 +907,7 @@ int peel_ref(const char *refname, unsigned char *sha1)
901907
struct ref_entry *r = search_ref_array(array, refname);
902908

903909
if (r != NULL && r->flag & REF_KNOWS_PEELED) {
904-
hashcpy(sha1, r->peeled);
910+
hashcpy(sha1, r->u.value.peeled);
905911
return 0;
906912
}
907913
}

0 commit comments

Comments
 (0)