9
9

More than 5 years have passed since last update.

Gitでpush漏れを防ぐ

Last updated at Posted at 2014-05-07

ターミナルでGitを使っていると、時々push漏れする時がある。なので、pushしてないのがわかるように、プロンプトに表示してしまおう。

Show Current Git Branch or Tag in Shell Prompt

上記の設定だとpush漏れしているかどうかわからないため、少々手を加える。
対象は bash

parse_git_branch () {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}

parse_git_tag () {
  git describe --tags 2> /dev/null
}

if_modified () {
  git status -uall 2> /dev/null | grep -E "(modified|Untracked)" > /dev/null 2>&1
}
if_pushed () {
  git log origin/$1..$1 2> /dev/null | grep -E "commit" > /dev/null 2>&1
}

parse_git_branch_or_tag() {
  local BRANCH="$(parse_git_branch)"
  if [ "$BRANCH" = "" ]; then
    return 0;
  fi

  local OUT="($BRANCH)"
  if [ "$OUT" == " ((no branch))" ]; then
    OUT="($(parse_git_tag))";
  fi

  if_modified
  if [ $? -eq 0 ] ; then
    local MOD_MARK="*"
    OUT=$OUT$MOD_MARK
  fi

  if_pushed $BRANCH
  if [ $? -eq 0 ] ; then
    local NON_PUSH_MARK="  NOT YET PUSHED!!!"
    OUT=$OUT$NON_PUSH_MARK
  fi
  echo $OUT
}

~/.bash/git-promptに保管。

source ~/.bash/git-prompt
PS1="[\W\[\033[32m\]\$(parse_git_branch_or_tag)\[\033[0m\]] "

Push漏れがあると、NOT YET PUSHED!!!とでる。

9
9
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
9
9