DEV Community

Bhupesh Varshney ๐Ÿ‘พ
Bhupesh Varshney ๐Ÿ‘พ

Posted on • Edited on • Originally published at bhupesh.gitbook.io

View a python function's history over-time with Git & FZF

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)

#!/usr/bin/env bash

# FZF Wrapper over git to interactively search code changes inside functions

readarray -t choices < <(git ls-files | fzf \
  --prompt="Choose File: " \
  --height 40% --reverse \
)

printf "%s\n" "$(grep -o -P '(?<=def ).*?(?=\()' $choices)" | fzf \
--ansi --preview "echo {} | xargs -I{} git log --color -L :{}:$choices" \
--prompt="Choose function/method: " \
--bind 'j:down,k:up,ctrl-j:preview-down,ctrl-k:preview-up,ctrl-space:toggle-preview' --preview-window right:60% \

Enter fullscreen mode Exit fullscreen mode

The actual trick is finding all python functions and methods names in a given file using grep

$ grep -o -P '(?<=def ).*?(?=\()' rich/columns.py
__init__
add_renderable
__rich_console__
iter_renderables
Enter fullscreen mode Exit fullscreen mode

Next we plug the function name with filename in this git command

git log -L :funciton_name:relative_file_path
Enter fullscreen mode Exit fullscreen mode

Read more about git log searching

Below is a screencast demoing the same for rich package

git-search-demo-gif

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

Top comments (0)