Skip to content

Commit ff8504e

Browse files
chriscoolgitster
authored andcommitted
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 <[email protected] Signed-off-by: Junio C Hamano <[email protected]>
1 parent 66589f8 commit ff8504e

File tree

1 file changed

+33
-37
lines changed

1 file changed

+33
-37
lines changed

builtin/repack.c

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,36 @@ static void remove_redundant_bitmaps(struct string_list *include,
806806
strbuf_release(&path);
807807
}
808808

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+
809839
static int write_cruft_pack(const struct pack_objects_args *args,
810840
const char *destination,
811841
const char *pack_prefix,
@@ -814,9 +844,8 @@ static int write_cruft_pack(const struct pack_objects_args *args,
814844
struct existing_packs *existing)
815845
{
816846
struct child_process cmd = CHILD_PROCESS_INIT;
817-
struct strbuf line = STRBUF_INIT;
818847
struct string_list_item *item;
819-
FILE *in, *out;
848+
FILE *in;
820849
int ret;
821850
const char *scratch;
822851
int local = skip_prefix(destination, packdir, &scratch);
@@ -861,27 +890,7 @@ static int write_cruft_pack(const struct pack_objects_args *args,
861890
fprintf(in, "%s.pack\n", item->string);
862891
fclose(in);
863892

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);
885894
}
886895

887896
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)
891900
struct string_list names = STRING_LIST_INIT_DUP;
892901
struct existing_packs existing = EXISTING_PACKS_INIT;
893902
struct pack_geometry geometry = { 0 };
894-
struct strbuf line = STRBUF_INIT;
895903
struct tempfile *refs_snapshot = NULL;
896904
int i, ext, ret;
897-
FILE *out;
898905
int show_progress;
899906

900907
/* variables to be filled by option parsing */
@@ -1124,18 +1131,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
11241131
fclose(in);
11251132
}
11261133

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);
11391135
if (ret)
11401136
goto cleanup;
11411137

0 commit comments

Comments
 (0)