{"thread":{"id":"20845","subject":"[PATCH 7/8] Add support for \"import\" helper command","startedAt":"2009-09-04T02:14:01Z","lastAt":"2009-09-04T02:14:01Z","messageCount":1,"participants":["Daniel Barkalow"],"isPatch":true,"patchVersion":1,"patchTotal":8},"messages":[{"id":"122383","messageId":"alpine.LNX.2.00.0909032213330.28290@iabervon.org","threadId":"20845","inReplyTo":null,"subject":"[PATCH 7/8] Add support for \"import\" helper command","fromName":"Daniel Barkalow","fromEmail":"barkalow@iabervon.org","sentAt":"2009-09-04T02:14:01Z","receivedAt":"2009-09-04T02:14:01Z","isPatch":true,"sender":{"key":"barkalow@iabervon.org","avatar":"https://avatars.githubusercontent.com/u/55364219?v=4"},"body":"This command, supported if the \"import\" capability is advertized,\nallows a helper to support fetching by outputting a git-fast-import\nstream.\n\nIf both \"fetch\" and \"import\" are advertized, git itself will use\n\"fetch\" (although other users may use \"import\" in this case).\n\nSigned-off-by: Daniel Barkalow <barkalow@iabervon.org>\n---\n Documentation/git-remote-helpers.txt |   10 ++++++\n transport-helper.c                   |   53 ++++++++++++++++++++++++++++++++++\n 2 files changed, 63 insertions(+), 0 deletions(-)\n\ndiff --git a/Documentation/git-remote-helpers.txt b/Documentation/git-remote-helpers.txt\nindex 173ee23..e9aa67e 100644\n--- a/Documentation/git-remote-helpers.txt\n+++ b/Documentation/git-remote-helpers.txt\n@@ -43,6 +43,13 @@ Commands are given by the caller on the helper's standard input, one per line.\n +\n Supported if the helper has the \"fetch\" capability.\n \n+'import' <name>::\n+\tProduces a fast-import stream which imports the current value\n+\tof the named ref. It may additionally import other refs as\n+\tneeded to construct the history efficiently.\n++\n+Supported if the helper has the \"import\" capability.\n+\n If a fatal error occurs, the program writes the error message to\n stderr and exits. The caller should expect that a suitable error\n message has been printed if the child closes the connection without\n@@ -57,6 +64,9 @@ CAPABILITIES\n 'fetch'::\n \tThis helper supports the 'fetch' command.\n \n+'import'::\n+\tThis helper supports the 'import' command.\n+\n REF LIST ATTRIBUTES\n -------------------\n \ndiff --git a/transport-helper.c b/transport-helper.c\nindex e2b5270..3825441 100644\n--- a/transport-helper.c\n+++ b/transport-helper.c\n@@ -11,6 +11,7 @@ struct helper_data\n \tconst char *name;\n \tstruct child_process *helper;\n \tunsigned fetch : 1;\n+\tunsigned import : 1;\n };\n \n static struct child_process *get_helper(struct transport *transport)\n@@ -50,6 +51,8 @@ static struct child_process *get_helper(struct transport *transport)\n \t\t\tbreak;\n \t\tif (!strcmp(buf.buf, \"fetch\"))\n \t\t\tdata->fetch = 1;\n+\t\tif (!strcmp(buf.buf, \"import\"))\n+\t\t\tdata->import = 1;\n \t}\n \treturn data->helper;\n }\n@@ -93,6 +96,53 @@ static int fetch_with_fetch(struct transport *transport,\n \treturn 0;\n }\n \n+static int get_importer(struct transport *transport, struct child_process *fastimport)\n+{\n+\tstruct helper_data *data = transport->data;\n+\tstruct child_process *helper = get_helper(transport);\n+\tmemset(fastimport, 0, sizeof(*fastimport));\n+\tfastimport->in = helper->out;\n+\tfastimport->argv = xcalloc(5, sizeof(*fastimport->argv));\n+\tfastimport->argv[0] = \"fast-import\";\n+\tfastimport->argv[1] = \"--quiet\";\n+\n+\tfastimport->git_cmd = 1;\n+\treturn start_command(fastimport);\n+}\n+\n+static int fetch_with_import(struct transport *transport,\n+\t\t\t     int nr_heads, struct ref **to_fetch)\n+{\n+\tstruct child_process fastimport;\n+\tstruct child_process *helper = get_helper(transport);\n+\tint i;\n+\tstruct ref *posn;\n+\tstruct strbuf buf = STRBUF_INIT;\n+\n+\tif (get_importer(transport, &fastimport))\n+\t\tdie(\"Couldn't run fast-import\");\n+\n+\tfor (i = 0; i < nr_heads; i++) {\n+\t\tposn = to_fetch[i];\n+\t\tif (posn->status & REF_STATUS_UPTODATE)\n+\t\t\tcontinue;\n+\n+\t\tstrbuf_addf(&buf, \"import %s\\n\", posn->name);\n+\t\twrite_in_full(helper->in, buf.buf, buf.len);\n+\t\tstrbuf_reset(&buf);\n+\t}\n+\tdisconnect_helper(transport);\n+\tfinish_command(&fastimport);\n+\n+\tfor (i = 0; i < nr_heads; i++) {\n+\t\tposn = to_fetch[i];\n+\t\tif (posn->status & REF_STATUS_UPTODATE)\n+\t\t\tcontinue;\n+\t\tread_ref(posn->name, posn->old_sha1);\n+\t}\n+\treturn 0;\n+}\n+\n static int fetch(struct transport *transport,\n \t\t int nr_heads, struct ref **to_fetch)\n {\n@@ -110,6 +160,9 @@ static int fetch(struct transport *transport,\n \tif (data->fetch)\n \t\treturn fetch_with_fetch(transport, nr_heads, to_fetch);\n \n+\tif (data->import)\n+\t\treturn fetch_with_import(transport, nr_heads, to_fetch);\n+\n \treturn -1;\n }\n \n-- \n1.6.4.2.419.gc86f8\n"}]}