http-push: refactor request url creation
Introduce two helper functions append_remote_object_url() and get_remote_object_url() and use them to remove various places that allocate and format the URL by hand. These functions generate a URL that point at the fan-out directory inside the remote object store (e.g. http://host/path/to/repo/objects/a1/) or at an individual loose object file. Signed-off-by: Tay Ray Chuan <rctay89@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Tay Ray Chuan committed
Jan 31, 2009 at 07:51 UTC
817d14a87a90481b60a1c28f79f7d838d985a45c
1 file changed
+27
-37
http-push.c
+27
-37
@@ -209,6 +209,22 @@ static struct curl_slist *get_dav_token_headers(struct remote_lock *lock, enum d
209
return dav_headers;
210
}
211
212
+static void append_remote_object_url(struct strbuf *buf, const char *url,
213
+ const char *hex,
214
+ int only_two_digit_prefix)
215
+{
216
+ strbuf_addf(buf, "%sobjects/%.*s/", url, 2, hex);
217
+ if (!only_two_digit_prefix)
218
+ strbuf_addf(buf, "%s", hex+2);
219
+}
220
+
221
+static char *get_remote_object_url(const char *url, const char *hex, int only_two_digit_prefix)
222
+{
223
+ struct strbuf buf = STRBUF_INIT;
224
+ append_remote_object_url(&buf, url, hex, only_two_digit_prefix);
225
+ return strbuf_detach(&buf, NULL);
226
+}
227
+
228
static void finish_request(struct transfer_request *request);
229
static void release_request(struct transfer_request *request);
230
@@ -255,7 +271,6 @@ static void start_fetch_loose(struct transfer_request *request)
271
char *filename;
272
char prevfile[PATH_MAX];
273
char *url;
258
- char *posn;
274
int prevlocal;
275
unsigned char prev_buf[PREV_BUF_SIZE];
276
ssize_t prev_read = 0;
@@ -305,17 +320,8 @@ static void start_fetch_loose(struct transfer_request *request)
320
321
git_SHA1_Init(&request->c);
322
308
- url = xmalloc(strlen(remote->url) + 50);
309
- request->url = xmalloc(strlen(remote->url) + 50);
310
- strcpy(url, remote->url);
311
- posn = url + strlen(remote->url);
312
- strcpy(posn, "objects/");
313
- posn += 8;
314
- memcpy(posn, hex, 2);
315
- posn += 2;
316
- *(posn++) = '/';
317
- strcpy(posn, hex + 2);
318
- strcpy(request->url, url);
323
+ url = get_remote_object_url(remote->url, hex, 0);
324
+ request->url = xstrdup(url);
325
326
/* If a previous temp file is present, process what was already
327
fetched. */
@@ -388,16 +394,8 @@ static void start_mkcol(struct transfer_request *request)
394
{
395
char *hex = sha1_to_hex(request->obj->sha1);
396
struct active_request_slot *slot;
391
- char *posn;
397
393
- request->url = xmalloc(strlen(remote->url) + 13);
394
- strcpy(request->url, remote->url);
395
- posn = request->url + strlen(remote->url);
396
- strcpy(posn, "objects/");
397
- posn += 8;
398
- memcpy(posn, hex, 2);
399
- posn += 2;
400
- strcpy(posn, "/");
398
+ request->url = get_remote_object_url(remote->url, hex, 1);
399
400
slot = get_active_slot();
401
slot->callback_func = process_response;
@@ -512,7 +510,7 @@ static void start_put(struct transfer_request *request)
510
{
511
char *hex = sha1_to_hex(request->obj->sha1);
512
struct active_request_slot *slot;
515
- char *posn;
513
+ struct strbuf buf = STRBUF_INIT;
514
enum object_type type;
515
char hdr[50];
516
void *unpacked;
@@ -551,21 +549,13 @@ static void start_put(struct transfer_request *request)
549
550
request->buffer.buf.len = stream.total_out;
551
554
- request->url = xmalloc(strlen(remote->url) +
555
- strlen(request->lock->token) + 51);
556
- strcpy(request->url, remote->url);
557
- posn = request->url + strlen(remote->url);
558
- strcpy(posn, "objects/");
559
- posn += 8;
560
- memcpy(posn, hex, 2);
561
- posn += 2;
562
- *(posn++) = '/';
563
- strcpy(posn, hex + 2);
564
- request->dest = xmalloc(strlen(request->url) + 14);
565
- sprintf(request->dest, "Destination: %s", request->url);
566
- posn += 38;
567
- *(posn++) = '_';
568
- strcpy(posn, request->lock->token);
552
+ strbuf_addstr(&buf, "Destination: ");
553
+ append_remote_object_url(&buf, remote->url, hex, 0);
554
+ request->dest = strbuf_detach(&buf, NULL);
555
+
556
+ append_remote_object_url(&buf, remote->url, hex, 0);
557
+ strbuf_addstr(&buf, request->lock->token);
558
+ request->url = strbuf_detach(&buf, NULL);
559
560
slot = get_active_slot();
561
slot->callback_func = process_response;