back
▲ 1 points

Ask HN: Share your favorite one-liner/bash function

by auraham·3y ago·0 comments·view on hn ↗
I just came up with this bash function for inspecting a file using `git diff C1..C2 -- filename`, where C1 and C2 are two consecutive commits.

    ,git_inspect() {
        if [ "$#" -ne 1 ] ; then
            echo "Usage:   $FUNCNAME filename"
            echo "Example: $FUNCNAME main.py"
        else
            git log --oneline --reverse --no-decorate -- "$1" | awk '{print $1}' | xargs | ruby -ne '$_.split.each_cons(2) {|pair| puts pair.join(" ") }' | fzf --no-sort --tac --preview-window=top,90% --preview="git difftool --extcmd icdiff -y {+1}..{+2} -- $1"
        fi
    }
In other words, that command do the following tasks:

- Create a list of commits IDs using `git log` and `awk`, example:

    commit_a
    commit_b
    commit_c
    commit_d
    commit_e

- Flatten the list into a single line using `xargs`:

    commit_a commit_b commit_c commit_d commit_e  
- Create pairs of commit IDs using `ruby`:

    commit_a commit_b
    commit_b commit_c
    commit_c commit_d
    commit_d commit_e

- Use `fzf` to select a pair of commits and run `git difftool commit..commit -- filename`.
// no comments yet