Skip to content

Commit fa2b50b

Browse files
author
Git for Windows Build Agent
committed
Merge pull request #487 from dscho/default-username
Improve the default user name & email logic
2 parents c3a2256 + eac64ee commit fa2b50b

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
@@ -1968,18 +1968,63 @@ int mingw_getpagesize(void)
19681968
return si.dwAllocationGranularity;
19691969
}
19701970

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

1976-
DWORD len = sizeof(user_name);
1977-
if (!GetUserName(user_name, &len))
2013+
len = sizeof(user_name);
2014+
if (!GetUserName(user_name, &len)) {
2015+
initialized = 1;
19782016
return NULL;
1979-
p.pw_name = user_name;
1980-
p.pw_gecos = "unknown";
1981-
p.pw_dir = NULL;
1982-
return &p;
2017+
}
2018+
2019+
p = xmalloc(sizeof(*p));
2020+
p->pw_name = user_name;
2021+
p->pw_gecos = get_extended_user_info(NameDisplay);
2022+
if (!p->pw_gecos)
2023+
p->pw_gecos = "unknown";
2024+
p->pw_dir = NULL;
2025+
2026+
initialized = 1;
2027+
return p;
19832028
}
19842029

19852030
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)