4 #include "parse-options.h"
9 static const char *proc_receive_usage
[] = {
10 "test-tool proc-receive [<options>]",
14 static int die_read_version
;
15 static int die_write_version
;
16 static int die_read_commands
;
17 static int die_read_push_options
;
18 static int die_write_report
;
19 static int no_push_options
;
20 static int use_atomic
;
21 static int use_push_options
;
23 static int version
= 1;
24 static struct string_list returns
= STRING_LIST_INIT_NODUP
;
28 const char *error_string
;
29 unsigned int skip_update
:1,
32 struct object_id old_oid
;
33 struct object_id new_oid
;
34 char ref_name
[FLEX_ARRAY
]; /* more */
37 static void proc_receive_verison(struct packet_reader
*reader
) {
38 int server_version
= 0;
41 die("die with the --die-read-version option");
46 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
49 /* Ignore version negotiation for version 0 */
53 if (reader
->pktlen
> 8 && starts_with(reader
->line
, "version=")) {
54 server_version
= atoi(reader
->line
+8);
55 if (server_version
!= 1)
56 die("bad protocol version: %d", server_version
);
57 linelen
= strlen(reader
->line
);
58 if (linelen
< reader
->pktlen
) {
59 const char *feature_list
= reader
->line
+ linelen
+ 1;
60 if (parse_feature_request(feature_list
, "atomic"))
62 if (parse_feature_request(feature_list
, "push-options"))
68 if (die_write_version
)
69 die("die with the --die-write-version option");
72 packet_write_fmt(1, "version=%d%c%s\n",
74 use_push_options
&& !no_push_options
? "push-options": "");
78 static void proc_receive_read_commands(struct packet_reader
*reader
,
79 struct command
**commands
)
81 struct command
**tail
= commands
;
84 struct object_id old_oid
, new_oid
;
89 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
92 if (die_read_commands
)
93 die("die with the --die-read-commands option");
95 if (parse_oid_hex(reader
->line
, &old_oid
, &p
) ||
97 parse_oid_hex(p
, &new_oid
, &p
) ||
99 die("protocol error: expected 'old new ref', got '%s'",
102 FLEX_ALLOC_STR(cmd
, ref_name
, refname
);
103 oidcpy(&cmd
->old_oid
, &old_oid
);
104 oidcpy(&cmd
->new_oid
, &new_oid
);
111 static void proc_receive_read_push_options(struct packet_reader
*reader
,
112 struct string_list
*options
)
115 if (no_push_options
|| !use_push_options
)
118 if (die_read_push_options
)
119 die("die with the --die-read-push-options option");
122 if (packet_reader_read(reader
) != PACKET_READ_NORMAL
)
125 string_list_append(options
, reader
->line
);
129 int cmd__proc_receive(int argc
, const char **argv
)
132 struct packet_reader reader
;
133 struct command
*commands
= NULL
;
134 struct string_list push_options
= STRING_LIST_INIT_DUP
;
135 struct string_list_item
*item
;
136 struct option options
[] = {
137 OPT_BOOL(0, "no-push-options", &no_push_options
,
138 "disable push options"),
139 OPT_BOOL(0, "die-read-version", &die_read_version
,
140 "die when reading version"),
141 OPT_BOOL(0, "die-write-version", &die_write_version
,
142 "die when writing version"),
143 OPT_BOOL(0, "die-read-commands", &die_read_commands
,
144 "die when reading commands"),
145 OPT_BOOL(0, "die-read-push-options", &die_read_push_options
,
146 "die when reading push-options"),
147 OPT_BOOL(0, "die-write-report", &die_write_report
,
148 "die when writing report"),
149 OPT_STRING_LIST('r', "return", &returns
, "old/new/ref/status/msg",
150 "return of results"),
151 OPT__VERBOSE(&verbose
, "be verbose"),
152 OPT_INTEGER('V', "version", &version
,
153 "use this protocol version number"),
157 setup_git_directory_gently(&nongit_ok
);
159 argc
= parse_options(argc
, argv
, "test-tools", options
, proc_receive_usage
, 0);
161 usage_msg_opt("Too many arguments.", proc_receive_usage
, options
);
162 packet_reader_init(&reader
, 0, NULL
, 0,
163 PACKET_READ_CHOMP_NEWLINE
|
164 PACKET_READ_GENTLE_ON_EOF
);
166 sigchain_push(SIGPIPE
, SIG_IGN
);
167 proc_receive_verison(&reader
);
168 proc_receive_read_commands(&reader
, &commands
);
169 proc_receive_read_push_options(&reader
, &push_options
);
174 if (use_push_options
|| use_atomic
)
175 fprintf(stderr
, "proc-receive:%s%s\n",
176 use_atomic
? " atomic": "",
177 use_push_options
? " push_options": "");
179 for (cmd
= commands
; cmd
; cmd
= cmd
->next
)
180 fprintf(stderr
, "proc-receive< %s %s %s\n",
181 oid_to_hex(&cmd
->old_oid
),
182 oid_to_hex(&cmd
->new_oid
),
185 if (push_options
.nr
> 0)
186 for_each_string_list_item(item
, &push_options
)
187 fprintf(stderr
, "proc-receive< %s\n", item
->string
);
190 for_each_string_list_item(item
, &returns
)
191 fprintf(stderr
, "proc-receive> %s\n", item
->string
);
194 if (die_write_report
)
195 die("die with the --die-write-report option");
197 for_each_string_list_item(item
, &returns
)
198 packet_write_fmt(1, "%s\n", item
->string
);
200 sigchain_pop(SIGPIPE
);