reachable: use traverse_commit_list instead of custom walk
[git] / reachable.c
1 #include "cache.h"
2 #include "refs.h"
3 #include "tag.h"
4 #include "commit.h"
5 #include "blob.h"
6 #include "diff.h"
7 #include "revision.h"
8 #include "reachable.h"
9 #include "cache-tree.h"
10 #include "progress.h"
11 #include "list-objects.h"
12
13 struct connectivity_progress {
14         struct progress *progress;
15         unsigned long count;
16 };
17
18 static void update_progress(struct connectivity_progress *cp)
19 {
20         cp->count++;
21         if ((cp->count & 1023) == 0)
22                 display_progress(cp->progress, cp->count);
23 }
24
25 static int add_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
26                 const char *email, unsigned long timestamp, int tz,
27                 const char *message, void *cb_data)
28 {
29         struct object *object;
30         struct rev_info *revs = (struct rev_info *)cb_data;
31
32         object = parse_object(osha1);
33         if (object)
34                 add_pending_object(revs, object, "");
35         object = parse_object(nsha1);
36         if (object)
37                 add_pending_object(revs, object, "");
38         return 0;
39 }
40
41 static int add_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
42 {
43         struct object *object = parse_object_or_die(sha1, path);
44         struct rev_info *revs = (struct rev_info *)cb_data;
45
46         add_pending_object(revs, object, "");
47
48         return 0;
49 }
50
51 static int add_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
52 {
53         for_each_reflog_ent(path, add_one_reflog_ent, cb_data);
54         return 0;
55 }
56
57 static void add_one_tree(const unsigned char *sha1, struct rev_info *revs)
58 {
59         struct tree *tree = lookup_tree(sha1);
60         if (tree)
61                 add_pending_object(revs, &tree->object, "");
62 }
63
64 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs)
65 {
66         int i;
67
68         if (it->entry_count >= 0)
69                 add_one_tree(it->sha1, revs);
70         for (i = 0; i < it->subtree_nr; i++)
71                 add_cache_tree(it->down[i]->cache_tree, revs);
72 }
73
74 static void add_cache_refs(struct rev_info *revs)
75 {
76         int i;
77
78         read_cache();
79         for (i = 0; i < active_nr; i++) {
80                 /*
81                  * The index can contain blobs and GITLINKs, GITLINKs are hashes
82                  * that don't actually point to objects in the repository, it's
83                  * almost guaranteed that they are NOT blobs, so we don't call
84                  * lookup_blob() on them, to avoid populating the hash table
85                  * with invalid information
86                  */
87                 if (S_ISGITLINK(active_cache[i]->ce_mode))
88                         continue;
89
90                 lookup_blob(active_cache[i]->sha1);
91                 /*
92                  * We could add the blobs to the pending list, but quite
93                  * frankly, we don't care. Once we've looked them up, and
94                  * added them as objects, we've really done everything
95                  * there is to do for a blob
96                  */
97         }
98         if (active_cache_tree)
99                 add_cache_tree(active_cache_tree, revs);
100 }
101
102 /*
103  * The traversal will have already marked us as SEEN, so we
104  * only need to handle any progress reporting here.
105  */
106 static void mark_object(struct object *obj, const struct name_path *path,
107                         const char *name, void *data)
108 {
109         update_progress(data);
110 }
111
112 static void mark_commit(struct commit *c, void *data)
113 {
114         mark_object(&c->object, NULL, NULL, data);
115 }
116
117 void mark_reachable_objects(struct rev_info *revs, int mark_reflog,
118                             struct progress *progress)
119 {
120         struct connectivity_progress cp;
121
122         /*
123          * Set up revision parsing, and mark us as being interested
124          * in all object types, not just commits.
125          */
126         revs->tag_objects = 1;
127         revs->blob_objects = 1;
128         revs->tree_objects = 1;
129
130         /* Add all refs from the index file */
131         add_cache_refs(revs);
132
133         /* Add all external refs */
134         for_each_ref(add_one_ref, revs);
135
136         /* detached HEAD is not included in the list above */
137         head_ref(add_one_ref, revs);
138
139         /* Add all reflog info */
140         if (mark_reflog)
141                 for_each_reflog(add_one_reflog, revs);
142
143         cp.progress = progress;
144         cp.count = 0;
145
146         /*
147          * Set up the revision walk - this will move all commits
148          * from the pending list to the commit walking list.
149          */
150         if (prepare_revision_walk(revs))
151                 die("revision walk setup failed");
152         traverse_commit_list(revs, mark_commit, mark_object, &cp);
153         display_progress(cp.progress, cp.count);
154 }