]> git.ipfire.org Git - thirdparty/git.git/blame - reachable.c
reachable: use traverse_commit_list instead of custom walk
[thirdparty/git.git] / reachable.c
CommitLineData
94421474
JH
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"
dc347195 10#include "progress.h"
5f78a431 11#include "list-objects.h"
94421474 12
0b26abc0
JK
13struct connectivity_progress {
14 struct progress *progress;
15 unsigned long count;
16};
17
18static 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
883d60fa
JS
25static 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)
94421474
JH
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
41static int add_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
42{
f7892d18 43 struct object *object = parse_object_or_die(sha1, path);
94421474
JH
44 struct rev_info *revs = (struct rev_info *)cb_data;
45
94421474
JH
46 add_pending_object(revs, object, "");
47
48 return 0;
49}
50
51static 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
57static void add_one_tree(const unsigned char *sha1, struct rev_info *revs)
58{
59 struct tree *tree = lookup_tree(sha1);
c3406635
MK
60 if (tree)
61 add_pending_object(revs, &tree->object, "");
94421474
JH
62}
63
64static 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
74static void add_cache_refs(struct rev_info *revs)
75{
76 int i;
77
78 read_cache();
79 for (i = 0; i < active_nr; i++) {
8d2244ba
AP
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 */
7a51ed66 87 if (S_ISGITLINK(active_cache[i]->ce_mode))
8d2244ba
AP
88 continue;
89
94421474
JH
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
5f78a431
JK
102/*
103 * The traversal will have already marked us as SEEN, so we
104 * only need to handle any progress reporting here.
105 */
106static void mark_object(struct object *obj, const struct name_path *path,
107 const char *name, void *data)
108{
109 update_progress(data);
110}
111
112static void mark_commit(struct commit *c, void *data)
113{
114 mark_object(&c->object, NULL, NULL, data);
115}
116
dc347195
NTND
117void mark_reachable_objects(struct rev_info *revs, int mark_reflog,
118 struct progress *progress)
94421474 119{
0b26abc0
JK
120 struct connectivity_progress cp;
121
94421474
JH
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
c40fdd01
MK
136 /* detached HEAD is not included in the list above */
137 head_ref(add_one_ref, revs);
138
eb8381c8 139 /* Add all reflog info */
94421474 140 if (mark_reflog)
eb8381c8 141 for_each_reflog(add_one_reflog, revs);
94421474 142
0b26abc0
JK
143 cp.progress = progress;
144 cp.count = 0;
145
94421474
JH
146 /*
147 * Set up the revision walk - this will move all commits
148 * from the pending list to the commit walking list.
149 */
3d51e1b5
MK
150 if (prepare_revision_walk(revs))
151 die("revision walk setup failed");
5f78a431 152 traverse_commit_list(revs, mark_commit, mark_object, &cp);
0b26abc0 153 display_progress(cp.progress, cp.count);
94421474 154}