git grepコマンドでマッチした行の上下の行も表示する方法
git grepコマンドでマッチした行の上下の行も表示するには-A,-B,-Cオプションを指定します。
オプションの説明
-, -C , --context Show leading and trailing lines, and place a line containing -- between contiguous groups of matches. -A , --after-context Show trailing lines, and place a line containing -- between contiguous groups of matches. -B , --before-context Show leading lines, and place a line containing -- between contiguous groups of matches.
-Aオプションを指定した場合(下の行を表示。数値は行数。-nは行番号を表示するオプション)
$ git grep -A 2 -n time_t | head -n 20 Documentation/RelNotes/2.14.0.txt:141: * Some platforms have ulong that is smaller than time_t, and our Documentation/RelNotes/2.14.0.txt-142- historical use of ulong for timestamp would mean they cannot Documentation/RelNotes/2.14.0.txt-143- represent some timestamp that the platform allows. Invent a -- Documentation/git-grep.txt:308:`git grep 'time_t' -- '*.[ch]'`:: Documentation/git-grep.txt:309: Looks for `time_t` in all tracked .c and .h files in the working Documentation/git-grep.txt-310- directory and its subdirectories. Documentation/git-grep.txt-311- -- archive-zip.c:599: time_t time; archive-zip.c-600- struct tm tm; archive-zip.c-601- -- archive-zip.c:605: time = (time_t)*timestamp; archive-zip.c-606- localtime_r(&time, &tm); archive-zip.c-607- *timestamp = time; -- archive.c:438: time_t archive_time; archive.c-439- struct tree *tree; archive.c-440- const struct commit *commit;
-Bオプションを指定した場合(上の行を表示)
$ git grep -B 2 -n time_t | head -n 20 Documentation/RelNotes/2.14.0.txt-139- AsciiDoc and AsciiDoctor. Documentation/RelNotes/2.14.0.txt-140- Documentation/RelNotes/2.14.0.txt:141: * Some platforms have ulong that is smaller than time_t, and our -- Documentation/git-grep.txt-306--------- Documentation/git-grep.txt-307- Documentation/git-grep.txt:308:`git grep 'time_t' -- '*.[ch]'`:: Documentation/git-grep.txt:309: Looks for `time_t` in all tracked .c and .h files in the working -- archive-zip.c-597-static void dos_time(timestamp_t *timestamp, int *dos_date, int *dos_time) archive-zip.c-598-{ archive-zip.c:599: time_t time; -- archive-zip.c-603- die(_("timestamp too large for this system: %"PRItime), archive-zip.c-604- *timestamp); archive-zip.c:605: time = (time_t)*timestamp; -- archive.c-436- const char *name = argv[0]; archive.c-437- const struct object_id *commit_oid; archive.c:438: time_t archive_time;
-Cオプションを指定した場合(上下の行を表示)
$ git grep -C 2 -n time_t | head -n 20 Documentation/RelNotes/2.14.0.txt-139- AsciiDoc and AsciiDoctor. Documentation/RelNotes/2.14.0.txt-140- Documentation/RelNotes/2.14.0.txt:141: * Some platforms have ulong that is smaller than time_t, and our Documentation/RelNotes/2.14.0.txt-142- historical use of ulong for timestamp would mean they cannot Documentation/RelNotes/2.14.0.txt-143- represent some timestamp that the platform allows. Invent a -- Documentation/git-grep.txt-306--------- Documentation/git-grep.txt-307- Documentation/git-grep.txt:308:`git grep 'time_t' -- '*.[ch]'`:: Documentation/git-grep.txt:309: Looks for `time_t` in all tracked .c and .h files in the working Documentation/git-grep.txt-310- directory and its subdirectories. Documentation/git-grep.txt-311- -- archive-zip.c-597-static void dos_time(timestamp_t *timestamp, int *dos_date, int *dos_time) archive-zip.c-598-{ archive-zip.c:599: time_t time; archive-zip.c-600- struct tm tm; archive-zip.c-601- -- archive-zip.c-603- die(_("timestamp too large for this system: %"PRItime),
オプションを指定しない場合
$ git grep -n time_t | head -n 20 Documentation/RelNotes/2.14.0.txt:141: * Some platforms have ulong that is smaller than time_t, and our Documentation/git-grep.txt:308:`git grep 'time_t' -- '*.[ch]'`:: Documentation/git-grep.txt:309: Looks for `time_t` in all tracked .c and .h files in the working archive-zip.c:599: time_t time; archive-zip.c:605: time = (time_t)*timestamp; archive.c:438: time_t archive_time; blame.c:187: time_t now; builtin/bugreport.c:127: time_t now = time(NULL); builtin/pack-objects.c:1164: time_t last_mtime = 0; commit-graph.c:2187: time_t now = time(NULL); commit-reach.c:722: time_t min_commit_date, commit-reach.c:821: time_t min_commit_date = cutoff_by_min_date ? from->item->date : 0; commit-reach.h:89: time_t min_commit_date, compat/mingw.c:737:static inline long long filetime_to_hnsec(const FILETIME *ft) compat/mingw.c:744:static inline void filetime_to_timespec(const FILETIME *ft, struct timespec *ts) compat/mingw.c:746: long long hnsec = filetime_to_hnsec(ft); compat/mingw.c:747: ts->tv_sec = (time_t)(hnsec / 10000000); compat/mingw.c:807: filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim)); compat/mingw.c:808: filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim)); compat/mingw.c:809: filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim));