git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] git.c: simplify striping extension of a file in handle_builtin()
@ 2016-02-20 14:27 Alexander Kuleshov
  2016-02-22  0:20 ` Eric Sunshine
  0 siblings, 1 reply; 2+ messages in thread
From: Alexander Kuleshov @ 2016-02-20 14:27 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git @ vger . kernel . org, Alexander Kuleshov

The handle_builtin() starts from striping 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
striping.

Signed-off-by: Alexander Kuleshov <kuleshovmail@gmail.com>
Helped-by: Jeff King <peff@peff.net>
---
 git-compat-util.h |  4 ----
 git.c             | 26 +++++++++++++++-----------
 2 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/git-compat-util.h b/git-compat-util.h
index 8f0e76b..b35251c 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -333,10 +333,6 @@ extern char *gitdirname(char *);
 #define _PATH_DEFPATH "/usr/local/bin:/usr/bin:/bin"
 #endif
 
-#ifndef STRIP_EXTENSION
-#define STRIP_EXTENSION ""
-#endif
-
 #ifndef has_dos_drive_prefix
 static inline int git_has_dos_drive_prefix(const char *path)
 {
diff --git a/git.c b/git.c
index 087ad31..6cc0c07 100644
--- a/git.c
+++ b/git.c
@@ -509,21 +509,25 @@ int is_builtin(const char *s)
 	return !!get_builtin(s);
 }
 
+#ifdef STRIP_EXTENSION
+static void strip_extension(const char **argv)
+{
+	size_t len;
+
+	if (strip_suffix(argv[0], STRIP_EXTENSION, &len))
+		argv[0] = xmemdupz(argv[0], len);
+}
+#else
+#define strip_extension(cmd)
+#endif
+
 static void handle_builtin(int argc, const char **argv)
 {
-	const char *cmd = argv[0];
-	int i;
-	static const char ext[] = STRIP_EXTENSION;
+	const char *cmd;
 	struct cmd_struct *builtin;
 
-	if (sizeof(ext) > 1) {
-		i = strlen(argv[0]) - strlen(ext);
-		if (i > 0 && !strcmp(argv[0] + i, ext)) {
-			char *argv0 = xstrdup(argv[0]);
-			argv[0] = cmd = argv0;
-			argv0[i] = '\0';
-		}
-	}
+	strip_extension(argv);
+	cmd = argv[0];
 
 	/* Turn "git cmd --help" into "git help cmd" */
 	if (argc > 1 && !strcmp(argv[1], "--help")) {
-- 
2.5.0

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] git.c: simplify striping extension of a file in handle_builtin()
  2016-02-20 14:27 [PATCH] git.c: simplify striping extension of a file in handle_builtin() Alexander Kuleshov
@ 2016-02-22  0:20 ` Eric Sunshine
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Sunshine @ 2016-02-22  0:20 UTC (permalink / raw)
  To: Alexander Kuleshov; +Cc: Junio C Hamano, git @ vger . kernel . org

On Sat, Feb 20, 2016 at 9:27 AM, Alexander Kuleshov
<kuleshovmail@gmail.com> wrote:
> git.c: simplify striping extension of a file in handle_builtin()

s/striping/stripping/g

(Note the '/g' above.)

The patch itself looks okay.

> The handle_builtin() starts from striping 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
> striping.
>
> Signed-off-by: Alexander Kuleshov <kuleshovmail@gmail.com>
> Helped-by: Jeff King <peff@peff.net>
> ---
> diff --git a/git-compat-util.h b/git-compat-util.h
> @@ -333,10 +333,6 @@ extern char *gitdirname(char *);
>  #define _PATH_DEFPATH "/usr/local/bin:/usr/bin:/bin"
>  #endif
>
> -#ifndef STRIP_EXTENSION
> -#define STRIP_EXTENSION ""
> -#endif
> -
>  #ifndef has_dos_drive_prefix
>  static inline int git_has_dos_drive_prefix(const char *path)
>  {
> diff --git a/git.c b/git.c
> index 087ad31..6cc0c07 100644
> --- a/git.c
> +++ b/git.c
> @@ -509,21 +509,25 @@ int is_builtin(const char *s)
>         return !!get_builtin(s);
>  }
>
> +#ifdef STRIP_EXTENSION
> +static void strip_extension(const char **argv)
> +{
> +       size_t len;
> +
> +       if (strip_suffix(argv[0], STRIP_EXTENSION, &len))
> +               argv[0] = xmemdupz(argv[0], len);
> +}
> +#else
> +#define strip_extension(cmd)
> +#endif
> +
>  static void handle_builtin(int argc, const char **argv)
>  {
> -       const char *cmd = argv[0];
> -       int i;
> -       static const char ext[] = STRIP_EXTENSION;
> +       const char *cmd;
>         struct cmd_struct *builtin;
>
> -       if (sizeof(ext) > 1) {
> -               i = strlen(argv[0]) - strlen(ext);
> -               if (i > 0 && !strcmp(argv[0] + i, ext)) {
> -                       char *argv0 = xstrdup(argv[0]);
> -                       argv[0] = cmd = argv0;
> -                       argv0[i] = '\0';
> -               }
> -       }
> +       strip_extension(argv);
> +       cmd = argv[0];
>
>         /* Turn "git cmd --help" into "git help cmd" */
>         if (argc > 1 && !strcmp(argv[1], "--help")) {
> --
> 2.5.0

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-02-22  0:20 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-02-20 14:27 [PATCH] git.c: simplify striping extension of a file in handle_builtin() Alexander Kuleshov
2016-02-22  0:20 ` Eric Sunshine

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).