Monday, December 4, 2017

How to search a block of code as a whole

Leave a Comment

My files has

.settings { }   .field {     margin-bottom: 1em; } 

I want to know when

.settings { } 

was formed.

I know if I want to search one line of code, I do git log -S".settings {".

But here I want to search

.settings { } 

as a whole, I tried

git log -S".settings {\r}" confirm.less.css git log -S".settings {\n}" confirm.less.css git log -S".settings {\r\n}" confirm.less.css 

but all retrun nothing.

How do I search a block of code as a whole containg new line characters?

2 Answers

Answers 1

Try

git log -L '/^.settings {/,+1':path/to/it 

That'll get you the whole history of updates to that range, but the oldest of them will be what you want.

Answers 2

There was a few issues getting this to work properly.

First, I think you need to handle the { } metacharacters in your RegEx by escaping them, and enable extended matching with --pickaxe-regex. I also used a simple \s* to greedily match any space including newlines between brackets.

Here is the resulting command that I came up with

git log -S".settings \{\s*\}" --pickaxe-regex confirm.less.css

Which returned the commit containing the first appearance.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment