git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alexander Kuleshov <kuleshovmail@gmail.com>
To: Junio C Hamano <gitster@pobox.com>
Cc: "git @ vger . kernel . org" <git@vger.kernel.org>,
	Alexander Kuleshov <kuleshovmail@gmail.com>
Subject: [PATCH] git-compat-util.h: move extension stripping from handle_builtin()
Date: Sat, 20 Feb 2016 14:10:58 +0600	[thread overview]
Message-ID: <1455955858-30081-1-git-send-email-kuleshovmail@gmail.com> (raw)

The handle_builtin() starts from striping of command extension if
STRIP_EXTENSION is enabled. As this is an OS dependent, let's move
to the git-compat-util.h as all similar functions to do handle_builtin()
more cleaner.
---
 git-compat-util.h | 18 ++++++++++++++++++
 git.c             | 13 +------------
 2 files changed, 19 insertions(+), 12 deletions(-)

diff --git a/git-compat-util.h b/git-compat-util.h
index 658d03b..57f2fda 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -323,6 +323,24 @@ extern char *gitbasename(char *);
 
 #ifndef STRIP_EXTENSION
 #define STRIP_EXTENSION ""
+static inline const char *strip_extension(const char **argv)
+{
+	return argv[0];
+}
+#else
+static inline const char *strip_extension(const char **argv)
+{
+	static const char ext[] = STRIP_EXTENSION;
+	int ext_len = strlen(argv[0]) - strlen(ext);
+
+	if (ext_len > 0 && !strcmp(argv[0] + ext_len, ext)) {
+		char *argv0 = xstrdup(argv[0]);
+		argv[0] = argv0;
+		argv0[ext_len] = '\0';
+	}
+
+	return argv[0];
+}
 #endif
 
 #ifndef has_dos_drive_prefix
diff --git a/git.c b/git.c
index 8751ef0..a4d2a46 100644
--- a/git.c
+++ b/git.c
@@ -506,19 +506,8 @@ int is_builtin(const char *s)
 
 static void handle_builtin(int argc, const char **argv)
 {
-	const char *cmd = argv[0];
-	int i;
-	static const char ext[] = STRIP_EXTENSION;
 	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';
-		}
-	}
+	const char *cmd = strip_extension(argv);
 
 	/* Turn "git cmd --help" into "git help cmd" */
 	if (argc > 1 && !strcmp(argv[1], "--help")) {
-- 
2.4.4.764.gf6c74eb.dirty

             reply	other threads:[~2016-02-20  8:15 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-20  8:10 Alexander Kuleshov [this message]
2016-02-20  8:48 ` [PATCH] git-compat-util.h: move extension stripping from handle_builtin() Jeff King
2016-02-20 12:35   ` Alexander Kuleshov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1455955858-30081-1-git-send-email-kuleshovmail@gmail.com \
    --to=kuleshovmail@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).