repack: refactor setup of pack-objects cmd

A subsequent patch will teach repack to run pack-objects with some same and some different arguments if repacking of promisor objects is required. Refactor the setup of the pack-objects cmd so that setting up the arguments common to both is done in a function. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Tan committed Aug 8, 2018 at 15:34 UTC 2b958e790b9c6f74442b94bfc0e297dcbba4db82
1 file changed +55 -44
builtin/repack.c
+55 -44
@@ -138,6 +138,47 @@ static void remove_redundant_pack(const char *dir_name, const char *base_name)
138 strbuf_release(&buf);
139 }
140
141 +struct pack_objects_args {
142 + const char *window;
143 + const char *window_memory;
144 + const char *depth;
145 + const char *threads;
146 + const char *max_pack_size;
147 + int no_reuse_delta;
148 + int no_reuse_object;
149 + int quiet;
150 + int local;
151 +};
152 +
153 +static void prepare_pack_objects(struct child_process *cmd,
154 + const struct pack_objects_args *args)
155 +{
156 + argv_array_push(&cmd->args, "pack-objects");
157 + if (args->window)
158 + argv_array_pushf(&cmd->args, "--window=%s", args->window);
159 + if (args->window_memory)
160 + argv_array_pushf(&cmd->args, "--window-memory=%s", args->window_memory);
161 + if (args->depth)
162 + argv_array_pushf(&cmd->args, "--depth=%s", args->depth);
163 + if (args->threads)
164 + argv_array_pushf(&cmd->args, "--threads=%s", args->threads);
165 + if (args->max_pack_size)
166 + argv_array_pushf(&cmd->args, "--max-pack-size=%s", args->max_pack_size);
167 + if (args->no_reuse_delta)
168 + argv_array_pushf(&cmd->args, "--no-reuse-delta");
169 + if (args->no_reuse_object)
170 + argv_array_pushf(&cmd->args, "--no-reuse-object");
171 + if (args->local)
172 + argv_array_push(&cmd->args, "--local");
173 + if (args->quiet)
174 + argv_array_push(&cmd->args, "--quiet");
175 + if (delta_base_offset)
176 + argv_array_push(&cmd->args, "--delta-base-offset");
177 + argv_array_push(&cmd->args, packtmp);
178 + cmd->git_cmd = 1;
179 + cmd->out = -1;
180 +}
181 +
182 #define ALL_INTO_ONE 1
183 #define LOOSEN_UNREACHABLE 2
184
@@ -165,15 +206,9 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
206 int delete_redundant = 0;
207 const char *unpack_unreachable = NULL;
208 int keep_unreachable = 0;
168 - const char *window = NULL, *window_memory = NULL;
169 - const char *depth = NULL;
170 - const char *threads = NULL;
171 - const char *max_pack_size = NULL;
209 struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
173 - int no_reuse_delta = 0, no_reuse_object = 0;
210 int no_update_server_info = 0;
175 - int quiet = 0;
176 - int local = 0;
211 + struct pack_objects_args po_args = {NULL};
212
213 struct option builtin_repack_options[] = {
214 OPT_BIT('a', NULL, &pack_everything,
@@ -183,14 +218,14 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
218 LOOSEN_UNREACHABLE | ALL_INTO_ONE),
219 OPT_BOOL('d', NULL, &delete_redundant,
220 N_("remove redundant packs, and run git-prune-packed")),
186 - OPT_BOOL('f', NULL, &no_reuse_delta,
221 + OPT_BOOL('f', NULL, &po_args.no_reuse_delta,
222 N_("pass --no-reuse-delta to git-pack-objects")),
188 - OPT_BOOL('F', NULL, &no_reuse_object,
223 + OPT_BOOL('F', NULL, &po_args.no_reuse_object,
224 N_("pass --no-reuse-object to git-pack-objects")),
225 OPT_BOOL('n', NULL, &no_update_server_info,
226 N_("do not run git-update-server-info")),
192 - OPT__QUIET(&quiet, N_("be quiet")),
193 - OPT_BOOL('l', "local", &local,
227 + OPT__QUIET(&po_args.quiet, N_("be quiet")),
228 + OPT_BOOL('l', "local", &po_args.local,
229 N_("pass --local to git-pack-objects")),
230 OPT_BOOL('b', "write-bitmap-index", &write_bitmaps,
231 N_("write bitmap index")),
@@ -198,15 +233,15 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
233 N_("with -A, do not loosen objects older than this")),
234 OPT_BOOL('k', "keep-unreachable", &keep_unreachable,
235 N_("with -a, repack unreachable objects")),
201 - OPT_STRING(0, "window", &window, N_("n"),
236 + OPT_STRING(0, "window", &po_args.window, N_("n"),
237 N_("size of the window used for delta compression")),
203 - OPT_STRING(0, "window-memory", &window_memory, N_("bytes"),
238 + OPT_STRING(0, "window-memory", &po_args.window_memory, N_("bytes"),
239 N_("same as the above, but limit memory size instead of entries count")),
205 - OPT_STRING(0, "depth", &depth, N_("n"),
240 + OPT_STRING(0, "depth", &po_args.depth, N_("n"),
241 N_("limits the maximum delta depth")),
207 - OPT_STRING(0, "threads", &threads, N_("n"),
242 + OPT_STRING(0, "threads", &po_args.threads, N_("n"),
243 N_("limits the maximum number of threads")),
209 - OPT_STRING(0, "max-pack-size", &max_pack_size, N_("bytes"),
244 + OPT_STRING(0, "max-pack-size", &po_args.max_pack_size, N_("bytes"),
245 N_("maximum size of each packfile")),
246 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects,
247 N_("repack objects in packs marked with .keep")),
@@ -238,7 +273,8 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
273
274 sigchain_push_common(remove_pack_on_signal);
275
241 - argv_array_push(&cmd.args, "pack-objects");
276 + prepare_pack_objects(&cmd, &po_args);
277 +
278 argv_array_push(&cmd.args, "--keep-true-parents");
279 if (!pack_kept_objects)
280 argv_array_push(&cmd.args, "--honor-pack-keep");
@@ -251,20 +287,6 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
287 argv_array_push(&cmd.args, "--indexed-objects");
288 if (repository_format_partial_clone)
289 argv_array_push(&cmd.args, "--exclude-promisor-objects");
254 - if (window)
255 - argv_array_pushf(&cmd.args, "--window=%s", window);
256 - if (window_memory)
257 - argv_array_pushf(&cmd.args, "--window-memory=%s", window_memory);
258 - if (depth)
259 - argv_array_pushf(&cmd.args, "--depth=%s", depth);
260 - if (threads)
261 - argv_array_pushf(&cmd.args, "--threads=%s", threads);
262 - if (max_pack_size)
263 - argv_array_pushf(&cmd.args, "--max-pack-size=%s", max_pack_size);
264 - if (no_reuse_delta)
265 - argv_array_pushf(&cmd.args, "--no-reuse-delta");
266 - if (no_reuse_object)
267 - argv_array_pushf(&cmd.args, "--no-reuse-object");
290 if (write_bitmaps)
291 argv_array_push(&cmd.args, "--write-bitmap-index");
292
@@ -292,17 +314,6 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
314 argv_array_push(&cmd.args, "--incremental");
315 }
316
295 - if (local)
296 - argv_array_push(&cmd.args, "--local");
297 - if (quiet)
298 - argv_array_push(&cmd.args, "--quiet");
299 - if (delta_base_offset)
300 - argv_array_push(&cmd.args, "--delta-base-offset");
301 -
302 - argv_array_push(&cmd.args, packtmp);
303 -
304 - cmd.git_cmd = 1;
305 - cmd.out = -1;
317 cmd.no_stdin = 1;
318
319 ret = start_command(&cmd);
@@ -320,7 +331,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
331 if (ret)
332 return ret;
333
323 - if (!names.nr && !quiet)
334 + if (!names.nr && !po_args.quiet)
335 printf("Nothing new to pack.\n");
336
337 /*
@@ -441,7 +452,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
452 if (!string_list_has_string(&names, sha1))
453 remove_redundant_pack(packdir, item->string);
454 }
444 - if (!quiet && isatty(2))
455 + if (!po_args.quiet && isatty(2))
456 opts |= PRUNE_PACKED_VERBOSE;
457 prune_packed_objects(opts);
458 }