From: Alexander Kuleshov <kuleshovmail@gmail.com>
To: JunioCHamanogitster@pobox.com
Cc: Git <git@vger.kernel.org>, Alexander Kuleshov <kuleshovmail@gmail.com>
Subject: [PATCH] environment.c: introduce DECLARE_GIT_GETTER helper macro
Date: Sun, 28 Feb 2016 01:35:44 +0600 [thread overview]
Message-ID: <1456601744-18404-1-git-send-email-kuleshovmail@gmail.com> (raw)
The environment.c contans a couple of functions which are
consist from the following pattern:
if (!env)
setup_git_env();
return env;
Let's move declaration of these functions to the DECLARE_GIT_GETTER
helper macro to prevent code duplication.
Signed-off-by: Alexander Kuleshov <kuleshovmail@gmail.com>
---
environment.c | 49 ++++++++++++++-----------------------------------
1 file changed, 14 insertions(+), 35 deletions(-)
diff --git a/environment.c b/environment.c
index 6dec9d0..f10fc7a 100644
--- a/environment.c
+++ b/environment.c
@@ -126,6 +126,14 @@ const char * const local_repo_env[] = {
NULL
};
+#define DECLARE_GIT_GETTER(type, name, env) \
+ type name(void) \
+ { \
+ if (!env) \
+ setup_git_env(); \
+ return env; \
+ }
+
static char *expand_namespace(const char *raw_namespace)
{
struct strbuf buf = STRBUF_INIT;
@@ -197,25 +205,11 @@ int is_bare_repository(void)
return is_bare_repository_cfg && !get_git_work_tree();
}
-const char *get_git_dir(void)
-{
- if (!git_dir)
- setup_git_env();
- return git_dir;
-}
-
const char *get_git_common_dir(void)
{
return git_common_dir;
}
-const char *get_git_namespace(void)
-{
- if (!namespace)
- setup_git_env();
- return namespace;
-}
-
const char *strip_namespace(const char *namespaced_ref)
{
if (!starts_with(namespaced_ref, get_git_namespace()))
@@ -249,13 +243,6 @@ const char *get_git_work_tree(void)
return work_tree;
}
-char *get_object_directory(void)
-{
- if (!git_object_dir)
- setup_git_env();
- return git_object_dir;
-}
-
int odb_mkstemp(char *template, size_t limit, const char *pattern)
{
int fd;
@@ -293,20 +280,6 @@ int odb_pack_keep(char *name, size_t namesz, const unsigned char *sha1)
return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
}
-char *get_index_file(void)
-{
- if (!git_index_file)
- setup_git_env();
- return git_index_file;
-}
-
-char *get_graft_file(void)
-{
- if (!git_graft_file)
- setup_git_env();
- return git_graft_file;
-}
-
int set_git_dir(const char *path)
{
if (setenv(GIT_DIR_ENVIRONMENT, path, 1))
@@ -325,3 +298,9 @@ const char *get_commit_output_encoding(void)
{
return git_commit_encoding ? git_commit_encoding : "UTF-8";
}
+
+DECLARE_GIT_GETTER(const char *, get_git_dir, git_dir)
+DECLARE_GIT_GETTER(const char *, get_git_namespace, namespace)
+DECLARE_GIT_GETTER(char *, get_object_directory, git_object_dir)
+DECLARE_GIT_GETTER(char *, get_index_file, git_index_file)
+DECLARE_GIT_GETTER(char *, get_graft_file, git_graft_file)
--
2.8.0.rc0.142.g64f2103.dirty
next reply other threads:[~2016-02-27 19:40 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-02-27 19:35 Alexander Kuleshov [this message]
2016-03-01 15:05 ` [PATCH] environment.c: introduce DECLARE_GIT_GETTER helper macro Jeff King
2016-03-01 18:14 ` Junio C Hamano
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=1456601744-18404-1-git-send-email-kuleshovmail@gmail.com \
--to=kuleshovmail@gmail.com \
--cc=JunioCHamanogitster@pobox.com \
--cc=git@vger.kernel.org \
/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).