146
for_each_ref(one_local_ref, NULL);
147
}
148
149
-static struct ref *set_ref_error(struct ref *refs, const char *line)
150
-{
151
- struct ref *ref;
152
-
153
- for (ref = refs; ref; ref = ref->next) {
154
- const char *msg;
155
- if (prefixcmp(line, ref->name))
156
- continue;
157
- msg = line + strlen(ref->name);
158
- if (*msg++ != ' ')
159
- continue;
160
- ref->status = REF_STATUS_REMOTE_REJECT;
161
- ref->error = xstrdup(msg);
162
- ref->error[strlen(ref->error)-1] = '\0';
163
- return ref;
164
- }
165
- return NULL;
166
-}
167
-
168
-/* a return value of -1 indicates that an error occurred,
169
- * but we were able to set individual ref errors. A return
170
- * value of -2 means we couldn't even get that far. */
149
static int receive_status(int in, struct ref *refs)
150
{
151
struct ref *hint;
152
char line[1000];
153
int ret = 0;
154
int len = packet_read_line(in, line, sizeof(line));
177
- if (len < 10 || memcmp(line, "unpack ", 7)) {
178
- fprintf(stderr, "did not receive status back\n");
179
- return -2;
180
- }
155
+ if (len < 10 || memcmp(line, "unpack ", 7))
156
+ return error("did not receive remote status");
157
if (memcmp(line, "unpack ok\n", 10)) {
182
- fputs(line, stderr);
158
+ char *p = line + strlen(line) - 1;
159
+ if (*p == '\n')
160
+ *p = '\0';
161
+ error("unpack failed: %s", line + 7);
162
ret = -1;
163
}
164
hint = NULL;
165
while (1) {
166
+ char *refname;
167
+ char *msg;
168
len = packet_read_line(in, line, sizeof(line));
169
if (!len)
170
break;
171
if (len < 3 ||
191
- (memcmp(line, "ok", 2) && memcmp(line, "ng", 2))) {
172
+ (memcmp(line, "ok ", 3) && memcmp(line, "ng ", 3))) {
173
fprintf(stderr, "protocol error: %s\n", line);
174
ret = -1;
175
break;
176
}
196
- if (!memcmp(line, "ok", 2))
197
- continue;
177
+
178
+ line[strlen(line)-1] = '\0';
179
+ refname = line + 3;
180
+ msg = strchr(refname, ' ');
181
+ if (msg)
182
+ *msg++ = '\0';
183
+
184
+ /* first try searching at our hint, falling back to all refs */
185
if (hint)
199
- hint = set_ref_error(hint, line + 3);
186
+ hint = find_ref_by_name(hint, refname);
187
if (!hint)
201
- hint = set_ref_error(refs, line + 3);
202
- ret = -1;
188
+ hint = find_ref_by_name(refs, refname);
189
+ if (!hint) {
190
+ warning("remote reported status on unknown ref: %s",
191
+ refname);
192
+ continue;
193
+ }
194
+ if (hint->status != REF_STATUS_EXPECTING_REPORT) {
195
+ warning("remote reported status on unexpected ref: %s",
196
+ refname);
197
+ continue;
198
+ }
199
+
200
+ if (line[0] == 'o' && line[1] == 'k')
201
+ hint->status = REF_STATUS_OK;
202
+ else {
203
+ hint->status = REF_STATUS_REMOTE_REJECT;
204
+ ret = -1;
205
+ }
206
+ if (msg)
207
+ hint->remote_status = xstrdup(msg);
208
+ /* start our next search from the next ref */
209
+ hint = hint->next;
210
}
211
return ret;
212
}
331
"non-fast forward");
332
break;
333
case REF_STATUS_REMOTE_REJECT:
327
- if (ref->deletion)
328
- print_ref_status('!', "[remote rejected]", ref, NULL, ref->error);
329
- else
330
- print_ref_status('!', "[remote rejected]", ref, ref->peer_ref, ref->error);
334
+ print_ref_status('!', "[remote rejected]", ref,
335
+ ref->deletion ? NULL : ref->peer_ref,
336
+ ref->remote_status);
337
+ break;
338
+ case REF_STATUS_EXPECTING_REPORT:
339
+ print_ref_status('!', "[remote failure]", ref,
340
+ ref->deletion ? NULL : ref->peer_ref,
341
+ "remote failed to report status");
342
break;
343
case REF_STATUS_OK:
344
print_ok_ref_status(ref);
445
hashcpy(ref->new_sha1, new_sha1);
446
if (!ref->deletion)
447
new_refs++;
437
- ref->status = REF_STATUS_OK;
448
449
if (!args.dry_run) {
450
char *old_hex = sha1_to_hex(ref->old_sha1);
461
packet_write(out, "%s %s %s",
462
old_hex, new_hex, ref->name);
463
}
464
+ ref->status = expect_status_report ?
465
+ REF_STATUS_EXPECTING_REPORT :
466
+ REF_STATUS_OK;
467
}
468
469
packet_flush(out);
475
}
476
close(out);
477
465
- if (expect_status_report) {
478
+ if (expect_status_report)
479
ret = receive_status(in, remote_refs);
467
- if (ret == -2)
468
- return -1;
469
- }
480
else
481
ret = 0;
482