Skip to content

Instantly share code, notes, and snippets.

@yonchu
Created October 23, 2012 01:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yonchu/3936073 to your computer and use it in GitHub Desktop.
Save yonchu/3936073 to your computer and use it in GitHub Desktop.
Gitリポジトリの色んな情報を表示. どんなリポジトリなのかを瞬時に把握するのに便利
# 実行イメージ
$ gits
===== Status (git status -sb) =====
## master...origin/master [ahead 15]
M bin/gits
===== Submodule Status (git submodule status) =====
184ce5516f2dc9bb440ca4bdcbd71a1176c9edb7 .tmux/tmux-powerline (heads/master)
d02fff8809db1ba9ee8634c0bcfed3cbb577fa51 .tmux/tmux-powerline-wrapper (heads/master)
9a584520b5cf4be0601a1b4016e14e40c409381f .vim/neobundle.vim (ver.2.0-203-g9a58452)
9c263266bc312685a9bcc554bc1c00f0c8e84b16 .zsh/cdd (heads/master)
0fe095d60c7d12115d3dc7ee53d0e3d49acba04c .zsh/dircolors-solarized (heads/master)
144967fe6900f1e40e13ac307de3c66f6287aa44 .zsh/functions/Completion/zsh-completions (0.3.0-95-g144967f)
7e7a3901daad41b6eead0865d7363c922f6ca8ad .zsh/git-edit (heads/master)
dfd4308ae84da481f774b3f5e732ee277d9d8769 .zsh/z (v1.4-2-gdfd4308)
45194671af8e1d6b37b16e214a58674762ab8e49 .zsh/zsh-syntax-highlighting (0.1.2-2-g4519467)
a706839a881a630939b72062b5ece0bf569d53d3 .zsh/zsh-vcs-prompt (heads/master)
===== Remote Update Info =====
(fast-forwardable) [.](master:master -> master) URL: /Users/yonchu/Dropbox/Repos/git/dotfiles.git
- (local out of date) [.tmux/tmux-powerline](master:master -> master) URL: https://github.com/erikw/tmux-powerline.git
- (local out of date) [.tmux/tmux-powerline-wrapper](master:master -> master) URL: https://github.com/yonchu/tmux-powerline-wrapper.git
- (up to date) [.vim/neobundle.vim](master:master -> master) URL: git://github.com/Shougo/neobundle.vim.git
- (up to date) [.zsh/cdd](master:master -> master) URL: git://github.com/m4i/cdd.git
- (up to date) [.zsh/dircolors-solarized](master:master -> master) URL: git://github.com/seebi/dircolors-solarized.git
- (up to date) [.zsh/functions/Completion/zsh-completions](master:master -> master) URL: https://github.com/zsh-users/zsh-completions.git
- (up to date) [.zsh/git-edit](master:master -> master) URL: https://github.com/yonchu/git-edit.git
- (up to date) [.zsh/z](master:master -> master) URL: git://github.com/rupa/z.git
- (up to date) [.zsh/zsh-syntax-highlighting](master:master -> master) URL: https://github.com/yonchu/zsh-syntax-highlighting.git
- (up to date) [.zsh/zsh-vcs-prompt](master:master -> master) URL: https://github.com/yonchu/zsh-vcs-prompt.git
===== Remote List (git remote -v) =====
github git@github.com:yonchu/dotfiles.git (fetch)
github git@github.com:yonchu/dotfiles.git (push)
origin /Users/yonchu/Dropbox/Repos/git/dotfiles.git (fetch)
origin /Users/yonchu/Dropbox/Repos/git/dotfiles.git (push)
===== Branch List (git branch -av) =====
* master 65d8813 [ahead 15] Update setup.sh
remotes/github/master 696affd Fix bugs about `git-check-remote-update` and `git-check-status`
remotes/origin/HEAD -> origin/master
remotes/origin/master 696affd Fix bugs about `git-check-remote-update` and `git-check-status`
===== Stash List (git stash list) =====
stash@{0}: WIP on master: a49010a Update .gitconfig, Add .gittemplate
#!/bin/bash
#
# Print detail infomation about current git repository.
#
if ! type git > /dev/null 2>&1; then
echo 'Error: Git is not installed' 2>&1
exit 1
fi
if [ "$(git rev-parse --is-inside-work-tree 2> /dev/null)" != "true" ]; then
echo 'Error: Not a git repository' 2>&1
exit 1
fi
## Common
get_branch () {
# HEAD branch
local head_branch=$(git symbolic-ref -q --short HEAD)
if [ -z "$head_branch" ]; then
local prehash=$(git rev-parse --short HEAD)
echo "Not on any branch : prehash=$prehash"
return 1
fi
echo "$head_branch"
}
get_remote_name () {
local head_branch=$1
# The remote name of HEAD branch (e.g. origin)
local remote_name=$(git config branch."${head_branch}".remote)
if [ -z "$remote_name" ]; then
echo 'No remote repository'
return 1
fi
echo "$remote_name"
}
cd_git_toplevel() {
cd "$1"
if [ "$(git rev-parse --is-inside-work-tree 2> /dev/null)" != "true" ]; then
return 1
fi
cd "$(git rev-parse --show-toplevel)" >/dev/null 2>&1
}
## Submodule
print_submodule_update_info() {
(
cd_git_toplevel "$1" || return 1
local -a submodule_dirs
IFS=$'\n' submodule_dirs=($(git submodule | cut -d ' ' -f 3 | sed 's/(.*)$//'))
if [ -z "$submodule_dirs" ]; then
return 0
fi
for sub_dir in "${submodule_dirs[@]}"; do
echo " - $(print_remote_update_info "$sub_dir")"
done
)
}
## Remote
print_remote_update_info () {
(
local dir=$1
cd_git_toplevel "$dir" || return 1
local status
local url
local branch
local merge_branch
local remote_branch
branch=$(get_branch)
if [ $? -eq 0 ]; then
remote_name=$(get_remote_name "$branch")
if [ $? -eq 0 ]; then
merge_branch=$(git config branch."$branch".merge | sed -E s/^.\{11\}//g)
if [ -z "$merge_branch" ]; then
status='No merge branch'
else
local remote_show=$(git remote show "$remote_name")
status=$(echo "$remote_show" \
| grep "^ *$merge_branch pushes" | sed 's/.*(\(.*\)).*/\1/')
url=$(echo "$remote_show" | grep '^ *Push' | sed 's/^.*URL: \(.*\)$/\1/')
remote_branch=$(echo "$remote_show" \
| grep "^ *$merge_branch pushes" | sed 's/.*pushes to \(.*\) (.*)/\1/')
fi
else
status=$remote_name
fi
else
status=$branch
branch=''
fi
if [ "$status" != "up to date" ]; then
status="\033[0;31m$status\033[0;39m"
fi
local branch_info=''
if [ -n "$branch" ]; then
branch_info=$branch
if [ -n "$merge_branch" ]; then
branch_info="$branch_info:$merge_branch -> $remote_branch"
fi
branch_info="($branch_info)"
fi
local update_info="($status) [${dir%/}]$branch_info${url:+ URL: $url}"
echo -e "$update_info"
)
}
print_update_info() {
local dir=$1
if [ ! -e "$dir" ]; then
echo -e "\033[0;31mDirectory not found!! : $dir\033[0;39m"
return 1
elif [ ! -d "$dir" ]; then
return 0
fi
print_remote_update_info "$dir" \
&& print_submodule_update_info "$dir"
}
## Main
cd_git_toplevel
echo -e "===== \033[0;32mStatus (git status -sb)\033[0;39m ====="
git status -sb
sub_status=$(git submodule status)
if [ -n "$sub_status" ]; then
echo -e "===== \033[0;33mSubmodule Status (git submodule status)\033[0;39m ====="
echo "$sub_status"
fi
echo -e "===== \033[0;32mRemote Update Info\033[0;39m ====="
print_update_info '.'
echo -e "===== \033[0;34mRemote List (git remote -v)\033[0;39m ====="
git remote -v
echo -e "===== \033[0;35mBranch List (git branch -av)\033[0;39m ====="
git branch -av
echo -e "===== \033[0;36mStash List (git stash list)\033[0;39m ====="
git stash list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment