1
1

More than 5 years have passed since last update.

git pullを対話的に実行する

Last updated at Posted at 2015-08-24

使い方

どういうことかというと、git pull -iを打てば、shellと対話しながらpullできるというシロモノです。
こんな感じ。

$ git pull -i
Please input remote (default: origin) upstream
Please input branch (default: master) master

# file diff
.gitignore                                            |      1 +
app/assets/javascripts/hoge.coffee                    |      7 +
app/controllers/hoge_controller.rb                    |      2 +-
3 files changed, 9 insertions(+), 1 deletions(-)

Are you sure you want to run? (y/N)

リモートリポジトリとローカルブランチを入力すると試しにdiffし、yesを入力すればpullを、noを入力すればpullがキャンセルされます。

やり方

githubから該当リポジトリをcloneします。
ファイル自体をコピペして置くだけでも大丈夫です。

git clone git@github.com:shibukk/git-extensions.git

.bashrc.zshrcに下記を記述します。
設置したshellのパスにするのと、shellの再起動を必ずしてください。

.zshrc
function git() {
    sh ~/git-extensions/pull_interactive.sh $@
}

あとはgit pullするときに-iをつけるだけで対話的にpullしてくれます。

説明など

gitコマンド全体をラップしてshellに渡します。

.zshrc
function git() {
    sh ~/git-extensions/pull_interactive.sh $@
}

getoptsだとpullの後にオプションが続く場合に取得できなかったので、$*をループして取得しています。

~/git-extensions/pull_interactive.sh
# -iオプションがあるかどうか確認する
i_option_flg=0
for opt in $*
do
  case $opt in
    '-i' )
      i_option_flg=1
      ;;
  esac
done

git diff --statを利用して、git pullを擬似的にdry-runします。

~/git-extensions/pull_interactive.sh
# dry-runをする
command git fetch $remote
command git diff --stat-width=500 $branch..$remote/$branch

yes/noの確認メッセージはこちらのサイトを参考にしました。分かりやすい解説ありがとうございました。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1