google/zx を使って 現在のHEADと差分のあるファイルをvimで開く

2021年09月02日

vとコマンドをうつと、vimで現在のHEADとの差分があるファイルをvimで開くように設定している。

現在のHEADとの差分があるファイルを表示する gitdiffls というコマンドを作成し zshrcで v というエイリアスを定義して実現している。

alias v='(){vim $(gitdiffls $1)}'

google/zx で以下書いている

#!/usr/bin/env zx
$.verbose = false

if (argv.staged || argv.s) {
  // show diff staging files
  const result = await $`git diff --name-only --staged`
  console.log(result.stdout)
  process.exit()
}

let arg = ''
if (argv._[1]) {
  // show diff branch etc..
  arg = argv._[1]
} else if (argv.default || argv.d) {
  // show diff default remote branch
  const defaultRemoteBranch = await $`git remote show origin | grep 'HEAD branch' | awk '{print $NF}'`
  arg = `origin/${defaultRemoteBranch}`.trim()
} else {
  // show diff HEAD in default
  arg = 'HEAD'
}

const result = await $`cat <(git status -u --short | grep "^\?\?" | cut -d' ' -f 2) <(git diff --name-only ${arg})`
console.log(result.stdout)

ちなみに

v origin/dev

で、リモートのdevブランチの差分があるファイルをvimで開く

めちゃくちゃ便利。