Skip to content

Commit 63ca1c0

Browse files
0xAXgitster
authored andcommitted
git.c: simplify stripping extension of a file in handle_builtin()
The handle_builtin() starts from stripping of command extension if STRIP_EXTENSION is enabled. Actually STRIP_EXTENSION does not used anywhere else. This patch introduces strip_extension() helper to strip STRIP_EXTENSION extension from argv[0] with the strip_suffix() instead of manually stripping. Signed-off-by: Alexander Kuleshov <[email protected]> Helped-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent be08dee commit 63ca1c0

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

git-compat-util.h

-4
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,6 @@ extern char *gitbasename(char *);
319319
#define _PATH_DEFPATH "/usr/local/bin:/usr/bin:/bin"
320320
#endif
321321

322-
#ifndef STRIP_EXTENSION
323-
#define STRIP_EXTENSION ""
324-
#endif
325-
326322
#ifndef has_dos_drive_prefix
327323
static inline int git_has_dos_drive_prefix(const char *path)
328324
{

git.c

+15-11
Original file line numberDiff line numberDiff line change
@@ -505,21 +505,25 @@ int is_builtin(const char *s)
505505
return !!get_builtin(s);
506506
}
507507

508+
#ifdef STRIP_EXTENSION
509+
static void strip_extension(const char **argv)
510+
{
511+
size_t len;
512+
513+
if (strip_suffix(argv[0], STRIP_EXTENSION, &len))
514+
argv[0] = xmemdupz(argv[0], len);
515+
}
516+
#else
517+
#define strip_extension(cmd)
518+
#endif
519+
508520
static void handle_builtin(int argc, const char **argv)
509521
{
510-
const char *cmd = argv[0];
511-
int i;
512-
static const char ext[] = STRIP_EXTENSION;
522+
const char *cmd;
513523
struct cmd_struct *builtin;
514524

515-
if (sizeof(ext) > 1) {
516-
i = strlen(argv[0]) - strlen(ext);
517-
if (i > 0 && !strcmp(argv[0] + i, ext)) {
518-
char *argv0 = xstrdup(argv[0]);
519-
argv[0] = cmd = argv0;
520-
argv0[i] = '\0';
521-
}
522-
}
525+
strip_extension(argv);
526+
cmd = argv[0];
523527

524528
/* Turn "git cmd --help" into "git help cmd" */
525529
if (argc > 1 && !strcmp(argv[1], "--help")) {

0 commit comments

Comments
 (0)