Skip to content

Commit 2d5ff66

Browse files
committed
Merge branch 'ps/config-error' into maint
Many codepaths forget to check return value from git_config_set(); the function is made to die() to make sure we do not proceed when setting a configuration variable failed. * ps/config-error: config: rename git_config_set_or_die to git_config_set config: rename git_config_set to git_config_set_gently compat: die when unable to set core.precomposeunicode sequencer: die on config error when saving replay opts init-db: die on config errors when initializing empty repo clone: die on config error in cmd_clone remote: die on config error when manipulating remotes remote: die on config error when setting/adding branches remote: die on config error when setting URL submodule--helper: die on config error when cloning module submodule: die on config error when linking modules branch: die on config error when editing branch description branch: die on config error when unsetting upstream branch: report errors in tracking branch setup config: introduce set_or_die wrappers
2 parents 9bb7103 + 3d18064 commit 2d5ff66

File tree

13 files changed

+159
-105
lines changed

13 files changed

+159
-105
lines changed

branch.c

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ static int should_setup_rebase(const char *origin)
4949
return 0;
5050
}
5151

52-
void install_branch_config(int flag, const char *local, const char *origin, const char *remote)
52+
static const char tracking_advice[] =
53+
N_("\n"
54+
"After fixing the error cause you may try to fix up\n"
55+
"the remote tracking information by invoking\n"
56+
"\"git branch --set-upstream-to=%s%s%s\".");
57+
58+
int install_branch_config(int flag, const char *local, const char *origin, const char *remote)
5359
{
5460
const char *shortname = NULL;
5561
struct strbuf key = STRBUF_INIT;
@@ -60,20 +66,23 @@ void install_branch_config(int flag, const char *local, const char *origin, cons
6066
&& !origin) {
6167
warning(_("Not setting branch %s as its own upstream."),
6268
local);
63-
return;
69+
return 0;
6470
}
6571

6672
strbuf_addf(&key, "branch.%s.remote", local);
67-
git_config_set(key.buf, origin ? origin : ".");
73+
if (git_config_set_gently(key.buf, origin ? origin : ".") < 0)
74+
goto out_err;
6875

6976
strbuf_reset(&key);
7077
strbuf_addf(&key, "branch.%s.merge", local);
71-
git_config_set(key.buf, remote);
78+
if (git_config_set_gently(key.buf, remote) < 0)
79+
goto out_err;
7280

7381
if (rebasing) {
7482
strbuf_reset(&key);
7583
strbuf_addf(&key, "branch.%s.rebase", local);
76-
git_config_set(key.buf, "true");
84+
if (git_config_set_gently(key.buf, "true") < 0)
85+
goto out_err;
7786
}
7887
strbuf_release(&key);
7988

@@ -102,23 +111,36 @@ void install_branch_config(int flag, const char *local, const char *origin, cons
102111
local, remote);
103112
}
104113
}
114+
115+
return 0;
116+
117+
out_err:
118+
strbuf_release(&key);
119+
error(_("Unable to write upstream branch configuration"));
120+
121+
advise(_(tracking_advice),
122+
origin ? origin : "",
123+
origin ? "/" : "",
124+
shortname ? shortname : remote);
125+
126+
return -1;
105127
}
106128

107129
/*
108130
* This is called when new_ref is branched off of orig_ref, and tries
109131
* to infer the settings for branch.<new_ref>.{remote,merge} from the
110132
* config.
111133
*/
112-
static int setup_tracking(const char *new_ref, const char *orig_ref,
113-
enum branch_track track, int quiet)
134+
static void setup_tracking(const char *new_ref, const char *orig_ref,
135+
enum branch_track track, int quiet)
114136
{
115137
struct tracking tracking;
116138
int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;
117139

118140
memset(&tracking, 0, sizeof(tracking));
119141
tracking.spec.dst = (char *)orig_ref;
120142
if (for_each_remote(find_tracked_branch, &tracking))
121-
return 1;
143+
return;
122144

123145
if (!tracking.matches)
124146
switch (track) {
@@ -127,18 +149,18 @@ static int setup_tracking(const char *new_ref, const char *orig_ref,
127149
case BRANCH_TRACK_OVERRIDE:
128150
break;
129151
default:
130-
return 1;
152+
return;
131153
}
132154

133155
if (tracking.matches > 1)
134-
return error(_("Not tracking: ambiguous information for ref %s"),
135-
orig_ref);
156+
die(_("Not tracking: ambiguous information for ref %s"),
157+
orig_ref);
136158

137-
install_branch_config(config_flags, new_ref, tracking.remote,
138-
tracking.src ? tracking.src : orig_ref);
159+
if (install_branch_config(config_flags, new_ref, tracking.remote,
160+
tracking.src ? tracking.src : orig_ref) < 0)
161+
exit(-1);
139162

140163
free(tracking.src);
141-
return 0;
142164
}
143165

144166
int read_branch_desc(struct strbuf *buf, const char *branch_name)

branch.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ void remove_branch_state(void);
4343
/*
4444
* Configure local branch "local" as downstream to branch "remote"
4545
* from remote "origin". Used by git branch --set-upstream.
46+
* Returns 0 on success.
4647
*/
4748
#define BRANCH_CONFIG_VERBOSE 01
48-
extern void install_branch_config(int flag, const char *local, const char *origin, const char *remote);
49+
extern int install_branch_config(int flag, const char *local, const char *origin, const char *remote);
4950

5051
/*
5152
* Read branch description

builtin/branch.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,6 @@ static const char edit_description[] = "BRANCH_DESCRIPTION";
570570

571571
static int edit_branch_description(const char *branch_name)
572572
{
573-
int status;
574573
struct strbuf buf = STRBUF_INIT;
575574
struct strbuf name = STRBUF_INIT;
576575

@@ -595,11 +594,11 @@ static int edit_branch_description(const char *branch_name)
595594
strbuf_stripspace(&buf, 1);
596595

597596
strbuf_addf(&name, "branch.%s.description", branch_name);
598-
status = git_config_set(name.buf, buf.len ? buf.buf : NULL);
597+
git_config_set(name.buf, buf.len ? buf.buf : NULL);
599598
strbuf_release(&name);
600599
strbuf_release(&buf);
601600

602-
return status;
601+
return 0;
603602
}
604603

605604
int cmd_branch(int argc, const char **argv, const char *prefix)

builtin/clone.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ static int checkout(void)
732732

733733
static int write_one_config(const char *key, const char *value, void *data)
734734
{
735-
return git_config_set_multivar(key, value ? value : "true", "^$", 0);
735+
return git_config_set_multivar_gently(key, value ? value : "true", "^$", 0);
736736
}
737737

738738
static void write_config(struct string_list *config)

builtin/config.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
582582
check_write();
583583
check_argc(argc, 2, 2);
584584
value = normalize_value(argv[0], argv[1]);
585-
ret = git_config_set_in_file(given_config_source.file, argv[0], value);
585+
ret = git_config_set_in_file_gently(given_config_source.file, argv[0], value);
586586
if (ret == CONFIG_NOTHING_SET)
587587
error("cannot overwrite multiple values with a single value\n"
588588
" Use a regexp, --add or --replace-all to change %s.", argv[0]);
@@ -592,23 +592,23 @@ int cmd_config(int argc, const char **argv, const char *prefix)
592592
check_write();
593593
check_argc(argc, 2, 3);
594594
value = normalize_value(argv[0], argv[1]);
595-
return git_config_set_multivar_in_file(given_config_source.file,
596-
argv[0], value, argv[2], 0);
595+
return git_config_set_multivar_in_file_gently(given_config_source.file,
596+
argv[0], value, argv[2], 0);
597597
}
598598
else if (actions == ACTION_ADD) {
599599
check_write();
600600
check_argc(argc, 2, 2);
601601
value = normalize_value(argv[0], argv[1]);
602-
return git_config_set_multivar_in_file(given_config_source.file,
603-
argv[0], value,
604-
CONFIG_REGEX_NONE, 0);
602+
return git_config_set_multivar_in_file_gently(given_config_source.file,
603+
argv[0], value,
604+
CONFIG_REGEX_NONE, 0);
605605
}
606606
else if (actions == ACTION_REPLACE_ALL) {
607607
check_write();
608608
check_argc(argc, 2, 3);
609609
value = normalize_value(argv[0], argv[1]);
610-
return git_config_set_multivar_in_file(given_config_source.file,
611-
argv[0], value, argv[2], 1);
610+
return git_config_set_multivar_in_file_gently(given_config_source.file,
611+
argv[0], value, argv[2], 1);
612612
}
613613
else if (actions == ACTION_GET) {
614614
check_argc(argc, 1, 2);
@@ -634,17 +634,17 @@ int cmd_config(int argc, const char **argv, const char *prefix)
634634
check_write();
635635
check_argc(argc, 1, 2);
636636
if (argc == 2)
637-
return git_config_set_multivar_in_file(given_config_source.file,
638-
argv[0], NULL, argv[1], 0);
637+
return git_config_set_multivar_in_file_gently(given_config_source.file,
638+
argv[0], NULL, argv[1], 0);
639639
else
640-
return git_config_set_in_file(given_config_source.file,
641-
argv[0], NULL);
640+
return git_config_set_in_file_gently(given_config_source.file,
641+
argv[0], NULL);
642642
}
643643
else if (actions == ACTION_UNSET_ALL) {
644644
check_write();
645645
check_argc(argc, 1, 2);
646-
return git_config_set_multivar_in_file(given_config_source.file,
647-
argv[0], NULL, argv[1], 1);
646+
return git_config_set_multivar_in_file_gently(given_config_source.file,
647+
argv[0], NULL, argv[1], 1);
648648
}
649649
else if (actions == ACTION_RENAME_SECTION) {
650650
int ret;

builtin/init-db.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ static int create_default_files(const char *template_path)
250250
git_config_set("core.bare", "false");
251251
/* allow template config file to override the default */
252252
if (log_all_ref_updates == -1)
253-
git_config_set("core.logallrefupdates", "true");
253+
git_config_set("core.logallrefupdates", "true");
254254
if (needs_work_tree_config(get_git_dir(), work_tree))
255255
git_config_set("core.worktree", work_tree);
256256
}

0 commit comments

Comments
 (0)