{"thread":{"id":"11209","subject":"[PATCH 2/2] Add support for URLs to git-apply","startedAt":"2007-12-09T10:04:31Z","lastAt":"2007-12-10T11:16:15Z","messageCount":7,"participants":["Mike Hommey","Andreas Ericsson","Junio C Hamano","Johannes Schindelin"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"62472","messageId":"1197194672-28568-1-git-send-email-mh@glandium.org","threadId":"11209","inReplyTo":null,"subject":"[PATCH 1/2] Move the file read logic to read_patch_file() in builtin-apply.c","fromName":"Mike Hommey","fromEmail":"mh@glandium.org","sentAt":"2007-12-09T10:04:31Z","receivedAt":"2007-12-09T10:04:31Z","isPatch":true,"sender":{"key":"mh@glandium.org","avatar":"https://avatars.githubusercontent.com/u/1038527?v=4"},"body":"This will allow to extend the read logic further. We also return a better\nerror message than usage() when the given filename can't be opened, and\navoid whitespace options not being set when reading from stdin with the\n\"-\" argument as a side effect.\n\nSigned-off-by: Mike Hommey <mh@glandium.org>\n---\n builtin-apply.c |   32 +++++++++++++++++---------------\n 1 files changed, 17 insertions(+), 15 deletions(-)\n\ndiff --git a/builtin-apply.c b/builtin-apply.c\nindex f2e9a33..8c8162a 100644\n--- a/builtin-apply.c\n+++ b/builtin-apply.c\n@@ -182,11 +182,23 @@ static void say_patch_name(FILE *output, const char *pre,\n #define CHUNKSIZE (8192)\n #define SLOP (16)\n \n-static void read_patch_file(struct strbuf *sb, int fd)\n+static void read_patch_file(struct strbuf *sb, const char *filename)\n {\n+\tint fd;\n+\n+\tif (!strcmp(filename, \"-\")) {\n+\t\tfd = 0;\n+\t} else {\n+\t\tfd = open(filename, O_RDONLY);\n+\t\tif (fd < 0)\n+\t\t\tdie(\"git-apply: could not open %s: %s\", filename,\n+\t\t\t    strerror(errno));\n+\t}\n+\n \tif (strbuf_read(sb, fd, 0) < 0)\n \t\tdie(\"git-apply: read returned %s\", strerror(errno));\n \n+\tclose(fd);\n \t/*\n \t * Make sure that we have some slop in the buffer\n \t * so that we can do speculative \"memcmp\" etc, and\n@@ -2705,7 +2717,7 @@ static void prefix_patches(struct patch *p)\n \t}\n }\n \n-static int apply_patch(int fd, const char *filename, int inaccurate_eof)\n+static int apply_patch(const char *filename, int inaccurate_eof)\n {\n \tsize_t offset;\n \tstruct strbuf buf;\n@@ -2714,7 +2726,7 @@ static int apply_patch(int fd, const char *filename, int inaccurate_eof)\n \n \tstrbuf_init(&buf, 0);\n \tpatch_input_file = filename;\n-\tread_patch_file(&buf, fd);\n+\tread_patch_file(&buf, filename);\n \toffset = 0;\n \twhile (offset < buf.len) {\n \t\tstruct patch *patch;\n@@ -2807,13 +2819,7 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)\n \tfor (i = 1; i < argc; i++) {\n \t\tconst char *arg = argv[i];\n \t\tchar *end;\n-\t\tint fd;\n \n-\t\tif (!strcmp(arg, \"-\")) {\n-\t\t\terrs |= apply_patch(0, \"<stdin>\", inaccurate_eof);\n-\t\t\tread_stdin = 0;\n-\t\t\tcontinue;\n-\t\t}\n \t\tif (!prefixcmp(arg, \"--exclude=\")) {\n \t\t\tstruct excludes *x = xmalloc(sizeof(*x));\n \t\t\tx->path = arg + 10;\n@@ -2916,17 +2922,13 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)\n \t\tif (0 < prefix_length)\n \t\t\targ = prefix_filename(prefix, prefix_length, arg);\n \n-\t\tfd = open(arg, O_RDONLY);\n-\t\tif (fd < 0)\n-\t\t\tusage(apply_usage);\n \t\tread_stdin = 0;\n \t\tset_default_whitespace_mode(whitespace_option);\n-\t\terrs |= apply_patch(fd, arg, inaccurate_eof);\n-\t\tclose(fd);\n+\t\terrs |= apply_patch(arg, inaccurate_eof);\n \t}\n \tset_default_whitespace_mode(whitespace_option);\n \tif (read_stdin)\n-\t\terrs |= apply_patch(0, \"<stdin>\", inaccurate_eof);\n+\t\terrs |= apply_patch(\"-\", inaccurate_eof);\n \tif (whitespace_error) {\n \t\tif (squelch_whitespace_errors &&\n \t\t    squelch_whitespace_errors < whitespace_error) {\n-- \n1.5.3.7\n"},{"id":"62471","messageId":"1197194672-28568-2-git-send-email-mh@glandium.org","threadId":"11209","inReplyTo":"1197194672-28568-1-git-send-email-mh@glandium.org","subject":"[PATCH 2/2] Add support for URLs to git-apply","fromName":"Mike Hommey","fromEmail":"mh@glandium.org","sentAt":"2007-12-09T10:04:32Z","receivedAt":"2007-12-09T10:04:32Z","isPatch":true,"sender":{"key":"mh@glandium.org","avatar":"https://avatars.githubusercontent.com/u/1038527?v=4"},"body":"Instead of doing several \"wget -O - url | git-apply -\" in a raw, you now\ncan just git-apply url1 url2 ...\n\nSigned-off-by: Mike Hommey <mh@glandium.org>\n---\n\nAfter spending an afternoon wgetting patches to applying them, I got fed up\nand just added basic url support to git-apply. Note that the fwrite_strbuf\nfunction should just live in http.c, but until the http api is fully switched\nto use strbufs instead of its struct buffer (which I'm planning to do), it\ncan stay like this.\n\n Documentation/git-apply.txt |    3 +-\n builtin-apply.c             |   66 +++++++++++++++++++++++++++++++++++++------\n 2 files changed, 59 insertions(+), 10 deletions(-)\n\ndiff --git a/Documentation/git-apply.txt b/Documentation/git-apply.txt\nindex bae3e7b..2d5d725 100644\n--- a/Documentation/git-apply.txt\n+++ b/Documentation/git-apply.txt\n@@ -25,7 +25,8 @@ OPTIONS\n -------\n <patch>...::\n \tThe files to read patch from.  '-' can be used to read\n-\tfrom the standard input.\n+\tfrom the standard input. They can also be http, https or\n+\tftp URLs.\n \n --stat::\n \tInstead of applying the patch, output diffstat for the\ndiff --git a/builtin-apply.c b/builtin-apply.c\nindex 8c8162a..e213bd2 100644\n--- a/builtin-apply.c\n+++ b/builtin-apply.c\n@@ -12,6 +12,9 @@\n #include \"blob.h\"\n #include \"delta.h\"\n #include \"builtin.h\"\n+#ifndef NO_CURL\n+#include \"http.h\"\n+#endif\n \n /*\n  *  --check turns on checking that the working tree matches the\n@@ -182,12 +185,55 @@ static void say_patch_name(FILE *output, const char *pre,\n #define CHUNKSIZE (8192)\n #define SLOP (16)\n \n-static void read_patch_file(struct strbuf *sb, const char *filename)\n+#ifndef NO_CURL\n+static size_t fwrite_strbuf(const void *ptr, size_t eltsize,\n+\t\t\tsize_t nmemb, struct strbuf *buffer)\n+{\n+\tsize_t size = eltsize * nmemb;\n+\tstrbuf_add(buffer, ptr, size);\n+\treturn size;\n+}\n+\n+static int used_http;\n+\n+static void read_patch_url(struct strbuf *sb, const char *url)\n+{\n+\tstruct active_request_slot *slot;\n+\tstruct slot_results results;\n+\n+\tif (! used_http) {\n+\t\thttp_init();\n+\t\tused_http = 1;\n+\t}\n+\n+\tslot = get_active_slot();\n+\tslot->results = &results;\n+\tcurl_easy_setopt(slot->curl, CURLOPT_FILE, sb);\n+\tcurl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_strbuf);\n+\tcurl_easy_setopt(slot->curl, CURLOPT_URL, url);\n+\tcurl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);\n+\n+\tif (start_active_slot(slot)) {\n+\t\trun_active_slot(slot);\n+\t\tif (results.curl_result != CURLE_OK)\n+\t\t\tdie(\"git-apply: could not open %s\", url);\n+\t}\n+}\n+#endif\n+\n+static void read_patch(struct strbuf *sb, const char *filename)\n {\n \tint fd;\n \n \tif (!strcmp(filename, \"-\")) {\n \t\tfd = 0;\n+#ifndef NO_CURL\n+\t} else if (!strncmp(filename, \"http://\", 7) ||\n+\t\t   !strncmp(filename, \"https://\", 8) ||\n+\t\t   !strncmp(filename, \"ftp://\", 6)) {\n+\t\tread_patch_url(sb, filename);\n+\t\treturn;\n+#endif\n \t} else {\n \t\tfd = open(filename, O_RDONLY);\n \t\tif (fd < 0)\n@@ -199,13 +245,6 @@ static void read_patch_file(struct strbuf *sb, const char *filename)\n \t\tdie(\"git-apply: read returned %s\", strerror(errno));\n \n \tclose(fd);\n-\t/*\n-\t * Make sure that we have some slop in the buffer\n-\t * so that we can do speculative \"memcmp\" etc, and\n-\t * see to it that it is NUL-filled.\n-\t */\n-\tstrbuf_grow(sb, SLOP);\n-\tmemset(sb->buf + sb->len, 0, SLOP);\n }\n \n static unsigned long linelen(const char *buffer, unsigned long size)\n@@ -2726,7 +2765,14 @@ static int apply_patch(const char *filename, int inaccurate_eof)\n \n \tstrbuf_init(&buf, 0);\n \tpatch_input_file = filename;\n-\tread_patch_file(&buf, filename);\n+\tread_patch(&buf, filename);\n+\t/*\n+\t * Make sure that we have some slop in the buffer\n+\t * so that we can do speculative \"memcmp\" etc, and\n+\t * see to it that it is NUL-filled.\n+\t */\n+\tstrbuf_grow(&buf, SLOP);\n+\tmemset(buf.buf + buf.len, 0, SLOP);\n \toffset = 0;\n \twhile (offset < buf.len) {\n \t\tstruct patch *patch;\n@@ -2926,6 +2972,8 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)\n \t\tset_default_whitespace_mode(whitespace_option);\n \t\terrs |= apply_patch(arg, inaccurate_eof);\n \t}\n+\tif (used_http)\n+\t\thttp_cleanup();\n \tset_default_whitespace_mode(whitespace_option);\n \tif (read_stdin)\n \t\terrs |= apply_patch(\"-\", inaccurate_eof);\n-- \n1.5.3.7\n"},{"id":"62490","messageId":"20071209170250.GA18955@glandium.org","threadId":"11209","inReplyTo":"1197194672-28568-2-git-send-email-mh@glandium.org","subject":"Re: [PATCH 2/2] Add support for URLs to git-apply","fromName":"Mike Hommey","fromEmail":"mh@glandium.org","sentAt":"2007-12-09T17:02:50Z","receivedAt":"2007-12-09T17:02:50Z","isPatch":true,"sender":{"key":"mh@glandium.org","avatar":"https://avatars.githubusercontent.com/u/1038527?v=4"},"body":"On Sun, Dec 09, 2007 at 11:04:32AM +0100, Mike Hommey wrote:\n> Instead of doing several \"wget -O - url | git-apply -\" in a raw, you now\n> can just git-apply url1 url2 ...\n> \n> Signed-off-by: Mike Hommey <mh@glandium.org>\n> ---\n> \n> After spending an afternoon wgetting patches to applying them, I got fed up\n> and just added basic url support to git-apply. Note that the fwrite_strbuf\n> function should just live in http.c, but until the http api is fully switched\n> to use strbufs instead of its struct buffer (which I'm planning to do), it\n> can stay like this.\n\nI'm going to resend these 2 patches, with some others that do some changes\nto the http code.\n\nMike\n"},{"id":"62523","messageId":"475C5869.4080900@op5.se","threadId":"11209","inReplyTo":"1197194672-28568-2-git-send-email-mh@glandium.org","subject":"Re: [PATCH 2/2] Add support for URLs to git-apply","fromName":"Andreas Ericsson","fromEmail":"ae@op5.se","sentAt":"2007-12-09T21:04:41Z","receivedAt":"2007-12-09T21:04:41Z","isPatch":true,"sender":{"key":"ae@op5.se","avatar":"https://gravatar.com/avatar/426e89595c75a8f5252dd0c989e5fabe5bcac616e68557427ad9aef6b0ca342a?d=mp&s=160"},"body":"Mike Hommey wrote:\n> Instead of doing several \"wget -O - url | git-apply -\" in a raw, you now\n> can just git-apply url1 url2 ...\n> \n\nI seriously like this idea. Combined with gitweb (or cgit), it could be\nused as a cherry-pick from someone else's repo :)\n\n-- \nAndreas Ericsson                   andreas.ericsson@op5.se\nOP5 AB                             www.op5.se\nTel: +46 8-230225                  Fax: +46 8-230231\n"},{"id":"62524","messageId":"7vhciro4vx.fsf@gitster.siamese.dyndns.org","threadId":"11209","inReplyTo":"475C5869.4080900@op5.se","subject":"Re: [PATCH 2/2] Add support for URLs to git-apply","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2007-12-09T22:54:58Z","receivedAt":"2007-12-09T22:54:58Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Andreas Ericsson <ae@op5.se> writes:\n\n> Mike Hommey wrote:\n>> Instead of doing several \"wget -O - url | git-apply -\" in a raw, you now\n>> can just git-apply url1 url2 ...\n>>\n>\n> I seriously like this idea. Combined with gitweb (or cgit), it could be\n> used as a cherry-pick from someone else's repo :)\n\nFWIW, my initial impression is that I seriously dislike this.  It may be\ngood if the patch were to git-am, but when git-apply rejects an\ninapplicable patch, there won't be nothing left for you to recover with\nand you need to re-download the patch anyway.\n\nNote that I said my \"initial\" impression.  I reserve the right to change\nmy mind, as always ;-)\n"},{"id":"62546","messageId":"20071210064659.GA4148@glandium.org","threadId":"11209","inReplyTo":"7vhciro4vx.fsf@gitster.siamese.dyndns.org","subject":"Re: [PATCH 2/2] Add support for URLs to git-apply","fromName":"Mike Hommey","fromEmail":"mh@glandium.org","sentAt":"2007-12-10T06:46:59Z","receivedAt":"2007-12-10T06:46:59Z","isPatch":true,"sender":{"key":"mh@glandium.org","avatar":"https://avatars.githubusercontent.com/u/1038527?v=4"},"body":"On Sun, Dec 09, 2007 at 02:54:58PM -0800, Junio C Hamano wrote:\n> Andreas Ericsson <ae@op5.se> writes:\n> \n> > Mike Hommey wrote:\n> >> Instead of doing several \"wget -O - url | git-apply -\" in a raw, you now\n> >> can just git-apply url1 url2 ...\n> >>\n> >\n> > I seriously like this idea. Combined with gitweb (or cgit), it could be\n> > used as a cherry-pick from someone else's repo :)\n> \n> FWIW, my initial impression is that I seriously dislike this.  It may be\n> good if the patch were to git-am, but when git-apply rejects an\n> inapplicable patch, there won't be nothing left for you to recover with\n> and you need to re-download the patch anyway.\n\nThere are some usecase differences between git-apply and git-am.\nProbably, this change would be good to have on both.\n\nMike\n"},{"id":"62563","messageId":"Pine.LNX.4.64.0712101113160.27959@racer.site","threadId":"11209","inReplyTo":"20071210064659.GA4148@glandium.org","subject":"Re: [PATCH 2/2] Add support for URLs to git-apply","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2007-12-10T11:16:15Z","receivedAt":"2007-12-10T11:16:15Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Mon, 10 Dec 2007, Mike Hommey wrote:\n\n> On Sun, Dec 09, 2007 at 02:54:58PM -0800, Junio C Hamano wrote:\n> > Andreas Ericsson <ae@op5.se> writes:\n> > \n> > > Mike Hommey wrote:\n> > >> Instead of doing several \"wget -O - url | git-apply -\" in a raw, \n> > >> you now can just git-apply url1 url2 ...\n> > >>\n> > >\n> > > I seriously like this idea. Combined with gitweb (or cgit), it could \n> > > be used as a cherry-pick from someone else's repo :)\n> > \n> > FWIW, my initial impression is that I seriously dislike this.  It may \n> > be good if the patch were to git-am, but when git-apply rejects an \n> > inapplicable patch, there won't be nothing left for you to recover \n> > with and you need to re-download the patch anyway.\n> \n> There are some usecase differences between git-apply and git-am. \n> Probably, this change would be good to have on both.\n\nBut what about Junio's comments about a failed patch?  You really want to \nhammer that poor webserver?\n\nMy first thought when seeing your patch was: this would give us a chance \nto \"clone\" via gitweb.  And while doing so, all but kill those webservers.  \nSo I thought it was wrong.\n\nWhen Junio mentioned git-am it was obvious to me that this is the \"right\" \nsolution.\n\nI mean, we go out of our way to be nice to the servers, putting more load \nonto the clients, because there are many clients, but only one server \n(which is unfair).\n\nPlease address these issues before further arguing that both apply and am \nshould learn about URLs.\n\nCiao,\nDscho\n"}]}