git-rev-list: add "end" commit and "--header" flag
[git.git] / rev-list.c
1 #include "cache.h"
2 #include "commit.h"
3
4 static const char rev_list_usage[] =
5         "usage: git-rev-list [OPTION] commit-id <commit-id>\n"
6                       "  --max-count=nr\n"
7                       "  --max-age=epoch\n"
8                       "  --min-age=epoch\n"
9                       "  --header";
10
11 int main(int argc, char **argv)
12 {
13         int nr_sha;
14         unsigned char sha1[2][20];
15         struct commit_list *list = NULL;
16         struct commit *commit, *end;
17         int i, verbose_header = 0;
18         unsigned long max_age = -1;
19         unsigned long min_age = -1;
20         int max_count = -1;
21
22         nr_sha = 0;
23         for (i = 1 ; i < argc; i++) {
24                 char *arg = argv[i];
25
26                 if (!strncmp(arg, "--max-count=", 12)) {
27                         max_count = atoi(arg + 12);
28                         continue;
29                 }
30                 if (!strncmp(arg, "--max-age=", 10)) {
31                         max_age = atoi(arg + 10);
32                         continue;
33                 }
34                 if (!strncmp(arg, "--min-age=", 10)) {
35                         min_age = atoi(arg + 10);
36                         continue;
37                 }
38                 if (!strcmp(arg, "--header")) {
39                         verbose_header = 1;
40                         continue;
41                 }
42
43                 if (nr_sha > 2 || get_sha1(arg, sha1[nr_sha]))
44                         usage(rev_list_usage);
45                 nr_sha++;
46         }
47
48         if (!nr_sha)
49                 usage(rev_list_usage);
50
51         commit = lookup_commit_reference(sha1[0]);
52         if (!commit || parse_commit(commit) < 0)
53                 die("bad starting commit object");
54
55         end = NULL;
56         if (nr_sha > 1) {
57                 end = lookup_commit_reference(sha1[1]);
58                 if (!end || parse_commit(end) < 0)
59                         die("bad ending commit object");
60         }
61
62         commit_list_insert(commit, &list);
63         do {
64                 struct commit *commit = pop_most_recent_commit(&list, 0x1);
65
66                 if (commit == end)
67                         break;
68                 if (min_age != -1 && (commit->date > min_age))
69                         continue;
70                 if (max_age != -1 && (commit->date < max_age))
71                         break;
72                 if (max_count != -1 && !max_count--)
73                         break;
74                 printf("%s\n", sha1_to_hex(commit->object.sha1));
75                 if (verbose_header)
76                         printf("%s%c", commit->buffer, 0);
77         } while (list);
78         return 0;
79 }