3 #include "parse-options.h"
8 static const char *proc_receive_usage[] = {
9 "test-tool proc-receive [<options>...]",
13 static int die_version;
14 static int die_readline;
15 static int no_push_options;
16 static int use_atomic;
17 static int use_push_options;
19 static int version = 1;
20 static struct string_list returns = STRING_LIST_INIT_NODUP;
24 const char *error_string;
25 unsigned int skip_update:1,
28 struct object_id old_oid;
29 struct object_id new_oid;
30 char ref_name[FLEX_ARRAY]; /* more */
33 static void proc_receive_verison(struct packet_reader *reader) {
34 int server_version = 0;
39 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
42 if (reader->pktlen > 8 && starts_with(reader->line, "version=")) {
43 server_version = atoi(reader->line+8);
44 linelen = strlen(reader->line);
45 if (linelen < reader->pktlen) {
46 const char *feature_list = reader->line + linelen + 1;
47 if (parse_feature_request(feature_list, "atomic"))
49 if (parse_feature_request(feature_list, "push-options"))
55 if (server_version != 1 || die_version)
56 die("bad protocol version: %d", server_version);
58 packet_write_fmt(1, "version=%d%c%s\n",
60 use_push_options && !no_push_options ? "push-options": "");
64 static void proc_receive_read_commands(struct packet_reader *reader,
65 struct command **commands)
67 struct command **tail = commands;
70 struct object_id old_oid, new_oid;
75 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
78 if (parse_oid_hex(reader->line, &old_oid, &p) ||
80 parse_oid_hex(p, &new_oid, &p) ||
83 die("protocol error: expected 'old new ref', got '%s'",
86 FLEX_ALLOC_STR(cmd, ref_name, refname);
87 oidcpy(&cmd->old_oid, &old_oid);
88 oidcpy(&cmd->new_oid, &new_oid);
95 static void proc_receive_read_push_options(struct packet_reader *reader,
96 struct string_list *options)
99 if (no_push_options || !use_push_options)
103 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
106 string_list_append(options, reader->line);
110 int cmd__proc_receive(int argc, const char **argv)
113 struct packet_reader reader;
114 struct command *commands = NULL;
115 struct string_list push_options = STRING_LIST_INIT_DUP;
116 struct string_list_item *item;
117 struct option options[] = {
118 OPT_BOOL(0, "no-push-options", &no_push_options,
119 "disable push options"),
120 OPT_BOOL(0, "die-version", &die_version,
121 "die during version negotiation"),
122 OPT_BOOL(0, "die-readline", &die_readline,
123 "die when readline"),
124 OPT_STRING_LIST('r', "return", &returns, "old/new/ref/status/msg",
125 "return of results"),
126 OPT__VERBOSE(&verbose, "be verbose"),
127 OPT_INTEGER('V', "version", &version,
128 "use this protocol version number"),
132 setup_git_directory_gently(&nongit_ok);
134 argc = parse_options(argc, argv, "test-tools", options, proc_receive_usage, 0);
136 usage_msg_opt("Too many arguments.", proc_receive_usage, options);
137 packet_reader_init(&reader, 0, NULL, 0,
138 PACKET_READ_CHOMP_NEWLINE |
139 PACKET_READ_DIE_ON_ERR_PACKET);
141 sigchain_push(SIGPIPE, SIG_IGN);
142 proc_receive_verison(&reader);
143 proc_receive_read_commands(&reader, &commands);
144 proc_receive_read_push_options(&reader, &push_options);
149 if (use_push_options || use_atomic)
150 fprintf(stderr, "proc-receive:%s%s\n",
151 use_atomic? " atomic": "",
152 use_push_options ? " push_options": "");
154 for (cmd = commands; cmd; cmd = cmd->next)
155 fprintf(stderr, "proc-receive< %s %s %s\n",
156 oid_to_hex(&cmd->old_oid),
157 oid_to_hex(&cmd->new_oid),
160 if (push_options.nr > 0)
161 for_each_string_list_item(item, &push_options)
162 fprintf(stderr, "proc-receive< %s\n", item->string);
165 for_each_string_list_item(item, &returns)
166 fprintf(stderr, "proc-receive> %s\n", item->string);
170 for_each_string_list_item(item, &returns)
171 packet_write_fmt(1, "%s\n", item->string);
173 sigchain_pop(SIGPIPE);