repack: refactor finishing pack-objects command

Create a new finish_pack_objects_cmd() to refactor duplicated code that handles reading the packfile names from the output of a `git pack-objects` command and putting it into a string_list, as well as calling finish_command(). While at it, beautify a code comment a bit in the new function. Signed-off-by: Christian Couder <chriscool@tuxfamily.org Signed-off-by: Junio C Hamano <gitster@pobox.com>

Christian Couder committed Oct 2, 2023 at 18:54 UTC ff8504e4ec5628edddb67f989256b7f2aea6c45d
1 file changed +33 -37
builtin/repack.c
+33 -37
@@ -806,6 +806,36 @@ static void remove_redundant_bitmaps(struct string_list *include,
806 strbuf_release(&path);
807 }
808
809 +static int finish_pack_objects_cmd(struct child_process *cmd,
810 + struct string_list *names,
811 + int local)
812 +{
813 + FILE *out;
814 + struct strbuf line = STRBUF_INIT;
815 +
816 + out = xfdopen(cmd->out, "r");
817 + while (strbuf_getline_lf(&line, out) != EOF) {
818 + struct string_list_item *item;
819 +
820 + if (line.len != the_hash_algo->hexsz)
821 + die(_("repack: Expecting full hex object ID lines only "
822 + "from pack-objects."));
823 + /*
824 + * Avoid putting packs written outside of the repository in the
825 + * list of names.
826 + */
827 + if (local) {
828 + item = string_list_append(names, line.buf);
829 + item->util = populate_pack_exts(line.buf);
830 + }
831 + }
832 + fclose(out);
833 +
834 + strbuf_release(&line);
835 +
836 + return finish_command(cmd);
837 +}
838 +
839 static int write_cruft_pack(const struct pack_objects_args *args,
840 const char *destination,
841 const char *pack_prefix,
@@ -814,9 +844,8 @@ static int write_cruft_pack(const struct pack_objects_args *args,
844 struct existing_packs *existing)
845 {
846 struct child_process cmd = CHILD_PROCESS_INIT;
817 - struct strbuf line = STRBUF_INIT;
847 struct string_list_item *item;
819 - FILE *in, *out;
848 + FILE *in;
849 int ret;
850 const char *scratch;
851 int local = skip_prefix(destination, packdir, &scratch);
@@ -861,27 +890,7 @@ static int write_cruft_pack(const struct pack_objects_args *args,
890 fprintf(in, "%s.pack\n", item->string);
891 fclose(in);
892
864 - out = xfdopen(cmd.out, "r");
865 - while (strbuf_getline_lf(&line, out) != EOF) {
866 - struct string_list_item *item;
867 -
868 - if (line.len != the_hash_algo->hexsz)
869 - die(_("repack: Expecting full hex object ID lines only "
870 - "from pack-objects."));
871 - /*
872 - * avoid putting packs written outside of the repository in the
873 - * list of names
874 - */
875 - if (local) {
876 - item = string_list_append(names, line.buf);
877 - item->util = populate_pack_exts(line.buf);
878 - }
879 - }
880 - fclose(out);
881 -
882 - strbuf_release(&line);
883 -
884 - return finish_command(&cmd);
893 + return finish_pack_objects_cmd(&cmd, names, local);
894 }
895
896 int cmd_repack(int argc, const char **argv, const char *prefix)
@@ -891,10 +900,8 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
900 struct string_list names = STRING_LIST_INIT_DUP;
901 struct existing_packs existing = EXISTING_PACKS_INIT;
902 struct pack_geometry geometry = { 0 };
894 - struct strbuf line = STRBUF_INIT;
903 struct tempfile *refs_snapshot = NULL;
904 int i, ext, ret;
897 - FILE *out;
905 int show_progress;
906
907 /* variables to be filled by option parsing */
@@ -1124,18 +1131,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
1131 fclose(in);
1132 }
1133
1127 - out = xfdopen(cmd.out, "r");
1128 - while (strbuf_getline_lf(&line, out) != EOF) {
1129 - struct string_list_item *item;
1130 -
1131 - if (line.len != the_hash_algo->hexsz)
1132 - die(_("repack: Expecting full hex object ID lines only from pack-objects."));
1133 - item = string_list_append(&names, line.buf);
1134 - item->util = populate_pack_exts(item->string);
1135 - }
1136 - strbuf_release(&line);
1137 - fclose(out);
1138 - ret = finish_command(&cmd);
1134 + ret = finish_pack_objects_cmd(&cmd, &names, 1);
1135 if (ret)
1136 goto cleanup;
1137