Skip to content

Commit c28c558

Browse files
committed
Merge 'default-ident' into HEAD
2 parents 2c08228 + 41966a7 commit c28c558

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

@@ -1871,18 +1872,63 @@ int mingw_getpagesize(void)
18711872
return si.dwAllocationGranularity;
18721873
}
18731874

1875+
/* See https://msdn.microsoft.com/en-us/library/windows/desktop/ms724435.aspx */
1876+
enum EXTENDED_NAME_FORMAT {
1877+
NameDisplay = 3,
1878+
NameUserPrincipal = 8
1879+
};
1880+
1881+
static char *get_extended_user_info(enum EXTENDED_NAME_FORMAT type)
1882+
{
1883+
DECLARE_PROC_ADDR(secur32.dll, BOOL, GetUserNameExW,
1884+
enum EXTENDED_NAME_FORMAT, LPCWSTR, PULONG);
1885+
static wchar_t wbuffer[1024];
1886+
DWORD len;
1887+
1888+
if (!INIT_PROC_ADDR(GetUserNameExW))
1889+
return NULL;
1890+
1891+
len = ARRAY_SIZE(wbuffer);
1892+
if (GetUserNameExW(type, wbuffer, &len)) {
1893+
char *converted = xmalloc((len *= 3));
1894+
if (xwcstoutf(converted, wbuffer, len) >= 0)
1895+
return converted;
1896+
free(converted);
1897+
}
1898+
1899+
return NULL;
1900+
}
1901+
1902+
char *mingw_query_user_email(void)
1903+
{
1904+
return get_extended_user_info(NameUserPrincipal);
1905+
}
1906+
18741907
struct passwd *getpwuid(int uid)
18751908
{
1909+
static unsigned initialized;
18761910
static char user_name[100];
1877-
static struct passwd p;
1911+
static struct passwd *p;
1912+
DWORD len;
1913+
1914+
if (initialized)
1915+
return p;
18781916

1879-
DWORD len = sizeof(user_name);
1880-
if (!GetUserName(user_name, &len))
1917+
len = sizeof(user_name);
1918+
if (!GetUserName(user_name, &len)) {
1919+
initialized = 1;
18811920
return NULL;
1882-
p.pw_name = user_name;
1883-
p.pw_gecos = "unknown";
1884-
p.pw_dir = NULL;
1885-
return &p;
1921+
}
1922+
1923+
p = xmalloc(sizeof(*p));
1924+
p->pw_name = user_name;
1925+
p->pw_gecos = get_extended_user_info(NameDisplay);
1926+
if (!p->pw_gecos)
1927+
p->pw_gecos = "unknown";
1928+
p->pw_dir = NULL;
1929+
1930+
initialized = 1;
1931+
return p;
18861932
}
18871933

18881934
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 @@ static inline void convert_slashes(char *path)
448448
int mingw_offset_1st_component(const char *path);
449449
#define offset_1st_component mingw_offset_1st_component
450450
#define PATH_SEP ';'
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
@@ -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)