Update tutorial for simplified "git" script.
[git.git] / rev-list.c
blob73b3e97452d6218b8958f291c08271f8146c4d3e
1 #include "cache.h"
2 #include "commit.h"
4 #define SEEN (1u << 0)
5 #define INTERESTING (1u << 1)
6 #define UNINTERESTING (1u << 2)
8 static const char rev_list_usage[] =
9 "usage: git-rev-list [OPTION] commit-id <commit-id>\n"
10 " --max-count=nr\n"
11 " --max-age=epoch\n"
12 " --min-age=epoch\n"
13 " --header\n"
14 " --pretty";
16 static void mark_parents_uninteresting(struct commit *commit)
18 struct commit_list *parents = commit->parents;
20 while (parents) {
21 struct commit *commit = parents->item;
22 commit->object.flags |= UNINTERESTING;
23 parents = parents->next;
27 static int everybody_uninteresting(struct commit_list *list)
29 while (list) {
30 struct commit *commit = list->item;
31 list = list->next;
32 if (commit->object.flags & UNINTERESTING)
33 continue;
34 return 0;
36 return 1;
39 int main(int argc, char **argv)
41 int nr_sha;
42 unsigned char sha1[2][20];
43 struct commit_list *list = NULL;
44 struct commit *commit, *end;
45 int i, verbose_header = 0, show_parents = 0, pretty_print = 0;
46 int hdr_termination = 0;
47 const char *prefix = "";
48 unsigned long max_age = -1;
49 unsigned long min_age = -1;
50 int max_count = -1;
52 nr_sha = 0;
53 for (i = 1 ; i < argc; i++) {
54 char *arg = argv[i];
56 if (!strncmp(arg, "--max-count=", 12)) {
57 max_count = atoi(arg + 12);
58 continue;
60 if (!strncmp(arg, "--max-age=", 10)) {
61 max_age = atoi(arg + 10);
62 continue;
64 if (!strncmp(arg, "--min-age=", 10)) {
65 min_age = atoi(arg + 10);
66 continue;
68 if (!strcmp(arg, "--header")) {
69 verbose_header = 1;
70 continue;
72 if (!strcmp(arg, "--pretty")) {
73 verbose_header = 1;
74 pretty_print = 1;
75 hdr_termination = '\n';
76 prefix = "commit ";
77 continue;
79 if (!strcmp(arg, "--parents")) {
80 show_parents = 1;
81 continue;
84 if (nr_sha > 2 || get_sha1(arg, sha1[nr_sha]))
85 usage(rev_list_usage);
86 nr_sha++;
89 if (!nr_sha)
90 usage(rev_list_usage);
92 commit = lookup_commit_reference(sha1[0]);
93 if (!commit || parse_commit(commit) < 0)
94 die("bad starting commit object");
96 end = NULL;
97 if (nr_sha > 1) {
98 end = lookup_commit_reference(sha1[1]);
99 if (!end || parse_commit(end) < 0)
100 die("bad ending commit object");
103 commit_list_insert(commit, &list);
104 if (end) {
105 struct commit_list *newlist = NULL;
106 struct commit_list **p = &newlist;
107 do {
108 struct commit *commit = pop_most_recent_commit(&list, SEEN);
109 struct object *obj = &commit->object;
111 if (commit == end || (obj->flags & UNINTERESTING)) {
112 mark_parents_uninteresting(commit);
113 if (everybody_uninteresting(list))
114 break;
115 continue;
117 p = &commit_list_insert(commit, p)->next;
118 } while (list);
119 list = newlist;
122 while (list) {
123 struct commit *commit = pop_most_recent_commit(&list, SEEN);
125 if (commit->object.flags & UNINTERESTING)
126 continue;
127 if (min_age != -1 && (commit->date > min_age))
128 continue;
129 if (max_age != -1 && (commit->date < max_age))
130 break;
131 if (max_count != -1 && !max_count--)
132 break;
133 printf("%s%s", prefix, sha1_to_hex(commit->object.sha1));
134 if (show_parents) {
135 struct commit_list *parents = commit->parents;
136 while (parents) {
137 printf(" %s", sha1_to_hex(parents->item->object.sha1));
138 parents = parents->next;
141 putchar('\n');
142 if (verbose_header) {
143 const char *buf = commit->buffer;
144 if (pretty_print) {
145 static char pretty_header[16384];
146 pretty_print_commit(commit->buffer, ~0, pretty_header, sizeof(pretty_header));
147 buf = pretty_header;
149 printf("%s%c", buf, hdr_termination);
152 return 0;