apply: fix typo in an error message
[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                          void *cb_data)
17 {
18         struct object *obj = &blob->object;
19
20         if (!revs->blob_objects)
21                 return;
22         if (!obj)
23                 die("bad blob object");
24         if (obj->flags & (UNINTERESTING | SEEN))
25                 return;
26         obj->flags |= SEEN;
27         show(obj, path, name, cb_data);
28 }
29
30 /*
31  * Processing a gitlink entry currently does nothing, since
32  * we do not recurse into the subproject.
33  *
34  * We *could* eventually add a flag that actually does that,
35  * which would involve:
36  *  - is the subproject actually checked out?
37  *  - if so, see if the subproject has already been added
38  *    to the alternates list, and add it if not.
39  *  - process the commit (or tag) the gitlink points to
40  *    recursively.
41  *
42  * However, it's unclear whether there is really ever any
43  * reason to see superprojects and subprojects as such a
44  * "unified" object pool (potentially resulting in a totally
45  * humongous pack - avoiding which was the whole point of
46  * having gitlinks in the first place!).
47  *
48  * So for now, there is just a note that we *could* follow
49  * the link, and how to do it. Whether it necessarily makes
50  * any sense what-so-ever to ever do that is another issue.
51  */
52 static void process_gitlink(struct rev_info *revs,
53                             const unsigned char *sha1,
54                             show_object_fn show,
55                             struct name_path *path,
56                             const char *name,
57                             void *cb_data)
58 {
59         /* Nothing to do */
60 }
61
62 static void process_tree(struct rev_info *revs,
63                          struct tree *tree,
64                          show_object_fn show,
65                          struct name_path *path,
66                          struct strbuf *base,
67                          const char *name,
68                          void *cb_data)
69 {
70         struct object *obj = &tree->object;
71         struct tree_desc desc;
72         struct name_entry entry;
73         struct name_path me;
74         enum interesting match = revs->diffopt.pathspec.nr == 0 ?
75                 all_entries_interesting: entry_not_interesting;
76         int baselen = base->len;
77
78         if (!revs->tree_objects)
79                 return;
80         if (!obj)
81                 die("bad tree object");
82         if (obj->flags & (UNINTERESTING | SEEN))
83                 return;
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, cb_data);
88         me.up = path;
89         me.elem = name;
90         me.elem_len = strlen(name);
91
92         if (!match) {
93                 strbuf_addstr(base, name);
94                 if (base->len)
95                         strbuf_addch(base, '/');
96         }
97
98         init_tree_desc(&desc, tree->buffer, tree->size);
99
100         while (tree_entry(&desc, &entry)) {
101                 if (match != all_entries_interesting) {
102                         match = tree_entry_interesting(&entry, base, 0,
103                                                        &revs->diffopt.pathspec);
104                         if (match == all_entries_not_interesting)
105                                 break;
106                         if (match == entry_not_interesting)
107                                 continue;
108                 }
109
110                 if (S_ISDIR(entry.mode))
111                         process_tree(revs,
112                                      lookup_tree(entry.sha1),
113                                      show, &me, base, entry.path,
114                                      cb_data);
115                 else if (S_ISGITLINK(entry.mode))
116                         process_gitlink(revs, entry.sha1,
117                                         show, &me, entry.path,
118                                         cb_data);
119                 else
120                         process_blob(revs,
121                                      lookup_blob(entry.sha1),
122                                      show, &me, entry.path,
123                                      cb_data);
124         }
125         strbuf_setlen(base, baselen);
126         free_tree_buffer(tree);
127 }
128
129 static void mark_edge_parents_uninteresting(struct commit *commit,
130                                             struct rev_info *revs,
131                                             show_edge_fn show_edge)
132 {
133         struct commit_list *parents;
134
135         for (parents = commit->parents; parents; parents = parents->next) {
136                 struct commit *parent = parents->item;
137                 if (!(parent->object.flags & UNINTERESTING))
138                         continue;
139                 mark_tree_uninteresting(parent->tree);
140                 if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
141                         parent->object.flags |= SHOWN;
142                         show_edge(parent);
143                 }
144         }
145 }
146
147 void mark_edges_uninteresting(struct rev_info *revs, show_edge_fn show_edge)
148 {
149         struct commit_list *list;
150         int i;
151
152         for (list = revs->commits; list; list = list->next) {
153                 struct commit *commit = list->item;
154
155                 if (commit->object.flags & UNINTERESTING) {
156                         mark_tree_uninteresting(commit->tree);
157                         if (revs->edge_hint && !(commit->object.flags & SHOWN)) {
158                                 commit->object.flags |= SHOWN;
159                                 show_edge(commit);
160                         }
161                         continue;
162                 }
163                 mark_edge_parents_uninteresting(commit, revs, show_edge);
164         }
165         for (i = 0; i < revs->cmdline.nr; i++) {
166                 struct object *obj = revs->cmdline.rev[i].item;
167                 struct commit *commit = (struct commit *)obj;
168                 if (obj->type != OBJ_COMMIT || !(obj->flags & UNINTERESTING))
169                         continue;
170                 mark_tree_uninteresting(commit->tree);
171                 if (revs->edge_hint && !(obj->flags & SHOWN)) {
172                         obj->flags |= SHOWN;
173                         show_edge(commit);
174                 }
175         }
176 }
177
178 static void add_pending_tree(struct rev_info *revs, struct tree *tree)
179 {
180         add_pending_object(revs, &tree->object, "");
181 }
182
183 void traverse_commit_list(struct rev_info *revs,
184                           show_commit_fn show_commit,
185                           show_object_fn show_object,
186                           void *data)
187 {
188         int i;
189         struct commit *commit;
190         struct strbuf base;
191
192         strbuf_init(&base, PATH_MAX);
193         while ((commit = get_revision(revs)) != NULL) {
194                 /*
195                  * an uninteresting boundary commit may not have its tree
196                  * parsed yet, but we are not going to show them anyway
197                  */
198                 if (commit->tree)
199                         add_pending_tree(revs, commit->tree);
200                 show_commit(commit, data);
201         }
202         for (i = 0; i < revs->pending.nr; i++) {
203                 struct object_array_entry *pending = revs->pending.objects + i;
204                 struct object *obj = pending->item;
205                 const char *name = pending->name;
206                 if (obj->flags & (UNINTERESTING | SEEN))
207                         continue;
208                 if (obj->type == OBJ_TAG) {
209                         obj->flags |= SEEN;
210                         show_object(obj, NULL, name, data);
211                         continue;
212                 }
213                 if (obj->type == OBJ_TREE) {
214                         process_tree(revs, (struct tree *)obj, show_object,
215                                      NULL, &base, name, data);
216                         continue;
217                 }
218                 if (obj->type == OBJ_BLOB) {
219                         process_blob(revs, (struct blob *)obj, show_object,
220                                      NULL, name, data);
221                         continue;
222                 }
223                 die("unknown pending object %s (%s)",
224                     sha1_to_hex(obj->sha1), name);
225         }
226         if (revs->pending.nr) {
227                 free(revs->pending.objects);
228                 revs->pending.nr = 0;
229                 revs->pending.alloc = 0;
230                 revs->pending.objects = NULL;
231         }
232         strbuf_release(&base);
233 }