cache.h 60.9 KB
Newer Older
1 2 3
#ifndef CACHE_H
#define CACHE_H

4
#include "git-compat-util.h"
5
#include "strbuf.h"
6
#include "hashmap.h"
7
#include "list.h"
8
#include "advice.h"
9
#include "gettext.h"
10
#include "convert.h"
11
#include "trace.h"
12
#include "string-list.h"
13
#include "pack-revindex.h"
14
#include "hash.h"
Brandon Williams's avatar
Brandon Williams committed
15
#include "path.h"
16
#include "sha1-array.h"
17
#include "repository.h"
18

19
#include <zlib.h>
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
typedef struct git_zstream {
	z_stream z;
	unsigned long avail_in;
	unsigned long avail_out;
	unsigned long total_in;
	unsigned long total_out;
	unsigned char *next_in;
	unsigned char *next_out;
} git_zstream;

void git_inflate_init(git_zstream *);
void git_inflate_init_gzip_only(git_zstream *);
void git_inflate_end(git_zstream *);
int git_inflate(git_zstream *, int flush);

void git_deflate_init(git_zstream *, int level);
void git_deflate_init_gzip(git_zstream *, int level);
37
void git_deflate_init_raw(git_zstream *, int level);
38
void git_deflate_end(git_zstream *);
39
int git_deflate_abort(git_zstream *);
40 41 42
int git_deflate_end_gently(git_zstream *);
int git_deflate(git_zstream *, int flush);
unsigned long git_deflate_bound(git_zstream *, unsigned long);
43

44 45 46 47
/* The length in bytes and in hex digits of an object name (SHA-1 value). */
#define GIT_SHA1_RAWSZ 20
#define GIT_SHA1_HEXSZ (2 * GIT_SHA1_RAWSZ)

48 49 50 51
/* The length in byte and in hex digits of the largest possible hash value. */
#define GIT_MAX_RAWSZ GIT_SHA1_RAWSZ
#define GIT_MAX_HEXSZ GIT_SHA1_HEXSZ

52
struct object_id {
53
	unsigned char hash[GIT_MAX_RAWSZ];
54 55
};

56 57
#define the_hash_algo the_repository->hash_algo

58
#if defined(DT_UNKNOWN) && !defined(NO_D_TYPE_IN_DIRENT)
59 60
#define DTYPE(de)	((de)->d_type)
#else
61 62 63 64
#undef DT_UNKNOWN
#undef DT_DIR
#undef DT_REG
#undef DT_LNK
65 66 67
#define DT_UNKNOWN	0
#define DT_DIR		1
#define DT_REG		2
68
#define DT_LNK		3
69 70 71
#define DTYPE(de)	DT_UNKNOWN
#endif

Martin Koegler's avatar
Martin Koegler committed
72 73 74
/* unknown mode (impossible combination S_IFIFO|S_IFCHR) */
#define S_IFINVALID     0030000

75 76 77 78 79 80
/*
 * A "directory link" is a link to another git directory.
 *
 * The value 0160000 is not normally a valid mode, and
 * also just happens to be S_IFDIR + S_IFLNK
 */
81 82
#define S_IFGITLINK	0160000
#define S_ISGITLINK(m)	(((m) & S_IFMT) == S_IFGITLINK)
83

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
/*
 * Some mode bits are also used internally for computations.
 *
 * They *must* not overlap with any valid modes, and they *must* not be emitted
 * to outside world - i.e. appear on disk or network. In other words, it's just
 * temporary fields, which we internally use, but they have to stay in-house.
 *
 * ( such approach is valid, as standard S_IF* fits into 16 bits, and in Git
 *   codebase mode is `unsigned int` which is assumed to be at least 32 bits )
 */

/* used internally in tree-diff */
#define S_DIFFTREE_IFXMIN_NEQ	0x80000000


99 100 101 102 103 104 105
/*
 * Intensive research over the course of many years has shown that
 * port 9418 is totally unused by anything else. Or
 *
 *	Your search - "port 9418" - did not match any documents.
 *
 * as www.google.com puts it.
106 107 108 109 110 111 112 113 114 115 116
 *
 * This port has been properly assigned for git use by IANA:
 * git (Assigned-9418) [I06-050728-0001].
 *
 *	git  9418/tcp   git pack transfer service
 *	git  9418/udp   git pack transfer service
 *
 * with Linus Torvalds <torvalds@osdl.org> as the point of
 * contact. September 2005.
 *
 * See http://www.iana.org/assignments/port-numbers
117 118 119
 */
#define DEFAULT_GIT_PORT 9418

120 121 122 123 124 125
/*
 * Basic data structures for the directory cache
 */

#define CACHE_SIGNATURE 0x44495243	/* "DIRC" */
struct cache_header {
126 127 128
	uint32_t hdr_signature;
	uint32_t hdr_version;
	uint32_t hdr_entries;
129 130
};

131 132 133
#define INDEX_FORMAT_LB 2
#define INDEX_FORMAT_UB 4

134 135 136 137 138 139
/*
 * The "cache_time" is just the low 32 bits of the
 * time. It doesn't matter if it overflows - we only
 * check it for equality in the 32 bits we save.
 */
struct cache_time {
140 141
	uint32_t sec;
	uint32_t nsec;
142 143
};

144 145 146 147 148 149 150 151 152 153
struct stat_data {
	struct cache_time sd_ctime;
	struct cache_time sd_mtime;
	unsigned int sd_dev;
	unsigned int sd_ino;
	unsigned int sd_uid;
	unsigned int sd_gid;
	unsigned int sd_size;
};

154
struct cache_entry {
155
	struct hashmap_entry ent;
156
	struct stat_data ce_stat_data;
157
	unsigned int ce_mode;
158
	unsigned int ce_flags;
159
	unsigned int ce_namelen;
160
	unsigned int index;	/* for link extension */
161
	struct object_id oid;
162
	char name[FLEX_ARRAY]; /* more */
163 164
};

165
#define CE_STAGEMASK (0x3000)
166
#define CE_EXTENDED  (0x4000)
Junio C Hamano's avatar
Junio C Hamano committed
167
#define CE_VALID     (0x8000)
168
#define CE_STAGESHIFT 12
169

170
/*
171
 * Range 0xFFFF0FFF in ce_flags is divided into
172 173 174 175 176 177 178
 * two parts: in-memory flags and on-disk ones.
 * Flags in CE_EXTENDED_FLAGS will get saved on-disk
 * if you want to save a new flag, add it in
 * CE_EXTENDED_FLAGS
 *
 * In-memory only flags
 */
179 180 181 182
#define CE_UPDATE            (1 << 16)
#define CE_REMOVE            (1 << 17)
#define CE_UPTODATE          (1 << 18)
#define CE_ADDED             (1 << 19)
183

184
#define CE_HASHED            (1 << 20)
185
#define CE_FSMONITOR_VALID   (1 << 21)
186 187
#define CE_WT_REMOVE         (1 << 22) /* remove in work directory */
#define CE_CONFLICTED        (1 << 23)
188

189
#define CE_UNPACKED          (1 << 24)
190
#define CE_NEW_SKIP_WORKTREE (1 << 25)
191

192 193 194
/* used to temporarily mark paths matched by pathspecs */
#define CE_MATCHED           (1 << 26)

195
#define CE_UPDATE_IN_BASE    (1 << 27)
196
#define CE_STRIP_NAME        (1 << 28)
197

198 199 200
/*
 * Extended on-disk flags
 */
201 202
#define CE_INTENT_TO_ADD     (1 << 29)
#define CE_SKIP_WORKTREE     (1 << 30)
203
/* CE_EXTENDED2 is for future extension */
204
#define CE_EXTENDED2         (1U << 31)
205

206
#define CE_EXTENDED_FLAGS (CE_INTENT_TO_ADD | CE_SKIP_WORKTREE)
207 208 209 210 211 212 213 214 215 216 217

/*
 * Safeguard to avoid saving wrong flags:
 *  - CE_EXTENDED2 won't get saved until its semantic is known
 *  - Bits in 0x0000FFFF have been saved in ce_flags already
 *  - Bits in 0x003F0000 are currently in-memory flags
 */
#if CE_EXTENDED_FLAGS & 0x803FFFFF
#error "CE_EXTENDED_FLAGS out of range"
#endif

218
/* Forward structure decls */
219
struct pathspec;
220
struct child_process;
221

222 223 224 225
/*
 * Copy the sha1 and stat state of a cache entry from one to
 * another. But we never change the name, or the hash state!
 */
226 227
static inline void copy_cache_entry(struct cache_entry *dst,
				    const struct cache_entry *src)
228
{
229
	unsigned int state = dst->ce_flags & CE_HASHED;
230 231

	/* Don't copy hash chain and name */
232 233 234
	memcpy(&dst->ce_stat_data, &src->ce_stat_data,
			offsetof(struct cache_entry, name) -
			offsetof(struct cache_entry, ce_stat_data));
235 236

	/* Restore the hash state */
237
	dst->ce_flags = (dst->ce_flags & ~CE_HASHED) | state;
238 239
}

240
static inline unsigned create_ce_flags(unsigned stage)
241
{
242
	return (stage << CE_STAGESHIFT);
243 244
}

245
#define ce_namelen(ce) ((ce)->ce_namelen)
246
#define ce_size(ce) cache_entry_size(ce_namelen(ce))
247
#define ce_stage(ce) ((CE_STAGEMASK & (ce)->ce_flags) >> CE_STAGESHIFT)
248
#define ce_uptodate(ce) ((ce)->ce_flags & CE_UPTODATE)
249
#define ce_skip_worktree(ce) ((ce)->ce_flags & CE_SKIP_WORKTREE)
250
#define ce_mark_uptodate(ce) ((ce)->ce_flags |= CE_UPTODATE)
251
#define ce_intent_to_add(ce) ((ce)->ce_flags & CE_INTENT_TO_ADD)
252

253
#define ce_permissions(mode) (((mode) & 0100) ? 0755 : 0644)
254 255 256
static inline unsigned int create_ce_mode(unsigned int mode)
{
	if (S_ISLNK(mode))
257
		return S_IFLNK;
258
	if (S_ISDIR(mode) || S_ISGITLINK(mode))
259 260
		return S_IFGITLINK;
	return S_IFREG | ce_permissions(mode);
261
}
262 263
static inline unsigned int ce_mode_from_stat(const struct cache_entry *ce,
					     unsigned int mode)
264
{
265 266
	extern int trust_executable_bit, has_symlinks;
	if (!has_symlinks && S_ISREG(mode) &&
267
	    ce && S_ISLNK(ce->ce_mode))
268
		return ce->ce_mode;
269
	if (!trust_executable_bit && S_ISREG(mode)) {
270
		if (ce && S_ISREG(ce->ce_mode))
271 272 273 274 275
			return ce->ce_mode;
		return create_ce_mode(0666);
	}
	return create_ce_mode(mode);
}
276 277 278 279 280 281 282 283 284 285 286 287
static inline int ce_to_dtype(const struct cache_entry *ce)
{
	unsigned ce_mode = ntohl(ce->ce_mode);
	if (S_ISREG(ce_mode))
		return DT_REG;
	else if (S_ISDIR(ce_mode) || S_ISGITLINK(ce_mode))
		return DT_DIR;
	else if (S_ISLNK(ce_mode))
		return DT_LNK;
	else
		return DT_UNKNOWN;
}
288 289 290 291 292 293 294 295 296 297
static inline unsigned int canon_mode(unsigned int mode)
{
	if (S_ISREG(mode))
		return S_IFREG | ce_permissions(mode);
	if (S_ISLNK(mode))
		return S_IFLNK;
	if (S_ISDIR(mode))
		return S_IFDIR;
	return S_IFGITLINK;
}
298

299
#define cache_entry_size(len) (offsetof(struct cache_entry,name) + (len) + 1)
300

301 302 303 304
#define SOMETHING_CHANGED	(1 << 0) /* unclassified changes go here */
#define CE_ENTRY_CHANGED	(1 << 1)
#define CE_ENTRY_REMOVED	(1 << 2)
#define CE_ENTRY_ADDED		(1 << 3)
305
#define RESOLVE_UNDO_CHANGED	(1 << 4)
306
#define CACHE_TREE_CHANGED	(1 << 5)
307
#define SPLIT_INDEX_ORDERED	(1 << 6)
308
#define UNTRACKED_CHANGED	(1 << 7)
309
#define FSMONITOR_CHANGED	(1 << 8)
310

311
struct split_index;
312 313
struct untracked_cache;

314 315
struct index_state {
	struct cache_entry **cache;
316
	unsigned int version;
317
	unsigned int cache_nr, cache_alloc, cache_changed;
318
	struct string_list *resolve_undo;
319
	struct cache_tree *cache_tree;
320
	struct split_index *split_index;
321
	struct cache_time timestamp;
322
	unsigned name_hash_initialized : 1,
323 324
		 initialized : 1,
		 drop_cache_tree : 1;
325
	struct hashmap name_hash;
326
	struct hashmap dir_hash;
327
	struct object_id oid;
328
	struct untracked_cache *untracked;
329
	uint64_t fsmonitor_last_update;
330
	struct ewah_bitmap *fsmonitor_dirty;
331 332 333 334
};

extern struct index_state the_index;

335
/* Name hashing */
336
extern int test_lazy_init_name_hash(struct index_state *istate, int try_threaded);
337
extern void add_name_hash(struct index_state *istate, struct cache_entry *ce);
338 339
extern void remove_name_hash(struct index_state *istate, struct cache_entry *ce);
extern void free_name_hash(struct index_state *istate);
340 341


342
#ifndef NO_THE_INDEX_COMPATIBILITY_MACROS
343 344 345 346 347
#define active_cache (the_index.cache)
#define active_nr (the_index.cache_nr)
#define active_alloc (the_index.cache_alloc)
#define active_cache_changed (the_index.cache_changed)
#define active_cache_tree (the_index.cache_tree)
348

349
#define read_cache() read_index(&the_index)
350
#define read_cache_from(path) read_index_from(&the_index, (path), (get_git_dir()))
351
#define read_cache_preload(pathspec) read_index_preload(&the_index, (pathspec))
352
#define is_cache_unborn() is_index_unborn(&the_index)
353
#define read_cache_unmerged() read_index_unmerged(&the_index)
354
#define discard_cache() discard_index(&the_index)
355
#define unmerged_cache() unmerged_index(&the_index)
356 357
#define cache_name_pos(name, namelen) index_name_pos(&the_index,(name),(namelen))
#define add_cache_entry(ce, option) add_index_entry(&the_index, (ce), (option))
358
#define rename_cache_entry_at(pos, new_name) rename_index_entry_at(&the_index, (pos), (new_name))
359 360
#define remove_cache_entry_at(pos) remove_index_entry_at(&the_index, (pos))
#define remove_file_from_cache(path) remove_file_from_index(&the_index, (path))
361 362
#define add_to_cache(path, st, flags) add_to_index(&the_index, (path), (st), (flags))
#define add_file_to_cache(path, flags) add_file_to_index(&the_index, (path), (flags))
363
#define chmod_cache_entry(ce, flip) chmod_index_entry(&the_index, (ce), (flip))
364
#define refresh_cache(flags) refresh_index(&the_index, (flags), NULL, NULL, NULL)
365 366
#define ce_match_stat(ce, st, options) ie_match_stat(&the_index, (ce), (st), (options))
#define ce_modified(ce, st, options) ie_modified(&the_index, (ce), (st), (options))
367 368
#define cache_dir_exists(name, namelen) index_dir_exists(&the_index, (name), (namelen))
#define cache_file_exists(name, namelen, igncase) index_file_exists(&the_index, (name), (namelen), (igncase))
369
#define cache_name_is_other(name, namelen) index_name_is_other(&the_index, (name), (namelen))
370
#define resolve_undo_clear() resolve_undo_clear_index(&the_index)
371
#define unmerge_cache_entry_at(at) unmerge_index_entry_at(&the_index, at)
372
#define unmerge_cache(pathspec) unmerge_index(&the_index, pathspec)
373
#define read_blob_data_from_cache(path, sz) read_blob_data_from_index(&the_index, (path), (sz))
374
#endif
375

376 377
#define TYPE_BITS 3

378 379 380 381 382
/*
 * Values in this enum (except those outside the 3 bit range) are part
 * of pack file format. See Documentation/technical/pack-format.txt
 * for more information.
 */
383 384 385 386 387 388 389 390 391 392
enum object_type {
	OBJ_BAD = -1,
	OBJ_NONE = 0,
	OBJ_COMMIT = 1,
	OBJ_TREE = 2,
	OBJ_BLOB = 3,
	OBJ_TAG = 4,
	/* 5 for future expansion */
	OBJ_OFS_DELTA = 6,
	OBJ_REF_DELTA = 7,
393
	OBJ_ANY,
394
	OBJ_MAX
395 396
};

397 398 399 400 401 402 403
static inline enum object_type object_type(unsigned int mode)
{
	return S_ISDIR(mode) ? OBJ_TREE :
		S_ISGITLINK(mode) ? OBJ_COMMIT :
		OBJ_BLOB;
}

404
/* Double-check local_repo_env below if you add to this list. */
405
#define GIT_DIR_ENVIRONMENT "GIT_DIR"
406
#define GIT_COMMON_DIR_ENVIRONMENT "GIT_COMMON_DIR"
407
#define GIT_NAMESPACE_ENVIRONMENT "GIT_NAMESPACE"
408
#define GIT_WORK_TREE_ENVIRONMENT "GIT_WORK_TREE"
409
#define GIT_PREFIX_ENVIRONMENT "GIT_PREFIX"
410
#define GIT_SUPER_PREFIX_ENVIRONMENT "GIT_INTERNAL_SUPER_PREFIX"
411
#define DEFAULT_GIT_DIR_ENVIRONMENT ".git"
412
#define DB_ENVIRONMENT "GIT_OBJECT_DIRECTORY"
413
#define INDEX_ENVIRONMENT "GIT_INDEX_FILE"
414
#define GRAFT_ENVIRONMENT "GIT_GRAFT_FILE"
415
#define GIT_SHALLOW_FILE_ENVIRONMENT "GIT_SHALLOW_FILE"
416 417
#define TEMPLATE_DIR_ENVIRONMENT "GIT_TEMPLATE_DIR"
#define CONFIG_ENVIRONMENT "GIT_CONFIG"
418
#define CONFIG_DATA_ENVIRONMENT "GIT_CONFIG_PARAMETERS"
419
#define EXEC_PATH_ENVIRONMENT "GIT_EXEC_PATH"
420
#define CEILING_DIRECTORIES_ENVIRONMENT "GIT_CEILING_DIRECTORIES"
421
#define NO_REPLACE_OBJECTS_ENVIRONMENT "GIT_NO_REPLACE_OBJECTS"
422
#define GIT_REPLACE_REF_BASE_ENVIRONMENT "GIT_REPLACE_REF_BASE"
423 424
#define GITATTRIBUTES_FILE ".gitattributes"
#define INFOATTRIBUTES_FILE "info/attributes"
Junio C Hamano's avatar
Junio C Hamano committed
425
#define ATTRIBUTE_MACRO_PREFIX "[attr]"
426
#define GITMODULES_FILE ".gitmodules"
427 428
#define GIT_NOTES_REF_ENVIRONMENT "GIT_NOTES_REF"
#define GIT_NOTES_DEFAULT_REF "refs/notes/commits"
429
#define GIT_NOTES_DISPLAY_REF_ENVIRONMENT "GIT_NOTES_DISPLAY_REF"
430 431
#define GIT_NOTES_REWRITE_REF_ENVIRONMENT "GIT_NOTES_REWRITE_REF"
#define GIT_NOTES_REWRITE_MODE_ENVIRONMENT "GIT_NOTES_REWRITE_MODE"
432
#define GIT_LITERAL_PATHSPECS_ENVIRONMENT "GIT_LITERAL_PATHSPECS"
433 434
#define GIT_GLOB_PATHSPECS_ENVIRONMENT "GIT_GLOB_PATHSPECS"
#define GIT_NOGLOB_PATHSPECS_ENVIRONMENT "GIT_NOGLOB_PATHSPECS"
435
#define GIT_ICASE_PATHSPECS_ENVIRONMENT "GIT_ICASE_PATHSPECS"
436
#define GIT_QUARANTINE_ENVIRONMENT "GIT_QUARANTINE_PATH"
437
#define GIT_OPTIONAL_LOCKS_ENVIRONMENT "GIT_OPTIONAL_LOCKS"
438
#define GIT_TEXT_DOMAIN_DIR_ENVIRONMENT "GIT_TEXTDOMAINDIR"
439

440 441 442 443 444 445 446
/*
 * Environment variable used in handshaking the wire protocol.
 * Contains a colon ':' separated list of keys with optional values
 * 'key[=value]'.  Presence of unknown keys and values must be
 * ignored.
 */
#define GIT_PROTOCOL_ENVIRONMENT "GIT_PROTOCOL"
447 448
/* HTTP header used to handshake the wire protocol */
#define GIT_PROTOCOL_HEADER "Git-Protocol"
449

450
/*
451 452 453 454 455 456 457 458 459 460 461
 * This environment variable is expected to contain a boolean indicating
 * whether we should or should not treat:
 *
 *   GIT_DIR=foo.git git ...
 *
 * as if GIT_WORK_TREE=. was given. It's not expected that users will make use
 * of this, but we use it internally to communicate to sub-processes that we
 * are in a bare repo. If not set, defaults to true.
 */
#define GIT_IMPLICIT_WORK_TREE_ENVIRONMENT "GIT_IMPLICIT_WORK_TREE"

462
/*
463 464 465 466
 * Repository-local GIT_* environment variables; these will be cleared
 * when git spawns a sub-process that runs inside another repository.
 * The array is NULL-terminated, which makes it easy to pass in the "env"
 * parameter of a run-command invocation, or to do a simple walk.
467
 */
468
extern const char * const local_repo_env[];
469

470
extern void setup_git_env(const char *git_dir);
471

472 473 474 475 476 477
/*
 * Returns true iff we have a configured git repository (either via
 * setup_git_directory, or in the environment via $GIT_DIR).
 */
int have_git_dir(void);

478 479
extern int is_bare_repository_cfg;
extern int is_bare_repository(void);
480
extern int is_inside_git_dir(void);
481
extern char *git_work_tree_cfg;
482
extern int is_inside_work_tree(void);
483
extern const char *get_git_dir(void);
484
extern const char *get_git_common_dir(void);
485 486
extern char *get_object_directory(void);
extern char *get_index_file(void);
487
extern char *get_graft_file(struct repository *r);
488
extern void set_git_dir(const char *path);
489
extern int get_common_dir_noenv(struct strbuf *sb, const char *gitdir);
490
extern int get_common_dir(struct strbuf *sb, const char *gitdir);
491 492
extern const char *get_git_namespace(void);
extern const char *strip_namespace(const char *namespaced_ref);
493
extern const char *get_super_prefix(void);
494
extern const char *get_git_work_tree(void);
495

496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
/*
 * Return true if the given path is a git directory; note that this _just_
 * looks at the directory itself. If you want to know whether "foo/.git"
 * is a repository, you must feed that path, not just "foo".
 */
extern int is_git_directory(const char *path);

/*
 * Return 1 if the given path is the root of a git repository or
 * submodule, else 0. Will not return 1 for bare repositories with the
 * exception of creating a bare repository in "foo/.git" and calling
 * is_git_repository("foo").
 *
 * If we run into read errors, we err on the side of saying "yes, it is",
 * as we usually consider sub-repos precious, and would prefer to err on the
 * side of not disrupting or deleting them.
 */
extern int is_nonbare_repository_dir(struct strbuf *path);

515 516 517 518 519 520 521
#define READ_GITFILE_ERR_STAT_FAILED 1
#define READ_GITFILE_ERR_NOT_A_FILE 2
#define READ_GITFILE_ERR_OPEN_FAILED 3
#define READ_GITFILE_ERR_READ_FAILED 4
#define READ_GITFILE_ERR_INVALID_FORMAT 5
#define READ_GITFILE_ERR_NO_PATH 6
#define READ_GITFILE_ERR_NOT_A_REPO 7
522
#define READ_GITFILE_ERR_TOO_LARGE 8
523
extern void read_gitfile_error_die(int error_code, const char *path, const char *dir);
524 525
extern const char *read_gitfile_gently(const char *path, int *return_error_code);
#define read_gitfile(path) read_gitfile_gently((path), NULL)
526 527 528
extern const char *resolve_gitdir_gently(const char *suspect, int *return_error_code);
#define resolve_gitdir(path) resolve_gitdir_gently((path), NULL)

529
extern void set_git_work_tree(const char *tree);
530 531

#define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"
532

533
extern void setup_work_tree(void);
534
/*
535 536 537 538 539 540
 * Find the commondir and gitdir of the repository that contains the current
 * working directory, without changing the working directory or other global
 * state. The result is appended to commondir and gitdir.  If the discovered
 * gitdir does not correspond to a worktree, then 'commondir' and 'gitdir' will
 * both have the same result appended to the buffer.  The return value is
 * either 0 upon success and non-zero if no repository was found.
541
 */
542 543
extern int discover_git_directory(struct strbuf *commondir,
				  struct strbuf *gitdir);
544
extern const char *setup_git_directory_gently(int *);
545
extern const char *setup_git_directory(void);
546
extern char *prefix_path(const char *prefix, int len, const char *path);
547
extern char *prefix_path_gently(const char *prefix, int len, int *remaining, const char *path);
548 549 550 551 552 553 554 555

/*
 * Concatenate "prefix" (if len is non-zero) and "path", with no
 * connecting characters (so "prefix" should end with a "/").
 * Unlike prefix_path, this should be used if the named file does
 * not have to interact with index entry; i.e. name of a random file
 * on the filesystem.
 *
556 557
 * The return value is always a newly allocated string (even if the
 * prefix was empty).
558
 */
559
extern char *prefix_filename(const char *prefix, const char *path);
560

561
extern int check_filename(const char *prefix, const char *name);
562 563 564
extern void verify_filename(const char *prefix,
			    const char *name,
			    int diagnose_misspelt_rev);
565
extern void verify_non_filename(const char *prefix, const char *name);
566
extern int path_inside_repo(const char *prefix, const char *path);
567

568
#define INIT_DB_QUIET 0x0001
569
#define INIT_DB_EXIST_OK 0x0002
570

571 572
extern int init_db(const char *git_dir, const char *real_git_dir,
		   const char *template_dir, unsigned int flags);
573

574
extern void sanitize_stdfds(void);
575
extern int daemonize(void);
576

577 578
#define alloc_nr(x) (((x)+16)*3/2)

Jeff King's avatar
Jeff King committed
579 580 581 582 583
/*
 * Realloc the buffer pointed at by variable 'x' so that it can hold
 * at least 'nr' entries; the number of entries currently allocated
 * is 'alloc', using the standard growing factor alloc_nr() macro.
 *
584
 * DO NOT USE any expression with side-effect for 'x', 'nr', or 'alloc'.
Jeff King's avatar
Jeff King committed
585 586 587
 */
#define ALLOC_GROW(x, nr, alloc) \
	do { \
Jeff King's avatar
Jeff King committed
588
		if ((nr) > alloc) { \
589 590 591 592
			if (alloc_nr(alloc) < (nr)) \
				alloc = (nr); \
			else \
				alloc = alloc_nr(alloc); \
593
			REALLOC_ARRAY(x, alloc); \
Jeff King's avatar
Jeff King committed
594
		} \
595
	} while (0)
Jeff King's avatar
Jeff King committed
596

597
/* Initialize and use the cache information */
598
struct lock_file;
599
extern int read_index(struct index_state *);
600
extern int read_index_preload(struct index_state *, const struct pathspec *pathspec);
601 602
extern int do_read_index(struct index_state *istate, const char *path,
			 int must_exist); /* for testting only! */
603 604
extern int read_index_from(struct index_state *, const char *path,
			   const char *gitdir);
605
extern int is_index_unborn(struct index_state *);
606
extern int read_index_unmerged(struct index_state *);
607 608

/* For use with `write_locked_index()`. */
609
#define COMMIT_LOCK		(1 << 0)
610
#define SKIP_IF_UNCHANGED	(1 << 1)
611 612

/*
613 614
 * Write the index while holding an already-taken lock. Close the lock,
 * and if `COMMIT_LOCK` is given, commit it.
615 616 617 618 619 620 621 622
 *
 * Unless a split index is in use, write the index into the lockfile.
 *
 * With a split index, write the shared index to a temporary file,
 * adjust its permissions and rename it into place, then write the
 * split index to the lockfile. If the temporary file for the shared
 * index cannot be created, fall back to the behavior described in
 * the previous paragraph.
623 624 625 626
 *
 * With `COMMIT_LOCK`, the lock is always committed or rolled back.
 * Without it, the lock is closed, but neither committed nor rolled
 * back.
627 628 629
 *
 * If `SKIP_IF_UNCHANGED` is given and the index is unchanged, nothing
 * is written (and the lock is rolled back if `COMMIT_LOCK` is given).
630
 */
631
extern int write_locked_index(struct index_state *, struct lock_file *lock, unsigned flags);
632

633
extern int discard_index(struct index_state *);
634
extern void move_index_extensions(struct index_state *dst, struct index_state *src);
635
extern int unmerged_index(const struct index_state *);
636 637 638 639 640 641 642 643 644

/**
 * Returns 1 if the index differs from HEAD, 0 otherwise. When on an unborn
 * branch, returns 1 if there are entries in the index, 0 otherwise. If an
 * strbuf is provided, the space-separated list of files that differ will be
 * appended to it.
 */
extern int index_has_changes(struct strbuf *sb);

645
extern int verify_path(const char *path, unsigned mode);
646
extern int strcmp_offset(const char *s1, const char *s2, size_t *first_change);
647 648
extern int index_dir_exists(struct index_state *istate, const char *name, int namelen);
extern void adjust_dirname_case(struct index_state *istate, char *name);
649
extern struct cache_entry *index_file_exists(struct index_state *istate, const char *name, int namelen, int igncase);
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667

/*
 * Searches for an entry defined by name and namelen in the given index.
 * If the return value is positive (including 0) it is the position of an
 * exact match. If the return value is negative, the negated value minus 1
 * is the position where the entry would be inserted.
 * Example: The current index consists of these files and its stages:
 *
 *   b#0, d#0, f#1, f#3
 *
 * index_name_pos(&index, "a", 1) -> -1
 * index_name_pos(&index, "b", 1) ->  0
 * index_name_pos(&index, "c", 1) -> -2
 * index_name_pos(&index, "d", 1) ->  1
 * index_name_pos(&index, "e", 1) -> -3
 * index_name_pos(&index, "f", 1) -> -3
 * index_name_pos(&index, "g", 1) -> -5
 */
668
extern int index_name_pos(const struct index_state *, const char *name, int namelen);
669

670 671
#define ADD_CACHE_OK_TO_ADD 1		/* Ok to add */
#define ADD_CACHE_OK_TO_REPLACE 2	/* Ok to replace file/directory */
672
#define ADD_CACHE_SKIP_DFCHECK 4	/* Ok to skip DF conflict checks */
673
#define ADD_CACHE_JUST_APPEND 8		/* Append only; tree.c::read_tree() */
674
#define ADD_CACHE_NEW_ONLY 16		/* Do not replace existing ones */
675
#define ADD_CACHE_KEEP_CACHE_TREE 32	/* Do not invalidate cache-tree */
676
extern int add_index_entry(struct index_state *, struct cache_entry *ce, int option);
677
extern void rename_index_entry_at(struct index_state *, int pos, const char *new_name);
678 679

/* Remove entry, return true if there are more entries to go. */
680
extern int remove_index_entry_at(struct index_state *, int pos);
681

682
extern void remove_marked_cache_entries(struct index_state *istate);
683
extern int remove_file_from_index(struct index_state *, const char *path);
684 685
#define ADD_CACHE_VERBOSE 1
#define ADD_CACHE_PRETEND 2
686
#define ADD_CACHE_IGNORE_ERRORS	4
687
#define ADD_CACHE_IGNORE_REMOVAL 8
688
#define ADD_CACHE_INTENT 16
689 690 691 692 693 694 695 696 697
/*
 * These two are used to add the contents of the file at path
 * to the index, marking the working tree up-to-date by storing
 * the cached stat info in the resulting cache entry.  A caller
 * that has already run lstat(2) on the path can call
 * add_to_index(), and all others can call add_file_to_index();
 * the latter will do necessary lstat(2) internally before
 * calling the former.
 */
698 699
extern int add_to_index(struct index_state *, const char *path, struct stat *, int flags);
extern int add_file_to_index(struct index_state *, const char *path, int flags);
700

701
extern struct cache_entry *make_cache_entry(unsigned int mode, const unsigned char *sha1, const char *path, int stage, unsigned int refresh_options);
702
extern int chmod_index_entry(struct index_state *, struct cache_entry *ce, char flip);
703
extern int ce_same_name(const struct cache_entry *a, const struct cache_entry *b);
704
extern void set_object_name_for_intent_to_add_entry(struct cache_entry *ce);
705
extern int index_name_is_other(const struct index_state *, const char *, int);
706
extern void *read_blob_data_from_index(const struct index_state *, const char *, unsigned long *);
707 708 709 710

/* do stat comparison even if CE_VALID is true */
#define CE_MATCH_IGNORE_VALID		01
/* do not check the contents but report dirty on racily-clean entries */
711 712 713
#define CE_MATCH_RACY_IS_DIRTY		02
/* do stat comparison even if CE_SKIP_WORKTREE is true */
#define CE_MATCH_IGNORE_SKIP_WORKTREE	04
714 715
/* ignore non-existent files during stat update  */
#define CE_MATCH_IGNORE_MISSING		0x08
716 717
/* enable stat refresh */
#define CE_MATCH_REFRESH		0x10
718 719 720 721
/* don't refresh_fsmonitor state or do stat comparison even if CE_FSMONITOR_VALID is true */
#define CE_MATCH_IGNORE_FSMONITOR 0X20
extern int ie_match_stat(struct index_state *, const struct cache_entry *, struct stat *, unsigned int);
extern int ie_modified(struct index_state *, const struct cache_entry *, struct stat *, unsigned int);
722

723 724
#define HASH_WRITE_OBJECT 1
#define HASH_FORMAT_CHECK 2
725
#define HASH_RENORMALIZE  4
726
extern int index_fd(struct object_id *oid, int fd, struct stat *st, enum object_type type, const char *path, unsigned flags);
727
extern int index_path(struct object_id *oid, const char *path, struct stat *st, unsigned flags);
728 729 730 731 732 733 734 735 736 737 738 739 740 741

/*
 * Record to sd the data from st that we use to check whether a file
 * might have changed.
 */
extern void fill_stat_data(struct stat_data *sd, struct stat *st);

/*
 * Return 0 if st is consistent with a file not having been changed
 * since sd was filled.  If there are differences, return a
 * combination of MTIME_CHANGED, CTIME_CHANGED, OWNER_CHANGED,
 * INODE_CHANGED, and DATA_CHANGED.
 */
extern int match_stat_data(const struct stat_data *sd, struct stat *st);
742 743
extern int match_stat_data_racy(const struct index_state *istate,
				const struct stat_data *sd, struct stat *st);
744

745 746
extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);

747 748 749 750
#define REFRESH_REALLY		0x0001	/* ignore_valid */
#define REFRESH_UNMERGED	0x0002	/* allow unmerged */
#define REFRESH_QUIET		0x0004	/* be quiet about it */
#define REFRESH_IGNORE_MISSING	0x0008	/* ignore non-existent */
751
#define REFRESH_IGNORE_SUBMODULES	0x0010	/* ignore submodules */
752
#define REFRESH_IN_PORCELAIN	0x0020	/* user friendly output, not "needs update" */
753
extern int refresh_index(struct index_state *, unsigned int flags, const struct pathspec *pathspec, char *seen, const char *header_msg);
754
extern struct cache_entry *refresh_cache_entry(struct cache_entry *, unsigned int);
755

756 757 758 759
/*
 * Opportunistically update the index but do not complain if we can't.
 * The lockfile is always committed or rolled back.
 */
760
extern void update_index_if_able(struct index_state *, struct lock_file *);
761 762

extern int hold_locked_index(struct lock_file *, int);
763
extern void set_alternate_index_output(const char *);
764

765
extern int verify_index_checksum;
766
extern int verify_ce_order;
767

768
/* Environment bits from configuration mechanism */
769
extern int trust_executable_bit;
770
extern int trust_ctime;
771
extern int check_stat;
772
extern int quote_path_fully;
773
extern int has_symlinks;
774
extern int minimum_abbrev, default_abbrev;
775
extern int ignore_case;
Junio C Hamano's avatar
Junio C Hamano committed
776
extern int assume_unchanged;
777
extern int prefer_symlink_refs;
778
extern int warn_ambiguous_refs;
779
extern int warn_on_object_refname_ambiguity;
780
extern const char *apply_default_whitespace;
781
extern const char *apply_default_ignorewhitespace;
782
extern const char *git_attributes_file;
783
extern const char *git_hooks_path;
784
extern int zlib_compression_level;
785
extern int core_compression_level;
786
extern int pack_compression_level;
787
extern size_t packed_git_window_size;
788
extern size_t packed_git_limit;
789
extern size_t delta_base_cache_limit;
790
extern unsigned long big_file_threshold;
791
extern unsigned long pack_size_limit_cfg;
792

793 794 795 796 797 798
/*
 * Accessors for the core.sharedrepository config which lazy-load the value
 * from the config (if not already set). The "reset" function can be
 * used to unset "set" or cached value, meaning that the value will be loaded
 * fresh from the config file on the next call to get_shared_repository().
 */
799 800
void set_shared_repository(int value);
int get_shared_repository(void);
801
void reset_shared_repository(void);
802

803 804 805 806
/*
 * Do replace refs need to be checked this run?  This variable is
 * initialized to true unless --no-replace-object is used or
 * $GIT_NO_REPLACE_OBJECTS is set, but is set to false by some
807
 * commands that do not want replace references to be active.
808
 */
809
extern int read_replace_refs;
810
extern char *git_replace_ref_base;
811

812
extern int fsync_object_files;
813
extern int core_preload_index;
814
extern int core_commit_graph;
815
extern int core_apply_sparse_checkout;
816
extern int precomposed_unicode;
817
extern int protect_hfs;
818
extern int protect_ntfs;
819
extern const char *core_fsmonitor;
820

821 822 823 824 825 826 827 828
/*
 * Include broken refs in all ref iterations, which will
 * generally choke dangerous operations rather than letting
 * them silently proceed without taking the broken ref into
 * account.
 */
extern int ref_paranoia;

829 830 831 832 833
/*
 * Returns the boolean value of $GIT_OPTIONAL_LOCKS (or the default value).
 */
int use_optional_locks(void);

834 835 836 837 838
/*
 * The character that begins a commented line in user-editable file
 * that is subject to stripspace.
 */
extern char comment_line_char;
839
extern int auto_comment_line_char;
840

841 842 843 844 845 846 847 848
/* Windows only */
enum hide_dotfiles_type {
	HIDE_DOTFILES_FALSE = 0,
	HIDE_DOTFILES_TRUE,
	HIDE_DOTFILES_DOTGITONLY
};
extern enum hide_dotfiles_type hide_dotfiles;

849 850 851 852 853 854 855 856
enum log_refs_config {
	LOG_REFS_UNSET = -1,
	LOG_REFS_NONE = 0,
	LOG_REFS_NORMAL,
	LOG_REFS_ALWAYS
};
extern enum log_refs_config log_all_ref_updates;

857
enum branch_track {
858
	BRANCH_TRACK_UNSPECIFIED = -1,
859 860 861 862
	BRANCH_TRACK_NEVER = 0,
	BRANCH_TRACK_REMOTE,
	BRANCH_TRACK_ALWAYS,
	BRANCH_TRACK_EXPLICIT,
863
	BRANCH_TRACK_OVERRIDE
864 865
};

866 867 868 869
enum rebase_setup_type {
	AUTOREBASE_NEVER = 0,
	AUTOREBASE_LOCAL,
	AUTOREBASE_REMOTE,
870
	AUTOREBASE_ALWAYS
871 872
};

873 874 875
enum push_default_type {
	PUSH_DEFAULT_NOTHING = 0,
	PUSH_DEFAULT_MATCHING,
876
	PUSH_DEFAULT_SIMPLE,
877
	PUSH_DEFAULT_UPSTREAM,
878 879
	PUSH_DEFAULT_CURRENT,
	PUSH_DEFAULT_UNSPECIFIED
880 881
};

882
extern enum branch_track git_branch_track;
883
extern enum rebase_setup_type autorebase;
884
extern enum push_default_type push_default;
885

886 887
enum object_creation_mode {
	OBJECT_CREATION_USES_HARDLINKS = 0,
888
	OBJECT_CREATION_USES_RENAMES = 1
889 890 891
};

extern enum object_creation_mode object_creation_mode;
892

893 894
extern char *notes_ref_name;

895 896
extern int grafts_replace_parents;

897 898 899 900 901
/*
 * GIT_REPO_VERSION is the version we write by default. The
 * _READ variant is the highest number we know how to
 * handle.
 */
902
#define GIT_REPO_VERSION 0
903
#define GIT_REPO_VERSION_READ 1
904
extern int repository_format_precious_objects;
905
extern char *repository_format_partial_clone;
906
extern const char *core_partial_clone_filter_default;
907

908 909 910
struct repository_format {
	int version;
	int precious_objects;
911
	char *partial_clone; /* value of extensions.partialclone */
912
	int is_bare;
913
	int hash_algo;
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
	char *work_tree;
	struct string_list unknown_extensions;
};

/*
 * Read the repository format characteristics from the config file "path" into
 * "format" struct. Returns the numeric version. On error, -1 is returned,
 * format->version is set to -1, and all other fields in the struct are
 * undefined.
 */
int read_repository_format(struct repository_format *format, const char *path);

/*
 * Verify that the repository described by repository_format is something we
 * can read. If it is, return 0. Otherwise, return -1, and "err" will describe
 * any errors encountered.
 */
int verify_repository_format(const struct repository_format *format,
			     struct strbuf *err);

934 935 936 937 938 939 940
/*
 * Check the repository format version in the path found in get_git_dir(),
 * and die if it is a version we don't understand. Generally one would
 * set_git_dir() before calling this, and use it only for "are we in a valid
 * repo?".
 */
extern void check_repository_format(void);
941

942 943 944 945 946 947
#define MTIME_CHANGED	0x0001
#define CTIME_CHANGED	0x0002
#define OWNER_CHANGED	0x0004
#define MODE_CHANGED    0x0008
#define INODE_CHANGED   0x0010
#define DATA_CHANGED    0x0020
948
#define TYPE_CHANGED    0x0040
949

950 951 952 953 954
/*
 * Return an abbreviated sha1 unique within this repository's object database.
 * The result will be at least `len` characters long, and will be NUL
 * terminated.
 *
955 956
 * The non-`_r` version returns a static buffer which remains valid until 4
 * more calls to find_unique_abbrev are made.
957 958
 *
 * The `_r` variant writes to a buffer supplied by the caller, which must be at
959
 * least `GIT_MAX_HEXSZ + 1` bytes. The return value is the number of bytes
960 961 962 963 964
 * written (excluding the NUL terminator).
 *
 * Note that while this version avoids the static buffer, it is not fully
 * reentrant, as it calls into other non-reentrant git code.
 */
965 966
extern const char *find_unique_abbrev(const struct object_id *oid, int len);
extern int find_unique_abbrev_r(char *hex, const struct object_id *oid, int len);
967

968
extern const unsigned char null_sha1[GIT_MAX_RAWSZ];
969
extern const struct object_id null_oid;
970 971

static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2)
972
{
973
	return memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
974
}
975

976 977 978 979 980
static inline int oidcmp(const struct object_id *oid1, const struct object_id *oid2)
{
	return hashcmp(oid1->hash, oid2->hash);
}

981
static inline int is_null_sha1(const unsigned char *sha1)
982
{
983
	return !hashcmp(sha1, null_sha1);
984
}
985

986 987 988 989 990
static inline int is_null_oid(const struct object_id *oid)
{
	return !hashcmp(oid->hash, null_sha1);
}

991 992
static inline void hashcpy(unsigned char *sha_dst, const unsigned char *sha_src)
{
993
	memcpy(sha_dst, sha_src, GIT_SHA1_RAWSZ);
994
}
995 996 997 998

static inline void oidcpy(struct object_id *dst, const struct object_id *src)
{
	hashcpy(dst->hash, src->hash);
999
}
1000

1001 1002 1003 1004 1005 1006 1007
static inline struct object_id *oiddup(const struct object_id *src)
{
	struct object_id *dst = xmalloc(sizeof(struct object_id));
	oidcpy(dst, src);
	return dst;
}

1008 1009
static inline void hashclr(unsigned char *hash)
{
1010
	memset(hash, 0, GIT_SHA1_RAWSZ);
1011
}
1012

1013 1014
static inline void oidclr(struct object_id *oid)
{
1015
	memset(oid->hash, 0, GIT_MAX_RAWSZ);
1016 1017
}

1018 1019 1020 1021
static inline void oidread(struct object_id *oid, const unsigned char *hash)
{
	memcpy(oid->hash, hash, the_hash_algo->rawsz);
}
1022 1023 1024

static inline int is_empty_blob_sha1(const unsigned char *sha1)
{
1025
	return !hashcmp(sha1, the_hash_algo->empty_blob->hash);
1026 1027
}

1028 1029
static inline int is_empty_blob_oid(const struct object_id *oid)
{
1030
	return !oidcmp(oid, the_hash_algo->empty_blob);
1031 1032 1033 1034
}

static inline int is_empty_tree_sha1(const unsigned char *sha1)
{
1035
	return !hashcmp(sha1, the_hash_algo->empty_tree->hash);
1036 1037 1038 1039
}

static inline int is_empty_tree_oid(const struct object_id *oid)
{
1040
	return !oidcmp(oid, the_hash_algo->empty_tree);
1041 1042
}

1043 1044 1045
const char *empty_tree_oid_hex(void);
const char *empty_blob_oid_hex(void);

1046 1047 1048 1049
/* set default permissions by passing mode arguments to open(2) */
int git_mkstemps_mode(char *pattern, int suffix_len, int mode);
int git_mkstemp_mode(char *pattern, int mode);

1050 1051 1052 1053 1054 1055 1056 1057
/*
 * NOTE NOTE NOTE!!
 *
 * PERM_UMASK, OLD_PERM_GROUP and OLD_PERM_EVERYBODY enumerations must
 * not be changed. Old repositories have core.sharedrepository written in
 * numeric format, and therefore these values are preserved for compatibility
 * reasons.
 */
1058
enum sharedrepo {
1059 1060 1061 1062
	PERM_UMASK          = 0,
	OLD_PERM_GROUP      = 1,
	OLD_PERM_EVERYBODY  = 2,
	PERM_GROUP          = 0660,
1063
	PERM_EVERYBODY      = 0664
1064 1065
};
int git_config_perm(const char *var, const char *value);
1066
int adjust_shared_perm(const char *path);
1067 1068 1069

/*
 * Create the directory containing the named path, using care to be
1070 1071 1072
 * somewhat safe against races. Return one of the scld_error values to
 * indicate success/failure. On error, set errno to describe the
 * problem.
1073 1074 1075 1076 1077 1078 1079
 *
 * SCLD_VANISHED indicates that one of the ancestor directories of the
 * path existed at one point during the function call and then
 * suddenly vanished, probably because another process pruned the
 * directory while we were working.  To be robust against this kind of
 * race, callers might want to try invoking the function again when it
 * returns SCLD_VANISHED.
1080 1081 1082 1083 1084
 *
 * safe_create_leading_directories() temporarily changes path while it
 * is working but restores it before returning.
 * safe_create_leading_directories_const() doesn't modify path, even
 * temporarily.
1085 1086 1087 1088 1089
 */
enum scld_error {
	SCLD_OK = 0,
	SCLD_FAILED = -1,
	SCLD_PERMS = -2,
1090 1091
	SCLD_EXISTS = -3,
	SCLD_VANISHED = -4
1092 1093 1094 1095
};
enum scld_error safe_create_leading_directories(char *path);
enum scld_error safe_create_leading_directories_const(const char *path);

1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
/*
 * Callback function for raceproof_create_file(). This function is
 * expected to do something that makes dirname(path) permanent despite
 * the fact that other processes might be cleaning up empty
 * directories at the same time. Usually it will create a file named
 * path, but alternatively it could create another file in that
 * directory, or even chdir() into that directory. The function should
 * return 0 if the action was completed successfully. On error, it
 * should return a nonzero result and set errno.
 * raceproof_create_file() treats two errno values specially:
 *
 * - ENOENT -- dirname(path) does not exist. In this case,
 *             raceproof_create_file() tries creating dirname(path)
 *             (and any parent directories, if necessary) and calls
 *             the function again.
 *
 * - EISDIR -- the file already exists and is a directory. In this
 *             case, raceproof_create_file() removes the directory if
 *             it is empty (and recursively any empty directories that
 *             it contains) and calls the function again.
 *
 * Any other errno causes raceproof_create_file() to fail with the
 * callback's return value and errno.
 *
 * Obviously, this function should be OK with being called again if it
 * fails with ENOENT or EISDIR. In other scenarios it will not be
 * called again.
 */
typedef int create_file_fn(const char *path, void *cb);

/*
 * Create a file in dirname(path) by calling fn, creating leading
 * directories if necessary. Retry a few times in case we are racing
 * with another process that is trying to clean up the directory that
 * contains path. See the documentation for create_file_fn for more
 * details.
 *
 * Return the value and set the errno that resulted from the most
 * recent call of fn. fn is always called at least once, and will be
 * called more than once if it returns ENOENT or EISDIR.
 */
int raceproof_create_file(const char *path, create_file_fn fn, void *cb);

1139
int mkdir_in_gitdir(const char *path);
1140
extern char *expand_user_path(const char *path, int real_home);
1141
const char *enter_repo(const char *path, int strict);
1142 1143
static inline int is_absolute_path(const char *path)
{
1144
	return is_dir_sep(path[0]) || has_dos_drive_prefix(path);
1145
}
1146
int is_directory(const char *);
1147 1148
char *strbuf_realpath(struct strbuf *resolved, const char *path,
		      int die_on_error);
1149
const char *real_path(const char *path);
1150
const char *real_path_if_valid(const char *path);
1151
char *real_pathdup(const char *path, int die_on_error);
1152
const char *absolute_path(const char *path);
1153
char *absolute_pathdup(const char *path);
1154
const char *remove_leading_path(const char *in, const char *prefix);
1155
const char *relative_path(const char *in, const char *prefix, struct strbuf *sb);
1156
int normalize_path_copy_len(char *dst, const char *src, int *prefix_len);
1157
int normalize_path_copy(char *dst, const char *src);
1158
int longest_ancestor_length(const char *path, struct string_list *prefixes);
1159
char *strip_path_suffix(const char *path, const char *suffix);
1160
int daemon_avoid_alias(const char *path);
1161 1162 1163 1164 1165 1166 1167 1168 1169

/*
 * These functions match their is_hfs_dotgit() counterparts; see utf8.h for
 * details.
 */
int is_ntfs_dotgit(const char *name);
int is_ntfs_dotgitmodules(const char *name);
int is_ntfs_dotgitignore(const char *name);
int is_ntfs_dotgitattributes(const char *name);
1170

1171 1172 1173 1174 1175 1176 1177 1178
/*
 * Returns true iff "str" could be confused as a command-line option when
 * passed to a sub-program like "ssh". Note that this has nothing to do with
 * shell-quoting, which should be handled separately; we're assuming here that
 * the string makes it verbatim to the sub-program.
 */
int looks_like_command_line_option(const char *str);

1179 1180 1181 1182 1183 1184 1185
/**
 * Return a newly allocated string with the evaluation of
 * "$XDG_CONFIG_HOME/git/$filename" if $XDG_CONFIG_HOME is non-empty, otherwise
 * "$HOME/.config/git/$filename". Return NULL upon error.
 */
extern char *xdg_config_home(const char *filename);

1186 1187 1188 1189 1190 1191 1192
/**
 * Return a newly allocated string with the evaluation of
 * "$XDG_CACHE_HOME/git/$filename" if $XDG_CACHE_HOME is non-empty, otherwise
 * "$HOME/.cache/git/$filename". Return NULL upon error.
 */
extern char *xdg_cache_home(const char *filename);

1193
extern int git_open_cloexec(const char *name, int flags);
1194
#define git_open(name) git_open_cloexec(name, O_RDONLY)
1195
extern int unpack_sha1_header(git_zstream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz);