Skip to content

Commit e970a26

Browse files
committed
Merge 'default-ident' into HEAD
2 parents 9a1b81d + e947ed5 commit e970a26

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
@@ -1822,18 +1822,63 @@ int mingw_getpagesize(void)
18221822
return si.dwAllocationGranularity;
18231823
}
18241824

1825+
/* See https://msdn.microsoft.com/en-us/library/windows/desktop/ms724435.aspx */
1826+
enum EXTENDED_NAME_FORMAT {
1827+
NameDisplay = 3,
1828+
NameUserPrincipal = 8
1829+
};
1830+
1831+
static char *get_extended_user_info(enum EXTENDED_NAME_FORMAT type)
1832+
{
1833+
DECLARE_PROC_ADDR(secur32.dll, BOOL, GetUserNameExW,
1834+
enum EXTENDED_NAME_FORMAT, LPCWSTR, PULONG);
1835+
static wchar_t wbuffer[1024];
1836+
DWORD len;
1837+
1838+
if (!INIT_PROC_ADDR(GetUserNameExW))
1839+
return NULL;
1840+
1841+
len = ARRAY_SIZE(wbuffer);
1842+
if (GetUserNameExW(type, wbuffer, &len)) {
1843+
char *converted = xmalloc((len *= 3));
1844+
if (xwcstoutf(converted, wbuffer, len) >= 0)
1845+
return converted;
1846+
free(converted);
1847+
}
1848+
1849+
return NULL;
1850+
}
1851+
1852+
char *mingw_query_user_email(void)
1853+
{
1854+
return get_extended_user_info(NameUserPrincipal);
1855+
}
1856+
18251857
struct passwd *getpwuid(int uid)
18261858
{
1859+
static unsigned initialized;
18271860
static char user_name[100];
1828-
static struct passwd p;
1861+
static struct passwd *p;
1862+
DWORD len;
1863+
1864+
if (initialized)
1865+
return p;
18291866

1830-
DWORD len = sizeof(user_name);
1831-
if (!GetUserName(user_name, &len))
1867+
len = sizeof(user_name);
1868+
if (!GetUserName(user_name, &len)) {
1869+
initialized = 1;
18321870
return NULL;
1833-
p.pw_name = user_name;
1834-
p.pw_gecos = "unknown";
1835-
p.pw_dir = NULL;
1836-
return &p;
1871+
}
1872+
1873+
p = xmalloc(sizeof(*p));
1874+
p->pw_name = user_name;
1875+
p->pw_gecos = get_extended_user_info(NameDisplay);
1876+
if (!p->pw_gecos)
1877+
p->pw_gecos = "unknown";
1878+
p->pw_dir = NULL;
1879+
1880+
initialized = 1;
1881+
return p;
18371882
}
18381883

18391884
static HANDLE timer_event;

compat/mingw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,8 @@ static inline void convert_slashes(char *path)
434434
int mingw_offset_1st_component(const char *path);
435435
#define offset_1st_component mingw_offset_1st_component
436436
#define PATH_SEP ';'
437+
extern char *mingw_query_user_email(void);
438+
#define query_user_email mingw_query_user_email
437439
#if !defined(__MINGW64_VERSION_MAJOR) && (!defined(_MSC_VER) || _MSC_VER < 1800)
438440
#define PRIuMAX "I64u"
439441
#define PRId64 "I64d"

git-compat-util.h

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

375+
#ifndef query_user_email
376+
#define query_user_email() NULL
377+
#endif
378+
375379
#if defined(__HP_cc) && (__HP_cc >= 61000)
376380
#define NORETURN __attribute__((noreturn))
377381
#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)