Skip to content

Commit 697202b

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c367852 commit 697202b

File tree

5 files changed

+13
-11
lines changed

5 files changed

+13
-11
lines changed

builtin/am.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,7 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
10001000

10011001
if (!patch_format) {
10021002
fprintf_ln(stderr, _("Patch format detection failed."));
1003-
exit(128);
1003+
die(NULL);
10041004
}
10051005

10061006
if (mkdir(state->dir, 0777) < 0 && errno != EEXIST)
@@ -1178,7 +1178,7 @@ static void NORETURN die_user_resolve(const struct am_state *state)
11781178
strbuf_release(&sb);
11791179
}
11801180

1181-
exit(128);
1181+
die(NULL);
11821182
}
11831183

11841184
/**

builtin/checkout.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
838838
init_tree_desc(&trees[0], &tree->object.oid,
839839
tree->buffer, tree->size);
840840
if (parse_tree(new_tree) < 0)
841-
exit(128);
841+
die(NULL);
842842
tree = new_tree;
843843
init_tree_desc(&trees[1], &tree->object.oid,
844844
tree->buffer, tree->size);
@@ -913,7 +913,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
913913
work,
914914
old_tree);
915915
if (ret < 0)
916-
exit(128);
916+
die(NULL);
917917
ret = reset_tree(new_tree,
918918
opts, 0,
919919
writeout_error, new_branch_info);

builtin/fetch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,7 @@ static int update_local_ref(struct ref *ref,
992992
fast_forward = repo_in_merge_bases(the_repository, current,
993993
updated);
994994
if (fast_forward < 0)
995-
exit(128);
995+
die(NULL);
996996
forced_updates_ms += (getnanotime() - t_before) / 1000000;
997997
} else {
998998
fast_forward = 1;

builtin/submodule--helper.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ static void runcommand_in_submodule_cb(const struct cache_entry *list_item,
303303
char *displaypath;
304304

305305
if (validate_submodule_path(path) < 0)
306-
exit(128);
306+
die(NULL);
307307

308308
displaypath = get_submodule_displaypath(path, info->prefix,
309309
info->super_prefix);
@@ -643,7 +643,7 @@ static void status_submodule(const char *path, const struct object_id *ce_oid,
643643
};
644644

645645
if (validate_submodule_path(path) < 0)
646-
exit(128);
646+
die(NULL);
647647

648648
if (!submodule_from_path(the_repository, null_oid(the_hash_algo), path))
649649
die(_("no submodule mapping found in .gitmodules for path '%s'"),
@@ -1257,7 +1257,7 @@ static void sync_submodule(const char *path, const char *prefix,
12571257
return;
12581258

12591259
if (validate_submodule_path(path) < 0)
1260-
exit(128);
1260+
die(NULL);
12611261

12621262
sub = submodule_from_path(the_repository, null_oid(the_hash_algo), path);
12631263

@@ -1402,7 +1402,7 @@ static void deinit_submodule(const char *path, const char *prefix,
14021402
char *sub_git_dir = xstrfmt("%s/.git", path);
14031403

14041404
if (validate_submodule_path(path) < 0)
1405-
exit(128);
1405+
die(NULL);
14061406

14071407
sub = submodule_from_path(the_repository, null_oid(the_hash_algo), path);
14081408

@@ -1724,7 +1724,7 @@ static int clone_submodule(const struct module_clone_data *clone_data,
17241724
char *to_free = NULL;
17251725

17261726
if (validate_submodule_path(clone_data_path) < 0)
1727-
exit(128);
1727+
die(NULL);
17281728

17291729
if (!is_absolute_path(clone_data->path))
17301730
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,
35243524
strip_dir_trailing_slashes(add_data.sm_path);
35253525

35263526
if (validate_submodule_path(add_data.sm_path) < 0)
3527-
exit(128);
3527+
die(NULL);
35283528

35293529
die_on_index_match(add_data.sm_path, force);
35303530
die_on_repo_without_commits(add_data.sm_path);

usage.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ static NORETURN void usage_builtin(const char *err, va_list params)
6767

6868
static void die_message_builtin(const char *err, va_list params)
6969
{
70+
if (!err)
71+
return;
7072
trace2_cmd_error_va(err, params);
7173
vreportf(_("fatal: "), err, params);
7274
}

0 commit comments

Comments
 (0)