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/tree.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 / tree.c` 220 lines ยท 5343 bytes Raw Blame History 1 #include "git-compat-util.h" 2 #include "hex.h" 3 #include "tree.h" 4 #include "object-name.h" 5 #include "odb.h" 6 #include "commit.h" 7 #include "alloc.h" 8 #include "tree-walk.h" 9 #include "repository.h" 10 #include "environment.h" 11 12 const char *tree_type = "tree"; 13 14 int read_tree_at(struct repository *r, 15 struct tree *tree, struct strbuf *base, 16 int depth, 17 const struct pathspec *pathspec, 18 read_tree_fn_t fn, void *context) 19 { 20 struct tree_desc desc; 21 struct name_entry entry; 22 struct object_id oid; 23 int len, oldlen = base->len; 24 enum interesting retval = entry_not_interesting; 25 26 if (depth > r->settings.max_allowed_tree_depth) 27 return error("exceeded maximum allowed tree depth"); 28 29 if (repo_parse_tree(r, tree)) 30 return -1; 31 32 init_tree_desc(&desc, &tree->object.oid, tree->buffer, tree->size); 33 34 while (tree_entry(&desc, &entry)) { 35 if (retval != all_entries_interesting) { 36 retval = tree_entry_interesting(r->index, &entry, 37 base, pathspec); 38 if (retval == all_entries_not_interesting) 39 break; 40 if (retval == entry_not_interesting) 41 continue; 42 } 43 44 switch (fn(&entry.oid, base, 45 entry.path, entry.mode, context)) { 46 case 0: 47 continue; 48 case READ_TREE_RECURSIVE: 49 break; 50 default: 51 return -1; 52 } 53 54 if (S_ISDIR(entry.mode)) 55 oidcpy(&oid, &entry.oid); 56 else if (S_ISGITLINK(entry.mode)) { 57 struct commit *commit; 58 59 commit = lookup_commit(r, &entry.oid); 60 if (!commit) 61 die("Commit %s in submodule path %s%s not found", 62 oid_to_hex(&entry.oid), 63 base->buf, entry.path); 64 65 if (repo_parse_commit(r, commit)) 66 die("Invalid commit %s in submodule path %s%s", 67 oid_to_hex(&entry.oid), 68 base->buf, entry.path); 69 70 oidcpy(&oid, get_commit_tree_oid(commit)); 71 } 72 else 73 continue; 74 75 len = tree_entry_len(&entry); 76 strbuf_add(base, entry.path, len); 77 strbuf_addch(base, '/'); 78 retval = read_tree_at(r, lookup_tree(r, &oid), 79 base, depth + 1, pathspec, 80 fn, context); 81 strbuf_setlen(base, oldlen); 82 if (retval) 83 return -1; 84 } 85 return 0; 86 } 87 88 int read_tree(struct repository *r, 89 struct tree *tree, 90 const struct pathspec *pathspec, 91 read_tree_fn_t fn, void *context) 92 { 93 struct strbuf sb = STRBUF_INIT; 94 int ret = read_tree_at(r, tree, &sb, 0, pathspec, fn, context); 95 strbuf_release(&sb); 96 return ret; 97 } 98 99 int base_name_compare(const char *name1, size_t len1, int mode1, 100 const char *name2, size_t len2, int mode2) 101 { 102 unsigned char c1, c2; 103 size_t len = len1 < len2 ? len1 : len2; 104 int cmp; 105 106 cmp = memcmp(name1, name2, len); 107 if (cmp) 108 return cmp; 109 c1 = name1[len]; 110 c2 = name2[len]; 111 if (!c1 && S_ISDIR(mode1)) 112 c1 = '/'; 113 if (!c2 && S_ISDIR(mode2)) 114 c2 = '/'; 115 return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0; 116 } 117 118 /* 119 * df_name_compare() is identical to base_name_compare(), except it 120 * compares conflicting directory/file entries as equal. Note that 121 * while a directory name compares as equal to a regular file, they 122 * then individually compare _differently_ to a filename that has 123 * a dot after the basename (because '\0' < '.' < '/'). 124 * 125 * This is used by routines that want to traverse the git namespace 126 * but then handle conflicting entries together when possible. 127 */ 128 int df_name_compare(const char *name1, size_t len1, int mode1, 129 const char *name2, size_t len2, int mode2) 130 { 131 unsigned char c1, c2; 132 size_t len = len1 < len2 ? len1 : len2; 133 int cmp; 134 135 cmp = memcmp(name1, name2, len); 136 if (cmp) 137 return cmp; 138 /* Directories and files compare equal (same length, same name) */ 139 if (len1 == len2) 140 return 0; 141 c1 = name1[len]; 142 if (!c1 && S_ISDIR(mode1)) 143 c1 = '/'; 144 c2 = name2[len]; 145 if (!c2 && S_ISDIR(mode2)) 146 c2 = '/'; 147 if (c1 == '/' && !c2) 148 return 0; 149 if (c2 == '/' && !c1) 150 return 0; 151 return c1 - c2; 152 } 153 154 int name_compare(const char *name1, size_t len1, const char *name2, size_t len2) 155 { 156 size_t min_len = (len1 < len2) ? len1 : len2; 157 int cmp = memcmp(name1, name2, min_len); 158 if (cmp) 159 return cmp; 160 if (len1 < len2) 161 return -1; 162 if (len1 > len2) 163 return 1; 164 return 0; 165 } 166 167 struct tree *lookup_tree(struct repository *r, const struct object_id *oid) 168 { 169 struct object *obj = lookup_object(r, oid); 170 if (!obj) 171 return create_object(r, oid, alloc_tree_node(r)); 172 return object_as_type(obj, OBJ_TREE, 0); 173 } 174 175 int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size) 176 { 177 if (item->object.parsed) 178 return 0; 179 item->object.parsed = 1; 180 item->buffer = buffer; 181 item->size = size; 182 183 return 0; 184 } 185 186 int repo_parse_tree_gently(struct repository *r, struct tree *item, 187 int quiet_on_missing) 188 { 189 enum object_type type; 190 void *buffer; 191 size_t size; 192 193 if (item->object.parsed) 194 return 0; 195 buffer = odb_read_object(r->objects, &item->object.oid, &type, &size); 196 if (!buffer) 197 return quiet_on_missing ? -1 : 198 error("Could not read %s", 199 oid_to_hex(&item->object.oid)); 200 if (type != OBJ_TREE) { 201 free(buffer); 202 return error("Object %s not a tree", 203 oid_to_hex(&item->object.oid)); 204 } 205 return parse_tree_buffer(item, buffer, size); 206 } 207 208 void free_tree_buffer(struct tree *tree) 209 { 210 FREE_AND_NULL(tree->buffer); 211 tree->size = 0; 212 tree->object.parsed = 0; 213 } 214 215 struct tree *repo_parse_tree_indirect(struct repository *r, 216 const struct object_id *oid) 217 { 218 struct object *obj = parse_object(r, oid); 219 return (struct tree *)repo_peel_to_type(r, NULL, 0, obj, OBJ_TREE); 220 }