Skip to content

Commit 558e5a8

Browse files
committed
Merge branch 'pt/xdg-config-path'
Code clean-up for xdg configuration path support. * pt/xdg-config-path: path.c: remove home_config_paths() git-config: replace use of home_config_paths() git-commit: replace use of home_config_paths() credential-store.c: replace home_config_paths() with xdg_config_home() dir.c: replace home_config_paths() with xdg_config_home() attr.c: replace home_config_paths() with xdg_config_home() path.c: implement xdg_config_home()
2 parents 7cb5073 + 846e5df commit 558e5a8

File tree

8 files changed

+34
-53
lines changed

8 files changed

+34
-53
lines changed

attr.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,6 @@ static int git_attr_system(void)
493493
static void bootstrap_attr_stack(void)
494494
{
495495
struct attr_stack *elem;
496-
char *xdg_attributes_file;
497496

498497
if (attr_stack)
499498
return;
@@ -512,10 +511,8 @@ static void bootstrap_attr_stack(void)
512511
}
513512
}
514513

515-
if (!git_attributes_file) {
516-
home_config_paths(NULL, &xdg_attributes_file, "attributes");
517-
git_attributes_file = xdg_attributes_file;
518-
}
514+
if (!git_attributes_file)
515+
git_attributes_file = xdg_config_home("attributes");
519516
if (git_attributes_file) {
520517
elem = read_attr_from_file(git_attributes_file, 1);
521518
if (elem) {

builtin/commit.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,12 +1398,10 @@ int cmd_status(int argc, const char **argv, const char *prefix)
13981398

13991399
static const char *implicit_ident_advice(void)
14001400
{
1401-
char *user_config = NULL;
1402-
char *xdg_config = NULL;
1403-
int config_exists;
1401+
char *user_config = expand_user_path("~/.gitconfig");
1402+
char *xdg_config = xdg_config_home("config");
1403+
int config_exists = file_exists(user_config) || file_exists(xdg_config);
14041404

1405-
home_config_paths(&user_config, &xdg_config, "config");
1406-
config_exists = file_exists(user_config) || file_exists(xdg_config);
14071405
free(user_config);
14081406
free(xdg_config);
14091407

builtin/config.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -488,10 +488,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
488488
}
489489

490490
if (use_global_config) {
491-
char *user_config = NULL;
492-
char *xdg_config = NULL;
493-
494-
home_config_paths(&user_config, &xdg_config, "config");
491+
char *user_config = expand_user_path("~/.gitconfig");
492+
char *xdg_config = xdg_config_home("config");
495493

496494
if (!user_config)
497495
/*

cache.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,6 @@ enum scld_error safe_create_leading_directories(char *path);
851851
enum scld_error safe_create_leading_directories_const(const char *path);
852852

853853
int mkdir_in_gitdir(const char *path);
854-
extern void home_config_paths(char **global, char **xdg, char *file);
855854
extern char *expand_user_path(const char *path);
856855
const char *enter_repo(const char *path, int strict);
857856
static inline int is_absolute_path(const char *path)
@@ -871,6 +870,13 @@ char *strip_path_suffix(const char *path, const char *suffix);
871870
int daemon_avoid_alias(const char *path);
872871
extern int is_ntfs_dotgit(const char *name);
873872

873+
/**
874+
* Return a newly allocated string with the evaluation of
875+
* "$XDG_CONFIG_HOME/git/$filename" if $XDG_CONFIG_HOME is non-empty, otherwise
876+
* "$HOME/.config/git/$filename". Return NULL upon error.
877+
*/
878+
extern char *xdg_config_home(const char *filename);
879+
874880
/* object replacement */
875881
#define LOOKUP_REPLACE_OBJECT 1
876882
extern void *read_sha1_file_extended(const unsigned char *sha1, enum object_type *type, unsigned long *size, unsigned flag);

config.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,10 +1187,8 @@ int git_config_system(void)
11871187
int git_config_early(config_fn_t fn, void *data, const char *repo_config)
11881188
{
11891189
int ret = 0, found = 0;
1190-
char *xdg_config = NULL;
1191-
char *user_config = NULL;
1192-
1193-
home_config_paths(&user_config, &xdg_config, "config");
1190+
char *xdg_config = xdg_config_home("config");
1191+
char *user_config = expand_user_path("~/.gitconfig");
11941192

11951193
if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK, 0)) {
11961194
ret += git_config_from_file(fn, git_etc_gitconfig(),

credential-store.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ int main(int argc, char **argv)
170170
} else {
171171
if ((file = expand_user_path("~/.git-credentials")))
172172
string_list_append_nodup(&fns, file);
173-
home_config_paths(NULL, &file, "credentials");
173+
file = xdg_config_home("credentials");
174174
if (file)
175175
string_list_append_nodup(&fns, file);
176176
}

dir.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,14 +1671,11 @@ int remove_dir_recursively(struct strbuf *path, int flag)
16711671
void setup_standard_excludes(struct dir_struct *dir)
16721672
{
16731673
const char *path;
1674-
char *xdg_path;
16751674

16761675
dir->exclude_per_dir = ".gitignore";
16771676
path = git_path("info/exclude");
1678-
if (!excludes_file) {
1679-
home_config_paths(NULL, &xdg_path, "ignore");
1680-
excludes_file = xdg_path;
1681-
}
1677+
if (!excludes_file)
1678+
excludes_file = xdg_config_home("ignore");
16821679
if (!access_or_warn(path, R_OK, 0))
16831680
add_excludes_from_file(dir, path);
16841681
if (excludes_file && !access_or_warn(excludes_file, R_OK, 0))

path.c

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -224,34 +224,6 @@ const char *mkpath(const char *fmt, ...)
224224
return cleanup_path(pathname->buf);
225225
}
226226

227-
void home_config_paths(char **global, char **xdg, char *file)
228-
{
229-
char *xdg_home = getenv("XDG_CONFIG_HOME");
230-
char *home = getenv("HOME");
231-
char *to_free = NULL;
232-
233-
if (!home) {
234-
if (global)
235-
*global = NULL;
236-
} else {
237-
if (!xdg_home) {
238-
to_free = mkpathdup("%s/.config", home);
239-
xdg_home = to_free;
240-
}
241-
if (global)
242-
*global = mkpathdup("%s/.gitconfig", home);
243-
}
244-
245-
if (xdg) {
246-
if (!xdg_home)
247-
*xdg = NULL;
248-
else
249-
*xdg = mkpathdup("%s/git/%s", xdg_home, file);
250-
}
251-
252-
free(to_free);
253-
}
254-
255227
const char *git_path_submodule(const char *path, const char *fmt, ...)
256228
{
257229
struct strbuf *buf = get_pathname();
@@ -931,3 +903,18 @@ int is_ntfs_dotgit(const char *name)
931903
len = -1;
932904
}
933905
}
906+
907+
char *xdg_config_home(const char *filename)
908+
{
909+
const char *home, *config_home;
910+
911+
assert(filename);
912+
config_home = getenv("XDG_CONFIG_HOME");
913+
if (config_home && *config_home)
914+
return mkpathdup("%s/git/%s", config_home, filename);
915+
916+
home = getenv("HOME");
917+
if (home)
918+
return mkpathdup("%s/.config/git/%s", home, filename);
919+
return NULL;
920+
}

0 commit comments

Comments
 (0)