Separate object listing routines out of rev-list
authorJunio C Hamano <junkio@cox.net>
Tue, 5 Sep 2006 04:50:12 +0000 (21:50 -0700)
committerJunio C Hamano <junkio@cox.net>
Thu, 7 Sep 2006 09:46:01 +0000 (02:46 -0700)
Create a separate file, list-objects.c, and move object listing
routines from rev-list to it.  The next round will use it in
pack-objects directly.

Signed-off-by: Junio C Hamano <junkio@cox.net>
Makefile
builtin-rev-list.c
list-objects.c [new file with mode: 0644]
list-objects.h [new file with mode: 0644]

index 7b3114f3aac8f040b2d7f291e62a980e282b722e..18cd79e7c065df505d412e9490585ef4f769309b 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -233,7 +233,7 @@ XDIFF_LIB=xdiff/lib.a
 
 LIB_H = \
        blob.h cache.h commit.h csum-file.h delta.h \
-       diff.h object.h pack.h pkt-line.h quote.h refs.h \
+       diff.h object.h pack.h pkt-line.h quote.h refs.h list-objects.h \
        run-command.h strbuf.h tag.h tree.h git-compat-util.h revision.h \
        tree-walk.h log-tree.h dir.h path-list.h unpack-trees.h builtin.h
 
@@ -250,7 +250,7 @@ LIB_OBJS = \
        server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \
        tag.o tree.o usage.o config.o environment.o ctype.o copy.o \
        fetch-clone.o revision.o pager.o tree-walk.o xdiff-interface.o \
-       write_or_die.o trace.o \
+       write_or_die.o trace.o list-objects.o \
        alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS)
 
 BUILTIN_OBJS = \
index 8fe8afbd0469c689cad111df6302508d352b6c29..0900737f40c5d079140f3e3d2835b8d884753a06 100644 (file)
@@ -7,6 +7,7 @@
 #include "tree-walk.h"
 #include "diff.h"
 #include "revision.h"
+#include "list-objects.h"
 #include "builtin.h"
 
 /* bits #0-15 in revision.h */
@@ -98,104 +99,19 @@ static void show_commit(struct commit *commit)
        commit->buffer = NULL;
 }
 
-static void process_blob(struct blob *blob,
-                        struct object_array *p,
-                        struct name_path *path,
-                        const char *name)
+static void show_object(struct object_array_entry *p)
 {
-       struct object *obj = &blob->object;
-
-       if (!revs.blob_objects)
-               return;
-       if (obj->flags & (UNINTERESTING | SEEN))
-               return;
-       obj->flags |= SEEN;
-       name = xstrdup(name);
-       add_object(obj, p, path, name);
-}
-
-static void process_tree(struct tree *tree,
-                        struct object_array *p,
-                        struct name_path *path,
-                        const char *name)
-{
-       struct object *obj = &tree->object;
-       struct tree_desc desc;
-       struct name_entry entry;
-       struct name_path me;
-
-       if (!revs.tree_objects)
-               return;
-       if (obj->flags & (UNINTERESTING | SEEN))
-               return;
-       if (parse_tree(tree) < 0)
-               die("bad tree object %s", sha1_to_hex(obj->sha1));
-       obj->flags |= SEEN;
-       name = xstrdup(name);
-       add_object(obj, p, path, name);
-       me.up = path;
-       me.elem = name;
-       me.elem_len = strlen(name);
-
-       desc.buf = tree->buffer;
-       desc.size = tree->size;
-
-       while (tree_entry(&desc, &entry)) {
-               if (S_ISDIR(entry.mode))
-                       process_tree(lookup_tree(entry.sha1), p, &me, entry.path);
-               else
-                       process_blob(lookup_blob(entry.sha1), p, &me, entry.path);
-       }
-       free(tree->buffer);
-       tree->buffer = NULL;
-}
-
-static void show_commit_list(struct rev_info *revs)
-{
-       int i;
-       struct commit *commit;
-       struct object_array objects = { 0, 0, NULL };
-
-       while ((commit = get_revision(revs)) != NULL) {
-               process_tree(commit->tree, &objects, NULL, "");
-               show_commit(commit);
-       }
-       for (i = 0; i < revs->pending.nr; i++) {
-               struct object_array_entry *pending = revs->pending.objects + i;
-               struct object *obj = pending->item;
-               const char *name = pending->name;
-               if (obj->flags & (UNINTERESTING | SEEN))
-                       continue;
-               if (obj->type == OBJ_TAG) {
-                       obj->flags |= SEEN;
-                       add_object_array(obj, name, &objects);
-                       continue;
-               }
-               if (obj->type == OBJ_TREE) {
-                       process_tree((struct tree *)obj, &objects, NULL, name);
-                       continue;
-               }
-               if (obj->type == OBJ_BLOB) {
-                       process_blob((struct blob *)obj, &objects, NULL, name);
-                       continue;
-               }
-               die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
-       }
-       for (i = 0; i < objects.nr; i++) {
-               struct object_array_entry *p = objects.objects + i;
-
-               /* An object with name "foo\n0000000..." can be used to
-                * confuse downstream git-pack-objects very badly.
-                */
-               const char *ep = strchr(p->name, '\n');
-               if (ep) {
-                       printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
-                              (int) (ep - p->name),
-                              p->name);
-               }
-               else
-                       printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
+       /* An object with name "foo\n0000000..." can be used to
+        * confuse downstream git-pack-objects very badly.
+        */
+       const char *ep = strchr(p->name, '\n');
+       if (ep) {
+               printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
+                      (int) (ep - p->name),
+                      p->name);
        }
+       else
+               printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
 }
 
 /*
@@ -389,7 +305,7 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
        if (bisect_list)
                revs.commits = find_bisection(revs.commits);
 
-       show_commit_list(&revs);
+       traverse_commit_list(&revs, show_commit, show_object);
 
        return 0;
 }
diff --git a/list-objects.c b/list-objects.c
new file mode 100644 (file)
index 0000000..adaf997
--- /dev/null
@@ -0,0 +1,107 @@
+#include "cache.h"
+#include "tag.h"
+#include "commit.h"
+#include "tree.h"
+#include "blob.h"
+#include "diff.h"
+#include "tree-walk.h"
+#include "revision.h"
+#include "list-objects.h"
+
+static void process_blob(struct rev_info *revs,
+                        struct blob *blob,
+                        struct object_array *p,
+                        struct name_path *path,
+                        const char *name)
+{
+       struct object *obj = &blob->object;
+
+       if (!revs->blob_objects)
+               return;
+       if (obj->flags & (UNINTERESTING | SEEN))
+               return;
+       obj->flags |= SEEN;
+       name = xstrdup(name);
+       add_object(obj, p, path, name);
+}
+
+static void process_tree(struct rev_info *revs,
+                        struct tree *tree,
+                        struct object_array *p,
+                        struct name_path *path,
+                        const char *name)
+{
+       struct object *obj = &tree->object;
+       struct tree_desc desc;
+       struct name_entry entry;
+       struct name_path me;
+
+       if (!revs->tree_objects)
+               return;
+       if (obj->flags & (UNINTERESTING | SEEN))
+               return;
+       if (parse_tree(tree) < 0)
+               die("bad tree object %s", sha1_to_hex(obj->sha1));
+       obj->flags |= SEEN;
+       name = xstrdup(name);
+       add_object(obj, p, path, name);
+       me.up = path;
+       me.elem = name;
+       me.elem_len = strlen(name);
+
+       desc.buf = tree->buffer;
+       desc.size = tree->size;
+
+       while (tree_entry(&desc, &entry)) {
+               if (S_ISDIR(entry.mode))
+                       process_tree(revs,
+                                    lookup_tree(entry.sha1),
+                                    p, &me, entry.path);
+               else
+                       process_blob(revs,
+                                    lookup_blob(entry.sha1),
+                                    p, &me, entry.path);
+       }
+       free(tree->buffer);
+       tree->buffer = NULL;
+}
+
+void traverse_commit_list(struct rev_info *revs,
+                         void (*show_commit)(struct commit *),
+                         void (*show_object)(struct object_array_entry *))
+{
+       int i;
+       struct commit *commit;
+       struct object_array objects = { 0, 0, NULL };
+
+       while ((commit = get_revision(revs)) != NULL) {
+               process_tree(revs, commit->tree, &objects, NULL, "");
+               show_commit(commit);
+       }
+       for (i = 0; i < revs->pending.nr; i++) {
+               struct object_array_entry *pending = revs->pending.objects + i;
+               struct object *obj = pending->item;
+               const char *name = pending->name;
+               if (obj->flags & (UNINTERESTING | SEEN))
+                       continue;
+               if (obj->type == OBJ_TAG) {
+                       obj->flags |= SEEN;
+                       add_object_array(obj, name, &objects);
+                       continue;
+               }
+               if (obj->type == OBJ_TREE) {
+                       process_tree(revs, (struct tree *)obj, &objects,
+                                    NULL, name);
+                       continue;
+               }
+               if (obj->type == OBJ_BLOB) {
+                       process_blob(revs, (struct blob *)obj, &objects,
+                                    NULL, name);
+                       continue;
+               }
+               die("unknown pending object %s (%s)",
+                   sha1_to_hex(obj->sha1), name);
+       }
+       for (i = 0; i < objects.nr; i++)
+               show_object(&objects.objects[i]);
+}
diff --git a/list-objects.h b/list-objects.h
new file mode 100644 (file)
index 0000000..8a5fae6
--- /dev/null
@@ -0,0 +1,8 @@
+#ifndef LIST_OBJECTS_H
+#define LIST_OBJECTS_H
+
+void traverse_commit_list(struct rev_info *revs,
+                         void (*show_commit)(struct commit *),
+                         void (*show_object)(struct object_array_entry *));
+
+#endif