repository.h 8.06 KB
Newer Older
Vicent Marti committed
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
Vicent Marti committed
3 4 5 6
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */
7 8 9
#ifndef INCLUDE_repository_h__
#define INCLUDE_repository_h__

10 11
#include "common.h"

12 13 14 15
#include "git2/common.h"
#include "git2/oid.h"
#include "git2/odb.h"
#include "git2/repository.h"
16
#include "git2/object.h"
17
#include "git2/config.h"
18

19
#include "array.h"
Vicent Marti committed
20
#include "cache.h"
21
#include "refs.h"
22
#include "str.h"
23
#include "object.h"
24
#include "attrcache.h"
25
#include "submodule.h"
26
#include "diff_driver.h"
27

28 29
#define DOT_GIT ".git"
#define GIT_DIR DOT_GIT "/"
30 31
#define GIT_DIR_MODE 0755
#define GIT_BARE_DIR_MODE 0777
32

33 34 35
/* Default DOS-compatible 8.3 "short name" for a git repository, "GIT~1" */
#define GIT_DIR_SHORTNAME "GIT~1"

36 37
extern bool git_repository__fsync_gitdir;

38 39
/** Cvar cache identifiers */
typedef enum {
40 41 42 43 44 45 46 47 48 49 50 51 52 53
	GIT_CONFIGMAP_AUTO_CRLF = 0,    /* core.autocrlf */
	GIT_CONFIGMAP_EOL,              /* core.eol */
	GIT_CONFIGMAP_SYMLINKS,         /* core.symlinks */
	GIT_CONFIGMAP_IGNORECASE,       /* core.ignorecase */
	GIT_CONFIGMAP_FILEMODE,         /* core.filemode */
	GIT_CONFIGMAP_IGNORESTAT,       /* core.ignorestat */
	GIT_CONFIGMAP_TRUSTCTIME,       /* core.trustctime */
	GIT_CONFIGMAP_ABBREV,           /* core.abbrev */
	GIT_CONFIGMAP_PRECOMPOSE,       /* core.precomposeunicode */
	GIT_CONFIGMAP_SAFE_CRLF,		/* core.safecrlf */
	GIT_CONFIGMAP_LOGALLREFUPDATES, /* core.logallrefupdates */
	GIT_CONFIGMAP_PROTECTHFS,       /* core.protectHFS */
	GIT_CONFIGMAP_PROTECTNTFS,      /* core.protectNTFS */
	GIT_CONFIGMAP_FSYNCOBJECTFILES, /* core.fsyncObjectFiles */
54
	GIT_CONFIGMAP_LONGPATHS,        /* core.longpaths */
55 56
	GIT_CONFIGMAP_CACHE_MAX
} git_configmap_item;
57 58

/**
59
 * Configuration map value enumerations
60
 *
61 62 63 64
 * These are the values that are actually stored in the configmap cache,
 * instead of their string equivalents. These values are internal and
 * symbolic; make sure that none of them is set to `-1`, since that is
 * the unique identifier for "not cached"
65 66 67
 */
typedef enum {
	/* The value hasn't been loaded from the cache yet */
68
	GIT_CONFIGMAP_NOT_CACHED = -1,
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

	/* core.safecrlf: false, 'fail', 'warn' */
	GIT_SAFE_CRLF_FALSE = 0,
	GIT_SAFE_CRLF_FAIL = 1,
	GIT_SAFE_CRLF_WARN = 2,

	/* core.autocrlf: false, true, 'input; */
	GIT_AUTO_CRLF_FALSE = 0,
	GIT_AUTO_CRLF_TRUE = 1,
	GIT_AUTO_CRLF_INPUT = 2,
	GIT_AUTO_CRLF_DEFAULT = GIT_AUTO_CRLF_FALSE,

	/* core.eol: unset, 'crlf', 'lf', 'native' */
	GIT_EOL_UNSET = 0,
	GIT_EOL_CRLF = 1,
	GIT_EOL_LF = 2,
#ifdef GIT_WIN32
	GIT_EOL_NATIVE = GIT_EOL_CRLF,
#else
	GIT_EOL_NATIVE = GIT_EOL_LF,
#endif
90 91 92
	GIT_EOL_DEFAULT = GIT_EOL_NATIVE,

	/* core.symlinks: bool */
93
	GIT_SYMLINKS_DEFAULT = GIT_CONFIGMAP_TRUE,
94
	/* core.ignorecase */
95
	GIT_IGNORECASE_DEFAULT = GIT_CONFIGMAP_FALSE,
96
	/* core.filemode */
97
	GIT_FILEMODE_DEFAULT = GIT_CONFIGMAP_TRUE,
98
	/* core.ignorestat */
99
	GIT_IGNORESTAT_DEFAULT = GIT_CONFIGMAP_FALSE,
100
	/* core.trustctime */
101
	GIT_TRUSTCTIME_DEFAULT = GIT_CONFIGMAP_TRUE,
102 103
	/* core.abbrev */
	GIT_ABBREV_DEFAULT = 7,
104
	/* core.precomposeunicode */
105
	GIT_PRECOMPOSE_DEFAULT = GIT_CONFIGMAP_FALSE,
106
	/* core.safecrlf */
107
	GIT_SAFE_CRLF_DEFAULT = GIT_CONFIGMAP_FALSE,
108
	/* core.logallrefupdates */
109 110
	GIT_LOGALLREFUPDATES_FALSE = GIT_CONFIGMAP_FALSE,
	GIT_LOGALLREFUPDATES_TRUE = GIT_CONFIGMAP_TRUE,
111
	GIT_LOGALLREFUPDATES_UNSET = 2,
112
	GIT_LOGALLREFUPDATES_ALWAYS = 3,
113
	GIT_LOGALLREFUPDATES_DEFAULT = GIT_LOGALLREFUPDATES_UNSET,
114
	/* core.protectHFS */
115
	GIT_PROTECTHFS_DEFAULT = GIT_CONFIGMAP_FALSE,
116
	/* core.protectNTFS */
117
	GIT_PROTECTNTFS_DEFAULT = GIT_CONFIGMAP_TRUE,
118
	/* core.fsyncObjectFiles */
119
	GIT_FSYNCOBJECTFILES_DEFAULT = GIT_CONFIGMAP_FALSE,
120
	/* core.longpaths */
121
	GIT_LONGPATHS_DEFAULT = GIT_CONFIGMAP_FALSE
122
} git_configmap_value;
123

124 125 126 127
/* internal repository init flags */
enum {
	GIT_REPOSITORY_INIT__HAS_DOTGIT = (1u << 16),
	GIT_REPOSITORY_INIT__NATURAL_WD = (1u << 17),
128
	GIT_REPOSITORY_INIT__IS_REINIT  = (1u << 18)
129 130 131
};

/** Internal structure for repository object */
132
struct git_repository {
133
	git_odb *_odb;
134
	git_refdb *_refdb;
135 136
	git_config *_config;
	git_index *_index;
137

Vicent Marti committed
138
	git_cache objects;
139
	git_attr_cache *attrcache;
140
	git_diff_driver_registry *diff_drivers;
141

142 143
	char *gitlink;
	char *gitdir;
144
	char *commondir;
145
	char *workdir;
Vicent Marti committed
146
	char *namespace;
147

148 149 150
	char *ident_name;
	char *ident_email;

151
	git_array_t(git_str) reserved_names;
152 153

	unsigned is_bare:1;
154
	unsigned is_worktree:1;
155

156
	unsigned int lru_counter;
157

158
	git_atomic32 attr_session_key;
159

160
	intptr_t configmap_cache[GIT_CONFIGMAP_CACHE_MAX];
161
	git_strmap *submodule_cache;
162 163
};

164 165
GIT_INLINE(git_attr_cache *) git_repository_attr_cache(git_repository *repo)
{
166
	return repo->attrcache;
167 168
}

169
int git_repository_head_tree(git_tree **tree, git_repository *repo);
170
int git_repository_create_head(const char *git_dir, const char *ref_name);
171

172 173 174 175 176 177
typedef int (*git_repository_foreach_worktree_cb)(git_repository *, void *);

int git_repository_foreach_worktree(git_repository *repo,
				    git_repository_foreach_worktree_cb cb,
				    void *payload);

178 179 180 181 182 183 184
/*
 * Weak pointers to repository internals.
 *
 * The returned pointers do not need to be freed. Do not keep
 * permanent references to these (i.e. between API calls), since they may
 * become invalidated if the user replaces a repository internal.
 */
185 186
int git_repository_config__weakptr(git_config **out, git_repository *repo);
int git_repository_odb__weakptr(git_odb **out, git_repository *repo);
187
int git_repository_refdb__weakptr(git_refdb **out, git_repository *repo);
188 189
int git_repository_index__weakptr(git_index **out, git_repository *repo);

190
/*
191
 * Configuration map cache
192 193
 *
 * Efficient access to the most used config variables of a repository.
Will Stamper committed
194
 * The cache is cleared every time the config backend is replaced.
195
 */
196 197
int git_repository__configmap_lookup(int *out, git_repository *repo, git_configmap_item item);
void git_repository__configmap_lookup_cache_clear(git_repository *repo);
198

199 200
int git_repository__item_path(git_str *out, const git_repository *repo, git_repository_item_t item);

201 202 203 204 205 206 207
GIT_INLINE(int) git_repository__ensure_not_bare(
	git_repository *repo,
	const char *operation_name)
{
	if (!git_repository_is_bare(repo))
		return 0;

208 209
	git_error_set(
		GIT_ERROR_REPOSITORY,
210
		"cannot %s. This operation is not allowed against bare repositories.",
211 212 213 214 215
		operation_name);

	return GIT_EBAREREPO;
}

216 217
int git_repository__set_orig_head(git_repository *repo, const git_oid *orig_head);

218 219
int git_repository__cleanup_files(git_repository *repo, const char *files[], size_t files_len);

220
/* The default "reserved names" for a repository */
221
extern git_str git_repository__reserved_names_win32[];
222 223
extern size_t git_repository__reserved_names_win32_len;

224
extern git_str git_repository__reserved_names_posix[];
225
extern size_t git_repository__reserved_names_posix_len;
226

227 228 229 230 231 232 233 234 235
/*
 * Gets any "reserved names" in the repository.  This will return paths
 * that should not be allowed in the repository (like ".git") to avoid
 * conflicting with the repository path, or with alternate mechanisms to
 * the repository path (eg, "GIT~1").  Every attempt will be made to look
 * up all possible reserved names - if there was a conflict for the shortname
 * GIT~1, for example, this function will try to look up the alternate
 * shortname.  If that fails, this function returns false, but out and outlen
 * will still be populated with good defaults.
236
 */
237
bool git_repository__reserved_names(
238
	git_str **out, size_t *outlen, git_repository *repo, bool include_ntfs);
239

240 241 242 243
/*
 * The default branch for the repository; the `init.defaultBranch`
 * configuration option, if set, or `master` if it is not.
 */
244
int git_repository_initialbranch(git_str *out, git_repository *repo);
245

246 247 248 249 250 251
/*
 * Given a relative `path`, this makes it absolute based on the
 * repository's working directory.  This will perform validation
 * to ensure that the path is not longer than MAX_PATH on Windows
 * (unless `core.longpaths` is set in the repo config).
 */
252
int git_repository_workdir_path(git_str *out, git_repository *repo, const char *path);
253

254 255 256 257
int git_repository__extensions(char ***out, size_t *out_len);
int git_repository__set_extensions(const char **extensions, size_t len);
void git_repository__free_extensions(void);

258
#endif