Git fork

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>

authored by

Patrick Steinhardt and committed by
Junio C Hamano
697202b0 c367852d

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