1#include "cache.h"
2#include "tag.h"
3#include "blob.h"
4#include "tree.h"
5#include "commit.h"
6#include "refs.h"
7#include "revision.h"
8
9static char *path_name(struct name_path *path, const char *name)
10{
11 struct name_path *p;
12 char *n, *m;
13 int nlen = strlen(name);
14 int len = nlen + 1;
15
16 for (p = path; p; p = p->up) {
17 if (p->elem_len)
18 len += p->elem_len + 1;
19 }
20 n = xmalloc(len);
21 m = n + len - (nlen + 1);
22 strcpy(m, name);
23 for (p = path; p; p = p->up) {
24 if (p->elem_len) {
25 m -= p->elem_len + 1;
26 memcpy(m, p->elem, p->elem_len);
27 m[p->elem_len] = '/';
28 }
29 }
30 return n;
31}
32
33struct object_list **add_object(struct object *obj,
34 struct object_list **p,
35 struct name_path *path,
36 const char *name)
37{
38 struct object_list *entry = xmalloc(sizeof(*entry));
39 entry->item = obj;
40 entry->next = *p;
41 entry->name = path_name(path, name);
42 *p = entry;
43 return &entry->next;
44}
45
46static void mark_blob_uninteresting(struct blob *blob)
47{
48 if (blob->object.flags & UNINTERESTING)
49 return;
50 blob->object.flags |= UNINTERESTING;
51}
52
53void mark_tree_uninteresting(struct tree *tree)
54{
55 struct object *obj = &tree->object;
56 struct tree_entry_list *entry;
57
58 if (obj->flags & UNINTERESTING)
59 return;
60 obj->flags |= UNINTERESTING;
61 if (!has_sha1_file(obj->sha1))
62 return;
63 if (parse_tree(tree) < 0)
64 die("bad tree %s", sha1_to_hex(obj->sha1));
65 entry = tree->entries;
66 tree->entries = NULL;
67 while (entry) {
68 struct tree_entry_list *next = entry->next;
69 if (entry->directory)
70 mark_tree_uninteresting(entry->item.tree);
71 else
72 mark_blob_uninteresting(entry->item.blob);
73 free(entry);
74 entry = next;
75 }
76}
77
78void mark_parents_uninteresting(struct commit *commit)
79{
80 struct commit_list *parents = commit->parents;
81
82 while (parents) {
83 struct commit *commit = parents->item;
84 commit->object.flags |= UNINTERESTING;
85
86 /*
87 * Normally we haven't parsed the parent
88 * yet, so we won't have a parent of a parent
89 * here. However, it may turn out that we've
90 * reached this commit some other way (where it
91 * wasn't uninteresting), in which case we need
92 * to mark its parents recursively too..
93 */
94 if (commit->parents)
95 mark_parents_uninteresting(commit);
96
97 /*
98 * A missing commit is ok iff its parent is marked
99 * uninteresting.
100 *
101 * We just mark such a thing parsed, so that when
102 * it is popped next time around, we won't be trying
103 * to parse it and get an error.
104 */
105 if (!has_sha1_file(commit->object.sha1))
106 commit->object.parsed = 1;
107 parents = parents->next;
108 }
109}
110
111static void add_pending_object(struct rev_info *revs, struct object *obj, const char *name)
112{
113 add_object(obj, &revs->pending_objects, NULL, name);
114}
115
116static struct commit *get_commit_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags)
117{
118 struct object *object;
119
120 object = parse_object(sha1);
121 if (!object)
122 die("bad object %s", name);
123
124 /*
125 * Tag object? Look what it points to..
126 */
127 while (object->type == tag_type) {
128 struct tag *tag = (struct tag *) object;
129 object->flags |= flags;
130 if (revs->tag_objects && !(object->flags & UNINTERESTING))
131 add_pending_object(revs, object, tag->tag);
132 object = parse_object(tag->tagged->sha1);
133 if (!object)
134 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
135 }
136
137 /*
138 * Commit object? Just return it, we'll do all the complex
139 * reachability crud.
140 */
141 if (object->type == commit_type) {
142 struct commit *commit = (struct commit *)object;
143 object->flags |= flags;
144 if (parse_commit(commit) < 0)
145 die("unable to parse commit %s", name);
146 if (flags & UNINTERESTING) {
147 mark_parents_uninteresting(commit);
148 revs->limited = 1;
149 }
150 return commit;
151 }
152
153 /*
154 * Tree object? Either mark it uniniteresting, or add it
155 * to the list of objects to look at later..
156 */
157 if (object->type == tree_type) {
158 struct tree *tree = (struct tree *)object;
159 if (!revs->tree_objects)
160 return NULL;
161 if (flags & UNINTERESTING) {
162 mark_tree_uninteresting(tree);
163 return NULL;
164 }
165 add_pending_object(revs, object, "");
166 return NULL;
167 }
168
169 /*
170 * Blob object? You know the drill by now..
171 */
172 if (object->type == blob_type) {
173 struct blob *blob = (struct blob *)object;
174 if (!revs->blob_objects)
175 return NULL;
176 if (flags & UNINTERESTING) {
177 mark_blob_uninteresting(blob);
178 return NULL;
179 }
180 add_pending_object(revs, object, "");
181 return NULL;
182 }
183 die("%s is unknown object", name);
184}
185
186static void add_one_commit(struct commit *commit, struct rev_info *revs)
187{
188 if (!commit || (commit->object.flags & SEEN))
189 return;
190 commit->object.flags |= SEEN;
191 commit_list_insert(commit, &revs->commits);
192}
193
194static int all_flags;
195static struct rev_info *all_revs;
196
197static int handle_one_ref(const char *path, const unsigned char *sha1)
198{
199 struct commit *commit = get_commit_reference(all_revs, path, sha1, all_flags);
200 add_one_commit(commit, all_revs);
201 return 0;
202}
203
204static void handle_all(struct rev_info *revs, unsigned flags)
205{
206 all_revs = revs;
207 all_flags = flags;
208 for_each_ref(handle_one_ref);
209}
210
211/*
212 * Parse revision information, filling in the "rev_info" structure,
213 * and removing the used arguments from the argument list.
214 *
215 * Returns the number of arguments left ("new argc").
216 */
217int setup_revisions(int argc, const char **argv, struct rev_info *revs)
218{
219 int i, flags, seen_dashdash;
220 const char *def = NULL;
221 const char **unrecognized = argv+1;
222 int left = 1;
223
224 memset(revs, 0, sizeof(*revs));
225 revs->lifo = 1;
226 revs->dense = 1;
227 revs->prefix = setup_git_directory();
228 revs->max_age = -1;
229 revs->min_age = -1;
230 revs->max_count = -1;
231
232 /* First, search for "--" */
233 seen_dashdash = 0;
234 for (i = 1; i < argc; i++) {
235 const char *arg = argv[i];
236 if (strcmp(arg, "--"))
237 continue;
238 argv[i] = NULL;
239 argc = i;
240 revs->paths = get_pathspec(revs->prefix, argv + i + 1);
241 seen_dashdash = 1;
242 break;
243 }
244
245 flags = 0;
246 for (i = 1; i < argc; i++) {
247 struct commit *commit;
248 const char *arg = argv[i];
249 unsigned char sha1[20];
250 char *dotdot;
251 int local_flags;
252
253 if (*arg == '-') {
254 if (!strncmp(arg, "--max-count=", 12)) {
255 revs->max_count = atoi(arg + 12);
256 continue;
257 }
258 if (!strncmp(arg, "--max-age=", 10)) {
259 revs->max_age = atoi(arg + 10);
260 revs->limited = 1;
261 continue;
262 }
263 if (!strncmp(arg, "--min-age=", 10)) {
264 revs->min_age = atoi(arg + 10);
265 revs->limited = 1;
266 continue;
267 }
268 if (!strcmp(arg, "--all")) {
269 handle_all(revs, flags);
270 continue;
271 }
272 if (!strcmp(arg, "--not")) {
273 flags ^= UNINTERESTING;
274 continue;
275 }
276 if (!strcmp(arg, "--default")) {
277 if (++i >= argc)
278 die("bad --default argument");
279 def = argv[i];
280 continue;
281 }
282 if (!strcmp(arg, "--topo-order")) {
283 revs->topo_order = 1;
284 revs->limited = 1;
285 continue;
286 }
287 if (!strcmp(arg, "--date-order")) {
288 revs->lifo = 0;
289 revs->topo_order = 1;
290 revs->limited = 1;
291 continue;
292 }
293 if (!strcmp(arg, "--dense")) {
294 revs->dense = 1;
295 continue;
296 }
297 if (!strcmp(arg, "--sparse")) {
298 revs->dense = 0;
299 continue;
300 }
301 if (!strcmp(arg, "--remove-empty")) {
302 revs->remove_empty_trees = 1;
303 continue;
304 }
305 if (!strcmp(arg, "--objects")) {
306 revs->tag_objects = 1;
307 revs->tree_objects = 1;
308 revs->blob_objects = 1;
309 continue;
310 }
311 if (!strcmp(arg, "--objects-edge")) {
312 revs->tag_objects = 1;
313 revs->tree_objects = 1;
314 revs->blob_objects = 1;
315 revs->edge_hint = 1;
316 continue;
317 }
318 if (!strcmp(arg, "--unpacked")) {
319 revs->unpacked = 1;
320 revs->limited = 1;
321 continue;
322 }
323 *unrecognized++ = arg;
324 left++;
325 continue;
326 }
327 dotdot = strstr(arg, "..");
328 if (dotdot) {
329 unsigned char from_sha1[20];
330 char *next = dotdot + 2;
331 *dotdot = 0;
332 if (!*next)
333 next = "HEAD";
334 if (!get_sha1(arg, from_sha1) && !get_sha1(next, sha1)) {
335 struct commit *exclude;
336 struct commit *include;
337
338 exclude = get_commit_reference(revs, arg, from_sha1, flags ^ UNINTERESTING);
339 include = get_commit_reference(revs, next, sha1, flags);
340 if (!exclude || !include)
341 die("Invalid revision range %s..%s", arg, next);
342 add_one_commit(exclude, revs);
343 add_one_commit(include, revs);
344 continue;
345 }
346 *dotdot = '.';
347 }
348 local_flags = 0;
349 if (*arg == '^') {
350 local_flags = UNINTERESTING;
351 arg++;
352 }
353 if (get_sha1(arg, sha1) < 0) {
354 struct stat st;
355 int j;
356
357 if (seen_dashdash || local_flags)
358 die("bad revision '%s'", arg);
359
360 /* If we didn't have a "--", all filenames must exist */
361 for (j = i; j < argc; j++) {
362 if (lstat(argv[j], &st) < 0)
363 die("'%s': %s", arg, strerror(errno));
364 }
365 revs->paths = get_pathspec(revs->prefix, argv + i);
366 break;
367 }
368 commit = get_commit_reference(revs, arg, sha1, flags ^ local_flags);
369 add_one_commit(commit, revs);
370 }
371 if (def && !revs->commits) {
372 unsigned char sha1[20];
373 struct commit *commit;
374 if (get_sha1(def, sha1) < 0)
375 die("bad default revision '%s'", def);
376 commit = get_commit_reference(revs, def, sha1, 0);
377 add_one_commit(commit, revs);
378 }
379 if (revs->paths)
380 revs->limited = 1;
381 *unrecognized = NULL;
382 return left;
383}