Skip to content

Commit 8945438

Browse files
committed
core: allow users to configure home directory
Some callers -- like our test suite and the test suites of our language bindings -- want to isolate the home directory to avoid accidentally including the executing user's actual home directory data. Previously, we combined the notion of a home directory and global configuration -- now that this is separated, we provide users the ability to configure both.
1 parent cdf5ae9 commit 8945438

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

include/git2/common.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,9 @@ typedef enum {
222222
GIT_OPT_GET_EXTENSIONS,
223223
GIT_OPT_SET_EXTENSIONS,
224224
GIT_OPT_GET_OWNER_VALIDATION,
225-
GIT_OPT_SET_OWNER_VALIDATION
225+
GIT_OPT_SET_OWNER_VALIDATION,
226+
GIT_OPT_GET_HOMEDIR,
227+
GIT_OPT_SET_HOMEDIR
226228
} git_libgit2_opt_t;
227229

228230
/**
@@ -468,6 +470,16 @@ typedef enum {
468470
* > Set that repository directories should be owned by the current
469471
* > user. The default is to validate ownership.
470472
*
473+
* opts(GIT_OPT_GET_HOMEDIR, git_buf *out)
474+
* > Gets the current user's home directory, as it will be used
475+
* > for file lookups. The path is written to the `out` buffer.
476+
*
477+
* opts(GIT_OPT_SET_HOMEDIR, const char *path)
478+
* > Sets the directory used as the current user's home directory,
479+
* > for file lookups.
480+
* >
481+
* > - `path` directory of home directory.
482+
*
471483
* @param option Option key
472484
* @param ... value to set the option
473485
* @return 0 on success, <0 on failure

src/libgit2/libgit2.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,25 @@ int git_libgit2_opts(int key, ...)
414414
git_repository__validate_ownership = (va_arg(ap, int) != 0);
415415
break;
416416

417+
case GIT_OPT_GET_HOMEDIR:
418+
{
419+
git_buf *out = va_arg(ap, git_buf *);
420+
git_str str = GIT_STR_INIT;
421+
const git_str *tmp;
422+
423+
if ((error = git_buf_tostr(&str, out)) < 0 ||
424+
(error = git_sysdir_get(&tmp, GIT_SYSDIR_HOME)) < 0 ||
425+
(error = git_str_put(&str, tmp->ptr, tmp->size)) < 0)
426+
break;
427+
428+
error = git_buf_fromstr(out, &str);
429+
}
430+
break;
431+
432+
case GIT_OPT_SET_HOMEDIR:
433+
error = git_sysdir_set(GIT_SYSDIR_HOME, va_arg(ap, const char *));
434+
break;
435+
417436
default:
418437
git_error_set(GIT_ERROR_INVALID, "invalid option key");
419438
error = -1;

0 commit comments

Comments
 (0)