Merge branch 'jc/calloc-fix'
[git] / repository.h
1 #ifndef REPOSITORY_H
2 #define REPOSITORY_H
3
4 #include "path.h"
5
6 struct config_set;
7 struct git_hash_algo;
8 struct index_state;
9 struct lock_file;
10 struct pathspec;
11 struct raw_object_store;
12 struct submodule_cache;
13
14 enum untracked_cache_setting {
15         UNTRACKED_CACHE_UNSET = -1,
16         UNTRACKED_CACHE_REMOVE = 0,
17         UNTRACKED_CACHE_KEEP = 1,
18         UNTRACKED_CACHE_WRITE = 2
19 };
20
21 enum fetch_negotiation_setting {
22         FETCH_NEGOTIATION_UNSET = -1,
23         FETCH_NEGOTIATION_NONE = 0,
24         FETCH_NEGOTIATION_DEFAULT = 1,
25         FETCH_NEGOTIATION_SKIPPING = 2,
26         FETCH_NEGOTIATION_NOOP = 3,
27 };
28
29 struct repo_settings {
30         int initialized;
31
32         int core_commit_graph;
33         int commit_graph_read_changed_paths;
34         int gc_write_commit_graph;
35         int fetch_write_commit_graph;
36
37         int index_version;
38         enum untracked_cache_setting core_untracked_cache;
39
40         int pack_use_sparse;
41         enum fetch_negotiation_setting fetch_negotiation_algorithm;
42
43         int core_multi_pack_index;
44 };
45
46 struct repository {
47         /* Environment */
48         /*
49          * Path to the git directory.
50          * Cannot be NULL after initialization.
51          */
52         char *gitdir;
53
54         /*
55          * Path to the common git directory.
56          * Cannot be NULL after initialization.
57          */
58         char *commondir;
59
60         /*
61          * Holds any information related to accessing the raw object content.
62          */
63         struct raw_object_store *objects;
64
65         /*
66          * All objects in this repository that have been parsed. This structure
67          * owns all objects it references, so users of "struct object *"
68          * generally do not need to free them; instead, when a repository is no
69          * longer used, call parsed_object_pool_clear() on this structure, which
70          * is called by the repositories repo_clear on its desconstruction.
71          */
72         struct parsed_object_pool *parsed_objects;
73
74         /*
75          * The store in which the refs are held. This should generally only be
76          * accessed via get_main_ref_store(), as that will lazily initialize
77          * the ref object.
78          */
79         struct ref_store *refs_private;
80
81         /*
82          * Contains path to often used file names.
83          */
84         struct path_cache cached_paths;
85
86         /*
87          * Path to the repository's graft file.
88          * Cannot be NULL after initialization.
89          */
90         char *graft_file;
91
92         /*
93          * Path to the current worktree's index file.
94          * Cannot be NULL after initialization.
95          */
96         char *index_file;
97
98         /*
99          * Path to the working directory.
100          * A NULL value indicates that there is no working directory.
101          */
102         char *worktree;
103
104         /*
105          * Path from the root of the top-level superproject down to this
106          * repository.  This is only non-NULL if the repository is initialized
107          * as a submodule of another repository.
108          */
109         char *submodule_prefix;
110
111         struct repo_settings settings;
112
113         /* Subsystems */
114         /*
115          * Repository's config which contains key-value pairs from the usual
116          * set of config files (i.e. repo specific .git/config, user wide
117          * ~/.gitconfig, XDG config file and the global /etc/gitconfig)
118          */
119         struct config_set *config;
120
121         /* Repository's submodule config as defined by '.gitmodules' */
122         struct submodule_cache *submodule_cache;
123
124         /*
125          * Repository's in-memory index.
126          * 'repo_read_index()' can be used to populate 'index'.
127          */
128         struct index_state *index;
129
130         /* Repository's current hash algorithm, as serialized on disk. */
131         const struct git_hash_algo *hash_algo;
132
133         /* A unique-id for tracing purposes. */
134         int trace2_repo_id;
135
136         /* True if commit-graph has been disabled within this process. */
137         int commit_graph_disabled;
138
139         /* Configurations */
140
141         /* Indicate if a repository has a different 'commondir' from 'gitdir' */
142         unsigned different_commondir:1;
143 };
144
145 extern struct repository *the_repository;
146
147 /*
148  * Define a custom repository layout. Any field can be NULL, which
149  * will default back to the path according to the default layout.
150  */
151 struct set_gitdir_args {
152         const char *commondir;
153         const char *object_dir;
154         const char *graft_file;
155         const char *index_file;
156         const char *alternate_db;
157 };
158
159 void repo_set_gitdir(struct repository *repo, const char *root,
160                      const struct set_gitdir_args *extra_args);
161 void repo_set_worktree(struct repository *repo, const char *path);
162 void repo_set_hash_algo(struct repository *repo, int algo);
163 void initialize_the_repository(void);
164 int repo_init(struct repository *r, const char *gitdir, const char *worktree);
165
166 /*
167  * Initialize the repository 'subrepo' as the submodule given by the
168  * struct submodule 'sub' in parent repository 'superproject'.
169  * Return 0 upon success and a non-zero value upon failure, which may happen
170  * if the submodule is not found, or 'sub' is NULL.
171  */
172 struct submodule;
173 int repo_submodule_init(struct repository *subrepo,
174                         struct repository *superproject,
175                         const struct submodule *sub);
176 void repo_clear(struct repository *repo);
177
178 /*
179  * Populates the repository's index from its index_file, an index struct will
180  * be allocated if needed.
181  *
182  * Return the number of index entries in the populated index or a value less
183  * than zero if an error occurred.  If the repository's index has already been
184  * populated then the number of entries will simply be returned.
185  */
186 int repo_read_index(struct repository *repo);
187 int repo_hold_locked_index(struct repository *repo,
188                            struct lock_file *lf,
189                            int flags);
190
191 int repo_read_index_preload(struct repository *,
192                             const struct pathspec *pathspec,
193                             unsigned refresh_flags);
194 int repo_read_index_unmerged(struct repository *);
195 /*
196  * Opportunistically update the index but do not complain if we can't.
197  * The lockfile is always committed or rolled back.
198  */
199 void repo_update_index_if_able(struct repository *, struct lock_file *);
200
201 void prepare_repo_settings(struct repository *r);
202
203 /*
204  * Return 1 if upgrade repository format to target_version succeeded,
205  * 0 if no upgrade is necessary, and -1 when upgrade is not possible.
206  */
207 int upgrade_repository_format(int target_version);
208
209 #endif /* REPOSITORY_H */