]> git.ipfire.org Git - thirdparty/git.git/commit - builtin/describe.c
describe: do not use cmd_*() as a subroutine
authorJunio C Hamano <gitster@pobox.com>
Tue, 10 Oct 2017 03:42:58 +0000 (12:42 +0900)
committerJunio C Hamano <gitster@pobox.com>
Wed, 11 Oct 2017 06:01:37 +0000 (15:01 +0900)
commitbe26d2b29b6a58fc53ed9d38c3f36b195f8a7cc9
treee8875d8ec57b73010cabbe1c2b39e5eca4740559
parent4010f1d1b782eb7585e0e0abcefa794bd5ff29a0
describe: do not use cmd_*() as a subroutine

The cmd_foo() function is a moral equivalent of 'main' for a Git
subcommand 'git foo', and as such, it is allowed to do many things
that make it unsuitable to be called as a subroutine, including

 - call exit(3) to terminate the process;

 - allocate resource held and used throughout its lifetime, without
   releasing it upon return/exit;

 - rely on global variables being initialized at program startup,
   and update them as needed, making another clean invocation of the
   function impossible.

The call to cmd_diff_index() "git describe" makes has been working
by accident that the function did not call exit(3); it sets a bad
precedent for people to cut and paste.

We could invoke it via the run_command() interface, but the diff
family of commands have helper functions in diff-lib.c that are
meant to be usable as subroutines, and using the latter does not
make the resulting code all that longer.  Use it.

Note that there is also an invocation of cmd_name_rev() at the end;
"git describe --contains" massages its command line arguments to be
suitable for "git name-rev" invocation and jumps to it, never to
regain control.  This call is left as-is as an exception to the
rule.  When we start to allow calling name-rev repeatedly as a
helper function, we would be able to remove this call as well.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/describe.c