Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search a block of code as a whole

Tags:

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?

like image 200
Gqqnbig Avatar asked Nov 22 '17 19:11

Gqqnbig


People also ask

How do I search an entire project in Vscode?

VS Code allows you to quickly search over all files in the currently opened folder. Press Ctrl+Shift+F and enter your search term. Search results are grouped into files containing the search term, with an indication of the hits in each file and its location.

How do you select a block of code?

You can select blocks of text by holding Shift+Alt (Shift+Option on macOS) while you drag your mouse.

How do you comment out multiple lines in code blocks?

You can also block comment multiple lines of code using the characters /* */ . Tip: The characters /* */ on lines that already are already commented out are not affected when you comment and uncomment code as described above.

How do you comment out large sections of code in Python?

To comment out multiple lines in Python, you can prepend each line with a hash ( # ). With this approach, you're technically making multiple single-line comments. The real workaround for making multi-line comments in Python is by using docstrings.


2 Answers

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.

like image 197
jthill Avatar answered Sep 21 '22 13:09

jthill


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.

like image 44
F. Rihani Avatar answered Sep 19 '22 13:09

F. Rihani