
Setiap repositori git berisi banyak file, folder, cabang, tag, dll. Terkadang diperlukan pencarian konten tertentu di repositori git menggunakan pola ekspresi reguler. Perintah `git grep` digunakan untuk mencari di cabang checkout dan file lokal. Tetapi jika user mencari konten di satu cabang, tetapi konten disimpan di cabang lain dari repositori, maka dia tidak akan mendapatkan hasil pencarian. Dalam hal ini, user harus menjalankan perintah `git grep` untuk menerapkan pencarian di semua cabang repositori.
Parameter konfigurasi perintah grep:
Parameter perintah `git grep` digunakan untuk mengkonfigurasi perintah ini telah disebutkan di bawah ini.
Parameter Name | Purpose |
---|---|
grep.patternType | It is used to set the default matching behavior. |
grep.fullName | It is set to true for enabling the –full-name option by default. |
grep.column | It is set to true for enabling the –column option by default. |
grep.lineNumber | It is set to true for enabling -n option by default. |
grep.extendedRegexp | It is set to true for enabling the –extended-regexp option by default. But this option will not work if the grep. Pattern type contains another value in place of the default value. |
grep. threads | It is used to set the number of grep worker threads. |
grep.fallbackToNoIndex | If it is set to true, then the git grep –no-index when the git grep executed outside of a git repository. default value of this parameter is false. |
Opsi perintah grep:
Itu `git grep`perintah memiliki banyak opsi untuk mencari konten repositori dengan cara yang berbeda. Beberapa opsi grep yang umum digunakan telah dijelaskan di bawah ini.
Option | Purpose |
---|---|
-i, –ignore-case | It is used for case insensitive matches of the patterns and the files. |
-I | It is used to don’t match the pattern in binary files. |
–max-depth <depth> | It is used for each given on the command line. depth value of -1 indicates no limit. This option is ignored if contains active wildcards. |
-r, –recursive | It works like –max-depth=-1, and it is the default value. |
–no-recursive | It works like –max-depth=0. |
-w, –word-regexp | It is used to match the pattern only at the word boundary. |
-v, –invert-match | It is used to select non-matching lines. |
–full-name | It is used to force the paths to the output relative to the project top directory. |
-e | It is used for the patterns starting with – and should be used with the grep. |
–and, –or, –not, ( … ) | These options are used to define the multiple patterns for searching. –or is the default operator and –and has higher precedence than –or. |
-E, –extended-regexp, -G, –basic-regexp | It is used for POSIX extended/basic regexp patterns. |
-P, –perl-regexp | It is used for Perl-compatible regular expression patterns. |
-F, –fixed-strings | It is used for the fixed string patterns. |
-f <file> | It is used to read the patterns from the file. |
-n, –line-number | It is used to prefix the line number to matching lines. |
-o, –only-matching | It is used to print only the matched (non-empty) parts of a matching line. |
-c, –count | It is used to show the number of lines that match. |
–break | It is used to print an empty line between the matches from the different files. |
–help | It is used to display all available options with the description of the grep command. |
Aktifkan konfigurasi grep:
Sebelum menjalankan perintah `git grep` dari tutorial ini, jalankan perintah berikut untuk mengaktifkan –extended-regexp dan -n pilihan dari perintah grep.
$ git config --global grep.extendRegexp true
$ git config --global grep.lineNumber true
Penggunaan perintah grep untuk mencari:
Repositori lokal bernama book-storetelah digunakan dalam tutorial ini untuk memeriksa output dari perintah grep untuk mencari konten di repositori. Repositori berisi dua file. Ini adalahbooklist.php dan booktype.php.
Jalankan perintah berikut untuk mencari kata ‘Book Type’ dalam file repositori.
$ git grep 'Book type' $(git rev-list –all)
Output berikut menunjukkan bahwa kata ‘Book type’ ada di line 1 dari booktype.php mengajukan.
Jalankan perintah berikut untuk mencari baris file repositori dengan nilai SHA komit yang berisi: ‘boo’di awal file. Di Sini,the -i opsi telah digunakan untuk pencarian case-insensitive.
$ git grep -i 'boo*' $(git rev-list --all)
Output berikut menunjukkan bahwa ‘boo’ berisi dua file pada baris nomor 1, tetapi entri untuk booklist.php file telah muncul dua kali untuk dua komit.
Pola telah dicari di dalam konten file repositori di perintah sebelumnya. Jalankan perintah berikut untuk mencari konten file tertentu.
$ git grep -f 'booktype.php.'
Output berikut menunjukkan bahwa the booktype.php file ada di repositori saat ini, dan file berisi satu baris.
Jalankan perintah berikut untuk mencari pola, ‘Pesan’ di dalam konten file repositori. Di sini, opsi -e telah digunakan untuk pencocokan pola.
$ git grep -e 'Book'
Output berikut menunjukkan bahwa keduanya booklist.php dan booktype.php file berisi kata ‘Book’ pada baris nomor 1.
Jalankan perintah berikut untuk mencari beberapa pola di dalam konten file repositori. Di sini, opsi -E telah digunakan untuk pencocokan pola regex, dan pipa (|) berfungsi sebagai OR logis. File yang mengandung kata‘Book’ atau ‘author’ akan ditampilkan setelah menjalankan perintah berikut.
$ git grep -E 'Book*|author.'
Output berikut menunjukkan bahwa kata ‘author’ ada dua kali di authorinfo.php file, dan kata ‘Book’ ada satu kali di booklist.php dan booktype.php mengajukan.
Kesimpulan:
Itu `git grep`adalah perintah yang berguna untuk mencari konten tertentu di repositori git. Pencarian dapat dilakukan dengan cara yang berbeda dengan menggunakan opsi yang berbeda dari perintah ini. Penggunaan beberapa opsi telah dijelaskan dalam tutorial ini dengan menggunakan repositori demo.