Skip to content

Commit c5f3da9

Browse files
committed
repository: use git_repository_item_path
The recent introduction of the commondir variable of a repository requires callers to distinguish whether their files are part of the dot-git directory or the common directory shared between multpile worktrees. In order to take the burden from callers and unify knowledge on which files reside where, the `git_repository_item_path` function has been introduced which encapsulate this knowledge. Modify most existing callers of `git_repository_path` to use `git_repository_item_path` instead, thus making them implicitly aware of the common directory.
1 parent cb3269c commit c5f3da9

File tree

9 files changed

+48
-27
lines changed

9 files changed

+48
-27
lines changed

src/attr.c

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ static int attr_setup(git_repository *repo, git_attr_session *attr_session)
292292
int error = 0;
293293
const char *workdir = git_repository_workdir(repo);
294294
git_index *idx = NULL;
295-
git_buf sys = GIT_BUF_INIT;
295+
git_buf path = GIT_BUF_INIT;
296296

297297
if (attr_session && attr_session->init_setup)
298298
return 0;
@@ -304,40 +304,45 @@ static int attr_setup(git_repository *repo, git_attr_session *attr_session)
304304
* definitions will be available for later file parsing
305305
*/
306306

307-
error = system_attr_file(&sys, attr_session);
307+
error = system_attr_file(&path, attr_session);
308308

309309
if (error == 0)
310310
error = preload_attr_file(
311-
repo, attr_session, GIT_ATTR_FILE__FROM_FILE, NULL, sys.ptr);
311+
repo, attr_session, GIT_ATTR_FILE__FROM_FILE, NULL, path.ptr);
312312

313313
if (error != GIT_ENOTFOUND)
314-
return error;
315-
316-
git_buf_free(&sys);
314+
goto out;
317315

318316
if ((error = preload_attr_file(
319317
repo, attr_session, GIT_ATTR_FILE__FROM_FILE,
320318
NULL, git_repository_attr_cache(repo)->cfg_attr_file)) < 0)
321-
return error;
319+
goto out;
320+
321+
if ((error = git_repository_item_path(&path,
322+
repo, GIT_REPOSITORY_ITEM_INFO)) < 0)
323+
goto out;
322324

323325
if ((error = preload_attr_file(
324326
repo, attr_session, GIT_ATTR_FILE__FROM_FILE,
325-
git_repository_path(repo), GIT_ATTR_FILE_INREPO)) < 0)
326-
return error;
327+
path.ptr, GIT_ATTR_FILE_INREPO)) < 0)
328+
goto out;
327329

328330
if (workdir != NULL &&
329331
(error = preload_attr_file(
330332
repo, attr_session, GIT_ATTR_FILE__FROM_FILE, workdir, GIT_ATTR_FILE)) < 0)
331-
return error;
333+
goto out;
332334

333335
if ((error = git_repository_index__weakptr(&idx, repo)) < 0 ||
334336
(error = preload_attr_file(
335337
repo, attr_session, GIT_ATTR_FILE__FROM_INDEX, NULL, GIT_ATTR_FILE)) < 0)
336-
return error;
338+
goto out;
337339

338340
if (attr_session)
339341
attr_session->init_setup = 1;
340342

343+
out:
344+
git_buf_free(&path);
345+
341346
return error;
342347
}
343348

@@ -472,7 +477,7 @@ static int collect_attr_files(
472477
git_vector *files)
473478
{
474479
int error = 0;
475-
git_buf dir = GIT_BUF_INIT;
480+
git_buf dir = GIT_BUF_INIT, attrfile = GIT_BUF_INIT;
476481
const char *workdir = git_repository_workdir(repo);
477482
attr_walk_up_info info = { NULL };
478483

@@ -494,9 +499,13 @@ static int collect_attr_files(
494499
* - $GIT_PREFIX/etc/gitattributes
495500
*/
496501

502+
error = git_repository_item_path(&attrfile, repo, GIT_REPOSITORY_ITEM_INFO);
503+
if (error < 0)
504+
goto cleanup;
505+
497506
error = push_attr_file(
498507
repo, attr_session, files, GIT_ATTR_FILE__FROM_FILE,
499-
git_repository_path(repo), GIT_ATTR_FILE_INREPO);
508+
attrfile.ptr, GIT_ATTR_FILE_INREPO);
500509
if (error < 0)
501510
goto cleanup;
502511

@@ -538,6 +547,7 @@ static int collect_attr_files(
538547
cleanup:
539548
if (error < 0)
540549
release_attr_files(files);
550+
git_buf_free(&attrfile);
541551
git_buf_free(&dir);
542552

543553
return error;

src/attr_file.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include "fileops.h"
1616

1717
#define GIT_ATTR_FILE ".gitattributes"
18-
#define GIT_ATTR_FILE_INREPO "info/attributes"
18+
#define GIT_ATTR_FILE_INREPO "attributes"
1919
#define GIT_ATTR_FILE_SYSTEM "gitattributes"
2020
#define GIT_ATTR_FILE_XDG "attributes"
2121

src/blob.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,8 @@ int git_blob_create_fromstream(git_writestream **out, git_repository *repo, cons
326326
stream->parent.close = blob_writestream_close;
327327
stream->parent.free = blob_writestream_free;
328328

329-
if ((error = git_buf_joinpath(&path,
330-
git_repository_path(repo), GIT_OBJECTS_DIR "streamed")) < 0)
329+
if ((error = git_repository_item_path(&path, repo, GIT_REPOSITORY_ITEM_OBJECTS)) < 0
330+
|| (error = git_buf_joinpath(&path, path.ptr, "streamed")) < 0)
331331
goto cleanup;
332332

333333
if ((error = git_filebuf_open_withsize(&stream->fbuf, git_buf_cstr(&path), GIT_FILEBUF_TEMPORARY,

src/clone.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -513,9 +513,8 @@ static int clone_local_into(git_repository *repo, git_remote *remote, const git_
513513
return error;
514514
}
515515

516-
git_buf_joinpath(&src_odb, git_repository_path(src), GIT_OBJECTS_DIR);
517-
git_buf_joinpath(&dst_odb, git_repository_path(repo), GIT_OBJECTS_DIR);
518-
if (git_buf_oom(&src_odb) || git_buf_oom(&dst_odb)) {
516+
if (git_repository_item_path(&src_odb, src, GIT_REPOSITORY_ITEM_OBJECTS) < 0
517+
|| git_repository_item_path(&dst_odb, repo, GIT_REPOSITORY_ITEM_OBJECTS) < 0) {
519518
error = -1;
520519
goto cleanup;
521520
}

src/ignore.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ int git_ignore__for_path(
277277
{
278278
int error = 0;
279279
const char *workdir = git_repository_workdir(repo);
280+
git_buf infopath = GIT_BUF_INIT;
280281

281282
assert(repo && ignores && path);
282283

@@ -322,10 +323,14 @@ int git_ignore__for_path(
322323
goto cleanup;
323324
}
324325

326+
if ((error = git_repository_item_path(&infopath,
327+
repo, GIT_REPOSITORY_ITEM_INFO)) < 0)
328+
goto cleanup;
329+
325330
/* load .git/info/exclude */
326331
error = push_ignore_file(
327332
ignores, &ignores->ign_global,
328-
git_repository_path(repo), GIT_IGNORE_FILE_INREPO);
333+
infopath.ptr, GIT_IGNORE_FILE_INREPO);
329334
if (error < 0)
330335
goto cleanup;
331336

@@ -336,6 +341,7 @@ int git_ignore__for_path(
336341
git_repository_attr_cache(repo)->cfg_excl_file);
337342

338343
cleanup:
344+
git_buf_free(&infopath);
339345
if (error < 0)
340346
git_ignore__free(ignores);
341347

src/ignore.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "attr_file.h"
1313

1414
#define GIT_IGNORE_FILE ".gitignore"
15-
#define GIT_IGNORE_FILE_INREPO "info/exclude"
15+
#define GIT_IGNORE_FILE_INREPO "exclude"
1616
#define GIT_IGNORE_FILE_XDG "ignore"
1717

1818
/* The git_ignores structure maintains three sets of ignores:

src/repository.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,8 @@ int git_repository_odb__weakptr(git_odb **out, git_repository *repo)
992992
git_buf odb_path = GIT_BUF_INIT;
993993
git_odb *odb;
994994

995-
if ((error = git_buf_joinpath(&odb_path, repo->commondir, GIT_OBJECTS_DIR)) < 0)
995+
if ((error = git_repository_item_path(&odb_path, repo,
996+
GIT_REPOSITORY_ITEM_OBJECTS)) < 0)
996997
return error;
997998

998999
error = git_odb_open(&odb, odb_path.ptr);

src/submodule.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -616,8 +616,10 @@ static int submodule_repo_init(
616616
* Old style: sub-repo goes directly into repo/<name>/.git/
617617
*/
618618
if (use_gitlink) {
619-
error = git_buf_join3(
620-
&repodir, '/', git_repository_path(parent_repo), "modules", path);
619+
error = git_repository_item_path(&repodir, parent_repo, GIT_REPOSITORY_ITEM_MODULES);
620+
if (error < 0)
621+
goto cleanup;
622+
error = git_buf_joinpath(&repodir, repodir.ptr, path);
621623
if (error < 0)
622624
goto cleanup;
623625

@@ -1084,8 +1086,10 @@ static int submodule_repo_create(
10841086
* <repo-dir>/modules/<name>/ with a gitlink in the
10851087
* sub-repo workdir directory to that repository.
10861088
*/
1087-
error = git_buf_join3(
1088-
&repodir, '/', git_repository_path(parent_repo), "modules", path);
1089+
error = git_repository_item_path(&repodir, parent_repo, GIT_REPOSITORY_ITEM_MODULES);
1090+
if (error < 0)
1091+
goto cleanup;
1092+
error = git_buf_joinpath(&repodir, repodir.ptr, path);
10891093
if (error < 0)
10901094
goto cleanup;
10911095

src/transports/local.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,8 @@ static int local_push(
375375
goto on_error;
376376
}
377377

378-
if ((error = git_buf_joinpath(&odb_path, git_repository_path(remote_repo), "objects/pack")) < 0)
378+
if ((error = git_repository_item_path(&odb_path, remote_repo, GIT_REPOSITORY_ITEM_OBJECTS)) < 0
379+
|| (error = git_buf_joinpath(&odb_path, odb_path.ptr, "pack")) < 0)
379380
goto on_error;
380381

381382
error = git_packbuilder_write(push->pb, odb_path.ptr, 0, transfer_to_push_transfer, (void *) cbs);

0 commit comments

Comments
 (0)