Code

modernize fetch/merge/pull examples
[git.git] / transport-helper.c
index c982bb25750a7fd7f89473edf77e17c68709e74e..f57e84c67641b5a03ba8d9ed5182507bb54dcd15 100644 (file)
@@ -37,8 +37,9 @@ static struct child_process *get_helper(struct transport *transport)
                die("Unable to run helper: git %s", helper->argv[0]);
        data->helper = helper;
 
-       write_in_full(data->helper->in, "capabilities\n", 13);
-       file = fdopen(helper->out, "r");
+       write_str_in_full(helper->in, "capabilities\n");
+
+       file = xfdopen(helper->out, "r");
        while (1) {
                if (strbuf_getline(&buf, file, '\n') == EOF)
                        exit(128); /* child died, message supplied already */
@@ -55,7 +56,7 @@ static int disconnect_helper(struct transport *transport)
 {
        struct helper_data *data = transport->data;
        if (data->helper) {
-               write_in_full(data->helper->in, "\n", 1);
+               write_str_in_full(data->helper->in, "\n");
                close(data->helper->in);
                finish_command(data->helper);
                free((char *)data->helper->argv[0]);
@@ -70,19 +71,20 @@ static int fetch_with_fetch(struct transport *transport,
                            int nr_heads, const struct ref **to_fetch)
 {
        struct child_process *helper = get_helper(transport);
-       FILE *file = fdopen(helper->out, "r");
+       FILE *file = xfdopen(helper->out, "r");
        int i;
        struct strbuf buf = STRBUF_INIT;
 
        for (i = 0; i < nr_heads; i++) {
-               struct ref *posn = to_fetch[i];
+               const struct ref *posn = to_fetch[i];
                if (posn->status & REF_STATUS_UPTODATE)
                        continue;
-               write_in_full(helper->in, "fetch ", 6);
-               write_in_full(helper->in, sha1_to_hex(posn->old_sha1), 40);
-               write_in_full(helper->in, " ", 1);
-               write_in_full(helper->in, posn->name, strlen(posn->name));
-               write_in_full(helper->in, "\n", 1);
+
+               strbuf_addf(&buf, "fetch %s %s\n",
+                           sha1_to_hex(posn->old_sha1), posn->name);
+               write_in_full(helper->in, buf.buf, buf.len);
+               strbuf_reset(&buf);
+
                if (strbuf_getline(&buf, file, '\n') == EOF)
                        exit(128); /* child died, message supplied already */
        }
@@ -119,9 +121,10 @@ static struct ref *get_refs_list(struct transport *transport, int for_push)
        FILE *file;
 
        helper = get_helper(transport);
-       write_in_full(helper->in, "list\n", 5);
 
-       file = fdopen(helper->out, "r");
+       write_str_in_full(helper->in, "list\n");
+
+       file = xfdopen(helper->out, "r");
        while (1) {
                char *eov, *eon;
                if (strbuf_getline(&buf, file, '\n') == EOF)
@@ -152,13 +155,10 @@ static struct ref *get_refs_list(struct transport *transport, int for_push)
        return ret;
 }
 
-int transport_helper_init(struct transport *transport)
+int transport_helper_init(struct transport *transport, const char *name)
 {
        struct helper_data *data = xcalloc(sizeof(*data), 1);
-       char *eom = strchr(transport->url, ':');
-       if (!eom)
-               return -1;
-       data->name = xstrndup(transport->url, eom - transport->url);
+       data->name = name;
 
        transport->data = data;
        transport->get_refs_list = get_refs_list;