9 #include "cache-tree.h"
11 static void process_blob(struct blob
*blob
,
12 struct object_array
*p
,
13 struct name_path
*path
,
16 struct object
*obj
= &blob
->object
;
19 die("bad blob object");
20 if (obj
->flags
& SEEN
)
23 /* Nothing to do, really .. The blob lookup was the important part */
26 static void process_gitlink(const unsigned char *sha1
,
27 struct object_array
*p
,
28 struct name_path
*path
,
31 /* I don't think we want to recurse into this, really. */
34 static void process_tree(struct tree
*tree
,
35 struct object_array
*p
,
36 struct name_path
*path
,
39 struct object
*obj
= &tree
->object
;
40 struct tree_desc desc
;
41 struct name_entry entry
;
45 die("bad tree object");
46 if (obj
->flags
& SEEN
)
49 if (parse_tree(tree
) < 0)
50 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
51 add_object(obj
, p
, path
, name
);
54 me
.elem_len
= strlen(name
);
56 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
58 while (tree_entry(&desc
, &entry
)) {
59 if (S_ISDIR(entry
.mode
))
60 process_tree(lookup_tree(entry
.sha1
), p
, &me
, entry
.path
);
61 else if (S_ISGITLINK(entry
.mode
))
62 process_gitlink(entry
.sha1
, p
, &me
, entry
.path
);
64 process_blob(lookup_blob(entry
.sha1
), p
, &me
, entry
.path
);
70 static void process_tag(struct tag
*tag
, struct object_array
*p
, const char *name
)
72 struct object
*obj
= &tag
->object
;
75 if (obj
->flags
& SEEN
)
83 if (parse_tag(tag
) < 0)
84 die("bad tag object %s", sha1_to_hex(obj
->sha1
));
86 add_object(tag
->tagged
, p
, NULL
, name
);
89 static void walk_commit_list(struct rev_info
*revs
)
92 struct commit
*commit
;
93 struct object_array objects
= OBJECT_ARRAY_INIT
;
95 /* Walk all commits, process their trees */
96 while ((commit
= get_revision(revs
)) != NULL
)
97 process_tree(commit
->tree
, &objects
, NULL
, "");
99 /* Then walk all the pending objects, recursively processing them too */
100 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
101 struct object_array_entry
*pending
= revs
->pending
.objects
+ i
;
102 struct object
*obj
= pending
->item
;
103 const char *name
= pending
->name
;
104 if (obj
->type
== OBJ_TAG
) {
105 process_tag((struct tag
*) obj
, &objects
, name
);
108 if (obj
->type
== OBJ_TREE
) {
109 process_tree((struct tree
*)obj
, &objects
, NULL
, name
);
112 if (obj
->type
== OBJ_BLOB
) {
113 process_blob((struct blob
*)obj
, &objects
, NULL
, name
);
116 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
120 static int add_one_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
121 const char *email
, unsigned long timestamp
, int tz
,
122 const char *message
, void *cb_data
)
124 struct object
*object
;
125 struct rev_info
*revs
= (struct rev_info
*)cb_data
;
127 object
= parse_object(osha1
);
129 add_pending_object(revs
, object
, "");
130 object
= parse_object(nsha1
);
132 add_pending_object(revs
, object
, "");
136 static int add_one_ref(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
138 struct object
*object
= parse_object(sha1
);
139 struct rev_info
*revs
= (struct rev_info
*)cb_data
;
142 die("bad object ref: %s:%s", path
, sha1_to_hex(sha1
));
143 add_pending_object(revs
, object
, "");
148 static int add_one_reflog(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
150 for_each_reflog_ent(path
, add_one_reflog_ent
, cb_data
);
154 static void add_one_tree(const unsigned char *sha1
, struct rev_info
*revs
)
156 struct tree
*tree
= lookup_tree(sha1
);
158 add_pending_object(revs
, &tree
->object
, "");
161 static void add_cache_tree(struct cache_tree
*it
, struct rev_info
*revs
)
165 if (it
->entry_count
>= 0)
166 add_one_tree(it
->sha1
, revs
);
167 for (i
= 0; i
< it
->subtree_nr
; i
++)
168 add_cache_tree(it
->down
[i
]->cache_tree
, revs
);
171 static void add_cache_refs(struct rev_info
*revs
)
176 for (i
= 0; i
< active_nr
; i
++) {
178 * The index can contain blobs and GITLINKs, GITLINKs are hashes
179 * that don't actually point to objects in the repository, it's
180 * almost guaranteed that they are NOT blobs, so we don't call
181 * lookup_blob() on them, to avoid populating the hash table
182 * with invalid information
184 if (S_ISGITLINK(active_cache
[i
]->ce_mode
))
187 lookup_blob(active_cache
[i
]->sha1
);
189 * We could add the blobs to the pending list, but quite
190 * frankly, we don't care. Once we've looked them up, and
191 * added them as objects, we've really done everything
192 * there is to do for a blob
195 if (active_cache_tree
)
196 add_cache_tree(active_cache_tree
, revs
);
199 void mark_reachable_objects(struct rev_info
*revs
, int mark_reflog
)
202 * Set up revision parsing, and mark us as being interested
203 * in all object types, not just commits.
205 revs
->tag_objects
= 1;
206 revs
->blob_objects
= 1;
207 revs
->tree_objects
= 1;
209 /* Add all refs from the index file */
210 add_cache_refs(revs
);
212 /* Add all external refs */
213 for_each_ref(add_one_ref
, revs
);
215 /* Add all reflog info */
217 for_each_reflog(add_one_reflog
, revs
);
220 * Set up the revision walk - this will move all commits
221 * from the pending list to the commit walking list.
223 if (prepare_revision_walk(revs
))
224 die("revision walk setup failed");
225 walk_commit_list(revs
);