🐟

fish + fzf + difit で差分を簡単に確認する

に公開

https://zenn.dev/whatasoda/articles/6e7b921bfbc968

コードレビューが捗りそうなので、fish shell 用の実装を考えてみました。

単一コミットの差分を確認する

ワンライナー:

difit (git log --oneline --decorate | fzf --prompt 'COMMIT >' | awk '{print $1}')

alias として保存する場合:

alias fzf_difit "difit (git log --oneline --decorate | fzf --prompt 'COMMIT >' | awk '{print \$1}')"

fzfの選択をキャンセルしたい場合:

function fzf_difit
  set commit (git log --oneline --decorate | fzf --prompt 'COMMIT >')
  if test $status -eq 0 -a -n "$commit"
    set commit_hash (echo $commit | awk '{print $1}')
    difit $commit_hash
  end
end

1日に何度も利用する場合にはキーバインドを設定するのが良いかもしれない。ctr-c/ctr-fに設定した場合:

~/.config/fish/config.fish
function fish_user_key_bindings
  bind \cf fzf_difit
end

from~toの範囲を確認する

~/.config/fish/functions/__fzf_difit.fish
function __fzf_difit -d "Select FROM and TO git commits with fzf and display diff using difit"
  set -l from_commit (git log --oneline --decorate -100 --color=always | \
    fzf \
      --ansi \
      --header "> difit \$TO \$FROM~1" \
      --prompt "Select \$FROM>" \
      --preview 'git log --oneline --decorate --color=always -1 {1}' \
      --preview-window=top:3:wrap
  )
  or return

  set -l from_hash (string split ' ' $from_commit)[1]

  set -l to_commit (git log --oneline --decorate -100 --color=always "$from_hash~1.." | \
    fzf \
      --ansi \
      --header "> difit \$TO $from_hash~1" \
      --prompt "Select \$TO>" \
      --preview 'git log --oneline --decorate --color=always -1 {1}' \
      --preview-window=top:3:wrap
  )
  or return

  set -l to_hash (string split ' ' $to_commit)[1]

  difit "$to_hash" "$from_hash~1"
end

Discussion