Bug Summary

File:builtin/repack.c
Location:line 202, column 2
Description:Value stored to 'argc' is never read

Annotated Source Code

1#include "builtin.h"
2#include "cache.h"
3#include "dir.h"
4#include "parse-options.h"
5#include "run-command.h"
6#include "sigchain.h"
7#include "strbuf.h"
8#include "string-list.h"
9#include "argv-array.h"
10
11static int delta_base_offset = 1;
12static int pack_kept_objects = -1;
13static int write_bitmaps;
14static char *packdir, *packtmp;
15
16static const char *const git_repack_usage[] = {
17 N_("git repack [<options>]")("git repack [<options>]"),
18 NULL((void*)0)
19};
20
21static const char incremental_bitmap_conflict_error[] = N_(("Incremental repacks are incompatible with bitmap indexes. Use\n"
"--no-write-bitmap-index or disable the pack.writebitmaps configuration."
)
22"Incremental repacks are incompatible with bitmap indexes. Use\n"("Incremental repacks are incompatible with bitmap indexes. Use\n"
"--no-write-bitmap-index or disable the pack.writebitmaps configuration."
)
23"--no-write-bitmap-index or disable the pack.writebitmaps configuration."("Incremental repacks are incompatible with bitmap indexes. Use\n"
"--no-write-bitmap-index or disable the pack.writebitmaps configuration."
)
24)("Incremental repacks are incompatible with bitmap indexes. Use\n"
"--no-write-bitmap-index or disable the pack.writebitmaps configuration."
)
;
25
26
27static int repack_config(const char *var, const char *value, void *cb)
28{
29 if (!strcmp(var, "repack.usedeltabaseoffset")) {
30 delta_base_offset = git_config_bool(var, value);
31 return 0;
32 }
33 if (!strcmp(var, "repack.packkeptobjects")) {
34 pack_kept_objects = git_config_bool(var, value);
35 return 0;
36 }
37 if (!strcmp(var, "repack.writebitmaps") ||
38 !strcmp(var, "pack.writebitmaps")) {
39 write_bitmaps = git_config_bool(var, value);
40 return 0;
41 }
42 return git_default_config(var, value, cb);
43}
44
45/*
46 * Remove temporary $GIT_OBJECT_DIRECTORY/pack/.tmp-$$-pack-* files.
47 */
48static void remove_temporary_files(void)
49{
50 struct strbuf buf = STRBUF_INIT{ 0, 0, strbuf_slopbuf };
51 size_t dirlen, prefixlen;
52 DIRPREC_DIR *dir;
53 struct direntdirent_prec_psx *e;
54
55 dir = opendir(packdir)precompose_utf8_opendir(packdir);
56 if (!dir)
57 return;
58
59 /* Point at the slash at the end of ".../objects/pack/" */
60 dirlen = strlen(packdir) + 1;
61 strbuf_addstr(&buf, packtmp);
62 /* Hold the length of ".tmp-%d-pack-" */
63 prefixlen = buf.len - dirlen;
64
65 while ((e = readdir(dir)precompose_utf8_readdir(dir))) {
66 if (strncmp(e->d_name, buf.buf + dirlen, prefixlen))
67 continue;
68 strbuf_setlen(&buf, dirlen);
69 strbuf_addstr(&buf, e->d_name);
70 unlink(buf.buf);
71 }
72 closedir(dir)precompose_utf8_closedir(dir);
73 strbuf_release(&buf);
74}
75
76static void remove_pack_on_signal(int signo)
77{
78 remove_temporary_files();
79 sigchain_pop(signo);
80 raise(signo);
81}
82
83/*
84 * Adds all packs hex strings to the fname list, which do not
85 * have a corresponding .keep file.
86 */
87static void get_non_kept_pack_filenames(struct string_list *fname_list)
88{
89 DIRPREC_DIR *dir;
90 struct direntdirent_prec_psx *e;
91 char *fname;
92
93 if (!(dir = opendir(packdir)precompose_utf8_opendir(packdir)))
94 return;
95
96 while ((e = readdir(dir)precompose_utf8_readdir(dir)) != NULL((void*)0)) {
97 size_t len;
98 if (!strip_suffix(e->d_name, ".pack", &len))
99 continue;
100
101 fname = xmemdupz(e->d_name, len);
102
103 if (!file_exists(mkpath("%s/%s.keep", packdir, fname)))
104 string_list_append_nodup(fname_list, fname);
105 else
106 free(fname);
107 }
108 closedir(dir)precompose_utf8_closedir(dir);
109}
110
111static void remove_redundant_pack(const char *dir_name, const char *base_name)
112{
113 const char *exts[] = {".pack", ".idx", ".keep", ".bitmap"};
114 int i;
115 struct strbuf buf = STRBUF_INIT{ 0, 0, strbuf_slopbuf };
116 size_t plen;
117
118 strbuf_addf(&buf, "%s/%s", dir_name, base_name);
119 plen = buf.len;
120
121 for (i = 0; i < ARRAY_SIZE(exts)(sizeof(exts) / sizeof((exts)[0]) + (sizeof(char [1 - 2*!(!__builtin_types_compatible_p
(__typeof__(exts), __typeof__(&(exts)[0])))]) - 1))
; i++) {
122 strbuf_setlen(&buf, plen);
123 strbuf_addstr(&buf, exts[i]);
124 unlink(buf.buf);
125 }
126 strbuf_release(&buf);
127}
128
129#define ALL_INTO_ONE1 1
130#define LOOSEN_UNREACHABLE2 2
131
132int cmd_repack(int argc, const char **argv, const char *prefix)
133{
134 struct {
135 const char *name;
136 unsigned optional:1;
137 } exts[] = {
138 {".pack"},
139 {".idx"},
140 {".bitmap", 1},
141 };
142 struct child_process cmd = CHILD_PROCESS_INIT{ ((void*)0), { empty_argv, 0, 0 }, { empty_argv, 0, 0 } };
143 struct string_list_item *item;
144 struct string_list names = STRING_LIST_INIT_DUP{ ((void*)0), 0, 0, 1, ((void*)0) };
145 struct string_list rollback = STRING_LIST_INIT_NODUP{ ((void*)0), 0, 0, 0, ((void*)0) };
146 struct string_list existing_packs = STRING_LIST_INIT_DUP{ ((void*)0), 0, 0, 1, ((void*)0) };
147 struct strbuf line = STRBUF_INIT{ 0, 0, strbuf_slopbuf };
148 int ext, ret, failed;
149 FILE *out;
150
151 /* variables to be filled by option parsing */
152 int pack_everything = 0;
153 int delete_redundant = 0;
154 const char *unpack_unreachable = NULL((void*)0);
155 int keep_unreachable = 0;
156 const char *window = NULL((void*)0), *window_memory = NULL((void*)0);
157 const char *depth = NULL((void*)0);
158 const char *max_pack_size = NULL((void*)0);
159 int no_reuse_delta = 0, no_reuse_object = 0;
160 int no_update_server_info = 0;
161 int quiet = 0;
162 int local = 0;
163
164 struct option builtin_repack_options[] = {
165 OPT_BIT('a', NULL, &pack_everything,{ OPTION_BIT, ('a'), (((void*)0)), (&pack_everything), ((
void*)0), (("pack everything in a single pack")), PARSE_OPT_NOARG
, ((void*)0), (1) }
166 N_("pack everything in a single pack"), ALL_INTO_ONE){ OPTION_BIT, ('a'), (((void*)0)), (&pack_everything), ((
void*)0), (("pack everything in a single pack")), PARSE_OPT_NOARG
, ((void*)0), (1) }
,
167 OPT_BIT('A', NULL, &pack_everything,{ OPTION_BIT, ('A'), (((void*)0)), (&pack_everything), ((
void*)0), (("same as -a, and turn unreachable objects loose")
), PARSE_OPT_NOARG, ((void*)0), (2 | 1) }
168 N_("same as -a, and turn unreachable objects loose"),{ OPTION_BIT, ('A'), (((void*)0)), (&pack_everything), ((
void*)0), (("same as -a, and turn unreachable objects loose")
), PARSE_OPT_NOARG, ((void*)0), (2 | 1) }
169 LOOSEN_UNREACHABLE | ALL_INTO_ONE){ OPTION_BIT, ('A'), (((void*)0)), (&pack_everything), ((
void*)0), (("same as -a, and turn unreachable objects loose")
), PARSE_OPT_NOARG, ((void*)0), (2 | 1) }
,
170 OPT_BOOL('d', NULL, &delete_redundant,{ OPTION_SET_INT, ('d'), (((void*)0)), (&delete_redundant
), ((void*)0), (("remove redundant packs, and run git-prune-packed"
)), PARSE_OPT_NOARG, ((void*)0), (1) }
171 N_("remove redundant packs, and run git-prune-packed")){ OPTION_SET_INT, ('d'), (((void*)0)), (&delete_redundant
), ((void*)0), (("remove redundant packs, and run git-prune-packed"
)), PARSE_OPT_NOARG, ((void*)0), (1) }
,
172 OPT_BOOL('f', NULL, &no_reuse_delta,{ OPTION_SET_INT, ('f'), (((void*)0)), (&no_reuse_delta),
((void*)0), (("pass --no-reuse-delta to git-pack-objects")),
PARSE_OPT_NOARG, ((void*)0), (1) }
173 N_("pass --no-reuse-delta to git-pack-objects")){ OPTION_SET_INT, ('f'), (((void*)0)), (&no_reuse_delta),
((void*)0), (("pass --no-reuse-delta to git-pack-objects")),
PARSE_OPT_NOARG, ((void*)0), (1) }
,
174 OPT_BOOL('F', NULL, &no_reuse_object,{ OPTION_SET_INT, ('F'), (((void*)0)), (&no_reuse_object)
, ((void*)0), (("pass --no-reuse-object to git-pack-objects")
), PARSE_OPT_NOARG, ((void*)0), (1) }
175 N_("pass --no-reuse-object to git-pack-objects")){ OPTION_SET_INT, ('F'), (((void*)0)), (&no_reuse_object)
, ((void*)0), (("pass --no-reuse-object to git-pack-objects")
), PARSE_OPT_NOARG, ((void*)0), (1) }
,
176 OPT_BOOL('n', NULL, &no_update_server_info,{ OPTION_SET_INT, ('n'), (((void*)0)), (&no_update_server_info
), ((void*)0), (("do not run git-update-server-info")), PARSE_OPT_NOARG
, ((void*)0), (1) }
177 N_("do not run git-update-server-info")){ OPTION_SET_INT, ('n'), (((void*)0)), (&no_update_server_info
), ((void*)0), (("do not run git-update-server-info")), PARSE_OPT_NOARG
, ((void*)0), (1) }
,
178 OPT__QUIET(&quiet, N_("be quiet")){ OPTION_COUNTUP, ('q'), ("quiet"), ((&quiet)), ((void*)0
), ((("be quiet"))), PARSE_OPT_NOARG }
,
179 OPT_BOOL('l', "local", &local,{ OPTION_SET_INT, ('l'), ("local"), (&local), ((void*)0),
(("pass --local to git-pack-objects")), PARSE_OPT_NOARG, ((void
*)0), (1) }
180 N_("pass --local to git-pack-objects")){ OPTION_SET_INT, ('l'), ("local"), (&local), ((void*)0),
(("pass --local to git-pack-objects")), PARSE_OPT_NOARG, ((void
*)0), (1) }
,
181 OPT_BOOL('b', "write-bitmap-index", &write_bitmaps,{ OPTION_SET_INT, ('b'), ("write-bitmap-index"), (&write_bitmaps
), ((void*)0), (("write bitmap index")), PARSE_OPT_NOARG, ((void
*)0), (1) }
182 N_("write bitmap index")){ OPTION_SET_INT, ('b'), ("write-bitmap-index"), (&write_bitmaps
), ((void*)0), (("write bitmap index")), PARSE_OPT_NOARG, ((void
*)0), (1) }
,
183 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"),{ OPTION_STRING, (0), ("unpack-unreachable"), (&unpack_unreachable
), (("approxidate")), (("with -A, do not loosen objects older than this"
)) }
184 N_("with -A, do not loosen objects older than this")){ OPTION_STRING, (0), ("unpack-unreachable"), (&unpack_unreachable
), (("approxidate")), (("with -A, do not loosen objects older than this"
)) }
,
185 OPT_BOOL('k', "keep-unreachable", &keep_unreachable,{ OPTION_SET_INT, ('k'), ("keep-unreachable"), (&keep_unreachable
), ((void*)0), (("with -a, repack unreachable objects")), PARSE_OPT_NOARG
, ((void*)0), (1) }
186 N_("with -a, repack unreachable objects")){ OPTION_SET_INT, ('k'), ("keep-unreachable"), (&keep_unreachable
), ((void*)0), (("with -a, repack unreachable objects")), PARSE_OPT_NOARG
, ((void*)0), (1) }
,
187 OPT_STRING(0, "window", &window, N_("n"),{ OPTION_STRING, (0), ("window"), (&window), (("n")), (("size of the window used for delta compression"
)) }
188 N_("size of the window used for delta compression")){ OPTION_STRING, (0), ("window"), (&window), (("n")), (("size of the window used for delta compression"
)) }
,
189 OPT_STRING(0, "window-memory", &window_memory, N_("bytes"),{ OPTION_STRING, (0), ("window-memory"), (&window_memory)
, (("bytes")), (("same as the above, but limit memory size instead of entries count"
)) }
190 N_("same as the above, but limit memory size instead of entries count")){ OPTION_STRING, (0), ("window-memory"), (&window_memory)
, (("bytes")), (("same as the above, but limit memory size instead of entries count"
)) }
,
191 OPT_STRING(0, "depth", &depth, N_("n"),{ OPTION_STRING, (0), ("depth"), (&depth), (("n")), (("limits the maximum delta depth"
)) }
192 N_("limits the maximum delta depth")){ OPTION_STRING, (0), ("depth"), (&depth), (("n")), (("limits the maximum delta depth"
)) }
,
193 OPT_STRING(0, "max-pack-size", &max_pack_size, N_("bytes"),{ OPTION_STRING, (0), ("max-pack-size"), (&max_pack_size)
, (("bytes")), (("maximum size of each packfile")) }
194 N_("maximum size of each packfile")){ OPTION_STRING, (0), ("max-pack-size"), (&max_pack_size)
, (("bytes")), (("maximum size of each packfile")) }
,
195 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects,{ OPTION_SET_INT, (0), ("pack-kept-objects"), (&pack_kept_objects
), ((void*)0), (("repack objects in packs marked with .keep")
), PARSE_OPT_NOARG, ((void*)0), (1) }
196 N_("repack objects in packs marked with .keep")){ OPTION_SET_INT, (0), ("pack-kept-objects"), (&pack_kept_objects
), ((void*)0), (("repack objects in packs marked with .keep")
), PARSE_OPT_NOARG, ((void*)0), (1) }
,
197 OPT_END(){ OPTION_END }
198 };
199
200 git_config(repack_config, NULL((void*)0));
201
202 argc = parse_options(argc, argv, prefix, builtin_repack_options,
Value stored to 'argc' is never read
203 git_repack_usage, 0);
204
205 if (delete_redundant && repository_format_precious_objects)
206 die(_("cannot delete packs in a precious-objects repo"));
207
208 if (keep_unreachable &&
209 (unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE2)))
210 die(_("--keep-unreachable and -A are incompatible"));
211
212 if (pack_kept_objects < 0)
213 pack_kept_objects = write_bitmaps;
214
215 if (write_bitmaps && !(pack_everything & ALL_INTO_ONE1))
216 die(_(incremental_bitmap_conflict_error));
217
218 packdir = mkpathdup("%s/pack", get_object_directory());
219 packtmp = mkpathdup("%s/.tmp-%d-pack", packdir, (int)getpid());
220
221 sigchain_push_common(remove_pack_on_signal);
222
223 argv_array_push(&cmd.args, "pack-objects");
224 argv_array_push(&cmd.args, "--keep-true-parents");
225 if (!pack_kept_objects)
226 argv_array_push(&cmd.args, "--honor-pack-keep");
227 argv_array_push(&cmd.args, "--non-empty");
228 argv_array_push(&cmd.args, "--all");
229 argv_array_push(&cmd.args, "--reflog");
230 argv_array_push(&cmd.args, "--indexed-objects");
231 if (window)
232 argv_array_pushf(&cmd.args, "--window=%s", window);
233 if (window_memory)
234 argv_array_pushf(&cmd.args, "--window-memory=%s", window_memory);
235 if (depth)
236 argv_array_pushf(&cmd.args, "--depth=%s", depth);
237 if (max_pack_size)
238 argv_array_pushf(&cmd.args, "--max-pack-size=%s", max_pack_size);
239 if (no_reuse_delta)
240 argv_array_pushf(&cmd.args, "--no-reuse-delta");
241 if (no_reuse_object)
242 argv_array_pushf(&cmd.args, "--no-reuse-object");
243 if (write_bitmaps)
244 argv_array_push(&cmd.args, "--write-bitmap-index");
245
246 if (pack_everything & ALL_INTO_ONE1) {
247 get_non_kept_pack_filenames(&existing_packs);
248
249 if (existing_packs.nr && delete_redundant) {
250 if (unpack_unreachable) {
251 argv_array_pushf(&cmd.args,
252 "--unpack-unreachable=%s",
253 unpack_unreachable);
254 argv_array_push(&cmd.env_array, "GIT_REF_PARANOIA=1");
255 } else if (pack_everything & LOOSEN_UNREACHABLE2) {
256 argv_array_push(&cmd.args,
257 "--unpack-unreachable");
258 } else if (keep_unreachable) {
259 argv_array_push(&cmd.args, "--keep-unreachable");
260 argv_array_push(&cmd.args, "--pack-loose-unreachable");
261 } else {
262 argv_array_push(&cmd.env_array, "GIT_REF_PARANOIA=1");
263 }
264 }
265 } else {
266 argv_array_push(&cmd.args, "--unpacked");
267 argv_array_push(&cmd.args, "--incremental");
268 }
269
270 if (local)
271 argv_array_push(&cmd.args, "--local");
272 if (quiet)
273 argv_array_push(&cmd.args, "--quiet");
274 if (delta_base_offset)
275 argv_array_push(&cmd.args, "--delta-base-offset");
276
277 argv_array_push(&cmd.args, packtmp);
278
279 cmd.git_cmd = 1;
280 cmd.out = -1;
281 cmd.no_stdin = 1;
282
283 ret = start_command(&cmd);
284 if (ret)
285 return ret;
286
287 out = xfdopen(cmd.out, "r");
288 while (strbuf_getline_lf(&line, out) != EOF(-1)) {
289 if (line.len != 40)
290 die("repack: Expecting 40 character sha1 lines only from pack-objects.");
291 string_list_append(&names, line.buf);
292 }
293 fclose(out);
294 ret = finish_command(&cmd);
295 if (ret)
296 return ret;
297
298 if (!names.nr && !quiet)
299 printf("Nothing new to pack.\n");
300
301 /*
302 * Ok we have prepared all new packfiles.
303 * First see if there are packs of the same name and if so
304 * if we can move them out of the way (this can happen if we
305 * repacked immediately after packing fully.
306 */
307 failed = 0;
308 for_each_string_list_item(item, &names)for (item = (&names)->items; item < (&names)->
items + (&names)->nr; ++item)
{
309 for (ext = 0; ext < ARRAY_SIZE(exts)(sizeof(exts) / sizeof((exts)[0]) + (sizeof(char [1 - 2*!(!__builtin_types_compatible_p
(__typeof__(exts), __typeof__(&(exts)[0])))]) - 1))
; ext++) {
310 char *fname, *fname_old;
311 fname = mkpathdup("%s/pack-%s%s", packdir,
312 item->string, exts[ext].name);
313 if (!file_exists(fname)) {
314 free(fname);
315 continue;
316 }
317
318 fname_old = mkpathdup("%s/old-%s%s", packdir,
319 item->string, exts[ext].name);
320 if (file_exists(fname_old))
321 if (unlink(fname_old))
322 failed = 1;
323
324 if (!failed && rename(fname, fname_old)) {
325 free(fname);
326 free(fname_old);
327 failed = 1;
328 break;
329 } else {
330 string_list_append(&rollback, fname);
331 free(fname_old);
332 }
333 }
334 if (failed)
335 break;
336 }
337 if (failed) {
338 struct string_list rollback_failure = STRING_LIST_INIT_DUP{ ((void*)0), 0, 0, 1, ((void*)0) };
339 for_each_string_list_item(item, &rollback)for (item = (&rollback)->items; item < (&rollback
)->items + (&rollback)->nr; ++item)
{
340 char *fname, *fname_old;
341 fname = mkpathdup("%s/%s", packdir, item->string);
342 fname_old = mkpathdup("%s/old-%s", packdir, item->string);
343 if (rename(fname_old, fname))
344 string_list_append(&rollback_failure, fname);
345 free(fname);
346 free(fname_old);
347 }
348
349 if (rollback_failure.nr) {
350 int i;
351 fprintf(stderr__stderrp,
352 "WARNING: Some packs in use have been renamed by\n"
353 "WARNING: prefixing old- to their name, in order to\n"
354 "WARNING: replace them with the new version of the\n"
355 "WARNING: file. But the operation failed, and the\n"
356 "WARNING: attempt to rename them back to their\n"
357 "WARNING: original names also failed.\n"
358 "WARNING: Please rename them in %s manually:\n", packdir);
359 for (i = 0; i < rollback_failure.nr; i++)
360 fprintf(stderr__stderrp, "WARNING: old-%s -> %s\n",
361 rollback_failure.items[i].string,
362 rollback_failure.items[i].string);
363 }
364 exit(1);
365 }
366
367 /* Now the ones with the same name are out of the way... */
368 for_each_string_list_item(item, &names)for (item = (&names)->items; item < (&names)->
items + (&names)->nr; ++item)
{
369 for (ext = 0; ext < ARRAY_SIZE(exts)(sizeof(exts) / sizeof((exts)[0]) + (sizeof(char [1 - 2*!(!__builtin_types_compatible_p
(__typeof__(exts), __typeof__(&(exts)[0])))]) - 1))
; ext++) {
370 char *fname, *fname_old;
371 struct stat statbuffer;
372 int exists = 0;
373 fname = mkpathdup("%s/pack-%s%s",
374 packdir, item->string, exts[ext].name);
375 fname_old = mkpathdup("%s-%s%s",
376 packtmp, item->string, exts[ext].name);
377 if (!stat(fname_old, &statbuffer)) {
378 statbuffer.st_mode &= ~(S_IWUSR0000200 | S_IWGRP0000020 | S_IWOTH0000002);
379 chmod(fname_old, statbuffer.st_mode);
380 exists = 1;
381 }
382 if (exists || !exts[ext].optional) {
383 if (rename(fname_old, fname))
384 die_errno(_("renaming '%s' failed"), fname_old);
385 }
386 free(fname);
387 free(fname_old);
388 }
389 }
390
391 /* Remove the "old-" files */
392 for_each_string_list_item(item, &names)for (item = (&names)->items; item < (&names)->
items + (&names)->nr; ++item)
{
393 for (ext = 0; ext < ARRAY_SIZE(exts)(sizeof(exts) / sizeof((exts)[0]) + (sizeof(char [1 - 2*!(!__builtin_types_compatible_p
(__typeof__(exts), __typeof__(&(exts)[0])))]) - 1))
; ext++) {
394 char *fname;
395 fname = mkpathdup("%s/old-%s%s",
396 packdir,
397 item->string,
398 exts[ext].name);
399 if (remove_path(fname))
400 warning(_("failed to remove '%s'"), fname);
401 free(fname);
402 }
403 }
404
405 /* End of pack replacement. */
406
407 if (delete_redundant) {
408 int opts = 0;
409 string_list_sort(&names);
410 for_each_string_list_item(item, &existing_packs)for (item = (&existing_packs)->items; item < (&
existing_packs)->items + (&existing_packs)->nr; ++item
)
{
411 char *sha1;
412 size_t len = strlen(item->string);
413 if (len < 40)
414 continue;
415 sha1 = item->string + len - 40;
416 if (!string_list_has_string(&names, sha1))
417 remove_redundant_pack(packdir, item->string);
418 }
419 if (!quiet && isatty(2))
420 opts |= PRUNE_PACKED_VERBOSE02;
421 prune_packed_objects(opts);
422 }
423
424 if (!no_update_server_info)
425 update_server_info(0);
426 remove_temporary_files();
427 string_list_clear(&names, 0);
428 string_list_clear(&rollback, 0);
429 string_list_clear(&existing_packs, 0);
430 strbuf_release(&line);
431
432 return 0;
433}