Skip to content

Commit be26d2b

Browse files
committed
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>
1 parent 4010f1d commit be26d2b

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

builtin/describe.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
#include "builtin.h"
88
#include "exec_cmd.h"
99
#include "parse-options.h"
10+
#include "revision.h"
1011
#include "diff.h"
1112
#include "hashmap.h"
1213
#include "argv-array.h"
1314
#include "run-command.h"
1415

15-
#define SEEN (1u << 0)
1616
#define MAX_TAGS (FLAG_BITS - 1)
1717

1818
static const char * const describe_usage[] = {
@@ -532,7 +532,9 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
532532
}
533533
} else if (dirty) {
534534
static struct lock_file index_lock;
535-
int fd;
535+
struct rev_info revs;
536+
struct argv_array args = ARGV_ARRAY_INIT;
537+
int fd, result;
536538

537539
read_cache_preload(NULL);
538540
refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED,
@@ -541,8 +543,13 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
541543
if (0 <= fd)
542544
update_index_if_able(&the_index, &index_lock);
543545

544-
if (!cmd_diff_index(ARRAY_SIZE(diff_index_args) - 1,
545-
diff_index_args, prefix))
546+
init_revisions(&revs, prefix);
547+
argv_array_pushv(&args, diff_index_args);
548+
if (setup_revisions(args.argc, args.argv, &revs, NULL) != 1)
549+
BUG("malformed internal diff-index command line");
550+
result = run_diff_index(&revs, 0);
551+
552+
if (!diff_result_code(&revs.diffopt, result))
546553
suffix = NULL;
547554
else
548555
suffix = dirty;

0 commit comments

Comments
 (0)