xdiff: add recs_match helper function
[git/git-svn.git] / reachable.c
blob9b0295469f562998ecffb8c8608d887c973bbce5
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"
13 struct connectivity_progress {
14 struct progress *progress;
15 unsigned long count;
18 static void update_progress(struct connectivity_progress *cp)
20 cp->count++;
21 if ((cp->count & 1023) == 0)
22 display_progress(cp->progress, cp->count);
25 static int add_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
27 struct object *object = parse_object_or_die(sha1, path);
28 struct rev_info *revs = (struct rev_info *)cb_data;
30 add_pending_object(revs, object, "");
32 return 0;
36 * The traversal will have already marked us as SEEN, so we
37 * only need to handle any progress reporting here.
39 static void mark_object(struct object *obj, const char *name, void *data)
41 update_progress(data);
44 static void mark_commit(struct commit *c, void *data)
46 mark_object(&c->object, NULL, data);
49 struct recent_data {
50 struct rev_info *revs;
51 unsigned long timestamp;
54 static void add_recent_object(const unsigned char *sha1,
55 unsigned long mtime,
56 struct recent_data *data)
58 struct object *obj;
59 enum object_type type;
61 if (mtime <= data->timestamp)
62 return;
65 * We do not want to call parse_object here, because
66 * inflating blobs and trees could be very expensive.
67 * However, we do need to know the correct type for
68 * later processing, and the revision machinery expects
69 * commits and tags to have been parsed.
71 type = sha1_object_info(sha1, NULL);
72 if (type < 0)
73 die("unable to get object info for %s", sha1_to_hex(sha1));
75 switch (type) {
76 case OBJ_TAG:
77 case OBJ_COMMIT:
78 obj = parse_object_or_die(sha1, NULL);
79 break;
80 case OBJ_TREE:
81 obj = (struct object *)lookup_tree(sha1);
82 break;
83 case OBJ_BLOB:
84 obj = (struct object *)lookup_blob(sha1);
85 break;
86 default:
87 die("unknown object type for %s: %s",
88 sha1_to_hex(sha1), typename(type));
91 if (!obj)
92 die("unable to lookup %s", sha1_to_hex(sha1));
94 add_pending_object(data->revs, obj, "");
97 static int add_recent_loose(const unsigned char *sha1,
98 const char *path, void *data)
100 struct stat st;
101 struct object *obj = lookup_object(sha1);
103 if (obj && obj->flags & SEEN)
104 return 0;
106 if (stat(path, &st) < 0) {
108 * It's OK if an object went away during our iteration; this
109 * could be due to a simultaneous repack. But anything else
110 * we should abort, since we might then fail to mark objects
111 * which should not be pruned.
113 if (errno == ENOENT)
114 return 0;
115 return error("unable to stat %s: %s",
116 sha1_to_hex(sha1), strerror(errno));
119 add_recent_object(sha1, st.st_mtime, data);
120 return 0;
123 static int add_recent_packed(const unsigned char *sha1,
124 struct packed_git *p, uint32_t pos,
125 void *data)
127 struct object *obj = lookup_object(sha1);
129 if (obj && obj->flags & SEEN)
130 return 0;
131 add_recent_object(sha1, p->mtime, data);
132 return 0;
135 int add_unseen_recent_objects_to_traversal(struct rev_info *revs,
136 unsigned long timestamp)
138 struct recent_data data;
139 int r;
141 data.revs = revs;
142 data.timestamp = timestamp;
144 r = for_each_loose_object(add_recent_loose, &data,
145 FOR_EACH_OBJECT_LOCAL_ONLY);
146 if (r)
147 return r;
148 return for_each_packed_object(add_recent_packed, &data,
149 FOR_EACH_OBJECT_LOCAL_ONLY);
152 void mark_reachable_objects(struct rev_info *revs, int mark_reflog,
153 unsigned long mark_recent,
154 struct progress *progress)
156 struct connectivity_progress cp;
159 * Set up revision parsing, and mark us as being interested
160 * in all object types, not just commits.
162 revs->tag_objects = 1;
163 revs->blob_objects = 1;
164 revs->tree_objects = 1;
166 /* Add all refs from the index file */
167 add_index_objects_to_pending(revs, 0);
169 /* Add all external refs */
170 for_each_ref(add_one_ref, revs);
172 /* detached HEAD is not included in the list above */
173 head_ref(add_one_ref, revs);
175 /* Add all reflog info */
176 if (mark_reflog)
177 add_reflogs_to_pending(revs, 0);
179 cp.progress = progress;
180 cp.count = 0;
183 * Set up the revision walk - this will move all commits
184 * from the pending list to the commit walking list.
186 if (prepare_revision_walk(revs))
187 die("revision walk setup failed");
188 traverse_commit_list(revs, mark_commit, mark_object, &cp);
190 if (mark_recent) {
191 revs->ignore_missing_links = 1;
192 if (add_unseen_recent_objects_to_traversal(revs, mark_recent))
193 die("unable to mark recent objects");
194 if (prepare_revision_walk(revs))
195 die("revision walk setup failed");
196 traverse_commit_list(revs, mark_commit, mark_object, &cp);
199 display_progress(cp.progress, cp.count);