{"thread":{"id":"9902","subject":"[PATCH] apply: get rid of --index-info in favor of --build-fake-ancestor","startedAt":"2007-09-17T22:34:06Z","lastAt":"2007-09-18T00:18:43Z","messageCount":4,"participants":["Johannes Schindelin","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"53356","messageId":"Pine.LNX.4.64.0709172330400.28586@racer.site","threadId":"9902","inReplyTo":null,"subject":"[PATCH] apply: get rid of --index-info in favor of --build-fake-ancestor","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2007-09-17T22:34:06Z","receivedAt":"2007-09-17T22:34:06Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"\ngit-am used \"git apply -z --index-info\" to find the original versions\nof the files touched by the diff, to be able to do an inexpensive\nthree-way merge.\n\nThis operation makes only sense in a repository, since the index\ninformation in the diff refers to blobs, which have to be present in\nthe current repository.\n\nTherefore, teach \"git apply\" a mode to write out the result as an\nindex file to begin with, obviating the need for scripts to do it\nthemselves.\n\nThe primary user for --index-info is \"git am\", which is converted to\nuse --build-fake-ancestor in this patch.\n\nSuggested by Junio.\n\nSigned-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>\n---\n\n\tSuggested by Junio, but the errors are all mine.\n\n\tI am not quite certain, though, if there is really no porcelain \n\tusing that option.  It has been around since Oct 7 2005 (!), so \n\tthere is a real chance that StGit, guilt or QGit use it.  In that \n\tcase, this patch is obviously wrong.\n\n Documentation/git-apply.txt |   11 +++++++----\n builtin-apply.c             |   35 ++++++++++++++++++++++-------------\n git-am.sh                   |    6 ++----\n 3 files changed, 31 insertions(+), 21 deletions(-)\n\ndiff --git a/Documentation/git-apply.txt b/Documentation/git-apply.txt\nindex 4c7e3a2..c1c54bf 100644\n--- a/Documentation/git-apply.txt\n+++ b/Documentation/git-apply.txt\n@@ -10,7 +10,7 @@ SYNOPSIS\n --------\n [verse]\n 'git-apply' [--stat] [--numstat] [--summary] [--check] [--index]\n-\t  [--apply] [--no-add] [--index-info] [-R | --reverse]\n+\t  [--apply] [--no-add] [--build-fake-ancestor <file>] [-R | --reverse]\n \t  [--allow-binary-replacement | --binary] [--reject] [-z]\n \t  [-pNUM] [-CNUM] [--inaccurate-eof] [--cached]\n \t  [--whitespace=<nowarn|warn|error|error-all|strip>]\n@@ -63,12 +63,15 @@ OPTIONS\n \tcached data, apply the patch, and store the result in the index,\n \twithout using the working tree. This implies '--index'.\n \n---index-info::\n+--build-fake-ancestor <file>::\n \tNewer git-diff output has embedded 'index information'\n \tfor each blob to help identify the original version that\n \tthe patch applies to.  When this flag is given, and if\n-\tthe original version of the blob is available locally,\n-\toutputs information about them to the standard output.\n+\tthe original versions of the blobs is available locally,\n+\tbuilds a temporary index containing those blobs.\n++\n+When a pure mode change is encountered (which has no index information),\n+the information is read from the current index instead.\n \n -R, --reverse::\n \tApply the patch in reverse.\ndiff --git a/builtin-apply.c b/builtin-apply.c\nindex 938fd61..1372169 100644\n--- a/builtin-apply.c\n+++ b/builtin-apply.c\n@@ -42,7 +42,7 @@ static int apply_in_reverse;\n static int apply_with_reject;\n static int apply_verbosely;\n static int no_add;\n-static int show_index_info;\n+static const char *fake_ancestor;\n static int line_termination = '\\n';\n static unsigned long p_context = ULONG_MAX;\n static const char apply_usage[] =\n@@ -2236,9 +2236,12 @@ static int get_current_sha1(const char *path, unsigned char *sha1)\n \treturn 0;\n }\n \n-static void show_index_list(struct patch *list)\n+/* Build an index that contains the just the files needed for a 3way merge */\n+static void build_fake_ancestor(struct patch *list, const char *filename)\n {\n \tstruct patch *patch;\n+\tstruct index_state result = { 0 };\n+\tint fd;\n \n \t/* Once we start supporting the reverse patch, it may be\n \t * worth showing the new sha1 prefix, but until then...\n@@ -2246,11 +2249,12 @@ static void show_index_list(struct patch *list)\n \tfor (patch = list; patch; patch = patch->next) {\n \t\tconst unsigned char *sha1_ptr;\n \t\tunsigned char sha1[20];\n+\t\tstruct cache_entry *ce;\n \t\tconst char *name;\n \n \t\tname = patch->old_name ? patch->old_name : patch->new_name;\n \t\tif (0 < patch->is_new)\n-\t\t\tsha1_ptr = null_sha1;\n+\t\t\tcontinue;\n \t\telse if (get_sha1(patch->old_sha1_prefix, sha1))\n \t\t\t/* git diff has no index line for mode/type changes */\n \t\t\tif (!patch->lines_added && !patch->lines_deleted) {\n@@ -2265,13 +2269,16 @@ static void show_index_list(struct patch *list)\n \t\telse\n \t\t\tsha1_ptr = sha1;\n \n-\t\tprintf(\"%06o %s\t\",patch->old_mode, sha1_to_hex(sha1_ptr));\n-\t\tif (line_termination && quote_c_style(name, NULL, NULL, 0))\n-\t\t\tquote_c_style(name, NULL, stdout, 0);\n-\t\telse\n-\t\t\tfputs(name, stdout);\n-\t\tputchar(line_termination);\n+\t\tce = make_cache_entry(patch->old_mode, sha1_ptr, name, 0, 0);\n+\t\tif (add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD))\n+\t\t\tdie (\"Could not add %s to temporary index\", name);\n \t}\n+\n+\tfd = open(filename, O_WRONLY | O_CREAT, 0666);\n+\tif (fd < 0 || write_index(&result, fd) || close(fd))\n+\t\tdie (\"Could not write temporary index to %s\", filename);\n+\n+\tdiscard_index(&result);\n }\n \n static void stat_patch_list(struct patch *patch)\n@@ -2791,8 +2798,8 @@ static int apply_patch(int fd, const char *filename, int inaccurate_eof)\n \tif (apply && write_out_results(list, skipped_patch))\n \t\texit(1);\n \n-\tif (show_index_info)\n-\t\tshow_index_list(list);\n+\tif (fake_ancestor)\n+\t\tbuild_fake_ancestor(list, fake_ancestor);\n \n \tif (diffstat)\n \t\tstat_patch_list(list);\n@@ -2900,9 +2907,11 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)\n \t\t\tapply = 1;\n \t\t\tcontinue;\n \t\t}\n-\t\tif (!strcmp(arg, \"--index-info\")) {\n+\t\tif (!strcmp(arg, \"--build-fake-ancestor\")) {\n \t\t\tapply = 0;\n-\t\t\tshow_index_info = 1;\n+\t\t\tif (++i >= argc)\n+\t\t\t\tdie (\"need a filename\");\n+\t\t\tfake_ancestor = argv[i];\n \t\t\tcontinue;\n \t\t}\n \t\tif (!strcmp(arg, \"-z\")) {\ndiff --git a/git-am.sh b/git-am.sh\nindex 4db4701..8340125 100755\n--- a/git-am.sh\n+++ b/git-am.sh\n@@ -62,10 +62,8 @@ fall_back_3way () {\n     mkdir \"$dotest/patch-merge-tmp-dir\"\n \n     # First see if the patch records the index info that we can use.\n-    git apply -z --index-info \"$dotest/patch\" \\\n-\t>\"$dotest/patch-merge-index-info\" &&\n-    GIT_INDEX_FILE=\"$dotest/patch-merge-tmp-index\" \\\n-    git update-index -z --index-info <\"$dotest/patch-merge-index-info\" &&\n+    git apply --build-fake-ancestor \"$dotest/patch-merge-tmp-index\" \\\n+\t\"$dotest/patch\" &&\n     GIT_INDEX_FILE=\"$dotest/patch-merge-tmp-index\" \\\n     git write-tree >\"$dotest/patch-merge-base+\" ||\n     cannot_fallback \"Repository lacks necessary blobs to fall back on 3-way merge.\"\n-- \n1.5.3.1.989.g2059\n"},{"id":"53358","messageId":"7v7imozzgm.fsf@gitster.siamese.dyndns.org","threadId":"9902","inReplyTo":"Pine.LNX.4.64.0709172330400.28586@racer.site","subject":"Re: [PATCH] apply: get rid of --index-info in favor of --build-fake-ancestor","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2007-09-17T22:42:01Z","receivedAt":"2007-09-17T22:42:01Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:\n\n> \tSuggested by Junio, but the errors are all mine.\n>\n> \tI am not quite certain, though, if there is really no porcelain \n> \tusing that option.  It has been around since Oct 7 2005 (!), so \n> \tthere is a real chance that StGit, guilt or QGit use it.  In that \n> \tcase, this patch is obviously wrong.\n\nYou do not have to do the deprecating/removing part if that is\nthe issue.\n"},{"id":"53364","messageId":"Pine.LNX.4.64.0709172355020.28586@racer.site","threadId":"9902","inReplyTo":"7v7imozzgm.fsf@gitster.siamese.dyndns.org","subject":"Re: [PATCH] apply: get rid of --index-info in favor of --build-fake-ancestor","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2007-09-17T23:04:40Z","receivedAt":"2007-09-17T23:04:40Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Mon, 17 Sep 2007, Junio C Hamano wrote:\n\n> Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:\n> \n> > \tSuggested by Junio, but the errors are all mine.\n> >\n> > \tI am not quite certain, though, if there is really no porcelain \n> > \tusing that option.  It has been around since Oct 7 2005 (!), so \n> > \tthere is a real chance that StGit, guilt or QGit use it.  In that \n> > \tcase, this patch is obviously wrong.\n> \n> You do not have to do the deprecating/removing part if that is\n> the issue.\n\nI'd rather avoid keeping this code, if nobody uses it anyway...\n\nSo I cloned StGit and guilt (the two porcelains that I feel are most used, \napart from what is in git.git, and I'm too lazy to find that mail \nanalysing the recent Git survey).  Neither of them has any --index-info in \ntheir complete history.\n\nSo I'm more confident now that there is no need to keep --index-info.\n\nCiao,\nDscho\n"},{"id":"53382","messageId":"7vzlzkygf0.fsf@gitster.siamese.dyndns.org","threadId":"9902","inReplyTo":"Pine.LNX.4.64.0709172355020.28586@racer.site","subject":"Re: [PATCH] apply: get rid of --index-info in favor of --build-fake-ancestor","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2007-09-18T00:18:43Z","receivedAt":"2007-09-18T00:18:43Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:\n\n> Hi,\n>\n> On Mon, 17 Sep 2007, Junio C Hamano wrote:\n>\n>> Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:\n>> \n>> > \tSuggested by Junio, but the errors are all mine.\n>> >\n>> > \tI am not quite certain, though, if there is really no porcelain \n>> > \tusing that option.  It has been around since Oct 7 2005 (!), so \n>> > \tthere is a real chance that StGit, guilt or QGit use it.  In that \n>> > \tcase, this patch is obviously wrong.\n>> \n>> You do not have to do the deprecating/removing part if that is\n>> the issue.\n>\n> I'd rather avoid keeping this code, if nobody uses it anyway...\n>\n> So I cloned StGit and guilt (the two porcelains that I feel are most used, \n> apart from what is in git.git, and I'm too lazy to find that mail \n> analysing the recent Git survey).  Neither of them has any --index-info in \n> their complete history.\n>\n> So I'm more confident now that there is no need to keep --index-info.\n\nOk, fair enough.\n"}]}