Sometimes, when you're fixing up files in git-managed files, you'll create a regression by nuking a line (or whole blocks) of code. If you can remember something that was in that nuked chunk of code, you can write a quick script to find all prior commits that referenced that chunk of code.
In this particular case, one of my peers was working on writing some Jenkins pipeline-definitions. The pipeline needed to automagically create S3 pre-signed URLs. At some point, the routine for doing so got nuked. Because coming up with the requisite interpolation-protections had been kind of a pain in the ass, we really didn't want to have to go through the pain of reinventing that particular wheel.
So, how to make git help us find the missing snippet. git log
, horsed to a quick loop-iteration, can do the trick:
for FILE in $( find <PROJECT\_ROOT\_DIR> -name "\*.groovy" ) do echo $FILE git log --pretty=" %H" -Spresign $FILE done | grep ^commit
In the above:
- We're executing within the directory created by the original
git clone
invocation. To limit the search-scope, you can also run it from a subdirectory of the project. - Since, in this example case, we know that all the Jenkins pipeline definitions end with the
.groovy
extension, we limit our search to just those file-types. - The
-Spresign
is used to tellgit log
to look for the stringpresign
. - The
--pretty=" %H"
suppresses all the other output from thegit log
command's output - ensuring that only the commit-ID is print. The leading spaces in the quoted string provide a bit of indenting to make the output-groupings a bit easier to intuit.
This quick loop provides us a nice list of commit-IDs like so:
./Deployment/Jenkins/agent/agent-instance.groovy 64e2039d593f653f75fd1776ca94bdf556166710 619a44054a6732bacfacc51305b353f8a7e5ebf6 1e8d5e40c7db2963671457afaf2d16e80e42951f bb7af6fd6ed54aeca54b084627e7e98f54025c85 ./Deployment/Jenkins/master/Jenkins\_master\_infra.groovy ./Deployment/Jenkins/master/Jenkins\_S3-MigrationHelper.groovy ./Deployment/Jenkins/master/Master-Ec2-Instance.groovy ./Deployment/Jenkins/master/Master-Elbv1.groovy ./Deployment/Jenkins/master/parent-instance.groovy
In the above, only the file Deployment/Jenkins/agent/agent-instance.groovy
contains our searched-for string. The first-listed commit-ID (indented under the filename) contains the subtraction-diff for the targeted string. Similary, the second commit-ID contains the code snippet we're actually after. The remaining commit-IDs contain the original "invention of the wheel",
In this particular case, we couldn't simply revert to the specific commit as there were a lot of other changes that were desired. However, it did let the developer use git show
so that he could copy out the full snippet he wanted back in the current version(s) of his pipelines.
Top comments (0)