Raw
1 #include "git-compat-util.h"
2 #include "packfile.h"
3 #include "packfile-list.h"
4
5 void packfile_list_clear(struct packfile_list *list)
6 {
7 struct packfile_list_entry *e, *next;
8
9 for (e = list->head; e; e = next) {
10 next = e->next;
11 free(e);
12 }
13
14 list->head = list->tail = NULL;
15 }
16
17 static struct packfile_list_entry *packfile_list_remove_internal(struct packfile_list *list,
18 struct packed_git *pack)
19 {
20 struct packfile_list_entry *e, *prev;
21
22 for (e = list->head, prev = NULL; e; prev = e, e = e->next) {
23 if (e->pack != pack)
24 continue;
25
26 if (prev)
27 prev->next = e->next;
28 if (list->head == e)
29 list->head = e->next;
30 if (list->tail == e)
31 list->tail = prev;
32
33 return e;
34 }
35
36 return NULL;
37 }
38
39 void packfile_list_remove(struct packfile_list *list, struct packed_git *pack)
40 {
41 free(packfile_list_remove_internal(list, pack));
42 }
43
44 void packfile_list_prepend(struct packfile_list *list, struct packed_git *pack)
45 {
46 struct packfile_list_entry *entry;
47
48 entry = packfile_list_remove_internal(list, pack);
49 if (!entry) {
50 entry = xmalloc(sizeof(*entry));
51 entry->pack = pack;
52 }
53 entry->next = list->head;
54
55 list->head = entry;
56 if (!list->tail)
57 list->tail = entry;
58 }
59
60 void packfile_list_append(struct packfile_list *list, struct packed_git *pack)
61 {
62 struct packfile_list_entry *entry;
63
64 entry = packfile_list_remove_internal(list, pack);
65 if (!entry) {
66 entry = xmalloc(sizeof(*entry));
67 entry->pack = pack;
68 }
69 entry->next = NULL;
70
71 if (list->tail) {
72 list->tail->next = entry;
73 list->tail = entry;
74 } else {
75 list->head = list->tail = entry;
76 }
77 }
78
79 struct packed_git *packfile_list_find_oid(struct packfile_list_entry *packs,
80 const struct object_id *oid)
81 {
82 for (; packs; packs = packs->next)
83 if (find_pack_entry_one(oid, packs->pack))
84 return packs->pack;
85 return NULL;
86 }