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
, unsigned int format
)
153 repo
->ref_storage_format
= format
;
157 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
158 * Return 0 upon success and a non-zero value upon failure.
160 static int repo_init_gitdir(struct repository
*repo
, const char *gitdir
)
164 char *abspath
= NULL
;
165 const char *resolved_gitdir
;
166 struct set_gitdir_args args
= { NULL
};
168 abspath
= real_pathdup(gitdir
, 0);
174 /* 'gitdir' must reference the gitdir directly */
175 resolved_gitdir
= resolve_gitdir_gently(abspath
, &error
);
176 if (!resolved_gitdir
) {
181 repo_set_gitdir(repo
, resolved_gitdir
, &args
);
188 void repo_set_worktree(struct repository
*repo
, const char *path
)
190 repo
->worktree
= real_pathdup(path
, 1);
192 trace2_def_repo(repo
);
195 static int read_and_verify_repository_format(struct repository_format
*format
,
196 const char *commondir
)
199 struct strbuf sb
= STRBUF_INIT
;
201 strbuf_addf(&sb
, "%s/config", commondir
);
202 read_repository_format(format
, sb
.buf
);
205 if (verify_repository_format(format
, &sb
) < 0) {
206 warning("%s", sb
.buf
);
215 * Initialize 'repo' based on the provided 'gitdir'.
216 * Return 0 upon success and a non-zero value upon failure.
218 int repo_init(struct repository
*repo
,
220 const char *worktree
)
222 struct repository_format format
= REPOSITORY_FORMAT_INIT
;
223 memset(repo
, 0, sizeof(*repo
));
225 initialize_repository(repo
);
227 if (repo_init_gitdir(repo
, gitdir
))
230 if (read_and_verify_repository_format(&format
, repo
->commondir
))
233 repo_set_hash_algo(repo
, format
.hash_algo
);
234 repo_set_compat_hash_algo(repo
, format
.compat_hash_algo
);
235 repo_set_ref_storage_format(repo
, format
.ref_storage_format
);
236 repo
->repository_format_worktree_config
= format
.worktree_config
;
238 /* take ownership of format.partial_clone */
239 repo
->repository_format_partial_clone
= format
.partial_clone
;
240 format
.partial_clone
= NULL
;
243 repo_set_worktree(repo
, worktree
);
245 if (repo
->compat_hash_algo
)
246 repo_read_loose_object_map(repo
);
248 clear_repository_format(&format
);
256 int repo_submodule_init(struct repository
*subrepo
,
257 struct repository
*superproject
,
259 const struct object_id
*treeish_name
)
261 struct strbuf gitdir
= STRBUF_INIT
;
262 struct strbuf worktree
= STRBUF_INIT
;
265 strbuf_repo_worktree_path(&gitdir
, superproject
, "%s/.git", path
);
266 strbuf_repo_worktree_path(&worktree
, superproject
, "%s", path
);
268 if (repo_init(subrepo
, gitdir
.buf
, worktree
.buf
)) {
270 * If initialization fails then it may be due to the submodule
271 * not being populated in the superproject's worktree. Instead
272 * we can try to initialize the submodule by finding it's gitdir
273 * in the superproject's 'modules' directory. In this case the
274 * submodule would not have a worktree.
276 const struct submodule
*sub
=
277 submodule_from_path(superproject
, treeish_name
, path
);
283 strbuf_reset(&gitdir
);
284 submodule_name_to_gitdir(&gitdir
, superproject
, sub
->name
);
286 if (repo_init(subrepo
, gitdir
.buf
, NULL
)) {
292 subrepo
->submodule_prefix
= xstrfmt("%s%s/",
293 superproject
->submodule_prefix
?
294 superproject
->submodule_prefix
:
298 strbuf_release(&gitdir
);
299 strbuf_release(&worktree
);
303 static void repo_clear_path_cache(struct repo_path_cache
*cache
)
305 FREE_AND_NULL(cache
->squash_msg
);
306 FREE_AND_NULL(cache
->squash_msg
);
307 FREE_AND_NULL(cache
->merge_msg
);
308 FREE_AND_NULL(cache
->merge_rr
);
309 FREE_AND_NULL(cache
->merge_mode
);
310 FREE_AND_NULL(cache
->merge_head
);
311 FREE_AND_NULL(cache
->fetch_head
);
312 FREE_AND_NULL(cache
->shallow
);
315 void repo_clear(struct repository
*repo
)
317 struct hashmap_iter iter
;
318 struct strmap_entry
*e
;
320 FREE_AND_NULL(repo
->gitdir
);
321 FREE_AND_NULL(repo
->commondir
);
322 FREE_AND_NULL(repo
->graft_file
);
323 FREE_AND_NULL(repo
->index_file
);
324 FREE_AND_NULL(repo
->worktree
);
325 FREE_AND_NULL(repo
->submodule_prefix
);
327 raw_object_store_clear(repo
->objects
);
328 FREE_AND_NULL(repo
->objects
);
330 parsed_object_pool_clear(repo
->parsed_objects
);
331 FREE_AND_NULL(repo
->parsed_objects
);
333 FREE_AND_NULL(repo
->settings
.fsmonitor
);
336 git_configset_clear(repo
->config
);
337 FREE_AND_NULL(repo
->config
);
340 if (repo
->submodule_cache
) {
341 submodule_cache_free(repo
->submodule_cache
);
342 repo
->submodule_cache
= NULL
;
346 discard_index(repo
->index
);
347 FREE_AND_NULL(repo
->index
);
350 if (repo
->promisor_remote_config
) {
351 promisor_remote_clear(repo
->promisor_remote_config
);
352 FREE_AND_NULL(repo
->promisor_remote_config
);
355 if (repo
->remote_state
) {
356 remote_state_clear(repo
->remote_state
);
357 FREE_AND_NULL(repo
->remote_state
);
360 strmap_for_each_entry(&repo
->submodule_ref_stores
, &iter
, e
)
361 ref_store_release(e
->value
);
362 strmap_clear(&repo
->submodule_ref_stores
, 1);
364 strmap_for_each_entry(&repo
->worktree_ref_stores
, &iter
, e
)
365 ref_store_release(e
->value
);
366 strmap_clear(&repo
->worktree_ref_stores
, 1);
368 repo_clear_path_cache(&repo
->cached_paths
);
371 int repo_read_index(struct repository
*repo
)
375 /* Complete the double-reference */
377 ALLOC_ARRAY(repo
->index
, 1);
378 index_state_init(repo
->index
, repo
);
379 } else if (repo
->index
->repo
!= repo
) {
380 BUG("repo's index should point back at itself");
383 res
= read_index_from(repo
->index
, repo
->index_file
, repo
->gitdir
);
385 prepare_repo_settings(repo
);
386 if (repo
->settings
.command_requires_full_index
)
387 ensure_full_index(repo
->index
);
390 * If sparse checkouts are in use, check whether paths with the
391 * SKIP_WORKTREE attribute are missing from the worktree; if not,
392 * clear that attribute for that path.
394 clear_skip_worktree_from_present_files(repo
->index
);
399 int repo_hold_locked_index(struct repository
*repo
,
400 struct lock_file
*lf
,
403 if (!repo
->index_file
)
404 BUG("the repo hasn't been setup");
405 return hold_lock_file_for_update(lf
, repo
->index_file
, flags
);