reachable: use traverse_commit_list instead of custom walk
[git/git-svn.git] / reachable.c
blob02bf6c2fe4fc90c2f3327988b009f1dc1a3b1e2c
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"
13 struct connectivity_progress {
14 struct progress *progress;
15 unsigned long count;
18 static void update_progress(struct connectivity_progress *cp)
20 cp->count++;
21 if ((cp->count & 1023) == 0)
22 display_progress(cp->progress, cp->count);
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)
29 struct object *object;
30 struct rev_info *revs = (struct rev_info *)cb_data;
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;
41 static int add_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
43 struct object *object = parse_object_or_die(sha1, path);
44 struct rev_info *revs = (struct rev_info *)cb_data;
46 add_pending_object(revs, object, "");
48 return 0;
51 static int add_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
53 for_each_reflog_ent(path, add_one_reflog_ent, cb_data);
54 return 0;
57 static void add_one_tree(const unsigned char *sha1, struct rev_info *revs)
59 struct tree *tree = lookup_tree(sha1);
60 if (tree)
61 add_pending_object(revs, &tree->object, "");
64 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs)
66 int i;
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);
74 static void add_cache_refs(struct rev_info *revs)
76 int i;
78 read_cache();
79 for (i = 0; i < active_nr; i++) {
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
87 if (S_ISGITLINK(active_cache[i]->ce_mode))
88 continue;
90 lookup_blob(active_cache[i]->sha1);
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
98 if (active_cache_tree)
99 add_cache_tree(active_cache_tree, revs);
103 * The traversal will have already marked us as SEEN, so we
104 * only need to handle any progress reporting here.
106 static void mark_object(struct object *obj, const struct name_path *path,
107 const char *name, void *data)
109 update_progress(data);
112 static void mark_commit(struct commit *c, void *data)
114 mark_object(&c->object, NULL, NULL, data);
117 void mark_reachable_objects(struct rev_info *revs, int mark_reflog,
118 struct progress *progress)
120 struct connectivity_progress cp;
123 * Set up revision parsing, and mark us as being interested
124 * in all object types, not just commits.
126 revs->tag_objects = 1;
127 revs->blob_objects = 1;
128 revs->tree_objects = 1;
130 /* Add all refs from the index file */
131 add_cache_refs(revs);
133 /* Add all external refs */
134 for_each_ref(add_one_ref, revs);
136 /* detached HEAD is not included in the list above */
137 head_ref(add_one_ref, revs);
139 /* Add all reflog info */
140 if (mark_reflog)
141 for_each_reflog(add_one_reflog, revs);
143 cp.progress = progress;
144 cp.count = 0;
147 * Set up the revision walk - this will move all commits
148 * from the pending list to the commit walking list.
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);