]> git.ipfire.org Git - thirdparty/git.git/blame - t/helper/test-proc-receive.c
setup.h: move declarations for setup.c functions from cache.h
[thirdparty/git.git] / t / helper / test-proc-receive.c
CommitLineData
15d3af5e
JX
1#include "cache.h"
2#include "connect.h"
41771fa4 3#include "hex.h"
15d3af5e
JX
4#include "parse-options.h"
5#include "pkt-line.h"
e38da487 6#include "setup.h"
15d3af5e
JX
7#include "sigchain.h"
8#include "test-tool.h"
9
10static const char *proc_receive_usage[] = {
23a9235d 11 "test-tool proc-receive [<options>]",
15d3af5e
JX
12 NULL
13};
14
f65003b4
JX
15static int die_read_version;
16static int die_write_version;
17static int die_read_commands;
18static int die_read_push_options;
19static int die_write_report;
15d3af5e
JX
20static int no_push_options;
21static int use_atomic;
22static int use_push_options;
23static int verbose;
24static int version = 1;
25static struct string_list returns = STRING_LIST_INIT_NODUP;
26
27struct command {
28 struct command *next;
29 const char *error_string;
30 unsigned int skip_update:1,
31 did_not_exist:1;
32 int index;
33 struct object_id old_oid;
34 struct object_id new_oid;
35 char ref_name[FLEX_ARRAY]; /* more */
36};
37
38static void proc_receive_verison(struct packet_reader *reader) {
39 int server_version = 0;
40
f65003b4
JX
41 if (die_read_version)
42 die("die with the --die-read-version option");
43
15d3af5e
JX
44 for (;;) {
45 int linelen;
46
47 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
48 break;
49
80ffeb94
JX
50 /* Ignore version negotiation for version 0 */
51 if (version == 0)
52 continue;
53
15d3af5e
JX
54 if (reader->pktlen > 8 && starts_with(reader->line, "version=")) {
55 server_version = atoi(reader->line+8);
80ffeb94
JX
56 if (server_version != 1)
57 die("bad protocol version: %d", server_version);
15d3af5e
JX
58 linelen = strlen(reader->line);
59 if (linelen < reader->pktlen) {
60 const char *feature_list = reader->line + linelen + 1;
61 if (parse_feature_request(feature_list, "atomic"))
62 use_atomic= 1;
63 if (parse_feature_request(feature_list, "push-options"))
64 use_push_options = 1;
65 }
66 }
67 }
68
f65003b4
JX
69 if (die_write_version)
70 die("die with the --die-write-version option");
71
80ffeb94
JX
72 if (version != 0)
73 packet_write_fmt(1, "version=%d%c%s\n",
74 version, '\0',
75 use_push_options && !no_push_options ? "push-options": "");
15d3af5e
JX
76 packet_flush(1);
77}
78
79static void proc_receive_read_commands(struct packet_reader *reader,
80 struct command **commands)
81{
82 struct command **tail = commands;
83
84 for (;;) {
85 struct object_id old_oid, new_oid;
86 struct command *cmd;
87 const char *refname;
88 const char *p;
89
90 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
91 break;
92
f65003b4
JX
93 if (die_read_commands)
94 die("die with the --die-read-commands option");
95
15d3af5e
JX
96 if (parse_oid_hex(reader->line, &old_oid, &p) ||
97 *p++ != ' ' ||
98 parse_oid_hex(p, &new_oid, &p) ||
f65003b4 99 *p++ != ' ')
15d3af5e
JX
100 die("protocol error: expected 'old new ref', got '%s'",
101 reader->line);
102 refname = p;
103 FLEX_ALLOC_STR(cmd, ref_name, refname);
104 oidcpy(&cmd->old_oid, &old_oid);
105 oidcpy(&cmd->new_oid, &new_oid);
106
107 *tail = cmd;
108 tail = &cmd->next;
109 }
110}
111
112static void proc_receive_read_push_options(struct packet_reader *reader,
113 struct string_list *options)
114{
115
116 if (no_push_options || !use_push_options)
117 return;
118
f65003b4
JX
119 if (die_read_push_options)
120 die("die with the --die-read-push-options option");
121
15d3af5e
JX
122 while (1) {
123 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
124 break;
125
126 string_list_append(options, reader->line);
127 }
128}
129
130int cmd__proc_receive(int argc, const char **argv)
131{
132 int nongit_ok = 0;
133 struct packet_reader reader;
134 struct command *commands = NULL;
135 struct string_list push_options = STRING_LIST_INIT_DUP;
136 struct string_list_item *item;
137 struct option options[] = {
138 OPT_BOOL(0, "no-push-options", &no_push_options,
139 "disable push options"),
f65003b4
JX
140 OPT_BOOL(0, "die-read-version", &die_read_version,
141 "die when reading version"),
142 OPT_BOOL(0, "die-write-version", &die_write_version,
143 "die when writing version"),
144 OPT_BOOL(0, "die-read-commands", &die_read_commands,
145 "die when reading commands"),
146 OPT_BOOL(0, "die-read-push-options", &die_read_push_options,
147 "die when reading push-options"),
148 OPT_BOOL(0, "die-write-report", &die_write_report,
149 "die when writing report"),
15d3af5e
JX
150 OPT_STRING_LIST('r', "return", &returns, "old/new/ref/status/msg",
151 "return of results"),
152 OPT__VERBOSE(&verbose, "be verbose"),
153 OPT_INTEGER('V', "version", &version,
154 "use this protocol version number"),
155 OPT_END()
156 };
157
158 setup_git_directory_gently(&nongit_ok);
159
160 argc = parse_options(argc, argv, "test-tools", options, proc_receive_usage, 0);
161 if (argc > 0)
162 usage_msg_opt("Too many arguments.", proc_receive_usage, options);
163 packet_reader_init(&reader, 0, NULL, 0,
164 PACKET_READ_CHOMP_NEWLINE |
f65003b4 165 PACKET_READ_GENTLE_ON_EOF);
15d3af5e
JX
166
167 sigchain_push(SIGPIPE, SIG_IGN);
168 proc_receive_verison(&reader);
169 proc_receive_read_commands(&reader, &commands);
170 proc_receive_read_push_options(&reader, &push_options);
171
172 if (verbose) {
173 struct command *cmd;
174
175 if (use_push_options || use_atomic)
176 fprintf(stderr, "proc-receive:%s%s\n",
177 use_atomic? " atomic": "",
178 use_push_options ? " push_options": "");
179
180 for (cmd = commands; cmd; cmd = cmd->next)
181 fprintf(stderr, "proc-receive< %s %s %s\n",
182 oid_to_hex(&cmd->old_oid),
183 oid_to_hex(&cmd->new_oid),
184 cmd->ref_name);
185
186 if (push_options.nr > 0)
187 for_each_string_list_item(item, &push_options)
188 fprintf(stderr, "proc-receive< %s\n", item->string);
189
190 if (returns.nr)
191 for_each_string_list_item(item, &returns)
192 fprintf(stderr, "proc-receive> %s\n", item->string);
193 }
194
f65003b4
JX
195 if (die_write_report)
196 die("die with the --die-write-report option");
15d3af5e
JX
197 if (returns.nr)
198 for_each_string_list_item(item, &returns)
199 packet_write_fmt(1, "%s\n", item->string);
200 packet_flush(1);
201 sigchain_pop(SIGPIPE);
202
203 return 0;
204}