]> asedeno.scripts.mit.edu Git - git.git/blobdiff - pack-objects.c
git-pack-objects: add "--stdout" flag to write the pack file to stdout
[git.git] / pack-objects.c
index 102af3054bc27e864117663628c69a5beb8c26a5..e9764604f6f17f4eb018b4fd7247550e90583f8c 100644 (file)
@@ -4,23 +4,23 @@
 #include "delta.h"
 #include "csum-file.h"
 
-static const char pack_usage[] = "git-pack-objects [--window=N] [--depth=N] base-name < object-list";
-
-enum object_type {
-       OBJ_NONE,
-       OBJ_COMMIT,
-       OBJ_TREE,
-       OBJ_BLOB,
-       OBJ_DELTA
-};
+static const char pack_usage[] = "git-pack-objects [--window=N] [--depth=N] {--stdout | base-name} < object-list";
 
+/*
+ * The object type is a single-character shorthand:
+ *  - 'C' for "Commit"
+ *  - 'T' for "Tree"
+ *  - 'B' for "Blob"
+ *  - 'G' for "taG"
+ *  - 'D' for "Delta"
+ */
 struct object_entry {
        unsigned char sha1[20];
        unsigned long size;
        unsigned long offset;
        unsigned int depth;
        unsigned int hash;
-       enum object_type type;
+       unsigned char type;
        unsigned long delta_size;
        struct object_entry *delta;
 };
@@ -67,7 +67,7 @@ static unsigned long write_object(struct sha1file *f, struct object_entry *entry
         * length, except for deltas that has the 20 bytes of delta sha
         * instead.
         */
-       header[0] = ".CTB"[entry->type];
+       header[0] = entry->type;
        hdrlen = 5;
        if (entry->delta) {
                header[0] = 'D';
@@ -87,10 +87,14 @@ static unsigned long write_object(struct sha1file *f, struct object_entry *entry
 static void write_pack_file(void)
 {
        int i;
-       struct sha1file *f = sha1create("%s.%s", base_name, "pack");
+       struct sha1file *f;
        unsigned long offset = 0;
        unsigned long mb;
 
+       if (!base_name)
+               f = sha1fd(1, "<stdout>");
+       else
+               f = sha1create("%s.%s", base_name, "pack");
        for (i = 0; i < nr_objects; i++) {
                struct object_entry *entry = objects + i;
                entry->offset = offset;
@@ -160,28 +164,24 @@ static void add_object_entry(unsigned char *sha1, unsigned int hash)
 
 static void check_object(struct object_entry *entry)
 {
-       char buffer[128];
-       char type[10];
-       unsigned long mapsize;
-       z_stream stream;
-       void *map;
-
-       map = map_sha1_file(entry->sha1, &mapsize);
-       if (!map)
-               die("unable to map %s", sha1_to_hex(entry->sha1));
-       if (unpack_sha1_header(&stream, map, mapsize, buffer, sizeof(buffer)) < 0)
-               die("unable to unpack %s header", sha1_to_hex(entry->sha1));
-       munmap(map, mapsize);
-       if (parse_sha1_header(buffer, type, &entry->size) < 0)
-               die("unable to parse %s header", sha1_to_hex(entry->sha1));
-       if (!strcmp(type, "commit")) {
-               entry->type = OBJ_COMMIT;
-       } else if (!strcmp(type, "tree")) {
-               entry->type = OBJ_TREE;
-       } else if (!strcmp(type, "blob")) {
-               entry->type = OBJ_BLOB;
-       } else
-               die("unable to pack object %s of type %s", sha1_to_hex(entry->sha1), type);
+       char type[20];
+
+       if (!sha1_object_info(entry->sha1, type, &entry->size)) {
+               if (!strcmp(type, "commit")) {
+                       entry->type = 'C';
+               } else if (!strcmp(type, "tree")) {
+                       entry->type = 'T';
+               } else if (!strcmp(type, "blob")) {
+                       entry->type = 'B';
+               } else if (!strcmp(type, "tag")) {
+                       entry->type = 'G';
+               } else
+                       die("unable to pack object %s of type %s",
+                           sha1_to_hex(entry->sha1), type);
+       }
+       else
+               die("unable to get type of object %s",
+                   sha1_to_hex(entry->sha1));
 }
 
 static void get_object_details(void)
@@ -338,7 +338,7 @@ static void find_deltas(struct object_entry **list, int window, int depth)
 int main(int argc, char **argv)
 {
        char line[PATH_MAX + 20];
-       int window = 10, depth = 10;
+       int window = 10, depth = 10, pack_to_stdout = 0;
        int i;
 
        for (i = 1; i < argc; i++) {
@@ -359,6 +359,10 @@ int main(int argc, char **argv)
                                        usage(pack_usage);
                                continue;
                        }
+                       if (!strcmp("--stdout", arg)) {
+                               pack_to_stdout = 1;
+                               continue;
+                       }
                        usage(pack_usage);
                }
                if (base_name)
@@ -366,7 +370,7 @@ int main(int argc, char **argv)
                base_name = arg;
        }
 
-       if (!base_name)
+       if (pack_to_stdout != !base_name)
                usage(pack_usage);
 
        while (fgets(line, sizeof(line), stdin) != NULL) {
@@ -388,7 +392,7 @@ int main(int argc, char **argv)
        }
        get_object_details();
 
-       printf("Packing %d objects\n", nr_objects);
+       fprintf(stderr, "Packing %d objects\n", nr_objects);
 
        sorted_by_sha = create_sorted_list(sha1_sort);
        sorted_by_type = create_sorted_list(type_size_sort);
@@ -396,6 +400,7 @@ int main(int argc, char **argv)
                find_deltas(sorted_by_type, window+1, depth);
 
        write_pack_file();
-       write_index_file();
+       if (!pack_to_stdout)
+               write_index_file();
        return 0;
 }