]> git.proxmox.com Git - libgit2.git/blame - src/repository.c
libgit2 0.18.0 "Big Ben"
[libgit2.git] / src / repository.c
CommitLineData
3315782c 1/*
359fc2d2 2 * Copyright (C) the libgit2 contributors. All rights reserved.
3315782c 3 *
bb742ede
VM
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
3315782c 6 */
e802d8cc 7#include <stdarg.h>
7784bcbb 8#include <ctype.h>
3315782c 9
44908fe7 10#include "git2/object.h"
d00d5464 11#include "git2/refdb.h"
d12299fe 12
3315782c
VM
13#include "common.h"
14#include "repository.h"
15#include "commit.h"
16#include "tag.h"
237da401 17#include "blob.h"
6fd195d7 18#include "fileops.h"
b22d1479 19#include "config.h"
9282e921 20#include "refs.h"
47bfa0be
RB
21#include "filter.h"
22#include "odb.h"
096d9e94 23#include "remote.h"
632d8b23 24#include "merge.h"
9282e921 25
7784bcbb 26#define GIT_FILE_CONTENT_PREFIX "gitdir:"
6a01b6bd 27
28990938 28#define GIT_BRANCH_MASTER "master"
29
29e948de 30#define GIT_REPO_VERSION 0
691aa968 31
ca1b6e54
RB
32#define GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
33
9462c471
VM
34static void drop_odb(git_repository *repo)
35{
36 if (repo->_odb != NULL) {
37 GIT_REFCOUNT_OWN(repo->_odb, NULL);
38 git_odb_free(repo->_odb);
39 repo->_odb = NULL;
eb2f3b47 40 }
9462c471 41}
691aa968 42
d00d5464
ET
43static void drop_refdb(git_repository *repo)
44{
45 if (repo->_refdb != NULL) {
46 GIT_REFCOUNT_OWN(repo->_refdb, NULL);
47 git_refdb_free(repo->_refdb);
48 repo->_refdb = NULL;
49 }
50}
51
9462c471
VM
52static void drop_config(git_repository *repo)
53{
54 if (repo->_config != NULL) {
55 GIT_REFCOUNT_OWN(repo->_config, NULL);
56 git_config_free(repo->_config);
57 repo->_config = NULL;
eb2f3b47 58 }
f2c25d18
VM
59
60 git_repository__cvar_cache_clear(repo);
691aa968
VM
61}
62
9462c471 63static void drop_index(git_repository *repo)
6fd195d7 64{
9462c471
VM
65 if (repo->_index != NULL) {
66 GIT_REFCOUNT_OWN(repo->_index, NULL);
67 git_index_free(repo->_index);
68 repo->_index = NULL;
69 }
e0011be3
VM
70}
71
9462c471 72void git_repository_free(git_repository *repo)
e0011be3 73{
9462c471
VM
74 if (repo == NULL)
75 return;
6fd195d7 76
9462c471 77 git_cache_free(&repo->objects);
73b51450 78 git_attr_cache_flush(repo);
bfc9ca59 79 git_submodule_config_free(repo);
6fd195d7 80
9462c471
VM
81 git__free(repo->path_repository);
82 git__free(repo->workdir);
6fd195d7 83
9462c471
VM
84 drop_config(repo);
85 drop_index(repo);
86 drop_odb(repo);
d00d5464 87 drop_refdb(repo);
9462c471
VM
88
89 git__free(repo);
6fd195d7
VM
90}
91
9462c471
VM
92/*
93 * Git repository open methods
94 *
95 * Open a repository object from its path
96 */
cb8a7961 97static bool valid_repository_path(git_buf *repository_path)
5ad739e8 98{
97769280 99 /* Check OBJECTS_DIR first, since it will generate the longest path name */
1a481123 100 if (git_path_contains_dir(repository_path, GIT_OBJECTS_DIR) == false)
cb8a7961 101 return false;
5ad739e8 102
97769280 103 /* Ensure HEAD file exists */
1a481123 104 if (git_path_contains_file(repository_path, GIT_HEAD_FILE) == false)
cb8a7961 105 return false;
5ad739e8 106
1a481123 107 if (git_path_contains_dir(repository_path, GIT_REFS_DIR) == false)
cb8a7961 108 return false;
5ad739e8 109
cb8a7961 110 return true;
5ad739e8
VM
111}
112
51d00446 113static git_repository *repository_alloc(void)
3315782c
VM
114{
115 git_repository *repo = git__malloc(sizeof(git_repository));
116 if (!repo)
117 return NULL;
118
119 memset(repo, 0x0, sizeof(git_repository));
120
cb8a7961 121 if (git_cache_init(&repo->objects, GIT_DEFAULT_CACHE_SIZE, &git_object__free) < 0) {
3286c408 122 git__free(repo);
81201a4c 123 return NULL;
124 }
3315782c 125
f2c25d18
VM
126 /* set all the entries in the cvar cache to `unset` */
127 git_repository__cvar_cache_clear(repo);
128
6fd195d7
VM
129 return repo;
130}
131
9462c471 132static int load_config_data(git_repository *repo)
ec3c7a16 133{
cb8a7961 134 int is_bare;
9462c471 135 git_config *config;
ec3c7a16 136
cb8a7961
VM
137 if (git_repository_config__weakptr(&config, repo) < 0)
138 return -1;
ec3c7a16 139
d3e9c4a5 140 /* Try to figure out if it's bare, default to non-bare if it's not set */
29e948de 141 if (git_config_get_bool(&is_bare, config, "core.bare") < 0)
d3e9c4a5
CMN
142 repo->is_bare = 0;
143 else
144 repo->is_bare = is_bare;
c94785a9 145
cb8a7961 146 return 0;
9462c471 147}
ec3c7a16 148
7784bcbb 149static int load_workdir(git_repository *repo, git_buf *parent_path)
9462c471 150{
7784bcbb
RB
151 int error;
152 git_config *config;
153 const char *worktree;
154 git_buf worktree_buf = GIT_BUF_INIT;
ec3c7a16 155
97769280 156 if (repo->is_bare)
cb8a7961 157 return 0;
ec3c7a16 158
7784bcbb 159 if (git_repository_config__weakptr(&config, repo) < 0)
cb8a7961 160 return -1;
ec3c7a16 161
29e948de 162 error = git_config_get_string(&worktree, config, "core.worktree");
5f4a61ae
RB
163 if (!error && worktree != NULL) {
164 error = git_path_prettify_dir(
165 &worktree_buf, worktree, repo->path_repository);
166 if (error < 0)
167 return error;
168 repo->workdir = git_buf_detach(&worktree_buf);
169 }
904b67e6 170 else if (error != GIT_ENOTFOUND)
7784bcbb
RB
171 return error;
172 else {
173 giterr_clear();
174
175 if (parent_path && git_path_isdir(parent_path->ptr))
176 repo->workdir = git_buf_detach(parent_path);
177 else {
178 git_path_dirname_r(&worktree_buf, repo->path_repository);
179 git_path_to_dir(&worktree_buf);
180 repo->workdir = git_buf_detach(&worktree_buf);
181 }
182 }
183
184 GITERR_CHECK_ALLOC(repo->workdir);
97769280 185
cb8a7961 186 return 0;
ec3c7a16
VM
187}
188
7784bcbb
RB
189/*
190 * This function returns furthest offset into path where a ceiling dir
191 * is found, so we can stop processing the path at that point.
192 *
193 * Note: converting this to use git_bufs instead of GIT_PATH_MAX buffers on
194 * the stack could remove directories name limits, but at the cost of doing
195 * repeated malloc/frees inside the loop below, so let's not do it now.
196 */
197static int find_ceiling_dir_offset(
198 const char *path,
199 const char *ceiling_directories)
200{
201 char buf[GIT_PATH_MAX + 1];
202 char buf2[GIT_PATH_MAX + 1];
203 const char *ceil, *sep;
44ef8b1b 204 size_t len, max_len = 0, min_len;
7784bcbb
RB
205
206 assert(path);
207
44ef8b1b 208 min_len = (size_t)(git_path_root(path) + 1);
7784bcbb
RB
209
210 if (ceiling_directories == NULL || min_len == 0)
44ef8b1b 211 return (int)min_len;
7784bcbb
RB
212
213 for (sep = ceil = ceiling_directories; *sep; ceil = sep + 1) {
214 for (sep = ceil; *sep && *sep != GIT_PATH_LIST_SEPARATOR; sep++);
215 len = sep - ceil;
216
44ef8b1b 217 if (len == 0 || len >= sizeof(buf) || git_path_root(ceil) == -1)
7784bcbb
RB
218 continue;
219
220 strncpy(buf, ceil, len);
221 buf[len] = '\0';
222
223 if (p_realpath(buf, buf2) == NULL)
224 continue;
225
226 len = strlen(buf2);
227 if (len > 0 && buf2[len-1] == '/')
228 buf[--len] = '\0';
229
230 if (!strncmp(path, buf2, len) &&
231 path[len] == '/' &&
232 len > max_len)
233 {
234 max_len = len;
235 }
236 }
237
44ef8b1b 238 return (int)(max_len <= min_len ? min_len : max_len);
7784bcbb
RB
239}
240
241/*
242 * Read the contents of `file_path` and set `path_out` to the repo dir that
243 * it points to. Before calling, set `path_out` to the base directory that
244 * should be used if the contents of `file_path` are a relative path.
245 */
246static int read_gitfile(git_buf *path_out, const char *file_path)
247{
248 int error = 0;
249 git_buf file = GIT_BUF_INIT;
250 size_t prefix_len = strlen(GIT_FILE_CONTENT_PREFIX);
251
252 assert(path_out && file_path);
253
254 if (git_futils_readbuffer(&file, file_path) < 0)
255 return -1;
256
257 git_buf_rtrim(&file);
258
662880ca
RB
259 if (git_buf_len(&file) <= prefix_len ||
260 memcmp(git_buf_cstr(&file), GIT_FILE_CONTENT_PREFIX, prefix_len) != 0)
7784bcbb
RB
261 {
262 giterr_set(GITERR_REPOSITORY, "The `.git` file at '%s' is malformed", file_path);
263 error = -1;
264 }
265 else if ((error = git_path_dirname_r(path_out, file_path)) >= 0) {
662880ca 266 const char *gitlink = git_buf_cstr(&file) + prefix_len;
0f49200c 267 while (*gitlink && git__isspace(*gitlink)) gitlink++;
662880ca
RB
268 error = git_path_prettify_dir(
269 path_out, gitlink, git_buf_cstr(path_out));
7784bcbb
RB
270 }
271
272 git_buf_free(&file);
273 return error;
274}
275
276static int find_repo(
277 git_buf *repo_path,
278 git_buf *parent_path,
279 const char *start_path,
280 uint32_t flags,
281 const char *ceiling_dirs)
691aa968 282{
7784bcbb
RB
283 int error;
284 git_buf path = GIT_BUF_INIT;
285 struct stat st;
286 dev_t initial_device = 0;
287 bool try_with_dot_git = false;
288 int ceiling_offset;
289
290 git_buf_free(repo_path);
291
292 if ((error = git_path_prettify_dir(&path, start_path, NULL)) < 0)
293 return error;
294
295 ceiling_offset = find_ceiling_dir_offset(path.ptr, ceiling_dirs);
296
297 if ((error = git_buf_joinpath(&path, path.ptr, DOT_GIT)) < 0)
298 return error;
299
fa6420f7 300 while (!error && !git_buf_len(repo_path)) {
7784bcbb
RB
301 if (p_stat(path.ptr, &st) == 0) {
302 /* check that we have not crossed device boundaries */
303 if (initial_device == 0)
304 initial_device = st.st_dev;
305 else if (st.st_dev != initial_device &&
306 (flags & GIT_REPOSITORY_OPEN_CROSS_FS) == 0)
307 break;
308
309 if (S_ISDIR(st.st_mode)) {
310 if (valid_repository_path(&path)) {
311 git_path_to_dir(&path);
312 git_buf_set(repo_path, path.ptr, path.size);
313 break;
314 }
315 }
316 else if (S_ISREG(st.st_mode)) {
317 git_buf repo_link = GIT_BUF_INIT;
318
319 if (!(error = read_gitfile(&repo_link, path.ptr))) {
320 if (valid_repository_path(&repo_link))
321 git_buf_swap(repo_path, &repo_link);
146f5c75
CMN
322
323 git_buf_free(&repo_link);
7784bcbb
RB
324 break;
325 }
326 git_buf_free(&repo_link);
327 }
328 }
329
330 /* move up one directory level */
331 if (git_path_dirname_r(&path, path.ptr) < 0) {
332 error = -1;
333 break;
334 }
335
336 if (try_with_dot_git) {
337 /* if we tried original dir with and without .git AND either hit
338 * directory ceiling or NO_SEARCH was requested, then be done.
339 */
340 if (path.ptr[ceiling_offset] == '\0' ||
341 (flags & GIT_REPOSITORY_OPEN_NO_SEARCH) != 0)
342 break;
343 /* otherwise look first for .git item */
344 error = git_buf_joinpath(&path, path.ptr, DOT_GIT);
345 }
346 try_with_dot_git = !try_with_dot_git;
347 }
348
349 if (!error && parent_path != NULL) {
fa6420f7 350 if (!git_buf_len(repo_path))
7784bcbb
RB
351 git_buf_clear(parent_path);
352 else {
353 git_path_dirname_r(parent_path, path.ptr);
354 git_path_to_dir(parent_path);
355 }
356 if (git_buf_oom(parent_path))
357 return -1;
358 }
359
360 git_buf_free(&path);
361
fa6420f7 362 if (!git_buf_len(repo_path) && !error) {
cb8a7961 363 giterr_set(GITERR_REPOSITORY,
7784bcbb 364 "Could not find repository from '%s'", start_path);
904b67e6 365 error = GIT_ENOTFOUND;
97769280 366 }
691aa968 367
7784bcbb
RB
368 return error;
369}
370
371int git_repository_open_ext(
372 git_repository **repo_ptr,
373 const char *start_path,
c9fc4a6f 374 unsigned int flags,
7784bcbb
RB
375 const char *ceiling_dirs)
376{
377 int error;
378 git_buf path = GIT_BUF_INIT, parent = GIT_BUF_INIT;
379 git_repository *repo;
380
662880ca
RB
381 if (repo_ptr)
382 *repo_ptr = NULL;
7784bcbb 383
662880ca
RB
384 error = find_repo(&path, &parent, start_path, flags, ceiling_dirs);
385 if (error < 0 || !repo_ptr)
7784bcbb
RB
386 return error;
387
e52ed7a5 388 repo = repository_alloc();
cb8a7961 389 GITERR_CHECK_ALLOC(repo);
691aa968 390
7784bcbb 391 repo->path_repository = git_buf_detach(&path);
cb8a7961 392 GITERR_CHECK_ALLOC(repo->path_repository);
691aa968 393
7784bcbb
RB
394 if ((error = load_config_data(repo)) < 0 ||
395 (error = load_workdir(repo, &parent)) < 0)
396 {
397 git_repository_free(repo);
398 return error;
399 }
691aa968 400
146f5c75 401 git_buf_free(&parent);
7784bcbb 402 *repo_ptr = repo;
cb8a7961 403 return 0;
7784bcbb 404}
97769280 405
7784bcbb
RB
406int git_repository_open(git_repository **repo_out, const char *path)
407{
408 return git_repository_open_ext(
409 repo_out, path, GIT_REPOSITORY_OPEN_NO_SEARCH, NULL);
410}
411
6782245e
CMN
412int git_repository_wrap_odb(git_repository **repo_out, git_odb *odb)
413{
414 git_repository *repo;
415
416 repo = repository_alloc();
417 GITERR_CHECK_ALLOC(repo);
418
419 git_repository_set_odb(repo, odb);
420 *repo_out = repo;
421
422 return 0;
423}
424
7784bcbb
RB
425int git_repository_discover(
426 char *repository_path,
427 size_t size,
428 const char *start_path,
429 int across_fs,
430 const char *ceiling_dirs)
431{
432 git_buf path = GIT_BUF_INIT;
433 uint32_t flags = across_fs ? GIT_REPOSITORY_OPEN_CROSS_FS : 0;
464cf248 434 int error;
7784bcbb
RB
435
436 assert(start_path && repository_path && size > 0);
437
438 *repository_path = '\0';
439
464cf248 440 if ((error = find_repo(&path, NULL, start_path, flags, ceiling_dirs)) < 0)
904b67e6 441 return error != GIT_ENOTFOUND ? -1 : error;
7784bcbb
RB
442
443 if (size < (size_t)(path.size + 1)) {
444 giterr_set(GITERR_REPOSITORY,
13b554e3 445 "The given buffer is too small to store the discovered path");
7784bcbb
RB
446 git_buf_free(&path);
447 return -1;
448 }
449
450 /* success: we discovered a repository */
451 git_buf_copy_cstr(repository_path, size, &path);
452 git_buf_free(&path);
453 return 0;
691aa968
VM
454}
455
9462c471 456static int load_config(
7784bcbb
RB
457 git_config **out,
458 git_repository *repo,
459 const char *global_config_path,
4258d483 460 const char *xdg_config_path,
7784bcbb 461 const char *system_config_path)
b22d1479 462{
cc6b4162 463 int error;
97769280 464 git_buf config_path = GIT_BUF_INIT;
9462c471 465 git_config *cfg = NULL;
b22d1479 466
9462c471 467 assert(repo && out);
07ff8817 468
cc6b4162
RB
469 if ((error = git_config_new(&cfg)) < 0)
470 return error;
b22d1479 471
cc6b4162
RB
472 error = git_buf_joinpath(
473 &config_path, repo->path_repository, GIT_CONFIG_FILENAME_INREPO);
474 if (error < 0)
cb8a7961 475 goto on_error;
97769280 476
cc6b4162
RB
477 if ((error = git_config_add_file_ondisk(
478 cfg, config_path.ptr, GIT_CONFIG_LEVEL_LOCAL, 0)) < 0 &&
479 error != GIT_ENOTFOUND)
cb8a7961
VM
480 goto on_error;
481
482 git_buf_free(&config_path);
b22d1479 483
cc6b4162
RB
484 if (global_config_path != NULL &&
485 (error = git_config_add_file_ondisk(
486 cfg, global_config_path, GIT_CONFIG_LEVEL_GLOBAL, 0)) < 0 &&
487 error != GIT_ENOTFOUND)
488 goto on_error;
8b4f9b17 489
cc6b4162
RB
490 if (xdg_config_path != NULL &&
491 (error = git_config_add_file_ondisk(
492 cfg, xdg_config_path, GIT_CONFIG_LEVEL_XDG, 0)) < 0 &&
493 error != GIT_ENOTFOUND)
494 goto on_error;
b22d1479 495
cc6b4162
RB
496 if (system_config_path != NULL &&
497 (error = git_config_add_file_ondisk(
498 cfg, system_config_path, GIT_CONFIG_LEVEL_SYSTEM, 0)) < 0 &&
499 error != GIT_ENOTFOUND)
500 goto on_error;
9ba9e513 501
38f7d026
RB
502 giterr_clear(); /* clear any lingering ENOTFOUND errors */
503
9462c471 504 *out = cfg;
cb8a7961 505 return 0;
b22d1479 506
cb8a7961
VM
507on_error:
508 git_buf_free(&config_path);
9462c471
VM
509 git_config_free(cfg);
510 *out = NULL;
cc6b4162 511 return error;
b22d1479
CMN
512}
513
9462c471 514int git_repository_config__weakptr(git_config **out, git_repository *repo)
40fe5fbe 515{
9462c471 516 if (repo->_config == NULL) {
4258d483 517 git_buf global_buf = GIT_BUF_INIT, xdg_buf = GIT_BUF_INIT, system_buf = GIT_BUF_INIT;
cb8a7961 518 int res;
9462c471
VM
519
520 const char *global_config_path = NULL;
4258d483 521 const char *xdg_config_path = NULL;
9462c471 522 const char *system_config_path = NULL;
40fe5fbe 523
cb8a7961 524 if (git_config_find_global_r(&global_buf) == 0)
97769280 525 global_config_path = global_buf.ptr;
40fe5fbe 526
4258d483
SS
527 if (git_config_find_xdg_r(&xdg_buf) == 0)
528 xdg_config_path = xdg_buf.ptr;
8b4f9b17 529
cb8a7961 530 if (git_config_find_system_r(&system_buf) == 0)
97769280 531 system_config_path = system_buf.ptr;
40fe5fbe 532
4258d483 533 res = load_config(&repo->_config, repo, global_config_path, xdg_config_path, system_config_path);
97769280
RB
534
535 git_buf_free(&global_buf);
a8918418 536 git_buf_free(&xdg_buf);
97769280
RB
537 git_buf_free(&system_buf);
538
cb8a7961
VM
539 if (res < 0)
540 return -1;
9462c471
VM
541
542 GIT_REFCOUNT_OWN(repo->_config, repo);
543 }
40fe5fbe 544
9462c471 545 *out = repo->_config;
cb8a7961 546 return 0;
40fe5fbe
CMN
547}
548
9462c471 549int git_repository_config(git_config **out, git_repository *repo)
fd0574e5 550{
cb8a7961
VM
551 if (git_repository_config__weakptr(out, repo) < 0)
552 return -1;
fd0574e5 553
cb8a7961
VM
554 GIT_REFCOUNT_INC(*out);
555 return 0;
9462c471
VM
556}
557
558void git_repository_set_config(git_repository *repo, git_config *config)
559{
560 assert(repo && config);
561
562 drop_config(repo);
563
564 repo->_config = config;
565 GIT_REFCOUNT_OWN(repo->_config, repo);
25e7c9b7 566 GIT_REFCOUNT_INC(repo->_config);
9462c471
VM
567}
568
569int git_repository_odb__weakptr(git_odb **out, git_repository *repo)
570{
571 assert(repo && out);
572
573 if (repo->_odb == NULL) {
97769280 574 git_buf odb_path = GIT_BUF_INIT;
cb8a7961 575 int res;
9462c471 576
cb8a7961
VM
577 if (git_buf_joinpath(&odb_path, repo->path_repository, GIT_OBJECTS_DIR) < 0)
578 return -1;
9462c471 579
cb8a7961 580 res = git_odb_open(&repo->_odb, odb_path.ptr);
97769280 581 git_buf_free(&odb_path); /* done with path */
cb8a7961
VM
582
583 if (res < 0)
584 return -1;
9462c471
VM
585
586 GIT_REFCOUNT_OWN(repo->_odb, repo);
587 }
fd0574e5 588
9462c471 589 *out = repo->_odb;
cb8a7961 590 return 0;
fd0574e5
RG
591}
592
9462c471 593int git_repository_odb(git_odb **out, git_repository *repo)
6fd195d7 594{
cb8a7961
VM
595 if (git_repository_odb__weakptr(out, repo) < 0)
596 return -1;
1795f879 597
cb8a7961
VM
598 GIT_REFCOUNT_INC(*out);
599 return 0;
9462c471 600}
6fd195d7 601
9462c471
VM
602void git_repository_set_odb(git_repository *repo, git_odb *odb)
603{
604 assert(repo && odb);
3315782c 605
9462c471 606 drop_odb(repo);
1795f879 607
9462c471
VM
608 repo->_odb = odb;
609 GIT_REFCOUNT_OWN(repo->_odb, repo);
baf861a5 610 GIT_REFCOUNT_INC(odb);
9462c471
VM
611}
612
d00d5464
ET
613int git_repository_refdb__weakptr(git_refdb **out, git_repository *repo)
614{
615 assert(out && repo);
616
617 if (repo->_refdb == NULL) {
618 int res;
619
620 res = git_refdb_open(&repo->_refdb, repo);
621
622 if (res < 0)
623 return -1;
624
625 GIT_REFCOUNT_OWN(repo->_refdb, repo);
626 }
627
628 *out = repo->_refdb;
629 return 0;
630}
631
632int git_repository_refdb(git_refdb **out, git_repository *repo)
633{
634 if (git_repository_refdb__weakptr(out, repo) < 0)
635 return -1;
636
637 GIT_REFCOUNT_INC(*out);
638 return 0;
639}
640
641void git_repository_set_refdb(git_repository *repo, git_refdb *refdb)
642{
643 assert (repo && refdb);
644
645 drop_refdb(repo);
646
647 repo->_refdb = refdb;
648 GIT_REFCOUNT_OWN(repo->_refdb, repo);
649 GIT_REFCOUNT_INC(refdb);
650}
651
9462c471
VM
652int git_repository_index__weakptr(git_index **out, git_repository *repo)
653{
654 assert(out && repo);
655
9462c471 656 if (repo->_index == NULL) {
cb8a7961 657 int res;
97769280 658 git_buf index_path = GIT_BUF_INIT;
9462c471 659
cb8a7961
VM
660 if (git_buf_joinpath(&index_path, repo->path_repository, GIT_INDEX_FILE) < 0)
661 return -1;
9462c471 662
cb8a7961 663 res = git_index_open(&repo->_index, index_path.ptr);
97769280 664 git_buf_free(&index_path); /* done with path */
cb8a7961
VM
665
666 if (res < 0)
667 return -1;
9462c471
VM
668
669 GIT_REFCOUNT_OWN(repo->_index, repo);
da825c92
RB
670
671 if (git_index_set_caps(repo->_index, GIT_INDEXCAP_FROM_OWNER) < 0)
672 return -1;
9462c471
VM
673 }
674
9462c471 675 *out = repo->_index;
cb8a7961 676 return 0;
9462c471 677}
1795f879 678
9462c471
VM
679int git_repository_index(git_index **out, git_repository *repo)
680{
cb8a7961
VM
681 if (git_repository_index__weakptr(out, repo) < 0)
682 return -1;
9462c471 683
cb8a7961
VM
684 GIT_REFCOUNT_INC(*out);
685 return 0;
3315782c
VM
686}
687
9462c471
VM
688void git_repository_set_index(git_repository *repo, git_index *index)
689{
690 assert(repo && index);
691
692 drop_index(repo);
693
694 repo->_index = index;
695 GIT_REFCOUNT_OWN(repo->_index, repo);
c1aefb35 696 GIT_REFCOUNT_INC(index);
9462c471
VM
697}
698
2c227b8b 699static int check_repositoryformatversion(git_config *config)
40c44d2f 700{
cb8a7961 701 int version;
5663e61a 702
29e948de 703 if (git_config_get_int32(&version, config, "core.repositoryformatversion") < 0)
cb8a7961 704 return -1;
5663e61a 705
29e948de 706 if (GIT_REPO_VERSION < version) {
cb8a7961
VM
707 giterr_set(GITERR_REPOSITORY,
708 "Unsupported repository version %d. Only versions up to %d are supported.",
29e948de 709 version, GIT_REPO_VERSION);
cb8a7961
VM
710 return -1;
711 }
5663e61a 712
cb8a7961 713 return 0;
5663e61a 714}
715
662880ca 716static int repo_init_create_head(const char *git_dir, const char *ref_name)
e1f8cad0 717{
97769280 718 git_buf ref_path = GIT_BUF_INIT;
9462c471 719 git_filebuf ref = GIT_FILEBUF_INIT;
662880ca 720 const char *fmt;
9462c471 721
cb8a7961 722 if (git_buf_joinpath(&ref_path, git_dir, GIT_HEAD_FILE) < 0 ||
662880ca
RB
723 git_filebuf_open(&ref, ref_path.ptr, 0) < 0)
724 goto fail;
725
726 if (!ref_name)
727 ref_name = GIT_BRANCH_MASTER;
728
74a24005 729 if (git__prefixcmp(ref_name, GIT_REFS_DIR) == 0)
662880ca
RB
730 fmt = "ref: %s\n";
731 else
74a24005 732 fmt = "ref: " GIT_REFS_HEADS_DIR "%s\n";
662880ca
RB
733
734 if (git_filebuf_printf(&ref, fmt, ref_name) < 0 ||
cb8a7961 735 git_filebuf_commit(&ref, GIT_REFS_FILE_MODE) < 0)
662880ca 736 goto fail;
9462c471 737
97769280 738 git_buf_free(&ref_path);
cb8a7961 739 return 0;
662880ca
RB
740
741fail:
742 git_buf_free(&ref_path);
743 git_filebuf_cleanup(&ref);
744 return -1;
9462c471
VM
745}
746
fac66990 747static bool is_chmod_supported(const char *file_path)
748{
749 struct stat st1, st2;
750 static int _is_supported = -1;
751
752 if (_is_supported > -1)
753 return _is_supported;
754
755 if (p_stat(file_path, &st1) < 0)
756 return false;
757
758 if (p_chmod(file_path, st1.st_mode ^ S_IXUSR) < 0)
759 return false;
760
761 if (p_stat(file_path, &st2) < 0)
762 return false;
763
764 _is_supported = (st1.st_mode != st2.st_mode);
ca1b6e54 765
fac66990 766 return _is_supported;
767}
768
693b23c0 769static bool is_filesystem_case_insensitive(const char *gitdir_path)
770{
771 git_buf path = GIT_BUF_INIT;
772 static int _is_insensitive = -1;
773
774 if (_is_insensitive > -1)
775 return _is_insensitive;
776
777 if (git_buf_joinpath(&path, gitdir_path, "CoNfIg") < 0)
778 goto cleanup;
779
780 _is_insensitive = git_path_exists(git_buf_cstr(&path));
781
782cleanup:
783 git_buf_free(&path);
784 return _is_insensitive;
785}
786
ca1b6e54
RB
787static bool are_symlinks_supported(const char *wd_path)
788{
789 git_buf path = GIT_BUF_INIT;
790 int fd;
791 struct stat st;
792 static int _symlinks_supported = -1;
793
794 if (_symlinks_supported > -1)
795 return _symlinks_supported;
796
797 if ((fd = git_futils_mktmp(&path, wd_path)) < 0 ||
798 p_close(fd) < 0 ||
799 p_unlink(path.ptr) < 0 ||
800 p_symlink("testing", path.ptr) < 0 ||
801 p_lstat(path.ptr, &st) < 0)
802 _symlinks_supported = false;
803 else
804 _symlinks_supported = (S_ISLNK(st.st_mode) != 0);
805
806 (void)p_unlink(path.ptr);
807 git_buf_free(&path);
808
809 return _symlinks_supported;
810}
811
270160b9 812static int create_empty_file(const char *path, mode_t mode)
813{
814 int fd;
815
816 if ((fd = p_creat(path, mode)) < 0) {
817 giterr_set(GITERR_OS, "Error while creating '%s'", path);
818 return -1;
819 }
820
821 if (p_close(fd) < 0) {
822 giterr_set(GITERR_OS, "Error while closing '%s'", path);
823 return -1;
824 }
825
826 return 0;
827}
828
662880ca 829static int repo_init_config(
ca1b6e54
RB
830 const char *repo_dir,
831 const char *work_dir,
832 git_repository_init_options *opts)
9462c471 833{
ca1b6e54 834 int error = 0;
97769280
RB
835 git_buf cfg_path = GIT_BUF_INIT;
836 git_config *config = NULL;
9462c471 837
ca1b6e54
RB
838#define SET_REPO_CONFIG(TYPE, NAME, VAL) do {\
839 if ((error = git_config_set_##TYPE(config, NAME, VAL)) < 0) \
840 goto cleanup; } while (0)
9462c471 841
ca1b6e54 842 if (git_buf_joinpath(&cfg_path, repo_dir, GIT_CONFIG_FILENAME_INREPO) < 0)
cb8a7961 843 return -1;
9462c471 844
270160b9 845 if (!git_path_isfile(git_buf_cstr(&cfg_path)) &&
846 create_empty_file(git_buf_cstr(&cfg_path), GIT_CONFIG_FILE_MODE) < 0) {
847 git_buf_free(&cfg_path);
848 return -1;
849 }
850
fac66990 851 if (git_config_open_ondisk(&config, git_buf_cstr(&cfg_path)) < 0) {
cb8a7961
VM
852 git_buf_free(&cfg_path);
853 return -1;
854 }
9462c471 855
662880ca 856 if ((opts->flags & GIT_REPOSITORY_INIT__IS_REINIT) != 0 &&
ca1b6e54
RB
857 (error = check_repositoryformatversion(config)) < 0)
858 goto cleanup;
2c227b8b 859
662880ca
RB
860 SET_REPO_CONFIG(
861 bool, "core.bare", (opts->flags & GIT_REPOSITORY_INIT_BARE) != 0);
862 SET_REPO_CONFIG(
863 int32, "core.repositoryformatversion", GIT_REPO_VERSION);
864 SET_REPO_CONFIG(
865 bool, "core.filemode", is_chmod_supported(git_buf_cstr(&cfg_path)));
866
ca1b6e54 867 if (!(opts->flags & GIT_REPOSITORY_INIT_BARE)) {
7623b1b6 868 SET_REPO_CONFIG(bool, "core.logallrefupdates", true);
869
ca1b6e54
RB
870 if (!are_symlinks_supported(work_dir))
871 SET_REPO_CONFIG(bool, "core.symlinks", false);
872
873 if (!(opts->flags & GIT_REPOSITORY_INIT__NATURAL_WD)) {
874 SET_REPO_CONFIG(string, "core.worktree", work_dir);
875 }
876 else if ((opts->flags & GIT_REPOSITORY_INIT__IS_REINIT) != 0) {
54b2a37a 877 if (git_config_delete_entry(config, "core.worktree") < 0)
89cd5708 878 giterr_clear();
ca1b6e54
RB
879 }
880 } else {
881 if (!are_symlinks_supported(repo_dir))
882 SET_REPO_CONFIG(bool, "core.symlinks", false);
883 }
884
662880ca 885 if (!(opts->flags & GIT_REPOSITORY_INIT__IS_REINIT) &&
ca1b6e54 886 is_filesystem_case_insensitive(repo_dir))
693b23c0 887 SET_REPO_CONFIG(bool, "core.ignorecase", true);
662880ca 888
ca1b6e54 889 if (opts->mode == GIT_REPOSITORY_INIT_SHARED_GROUP) {
662880ca
RB
890 SET_REPO_CONFIG(int32, "core.sharedrepository", 1);
891 SET_REPO_CONFIG(bool, "receive.denyNonFastforwards", true);
ca1b6e54
RB
892 }
893 else if (opts->mode == GIT_REPOSITORY_INIT_SHARED_ALL) {
662880ca
RB
894 SET_REPO_CONFIG(int32, "core.sharedrepository", 2);
895 SET_REPO_CONFIG(bool, "receive.denyNonFastforwards", true);
896 }
9462c471 897
ca1b6e54 898cleanup:
97769280 899 git_buf_free(&cfg_path);
9462c471 900 git_config_free(config);
662880ca 901
ca1b6e54 902 return error;
d2d6912e 903}
e1f8cad0 904
dc34da6e 905static int repo_write_template(
991a56c7
RB
906 const char *git_dir,
907 bool allow_overwrite,
908 const char *file,
909 mode_t mode,
662880ca 910 bool hidden,
991a56c7 911 const char *content)
dc34da6e
RB
912{
913 git_buf path = GIT_BUF_INIT;
991a56c7 914 int fd, error = 0, flags;
dc34da6e
RB
915
916 if (git_buf_joinpath(&path, git_dir, file) < 0)
917 return -1;
918
991a56c7
RB
919 if (allow_overwrite)
920 flags = O_WRONLY | O_CREAT | O_TRUNC;
921 else
922 flags = O_WRONLY | O_CREAT | O_EXCL;
923
924 fd = p_open(git_buf_cstr(&path), flags, mode);
dc34da6e 925
db628072
RB
926 if (fd >= 0) {
927 error = p_write(fd, content, strlen(content));
dc34da6e 928
db628072
RB
929 p_close(fd);
930 }
931 else if (errno != EEXIST)
932 error = fd;
dc34da6e 933
662880ca
RB
934#ifdef GIT_WIN32
935 if (!error && hidden) {
936 if (p_hide_directory__w32(path.ptr) < 0)
937 error = -1;
938 }
939#else
940 GIT_UNUSED(hidden);
941#endif
942
dc34da6e 943 git_buf_free(&path);
db628072
RB
944
945 if (error)
946 giterr_set(GITERR_OS,
947 "Failed to initialize repository with template '%s'", file);
948
949 return error;
dc34da6e
RB
950}
951
662880ca
RB
952static int repo_write_gitlink(
953 const char *in_dir, const char *to_repo)
4b8e27c8 954{
662880ca
RB
955 int error;
956 git_buf buf = GIT_BUF_INIT;
957 struct stat st;
958
959 git_path_dirname_r(&buf, to_repo);
960 git_path_to_dir(&buf);
961 if (git_buf_oom(&buf))
cb8a7961 962 return -1;
a67a096a 963
662880ca
RB
964 /* don't write gitlink to natural workdir */
965 if (git__suffixcmp(to_repo, "/" DOT_GIT "/") == 0 &&
966 strcmp(in_dir, buf.ptr) == 0)
967 {
968 error = GIT_PASSTHROUGH;
969 goto cleanup;
970 }
971
972 if ((error = git_buf_joinpath(&buf, in_dir, DOT_GIT)) < 0)
973 goto cleanup;
974
975 if (!p_stat(buf.ptr, &st) && !S_ISREG(st.st_mode)) {
976 giterr_set(GITERR_REPOSITORY,
977 "Cannot overwrite gitlink file into path '%s'", in_dir);
978 error = GIT_EEXISTS;
979 goto cleanup;
980 }
981
982 git_buf_clear(&buf);
983
984 error = git_buf_printf(&buf, "%s %s", GIT_FILE_CONTENT_PREFIX, to_repo);
985
986 if (!error)
18f08264 987 error = repo_write_template(in_dir, true, DOT_GIT, 0666, true, buf.ptr);
662880ca
RB
988
989cleanup:
990 git_buf_free(&buf);
991 return error;
992}
993
ca1b6e54
RB
994static mode_t pick_dir_mode(git_repository_init_options *opts)
995{
996 if (opts->mode == GIT_REPOSITORY_INIT_SHARED_UMASK)
18f08264 997 return 0777;
ca1b6e54
RB
998 if (opts->mode == GIT_REPOSITORY_INIT_SHARED_GROUP)
999 return (0775 | S_ISGID);
1000 if (opts->mode == GIT_REPOSITORY_INIT_SHARED_ALL)
1001 return (0777 | S_ISGID);
1002 return opts->mode;
1003}
1004
662880ca
RB
1005#include "repo_template.h"
1006
1007static int repo_init_structure(
1008 const char *repo_dir,
1009 const char *work_dir,
1010 git_repository_init_options *opts)
1011{
ca1b6e54 1012 int error = 0;
662880ca 1013 repo_template_item *tpl;
ca1b6e54
RB
1014 bool external_tpl =
1015 ((opts->flags & GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE) != 0);
1016 mode_t dmode = pick_dir_mode(opts);
662880ca
RB
1017
1018 /* Hide the ".git" directory */
17837602 1019#ifdef GIT_WIN32
2eb4edf5 1020 if ((opts->flags & GIT_REPOSITORY_INIT__HAS_DOTGIT) != 0) {
662880ca 1021 if (p_hide_directory__w32(repo_dir) < 0) {
cb8a7961
VM
1022 giterr_set(GITERR_REPOSITORY,
1023 "Failed to mark Git repository folder as hidden");
1024 return -1;
1025 }
17837602 1026 }
2eb4edf5
RB
1027#endif
1028
1029 /* Create the .git gitlink if appropriate */
1030 if ((opts->flags & GIT_REPOSITORY_INIT_BARE) == 0 &&
1031 (opts->flags & GIT_REPOSITORY_INIT__NATURAL_WD) == 0)
1032 {
662880ca 1033 if (repo_write_gitlink(work_dir, repo_dir) < 0)
cb8a7961 1034 return -1;
97769280 1035 }
1c2c7c0d 1036
ca1b6e54
RB
1037 /* Copy external template if requested */
1038 if (external_tpl) {
1039 git_config *cfg;
1040 const char *tdir;
662880ca 1041
ca1b6e54
RB
1042 if (opts->template_path)
1043 tdir = opts->template_path;
85bd1746 1044 else if ((error = git_config_open_default(&cfg)) < 0)
ca1b6e54
RB
1045 return error;
1046 else {
1047 error = git_config_get_string(&tdir, cfg, "init.templatedir");
662880ca 1048
ca1b6e54 1049 git_config_free(cfg);
662880ca 1050
ca1b6e54
RB
1051 if (error && error != GIT_ENOTFOUND)
1052 return error;
1053
1054 giterr_clear();
1055 tdir = GIT_TEMPLATE_DIR;
662880ca 1056 }
ca1b6e54
RB
1057
1058 error = git_futils_cp_r(tdir, repo_dir,
18f08264
RB
1059 GIT_CPDIR_COPY_SYMLINKS | GIT_CPDIR_CHMOD_DIRS |
1060 GIT_CPDIR_SIMPLE_TO_MODE, dmode);
ca1b6e54
RB
1061
1062 if (error < 0) {
1063 if (strcmp(tdir, GIT_TEMPLATE_DIR) != 0)
1064 return error;
1065
1066 /* if template was default, ignore error and use internal */
1067 giterr_clear();
1068 external_tpl = false;
b7b1acfd 1069 error = 0;
ca1b6e54
RB
1070 }
1071 }
1072
1073 /* Copy internal template
1074 * - always ensure existence of dirs
1075 * - only create files if no external template was specified
1076 */
1077 for (tpl = repo_template; !error && tpl->path; ++tpl) {
1078 if (!tpl->content)
1079 error = git_futils_mkdir(
1080 tpl->path, repo_dir, dmode, GIT_MKDIR_PATH | GIT_MKDIR_CHMOD);
1081 else if (!external_tpl) {
662880ca
RB
1082 const char *content = tpl->content;
1083
1084 if (opts->description && strcmp(tpl->path, GIT_DESC_FILE) == 0)
1085 content = opts->description;
1086
ca1b6e54
RB
1087 error = repo_write_template(
1088 repo_dir, false, tpl->path, tpl->mode, false, content);
662880ca 1089 }
dc34da6e
RB
1090 }
1091
ca1b6e54 1092 return error;
4b8e27c8 1093}
1094
3c42e4ef
RB
1095static int mkdir_parent(git_buf *buf, uint32_t mode, bool skip2)
1096{
0d1b094b
RB
1097 /* When making parent directories during repository initialization
1098 * don't try to set gid or grant world write access
1099 */
3c42e4ef 1100 return git_futils_mkdir(
0d1b094b 1101 buf->ptr, NULL, mode & ~(S_ISGID | 0002),
3c42e4ef
RB
1102 GIT_MKDIR_PATH | GIT_MKDIR_VERIFY_DIR |
1103 (skip2 ? GIT_MKDIR_SKIP_LAST2 : GIT_MKDIR_SKIP_LAST));
1104}
1105
662880ca
RB
1106static int repo_init_directories(
1107 git_buf *repo_path,
1108 git_buf *wd_path,
1109 const char *given_repo,
1110 git_repository_init_options *opts)
4b8e27c8 1111{
662880ca 1112 int error = 0;
3c42e4ef 1113 bool is_bare, add_dotgit, has_dotgit, natural_wd;
ca1b6e54 1114 mode_t dirmode;
932d1baf 1115
3c42e4ef
RB
1116 /* There are three possible rules for what we are allowed to create:
1117 * - MKPATH means anything we need
1118 * - MKDIR means just the .git directory and its parent and the workdir
1119 * - Neither means only the .git directory can be created
1120 *
1121 * There are 5 "segments" of path that we might need to deal with:
1122 * 1. The .git directory
1123 * 2. The parent of the .git directory
1124 * 3. Everything above the parent of the .git directory
1125 * 4. The working directory (often the same as #2)
1126 * 5. Everything above the working directory (often the same as #3)
1127 *
1128 * For all directories created, we start with the init_mode value for
1129 * permissions and then strip off bits in some cases:
1130 *
1131 * For MKPATH, we create #3 (and #5) paths without S_ISGID or S_IWOTH
1132 * For MKPATH and MKDIR, we create #2 (and #4) without S_ISGID
1133 * For all rules, we create #1 using the untouched init_mode
1134 */
1135
662880ca 1136 /* set up repo path */
4b8e27c8 1137
3c42e4ef
RB
1138 is_bare = ((opts->flags & GIT_REPOSITORY_INIT_BARE) != 0);
1139
662880ca
RB
1140 add_dotgit =
1141 (opts->flags & GIT_REPOSITORY_INIT_NO_DOTGIT_DIR) == 0 &&
3c42e4ef 1142 !is_bare &&
662880ca
RB
1143 git__suffixcmp(given_repo, "/" DOT_GIT) != 0 &&
1144 git__suffixcmp(given_repo, "/" GIT_DIR) != 0;
4b8e27c8 1145
662880ca
RB
1146 if (git_buf_joinpath(repo_path, given_repo, add_dotgit ? GIT_DIR : "") < 0)
1147 return -1;
693b23c0 1148
662880ca
RB
1149 has_dotgit = (git__suffixcmp(repo_path->ptr, "/" GIT_DIR) == 0);
1150 if (has_dotgit)
1151 opts->flags |= GIT_REPOSITORY_INIT__HAS_DOTGIT;
1152
1153 /* set up workdir path */
1154
3c42e4ef 1155 if (!is_bare) {
662880ca 1156 if (opts->workdir_path) {
ca1b6e54
RB
1157 if (git_path_join_unrooted(
1158 wd_path, opts->workdir_path, repo_path->ptr, NULL) < 0)
1159 return -1;
662880ca
RB
1160 } else if (has_dotgit) {
1161 if (git_path_dirname_r(wd_path, repo_path->ptr) < 0)
1162 return -1;
1163 } else {
1164 giterr_set(GITERR_REPOSITORY, "Cannot pick working directory"
1165 " for non-bare repository that isn't a '.git' directory");
1166 return -1;
1167 }
693b23c0 1168
662880ca
RB
1169 if (git_path_to_dir(wd_path) < 0)
1170 return -1;
1171 } else {
1172 git_buf_clear(wd_path);
1173 }
1174
1175 natural_wd =
1176 has_dotgit &&
1177 wd_path->size > 0 &&
1178 wd_path->size + strlen(GIT_DIR) == repo_path->size &&
1179 memcmp(repo_path->ptr, wd_path->ptr, wd_path->size) == 0;
1180 if (natural_wd)
1181 opts->flags |= GIT_REPOSITORY_INIT__NATURAL_WD;
1182
662880ca
RB
1183 /* create directories as needed / requested */
1184
ca1b6e54 1185 dirmode = pick_dir_mode(opts);
662880ca 1186
3c42e4ef
RB
1187 if ((opts->flags & GIT_REPOSITORY_INIT_MKPATH) != 0) {
1188 /* create path #5 */
1189 if (wd_path->size > 0 &&
1190 (error = mkdir_parent(wd_path, dirmode, false)) < 0)
1191 return error;
1192
1193 /* create path #3 (if not the same as #5) */
1194 if (!natural_wd &&
1195 (error = mkdir_parent(repo_path, dirmode, has_dotgit)) < 0)
1196 return error;
1197 }
1198
1199 if ((opts->flags & GIT_REPOSITORY_INIT_MKDIR) != 0 ||
1200 (opts->flags & GIT_REPOSITORY_INIT_MKPATH) != 0)
1201 {
1202 /* create path #4 */
1203 if (wd_path->size > 0 &&
1204 (error = git_futils_mkdir(
1205 wd_path->ptr, NULL, dirmode & ~S_ISGID,
1206 GIT_MKDIR_VERIFY_DIR)) < 0)
1207 return error;
1208
1209 /* create path #2 (if not the same as #4) */
1210 if (!natural_wd &&
1211 (error = git_futils_mkdir(
1212 repo_path->ptr, NULL, dirmode & ~S_ISGID,
1213 GIT_MKDIR_VERIFY_DIR | GIT_MKDIR_SKIP_LAST)) < 0)
1214 return error;
662880ca 1215 }
662880ca 1216
ca1b6e54
RB
1217 if ((opts->flags & GIT_REPOSITORY_INIT_MKDIR) != 0 ||
1218 (opts->flags & GIT_REPOSITORY_INIT_MKPATH) != 0 ||
1219 has_dotgit)
1220 {
3c42e4ef 1221 /* create path #1 */
18f08264
RB
1222 error = git_futils_mkdir(repo_path->ptr, NULL, dirmode,
1223 GIT_MKDIR_VERIFY_DIR | ((dirmode & S_ISGID) ? GIT_MKDIR_CHMOD : 0));
662880ca 1224 }
ca1b6e54 1225
662880ca
RB
1226 /* prettify both directories now that they are created */
1227
1228 if (!error) {
1229 error = git_path_prettify_dir(repo_path, repo_path->ptr, NULL);
1230
1231 if (!error && wd_path->size > 0)
1232 error = git_path_prettify_dir(wd_path, wd_path->ptr, NULL);
1233 }
1234
1235 return error;
1236}
1237
1238static int repo_init_create_origin(git_repository *repo, const char *url)
1239{
1240 int error;
1241 git_remote *remote;
1242
29f27599 1243 if (!(error = git_remote_create(&remote, repo, GIT_REMOTE_ORIGIN, url))) {
662880ca
RB
1244 git_remote_free(remote);
1245 }
1246
1247 return error;
1248}
1249
1250int git_repository_init(
1251 git_repository **repo_out, const char *path, unsigned is_bare)
1252{
b4d13652 1253 git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
662880ca 1254
662880ca
RB
1255 opts.flags = GIT_REPOSITORY_INIT_MKPATH; /* don't love this default */
1256 if (is_bare)
1257 opts.flags |= GIT_REPOSITORY_INIT_BARE;
1258
1259 return git_repository_init_ext(repo_out, path, &opts);
1260}
1261
1262int git_repository_init_ext(
c9fc4a6f 1263 git_repository **out,
662880ca
RB
1264 const char *given_repo,
1265 git_repository_init_options *opts)
1266{
1267 int error;
1268 git_buf repo_path = GIT_BUF_INIT, wd_path = GIT_BUF_INIT;
1269
c9fc4a6f 1270 assert(out && given_repo && opts);
662880ca 1271
c7231c45 1272 GITERR_CHECK_VERSION(opts, GIT_REPOSITORY_INIT_OPTIONS_VERSION, "git_repository_init_options");
b4d13652 1273
662880ca
RB
1274 error = repo_init_directories(&repo_path, &wd_path, given_repo, opts);
1275 if (error < 0)
693b23c0 1276 goto cleanup;
662880ca
RB
1277
1278 if (valid_repository_path(&repo_path)) {
1279
1280 if ((opts->flags & GIT_REPOSITORY_INIT_NO_REINIT) != 0) {
1281 giterr_set(GITERR_REPOSITORY,
1282 "Attempt to reinitialize '%s'", given_repo);
1283 error = GIT_EEXISTS;
1284 goto cleanup;
1285 }
1286
1287 opts->flags |= GIT_REPOSITORY_INIT__IS_REINIT;
1288
ca1b6e54
RB
1289 error = repo_init_config(
1290 git_buf_cstr(&repo_path), git_buf_cstr(&wd_path), opts);
662880ca
RB
1291
1292 /* TODO: reinitialize the templates */
1293 }
1294 else {
1295 if (!(error = repo_init_structure(
1296 git_buf_cstr(&repo_path), git_buf_cstr(&wd_path), opts)) &&
ca1b6e54
RB
1297 !(error = repo_init_config(
1298 git_buf_cstr(&repo_path), git_buf_cstr(&wd_path), opts)))
662880ca
RB
1299 error = repo_init_create_head(
1300 git_buf_cstr(&repo_path), opts->initial_head);
cb8a7961 1301 }
662880ca
RB
1302 if (error < 0)
1303 goto cleanup;
1304
c9fc4a6f 1305 error = git_repository_open(out, git_buf_cstr(&repo_path));
d2d6912e 1306
662880ca 1307 if (!error && opts->origin_url)
c9fc4a6f 1308 error = repo_init_create_origin(*out, opts->origin_url);
693b23c0 1309
1310cleanup:
662880ca
RB
1311 git_buf_free(&repo_path);
1312 git_buf_free(&wd_path);
1313
1314 return error;
40c44d2f 1315}
35502d2e 1316
c682886e 1317int git_repository_head_detached(git_repository *repo)
35502d2e
CMN
1318{
1319 git_reference *ref;
9462c471 1320 git_odb *odb = NULL;
cb8a7961 1321 int exists;
9462c471 1322
cb8a7961
VM
1323 if (git_repository_odb__weakptr(&odb, repo) < 0)
1324 return -1;
35502d2e 1325
cb8a7961
VM
1326 if (git_reference_lookup(&ref, repo, GIT_HEAD_FILE) < 0)
1327 return -1;
35502d2e 1328
75abd2b9
MS
1329 if (git_reference_type(ref) == GIT_REF_SYMBOLIC) {
1330 git_reference_free(ref);
35502d2e 1331 return 0;
75abd2b9 1332 }
35502d2e 1333
2508cc66 1334 exists = git_odb_exists(odb, git_reference_target(ref));
75abd2b9
MS
1335
1336 git_reference_free(ref);
cb8a7961 1337 return exists;
35502d2e
CMN
1338}
1339
3601c4bf 1340int git_repository_head(git_reference **head_out, git_repository *repo)
35502d2e 1341{
b1a3a70e 1342 git_reference *head;
8b05bea8 1343 int error;
1344
b1a3a70e 1345 if ((error = git_reference_lookup(&head, repo, GIT_HEAD_FILE)) < 0)
1346 return error;
1347
1348 if (git_reference_type(head) == GIT_REF_OID) {
1349 *head_out = head;
1350 return 0;
1351 }
1352
2508cc66 1353 error = git_reference_lookup_resolved(head_out, repo, git_reference_symbolic_target(head), -1);
b1a3a70e 1354 git_reference_free(head);
8b05bea8 1355
1356 return error == GIT_ENOTFOUND ? GIT_EORPHANEDHEAD : error;
3601c4bf 1357}
1358
1359int git_repository_head_orphan(git_repository *repo)
1360{
cb8a7961 1361 git_reference *ref = NULL;
3601c4bf 1362 int error;
1363
1364 error = git_repository_head(&ref, repo);
cb8a7961 1365 git_reference_free(ref);
35502d2e 1366
8b05bea8 1367 if (error == GIT_EORPHANEDHEAD)
cb8a7961 1368 return 1;
75abd2b9 1369
cb8a7961
VM
1370 if (error < 0)
1371 return -1;
1372
1373 return 0;
35502d2e 1374}
e0011be3 1375
0066955d 1376static int at_least_one_cb(const char *refname, void *payload)
41233c40 1377{
6091457e 1378 GIT_UNUSED(refname);
1379 GIT_UNUSED(payload);
41233c40 1380
6091457e 1381 return GIT_EUSER;
1382}
41233c40 1383
6091457e 1384static int repo_contains_no_reference(git_repository *repo)
1385{
1386 int error;
1387
1388 error = git_reference_foreach(repo, GIT_REF_LISTALL, at_least_one_cb, NULL);
0f489fb2 1389
6091457e 1390 if (error == GIT_EUSER)
0f489fb2 1391 return 0;
41233c40 1392
6091457e 1393 return error == 0 ? 1 : error;
1394}
75abd2b9 1395
6091457e 1396int git_repository_is_empty(git_repository *repo)
1397{
1398 git_reference *head = NULL;
0066955d 1399 int error;
cb8a7961 1400
6091457e 1401 if (git_reference_lookup(&head, repo, GIT_HEAD_FILE) < 0)
cb8a7961
VM
1402 return -1;
1403
6091457e 1404 if (!(error = git_reference_type(head) == GIT_REF_SYMBOLIC))
1405 goto cleanup;
1406
1407 if (!(error = strcmp(
2508cc66 1408 git_reference_symbolic_target(head),
6091457e 1409 GIT_REFS_HEADS_DIR "master") == 0))
1410 goto cleanup;
1411
1412 error = repo_contains_no_reference(repo);
1413
1414cleanup:
1415 git_reference_free(head);
1416 return error < 0 ? -1 : error;
41233c40
VM
1417}
1418
9462c471 1419const char *git_repository_path(git_repository *repo)
4a34b3a9 1420{
1421 assert(repo);
9462c471
VM
1422 return repo->path_repository;
1423}
4a34b3a9 1424
9462c471
VM
1425const char *git_repository_workdir(git_repository *repo)
1426{
1427 assert(repo);
602ee38b 1428
9462c471
VM
1429 if (repo->is_bare)
1430 return NULL;
602ee38b 1431
9462c471
VM
1432 return repo->workdir;
1433}
602ee38b 1434
991a56c7
RB
1435int git_repository_set_workdir(
1436 git_repository *repo, const char *workdir, int update_gitlink)
9462c471 1437{
991a56c7 1438 int error = 0;
b78fb64d 1439 git_buf path = GIT_BUF_INIT;
1440
9462c471 1441 assert(repo && workdir);
602ee38b 1442
b78fb64d 1443 if (git_path_prettify_dir(&path, workdir, NULL) < 0)
1444 return -1;
9462c471 1445
991a56c7
RB
1446 if (repo->workdir && strcmp(repo->workdir, path.ptr) == 0)
1447 return 0;
9462c471 1448
991a56c7
RB
1449 if (update_gitlink) {
1450 git_config *config;
1451
1452 if (git_repository_config__weakptr(&config, repo) < 0)
1453 return -1;
1454
662880ca 1455 error = repo_write_gitlink(path.ptr, git_repository_path(repo));
991a56c7
RB
1456
1457 /* passthrough error means gitlink is unnecessary */
1458 if (error == GIT_PASSTHROUGH)
54b2a37a 1459 error = git_config_delete_entry(config, "core.worktree");
991a56c7
RB
1460 else if (!error)
1461 error = git_config_set_string(config, "core.worktree", path.ptr);
1462
1463 if (!error)
1464 error = git_config_set_bool(config, "core.bare", false);
1465 }
1466
1467 if (!error) {
1468 char *old_workdir = repo->workdir;
1469
1470 repo->workdir = git_buf_detach(&path);
1471 repo->is_bare = 0;
1472
1473 git__free(old_workdir);
1474 }
1475
1476 return error;
4a34b3a9 1477}
fa9bcd81 1478
1479int git_repository_is_bare(git_repository *repo)
1480{
1481 assert(repo);
1482 return repo->is_bare;
1483}
f917481e
RB
1484
1485int git_repository_head_tree(git_tree **tree, git_repository *repo)
1486{
5cec896a 1487 git_reference *head;
1488 git_object *obj;
1489 int error;
f917481e 1490
5cec896a 1491 if ((error = git_repository_head(&head, repo)) < 0)
1492 return error;
f917481e 1493
5cec896a 1494 if ((error = git_reference_peel(&obj, head, GIT_OBJ_TREE)) < 0)
1495 goto cleanup;
f917481e
RB
1496
1497 *tree = (git_tree *)obj;
5cec896a 1498
1499cleanup:
1500 git_reference_free(head);
1501 return error;
f917481e 1502}
074841ec 1503
074841ec
CMN
1504int git_repository_message(char *buffer, size_t len, git_repository *repo)
1505{
0ac349a9
VM
1506 git_buf buf = GIT_BUF_INIT, path = GIT_BUF_INIT;
1507 struct stat st;
0ac349a9 1508 int error;
074841ec 1509
632d8b23 1510 if (git_buf_joinpath(&path, repo->path_repository, GIT_MERGE_MSG_FILE) < 0)
0ac349a9 1511 return -1;
074841ec 1512
e9ca852e 1513 if ((error = p_stat(git_buf_cstr(&path), &st)) < 0) {
074841ec
CMN
1514 if (errno == ENOENT)
1515 error = GIT_ENOTFOUND;
0ac349a9 1516 }
e9ca852e
RB
1517 else if (buffer != NULL) {
1518 error = git_futils_readbuffer(&buf, git_buf_cstr(&path));
1519 git_buf_copy_cstr(buffer, len, &buf);
0ac349a9 1520 }
074841ec 1521
0ac349a9
VM
1522 git_buf_free(&path);
1523 git_buf_free(&buf);
074841ec 1524
e9ca852e
RB
1525 if (!error)
1526 error = (int)st.st_size + 1; /* add 1 for NUL byte */
1527
1528 return error;
074841ec
CMN
1529}
1530
1531int git_repository_message_remove(git_repository *repo)
1532{
0ac349a9
VM
1533 git_buf path = GIT_BUF_INIT;
1534 int error;
074841ec 1535
632d8b23 1536 if (git_buf_joinpath(&path, repo->path_repository, GIT_MERGE_MSG_FILE) < 0)
0ac349a9 1537 return -1;
074841ec
CMN
1538
1539 error = p_unlink(git_buf_cstr(&path));
1540 git_buf_free(&path);
1541
1542 return error;
1543}
47bfa0be
RB
1544
1545int git_repository_hashfile(
1546 git_oid *out,
1547 git_repository *repo,
1548 const char *path,
1549 git_otype type,
1550 const char *as_path)
1551{
1552 int error;
1553 git_vector filters = GIT_VECTOR_INIT;
b1127a30 1554 git_file fd = -1;
47bfa0be
RB
1555 git_off_t len;
1556 git_buf full_path = GIT_BUF_INIT;
1557
a13fb55a
RB
1558 assert(out && path && repo); /* as_path can be NULL */
1559
1560 /* At some point, it would be nice if repo could be NULL to just
1561 * apply filter rules defined in system and global files, but for
1562 * now that is not possible because git_filters_load() needs it.
1563 */
47bfa0be
RB
1564
1565 error = git_path_join_unrooted(
1566 &full_path, path, repo ? git_repository_workdir(repo) : NULL, NULL);
1567 if (error < 0)
1568 return error;
1569
1570 if (!as_path)
1571 as_path = path;
1572
1573 /* passing empty string for "as_path" indicated --no-filters */
1574 if (strlen(as_path) > 0) {
1575 error = git_filters_load(&filters, repo, as_path, GIT_FILTER_TO_ODB);
1576 if (error < 0)
1577 return error;
1578 } else {
1579 error = 0;
1580 }
1581
1582 /* at this point, error is a count of the number of loaded filters */
1583
1584 fd = git_futils_open_ro(full_path.ptr);
1585 if (fd < 0) {
1586 error = fd;
1587 goto cleanup;
1588 }
1589
1590 len = git_futils_filesize(fd);
1591 if (len < 0) {
75050223 1592 error = (int)len;
47bfa0be
RB
1593 goto cleanup;
1594 }
1595
1596 if (!git__is_sizet(len)) {
1597 giterr_set(GITERR_OS, "File size overflow for 32-bit systems");
1598 error = -1;
1599 goto cleanup;
1600 }
1601
75050223 1602 error = git_odb__hashfd_filtered(out, fd, (size_t)len, type, &filters);
47bfa0be
RB
1603
1604cleanup:
b1127a30
SS
1605 if (fd >= 0)
1606 p_close(fd);
47bfa0be
RB
1607 git_filters_free(&filters);
1608 git_buf_free(&full_path);
1609
1610 return error;
1611}
1612
44af67a8 1613static bool looks_like_a_branch(const char *refname)
1614{
1615 return git__prefixcmp(refname, GIT_REFS_HEADS_DIR) == 0;
1616}
1617
1618int git_repository_set_head(
1619 git_repository* repo,
1620 const char* refname)
1621{
1622 git_reference *ref,
1623 *new_head = NULL;
1624 int error;
1625
1626 assert(repo && refname);
1627
1628 error = git_reference_lookup(&ref, repo, refname);
1629 if (error < 0 && error != GIT_ENOTFOUND)
1630 return error;
1631
1632 if (!error) {
1633 if (git_reference_is_branch(ref))
2508cc66 1634 error = git_reference_symbolic_create(&new_head, repo, GIT_HEAD_FILE, git_reference_name(ref), 1);
44af67a8 1635 else
2508cc66 1636 error = git_repository_set_head_detached(repo, git_reference_target(ref));
44af67a8 1637 } else if (looks_like_a_branch(refname))
2508cc66 1638 error = git_reference_symbolic_create(&new_head, repo, GIT_HEAD_FILE, refname, 1);
44af67a8 1639
1640 git_reference_free(ref);
1641 git_reference_free(new_head);
1642 return error;
1643}
1644
4ebe38bd 1645int git_repository_set_head_detached(
1646 git_repository* repo,
1647 const git_oid* commitish)
1648{
1649 int error;
1650 git_object *object,
1651 *peeled = NULL;
1652 git_reference *new_head = NULL;
1653
1654 assert(repo && commitish);
1655
1656 if ((error = git_object_lookup(&object, repo, commitish, GIT_OBJ_ANY)) < 0)
1657 return error;
1658
1659 if ((error = git_object_peel(&peeled, object, GIT_OBJ_COMMIT)) < 0)
1660 goto cleanup;
1661
2508cc66 1662 error = git_reference_create(&new_head, repo, GIT_HEAD_FILE, git_object_id(peeled), 1);
4ebe38bd 1663
1664cleanup:
1665 git_object_free(object);
1666 git_object_free(peeled);
1667 git_reference_free(new_head);
1668 return error;
1669}
1670
3f4c3072 1671int git_repository_detach_head(
1672 git_repository* repo)
1673{
1674 git_reference *old_head = NULL,
1675 *new_head = NULL;
1676 git_object *object = NULL;
8b05bea8 1677 int error;
3f4c3072 1678
1679 assert(repo);
1680
8b05bea8 1681 if ((error = git_repository_head(&old_head, repo)) < 0)
1682 return error;
3f4c3072 1683
2508cc66 1684 if ((error = git_object_lookup(&object, repo, git_reference_target(old_head), GIT_OBJ_COMMIT)) < 0)
3f4c3072 1685 goto cleanup;
1686
2508cc66 1687 error = git_reference_create(&new_head, repo, GIT_HEAD_FILE, git_reference_target(old_head), 1);
3f4c3072 1688
1689cleanup:
1690 git_object_free(object);
1691 git_reference_free(old_head);
1692 git_reference_free(new_head);
1693 return error;
1694}
632d8b23 1695
31966d20 1696/**
1697 * Loosely ported from git.git
1698 * https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh#L198-289
1699 */
632d8b23
ET
1700int git_repository_state(git_repository *repo)
1701{
1702 git_buf repo_path = GIT_BUF_INIT;
1703 int state = GIT_REPOSITORY_STATE_NONE;
1704
1705 assert(repo);
1706
1707 if (git_buf_puts(&repo_path, repo->path_repository) < 0)
1708 return -1;
1709
31966d20 1710 if (git_path_contains_file(&repo_path, GIT_REBASE_MERGE_INTERACTIVE_FILE))
1711 state = GIT_REPOSITORY_STATE_REBASE_INTERACTIVE;
1712 else if (git_path_contains_dir(&repo_path, GIT_REBASE_MERGE_DIR))
1713 state = GIT_REPOSITORY_STATE_REBASE_MERGE;
1714 else if (git_path_contains_file(&repo_path, GIT_REBASE_APPLY_REBASING_FILE))
1715 state = GIT_REPOSITORY_STATE_REBASE;
1716 else if (git_path_contains_file(&repo_path, GIT_REBASE_APPLY_APPLYING_FILE))
1717 state = GIT_REPOSITORY_STATE_APPLY_MAILBOX;
1718 else if (git_path_contains_dir(&repo_path, GIT_REBASE_APPLY_DIR))
1719 state = GIT_REPOSITORY_STATE_APPLY_MAILBOX_OR_REBASE;
1720 else if (git_path_contains_file(&repo_path, GIT_MERGE_HEAD_FILE))
632d8b23
ET
1721 state = GIT_REPOSITORY_STATE_MERGE;
1722 else if(git_path_contains_file(&repo_path, GIT_REVERT_HEAD_FILE))
1723 state = GIT_REPOSITORY_STATE_REVERT;
1724 else if(git_path_contains_file(&repo_path, GIT_CHERRY_PICK_HEAD_FILE))
1725 state = GIT_REPOSITORY_STATE_CHERRY_PICK;
31966d20 1726 else if(git_path_contains_file(&repo_path, GIT_BISECT_LOG_FILE))
1727 state = GIT_REPOSITORY_STATE_BISECT;
632d8b23
ET
1728
1729 git_buf_free(&repo_path);
1730 return state;
1731}