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 <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Dec 5, 2013 at 20:02 UTC
31c42bff35ffc0a99ca77947389749d1397e1079
1 file changed
+30
-40
builtin/receive-pack.c
+30
-40
@@ -13,6 +13,7 @@
13
#include "string-list.h"
14
#include "sha1-array.h"
15
#include "connected.h"
16
+#include "argv-array.h"
17
#include "version.h"
18
19
static const char receive_pack_usage[] = "git receive-pack <git-dir>";
@@ -822,8 +823,11 @@ static const char *pack_lockfile;
823
static const char *unpack(int err_fd)
824
{
825
struct pack_header hdr;
826
+ struct argv_array av = ARGV_ARRAY_INIT;
827
const char *hdr_err;
828
+ int status;
829
char hdr_arg[38];
830
+ struct child_process child;
831
int fsck_objects = (receive_fsck_objects >= 0
832
? receive_fsck_objects
833
: transfer_fsck_objects >= 0
@@ -840,63 +844,49 @@ static const char *unpack(int err_fd)
844
"--pack_header=%"PRIu32",%"PRIu32,
845
ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
846
847
+ memset(&child, 0, sizeof(child));
848
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);
850
if (quiet)
849
- unpacker[i++] = "-q";
851
+ argv_array_push(&av, "-q");
852
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;
855
child.no_stdout = 1;
856
child.err = err_fd;
857
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";
861
} else {
864
- const char *keeper[7];
865
- int s, status, i = 0;
862
+ int s;
863
char keep_arg[256];
867
- struct child_process ip;
864
865
s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid());
866
if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
867
strcpy(keep_arg + s, "localhost");
868
873
- keeper[i++] = "index-pack";
874
- keeper[i++] = "--stdin";
869
+ argv_array_pushl(&av, "index-pack",
870
+ "--stdin", hdr_arg, keep_arg, NULL);
871
if (fsck_objects)
876
- keeper[i++] = "--strict";
872
+ argv_array_push(&av, "--strict");
873
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)
881
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();
888
}
889
+ return NULL;
890
}
891
892
static const char *unpack_with_sideband(void)