blob: 6c461c5b9def017947c90bd84140ae48556e252a [file] [log] [blame]
Brandon Williams359efef2017-06-22 11:43:32 -07001#ifndef REPOSITORY_H
2#define REPOSITORY_H
3
Ævar Arnfjörð Bjarmason1e8697b2022-09-01 01:18:12 +02004#include "git-compat-util.h"
Elijah Newrenef3ca952018-08-15 10:54:05 -07005#include "path.h"
6
Brandon Williams3b256222017-06-22 11:43:42 -07007struct config_set;
Jeff Hostetler1e0ea5c2022-03-25 18:02:46 +00008struct fsmonitor_settings;
brian m. carlson78a67662017-11-12 21:28:53 +00009struct git_hash_algo;
Stefan Beller90c62152018-03-23 18:20:55 +010010struct index_state;
Nguyễn Thái Ngọc Duy3a95f312019-01-12 09:13:24 +070011struct lock_file;
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 09:13:26 +070012struct pathspec;
Stefan Beller90c62152018-03-23 18:20:55 +010013struct raw_object_store;
14struct submodule_cache;
Jonathan Tanef7dc2e2021-06-17 10:13:23 -070015struct promisor_remote_config;
Glen Choofd3cb052021-11-17 16:53:22 -080016struct remote_state;
Brandon Williams3b256222017-06-22 11:43:42 -070017
Derrick Stoleead0fb652019-08-13 11:37:46 -070018enum untracked_cache_setting {
Ævar Arnfjörð Bjarmason3050b6d2021-09-21 15:13:02 +020019 UNTRACKED_CACHE_KEEP,
20 UNTRACKED_CACHE_REMOVE,
21 UNTRACKED_CACHE_WRITE,
Derrick Stoleead0fb652019-08-13 11:37:46 -070022};
23
Derrick Stoleeaaf633c2019-08-13 11:37:48 -070024enum fetch_negotiation_setting {
Elijah Newren714edc62022-02-02 03:42:40 +000025 FETCH_NEGOTIATION_CONSECUTIVE,
Ævar Arnfjörð Bjarmason3050b6d2021-09-21 15:13:02 +020026 FETCH_NEGOTIATION_SKIPPING,
27 FETCH_NEGOTIATION_NOOP,
Derrick Stoleeaaf633c2019-08-13 11:37:48 -070028};
29
Derrick Stolee7211b9e2019-08-13 11:37:43 -070030struct repo_settings {
31 int initialized;
32
33 int core_commit_graph;
Taylor Blaua92d8522022-07-14 14:43:06 -070034 int commit_graph_generation_version;
Taylor Blaub66d8472020-09-09 11:23:10 -040035 int commit_graph_read_changed_paths;
Derrick Stolee7211b9e2019-08-13 11:37:43 -070036 int gc_write_commit_graph;
Emily Shafferc6955922022-10-26 17:32:45 -040037 int gc_cruft_packs;
Derrick Stolee50f26bd2019-09-02 19:22:02 -070038 int fetch_write_commit_graph;
Ævar Arnfjörð Bjarmasonc21919f2021-09-21 15:13:03 +020039 int command_requires_full_index;
40 int sparse_index;
Derrick Stolee7211b9e2019-08-13 11:37:43 -070041
Jeff Hostetler1e0ea5c2022-03-25 18:02:46 +000042 struct fsmonitor_settings *fsmonitor; /* lazily loaded */
43
Derrick Stolee7211b9e2019-08-13 11:37:43 -070044 int index_version;
Derrick Stoleead0fb652019-08-13 11:37:46 -070045 enum untracked_cache_setting core_untracked_cache;
Derrick Stolee7211b9e2019-08-13 11:37:43 -070046
47 int pack_use_sparse;
Derrick Stoleeaaf633c2019-08-13 11:37:48 -070048 enum fetch_negotiation_setting fetch_negotiation_algorithm;
Derrick Stolee18e449f2020-09-25 12:33:34 +000049
50 int core_multi_pack_index;
Derrick Stolee7211b9e2019-08-13 11:37:43 -070051};
52
Ævar Arnfjörð Bjarmason759f3402022-03-04 19:32:17 +010053struct repo_path_cache {
54 char *squash_msg;
55 char *merge_msg;
56 char *merge_rr;
57 char *merge_mode;
58 char *merge_head;
59 char *merge_autostash;
60 char *auto_merge;
61 char *fetch_head;
62 char *shallow;
63};
64
Brandon Williams359efef2017-06-22 11:43:32 -070065struct repository {
66 /* Environment */
67 /*
68 * Path to the git directory.
69 * Cannot be NULL after initialization.
70 */
71 char *gitdir;
72
73 /*
74 * Path to the common git directory.
75 * Cannot be NULL after initialization.
76 */
77 char *commondir;
78
79 /*
Stefan Beller90c62152018-03-23 18:20:55 +010080 * Holds any information related to accessing the raw object content.
Brandon Williams359efef2017-06-22 11:43:32 -070081 */
Stefan Beller90c62152018-03-23 18:20:55 +010082 struct raw_object_store *objects;
Nguyễn Thái Ngọc Duy7bc0dca2018-03-03 18:35:57 +070083
Stefan Beller99bf1152018-05-08 12:37:24 -070084 /*
85 * All objects in this repository that have been parsed. This structure
86 * owns all objects it references, so users of "struct object *"
87 * generally do not need to free them; instead, when a repository is no
88 * longer used, call parsed_object_pool_clear() on this structure, which
89 * is called by the repositories repo_clear on its desconstruction.
90 */
91 struct parsed_object_pool *parsed_objects;
92
Jeff King02204612020-04-09 23:04:11 -040093 /*
94 * The store in which the refs are held. This should generally only be
95 * accessed via get_main_ref_store(), as that will lazily initialize
96 * the ref object.
97 */
98 struct ref_store *refs_private;
Stefan Beller64a74162018-04-11 17:21:14 -070099
Brandon Williams359efef2017-06-22 11:43:32 -0700100 /*
Stefan Beller102de882018-05-17 15:51:51 -0700101 * Contains path to often used file names.
102 */
Ævar Arnfjörð Bjarmason759f3402022-03-04 19:32:17 +0100103 struct repo_path_cache cached_paths;
Stefan Beller102de882018-05-17 15:51:51 -0700104
105 /*
Brandon Williams359efef2017-06-22 11:43:32 -0700106 * Path to the repository's graft file.
107 * Cannot be NULL after initialization.
108 */
109 char *graft_file;
110
111 /*
112 * Path to the current worktree's index file.
113 * Cannot be NULL after initialization.
114 */
115 char *index_file;
116
117 /*
118 * Path to the working directory.
119 * A NULL value indicates that there is no working directory.
120 */
121 char *worktree;
122
Brandon Williams96dc8832017-06-22 11:43:47 -0700123 /*
124 * Path from the root of the top-level superproject down to this
125 * repository. This is only non-NULL if the repository is initialized
126 * as a submodule of another repository.
127 */
128 char *submodule_prefix;
129
Derrick Stolee7211b9e2019-08-13 11:37:43 -0700130 struct repo_settings settings;
131
Brandon Williams3b256222017-06-22 11:43:42 -0700132 /* Subsystems */
133 /*
134 * Repository's config which contains key-value pairs from the usual
135 * set of config files (i.e. repo specific .git/config, user wide
136 * ~/.gitconfig, XDG config file and the global /etc/gitconfig)
137 */
138 struct config_set *config;
139
Brandon Williamsbf12fcd2017-06-22 11:43:44 -0700140 /* Repository's submodule config as defined by '.gitmodules' */
141 struct submodule_cache *submodule_cache;
142
Brandon Williams639e30b2017-06-22 11:43:43 -0700143 /*
144 * Repository's in-memory index.
145 * 'repo_read_index()' can be used to populate 'index'.
146 */
147 struct index_state *index;
148
Glen Choofd3cb052021-11-17 16:53:22 -0800149 /* Repository's remotes and associated structures. */
150 struct remote_state *remote_state;
151
brian m. carlson78a67662017-11-12 21:28:53 +0000152 /* Repository's current hash algorithm, as serialized on disk. */
153 const struct git_hash_algo *hash_algo;
154
Jeff Hostetleree4512e2019-02-22 14:25:01 -0800155 /* A unique-id for tracing purposes. */
156 int trace2_repo_id;
157
Jeff King6abada12019-09-12 10:44:45 -0400158 /* True if commit-graph has been disabled within this process. */
159 int commit_graph_disabled;
160
Jonathan Tanebaf3bc2021-06-17 10:13:22 -0700161 /* Configurations related to promisor remotes. */
162 char *repository_format_partial_clone;
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700163 struct promisor_remote_config *promisor_remote_config;
Jonathan Tanebaf3bc2021-06-17 10:13:22 -0700164
Brandon Williams359efef2017-06-22 11:43:32 -0700165 /* Configurations */
Brandon Williams359efef2017-06-22 11:43:32 -0700166
167 /* Indicate if a repository has a different 'commondir' from 'gitdir' */
168 unsigned different_commondir:1;
169};
170
171extern struct repository *the_repository;
172
Nguyễn Thái Ngọc Duy00a3da22018-03-23 16:55:23 +0100173/*
174 * Define a custom repository layout. Any field can be NULL, which
175 * will default back to the path according to the default layout.
176 */
Nguyễn Thái Ngọc Duy357a03e2018-03-03 18:35:55 +0700177struct set_gitdir_args {
178 const char *commondir;
179 const char *object_dir;
180 const char *graft_file;
181 const char *index_file;
Nguyễn Thái Ngọc Duy7bc0dca2018-03-03 18:35:57 +0700182 const char *alternate_db;
Neeraj Singhecd81df2021-12-06 22:05:05 +0000183 int disable_ref_updates;
Nguyễn Thái Ngọc Duy357a03e2018-03-03 18:35:55 +0700184};
185
Nguyễn Thái Ngọc Duyc2ec4172018-06-30 11:20:29 +0200186void repo_set_gitdir(struct repository *repo, const char *root,
187 const struct set_gitdir_args *extra_args);
188void repo_set_worktree(struct repository *repo, const char *path);
189void repo_set_hash_algo(struct repository *repo, int algo);
190void initialize_the_repository(void);
Ævar Arnfjörð Bjarmason1e8697b2022-09-01 01:18:12 +0200191RESULT_MUST_BE_USED
Nguyễn Thái Ngọc Duyc2ec4172018-06-30 11:20:29 +0200192int repo_init(struct repository *r, const char *gitdir, const char *worktree);
Stefan Bellerd5498e02018-11-28 16:27:53 -0800193
194/*
Jonathan Tan8eb8dcf2021-09-09 11:47:28 -0700195 * Initialize the repository 'subrepo' as the submodule at the given path. If
196 * the submodule's gitdir cannot be found at <path>/.git, this function calls
197 * submodule_from_path() to try to find it. treeish_name is only used if
198 * submodule_from_path() needs to be called; see its documentation for more
199 * information.
200 * Return 0 upon success and a non-zero value upon failure.
Stefan Bellerd5498e02018-11-28 16:27:53 -0800201 */
Jonathan Tan8eb8dcf2021-09-09 11:47:28 -0700202struct object_id;
Ævar Arnfjörð Bjarmason1e8697b2022-09-01 01:18:12 +0200203RESULT_MUST_BE_USED
Stefan Bellerd5498e02018-11-28 16:27:53 -0800204int repo_submodule_init(struct repository *subrepo,
Nguyễn Thái Ngọc Duyc2ec4172018-06-30 11:20:29 +0200205 struct repository *superproject,
Jonathan Tan8eb8dcf2021-09-09 11:47:28 -0700206 const char *path,
207 const struct object_id *treeish_name);
Nguyễn Thái Ngọc Duyc2ec4172018-06-30 11:20:29 +0200208void repo_clear(struct repository *repo);
Brandon Williams359efef2017-06-22 11:43:32 -0700209
Brandon Williams3f138772017-07-18 12:05:18 -0700210/*
211 * Populates the repository's index from its index_file, an index struct will
212 * be allocated if needed.
213 *
214 * Return the number of index entries in the populated index or a value less
Elijah Newren15beaaa2019-11-05 17:07:23 +0000215 * than zero if an error occurred. If the repository's index has already been
Brandon Williams3f138772017-07-18 12:05:18 -0700216 * populated then the number of entries will simply be returned.
217 */
Nguyễn Thái Ngọc Duyc2ec4172018-06-30 11:20:29 +0200218int repo_read_index(struct repository *repo);
Nguyễn Thái Ngọc Duy3a95f312019-01-12 09:13:24 +0700219int repo_hold_locked_index(struct repository *repo,
220 struct lock_file *lf,
221 int flags);
Brandon Williams639e30b2017-06-22 11:43:43 -0700222
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 09:13:26 +0700223int repo_read_index_preload(struct repository *,
224 const struct pathspec *pathspec,
225 unsigned refresh_flags);
226int repo_read_index_unmerged(struct repository *);
Nguyễn Thái Ngọc Duy1b0d9682019-01-12 09:13:27 +0700227/*
228 * Opportunistically update the index but do not complain if we can't.
229 * The lockfile is always committed or rolled back.
230 */
231void repo_update_index_if_able(struct repository *, struct lock_file *);
232
Derrick Stolee7211b9e2019-08-13 11:37:43 -0700233void prepare_repo_settings(struct repository *r);
Brandon Williams359efef2017-06-22 11:43:32 -0700234
Xin Li16af5f12020-06-05 02:10:01 -0700235/*
236 * Return 1 if upgrade repository format to target_version succeeded,
237 * 0 if no upgrade is necessary, and -1 when upgrade is not possible.
238 */
239int upgrade_repository_format(int target_version);
240
Brandon Williams359efef2017-06-22 11:43:32 -0700241#endif /* REPOSITORY_H */