List Modified Files by Filtering Keyword in git Commit Messages
As you can see in the above screenshot, my last commit in this PR deliberately fails the CI flow in Drone and we can see the "All checks have failed" warning. But this PR is still mergable (even by a non-admin user).
I was pretty suprised when I was looking for a git command that can list out all the modified files in git commits that contain certain string in commit messages.
Then I found that there's no such a command out of the box.
So I start searching for different commands and put those pieces together.
I have the following restrictions on company's working environment:
- only
git
command is available gh
command is not available
An alternative would be searching the keyword using GUI then open each commit hashes to get the modified files manually. If you've only a few commits, this approach could work pretty well. But it's a time consuming task if you got a lot commits to deal with. So doing it in CLI way would be more efficient.
1git log --pretty=format:"%h %ae %ar %s" | \
2grep 'ticket-1054' | \
3grep 'user@example.com' | \
4grep -v "Merge pull request"
5
653fd98e9 user@example.com 26 hours ago fix: ticket-1054 gate entries feature (case 2904)
7ff96c35f user@example.com 28 hours ago fix: ticket-1054 gate entries feature
8b80f5a95 user@example.com 2 days ago fix: ticket-1054 gate entries feature (case 2902, 2903)
9fa82d145 user@example.com 9 days ago fix: ticket-1054 gate entries feature (case 2891)
105e1aadb0 user@example.com 9 days ago fix: ticket-1054 gate entries feature (translation)
11400afa9a user@example.com 2 weeks ago fix: ticket-1054 gate entries feature (case 2891)
12d3714b22 user@example.com 2 weeks ago fix: ticket-1054 gate entries feature (case 2891)
136e3958e9 user@example.com 2 weeks ago fix: ticket-1054 gate entries feature (case 2889, 2890)
1412ea69f9 user@example.com 2 weeks ago fix: ticket-1054 gate entries feature (case 2888)
15f8ce5a2c user@example.com 2 weeks ago fix: ticket-1054 gate entries feature (case 2887)
1697c0d8c5 user@example.com 2 weeks ago fix: ticket-1054 gate entries feature (case 2884, 2885, 2886, 2887)
17f8f40bcf user@example.com 2 weeks ago fix: ticket-1054 gate entries feature (case 2884, 2885, 2886, 2887)
183531096f user@example.com 3 weeks ago feat: ticket-1054 gate entries feature (translation)
19cab6e773 user@example.com 6 weeks ago fix: ticket-1054 gate entries feature (translation)
20a4762b29 user@example.com 6 weeks ago fix: ticket-1054 gate entries feature (translation)
21f7473d05 user@example.com 6 weeks ago fix: ticket-1054 gate entries feature (case 2816)
2218339de1 user@example.com 6 weeks ago fix: ticket-1054 gate entries feature (case 2816)
234ad4801c user@example.com 7 weeks ago fix: ticket-1054 gate entries feature (case 2816)
242fee90ac user@example.com 7 weeks ago fix: ticket-1054 gate entries feature (case 2815, 2816)
25e8f11317 user@example.com 7 weeks ago fix: ticket-1054 gate entries feature (case 2812, 2814)
2681cab87c user@example.com 10 weeks ago feat: ticket-1054 gate entries feature
Command | Explanation |
---|---|
git log | I use git log command to extract the commit messages Docs |
git log --pretty %h | Abbreviated commit hash Docs |
git log --pretty %ae | Author email Docs |
git log --pretty %ar | Author date, relative Docs |
git log --pretty %s | Subject(first line of the commit message) Docs |
grep 'ticket-1054' | ticket-* is branch name in commit message, which is a my custom keyword in this case. It can be any keyword depends on your needs. |
grep 'user@example.com' | filter by email address |
`bbgrep -v 'Merge pull request' | exclude merge pull request commits (commit of a pull request merge won't show modified files in git diff-tree command later) |
Then we use git diff-tree --no-commit-id --name-only -r <commit-hash>
to loop through all commits to list out the modified files.
1git diff-tree --no-commit-id --name-only -r 53fd98e9
2
3api/queries_pr.go
4api/query_builder.go
5pkg/cmd/pr/checks/aggregate.go
6pkg/cmd/pr/checks/checks.go
7pkg/cmd/pr/checks/checks_test.go
8pkg/cmd/pr/checks/fixtures/allPassing.json
9pkg/cmd/pr/checks/fixtures/onlyRequired.json
10pkg/cmd/pr/checks/fixtures/someFailing.json
11pkg/cmd/pr/checks/fixtures/somePending.json
12pkg/cmd/pr/checks/fixtures/someSkipping.json
13pkg/cmd/pr/checks/fixtures/withStatuses.json
Command | Explanation |
---|---|
git diff-tree | Show modified files Docs |
git diff-tree --no-commit-id | Hide commit ID output Docs |
git diff-tree --name-only | Show only names of changed files. Docs |
git diff-tree -r | recurse into sub-trees (show full path instead of directory only) Docs |