Search specific git commit by commit message and exlude branches

853 Views Asked by At

I am currently working on a program to filter slow db queries that are stored in a database on the command line.

I would like to search all the commits in all the branches except the one specified and return all the commits that match.

My filter conditions are stored in a database (which is provided for me).

Example:

I have the following entry in the database:

ID key       Query
1  ABCDEF    select * from example
2  0ABCDE    select * from another_example
3  1ABCDE    select * from you_get_the_picture

I am now interested in the queries that haven't been fixed in the code yet. So I need to search through the branches with the key as a search filter.

If the key is found in a commit, skip and look up the next. If no match is found, print it to stdout. All the code works fine but I'm having trouble finding the correct git command.

I have the following:

git grep 'ABCDEF' $(git rev-list ^origin/master) | xargs git show -s --format=%N%s

Which should return all commits containing "ABCDEF" in the commit message in all the branches except origin/master.

However, the git command doesn't return anything which is not possible since I know that these commits are there.

Is my git command not correct? Thanks in advance for any pointers.

2

There are 2 best solutions below

2
On BEST ANSWER

This is not what you want:

$ git rev-list ^origin/master
$

You asked git rev-list to exclude all revs reachable from origin/master, and to include nothing, so it produces nothing.

This may be what you meant:

$ git rev-list --branches ^origin/master
c2eb39026567499ba9fe0c679766c370462ae26f

Or you might want --tags and/or --remotes as well or instead; or even --all although that includes references like refs/stash.

Of course, that goes inside the git grep commit arguments as you've shown in your example code; it should work from there—except that git grep produces the matching lines, not commit IDs.

0
On

You have now (May 2021, seven years later) a much more precise filtering mechanism with Git 2.32 (Q2 2021): "git rev-list"(man) learns the --filter=object:type=<type> option, which can be used to exclude objects of the given kind from the packfile generated by pack-objects.

See commit 9cf68b2, commit 169a15e, commit 7ab6aaf, commit b0c42a5 (19 Apr 2021), commit 9a2a4f9 (12 Apr 2021), and commit 628d81b, commit b2025da, commit a812789 (09 Apr 2021) by Patrick Steinhardt (pks-t).
(Merged by Junio C Hamano -- gitster -- in commit 8585d6c, 07 May 2021)

list-objects: implement object type filter

Signed-off-by: Patrick Steinhardt

While it already is possible to filter objects by some criteria in git-rev-list, it is not yet possible to filter out only a specific type of objects.

This makes some filters less useful.

The blob:limit filter for example filters blobs such that only those which are smaller than the given limit are returned.
But it is unfit to ask only for these smallish blobs, given that git-rev-list will continue to print tags, commits and trees.

Now that we have the infrastructure in place to also filter tags and commits, we can improve this situation by implementing a new filter which selects objects based on their type.
Above query can thus trivially be implemented with the following command:

$ git rev-list --objects --filter=object:type=blob \
    --filter=blob:limit=200

Furthermore, this filter allows to optimize for certain other cases: if for example only tags or commits have been selected, there is no need to walk down trees.

The new filter is not yet supported in bitmaps.
This is going to be implemented in a subsequent commit.

git config now includes in its man page:

blob:limit, object:type, tree, sparse:oid, or combine. If using combined filters, both combine and all of the nested filter kinds must be allowed. Defaults to uploadpackfilter.allow.

rev-list-options now includes in its man page:

The form '--filter=object:type=(tag|commit|tree|blob)' omits all objects which are not of the requested type.

And:

rev-list: allow filtering of provided items

Signed-off-by: Patrick Steinhardt

When providing an object filter, it is currently impossible to also filter provided items.
E.g.
when executing git rev-list(man) HEAD , the commit this reference points to will be treated as user-provided and is thus excluded from the filtering mechanism.
This makes it harder than necessary to properly use the new --filter=object:type filter given that even if the user wants to only see blobs, he'll still see commits of provided references.

Improve this by introducing a new --filter-provided-objects option to the git-rev-parse(1) command.
If given, then all user-provided references will be subject to filtering.

rev-list-options now includes in its man page:

--filter-provided-objects

Filter the list of explicitly provided objects, which would otherwise always be printed even if they did not match any of the filters. Only useful with --filter=.