3 #include "parse-options.h"
8 static const char *proc_receive_usage[] = {
9 "test-tool proc-receive [<options>...]",
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;
22 static int version = 1;
23 static struct string_list returns = STRING_LIST_INIT_NODUP;
27 const char *error_string;
28 unsigned int skip_update:1,
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;
40 die("die with the --die-read-version option");
45 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
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"))
55 if (parse_feature_request(feature_list, "push-options"))
61 if (server_version != 1)
62 die("bad protocol version: %d", server_version);
64 if (die_write_version)
65 die("die with the --die-write-version option");
67 packet_write_fmt(1, "version=%d%c%s\n",
69 use_push_options && !no_push_options ? "push-options": "");
73 static void proc_receive_read_commands(struct packet_reader *reader,
74 struct command **commands)
76 struct command **tail = commands;
79 struct object_id old_oid, new_oid;
84 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
87 if (die_read_commands)
88 die("die with the --die-read-commands option");
90 if (parse_oid_hex(reader->line, &old_oid, &p) ||
92 parse_oid_hex(p, &new_oid, &p) ||
94 die("protocol error: expected 'old new ref', got '%s'",
97 FLEX_ALLOC_STR(cmd, ref_name, refname);
98 oidcpy(&cmd->old_oid, &old_oid);
99 oidcpy(&cmd->new_oid, &new_oid);
106 static void proc_receive_read_push_options(struct packet_reader *reader,
107 struct string_list *options)
110 if (no_push_options || !use_push_options)
113 if (die_read_push_options)
114 die("die with the --die-read-push-options option");
117 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
120 string_list_append(options, reader->line);
124 int cmd__proc_receive(int argc, const char **argv)
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"),
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"),
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"),
152 setup_git_directory_gently(&nongit_ok);
154 argc = parse_options(argc, argv, "test-tools", options, proc_receive_usage, 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 |
159 PACKET_READ_GENTLE_ON_EOF);
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);
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": "");
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),
180 if (push_options.nr > 0)
181 for_each_string_list_item(item, &push_options)
182 fprintf(stderr, "proc-receive< %s\n", item->string);
185 for_each_string_list_item(item, &returns)
186 fprintf(stderr, "proc-receive> %s\n", item->string);
189 if (die_write_report)
190 die("die with the --die-write-report option");
192 for_each_string_list_item(item, &returns)
193 packet_write_fmt(1, "%s\n", item->string);
195 sigchain_pop(SIGPIPE);