Step changes in git based on regular expressions

A lot of my debugging code is wrapped in a header / footer. I usually run a script that simply removes all of it from the source code before I start and commit the changes. If the debugging code should be saved, I ran git add -p and would execute the individual fragments.

My question is, is it possible to generate regex based changes? For example, given this javascript snippet:

 function littleNinja( sword ) { /* debug:start */ console.log( sword ); /* debug:stop */ // do stuff } 

I don't want git to run the lines between and debug:start and debug:stop , and I hope this process can be automated.

+7
source share
1 answer

A good way to do this is to use clean and grease filters to filter out selected lines as they are set. (The description below is essentially the same as the procedure described in this section of Pro Git, and which is very similar to the one in git attributes .) First of all, we need a command that deletes the lines between (and including) each debug:start and debug:end - fortunately, this is a simple job with sed :

 sed '/debug:start/,/debug:stop/d' < foo.js 

Now create a .gitattributes file at the top level of your git repository, which contains the following line:

 *.js filter=strip-debug 

And now determine what the strip-debug filter does both during cleaning (when setting) and when lubricating (when checked out):

 git config filter.strip-debug.clean "sed '/debug:start/,/debug:stop/d'" git config filter.strip-debug.smudge cat 

I (very briefly) checked this and seems to be doing what you want.

+8
source

All Articles