Notes & TILs
Search…
View a python function's history over-time with Git & FZF
Posted on 12 Dec, 2021
I am kinda hooked up on modifying my git terminal workflow and integrate FZF wherever I can, this is such an example script to see changes in a python function overtime in your git history. And yes, its interesting (& stupid)
1
#!/usr/bin/env bash
2
​
3
# FZF Wrapper over git to interactively search code changes inside functions
4
​
5
readarray -t choices < <(git ls-files | fzf \
6
--prompt="Choose File: " \
7
--height 40% --reverse \
8
)
9
​
10
printf "%s\n" "$(grep -o -P '(?<=def ).*?(?=\()' $choices)" | fzf \
11
--ansi --preview "echo {} | xargs -I{} git log --color -L :{}:$choices" \
12
--prompt="Choose function/method: " \
13
--bind 'j:down,k:up,ctrl-j:preview-down,ctrl-k:preview-up,ctrl-space:toggle-preview' --preview-window right:60% \
Copied!
The actual trick is finding all python functions and methods names in a given file using grep
1
$ grep -o -P '(?<=def ).*?(?=\()' rich/columns.py
2
__init__
3
add_renderable
4
__rich_console__
5
iter_renderables
Copied!
Next we plug the function name with filename in this git command
1
git log -L :funciton_name:relative_file_path
Copied!
Read more about git log searching​
Below is a screencast demoing the same for rich package
​​
​
​
If you are still interested, here is the script to hack around.
I wish this was scalable to all the programming languages but currently I don't have any clue on how to do this If you want to contribute help me by adding grep patterns to do the same search for you favourite programming language. Thanks
Last modified 1mo ago
Copy link