- Git Version Control Cookbook
- Aske Olsson Rasmus Voss
- 604字
- 2021-12-08 12:33:49
Searching through history code
Sometimes it is not enough; by just looking through the commit messages in the history, you may want to know which commits touched a specific method or variable. This is also possible using git log
. You can perform a search for a string, for example, a variable or method, and git log
will give you the commits, adding or deleting the string from the history. In this way, you can easily get the full commit context for the piece of code.
Getting ready
Again, we will use the JGit repository with the master branch pointing to b14a939
:
$ git checkout master && git reset --hard b14a939
How to do it...
We would like to find all the commits that have changes made to lines that contain the method "isOutdated"
. Again, we will just display the commits on one line each then we can check them individually later:
$ git log -G"isOutdated" --oneline f32b861 JGit 3.0: move internal classes into an internal subpackage c9e4a78 Add isOutdated method to DirCache 797ebba Add support for getting the system wide configuration ad5238d Move FileRepository to storage.file.FileRepository 4c14b76 Make lib.Repository abstract and lib.FileRepository its implementation c9c57d3 Rename Repository 'config' as 'repoConfig' 5c780b3 Fix unit tests using MockSystemReader with user configuration cc905e7 Make Repository.getConfig aware of changed config
Eight commits have patches that involve the string "isOutdated"
.
How it works...
Git traverses the history, the DAG, looking at each commit for the string "isOutdated"
in the patch between the parent commit and the current commit. This method is quite convenient to find out when a given string was introduced or deleted and to get the full context and commit at that point in time.
There's more...
The -G
option used with git log
will look for differences in the patches that contain added or deleted lines that match the given string. However, these lines could also have been added or removed due to some other refactoring/renaming of a variable or method. There is another option that can be used with git log
, -S
, which will look through the difference in the patch text similar to the -G
option, but only match commits where there is a change in the number of occurrences of the specified string, that is, a line added or removed, but not added and removed.
Let's see the output of the -S
option:
$ git log -S"isOutdated" --oneline f32b861 JGit 3.0: move internal classes into an internal subpackage c9e4a78 Add isOutdated method to DirCache 797ebba Add support for getting the system wide configuration ad5238d Move FileRepository to storage.file.FileRepository 4c14b76 Make lib.Repository abstract and lib.FileRepository its implementation 5c780b3 Fix unit tests using MockSystemReader with user configuation cc905e7 Make Repository.getConfig aware of changed config
The search matches seven commits, whereas the search with the –G
option matches eight commits. The difference is the commit with the ID c9c57d3
is only found with the –G
option in the first list. A closer look at this commit shows that the isOutdated
string is only touched due to renaming of another object, and this is why it is filtered away from the list of matching commits in the last list when using the –S
option. We can see the content of the commit with the git show
command, and use grep -C4
to limit the output to just the four lines before and after the search string:
$ git show c9c57d3 | grep -C4 "isOutdated" @@ -417,14 +417,14 @@ public FileBasedConfig getConfig() { throw new RuntimeException(e); } } - if (config.isOutdated()) { + if (repoConfig.isOutdated()) { try { - loadConfig(); + loadRepoConfig(); } catch (IOException e) {
- DB2 V9權威指南
- Django Design Patterns and Best Practices
- Processing互動編程藝術
- Python編程實戰
- BIM概論及Revit精講
- Corona SDK Mobile Game Development:Beginner's Guide(Second Edition)
- 青少年信息學競賽
- Python Data Structures and Algorithms
- 0 bug:C/C++商用工程之道
- Python程序設計與算法基礎教程(第2版)(微課版)
- ASP.NET Web API Security Essentials
- Hands-On Robotics Programming with C++
- Python一行流:像專家一樣寫代碼
- Joomla!Search Engine Optimization
- Java程序設計教程