PageRenderTime 65ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/refdb_fs.c

http://github.com/libgit2/libgit2
C | 2196 lines | 1536 code | 465 blank | 195 comment | 412 complexity | 7d6548a63f9b791a35ef48bc6225a580 MD5 | raw file
Possible License(s): LGPL-2.1, MIT, CC0-1.0
  1. /*
  2. * Copyright (C) the libgit2 contributors. All rights reserved.
  3. *
  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.
  6. */
  7. #include "refs.h"
  8. #include "hash.h"
  9. #include "repository.h"
  10. #include "futils.h"
  11. #include "filebuf.h"
  12. #include "pack.h"
  13. #include "parse.h"
  14. #include "reflog.h"
  15. #include "refdb.h"
  16. #include "iterator.h"
  17. #include "sortedcache.h"
  18. #include "signature.h"
  19. #include "wildmatch.h"
  20. #include <git2/tag.h>
  21. #include <git2/object.h>
  22. #include <git2/refdb.h>
  23. #include <git2/branch.h>
  24. #include <git2/sys/refdb_backend.h>
  25. #include <git2/sys/refs.h>
  26. #include <git2/sys/reflog.h>
  27. #define DEFAULT_NESTING_LEVEL 5
  28. #define MAX_NESTING_LEVEL 10
  29. enum {
  30. PACKREF_HAS_PEEL = 1,
  31. PACKREF_WAS_LOOSE = 2,
  32. PACKREF_CANNOT_PEEL = 4,
  33. PACKREF_SHADOWED = 8,
  34. };
  35. enum {
  36. PEELING_NONE = 0,
  37. PEELING_STANDARD,
  38. PEELING_FULL
  39. };
  40. struct packref {
  41. git_oid oid;
  42. git_oid peel;
  43. char flags;
  44. char name[GIT_FLEX_ARRAY];
  45. };
  46. typedef struct refdb_fs_backend {
  47. git_refdb_backend parent;
  48. git_repository *repo;
  49. /* path to git directory */
  50. char *gitpath;
  51. /* path to common objects' directory */
  52. char *commonpath;
  53. git_sortedcache *refcache;
  54. int peeling_mode;
  55. git_iterator_flag_t iterator_flags;
  56. uint32_t direach_flags;
  57. int fsync;
  58. } refdb_fs_backend;
  59. static int refdb_reflog_fs__delete(git_refdb_backend *_backend, const char *name);
  60. static int packref_cmp(const void *a_, const void *b_)
  61. {
  62. const struct packref *a = a_, *b = b_;
  63. return strcmp(a->name, b->name);
  64. }
  65. static int packed_reload(refdb_fs_backend *backend)
  66. {
  67. int error;
  68. git_buf packedrefs = GIT_BUF_INIT;
  69. char *scan, *eof, *eol;
  70. if (!backend->gitpath)
  71. return 0;
  72. error = git_sortedcache_lockandload(backend->refcache, &packedrefs);
  73. /*
  74. * If we can't find the packed-refs, clear table and return.
  75. * Any other error just gets passed through.
  76. * If no error, and file wasn't changed, just return.
  77. * Anything else means we need to refresh the packed refs.
  78. */
  79. if (error <= 0) {
  80. if (error == GIT_ENOTFOUND) {
  81. git_sortedcache_clear(backend->refcache, true);
  82. git_error_clear();
  83. error = 0;
  84. }
  85. return error;
  86. }
  87. /* At this point, refresh the packed refs from the loaded buffer. */
  88. git_sortedcache_clear(backend->refcache, false);
  89. scan = (char *)packedrefs.ptr;
  90. eof = scan + packedrefs.size;
  91. backend->peeling_mode = PEELING_NONE;
  92. if (*scan == '#') {
  93. static const char *traits_header = "# pack-refs with: ";
  94. if (git__prefixcmp(scan, traits_header) == 0) {
  95. scan += strlen(traits_header);
  96. eol = strchr(scan, '\n');
  97. if (!eol)
  98. goto parse_failed;
  99. *eol = '\0';
  100. if (strstr(scan, " fully-peeled ") != NULL) {
  101. backend->peeling_mode = PEELING_FULL;
  102. } else if (strstr(scan, " peeled ") != NULL) {
  103. backend->peeling_mode = PEELING_STANDARD;
  104. }
  105. scan = eol + 1;
  106. }
  107. }
  108. while (scan < eof && *scan == '#') {
  109. if (!(eol = strchr(scan, '\n')))
  110. goto parse_failed;
  111. scan = eol + 1;
  112. }
  113. while (scan < eof) {
  114. struct packref *ref;
  115. git_oid oid;
  116. /* parse "<OID> <refname>\n" */
  117. if (git_oid_fromstr(&oid, scan) < 0)
  118. goto parse_failed;
  119. scan += GIT_OID_HEXSZ;
  120. if (*scan++ != ' ')
  121. goto parse_failed;
  122. if (!(eol = strchr(scan, '\n')))
  123. goto parse_failed;
  124. *eol = '\0';
  125. if (eol[-1] == '\r')
  126. eol[-1] = '\0';
  127. if (git_sortedcache_upsert((void **)&ref, backend->refcache, scan) < 0)
  128. goto parse_failed;
  129. scan = eol + 1;
  130. git_oid_cpy(&ref->oid, &oid);
  131. /* look for optional "^<OID>\n" */
  132. if (*scan == '^') {
  133. if (git_oid_fromstr(&oid, scan + 1) < 0)
  134. goto parse_failed;
  135. scan += GIT_OID_HEXSZ + 1;
  136. if (scan < eof) {
  137. if (!(eol = strchr(scan, '\n')))
  138. goto parse_failed;
  139. scan = eol + 1;
  140. }
  141. git_oid_cpy(&ref->peel, &oid);
  142. ref->flags |= PACKREF_HAS_PEEL;
  143. }
  144. else if (backend->peeling_mode == PEELING_FULL ||
  145. (backend->peeling_mode == PEELING_STANDARD &&
  146. git__prefixcmp(ref->name, GIT_REFS_TAGS_DIR) == 0))
  147. ref->flags |= PACKREF_CANNOT_PEEL;
  148. }
  149. git_sortedcache_wunlock(backend->refcache);
  150. git_buf_dispose(&packedrefs);
  151. return 0;
  152. parse_failed:
  153. git_error_set(GIT_ERROR_REFERENCE, "corrupted packed references file");
  154. git_sortedcache_clear(backend->refcache, false);
  155. git_sortedcache_wunlock(backend->refcache);
  156. git_buf_dispose(&packedrefs);
  157. return -1;
  158. }
  159. static int loose_parse_oid(
  160. git_oid *oid, const char *filename, git_buf *file_content)
  161. {
  162. const char *str = git_buf_cstr(file_content);
  163. if (git_buf_len(file_content) < GIT_OID_HEXSZ)
  164. goto corrupted;
  165. /* we need to get 40 OID characters from the file */
  166. if (git_oid_fromstr(oid, str) < 0)
  167. goto corrupted;
  168. /* If the file is longer than 40 chars, the 41st must be a space */
  169. str += GIT_OID_HEXSZ;
  170. if (*str == '\0' || git__isspace(*str))
  171. return 0;
  172. corrupted:
  173. git_error_set(GIT_ERROR_REFERENCE, "corrupted loose reference file: %s", filename);
  174. return -1;
  175. }
  176. static int loose_readbuffer(git_buf *buf, const char *base, const char *path)
  177. {
  178. int error;
  179. /* build full path to file */
  180. if ((error = git_buf_joinpath(buf, base, path)) < 0 ||
  181. (error = git_futils_readbuffer(buf, buf->ptr)) < 0)
  182. git_buf_dispose(buf);
  183. return error;
  184. }
  185. static int loose_lookup_to_packfile(refdb_fs_backend *backend, const char *name)
  186. {
  187. int error = 0;
  188. git_buf ref_file = GIT_BUF_INIT;
  189. struct packref *ref = NULL;
  190. git_oid oid;
  191. /* if we fail to load the loose reference, assume someone changed
  192. * the filesystem under us and skip it...
  193. */
  194. if (loose_readbuffer(&ref_file, backend->gitpath, name) < 0) {
  195. git_error_clear();
  196. goto done;
  197. }
  198. /* skip symbolic refs */
  199. if (!git__prefixcmp(git_buf_cstr(&ref_file), GIT_SYMREF))
  200. goto done;
  201. /* parse OID from file */
  202. if ((error = loose_parse_oid(&oid, name, &ref_file)) < 0)
  203. goto done;
  204. git_sortedcache_wlock(backend->refcache);
  205. if (!(error = git_sortedcache_upsert(
  206. (void **)&ref, backend->refcache, name))) {
  207. git_oid_cpy(&ref->oid, &oid);
  208. ref->flags = PACKREF_WAS_LOOSE;
  209. }
  210. git_sortedcache_wunlock(backend->refcache);
  211. done:
  212. git_buf_dispose(&ref_file);
  213. return error;
  214. }
  215. static int _dirent_loose_load(void *payload, git_buf *full_path)
  216. {
  217. refdb_fs_backend *backend = payload;
  218. const char *file_path;
  219. if (git__suffixcmp(full_path->ptr, ".lock") == 0)
  220. return 0;
  221. if (git_path_isdir(full_path->ptr)) {
  222. int error = git_path_direach(
  223. full_path, backend->direach_flags, _dirent_loose_load, backend);
  224. /* Race with the filesystem, ignore it */
  225. if (error == GIT_ENOTFOUND) {
  226. git_error_clear();
  227. return 0;
  228. }
  229. return error;
  230. }
  231. file_path = full_path->ptr + strlen(backend->gitpath);
  232. return loose_lookup_to_packfile(backend, file_path);
  233. }
  234. /*
  235. * Load all the loose references from the repository
  236. * into the in-memory Packfile, and build a vector with
  237. * all the references so it can be written back to
  238. * disk.
  239. */
  240. static int packed_loadloose(refdb_fs_backend *backend)
  241. {
  242. int error;
  243. git_buf refs_path = GIT_BUF_INIT;
  244. if (git_buf_joinpath(&refs_path, backend->gitpath, GIT_REFS_DIR) < 0)
  245. return -1;
  246. /*
  247. * Load all the loose files from disk into the Packfile table.
  248. * This will overwrite any old packed entries with their
  249. * updated loose versions
  250. */
  251. error = git_path_direach(
  252. &refs_path, backend->direach_flags, _dirent_loose_load, backend);
  253. git_buf_dispose(&refs_path);
  254. return error;
  255. }
  256. static int refdb_fs_backend__exists(
  257. int *exists,
  258. git_refdb_backend *_backend,
  259. const char *ref_name)
  260. {
  261. refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
  262. git_buf ref_path = GIT_BUF_INIT;
  263. int error;
  264. assert(backend);
  265. *exists = 0;
  266. if ((error = git_buf_joinpath(&ref_path, backend->gitpath, ref_name)) < 0)
  267. goto out;
  268. if (git_path_isfile(ref_path.ptr)) {
  269. *exists = 1;
  270. goto out;
  271. }
  272. if ((error = packed_reload(backend)) < 0)
  273. goto out;
  274. if (git_sortedcache_lookup(backend->refcache, ref_name) != NULL) {
  275. *exists = 1;
  276. goto out;
  277. }
  278. out:
  279. git_buf_dispose(&ref_path);
  280. return error;
  281. }
  282. static const char *loose_parse_symbolic(git_buf *file_content)
  283. {
  284. const unsigned int header_len = (unsigned int)strlen(GIT_SYMREF);
  285. const char *refname_start;
  286. refname_start = (const char *)file_content->ptr;
  287. if (git_buf_len(file_content) < header_len + 1) {
  288. git_error_set(GIT_ERROR_REFERENCE, "corrupted loose reference file");
  289. return NULL;
  290. }
  291. /*
  292. * Assume we have already checked for the header
  293. * before calling this function
  294. */
  295. refname_start += header_len;
  296. return refname_start;
  297. }
  298. /*
  299. * Returns whether a reference is stored per worktree or not.
  300. * Per-worktree references are:
  301. *
  302. * - all pseudorefs, e.g. HEAD and MERGE_HEAD
  303. * - all references stored inside of "refs/bisect/"
  304. */
  305. static bool is_per_worktree_ref(const char *ref_name)
  306. {
  307. return git__prefixcmp(ref_name, "refs/") != 0 ||
  308. git__prefixcmp(ref_name, "refs/bisect/") == 0;
  309. }
  310. static int loose_lookup(
  311. git_reference **out,
  312. refdb_fs_backend *backend,
  313. const char *ref_name)
  314. {
  315. git_buf ref_file = GIT_BUF_INIT;
  316. int error = 0;
  317. const char *ref_dir;
  318. if (out)
  319. *out = NULL;
  320. if (is_per_worktree_ref(ref_name))
  321. ref_dir = backend->gitpath;
  322. else
  323. ref_dir = backend->commonpath;
  324. if ((error = loose_readbuffer(&ref_file, ref_dir, ref_name)) < 0)
  325. /* cannot read loose ref file - gah */;
  326. else if (git__prefixcmp(git_buf_cstr(&ref_file), GIT_SYMREF) == 0) {
  327. const char *target;
  328. git_buf_rtrim(&ref_file);
  329. if (!(target = loose_parse_symbolic(&ref_file)))
  330. error = -1;
  331. else if (out != NULL)
  332. *out = git_reference__alloc_symbolic(ref_name, target);
  333. } else {
  334. git_oid oid;
  335. if (!(error = loose_parse_oid(&oid, ref_name, &ref_file)) &&
  336. out != NULL)
  337. *out = git_reference__alloc(ref_name, &oid, NULL);
  338. }
  339. git_buf_dispose(&ref_file);
  340. return error;
  341. }
  342. static int ref_error_notfound(const char *name)
  343. {
  344. git_error_set(GIT_ERROR_REFERENCE, "reference '%s' not found", name);
  345. return GIT_ENOTFOUND;
  346. }
  347. static int packed_lookup(
  348. git_reference **out,
  349. refdb_fs_backend *backend,
  350. const char *ref_name)
  351. {
  352. int error = 0;
  353. struct packref *entry;
  354. if ((error = packed_reload(backend)) < 0)
  355. return error;
  356. if (git_sortedcache_rlock(backend->refcache) < 0)
  357. return -1;
  358. entry = git_sortedcache_lookup(backend->refcache, ref_name);
  359. if (!entry) {
  360. error = ref_error_notfound(ref_name);
  361. } else {
  362. *out = git_reference__alloc(ref_name, &entry->oid, &entry->peel);
  363. if (!*out)
  364. error = -1;
  365. }
  366. git_sortedcache_runlock(backend->refcache);
  367. return error;
  368. }
  369. static int refdb_fs_backend__lookup(
  370. git_reference **out,
  371. git_refdb_backend *_backend,
  372. const char *ref_name)
  373. {
  374. refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
  375. int error;
  376. assert(backend);
  377. if (!(error = loose_lookup(out, backend, ref_name)))
  378. return 0;
  379. /* only try to lookup this reference on the packfile if it
  380. * wasn't found on the loose refs; not if there was a critical error */
  381. if (error == GIT_ENOTFOUND) {
  382. git_error_clear();
  383. error = packed_lookup(out, backend, ref_name);
  384. }
  385. return error;
  386. }
  387. typedef struct {
  388. git_reference_iterator parent;
  389. char *glob;
  390. git_pool pool;
  391. git_vector loose;
  392. git_sortedcache *cache;
  393. size_t loose_pos;
  394. size_t packed_pos;
  395. } refdb_fs_iter;
  396. static void refdb_fs_backend__iterator_free(git_reference_iterator *_iter)
  397. {
  398. refdb_fs_iter *iter = GIT_CONTAINER_OF(_iter, refdb_fs_iter, parent);
  399. git_vector_free(&iter->loose);
  400. git_pool_clear(&iter->pool);
  401. git_sortedcache_free(iter->cache);
  402. git__free(iter);
  403. }
  404. static int iter_load_loose_paths(refdb_fs_backend *backend, refdb_fs_iter *iter)
  405. {
  406. int error = 0;
  407. git_buf path = GIT_BUF_INIT;
  408. git_iterator *fsit = NULL;
  409. git_iterator_options fsit_opts = GIT_ITERATOR_OPTIONS_INIT;
  410. const git_index_entry *entry = NULL;
  411. const char *ref_prefix = GIT_REFS_DIR;
  412. size_t ref_prefix_len = strlen(ref_prefix);
  413. if (!backend->commonpath) /* do nothing if no commonpath for loose refs */
  414. return 0;
  415. fsit_opts.flags = backend->iterator_flags;
  416. if (iter->glob) {
  417. const char *last_sep = NULL;
  418. const char *pos;
  419. for (pos = iter->glob; *pos; ++pos) {
  420. switch (*pos) {
  421. case '?':
  422. case '*':
  423. case '[':
  424. case '\\':
  425. break;
  426. case '/':
  427. last_sep = pos;
  428. /* FALLTHROUGH */
  429. default:
  430. continue;
  431. }
  432. break;
  433. }
  434. if (last_sep) {
  435. ref_prefix = iter->glob;
  436. ref_prefix_len = (last_sep - ref_prefix) + 1;
  437. }
  438. }
  439. if ((error = git_buf_printf(&path, "%s/", backend->commonpath)) < 0 ||
  440. (error = git_buf_put(&path, ref_prefix, ref_prefix_len)) < 0) {
  441. git_buf_dispose(&path);
  442. return error;
  443. }
  444. if ((error = git_iterator_for_filesystem(&fsit, path.ptr, &fsit_opts)) < 0) {
  445. git_buf_dispose(&path);
  446. return (iter->glob && error == GIT_ENOTFOUND)? 0 : error;
  447. }
  448. error = git_buf_sets(&path, ref_prefix);
  449. while (!error && !git_iterator_advance(&entry, fsit)) {
  450. const char *ref_name;
  451. char *ref_dup;
  452. git_buf_truncate(&path, ref_prefix_len);
  453. git_buf_puts(&path, entry->path);
  454. ref_name = git_buf_cstr(&path);
  455. if (git__suffixcmp(ref_name, ".lock") == 0 ||
  456. (iter->glob && wildmatch(iter->glob, ref_name, 0) != 0))
  457. continue;
  458. ref_dup = git_pool_strdup(&iter->pool, ref_name);
  459. if (!ref_dup)
  460. error = -1;
  461. else
  462. error = git_vector_insert(&iter->loose, ref_dup);
  463. }
  464. git_iterator_free(fsit);
  465. git_buf_dispose(&path);
  466. return error;
  467. }
  468. static int refdb_fs_backend__iterator_next(
  469. git_reference **out, git_reference_iterator *_iter)
  470. {
  471. int error = GIT_ITEROVER;
  472. refdb_fs_iter *iter = GIT_CONTAINER_OF(_iter, refdb_fs_iter, parent);
  473. refdb_fs_backend *backend = GIT_CONTAINER_OF(iter->parent.db->backend, refdb_fs_backend, parent);
  474. struct packref *ref;
  475. while (iter->loose_pos < iter->loose.length) {
  476. const char *path = git_vector_get(&iter->loose, iter->loose_pos++);
  477. if (loose_lookup(out, backend, path) == 0) {
  478. ref = git_sortedcache_lookup(iter->cache, path);
  479. if (ref)
  480. ref->flags |= PACKREF_SHADOWED;
  481. return 0;
  482. }
  483. git_error_clear();
  484. }
  485. error = GIT_ITEROVER;
  486. while (iter->packed_pos < git_sortedcache_entrycount(iter->cache)) {
  487. ref = git_sortedcache_entry(iter->cache, iter->packed_pos++);
  488. if (!ref) /* stop now if another thread deleted refs and we past end */
  489. break;
  490. if (ref->flags & PACKREF_SHADOWED)
  491. continue;
  492. if (iter->glob && wildmatch(iter->glob, ref->name, 0) != 0)
  493. continue;
  494. *out = git_reference__alloc(ref->name, &ref->oid, &ref->peel);
  495. error = (*out != NULL) ? 0 : -1;
  496. break;
  497. }
  498. return error;
  499. }
  500. static int refdb_fs_backend__iterator_next_name(
  501. const char **out, git_reference_iterator *_iter)
  502. {
  503. int error = GIT_ITEROVER;
  504. refdb_fs_iter *iter = GIT_CONTAINER_OF(_iter, refdb_fs_iter, parent);
  505. refdb_fs_backend *backend = GIT_CONTAINER_OF(iter->parent.db->backend, refdb_fs_backend, parent);
  506. struct packref *ref;
  507. while (iter->loose_pos < iter->loose.length) {
  508. const char *path = git_vector_get(&iter->loose, iter->loose_pos++);
  509. struct packref *ref;
  510. if (loose_lookup(NULL, backend, path) == 0) {
  511. ref = git_sortedcache_lookup(iter->cache, path);
  512. if (ref)
  513. ref->flags |= PACKREF_SHADOWED;
  514. *out = path;
  515. return 0;
  516. }
  517. git_error_clear();
  518. }
  519. error = GIT_ITEROVER;
  520. while (iter->packed_pos < git_sortedcache_entrycount(iter->cache)) {
  521. ref = git_sortedcache_entry(iter->cache, iter->packed_pos++);
  522. if (!ref) /* stop now if another thread deleted refs and we past end */
  523. break;
  524. if (ref->flags & PACKREF_SHADOWED)
  525. continue;
  526. if (iter->glob && wildmatch(iter->glob, ref->name, 0) != 0)
  527. continue;
  528. *out = ref->name;
  529. error = 0;
  530. break;
  531. }
  532. return error;
  533. }
  534. static int refdb_fs_backend__iterator(
  535. git_reference_iterator **out, git_refdb_backend *_backend, const char *glob)
  536. {
  537. refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
  538. refdb_fs_iter *iter = NULL;
  539. int error;
  540. assert(backend);
  541. iter = git__calloc(1, sizeof(refdb_fs_iter));
  542. GIT_ERROR_CHECK_ALLOC(iter);
  543. git_pool_init(&iter->pool, 1);
  544. if ((error = git_vector_init(&iter->loose, 8, NULL)) < 0)
  545. goto out;
  546. if (glob != NULL &&
  547. (iter->glob = git_pool_strdup(&iter->pool, glob)) == NULL) {
  548. error = GIT_ERROR_NOMEMORY;
  549. goto out;
  550. }
  551. if ((error = iter_load_loose_paths(backend, iter)) < 0)
  552. goto out;
  553. if ((error = packed_reload(backend)) < 0)
  554. goto out;
  555. if ((error = git_sortedcache_copy(&iter->cache, backend->refcache, 1, NULL, NULL)) < 0)
  556. goto out;
  557. iter->parent.next = refdb_fs_backend__iterator_next;
  558. iter->parent.next_name = refdb_fs_backend__iterator_next_name;
  559. iter->parent.free = refdb_fs_backend__iterator_free;
  560. *out = (git_reference_iterator *)iter;
  561. out:
  562. if (error)
  563. refdb_fs_backend__iterator_free((git_reference_iterator *)iter);
  564. return error;
  565. }
  566. static bool ref_is_available(
  567. const char *old_ref, const char *new_ref, const char *this_ref)
  568. {
  569. if (old_ref == NULL || strcmp(old_ref, this_ref)) {
  570. size_t reflen = strlen(this_ref);
  571. size_t newlen = strlen(new_ref);
  572. size_t cmplen = reflen < newlen ? reflen : newlen;
  573. const char *lead = reflen < newlen ? new_ref : this_ref;
  574. if (!strncmp(new_ref, this_ref, cmplen) && lead[cmplen] == '/') {
  575. return false;
  576. }
  577. }
  578. return true;
  579. }
  580. static int reference_path_available(
  581. refdb_fs_backend *backend,
  582. const char *new_ref,
  583. const char* old_ref,
  584. int force)
  585. {
  586. size_t i;
  587. int error;
  588. if ((error = packed_reload(backend)) < 0)
  589. return error;
  590. if (!force) {
  591. int exists;
  592. if ((error = refdb_fs_backend__exists(
  593. &exists, (git_refdb_backend *)backend, new_ref)) < 0) {
  594. return error;
  595. }
  596. if (exists) {
  597. git_error_set(GIT_ERROR_REFERENCE,
  598. "failed to write reference '%s': a reference with "
  599. "that name already exists.", new_ref);
  600. return GIT_EEXISTS;
  601. }
  602. }
  603. git_sortedcache_rlock(backend->refcache);
  604. for (i = 0; i < git_sortedcache_entrycount(backend->refcache); ++i) {
  605. struct packref *ref = git_sortedcache_entry(backend->refcache, i);
  606. if (ref && !ref_is_available(old_ref, new_ref, ref->name)) {
  607. git_sortedcache_runlock(backend->refcache);
  608. git_error_set(GIT_ERROR_REFERENCE,
  609. "path to reference '%s' collides with existing one", new_ref);
  610. return -1;
  611. }
  612. }
  613. git_sortedcache_runlock(backend->refcache);
  614. return 0;
  615. }
  616. static int loose_lock(git_filebuf *file, refdb_fs_backend *backend, const char *name)
  617. {
  618. int error, filebuf_flags;
  619. git_buf ref_path = GIT_BUF_INIT;
  620. const char *basedir;
  621. assert(file && backend && name);
  622. if (!git_path_isvalid(backend->repo, name, 0, GIT_PATH_REJECT_FILESYSTEM_DEFAULTS)) {
  623. git_error_set(GIT_ERROR_INVALID, "invalid reference name '%s'", name);
  624. return GIT_EINVALIDSPEC;
  625. }
  626. if (is_per_worktree_ref(name))
  627. basedir = backend->gitpath;
  628. else
  629. basedir = backend->commonpath;
  630. /* Remove a possibly existing empty directory hierarchy
  631. * which name would collide with the reference name
  632. */
  633. if ((error = git_futils_rmdir_r(name, basedir, GIT_RMDIR_SKIP_NONEMPTY)) < 0)
  634. return error;
  635. if (git_buf_joinpath(&ref_path, basedir, name) < 0)
  636. return -1;
  637. filebuf_flags = GIT_FILEBUF_CREATE_LEADING_DIRS;
  638. if (backend->fsync)
  639. filebuf_flags |= GIT_FILEBUF_FSYNC;
  640. error = git_filebuf_open(file, ref_path.ptr, filebuf_flags, GIT_REFS_FILE_MODE);
  641. if (error == GIT_EDIRECTORY)
  642. git_error_set(GIT_ERROR_REFERENCE, "cannot lock ref '%s', there are refs beneath that folder", name);
  643. git_buf_dispose(&ref_path);
  644. return error;
  645. }
  646. static int loose_commit(git_filebuf *file, const git_reference *ref)
  647. {
  648. assert(file && ref);
  649. if (ref->type == GIT_REFERENCE_DIRECT) {
  650. char oid[GIT_OID_HEXSZ + 1];
  651. git_oid_nfmt(oid, sizeof(oid), &ref->target.oid);
  652. git_filebuf_printf(file, "%s\n", oid);
  653. } else if (ref->type == GIT_REFERENCE_SYMBOLIC) {
  654. git_filebuf_printf(file, GIT_SYMREF "%s\n", ref->target.symbolic);
  655. } else {
  656. assert(0); /* don't let this happen */
  657. }
  658. return git_filebuf_commit(file);
  659. }
  660. static int refdb_fs_backend__lock(void **out, git_refdb_backend *_backend, const char *refname)
  661. {
  662. int error;
  663. git_filebuf *lock;
  664. refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
  665. lock = git__calloc(1, sizeof(git_filebuf));
  666. GIT_ERROR_CHECK_ALLOC(lock);
  667. if ((error = loose_lock(lock, backend, refname)) < 0) {
  668. git__free(lock);
  669. return error;
  670. }
  671. *out = lock;
  672. return 0;
  673. }
  674. static int refdb_fs_backend__write_tail(
  675. git_refdb_backend *_backend,
  676. const git_reference *ref,
  677. git_filebuf *file,
  678. int update_reflog,
  679. const git_oid *old_id,
  680. const char *old_target,
  681. const git_signature *who,
  682. const char *message);
  683. static int refdb_fs_backend__delete_tail(
  684. git_refdb_backend *_backend,
  685. git_filebuf *file,
  686. const char *ref_name,
  687. const git_oid *old_id,
  688. const char *old_target);
  689. static int refdb_fs_backend__unlock(git_refdb_backend *backend, void *payload, int success, int update_reflog,
  690. const git_reference *ref, const git_signature *sig, const char *message)
  691. {
  692. git_filebuf *lock = (git_filebuf *) payload;
  693. int error = 0;
  694. if (success == 2)
  695. error = refdb_fs_backend__delete_tail(backend, lock, ref->name, NULL, NULL);
  696. else if (success)
  697. error = refdb_fs_backend__write_tail(backend, ref, lock, update_reflog, NULL, NULL, sig, message);
  698. else
  699. git_filebuf_cleanup(lock);
  700. git__free(lock);
  701. return error;
  702. }
  703. /*
  704. * Find out what object this reference resolves to.
  705. *
  706. * For references that point to a 'big' tag (e.g. an
  707. * actual tag object on the repository), we need to
  708. * cache on the packfile the OID of the object to
  709. * which that 'big tag' is pointing to.
  710. */
  711. static int packed_find_peel(refdb_fs_backend *backend, struct packref *ref)
  712. {
  713. git_object *object;
  714. if (ref->flags & PACKREF_HAS_PEEL || ref->flags & PACKREF_CANNOT_PEEL)
  715. return 0;
  716. /*
  717. * Find the tagged object in the repository
  718. */
  719. if (git_object_lookup(&object, backend->repo, &ref->oid, GIT_OBJECT_ANY) < 0)
  720. return -1;
  721. /*
  722. * If the tagged object is a Tag object, we need to resolve it;
  723. * if the ref is actually a 'weak' ref, we don't need to resolve
  724. * anything.
  725. */
  726. if (git_object_type(object) == GIT_OBJECT_TAG) {
  727. git_tag *tag = (git_tag *)object;
  728. /*
  729. * Find the object pointed at by this tag
  730. */
  731. git_oid_cpy(&ref->peel, git_tag_target_id(tag));
  732. ref->flags |= PACKREF_HAS_PEEL;
  733. /*
  734. * The reference has now cached the resolved OID, and is
  735. * marked at such. When written to the packfile, it'll be
  736. * accompanied by this resolved oid
  737. */
  738. }
  739. git_object_free(object);
  740. return 0;
  741. }
  742. /*
  743. * Write a single reference into a packfile
  744. */
  745. static int packed_write_ref(struct packref *ref, git_filebuf *file)
  746. {
  747. char oid[GIT_OID_HEXSZ + 1];
  748. git_oid_nfmt(oid, sizeof(oid), &ref->oid);
  749. /*
  750. * For references that peel to an object in the repo, we must
  751. * write the resulting peel on a separate line, e.g.
  752. *
  753. * 6fa8a902cc1d18527e1355773c86721945475d37 refs/tags/libgit2-0.4
  754. * ^2ec0cb7959b0bf965d54f95453f5b4b34e8d3100
  755. *
  756. * This obviously only applies to tags.
  757. * The required peels have already been loaded into `ref->peel_target`.
  758. */
  759. if (ref->flags & PACKREF_HAS_PEEL) {
  760. char peel[GIT_OID_HEXSZ + 1];
  761. git_oid_nfmt(peel, sizeof(peel), &ref->peel);
  762. if (git_filebuf_printf(file, "%s %s\n^%s\n", oid, ref->name, peel) < 0)
  763. return -1;
  764. } else {
  765. if (git_filebuf_printf(file, "%s %s\n", oid, ref->name) < 0)
  766. return -1;
  767. }
  768. return 0;
  769. }
  770. /*
  771. * Remove all loose references
  772. *
  773. * Once we have successfully written a packfile,
  774. * all the loose references that were packed must be
  775. * removed from disk.
  776. *
  777. * This is a dangerous method; make sure the packfile
  778. * is well-written, because we are destructing references
  779. * here otherwise.
  780. */
  781. static int packed_remove_loose(refdb_fs_backend *backend)
  782. {
  783. size_t i;
  784. git_filebuf lock = GIT_FILEBUF_INIT;
  785. git_buf ref_content = GIT_BUF_INIT;
  786. int error = 0;
  787. /* backend->refcache is already locked when this is called */
  788. for (i = 0; i < git_sortedcache_entrycount(backend->refcache); ++i) {
  789. struct packref *ref = git_sortedcache_entry(backend->refcache, i);
  790. git_oid current_id;
  791. if (!ref || !(ref->flags & PACKREF_WAS_LOOSE))
  792. continue;
  793. git_filebuf_cleanup(&lock);
  794. /* We need to stop anybody from updating the ref while we try to do a safe delete */
  795. error = loose_lock(&lock, backend, ref->name);
  796. /* If someone else is updating it, let them do it */
  797. if (error == GIT_EEXISTS || error == GIT_ENOTFOUND)
  798. continue;
  799. if (error < 0) {
  800. git_buf_dispose(&ref_content);
  801. git_error_set(GIT_ERROR_REFERENCE, "failed to lock loose reference '%s'", ref->name);
  802. return error;
  803. }
  804. error = git_futils_readbuffer(&ref_content, lock.path_original);
  805. /* Someone else beat us to cleaning up the ref, let's simply continue */
  806. if (error == GIT_ENOTFOUND)
  807. continue;
  808. /* This became a symref between us packing and trying to delete it, so ignore it */
  809. if (!git__prefixcmp(ref_content.ptr, GIT_SYMREF))
  810. continue;
  811. /* Figure out the current id; if we find a bad ref file, skip it so we can do the rest */
  812. if (loose_parse_oid(&current_id, lock.path_original, &ref_content) < 0)
  813. continue;
  814. /* If the ref moved since we packed it, we must not delete it */
  815. if (!git_oid_equal(&current_id, &ref->oid))
  816. continue;
  817. /*
  818. * if we fail to remove a single file, this is *not* good,
  819. * but we should keep going and remove as many as possible.
  820. * If we fail to remove, the ref is still in the old state, so
  821. * we haven't lost information.
  822. */
  823. p_unlink(lock.path_original);
  824. }
  825. git_buf_dispose(&ref_content);
  826. git_filebuf_cleanup(&lock);
  827. return 0;
  828. }
  829. /*
  830. * Write all the contents in the in-memory packfile to disk.
  831. */
  832. static int packed_write(refdb_fs_backend *backend)
  833. {
  834. git_sortedcache *refcache = backend->refcache;
  835. git_filebuf pack_file = GIT_FILEBUF_INIT;
  836. int error, open_flags = 0;
  837. size_t i;
  838. /* lock the cache to updates while we do this */
  839. if ((error = git_sortedcache_wlock(refcache)) < 0)
  840. return error;
  841. if (backend->fsync)
  842. open_flags = GIT_FILEBUF_FSYNC;
  843. /* Open the file! */
  844. if ((error = git_filebuf_open(&pack_file, git_sortedcache_path(refcache), open_flags, GIT_PACKEDREFS_FILE_MODE)) < 0)
  845. goto fail;
  846. /* Packfiles have a header... apparently
  847. * This is in fact not required, but we might as well print it
  848. * just for kicks */
  849. if ((error = git_filebuf_printf(&pack_file, "%s\n", GIT_PACKEDREFS_HEADER)) < 0)
  850. goto fail;
  851. for (i = 0; i < git_sortedcache_entrycount(refcache); ++i) {
  852. struct packref *ref = git_sortedcache_entry(refcache, i);
  853. assert(ref);
  854. if ((error = packed_find_peel(backend, ref)) < 0)
  855. goto fail;
  856. if ((error = packed_write_ref(ref, &pack_file)) < 0)
  857. goto fail;
  858. }
  859. /* if we've written all the references properly, we can commit
  860. * the packfile to make the changes effective */
  861. if ((error = git_filebuf_commit(&pack_file)) < 0)
  862. goto fail;
  863. /* when and only when the packfile has been properly written,
  864. * we can go ahead and remove the loose refs */
  865. if ((error = packed_remove_loose(backend)) < 0)
  866. goto fail;
  867. git_sortedcache_updated(refcache);
  868. git_sortedcache_wunlock(refcache);
  869. /* we're good now */
  870. return 0;
  871. fail:
  872. git_filebuf_cleanup(&pack_file);
  873. git_sortedcache_wunlock(refcache);
  874. return error;
  875. }
  876. static int packed_delete(refdb_fs_backend *backend, const char *ref_name)
  877. {
  878. size_t pack_pos;
  879. int error, found = 0;
  880. if ((error = packed_reload(backend)) < 0)
  881. goto cleanup;
  882. if ((error = git_sortedcache_wlock(backend->refcache)) < 0)
  883. goto cleanup;
  884. /* If a packed reference exists, remove it from the packfile and repack if necessary */
  885. error = git_sortedcache_lookup_index(&pack_pos, backend->refcache, ref_name);
  886. if (error == 0) {
  887. error = git_sortedcache_remove(backend->refcache, pack_pos);
  888. found = 1;
  889. }
  890. if (error == GIT_ENOTFOUND)
  891. error = 0;
  892. git_sortedcache_wunlock(backend->refcache);
  893. if (found)
  894. error = packed_write(backend);
  895. cleanup:
  896. return error;
  897. }
  898. static int reflog_append(refdb_fs_backend *backend, const git_reference *ref, const git_oid *old, const git_oid *new, const git_signature *author, const char *message);
  899. static int has_reflog(git_repository *repo, const char *name);
  900. static int should_write_reflog(int *write, git_repository *repo, const char *name)
  901. {
  902. int error, logall;
  903. error = git_repository__configmap_lookup(&logall, repo, GIT_CONFIGMAP_LOGALLREFUPDATES);
  904. if (error < 0)
  905. return error;
  906. /* Defaults to the opposite of the repo being bare */
  907. if (logall == GIT_LOGALLREFUPDATES_UNSET)
  908. logall = !git_repository_is_bare(repo);
  909. *write = 0;
  910. switch (logall) {
  911. case GIT_LOGALLREFUPDATES_FALSE:
  912. *write = 0;
  913. break;
  914. case GIT_LOGALLREFUPDATES_TRUE:
  915. /* Only write if it already has a log,
  916. * or if it's under heads/, remotes/ or notes/
  917. */
  918. *write = has_reflog(repo, name) ||
  919. !git__prefixcmp(name, GIT_REFS_HEADS_DIR) ||
  920. !git__strcmp(name, GIT_HEAD_FILE) ||
  921. !git__prefixcmp(name, GIT_REFS_REMOTES_DIR) ||
  922. !git__prefixcmp(name, GIT_REFS_NOTES_DIR);
  923. break;
  924. case GIT_LOGALLREFUPDATES_ALWAYS:
  925. *write = 1;
  926. break;
  927. }
  928. return 0;
  929. }
  930. static int cmp_old_ref(int *cmp, git_refdb_backend *backend, const char *name,
  931. const git_oid *old_id, const char *old_target)
  932. {
  933. int error = 0;
  934. git_reference *old_ref = NULL;
  935. *cmp = 0;
  936. /* It "matches" if there is no old value to compare against */
  937. if (!old_id && !old_target)
  938. return 0;
  939. if ((error = refdb_fs_backend__lookup(&old_ref, backend, name)) < 0)
  940. goto out;
  941. /* If the types don't match, there's no way the values do */
  942. if (old_id && old_ref->type != GIT_REFERENCE_DIRECT) {
  943. *cmp = -1;
  944. goto out;
  945. }
  946. if (old_target && old_ref->type != GIT_REFERENCE_SYMBOLIC) {
  947. *cmp = 1;
  948. goto out;
  949. }
  950. if (old_id && old_ref->type == GIT_REFERENCE_DIRECT)
  951. *cmp = git_oid_cmp(old_id, &old_ref->target.oid);
  952. if (old_target && old_ref->type == GIT_REFERENCE_SYMBOLIC)
  953. *cmp = git__strcmp(old_target, old_ref->target.symbolic);
  954. out:
  955. git_reference_free(old_ref);
  956. return error;
  957. }
  958. /*
  959. * The git.git comment regarding this, for your viewing pleasure:
  960. *
  961. * Special hack: If a branch is updated directly and HEAD
  962. * points to it (may happen on the remote side of a push
  963. * for example) then logically the HEAD reflog should be
  964. * updated too.
  965. * A generic solution implies reverse symref information,
  966. * but finding all symrefs pointing to the given branch
  967. * would be rather costly for this rare event (the direct
  968. * update of a branch) to be worth it. So let's cheat and
  969. * check with HEAD only which should cover 99% of all usage
  970. * scenarios (even 100% of the default ones).
  971. */
  972. static int maybe_append_head(refdb_fs_backend *backend, const git_reference *ref, const git_signature *who, const char *message)
  973. {
  974. int error;
  975. git_oid old_id;
  976. git_reference *tmp = NULL, *head = NULL, *peeled = NULL;
  977. const char *name;
  978. if (ref->type == GIT_REFERENCE_SYMBOLIC)
  979. return 0;
  980. /* if we can't resolve, we use {0}*40 as old id */
  981. if (git_reference_name_to_id(&old_id, backend->repo, ref->name) < 0)
  982. memset(&old_id, 0, sizeof(old_id));
  983. if ((error = git_reference_lookup(&head, backend->repo, GIT_HEAD_FILE)) < 0)
  984. return error;
  985. if (git_reference_type(head) == GIT_REFERENCE_DIRECT)
  986. goto cleanup;
  987. if ((error = git_reference_lookup(&tmp, backend->repo, GIT_HEAD_FILE)) < 0)
  988. goto cleanup;
  989. /* Go down the symref chain until we find the branch */
  990. while (git_reference_type(tmp) == GIT_REFERENCE_SYMBOLIC) {
  991. error = git_reference_lookup(&peeled, backend->repo, git_reference_symbolic_target(tmp));
  992. if (error < 0)
  993. break;
  994. git_reference_free(tmp);
  995. tmp = peeled;
  996. }
  997. if (error == GIT_ENOTFOUND) {
  998. error = 0;
  999. name = git_reference_symbolic_target(tmp);
  1000. } else if (error < 0) {
  1001. goto cleanup;
  1002. } else {
  1003. name = git_reference_name(tmp);
  1004. }
  1005. if (strcmp(name, ref->name))
  1006. goto cleanup;
  1007. error = reflog_append(backend, head, &old_id, git_reference_target(ref), who, message);
  1008. cleanup:
  1009. git_reference_free(tmp);
  1010. git_reference_free(head);
  1011. return error;
  1012. }
  1013. static int refdb_fs_backend__write(
  1014. git_refdb_backend *_backend,
  1015. const git_reference *ref,
  1016. int force,
  1017. const git_signature *who,
  1018. const char *message,
  1019. const git_oid *old_id,
  1020. const char *old_target)
  1021. {
  1022. refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
  1023. git_filebuf file = GIT_FILEBUF_INIT;
  1024. int error = 0;
  1025. assert(backend);
  1026. if ((error = reference_path_available(backend, ref->name, NULL, force)) < 0)
  1027. return error;
  1028. /* We need to perform the reflog append and old value check under the ref's lock */
  1029. if ((error = loose_lock(&file, backend, ref->name)) < 0)
  1030. return error;
  1031. return refdb_fs_backend__write_tail(_backend, ref, &file, true, old_id, old_target, who, message);
  1032. }
  1033. static int refdb_fs_backend__write_tail(
  1034. git_refdb_backend *_backend,
  1035. const git_reference *ref,
  1036. git_filebuf *file,
  1037. int update_reflog,
  1038. const git_oid *old_id,
  1039. const char *old_target,
  1040. const git_signature *who,
  1041. const char *message)
  1042. {
  1043. refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
  1044. int error = 0, cmp = 0, should_write;
  1045. const char *new_target = NULL;
  1046. const git_oid *new_id = NULL;
  1047. if ((error = cmp_old_ref(&cmp, _backend, ref->name, old_id, old_target)) < 0)
  1048. goto on_error;
  1049. if (cmp) {
  1050. git_error_set(GIT_ERROR_REFERENCE, "old reference value does not match");
  1051. error = GIT_EMODIFIED;
  1052. goto on_error;
  1053. }
  1054. if (ref->type == GIT_REFERENCE_SYMBOLIC)
  1055. new_target = ref->target.symbolic;
  1056. else
  1057. new_id = &ref->target.oid;
  1058. error = cmp_old_ref(&cmp, _backend, ref->name, new_id, new_target);
  1059. if (error < 0 && error != GIT_ENOTFOUND)
  1060. goto on_error;
  1061. /* Don't update if we have the same value */
  1062. if (!error && !cmp) {
  1063. error = 0;
  1064. goto on_error; /* not really error */
  1065. }
  1066. if (update_reflog) {
  1067. if ((error = should_write_reflog(&should_write, backend->repo, ref->name)) < 0)
  1068. goto on_error;
  1069. if (should_write) {
  1070. if ((error = reflog_append(backend, ref, NULL, NULL, who, message)) < 0)
  1071. goto on_error;
  1072. if ((error = maybe_append_head(backend, ref, who, message)) < 0)
  1073. goto on_error;
  1074. }
  1075. }
  1076. return loose_commit(file, ref);
  1077. on_error:
  1078. git_filebuf_cleanup(file);
  1079. return error;
  1080. }
  1081. static void refdb_fs_backend__prune_refs(
  1082. refdb_fs_backend *backend,
  1083. const char *ref_name,
  1084. const char *prefix)
  1085. {
  1086. git_buf relative_path = GIT_BUF_INIT;
  1087. git_buf base_path = GIT_BUF_INIT;
  1088. size_t commonlen;
  1089. assert(backend && ref_name);
  1090. if (git_buf_sets(&relative_path, ref_name) < 0)
  1091. goto cleanup;
  1092. git_path_squash_slashes(&relative_path);
  1093. if ((commonlen = git_path_common_dirlen("refs/heads/", git_buf_cstr(&relative_path))) == strlen("refs/heads/") ||
  1094. (commonlen = git_path_common_dirlen("refs/tags/", git_buf_cstr(&relative_path))) == strlen("refs/tags/") ||
  1095. (commonlen = git_path_common_dirlen("refs/remotes/", git_buf_cstr(&relative_path))) == strlen("refs/remotes/")) {
  1096. git_buf_truncate(&relative_path, commonlen);
  1097. if (prefix) {
  1098. if (git_buf_join3(&base_path, '/', backend->commonpath, prefix, git_buf_cstr(&relative_path)) < 0)
  1099. goto cleanup;
  1100. } else {
  1101. if (git_buf_joinpath(&base_path, backend->commonpath, git_buf_cstr(&relative_path)) < 0)
  1102. goto cleanup;
  1103. }
  1104. git_futils_rmdir_r(ref_name + commonlen, git_buf_cstr(&base_path), GIT_RMDIR_EMPTY_PARENTS | GIT_RMDIR_SKIP_ROOT);
  1105. }
  1106. cleanup:
  1107. git_buf_dispose(&relative_path);
  1108. git_buf_dispose(&base_path);
  1109. }
  1110. static int refdb_fs_backend__delete(
  1111. git_refdb_backend *_backend,
  1112. const char *ref_name,
  1113. const git_oid *old_id, const char *old_target)
  1114. {
  1115. refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
  1116. git_filebuf file = GIT_FILEBUF_INIT;
  1117. int error = 0;
  1118. assert(backend && ref_name);
  1119. if ((error = loose_lock(&file, backend, ref_name)) < 0)
  1120. return error;
  1121. if ((error = refdb_reflog_fs__delete(_backend, ref_name)) < 0) {
  1122. git_filebuf_cleanup(&file);
  1123. return error;
  1124. }
  1125. return refdb_fs_backend__delete_tail(_backend, &file, ref_name, old_id, old_target);
  1126. }
  1127. static int loose_delete(refdb_fs_backend *backend, const char *ref_name)
  1128. {
  1129. git_buf loose_path = GIT_BUF_INIT;
  1130. int error = 0;
  1131. if (git_buf_joinpath(&loose_path, backend->commonpath, ref_name) < 0)
  1132. return -1;
  1133. error = p_unlink(loose_path.ptr);
  1134. if (error < 0 && errno == ENOENT)
  1135. error = GIT_ENOTFOUND;
  1136. else if (error != 0)
  1137. error = -1;
  1138. git_buf_dispose(&loose_path);
  1139. return error;
  1140. }
  1141. static int refdb_fs_backend__delete_tail(
  1142. git_refdb_backend *_backend,
  1143. git_filebuf *file,
  1144. const char *ref_name,
  1145. const git_oid *old_id, const char *old_target)
  1146. {
  1147. refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
  1148. int error = 0, cmp = 0;
  1149. bool packed_deleted = 0;
  1150. error = cmp_old_ref(&cmp, _backend, ref_name, old_id, old_target);
  1151. if (error < 0)
  1152. goto cleanup;
  1153. if (cmp) {
  1154. git_error_set(GIT_ERROR_REFERENCE, "old reference value does not match");
  1155. error = GIT_EMODIFIED;
  1156. goto cleanup;
  1157. }
  1158. /*
  1159. * To ensure that an external observer will see either the current ref value
  1160. * (because the loose ref still exists), or a missing ref (after the packed-file is
  1161. * unlocked, there will be nothing left), we must ensure things happen in the
  1162. * following order:
  1163. *
  1164. * - the packed-ref file is locked and loaded, as well as a loose one, if it exists
  1165. * - we optimistically delete a packed ref, keeping track of whether it existed
  1166. * - we delete the loose ref, note that we have its .lock
  1167. * - the loose ref is "unlocked", then the packed-ref file is rewritten and unlocked
  1168. * - we should prune the path components if a loose ref was deleted
  1169. *
  1170. * Note that, because our packed backend doesn't expose its filesystem lock,
  1171. * we might not be able to guarantee that this is what actually happens (ie.
  1172. * as our current code never write packed-refs.lock, nothing stops observers
  1173. * from grabbing a "stale" value from there).
  1174. */
  1175. if ((error = packed_delete(backend, ref_name)) < 0 && error != GIT_ENOTFOUND)
  1176. goto cleanup;
  1177. if (error == 0)
  1178. packed_deleted = 1;
  1179. if ((error = loose_delete(backend, ref_name)) < 0 && error != GIT_ENOTFOUND)
  1180. goto cleanup;
  1181. if (error == GIT_ENOTFOUND) {
  1182. error = packed_deleted ? 0 : ref_error_notfound(ref_name);
  1183. goto cleanup;
  1184. }
  1185. cleanup:
  1186. git_filebuf_cleanup(file);
  1187. if (error == 0)
  1188. refdb_fs_backend__prune_refs(backend, ref_name, "");
  1189. return error;
  1190. }
  1191. static int refdb_reflog_fs__rename(git_refdb_backend *_backend, const char *old_name, const char *new_name);
  1192. static int refdb_fs_backend__rename(
  1193. git_reference **out,
  1194. git_refdb_backend *_backend,
  1195. const char *old_name,
  1196. const char *new_name,
  1197. int force,
  1198. const git_signature *who,
  1199. const char *message)
  1200. {
  1201. refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
  1202. git_reference *old, *new = NULL;
  1203. git_filebuf file = GIT_FILEBUF_INIT;
  1204. int error;
  1205. assert(backend);
  1206. if ((error = reference_path_available(
  1207. backend, new_name, old_name, force)) < 0 ||
  1208. (error = refdb_fs_backend__lookup(&old, _backend, old_name)) < 0)
  1209. return error;
  1210. if ((error = refdb_fs_backend__delete(_backend, old_name, NULL, NULL)) < 0) {
  1211. git_reference_free(old);
  1212. return error;
  1213. }
  1214. new = git_reference__realloc(&old, new_name);
  1215. if (!new) {
  1216. git_reference_free(old);
  1217. return -1;
  1218. }
  1219. if ((error = loose_lock(&file, backend, new->name)) < 0) {
  1220. git_reference_free(new);
  1221. return error;
  1222. }
  1223. /* Try to rename the refog; it's ok if the old doesn't exist */
  1224. error = refdb_reflog_fs__rename(_backend, old_name, new_name);
  1225. if (((error == 0) || (error == GIT_ENOTFOUND)) &&
  1226. ((error = reflog_append(backend, new, git_reference_target(new), NULL, who, message)) < 0)) {
  1227. git_reference_free(new);
  1228. git_filebuf_cleanup(&file);
  1229. return error;
  1230. }
  1231. if (error < 0) {
  1232. git_reference_free(new);
  1233. git_filebuf_cleanup(&file);
  1234. return error;
  1235. }
  1236. if ((error = loose_commit(&file, new)) < 0 || out == NULL) {
  1237. git_reference_free(new);
  1238. return error;
  1239. }
  1240. *out = new;
  1241. return 0;
  1242. }
  1243. static int refdb_fs_backend__compress(git_refdb_backend *_backend)
  1244. {
  1245. int error;
  1246. refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
  1247. assert(backend);
  1248. if ((error = packed_reload(backend)) < 0 || /* load the existing packfile */
  1249. (error = packed_loadloose(backend)) < 0 || /* add all the loose refs */
  1250. (error = packed_write(backend)) < 0) /* write back to disk */
  1251. return error;
  1252. return 0;
  1253. }
  1254. static void refdb_fs_backend__free(git_refdb_backend *_backend)
  1255. {
  1256. refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
  1257. assert(backend);
  1258. git_sortedcache_free(backend->refcache);
  1259. git__free(backend->gitpath);
  1260. git__free(backend->commonpath);
  1261. git__free(backend);
  1262. }
  1263. static char *setup_namespace(git_repository *repo, const char *in)
  1264. {
  1265. git_buf path = GIT_BUF_INIT;
  1266. char *parts, *start, *end, *out = NULL;
  1267. if (!in)
  1268. goto done;
  1269. git_buf_puts(&path, in);
  1270. /* if the repo is not namespaced, nothing else to do */
  1271. if (repo->namespace == NULL) {
  1272. out = git_buf_detach(&path);
  1273. goto done;
  1274. }
  1275. parts = end = git__strdup(repo->namespace);
  1276. if (parts == NULL)
  1277. goto done;
  1278. /*
  1279. * From `man gitnamespaces`:
  1280. * namespaces which include a / will expand to a hierarchy
  1281. * of namespaces; for example, GIT_NAMESPACE=foo/bar will store
  1282. * refs under refs/namespaces/foo/refs/namespaces/bar/
  1283. */
  1284. while ((start = git__strsep(&end, "/")) != NULL)
  1285. git_buf_printf(&path, "refs/namespaces/%s/", start);
  1286. git_buf_printf(&path, "refs/namespaces/%s/refs", end);
  1287. git__free(parts);
  1288. /* Make sure that the folder with the namespace exists */
  1289. if (git_futils_mkdir_relative(git_buf_cstr(&path), in, 0777,
  1290. GIT_MKDIR_PATH, NULL) < 0)
  1291. goto done;
  1292. /* Return root of the namespaced gitpath, i.e. without the trailing '/refs' */
  1293. git_buf_rtruncate_at_char(&path, '/');
  1294. out = git_buf_detach(&path);
  1295. done:
  1296. git_buf_dispose(&path);
  1297. return out;
  1298. }
  1299. static int reflog_alloc(git_reflog **reflog, const char *name)
  1300. {
  1301. git_reflog *log;
  1302. *reflog = NULL;
  1303. log = git__calloc(1, sizeof(git_reflog));
  1304. GIT_ERROR_CHECK_ALLOC(log);
  1305. log->ref_name = git__strdup(name);
  1306. GIT_ERROR_CHECK_ALLOC(log->ref_name);
  1307. if (git_vector_init(&log->entries, 0, NULL) < 0) {
  1308. git__free(log->ref_name);
  1309. git__free(log);
  1310. return -1;
  1311. }
  1312. *reflog = log;
  1313. return 0;
  1314. }
  1315. static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size)
  1316. {
  1317. git_parse_ctx parser = GIT_PARSE_CTX_INIT;
  1318. if ((git_parse_ctx_init(&parser, buf, buf_size)) < 0)
  1319. return -1;
  1320. for (; parser.remain_len; git_parse_advance_line(&parser)) {
  1321. git_reflog_entry *entry;
  1322. const char *sig;
  1323. char c;
  1324. entry = git__calloc(1, sizeof(*entry));
  1325. GIT_ERROR_CHECK_ALLOC(entry);
  1326. entry->committer = git__calloc(1, sizeof(*entry->committer));
  1327. GIT_ERROR_CHECK_ALLOC(entry->committer);
  1328. if (git_parse_advance_oid(&entry->oid_old, &parser) < 0 ||
  1329. git_parse_advance_expected(&parser, " ", 1) < 0 ||
  1330. git_parse_advance_oid(&entry->oid_cur, &parser) < 0)
  1331. goto next;
  1332. sig = parser.line;
  1333. while (git_parse_peek(&c, &parser, 0) == 0 && c != '\t' && c != '\n')
  1334. git_parse_advance_chars(&parser, 1);
  1335. if (git_signature__parse(entry->committer, &sig, parser.line, NULL, 0) < 0)
  1336. goto next;
  1337. if (c == '\t') {
  1338. size_t len;
  1339. git_parse_advance_chars(&parser, 1);
  1340. len = parser.line_len;
  1341. if (parser.line[len - 1] == '\n')
  1342. len--;
  1343. entry->msg = git__strndup(parser.line, len);
  1344. GIT_ERROR_CHECK_ALLOC(entry->msg);
  1345. }
  1346. if ((git_vector_insert(&log->entries, entry)) < 0) {
  1347. git_reflog_entry__free(entry);
  1348. return -1;
  1349. }
  1350. continue;
  1351. next:
  1352. git_reflog_entry__free(entry);
  1353. }
  1354. return 0;
  1355. }
  1356. static int create_new_reflog_file(const char *filepath)
  1357. {
  1358. int fd, error;
  1359. if ((error = git_futils_mkpath2file(filepath, GIT_REFLOG_DIR_MODE)) < 0)
  1360. return error;
  1361. if ((fd = p_open(filepath,
  1362. O_WRONLY | O_CREAT,
  1363. GIT_REFLOG_FILE_MODE)) < 0)
  1364. return -1;
  1365. return p_close(fd);
  1366. }
  1367. GIT_INLINE(int) retrieve_reflog_path(git_buf *path, git_repository *repo, const char *name)
  1368. {
  1369. if (strcmp(name, GIT_HEAD_FILE) == 0)
  1370. return git_buf_join3(path, '/', repo->gitdir, GIT_REFLOG_DIR, name);
  1371. return git_buf_join3(path, '/', repo->commondir, GIT_REFLOG_DIR, name);
  1372. }
  1373. static int refdb_reflog_fs__ensure_log(git_refdb_backend *_backend, const char *name)
  1374. {
  1375. refdb_fs_backend *backend;
  1376. git_repository *repo;
  1377. git_buf path = GIT_BUF_INIT;
  1378. int error;
  1379. assert(_backend && name);
  1380. backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
  1381. repo = backend->repo;
  1382. if ((error = retrieve_reflog_path(&path, repo, name)) < 0)
  1383. return error;
  1384. error = create_new_reflog_file(git_buf_cstr(&path));
  1385. git_buf_dispose(&path);
  1386. return error;
  1387. }
  1388. static int has_reflog(git_repository *repo, const char *name)
  1389. {
  1390. int ret = 0;
  1391. git_buf path = GIT_BUF_INIT;
  1392. if (retrieve_reflog_path(&path, repo, name) < 0)
  1393. goto cleanup;
  1394. ret = git_path_isfile(git_buf_cstr(&path));
  1395. cleanup:
  1396. git_buf_dispose(&path);
  1397. return ret;
  1398. }
  1399. static int refdb_reflog_fs__has_log(git_refdb_backend *_backend, const char *name)
  1400. {
  1401. refdb_fs_backend *backend;
  1402. assert(_backend && name);
  1403. backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
  1404. return has_reflog(backend->repo, name);
  1405. }
  1406. static int refdb_reflog_fs__read(git_reflog **out, git_refdb_backend *_backend, const char *name)
  1407. {
  1408. int error = -1;
  1409. git_buf log_path = GIT_BUF_INIT;
  1410. git_buf log_file = GIT_BUF_INIT;
  1411. git_reflog *log = NULL;
  1412. git_repository *repo;
  1413. refdb_fs_backend *backend;
  1414. assert(out && _backend && name);
  1415. backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
  1416. repo = backend->repo;
  1417. if (reflog_alloc(&log, name) < 0)
  1418. return -1;
  1419. if (retrieve_reflog_path(&log_path, repo, name) < 0)
  1420. goto cleanup;
  1421. error = git_futils_readbuffer(&log_file, git_buf_cstr(&log_path));
  1422. if (error < 0 && error != GIT_ENOTFOUND)
  1423. goto cleanup;
  1424. if ((error == GIT_ENOTFOUND) &&
  1425. ((error = create_new_reflog_file(git_buf_cstr(&log_path))) < 0))
  1426. goto cleanup;
  1427. if ((error = reflog_parse(log,
  1428. git_buf_cstr(&log_file), git_buf_len(&log_file))) < 0)
  1429. goto cleanup;
  1430. *out = log;
  1431. goto success;
  1432. cleanup:
  1433. git_reflog_free(log);
  1434. success:
  1435. git_buf_dispose(&log_file);
  1436. git_buf_dispose(&log_path);
  1437. return error;
  1438. }
  1439. static int serialize_reflog_entry(
  1440. git_buf *buf,
  1441. const git_oid *oid_old,
  1442. const git_oid *oid_new,
  1443. const git_signature *committer,
  1444. const char *msg)
  1445. {
  1446. char raw_old[GIT_OID_HEXSZ+1];
  1447. char raw_new[GIT_OID_HEXSZ+1];
  1448. git_oid_tostr(raw_old, GIT_OID_HEXSZ+1, oid_old);
  1449. git_oid_tostr(raw_new, GIT_OID_HEXSZ+1, oid_new);
  1450. git_buf_clear(buf);
  1451. git_buf_puts(buf, raw_old);
  1452. git_buf_putc(buf, ' ');
  1453. git_buf_puts(buf, raw_new);
  1454. git_signature__writebuf(buf, " ", committer);
  1455. /* drop trailing LF */
  1456. git_buf_rtrim(buf);
  1457. if (msg) {
  1458. size_t i;
  1459. git_buf_putc(buf, '\t');
  1460. git_buf_puts(buf, msg);
  1461. for (i = 0; i < buf->size - 2; i++)
  1462. if (buf->ptr[i] == '\n')
  1463. buf->ptr[i] = ' ';
  1464. git_buf_rtrim(buf);
  1465. }
  1466. git_buf_putc(buf, '\n');
  1467. return git_buf_oom(buf);
  1468. }
  1469. static int lock_reflog(git_filebuf *file, refdb_fs_backend *backend, const char *refname)
  1470. {
  1471. git_repository *repo;
  1472. git_buf log_path = GIT_BUF_INIT;
  1473. int error;
  1474. repo = backend->repo;
  1475. if (!git_path_isvalid(backend->repo, refname, 0, GIT_PATH_REJECT_FILESYSTEM_DEFAULTS)) {
  1476. git_error_set(GIT_ERROR_INVALID, "invalid reference name '%s'", refname);
  1477. return GIT_EINVALIDSPEC;
  1478. }
  1479. if (retrieve_reflog_path(&log_path, repo, refname) < 0)
  1480. return -1;
  1481. if (!git_path_isfile(git_buf_cstr(&log_path))) {
  1482. git_error_set(GIT_ERROR_INVALID,
  1483. "log file for reference '%s' doesn't exist", refname);
  1484. error = -1;
  1485. goto cleanup;
  1486. }
  1487. error = git_filebuf_open(file, git_buf_cstr(&log_path), 0, GIT_REFLOG_FILE_MODE);
  1488. cleanup:
  1489. git_buf_dispose(&log_path);
  1490. return error;
  1491. }
  1492. static int refdb_reflog_fs__write(git_refdb_backend *_backend, git_reflog *reflog)
  1493. {
  1494. int error = -1;
  1495. unsigned int i;
  1496. git_reflog_entry *entry;
  1497. refdb_fs_backend *backend;
  1498. git_buf log = GIT_BUF_INIT;
  1499. git_filebuf fbuf = GIT_FILEBUF_INIT;
  1500. assert(_backend && reflog);
  1501. backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
  1502. if ((error = lock_reflog(&fbuf, backend, reflog->ref_name)) < 0)
  1503. return -1;
  1504. git_vector_foreach(&reflog->entries, i, entry) {
  1505. if (serialize_reflog_entry(&log, &(entry->oid_old), &(entry->oid_cur), entry->committer, entry->msg) < 0)
  1506. goto cleanup;
  1507. if ((error = git_filebuf_write(&fbuf, log.ptr, log.size)) < 0)
  1508. goto cleanup;
  1509. }
  1510. error = git_filebuf_commit(&fbuf);
  1511. goto success;
  1512. cleanup:
  1513. git_filebuf_cleanup(&fbuf);
  1514. success:
  1515. git_buf_dispose(&log);
  1516. return error;
  1517. }
  1518. /* Append to the reflog, must be called under reference lock */
  1519. static int reflog_append(refdb_fs_backend *backend, const git_reference *ref, const git_oid *old, const git_oid *new, const git_signature *who, const char *message)
  1520. {
  1521. int error, is_symbolic, open_flags;
  1522. git_oid old_id = {{0}}, new_id = {{0}};
  1523. git_buf buf = GIT_BUF_INIT, path = GIT_BUF_INIT;
  1524. git_repository *repo = backend->repo;
  1525. is_symbolic = ref->type == GIT_REFERENCE_SYMBOLIC;
  1526. /* "normal" symbolic updates do not write */
  1527. if (is_symbolic &&
  1528. strcmp(ref->name, GIT_HEAD_FILE) &&
  1529. !(old && new))
  1530. return 0;
  1531. /* From here on is_symbolic also means that it's HEAD */
  1532. if (old) {
  1533. git_oid_cpy(&old_id, old);
  1534. } else {
  1535. error = git_reference_name_to_id(&old_id, repo, ref->name);
  1536. if (error < 0 && error != GIT_ENOTFOUND)
  1537. return error;
  1538. }
  1539. if (new) {
  1540. git_oid_cpy(&new_id, new);
  1541. } else {
  1542. if (!is_symbolic) {
  1543. git_oid_cpy(&new_id, git_reference_target(ref));
  1544. } else {
  1545. error = git_reference_name_to_id(&new_id, repo, git_reference_symbolic_target(ref));
  1546. if (error < 0 && error != GIT_ENOTFOUND)
  1547. return error;
  1548. /* detaching HEAD does not create an entry */
  1549. if (error == GIT_ENOTFOUND)
  1550. return 0;
  1551. git_error_clear();
  1552. }
  1553. }
  1554. if ((error = serialize_reflog_entry(&buf, &old_id, &new_id, who, message)) < 0)
  1555. goto cleanup;
  1556. if ((error = retrieve_reflog_path(&path, repo, ref->name)) < 0)
  1557. goto cleanup;
  1558. if (((error = git_futils_mkpath2file(git_buf_cstr(&path), 0777)) < 0) &&
  1559. (error != GIT_EEXISTS)) {
  1560. goto cleanup;
  1561. }
  1562. /* If the new branch matches part of the namespace of a previously deleted branch,
  1563. * there maybe an obsolete/unused directory (or directory hierarchy) in the way.
  1564. */
  1565. if (git_path_isdir(git_buf_cstr(&path))) {
  1566. if ((error = git_futils_rmdir_r(git_buf_cstr(&path), NULL, GIT_RMDIR_SKIP_NONEMPTY)) < 0) {
  1567. if (error == GIT_ENOTFOUND)
  1568. error = 0;
  1569. } else if (git_path_isdir(git_buf_cstr(&path))) {
  1570. git_error_set(GIT_ERROR_REFERENCE, "cannot create reflog at '%s', there are reflogs beneath that folder",
  1571. ref->name);
  1572. error = GIT_EDIRECTORY;
  1573. }
  1574. if (error != 0)
  1575. goto cleanup;
  1576. }
  1577. open_flags = O_WRONLY | O_CREAT | O_APPEND;
  1578. if (backend->fsync)
  1579. open_flags |= O_FSYNC;
  1580. error = git_futils_writebuffer(&buf, git_buf_cstr(&path), open_flags, GIT_REFLOG_FILE_MODE);
  1581. cleanup:
  1582. git_buf_dispose(&buf);
  1583. git_buf_dispose(&path);
  1584. return error;
  1585. }
  1586. static int refdb_reflog_fs__rename(git_refdb_backend *_backend, const char *old_name, const char *new_name)
  1587. {
  1588. int error = 0, fd;
  1589. git_buf old_path = GIT_BUF_INIT;
  1590. git_buf new_path = GIT_BUF_INIT;
  1591. git_buf temp_path = GIT_BUF_INIT;
  1592. git_buf normalized = GIT_BUF_INIT;
  1593. git_repository *repo;
  1594. refdb_fs_backend *backend;
  1595. assert(_backend && old_name && new_name);
  1596. backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
  1597. repo = backend->repo;
  1598. if ((error = git_reference__normalize_name(
  1599. &normalized, new_name, GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL)) < 0)
  1600. return error;
  1601. if (git_buf_joinpath(&temp_path, repo->gitdir, GIT_REFLOG_DIR) < 0)
  1602. return -1;
  1603. if (git_buf_joinpath(&old_path, git_buf_cstr(&temp_path), old_name) < 0)
  1604. return -1;
  1605. if (git_buf_joinpath(&new_path, git_buf_cstr(&temp_path), git_buf_cstr(&normalized)) < 0)
  1606. return -1;
  1607. if (!git_path_exists(git_buf_cstr(&old_path))) {
  1608. error = GIT_ENOTFOUND;
  1609. goto cleanup;
  1610. }
  1611. /*
  1612. * Move the reflog to a temporary place. This two-phase renaming is required
  1613. * in order to cope with funny renaming use cases when one tries to move a reference
  1614. * to a partially colliding namespace:
  1615. * - a/b -> a/b/c
  1616. * - a/b/c/d -> a/b/c
  1617. */
  1618. if (git_buf_joinpath(&temp_path, git_buf_cstr(&temp_path), "temp_reflog") < 0)
  1619. return -1;
  1620. if ((fd = git_futils_mktmp(&temp_path, git_buf_cstr(&temp_path), GIT_REFLOG_FILE_MODE)) < 0) {
  1621. error = -1;
  1622. goto cleanup;
  1623. }
  1624. p_close(fd);
  1625. if (p_rename(git_buf_cstr(&old_path), git_buf_cstr(&temp_path)) < 0) {
  1626. git_error_set(GIT_ERROR_OS, "failed to rename reflog for %s", new_name);
  1627. error = -1;
  1628. goto cleanup;
  1629. }
  1630. if (git_path_isdir(git_buf_cstr(&new_path)) &&
  1631. (git_futils_rmdir_r(git_buf_cstr(&new_path), NULL, GIT_RMDIR_SKIP_NONEMPTY) < 0)) {
  1632. error = -1;
  1633. goto cleanup;
  1634. }
  1635. if (git_futils_mkpath2file(git_buf_cstr(&new_path), GIT_REFLOG_DIR_MODE) < 0) {
  1636. error = -1;
  1637. goto cleanup;
  1638. }
  1639. if (p_rename(git_buf_cstr(&temp_path), git_buf_cstr(&new_path)) < 0) {
  1640. git_error_set(GIT_ERROR_OS, "failed to rename reflog for %s", new_name);
  1641. error = -1;
  1642. }
  1643. cleanup:
  1644. git_buf_dispose(&temp_path);
  1645. git_buf_dispose(&old_path);
  1646. git_buf_dispose(&new_path);
  1647. git_buf_dispose(&normalized);
  1648. return error;
  1649. }
  1650. static int refdb_reflog_fs__delete(git_refdb_backend *_backend, const char *name)
  1651. {
  1652. refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
  1653. git_buf path = GIT_BUF_INIT;
  1654. int error;
  1655. assert(_backend && name);
  1656. if ((error = retrieve_reflog_path(&path, backend->repo, name)) < 0)
  1657. goto out;
  1658. if (!git_path_exists(path.ptr))
  1659. goto out;
  1660. if ((error = p_unlink(path.ptr)) < 0)
  1661. goto out;
  1662. refdb_fs_backend__prune_refs(backend, name, GIT_REFLOG_DIR);
  1663. out:
  1664. git_buf_dispose(&path);
  1665. return error;
  1666. }
  1667. int git_refdb_backend_fs(
  1668. git_refdb_backend **backend_out,
  1669. git_repository *repository)
  1670. {
  1671. int t = 0;
  1672. git_buf gitpath = GIT_BUF_INIT;
  1673. refdb_fs_backend *backend;
  1674. backend = git__calloc(1, sizeof(refdb_fs_backend));
  1675. GIT_ERROR_CHECK_ALLOC(backend);
  1676. if (git_refdb_init_backend(&backend->parent, GIT_REFDB_BACKEND_VERSION) < 0)
  1677. goto fail;
  1678. backend->repo = repository;
  1679. if (repository->gitdir) {
  1680. backend->gitpath = setup_namespace(repository, repository->gitdir);
  1681. if (backend->gitpath == NULL)
  1682. goto fail;
  1683. }
  1684. if (repository->commondir) {
  1685. backend->commonpath = setup_namespace(repository, repository->commondir);
  1686. if (backend->commonpath == NULL)
  1687. goto fail;
  1688. }
  1689. if (git_buf_joinpath(&gitpath, backend->commonpath, GIT_PACKEDREFS_FILE) < 0 ||
  1690. git_sortedcache_new(
  1691. &backend->refcache, offsetof(struct packref, name),
  1692. NULL, NULL, packref_cmp, git_buf_cstr(&gitpath)) < 0)
  1693. goto fail;
  1694. git_buf_dispose(&gitpath);
  1695. if (!git_repository__configmap_lookup(&t, backend->repo, GIT_CONFIGMAP_IGNORECASE) && t) {
  1696. backend->iterator_flags |= GIT_ITERATOR_IGNORE_CASE;
  1697. backend->direach_flags |= GIT_PATH_DIR_IGNORE_CASE;
  1698. }
  1699. if (!git_repository__configmap_lookup(&t, backend->repo, GIT_CONFIGMAP_PRECOMPOSE) && t) {
  1700. backend->iterator_flags |= GIT_ITERATOR_PRECOMPOSE_UNICODE;
  1701. backend->direach_flags |= GIT_PATH_DIR_PRECOMPOSE_UNICODE;
  1702. }
  1703. if ((!git_repository__configmap_lookup(&t, backend->repo, GIT_CONFIGMAP_FSYNCOBJECTFILES) && t) ||
  1704. git_repository__fsync_gitdir)
  1705. backend->fsync = 1;
  1706. backend->iterator_flags |= GIT_ITERATOR_DESCEND_SYMLINKS;
  1707. backend->parent.exists = &refdb_fs_backend__exists;
  1708. backend->parent.lookup = &refdb_fs_backend__lookup;
  1709. backend->parent.iterator = &refdb_fs_backend__iterator;
  1710. backend->parent.write = &refdb_fs_backend__write;
  1711. backend->parent.del = &refdb_fs_backend__delete;
  1712. backend->parent.rename = &refdb_fs_backend__rename;
  1713. backend->parent.compress = &refdb_fs_backend__compress;
  1714. backend->parent.lock = &refdb_fs_backend__lock;
  1715. backend->parent.unlock = &refdb_fs_backend__unlock;
  1716. backend->parent.has_log = &refdb_reflog_fs__has_log;
  1717. backend->parent.ensure_log = &refdb_reflog_fs__ensure_log;
  1718. backend->parent.free = &refdb_fs_backend__free;
  1719. backend->parent.reflog_read = &refdb_reflog_fs__read;
  1720. backend->parent.reflog_write = &refdb_reflog_fs__write;
  1721. backend->parent.reflog_rename = &refdb_reflog_fs__rename;
  1722. backend->parent.reflog_delete = &refdb_reflog_fs__delete;
  1723. *backend_out = (git_refdb_backend *)backend;
  1724. return 0;
  1725. fail:
  1726. git_buf_dispose(&gitpath);
  1727. git__free(backend->gitpath);
  1728. git__free(backend->commonpath);
  1729. git__free(backend);
  1730. return -1;
  1731. }