diff --git a/README.md b/README.md index 794a2a4..6c899e8 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,11 @@ # Informative git prompt for bash and fish -This prompt is a port of the "Informative git prompt for zsh" which you can find [here](https://github.com/olivierverdier/zsh-git-prompt) +This prompt is a port of the "Informative git prompt for zsh" which you can +find [here](https://github.com/olivierverdier/zsh-git-prompt) A ``bash`` prompt that displays information about the current git repository. -In particular the branch name, difference with remote branch, number of files staged, changed, etc. +In particular the branch name, difference with remote branch, number of files +staged, changed, etc. (an original idea from this [blog post][]). @@ -55,13 +57,19 @@ The symbols are as follows: ## Configuration 1. You can use ``GIT_PROMPT_START`` and ``GIT_PROMPT_END`` to tweak your prompt -2. If you want to tweak the colors, currently you have to tweak it in the - ``gitprompt.sh``. See the definitions of ``GIT_PROMPT_xxx1``, which include - various colors names. + +2. The default colors are defined within ``gitprompt.sh``, but may be + overridden by copying ``git-prompt-colors.sh`` to your home directory at + ``~/.git-prompt-colors.sh``. This file may also be found in the same + directory as ``gitprompt.sh``, but without the leading ``.``. + 3. The current git repo information is obtained by the script `gitstatus.sh` or `gitstatus.py`. Both scripts do the same thing, but the bash script is a - tad bit more quick, and is used by default. -4. You can define ``prompt_callback`` function to tweak your prompt dynamicaly + tad more quick, and is used by default. If you prefer the python script + (possibly because you have enhanced it), simply delete or change the name of + ``gitstatus.sh``. + +4. You can define ``prompt_callback`` function to tweak your prompt dynamically. ```sh function prompt_callback { @@ -71,6 +79,9 @@ function prompt_callback { } ``` +5. You can get help on the git prompt with the function ``git_prompt_help``. + Examples are available with ``git_prompt_examples``. + **Enjoy!** [blog post]: http://sebastiancelis.com/2009/nov/16/zsh-prompt-git-users/ diff --git a/git-prompt-colors.sh b/git-prompt-colors.sh new file mode 100644 index 0000000..686804c --- /dev/null +++ b/git-prompt-colors.sh @@ -0,0 +1,13 @@ + # These are the color definitions used by gitprompt.sh + GIT_PROMPT_PREFIX="[" # start of the git info string + GIT_PROMPT_SUFFIX="]" # the end of the git info string + GIT_PROMPT_SEPARATOR="|" # separates each item + + GIT_PROMPT_BRANCH="${Magenta}" # the git branch that is active in the current directory + GIT_PROMPT_STAGED="${Red}●" # the number of staged files/directories + GIT_PROMPT_CONFLICTS="${Red}✖" # the number of files in conflict + GIT_PROMPT_CHANGED="${Blue}✚" # the number of changed files + GIT_PROMPT_REMOTE=" " # the remote branch name (if any) + GIT_PROMPT_UNTRACKED="${Cyan}…" # the number of untracked files/dirs + GIT_PROMPT_STASHED="${BoldBlue}⚑" # the number of stashed files/dir + GIT_PROMPT_CLEAN="${BoldGreen}✔" # a colored flag indicating a "clean" repo diff --git a/git-prompt-help.sh b/git-prompt-help.sh new file mode 100644 index 0000000..b9c699b --- /dev/null +++ b/git-prompt-help.sh @@ -0,0 +1,56 @@ +#!/bin/bash +# git-prompt-help -- show useful info to help new users with the information +# being displayed. + +git_prompt_help() { + cat <&2 +The git prompt format is [|] + +BRANCH is a branch name, such as "master" or "stage", a tag name, or commit +hash prefixed with ':'. + +TRACKING indicates how the local branch differs from the +remote branch. It can be empty, or one of: + + ↑N - ahead of remote by N commits + ↓N - behind remote by N commits + ↓M↑N - branches diverged, other by M commits, yours by N commits + +LOCALSTATUS is one of the following: + + ✔ - repository clean + ●N - N staged files + ✖N - N unmerged files + ✚N - N changed but *unstaged* files + …N - N untracked files + ⚑N - N stash entries + +See "git_prompt_examples" for examples. +EOF +} +help_git_prompt() { git_prompt_help ; } + +git_prompt_examples() { + cat <&2 +These are examples of the git prompt: + + (master↑3|✚1) - on branch "master", ahead of remote by 3 commits, 1 + file changed but not staged + + (status|●2) - on branch "status", 2 files staged + + (master|✚7…) - on branch "master", 7 files changed, some files untracked + + (master|✖2✚3) - on branch "master", 2 conflicts, 3 files changed + + (master|⚑2) - on branch "master", 2 stash entries + + (experimental↓2↑3|✔) - on branch "experimental"; your branch has diverged + by 3 commits, remote by 2 commits; the repository is + otherwise clean + + (:70c2952|✔) - not on any branch; parent commit has hash "70c2952"; the + repository is otherwise clean +EOF +} + diff --git a/gitprompt.sh b/gitprompt.sh index de6fa98..fc21851 100755 --- a/gitprompt.sh +++ b/gitprompt.sh @@ -44,18 +44,40 @@ function git_prompt_config() local Blue="\[\033[0;34m\]" local Cyan="\[\033[0;36m\]" - # Default values for the appearance of the prompt. Configure at will. - GIT_PROMPT_PREFIX="[" - GIT_PROMPT_SUFFIX="]" - GIT_PROMPT_SEPARATOR="|" - GIT_PROMPT_BRANCH="${Magenta}" - GIT_PROMPT_STAGED="${Red}● " - GIT_PROMPT_CONFLICTS="${Red}✖ " - GIT_PROMPT_CHANGED="${Blue}✚ " - GIT_PROMPT_REMOTE=" " - GIT_PROMPT_UNTRACKED="${Cyan}…" - GIT_PROMPT_STASHED="${BoldBlue}⚑ " - GIT_PROMPT_CLEAN="${BoldGreen}✔" + # source the user's ~/.git-prompt-colors.sh file, or the one that should be + # sitting in the same directory as this script + + if [[ -z "$__GIT_PROMPT_COLORS_FILE" ]]; then + local pfx file dir + for dir in "$HOME" "$__GIT_PROMPT_DIR" ; do + for pfx in '.' '' ; do + file="$dir/${pfx}git-prompt-colors.sh" + if [[ -f "$file" ]]; then + __GIT_PROMPT_COLORS_FILE="$file" + break 2 + fi + done + done + fi + # if the envar is defined, source the file for custom colors + if [[ -n "$__GIT_PROMPT_COLORS_FILE" && -f "$__GIT_PROMPT_COLORS_FILE" ]]; then + source "$__GIT_PROMPT_COLORS_FILE" + else + # Default values for the appearance of the prompt. Do not change these + # below. Instead, copy these to `~/.git-prompt-colors.sh` and change them + # there. + GIT_PROMPT_PREFIX="[" + GIT_PROMPT_SUFFIX="]" + GIT_PROMPT_SEPARATOR="|" + GIT_PROMPT_BRANCH="${Magenta}" + GIT_PROMPT_STAGED="${Red}●" + GIT_PROMPT_CONFLICTS="${Red}✖" + GIT_PROMPT_CHANGED="${Blue}✚" + GIT_PROMPT_REMOTE=" " + GIT_PROMPT_UNTRACKED="${Cyan}…" + GIT_PROMPT_STASHED="${BoldBlue}⚑" + GIT_PROMPT_CLEAN="${BoldGreen}✔" + fi # Various variables you might want for your PS1 prompt instead local Time12a="\$(date +%H:%M)" @@ -91,8 +113,6 @@ function git_prompt_config() break fi done - # The old way - #__GIT_STATUS_CMD="${__GIT_PROMPT_DIR}/gitstatus.py" fi } diff --git a/gitstatus.sh b/gitstatus.sh index 4b199ef..1feeb8a 100755 --- a/gitstatus.sh +++ b/gitstatus.sh @@ -1,4 +1,4 @@ -#/bin/bash +#!/bin/bash # -*- coding: UTF-8 -*- # gitstatus.sh -- produce the current git repo status on STDOUT # Functionally equivalent to 'gitstatus.py', but written in bash (not python).