Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 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" |
Nguyễn Thái Ngọc Duy | dc34719 | 2011-11-05 19:00:08 +0700 | [diff] [blame] | 10 | #include "progress.h" |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 11 | |
Jeff King | 0b26abc | 2011-11-08 00:37:00 -0500 | [diff] [blame] | 12 | struct connectivity_progress { |
| 13 | struct progress *progress; |
| 14 | unsigned long count; |
| 15 | }; |
| 16 | |
| 17 | static void update_progress(struct connectivity_progress *cp) |
| 18 | { |
| 19 | cp->count++; |
| 20 | if ((cp->count & 1023) == 0) |
| 21 | display_progress(cp->progress, cp->count); |
| 22 | } |
| 23 | |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 24 | static void process_blob(struct blob *blob, |
| 25 | struct object_array *p, |
| 26 | struct name_path *path, |
Jeff King | 0b26abc | 2011-11-08 00:37:00 -0500 | [diff] [blame] | 27 | const char *name, |
| 28 | struct connectivity_progress *cp) |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 29 | { |
| 30 | struct object *obj = &blob->object; |
| 31 | |
Martin Koegler | f7de5a5 | 2008-02-18 21:48:00 +0100 | [diff] [blame] | 32 | if (!blob) |
| 33 | die("bad blob object"); |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 34 | if (obj->flags & SEEN) |
| 35 | return; |
| 36 | obj->flags |= SEEN; |
Jeff King | 0b26abc | 2011-11-08 00:37:00 -0500 | [diff] [blame] | 37 | update_progress(cp); |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 38 | /* Nothing to do, really .. The blob lookup was the important part */ |
| 39 | } |
| 40 | |
Andy Parkins | 8d2244b | 2007-07-02 13:56:58 +0100 | [diff] [blame] | 41 | static void process_gitlink(const unsigned char *sha1, |
| 42 | struct object_array *p, |
| 43 | struct name_path *path, |
| 44 | const char *name) |
| 45 | { |
| 46 | /* I don't think we want to recurse into this, really. */ |
| 47 | } |
| 48 | |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 49 | static void process_tree(struct tree *tree, |
| 50 | struct object_array *p, |
| 51 | struct name_path *path, |
Jeff King | 0b26abc | 2011-11-08 00:37:00 -0500 | [diff] [blame] | 52 | const char *name, |
| 53 | struct connectivity_progress *cp) |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 54 | { |
| 55 | struct object *obj = &tree->object; |
| 56 | struct tree_desc desc; |
| 57 | struct name_entry entry; |
| 58 | struct name_path me; |
| 59 | |
Martin Koegler | f7de5a5 | 2008-02-18 21:48:00 +0100 | [diff] [blame] | 60 | if (!tree) |
| 61 | die("bad tree object"); |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 62 | if (obj->flags & SEEN) |
| 63 | return; |
| 64 | obj->flags |= SEEN; |
Jeff King | 0b26abc | 2011-11-08 00:37:00 -0500 | [diff] [blame] | 65 | update_progress(cp); |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 66 | if (parse_tree(tree) < 0) |
| 67 | die("bad tree object %s", sha1_to_hex(obj->sha1)); |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 68 | add_object(obj, p, path, name); |
| 69 | me.up = path; |
| 70 | me.elem = name; |
| 71 | me.elem_len = strlen(name); |
| 72 | |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 73 | init_tree_desc(&desc, tree->buffer, tree->size); |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 74 | |
| 75 | while (tree_entry(&desc, &entry)) { |
| 76 | if (S_ISDIR(entry.mode)) |
Jeff King | 0b26abc | 2011-11-08 00:37:00 -0500 | [diff] [blame] | 77 | process_tree(lookup_tree(entry.sha1), p, &me, entry.path, cp); |
Junio C Hamano | b941ffa | 2007-07-02 16:41:44 -0700 | [diff] [blame] | 78 | else if (S_ISGITLINK(entry.mode)) |
Andy Parkins | 8d2244b | 2007-07-02 13:56:58 +0100 | [diff] [blame] | 79 | process_gitlink(entry.sha1, p, &me, entry.path); |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 80 | else |
Jeff King | 0b26abc | 2011-11-08 00:37:00 -0500 | [diff] [blame] | 81 | process_blob(lookup_blob(entry.sha1), p, &me, entry.path, cp); |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 82 | } |
| 83 | free(tree->buffer); |
| 84 | tree->buffer = NULL; |
| 85 | } |
| 86 | |
Jeff King | 0b26abc | 2011-11-08 00:37:00 -0500 | [diff] [blame] | 87 | static void process_tag(struct tag *tag, struct object_array *p, |
| 88 | const char *name, struct connectivity_progress *cp) |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 89 | { |
| 90 | struct object *obj = &tag->object; |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 91 | |
| 92 | if (obj->flags & SEEN) |
| 93 | return; |
| 94 | obj->flags |= SEEN; |
Jeff King | 0b26abc | 2011-11-08 00:37:00 -0500 | [diff] [blame] | 95 | update_progress(cp); |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 96 | |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 97 | if (parse_tag(tag) < 0) |
| 98 | die("bad tag object %s", sha1_to_hex(obj->sha1)); |
Martin Koegler | cc36934 | 2008-02-18 21:47:59 +0100 | [diff] [blame] | 99 | if (tag->tagged) |
| 100 | add_object(tag->tagged, p, NULL, name); |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Jeff King | 0b26abc | 2011-11-08 00:37:00 -0500 | [diff] [blame] | 103 | static void walk_commit_list(struct rev_info *revs, |
| 104 | struct connectivity_progress *cp) |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 105 | { |
| 106 | int i; |
| 107 | struct commit *commit; |
Thiago Farina | 3cd4745 | 2010-08-28 23:04:17 -0300 | [diff] [blame] | 108 | struct object_array objects = OBJECT_ARRAY_INIT; |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 109 | |
| 110 | /* Walk all commits, process their trees */ |
Nguyễn Thái Ngọc Duy | dc34719 | 2011-11-05 19:00:08 +0700 | [diff] [blame] | 111 | while ((commit = get_revision(revs)) != NULL) { |
Jeff King | 0b26abc | 2011-11-08 00:37:00 -0500 | [diff] [blame] | 112 | process_tree(commit->tree, &objects, NULL, "", cp); |
| 113 | update_progress(cp); |
Nguyễn Thái Ngọc Duy | dc34719 | 2011-11-05 19:00:08 +0700 | [diff] [blame] | 114 | } |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 115 | |
| 116 | /* Then walk all the pending objects, recursively processing them too */ |
| 117 | for (i = 0; i < revs->pending.nr; i++) { |
| 118 | struct object_array_entry *pending = revs->pending.objects + i; |
| 119 | struct object *obj = pending->item; |
| 120 | const char *name = pending->name; |
| 121 | if (obj->type == OBJ_TAG) { |
Jeff King | 0b26abc | 2011-11-08 00:37:00 -0500 | [diff] [blame] | 122 | process_tag((struct tag *) obj, &objects, name, cp); |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 123 | continue; |
| 124 | } |
| 125 | if (obj->type == OBJ_TREE) { |
Jeff King | 0b26abc | 2011-11-08 00:37:00 -0500 | [diff] [blame] | 126 | process_tree((struct tree *)obj, &objects, NULL, name, cp); |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 127 | continue; |
| 128 | } |
| 129 | if (obj->type == OBJ_BLOB) { |
Jeff King | 0b26abc | 2011-11-08 00:37:00 -0500 | [diff] [blame] | 130 | process_blob((struct blob *)obj, &objects, NULL, name, cp); |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 131 | continue; |
| 132 | } |
| 133 | die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name); |
| 134 | } |
| 135 | } |
| 136 | |
Johannes Schindelin | 883d60f | 2007-01-08 01:59:54 +0100 | [diff] [blame] | 137 | static int add_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1, |
| 138 | const char *email, unsigned long timestamp, int tz, |
| 139 | const char *message, void *cb_data) |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 140 | { |
| 141 | struct object *object; |
| 142 | struct rev_info *revs = (struct rev_info *)cb_data; |
| 143 | |
| 144 | object = parse_object(osha1); |
| 145 | if (object) |
| 146 | add_pending_object(revs, object, ""); |
| 147 | object = parse_object(nsha1); |
| 148 | if (object) |
| 149 | add_pending_object(revs, object, ""); |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | static int add_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data) |
| 154 | { |
| 155 | struct object *object = parse_object(sha1); |
| 156 | struct rev_info *revs = (struct rev_info *)cb_data; |
| 157 | |
| 158 | if (!object) |
| 159 | die("bad object ref: %s:%s", path, sha1_to_hex(sha1)); |
| 160 | add_pending_object(revs, object, ""); |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | static int add_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data) |
| 166 | { |
| 167 | for_each_reflog_ent(path, add_one_reflog_ent, cb_data); |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | static void add_one_tree(const unsigned char *sha1, struct rev_info *revs) |
| 172 | { |
| 173 | struct tree *tree = lookup_tree(sha1); |
Martin Koegler | c340663 | 2008-02-18 21:47:55 +0100 | [diff] [blame] | 174 | if (tree) |
| 175 | add_pending_object(revs, &tree->object, ""); |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | static void add_cache_tree(struct cache_tree *it, struct rev_info *revs) |
| 179 | { |
| 180 | int i; |
| 181 | |
| 182 | if (it->entry_count >= 0) |
| 183 | add_one_tree(it->sha1, revs); |
| 184 | for (i = 0; i < it->subtree_nr; i++) |
| 185 | add_cache_tree(it->down[i]->cache_tree, revs); |
| 186 | } |
| 187 | |
| 188 | static void add_cache_refs(struct rev_info *revs) |
| 189 | { |
| 190 | int i; |
| 191 | |
| 192 | read_cache(); |
| 193 | for (i = 0; i < active_nr; i++) { |
Andy Parkins | 8d2244b | 2007-07-02 13:56:58 +0100 | [diff] [blame] | 194 | /* |
| 195 | * The index can contain blobs and GITLINKs, GITLINKs are hashes |
| 196 | * that don't actually point to objects in the repository, it's |
| 197 | * almost guaranteed that they are NOT blobs, so we don't call |
| 198 | * lookup_blob() on them, to avoid populating the hash table |
| 199 | * with invalid information |
| 200 | */ |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 201 | if (S_ISGITLINK(active_cache[i]->ce_mode)) |
Andy Parkins | 8d2244b | 2007-07-02 13:56:58 +0100 | [diff] [blame] | 202 | continue; |
| 203 | |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 204 | lookup_blob(active_cache[i]->sha1); |
| 205 | /* |
| 206 | * We could add the blobs to the pending list, but quite |
| 207 | * frankly, we don't care. Once we've looked them up, and |
| 208 | * added them as objects, we've really done everything |
| 209 | * there is to do for a blob |
| 210 | */ |
| 211 | } |
| 212 | if (active_cache_tree) |
| 213 | add_cache_tree(active_cache_tree, revs); |
| 214 | } |
| 215 | |
Nguyễn Thái Ngọc Duy | dc34719 | 2011-11-05 19:00:08 +0700 | [diff] [blame] | 216 | void mark_reachable_objects(struct rev_info *revs, int mark_reflog, |
| 217 | struct progress *progress) |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 218 | { |
Jeff King | 0b26abc | 2011-11-08 00:37:00 -0500 | [diff] [blame] | 219 | struct connectivity_progress cp; |
| 220 | |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 221 | /* |
| 222 | * Set up revision parsing, and mark us as being interested |
| 223 | * in all object types, not just commits. |
| 224 | */ |
| 225 | revs->tag_objects = 1; |
| 226 | revs->blob_objects = 1; |
| 227 | revs->tree_objects = 1; |
| 228 | |
| 229 | /* Add all refs from the index file */ |
| 230 | add_cache_refs(revs); |
| 231 | |
| 232 | /* Add all external refs */ |
| 233 | for_each_ref(add_one_ref, revs); |
| 234 | |
Nicolas Pitre | eb8381c | 2007-02-03 13:25:43 -0500 | [diff] [blame] | 235 | /* Add all reflog info */ |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 236 | if (mark_reflog) |
Nicolas Pitre | eb8381c | 2007-02-03 13:25:43 -0500 | [diff] [blame] | 237 | for_each_reflog(add_one_reflog, revs); |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 238 | |
Jeff King | 0b26abc | 2011-11-08 00:37:00 -0500 | [diff] [blame] | 239 | cp.progress = progress; |
| 240 | cp.count = 0; |
| 241 | |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 242 | /* |
| 243 | * Set up the revision walk - this will move all commits |
| 244 | * from the pending list to the commit walking list. |
| 245 | */ |
Martin Koegler | 3d51e1b | 2008-02-18 08:31:56 +0100 | [diff] [blame] | 246 | if (prepare_revision_walk(revs)) |
| 247 | die("revision walk setup failed"); |
Jeff King | 0b26abc | 2011-11-08 00:37:00 -0500 | [diff] [blame] | 248 | walk_commit_list(revs, &cp); |
| 249 | display_progress(cp.progress, cp.count); |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 250 | } |