Skip to content

Commit 16ce0b9

Browse files
committed
Merge branch 'js/mingw-default-ident'
The logic to select the default user name and e-mail on Windows has been improved. * js/mingw-default-ident: mingw: use domain information for default email getpwuid(mingw): provide a better default for the user name getpwuid(mingw): initialize the structure only once
2 parents 1e5d454 + 501afcb commit 16ce0b9

File tree

4 files changed

+62
-7
lines changed

4 files changed

+62
-7
lines changed

compat/mingw.c

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "../strbuf.h"
66
#include "../run-command.h"
77
#include "../cache.h"
8+
#include "win32/lazyload.h"
89

910
#define HCAST(type, handle) ((type)(intptr_t)handle)
1011

@@ -1798,18 +1799,63 @@ int mingw_getpagesize(void)
17981799
return si.dwAllocationGranularity;
17991800
}
18001801

1802+
/* See https://msdn.microsoft.com/en-us/library/windows/desktop/ms724435.aspx */
1803+
enum EXTENDED_NAME_FORMAT {
1804+
NameDisplay = 3,
1805+
NameUserPrincipal = 8
1806+
};
1807+
1808+
static char *get_extended_user_info(enum EXTENDED_NAME_FORMAT type)
1809+
{
1810+
DECLARE_PROC_ADDR(secur32.dll, BOOL, GetUserNameExW,
1811+
enum EXTENDED_NAME_FORMAT, LPCWSTR, PULONG);
1812+
static wchar_t wbuffer[1024];
1813+
DWORD len;
1814+
1815+
if (!INIT_PROC_ADDR(GetUserNameExW))
1816+
return NULL;
1817+
1818+
len = ARRAY_SIZE(wbuffer);
1819+
if (GetUserNameExW(type, wbuffer, &len)) {
1820+
char *converted = xmalloc((len *= 3));
1821+
if (xwcstoutf(converted, wbuffer, len) >= 0)
1822+
return converted;
1823+
free(converted);
1824+
}
1825+
1826+
return NULL;
1827+
}
1828+
1829+
char *mingw_query_user_email(void)
1830+
{
1831+
return get_extended_user_info(NameUserPrincipal);
1832+
}
1833+
18011834
struct passwd *getpwuid(int uid)
18021835
{
1836+
static unsigned initialized;
18031837
static char user_name[100];
1804-
static struct passwd p;
1838+
static struct passwd *p;
1839+
DWORD len;
1840+
1841+
if (initialized)
1842+
return p;
18051843

1806-
DWORD len = sizeof(user_name);
1807-
if (!GetUserName(user_name, &len))
1844+
len = sizeof(user_name);
1845+
if (!GetUserName(user_name, &len)) {
1846+
initialized = 1;
18081847
return NULL;
1809-
p.pw_name = user_name;
1810-
p.pw_gecos = "unknown";
1811-
p.pw_dir = NULL;
1812-
return &p;
1848+
}
1849+
1850+
p = xmalloc(sizeof(*p));
1851+
p->pw_name = user_name;
1852+
p->pw_gecos = get_extended_user_info(NameDisplay);
1853+
if (!p->pw_gecos)
1854+
p->pw_gecos = "unknown";
1855+
p->pw_dir = NULL;
1856+
1857+
initialized = 1;
1858+
return p;
18131859
}
18141860

18151861
static HANDLE timer_event;

compat/mingw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,8 @@ static inline void convert_slashes(char *path)
424424
int mingw_offset_1st_component(const char *path);
425425
#define offset_1st_component mingw_offset_1st_component
426426
#define PATH_SEP ';'
427+
extern char *mingw_query_user_email(void);
428+
#define query_user_email mingw_query_user_email
427429
#if !defined(__MINGW64_VERSION_MAJOR) && (!defined(_MSC_VER) || _MSC_VER < 1800)
428430
#define PRIuMAX "I64u"
429431
#define PRId64 "I64d"

git-compat-util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,10 @@ static inline char *git_find_last_dir_sep(const char *path)
382382
#define find_last_dir_sep git_find_last_dir_sep
383383
#endif
384384

385+
#ifndef query_user_email
386+
#define query_user_email() NULL
387+
#endif
388+
385389
#if defined(__HP_cc) && (__HP_cc >= 61000)
386390
#define NORETURN __attribute__((noreturn))
387391
#define NORETURN_PTR

ident.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ const char *ident_default_email(void)
168168
strbuf_addstr(&git_default_email, email);
169169
committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
170170
author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
171+
} else if ((email = query_user_email()) && email[0]) {
172+
strbuf_addstr(&git_default_email, email);
173+
free((char *)email);
171174
} else
172175
copy_email(xgetpwuid_self(&default_email_is_bogus),
173176
&git_default_email, &default_email_is_bogus);

0 commit comments

Comments
 (0)