3 #include "parse-options.h"
8 static const char *proc_receive_usage
[] = {
9 "test-tool proc-receive [<options>...]",
13 static int die_read_version
;
14 static int die_write_version
;
15 static int die_read_commands
;
16 static int die_read_push_options
;
17 static int die_write_report
;
18 static int no_push_options
;
19 static int use_atomic
;
20 static int use_push_options
;
22 static int version
= 1;
23 static struct string_list returns
= STRING_LIST_INIT_NODUP
;
27 const char *error_string
;
28 unsigned int skip_update
:1,
31 struct object_id old_oid
;
32 struct object_id new_oid
;
33 char ref_name
[FLEX_ARRAY
]; /* more */
36 static void proc_receive_verison(struct packet_reader
*reader
) {
37 int server_version
= 0;
40 die("die with the --die-read-version option");
45 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
48 /* Ignore version negotiation for version 0 */
52 if (reader
->pktlen
> 8 && starts_with(reader
->line
, "version=")) {
53 server_version
= atoi(reader
->line
+8);
54 if (server_version
!= 1)
55 die("bad protocol version: %d", server_version
);
56 linelen
= strlen(reader
->line
);
57 if (linelen
< reader
->pktlen
) {
58 const char *feature_list
= reader
->line
+ linelen
+ 1;
59 if (parse_feature_request(feature_list
, "atomic"))
61 if (parse_feature_request(feature_list
, "push-options"))
67 if (die_write_version
)
68 die("die with the --die-write-version option");
71 packet_write_fmt(1, "version=%d%c%s\n",
73 use_push_options
&& !no_push_options
? "push-options": "");
77 static void proc_receive_read_commands(struct packet_reader
*reader
,
78 struct command
**commands
)
80 struct command
**tail
= commands
;
83 struct object_id old_oid
, new_oid
;
88 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
91 if (die_read_commands
)
92 die("die with the --die-read-commands option");
94 if (parse_oid_hex(reader
->line
, &old_oid
, &p
) ||
96 parse_oid_hex(p
, &new_oid
, &p
) ||
98 die("protocol error: expected 'old new ref', got '%s'",
101 FLEX_ALLOC_STR(cmd
, ref_name
, refname
);
102 oidcpy(&cmd
->old_oid
, &old_oid
);
103 oidcpy(&cmd
->new_oid
, &new_oid
);
110 static void proc_receive_read_push_options(struct packet_reader
*reader
,
111 struct string_list
*options
)
114 if (no_push_options
|| !use_push_options
)
117 if (die_read_push_options
)
118 die("die with the --die-read-push-options option");
121 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
124 string_list_append(options
, reader
->line
);
128 int cmd__proc_receive(int argc
, const char **argv
)
131 struct packet_reader reader
;
132 struct command
*commands
= NULL
;
133 struct string_list push_options
= STRING_LIST_INIT_DUP
;
134 struct string_list_item
*item
;
135 struct option options
[] = {
136 OPT_BOOL(0, "no-push-options", &no_push_options
,
137 "disable push options"),
138 OPT_BOOL(0, "die-read-version", &die_read_version
,
139 "die when reading version"),
140 OPT_BOOL(0, "die-write-version", &die_write_version
,
141 "die when writing version"),
142 OPT_BOOL(0, "die-read-commands", &die_read_commands
,
143 "die when reading commands"),
144 OPT_BOOL(0, "die-read-push-options", &die_read_push_options
,
145 "die when reading push-options"),
146 OPT_BOOL(0, "die-write-report", &die_write_report
,
147 "die when writing report"),
148 OPT_STRING_LIST('r', "return", &returns
, "old/new/ref/status/msg",
149 "return of results"),
150 OPT__VERBOSE(&verbose
, "be verbose"),
151 OPT_INTEGER('V', "version", &version
,
152 "use this protocol version number"),
156 setup_git_directory_gently(&nongit_ok
);
158 argc
= parse_options(argc
, argv
, "test-tools", options
, proc_receive_usage
, 0);
160 usage_msg_opt("Too many arguments.", proc_receive_usage
, options
);
161 packet_reader_init(&reader
, 0, NULL
, 0,
162 PACKET_READ_CHOMP_NEWLINE
|
163 PACKET_READ_GENTLE_ON_EOF
);
165 sigchain_push(SIGPIPE
, SIG_IGN
);
166 proc_receive_verison(&reader
);
167 proc_receive_read_commands(&reader
, &commands
);
168 proc_receive_read_push_options(&reader
, &push_options
);
173 if (use_push_options
|| use_atomic
)
174 fprintf(stderr
, "proc-receive:%s%s\n",
175 use_atomic
? " atomic": "",
176 use_push_options
? " push_options": "");
178 for (cmd
= commands
; cmd
; cmd
= cmd
->next
)
179 fprintf(stderr
, "proc-receive< %s %s %s\n",
180 oid_to_hex(&cmd
->old_oid
),
181 oid_to_hex(&cmd
->new_oid
),
184 if (push_options
.nr
> 0)
185 for_each_string_list_item(item
, &push_options
)
186 fprintf(stderr
, "proc-receive< %s\n", item
->string
);
189 for_each_string_list_item(item
, &returns
)
190 fprintf(stderr
, "proc-receive> %s\n", item
->string
);
193 if (die_write_report
)
194 die("die with the --die-write-report option");
196 for_each_string_list_item(item
, &returns
)
197 packet_write_fmt(1, "%s\n", item
->string
);
199 sigchain_pop(SIGPIPE
);