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.

278 lines
6.5KB

  1. /*
  2. * not really _using_ the compat macros, just make sure the_index
  3. * declaration matches the definition in this file.
  4. */
  5. #define USE_THE_INDEX_COMPATIBILITY_MACROS
  6. #include "cache.h"
  7. #include "repository.h"
  8. #include "object-store.h"
  9. #include "config.h"
  10. #include "object.h"
  11. #include "lockfile.h"
  12. #include "submodule-config.h"
  13. /* The main repository */
  14. static struct repository the_repo;
  15. struct repository *the_repository;
  16. struct index_state the_index;
  17. void initialize_the_repository(void)
  18. {
  19. the_repository = &the_repo;
  20. the_repo.index = &the_index;
  21. the_repo.objects = raw_object_store_new();
  22. the_repo.parsed_objects = parsed_object_pool_new();
  23. repo_set_hash_algo(&the_repo, GIT_HASH_SHA1);
  24. }
  25. static void expand_base_dir(char **out, const char *in,
  26. const char *base_dir, const char *def_in)
  27. {
  28. free(*out);
  29. if (in)
  30. *out = xstrdup(in);
  31. else
  32. *out = xstrfmt("%s/%s", base_dir, def_in);
  33. }
  34. static void repo_set_commondir(struct repository *repo,
  35. const char *commondir)
  36. {
  37. struct strbuf sb = STRBUF_INIT;
  38. free(repo->commondir);
  39. if (commondir) {
  40. repo->different_commondir = 1;
  41. repo->commondir = xstrdup(commondir);
  42. return;
  43. }
  44. repo->different_commondir = get_common_dir_noenv(&sb, repo->gitdir);
  45. repo->commondir = strbuf_detach(&sb, NULL);
  46. }
  47. void repo_set_gitdir(struct repository *repo,
  48. const char *root,
  49. const struct set_gitdir_args *o)
  50. {
  51. const char *gitfile = read_gitfile(root);
  52. /*
  53. * repo->gitdir is saved because the caller could pass "root"
  54. * that also points to repo->gitdir. We want to keep it alive
  55. * until after xstrdup(root). Then we can free it.
  56. */
  57. char *old_gitdir = repo->gitdir;
  58. repo->gitdir = xstrdup(gitfile ? gitfile : root);
  59. free(old_gitdir);
  60. repo_set_commondir(repo, o->commondir);
  61. if (!repo->objects->odb) {
  62. repo->objects->odb = xcalloc(1, sizeof(*repo->objects->odb));
  63. repo->objects->odb_tail = &repo->objects->odb->next;
  64. }
  65. expand_base_dir(&repo->objects->odb->path, o->object_dir,
  66. repo->commondir, "objects");
  67. free(repo->objects->alternate_db);
  68. repo->objects->alternate_db = xstrdup_or_null(o->alternate_db);
  69. expand_base_dir(&repo->graft_file, o->graft_file,
  70. repo->commondir, "info/grafts");
  71. expand_base_dir(&repo->index_file, o->index_file,
  72. repo->gitdir, "index");
  73. }
  74. void repo_set_hash_algo(struct repository *repo, int hash_algo)
  75. {
  76. repo->hash_algo = &hash_algos[hash_algo];
  77. }
  78. /*
  79. * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
  80. * Return 0 upon success and a non-zero value upon failure.
  81. */
  82. static int repo_init_gitdir(struct repository *repo, const char *gitdir)
  83. {
  84. int ret = 0;
  85. int error = 0;
  86. char *abspath = NULL;
  87. const char *resolved_gitdir;
  88. struct set_gitdir_args args = { NULL };
  89. abspath = real_pathdup(gitdir, 0);
  90. if (!abspath) {
  91. ret = -1;
  92. goto out;
  93. }
  94. /* 'gitdir' must reference the gitdir directly */
  95. resolved_gitdir = resolve_gitdir_gently(abspath, &error);
  96. if (!resolved_gitdir) {
  97. ret = -1;
  98. goto out;
  99. }
  100. repo_set_gitdir(repo, resolved_gitdir, &args);
  101. out:
  102. free(abspath);
  103. return ret;
  104. }
  105. void repo_set_worktree(struct repository *repo, const char *path)
  106. {
  107. repo->worktree = real_pathdup(path, 1);
  108. trace2_def_repo(repo);
  109. }
  110. static int read_and_verify_repository_format(struct repository_format *format,
  111. const char *commondir)
  112. {
  113. int ret = 0;
  114. struct strbuf sb = STRBUF_INIT;
  115. strbuf_addf(&sb, "%s/config", commondir);
  116. read_repository_format(format, sb.buf);
  117. strbuf_reset(&sb);
  118. if (verify_repository_format(format, &sb) < 0) {
  119. warning("%s", sb.buf);
  120. ret = -1;
  121. }
  122. strbuf_release(&sb);
  123. return ret;
  124. }
  125. /*
  126. * Initialize 'repo' based on the provided 'gitdir'.
  127. * Return 0 upon success and a non-zero value upon failure.
  128. */
  129. int repo_init(struct repository *repo,
  130. const char *gitdir,
  131. const char *worktree)
  132. {
  133. struct repository_format format = REPOSITORY_FORMAT_INIT;
  134. memset(repo, 0, sizeof(*repo));
  135. repo->objects = raw_object_store_new();
  136. repo->parsed_objects = parsed_object_pool_new();
  137. if (repo_init_gitdir(repo, gitdir))
  138. goto error;
  139. if (read_and_verify_repository_format(&format, repo->commondir))
  140. goto error;
  141. repo_set_hash_algo(repo, format.hash_algo);
  142. if (worktree)
  143. repo_set_worktree(repo, worktree);
  144. clear_repository_format(&format);
  145. return 0;
  146. error:
  147. repo_clear(repo);
  148. return -1;
  149. }
  150. int repo_submodule_init(struct repository *subrepo,
  151. struct repository *superproject,
  152. const struct submodule *sub)
  153. {
  154. struct strbuf gitdir = STRBUF_INIT;
  155. struct strbuf worktree = STRBUF_INIT;
  156. int ret = 0;
  157. if (!sub) {
  158. ret = -1;
  159. goto out;
  160. }
  161. strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", sub->path);
  162. strbuf_repo_worktree_path(&worktree, superproject, "%s", sub->path);
  163. if (repo_init(subrepo, gitdir.buf, worktree.buf)) {
  164. /*
  165. * If initilization fails then it may be due to the submodule
  166. * not being populated in the superproject's worktree. Instead
  167. * we can try to initilize the submodule by finding it's gitdir
  168. * in the superproject's 'modules' directory. In this case the
  169. * submodule would not have a worktree.
  170. */
  171. strbuf_reset(&gitdir);
  172. strbuf_repo_git_path(&gitdir, superproject,
  173. "modules/%s", sub->name);
  174. if (repo_init(subrepo, gitdir.buf, NULL)) {
  175. ret = -1;
  176. goto out;
  177. }
  178. }
  179. subrepo->submodule_prefix = xstrfmt("%s%s/",
  180. superproject->submodule_prefix ?
  181. superproject->submodule_prefix :
  182. "", sub->path);
  183. out:
  184. strbuf_release(&gitdir);
  185. strbuf_release(&worktree);
  186. return ret;
  187. }
  188. void repo_clear(struct repository *repo)
  189. {
  190. FREE_AND_NULL(repo->gitdir);
  191. FREE_AND_NULL(repo->commondir);
  192. FREE_AND_NULL(repo->graft_file);
  193. FREE_AND_NULL(repo->index_file);
  194. FREE_AND_NULL(repo->worktree);
  195. FREE_AND_NULL(repo->submodule_prefix);
  196. raw_object_store_clear(repo->objects);
  197. FREE_AND_NULL(repo->objects);
  198. parsed_object_pool_clear(repo->parsed_objects);
  199. FREE_AND_NULL(repo->parsed_objects);
  200. if (repo->config) {
  201. git_configset_clear(repo->config);
  202. FREE_AND_NULL(repo->config);
  203. }
  204. if (repo->submodule_cache) {
  205. submodule_cache_free(repo->submodule_cache);
  206. repo->submodule_cache = NULL;
  207. }
  208. if (repo->index) {
  209. discard_index(repo->index);
  210. if (repo->index != &the_index)
  211. FREE_AND_NULL(repo->index);
  212. }
  213. }
  214. int repo_read_index(struct repository *repo)
  215. {
  216. if (!repo->index)
  217. repo->index = xcalloc(1, sizeof(*repo->index));
  218. return read_index_from(repo->index, repo->index_file, repo->gitdir);
  219. }
  220. int repo_hold_locked_index(struct repository *repo,
  221. struct lock_file *lf,
  222. int flags)
  223. {
  224. if (!repo->index_file)
  225. BUG("the repo hasn't been setup");
  226. return hold_lock_file_for_update(lf, repo->index_file, flags);
  227. }