Quote reference names while fetching with curl.
curl_escape ought to do this, but we should not let it quote slashes (nobody said refs/tags cannot have subdirectories), so we roll our own safer version. With this, the last part of git-clone from Martin's moodle repository that used to fail now works, which reads: $ git-http-fetch -v -a -w 'tags/MOODLE_15_MERGED **INVALID**' \ 'tags/MOODLE_15_MERGED **INVALID**' \ http://locke.catalyst.net.nz/git/moodle.git/ Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Oct 10, 2005 at 23:22 UTC
94fa447ace56bc6514bdd66cafe8f56f0e3ce936
1 file changed
+47
-8
http-fetch.c
+47
-8
@@ -969,9 +969,54 @@ int fetch(unsigned char *sha1)
969
alt->base);
970
}
971
972
+static inline int needs_quote(int ch)
973
+{
974
+ switch (ch) {
975
+ case '/': case '-': case '.':
976
+ case 'A'...'Z': case 'a'...'z': case '0'...'9':
977
+ return 0;
978
+ default:
979
+ return 1;
980
+ }
981
+}
982
+
983
+static inline int hex(int v)
984
+{
985
+ if (v < 10) return '0' + v;
986
+ else return 'A' + v - 10;
987
+}
988
+
989
+static char *quote_ref_url(const char *base, const char *ref)
990
+{
991
+ const char *cp;
992
+ char *dp, *qref;
993
+ int len, baselen, ch;
994
+
995
+ baselen = strlen(base);
996
+ len = baselen + 6; /* "refs/" + NUL */
997
+ for (cp = ref; (ch = *cp) != 0; cp++, len++)
998
+ if (needs_quote(ch))
999
+ len += 2; /* extra two hex plus replacement % */
1000
+ qref = xmalloc(len);
1001
+ memcpy(qref, base, baselen);
1002
+ memcpy(qref + baselen, "refs/", 5);
1003
+ for (cp = ref, dp = qref + baselen + 5; (ch = *cp) != 0; cp++) {
1004
+ if (needs_quote(ch)) {
1005
+ *dp++ = '%';
1006
+ *dp++ = hex((ch >> 4) & 0xF);
1007
+ *dp++ = hex(ch & 0xF);
1008
+ }
1009
+ else
1010
+ *dp++ = ch;
1011
+ }
1012
+ *dp = 0;
1013
+
1014
+ return qref;
1015
+}
1016
+
1017
int fetch_ref(char *ref, unsigned char *sha1)
1018
{
974
- char *url, *posn;
1019
+ char *url;
1020
char hex[42];
1021
struct buffer buffer;
1022
char *base = alt->base;
@@ -981,13 +1026,7 @@ int fetch_ref(char *ref, unsigned char *sha1)
1026
buffer.buffer = hex;
1027
hex[41] = '\0';
1028
984
- url = xmalloc(strlen(base) + 6 + strlen(ref));
985
- strcpy(url, base);
986
- posn = url + strlen(base);
987
- strcpy(posn, "refs/");
988
- posn += 5;
989
- strcpy(posn, ref);
990
-
1029
+ url = quote_ref_url(base, ref);
1030
slot = get_active_slot();
1031
curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
1032
curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);