Skip to contentSkip to conversation

Removed unnecessary use of global variables.

PATCH In review Erik Faye-Lund 5 participants 6 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Removed unnecessary use of global variables.

git_config() now takes a third data-parameter that is passed back to the callback-function. At the time this code was written, that parameter did not exist, so a somewhat nasty (but by all means correct) use of global variables was introduced. In commit ef90d6d4208a5130185b04f06e5f90a5f9959fe3 Johannes Schindelin <[email protected]> introduced a parameter for similar purposes.

I've changed the code to utilize this parameter to pass the string. In addition, I've made the function calculate the string length on usage instead, to reduce the parameters needed to what the callback-interface supplies.

---
 connect.c |   16 ++++++----------
 1 files changed, 6 insertions(+), 10 deletions(-)
connect.c +6 -10
index 2f23ab3..98fbaea 100644
@@ -371,14 +371,13 @@ static void git_tcp_connect(int fd[2], char *host, int flags)
 	fd[1] = dup(sockfd);
 }
 
-
 static char *git_proxy_command;
-static const char *rhost_name;
-static int rhost_len;
-
 static int git_proxy_command_options(const char *var, const char *value,
-		void *cb)
+		void *data)
 {
+	const char *rhost_name = data;
+	const size_t rhost_len = strlen(rhost_name);
+
 	if (!strcmp(var, "core.gitproxy")) {
 		const char *for_pos;
 		int matchlen = -1;
@@ -421,16 +420,13 @@ static int git_proxy_command_options(const char *var, const char *value,
 		return 0;
 	}
 
-	return git_default_config(var, value, cb);
+	return git_default_config(var, value, data);
 }
 
 static int git_use_proxy(const char *host)
 {
-	rhost_name = host;
-	rhost_len = strlen(host);
 	git_proxy_command = getenv("GIT_PROXY_COMMAND");
-	git_config(git_proxy_command_options, NULL);
-	rhost_name = NULL;
+	git_config(git_proxy_command_options, (void*)host);
 	return (git_proxy_command && *git_proxy_command);
 }
 
Signature · 1 line
1.6.2.GIT
#1 lore raw

Sorry about the whole double-post thing, this whole submitting patches over email thing is new to me, and I'm making too many mistakes right now. Hopefully, I'll improve in the future ;)

Quoted 67 lines · Erik Faye-Lund

git_config() now takes a third data-parameter that is passed back to the callback-function. At the time this code was written, that parameter did not exist, so a somewhat nasty (but by all means correct) use of global variables was introduced. In commit ef90d6d4208a5130185b04f06e5f90a5f9959fe3 Johannes Schindelin <[email protected]> introduced a parameter for similar purposes.

I've changed the code to utilize this parameter to pass the string. In addition, I've made the function calculate the string length on usage instead, to reduce the parameters needed to what the callback-interface supplies.

---
 connect.c |   16 ++++++----------

 1 files changed, 6 insertions(+), 10 deletions(-)
connect.c +6 -11
index 2f23ab3..98fbaea 100644
@@ -371,14 +371,13 @@ static void git_tcp_connect(int fd[2], char *host, int flags)
         fd[1] = dup(sockfd);
  }
 
-
  static char *git_proxy_command;
-static const char *rhost_name;
-static int rhost_len;
-
  static int git_proxy_command_options(const char *var, const char *value,
-               void *cb)
+               void *data)
  {
+       const char *rhost_name = data;
+       const size_t rhost_len = strlen(rhost_name);
+
         if (!strcmp(var, "core.gitproxy")) {
                 const char *for_pos;
                 int matchlen = -1;
@@ -421,16 +420,13 @@ static int git_proxy_command_options(const char *var, const char *value,
                 return 0;
         }
 
-       return git_default_config(var, value, cb);
+       return git_default_config(var, value, data);
  }
 
  static int git_use_proxy(const char *host)
  {
-       rhost_name = host;
-       rhost_len = strlen(host);
         git_proxy_command = getenv("GIT_PROXY_COMMAND");
-       git_config(git_proxy_command_options, NULL);
-       rhost_name = NULL;
+       git_config(git_proxy_command_options, (void*)host);
         return (git_proxy_command && *git_proxy_command);
  }
 
--

1.6.2.GIT

--
To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

#2 lore raw

Hi,

Quoted 3 lines · Erik Faye-Lund

Sorry about the whole double-post thing, this whole submitting patches over email thing is new to me, and I'm making too many mistakes right now. Hopefully, I'll improve in the future ;)

No need to be sorry. Thanks for your contribution!

Ciao,
Dscho

#3 lore raw
In reply to Erik Faye-Lund

Hi,

Quoted 7 lines · Erik Faye-Lund

git_config() now takes a third data-parameter that is passed back to the callback-function. At the time this code was written, that parameter did not exist, so a somewhat nasty (but by all means correct) use of global variables was introduced. In commit ef90d6d4208a5130185b04f06e5f90a5f9959fe3 Johannes Schindelin <[email protected]> introduced a parameter for similar purposes.

We tend to quote commits in this form: ef90d6d(Provide git_config with a callback-data parameter)

Quoted 4 lines

I've changed the code to utilize this parameter to pass the string. In addition, I've made the function calculate the string length on usage instead, to reduce the parameters needed to what the callback-interface supplies.

Usually, commit messages are held in a more imperative form than a subjective one:

Utilize this parameter to pass the string.

Quoted 10 lines
connect.c +0 -1
index 2f23ab3..98fbaea 100644
@@ -371,14 +371,13 @@ static void git_tcp_connect(int fd[2], char *host, int flags)
 	fd[1] = dup(sockfd);
 }
 
-
 static char *git_proxy_command;

connect.c static void git_tcp_connect(int fd[2], char *host, int flags) · line 380

Quoted 10 lines
connect.c +4 -4
-static const char *rhost_name;
-static int rhost_len;
-
 static int git_proxy_command_options(const char *var, const char *value,
-		void *cb)
+		void *data)
 {
+	const char *rhost_name = data;
+	const size_t rhost_len = strlen(rhost_name);
+

git_proxy_command_options is called for each and every config variable. The idea of having the length in a local variable was to avoid recalculating the length each and every time. I think I'd actually use a strbuf for that.

BTW I would not rename "cb", as it is only distracting the reader of the patch.

But I like the idea of your patch (as you can see from me replying ;-)

Thanks,
Dscho

#4 lore raw

Quoting Johannes Schindelin <[email protected]>:

Quoted 12 lines

Hi,

Quoted 7 lines · Erik Faye-Lund

git_config() now takes a third data-parameter that is passed back to the callback-function. At the time this code was written, that parameter did not exist, so a somewhat nasty (but by all means correct) use of global variables was introduced. In commit ef90d6d4208a5130185b04f06e5f90a5f9959fe3 Johannes Schindelin <[email protected]> introduced a parameter for similar purposes.

Quoted 2 lines

We tend to quote commits in this form: ef90d6d(Provide git_config with a callback-data parameter)

Your review comments are a subset of the ones Junio sent about 8 hours before you did, and are almost identical except for the comment on the subject line yours didn't have. I'm curious about two things.

1. Are you and Junio one same person, and if so what made you change your mind during these 8 hours ;-)?

2. Junio said "ef90d6d (Provide git_config with a callback-data parameter, 2008-05-14)" and yours is slightly different. Both are equally readable but I think it would help to make sure everybody uses the same format within one project. Do we need a helper command that everybody can use?

Signature · 2 lines
Nanako Shiraishi
http://ivory.ap.teacup.com/nanako3/
#5 lore raw
Quoted 1 line · Nanako Shiraishi

1. Are you and Junio one same person, and if so what made you change your mind during these 8 hours ;-)?

Last year during Gittogether, Dscho and I were seen in a room sitting parhaps 4 meters apart; I suspect we are different people.

http://picasaweb.google.com/dsymonds/GitTogether08#5263025230667494002

Quoted 1 line

2. Junio said "ef90d6d (Provide git_config with a callback-data parameter, 2008-05-14)" and yours is slightly different. Both are equally readable but I think it would help to make sure everybody uses the same format within one project. Do we need a helper command that everybody can use?

I use these two aliases while I am writing e-mails quite often:

[alias]

	one = "!sh -c 'git show -s --pretty=\"format:%h (%s, %ai\" \"$@\" | sed -e \"s/ [012][0-9]:[0-5][0-9]:[0-5][0-9] [-+][0-9][0-9][0-9][0-9]$/)/\"' -"
	who = "!sh -c 'git log -1 --pretty=\"format:%an <%ae>\" --author=\"$1\"' -"