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