builtin/repack: replace hard-coded constants

Note that while the error messages here are not translated, the end user should never see them. We invoke git pack-objects shortly before both invocations, so we can be fairly certain that the data we're receiving is in fact valid. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed Oct 15, 2018 at 00:01 UTC 2f0c9e9a9bbe8015da2c0838f3051c0a336dee26
1 file changed +7 -6
builtin/repack.c
+7 -6
@@ -235,8 +235,8 @@ static void repack_promisor_objects(const struct pack_objects_args *args,
235 while (strbuf_getline_lf(&line, out) != EOF) {
236 char *promisor_name;
237 int fd;
238 - if (line.len != 40)
239 - die("repack: Expecting 40 character sha1 lines only from pack-objects.");
238 + if (line.len != the_hash_algo->hexsz)
239 + die("repack: Expecting full hex object ID lines only from pack-objects.");
240 string_list_append(names, line.buf);
241
242 /*
@@ -407,8 +407,8 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
407
408 out = xfdopen(cmd.out, "r");
409 while (strbuf_getline_lf(&line, out) != EOF) {
410 - if (line.len != 40)
411 - die("repack: Expecting 40 character sha1 lines only from pack-objects.");
410 + if (line.len != the_hash_algo->hexsz)
411 + die("repack: Expecting full hex object ID lines only from pack-objects.");
412 string_list_append(&names, line.buf);
413 }
414 fclose(out);
@@ -535,14 +535,15 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
535 reprepare_packed_git(the_repository);
536
537 if (delete_redundant) {
538 + const int hexsz = the_hash_algo->hexsz;
539 int opts = 0;
540 string_list_sort(&names);
541 for_each_string_list_item(item, &existing_packs) {
542 char *sha1;
543 size_t len = strlen(item->string);
543 - if (len < 40)
544 + if (len < hexsz)
545 continue;
545 - sha1 = item->string + len - 40;
546 + sha1 = item->string + len - hexsz;
547 if (!string_list_has_string(&names, sha1))
548 remove_redundant_pack(packdir, item->string);
549 }