Skip to content

Commit 5c87b5a

Browse files
authored
Merge pull request libgit2#5152 from csware/attr-system-attr-file
Fix Regression: attr: Correctly load system attr file (on Windows)
2 parents c4c1500 + c87abec commit 5c87b5a

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/attr.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ static int attr_setup(git_repository *repo, git_attr_session *attr_session)
333333
NULL, git_repository_attr_cache(repo)->cfg_attr_file)) < 0)
334334
goto out;
335335

336+
git_buf_clear(&path); /* git_repository_item_path expects an empty buffer, because it uses git_buf_set */
336337
if ((error = git_repository_item_path(&path, repo, GIT_REPOSITORY_ITEM_INFO)) < 0 ||
337338
(error = preload_attr_file(repo, attr_session, GIT_ATTR_FILE__FROM_FILE,
338339
path.ptr, GIT_ATTR_FILE_INREPO)) < 0) {

src/attr_file.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,7 @@ int git_attr_session__init(git_attr_session *session, git_repository *repo)
919919
{
920920
assert(repo);
921921

922+
memset(session, 0, sizeof(*session));
922923
session->key = git_atomic_inc(&repo->attr_session_key);
923924

924925
return 0;

tests/attr/repo.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "fileops.h"
33
#include "git2/attr.h"
44
#include "attr.h"
5+
#include "sysdir.h"
56

67
#include "attr_expect.h"
78
#include "git2/sys/repository.h"
@@ -17,6 +18,7 @@ void test_attr_repo__cleanup(void)
1718
{
1819
cl_git_sandbox_cleanup();
1920
g_repo = NULL;
21+
cl_sandbox_set_search_path_defaults();
2022
}
2123

2224
static struct attr_expected get_one_test_cases[] = {
@@ -376,3 +378,46 @@ void test_attr_repo__bare_repo_with_index(void)
376378
cl_assert(GIT_ATTR_IS_FALSE(values[2]));
377379
cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[3]));
378380
}
381+
382+
void test_attr_repo__sysdir(void)
383+
{
384+
git_buf sysdir = GIT_BUF_INIT;
385+
const char *value;
386+
387+
cl_git_pass(p_mkdir("system", 0777));
388+
cl_git_rewritefile("system/gitattributes", "file merge=foo");
389+
cl_git_pass(git_buf_joinpath(&sysdir, clar_sandbox_path(), "system"));
390+
cl_git_pass(git_sysdir_set(GIT_SYSDIR_SYSTEM, sysdir.ptr));
391+
g_repo = cl_git_sandbox_reopen();
392+
393+
cl_git_pass(git_attr_get(&value, g_repo, 0, "file", "merge"));
394+
cl_assert_equal_s(value, "foo");
395+
396+
cl_git_pass(p_unlink("system/gitattributes"));
397+
cl_git_pass(p_rmdir("system"));
398+
git_buf_dispose(&sysdir);
399+
}
400+
401+
void test_attr_repo__sysdir_with_session(void)
402+
{
403+
const char *values[2], *attrs[2] = { "foo", "bar" };
404+
git_buf sysdir = GIT_BUF_INIT;
405+
git_attr_session session;
406+
407+
cl_git_pass(p_mkdir("system", 0777));
408+
cl_git_rewritefile("system/gitattributes", "file foo=1 bar=2");
409+
cl_git_pass(git_buf_joinpath(&sysdir, clar_sandbox_path(), "system"));
410+
cl_git_pass(git_sysdir_set(GIT_SYSDIR_SYSTEM, sysdir.ptr));
411+
g_repo = cl_git_sandbox_reopen();
412+
413+
cl_git_pass(git_attr_session__init(&session, g_repo));
414+
cl_git_pass(git_attr_get_many_with_session(values, g_repo, &session, 0, "file", ARRAY_SIZE(attrs), attrs));
415+
416+
cl_assert_equal_s(values[0], "1");
417+
cl_assert_equal_s(values[1], "2");
418+
419+
cl_git_pass(p_unlink("system/gitattributes"));
420+
cl_git_pass(p_rmdir("system"));
421+
git_buf_dispose(&sysdir);
422+
git_attr_session__free(&session);
423+
}

0 commit comments

Comments
 (0)