Make report() from usage.c public as vreportf() and use it.
[gitweb.git] / builtin-pack-objects.c
index dcfe62aa02ca69cad39b4dd681a60f052b20aba5..6b2f65c6db833df59f8b7ab50244f630a828c9a1 100644 (file)
@@ -154,33 +154,6 @@ static unsigned long do_compress(void **pptr, unsigned long size)
        return stream.total_out;
 }
 
-/*
- * The per-object header is a pretty dense thing, which is
- *  - first byte: low four bits are "size", then three bits of "type",
- *    and the high bit is "size continues".
- *  - each byte afterwards: low seven bits are size continuation,
- *    with the high bit being "size continues"
- */
-static int encode_header(enum object_type type, unsigned long size, unsigned char *hdr)
-{
-       int n = 1;
-       unsigned char c;
-
-       if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
-               die("bad type %d", type);
-
-       c = (type << 4) | (size & 15);
-       size >>= 4;
-       while (size) {
-               *hdr++ = c | 0x80;
-               c = size & 0x7f;
-               size >>= 7;
-               n++;
-       }
-       *hdr = c;
-       return n;
-}
-
 /*
  * we are going to reuse the existing object data as is.  make
  * sure it is not corrupt.
@@ -321,7 +294,7 @@ static unsigned long write_object(struct sha1file *f,
                 * The object header is a byte of 'type' followed by zero or
                 * more bytes of length.
                 */
-               hdrlen = encode_header(type, size, header);
+               hdrlen = encode_in_pack_object_header(type, size, header);
 
                if (type == OBJ_OFS_DELTA) {
                        /*
@@ -372,7 +345,7 @@ static unsigned long write_object(struct sha1file *f,
                if (entry->delta)
                        type = (allow_ofs_delta && entry->delta->idx.offset) ?
                                OBJ_OFS_DELTA : OBJ_REF_DELTA;
-               hdrlen = encode_header(type, entry->size, header);
+               hdrlen = encode_in_pack_object_header(type, entry->size, header);
 
                offset = entry->in_pack_offset;
                revidx = find_pack_revindex(p, offset);
@@ -445,13 +418,9 @@ static int write_one(struct sha1file *f,
        if (e->idx.offset || e->preferred_base)
                return -1;
 
-       /*
-        * If we are deltified, attempt to write out base object first.
-        * If that fails due to the pack size limit then the current
-        * object might still possibly fit undeltified within that limit.
-        */
-       if (e->delta)
-              write_one(f, e->delta, offset);
+       /* if we are deltified, write out base object first. */
+       if (e->delta && !write_one(f, e->delta, offset))
+               return 0;
 
        e->idx.offset = *offset;
        size = write_object(f, e, *offset);
@@ -505,9 +474,11 @@ static void write_pack_file(void)
                sha1write(f, &hdr, sizeof(hdr));
                offset = sizeof(hdr);
                nr_written = 0;
-               for (i = 0; i < nr_objects; i++)
-                       if (write_one(f, objects + i, &offset) == 1)
-                               display_progress(progress_state, written);
+               for (; i < nr_objects; i++) {
+                       if (!write_one(f, objects + i, &offset))
+                               break;
+                       display_progress(progress_state, written);
+               }
 
                /*
                 * Did we write the wrong # entries in the header?
@@ -582,7 +553,7 @@ static void write_pack_file(void)
                        written_list[j]->offset = (off_t)-1;
                }
                nr_remaining -= nr_written;
-       } while (nr_remaining);
+       } while (nr_remaining && i < nr_objects);
 
        free(written_list);
        stop_progress(&progress_state);