提交 eb2fc8f8 编写于 作者: J Junio C Hamano

Merge branch 'mm/config-pathname-tilde-expand'

* mm/config-pathname-tilde-expand:
  Documentation: avoid xmlto input error
  expand_user_path: expand ~ to $HOME, not to the actual homedir.
  Expand ~ and ~user in core.excludesfile, commit.template
...@@ -380,8 +380,9 @@ Common unit suffixes of 'k', 'm', or 'g' are supported. ...@@ -380,8 +380,9 @@ Common unit suffixes of 'k', 'm', or 'g' are supported.
core.excludesfile:: core.excludesfile::
In addition to '.gitignore' (per-directory) and In addition to '.gitignore' (per-directory) and
'.git/info/exclude', git looks into this file for patterns '.git/info/exclude', git looks into this file for patterns
of files which are not meant to be tracked. See of files which are not meant to be tracked. "{tilde}/" is expanded
linkgit:gitignore[5]. to the value of `$HOME` and "{tilde}user/" to the specified user's
home directory. See linkgit:gitignore[5].
core.editor:: core.editor::
Commands such as `commit` and `tag` that lets you edit Commands such as `commit` and `tag` that lets you edit
...@@ -681,6 +682,8 @@ color.ui:: ...@@ -681,6 +682,8 @@ color.ui::
commit.template:: commit.template::
Specify a file to use as the template for new commit messages. Specify a file to use as the template for new commit messages.
"{tilde}/" is expanded to the value of `$HOME` and "{tilde}user/" to the
specified user's home directory.
diff.autorefreshindex:: diff.autorefreshindex::
When using 'git-diff' to compare with work tree When using 'git-diff' to compare with work tree
......
...@@ -999,7 +999,7 @@ static int git_commit_config(const char *k, const char *v, void *cb) ...@@ -999,7 +999,7 @@ static int git_commit_config(const char *k, const char *v, void *cb)
struct wt_status *s = cb; struct wt_status *s = cb;
if (!strcmp(k, "commit.template")) if (!strcmp(k, "commit.template"))
return git_config_string(&template_file, k, v); return git_config_pathname(&template_file, k, v);
return git_status_config(k, v, s); return git_status_config(k, v, s);
} }
......
...@@ -649,6 +649,7 @@ int set_shared_perm(const char *path, int mode); ...@@ -649,6 +649,7 @@ int set_shared_perm(const char *path, int mode);
#define adjust_shared_perm(path) set_shared_perm((path), 0) #define adjust_shared_perm(path) set_shared_perm((path), 0)
int safe_create_leading_directories(char *path); int safe_create_leading_directories(char *path);
int safe_create_leading_directories_const(const char *path); int safe_create_leading_directories_const(const char *path);
extern char *expand_user_path(const char *path);
char *enter_repo(char *path, int strict); char *enter_repo(char *path, int strict);
static inline int is_absolute_path(const char *path) static inline int is_absolute_path(const char *path)
{ {
...@@ -909,6 +910,7 @@ extern unsigned long git_config_ulong(const char *, const char *); ...@@ -909,6 +910,7 @@ extern unsigned long git_config_ulong(const char *, const char *);
extern int git_config_bool_or_int(const char *, const char *, int *); extern int git_config_bool_or_int(const char *, const char *, int *);
extern int git_config_bool(const char *, const char *); extern int git_config_bool(const char *, const char *);
extern int git_config_string(const char **, const char *, const char *); extern int git_config_string(const char **, const char *, const char *);
extern int git_config_pathname(const char **, const char *, const char *);
extern int git_config_set(const char *, const char *); extern int git_config_set(const char *, const char *);
extern int git_config_set_multivar(const char *, const char *, const char *, int); extern int git_config_set_multivar(const char *, const char *, const char *, int);
extern int git_config_rename_section(const char *, const char *); extern int git_config_rename_section(const char *, const char *);
......
...@@ -351,6 +351,16 @@ int git_config_string(const char **dest, const char *var, const char *value) ...@@ -351,6 +351,16 @@ int git_config_string(const char **dest, const char *var, const char *value)
return 0; return 0;
} }
int git_config_pathname(const char **dest, const char *var, const char *value)
{
if (!value)
return config_error_nonbool(var);
*dest = expand_user_path(value);
if (!*dest)
die("Failed to expand user dir in: '%s'", value);
return 0;
}
static int git_default_core_config(const char *var, const char *value) static int git_default_core_config(const char *var, const char *value)
{ {
/* This needs a better name */ /* This needs a better name */
...@@ -479,7 +489,7 @@ static int git_default_core_config(const char *var, const char *value) ...@@ -479,7 +489,7 @@ static int git_default_core_config(const char *var, const char *value)
return git_config_string(&editor_program, var, value); return git_config_string(&editor_program, var, value);
if (!strcmp(var, "core.excludesfile")) if (!strcmp(var, "core.excludesfile"))
return git_config_string(&excludes_file, var, value); return git_config_pathname(&excludes_file, var, value);
if (!strcmp(var, "core.whitespace")) { if (!strcmp(var, "core.whitespace")) {
if (!value) if (!value)
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
* which is what it's designed for. * which is what it's designed for.
*/ */
#include "cache.h" #include "cache.h"
#include "strbuf.h"
static char bad_path[] = "/bad-path/"; static char bad_path[] = "/bad-path/";
...@@ -207,43 +208,49 @@ int validate_headref(const char *path) ...@@ -207,43 +208,49 @@ int validate_headref(const char *path)
return -1; return -1;
} }
static char *user_path(char *buf, char *path, int sz) static struct passwd *getpw_str(const char *username, size_t len)
{ {
struct passwd *pw; struct passwd *pw;
char *slash; char *username_z = xmalloc(len + 1);
int len, baselen; memcpy(username_z, username, len);
username_z[len] = '\0';
pw = getpwnam(username_z);
free(username_z);
return pw;
}
if (!path || path[0] != '~') /*
return NULL; * Return a string with ~ and ~user expanded via getpw*. If buf != NULL,
path++; * then it is a newly allocated string. Returns NULL on getpw failure or
slash = strchr(path, '/'); * if path is NULL.
if (path[0] == '/' || !path[0]) { */
pw = getpwuid(getuid()); char *expand_user_path(const char *path)
} {
else { struct strbuf user_path = STRBUF_INIT;
if (slash) { const char *first_slash = strchrnul(path, '/');
*slash = 0; const char *to_copy = path;
pw = getpwnam(path);
*slash = '/'; if (path == NULL)
goto return_null;
if (path[0] == '~') {
const char *username = path + 1;
size_t username_len = first_slash - username;
if (username_len == 0) {
const char *home = getenv("HOME");
strbuf_add(&user_path, home, strlen(home));
} else {
struct passwd *pw = getpw_str(username, username_len);
if (!pw)
goto return_null;
strbuf_add(&user_path, pw->pw_dir, strlen(pw->pw_dir));
} }
else to_copy = first_slash;
pw = getpwnam(path);
} }
if (!pw || !pw->pw_dir || sz <= strlen(pw->pw_dir)) strbuf_add(&user_path, to_copy, strlen(to_copy));
return NULL; return strbuf_detach(&user_path, NULL);
baselen = strlen(pw->pw_dir); return_null:
memcpy(buf, pw->pw_dir, baselen); strbuf_release(&user_path);
while ((1 < baselen) && (buf[baselen-1] == '/')) { return NULL;
buf[baselen-1] = 0;
baselen--;
}
if (slash && slash[1]) {
len = strlen(slash);
if (sz <= baselen + len)
return NULL;
memcpy(buf + baselen, slash, len + 1);
}
return buf;
} }
/* /*
...@@ -291,8 +298,18 @@ char *enter_repo(char *path, int strict) ...@@ -291,8 +298,18 @@ char *enter_repo(char *path, int strict)
if (PATH_MAX <= len) if (PATH_MAX <= len)
return NULL; return NULL;
if (path[0] == '~') { if (path[0] == '~') {
if (!user_path(used_path, path, PATH_MAX)) char *newpath = expand_user_path(path);
if (!newpath || (PATH_MAX - 10 < strlen(newpath))) {
free(newpath);
return NULL; return NULL;
}
/*
* Copy back into the static buffer. A pity
* since newpath was not bounded, but other
* branches of the if are limited by PATH_MAX
* anyway.
*/
strcpy(used_path, newpath); free(newpath);
strcpy(validated_path, path); strcpy(validated_path, path);
path = used_path; path = used_path;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册