Skip to content

Commit 3813db7

Browse files
dschojamill
authored andcommitted
Merge 'default-ident' into HEAD
2 parents 45621a1 + 91113e8 commit 3813db7

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

@@ -1909,18 +1910,63 @@ int mingw_getpagesize(void)
19091910
return si.dwAllocationGranularity;
19101911
}
19111912

1913+
/* See https://msdn.microsoft.com/en-us/library/windows/desktop/ms724435.aspx */
1914+
enum EXTENDED_NAME_FORMAT {
1915+
NameDisplay = 3,
1916+
NameUserPrincipal = 8
1917+
};
1918+
1919+
static char *get_extended_user_info(enum EXTENDED_NAME_FORMAT type)
1920+
{
1921+
DECLARE_PROC_ADDR(secur32.dll, BOOL, GetUserNameExW,
1922+
enum EXTENDED_NAME_FORMAT, LPCWSTR, PULONG);
1923+
static wchar_t wbuffer[1024];
1924+
DWORD len;
1925+
1926+
if (!INIT_PROC_ADDR(GetUserNameExW))
1927+
return NULL;
1928+
1929+
len = ARRAY_SIZE(wbuffer);
1930+
if (GetUserNameExW(type, wbuffer, &len)) {
1931+
char *converted = xmalloc((len *= 3));
1932+
if (xwcstoutf(converted, wbuffer, len) >= 0)
1933+
return converted;
1934+
free(converted);
1935+
}
1936+
1937+
return NULL;
1938+
}
1939+
1940+
char *mingw_query_user_email(void)
1941+
{
1942+
return get_extended_user_info(NameUserPrincipal);
1943+
}
1944+
19121945
struct passwd *getpwuid(int uid)
19131946
{
1947+
static unsigned initialized;
19141948
static char user_name[100];
1915-
static struct passwd p;
1949+
static struct passwd *p;
1950+
DWORD len;
1951+
1952+
if (initialized)
1953+
return p;
19161954

1917-
DWORD len = sizeof(user_name);
1918-
if (!GetUserName(user_name, &len))
1955+
len = sizeof(user_name);
1956+
if (!GetUserName(user_name, &len)) {
1957+
initialized = 1;
19191958
return NULL;
1920-
p.pw_name = user_name;
1921-
p.pw_gecos = "unknown";
1922-
p.pw_dir = NULL;
1923-
return &p;
1959+
}
1960+
1961+
p = xmalloc(sizeof(*p));
1962+
p->pw_name = user_name;
1963+
p->pw_gecos = get_extended_user_info(NameDisplay);
1964+
if (!p->pw_gecos)
1965+
p->pw_gecos = "unknown";
1966+
p->pw_dir = NULL;
1967+
1968+
initialized = 1;
1969+
return p;
19241970
}
19251971

19261972
static HANDLE timer_event;

compat/mingw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,8 @@ static inline void convert_slashes(char *path)
450450
int mingw_offset_1st_component(const char *path);
451451
#define offset_1st_component mingw_offset_1st_component
452452
#define PATH_SEP ';'
453+
extern char *mingw_query_user_email(void);
454+
#define query_user_email mingw_query_user_email
453455
#if !defined(__MINGW64_VERSION_MAJOR) && (!defined(_MSC_VER) || _MSC_VER < 1800)
454456
#define PRIuMAX "I64u"
455457
#define PRId64 "I64d"

git-compat-util.h

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

395+
#ifndef query_user_email
396+
#define query_user_email() NULL
397+
#endif
398+
395399
#if defined(__HP_cc) && (__HP_cc >= 61000)
396400
#define NORETURN __attribute__((noreturn))
397401
#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)