Skip to content

Commit 5135dda

Browse files
committed
Introduce git_sysdir_expand_global_file
Provide a mechanism for callers to expand the full path of a file in the global configuration directory (that is to say, the home directory) even if the file doesn't necessarily exist. This lets callers use their own logic for building paths separate from handling file existence.
1 parent 301dc26 commit 5135dda

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/sysdir.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,14 @@ int git_sysdir_find_template_dir(git_buf *path)
275275
path, NULL, GIT_SYSDIR_TEMPLATE, "template");
276276
}
277277

278+
int git_sysdir_expand_global_file(git_buf *path, const char *filename)
279+
{
280+
int error;
281+
282+
if ((error = git_sysdir_find_global_file(path, NULL)) == 0) {
283+
if (filename)
284+
error = git_buf_joinpath(path, path->ptr, filename);
285+
}
286+
287+
return error;
288+
}

src/sysdir.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ extern int git_sysdir_find_programdata_file(git_buf *path, const char *filename)
5555
*/
5656
extern int git_sysdir_find_template_dir(git_buf *path);
5757

58+
/**
59+
* Expand the name of a "global" file (i.e. one in a user's home
60+
* directory). Unlike `find_global_file` (above), this makes no
61+
* attempt to check for the existence of the file, and is useful if
62+
* you want the full path regardless of existence.
63+
*
64+
* @param path buffer to write the full path into
65+
* @param filename name of file in the home directory
66+
* @return 0 on success or -1 on error
67+
*/
68+
extern int git_sysdir_expand_global_file(git_buf *path, const char *filename);
69+
5870
typedef enum {
5971
GIT_SYSDIR_SYSTEM = 0,
6072
GIT_SYSDIR_GLOBAL = 1,

0 commit comments

Comments
 (0)