]>
Commit | Line | Data |
---|---|---|
61a7b982 | 1 | #include "test-tool.h" |
15d3af5e | 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 | 7 | #include "sigchain.h" |
15d3af5e JX |
8 | |
9 | static const char *proc_receive_usage[] = { | |
23a9235d | 10 | "test-tool proc-receive [<options>]", |
15d3af5e JX |
11 | NULL |
12 | }; | |
13 | ||
f65003b4 JX |
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; | |
15d3af5e JX |
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; | |
25 | ||
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 */ | |
35 | }; | |
36 | ||
37 | static void proc_receive_verison(struct packet_reader *reader) { | |
38 | int server_version = 0; | |
39 | ||
f65003b4 JX |
40 | if (die_read_version) |
41 | die("die with the --die-read-version option"); | |
42 | ||
15d3af5e JX |
43 | for (;;) { |
44 | int linelen; | |
45 | ||
46 | if (packet_reader_read(reader) != PACKET_READ_NORMAL) | |
47 | break; | |
48 | ||
80ffeb94 JX |
49 | /* Ignore version negotiation for version 0 */ |
50 | if (version == 0) | |
51 | continue; | |
52 | ||
15d3af5e JX |
53 | if (reader->pktlen > 8 && starts_with(reader->line, "version=")) { |
54 | server_version = atoi(reader->line+8); | |
80ffeb94 JX |
55 | if (server_version != 1) |
56 | die("bad protocol version: %d", server_version); | |
15d3af5e JX |
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; | |
64 | } | |
65 | } | |
66 | } | |
67 | ||
f65003b4 JX |
68 | if (die_write_version) |
69 | die("die with the --die-write-version option"); | |
70 | ||
80ffeb94 JX |
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": ""); | |
15d3af5e JX |
75 | packet_flush(1); |
76 | } | |
77 | ||
78 | static void proc_receive_read_commands(struct packet_reader *reader, | |
79 | struct command **commands) | |
80 | { | |
81 | struct command **tail = commands; | |
82 | ||
83 | for (;;) { | |
84 | struct object_id old_oid, new_oid; | |
85 | struct command *cmd; | |
86 | const char *refname; | |
87 | const char *p; | |
88 | ||
89 | if (packet_reader_read(reader) != PACKET_READ_NORMAL) | |
90 | break; | |
91 | ||
f65003b4 JX |
92 | if (die_read_commands) |
93 | die("die with the --die-read-commands option"); | |
94 | ||
15d3af5e JX |
95 | if (parse_oid_hex(reader->line, &old_oid, &p) || |
96 | *p++ != ' ' || | |
97 | parse_oid_hex(p, &new_oid, &p) || | |
f65003b4 | 98 | *p++ != ' ') |
15d3af5e JX |
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); | |
105 | ||
106 | *tail = cmd; | |
107 | tail = &cmd->next; | |
108 | } | |
109 | } | |
110 | ||
111 | static void proc_receive_read_push_options(struct packet_reader *reader, | |
112 | struct string_list *options) | |
113 | { | |
114 | ||
115 | if (no_push_options || !use_push_options) | |
116 | return; | |
117 | ||
f65003b4 JX |
118 | if (die_read_push_options) |
119 | die("die with the --die-read-push-options option"); | |
120 | ||
15d3af5e JX |
121 | while (1) { |
122 | if (packet_reader_read(reader) != PACKET_READ_NORMAL) | |
123 | break; | |
124 | ||
125 | string_list_append(options, reader->line); | |
126 | } | |
127 | } | |
128 | ||
129 | int cmd__proc_receive(int argc, const char **argv) | |
130 | { | |
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"), | |
f65003b4 JX |
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"), | |
15d3af5e JX |
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() | |
155 | }; | |
156 | ||
157 | setup_git_directory_gently(&nongit_ok); | |
158 | ||
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 | | |
f65003b4 | 164 | PACKET_READ_GENTLE_ON_EOF); |
15d3af5e JX |
165 | |
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); | |
170 | ||
171 | if (verbose) { | |
172 | struct command *cmd; | |
173 | ||
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": ""); | |
178 | ||
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); | |
184 | ||
185 | if (push_options.nr > 0) | |
186 | for_each_string_list_item(item, &push_options) | |
187 | fprintf(stderr, "proc-receive< %s\n", item->string); | |
188 | ||
189 | if (returns.nr) | |
190 | for_each_string_list_item(item, &returns) | |
191 | fprintf(stderr, "proc-receive> %s\n", item->string); | |
192 | } | |
193 | ||
f65003b4 JX |
194 | if (die_write_report) |
195 | die("die with the --die-write-report option"); | |
15d3af5e JX |
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); | |
201 | ||
202 | return 0; | |
203 | } |