+ struct ref *ref_list = NULL, **last_ref = &ref_list;
+ struct ref *ref;
+ int new_refs;
+
+ /*
+ * Read all the refs from the other end
+ */
+ for (;;) {
+ unsigned char old_sha1[20];
+ static char buffer[1000];
+ char *name;
+ int len;
+
+ len = packet_read_line(in, buffer, sizeof(buffer));
+ if (!len)
+ break;
+ if (buffer[len-1] == '\n')
+ buffer[--len] = 0;
+
+ if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
+ die("protocol error: expected sha/ref, got '%s'", buffer);
+ name = buffer + 41;
+ ref = xmalloc(sizeof(*ref) + len - 40);
+ memcpy(ref->old_sha1, old_sha1, 20);
+ memset(ref->new_sha1, 0, 20);
+ memcpy(ref->name, buffer + 41, len - 40);
+ ref->next = NULL;
+ *last_ref = ref;
+ last_ref = &ref->next;
+ }
+
+ /*
+ * Go through the refs, see if we want to update
+ * any of them..
+ */
+ for (ref = ref_list; ref; ref = ref->next) {
+ unsigned char new_sha1[20];
+ char *name = ref->name;
+
+ if (nr_match && !path_match(name, nr_match, match))
+ continue;
+
+ if (read_ref(name, new_sha1) < 0)
+ continue;
+
+ if (!memcmp(ref->old_sha1, new_sha1, 20)) {
+ fprintf(stderr, "'%s' unchanged\n", name);
+ continue;
+ }
+
+ if (!ref_newer(new_sha1, ref->old_sha1)) {
+ error("remote '%s' points to object I don't have", name);
+ continue;
+ }
+
+ /* Ok, mark it for update */
+ memcpy(ref->new_sha1, new_sha1, 20);
+ }
+
+ /*
+ * See if we have any refs that the other end didn't have
+ */
+ if (nr_match) {
+ local_ref_nr_match = nr_match;
+ local_ref_match = match;
+ local_ref_list = last_ref;
+ for_each_ref(try_to_match);