[PATCH v2] Support "core.excludesfile = ~/.gitignore"
- From
- Karl Chen <quarl@cs.berkeley.edu>
- Date
- Aug 25, 2008, 19:07 UTC
- Message-ID
- <quack.20080825T1207.lthk5e46hi4_-_@roar.cs.berkeley.edu>
- In-Reply-To
- <7vhc9aj82i.fsf@gitster.siamese.dyndns.org>
The config variable core.excludesfile is parsed to substitute ~ and ~user with getpw entries.
Signed-off-by: Karl Chen <quarl@quarl.org> --- config.c | 41 +++++++++++++++++++++++++++++++++++++++-- 1 files changed, 39 insertions(+), 2 deletions(-)
Based on the discussion it sounds like there are complications to supporting relative paths (due to worktree config), and "$HOME" (when generalized, due to bootstrapping issues with $GIT_*).
Since ~ and ~user are orthogonal to these, can I suggest going forward with this, without blocking on those two?
I have reworked the patch to use getpw to support ~user. $HOME can eventually be supported via $ENVVARs.
diff --git a/config.c b/config.c index 53f04a0..6a83c64 100644 --- a/config.c +++ b/config.c @@ -334,6 +334,42 @@ int git_config_string(const char **dest, const char *var, const char *value) return 0; } +/* + * Expand ~ and ~user. Returns a newly malloced string. (If input does not + * start with "~", equivalent to xstrdup.) + */ +static char *expand_userdir(const char *value) { + if (value[0] == '~') { + struct passwd *pw; + char *expanded_dir; + const char *slash = strchr(value+1, '/'); + const char *after_username = slash ? slash : value+strlen(value); + if (after_username == value+1) { + pw = getpwuid(getuid()); + if (!pw) die("You don't exist!"); + } else { + char save = *after_username; + *(char*)after_username = '\0'; + pw = getpwnam(value+1); + if (!pw) die("No such user: '%s'", value+1); + *(char*)after_username = save; + } + expanded_dir = xmalloc(strlen(pw->pw_dir) + strlen(after_username) + 1); + strcpy(expanded_dir, pw->pw_dir); + strcat(expanded_dir, after_username); + return expanded_dir; + } else { + return xstrdup(value); + } +} + +int git_config_userdir(const char **dest, const char *var, const char *value) { + if (!value) + return config_error_nonbool(var); + *dest = expand_userdir(value); + return 0; +} + static int git_default_core_config(const char *var, const char *value) { /* This needs a better name */ @@ -456,8 +492,9 @@ static int git_default_core_config(const char *var, const char *value) if (!strcmp(var, "core.editor")) return git_config_string(&editor_program, var, value); - if (!strcmp(var, "core.excludesfile")) - return git_config_string(&excludes_file, var, value); + if (!strcmp(var, "core.excludesfile")) { + return git_config_userdir(&excludes_file, var, value); + } if (!strcmp(var, "core.whitespace")) { if (!value)
-- 1.5.6.2