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 <kuleshovmail@gmail.com> Helped-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Alexander Kuleshov committed
Feb 22, 2016 at 13:18 UTC
63ca1c099c36e61b0e9cd7fa0b912c0b9d89b628
2 files changed
+15
-15
git-compat-util.h
-4
@@ -319,10 +319,6 @@ extern char *gitbasename(char *);
319
#define _PATH_DEFPATH "/usr/local/bin:/usr/bin:/bin"
320
#endif
321
322
-#ifndef STRIP_EXTENSION
323
-#define STRIP_EXTENSION ""
324
-#endif
325
-
322
#ifndef has_dos_drive_prefix
323
static inline int git_has_dos_drive_prefix(const char *path)
324
{
git.c
+15
-11
@@ -505,21 +505,25 @@ int is_builtin(const char *s)
505
return !!get_builtin(s);
506
}
507
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
+
520
static void handle_builtin(int argc, const char **argv)
521
{
510
- const char *cmd = argv[0];
511
- int i;
512
- static const char ext[] = STRIP_EXTENSION;
522
+ const char *cmd;
523
struct cmd_struct *builtin;
524
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];
527
528
/* Turn "git cmd --help" into "git help cmd" */
529
if (argc > 1 && !strcmp(argv[1], "--help")) {