@matheusbsbs / matheus-bagliano / commits / 113106e06c
Fork 0

http.c: use strbuf API in quote_ref_url

In addition, ''quote_ref_url'' inserts a slash between the base URL and remote ref path only if needed. Previously, this insertion wasn't contingent on the lack of a separating slash. Signed-off-by: Tay Ray Chuan <rctay89@gmail.com> Acked-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Tay Ray Chuan committed Mar 8, 2009 at 00:47 UTC 113106e06c48cc80432fd1be8af912898e8f240e
1 file changed +10 -20
http.c
+10 -20
@@ -573,31 +573,21 @@ static inline int hex(int v)
573
574 static char *quote_ref_url(const char *base, const char *ref)
575 {
576 + struct strbuf buf = STRBUF_INIT;
577 const char *cp;
577 - char *dp, *qref;
578 - int len, baselen, ch;
578 + int ch;
579
580 - baselen = strlen(base);
581 - len = baselen + 2; /* '/' after base and terminating NUL */
582 - for (cp = ref; (ch = *cp) != 0; cp++, len++)
580 + strbuf_addstr(&buf, base);
581 + if (buf.len && buf.buf[buf.len - 1] != '/' && *ref != '/')
582 + strbuf_addstr(&buf, "/");
583 +
584 + for (cp = ref; (ch = *cp) != 0; cp++)
585 if (needs_quote(ch))
584 - len += 2; /* extra two hex plus replacement % */
585 - qref = xmalloc(len);
586 - memcpy(qref, base, baselen);
587 - dp = qref + baselen;
588 - *(dp++) = '/';
589 - for (cp = ref; (ch = *cp) != 0; cp++) {
590 - if (needs_quote(ch)) {
591 - *dp++ = '%';
592 - *dp++ = hex((ch >> 4) & 0xF);
593 - *dp++ = hex(ch & 0xF);
594 - }
586 + strbuf_addf(&buf, "%%%02x", ch);
587 else
596 - *dp++ = ch;
597 - }
598 - *dp = 0;
588 + strbuf_addch(&buf, *cp);
589
600 - return qref;
590 + return strbuf_detach(&buf, NULL);
591 }
592
593 int http_fetch_ref(const char *base, struct ref *ref)