* PATCH] http-push: refactor request url creation
@ 2009-01-24 16:16 Ray Chuan
2009-01-24 17:03 ` Johannes Schindelin
0 siblings, 1 reply; 2+ messages in thread
From: Ray Chuan @ 2009-01-24 16:16 UTC (permalink / raw)
To: git, Johannes Schindelin
Currently, functions that deal with objects on the remote repository
have to allocate and do strcpys to generate the URL.
This patch saves them this trouble, by providing a function that
returns a URL: either the object's 2-digit hex directory (eg.
/objects/a1/) or the complete object location (eg. /objects/a1/b2).
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Tay Ray Chuan <rctay89@gmail.com>
---
http-push.c | 58 +++++++++++++++++++---------------------------------------
1 files changed, 19 insertions(+), 39 deletions(-)
diff --git a/http-push.c b/http-push.c
index cb5bf95..715954e 100644
--- a/http-push.c
+++ b/http-push.c
@@ -177,6 +177,16 @@ struct remote_ls_ctx
struct remote_ls_ctx *parent;
};
+static char *get_remote_object_url(const char *url, const char *hex,
int only_two_digit_postfix) {
+ struct strbuf buf = STRBUF_INIT;
+
+ strbuf_addf(&buf, "%sobjects/%.*s/", url, 2, hex);
+ if(!only_two_digit_postfix)
+ strbuf_addf(&buf, "%s", hex+2);
+
+ return strbuf_detach(&buf, NULL);
+}
+
static void finish_request(struct transfer_request *request);
static void release_request(struct transfer_request *request);
@@ -222,8 +232,6 @@ static void start_fetch_loose(struct
transfer_request *request)
char *hex = sha1_to_hex(request->obj->sha1);
char *filename;
char prevfile[PATH_MAX];
- char *url;
- char *posn;
int prevlocal;
unsigned char prev_buf[PREV_BUF_SIZE];
ssize_t prev_read = 0;
@@ -273,17 +281,7 @@ static void start_fetch_loose(struct
transfer_request *request)
git_SHA1_Init(&request->c);
- url = xmalloc(strlen(remote->url) + 50);
- request->url = xmalloc(strlen(remote->url) + 50);
- strcpy(url, remote->url);
- posn = url + strlen(remote->url);
- strcpy(posn, "objects/");
- posn += 8;
- memcpy(posn, hex, 2);
- posn += 2;
- *(posn++) = '/';
- strcpy(posn, hex + 2);
- strcpy(request->url, url);
+ request->url = get_remote_object_url(remote->url, hex, 0);
/* If a previous temp file is present, process what was already
fetched. */
@@ -327,7 +325,7 @@ static void start_fetch_loose(struct
transfer_request *request)
curl_easy_setopt(slot->curl, CURLOPT_FILE, request);
curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
- curl_easy_setopt(slot->curl, CURLOPT_URL, url);
+ curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
/* If we have successfully processed data from a previous fetch
@@ -356,16 +354,8 @@ static void start_mkcol(struct transfer_request *request)
{
char *hex = sha1_to_hex(request->obj->sha1);
struct active_request_slot *slot;
- char *posn;
- request->url = xmalloc(strlen(remote->url) + 13);
- strcpy(request->url, remote->url);
- posn = request->url + strlen(remote->url);
- strcpy(posn, "objects/");
- posn += 8;
- memcpy(posn, hex, 2);
- posn += 2;
- strcpy(posn, "/");
+ request->url = get_remote_object_url(remote->url, hex, 1);
slot = get_active_slot();
slot->callback_func = process_response;
@@ -480,7 +470,7 @@ static void start_put(struct transfer_request *request)
{
char *hex = sha1_to_hex(request->obj->sha1);
struct active_request_slot *slot;
- char *posn;
+ struct strbuf url_buf = STRBUF_INIT;
enum object_type type;
char hdr[50];
void *unpacked;
@@ -519,21 +509,11 @@ static void start_put(struct transfer_request *request)
request->buffer.buf.len = stream.total_out;
- request->url = xmalloc(strlen(remote->url) +
- strlen(request->lock->token) + 51);
- strcpy(request->url, remote->url);
- posn = request->url + strlen(remote->url);
- strcpy(posn, "objects/");
- posn += 8;
- memcpy(posn, hex, 2);
- posn += 2;
- *(posn++) = '/';
- strcpy(posn, hex + 2);
- request->dest = xmalloc(strlen(request->url) + 14);
- sprintf(request->dest, "Destination: %s", request->url);
- posn += 38;
- *(posn++) = '_';
- strcpy(posn, request->lock->token);
+ strbuf_addf(&url_buf, "Destination: %s",
get_remote_object_url(remote->url, hex, 0));
+ request->dest = strbuf_detach(&url_buf, NULL);
+
+ strbuf_addf(&url_buf, "%s_%s", get_remote_object_url(remote->url,
hex, 0), request->lock->token);
+ request->url = strbuf_detach(&url_buf, NULL);
slot = get_active_slot();
slot->callback_func = process_response;
--
1.6.0.4
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: PATCH] http-push: refactor request url creation
2009-01-24 16:16 PATCH] http-push: refactor request url creation Ray Chuan
@ 2009-01-24 17:03 ` Johannes Schindelin
0 siblings, 0 replies; 2+ messages in thread
From: Johannes Schindelin @ 2009-01-24 17:03 UTC (permalink / raw)
To: Ray Chuan; +Cc: git
Hi,
On Sun, 25 Jan 2009, Ray Chuan wrote:
> Currently, functions that deal with objects on the remote repository
> have to allocate and do strcpys to generate the URL.
>
> This patch saves them this trouble, by providing a function that
> returns a URL: either the object's 2-digit hex directory (eg.
> /objects/a1/) or the complete object location (eg. /objects/a1/b2).
>
> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Make that an Acked-by: okay?
> diff --git a/http-push.c b/http-push.c
> index cb5bf95..715954e 100644
> --- a/http-push.c
> +++ b/http-push.c
> @@ -177,6 +177,16 @@ struct remote_ls_ctx
> struct remote_ls_ctx *parent;
> };
>
> +static char *get_remote_object_url(const char *url, const char *hex,
> int only_two_digit_postfix) {
It is actually a prefix.
Ciao,
Dscho
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2009-01-24 17:04 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-01-24 16:16 PATCH] http-push: refactor request url creation Ray Chuan
2009-01-24 17:03 ` Johannes Schindelin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox