1 #include "git-compat-util.h"
3 #include "repository.h"
4 #include "object-store-ll.h"
9 #include "read-cache-ll.h"
13 #include "submodule-config.h"
14 #include "sparse-index.h"
16 #include "promisor-remote.h"
19 /* The main repository */
20 static struct repository the_repo
;
21 struct repository
*the_repository
= &the_repo
;
24 * An escape hatch: if we hit a bug in the production code that fails
25 * to set an appropriate hash algorithm (most likely to happen when
26 * running outside a repository), we can tell the user who reported
27 * the crash to set the environment variable to "sha1" (all lowercase)
28 * to revert to the historical behaviour of defaulting to SHA-1.
30 static void set_default_hash_algo(struct repository
*repo
)
32 const char *hash_name
;
35 hash_name
= getenv("GIT_TEST_DEFAULT_HASH_ALGO");
38 algo
= hash_algo_by_name(hash_name
);
39 if (algo
== GIT_HASH_UNKNOWN
)
42 repo_set_hash_algo(repo
, algo
);
45 void initialize_repository(struct repository
*repo
)
47 repo
->objects
= raw_object_store_new();
48 repo
->remote_state
= remote_state_new();
49 repo
->parsed_objects
= parsed_object_pool_new();
50 ALLOC_ARRAY(repo
->index
, 1);
51 index_state_init(repo
->index
, repo
);
54 * When a command runs inside a repository, it learns what
55 * hash algorithm is in use from the repository, but some
56 * commands are designed to work outside a repository, yet
57 * they want to access the_hash_algo, if only for the length
58 * of the hashed value to see if their input looks like a
59 * plausible hash value.
61 * We are in the process of identifying such code paths and
62 * giving them an appropriate default individually; any
63 * unconverted code paths that try to access the_hash_algo
64 * will thus fail. The end-users however have an escape hatch
65 * to set GIT_TEST_DEFAULT_HASH_ALGO environment variable to
66 * "sha1" to get back the old behaviour of defaulting to SHA-1.
68 * This escape hatch is deliberately kept unadvertised, so
69 * that they see crashes and we can get a report before
70 * telling them about it.
72 if (repo
== the_repository
)
73 set_default_hash_algo(repo
);
76 static void expand_base_dir(char **out
, const char *in
,
77 const char *base_dir
, const char *def_in
)
83 *out
= xstrfmt("%s/%s", base_dir
, def_in
);
86 static void repo_set_commondir(struct repository
*repo
,
87 const char *commondir
)
89 struct strbuf sb
= STRBUF_INIT
;
91 free(repo
->commondir
);
94 repo
->different_commondir
= 1;
95 repo
->commondir
= xstrdup(commondir
);
99 repo
->different_commondir
= get_common_dir_noenv(&sb
, repo
->gitdir
);
100 repo
->commondir
= strbuf_detach(&sb
, NULL
);
103 void repo_set_gitdir(struct repository
*repo
,
105 const struct set_gitdir_args
*o
)
107 const char *gitfile
= read_gitfile(root
);
109 * repo->gitdir is saved because the caller could pass "root"
110 * that also points to repo->gitdir. We want to keep it alive
111 * until after xstrdup(root). Then we can free it.
113 char *old_gitdir
= repo
->gitdir
;
115 repo
->gitdir
= xstrdup(gitfile
? gitfile
: root
);
118 repo_set_commondir(repo
, o
->commondir
);
120 if (!repo
->objects
->odb
) {
121 CALLOC_ARRAY(repo
->objects
->odb
, 1);
122 repo
->objects
->odb_tail
= &repo
->objects
->odb
->next
;
124 expand_base_dir(&repo
->objects
->odb
->path
, o
->object_dir
,
125 repo
->commondir
, "objects");
127 repo
->objects
->odb
->disable_ref_updates
= o
->disable_ref_updates
;
129 free(repo
->objects
->alternate_db
);
130 repo
->objects
->alternate_db
= xstrdup_or_null(o
->alternate_db
);
131 expand_base_dir(&repo
->graft_file
, o
->graft_file
,
132 repo
->commondir
, "info/grafts");
133 expand_base_dir(&repo
->index_file
, o
->index_file
,
134 repo
->gitdir
, "index");
137 void repo_set_hash_algo(struct repository
*repo
, int hash_algo
)
139 repo
->hash_algo
= &hash_algos
[hash_algo
];
142 void repo_set_compat_hash_algo(struct repository
*repo
, int algo
)
144 if (hash_algo_by_ptr(repo
->hash_algo
) == algo
)
145 BUG("hash_algo and compat_hash_algo match");
146 repo
->compat_hash_algo
= algo
? &hash_algos
[algo
] : NULL
;
147 if (repo
->compat_hash_algo
)
148 repo_read_loose_object_map(repo
);
151 void repo_set_ref_storage_format(struct repository
*repo
,
152 enum ref_storage_format format
)
154 repo
->ref_storage_format
= format
;
158 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
159 * Return 0 upon success and a non-zero value upon failure.
161 static int repo_init_gitdir(struct repository
*repo
, const char *gitdir
)
165 char *abspath
= NULL
;
166 const char *resolved_gitdir
;
167 struct set_gitdir_args args
= { NULL
};
169 abspath
= real_pathdup(gitdir
, 0);
175 /* 'gitdir' must reference the gitdir directly */
176 resolved_gitdir
= resolve_gitdir_gently(abspath
, &error
);
177 if (!resolved_gitdir
) {
182 repo_set_gitdir(repo
, resolved_gitdir
, &args
);
189 void repo_set_worktree(struct repository
*repo
, const char *path
)
191 repo
->worktree
= real_pathdup(path
, 1);
193 trace2_def_repo(repo
);
196 static int read_and_verify_repository_format(struct repository_format
*format
,
197 const char *commondir
)
200 struct strbuf sb
= STRBUF_INIT
;
202 strbuf_addf(&sb
, "%s/config", commondir
);
203 read_repository_format(format
, sb
.buf
);
206 if (verify_repository_format(format
, &sb
) < 0) {
207 warning("%s", sb
.buf
);
216 * Initialize 'repo' based on the provided 'gitdir'.
217 * Return 0 upon success and a non-zero value upon failure.
219 int repo_init(struct repository
*repo
,
221 const char *worktree
)
223 struct repository_format format
= REPOSITORY_FORMAT_INIT
;
224 memset(repo
, 0, sizeof(*repo
));
226 initialize_repository(repo
);
228 if (repo_init_gitdir(repo
, gitdir
))
231 if (read_and_verify_repository_format(&format
, repo
->commondir
))
234 repo_set_hash_algo(repo
, format
.hash_algo
);
235 repo_set_compat_hash_algo(repo
, format
.compat_hash_algo
);
236 repo_set_ref_storage_format(repo
, format
.ref_storage_format
);
237 repo
->repository_format_worktree_config
= format
.worktree_config
;
239 /* take ownership of format.partial_clone */
240 repo
->repository_format_partial_clone
= format
.partial_clone
;
241 format
.partial_clone
= NULL
;
244 repo_set_worktree(repo
, worktree
);
246 if (repo
->compat_hash_algo
)
247 repo_read_loose_object_map(repo
);
249 clear_repository_format(&format
);
257 int repo_submodule_init(struct repository
*subrepo
,
258 struct repository
*superproject
,
260 const struct object_id
*treeish_name
)
262 struct strbuf gitdir
= STRBUF_INIT
;
263 struct strbuf worktree
= STRBUF_INIT
;
266 strbuf_repo_worktree_path(&gitdir
, superproject
, "%s/.git", path
);
267 strbuf_repo_worktree_path(&worktree
, superproject
, "%s", path
);
269 if (repo_init(subrepo
, gitdir
.buf
, worktree
.buf
)) {
271 * If initialization fails then it may be due to the submodule
272 * not being populated in the superproject's worktree. Instead
273 * we can try to initialize the submodule by finding it's gitdir
274 * in the superproject's 'modules' directory. In this case the
275 * submodule would not have a worktree.
277 const struct submodule
*sub
=
278 submodule_from_path(superproject
, treeish_name
, path
);
284 strbuf_reset(&gitdir
);
285 submodule_name_to_gitdir(&gitdir
, superproject
, sub
->name
);
287 if (repo_init(subrepo
, gitdir
.buf
, NULL
)) {
293 subrepo
->submodule_prefix
= xstrfmt("%s%s/",
294 superproject
->submodule_prefix
?
295 superproject
->submodule_prefix
:
299 strbuf_release(&gitdir
);
300 strbuf_release(&worktree
);
304 static void repo_clear_path_cache(struct repo_path_cache
*cache
)
306 FREE_AND_NULL(cache
->squash_msg
);
307 FREE_AND_NULL(cache
->squash_msg
);
308 FREE_AND_NULL(cache
->merge_msg
);
309 FREE_AND_NULL(cache
->merge_rr
);
310 FREE_AND_NULL(cache
->merge_mode
);
311 FREE_AND_NULL(cache
->merge_head
);
312 FREE_AND_NULL(cache
->fetch_head
);
313 FREE_AND_NULL(cache
->shallow
);
316 void repo_clear(struct repository
*repo
)
318 struct hashmap_iter iter
;
319 struct strmap_entry
*e
;
321 FREE_AND_NULL(repo
->gitdir
);
322 FREE_AND_NULL(repo
->commondir
);
323 FREE_AND_NULL(repo
->graft_file
);
324 FREE_AND_NULL(repo
->index_file
);
325 FREE_AND_NULL(repo
->worktree
);
326 FREE_AND_NULL(repo
->submodule_prefix
);
328 raw_object_store_clear(repo
->objects
);
329 FREE_AND_NULL(repo
->objects
);
331 parsed_object_pool_clear(repo
->parsed_objects
);
332 FREE_AND_NULL(repo
->parsed_objects
);
334 FREE_AND_NULL(repo
->settings
.fsmonitor
);
337 git_configset_clear(repo
->config
);
338 FREE_AND_NULL(repo
->config
);
341 if (repo
->submodule_cache
) {
342 submodule_cache_free(repo
->submodule_cache
);
343 repo
->submodule_cache
= NULL
;
347 discard_index(repo
->index
);
348 FREE_AND_NULL(repo
->index
);
351 if (repo
->promisor_remote_config
) {
352 promisor_remote_clear(repo
->promisor_remote_config
);
353 FREE_AND_NULL(repo
->promisor_remote_config
);
356 if (repo
->remote_state
) {
357 remote_state_clear(repo
->remote_state
);
358 FREE_AND_NULL(repo
->remote_state
);
361 strmap_for_each_entry(&repo
->submodule_ref_stores
, &iter
, e
)
362 ref_store_release(e
->value
);
363 strmap_clear(&repo
->submodule_ref_stores
, 1);
365 strmap_for_each_entry(&repo
->worktree_ref_stores
, &iter
, e
)
366 ref_store_release(e
->value
);
367 strmap_clear(&repo
->worktree_ref_stores
, 1);
369 repo_clear_path_cache(&repo
->cached_paths
);
372 int repo_read_index(struct repository
*repo
)
376 /* Complete the double-reference */
378 ALLOC_ARRAY(repo
->index
, 1);
379 index_state_init(repo
->index
, repo
);
380 } else if (repo
->index
->repo
!= repo
) {
381 BUG("repo's index should point back at itself");
384 res
= read_index_from(repo
->index
, repo
->index_file
, repo
->gitdir
);
386 prepare_repo_settings(repo
);
387 if (repo
->settings
.command_requires_full_index
)
388 ensure_full_index(repo
->index
);
391 * If sparse checkouts are in use, check whether paths with the
392 * SKIP_WORKTREE attribute are missing from the worktree; if not,
393 * clear that attribute for that path.
395 clear_skip_worktree_from_present_files(repo
->index
);
400 int repo_hold_locked_index(struct repository
*repo
,
401 struct lock_file
*lf
,
404 if (!repo
->index_file
)
405 BUG("the repo hasn't been setup");
406 return hold_lock_file_for_update(lf
, repo
->index_file
, flags
);