Skip to content

Commit 52e8393

Browse files
committed
Merge pull request #487 from dscho/default-username
Improve the default user name & email logic
2 parents fa5348d + 5114bb5 commit 52e8393

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
@@ -1951,18 +1951,63 @@ int mingw_getpagesize(void)
19511951
return si.dwAllocationGranularity;
19521952
}
19531953

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

1959-
DWORD len = sizeof(user_name);
1960-
if (!GetUserName(user_name, &len))
1996+
len = sizeof(user_name);
1997+
if (!GetUserName(user_name, &len)) {
1998+
initialized = 1;
19611999
return NULL;
1962-
p.pw_name = user_name;
1963-
p.pw_gecos = "unknown";
1964-
p.pw_dir = NULL;
1965-
return &p;
2000+
}
2001+
2002+
p = xmalloc(sizeof(*p));
2003+
p->pw_name = user_name;
2004+
p->pw_gecos = get_extended_user_info(NameDisplay);
2005+
if (!p->pw_gecos)
2006+
p->pw_gecos = "unknown";
2007+
p->pw_dir = NULL;
2008+
2009+
initialized = 1;
2010+
return p;
19662011
}
19672012

19682013
static HANDLE timer_event;

compat/mingw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,8 @@ int mingw_offset_1st_component(const char *path);
445445
#define PATH_SEP ';'
446446
extern const char *program_data_config(void);
447447
#define git_program_data_config program_data_config
448+
extern char *mingw_query_user_email(void);
449+
#define query_user_email mingw_query_user_email
448450
#if !defined(__MINGW64_VERSION_MAJOR) && (!defined(_MSC_VER) || _MSC_VER < 1800)
449451
#define PRIuMAX "I64u"
450452
#define PRId64 "I64d"

git-compat-util.h

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

379+
#ifndef query_user_email
380+
#define query_user_email() NULL
381+
#endif
382+
379383
#if defined(__HP_cc) && (__HP_cc >= 61000)
380384
#define NORETURN __attribute__((noreturn))
381385
#define NORETURN_PTR

ident.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,9 @@ const char *ident_default_email(void)
171171
strbuf_addstr(&git_default_email, email);
172172
committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
173173
author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
174-
} else
174+
} else if ((email = query_user_email()) && email[0])
175+
strbuf_addstr(&git_default_email, email);
176+
else
175177
copy_email(xgetpwuid_self(&default_email_is_bogus),
176178
&git_default_email, &default_email_is_bogus);
177179
strbuf_trim(&git_default_email);

0 commit comments

Comments
 (0)