merge-recursive: get rid of virtual_id
[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                          struct object_array *p,
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         name = xstrdup(name);
27         add_object(obj, p, path, name);
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                             struct object_array *p,
55                             struct name_path *path,
56                             const char *name)
57 {
58         /* Nothing to do */
59 }
60
61 static void process_tree(struct rev_info *revs,
62                          struct tree *tree,
63                          struct object_array *p,
64                          struct name_path *path,
65                          const char *name)
66 {
67         struct object *obj = &tree->object;
68         struct tree_desc desc;
69         struct name_entry entry;
70         struct name_path me;
71
72         if (!revs->tree_objects)
73                 return;
74         if (!obj)
75                 die("bad tree object");
76         if (obj->flags & (UNINTERESTING | SEEN))
77                 return;
78         if (parse_tree(tree) < 0)
79                 die("bad tree object %s", sha1_to_hex(obj->sha1));
80         obj->flags |= SEEN;
81         name = xstrdup(name);
82         add_object(obj, p, path, name);
83         me.up = path;
84         me.elem = name;
85         me.elem_len = strlen(name);
86
87         init_tree_desc(&desc, tree->buffer, tree->size);
88
89         while (tree_entry(&desc, &entry)) {
90                 if (S_ISDIR(entry.mode))
91                         process_tree(revs,
92                                      lookup_tree(entry.sha1),
93                                      p, &me, entry.path);
94                 else if (S_ISGITLINK(entry.mode))
95                         process_gitlink(revs, entry.sha1,
96                                         p, &me, entry.path);
97                 else
98                         process_blob(revs,
99                                      lookup_blob(entry.sha1),
100                                      p, &me, entry.path);
101         }
102         free(tree->buffer);
103         tree->buffer = NULL;
104 }
105
106 static void mark_edge_parents_uninteresting(struct commit *commit,
107                                             struct rev_info *revs,
108                                             show_edge_fn show_edge)
109 {
110         struct commit_list *parents;
111
112         for (parents = commit->parents; parents; parents = parents->next) {
113                 struct commit *parent = parents->item;
114                 if (!(parent->object.flags & UNINTERESTING))
115                         continue;
116                 mark_tree_uninteresting(parent->tree);
117                 if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
118                         parent->object.flags |= SHOWN;
119                         show_edge(parent);
120                 }
121         }
122 }
123
124 void mark_edges_uninteresting(struct commit_list *list,
125                               struct rev_info *revs,
126                               show_edge_fn show_edge)
127 {
128         for ( ; list; list = list->next) {
129                 struct commit *commit = list->item;
130
131                 if (commit->object.flags & UNINTERESTING) {
132                         mark_tree_uninteresting(commit->tree);
133                         continue;
134                 }
135                 mark_edge_parents_uninteresting(commit, revs, show_edge);
136         }
137 }
138
139 void traverse_commit_list(struct rev_info *revs,
140                           void (*show_commit)(struct commit *),
141                           void (*show_object)(struct object_array_entry *))
142 {
143         int i;
144         struct commit *commit;
145         struct object_array objects = { 0, 0, NULL };
146
147         while ((commit = get_revision(revs)) != NULL) {
148                 process_tree(revs, commit->tree, &objects, NULL, "");
149                 show_commit(commit);
150         }
151         for (i = 0; i < revs->pending.nr; i++) {
152                 struct object_array_entry *pending = revs->pending.objects + i;
153                 struct object *obj = pending->item;
154                 const char *name = pending->name;
155                 if (obj->flags & (UNINTERESTING | SEEN))
156                         continue;
157                 if (obj->type == OBJ_TAG) {
158                         obj->flags |= SEEN;
159                         add_object_array(obj, name, &objects);
160                         continue;
161                 }
162                 if (obj->type == OBJ_TREE) {
163                         process_tree(revs, (struct tree *)obj, &objects,
164                                      NULL, name);
165                         continue;
166                 }
167                 if (obj->type == OBJ_BLOB) {
168                         process_blob(revs, (struct blob *)obj, &objects,
169                                      NULL, name);
170                         continue;
171                 }
172                 die("unknown pending object %s (%s)",
173                     sha1_to_hex(obj->sha1), name);
174         }
175         for (i = 0; i < objects.nr; i++)
176                 show_object(&objects.objects[i]);
177         free(objects.objects);
178         if (revs->pending.nr) {
179                 free(revs->pending.objects);
180                 revs->pending.nr = 0;
181                 revs->pending.alloc = 0;
182                 revs->pending.objects = NULL;
183         }
184 }