Teaching the diff machinery about blobfind [WAS: git describe <blob>]
Subsystems (2)
cover Teaching the diff machinery about blobfind [WAS: git describe <blob>]
This seems to be an easier approach; thanks Junio for hinting at it. This certainly solves our immediate needs, we may want to build 'git describe <blob>' on top of it or defer it until later.
Thanks,
Stefan
previous descussion
https://public-inbox.org/git/[email protected]/
Stefan Beller (1):
diffcore: add a filter to find a specific blob
Documentation/diff-options.txt | 4 ++++ Makefile | 1 + builtin/log.c | 2 +- diff.c | 20 ++++++++++++++++- diff.h | 3 +++ diffcore-blobfind.c | 51 ++++++++++++++++++++++++++++++++++++++++++ diffcore.h | 1 + revision.c | 3 ++- t/t4064-diff-blobfind.sh | 35 +++++++++++++++++++++++++++++ 9 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 diffcore-blobfind.c create mode 100755 t/t4064-diff-blobfind.sh
Signature · 1 line
2.15.0.128.gcadd42da22
PATCH 1/1 diffcore: add a filter to find a specific blob
Sometimes users are given a hash of an object and they want to identify it further (ex.: Use verify-pack to find the largest blobs, but what are these? or [1])
One might be tempted to extend git-describe to also work with blobs, such that git describe <blob-id> gives a description as '<commit-ish>:<path>'. This was implemented at [2]; as seen by the sheer number of responses (>110), it turns out this is tricky to get right. The hard part to get right is picking the correct 'commit-ish' as that could be the commit that (re-)introduced the blob or the blob that removed the blob; the blob could exist in different branches.
Junio hinted at a different approach of solving this problem, which this patch implements. Teach the diff machinery another flag for restricting the information to what is shown. For example:
$ ./git log --oneline --blobfind=v2.0.0:Makefile b2feb64309 Revert the whole "ask curl-config" topic for now 47fbfded53 i18n: only extract comments marked with "TRANSLATORS:"
we observe that the Makefile as shipped with 2.0 was introduced in v1.9.2-471-g47fbfded53 and replaced in v2.0.0-rc1-5-gb2feb64309 by a different blob.
[1] https://stackoverflow.com/questions/223678/which-commit-has-this-blob
[2] https://public-inbox.org/git/[email protected]/
---
On playing around with this, trying to find more interesting cases, I observed:
git log --oneline --blobfind=HEAD:COPYING 703601d678 Update COPYING with GPLv2 with new FSF address
git log --oneline --blobfind=703601d678^:COPYING
459b8d22e5 tests: do not borrow from COPYING and README from the real source 703601d678 Update COPYING with GPLv2 with new FSF address 075b845a85 Add a COPYING notice, making it explicit that the license is GPLv2.
t/diff-lib/COPYING may need an update of the adress of the FSF, #leftoverbits I guess.
Another interesting case that I found was
git log --oneline --blobfind=v2.14.0:Makefile 3921a0b3c3 perf: add test for writing the index 36f048c5e4 sha1dc: build git plumbing code more explicitly 2118805b92 Makefile: add style build rule
all of which were after v2.14, such that the introduction of that blob doesn't show up; I suspect it came in via a merge as unrelated series may have updated the Makefile in parallel, though git-log should have told me?
Thanks,
Stefan
Documentation/diff-options.txt | 4 ++++ Makefile | 1 + builtin/log.c | 2 +- diff.c | 20 ++++++++++++++++- diff.h | 3 +++ diffcore-blobfind.c | 51 ++++++++++++++++++++++++++++++++++++++++++ diffcore.h | 1 + revision.c | 3 ++- t/t4064-diff-blobfind.sh | 35 +++++++++++++++++++++++++++++ 9 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 diffcore-blobfind.c create mode 100755 t/t4064-diff-blobfind.sh
Documentation/diff-options.txt +4 -0
@@ -500,6 +500,10 @@ information. --pickaxe-regex:: Treat the <string> given to `-S` as an extended POSIX regular expression to match. +--blobfind=<blob-id>:: + Restrict the output such that one side of the diff + matches the given blob-id. + endif::git-format-patch[] -O<orderfile>::
Makefile +1 -0
@@ -775,6 +775,7 @@ LIB_OBJS += date.o LIB_OBJS += decorate.o LIB_OBJS += diffcore-break.o LIB_OBJS += diffcore-delta.o +LIB_OBJS += diffcore-blobfind.o LIB_OBJS += diffcore-order.o LIB_OBJS += diffcore-pickaxe.o LIB_OBJS += diffcore-rename.o
builtin/log.c +1 -1
@@ -181,7 +181,7 @@ static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,init_display_notes (&rev->notes_opt);if (rev->diffopt.pickaxe || rev->diffopt.filter || - rev->diffopt.flags.follow_renames) + rev->diffopt.flags.follow_renames || rev->diffopt.blobfind) rev->always_show_header =0 ;if (source)
diff.c +19 -1
@@ -4082,6 +4082,7 @@ void diff_setup(struct diff_options *options) options->interhunkcontext = diff_interhunk_context_default; options->ws_error_highlight = ws_error_highlight_default; options->flags.rename_empty =1 ; + options->blobfind =NULL ;/* pathchange left =NULL by default */ options->change = diff_change;
@@ -4487,6 +4488,19 @@ static int parse_ws_error_highlight_opt(struct diff_options *opt, const char *arreturn 1 ; } +static int parse_blobfind_opt (struct diff_options *opt,const char *arg) +{ +struct object_id oid; + +if (get_oid_blob (arg, &oid) ||sha1_object_info (oid.hash,NULL ) !=OBJ_BLOB ) +return error ("object '%s' is not a blob" , arg); + +if (!opt->blobfind) + opt->blobfind =xcalloc (1 ,sizeof (*opt->blobfind)); +oidset_insert (opt->blobfind, &oid); +return 1 ; +} +int diff_opt_parse (struct diff_options *options,const char **av,int ac,const char *prefix) {
@@ -4736,7 +4750,8 @@ int diff_opt_parse(struct diff_options *options,else if ((argcount =short_opt ('O' , av, &optarg))) { options->orderfile =prefix_filename (prefix, optarg);return argcount; - } + }else if (skip_prefix (arg,"--blobfind=" , &arg)) +return parse_blobfind_opt (options, arg);else if ((argcount =parse_long_opt ("diff-filter" , av, &optarg))) {int offending =parse_diff_filter_opt (optarg, options);if (offending)
@@ -5770,6 +5785,9 @@ void diffcore_std(struct diff_options *options)diffcore_skip_stat_unmatch (options);if (!options->found_follow) {/* See try_to_follow_renames() in tree-diff.c */ + +if (options->blobfind) +diffcore_blobfind (options);if (options->break_opt !=-1 )diffcore_break (options->break_opt);if (options->detect_rename)
diff.h +3 -0
@@ -7,6 +7,7 @@#include "tree-walk.h" #include "pathspec.h" #include "object.h" +#include "oidset.h" struct rev_info ;struct diff_options ;
@@ -174,6 +175,8 @@ struct diff_options {enum diff_words_type word_diff;enum diff_submodule_format submodule_format; +struct oidset *blobfind; +/* this is set by diffcore for DIFF_FORMAT_PATCH */ int found_changes;
diffcore-blobfind.c new file +51 -0
51 lines not on this page · raw message
diffcore.h +1 -0
@@ -107,6 +107,7 @@ extern struct diff_filepair *diff_queue(struct diff_queue_struct *, struct diff_filespec *);extern void diff_q (struct diff_queue_struct *,struct diff_filepair *); +extern void diffcore_blobfind (struct diff_options *);extern void diffcore_break (int );extern void diffcore_rename (struct diff_options *);extern void diffcore_merge_broken (void );
revision.c +2 -1
@@ -2409,7 +2409,8 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s/* Pickaxe, diff-filter and rename following need diffs */ if (revs->diffopt.pickaxe || revs->diffopt.filter || - revs->diffopt.flags.follow_renames) + revs->diffopt.flags.follow_renames || + revs->diffopt.blobfind) revs->diff =1 ;if (revs->topo_order)
t/t4064-diff-blobfind.sh new file +35 -0
@@ -0,0 +1,35 @@ +#!/bin/sh + +test_description='test finding specific blobs in the revision walking' +. ./test-lib.sh + +test_expect_success 'setup ' ' + git commit --allow-empty -m "empty initial commit" && + + echo "Hello, world!" >greeting && + git add greeting && + git commit -m "add the greeting blob" && # borrowed from Git from the Bottom Up + git tag -m "the blob" greeting $(git rev-parse HEAD:greeting) && + + echo asdf >unrelated && + git add unrelated && + git commit -m "unrelated history" && + + git revert HEAD^ && + + git commit --allow-empty -m "another unrelated commit" +' + +test_expect_success 'find the greeting blob' ' + cat >expect <<-EOF && + Revert "add the greeting blob" + add the greeting blob + EOF + + git log --abbrev=12 --oneline --blobfind=greeting^{blob} >actual.raw && + cut -c 14- actual.raw >actual && + + test_cmp expect actual +' + +test_done
Signature · 1 line
2.15.0.128.gcadd42da22
Re: [PATCH 1/1] diffcore: add a filter to find a specific blob
Quoted 35 lines · Stefan Beller
Sometimes users are given a hash of an object and they want to identify it further (ex.: Use verify-pack to find the largest blobs, but what are these? or [1])
One might be tempted to extend git-describe to also work with blobs, such that
git describe <blob-id>gives a description as '<commit-ish>:<path>'. This was implemented at [2]; as seen by the sheer number of responses (>110), it turns out this is tricky to get right. The hard part to get right is picking the correct 'commit-ish' as that could be the commit that (re-)introduced the blob or the blob that removed the blob; the blob could exist in different branches.Junio hinted at a different approach of solving this problem, which this patch implements. Teach the diff machinery another flag for restricting the information to what is shown. For example:
$ ./git log --oneline --blobfind=v2.0.0:Makefile b2feb64309 Revert the whole "ask curl-config" topic for now 47fbfded53 i18n: only extract comments marked with "TRANSLATORS:"we observe that the
Makefileas shipped with 2.0 was introduced in v1.9.2-471-g47fbfded53 and replaced in v2.0.0-rc1-5-gb2feb64309 by a different blob.[1] https://stackoverflow.com/questions/223678/which-commit-has-this-blob
[2] https://public-inbox.org/git/[email protected]/Signed-off-by Stefan Beller <[email protected]>---
On playing around with this, trying to find more interesting cases, I observed:
git log --oneline --blobfind=HEAD:COPYING
703601d678Update COPYING with GPLv2 with new FSF address
Quoted 7 lines
git log --oneline --blobfind=703601d678^:COPYING
459b8d22e5tests: do not borrow from COPYING and README from the real source703601d678Update COPYING with GPLv2 with new FSF address075b845a85Add a COPYING notice, making it explicit that the license is GPLv2.t/diff-lib/COPYING may need an update of the adress of the FSF, # leftoverbits I guess.
I do not think so. See tz/fsf-address-update topic for details.
Please do not contaminate the list archive with careless mention of "hash-mark plus left over bits", as it will make searching the real good bits harder. Thanks.
Quoted 9 lines
Another interesting case that I found was
git log --oneline --blobfind=v2.14.0:Makefile3921a0b3c3perf: add test for writing the index36f048c5e4sha1dc: build git plumbing code more explicitly2118805b92Makefile: add style build ruleall of which were after v2.14, such that the introduction of that blob doesn't show up; I suspect it came in via a merge as unrelated series may have updated the
Makefilein parallel, though git-log should have told me?
If that is the case, shouldn't we make this new mode imply --full-history to forbid history simplification? "git log" is a tool to find _an_ explanation of the current state, and the usual history simplification makes tons of sense there, but blobfind is run most likely in order to find _all_ mention of the set of blobs given.
Documentation/diff-options.txt information. · line 507
Quoted 13 lines
Documentation/diff-options.txt +4
-0@@ -500,6 +500,10 @@ information. --pickaxe-regex:: Treat the <string> given to `-S` as an extended POSIX regular expression to match. +--blobfind=<blob-id>:: + Restrict the output such that one side of the diff + matches the given blob-id. + endif::git-format-patch[]
Can we have a blank line between these enumerations to make the source easier to read? Thanks.
Quoted 20 lines
diffcore-blobfind.c new file +14
-0@@ -0,0 +1,51 @@ +/* +* Copyright (c) 2017 Google Inc. +*/ +#include "cache.h" +#include "diff.h" +#include "diffcore.h" + +static void diffcore_filter_blobs (struct diff_queue_struct *q, +struct diff_options *options) +{ +int i, j =0 , c = q->nr; + +if (!options->blobfind) +BUG ("blobfind oidset not initialized???" );
diffcore-blobfind.c line 24
Quoted 10 lines
diffcore-blobfind.c new file +10
-0+ +for (i =0 ; i < q->nr; i++) { +struct diff_filepair *p = q->queue[i]; + +if (DIFF_PAIR_UNMERGED (p) || + (DIFF_FILE_VALID (p->one) && +oidset_contains (options->blobfind, &p->one->oid)) || + (DIFF_FILE_VALID (p->two) && +oidset_contains (options->blobfind, &p->two->oid))) +continue ;
So, we keep an unmerged pair, a pair that mentions a sought-blob on one side or the other side? I am not sure if we want to keep the unmerged pair for the purpose of this one.
diffcore-blobfind.c line 28
Quoted 3 lines
diffcore-blobfind.c new file +3
-0+diff_free_filepair (p); + q->queue[i] =NULL ; + c--;
Also, if you are doing the in-place shrinking and have already introduced another counter 'j' that is initialized to 0, I think it makes more sense to do the shrinking in-place. 'i' will stay to be the source-scan pointer that runs 0 thru q->nr, while 'j' can be used in this loop (where you have 'continue') to move the current one that is determined to survive from q->queue[i] to q->queue[j++].
Then you do not need 'c'; when the loop ends, 'j' would be the number of surviving entries and q->nr can be adjusted to it. Unlike the usual pattern taken by the other diffcore transformations where a new queue is populated and the old one discarded, this would leave the q->queue[] over-allocated, but I do not think it is too bad.
Re: [PATCH 1/1] diffcore: add a filter to find a specific blob
Quoted 6 lines · Junio C Hamano
If that is the case, shouldn't we make this new mode imply --full-history to forbid history simplification? "git log" is a tool to find _an_ explanation of the current state, and the usual history simplification makes tons of sense there, but blobfind is run most likely in order to find _all_ mention of the set of blobs given.
One scenario that I think we may want to be careful about is this:
---o---*---*---A*--M*--o---X
\ /
o---*---o---Bwhere commits marked with '*' has the same blob M:Makefile you are looking for at the same path Makefile, and we start traversal at X with "git log --blobfind=M:Makefile X" (or even with a pathspec, i.e. "git log --blobfind=M:Makefile X -- Makefile).
The usual merge simplification rules would say "Ah, M and A are TREESAME so we do not have to look at the side branch that ends at B". If the user is interested in finding all the introduction and the retirement of a specific blob object, we would miss the transition around the '*' on that side branch and ends up finding only the transitions after the fork point where the blob is introduced, and after M where the blob is retired.
Another interesting case we may want to be careful is this:
---A*--M*--o---X
/
---B*for the same reason. The usual merge simplification rules are designed to come up with _an_ explanation for the state in X, and because M is TREESAME with both A and B, it would pick just one (the first parent) while ignoring the other. Again, that would not be appropriate if the reason why the user is running the command is to find all the introduction and the retirement of an object.
It may be worth covering these in the tests (I didn't try to see specifically if the patch has these cases already, as I didn't think of the issue when I responded---sorry about that).
Thanks.
Re: [PATCH 1/1] diffcore: add a filter to find a specific blob
In reply to Junio C HamanoJunio C Hamano <[email protected]> writes:
After saying "Will merge to 'next'" in the recent "What's cooking" report, I noticed that a few loose ends were never tied on this topic.
Quoted 33 lines
Documentation/diff-options.txt information. · line 507
Quoted 13 lines
Documentation/diff-options.txt +4
-0@@ -500,6 +500,10 @@ information. --pickaxe-regex:: Treat the <string> given to `-S` as an extended POSIX regular expression to match. +--blobfind=<blob-id>:: + Restrict the output such that one side of the diff + matches the given blob-id. + endif::git-format-patch[]Can we have a blank line between these enumerations to make the source easier to read? Thanks.
...
So, we keep an unmerged pair, a pair that mentions a sought-blob on one side or the other side? I am not sure if we want to keep the unmerged pair for the purpose of this one.Documentation/diff-options.txt information.
Quoted 3 lines
Documentation/diff-options.txt +3
-0+ diff_free_filepair(p); + q->queue[i] = NULL; + c--;Also, if you are doing the in-place shrinking and have already introduced another counter 'j' that is initialized to 0, I think it makes more sense to do the shrinking in-place. 'i' will stay to be the source-scan pointer that runs 0 thru
q->nr, while 'j' can be used in this loop (where you have 'continue') to move the current one that is determined to survive from q->queue[i] to q->queue[j++].
Quoted 5 lines
Then you do not need 'c'; when the loop ends, 'j' would be the number of surviving entries and
q->nrcan be adjusted to it. Unlike the usual pattern taken by the other diffcore transformations where a new queue is populated and the old one discarded, this would leave the q->queue[] over-allocated, but I do not think it is too bad.
Here is to illustrate the last point. I still think we should keep the unmerged entries for the purpose of blobfind but it should be trivial to fix that.
diffcore-blobfind.c | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-)
diffcore-blobfind.c +12 -21
@@ -8,40 +8,31 @@static void diffcore_filter_blobs (struct diff_queue_struct *q,struct diff_options *options) { -int i, j =0 , c = q->nr; +int src, dst;if (!options->blobfind)BUG ("blobfind oidset not initialized???" ); -for (i =0 ; i < q->nr; i++) { -struct diff_filepair *p = q->queue[i]; +for (src = dst =0 ; src < q->nr; src++) { +struct diff_filepair *p = q->queue[src];if (DIFF_PAIR_UNMERGED (p) || (DIFF_FILE_VALID (p->one) &&oidset_contains (options->blobfind, &p->one->oid)) || (DIFF_FILE_VALID (p->two) && -oidset_contains (options->blobfind, &p->two->oid))) -continue ; - -diff_free_filepair (p); - q->queue[i] =NULL ; - c--; - } - -/* Keep it sorted. */ - i =0 ; j =0 ; -while (i < c) { -while (!q->queue[j]) - j++; - q->queue[i] = q->queue[j]; - i++; j++; +oidset_contains (options->blobfind, &p->two->oid))) { + q->queue[dst] = p; + dst++; + }else { +diff_free_filepair (p); + } } - q->nr = c; - -if (!c) { +if (!dst) {free (q->queue);DIFF_QUEUE_CLEAR (q); + }else { + q->nr = dst; } }