@matheusbsbs / matheus-bagliano / commits / cfee10a773
Fork 0

send-pack/receive-pack: allow errors to be reported back to pusher.

This updates the protocol between git-send-pack/git-receive-pack in a backward compatible way to allow failures at the receiving end to be propagated back to the sender. Most notably, versions of git-push before this could not notice if the update hook on the receiving end refused to update the ref for its own policy reasons. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Dec 25, 2005 at 23:18 UTC cfee10a773be0f8d0024f2567164c910d4213244
3 files changed +148 -39
connect.c
+4 -4
@@ -34,10 +34,6 @@ struct ref **get_remote_heads(int in, struct ref **list,
34 die("protocol error: expected sha/ref, got '%s'", buffer);
35 name = buffer + 41;
36
37 - if (ignore_funny && 45 < len && !memcmp(name, "refs/", 5) &&
38 - check_ref_format(name + 5))
39 - continue;
40 -
37 name_len = strlen(name);
38 if (len != name_len + 41) {
39 if (server_capabilities)
@@ -45,6 +41,10 @@ struct ref **get_remote_heads(int in, struct ref **list,
41 server_capabilities = strdup(name + name_len + 1);
42 }
43
44 + if (ignore_funny && 45 < len && !memcmp(name, "refs/", 5) &&
45 + check_ref_format(name + 5))
46 + continue;
47 +
48 if (nr_match && !path_match(name, nr_match, match))
49 continue;
50 ref = xcalloc(1, sizeof(*ref) + len - 40);
receive-pack.c
+88 -32
@@ -8,20 +8,33 @@ static const char receive_pack_usage[] = "git-receive-pack <git-dir>";
8
9 static const char unpacker[] = "git-unpack-objects";
10
11 +static int report_status = 0;
12 +
13 +static char capabilities[] = "report-status";
14 +static int capabilities_sent = 0;
15 +
16 static int show_ref(const char *path, const unsigned char *sha1)
17 {
13 - packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
18 + if (capabilities_sent)
19 + packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
20 + else
21 + packet_write(1, "%s %s%c%s\n",
22 + sha1_to_hex(sha1), path, 0, capabilities);
23 + capabilities_sent = 1;
24 return 0;
25 }
26
27 static void write_head_info(void)
28 {
29 for_each_ref(show_ref);
30 + if (!capabilities_sent)
31 + show_ref("capabilities^{}", null_sha1);
32 +
33 }
34
35 struct command {
36 struct command *next;
24 - unsigned char updated;
37 + const char *error_string;
38 unsigned char old_sha1[20];
39 unsigned char new_sha1[20];
40 char ref_name[0];
@@ -71,33 +84,37 @@ static int run_update_hook(const char *refname,
84 case 0:
85 return 0;
86 case -ERR_RUN_COMMAND_FORK:
74 - die("hook fork failed");
87 + return error("hook fork failed");
88 case -ERR_RUN_COMMAND_EXEC:
76 - die("hook execute failed");
89 + return error("hook execute failed");
90 case -ERR_RUN_COMMAND_WAITPID:
78 - die("waitpid failed");
91 + return error("waitpid failed");
92 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
80 - die("waitpid is confused");
93 + return error("waitpid is confused");
94 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
82 - fprintf(stderr, "%s died of signal\n", update_hook);
83 - return -1;
95 + return error("%s died of signal\n", update_hook);
96 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
85 - die("%s died strangely", update_hook);
97 + return error("%s died strangely", update_hook);
98 default:
99 error("%s exited with error code %d", update_hook, -code);
100 return -code;
101 }
102 }
103
92 -static int update(const char *name,
93 - unsigned char *old_sha1, unsigned char *new_sha1)
104 +static int update(struct command *cmd)
105 {
106 + const char *name = cmd->ref_name;
107 + unsigned char *old_sha1 = cmd->old_sha1;
108 + unsigned char *new_sha1 = cmd->new_sha1;
109 char new_hex[60], *old_hex, *lock_name;
110 int newfd, namelen, written;
111
98 - if (!strncmp(name, "refs/", 5) && check_ref_format(name + 5))
112 + cmd->error_string = NULL;
113 + if (!strncmp(name, "refs/", 5) && check_ref_format(name + 5)) {
114 + cmd->error_string = "funny refname";
115 return error("refusing to create funny ref '%s' locally",
116 name);
117 + }
118
119 namelen = strlen(name);
120 lock_name = xmalloc(namelen + 10);
@@ -106,16 +123,19 @@ static int update(const char *name,
123
124 strcpy(new_hex, sha1_to_hex(new_sha1));
125 old_hex = sha1_to_hex(old_sha1);
109 - if (!has_sha1_file(new_sha1))
126 + if (!has_sha1_file(new_sha1)) {
127 + cmd->error_string = "bad pack";
128 return error("unpack should have generated %s, "
129 "but I can't find it!", new_hex);
112 -
130 + }
131 safe_create_leading_directories(lock_name);
132
133 newfd = open(lock_name, O_CREAT | O_EXCL | O_WRONLY, 0666);
116 - if (newfd < 0)
134 + if (newfd < 0) {
135 + cmd->error_string = "can't lock";
136 return error("unable to create %s (%s)",
137 lock_name, strerror(errno));
138 + }
139
140 /* Write the ref with an ending '\n' */
141 new_hex[40] = '\n';
@@ -127,18 +147,22 @@ static int update(const char *name,
147 close(newfd);
148 if (written != 41) {
149 unlink(lock_name);
150 + cmd->error_string = "can't write";
151 return error("unable to write %s", lock_name);
152 }
153 if (verify_old_ref(name, old_hex) < 0) {
154 unlink(lock_name);
155 + cmd->error_string = "raced";
156 return error("%s changed during push", name);
157 }
158 if (run_update_hook(name, old_hex, new_hex)) {
159 unlink(lock_name);
160 + cmd->error_string = "hook declined";
161 return error("hook declined to update %s\n", name);
162 }
163 else if (rename(lock_name, name) < 0) {
164 unlink(lock_name);
165 + cmd->error_string = "can't rename";
166 return error("unable to replace %s", name);
167 }
168 else {
@@ -158,7 +182,7 @@ static void run_update_post_hook(struct command *cmd)
182 if (access(update_post_hook, X_OK) < 0)
183 return;
184 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
161 - if (!cmd_p->updated)
185 + if (cmd_p->error_string)
186 continue;
187 argc++;
188 }
@@ -166,7 +190,7 @@ static void run_update_post_hook(struct command *cmd)
190 argv[0] = update_post_hook;
191
192 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
169 - if (!cmd_p->updated)
193 + if (cmd_p->error_string)
194 continue;
195 argv[argc] = xmalloc(strlen(cmd_p->ref_name) + 1);
196 strcpy(argv[argc], cmd_p->ref_name);
@@ -185,8 +209,7 @@ static void execute_commands(void)
209 struct command *cmd = commands;
210
211 while (cmd) {
188 - cmd->updated = !update(cmd->ref_name,
189 - cmd->old_sha1, cmd->new_sha1);
212 + update(cmd);
213 cmd = cmd->next;
214 }
215 run_update_post_hook(commands);
@@ -199,7 +222,8 @@ static void read_head_info(void)
222 static char line[1000];
223 unsigned char old_sha1[20], new_sha1[20];
224 struct command *cmd;
202 - int len;
225 + char *refname;
226 + int len, reflen;
227
228 len = packet_read_line(0, line, sizeof(line));
229 if (!len)
@@ -211,38 +235,66 @@ static void read_head_info(void)
235 line[81] != ' ' ||
236 get_sha1_hex(line, old_sha1) ||
237 get_sha1_hex(line + 41, new_sha1))
214 - die("protocol error: expected old/new/ref, got '%s'", line);
238 + die("protocol error: expected old/new/ref, got '%s'",
239 + line);
240 +
241 + refname = line + 82;
242 + reflen = strlen(refname);
243 + if (reflen + 82 < len) {
244 + if (strstr(refname + reflen + 1, "report-status"))
245 + report_status = 1;
246 + }
247 cmd = xmalloc(sizeof(struct command) + len - 80);
248 memcpy(cmd->old_sha1, old_sha1, 20);
249 memcpy(cmd->new_sha1, new_sha1, 20);
250 memcpy(cmd->ref_name, line + 82, len - 81);
251 + cmd->error_string = "n/a (unpacker error)";
252 cmd->next = NULL;
253 *p = cmd;
254 p = &cmd->next;
255 }
256 }
257
225 -static void unpack(void)
258 +static const char *unpack(int *error_code)
259 {
260 int code = run_command(unpacker, NULL);
261 +
262 + *error_code = 0;
263 switch (code) {
264 case 0:
230 - return;
265 + return NULL;
266 case -ERR_RUN_COMMAND_FORK:
232 - die("unpack fork failed");
267 + return "unpack fork failed";
268 case -ERR_RUN_COMMAND_EXEC:
234 - die("unpack execute failed");
269 + return "unpack execute failed";
270 case -ERR_RUN_COMMAND_WAITPID:
236 - die("waitpid failed");
271 + return "waitpid failed";
272 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
238 - die("waitpid is confused");
273 + return "waitpid is confused";
274 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
240 - die("%s died of signal", unpacker);
275 + return "unpacker died of signal";
276 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
242 - die("%s died strangely", unpacker);
277 + return "unpacker died strangely";
278 default:
244 - die("%s exited with error code %d", unpacker, -code);
279 + *error_code = -code;
280 + return "unpacker exited with error code";
281 + }
282 +}
283 +
284 +static void report(const char *unpack_status)
285 +{
286 + struct command *cmd;
287 + packet_write(1, "unpack %s\n",
288 + unpack_status ? unpack_status : "ok");
289 + for (cmd = commands; cmd; cmd = cmd->next) {
290 + if (!cmd->error_string)
291 + packet_write(1, "ok %s\n",
292 + cmd->ref_name);
293 + else
294 + packet_write(1, "ng %s %s\n",
295 + cmd->ref_name, cmd->error_string);
296 }
297 + packet_flush(1);
298 }
299
300 int main(int argc, char **argv)
@@ -275,8 +327,12 @@ int main(int argc, char **argv)
327
328 read_head_info();
329 if (commands) {
278 - unpack();
279 - execute_commands();
330 + int code;
331 + const char *unpack_status = unpack(&code);
332 + if (!unpack_status)
333 + execute_commands();
334 + if (report_status)
335 + report(unpack_status);
336 }
337 return 0;
338 }
send-pack.c
+56 -3
@@ -176,16 +176,53 @@ static void get_local_heads(void)
176 for_each_ref(one_local_ref);
177 }
178
179 +static int receive_status(int in)
180 +{
181 + char line[1000];
182 + int ret = 0;
183 + int len = packet_read_line(in, line, sizeof(line));
184 + if (len < 10 || memcmp(line, "unpack ", 7)) {
185 + fprintf(stderr, "did not receive status back\n");
186 + return -1;
187 + }
188 + if (memcmp(line, "unpack ok\n", 10)) {
189 + fputs(line, stderr);
190 + ret = -1;
191 + }
192 + while (1) {
193 + len = packet_read_line(in, line, sizeof(line));
194 + if (!len)
195 + break;
196 + if (len < 3 ||
197 + (memcmp(line, "ok", 2) && memcmp(line, "ng", 2))) {
198 + fprintf(stderr, "protocol error: %s\n", line);
199 + ret = -1;
200 + break;
201 + }
202 + if (!memcmp(line, "ok", 2))
203 + continue;
204 + fputs(line, stderr);
205 + ret = -1;
206 + }
207 + return ret;
208 +}
209 +
210 static int send_pack(int in, int out, int nr_refspec, char **refspec)
211 {
212 struct ref *ref;
213 int new_refs;
214 int ret = 0;
215 + int ask_for_status_report = 0;
216 + int expect_status_report = 0;
217
218 /* No funny business with the matcher */
219 remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, 1);
220 get_local_heads();
221
222 + /* Does the other end support the reporting? */
223 + if (server_supports("report-status"))
224 + ask_for_status_report = 1;
225 +
226 /* match them up */
227 if (!remote_tail)
228 remote_tail = &remote_refs;
@@ -260,7 +297,17 @@ static int send_pack(int in, int out, int nr_refspec, char **refspec)
297 new_refs++;
298 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
299 new_hex = sha1_to_hex(ref->new_sha1);
263 - packet_write(out, "%s %s %s", old_hex, new_hex, ref->name);
300 +
301 + if (ask_for_status_report) {
302 + packet_write(out, "%s %s %s%c%s",
303 + old_hex, new_hex, ref->name, 0,
304 + "report-status");
305 + ask_for_status_report = 0;
306 + expect_status_report = 1;
307 + }
308 + else
309 + packet_write(out, "%s %s %s",
310 + old_hex, new_hex, ref->name);
311 fprintf(stderr, "updating '%s'", ref->name);
312 if (strcmp(ref->name, ref->peer_ref->name))
313 fprintf(stderr, " using '%s'", ref->peer_ref->name);
@@ -270,9 +317,15 @@ static int send_pack(int in, int out, int nr_refspec, char **refspec)
317 packet_flush(out);
318 if (new_refs)
319 pack_objects(out, remote_refs);
273 - else if (ret == 0)
274 - fprintf(stderr, "Everything up-to-date\n");
320 close(out);
321 +
322 + if (expect_status_report) {
323 + if (receive_status(in))
324 + ret = -4;
325 + }
326 +
327 + if (!new_refs && ret == 0)
328 + fprintf(stderr, "Everything up-to-date\n");
329 return ret;
330 }
331