Skip to content

Commit 31c42bf

Browse files
pcloudsgitster
authored andcommitted
receive-pack: reorder some code in unpack()
This is the preparation for adding --shallow-file to both unpack-objects and index-pack. To sum up: - struct argv_array used instead of const char ** - status/code, ip/child, unpacker/keeper are moved out to function top level - successful flow now ends at the end of the function Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 48d25ca commit 31c42bf

File tree

1 file changed

+30
-40
lines changed

1 file changed

+30
-40
lines changed

builtin/receive-pack.c

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "string-list.h"
1414
#include "sha1-array.h"
1515
#include "connected.h"
16+
#include "argv-array.h"
1617
#include "version.h"
1718

1819
static const char receive_pack_usage[] = "git receive-pack <git-dir>";
@@ -822,8 +823,11 @@ static const char *pack_lockfile;
822823
static const char *unpack(int err_fd)
823824
{
824825
struct pack_header hdr;
826+
struct argv_array av = ARGV_ARRAY_INIT;
825827
const char *hdr_err;
828+
int status;
826829
char hdr_arg[38];
830+
struct child_process child;
827831
int fsck_objects = (receive_fsck_objects >= 0
828832
? receive_fsck_objects
829833
: transfer_fsck_objects >= 0
@@ -840,63 +844,49 @@ static const char *unpack(int err_fd)
840844
"--pack_header=%"PRIu32",%"PRIu32,
841845
ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
842846

847+
memset(&child, 0, sizeof(child));
843848
if (ntohl(hdr.hdr_entries) < unpack_limit) {
844-
int code, i = 0;
845-
struct child_process child;
846-
const char *unpacker[5];
847-
unpacker[i++] = "unpack-objects";
849+
argv_array_pushl(&av, "unpack-objects", hdr_arg, NULL);
848850
if (quiet)
849-
unpacker[i++] = "-q";
851+
argv_array_push(&av, "-q");
850852
if (fsck_objects)
851-
unpacker[i++] = "--strict";
852-
unpacker[i++] = hdr_arg;
853-
unpacker[i++] = NULL;
854-
memset(&child, 0, sizeof(child));
855-
child.argv = unpacker;
853+
argv_array_push(&av, "--strict");
854+
child.argv = av.argv;
856855
child.no_stdout = 1;
857856
child.err = err_fd;
858857
child.git_cmd = 1;
859-
code = run_command(&child);
860-
if (!code)
861-
return NULL;
862-
return "unpack-objects abnormal exit";
858+
status = run_command(&child);
859+
if (status)
860+
return "unpack-objects abnormal exit";
863861
} else {
864-
const char *keeper[7];
865-
int s, status, i = 0;
862+
int s;
866863
char keep_arg[256];
867-
struct child_process ip;
868864

869865
s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid());
870866
if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
871867
strcpy(keep_arg + s, "localhost");
872868

873-
keeper[i++] = "index-pack";
874-
keeper[i++] = "--stdin";
869+
argv_array_pushl(&av, "index-pack",
870+
"--stdin", hdr_arg, keep_arg, NULL);
875871
if (fsck_objects)
876-
keeper[i++] = "--strict";
872+
argv_array_push(&av, "--strict");
877873
if (fix_thin)
878-
keeper[i++] = "--fix-thin";
879-
keeper[i++] = hdr_arg;
880-
keeper[i++] = keep_arg;
881-
keeper[i++] = NULL;
882-
memset(&ip, 0, sizeof(ip));
883-
ip.argv = keeper;
884-
ip.out = -1;
885-
ip.err = err_fd;
886-
ip.git_cmd = 1;
887-
status = start_command(&ip);
888-
if (status) {
874+
argv_array_push(&av, "--fix-thin");
875+
child.argv = av.argv;
876+
child.out = -1;
877+
child.err = err_fd;
878+
child.git_cmd = 1;
879+
status = start_command(&child);
880+
if (status)
889881
return "index-pack fork failed";
890-
}
891-
pack_lockfile = index_pack_lockfile(ip.out);
892-
close(ip.out);
893-
status = finish_command(&ip);
894-
if (!status) {
895-
reprepare_packed_git();
896-
return NULL;
897-
}
898-
return "index-pack abnormal exit";
882+
pack_lockfile = index_pack_lockfile(child.out);
883+
close(child.out);
884+
status = finish_command(&child);
885+
if (status)
886+
return "index-pack abnormal exit";
887+
reprepare_packed_git();
899888
}
889+
return NULL;
900890
}
901891

902892
static const char *unpack_with_sideband(void)

0 commit comments

Comments
 (0)