官术网_书友最值得收藏!

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) {
主站蜘蛛池模板: 宁化县| 上虞市| 金塔县| 林州市| 无棣县| 东乌珠穆沁旗| 瑞金市| 葵青区| 个旧市| 太康县| 邵武市| 逊克县| 娄烦县| 黄陵县| 新密市| 尉犁县| 金湖县| 英吉沙县| 津南区| 舟曲县| 武功县| 邵阳市| 荃湾区| 黎平县| 偃师市| 佛坪县| 桐庐县| 江北区| 万荣县| 思茅市| 扎鲁特旗| 康保县| 淮南市| 巫山县| 和田县| 怀宁县| 资溪县| 淅川县| 藁城市| 榆树市| 九龙城区|