Skip to content

Commit bce1719

Browse files
committed
sysdir: provide actual home directory
Provide a mechanism to look up the user's home directory, using the same mechanism that we use for locating the global configuration path (a fancy name for saying "the home directory"). SSH known hosts lookups now use this, instead of simply looking at the HOME environment variable, to support Windows-style home directory lookups in `HOME`, `HOMEPATH`, or `USERPROFILE`.
1 parent 9a98636 commit bce1719

File tree

2 files changed

+58
-10
lines changed

2 files changed

+58
-10
lines changed

src/libgit2/sysdir.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ static int get_passwd_home(git_str *out, uid_t uid)
321321
}
322322
#endif
323323

324-
static int git_sysdir_guess_global_dirs(git_str *out)
324+
static int git_sysdir_guess_home_dirs(git_str *out)
325325
{
326326
#ifdef GIT_WIN32
327327
static const wchar_t *global_tmpls[4] = {
@@ -367,6 +367,11 @@ static int git_sysdir_guess_global_dirs(git_str *out)
367367
#endif
368368
}
369369

370+
static int git_sysdir_guess_global_dirs(git_str *out)
371+
{
372+
return git_sysdir_guess_home_dirs(out);
373+
}
374+
370375
static int git_sysdir_guess_xdg_dirs(git_str *out)
371376
{
372377
#ifdef GIT_WIN32
@@ -434,6 +439,7 @@ static struct git_sysdir__dir git_sysdir__dirs[] = {
434439
{ GIT_STR_INIT, git_sysdir_guess_xdg_dirs },
435440
{ GIT_STR_INIT, git_sysdir_guess_programdata_dirs },
436441
{ GIT_STR_INIT, git_sysdir_guess_template_dirs },
442+
{ GIT_STR_INIT, git_sysdir_guess_home_dirs }
437443
};
438444

439445
static void git_sysdir_global_shutdown(void)
@@ -613,6 +619,12 @@ int git_sysdir_find_template_dir(git_str *path)
613619
path, NULL, GIT_SYSDIR_TEMPLATE, "template");
614620
}
615621

622+
int git_sysdir_find_homedir(git_str *path)
623+
{
624+
return git_sysdir_find_in_dirlist(
625+
path, NULL, GIT_SYSDIR_HOME, "home directory");
626+
}
627+
616628
int git_sysdir_expand_global_file(git_str *path, const char *filename)
617629
{
618630
int error;
@@ -624,3 +636,15 @@ int git_sysdir_expand_global_file(git_str *path, const char *filename)
624636

625637
return error;
626638
}
639+
640+
int git_sysdir_expand_homedir_file(git_str *path, const char *filename)
641+
{
642+
int error;
643+
644+
if ((error = git_sysdir_find_homedir(path)) == 0) {
645+
if (filename)
646+
error = git_str_joinpath(path, path->ptr, filename);
647+
}
648+
649+
return error;
650+
}

src/libgit2/sysdir.h

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,48 @@ extern int git_sysdir_find_programdata_file(git_str *path, const char *filename)
5757
extern int git_sysdir_find_template_dir(git_str *path);
5858

5959
/**
60-
* Expand the name of a "global" file (i.e. one in a user's home
61-
* directory). Unlike `find_global_file` (above), this makes no
62-
* attempt to check for the existence of the file, and is useful if
63-
* you want the full path regardless of existence.
60+
* Find the home directory. On Windows, this will look at the `HOME`,
61+
* `HOMEPATH`, and `USERPROFILE` environment variables (in that order)
62+
* and return the first path that is set and exists. On other systems,
63+
* this will simply return the contents of the `HOME` environment variable.
64+
*
65+
* @param path buffer to write the full path into
66+
* @return 0 if found, GIT_ENOTFOUND if not found, or -1 on other OS error
67+
*/
68+
extern int git_sysdir_find_homedir(git_str *path);
69+
70+
/**
71+
* Expand the name of a "global" file -- by default inside the user's
72+
* home directory, but can be overridden by the user configuration.
73+
* Unlike `find_global_file` (above), this makes no attempt to check
74+
* for the existence of the file, and is useful if you want the full
75+
* path regardless of existence.
6476
*
6577
* @param path buffer to write the full path into
6678
* @param filename name of file in the home directory
6779
* @return 0 on success or -1 on error
6880
*/
6981
extern int git_sysdir_expand_global_file(git_str *path, const char *filename);
7082

83+
/**
84+
* Expand the name of a file in the user's home directory. This
85+
* function makes no attempt to check for the existence of the file,
86+
* and is useful if you want the full path regardless of existence.
87+
*
88+
* @param path buffer to write the full path into
89+
* @param filename name of file in the home directory
90+
* @return 0 on success or -1 on error
91+
*/
92+
extern int git_sysdir_expand_homedir_file(git_str *path, const char *filename);
93+
7194
typedef enum {
72-
GIT_SYSDIR_SYSTEM = 0,
73-
GIT_SYSDIR_GLOBAL = 1,
74-
GIT_SYSDIR_XDG = 2,
95+
GIT_SYSDIR_SYSTEM = 0,
96+
GIT_SYSDIR_GLOBAL = 1,
97+
GIT_SYSDIR_XDG = 2,
7598
GIT_SYSDIR_PROGRAMDATA = 3,
76-
GIT_SYSDIR_TEMPLATE = 4,
77-
GIT_SYSDIR__MAX = 5
99+
GIT_SYSDIR_TEMPLATE = 4,
100+
GIT_SYSDIR_HOME = 5,
101+
GIT_SYSDIR__MAX = 6
78102
} git_sysdir_t;
79103

80104
/**

0 commit comments

Comments
 (0)