+static int compare_seen(const void *s1, const void *s2)
+{
+ return memcmp(s1, s2, 20);
+}
+
+static int lookup_seen(unsigned char *sha1, char *tag)
+{
+ int first = 0, last = nr_seen;
+
+ while (last > first) {
+ int next = (last + first) / 2;
+ struct seen *s = seen + next;
+ int cmp = memcmp(sha1, s->sha1, 20);
+
+ if (cmp < 0) {
+ last = next;
+ continue;
+ }
+ if (cmp > 0) {
+ first = next+1;
+ continue;
+ }
+ if (strcmp(tag, s->tag))
+ break;
+ s->needed++;
+ return 1;
+ }
+ return 0;
+}
+
+static void check_connectivity(void)
+{
+ int i;
+
+ /* Sort the "seen" tags for quicker lookup */
+ qsort(seen, nr_seen, sizeof(struct seen), compare_seen);
+
+ /* Look up all the requirements, warn about missing objects.. */
+ for (i = 0; i < nr_needs; i++) {
+ struct needs *n = needs + i;
+ char hex[60];
+
+ if (lookup_seen(n->needs, n->tag))
+ continue;
+ strcpy(hex, sha1_to_hex(n->parent));
+ }
+
+ /* Tell the user about things not referenced.. */
+ for (i = 0; i < nr_seen; i++) {
+ struct seen *s = seen + i;
+
+ if (s->needed)
+ continue;
+ printf("unreferenced %s: %s\n", s->tag, sha1_to_hex(s->sha1));
+ }
+}
+
+static void mark_needs_sha1(unsigned char *parent, const char * tag, unsigned char *child)