Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git grep #679

Closed
mac2000 opened this issue Sep 8, 2021 · 3 comments
Closed

git grep #679

mac2000 opened this issue Sep 8, 2021 · 3 comments

Comments

@mac2000
Copy link

mac2000 commented Sep 8, 2021

Will be so nice to have git grep command, e.g.:

$ git grep "Something"
src/demo.js:                        console.log('Something happened')
test/demo.spec.js:  "Seomething": "Should not be printed",
const results = await git.grep('Something')
console.log(results[0][0]) // src/demo.js
console.log(results[0][1]) // console.log('Something happened')

it might be as easy as calling exec sync and parsing stdout with something like:

stdout.split("\n").map(line => line.trim()).filter(line => !!line).map(line => line.split(/:\s+/g))

something like:

src/lib/tasks/grep.ts

import { StringTask } from '../types';

export function grepTask(regexp: string): StringTask<string[][]> {
   return {
      commands: ['grep', regexp],
      format: 'utf-8',
      parser (stdOut) {
         return stdOut.split("\n").map(line => line.trim()).filter(line => !!line).map(line => line.split(/:\s+/g));
      }
   }
}
@steveukx
Copy link
Owner

steveukx commented Sep 8, 2021

git grep is a great suggestion, thank you for opening the issue.

I will add a task, similar to the implementation you've suggested but making use of the utilities in the package for parsing responses line by line, and allowing supported options to be passed through to git.

@mac2000
Copy link
Author

mac2000 commented Sep 17, 2021

BTW: just in case, I gave you wrong chain, it wont handle strings like:

"foo": "acme: 42"

more correct one will be:

stdou
    .toString()
    .split("\n")
    .map(line => line.trim())
    .filter(line => !!line)
    .map(line => [line.split(/:\s+/, 1).shift(), line.substring(line.split(/:\s+/, 1).shift().length).replace(/^:\s+/, '')])
    .map(([path, line]) => ({path, line}))

yep, it isn't optiomal, just want to highlight mistake

@steveukx steveukx mentioned this issue Oct 18, 2021
@steveukx
Copy link
Owner

steveukx commented Oct 19, 2021

Hi, you can now make use of git.grep(...) in simple-git version 2.47.0 on npm now.

Examples: https://github.com/steveukx/git-js/blob/main/examples/git-grep.md

Please do let me know if you have any difficulties using it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants