7

Consider a Python configuration file with the following line:

THEME = 'gum'

The THEME key appears only once, so when I want to know what the THEME is, I grep the file:

grep THEME pelicanconf.py

The file is kept in git, and I would like to grep for THEME in all previous git commits, in order to tell when was this line changed.

Is there an elegant way to grep the entire history of a git file?

Adam Matan
  • 128,757
  • 147
  • 397
  • 562

2 Answers2

4

git log -S'THEME' -- pelicanconf.py | xargs -n 1 git show shows the content of every commit that changed THEME

However, it prints out full commits, not only changes.

var="THEME"; git log -S"$var" -p -- pelicanconf.py | egrep "$var|commit|Date:"

would show you all variants of THEME with commit hashes and dates.

Thx @knittl for -p option.

Also, found a solution with gitk: see here.

Community
  • 1
  • 1
John_West
  • 2,239
  • 4
  • 24
  • 44
2

The way to do what you want is to use the bisect run <script>.
Use the bisect as you did so far and use the bisect with the script option.

The bisect search all your commits (in the given range) and with the suitable exit code you can tell if the code is good or bad.

The script will return the appropriate code for skipping (or 125 for not testable - much more suitable in your case).

Note that the script (my_script in the above example) should exit with code 0 if the current source code is good, and exit with a code between 1 and 127 (inclusive), except 125, if the current source code is bad.

Any other exit code will abort the bisect process. It should be noted that a program that terminates via "exit(-1)" leaves $? = 255, (see the exit(3) manual page), as the value is chopped with "& 0377".

The special exit code 125 should be used when the current source code cannot be tested. If the script exits with this code, the current revision will be skipped (see git bisect skip above). 125 was chosen as the highest sensible value to use for this purpose, because 126 and 127 are used by POSIX shells to signal specific error status (127 is for command not found, 126 is for command found but not executable---these details do not matter, as they are normal errors in the script, as far as "bisect run" is concerned).

Sample project for bisect


Using git log

You can pass -p flag to the log to follow a specific file

git log -p filename
Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167