Skip to content

Commit 999425c

Browse files
pks-tgitster
authored andcommitted
builtin/config: refactor functions to have common exit paths
Refactor functions to have a single exit path. This will make it easier in subsequent commits to add common cleanup code. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 12b2306 commit 999425c

File tree

1 file changed

+38
-26
lines changed

1 file changed

+38
-26
lines changed

builtin/config.c

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,7 @@ static int cmd_config_get(int argc, const char **argv, const char *prefix)
798798
OPT_STRING(0, "default", &default_value, N_("value"), N_("use default value when missing entry")),
799799
OPT_END(),
800800
};
801+
int ret;
801802

802803
argc = parse_options(argc, argv, prefix, opts, builtin_config_get_usage,
803804
PARSE_OPT_STOP_AT_NON_OPTION);
@@ -816,8 +817,11 @@ static int cmd_config_get(int argc, const char **argv, const char *prefix)
816817
setup_auto_pager("config", 1);
817818

818819
if (url)
819-
return get_urlmatch(argv[0], url);
820-
return get_value(argv[0], value_pattern, flags);
820+
ret = get_urlmatch(argv[0], url);
821+
else
822+
ret = get_value(argv[0], value_pattern, flags);
823+
824+
return ret;
821825
}
822826

823827
static int cmd_config_set(int argc, const char **argv, const char *prefix)
@@ -888,6 +892,7 @@ static int cmd_config_unset(int argc, const char **argv, const char *prefix)
888892
OPT_BIT(0, "fixed-value", &flags, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE),
889893
OPT_END(),
890894
};
895+
int ret;
891896

892897
argc = parse_options(argc, argv, prefix, opts, builtin_config_unset_usage,
893898
PARSE_OPT_STOP_AT_NON_OPTION);
@@ -900,12 +905,14 @@ static int cmd_config_unset(int argc, const char **argv, const char *prefix)
900905
check_write();
901906

902907
if ((flags & CONFIG_FLAGS_MULTI_REPLACE) || value_pattern)
903-
return git_config_set_multivar_in_file_gently(given_config_source.file,
904-
argv[0], NULL, value_pattern,
905-
NULL, flags);
908+
ret = git_config_set_multivar_in_file_gently(given_config_source.file,
909+
argv[0], NULL, value_pattern,
910+
NULL, flags);
906911
else
907-
return git_config_set_in_file_gently(given_config_source.file, argv[0],
908-
NULL, NULL);
912+
ret = git_config_set_in_file_gently(given_config_source.file, argv[0],
913+
NULL, NULL);
914+
915+
return ret;
909916
}
910917

911918
static int cmd_config_rename_section(int argc, const char **argv, const char *prefix)
@@ -926,11 +933,13 @@ static int cmd_config_rename_section(int argc, const char **argv, const char *pr
926933
ret = git_config_rename_section_in_file(given_config_source.file,
927934
argv[0], argv[1]);
928935
if (ret < 0)
929-
return ret;
936+
goto out;
930937
else if (!ret)
931938
die(_("no such section: %s"), argv[0]);
939+
ret = 0;
932940

933-
return 0;
941+
out:
942+
return ret;
934943
}
935944

936945
static int cmd_config_remove_section(int argc, const char **argv, const char *prefix)
@@ -951,11 +960,13 @@ static int cmd_config_remove_section(int argc, const char **argv, const char *pr
951960
ret = git_config_rename_section_in_file(given_config_source.file,
952961
argv[0], NULL);
953962
if (ret < 0)
954-
return ret;
963+
goto out;
955964
else if (!ret)
956965
die(_("no such section: %s"), argv[0]);
966+
ret = 0;
957967

958-
return 0;
968+
out:
969+
return ret;
959970
}
960971

961972
static int show_editor(void)
@@ -1199,49 +1210,49 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
11991210
}
12001211
else if (actions == ACTION_GET) {
12011212
check_argc(argc, 1, 2);
1202-
return get_value(argv[0], argv[1], flags);
1213+
ret = get_value(argv[0], argv[1], flags);
12031214
}
12041215
else if (actions == ACTION_GET_ALL) {
12051216
do_all = 1;
12061217
check_argc(argc, 1, 2);
1207-
return get_value(argv[0], argv[1], flags);
1218+
ret = get_value(argv[0], argv[1], flags);
12081219
}
12091220
else if (actions == ACTION_GET_REGEXP) {
12101221
show_keys = 1;
12111222
use_key_regexp = 1;
12121223
do_all = 1;
12131224
check_argc(argc, 1, 2);
1214-
return get_value(argv[0], argv[1], flags);
1225+
ret = get_value(argv[0], argv[1], flags);
12151226
}
12161227
else if (actions == ACTION_GET_URLMATCH) {
12171228
check_argc(argc, 2, 2);
1218-
return get_urlmatch(argv[0], argv[1]);
1229+
ret = get_urlmatch(argv[0], argv[1]);
12191230
}
12201231
else if (actions == ACTION_UNSET) {
12211232
check_write();
12221233
check_argc(argc, 1, 2);
12231234
if (argc == 2)
1224-
return git_config_set_multivar_in_file_gently(given_config_source.file,
1225-
argv[0], NULL, argv[1],
1226-
NULL, flags);
1235+
ret = git_config_set_multivar_in_file_gently(given_config_source.file,
1236+
argv[0], NULL, argv[1],
1237+
NULL, flags);
12271238
else
1228-
return git_config_set_in_file_gently(given_config_source.file,
1229-
argv[0], NULL, NULL);
1239+
ret = git_config_set_in_file_gently(given_config_source.file,
1240+
argv[0], NULL, NULL);
12301241
}
12311242
else if (actions == ACTION_UNSET_ALL) {
12321243
check_write();
12331244
check_argc(argc, 1, 2);
1234-
return git_config_set_multivar_in_file_gently(given_config_source.file,
1235-
argv[0], NULL, argv[1],
1236-
NULL, flags | CONFIG_FLAGS_MULTI_REPLACE);
1245+
ret = git_config_set_multivar_in_file_gently(given_config_source.file,
1246+
argv[0], NULL, argv[1],
1247+
NULL, flags | CONFIG_FLAGS_MULTI_REPLACE);
12371248
}
12381249
else if (actions == ACTION_RENAME_SECTION) {
12391250
check_write();
12401251
check_argc(argc, 2, 2);
12411252
ret = git_config_rename_section_in_file(given_config_source.file,
12421253
argv[0], argv[1]);
12431254
if (ret < 0)
1244-
return ret;
1255+
goto out;
12451256
else if (!ret)
12461257
die(_("no such section: %s"), argv[0]);
12471258
else
@@ -1253,7 +1264,7 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
12531264
ret = git_config_rename_section_in_file(given_config_source.file,
12541265
argv[0], NULL);
12551266
if (ret < 0)
1256-
return ret;
1267+
goto out;
12571268
else if (!ret)
12581269
die(_("no such section: %s"), argv[0]);
12591270
else
@@ -1267,9 +1278,10 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
12671278
check_argc(argc, 1, 2);
12681279
if (argc == 2)
12691280
color_stdout_is_tty = git_config_bool("command line", argv[1]);
1270-
return get_colorbool(argv[0], argc == 2);
1281+
ret = get_colorbool(argv[0], argc == 2);
12711282
}
12721283

1284+
out:
12731285
free(comment);
12741286
free(value);
12751287
return ret;

0 commit comments

Comments
 (0)