THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

252 lines
5.9KB

  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"
  12. #include "packfile.h"
  13. #include "worktree.h"
  14. #include "object-store.h"
  15. #include "pack-bitmap.h"
  16. struct connectivity_progress {
  17. struct progress *progress;
  18. unsigned long count;
  19. };
  20. static void update_progress(struct connectivity_progress *cp)
  21. {
  22. cp->count++;
  23. if ((cp->count & 1023) == 0)
  24. display_progress(cp->progress, cp->count);
  25. }
  26. static int add_one_ref(const char *path, const struct object_id *oid,
  27. int flag, void *cb_data)
  28. {
  29. struct rev_info *revs = (struct rev_info *)cb_data;
  30. struct object *object;
  31. if ((flag & REF_ISSYMREF) && (flag & REF_ISBROKEN)) {
  32. warning("symbolic ref is dangling: %s", path);
  33. return 0;
  34. }
  35. object = parse_object_or_die(oid, path);
  36. add_pending_object(revs, object, "");
  37. return 0;
  38. }
  39. /*
  40. * The traversal will have already marked us as SEEN, so we
  41. * only need to handle any progress reporting here.
  42. */
  43. static void mark_object(struct object *obj, const char *name, void *data)
  44. {
  45. update_progress(data);
  46. }
  47. static void mark_commit(struct commit *c, void *data)
  48. {
  49. mark_object(&c->object, NULL, data);
  50. }
  51. struct recent_data {
  52. struct rev_info *revs;
  53. timestamp_t timestamp;
  54. };
  55. static void add_recent_object(const struct object_id *oid,
  56. timestamp_t mtime,
  57. struct recent_data *data)
  58. {
  59. struct object *obj;
  60. enum object_type type;
  61. if (mtime <= data->timestamp)
  62. return;
  63. /*
  64. * We do not want to call parse_object here, because
  65. * inflating blobs and trees could be very expensive.
  66. * However, we do need to know the correct type for
  67. * later processing, and the revision machinery expects
  68. * commits and tags to have been parsed.
  69. */
  70. type = oid_object_info(the_repository, oid, NULL);
  71. if (type < 0)
  72. die("unable to get object info for %s", oid_to_hex(oid));
  73. switch (type) {
  74. case OBJ_TAG:
  75. case OBJ_COMMIT:
  76. obj = parse_object_or_die(oid, NULL);
  77. break;
  78. case OBJ_TREE:
  79. obj = (struct object *)lookup_tree(the_repository, oid);
  80. break;
  81. case OBJ_BLOB:
  82. obj = (struct object *)lookup_blob(the_repository, oid);
  83. break;
  84. default:
  85. die("unknown object type for %s: %s",
  86. oid_to_hex(oid), type_name(type));
  87. }
  88. if (!obj)
  89. die("unable to lookup %s", oid_to_hex(oid));
  90. add_pending_object(data->revs, obj, "");
  91. }
  92. static int add_recent_loose(const struct object_id *oid,
  93. const char *path, void *data)
  94. {
  95. struct stat st;
  96. struct object *obj = lookup_object(the_repository, oid);
  97. if (obj && obj->flags & SEEN)
  98. return 0;
  99. if (stat(path, &st) < 0) {
  100. /*
  101. * It's OK if an object went away during our iteration; this
  102. * could be due to a simultaneous repack. But anything else
  103. * we should abort, since we might then fail to mark objects
  104. * which should not be pruned.
  105. */
  106. if (errno == ENOENT)
  107. return 0;
  108. return error_errno("unable to stat %s", oid_to_hex(oid));
  109. }
  110. add_recent_object(oid, st.st_mtime, data);
  111. return 0;
  112. }
  113. static int add_recent_packed(const struct object_id *oid,
  114. struct packed_git *p, uint32_t pos,
  115. void *data)
  116. {
  117. struct object *obj = lookup_object(the_repository, oid);
  118. if (obj && obj->flags & SEEN)
  119. return 0;
  120. add_recent_object(oid, p->mtime, data);
  121. return 0;
  122. }
  123. int add_unseen_recent_objects_to_traversal(struct rev_info *revs,
  124. timestamp_t timestamp)
  125. {
  126. struct recent_data data;
  127. int r;
  128. data.revs = revs;
  129. data.timestamp = timestamp;
  130. r = for_each_loose_object(add_recent_loose, &data,
  131. FOR_EACH_OBJECT_LOCAL_ONLY);
  132. if (r)
  133. return r;
  134. return for_each_packed_object(add_recent_packed, &data,
  135. FOR_EACH_OBJECT_LOCAL_ONLY);
  136. }
  137. static void *lookup_object_by_type(struct repository *r,
  138. const struct object_id *oid,
  139. enum object_type type)
  140. {
  141. switch (type) {
  142. case OBJ_COMMIT:
  143. return lookup_commit(r, oid);
  144. case OBJ_TREE:
  145. return lookup_tree(r, oid);
  146. case OBJ_TAG:
  147. return lookup_tag(r, oid);
  148. case OBJ_BLOB:
  149. return lookup_blob(r, oid);
  150. default:
  151. die("BUG: unknown object type %d", type);
  152. }
  153. }
  154. static int mark_object_seen(const struct object_id *oid,
  155. enum object_type type,
  156. int exclude,
  157. uint32_t name_hash,
  158. struct packed_git *found_pack,
  159. off_t found_offset)
  160. {
  161. struct object *obj = lookup_object_by_type(the_repository, oid, type);
  162. if (!obj)
  163. die("unable to create object '%s'", oid_to_hex(oid));
  164. obj->flags |= SEEN;
  165. return 0;
  166. }
  167. void mark_reachable_objects(struct rev_info *revs, int mark_reflog,
  168. timestamp_t mark_recent, struct progress *progress)
  169. {
  170. struct connectivity_progress cp;
  171. struct bitmap_index *bitmap_git;
  172. /*
  173. * Set up revision parsing, and mark us as being interested
  174. * in all object types, not just commits.
  175. */
  176. revs->tag_objects = 1;
  177. revs->blob_objects = 1;
  178. revs->tree_objects = 1;
  179. /* Add all refs from the index file */
  180. add_index_objects_to_pending(revs, 0);
  181. /* Add all external refs */
  182. for_each_ref(add_one_ref, revs);
  183. /* detached HEAD is not included in the list above */
  184. head_ref(add_one_ref, revs);
  185. other_head_refs(add_one_ref, revs);
  186. /* Add all reflog info */
  187. if (mark_reflog)
  188. add_reflogs_to_pending(revs, 0);
  189. cp.progress = progress;
  190. cp.count = 0;
  191. bitmap_git = prepare_bitmap_walk(revs);
  192. if (bitmap_git) {
  193. traverse_bitmap_commit_list(bitmap_git, mark_object_seen);
  194. free_bitmap_index(bitmap_git);
  195. return;
  196. }
  197. /*
  198. * Set up the revision walk - this will move all commits
  199. * from the pending list to the commit walking list.
  200. */
  201. if (prepare_revision_walk(revs))
  202. die("revision walk setup failed");
  203. traverse_commit_list(revs, mark_commit, mark_object, &cp);
  204. if (mark_recent) {
  205. revs->ignore_missing_links = 1;
  206. if (add_unseen_recent_objects_to_traversal(revs, mark_recent))
  207. die("unable to mark recent objects");
  208. if (prepare_revision_walk(revs))
  209. die("revision walk setup failed");
  210. traverse_commit_list(revs, mark_commit, mark_object, &cp);
  211. }
  212. display_progress(cp.progress, cp.count);
  213. }