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

grep other branches without checkout #872

Open
Bill2015 opened this issue Dec 9, 2022 · 2 comments
Open

grep other branches without checkout #872

Bill2015 opened this issue Dec 9, 2022 · 2 comments
Labels

Comments

@Bill2015
Copy link

Bill2015 commented Dec 9, 2022

I am trying to use the git grep command to search for text in other branches without checking out to those branches.
Although the git grep command allows me to do this by specifying the branch name (e.g. git grep <Regex> <branchName>), the current grep() function does not seem to support this.

The only way to search for text in other branches is to disable ts-check and then use the following code:

// @ts-nocheck
const result = await simpleGit().grep(['hello', 'origin/master'], ['-C', '1']);

Is there any other way to do this, or is it necessary to implement this feature?

@steveukx
Copy link
Owner

Hello, have you followed the examples in https://github.com/steveukx/git-js/blob/main/examples/git-grep.md ? The grepQueryBuilder provides more of a builder patter to create the grep query that may be of use to you.

I haven't explicitly added options for querying on branches other than the one you have checked out. What would you expect that git command to look like if you were running it in terminal directly?

@steveukx steveukx added question more-info-needed More information is required in order to investigate labels Dec 10, 2022
@Bill2015
Copy link
Author

Thanks for your reply.
I apologize for my poor English, as it is not my first language.

I've used the examples, but the QueryBuilder only support the keyword.

But if i want search keyword components or 'utils'
I can run this command in terminal

git grep -e components -e utils origin/master

Also if I add pathspec filter it will be like this

git grep -e components -e utils origin/master -- '*.tsx'

Also I've look the source code
In this line

const commands = ['grep', '--null', '-n', '--full-name', ...options, ...searchTerm];

The options is front of searchTerm so it is no way to add branch or pathspec
Also if wrote like this

const result = await simpleGit().grep('ts-node origin/frontend');

it will run this like below

git grep -e 'components origin/master'

The git will combine the components and origin/master as a search term
And this source code when I input searchTerm as a string do

if (typeof searchTerm === 'string') {

If this a enhanced feature or bug, could I contribute this?

@github-actions github-actions bot removed the more-info-needed More information is required in order to investigate label Dec 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants