Skip to content

Commit d1dfc01

Browse files
committed
Merge branch 'mm/config-pathname-tilde-expand' into maint
* 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
2 parents f2c0ca4 + 0f7fb21 commit d1dfc01

File tree

5 files changed

+69
-37
lines changed

5 files changed

+69
-37
lines changed

Documentation/config.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,9 @@ Common unit suffixes of 'k', 'm', or 'g' are supported.
380380
core.excludesfile::
381381
In addition to '.gitignore' (per-directory) and
382382
'.git/info/exclude', git looks into this file for patterns
383-
of files which are not meant to be tracked. See
384-
linkgit:gitignore[5].
383+
of files which are not meant to be tracked. "{tilde}/" is expanded
384+
to the value of `$HOME` and "{tilde}user/" to the specified user's
385+
home directory. See linkgit:gitignore[5].
385386

386387
core.editor::
387388
Commands such as `commit` and `tag` that lets you edit
@@ -670,6 +671,8 @@ color.ui::
670671

671672
commit.template::
672673
Specify a file to use as the template for new commit messages.
674+
"{tilde}/" is expanded to the value of `$HOME` and "{tilde}user/" to the
675+
specified user's home directory.
673676

674677
diff.autorefreshindex::
675678
When using 'git-diff' to compare with work tree

builtin-commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ static int git_commit_config(const char *k, const char *v, void *cb)
954954
struct wt_status *s = cb;
955955

956956
if (!strcmp(k, "commit.template"))
957-
return git_config_string(&template_file, k, v);
957+
return git_config_pathname(&template_file, k, v);
958958

959959
return git_status_config(k, v, s);
960960
}

cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ int set_shared_perm(const char *path, int mode);
644644
#define adjust_shared_perm(path) set_shared_perm((path), 0)
645645
int safe_create_leading_directories(char *path);
646646
int safe_create_leading_directories_const(const char *path);
647+
extern char *expand_user_path(const char *path);
647648
char *enter_repo(char *path, int strict);
648649
static inline int is_absolute_path(const char *path)
649650
{
@@ -902,6 +903,7 @@ extern unsigned long git_config_ulong(const char *, const char *);
902903
extern int git_config_bool_or_int(const char *, const char *, int *);
903904
extern int git_config_bool(const char *, const char *);
904905
extern int git_config_string(const char **, const char *, const char *);
906+
extern int git_config_pathname(const char **, const char *, const char *);
905907
extern int git_config_set(const char *, const char *);
906908
extern int git_config_set_multivar(const char *, const char *, const char *, int);
907909
extern int git_config_rename_section(const char *, const char *);

config.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,16 @@ int git_config_string(const char **dest, const char *var, const char *value)
351351
return 0;
352352
}
353353

354+
int git_config_pathname(const char **dest, const char *var, const char *value)
355+
{
356+
if (!value)
357+
return config_error_nonbool(var);
358+
*dest = expand_user_path(value);
359+
if (!*dest)
360+
die("Failed to expand user dir in: '%s'", value);
361+
return 0;
362+
}
363+
354364
static int git_default_core_config(const char *var, const char *value)
355365
{
356366
/* This needs a better name */
@@ -474,7 +484,7 @@ static int git_default_core_config(const char *var, const char *value)
474484
return git_config_string(&editor_program, var, value);
475485

476486
if (!strcmp(var, "core.excludesfile"))
477-
return git_config_string(&excludes_file, var, value);
487+
return git_config_pathname(&excludes_file, var, value);
478488

479489
if (!strcmp(var, "core.whitespace")) {
480490
if (!value)

path.c

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* which is what it's designed for.
1212
*/
1313
#include "cache.h"
14+
#include "strbuf.h"
1415

1516
static char bad_path[] = "/bad-path/";
1617

@@ -207,43 +208,49 @@ int validate_headref(const char *path)
207208
return -1;
208209
}
209210

210-
static char *user_path(char *buf, char *path, int sz)
211+
static struct passwd *getpw_str(const char *username, size_t len)
211212
{
212213
struct passwd *pw;
213-
char *slash;
214-
int len, baselen;
214+
char *username_z = xmalloc(len + 1);
215+
memcpy(username_z, username, len);
216+
username_z[len] = '\0';
217+
pw = getpwnam(username_z);
218+
free(username_z);
219+
return pw;
220+
}
215221

216-
if (!path || path[0] != '~')
217-
return NULL;
218-
path++;
219-
slash = strchr(path, '/');
220-
if (path[0] == '/' || !path[0]) {
221-
pw = getpwuid(getuid());
222-
}
223-
else {
224-
if (slash) {
225-
*slash = 0;
226-
pw = getpwnam(path);
227-
*slash = '/';
222+
/*
223+
* Return a string with ~ and ~user expanded via getpw*. If buf != NULL,
224+
* then it is a newly allocated string. Returns NULL on getpw failure or
225+
* if path is NULL.
226+
*/
227+
char *expand_user_path(const char *path)
228+
{
229+
struct strbuf user_path = STRBUF_INIT;
230+
const char *first_slash = strchrnul(path, '/');
231+
const char *to_copy = path;
232+
233+
if (path == NULL)
234+
goto return_null;
235+
if (path[0] == '~') {
236+
const char *username = path + 1;
237+
size_t username_len = first_slash - username;
238+
if (username_len == 0) {
239+
const char *home = getenv("HOME");
240+
strbuf_add(&user_path, home, strlen(home));
241+
} else {
242+
struct passwd *pw = getpw_str(username, username_len);
243+
if (!pw)
244+
goto return_null;
245+
strbuf_add(&user_path, pw->pw_dir, strlen(pw->pw_dir));
228246
}
229-
else
230-
pw = getpwnam(path);
247+
to_copy = first_slash;
231248
}
232-
if (!pw || !pw->pw_dir || sz <= strlen(pw->pw_dir))
233-
return NULL;
234-
baselen = strlen(pw->pw_dir);
235-
memcpy(buf, pw->pw_dir, baselen);
236-
while ((1 < baselen) && (buf[baselen-1] == '/')) {
237-
buf[baselen-1] = 0;
238-
baselen--;
239-
}
240-
if (slash && slash[1]) {
241-
len = strlen(slash);
242-
if (sz <= baselen + len)
243-
return NULL;
244-
memcpy(buf + baselen, slash, len + 1);
245-
}
246-
return buf;
249+
strbuf_add(&user_path, to_copy, strlen(to_copy));
250+
return strbuf_detach(&user_path, NULL);
251+
return_null:
252+
strbuf_release(&user_path);
253+
return NULL;
247254
}
248255

249256
/*
@@ -291,8 +298,18 @@ char *enter_repo(char *path, int strict)
291298
if (PATH_MAX <= len)
292299
return NULL;
293300
if (path[0] == '~') {
294-
if (!user_path(used_path, path, PATH_MAX))
301+
char *newpath = expand_user_path(path);
302+
if (!newpath || (PATH_MAX - 10 < strlen(newpath))) {
303+
free(newpath);
295304
return NULL;
305+
}
306+
/*
307+
* Copy back into the static buffer. A pity
308+
* since newpath was not bounded, but other
309+
* branches of the if are limited by PATH_MAX
310+
* anyway.
311+
*/
312+
strcpy(used_path, newpath); free(newpath);
296313
strcpy(validated_path, path);
297314
path = used_path;
298315
}

0 commit comments

Comments
 (0)