Merge branch 'dk/blame-el' into pu
[git] / list-objects.c
1 #include "cache.h"
2 #include "tag.h"
3 #include "commit.h"
4 #include "tree.h"
5 #include "blob.h"
6 #include "diff.h"
7 #include "tree-walk.h"
8 #include "revision.h"
9 #include "list-objects.h"
10
11 static void process_blob(struct rev_info *revs,
12                          struct blob *blob,
13                          show_object_fn show,
14                          struct name_path *path,
15                          const char *name)
16 {
17         struct object *obj = &blob->object;
18
19         if (!revs->blob_objects)
20                 return;
21         if (!obj)
22                 die("bad blob object");
23         if (obj->flags & (UNINTERESTING | SEEN))
24                 return;
25         obj->flags |= SEEN;
26         show(obj, path, name);
27 }
28
29 /*
30  * Processing a gitlink entry currently does nothing, since
31  * we do not recurse into the subproject.
32  *
33  * We *could* eventually add a flag that actually does that,
34  * which would involve:
35  *  - is the subproject actually checked out?
36  *  - if so, see if the subproject has already been added
37  *    to the alternates list, and add it if not.
38  *  - process the commit (or tag) the gitlink points to
39  *    recursively.
40  *
41  * However, it's unclear whether there is really ever any
42  * reason to see superprojects and subprojects as such a
43  * "unified" object pool (potentially resulting in a totally
44  * humongous pack - avoiding which was the whole point of
45  * having gitlinks in the first place!).
46  *
47  * So for now, there is just a note that we *could* follow
48  * the link, and how to do it. Whether it necessarily makes
49  * any sense what-so-ever to ever do that is another issue.
50  */
51 static void process_gitlink(struct rev_info *revs,
52                             const unsigned char *sha1,
53                             show_object_fn show,
54                             struct name_path *path,
55                             const char *name)
56 {
57         /* Nothing to do */
58 }
59
60 static void process_tree(struct rev_info *revs,
61                          struct tree *tree,
62                          show_object_fn show,
63                          struct name_path *path,
64                          const char *name)
65 {
66         struct object *obj = &tree->object;
67         struct tree_desc desc;
68         struct name_entry entry;
69         struct name_path me;
70
71         if (!revs->tree_objects)
72                 return;
73         if (!obj)
74                 die("bad tree object");
75         if (obj->flags & (UNINTERESTING | SEEN))
76                 return;
77         if (obj->flags & FACE_VALUE) {
78                 obj->flags |= SEEN;
79                 show(obj, path, name);
80                 /* not parsing the tree saves a lot of time! */
81                 return;
82         }
83
84         if (parse_tree(tree) < 0)
85                 die("bad tree object %s", sha1_to_hex(obj->sha1));
86         obj->flags |= SEEN;
87         show(obj, path, name);
88
89         me.up = path;
90         me.elem = name;
91         me.elem_len = strlen(name);
92         init_tree_desc(&desc, tree->buffer, tree->size);
93
94         while (tree_entry(&desc, &entry)) {
95                 if (S_ISDIR(entry.mode)) {
96                         struct tree *subtree = lookup_tree(entry.sha1);
97                         if (!subtree)
98                                 continue;
99
100                         subtree->object.flags &= ~FACE_VALUE;
101                         process_tree(revs,
102                                      subtree,
103                                      show, &me, entry.path);
104                 } else if (S_ISGITLINK(entry.mode))
105                         process_gitlink(revs, entry.sha1,
106                                         show, &me, entry.path);
107                 else
108                         process_blob(revs,
109                                      lookup_blob(entry.sha1),
110                                      show, &me, entry.path);
111         }
112         free(tree->buffer);
113         tree->buffer = NULL;
114 }
115
116 static void mark_edge_parents_uninteresting(struct commit *commit,
117                                             struct rev_info *revs,
118                                             show_edge_fn show_edge)
119 {
120         struct commit_list *parents;
121
122         for (parents = commit->parents; parents; parents = parents->next) {
123                 struct commit *parent = parents->item;
124                 if (!(parent->object.flags & UNINTERESTING))
125                         continue;
126                 mark_tree_uninteresting(parent->tree);
127                 if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
128                         parent->object.flags |= SHOWN;
129                         show_edge(parent);
130                 }
131         }
132 }
133
134 void mark_edges_uninteresting(struct commit_list *list,
135                               struct rev_info *revs,
136                               show_edge_fn show_edge)
137 {
138         for ( ; list; list = list->next) {
139                 struct commit *commit = list->item;
140
141                 if (commit->object.flags & UNINTERESTING) {
142                         mark_tree_uninteresting(commit->tree);
143                         continue;
144                 }
145                 mark_edge_parents_uninteresting(commit, revs, show_edge);
146         }
147 }
148
149 static void add_pending_tree(struct rev_info *revs, struct tree *tree)
150 {
151         tree->object.flags &= ~FACE_VALUE;
152         add_pending_object(revs, &tree->object, "");
153 }
154
155 void traverse_commit_list(struct rev_info *revs,
156                           show_commit_fn show_commit,
157                           show_object_fn show_object,
158                           void *data)
159 {
160         int i;
161         struct commit *commit;
162         enum object_type what = OBJ_TAG;
163         char face_value = 0;
164
165         while ((commit = get_revision(revs)) != NULL) {
166                 if (!(commit->object.flags & FACE_VALUE))
167                         add_pending_tree(revs, commit->tree);
168                 else
169                         face_value = 1;
170                 show_commit(commit, data);
171         }
172
173 loop_objects:
174         for (i = 0; i < revs->pending.nr; i++) {
175                 struct object_array_entry *pending = revs->pending.objects + i;
176                 struct object *obj = pending->item;
177                 const char *name = pending->name;
178                 if (obj->flags & (UNINTERESTING | SEEN))
179                         continue;
180                 if (obj->type != what && face_value)
181                         continue;
182
183                 if (obj->type == OBJ_TAG) {
184                         obj->flags |= SEEN;
185                         show_object(obj, NULL, name);
186                         continue;
187                 }
188                 if (obj->type == OBJ_TREE) {
189                         process_tree(revs, (struct tree *)obj, show_object,
190                                      NULL, name);
191                         continue;
192                 }
193                 if (obj->type == OBJ_BLOB) {
194                         process_blob(revs, (struct blob *)obj, show_object,
195                                      NULL, name);
196                         continue;
197                 }
198                 die("unknown pending object %s (%s)",
199                     sha1_to_hex(obj->sha1), name);
200         }
201         if (face_value) {
202                 switch (what) {
203                 case OBJ_TAG :
204                         what = OBJ_TREE;
205                         goto loop_objects;
206                 case OBJ_TREE :
207                         what = OBJ_BLOB;
208                         goto loop_objects;
209                 default :
210                         break;
211                 }
212         }
213
214         if (revs->pending.nr) {
215                 free(revs->pending.objects);
216                 revs->pending.nr = 0;
217                 revs->pending.alloc = 0;
218                 revs->pending.objects = NULL;
219         }
220 }