Skip to content

Improve the default user name & email logic #487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 52 additions & 7 deletions compat/mingw.c
Original file line number Diff line number Diff line change
Expand Up @@ -1915,18 +1915,63 @@ int mingw_getpagesize(void)
return si.dwAllocationGranularity;
}

/* See https://msdn.microsoft.com/en-us/library/windows/desktop/ms724435.aspx */
enum EXTENDED_NAME_FORMAT {
NameDisplay = 3,
NameUserPrincipal = 8
};

static char *get_extended_user_info(enum EXTENDED_NAME_FORMAT type)
{
DECLARE_PROC_ADDR(secur32.dll, BOOL, GetUserNameExW,
enum EXTENDED_NAME_FORMAT, LPCWSTR, PULONG);
static wchar_t wbuffer[1024];
DWORD len;

if (!INIT_PROC_ADDR(GetUserNameExW))
return NULL;

len = ARRAY_SIZE(wbuffer);
if (GetUserNameExW(type, wbuffer, &len)) {
char *converted = xmalloc((len *= 3));
if (xwcstoutf(converted, wbuffer, len) >= 0)
return converted;
free(converted);
}

return NULL;
}

char *mingw_query_user_email(void)
{
return get_extended_user_info(NameUserPrincipal);
}

struct passwd *getpwuid(int uid)
{
static unsigned initialized;
static char user_name[100];
static struct passwd p;
static struct passwd *p;
DWORD len;

if (initialized)
return p;

DWORD len = sizeof(user_name);
if (!GetUserName(user_name, &len))
len = sizeof(user_name);
if (!GetUserName(user_name, &len)) {
initialized = 1;
return NULL;
p.pw_name = user_name;
p.pw_gecos = "unknown";
p.pw_dir = NULL;
return &p;
}

p = xmalloc(sizeof(*p));
p->pw_name = user_name;
p->pw_gecos = get_extended_user_info(NameDisplay);
if (!p->pw_gecos)
p->pw_gecos = "unknown";
p->pw_dir = NULL;

initialized = 1;
return p;
}

static HANDLE timer_event;
Expand Down
2 changes: 2 additions & 0 deletions compat/mingw.h
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,8 @@ int mingw_offset_1st_component(const char *path);
#define PATH_SEP ';'
extern const char *program_data_config(void);
#define git_program_data_config program_data_config
extern char *mingw_query_user_email(void);
#define query_user_email mingw_query_user_email
#ifndef __MINGW64_VERSION_MAJOR
#define PRIuMAX "I64u"
#define PRId64 "I64d"
Expand Down
4 changes: 4 additions & 0 deletions git-compat-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,10 @@ static inline char *git_find_last_dir_sep(const char *path)
#define git_program_data_config() NULL
#endif

#ifndef query_user_email
#define query_user_email() NULL
#endif

#if defined(__HP_cc) && (__HP_cc >= 61000)
#define NORETURN __attribute__((noreturn))
#define NORETURN_PTR
Expand Down
4 changes: 3 additions & 1 deletion ident.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ const char *ident_default_email(void)
strbuf_addstr(&git_default_email, email);
committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
} else
} else if ((email = query_user_email()) && email[0])
strbuf_addstr(&git_default_email, email);
else
copy_email(xgetpwuid_self(), &git_default_email);
strbuf_trim(&git_default_email);
}
Expand Down