Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/walker.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / walker.c` 360 lines ยท 8306 bytes Raw Blame History 1 #define USE_THE_REPOSITORY_VARIABLE 2 3 #include "git-compat-util.h" 4 #include "gettext.h" 5 #include "hex.h" 6 #include "walker.h" 7 #include "repository.h" 8 #include "odb.h" 9 #include "commit.h" 10 #include "strbuf.h" 11 #include "tree.h" 12 #include "tree-walk.h" 13 #include "tag.h" 14 #include "blob.h" 15 #include "refs.h" 16 #include "progress.h" 17 #include "prio-queue.h" 18 19 static struct object_id current_commit_oid; 20 21 void walker_say(struct walker *walker, const char *fmt, ...) 22 { 23 if (walker->get_verbosely) { 24 va_list ap; 25 va_start(ap, fmt); 26 vfprintf(stderr, fmt, ap); 27 va_end(ap); 28 } 29 } 30 31 static void report_missing(const struct object *obj) 32 { 33 fprintf(stderr, "Cannot obtain needed %s %s\n", 34 obj->type ? type_name(obj->type): "object", 35 oid_to_hex(&obj->oid)); 36 if (!is_null_oid(¤t_commit_oid)) 37 fprintf(stderr, "while processing commit %s.\n", 38 oid_to_hex(¤t_commit_oid)); 39 } 40 41 static int process(struct walker *walker, struct object *obj); 42 43 static int process_tree(struct walker *walker, struct tree *tree) 44 { 45 struct tree_desc desc; 46 struct name_entry entry; 47 48 if (repo_parse_tree(the_repository, tree)) 49 return -1; 50 51 init_tree_desc(&desc, &tree->object.oid, tree->buffer, tree->size); 52 while (tree_entry(&desc, &entry)) { 53 struct object *obj = NULL; 54 55 /* submodule commits are not stored in the superproject */ 56 if (S_ISGITLINK(entry.mode)) 57 continue; 58 if (S_ISDIR(entry.mode)) { 59 struct tree *tree = lookup_tree(the_repository, 60 &entry.oid); 61 if (tree) 62 obj = &tree->object; 63 } 64 else { 65 struct blob *blob = lookup_blob(the_repository, 66 &entry.oid); 67 if (blob) 68 obj = &blob->object; 69 } 70 if (!obj || process(walker, obj)) 71 return -1; 72 } 73 free_tree_buffer(tree); 74 return 0; 75 } 76 77 /* Remember to update object flag allocation in object.h */ 78 #define COMPLETE (1U << 0) 79 #define SEEN (1U << 1) 80 #define TO_SCAN (1U << 2) 81 82 static struct prio_queue complete = { compare_commits_by_commit_date }; 83 84 static int process_commit(struct walker *walker, struct commit *commit) 85 { 86 struct commit_list *parents; 87 struct commit *item; 88 89 if (repo_parse_commit(the_repository, commit)) 90 return -1; 91 92 while ((item = prio_queue_peek(&complete))) { 93 if (item->date < commit->date) 94 break; 95 pop_most_recent_commit(&complete, COMPLETE); 96 } 97 98 if (commit->object.flags & COMPLETE) 99 return 0; 100 101 oidcpy(¤t_commit_oid, &commit->object.oid); 102 103 walker_say(walker, "walk %s\n", oid_to_hex(&commit->object.oid)); 104 105 if (process(walker, &repo_get_commit_tree(the_repository, commit)->object)) 106 return -1; 107 108 for (parents = commit->parents; parents; parents = parents->next) { 109 if (process(walker, &parents->item->object)) 110 return -1; 111 } 112 113 return 0; 114 } 115 116 static int process_tag(struct walker *walker, struct tag *tag) 117 { 118 if (parse_tag(the_repository, tag)) 119 return -1; 120 return process(walker, tag->tagged); 121 } 122 123 static struct object_list *process_queue = NULL; 124 static struct object_list **process_queue_end = &process_queue; 125 126 static int process_object(struct walker *walker, struct object *obj) 127 { 128 if (obj->type == OBJ_COMMIT) { 129 if (process_commit(walker, (struct commit *)obj)) 130 return -1; 131 return 0; 132 } 133 if (obj->type == OBJ_TREE) { 134 if (process_tree(walker, (struct tree *)obj)) 135 return -1; 136 return 0; 137 } 138 if (obj->type == OBJ_BLOB) { 139 return 0; 140 } 141 if (obj->type == OBJ_TAG) { 142 if (process_tag(walker, (struct tag *)obj)) 143 return -1; 144 return 0; 145 } 146 return error("Unable to determine requirements " 147 "of type %s for %s", 148 type_name(obj->type), oid_to_hex(&obj->oid)); 149 } 150 151 static int process(struct walker *walker, struct object *obj) 152 { 153 if (obj->flags & SEEN) 154 return 0; 155 obj->flags |= SEEN; 156 157 if (odb_has_object(the_repository->objects, &obj->oid, 158 ODB_HAS_OBJECT_RECHECK_PACKED | ODB_HAS_OBJECT_FETCH_PROMISOR)) { 159 /* We already have it, so we should scan it now. */ 160 obj->flags |= TO_SCAN; 161 } 162 else { 163 if (obj->flags & COMPLETE) 164 return 0; 165 walker->prefetch(walker, &obj->oid); 166 } 167 168 object_list_insert(obj, process_queue_end); 169 process_queue_end = &(*process_queue_end)->next; 170 return 0; 171 } 172 173 static int loop(struct walker *walker) 174 { 175 struct object_list *elem; 176 struct progress *progress = NULL; 177 uint64_t nr = 0; 178 179 if (walker->get_progress) 180 progress = start_delayed_progress(the_repository, 181 _("Fetching objects"), 0); 182 183 while (process_queue) { 184 struct object *obj = process_queue->item; 185 elem = process_queue; 186 process_queue = elem->next; 187 free(elem); 188 if (!process_queue) 189 process_queue_end = &process_queue; 190 191 /* If we are not scanning this object, we placed it in 192 * the queue because we needed to fetch it first. 193 */ 194 if (! (obj->flags & TO_SCAN)) { 195 if (walker->fetch(walker, &obj->oid)) { 196 stop_progress(&progress); 197 report_missing(obj); 198 return -1; 199 } 200 } 201 if (!obj->type) 202 parse_object(the_repository, &obj->oid); 203 if (process_object(walker, obj)) { 204 stop_progress(&progress); 205 return -1; 206 } 207 display_progress(progress, ++nr); 208 } 209 stop_progress(&progress); 210 return 0; 211 } 212 213 static int interpret_target(struct walker *walker, char *target, struct object_id *oid) 214 { 215 if (!get_oid_hex(target, oid)) 216 return 0; 217 if (!check_refname_format(target, 0)) { 218 struct ref *ref = alloc_ref(target); 219 if (!walker->fetch_ref(walker, ref)) { 220 oidcpy(oid, &ref->old_oid); 221 free(ref); 222 return 0; 223 } 224 free(ref); 225 } 226 return -1; 227 } 228 229 static int mark_complete(const struct reference *ref, void *cb_data UNUSED) 230 { 231 struct commit *commit = lookup_commit_reference_gently(the_repository, 232 ref->oid, 1); 233 234 if (commit) { 235 commit->object.flags |= COMPLETE; 236 prio_queue_put(&complete, commit); 237 } 238 return 0; 239 } 240 241 int walker_targets_stdin(char ***target, const char ***write_ref) 242 { 243 int targets = 0, targets_alloc = 0; 244 struct strbuf buf = STRBUF_INIT; 245 *target = NULL; *write_ref = NULL; 246 while (1) { 247 char *rf_one = NULL; 248 char *tg_one; 249 250 if (strbuf_getline_lf(&buf, stdin) == EOF) 251 break; 252 tg_one = buf.buf; 253 rf_one = strchr(tg_one, '\t'); 254 if (rf_one) 255 *rf_one++ = 0; 256 257 if (targets >= targets_alloc) { 258 targets_alloc = targets_alloc ? targets_alloc * 2 : 64; 259 REALLOC_ARRAY(*target, targets_alloc); 260 REALLOC_ARRAY(*write_ref, targets_alloc); 261 } 262 (*target)[targets] = xstrdup(tg_one); 263 (*write_ref)[targets] = xstrdup_or_null(rf_one); 264 targets++; 265 } 266 strbuf_release(&buf); 267 return targets; 268 } 269 270 void walker_targets_free(int targets, char **target, const char **write_ref) 271 { 272 while (targets--) { 273 free(target[targets]); 274 if (write_ref) 275 free((char *) write_ref[targets]); 276 } 277 } 278 279 int walker_fetch(struct walker *walker, int targets, char **target, 280 const char **write_ref, const char *write_ref_log_details) 281 { 282 struct strbuf refname = STRBUF_INIT; 283 struct strbuf err = STRBUF_INIT; 284 struct ref_transaction *transaction = NULL; 285 struct object_id *oids; 286 char *msg = NULL; 287 int i, ret = -1; 288 289 save_commit_buffer = 0; 290 291 ALLOC_ARRAY(oids, targets); 292 293 if (write_ref) { 294 transaction = ref_store_transaction_begin(get_main_ref_store(the_repository), 295 0, &err); 296 if (!transaction) { 297 error("%s", err.buf); 298 goto done; 299 } 300 } 301 302 if (!walker->get_recover) { 303 refs_for_each_ref(get_main_ref_store(the_repository), 304 mark_complete, NULL); 305 } 306 307 for (i = 0; i < targets; i++) { 308 if (interpret_target(walker, target[i], oids + i)) { 309 error("Could not interpret response from server '%s' as something to pull", target[i]); 310 goto done; 311 } 312 if (process(walker, lookup_unknown_object(the_repository, &oids[i]))) 313 goto done; 314 } 315 316 if (loop(walker)) 317 goto done; 318 if (!write_ref) { 319 ret = 0; 320 goto done; 321 } 322 if (write_ref_log_details) { 323 msg = xstrfmt("fetch from %s", write_ref_log_details); 324 } else { 325 msg = NULL; 326 } 327 for (i = 0; i < targets; i++) { 328 if (!write_ref[i]) 329 continue; 330 strbuf_reset(&refname); 331 strbuf_addf(&refname, "refs/%s", write_ref[i]); 332 if (ref_transaction_update(transaction, refname.buf, 333 oids + i, NULL, NULL, NULL, 0, 334 msg ? msg : "fetch (unknown)", 335 &err)) { 336 error("%s", err.buf); 337 goto done; 338 } 339 } 340 if (ref_transaction_commit(transaction, &err)) { 341 error("%s", err.buf); 342 goto done; 343 } 344 345 ret = 0; 346 347 done: 348 ref_transaction_free(transaction); 349 free(msg); 350 free(oids); 351 strbuf_release(&err); 352 strbuf_release(&refname); 353 return ret; 354 } 355 356 void walker_free(struct walker *walker) 357 { 358 walker->cleanup(walker); 359 free(walker); 360 }