[PATCH] ssh-protocol version, command types, response code
[git/git.git] / rev-list.c
CommitLineData
64745109
LT
1#include "cache.h"
2#include "commit.h"
3
8906300f
LT
4#define SEEN (1u << 0)
5#define INTERESTING (1u << 1)
6#define UNINTERESTING (1u << 2)
7
a6f68d47
LT
8static 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"
9d97aa64
LT
13 " --header\n"
14 " --pretty";
a6f68d47 15
81f2bb1f
LT
16static int verbose_header = 0;
17static int show_parents = 0;
18static int pretty_print = 0;
19static int hdr_termination = 0;
20static const char *prefix = "";
21static unsigned long max_age = -1;
22static unsigned long min_age = -1;
23static int max_count = -1;
24
25static void show_commit(struct commit *commit)
26{
27 printf("%s%s", prefix, sha1_to_hex(commit->object.sha1));
28 if (show_parents) {
29 struct commit_list *parents = commit->parents;
30 while (parents) {
31 printf(" %s", sha1_to_hex(parents->item->object.sha1));
32 parents = parents->next;
33 }
34 }
35 putchar('\n');
36 if (verbose_header) {
37 const char *buf = commit->buffer;
38 if (pretty_print) {
39 static char pretty_header[16384];
40 pretty_print_commit(commit->buffer, ~0, pretty_header, sizeof(pretty_header));
41 buf = pretty_header;
42 }
43 printf("%s%c", buf, hdr_termination);
44 }
45}
46
47static void show_commit_list(struct commit_list *list)
48{
49 while (list) {
50 struct commit *commit = pop_most_recent_commit(&list, SEEN);
51
52 if (commit->object.flags & UNINTERESTING)
53 continue;
54 if (min_age != -1 && (commit->date > min_age))
55 continue;
56 if (max_age != -1 && (commit->date < max_age))
57 break;
58 if (max_count != -1 && !max_count--)
59 break;
60 show_commit(commit);
61 }
62}
63
8906300f
LT
64static void mark_parents_uninteresting(struct commit *commit)
65{
66 struct commit_list *parents = commit->parents;
67
68 while (parents) {
69 struct commit *commit = parents->item;
70 commit->object.flags |= UNINTERESTING;
71 parents = parents->next;
72 }
73}
74
75static int everybody_uninteresting(struct commit_list *list)
76{
77 while (list) {
78 struct commit *commit = list->item;
79 list = list->next;
80 if (commit->object.flags & UNINTERESTING)
81 continue;
82 return 0;
83 }
84 return 1;
85}
86
3b42a63c
LT
87struct commit_list *limit_list(struct commit_list *list, struct commit *end)
88{
89 struct commit_list *newlist = NULL;
90 struct commit_list **p = &newlist;
91 do {
92 struct commit *commit = pop_most_recent_commit(&list, SEEN);
93 struct object *obj = &commit->object;
94
95 if (commit == end || (obj->flags & UNINTERESTING)) {
96 mark_parents_uninteresting(commit);
97 if (everybody_uninteresting(list))
98 break;
99 continue;
100 }
101 p = &commit_list_insert(commit, p)->next;
102 } while (list);
103 return newlist;
104}
105
64745109
LT
106int main(int argc, char **argv)
107{
a6f68d47
LT
108 int nr_sha;
109 unsigned char sha1[2][20];
64745109 110 struct commit_list *list = NULL;
a6f68d47 111 struct commit *commit, *end;
81f2bb1f 112 int i;
64745109 113
a6f68d47 114 nr_sha = 0;
fcfda02b
KS
115 for (i = 1 ; i < argc; i++) {
116 char *arg = argv[i];
117
118 if (!strncmp(arg, "--max-count=", 12)) {
119 max_count = atoi(arg + 12);
a6f68d47
LT
120 continue;
121 }
122 if (!strncmp(arg, "--max-age=", 10)) {
fcfda02b 123 max_age = atoi(arg + 10);
a6f68d47
LT
124 continue;
125 }
126 if (!strncmp(arg, "--min-age=", 10)) {
fcfda02b 127 min_age = atoi(arg + 10);
a6f68d47 128 continue;
fcfda02b 129 }
a6f68d47
LT
130 if (!strcmp(arg, "--header")) {
131 verbose_header = 1;
132 continue;
133 }
9d97aa64
LT
134 if (!strcmp(arg, "--pretty")) {
135 verbose_header = 1;
136 pretty_print = 1;
137 hdr_termination = '\n';
138 prefix = "commit ";
139 continue;
140 }
97658004
LT
141 if (!strcmp(arg, "--parents")) {
142 show_parents = 1;
143 continue;
144 }
a6f68d47
LT
145
146 if (nr_sha > 2 || get_sha1(arg, sha1[nr_sha]))
147 usage(rev_list_usage);
148 nr_sha++;
fcfda02b
KS
149 }
150
a6f68d47
LT
151 if (!nr_sha)
152 usage(rev_list_usage);
64745109 153
a6f68d47 154 commit = lookup_commit_reference(sha1[0]);
64745109 155 if (!commit || parse_commit(commit) < 0)
a6f68d47
LT
156 die("bad starting commit object");
157
158 end = NULL;
159 if (nr_sha > 1) {
160 end = lookup_commit_reference(sha1[1]);
161 if (!end || parse_commit(end) < 0)
162 die("bad ending commit object");
163 }
64745109
LT
164
165 commit_list_insert(commit, &list);
3b42a63c
LT
166 if (end)
167 list = limit_list(list, end);
8906300f 168
81f2bb1f 169 show_commit_list(list);
64745109
LT
170 return 0;
171}