Raw
1 #include "git-compat-util.h"
2 #include "repack.h"
3 #include "packfile.h"
4 #include "repository.h"
5 #include "run-command.h"
6
7 static void combine_small_cruft_packs(FILE *in, off_t combine_cruft_below_size,
8 struct existing_packs *existing)
9 {
10 struct packed_git *p;
11 struct strbuf buf = STRBUF_INIT;
12 size_t i;
13
14 repo_for_each_pack(existing->repo, p) {
15 if (!(p->is_cruft && p->pack_local))
16 continue;
17
18 strbuf_reset(&buf);
19 strbuf_addstr(&buf, pack_basename(p));
20 strbuf_strip_suffix(&buf, ".pack");
21
22 if (!string_list_has_string(&existing->cruft_packs, buf.buf))
23 continue;
24
25 if (p->pack_size < combine_cruft_below_size) {
26 fprintf(in, "-%s\n", pack_basename(p));
27 } else {
28 existing_packs_retain_cruft(existing, p);
29 fprintf(in, "%s\n", pack_basename(p));
30 }
31 }
32
33 for (i = 0; i < existing->non_kept_packs.nr; i++)
34 fprintf(in, "-%s.pack\n",
35 existing->non_kept_packs.items[i].string);
36
37 strbuf_release(&buf);
38 }
39
40 int write_cruft_pack(const struct write_pack_opts *opts,
41 const char *cruft_expiration,
42 unsigned long combine_cruft_below_size,
43 struct string_list *names,
44 struct existing_packs *existing)
45 {
46 struct child_process cmd = CHILD_PROCESS_INIT;
47 struct string_list_item *item;
48 FILE *in;
49 int ret;
50 const char *pack_prefix = write_pack_opts_pack_prefix(opts);
51
52 prepare_pack_objects(&cmd, opts->po_args, opts->destination);
53
54 strvec_push(&cmd.args, "--cruft");
55 if (cruft_expiration)
56 strvec_pushf(&cmd.args, "--cruft-expiration=%s",
57 cruft_expiration);
58
59 strvec_push(&cmd.args, "--non-empty");
60
61 cmd.in = -1;
62
63 ret = start_command(&cmd);
64 if (ret)
65 return ret;
66
67 /*
68 * names has a confusing double use: it both provides the list
69 * of just-written new packs, and accepts the name of the cruft
70 * pack we are writing.
71 *
72 * By the time it is read here, it contains only the pack(s)
73 * that were just written, which is exactly the set of packs we
74 * want to consider kept.
75 *
76 * If `--expire-to` is given, the double-use served by `names`
77 * ensures that the pack written to `--expire-to` excludes any
78 * objects contained in the cruft pack.
79 */
80 in = xfdopen(cmd.in, "w");
81 for_each_string_list_item(item, names)
82 fprintf(in, "%s-%s.pack\n", pack_prefix, item->string);
83 if (combine_cruft_below_size && !cruft_expiration) {
84 combine_small_cruft_packs(in, combine_cruft_below_size,
85 existing);
86 } else {
87 for_each_string_list_item(item, &existing->non_kept_packs)
88 fprintf(in, "-%s.pack\n", item->string);
89 for_each_string_list_item(item, &existing->cruft_packs)
90 fprintf(in, "-%s.pack\n", item->string);
91 }
92 for_each_string_list_item(item, &existing->kept_packs)
93 fprintf(in, "%s.pack\n", item->string);
94 fclose(in);
95
96 return finish_pack_objects_cmd(existing->repo->hash_algo, opts, &cmd,
97 names);
98 }