repack: refactor to avoid double-negation of update-server-info

By default, git-repack(1) runs `update_server_info()` to generate info required for the dumb HTTP protocol. This can be disabled via the `-n` flag, which then sets the `no_update_server_info` flag. Further down the code this leads to some double-negation logic, which is about to become more confusing as we're about to add a new config which allows the user to permanently disable generation of the info. Refactor the code to avoid the double-negation and add some tests which verify that the flag continues to work as expected. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Mar 14, 2022 at 08:42 UTC 64a6151da7fa4b36eb2818047a9b76797e25b46e
2 files changed +36 -4
builtin/repack.c
+4 -4
@@ -620,7 +620,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
620 const char *unpack_unreachable = NULL;
621 int keep_unreachable = 0;
622 struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
623 - int no_update_server_info = 0;
623 + int run_update_server_info = 1;
624 struct pack_objects_args po_args = {NULL};
625 int geometric_factor = 0;
626 int write_midx = 0;
@@ -637,8 +637,8 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
637 N_("pass --no-reuse-delta to git-pack-objects")),
638 OPT_BOOL('F', NULL, &po_args.no_reuse_object,
639 N_("pass --no-reuse-object to git-pack-objects")),
640 - OPT_BOOL('n', NULL, &no_update_server_info,
641 - N_("do not run git-update-server-info")),
640 + OPT_NEGBIT('n', NULL, &run_update_server_info,
641 + N_("do not run git-update-server-info"), 1),
642 OPT__QUIET(&po_args.quiet, N_("be quiet")),
643 OPT_BOOL('l', "local", &po_args.local,
644 N_("pass --local to git-pack-objects")),
@@ -939,7 +939,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
939 prune_shallow(PRUNE_QUICK);
940 }
941
942 - if (!no_update_server_info)
942 + if (run_update_server_info)
943 update_server_info(0);
944 remove_temporary_files();
945
t/t7700-repack.sh
+32
@@ -385,4 +385,36 @@ test_expect_success TTY '--quiet disables progress' '
385 test_must_be_empty stderr
386 '
387
388 +test_expect_success 'setup for update-server-info' '
389 + git init update-server-info &&
390 + test_commit -C update-server-info message
391 +'
392 +
393 +test_server_info_present () {
394 + test_path_is_file update-server-info/.git/objects/info/packs &&
395 + test_path_is_file update-server-info/.git/info/refs
396 +}
397 +
398 +test_server_info_missing () {
399 + test_path_is_missing update-server-info/.git/objects/info/packs &&
400 + test_path_is_missing update-server-info/.git/info/refs
401 +}
402 +
403 +test_server_info_cleanup () {
404 + rm -f update-server-info/.git/objects/info/packs update-server-info/.git/info/refs &&
405 + test_server_info_missing
406 +}
407 +
408 +test_expect_success 'updates server info by default' '
409 + test_server_info_cleanup &&
410 + git -C update-server-info repack &&
411 + test_server_info_present
412 +'
413 +
414 +test_expect_success '-n skips updating server info' '
415 + test_server_info_cleanup &&
416 + git -C update-server-info repack -n &&
417 + test_server_info_missing
418 +'
419 +
420 test_done