]> asedeno.scripts.mit.edu Git - git.git/blob - walker.c
Modularize commit-walker
[git.git] / walker.c
1 #include "cache.h"
2 #include "walker.h"
3 #include "commit.h"
4 #include "tree.h"
5 #include "tree-walk.h"
6 #include "tag.h"
7 #include "blob.h"
8 #include "refs.h"
9 #include "strbuf.h"
10
11 static unsigned char current_commit_sha1[20];
12
13 void walker_say(struct walker *walker, const char *fmt, const char *hex)
14 {
15         if (walker->get_verbosely)
16                 fprintf(stderr, fmt, hex);
17 }
18
19 static void report_missing(const struct object *obj)
20 {
21         char missing_hex[41];
22         strcpy(missing_hex, sha1_to_hex(obj->sha1));;
23         fprintf(stderr, "Cannot obtain needed %s %s\n",
24                 obj->type ? typename(obj->type): "object", missing_hex);
25         if (!is_null_sha1(current_commit_sha1))
26                 fprintf(stderr, "while processing commit %s.\n",
27                         sha1_to_hex(current_commit_sha1));
28 }
29
30 static int process(struct walker *walker, struct object *obj);
31
32 static int process_tree(struct walker *walker, struct tree *tree)
33 {
34         struct tree_desc desc;
35         struct name_entry entry;
36
37         if (parse_tree(tree))
38                 return -1;
39
40         init_tree_desc(&desc, tree->buffer, tree->size);
41         while (tree_entry(&desc, &entry)) {
42                 struct object *obj = NULL;
43
44                 /* submodule commits are not stored in the superproject */
45                 if (S_ISGITLINK(entry.mode))
46                         continue;
47                 if (S_ISDIR(entry.mode)) {
48                         struct tree *tree = lookup_tree(entry.sha1);
49                         if (tree)
50                                 obj = &tree->object;
51                 }
52                 else {
53                         struct blob *blob = lookup_blob(entry.sha1);
54                         if (blob)
55                                 obj = &blob->object;
56                 }
57                 if (!obj || process(walker, obj))
58                         return -1;
59         }
60         free(tree->buffer);
61         tree->buffer = NULL;
62         tree->size = 0;
63         return 0;
64 }
65
66 #define COMPLETE        (1U << 0)
67 #define SEEN            (1U << 1)
68 #define TO_SCAN         (1U << 2)
69
70 static struct commit_list *complete = NULL;
71
72 static int process_commit(struct walker *walker, struct commit *commit)
73 {
74         if (parse_commit(commit))
75                 return -1;
76
77         while (complete && complete->item->date >= commit->date) {
78                 pop_most_recent_commit(&complete, COMPLETE);
79         }
80
81         if (commit->object.flags & COMPLETE)
82                 return 0;
83
84         hashcpy(current_commit_sha1, commit->object.sha1);
85
86         walker_say(walker, "walk %s\n", sha1_to_hex(commit->object.sha1));
87
88         if (walker->get_tree) {
89                 if (process(walker, &commit->tree->object))
90                         return -1;
91                 if (!walker->get_all)
92                         walker->get_tree = 0;
93         }
94         if (walker->get_history) {
95                 struct commit_list *parents = commit->parents;
96                 for (; parents; parents = parents->next) {
97                         if (process(walker, &parents->item->object))
98                                 return -1;
99                 }
100         }
101         return 0;
102 }
103
104 static int process_tag(struct walker *walker, struct tag *tag)
105 {
106         if (parse_tag(tag))
107                 return -1;
108         return process(walker, tag->tagged);
109 }
110
111 static struct object_list *process_queue = NULL;
112 static struct object_list **process_queue_end = &process_queue;
113
114 static int process_object(struct walker *walker, struct object *obj)
115 {
116         if (obj->type == OBJ_COMMIT) {
117                 if (process_commit(walker, (struct commit *)obj))
118                         return -1;
119                 return 0;
120         }
121         if (obj->type == OBJ_TREE) {
122                 if (process_tree(walker, (struct tree *)obj))
123                         return -1;
124                 return 0;
125         }
126         if (obj->type == OBJ_BLOB) {
127                 return 0;
128         }
129         if (obj->type == OBJ_TAG) {
130                 if (process_tag(walker, (struct tag *)obj))
131                         return -1;
132                 return 0;
133         }
134         return error("Unable to determine requirements "
135                      "of type %s for %s",
136                      typename(obj->type), sha1_to_hex(obj->sha1));
137 }
138
139 static int process(struct walker *walker, struct object *obj)
140 {
141         if (obj->flags & SEEN)
142                 return 0;
143         obj->flags |= SEEN;
144
145         if (has_sha1_file(obj->sha1)) {
146                 /* We already have it, so we should scan it now. */
147                 obj->flags |= TO_SCAN;
148         }
149         else {
150                 if (obj->flags & COMPLETE)
151                         return 0;
152                 walker->prefetch(walker, obj->sha1);
153         }
154
155         object_list_insert(obj, process_queue_end);
156         process_queue_end = &(*process_queue_end)->next;
157         return 0;
158 }
159
160 static int loop(struct walker *walker)
161 {
162         struct object_list *elem;
163
164         while (process_queue) {
165                 struct object *obj = process_queue->item;
166                 elem = process_queue;
167                 process_queue = elem->next;
168                 free(elem);
169                 if (!process_queue)
170                         process_queue_end = &process_queue;
171
172                 /* If we are not scanning this object, we placed it in
173                  * the queue because we needed to fetch it first.
174                  */
175                 if (! (obj->flags & TO_SCAN)) {
176                         if (walker->fetch(walker, obj->sha1)) {
177                                 report_missing(obj);
178                                 return -1;
179                         }
180                 }
181                 if (!obj->type)
182                         parse_object(obj->sha1);
183                 if (process_object(walker, obj))
184                         return -1;
185         }
186         return 0;
187 }
188
189 static int interpret_target(struct walker *walker, char *target, unsigned char *sha1)
190 {
191         if (!get_sha1_hex(target, sha1))
192                 return 0;
193         if (!check_ref_format(target)) {
194                 if (!walker->fetch_ref(walker, target, sha1)) {
195                         return 0;
196                 }
197         }
198         return -1;
199 }
200
201 static int mark_complete(const char *path, const unsigned char *sha1, int flag, void *cb_data)
202 {
203         struct commit *commit = lookup_commit_reference_gently(sha1, 1);
204         if (commit) {
205                 commit->object.flags |= COMPLETE;
206                 insert_by_date(commit, &complete);
207         }
208         return 0;
209 }
210
211 int walker_targets_stdin(char ***target, const char ***write_ref)
212 {
213         int targets = 0, targets_alloc = 0;
214         struct strbuf buf;
215         *target = NULL; *write_ref = NULL;
216         strbuf_init(&buf);
217         while (1) {
218                 char *rf_one = NULL;
219                 char *tg_one;
220
221                 read_line(&buf, stdin, '\n');
222                 if (buf.eof)
223                         break;
224                 tg_one = buf.buf;
225                 rf_one = strchr(tg_one, '\t');
226                 if (rf_one)
227                         *rf_one++ = 0;
228
229                 if (targets >= targets_alloc) {
230                         targets_alloc = targets_alloc ? targets_alloc * 2 : 64;
231                         *target = xrealloc(*target, targets_alloc * sizeof(**target));
232                         *write_ref = xrealloc(*write_ref, targets_alloc * sizeof(**write_ref));
233                 }
234                 (*target)[targets] = xstrdup(tg_one);
235                 (*write_ref)[targets] = rf_one ? xstrdup(rf_one) : NULL;
236                 targets++;
237         }
238         return targets;
239 }
240
241 void walker_targets_free(int targets, char **target, const char **write_ref)
242 {
243         while (targets--) {
244                 free(target[targets]);
245                 if (write_ref && write_ref[targets])
246                         free((char *) write_ref[targets]);
247         }
248 }
249
250 int walker_fetch(struct walker *walker, int targets, char **target,
251                  const char **write_ref, const char *write_ref_log_details)
252 {
253         struct ref_lock **lock = xcalloc(targets, sizeof(struct ref_lock *));
254         unsigned char *sha1 = xmalloc(targets * 20);
255         char *msg;
256         int ret;
257         int i;
258
259         save_commit_buffer = 0;
260         track_object_refs = 0;
261
262         for (i = 0; i < targets; i++) {
263                 if (!write_ref || !write_ref[i])
264                         continue;
265
266                 lock[i] = lock_ref_sha1(write_ref[i], NULL);
267                 if (!lock[i]) {
268                         error("Can't lock ref %s", write_ref[i]);
269                         goto unlock_and_fail;
270                 }
271         }
272
273         if (!walker->get_recover)
274                 for_each_ref(mark_complete, NULL);
275
276         for (i = 0; i < targets; i++) {
277                 if (interpret_target(walker, target[i], &sha1[20 * i])) {
278                         error("Could not interpret %s as something to pull", target[i]);
279                         goto unlock_and_fail;
280                 }
281                 if (process(walker, lookup_unknown_object(&sha1[20 * i])))
282                         goto unlock_and_fail;
283         }
284
285         if (loop(walker))
286                 goto unlock_and_fail;
287
288         if (write_ref_log_details) {
289                 msg = xmalloc(strlen(write_ref_log_details) + 12);
290                 sprintf(msg, "fetch from %s", write_ref_log_details);
291         } else {
292                 msg = NULL;
293         }
294         for (i = 0; i < targets; i++) {
295                 if (!write_ref || !write_ref[i])
296                         continue;
297                 ret = write_ref_sha1(lock[i], &sha1[20 * i], msg ? msg : "fetch (unknown)");
298                 lock[i] = NULL;
299                 if (ret)
300                         goto unlock_and_fail;
301         }
302         free(msg);
303
304         return 0;
305
306 unlock_and_fail:
307         for (i = 0; i < targets; i++)
308                 if (lock[i])
309                         unlock_ref(lock[i]);
310
311         return -1;
312 }
313
314 void walker_free(struct walker *walker)
315 {
316         walker->cleanup(walker);
317         free(walker);
318 }