9 #include "cache-tree.h"
12 struct connectivity_progress
{
13 struct progress
*progress
;
17 static void update_progress(struct connectivity_progress
*cp
)
20 if ((cp
->count
& 1023) == 0)
21 display_progress(cp
->progress
, cp
->count
);
24 static void process_blob(struct blob
*blob
,
25 struct object_array
*p
,
26 struct name_path
*path
,
28 struct connectivity_progress
*cp
)
30 struct object
*obj
= &blob
->object
;
33 die("bad blob object");
34 if (obj
->flags
& SEEN
)
38 /* Nothing to do, really .. The blob lookup was the important part */
41 static void process_gitlink(const unsigned char *sha1
,
42 struct object_array
*p
,
43 struct name_path
*path
,
46 /* I don't think we want to recurse into this, really. */
49 static void process_tree(struct tree
*tree
,
50 struct object_array
*p
,
51 struct name_path
*path
,
53 struct connectivity_progress
*cp
)
55 struct object
*obj
= &tree
->object
;
56 struct tree_desc desc
;
57 struct name_entry entry
;
61 die("bad tree object");
62 if (obj
->flags
& SEEN
)
66 if (parse_tree(tree
) < 0)
67 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
68 add_object(obj
, p
, path
, name
);
71 me
.elem_len
= strlen(name
);
73 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
75 while (tree_entry(&desc
, &entry
)) {
76 if (S_ISDIR(entry
.mode
))
77 process_tree(lookup_tree(entry
.sha1
), p
, &me
, entry
.path
, cp
);
78 else if (S_ISGITLINK(entry
.mode
))
79 process_gitlink(entry
.sha1
, p
, &me
, entry
.path
);
81 process_blob(lookup_blob(entry
.sha1
), p
, &me
, entry
.path
, cp
);
83 free_tree_buffer(tree
);
86 static void process_tag(struct tag
*tag
, struct object_array
*p
,
87 const char *name
, struct connectivity_progress
*cp
)
89 struct object
*obj
= &tag
->object
;
91 if (obj
->flags
& SEEN
)
96 if (parse_tag(tag
) < 0)
97 die("bad tag object %s", sha1_to_hex(obj
->sha1
));
99 add_object(tag
->tagged
, p
, NULL
, name
);
102 static void walk_commit_list(struct rev_info
*revs
,
103 struct connectivity_progress
*cp
)
106 struct commit
*commit
;
107 struct object_array objects
= OBJECT_ARRAY_INIT
;
109 /* Walk all commits, process their trees */
110 while ((commit
= get_revision(revs
)) != NULL
) {
111 process_tree(commit
->tree
, &objects
, NULL
, "", cp
);
115 /* Then walk all the pending objects, recursively processing them too */
116 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
117 struct object_array_entry
*pending
= revs
->pending
.objects
+ i
;
118 struct object
*obj
= pending
->item
;
119 const char *name
= pending
->name
;
120 if (obj
->type
== OBJ_TAG
) {
121 process_tag((struct tag
*) obj
, &objects
, name
, cp
);
124 if (obj
->type
== OBJ_TREE
) {
125 process_tree((struct tree
*)obj
, &objects
, NULL
, name
, cp
);
128 if (obj
->type
== OBJ_BLOB
) {
129 process_blob((struct blob
*)obj
, &objects
, NULL
, name
, cp
);
132 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
136 static int add_one_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
137 const char *email
, unsigned long timestamp
, int tz
,
138 const char *message
, void *cb_data
)
140 struct object
*object
;
141 struct rev_info
*revs
= (struct rev_info
*)cb_data
;
143 object
= parse_object(osha1
);
145 add_pending_object(revs
, object
, "");
146 object
= parse_object(nsha1
);
148 add_pending_object(revs
, object
, "");
152 static int add_one_ref(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
154 struct object
*object
= parse_object_or_die(sha1
, path
);
155 struct rev_info
*revs
= (struct rev_info
*)cb_data
;
157 add_pending_object(revs
, object
, "");
162 static int add_one_reflog(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
164 for_each_reflog_ent(path
, add_one_reflog_ent
, cb_data
);
168 static void add_one_tree(const unsigned char *sha1
, struct rev_info
*revs
)
170 struct tree
*tree
= lookup_tree(sha1
);
172 add_pending_object(revs
, &tree
->object
, "");
175 static void add_cache_tree(struct cache_tree
*it
, struct rev_info
*revs
)
179 if (it
->entry_count
>= 0)
180 add_one_tree(it
->sha1
, revs
);
181 for (i
= 0; i
< it
->subtree_nr
; i
++)
182 add_cache_tree(it
->down
[i
]->cache_tree
, revs
);
185 static void add_cache_refs(struct rev_info
*revs
)
190 for (i
= 0; i
< active_nr
; i
++) {
192 * The index can contain blobs and GITLINKs, GITLINKs are hashes
193 * that don't actually point to objects in the repository, it's
194 * almost guaranteed that they are NOT blobs, so we don't call
195 * lookup_blob() on them, to avoid populating the hash table
196 * with invalid information
198 if (S_ISGITLINK(active_cache
[i
]->ce_mode
))
201 lookup_blob(active_cache
[i
]->sha1
);
203 * We could add the blobs to the pending list, but quite
204 * frankly, we don't care. Once we've looked them up, and
205 * added them as objects, we've really done everything
206 * there is to do for a blob
209 if (active_cache_tree
)
210 add_cache_tree(active_cache_tree
, revs
);
213 void mark_reachable_objects(struct rev_info
*revs
, int mark_reflog
,
214 struct progress
*progress
)
216 struct connectivity_progress cp
;
219 * Set up revision parsing, and mark us as being interested
220 * in all object types, not just commits.
222 revs
->tag_objects
= 1;
223 revs
->blob_objects
= 1;
224 revs
->tree_objects
= 1;
226 /* Add all refs from the index file */
227 add_cache_refs(revs
);
229 /* Add all external refs */
230 for_each_ref(add_one_ref
, revs
);
232 /* detached HEAD is not included in the list above */
233 head_ref(add_one_ref
, revs
);
235 /* Add all reflog info */
237 for_each_reflog(add_one_reflog
, revs
);
239 cp
.progress
= progress
;
243 * Set up the revision walk - this will move all commits
244 * from the pending list to the commit walking list.
246 if (prepare_revision_walk(revs
))
247 die("revision walk setup failed");
248 walk_commit_list(revs
, &cp
);
249 display_progress(cp
.progress
, cp
.count
);