{"thread":{"id":"59463","subject":"[PATCH] blame: allow --contents to work with non-HEAD commit","startedAt":"2023-03-24T01:05:10Z","lastAt":"2023-03-24T06:00:20Z","messageCount":3,"participants":["Jacob Keller","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"474069","messageId":"20230324010457.275902-1-jacob.e.keller@intel.com","threadId":"59463","inReplyTo":null,"subject":"[PATCH] blame: allow --contents to work with non-HEAD commit","fromName":"Jacob Keller","fromEmail":"jacob.e.keller@intel.com","sentAt":"2023-03-24T01:04:57Z","receivedAt":"2023-03-24T01:05:10Z","isPatch":true,"sender":{"key":"jacob.e.keller@intel.com","avatar":"https://avatars.githubusercontent.com/u/874719?v=4"},"body":"From: Jacob Keller <jacob.keller@gmail.com>\n\nThe --contents option can be used with git blame to blame the file as if\nit had the contents from the specified file. This is akin to copying the\ncontents into the working tree and then running git blame. This option\nhas been supported since 1cfe77333f27 (\"git-blame: no rev means start\nfrom the working tree file.\")\n\nThe --contents option always blames the file as if it was based on the\ncurrent HEAD commit. If you try to pass a revision while using\n--contents, you get the following error:\n\n  fatal: cannot use --contents with final commit object name\n\nThis is because the blame process generates a fake working tree commit\nwhich always uses the HEAD object.\n\nFix fake_working_tree_commit to take the object ID to use for the\nparent instead of always using HEAD. If both a revision and --contents\nis provided, look up the object ID from the provided revision instead of\nusing HEAD.\n\nThis enables use of --contents with an arbitrary revision, rather than\nforcing the use of the local HEAD commit. This makes the --contents\noption significantly more flexible.\n\nReword the documentation so that its clear that --contents can be used\nwith <rev>.\n\nAdd tests for the --contents option to the annotate-tests.sh test\nscript.\n\nSigned-off-by: Jacob Keller <jacob.keller@gmail.com>\n---\nI ran into this because I use --contents in a process I'm working on for\ncomparing differences between two forks of a project. I use blame --contents\nwhere I point blame to the contents from the other repository. It is useful\nto be able to do with at arbitrary commits. Currently I have to switch the\nworking tree to the commit rather than being able to target a commit by its\noid.\n\nWith this change I can more easily run this process without the need to\nactually check the contents out in the working tree. Its relatively simple\nto make --contents work with a revision, since I just need to generate the\nfake contents starting from that revision instead of starting from HEAD.\n\nThis might make it possible for --contents to work with --reverse now as\nwell, but I haven't investigated that.\n\n Documentation/blame-options.txt |  9 ++++-----\n blame.c                         | 27 ++++++++++++++++-----------\n t/annotate-tests.sh             | 14 ++++++++++++++\n 3 files changed, 34 insertions(+), 16 deletions(-)\n\ndiff --git a/Documentation/blame-options.txt b/Documentation/blame-options.txt\nindex 9a663535f443..6476dd327377 100644\n--- a/Documentation/blame-options.txt\n+++ b/Documentation/blame-options.txt\n@@ -64,11 +64,10 @@ include::line-range-format.txt[]\n \tmanual page.\n \n --contents <file>::\n-\tWhen <rev> is not specified, the command annotates the\n-\tchanges starting backwards from the working tree copy.\n-\tThis flag makes the command pretend as if the working\n-\ttree copy has the contents of the named file (specify\n-\t`-` to make the command read from the standard input).\n+\tPretend the file being annotated has the contents from the named\n+\tfile instead of using the contents of <rev> or the working tree\n+\tcopy. You may specify '-' to make the command read from standard\n+\tinput for the file contents.\n \n --date <format>::\n \tSpecifies the format used to output dates. If --date is not\ndiff --git a/blame.c b/blame.c\nindex e45d8a3bf92a..52fca5a7f5b7 100644\n--- a/blame.c\n+++ b/blame.c\n@@ -177,12 +177,12 @@ static void set_commit_buffer_from_strbuf(struct repository *r,\n static struct commit *fake_working_tree_commit(struct repository *r,\n \t\t\t\t\t       struct diff_options *opt,\n \t\t\t\t\t       const char *path,\n-\t\t\t\t\t       const char *contents_from)\n+\t\t\t\t\t       const char *contents_from,\n+\t\t\t\t\t       struct object_id *oid)\n {\n \tstruct commit *commit;\n \tstruct blame_origin *origin;\n \tstruct commit_list **parent_tail, *parent;\n-\tstruct object_id head_oid;\n \tstruct strbuf buf = STRBUF_INIT;\n \tconst char *ident;\n \ttime_t now;\n@@ -198,10 +198,7 @@ static struct commit *fake_working_tree_commit(struct repository *r,\n \tcommit->date = now;\n \tparent_tail = &commit->parents;\n \n-\tif (!resolve_ref_unsafe(\"HEAD\", RESOLVE_REF_READING, &head_oid, NULL))\n-\t\tdie(\"no such ref: HEAD\");\n-\n-\tparent_tail = append_parent(r, parent_tail, &head_oid);\n+\tparent_tail = append_parent(r, parent_tail, oid);\n \tappend_merge_parents(r, parent_tail);\n \tverify_working_tree_path(r, commit, path);\n \n@@ -2772,22 +2769,30 @@ void setup_scoreboard(struct blame_scoreboard *sb,\n \t\tsb->commits.compare = compare_commits_by_reverse_commit_date;\n \t}\n \n-\tif (sb->final && sb->contents_from)\n-\t\tdie(_(\"cannot use --contents with final commit object name\"));\n-\n \tif (sb->reverse && sb->revs->first_parent_only)\n \t\tsb->revs->children.name = NULL;\n \n-\tif (!sb->final) {\n+\tif (sb->contents_from || !sb->final) {\n+\t\tstruct object_id head_oid, *parent_oid;\n+\n \t\t/*\n \t\t * \"--not A B -- path\" without anything positive;\n \t\t * do not default to HEAD, but use the working tree\n \t\t * or \"--contents\".\n \t\t */\n+\t\tif (sb->final) {\n+\t\t\tparent_oid = &sb->final->object.oid;\n+\t\t} else {\n+\t\t\tif (!resolve_ref_unsafe(\"HEAD\", RESOLVE_REF_READING, &head_oid, NULL))\n+\t\t\t\tdie(\"no such ref: HEAD\");\n+\t\t\tparent_oid = &head_oid;\n+\t\t}\n+\n \t\tsetup_work_tree();\n \t\tsb->final = fake_working_tree_commit(sb->repo,\n \t\t\t\t\t\t     &sb->revs->diffopt,\n-\t\t\t\t\t\t     sb->path, sb->contents_from);\n+\t\t\t\t\t\t     sb->path, sb->contents_from,\n+\t\t\t\t\t\t     parent_oid);\n \t\tadd_pending_object(sb->revs, &(sb->final->object), \":\");\n \t}\n \ndiff --git a/t/annotate-tests.sh b/t/annotate-tests.sh\nindex f1b9a6ce4dae..b35be20cf327 100644\n--- a/t/annotate-tests.sh\n+++ b/t/annotate-tests.sh\n@@ -72,6 +72,16 @@ test_expect_success 'blame 1 author' '\n \tcheck_count A 2\n '\n \n+test_expect_success 'blame with --contents' '\n+\tcheck_count --contents=file A 2\n+'\n+\n+test_expect_success 'blame with --contents changed' '\n+\techo \"1A quick brown fox jumps over the\" >contents &&\n+\techo \"another lazy dog\" >>contents &&\n+\tcheck_count --contents=contents A 1 \"Not Committed Yet\" 1\n+'\n+\n test_expect_success 'blame in a bare repo without starting commit' '\n \tgit clone --bare . bare.git &&\n \t(\n@@ -98,6 +108,10 @@ test_expect_success 'blame 2 authors' '\n \tcheck_count A 2 B 2\n '\n \n+test_expect_success 'blame with --contents and revision' '\n+\tcheck_count -h testTag --contents=file A 2 \"Not Committed Yet\" 2\n+'\n+\n test_expect_success 'setup B1 lines (branch1)' '\n \tgit checkout -b branch1 main &&\n \techo \"3A slow green fox jumps into the\" >>file &&\n-- \n2.40.0.131.g970374df36d8\n\n"},{"id":"474073","messageId":"xmqqy1nm69se.fsf@gitster.g","threadId":"59463","inReplyTo":"20230324010457.275902-1-jacob.e.keller@intel.com","subject":"Re: [PATCH] blame: allow --contents to work with non-HEAD commit","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2023-03-24T04:41:21Z","receivedAt":"2023-03-24T04:41:25Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jacob Keller <jacob.e.keller@intel.com> writes:\n\n> From: Jacob Keller <jacob.keller@gmail.com>\n>\n> The --contents option can be used with git blame to blame the file as if\n> it had the contents from the specified file. This is akin to copying the\n> contents into the working tree and then running git blame. This option\n> has been supported since 1cfe77333f27 (\"git-blame: no rev means start\n> from the working tree file.\")\n>\n> The --contents option always blames the file as if it was based on the\n> current HEAD commit. If you try to pass a revision while using\n> --contents, you get the following error:\n>\n>   fatal: cannot use --contents with final commit object name\n>\n> This is because the blame process generates a fake working tree commit\n> which always uses the HEAD object.\n\n\"the HEAD object as its sole parent.\"\n\n> Fix fake_working_tree_commit to take the object ID to use for the\n> parent instead of always using HEAD. If both a revision and --contents\n> is provided, look up the object ID from the provided revision instead of\n> using HEAD.\n\nAn obvious enhancement.\n\nAs the original author of 1cfe7733 (git-blame: no rev means start\nfrom the working tree file., 2007-01-30), I am not sure if the verb\n\"fix\" is fair to describe this change, though.  If you update the\nworking tree file with contents that is vastly different and totally\nunrelated to the version at HEAD, then with this new feature, your\n\"blame\" can start at the working tree file, and then some commit\nthat is totally unrelated to HEAD, and down the history from it, and\neverything should make sense, but if you smudge your working tree\nfiles that way, it would be quite awkward to use the working tree to\nadvance the history that leads to HEAD.  That is the reason why I\ndesigned the \"fake commit based on off-history contents\" features to\nwork only with HEAD.  But unlike actually messing with the contents\nof the working tree files, feeding a temporary contents via the\n\"--contents\" option has much less chance of breaking the next\ncommit, so I do not have any objection to this patch.\n\nThanks.\n"},{"id":"474076","messageId":"4f01d1a1-8e4b-39f2-1ccc-980f2345db73@intel.com","threadId":"59463","inReplyTo":"xmqqy1nm69se.fsf@gitster.g","subject":"Re: [PATCH] blame: allow --contents to work with non-HEAD commit","fromName":"Jacob Keller","fromEmail":"jacob.e.keller@intel.com","sentAt":"2023-03-24T06:00:10Z","receivedAt":"2023-03-24T06:00:20Z","isPatch":true,"sender":{"key":"jacob.e.keller@intel.com","avatar":"https://avatars.githubusercontent.com/u/874719?v=4"},"body":"\n\nOn 3/23/2023 9:41 PM, Junio C Hamano wrote:\n> Jacob Keller <jacob.e.keller@intel.com> writes:\n> \n>> From: Jacob Keller <jacob.keller@gmail.com>\n>>\n>> The --contents option can be used with git blame to blame the file as if\n>> it had the contents from the specified file. This is akin to copying the\n>> contents into the working tree and then running git blame. This option\n>> has been supported since 1cfe77333f27 (\"git-blame: no rev means start\n>> from the working tree file.\")\n>>\n>> The --contents option always blames the file as if it was based on the\n>> current HEAD commit. If you try to pass a revision while using\n>> --contents, you get the following error:\n>>\n>>   fatal: cannot use --contents with final commit object name\n>>\n>> This is because the blame process generates a fake working tree commit\n>> which always uses the HEAD object.\n> \n> \"the HEAD object as its sole parent.\"\n> \n\nAh, good correction.\n\n>> Fix fake_working_tree_commit to take the object ID to use for the\n>> parent instead of always using HEAD. If both a revision and --contents\n>> is provided, look up the object ID from the provided revision instead of\n>> using HEAD.\n> \n> An obvious enhancement.\n> \n> As the original author of 1cfe7733 (git-blame: no rev means start\n> from the working tree file., 2007-01-30), I am not sure if the verb\n> \"fix\" is fair to describe this change, though. \n\nRight, this is an enhancement, not a fix. I reworded this in v2.\n\n> If you update the\n> working tree file with contents that is vastly different and totally\n> unrelated to the version at HEAD, then with this new feature, your\n> \"blame\" can start at the working tree file, and then some commit\n> that is totally unrelated to HEAD, and down the history from it, and\n> everything should make sense, but if you smudge your working tree\n> files that way, it would be quite awkward to use the working tree to\n> advance the history that leads to HEAD.  That is the reason why I\n> designed the \"fake commit based on off-history contents\" features to\n> work only with HEAD.  But unlike actually messing with the contents\n> of the working tree files, feeding a temporary contents via the\n> \"--contents\" option has much less chance of breaking the next\n> commit, so I do not have any objection to this patch.\n> > Thanks.\n\nRight. This doesn't change the behavior for if --contents is not\nprovided. If a revision is specified, we ignore the working tree and\njust use the revision. If no revision is specified, we use HEAD but\ngenerate the fake working commit that includes the staged changes. Using\nworking tree with arbitrary commits doesn't usually make sense. If you\n*do* actually want that, its possible to do now with \"--contents\npath/to/working-tree-file\", but you have to opt-in by using --contents.\n\nThe change should only modify the behavior if --contents is provided. In\nthat case, we always use that file contents and assume you know what\nyou're doing with respect to the contents you want to blame.\n"}]}