Skip to content

Commit 5269287

Browse files
committed
Merge pull request #487 from dscho/default-username
Improve the default user name & email logic
2 parents 423ff0d + 662009b commit 5269287

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
@@ -1915,18 +1915,63 @@ int mingw_getpagesize(void)
19151915
return si.dwAllocationGranularity;
19161916
}
19171917

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

1923-
DWORD len = sizeof(user_name);
1924-
if (!GetUserName(user_name, &len))
1960+
len = sizeof(user_name);
1961+
if (!GetUserName(user_name, &len)) {
1962+
initialized = 1;
19251963
return NULL;
1926-
p.pw_name = user_name;
1927-
p.pw_gecos = "unknown";
1928-
p.pw_dir = NULL;
1929-
return &p;
1964+
}
1965+
1966+
p = xmalloc(sizeof(*p));
1967+
p->pw_name = user_name;
1968+
p->pw_gecos = get_extended_user_info(NameDisplay);
1969+
if (!p->pw_gecos)
1970+
p->pw_gecos = "unknown";
1971+
p->pw_dir = NULL;
1972+
1973+
initialized = 1;
1974+
return p;
19301975
}
19311976

19321977
static HANDLE timer_event;

compat/mingw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,8 @@ int mingw_offset_1st_component(const char *path);
430430
#define PATH_SEP ';'
431431
extern const char *program_data_config(void);
432432
#define git_program_data_config program_data_config
433+
extern char *mingw_query_user_email(void);
434+
#define query_user_email mingw_query_user_email
433435
#ifndef __MINGW64_VERSION_MAJOR
434436
#define PRIuMAX "I64u"
435437
#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
@@ -120,7 +120,9 @@ const char *ident_default_email(void)
120120
strbuf_addstr(&git_default_email, email);
121121
committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
122122
author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
123-
} else
123+
} else if ((email = query_user_email()) && email[0])
124+
strbuf_addstr(&git_default_email, email);
125+
else
124126
copy_email(xgetpwuid_self(), &git_default_email);
125127
strbuf_trim(&git_default_email);
126128
}

0 commit comments

Comments
 (0)