ls-tree: major rewrite to do pathspec
[gitweb.git] / rev-list.c
index 5f125fdf29abeae752580392f4a9a455638acb58..e17f928061250c24465e1b96f89a18acb98a748c 100644 (file)
 #define TREECHANGE     (1u << 4)
 
 static const char rev_list_usage[] =
-       "git-rev-list [OPTION] commit-id <commit-id>\n"
-                     "  --max-count=nr\n"
-                     "  --max-age=epoch\n"
-                     "  --min-age=epoch\n"
-                     "  --parents\n"
-                     "  --bisect\n"
-                     "  --objects\n"
-                     "  --unpacked\n"
-                     "  --header\n"
-                     "  --pretty\n"
-                     "  --no-merges\n"
-                     "  --merge-order [ --show-breaks ]\n"
-                     "  --topo-order";
-
-static int dense = 0;
+"git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
+"  limiting output:\n"
+"    --max-count=nr\n"
+"    --max-age=epoch\n"
+"    --min-age=epoch\n"
+"    --sparse\n"
+"    --no-merges\n"
+"    --all\n"
+"  ordering output:\n"
+"    --merge-order [ --show-breaks ]\n"
+"    --topo-order\n"
+"  formatting output:\n"
+"    --parents\n"
+"    --objects\n"
+"    --unpacked\n"
+"    --header | --pretty\n"
+"  special purpose:\n"
+"    --bisect"
+;
+
+static int dense = 1;
 static int unpacked = 0;
 static int bisect_list = 0;
 static int tag_objects = 0;
@@ -81,23 +87,28 @@ static void show_commit(struct commit *commit)
        fflush(stdout);
 }
 
-static void rewrite_one(struct commit **pp)
+static int rewrite_one(struct commit **pp)
 {
        for (;;) {
                struct commit *p = *pp;
                if (p->object.flags & (TREECHANGE | UNINTERESTING))
-                       return;
-               /* Only single-parent commits don't have TREECHANGE */
+                       return 0;
+               if (!p->parents)
+                       return -1;
                *pp = p->parents->item;
        }
 }
 
 static void rewrite_parents(struct commit *commit)
 {
-       struct commit_list *parent = commit->parents;
-       while (parent) {
-               rewrite_one(&parent->item);
-               parent = parent->next;
+       struct commit_list **pp = &commit->parents;
+       while (*pp) {
+               struct commit_list *parent = *pp;
+               if (rewrite_one(&parent->item) < 0) {
+                       *pp = parent->next;
+                       continue;
+               }
+               pp = &parent->next;
        }
 }
 
@@ -113,8 +124,6 @@ static int filter_commit(struct commit * commit)
                stop_traversal=1;
                return CONTINUE;
        }
-       if (max_count != -1 && !max_count--)
-               return STOP;
        if (no_merges && (commit->parents && commit->parents->next))
                return CONTINUE;
        if (paths && dense) {
@@ -137,6 +146,9 @@ static int process_commit(struct commit * commit)
                return CONTINUE;
        }
 
+       if (max_count != -1 && !max_count--)
+               return STOP;
+
        show_commit(commit);
 
        return CONTINUE;
@@ -439,6 +451,30 @@ static int same_tree(struct tree *t1, struct tree *t2)
        return !is_different;
 }
 
+static int same_tree_as_empty(struct tree *t1)
+{
+       int retval;
+       void *tree;
+       struct tree_desc empty, real;
+
+       if (!t1)
+               return 0;
+
+       tree = read_object_with_reference(t1->object.sha1, "tree", &real.size, NULL);
+       if (!tree)
+               return 0;
+       real.buf = tree;
+
+       empty.buf = "";
+       empty.size = 0;
+
+       is_different = 0;
+       retval = diff_tree(&empty, &real, "", &diff_opt);
+       free(tree);
+
+       return retval >= 0 && !is_different;
+}
+
 static struct commit *try_to_simplify_merge(struct commit *commit, struct commit_list *parent)
 {
        if (!commit->tree)
@@ -523,11 +559,17 @@ static void compress_list(struct commit_list *list)
                struct commit_list *parent = commit->parents;
                list = list->next;
 
+               if (!parent) {
+                       if (!same_tree_as_empty(commit->tree))
+                               commit->object.flags |= TREECHANGE;
+                       continue;
+               }
+
                /*
                 * Exactly one parent? Check if it leaves the tree
                 * unchanged
                 */
-               if (parent && !parent->next) {
+               if (!parent->next) {
                        struct tree *t1 = commit->tree;
                        struct tree *t2 = parent->item->tree;
                        if (!t1 || !t2 || same_tree(t1, t2))
@@ -578,13 +620,10 @@ static void add_pending_object(struct object *obj, const char *name)
        add_object(obj, &pending_objects, name);
 }
 
-static struct commit *get_commit_reference(const char *name, unsigned int flags)
+static struct commit *get_commit_reference(const char *name, const unsigned char *sha1, unsigned int flags)
 {
-       unsigned char sha1[20];
        struct object *object;
 
-       if (get_sha1(name, sha1))
-               usage(rev_list_usage);
        object = parse_object(sha1);
        if (!object)
                die("bad object %s", name);
@@ -662,7 +701,7 @@ static struct commit_list **global_lst;
 
 static int include_one_commit(const char *path, const unsigned char *sha1)
 {
-       struct commit *com = get_commit_reference(path, 0);
+       struct commit *com = get_commit_reference(path, sha1, 0);
        handle_one_commit(com, global_lst);
        return 0;
 }
@@ -685,6 +724,7 @@ int main(int argc, const char **argv)
                const char *arg = argv[i];
                char *dotdot;
                struct commit *commit;
+               unsigned char sha1[20];
 
                if (!strncmp(arg, "--max-count=", 12)) {
                        max_count = atoi(arg + 12);
@@ -758,12 +798,12 @@ int main(int argc, const char **argv)
                        dense = 1;
                        continue;
                }
+               if (!strcmp(arg, "--sparse")) {
+                       dense = 0;
+                       continue;
+               }
                if (!strcmp(arg, "--")) {
-                       paths = get_pathspec(prefix, argv + i + 1);
-                       if (paths) {
-                               limited = 1;
-                               diff_tree_setup_paths(paths);
-                       }
+                       i++;
                        break;
                }
 
@@ -773,15 +813,19 @@ int main(int argc, const char **argv)
                flags = 0;
                dotdot = strstr(arg, "..");
                if (dotdot) {
+                       unsigned char from_sha1[20];
                        char *next = dotdot + 2;
-                       struct commit *exclude = NULL;
-                       struct commit *include = NULL;
                        *dotdot = 0;
                        if (!*next)
                                next = "HEAD";
-                       exclude = get_commit_reference(arg, UNINTERESTING);
-                       include = get_commit_reference(next, 0);
-                       if (exclude && include) {
+                       if (!get_sha1(arg, from_sha1) && !get_sha1(next, sha1)) {
+                               struct commit *exclude;
+                               struct commit *include;
+                               
+                               exclude = get_commit_reference(arg, from_sha1, UNINTERESTING);
+                               include = get_commit_reference(next, sha1, 0);
+                               if (!exclude || !include)
+                                       die("Invalid revision range %s..%s", arg, next);
                                limited = 1;
                                handle_one_commit(exclude, &list);
                                handle_one_commit(include, &list);
@@ -794,10 +838,21 @@ int main(int argc, const char **argv)
                        arg++;
                        limited = 1;
                }
-               commit = get_commit_reference(arg, flags);
+               if (get_sha1(arg, sha1) < 0)
+                       break;
+               commit = get_commit_reference(arg, sha1, flags);
                handle_one_commit(commit, &list);
        }
 
+       if (!list)
+               usage(rev_list_usage);
+
+       paths = get_pathspec(prefix, argv + i);
+       if (paths) {
+               limited = 1;
+               diff_tree_setup_paths(paths);
+       }
+
        save_commit_buffer = verbose_header;
        track_object_refs = 0;