Skip to content

Commit ae830c4

Browse files
committed
Merge pull request #487 from dscho/default-username
Improve the default user name & email logic
2 parents f177f92 + 026f541 commit ae830c4

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
@@ -1954,18 +1954,63 @@ int mingw_getpagesize(void)
19541954
return si.dwAllocationGranularity;
19551955
}
19561956

1957+
/* See https://msdn.microsoft.com/en-us/library/windows/desktop/ms724435.aspx */
1958+
enum EXTENDED_NAME_FORMAT {
1959+
NameDisplay = 3,
1960+
NameUserPrincipal = 8
1961+
};
1962+
1963+
static char *get_extended_user_info(enum EXTENDED_NAME_FORMAT type)
1964+
{
1965+
DECLARE_PROC_ADDR(secur32.dll, BOOL, GetUserNameExW,
1966+
enum EXTENDED_NAME_FORMAT, LPCWSTR, PULONG);
1967+
static wchar_t wbuffer[1024];
1968+
DWORD len;
1969+
1970+
if (!INIT_PROC_ADDR(GetUserNameExW))
1971+
return NULL;
1972+
1973+
len = ARRAY_SIZE(wbuffer);
1974+
if (GetUserNameExW(type, wbuffer, &len)) {
1975+
char *converted = xmalloc((len *= 3));
1976+
if (xwcstoutf(converted, wbuffer, len) >= 0)
1977+
return converted;
1978+
free(converted);
1979+
}
1980+
1981+
return NULL;
1982+
}
1983+
1984+
char *mingw_query_user_email(void)
1985+
{
1986+
return get_extended_user_info(NameUserPrincipal);
1987+
}
1988+
19571989
struct passwd *getpwuid(int uid)
19581990
{
1991+
static unsigned initialized;
19591992
static char user_name[100];
1960-
static struct passwd p;
1993+
static struct passwd *p;
1994+
DWORD len;
1995+
1996+
if (initialized)
1997+
return p;
19611998

1962-
DWORD len = sizeof(user_name);
1963-
if (!GetUserName(user_name, &len))
1999+
len = sizeof(user_name);
2000+
if (!GetUserName(user_name, &len)) {
2001+
initialized = 1;
19642002
return NULL;
1965-
p.pw_name = user_name;
1966-
p.pw_gecos = "unknown";
1967-
p.pw_dir = NULL;
1968-
return &p;
2003+
}
2004+
2005+
p = xmalloc(sizeof(*p));
2006+
p->pw_name = user_name;
2007+
p->pw_gecos = get_extended_user_info(NameDisplay);
2008+
if (!p->pw_gecos)
2009+
p->pw_gecos = "unknown";
2010+
p->pw_dir = NULL;
2011+
2012+
initialized = 1;
2013+
return p;
19692014
}
19702015

19712016
static HANDLE timer_event;

compat/mingw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,8 @@ int mingw_offset_1st_component(const char *path);
438438
#define PATH_SEP ';'
439439
extern const char *program_data_config(void);
440440
#define git_program_data_config program_data_config
441+
extern char *mingw_query_user_email(void);
442+
#define query_user_email mingw_query_user_email
441443
#ifndef __MINGW64_VERSION_MAJOR
442444
#define PRIuMAX "I64u"
443445
#define PRId64 "I64d"

git-compat-util.h

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

378+
#ifndef query_user_email
379+
#define query_user_email() NULL
380+
#endif
381+
378382
#if defined(__HP_cc) && (__HP_cc >= 61000)
379383
#define NORETURN __attribute__((noreturn))
380384
#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)