I want to search for multiple repositories in the Windows batch.

Asked 2 years ago, Updated 2 years ago, 136 views

What do you want to do

The purpose is to make it possible to search for the same search word in bulk for various locally pulled repositories and output it as text files.

ideal

Once you have entered a search word, you are looking for a remote repository that you pulled locally, and you are looking for a file that lists the search results to be printed locally.

If there is a list of directories such as the following, I would like to run a git grep "search word" command on all repositories to list the files that were able to be printed in a text file and some of the source code that was searched in a text file.
I'm sorry to ask you a rudimentary question, but I'd appreciate it if you could let me know.

C: ggit/repositions_aaa
C: ggit / repositories_bbb
C: ggit/repositions_ccc
C: ggit / repositories_ddd

git batch-file command-prompt

2022-09-30 16:36

3 Answers

"It says ""In Windows Batch"", but if you can use Git, you must have installed Git Bash, so I think it would be easier to use the Unix command."

If the repository is in one folder or less, you can combine the find command, the xargs command, and the grep command as follows:

$cd/c/git
$ find.|xargs grep-l "keyword"

The grep -l is an option to display only the filename.

Starting from the directory where you executed the command, the path to the file containing the keyword is listed, so you should be able to determine which repository it contains.
If you want to leave a result as a text file, just save it as a redirect.


2022-09-30 16:36

As requested, the Windows batch will look like this:

  • Considering that the destination directory has a repository/folder other than the destination repository, or that the destination directory is distributed in the first place, specify a list of destination repositories in the text file and process them with the FOR command. The list is a line-by-line entry with a full path starting with the drive name
  • Information indicating which repository is attached to all discovered lines using the sed command in Git Bash. The git grep result is a relative path from the top of the repository even if you specify the full name, so insert the path of the repository before that
  • Add a path to the Unix commands folder in Git Bash (if already built in, don't use that line)
  • Git grep options/search patterns can be specified (up to 10) (if always specified, pre-embedded in the appropriate line of the batch file)
@ECHO OFF
IF "%3" == "(
  @ECHO parameter is missing.
  @ECHO Usage: %~n0 target repository list file path result output file path git grep option/pattern 1 or more 10 or less
  EXIT/B2
)
IF NOT EXIST "%-1" (
  @ECHO Destination repository list file does not exist.
  EXIT/B1
)
SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
SET PATH=C:\Program Files\Git\usr\bin; %PATH%
SET TARGETREPOLIST=%-1
SET OUTPUTPATH = "%-2"
SHIFT
SHIFT
SHIFT
SET GREPARGS = %0%1%2%3%4%5%6%8%9
PUSHD
FOR/F%%Ein(%TARGETREPOLIST%)do(
    CALL: REPOGREP%%E
)
POPD
EXIT/B0

—REPOGREP
CD/D "%-1"
@ECHO=========="%~1" processing in progress==========
SET REPODIR = % to 1
US>SET REPODIR=%REPODIR:\=\/%
git grep%GREPARGS% | sed-e"/^-*$/!s/^/%REPODIR%\//">%OUTPUTPATH%
EXIT/B0

If the destination repository is not a text file, but everything below the specified directory:

 FOR/F%%Ein(%TARGETREPOLIST%)do(

It would be better to do the following.

 FOR/D%%Ein(%TARGETREPOLIST%\*)do(

If you want to include information indicating which repository, instead of attaching it to every line, in the output as a delimited line:

SET REPODIR=%-1
US>SET REPODIR=%REPODIR:\=\/%
git grep%GREPARGS% | sed-e"/^-*$/!s/^/%REPODIR%\//">%OUTPUTPATH%

It would be better to do the following.

@ECHO=========="%-1"==========>>%OUTPUTPATH%
git grep%GREPARGS%>>%OUTPUTPATH%

Please make changes as needed or requested.


2022-09-30 16:36

Since it's a Windows environment, you might want to use a PowerShell script.

grep_git_repos.ps1

Param ([string])$topdir, [string]$searchWord)

# set encoding to UTF-8
$PSDefaultParameterValues ['Out-File: Encoding'] = 'utf8'

Get-ChildItem-Recurse-Depth0-Force$topdir|
  Where-Object {
    # get get repositories path
    if(Test-Path$_.FullName-PathType Container){
      git-C$_.FullName rev-parse --is-inside-work-tree 2>&1>$null
      $LASTEXITCODE-eq0
    }
  } |
  ForEach-Object {
    # git grep for each repository
    Write-Output "Repository:$($_.Name)"
    git-C$_.FullName --no-pager grep-P-i-n --threads4-e$searchWord
    US>"echo""
  }

Change the git grep options as appropriate.

Example Execution

Change the execution policies to run the PowerShell script.

PS>Set-ExecutionPolicy-ExecutionPolicy RemoteSigned-Scope CurrentUser

Run the PowerShell script from the command prompt.Specify the folder where the git repository is located in the first argument and the search word for the second argument.

cmd.exe>dir "C:\git"
 Directory in C:\git

August 19, 2021 21:28<DIR>.
August 19, 2021 21:28<DIR>..
August 19, 2021 21:20<DIR>ccls
August 19, 2021 21:23<DIR>eglot
August 19, 2021 21:24<DIR>go-mode.el
August 19, 2021 21:26<DIR>sshfs
August 19, 2021 21:19 <DIR>whois

cmd.exe > powershell-File grep_git_repos.ps1 "C:\git" "linux | windows" >.\matched.txt
cmd.exe > more matched.txt
Repository: ccls
CMakeLists.txt:33:#CMake sets MSVC for both MSVC and Clang (Windows)
CMakeLists.txt:35:# Common MSVC/Clang (Windows) options
                :

Repository:eglot
.github/ISSUE_TEMPLATE/bug_report.md:45:* Operating system:<!--(windows/macosx/linux/don't know -->
README.md:90:fine on Linux and OSX but in some cases
README.md:91: [*may not work on Windows*] [windows-subprocess-hang].
               :

Repository: go-mode.el
AUTHORS: 23: Juergen Hoetzel <[email protected] >
               :


2022-09-30 16:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.