Skip to content

Commit 7d1f442

Browse files
ttaylorrgitster
authored andcommitted
repack: remove 'existing_packs' API from the builtin
The repack builtin defines an API for keeping track of which packs were found in the repository at the beginning of the repack operation. This is used to classify what state a pack was in (kept, non-kept, or cruft), and is also used to mark which packs to delete (or keep) at the end of a repack operation. Now that the prerequisite refactoring is complete, this API is isolated enough that it can be moved out to repack.[ch] and removed from the builtin entirely. As a result, some of its functions become static within repack.c, cleaning up the visible API. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent dab24e4 commit 7d1f442

3 files changed

Lines changed: 192 additions & 173 deletions

File tree

builtin/repack.c

Lines changed: 0 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
#include "builtin.h"
55
#include "config.h"
6-
#include "dir.h"
76
#include "environment.h"
87
#include "gettext.h"
98
#include "hex.h"
@@ -108,178 +107,6 @@ static int repack_config(const char *var, const char *value,
108107
return git_default_config(var, value, ctx, cb);
109108
}
110109

111-
struct existing_packs {
112-
struct repository *repo;
113-
struct string_list kept_packs;
114-
struct string_list non_kept_packs;
115-
struct string_list cruft_packs;
116-
};
117-
118-
#define EXISTING_PACKS_INIT { \
119-
.kept_packs = STRING_LIST_INIT_DUP, \
120-
.non_kept_packs = STRING_LIST_INIT_DUP, \
121-
.cruft_packs = STRING_LIST_INIT_DUP, \
122-
}
123-
124-
static int existing_packs_has_non_kept(const struct existing_packs *existing)
125-
{
126-
return existing->non_kept_packs.nr || existing->cruft_packs.nr;
127-
}
128-
129-
static void existing_pack_mark_for_deletion(struct string_list_item *item)
130-
{
131-
item->util = (void*)((uintptr_t)item->util | DELETE_PACK);
132-
}
133-
134-
static void existing_pack_unmark_for_deletion(struct string_list_item *item)
135-
{
136-
item->util = (void*)((uintptr_t)item->util & ~DELETE_PACK);
137-
}
138-
139-
static int existing_pack_is_marked_for_deletion(struct string_list_item *item)
140-
{
141-
return (uintptr_t)item->util & DELETE_PACK;
142-
}
143-
144-
static void existing_packs_mark_retained(struct string_list_item *item)
145-
{
146-
item->util = (void*)((uintptr_t)item->util | RETAIN_PACK);
147-
}
148-
149-
static int existing_pack_is_retained(struct string_list_item *item)
150-
{
151-
return (uintptr_t)item->util & RETAIN_PACK;
152-
}
153-
154-
static void existing_packs_mark_for_deletion_1(const struct git_hash_algo *algop,
155-
struct string_list *names,
156-
struct string_list *list)
157-
{
158-
struct string_list_item *item;
159-
const size_t hexsz = algop->hexsz;
160-
161-
for_each_string_list_item(item, list) {
162-
char *sha1;
163-
size_t len = strlen(item->string);
164-
if (len < hexsz)
165-
continue;
166-
sha1 = item->string + len - hexsz;
167-
168-
if (existing_pack_is_retained(item)) {
169-
existing_pack_unmark_for_deletion(item);
170-
} else if (!string_list_has_string(names, sha1)) {
171-
/*
172-
* Mark this pack for deletion, which ensures
173-
* that this pack won't be included in a MIDX
174-
* (if `--write-midx` was given) and that we
175-
* will actually delete this pack (if `-d` was
176-
* given).
177-
*/
178-
existing_pack_mark_for_deletion(item);
179-
}
180-
}
181-
}
182-
183-
static void existing_packs_retain_cruft(struct existing_packs *existing,
184-
struct packed_git *cruft)
185-
{
186-
struct strbuf buf = STRBUF_INIT;
187-
struct string_list_item *item;
188-
189-
strbuf_addstr(&buf, pack_basename(cruft));
190-
strbuf_strip_suffix(&buf, ".pack");
191-
192-
item = string_list_lookup(&existing->cruft_packs, buf.buf);
193-
if (!item)
194-
BUG("could not find cruft pack '%s'", pack_basename(cruft));
195-
196-
existing_packs_mark_retained(item);
197-
strbuf_release(&buf);
198-
}
199-
200-
static void existing_packs_mark_for_deletion(struct existing_packs *existing,
201-
struct string_list *names)
202-
203-
{
204-
const struct git_hash_algo *algop = existing->repo->hash_algo;
205-
existing_packs_mark_for_deletion_1(algop, names,
206-
&existing->non_kept_packs);
207-
existing_packs_mark_for_deletion_1(algop, names,
208-
&existing->cruft_packs);
209-
}
210-
211-
static void remove_redundant_packs_1(struct repository *repo,
212-
struct string_list *packs,
213-
const char *packdir)
214-
{
215-
struct string_list_item *item;
216-
for_each_string_list_item(item, packs) {
217-
if (!existing_pack_is_marked_for_deletion(item))
218-
continue;
219-
repack_remove_redundant_pack(repo, packdir, item->string);
220-
}
221-
}
222-
223-
static void existing_packs_remove_redundant(struct existing_packs *existing,
224-
const char *packdir)
225-
{
226-
remove_redundant_packs_1(existing->repo, &existing->non_kept_packs,
227-
packdir);
228-
remove_redundant_packs_1(existing->repo, &existing->cruft_packs,
229-
packdir);
230-
}
231-
232-
static void existing_packs_release(struct existing_packs *existing)
233-
{
234-
string_list_clear(&existing->kept_packs, 0);
235-
string_list_clear(&existing->non_kept_packs, 0);
236-
string_list_clear(&existing->cruft_packs, 0);
237-
}
238-
239-
/*
240-
* Adds all packs hex strings (pack-$HASH) to either packs->non_kept
241-
* or packs->kept based on whether each pack has a corresponding
242-
* .keep file or not. Packs without a .keep file are not to be kept
243-
* if we are going to pack everything into one file.
244-
*/
245-
static void existing_packs_collect(struct existing_packs *existing,
246-
const struct string_list *extra_keep)
247-
{
248-
struct packfile_store *packs = existing->repo->objects->packfiles;
249-
struct packed_git *p;
250-
struct strbuf buf = STRBUF_INIT;
251-
252-
for (p = packfile_store_get_all_packs(packs); p; p = p->next) {
253-
size_t i;
254-
const char *base;
255-
256-
if (!p->pack_local)
257-
continue;
258-
259-
base = pack_basename(p);
260-
261-
for (i = 0; i < extra_keep->nr; i++)
262-
if (!fspathcmp(base, extra_keep->items[i].string))
263-
break;
264-
265-
strbuf_reset(&buf);
266-
strbuf_addstr(&buf, base);
267-
strbuf_strip_suffix(&buf, ".pack");
268-
269-
if ((extra_keep->nr > 0 && i < extra_keep->nr) || p->pack_keep)
270-
string_list_append(&existing->kept_packs, buf.buf);
271-
else if (p->is_cruft)
272-
string_list_append(&existing->cruft_packs, buf.buf);
273-
else
274-
string_list_append(&existing->non_kept_packs, buf.buf);
275-
}
276-
277-
string_list_sort(&existing->kept_packs);
278-
string_list_sort(&existing->non_kept_packs);
279-
string_list_sort(&existing->cruft_packs);
280-
strbuf_release(&buf);
281-
}
282-
283110
struct write_oid_context {
284111
struct child_process *cmd;
285112
const struct git_hash_algo *algop;

repack.c

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "git-compat-util.h"
2+
#include "dir.h"
23
#include "midx.h"
34
#include "odb.h"
45
#include "packfile.h"
@@ -62,3 +63,159 @@ void repack_remove_redundant_pack(struct repository *repo, const char *dir_name,
6263
unlink_pack_path(buf.buf, 1);
6364
strbuf_release(&buf);
6465
}
66+
67+
#define DELETE_PACK 1
68+
#define RETAIN_PACK 2
69+
70+
void existing_packs_collect(struct existing_packs *existing,
71+
const struct string_list *extra_keep)
72+
{
73+
struct packfile_store *packs = existing->repo->objects->packfiles;
74+
struct packed_git *p;
75+
struct strbuf buf = STRBUF_INIT;
76+
77+
for (p = packfile_store_get_all_packs(packs); p; p = p->next) {
78+
size_t i;
79+
const char *base;
80+
81+
if (!p->pack_local)
82+
continue;
83+
84+
base = pack_basename(p);
85+
86+
for (i = 0; i < extra_keep->nr; i++)
87+
if (!fspathcmp(base, extra_keep->items[i].string))
88+
break;
89+
90+
strbuf_reset(&buf);
91+
strbuf_addstr(&buf, base);
92+
strbuf_strip_suffix(&buf, ".pack");
93+
94+
if ((extra_keep->nr > 0 && i < extra_keep->nr) || p->pack_keep)
95+
string_list_append(&existing->kept_packs, buf.buf);
96+
else if (p->is_cruft)
97+
string_list_append(&existing->cruft_packs, buf.buf);
98+
else
99+
string_list_append(&existing->non_kept_packs, buf.buf);
100+
}
101+
102+
string_list_sort(&existing->kept_packs);
103+
string_list_sort(&existing->non_kept_packs);
104+
string_list_sort(&existing->cruft_packs);
105+
strbuf_release(&buf);
106+
}
107+
108+
int existing_packs_has_non_kept(const struct existing_packs *existing)
109+
{
110+
return existing->non_kept_packs.nr || existing->cruft_packs.nr;
111+
}
112+
113+
static void existing_pack_mark_for_deletion(struct string_list_item *item)
114+
{
115+
item->util = (void*)((uintptr_t)item->util | DELETE_PACK);
116+
}
117+
118+
static void existing_pack_unmark_for_deletion(struct string_list_item *item)
119+
{
120+
item->util = (void*)((uintptr_t)item->util & ~DELETE_PACK);
121+
}
122+
123+
int existing_pack_is_marked_for_deletion(struct string_list_item *item)
124+
{
125+
return (uintptr_t)item->util & DELETE_PACK;
126+
}
127+
128+
static void existing_packs_mark_retained(struct string_list_item *item)
129+
{
130+
item->util = (void*)((uintptr_t)item->util | RETAIN_PACK);
131+
}
132+
133+
static int existing_pack_is_retained(struct string_list_item *item)
134+
{
135+
return (uintptr_t)item->util & RETAIN_PACK;
136+
}
137+
138+
static void existing_packs_mark_for_deletion_1(const struct git_hash_algo *algop,
139+
struct string_list *names,
140+
struct string_list *list)
141+
{
142+
struct string_list_item *item;
143+
const size_t hexsz = algop->hexsz;
144+
145+
for_each_string_list_item(item, list) {
146+
char *sha1;
147+
size_t len = strlen(item->string);
148+
if (len < hexsz)
149+
continue;
150+
sha1 = item->string + len - hexsz;
151+
152+
if (existing_pack_is_retained(item)) {
153+
existing_pack_unmark_for_deletion(item);
154+
} else if (!string_list_has_string(names, sha1)) {
155+
/*
156+
* Mark this pack for deletion, which ensures
157+
* that this pack won't be included in a MIDX
158+
* (if `--write-midx` was given) and that we
159+
* will actually delete this pack (if `-d` was
160+
* given).
161+
*/
162+
existing_pack_mark_for_deletion(item);
163+
}
164+
}
165+
}
166+
167+
void existing_packs_retain_cruft(struct existing_packs *existing,
168+
struct packed_git *cruft)
169+
{
170+
struct strbuf buf = STRBUF_INIT;
171+
struct string_list_item *item;
172+
173+
strbuf_addstr(&buf, pack_basename(cruft));
174+
strbuf_strip_suffix(&buf, ".pack");
175+
176+
item = string_list_lookup(&existing->cruft_packs, buf.buf);
177+
if (!item)
178+
BUG("could not find cruft pack '%s'", pack_basename(cruft));
179+
180+
existing_packs_mark_retained(item);
181+
strbuf_release(&buf);
182+
}
183+
184+
void existing_packs_mark_for_deletion(struct existing_packs *existing,
185+
struct string_list *names)
186+
187+
{
188+
const struct git_hash_algo *algop = existing->repo->hash_algo;
189+
existing_packs_mark_for_deletion_1(algop, names,
190+
&existing->non_kept_packs);
191+
existing_packs_mark_for_deletion_1(algop, names,
192+
&existing->cruft_packs);
193+
}
194+
195+
static void remove_redundant_packs_1(struct repository *repo,
196+
struct string_list *packs,
197+
const char *packdir)
198+
{
199+
struct string_list_item *item;
200+
for_each_string_list_item(item, packs) {
201+
if (!existing_pack_is_marked_for_deletion(item))
202+
continue;
203+
repack_remove_redundant_pack(repo, packdir, item->string);
204+
}
205+
}
206+
207+
void existing_packs_remove_redundant(struct existing_packs *existing,
208+
const char *packdir)
209+
{
210+
remove_redundant_packs_1(existing->repo, &existing->non_kept_packs,
211+
packdir);
212+
remove_redundant_packs_1(existing->repo, &existing->cruft_packs,
213+
packdir);
214+
}
215+
216+
void existing_packs_release(struct existing_packs *existing)
217+
{
218+
string_list_clear(&existing->kept_packs, 0);
219+
string_list_clear(&existing->non_kept_packs, 0);
220+
string_list_clear(&existing->cruft_packs, 0);
221+
}

repack.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define REPACK_H
33

44
#include "list-objects-filter-options.h"
5+
#include "string-list.h"
56

67
struct pack_objects_args {
78
char *window;
@@ -31,4 +32,38 @@ void pack_objects_args_release(struct pack_objects_args *args);
3132
void repack_remove_redundant_pack(struct repository *repo, const char *dir_name,
3233
const char *base_name);
3334

35+
struct repository;
36+
struct packed_git;
37+
38+
struct existing_packs {
39+
struct repository *repo;
40+
struct string_list kept_packs;
41+
struct string_list non_kept_packs;
42+
struct string_list cruft_packs;
43+
};
44+
45+
#define EXISTING_PACKS_INIT { \
46+
.kept_packs = STRING_LIST_INIT_DUP, \
47+
.non_kept_packs = STRING_LIST_INIT_DUP, \
48+
.cruft_packs = STRING_LIST_INIT_DUP, \
49+
}
50+
51+
/*
52+
* Adds all packs hex strings (pack-$HASH) to either packs->non_kept
53+
* or packs->kept based on whether each pack has a corresponding
54+
* .keep file or not. Packs without a .keep file are not to be kept
55+
* if we are going to pack everything into one file.
56+
*/
57+
void existing_packs_collect(struct existing_packs *existing,
58+
const struct string_list *extra_keep);
59+
int existing_packs_has_non_kept(const struct existing_packs *existing);
60+
int existing_pack_is_marked_for_deletion(struct string_list_item *item);
61+
void existing_packs_retain_cruft(struct existing_packs *existing,
62+
struct packed_git *cruft);
63+
void existing_packs_mark_for_deletion(struct existing_packs *existing,
64+
struct string_list *names);
65+
void existing_packs_remove_redundant(struct existing_packs *existing,
66+
const char *packdir);
67+
void existing_packs_release(struct existing_packs *existing);
68+
3469
#endif /* REPACK_H */

0 commit comments

Comments
 (0)