Merge branch 'jk/report-fn-typedef'
[git] / t / helper / test-proc-receive.c
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
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;
18 static int verbose;
19 static int version = 1;
20 static struct string_list returns = STRING_LIST_INIT_NODUP;
21
22 struct command {
23         struct command *next;
24         const char *error_string;
25         unsigned int skip_update:1,
26                      did_not_exist:1;
27         int index;
28         struct object_id old_oid;
29         struct object_id new_oid;
30         char ref_name[FLEX_ARRAY]; /* more */
31 };
32
33 static void proc_receive_verison(struct packet_reader *reader) {
34         int server_version = 0;
35
36         for (;;) {
37                 int linelen;
38
39                 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
40                         break;
41
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"))
48                                         use_atomic= 1;
49                                 if (parse_feature_request(feature_list, "push-options"))
50                                         use_push_options = 1;
51                         }
52                 }
53         }
54
55         if (server_version != 1 || die_version)
56                 die("bad protocol version: %d", server_version);
57
58         packet_write_fmt(1, "version=%d%c%s\n",
59                          version, '\0',
60                          use_push_options && !no_push_options ? "push-options": "");
61         packet_flush(1);
62 }
63
64 static void proc_receive_read_commands(struct packet_reader *reader,
65                                        struct command **commands)
66 {
67         struct command **tail = commands;
68
69         for (;;) {
70                 struct object_id old_oid, new_oid;
71                 struct command *cmd;
72                 const char *refname;
73                 const char *p;
74
75                 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
76                         break;
77
78                 if (parse_oid_hex(reader->line, &old_oid, &p) ||
79                     *p++ != ' ' ||
80                     parse_oid_hex(p, &new_oid, &p) ||
81                     *p++ != ' ' ||
82                     die_readline)
83                         die("protocol error: expected 'old new ref', got '%s'",
84                             reader->line);
85                 refname = p;
86                 FLEX_ALLOC_STR(cmd, ref_name, refname);
87                 oidcpy(&cmd->old_oid, &old_oid);
88                 oidcpy(&cmd->new_oid, &new_oid);
89
90                 *tail = cmd;
91                 tail = &cmd->next;
92         }
93 }
94
95 static void proc_receive_read_push_options(struct packet_reader *reader,
96                                            struct string_list *options)
97 {
98
99         if (no_push_options || !use_push_options)
100                return;
101
102         while (1) {
103                 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
104                         break;
105
106                 string_list_append(options, reader->line);
107         }
108 }
109
110 int cmd__proc_receive(int argc, const char **argv)
111 {
112         int nongit_ok = 0;
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"),
129                 OPT_END()
130         };
131
132         setup_git_directory_gently(&nongit_ok);
133
134         argc = parse_options(argc, argv, "test-tools", options, proc_receive_usage, 0);
135         if (argc > 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);
140
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);
145
146         if (verbose) {
147                 struct command *cmd;
148
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": "");
153
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),
158                                 cmd->ref_name);
159
160                 if (push_options.nr > 0)
161                         for_each_string_list_item(item, &push_options)
162                                 fprintf(stderr, "proc-receive< %s\n", item->string);
163
164                 if (returns.nr)
165                         for_each_string_list_item(item, &returns)
166                                 fprintf(stderr, "proc-receive> %s\n", item->string);
167         }
168
169         if (returns.nr)
170                 for_each_string_list_item(item, &returns)
171                         packet_write_fmt(1, "%s\n", item->string);
172         packet_flush(1);
173         sigchain_pop(SIGPIPE);
174
175         return 0;
176 }