Skip to content

Commit 1d8a6f6

Browse files
committed
Merge branch 'mm/config-edit-global'
Start "git config --edit --global" from a skeletal per-user configuration file contents, instead of a total blank, when the user does not already have any. This immediately reduces the need for a later "Have you forgotten setting core.user?" and we can add more to the template as we gain more experience. * mm/config-edit-global: commit: advertise config --global --edit on guessed identity home_config_paths(): let the caller ignore xdg path config --global --edit: create a template file if needed
2 parents c518279 + 8b27ff7 commit 1d8a6f6

File tree

5 files changed

+69
-10
lines changed

5 files changed

+69
-10
lines changed

builtin/commit.c

+33-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,20 @@ static const char * const builtin_status_usage[] = {
4242
NULL
4343
};
4444

45-
static const char implicit_ident_advice[] =
45+
static const char implicit_ident_advice_noconfig[] =
46+
N_("Your name and email address were configured automatically based\n"
47+
"on your username and hostname. Please check that they are accurate.\n"
48+
"You can suppress this message by setting them explicitly. Run the\n"
49+
"following command and follow the instructions in your editor to edit\n"
50+
"your configuration file:\n"
51+
"\n"
52+
" git config --global --edit\n"
53+
"\n"
54+
"After doing this, you may fix the identity used for this commit with:\n"
55+
"\n"
56+
" git commit --amend --reset-author\n");
57+
58+
static const char implicit_ident_advice_config[] =
4659
N_("Your name and email address were configured automatically based\n"
4760
"on your username and hostname. Please check that they are accurate.\n"
4861
"You can suppress this message by setting them explicitly:\n"
@@ -1402,6 +1415,24 @@ int cmd_status(int argc, const char **argv, const char *prefix)
14021415
return 0;
14031416
}
14041417

1418+
static const char *implicit_ident_advice(void)
1419+
{
1420+
char *user_config = NULL;
1421+
char *xdg_config = NULL;
1422+
int config_exists;
1423+
1424+
home_config_paths(&user_config, &xdg_config, "config");
1425+
config_exists = file_exists(user_config) || file_exists(xdg_config);
1426+
free(user_config);
1427+
free(xdg_config);
1428+
1429+
if (config_exists)
1430+
return _(implicit_ident_advice_config);
1431+
else
1432+
return _(implicit_ident_advice_noconfig);
1433+
1434+
}
1435+
14051436
static void print_summary(const char *prefix, const unsigned char *sha1,
14061437
int initial_commit)
14071438
{
@@ -1440,7 +1471,7 @@ static void print_summary(const char *prefix, const unsigned char *sha1,
14401471
strbuf_addbuf_percentquote(&format, &committer_ident);
14411472
if (advice_implicit_identity) {
14421473
strbuf_addch(&format, '\n');
1443-
strbuf_addstr(&format, _(implicit_ident_advice));
1474+
strbuf_addstr(&format, implicit_ident_advice());
14441475
}
14451476
}
14461477
strbuf_release(&author_ident);

builtin/config.c

+28-3
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,20 @@ static int get_urlmatch(const char *var, const char *url)
445445
return 0;
446446
}
447447

448+
static char *default_user_config(void)
449+
{
450+
struct strbuf buf = STRBUF_INIT;
451+
strbuf_addf(&buf,
452+
_("# This is Git's per-user configuration file.\n"
453+
"[core]\n"
454+
"# Please adapt and uncomment the following lines:\n"
455+
"# user = %s\n"
456+
"# email = %s\n"),
457+
ident_default_name(),
458+
ident_default_email());
459+
return strbuf_detach(&buf, NULL);
460+
}
461+
448462
int cmd_config(int argc, const char **argv, const char *prefix)
449463
{
450464
int nongit = !startup_info->have_repository;
@@ -551,6 +565,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
551565
}
552566
}
553567
else if (actions == ACTION_EDIT) {
568+
const char *config_file = given_config_source.file ?
569+
given_config_source.file : git_path("config");
554570
check_argc(argc, 0, 0);
555571
if (!given_config_source.file && nongit)
556572
die("not in a git directory");
@@ -559,9 +575,18 @@ int cmd_config(int argc, const char **argv, const char *prefix)
559575
if (given_config_source.blob)
560576
die("editing blobs is not supported");
561577
git_config(git_default_config, NULL);
562-
launch_editor(given_config_source.file ?
563-
given_config_source.file : git_path("config"),
564-
NULL, NULL);
578+
if (use_global_config) {
579+
int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
580+
if (fd) {
581+
char *content = default_user_config();
582+
write_str_in_full(fd, content);
583+
free(content);
584+
close(fd);
585+
}
586+
else if (errno != EEXIST)
587+
die_errno(_("cannot create configuration file %s"), config_file);
588+
}
589+
launch_editor(config_file, NULL, NULL);
565590
}
566591
else if (actions == ACTION_SET) {
567592
int ret;

cache.h

+1
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,7 @@ extern const char *git_author_info(int);
10621062
extern const char *git_committer_info(int);
10631063
extern const char *fmt_ident(const char *name, const char *email, const char *date_str, int);
10641064
extern const char *fmt_name(const char *name, const char *email);
1065+
extern const char *ident_default_name(void);
10651066
extern const char *ident_default_email(void);
10661067
extern const char *git_editor(void);
10671068
extern const char *git_pager(int stdout_is_tty);

ident.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ static void copy_email(const struct passwd *pw, struct strbuf *email)
102102
add_domainname(email);
103103
}
104104

105-
static const char *ident_default_name(void)
105+
const char *ident_default_name(void)
106106
{
107107
if (!git_default_name.len) {
108108
copy_gecos(xgetpwuid_self(), &git_default_name);

path.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,12 @@ void home_config_paths(char **global, char **xdg, char *file)
148148
*global = mkpathdup("%s/.gitconfig", home);
149149
}
150150

151-
if (!xdg_home)
152-
*xdg = NULL;
153-
else
154-
*xdg = mkpathdup("%s/git/%s", xdg_home, file);
151+
if (xdg) {
152+
if (!xdg_home)
153+
*xdg = NULL;
154+
else
155+
*xdg = mkpathdup("%s/git/%s", xdg_home, file);
156+
}
155157

156158
free(to_free);
157159
}

0 commit comments

Comments
 (0)