Merge branch '2.30-rc1' of github.com:bitigchi/git-po
[alt-git.git] / t / helper / test-proc-receive.c
blobcc08506cf0bb73b1aefda41079f60cba1edcb9c1
1 #include "cache.h"
2 #include "connect.h"
3 #include "parse-options.h"
4 #include "pkt-line.h"
5 #include "sigchain.h"
6 #include "test-tool.h"
8 static const char *proc_receive_usage[] = {
9 "test-tool proc-receive [<options>...]",
10 NULL
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;
21 static int verbose;
22 static int version = 1;
23 static struct string_list returns = STRING_LIST_INIT_NODUP;
25 struct command {
26 struct command *next;
27 const char *error_string;
28 unsigned int skip_update:1,
29 did_not_exist:1;
30 int index;
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;
39 if (die_read_version)
40 die("die with the --die-read-version option");
42 for (;;) {
43 int linelen;
45 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
46 break;
48 /* Ignore version negotiation for version 0 */
49 if (version == 0)
50 continue;
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"))
60 use_atomic= 1;
61 if (parse_feature_request(feature_list, "push-options"))
62 use_push_options = 1;
67 if (die_write_version)
68 die("die with the --die-write-version option");
70 if (version != 0)
71 packet_write_fmt(1, "version=%d%c%s\n",
72 version, '\0',
73 use_push_options && !no_push_options ? "push-options": "");
74 packet_flush(1);
77 static void proc_receive_read_commands(struct packet_reader *reader,
78 struct command **commands)
80 struct command **tail = commands;
82 for (;;) {
83 struct object_id old_oid, new_oid;
84 struct command *cmd;
85 const char *refname;
86 const char *p;
88 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
89 break;
91 if (die_read_commands)
92 die("die with the --die-read-commands option");
94 if (parse_oid_hex(reader->line, &old_oid, &p) ||
95 *p++ != ' ' ||
96 parse_oid_hex(p, &new_oid, &p) ||
97 *p++ != ' ')
98 die("protocol error: expected 'old new ref', got '%s'",
99 reader->line);
100 refname = p;
101 FLEX_ALLOC_STR(cmd, ref_name, refname);
102 oidcpy(&cmd->old_oid, &old_oid);
103 oidcpy(&cmd->new_oid, &new_oid);
105 *tail = cmd;
106 tail = &cmd->next;
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)
115 return;
117 if (die_read_push_options)
118 die("die with the --die-read-push-options option");
120 while (1) {
121 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
122 break;
124 string_list_append(options, reader->line);
128 int cmd__proc_receive(int argc, const char **argv)
130 int nongit_ok = 0;
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"),
153 OPT_END()
156 setup_git_directory_gently(&nongit_ok);
158 argc = parse_options(argc, argv, "test-tools", options, proc_receive_usage, 0);
159 if (argc > 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);
170 if (verbose) {
171 struct command *cmd;
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),
182 cmd->ref_name);
184 if (push_options.nr > 0)
185 for_each_string_list_item(item, &push_options)
186 fprintf(stderr, "proc-receive< %s\n", item->string);
188 if (returns.nr)
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");
195 if (returns.nr)
196 for_each_string_list_item(item, &returns)
197 packet_write_fmt(1, "%s\n", item->string);
198 packet_flush(1);
199 sigchain_pop(SIGPIPE);
201 return 0;