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.

251 lines
6.3KB

  1. #include "cache.h"
  2. #include "cache-tree.h"
  3. #include "tree.h"
  4. #include "object-store.h"
  5. #include "blob.h"
  6. #include "commit.h"
  7. #include "tag.h"
  8. #include "alloc.h"
  9. #include "tree-walk.h"
  10. #include "repository.h"
  11. const char *tree_type = "tree";
  12. static int read_one_entry_opt(struct index_state *istate,
  13. const struct object_id *oid,
  14. const char *base, int baselen,
  15. const char *pathname,
  16. unsigned mode, int stage, int opt)
  17. {
  18. int len;
  19. struct cache_entry *ce;
  20. if (S_ISDIR(mode))
  21. return READ_TREE_RECURSIVE;
  22. len = strlen(pathname);
  23. ce = make_empty_cache_entry(istate, baselen + len);
  24. ce->ce_mode = create_ce_mode(mode);
  25. ce->ce_flags = create_ce_flags(stage);
  26. ce->ce_namelen = baselen + len;
  27. memcpy(ce->name, base, baselen);
  28. memcpy(ce->name + baselen, pathname, len+1);
  29. oidcpy(&ce->oid, oid);
  30. return add_index_entry(istate, ce, opt);
  31. }
  32. static int read_one_entry(const struct object_id *oid, struct strbuf *base,
  33. const char *pathname, unsigned mode, int stage,
  34. void *context)
  35. {
  36. struct index_state *istate = context;
  37. return read_one_entry_opt(istate, oid, base->buf, base->len, pathname,
  38. mode, stage,
  39. ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
  40. }
  41. /*
  42. * This is used when the caller knows there is no existing entries at
  43. * the stage that will conflict with the entry being added.
  44. */
  45. static int read_one_entry_quick(const struct object_id *oid, struct strbuf *base,
  46. const char *pathname, unsigned mode, int stage,
  47. void *context)
  48. {
  49. struct index_state *istate = context;
  50. return read_one_entry_opt(istate, oid, base->buf, base->len, pathname,
  51. mode, stage,
  52. ADD_CACHE_JUST_APPEND);
  53. }
  54. static int read_tree_1(struct repository *r,
  55. struct tree *tree, struct strbuf *base,
  56. int stage, const struct pathspec *pathspec,
  57. read_tree_fn_t fn, void *context)
  58. {
  59. struct tree_desc desc;
  60. struct name_entry entry;
  61. struct object_id oid;
  62. int len, oldlen = base->len;
  63. enum interesting retval = entry_not_interesting;
  64. if (parse_tree(tree))
  65. return -1;
  66. init_tree_desc(&desc, tree->buffer, tree->size);
  67. while (tree_entry(&desc, &entry)) {
  68. if (retval != all_entries_interesting) {
  69. retval = tree_entry_interesting(r->index, &entry,
  70. base, 0, pathspec);
  71. if (retval == all_entries_not_interesting)
  72. break;
  73. if (retval == entry_not_interesting)
  74. continue;
  75. }
  76. switch (fn(&entry.oid, base,
  77. entry.path, entry.mode, stage, context)) {
  78. case 0:
  79. continue;
  80. case READ_TREE_RECURSIVE:
  81. break;
  82. default:
  83. return -1;
  84. }
  85. if (S_ISDIR(entry.mode))
  86. oidcpy(&oid, &entry.oid);
  87. else if (S_ISGITLINK(entry.mode)) {
  88. struct commit *commit;
  89. commit = lookup_commit(r, &entry.oid);
  90. if (!commit)
  91. die("Commit %s in submodule path %s%s not found",
  92. oid_to_hex(&entry.oid),
  93. base->buf, entry.path);
  94. if (parse_commit(commit))
  95. die("Invalid commit %s in submodule path %s%s",
  96. oid_to_hex(&entry.oid),
  97. base->buf, entry.path);
  98. oidcpy(&oid, get_commit_tree_oid(commit));
  99. }
  100. else
  101. continue;
  102. len = tree_entry_len(&entry);
  103. strbuf_add(base, entry.path, len);
  104. strbuf_addch(base, '/');
  105. retval = read_tree_1(r, lookup_tree(r, &oid),
  106. base, stage, pathspec,
  107. fn, context);
  108. strbuf_setlen(base, oldlen);
  109. if (retval)
  110. return -1;
  111. }
  112. return 0;
  113. }
  114. int read_tree_recursive(struct repository *r,
  115. struct tree *tree,
  116. const char *base, int baselen,
  117. int stage, const struct pathspec *pathspec,
  118. read_tree_fn_t fn, void *context)
  119. {
  120. struct strbuf sb = STRBUF_INIT;
  121. int ret;
  122. strbuf_add(&sb, base, baselen);
  123. ret = read_tree_1(r, tree, &sb, stage, pathspec, fn, context);
  124. strbuf_release(&sb);
  125. return ret;
  126. }
  127. static int cmp_cache_name_compare(const void *a_, const void *b_)
  128. {
  129. const struct cache_entry *ce1, *ce2;
  130. ce1 = *((const struct cache_entry **)a_);
  131. ce2 = *((const struct cache_entry **)b_);
  132. return cache_name_stage_compare(ce1->name, ce1->ce_namelen, ce_stage(ce1),
  133. ce2->name, ce2->ce_namelen, ce_stage(ce2));
  134. }
  135. int read_tree(struct repository *r, struct tree *tree, int stage,
  136. struct pathspec *match, struct index_state *istate)
  137. {
  138. read_tree_fn_t fn = NULL;
  139. int i, err;
  140. /*
  141. * Currently the only existing callers of this function all
  142. * call it with stage=1 and after making sure there is nothing
  143. * at that stage; we could always use read_one_entry_quick().
  144. *
  145. * But when we decide to straighten out git-read-tree not to
  146. * use unpack_trees() in some cases, this will probably start
  147. * to matter.
  148. */
  149. /*
  150. * See if we have cache entry at the stage. If so,
  151. * do it the original slow way, otherwise, append and then
  152. * sort at the end.
  153. */
  154. for (i = 0; !fn && i < istate->cache_nr; i++) {
  155. const struct cache_entry *ce = istate->cache[i];
  156. if (ce_stage(ce) == stage)
  157. fn = read_one_entry;
  158. }
  159. if (!fn)
  160. fn = read_one_entry_quick;
  161. err = read_tree_recursive(r, tree, "", 0, stage, match, fn, istate);
  162. if (fn == read_one_entry || err)
  163. return err;
  164. /*
  165. * Sort the cache entry -- we need to nuke the cache tree, though.
  166. */
  167. cache_tree_free(&istate->cache_tree);
  168. QSORT(istate->cache, istate->cache_nr, cmp_cache_name_compare);
  169. return 0;
  170. }
  171. struct tree *lookup_tree(struct repository *r, const struct object_id *oid)
  172. {
  173. struct object *obj = lookup_object(r, oid);
  174. if (!obj)
  175. return create_object(r, oid, alloc_tree_node(r));
  176. return object_as_type(r, obj, OBJ_TREE, 0);
  177. }
  178. int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
  179. {
  180. if (item->object.parsed)
  181. return 0;
  182. item->object.parsed = 1;
  183. item->buffer = buffer;
  184. item->size = size;
  185. return 0;
  186. }
  187. int parse_tree_gently(struct tree *item, int quiet_on_missing)
  188. {
  189. enum object_type type;
  190. void *buffer;
  191. unsigned long size;
  192. if (item->object.parsed)
  193. return 0;
  194. buffer = read_object_file(&item->object.oid, &type, &size);
  195. if (!buffer)
  196. return quiet_on_missing ? -1 :
  197. error("Could not read %s",
  198. oid_to_hex(&item->object.oid));
  199. if (type != OBJ_TREE) {
  200. free(buffer);
  201. return error("Object %s not a tree",
  202. oid_to_hex(&item->object.oid));
  203. }
  204. return parse_tree_buffer(item, buffer, size);
  205. }
  206. void free_tree_buffer(struct tree *tree)
  207. {
  208. FREE_AND_NULL(tree->buffer);
  209. tree->size = 0;
  210. tree->object.parsed = 0;
  211. }
  212. struct tree *parse_tree_indirect(const struct object_id *oid)
  213. {
  214. struct repository *r = the_repository;
  215. struct object *obj = parse_object(r, oid);
  216. return (struct tree *)repo_peel_to_type(r, NULL, 0, obj, OBJ_TREE);
  217. }