hack のためのネタ帳, etc,,,

公式ページ等

  • GitHub / git / git / blob / master / contrib / completion / git-prompt.sh

使い方

基本的には、上記 git 公式リポジトリにある git-prompt.sh の冒頭部分のコメントに書いてある。

配置

git-prompt.sh の公式なコメントとしては ~/.git-prompt.sh にコピーして source ~/.git-prompt.sh せよとある。
Ubuntu の場合は、git パッケージに含まれてて、bash_completion を入れてると自動的に読み込まれているので不要。
Cygwin の場合は、少なくとも 2018-03-27 現在においては、git-prompt.sh を含んでいるパッケージが存在しないので、上記 git の公式リポジトリから拾ってくるしかない。

Cygwin の場合はとりあえず bash-completion を入れた上で以下のようにして /etc/bash_completion.d に放り込んどけばよいだろう。
( mkdir -p /etc/bash_completion.d; cd /etc/bash_completion.d; wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh )

設定

うちの bash の設定は、shell - prompt#bash-current

bash の場合 PS1 に以下のように __git_ps1 を埋めておけば良い。
PS1='... $(__git_ps1 "\n(%s)") ...'
書式を与えない場合は " (%s)" がデフォルトの書式

GIT_PS1_SHOWCOLORHINTS によるカラー化を有効にするには以下のようにする。
function gen_ps1 ()
{
  __git_ps1 'PRE' 'POST' 'FORMAT'
}
PROMPT_COMMAND='gen_ps1'
上記で、もし __git_ps1 に与えるパラメータが
__git_ps1 '\u@\h: \w' '\n$ ' '\n(%s)'
であれば PROMPT は以下のようになる
user@host: ~/git
(master)
$ 

あとは後述のカスタマイズに関する設定を以下のような感じでしておく
GIT_PS1_DESCRIBE_STYLE=default
GIT_PS1_SHOWCOLORHINTS=1
GIT_PS1_SHOWDIRTYSTATE=1
GIT_PS1_SHOWSTASHSTATE=1
GIT_PS1_SHOWUNTRACKEDFILES=1
GIT_PS1_SHOWUPSTREAM=auto
# GIT_PS1_DESCRIBE_STYLE
# GIT_PS1_HIDE_IF_PWD_IGNORED

表示

ちょっと不正確かもしれないけど多分以下のような感じ
表示状態条件
REBASE-irebase 中 merge 待ち? (interactive)test -f .git/rebase-merge/interactive
REBASE-mrebase 中 merge 待ち? (non interactive)test -d .git/rebase-merge/
REBASErebase 中 apply 待ち?test -f .git/rebase-apply/rebasing
AMam 中 apply 待ち?test -f .git/rebase-apply/applying
AM/REBASEam/rebase 中 apply 待ち?test -d .git/rebase-apply/
MERGINGmerge 中test -f .git/MERGE_HEAD
CHERRY-PICKINGcherry-pick 中test -f .git/CHERRY_PICK_HEAD
REVERTINGrevert 中test -f .git/REVERT_HEAD
BISECTINGbicect 中test -f .git/BISECT_LOG

カスタマイズ

以下の環境変数を設定すると、動作をカスタマイズ出来る。
対応する git config variable が存在する場合は、git config によりリポジトリ毎に設定を上書き出来る。

GIT_PS1_SHOWDIRTYSTATE

dirty state (tracked file の更新の有無と staged の状況) の表示

git config variable: bash.showDirtyState
available value: nonempty な任意の文字列
表示状態
*unstaged
+staged
#current branch does not have any commits yet (?)

GIT_PS1_SHOWSTASHSTATE

stash state 表示

git config variable: ?
available value: nonempty な任意の文字列
表示状態
$something is stashed

GIT_PS1_SHOWUNTRACKEDFILES

untracked files の有無を表示する

git config variable: ?
available value: nonempty な任意の文字列
表示状態
%untracked files が存在

GIT_PS1_SHOWUPSTREAM

HEAD と upstream を比較する

git config variable: bash.showUpstream
available value: スペースで区切った以下の値
value意味
autoauto
verboseshow number of commits ahead/behind (+/-) upstream
nameif verbose, then also show the upstream abbrev name
legacydon't use the '--count' option available in recent versions of git-rev-list
gitalways compare HEAD to @{upstream}
svnalways compare HEAD to your SVN upstream
表示状態
<you are ahead
>you are behind
<>you have diverged
=there is no difference

GIT_PS1_STATESEPARATOR

branch name 間の区切り文字を変更できる。

git config variable: ?
available value: 任意の文字列 (default: " ")

GIT_PS1_DESCRIBE_STYLE

detatched HEAD として checkout された commit の詳細情報を表示

git config variable: ?
available value: 以下の値
value意味
containsrelative to newer annotated tag (v1.6.3.2~35)
branchrelative to newer tag or branch (master~4)
describerelative to older annotated tag (v1.6.3.1-13-gdd42c2f)
tagrelative to any older tag (v1.6.3.1-13-gdd42c2f)
defaultexactly matching tag

GIT_PS1_SHOWCOLORHINTS

dirty state をカラー表示する

git config variable: ?
available value: nonempty な任意の文字列

GIT_PS1_HIDE_IF_PWD_IGNORED

current directory が .gitignore に設定されていた場合、表示を抑制する

git config variable: bash.hideIfPwdIgnored
available value: nonempty な任意の文字列

コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

Wiki内検索

フリーエリア

管理人/副管理人のみ編集できます