sha1_to_hex: properly terminate the SHA1
[git.git] / rev-list.c
index ac7a47f..d060966 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";
+"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;
@@ -118,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) {
@@ -142,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;
@@ -343,7 +350,8 @@ static int count_distance(struct commit_list *entry)
 
                if (commit->object.flags & (UNINTERESTING | COUNTED))
                        break;
-               nr++;
+               if (!paths || (commit->object.flags & TREECHANGE))
+                       nr++;
                commit->object.flags |= COUNTED;
                p = commit->parents;
                entry = p;
@@ -355,6 +363,7 @@ static int count_distance(struct commit_list *entry)
                        }
                }
        }
+
        return nr;
 }
 
@@ -375,15 +384,20 @@ static struct commit_list *find_bisection(struct commit_list *list)
        nr = 0;
        p = list;
        while (p) {
-               nr++;
+               if (!paths || (p->item->object.flags & TREECHANGE))
+                       nr++;
                p = p->next;
        }
        closest = 0;
        best = list;
 
-       p = list;
-       while (p) {
-               int distance = count_distance(p);
+       for (p = list; p; p = p->next) {
+               int distance;
+
+               if (paths && !(p->item->object.flags & TREECHANGE))
+                       continue;
+
+               distance = count_distance(p);
                clear_distance(list);
                if (nr - distance < distance)
                        distance = nr - distance;
@@ -391,7 +405,6 @@ static struct commit_list *find_bisection(struct commit_list *list)
                        best = p;
                        closest = distance;
                }
-               p = p->next;
        }
        if (best)
                best->next = NULL;
@@ -613,13 +626,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))
-               return NULL;
        object = parse_object(sha1);
        if (!object)
                die("bad object %s", name);
@@ -697,7 +707,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;
 }
@@ -720,6 +730,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);
@@ -808,15 +819,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);
@@ -829,13 +844,14 @@ int main(int argc, const char **argv)
                        arg++;
                        limited = 1;
                }
-               commit = get_commit_reference(arg, flags);
-               if (!commit)
+               if (get_sha1(arg, sha1) < 0)
                        break;
+               commit = get_commit_reference(arg, sha1, flags);
                handle_one_commit(commit, &list);
        }
 
-       if (!list)
+       if (!list &&
+           (!(tag_objects||tree_objects||blob_objects) && !pending_objects))
                usage(rev_list_usage);
 
        paths = get_pathspec(prefix, argv + i);