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>

brian m. carlson committed May 2, 2018 at 00:25 UTC 6390fe20eba4afb1d70e05714c62be7908ad592a
1 file changed +26 -24
builtin/pack-redundant.c
+26 -24
@@ -20,7 +20,7 @@ static int load_all_packs, verbose, alt_odb;
20
21 struct llist_item {
22 struct llist_item *next;
23 - const unsigned char *sha1;
23 + const struct object_id *oid;
24 };
25 static struct llist {
26 struct llist_item *front;
@@ -90,14 +90,14 @@ static struct llist * llist_copy(struct llist *list)
90 return ret;
91
92 new_item = ret->front = llist_item_get();
93 - new_item->sha1 = list->front->sha1;
93 + new_item->oid = list->front->oid;
94
95 old_item = list->front->next;
96 while (old_item) {
97 prev = new_item;
98 new_item = llist_item_get();
99 prev->next = new_item;
100 - new_item->sha1 = old_item->sha1;
100 + new_item->oid = old_item->oid;
101 old_item = old_item->next;
102 }
103 new_item->next = NULL;
@@ -108,10 +108,10 @@ static struct llist * llist_copy(struct llist *list)
108
109 static inline struct llist_item *llist_insert(struct llist *list,
110 struct llist_item *after,
111 - const unsigned char *sha1)
111 + const struct object_id *oid)
112 {
113 struct llist_item *new_item = llist_item_get();
114 - new_item->sha1 = sha1;
114 + new_item->oid = oid;
115 new_item->next = NULL;
116
117 if (after != NULL) {
@@ -131,21 +131,21 @@ static inline struct llist_item *llist_insert(struct llist *list,
131 }
132
133 static inline struct llist_item *llist_insert_back(struct llist *list,
134 - const unsigned char *sha1)
134 + const struct object_id *oid)
135 {
136 - return llist_insert(list, list->back, sha1);
136 + return llist_insert(list, list->back, oid);
137 }
138
139 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)
141 {
142 struct llist_item *prev = NULL, *l;
143
144 l = (hint == NULL) ? list->front : hint;
145 while (l) {
146 - int cmp = hashcmp(l->sha1, sha1);
146 + int cmp = oidcmp(l->oid, oid);
147 if (cmp > 0) { /* we insert before this entry */
148 - return llist_insert(list, prev, sha1);
148 + return llist_insert(list, prev, oid);
149 }
150 if (!cmp) { /* already exists */
151 return l;
@@ -154,11 +154,11 @@ static inline struct llist_item *llist_insert_sorted_unique(struct llist *list,
154 l = l->next;
155 }
156 /* insert at the end */
157 - return llist_insert_back(list, sha1);
157 + return llist_insert_back(list, oid);
158 }
159
160 /* 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)
162 {
163 struct llist_item *prev, *l;
164
@@ -166,7 +166,7 @@ redo_from_start:
166 l = (hint == NULL) ? list->front : hint;
167 prev = NULL;
168 while (l) {
169 - int cmp = hashcmp(l->sha1, sha1);
169 + int cmp = oidcmp(l->oid, oid);
170 if (cmp > 0) /* not in list, since sorted */
171 return prev;
172 if (!cmp) { /* found */
@@ -201,7 +201,7 @@ static void llist_sorted_difference_inplace(struct llist *A,
201 b = B->front;
202
203 while (b) {
204 - hint = llist_sorted_remove(A, b->sha1, hint);
204 + hint = llist_sorted_remove(A, b->oid, hint);
205 b = b->next;
206 }
207 }
@@ -268,9 +268,11 @@ static void cmp_two_packs(struct pack_list *p1, struct pack_list *p2)
268 /* cmp ~ p1 - p2 */
269 if (cmp == 0) {
270 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);
273 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);
276 p1_off += p1_step;
277 p2_off += p2_step;
278 continue;
@@ -501,7 +503,7 @@ static void load_all_objects(void)
503 l = pl->all_objects->front;
504 while (l) {
505 hint = llist_insert_sorted_unique(all_objects,
504 - l->sha1, hint);
506 + l->oid, hint);
507 l = l->next;
508 }
509 pl = pl->next;
@@ -562,7 +564,7 @@ static struct pack_list * add_pack(struct packed_git *p)
564 base += 256 * 4 + ((p->index_version < 2) ? 4 : 8);
565 step = the_hash_algo->rawsz + ((p->index_version < 2) ? 4 : 0);
566 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));
568 off += step;
569 }
570 /* 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)
605 int i;
606 struct pack_list *min, *red, *pl;
607 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 */
610
611 if (argc == 2 && !strcmp(argv[1], "-h"))
612 usage(pack_redundant_usage);
@@ -652,10 +654,10 @@ int cmd_pack_redundant(int argc, const char **argv, const char *prefix)
654 llist_init(&ignore);
655 if (!isatty(0)) {
656 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);
661 }
662 }
663 llist_sorted_difference_inplace(all_objects, ignore);