Skip to content

Commit 6fe247a

Browse files
committed
Merge pull request #487 from dscho/default-username
Improve the default user name & email logic
2 parents 5991ccc + 588c38c commit 6fe247a

File tree

4 files changed

+61
-8
lines changed

4 files changed

+61
-8
lines changed

compat/mingw.c

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1970,18 +1970,63 @@ int mingw_getpagesize(void)
19701970
return si.dwAllocationGranularity;
19711971
}
19721972

1973+
/* See https://msdn.microsoft.com/en-us/library/windows/desktop/ms724435.aspx */
1974+
enum EXTENDED_NAME_FORMAT {
1975+
NameDisplay = 3,
1976+
NameUserPrincipal = 8
1977+
};
1978+
1979+
static char *get_extended_user_info(enum EXTENDED_NAME_FORMAT type)
1980+
{
1981+
DECLARE_PROC_ADDR(secur32.dll, BOOL, GetUserNameExW,
1982+
enum EXTENDED_NAME_FORMAT, LPCWSTR, PULONG);
1983+
static wchar_t wbuffer[1024];
1984+
DWORD len;
1985+
1986+
if (!INIT_PROC_ADDR(GetUserNameExW))
1987+
return NULL;
1988+
1989+
len = ARRAY_SIZE(wbuffer);
1990+
if (GetUserNameExW(type, wbuffer, &len)) {
1991+
char *converted = xmalloc((len *= 3));
1992+
if (xwcstoutf(converted, wbuffer, len) >= 0)
1993+
return converted;
1994+
free(converted);
1995+
}
1996+
1997+
return NULL;
1998+
}
1999+
2000+
char *mingw_query_user_email(void)
2001+
{
2002+
return get_extended_user_info(NameUserPrincipal);
2003+
}
2004+
19732005
struct passwd *getpwuid(int uid)
19742006
{
2007+
static unsigned initialized;
19752008
static char user_name[100];
1976-
static struct passwd p;
2009+
static struct passwd *p;
2010+
DWORD len;
2011+
2012+
if (initialized)
2013+
return p;
19772014

1978-
DWORD len = sizeof(user_name);
1979-
if (!GetUserName(user_name, &len))
2015+
len = sizeof(user_name);
2016+
if (!GetUserName(user_name, &len)) {
2017+
initialized = 1;
19802018
return NULL;
1981-
p.pw_name = user_name;
1982-
p.pw_gecos = "unknown";
1983-
p.pw_dir = NULL;
1984-
return &p;
2019+
}
2020+
2021+
p = xmalloc(sizeof(*p));
2022+
p->pw_name = user_name;
2023+
p->pw_gecos = get_extended_user_info(NameDisplay);
2024+
if (!p->pw_gecos)
2025+
p->pw_gecos = "unknown";
2026+
p->pw_dir = NULL;
2027+
2028+
initialized = 1;
2029+
return p;
19852030
}
19862031

19872032
static HANDLE timer_event;

compat/mingw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,8 @@ int mingw_offset_1st_component(const char *path);
448448
#define PATH_SEP ';'
449449
extern const char *program_data_config(void);
450450
#define git_program_data_config program_data_config
451+
extern char *mingw_query_user_email(void);
452+
#define query_user_email mingw_query_user_email
451453
#if !defined(__MINGW64_VERSION_MAJOR) && (!defined(_MSC_VER) || _MSC_VER < 1800)
452454
#define PRIuMAX "I64u"
453455
#define PRId64 "I64d"

git-compat-util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,10 @@ static inline char *git_find_last_dir_sep(const char *path)
376376
#define git_program_data_config() NULL
377377
#endif
378378

379+
#ifndef query_user_email
380+
#define query_user_email() NULL
381+
#endif
382+
379383
#if defined(__HP_cc) && (__HP_cc >= 61000)
380384
#define NORETURN __attribute__((noreturn))
381385
#define NORETURN_PTR

ident.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,9 @@ const char *ident_default_email(void)
169169
strbuf_addstr(&git_default_email, email);
170170
committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
171171
author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
172-
} else
172+
} else if ((email = query_user_email()) && email[0])
173+
strbuf_addstr(&git_default_email, email);
174+
else
173175
copy_email(xgetpwuid_self(&default_email_is_bogus),
174176
&git_default_email, &default_email_is_bogus);
175177
strbuf_trim(&git_default_email);

0 commit comments

Comments
 (0)