Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch 'ml/connect-refactor'

* ml/connect-refactor:
  connect.c: move duplicated code to a new function 'get_host_and_port'
  • Loading branch information...
commit a0626bcc66828dbfd4a2f6d18eb52a411fc268ae 2 parents 34c014d + 72a534d
Junio C Hamano gitster authored

Showing 1 changed file with 29 additions and 54 deletions. Show diff stats Hide diff stats

  1. +29 54 connect.c
83 connect.c
@@ -152,6 +152,28 @@ static enum protocol get_protocol(const char *name)
152 152 #define STR_(s) # s
153 153 #define STR(s) STR_(s)
154 154
  155 +static void get_host_and_port(char **host, const char **port)
  156 +{
  157 + char *colon, *end;
  158 +
  159 + if (*host[0] == '[') {
  160 + end = strchr(*host + 1, ']');
  161 + if (end) {
  162 + *end = 0;
  163 + end++;
  164 + (*host)++;
  165 + } else
  166 + end = *host;
  167 + } else
  168 + end = *host;
  169 + colon = strchr(end, ':');
  170 +
  171 + if (colon) {
  172 + *colon = 0;
  173 + *port = colon + 1;
  174 + }
  175 +}
  176 +
155 177 #ifndef NO_IPV6
156 178
157 179 static const char *ai_name(const struct addrinfo *ai)
@@ -170,30 +192,14 @@ static const char *ai_name(const struct addrinfo *ai)
170 192 static int git_tcp_connect_sock(char *host, int flags)
171 193 {
172 194 int sockfd = -1, saved_errno = 0;
173   - char *colon, *end;
174 195 const char *port = STR(DEFAULT_GIT_PORT);
175 196 struct addrinfo hints, *ai0, *ai;
176 197 int gai;
177 198 int cnt = 0;
178 199
179   - if (host[0] == '[') {
180   - end = strchr(host + 1, ']');
181   - if (end) {
182   - *end = 0;
183   - end++;
184   - host++;
185   - } else
186   - end = host;
187   - } else
188   - end = host;
189   - colon = strchr(end, ':');
190   -
191   - if (colon) {
192   - *colon = 0;
193   - port = colon + 1;
194   - if (!*port)
195   - port = "<none>";
196   - }
  200 + get_host_and_port(&host, &port);
  201 + if (!*port)
  202 + port = "<none>";
197 203
198 204 memset(&hints, 0, sizeof(hints));
199 205 hints.ai_socktype = SOCK_STREAM;
@@ -251,30 +257,15 @@ static int git_tcp_connect_sock(char *host, int flags)
251 257 static int git_tcp_connect_sock(char *host, int flags)
252 258 {
253 259 int sockfd = -1, saved_errno = 0;
254   - char *colon, *end;
255   - char *port = STR(DEFAULT_GIT_PORT), *ep;
  260 + const char *port = STR(DEFAULT_GIT_PORT);
  261 + char *ep;
256 262 struct hostent *he;
257 263 struct sockaddr_in sa;
258 264 char **ap;
259 265 unsigned int nport;
260 266 int cnt;
261 267
262   - if (host[0] == '[') {
263   - end = strchr(host + 1, ']');
264   - if (end) {
265   - *end = 0;
266   - end++;
267   - host++;
268   - } else
269   - end = host;
270   - } else
271   - end = host;
272   - colon = strchr(end, ':');
273   -
274   - if (colon) {
275   - *colon = 0;
276   - port = colon + 1;
277   - }
  268 + get_host_and_port(&host, &port);
278 269
279 270 if (flags & CONNECT_VERBOSE)
280 271 fprintf(stderr, "Looking up %s ... ", host);
@@ -406,26 +397,10 @@ static int git_use_proxy(const char *host)
406 397 static void git_proxy_connect(int fd[2], char *host)
407 398 {
408 399 const char *port = STR(DEFAULT_GIT_PORT);
409   - char *colon, *end;
410 400 const char *argv[4];
411 401 struct child_process proxy;
412 402
413   - if (host[0] == '[') {
414   - end = strchr(host + 1, ']');
415   - if (end) {
416   - *end = 0;
417   - end++;
418   - host++;
419   - } else
420   - end = host;
421   - } else
422   - end = host;
423   - colon = strchr(end, ':');
424   -
425   - if (colon) {
426   - *colon = 0;
427   - port = colon + 1;
428   - }
  403 + get_host_and_port(&host, &port);
429 404
430 405 argv[0] = git_proxy_command;
431 406 argv[1] = host;

0 comments on commit a0626bc

Please sign in to comment.
Something went wrong with that request. Please try again.