Skip to content

Commit 8f50d42

Browse files
committed
Merge pull request #487 from dscho/default-username
Improve the default user name & email logic
2 parents 074f54e + a894849 commit 8f50d42

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
@@ -1932,18 +1932,63 @@ int mingw_getpagesize(void)
19321932
return si.dwAllocationGranularity;
19331933
}
19341934

1935+
/* See https://msdn.microsoft.com/en-us/library/windows/desktop/ms724435.aspx */
1936+
enum EXTENDED_NAME_FORMAT {
1937+
NameDisplay = 3,
1938+
NameUserPrincipal = 8
1939+
};
1940+
1941+
static char *get_extended_user_info(enum EXTENDED_NAME_FORMAT type)
1942+
{
1943+
DECLARE_PROC_ADDR(secur32.dll, BOOL, GetUserNameExW,
1944+
enum EXTENDED_NAME_FORMAT, LPCWSTR, PULONG);
1945+
static wchar_t wbuffer[1024];
1946+
DWORD len;
1947+
1948+
if (!INIT_PROC_ADDR(GetUserNameExW))
1949+
return NULL;
1950+
1951+
len = ARRAY_SIZE(wbuffer);
1952+
if (GetUserNameExW(type, wbuffer, &len)) {
1953+
char *converted = xmalloc((len *= 3));
1954+
if (xwcstoutf(converted, wbuffer, len) >= 0)
1955+
return converted;
1956+
free(converted);
1957+
}
1958+
1959+
return NULL;
1960+
}
1961+
1962+
char *mingw_query_user_email(void)
1963+
{
1964+
return get_extended_user_info(NameUserPrincipal);
1965+
}
1966+
19351967
struct passwd *getpwuid(int uid)
19361968
{
1969+
static unsigned initialized;
19371970
static char user_name[100];
1938-
static struct passwd p;
1971+
static struct passwd *p;
1972+
DWORD len;
1973+
1974+
if (initialized)
1975+
return p;
19391976

1940-
DWORD len = sizeof(user_name);
1941-
if (!GetUserName(user_name, &len))
1977+
len = sizeof(user_name);
1978+
if (!GetUserName(user_name, &len)) {
1979+
initialized = 1;
19421980
return NULL;
1943-
p.pw_name = user_name;
1944-
p.pw_gecos = "unknown";
1945-
p.pw_dir = NULL;
1946-
return &p;
1981+
}
1982+
1983+
p = xmalloc(sizeof(*p));
1984+
p->pw_name = user_name;
1985+
p->pw_gecos = get_extended_user_info(NameDisplay);
1986+
if (!p->pw_gecos)
1987+
p->pw_gecos = "unknown";
1988+
p->pw_dir = NULL;
1989+
1990+
initialized = 1;
1991+
return p;
19471992
}
19481993

19491994
static HANDLE timer_event;

compat/mingw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,8 @@ int mingw_offset_1st_component(const char *path);
433433
#define PATH_SEP ';'
434434
extern const char *program_data_config(void);
435435
#define git_program_data_config program_data_config
436+
extern char *mingw_query_user_email(void);
437+
#define query_user_email mingw_query_user_email
436438
#ifndef __MINGW64_VERSION_MAJOR
437439
#define PRIuMAX "I64u"
438440
#define PRId64 "I64d"

git-compat-util.h

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

376+
#ifndef query_user_email
377+
#define query_user_email() NULL
378+
#endif
379+
376380
#if defined(__HP_cc) && (__HP_cc >= 61000)
377381
#define NORETURN __attribute__((noreturn))
378382
#define NORETURN_PTR

ident.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +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
171+
} else if ((email = query_user_email()) && email[0])
172+
strbuf_addstr(&git_default_email, email);
173+
else
172174
copy_email(xgetpwuid_self(&default_email_is_bogus),
173175
&git_default_email, &default_email_is_bogus);
174176
strbuf_trim(&git_default_email);

0 commit comments

Comments
 (0)