usage: allow dying without writing an error message

Sometimes code wants to die in a situation where it already has written an error message. To use the same error code as `die()` we have to use `exit(128)`, which is easy to get wrong and leaves magic numbers all over our codebase. Teach `die_message_builtin()` to not print any error when passed a `NULL` pointer as error string. Like this, such users can now call `die(NULL)` to achieve the same result without any hardcoded error codes. Adapt a couple of builtins to use this new pattern to demonstrate that there is a need for such a helper. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jun 3, 2025 at 16:01 UTC 697202b0b1c7c02208c620f96c608e0817d079dd
5 files changed +13 -11
builtin/am.c
+2 -2
@@ -1000,7 +1000,7 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
1000
1001 if (!patch_format) {
1002 fprintf_ln(stderr, _("Patch format detection failed."));
1003 - exit(128);
1003 + die(NULL);
1004 }
1005
1006 if (mkdir(state->dir, 0777) < 0 && errno != EEXIST)
@@ -1178,7 +1178,7 @@ static void NORETURN die_user_resolve(const struct am_state *state)
1178 strbuf_release(&sb);
1179 }
1180
1181 - exit(128);
1181 + die(NULL);
1182 }
1183
1184 /**
builtin/checkout.c
+2 -2
@@ -838,7 +838,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
838 init_tree_desc(&trees[0], &tree->object.oid,
839 tree->buffer, tree->size);
840 if (parse_tree(new_tree) < 0)
841 - exit(128);
841 + die(NULL);
842 tree = new_tree;
843 init_tree_desc(&trees[1], &tree->object.oid,
844 tree->buffer, tree->size);
@@ -913,7 +913,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
913 work,
914 old_tree);
915 if (ret < 0)
916 - exit(128);
916 + die(NULL);
917 ret = reset_tree(new_tree,
918 opts, 0,
919 writeout_error, new_branch_info);
builtin/fetch.c
+1 -1
@@ -992,7 +992,7 @@ static int update_local_ref(struct ref *ref,
992 fast_forward = repo_in_merge_bases(the_repository, current,
993 updated);
994 if (fast_forward < 0)
995 - exit(128);
995 + die(NULL);
996 forced_updates_ms += (getnanotime() - t_before) / 1000000;
997 } else {
998 fast_forward = 1;
builtin/submodule--helper.c
+6 -6
@@ -303,7 +303,7 @@ static void runcommand_in_submodule_cb(const struct cache_entry *list_item,
303 char *displaypath;
304
305 if (validate_submodule_path(path) < 0)
306 - exit(128);
306 + die(NULL);
307
308 displaypath = get_submodule_displaypath(path, info->prefix,
309 info->super_prefix);
@@ -643,7 +643,7 @@ static void status_submodule(const char *path, const struct object_id *ce_oid,
643 };
644
645 if (validate_submodule_path(path) < 0)
646 - exit(128);
646 + die(NULL);
647
648 if (!submodule_from_path(the_repository, null_oid(the_hash_algo), path))
649 die(_("no submodule mapping found in .gitmodules for path '%s'"),
@@ -1257,7 +1257,7 @@ static void sync_submodule(const char *path, const char *prefix,
1257 return;
1258
1259 if (validate_submodule_path(path) < 0)
1260 - exit(128);
1260 + die(NULL);
1261
1262 sub = submodule_from_path(the_repository, null_oid(the_hash_algo), path);
1263
@@ -1402,7 +1402,7 @@ static void deinit_submodule(const char *path, const char *prefix,
1402 char *sub_git_dir = xstrfmt("%s/.git", path);
1403
1404 if (validate_submodule_path(path) < 0)
1405 - exit(128);
1405 + die(NULL);
1406
1407 sub = submodule_from_path(the_repository, null_oid(the_hash_algo), path);
1408
@@ -1724,7 +1724,7 @@ static int clone_submodule(const struct module_clone_data *clone_data,
1724 char *to_free = NULL;
1725
1726 if (validate_submodule_path(clone_data_path) < 0)
1727 - exit(128);
1727 + die(NULL);
1728
1729 if (!is_absolute_path(clone_data->path))
1730 clone_data_path = to_free = xstrfmt("%s/%s", repo_get_work_tree(the_repository),
@@ -3524,7 +3524,7 @@ static int module_add(int argc, const char **argv, const char *prefix,
3524 strip_dir_trailing_slashes(add_data.sm_path);
3525
3526 if (validate_submodule_path(add_data.sm_path) < 0)
3527 - exit(128);
3527 + die(NULL);
3528
3529 die_on_index_match(add_data.sm_path, force);
3530 die_on_repo_without_commits(add_data.sm_path);
usage.c
+2
@@ -67,6 +67,8 @@ static NORETURN void usage_builtin(const char *err, va_list params)
67
68 static void die_message_builtin(const char *err, va_list params)
69 {
70 + if (!err)
71 + return;
72 trace2_cmd_error_va(err, params);
73 vreportf(_("fatal: "), err, params);
74 }