treewide: remove cache.h inclusion due to setup.h changes
[alt-git.git] / t / helper / test-proc-receive.c
blobf30022d2225b0a81cb026a9ccc24366ebdb9d2b4
1 #include "test-tool.h"
2 #include "connect.h"
3 #include "hex.h"
4 #include "parse-options.h"
5 #include "pkt-line.h"
6 #include "setup.h"
7 #include "sigchain.h"
9 static const char *proc_receive_usage[] = {
10 "test-tool proc-receive [<options>]",
11 NULL
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;
22 static int verbose;
23 static int version = 1;
24 static struct string_list returns = STRING_LIST_INIT_NODUP;
26 struct command {
27 struct command *next;
28 const char *error_string;
29 unsigned int skip_update:1,
30 did_not_exist:1;
31 int index;
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;
40 if (die_read_version)
41 die("die with the --die-read-version option");
43 for (;;) {
44 int linelen;
46 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
47 break;
49 /* Ignore version negotiation for version 0 */
50 if (version == 0)
51 continue;
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"))
61 use_atomic= 1;
62 if (parse_feature_request(feature_list, "push-options"))
63 use_push_options = 1;
68 if (die_write_version)
69 die("die with the --die-write-version option");
71 if (version != 0)
72 packet_write_fmt(1, "version=%d%c%s\n",
73 version, '\0',
74 use_push_options && !no_push_options ? "push-options": "");
75 packet_flush(1);
78 static void proc_receive_read_commands(struct packet_reader *reader,
79 struct command **commands)
81 struct command **tail = commands;
83 for (;;) {
84 struct object_id old_oid, new_oid;
85 struct command *cmd;
86 const char *refname;
87 const char *p;
89 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
90 break;
92 if (die_read_commands)
93 die("die with the --die-read-commands option");
95 if (parse_oid_hex(reader->line, &old_oid, &p) ||
96 *p++ != ' ' ||
97 parse_oid_hex(p, &new_oid, &p) ||
98 *p++ != ' ')
99 die("protocol error: expected 'old new ref', got '%s'",
100 reader->line);
101 refname = p;
102 FLEX_ALLOC_STR(cmd, ref_name, refname);
103 oidcpy(&cmd->old_oid, &old_oid);
104 oidcpy(&cmd->new_oid, &new_oid);
106 *tail = cmd;
107 tail = &cmd->next;
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)
116 return;
118 if (die_read_push_options)
119 die("die with the --die-read-push-options option");
121 while (1) {
122 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
123 break;
125 string_list_append(options, reader->line);
129 int cmd__proc_receive(int argc, const char **argv)
131 int nongit_ok = 0;
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"),
154 OPT_END()
157 setup_git_directory_gently(&nongit_ok);
159 argc = parse_options(argc, argv, "test-tools", options, proc_receive_usage, 0);
160 if (argc > 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);
171 if (verbose) {
172 struct command *cmd;
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),
183 cmd->ref_name);
185 if (push_options.nr > 0)
186 for_each_string_list_item(item, &push_options)
187 fprintf(stderr, "proc-receive< %s\n", item->string);
189 if (returns.nr)
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");
196 if (returns.nr)
197 for_each_string_list_item(item, &returns)
198 packet_write_fmt(1, "%s\n", item->string);
199 packet_flush(1);
200 sigchain_pop(SIGPIPE);
202 return 0;