Skip to content

Commit 6390fe2

Browse files
bk2204gitster
authored andcommitted
pack-redundant: convert linked lists to use struct object_id
Convert struct llist_item and the rest of the linked list code to use struct object_id. Add a use of GIT_MAX_HEXSZ to avoid a dependency on a hard-coded constant. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 75691ea commit 6390fe2

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

builtin/pack-redundant.c

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ static int load_all_packs, verbose, alt_odb;
2020

2121
struct llist_item {
2222
struct llist_item *next;
23-
const unsigned char *sha1;
23+
const struct object_id *oid;
2424
};
2525
static struct llist {
2626
struct llist_item *front;
@@ -90,14 +90,14 @@ static struct llist * llist_copy(struct llist *list)
9090
return ret;
9191

9292
new_item = ret->front = llist_item_get();
93-
new_item->sha1 = list->front->sha1;
93+
new_item->oid = list->front->oid;
9494

9595
old_item = list->front->next;
9696
while (old_item) {
9797
prev = new_item;
9898
new_item = llist_item_get();
9999
prev->next = new_item;
100-
new_item->sha1 = old_item->sha1;
100+
new_item->oid = old_item->oid;
101101
old_item = old_item->next;
102102
}
103103
new_item->next = NULL;
@@ -108,10 +108,10 @@ static struct llist * llist_copy(struct llist *list)
108108

109109
static inline struct llist_item *llist_insert(struct llist *list,
110110
struct llist_item *after,
111-
const unsigned char *sha1)
111+
const struct object_id *oid)
112112
{
113113
struct llist_item *new_item = llist_item_get();
114-
new_item->sha1 = sha1;
114+
new_item->oid = oid;
115115
new_item->next = NULL;
116116

117117
if (after != NULL) {
@@ -131,21 +131,21 @@ static inline struct llist_item *llist_insert(struct llist *list,
131131
}
132132

133133
static inline struct llist_item *llist_insert_back(struct llist *list,
134-
const unsigned char *sha1)
134+
const struct object_id *oid)
135135
{
136-
return llist_insert(list, list->back, sha1);
136+
return llist_insert(list, list->back, oid);
137137
}
138138

139139
static inline struct llist_item *llist_insert_sorted_unique(struct llist *list,
140-
const unsigned char *sha1, struct llist_item *hint)
140+
const struct object_id *oid, struct llist_item *hint)
141141
{
142142
struct llist_item *prev = NULL, *l;
143143

144144
l = (hint == NULL) ? list->front : hint;
145145
while (l) {
146-
int cmp = hashcmp(l->sha1, sha1);
146+
int cmp = oidcmp(l->oid, oid);
147147
if (cmp > 0) { /* we insert before this entry */
148-
return llist_insert(list, prev, sha1);
148+
return llist_insert(list, prev, oid);
149149
}
150150
if (!cmp) { /* already exists */
151151
return l;
@@ -154,19 +154,19 @@ static inline struct llist_item *llist_insert_sorted_unique(struct llist *list,
154154
l = l->next;
155155
}
156156
/* insert at the end */
157-
return llist_insert_back(list, sha1);
157+
return llist_insert_back(list, oid);
158158
}
159159

160160
/* returns a pointer to an item in front of sha1 */
161-
static inline struct llist_item * llist_sorted_remove(struct llist *list, const unsigned char *sha1, struct llist_item *hint)
161+
static inline struct llist_item * llist_sorted_remove(struct llist *list, const struct object_id *oid, struct llist_item *hint)
162162
{
163163
struct llist_item *prev, *l;
164164

165165
redo_from_start:
166166
l = (hint == NULL) ? list->front : hint;
167167
prev = NULL;
168168
while (l) {
169-
int cmp = hashcmp(l->sha1, sha1);
169+
int cmp = oidcmp(l->oid, oid);
170170
if (cmp > 0) /* not in list, since sorted */
171171
return prev;
172172
if (!cmp) { /* found */
@@ -201,7 +201,7 @@ static void llist_sorted_difference_inplace(struct llist *A,
201201
b = B->front;
202202

203203
while (b) {
204-
hint = llist_sorted_remove(A, b->sha1, hint);
204+
hint = llist_sorted_remove(A, b->oid, hint);
205205
b = b->next;
206206
}
207207
}
@@ -268,9 +268,11 @@ static void cmp_two_packs(struct pack_list *p1, struct pack_list *p2)
268268
/* cmp ~ p1 - p2 */
269269
if (cmp == 0) {
270270
p1_hint = llist_sorted_remove(p1->unique_objects,
271-
p1_base + p1_off, p1_hint);
271+
(const struct object_id *)(p1_base + p1_off),
272+
p1_hint);
272273
p2_hint = llist_sorted_remove(p2->unique_objects,
273-
p1_base + p1_off, p2_hint);
274+
(const struct object_id *)(p1_base + p1_off),
275+
p2_hint);
274276
p1_off += p1_step;
275277
p2_off += p2_step;
276278
continue;
@@ -501,7 +503,7 @@ static void load_all_objects(void)
501503
l = pl->all_objects->front;
502504
while (l) {
503505
hint = llist_insert_sorted_unique(all_objects,
504-
l->sha1, hint);
506+
l->oid, hint);
505507
l = l->next;
506508
}
507509
pl = pl->next;
@@ -562,7 +564,7 @@ static struct pack_list * add_pack(struct packed_git *p)
562564
base += 256 * 4 + ((p->index_version < 2) ? 4 : 8);
563565
step = the_hash_algo->rawsz + ((p->index_version < 2) ? 4 : 0);
564566
while (off < p->num_objects * step) {
565-
llist_insert_back(l.all_objects, base + off);
567+
llist_insert_back(l.all_objects, (const struct object_id *)(base + off));
566568
off += step;
567569
}
568570
/* this list will be pruned in cmp_two_packs later */
@@ -603,8 +605,8 @@ int cmd_pack_redundant(int argc, const char **argv, const char *prefix)
603605
int i;
604606
struct pack_list *min, *red, *pl;
605607
struct llist *ignore;
606-
unsigned char *sha1;
607-
char buf[42]; /* 40 byte sha1 + \n + \0 */
608+
struct object_id *oid;
609+
char buf[GIT_MAX_HEXSZ + 2]; /* hex hash + \n + \0 */
608610

609611
if (argc == 2 && !strcmp(argv[1], "-h"))
610612
usage(pack_redundant_usage);
@@ -652,10 +654,10 @@ int cmd_pack_redundant(int argc, const char **argv, const char *prefix)
652654
llist_init(&ignore);
653655
if (!isatty(0)) {
654656
while (fgets(buf, sizeof(buf), stdin)) {
655-
sha1 = xmalloc(20);
656-
if (get_sha1_hex(buf, sha1))
657-
die("Bad sha1 on stdin: %s", buf);
658-
llist_insert_sorted_unique(ignore, sha1, NULL);
657+
oid = xmalloc(sizeof(*oid));
658+
if (get_oid_hex(buf, oid))
659+
die("Bad object ID on stdin: %s", buf);
660+
llist_insert_sorted_unique(ignore, oid, NULL);
659661
}
660662
}
661663
llist_sorted_difference_inplace(all_objects, ignore);

0 commit comments

Comments
 (0)