+ " --parents\n"
+ " --bisect\n"
+ " --objects\n"
+ " --unpacked\n"
+ " --header\n"
+ " --pretty\n"
+ " --no-merges\n"
+ " --merge-order [ --show-breaks ]\n"
+ " --topo-order";
+
+static int unpacked = 0;
+static int bisect_list = 0;
+static int tag_objects = 0;
+static int tree_objects = 0;
+static int blob_objects = 0;
+static int verbose_header = 0;
+static int show_parents = 0;
+static int hdr_termination = 0;
+static const char *commit_prefix = "";
+static unsigned long max_age = -1;
+static unsigned long min_age = -1;
+static int max_count = -1;
+static enum cmit_fmt commit_format = CMIT_FMT_RAW;
+static int merge_order = 0;
+static int show_breaks = 0;
+static int stop_traversal = 0;
+static int topo_order = 0;
+static int no_merges = 0;
+
+static void show_commit(struct commit *commit)
+{
+ commit->object.flags |= SHOWN;
+ if (show_breaks) {
+ commit_prefix = "| ";
+ if (commit->object.flags & DISCONTINUITY) {
+ commit_prefix = "^ ";
+ } else if (commit->object.flags & BOUNDARY) {
+ commit_prefix = "= ";
+ }
+ }
+ printf("%s%s", commit_prefix, sha1_to_hex(commit->object.sha1));
+ if (show_parents) {
+ struct commit_list *parents = commit->parents;
+ while (parents) {
+ printf(" %s", sha1_to_hex(parents->item->object.sha1));
+ parents = parents->next;
+ }
+ }
+ if (commit_format == CMIT_FMT_ONELINE)
+ putchar(' ');
+ else
+ putchar('\n');
+
+ if (verbose_header) {
+ static char pretty_header[16384];
+ pretty_print_commit(commit_format, commit->buffer, ~0, pretty_header, sizeof(pretty_header));
+ printf("%s%c", pretty_header, hdr_termination);
+ }
+ fflush(stdout);
+}
+
+static int filter_commit(struct commit * commit)
+{
+ if (stop_traversal && (commit->object.flags & BOUNDARY))
+ return STOP;
+ if (commit->object.flags & (UNINTERESTING|SHOWN))
+ return CONTINUE;
+ if (min_age != -1 && (commit->date > min_age))
+ return CONTINUE;
+ if (max_age != -1 && (commit->date < max_age)) {
+ stop_traversal=1;
+ return CONTINUE;
+ }
+ if (max_count != -1 && !max_count--)
+ return STOP;
+ if (no_merges && (commit->parents && commit->parents->next))
+ return CONTINUE;
+ return DO;
+}
+
+static int process_commit(struct commit * commit)
+{
+ int action=filter_commit(commit);
+
+ if (action == STOP) {
+ return STOP;
+ }
+
+ if (action == CONTINUE) {
+ return CONTINUE;
+ }
+
+ show_commit(commit);
+
+ return CONTINUE;
+}
+
+static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
+{
+ struct object_list *entry = xmalloc(sizeof(*entry));
+ entry->item = obj;
+ entry->next = *p;
+ entry->name = name;
+ *p = entry;
+ return &entry->next;
+}
+
+static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
+{
+ struct object *obj = &blob->object;
+
+ if (!blob_objects)
+ return p;
+ if (obj->flags & (UNINTERESTING | SEEN))
+ return p;
+ obj->flags |= SEEN;
+ return add_object(obj, p, name);
+}
+
+static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
+{
+ struct object *obj = &tree->object;
+ struct tree_entry_list *entry;
+
+ if (!tree_objects)
+ return p;
+ if (obj->flags & (UNINTERESTING | SEEN))
+ return p;
+ if (parse_tree(tree) < 0)
+ die("bad tree object %s", sha1_to_hex(obj->sha1));
+ obj->flags |= SEEN;
+ p = add_object(obj, p, name);
+ entry = tree->entries;
+ tree->entries = NULL;
+ while (entry) {
+ struct tree_entry_list *next = entry->next;
+ if (entry->directory)
+ p = process_tree(entry->item.tree, p, entry->name);
+ else
+ p = process_blob(entry->item.blob, p, entry->name);
+ free(entry);
+ entry = next;
+ }
+ return p;
+}
+
+static struct object_list *pending_objects = NULL;
+
+static void show_commit_list(struct commit_list *list)
+{
+ struct object_list *objects = NULL, **p = &objects, *pending;
+ while (list) {
+ struct commit *commit = pop_most_recent_commit(&list, SEEN);
+
+ p = process_tree(commit->tree, p, "");
+ if (process_commit(commit) == STOP)
+ break;
+ }
+ for (pending = pending_objects; pending; pending = pending->next) {
+ struct object *obj = pending->item;
+ const char *name = pending->name;
+ if (obj->flags & (UNINTERESTING | SEEN))
+ continue;
+ if (obj->type == tag_type) {
+ obj->flags |= SEEN;
+ p = add_object(obj, p, name);
+ continue;
+ }
+ if (obj->type == tree_type) {
+ p = process_tree((struct tree *)obj, p, name);
+ continue;
+ }
+ if (obj->type == blob_type) {
+ p = process_blob((struct blob *)obj, p, name);
+ continue;
+ }
+ die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
+ }
+ while (objects) {
+ /* An object with name "foo\n0000000000000000000000000000000000000000"
+ * can be used confuse downstream git-pack-objects very badly.
+ */
+ const char *ep = strchr(objects->name, '\n');
+ if (ep) {
+ printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
+ (int) (ep - objects->name),
+ objects->name);
+ }
+ else
+ printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
+ objects = objects->next;
+ }
+}
+
+static void mark_blob_uninteresting(struct blob *blob)
+{
+ if (!blob_objects)
+ return;
+ if (blob->object.flags & UNINTERESTING)
+ return;
+ blob->object.flags |= UNINTERESTING;
+}
+
+static void mark_tree_uninteresting(struct tree *tree)
+{
+ struct object *obj = &tree->object;
+ struct tree_entry_list *entry;
+
+ if (!tree_objects)
+ return;
+ if (obj->flags & UNINTERESTING)
+ return;
+ obj->flags |= UNINTERESTING;
+ if (!has_sha1_file(obj->sha1))
+ return;
+ if (parse_tree(tree) < 0)
+ die("bad tree %s", sha1_to_hex(obj->sha1));
+ entry = tree->entries;
+ tree->entries = NULL;
+ while (entry) {
+ struct tree_entry_list *next = entry->next;
+ if (entry->directory)
+ mark_tree_uninteresting(entry->item.tree);
+ else
+ mark_blob_uninteresting(entry->item.blob);
+ free(entry);
+ entry = next;
+ }
+}