Initial commit

master
Martin Gondermann 13 years ago
commit 6f4c272200
  1. 52
      README.md
  2. 94
      gitprompt.sh
  3. 76
      gitstatus.py

@ -0,0 +1,52 @@
# Informative git prompt for bash
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.
(an original idea from this [blog post][]).
## Examples
The prompt may look like the following:
* ``(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
* ``(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
## Prompt Structure
By default, the general appearance of the prompt is::
(<branch> <branch tracking>|<local status>)
The symbols are as follows:
- Local Status Symbols
- ``✔``: repository clean
- ``●n``: there are ``n`` staged files
- ``✖n``: there are ``n`` unmerged files
- ``✚n``: there are ``n`` changed but *unstaged* files
- ``…n``: there are ``n`` untracked files
- Branch Tracking Symbols
- ``↑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
- Branch Symbol:<br />
When the branch name starts with a colon ``:``, it means it's actually a hash, not a branch (although it should be pretty clear, unless you name your branches like hashes :-)
## Install
1. Create the directory ``~/.bash`` if it does not exist (this location is customizable).
1. Move the file ``gitstatus.py`` into ``~/.bash/``.
1. Source the file ``gitprompt.sh`` from your ``~/.bashrc`` config file, and, configure your prompt in ``~/.bash/gitprompt.sh``. For this you have to set the variables PROMPT\_START and PROMPT\_END.
1. You may also redefine the function ``setGitPrompt`` to adapt it to your needs (to change the order in which the information is displayed).
1. Go in a git repository and test it!
**Enjoy!**
[blog post]: http://sebastiancelis.com/2009/nov/16/zsh-prompt-git-users/

@ -0,0 +1,94 @@
__GIT_PROMPT_DIR=~/.bash
# Colors
# Reset
ResetColor="\[\033[0m\]" # Text Reset
# Regular Colors
Red="\[\033[0;31m\]" # Red
Yellow="\[\033[0;33m\]" # Yellow
Blue="\[\033[0;34m\]" # Blue
# Bold
BGreen="\[\033[1;32m\]" # Green
# High Intensty
IBlack="\[\033[0;90m\]" # Black
# Bold High Intensty
Magenta="\[\033[1;95m\]" # Purple
# Various variables you might want for your PS1 prompt instead
Time12a="\@"
PathShort="\w"
# 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="…"
GIT_PROMPT_CLEAN="${BGreen}"
PROMPT_START="$IBlack$Time12a$ResetColor$Yellow$PathShort$ResetColor"
PROMPT_END=" % "
function update_current_git_vars() {
unset __CURRENT_GIT_STATUS
local gitstatus="${__GIT_PROMPT_DIR}/gitstatus.py"
_GIT_STATUS=$(python $gitstatus)
__CURRENT_GIT_STATUS=($_GIT_STATUS)
GIT_BRANCH=${__CURRENT_GIT_STATUS[0]}
GIT_REMOTE=${__CURRENT_GIT_STATUS[1]}
if [[ "." == "$GIT_REMOTE" ]]; then
unset GIT_REMOTE
fi
GIT_STAGED=${__CURRENT_GIT_STATUS[2]}
GIT_CONFLICTS=${__CURRENT_GIT_STATUS[3]}
GIT_CHANGED=${__CURRENT_GIT_STATUS[4]}
GIT_UNTRACKED=${__CURRENT_GIT_STATUS[5]}
GIT_CLEAN=${__CURRENT_GIT_STATUS[6]}
}
function setGitPrompt() {
update_current_git_vars
if [ -n "$__CURRENT_GIT_STATUS" ]; then
STATUS=" $GIT_PROMPT_PREFIX$GIT_PROMPT_BRANCH$GIT_BRANCH$ResetColor"
if [ -n "$GIT_REMOTE" ]; then
STATUS="$STATUS$GIT_PROMPT_REMOTE$GIT_REMOTE$ResetColor"
fi
STATUS="$STATUS$GIT_PROMPT_SEPARATOR"
if [ "$GIT_STAGED" -ne "0" ]; then
STATUS="$STATUS$GIT_PROMPT_STAGED$GIT_STAGED$ResetColor"
fi
if [ "$GIT_CONFLICTS" -ne "0" ]; then
STATUS="$STATUS$GIT_PROMPT_CONFLICTS$GIT_CONFLICTS$ResetColor"
fi
if [ "$GIT_CHANGED" -ne "0" ]; then
STATUS="$STATUS$GIT_PROMPT_CHANGED$GIT_CHANGED$ResetColor"
fi
if [ "$GIT_UNTRACKED" -ne "0" ]; then
STATUS="$STATUS$GIT_PROMPT_UNTRACKED$GIT_UNTRACKED$ResetColor"
fi
if [ "$GIT_CLEAN" -eq "1" ]; then
STATUS="$STATUS$GIT_PROMPT_CLEAN"
fi
STATUS="$STATUS$ResetColor$GIT_PROMPT_SUFFIX"
PS1="$PROMPT_START$STATUS$PROMPT_END"
else
PS1="$PROMPT_START$PROMPT_END"
fi
}
PROMPT_COMMAND=setGitPrompt

@ -0,0 +1,76 @@
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from __future__ import print_function
# change those symbols to whatever you prefer
symbols = {'ahead of': '↑·', 'behind': '↓·', 'prehash':':'}
from subprocess import Popen, PIPE
import sys
gitsym = Popen(['git', 'symbolic-ref', 'HEAD'], stdout=PIPE, stderr=PIPE)
branch, error = gitsym.communicate()
error_string = error.decode('utf-8')
if 'fatal: Not a git repository' in error_string:
sys.exit(0)
branch = branch.strip()[11:]
res, err = Popen(['git','diff','--name-status'], stdout=PIPE, stderr=PIPE).communicate()
err_string = err.decode('utf-8')
if 'fatal' in err_string:
sys.exit(0)
changed_files = [namestat[0] for namestat in res.splitlines()]
staged_files = [namestat[0] for namestat in Popen(['git','diff', '--staged','--name-status'], stdout=PIPE).communicate()[0].splitlines()]
nb_changed = len(changed_files) - changed_files.count('U')
nb_U = staged_files.count('U')
nb_staged = len(staged_files) - nb_U
staged = str(nb_staged)
conflicts = str(nb_U)
changed = str(nb_changed)
nb_untracked = len(Popen(['git','ls-files','--others','--exclude-standard'],stdout=PIPE).communicate()[0].splitlines())
untracked = str(nb_untracked)
if not nb_changed and not nb_staged and not nb_U and not nb_untracked:
clean = '1'
else:
clean = '0'
remote = ''
if not branch: # not on any branch
branch = symbols['prehash']+ Popen(['git','rev-parse','--short','HEAD'], stdout=PIPE).communicate()[0][:-1]
else:
remote_name = Popen(['git','config','branch.%s.remote' % branch], stdout=PIPE).communicate()[0].strip()
if remote_name:
merge_name = Popen(['git','config','branch.%s.merge' % branch], stdout=PIPE).communicate()[0].strip()
if remote_name == '.': # local
remote_ref = merge_name
else:
remote_ref = 'refs/remotes/%s/%s' % (remote_name, merge_name[11:])
revgit = Popen(['git', 'rev-list', '--left-right', '%s...HEAD' % remote_ref],stdout=PIPE, stderr=PIPE)
revlist = revgit.communicate()[0]
if revgit.poll(): # fallback to local
revlist = Popen(['git', 'rev-list', '--left-right', '%s...HEAD' % merge_name],stdout=PIPE, stderr=PIPE).communicate()[0]
behead = revlist.splitlines()
ahead = len([x for x in behead if x[0]=='>'])
behind = len(behead) - ahead
if behind:
remote += '%s%s' % (symbols['behind'], behind)
if ahead:
remote += '%s%s' % (symbols['ahead of'], ahead)
if remote == "":
remote = '.'
out = '\n'.join([
str(branch),
remote,
staged,
conflicts,
changed,
untracked,
clean])
print(out)
Loading…
Cancel
Save