diff options
author | Claudio Bandera <claudio.bandera@kit.edu> | 2015-09-17 18:40:24 +0200 |
---|---|---|
committer | Claudio Bandera <claudio.bandera@kit.edu> | 2015-09-17 18:40:24 +0200 |
commit | 92c2e869763dcc25c840c2e6cfcd9863abf201dd (patch) | |
tree | e032548195fccf98708daafa8f3e93c0781ede4e | |
parent | 2dbf0c220fc971c9493392a70c71954001fee7fd (diff) | |
parent | 1f6a8f84946a0cb6936916f47b5a3870fa12a5a6 (diff) | |
download | git-sonar-92c2e869763dcc25c840c2e6cfcd9863abf201dd.tar.gz git-sonar-92c2e869763dcc25c840c2e6cfcd9863abf201dd.zip |
Merge remote-tracking branch 'upstream/master' into hotfix/unittests
-rw-r--r-- | Makefile | 35 | ||||
-rw-r--r-- | README.md | 342 | ||||
-rwxr-xr-x | git-radar | 5 | ||||
-rwxr-xr-x | prompt.bash | 10 | ||||
-rwxr-xr-x | prompt.zsh | 11 | ||||
-rwxr-xr-x | radar-base.sh | 229 | ||||
-rwxr-xr-x | test | 2 | ||||
-rwxr-xr-x | test-branches.sh | 2 | ||||
-rwxr-xr-x | test-colors.sh | 492 | ||||
-rwxr-xr-x | test-commits.sh | 29 | ||||
-rwxr-xr-x | test-radar-base.sh | 28 |
11 files changed, 1050 insertions, 135 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5e73c9f --- /dev/null +++ b/Makefile @@ -0,0 +1,35 @@ +SOURCES=git-radar radar-base.sh prompt.zsh prompt.bash fetch.sh +PREFIX=$(HOME)/.local + +all: + @echo 'Simple Install script for *git-radar* ' + @echo 'For a normal installation for your user only use:' + @echo ' make install' + @echo '' + @echo 'If you want to install *git-radar* system wide you should change' + @echo 'the prefix' + @echo '' + @echo ' PREFIX=/usr/local/bin make install' + @echo '' + @echo 'For a development install (symlinking files) do:' + @echo '' + @echo ' make develop' + +.PHONY: install develop + +install: $(SOURCES) + @echo 'Installing in ' $(PREFIX)/bin + cp git-radar $(PREFIX)/bin + cp radar-base.sh $(PREFIX)/bin + cp prompt.zsh $(PREFIX)/bin + cp prompt.bash $(PREFIX)/bin + cp fetch.sh $(PREFIX)/bin + + +develop: $(SOURCES) + @echo 'Symlinking in ' $(PREFIX)/bin + ln -s $(PWD)/git-radar $(PREFIX)/bin/git-radar + ln -s $(PWD)/radar-base.sh $(PREFIX)/bin/radar-base.sh + ln -s $(PWD)/prompt.zsh $(PREFIX)/bin/prompt.zsh + ln -s $(PWD)/prompt.bash $(PREFIX)/bin/prompt.bash + ln -s $(PWD)/fetch.sh $(PREFIX)/bin/fetch.sh @@ -8,14 +8,44 @@ Git-radar is a tool you can add to your prompt to provide at-a-glance information on your git repo. It's a labour of love I've been dogfooding for the last few years. Maybe it can help you too. +**Table of Contents** + +- [Installation](#installation) +- [Usage](#usage) +- [Features](#features) + - [Files status](#files-status) + - [Local commits status](#local-commits-status) + - [Remote commits status](#remote-commits-status) + - [(Optional) Auto-fetch repos](#optional-auto-fetch-repos) +- [Support](#support) + - [Ensuring prompt execution](#ensuring-prompt-execution) + - [Configuring colours](#configuring-colours) + - [Exporting Environment Variables](#exporting-environment-variables) + - [Setting an RC file](#setting-an-rc-file) + - [Bash Colour Codes](#bash-colour-codes) + - [Zsh Colour Codes](#zsh-colour-codes) + - [Configuration values](#configuration-values) + - [Colouring the Branch part](#colouring-the-branch-part) + - [Colouring the local commits status](#colouring-the-local-commits-status) + - [Colouring the remote commits status](#colouring-the-remote-commits-status) + - [Colouring the file changes status](#colouring-the-file-changes-status) +- [License](#license) + ## Installation -Install from brew: +### Install from brew: ``` > brew install michaeldfallen/formula/git-radar ``` +### Manually: + +``` +> cd ~ && git clone https://github.com/michaeldfallen/git-radar .git-radar +> echo 'export PATH=$PATH:$HOME/.git-radar' >> ~/.bashrc +``` + Then run `git-radar` to see the docs and prove it's installed. ## Usage @@ -29,16 +59,17 @@ Add to your `.bashrc` ```bash export PS1="$PS1\$(git-radar --bash --fetch)" ``` -(note: the `\` escaping the `$` is important) +[(note: the `\` escaping the `$` is important)](#ensuring-prompt-execution) **Zsh** Add to your `.zshrc` ```zsh -export PROMPT="$PROMPT$(git-radar --zsh --fetch) " +export PROMPT="$PROMPT\$(git-radar --zsh --fetch) " ``` +[(note: the `\` escaping the `$` is important)](#ensuring-prompt-execution) -**fish** +**Fish** Add to your `config.fish` ```bash @@ -115,6 +146,20 @@ Prompt | Meaning ![git:(m 4 → my-branch)] | There are 4 commits on `origin/master` that aren't on `origin/my-branch` ![git:(m 1 ⇄ 2 my-branch)] | `origin/master` and `origin/my-branch` have diverged, we'll need to rebase or merge +If you don't rely on this status, you can always hide this part of the prompt by calling git-radar with `--no-remote-status`. + +**Bash** +```bash +export PS1="$PS1\$(git-radar --bash --fetch --no-remote-status) " +``` +[(note: the `\` escaping the `$` is important)](#ensuring-prompt-execution) + +**Zsh** +```zsh +export PROMPT="$PROMPT\$(git-radar --zsh --fetch --no-remote-status) " +``` +[(note: the `\` escaping the `$` is important)](#ensuring-prompt-execution) + ### (Optional) Auto-fetch repos Ensuring your refs are up to date I found can be a pain. To streamline this @@ -130,12 +175,297 @@ To use this feature, when setting your prompt, call git-radar with `--fetch`: ```bash export PS1="$PS1\$(git-radar --bash --fetch)" ``` -(note: the `\` escaping the `$` is important) +[(note: the `\` escaping the `$` is important)](#ensuring-prompt-execution) **Zsh** ```zsh -export PROMPT="$PROMPT$(git-radar --zsh --fetch) " +export PROMPT="$PROMPT\$(git-radar --zsh --fetch) " +``` +[(note: the `\` escaping the `$` is important)](#ensuring-prompt-execution) + +## Support + +### Ensuring prompt execution + +When setting your prompt variable, `PROMPT` in Zsh and `PS1` in Bash, it's +important that the function executes each time the prompt renders. That way the +prompt will respond to changes in your git repo. To ensure this you will need +to escape the execution of the function. There are two ways to do this: + +**1. Use `$'` to render raw characters** +```bash +export PROMPT=$'$(git-radar --zsh)' +export PS1=$'$(git-radar --bash)' +``` + +**2. Use `\` to escape execution of the subshell** +```bash +export PROMPT="\$(git-radar --zsh)" +export PS1="\$(git-radar --bash)" +``` + +### Configuring colours + +You can configure the colour scheme in two ways: export +[Environment Variables](#exporting-environment-variables) +or use an [rc file](#setting-an-rc-file). + +#### Exporting Environment Variables + +To configure the prompt this way just add to your `~/.bashrc` or `~/.zshrc` an +export directive with the value you want to change. + +**Example: Change the branch colour in Zsh** + +In `~/.zshrc`: +```zsh +export GIT_RADAR_COLOR_BRANCH='$fg[yellow]' +``` + +**Example: Change the branch colour in Bash** + +In `~/.bashrc`: +```zsh +export GIT_RADAR_COLOR_BRANCH='\\033[0;33m' +``` + +#### Setting an RC file + +Git radar supports multiple rc files. One of these will be sourced when the +prompt renders. + +**Example: Change the branch colour in Zsh** + +In `~/.gitradarrc`: +```zsh +GIT_RADAR_COLOR_BRANCH='$fg[yellow]' +``` + +**Basic RC file** + +Create a file at `~/.gitradarrc` which sets the Environment variables listed in +[Configuration values](#configuration-values) using colour codes listed in +either [Zsh Colour Codes](#zsh-colour-codes) or +[Bash Colour Codes](#Bash-Colour-Codes) depending on your shell. + +**Shell specific RC file** + +If you use both Bash and Zsh you can set RC files that are specific for those +shells. + +For Bash: Create a file at `~/.gitradarrc.bash` + +For Zsh: Create a file at `~/.gitradarrc.zsh` + + +#### Bash Colour Codes + +Bash colour codes make use of the colours your terminal app claims to be `red` +or `green`. Using one of these codes will only produce the colour your terminal +claims, so you should customise your colour scheme on your terminal as well as +customising git-radar. + +Note the "Bright" colours can be shown as bold instead, it depends on your +terminal. By default, for example, the Mac OSX Terminal.app uses the "Bright" +colours to provide 8 new lighter colours but some terminals only support 8 and +will show the text as bold instead. + +Colour | Code for Text | Code for Background +--------------|----------------|-------------------- +Black | `\\033[0;30m` | `\\033[0;40m` +Red | `\\033[0;31m` | `\\033[0;41m` +Green | `\\033[0;32m` | `\\033[0;42m` +Yellow | `\\033[0;33m` | `\\033[0;43m` +Blue | `\\033[0;34m` | `\\033[0;44m` +Magenta | `\\033[0;35m` | `\\033[0;45m` +Cyan | `\\033[0;36m` | `\\033[0;46m` +White | `\\033[0;37m` | `\\033[0;47m` +Bright Black | `\\033[1;30m` | `\\033[1;40m` +Bright Red | `\\033[1;31m` | `\\033[1;41m` +Bright Green | `\\033[1;32m` | `\\033[1;42m` +Bright Yellow | `\\033[1;33m` | `\\033[1;43m` +Bright Blue | `\\033[1;34m` | `\\033[1;44m` +Bright Magenta| `\\033[1;35m` | `\\033[1;45m` +Bright Cyan | `\\033[1;36m` | `\\033[1;46m` +Bright White | `\\033[1;37m` | `\\033[1;47m` +Reset | `\\033[0m` | `\\033[0m` + +Note the Reset will set back to what your terminal claims as standard text and +background. + +#### Zsh Colour Codes + +Zsh also provides a way to access the colours that your terminal claims as `red` +or `green`, etc. + +Note the "Bright" colours can be shown as bold instead, it depends on your +terminal. By default, for example, the Mac OSX Terminal.app uses the "Bright" +colours to provide 8 new lighter colours but some terminals only support 8 and +will show the text as bold instead. + +Colour | Code for Text | Code for Background +--------------|--------------------|-------------------- +Black | `$fg[black]` | `$bg[black]` +Red | `$fg[red]` | `$bg[red]` +Green | `$fg[green]` | `$bg[green]` +Yellow | `$fg[yellow]` | `$bg[yellow]` +Blue | `$fg[blue]` | `$bg[blue]` +Magenta | `$fg[magenta]` | `$bg[magenta]` +Cyan | `$fg[cyan]` | `$bg[cyan]` +White | `$fg[white]` | `$bg[white]` +Bright Black | `$fg_bold[black]` | `$bg_bold[black]` +Bright Red | `$fg_bold[red]` | `$bg_bold[red]` +Bright Green | `$fg_bold[green]` | `$bg_bold[green]` +Bright Yellow | `$fg_bold[yellow]` | `$bg_bold[yellow]` +Bright Blue | `$fg_bold[blue]` | `$bg_bold[blue]` +Bright Magenta| `$fg_bold[magenta]`| `$bg_bold[magenta]` +Bright Cyan | `$fg_bold[cyan]` | `$bg_bold[cyan]` +Bright White | `$fg_bold[white]` | `$bg_bold[white]` +Reset | `$reset_color` | `$reset_color` + +#### Configuration values + +All these values should be set using a the correct colour code for your +terminal. You should also choose the colour code based on what shell you are +using. There is a way to support [colouring multiple shells using rc files](#setting-an-rc-file). + +##### Colouring the Branch part + +**GIT_RADAR_COLOR_BRANCH='[colour code]'** +``` +git:(my-branch) + ^^^^^^^^^ +``` +The colour to use for the Branch or git reference. + +It is unset by +`GIT_RADAR_COLOR_BRANCH_RESET` which you can set if you want a different +background colour to return to. + +##### Colouring the local commits status + +**GIT_RADAR_COLOR_LOCAL_AHEAD='[colour code]'** +``` +git:(my-branch 1↑) + ^ +``` +The colour to use for the arrow that indicates how many commits you have to push +up. + +It is unset by `GIT_RADAR_COLOR_LOCAL_RESET` which you can set if you want +a different background colour to return to. + +**GIT_RADAR_COLOR_LOCAL_BEHIND='[colour code]'** +``` +git:(my-branch 1↓) + ^ +``` +The colour to use for the arrow that indicates how many commits you have to pull +down. + +It is unset by `GIT_RADAR_COLOR_LOCAL_RESET` which you can set if you want +a different background colour to return to. + +**GIT_RADAR_COLOR_LOCAL_DIVERGED='[colour code]'** +``` +git:(my-branch 1⇵1) + ^ +``` +The colour to use for the arrow that indicates how many commits your branch has diverged by. + +It is unset by `GIT_RADAR_COLOR_LOCAL_RESET` which you can set if you want +a different background colour to return to. + +##### Colouring the remote commits status + +**GIT_RADAR_COLOR_REMOTE_AHEAD='[colour code]'** +``` +git:(m ← 1 my-branch) + ^ +``` +The colour to use for the arrow that indicates how many commits your branch has to merge on to master. + +It is unset by `GIT_RADAR_COLOR_REMOTE_RESET` which you can set if you want +a different background colour to return to. + +**GIT_RADAR_COLOR_REMOTE_BEHIND='[colour code]'** +``` +git:(m 1 → my-branch) + ^ +``` +The colour to use for the arrow that indicates how many commits your branch is +behind master. + +It is unset by `GIT_RADAR_COLOR_REMOTE_RESET` which you can set if you want +a different background colour to return to. + +**GIT_RADAR_COLOR_REMOTE_DIVERGED='[colour code]'** ``` +git:(m 1 ⇄ 1 my-branch) + ^ +``` +The colour to use for the arrow that indicates how many commits your branch has +diverged from master. + +It is unset by `GIT_RADAR_COLOR_REMOTE_RESET` which you can set if you want +a different background colour to return to. + +**GIT_RADAR_COLOR_REMOTE_NOT_UPSTREAM='[colour code]'** +``` +git:(upstream ⚡ my-branch) + ^ +``` +The colour to use for the lightning bolt which indicates that your branch is not +tracking an upstream branch. + +It is unset by `GIT_RADAR_COLOR_REMOTE_RESET` which you can set if you want +a different background colour to return to. + +##### Colouring the file changes status + +**GIT_RADAR_COLOR_CHANGES_STAGED='[colour code]'** +``` +git:(my-branch) 1M + ^ +``` +The colour to use for the letters that indicate changes that have been staged to +commit. + +It is unset by `GIT_RADAR_COLOR_CHANGES_RESET` which you can set if you want +a different background colour to return to. + +**GIT_RADAR_COLOR_CHANGES_UNSTAGED='[colour code]'** +``` +git:(my-branch) 1M + ^ +``` +The colour to use for the letters that indicate changes that have not yet been +staged to commit. + +It is unset by `GIT_RADAR_COLOR_CHANGES_RESET` which you can set if you want +a different background colour to return to. + +**GIT_RADAR_COLOR_CHANGES_CONFLICTED='[colour code]'** +``` +git:(my-branch) 1B + ^ +``` +The colour to use for the letters that indicate changes that have conflicts that +need resolved. + +It is unset by `GIT_RADAR_COLOR_CHANGES_RESET` which you can set if you want +a different background colour to return to. + +**GIT_RADAR_COLOR_CHANGES_UNTRACKED='[colour code]'** +``` +git:(my-branch) 1A + ^ +``` +The colour to use for the letters that indicate files that are currently not +tracked by git. + +It is unset by `GIT_RADAR_COLOR_CHANGES_RESET` which you can set if you want +a different background colour to return to. ## License @@ -11,6 +11,7 @@ else fi dot="$(cd "$(dirname "$([ -L "$0" ] && $READLINK_CMD -f "$0" || echo "$0")")"; pwd)" +args=$@ if [[ -z $@ ]]; then _git="\033[1;30mgit:(\033[0m" @@ -92,9 +93,9 @@ while [[ $# > 0 ]];do nohup $dot/fetch.sh >/dev/null 2>&1 & fi if [[ "$command" == "--zsh" ]]; then - $dot/prompt.zsh + $dot/prompt.zsh $args fi if [[ "$command" == "--bash" || "$command" == "--fish" ]]; then - $dot/prompt.bash + $dot/prompt.bash $args fi done diff --git a/prompt.bash b/prompt.bash index a3fe420..51c4db7 100755 --- a/prompt.bash +++ b/prompt.bash @@ -1,13 +1,17 @@ #! /usr/bin/env bash dot="$(cd "$(dirname "$0")"; pwd)" +args=$@ source "$dot/radar-base.sh" if is_repo; then + prepare_bash_colors printf " \x01\033[1;30m\x02git:(\x01\033[0m\x02" - bash_color_remote_commits + if show_remote_status $args; then + color_remote_commits + fi readable_branch_name - bash_color_local_commits + color_local_commits printf "\x01\033[1;30m\x02)\x01\033[0m\x02" - bash_color_changes_status + color_changes_status fi @@ -1,14 +1,19 @@ #! /usr/bin/env zsh dot="$(cd "$(dirname "$0")"; pwd)" +args=$@ source "$dot/radar-base.sh" if is_repo; then autoload colors && colors + + prepare_zsh_colors printf '%s' "%{$fg_bold[black]%} git:(%{$reset_color%}" - zsh_color_remote_commits + if show_remote_status $args; then + color_remote_commits + fi readable_branch_name - zsh_color_local_commits + color_local_commits printf '%s' "%{$fg_bold[black]%})%{$reset_color%}" - zsh_color_changes_status + color_changes_status fi diff --git a/radar-base.sh b/radar-base.sh index 8d18fe3..41f4512 100755 --- a/radar-base.sh +++ b/radar-base.sh @@ -1,6 +1,75 @@ +NO_REMOTE_STATUS='--no-remote-status' + dot_git="" cwd="" remote="" +rcfile_path="$HOME" + +prepare_bash_colors() { + if [ -f "$rcfile_path/.gitradarrc.bash" ]; then + source "$rcfile_path/.gitradarrc.bash" + elif [ -f "$rcfile_path/.gitradarrc" ]; then + source "$rcfile_path/.gitradarrc" + fi + + PRINT_F_OPTION="" + + COLOR_REMOTE_AHEAD="\x01${GIT_RADAR_COLOR_REMOTE_AHEAD:-"\\033[1;32m"}\x02" + COLOR_REMOTE_BEHIND="\x01${GIT_RADAR_COLOR_REMOTE_BEHIND:-"\\033[1;31m"}\x02" + COLOR_REMOTE_DIVERGED="\x01${GIT_RADAR_COLOR_REMOTE_DIVERGED:-"\\033[1;33m"}\x02" + COLOR_REMOTE_NOT_UPSTREAM="\x01${GIT_RADAR_COLOR_REMOTE_NOT_UPSTREAM:-"\\033[1;31m"}\x02" + + COLOR_LOCAL_AHEAD="\x01${GIT_RADAR_COLOR_LOCAL_AHEAD:-"\\033[1;32m"}\x02" + COLOR_LOCAL_BEHIND="\x01${GIT_RADAR_COLOR_LOCAL_BEHIND:-"\\033[1;31m"}\x02" + COLOR_LOCAL_DIVERGED="\x01${GIT_RADAR_COLOR_LOCAL_DIVERGED:-"\\033[1;33m"}\x02" + + COLOR_CHANGES_STAGED="\x01${GIT_RADAR_COLOR_CHANGES_STAGED:-"\\033[1;32m"}\x02" + COLOR_CHANGES_UNSTAGED="\x01${GIT_RADAR_COLOR_CHANGES_UNSTAGED:-"\\033[1;31m"}\x02" + COLOR_CHANGES_CONFLICTED="\x01${GIT_RADAR_COLOR_CHANGES_CONFLICTED:-"\\033[1;33m"}\x02" + COLOR_CHANGES_UNTRACKED="\x01${GIT_RADAR_COLOR_CHANGES_UNTRACKED:-"\\033[1;37m"}\x02" + + COLOR_BRANCH="\x01${GIT_RADAR_COLOR_BRANCH:-"\\033[0m"}\x02" + MASTER_SYMBOL="${GIT_RADAR_MASTER_SYMBOL:-"\\x01\\033[0m\\x02\\xF0\\x9D\\x98\\xAE\\x01\\033[0m\\x02"}" + + RESET_COLOR_LOCAL="\x01${GIT_RADAR_COLOR_LOCAL_RESET:-"\\033[0m"}\x02" + RESET_COLOR_REMOTE="\x01${GIT_RADAR_COLOR_REMOTE_RESET:-"\\033[0m"}\x02" + RESET_COLOR_CHANGES="\x01${GIT_RADAR_COLOR_CHANGES_RESET:-"\\033[0m"}\x02" + RESET_COLOR_BRANCH="\x01${GIT_RADAR_COLOR_BRANCH_RESET:-"\\033[0m"}\x02" +} + +prepare_zsh_colors() { + if [ -f "$rcfile_path/.gitradarrc.zsh" ]; then + source "$rcfile_path/.gitradarrc.zsh" + elif [ -f "$rcfile_path/.gitradarrc" ]; then + source "$rcfile_path/.gitradarrc" + fi + + PRINT_F_OPTION="%s" + + COLOR_REMOTE_AHEAD="%{${GIT_RADAR_COLOR_REMOTE_AHEAD:-$fg_bold[green]}%}" + COLOR_REMOTE_BEHIND="%{${GIT_RADAR_COLOR_REMOTE_BEHIND:-$fg_bold[red]}%}" + COLOR_REMOTE_DIVERGED="%{${GIT_RADAR_COLOR_REMOTE_DIVERGED:-$fg_bold[yellow]}%}" + COLOR_REMOTE_NOT_UPSTREAM="%{${GIT_RADAR_COLOR_REMOTE_NOT_UPSTREAM:-$fg_bold[red]}%}" + + COLOR_LOCAL_AHEAD="%{${GIT_RADAR_COLOR_LOCAL_AHEAD:-$fg_bold[green]}%}" + COLOR_LOCAL_BEHIND="%{${GIT_RADAR_COLOR_LOCAL_BEHIND:-$fg_bold[red]}%}" + COLOR_LOCAL_DIVERGED="%{${GIT_RADAR_COLOR_LOCAL_DIVERGED:-$fg_bold[yellow]}%}" + + COLOR_CHANGES_STAGED="%{${GIT_RADAR_COLOR_CHANGES_STAGED:-$fg_bold[green]}%}" + COLOR_CHANGES_UNSTAGED="%{${GIT_RADAR_COLOR_CHANGES_UNSTAGED:-$fg_bold[red]}%}" + COLOR_CHANGES_CONFLICTED="%{${GIT_RADAR_COLOR_CHANGES_CONFLICTED:-$fg_bold[yellow]}%}" + COLOR_CHANGES_UNTRACKED="%{${GIT_RADAR_COLOR_CHANGES_UNTRACKED:-$fg_bold[white]}%}" + + local italic_m="$(printf '\xF0\x9D\x98\xAE')" + + COLOR_BRANCH="%{${GIT_RADAR_COLOR_BRANCH:-$reset_color}%}" + MASTER_SYMBOL="${GIT_RADAR_MASTER_SYMBOL:-"%{$reset_color%}$italic_m%{$reset_color%}"}" + + RESET_COLOR_LOCAL="%{${GIT_RADAR_COLOR_LOCAL_RESET:-$reset_color}%}" + RESET_COLOR_REMOTE="%{${GIT_RADAR_COLOR_REMOTE_RESET:-$reset_color}%}" + RESET_COLOR_CHANGES="%{${GIT_RADAR_COLOR_CHANGES_RESET:-$reset_color}%}" + RESET_COLOR_BRANCH="%{${GIT_RADAR_COLOR_BRANCH_RESET:-$reset_color}%}" +} in_current_dir() { local wd="$(pwd)" @@ -115,12 +184,6 @@ branch_ref() { fi } -readable_branch_name() { - if is_repo; then - printf '%s' "$(branch_name || printf '%s' "detached@$(commit_short_sha)")" - fi -} - remote_branch_name() { local localRef="\/$(branch_name)$" if [[ -n "$localRef" ]]; then @@ -298,23 +361,17 @@ untracked_status() { printf '%s' "$untracked_string" } -bash_color_changes_status() { +color_changes_status() { local separator="${1:- }" local porcelain="$(porcelain_status)" local changes="" if [[ -n "$porcelain" ]]; then - local green_staged_prefix="\x01\033[1;32m\x02" - local red_unstaged_prefix="\x01\033[1;31m\x02" - local yellow_conflicted_prefix="\x01\033[1;33m\x02" - local grey_untracked_prefix="\x01\033[1;37m\x02" - local reset_suffix="\x01\033[0m\x02" - - local staged_changes="$(staged_status "$porcelain" "$green_staged_prefix" "$reset_suffix")" - local unstaged_changes="$(unstaged_status "$porcelain" "$red_unstaged_prefix" "$reset_suffix")" - local untracked_changes="$(untracked_status "$porcelain" "$grey_untracked_prefix" "$reset_suffix")" - local conflicted_changes="$(conflicted_status "$porcelain" "$yellow_conflicted_prefix" "$reset_suffix")" + local staged_changes="$(staged_status "$porcelain" "$COLOR_CHANGES_STAGED" "$RESET_COLOR_CHANGES")" + local unstaged_changes="$(unstaged_status "$porcelain" "$COLOR_CHANGES_UNSTAGED" "$RESET_COLOR_CHANGES")" + local untracked_changes="$(untracked_status "$porcelain" "$COLOR_CHANGES_UNTRACKED" "$RESET_COLOR_CHANGES")" + local conflicted_changes="$(conflicted_status "$porcelain" "$COLOR_CHANGES_CONFLICTED" "$RESET_COLOR_CHANGES")" if [[ -n "$staged_changes" ]]; then staged_changes="$separator$staged_changes" fi @@ -333,53 +390,23 @@ bash_color_changes_status() { changes="$staged_changes$conflicted_changes$unstaged_changes$untracked_changes" fi - printf "$changes" + printf $PRINT_F_OPTION "$changes" } -zsh_color_changes_status() { - local separator="${1:- }" - - local porcelain="$(porcelain_status)" - local changes="" - - if [[ -n "$porcelain" ]]; then - local staged_prefix="%{$fg_bold[green]%}" - local unstaged_prefix="%{$fg_bold[red]%}" - local conflicted_prefix="%{$fg_bold[yellow]%}" - local untracked_prefix="%{$fg_bold[white]%}" - local suffix="%{$reset_color%}" - - local staged_changes="$(staged_status "$porcelain" "$staged_prefix" "$suffix")" - local unstaged_changes="$(unstaged_status "$porcelain" "$unstaged_prefix" "$suffix")" - local untracked_changes="$(untracked_status "$porcelain" "$untracked_prefix" "$suffix")" - local conflicted_changes="$(conflicted_status "$porcelain" "$conflicted_prefix" "$suffix")" - if [[ -n "$staged_changes" ]]; then - staged_changes="$separator$staged_changes" - fi - - if [[ -n "$unstaged_changes" ]]; then - unstaged_changes="$separator$unstaged_changes" - fi - - if [[ -n "$conflicted_changes" ]]; then - conflicted_changes="$separator$conflicted_changes" - fi - - if [[ -n "$untracked_changes" ]]; then - untracked_changes="$separator$untracked_changes" - fi +bash_color_changes_status() { + color_changes_status +} - changes="$staged_changes$conflicted_changes$unstaged_changes$untracked_changes" - fi - printf %s "$changes" +zsh_color_changes_status() { + color_changes_status } -bash_color_local_commits() { +color_local_commits() { local separator="${1:- }" - local green_ahead_arrow="\x01\033[1;32m\x02↑\x01\033[0m\x02" - local red_behind_arrow="\x01\033[1;31m\x02↓\x01\033[0m\x02" - local yellow_diverged_arrow="\x01\033[1;33m\x02⇵\x01\033[0m\x02" + local green_ahead_arrow="${COLOR_LOCAL_AHEAD}↑$RESET_COLOR_LOCAL" + local red_behind_arrow="${COLOR_LOCAL_BEHIND}↓$RESET_COLOR_LOCAL" + local yellow_diverged_arrow="${COLOR_LOCAL_DIVERGED}⇵$RESET_COLOR_LOCAL" local local_commits="" if remote_branch="$(remote_branch_name)"; then @@ -394,78 +421,66 @@ bash_color_local_commits() { local_commits="$separator$local_ahead$green_ahead_arrow" fi fi - printf "$local_commits" + printf $PRINT_F_OPTION "$local_commits" } -zsh_color_local_commits() { - local separator="${1:- }" - - local ahead_arrow="%{$fg_bold[green]%}↑%{$reset_color%}" - local behind_arrow="%{$fg_bold[red]%}↓%{$reset_color%}" - local diverged_arrow="%{$fg_bold[yellow]%}⇵%{$reset_color%}" - - local local_commits="" - if remote_branch="$(remote_branch_name)"; then - local_ahead="$(commits_ahead_of_remote "$remote_branch")" - local_behind="$(commits_behind_of_remote "$remote_branch")" +bash_color_local_commits() { + color_local_commits +} - if [[ "$local_behind" -gt "0" && "$local_ahead" -gt "0" ]]; then - local_commits="$separator$local_behind$diverged_arrow$local_ahead" - elif [[ "$local_behind" -gt "0" ]]; then - local_commits="$separator$local_behind$behind_arrow" - elif [[ "$local_ahead" -gt "0" ]]; then - local_commits="$separator$local_ahead$ahead_arrow" - fi - fi - printf %s "$local_commits" +zsh_color_local_commits() { + color_local_commits } -bash_color_remote_commits() { - local remote_master="\xF0\x9D\x98\xAE" # an italic m to represent master - local green_ahead_arrow="\x01\033[1;32m\x02←\x01\033[0m\x02" - local red_behind_arrow="\x01\033[1;31m\x02→\x01\033[0m\x02" - local yellow_diverged_arrow="\x01\033[1;33m\x02⇄\x01\033[0m\x02" - local not_upstream="\x01\033[1;31m\x02⚡\x01\033[0m\x02" +color_remote_commits() { + local green_ahead_arrow="${COLOR_REMOTE_AHEAD}←$RESET_COLOR_REMOTE" + local red_behind_arrow="${COLOR_REMOTE_BEHIND}→$RESET_COLOR_REMOTE" + local yellow_diverged_arrow="${COLOR_REMOTE_DIVERGED}⇄$RESET_COLOR_REMOTE" + local not_upstream="${COLOR_REMOTE_NOT_UPSTREAM}⚡$RESET_COLOR_REMOTE" if remote_branch="$(remote_branch_name)"; then remote_ahead="$(remote_ahead_of_master "$remote_branch")" remote_behind="$(remote_behind_of_master "$remote_branch")" if [[ "$remote_behind" -gt "0" && "$remote_ahead" -gt "0" ]]; then - remote="$remote_master $remote_behind $yellow_diverged_arrow $remote_ahead " + remote="$MASTER_SYMBOL $remote_behind $yellow_diverged_arrow $remote_ahead " elif [[ "$remote_ahead" -gt "0" ]]; then - remote="$remote_master $green_ahead_arrow $remote_ahead " + remote="$MASTER_SYMBOL $green_ahead_arrow $remote_ahead " elif [[ "$remote_behind" -gt "0" ]]; then - remote="$remote_master $remote_behind $red_behind_arrow " + remote="$MASTER_SYMBOL $remote_behind $red_behind_arrow " fi else remote="upstream $not_upstream " fi - printf "$remote" + printf $PRINT_F_OPTION "$remote" } -zsh_color_remote_commits() { - local remote_master="$(printf '\xF0\x9D\x98\xAE')" # an italic m to represent master - local green_ahead_arrow="%{$fg_bold[green]%}←%{$reset_color%}" - local red_behind_arrow="%{$fg_bold[red]%}→%{$reset_color%}" - local yellow_diverged_arrow="%{$fg_bold[yellow]%}⇄%{$reset_color%}" - local not_upstream="%{$fg_bold[red]%}⚡%{$reset_color%}" +bash_color_remote_commits() { + color_remote_commits +} - if remote_branch="$(remote_branch_name)"; then - remote_ahead="$(remote_ahead_of_master "$remote_branch")" - remote_behind="$(remote_behind_of_master "$remote_branch")" +zsh_color_remote_commits() { + color_remote_commits +} - if [[ "$remote_behind" -gt "0" && "$remote_ahead" -gt "0" ]]; then - remote="$remote_master $remote_behind $yellow_diverged_arrow $remote_ahead " - elif [[ "$remote_ahead" -gt "0" ]]; then - remote="$remote_master $green_ahead_arrow $remote_ahead " - elif [[ "$remote_behind" -gt "0" ]]; then - remote="$remote_master $remote_behind $red_behind_arrow " - fi - else - remote="upstream $not_upstream " +readable_branch_name() { + if is_repo; then + printf $PRINT_F_OPTION "$COLOR_BRANCH$(branch_name || printf '%s' "detached@$(commit_short_sha)")$RESET_COLOR_BRANCH" fi +} - printf %s "$remote" +zsh_readable_branch_name() { + readable_branch_name +} + +bash_readable_branch_name() { + readable_branch_name +} + +show_remote_status() { + if [[ $@ == *$NO_REMOTE_STATUS* ]]; then + return 1 # don't show the git remote status + fi + return 0 } @@ -1,7 +1,9 @@ #!/bin/sh +./test-radar-base.sh ./test-directories.sh ./test-commits.sh ./test-branches.sh ./test-files.sh ./test-status.sh +./test-colors.sh diff --git a/test-branches.sh b/test-branches.sh index b972a25..b3f0ac2 100755 --- a/test-branches.sh +++ b/test-branches.sh @@ -55,6 +55,8 @@ test_detached_from_branch() { assertNotEquals "master" "$(branch_name)" assertEquals "$sha" "$(branch_ref)" + assertEquals "detached@$sha" "$(zsh_readable_branch_name)" + assertEquals "detached@$sha" "$(bash_readable_branch_name)" assertEquals "detached@$sha" "$(readable_branch_name)" rm_tmp diff --git a/test-colors.sh b/test-colors.sh new file mode 100755 index 0000000..73c20f7 --- /dev/null +++ b/test-colors.sh @@ -0,0 +1,492 @@ +scriptDir="$(cd "$(dirname "$0")"; pwd)" + +source "$scriptDir/radar-base.sh" + +cd_to_tmp() { + tmpfile="/tmp/git-prompt-tests-$(time_now)$1" + mkdir -p "$tmpfile" + cd "$tmpfile" +} + +rm_tmp() { + cd $scriptDir + rm -rf /tmp/git-prompt-tests* +} + +mock_zsh_colors() { + fg_bold[green]=1 + fg_bold[red]=2 + fg_bold[yellow]=3 + fg_bold[white]=4 + + reset_color=0 +} + +test_no_rcfile_bash() { + reset_env_vars + prepare_bash_colors + + assertEquals "$COLOR_REMOTE_AHEAD" "\x01\033[1;32m\x02" + assertEquals "$COLOR_REMOTE_BEHIND" "\x01\033[1;31m\x02" + assertEquals "$COLOR_REMOTE_DIVERGED" "\x01\033[1;33m\x02" + assertEquals "$COLOR_REMOTE_NOT_UPSTREAM" "\x01\033[1;31m\x02" + + assertEquals "$COLOR_LOCAL_AHEAD" "\x01\033[1;32m\x02" + assertEquals "$COLOR_LOCAL_BEHIND" "\x01\033[1;31m\x02" + assertEquals "$COLOR_LOCAL_DIVERGED" "\x01\033[1;33m\x02" + + assertEquals "$COLOR_CHANGES_STAGED" "\x01\033[1;32m\x02" + assertEquals "$COLOR_CHANGES_UNSTAGED" "\x01\033[1;31m\x02" + assertEquals "$COLOR_CHANGES_CONFLICTED" "\x01\033[1;33m\x02" + assertEquals "$COLOR_CHANGES_UNTRACKED" "\x01\033[1;37m\x02" + + assertEquals "$RESET_COLOR_LOCAL" "\x01\033[0m\x02" + assertEquals "$RESET_COLOR_REMOTE" "\x01\033[0m\x02" + assertEquals "$RESET_COLOR_CHANGES" "\x01\033[0m\x02" +} + +test_no_rcfile_zsh() { + reset_env_vars + mock_zsh_colors + prepare_zsh_colors + + assertEquals "$COLOR_REMOTE_AHEAD" "%{$fg_bold[green]%}" + assertEquals "$COLOR_REMOTE_BEHIND" "%{$fg_bold[red]%}" + assertEquals "$COLOR_REMOTE_DIVERGED" "%{$fg_bold[yellow]%}" + assertEquals "$COLOR_REMOTE_NOT_UPSTREAM" "%{$fg_bold[red]%}" + + assertEquals "$COLOR_LOCAL_AHEAD" "%{$fg_bold[green]%}" + assertEquals "$COLOR_LOCAL_BEHIND" "%{$fg_bold[red]%}" + assertEquals "$COLOR_LOCAL_DIVERGED" "%{$fg_bold[yellow]%}" + + assertEquals "$COLOR_CHANGES_STAGED" "%{$fg_bold[green]%}" + assertEquals "$COLOR_CHANGES_UNSTAGED" "%{$fg_bold[red]%}" + assertEquals "$COLOR_CHANGES_CONFLICTED" "%{$fg_bold[yellow]%}" + assertEquals "$COLOR_CHANGES_UNTRACKED" "%{$fg_bold[white]%}" + + assertEquals "$RESET_COLOR_LOCAL" "%{$reset_color%}" + assertEquals "$RESET_COLOR_REMOTE" "%{$reset_color%}" + assertEquals "$RESET_COLOR_CHANGES" "%{$reset_color%}" +} + +set_env_vars() { + export GIT_RADAR_COLOR_REMOTE_AHEAD="remote-ahead" + export GIT_RADAR_COLOR_REMOTE_BEHIND="remote-behind" + export GIT_RADAR_COLOR_REMOTE_DIVERGED="remote-diverged" + export GIT_RADAR_COLOR_REMOTE_NOT_UPSTREAM="not-upstream" + + export GIT_RADAR_COLOR_LOCAL_AHEAD="local-ahead" + export GIT_RADAR_COLOR_LOCAL_BEHIND="local-behind" + export GIT_RADAR_COLOR_LOCAL_DIVERGED="local-diverged" + + export GIT_RADAR_COLOR_CHANGES_STAGED="changes-staged" + export GIT_RADAR_COLOR_CHANGES_UNSTAGED="changes-unstaged" + export GIT_RADAR_COLOR_CHANGES_CONFLICTED="changes-conflicted" + export GIT_RADAR_COLOR_CHANGES_UNTRACKED="changes-untracked" + + export GIT_RADAR_COLOR_BRANCH="branch-color" + export GIT_RADAR_MASTER_SYMBOL="m" + + export GIT_RADAR_COLOR_LOCAL_RESET="local-reset" + export GIT_RADAR_COLOR_REMOTE_RESET="remote-reset" + export GIT_RADAR_COLOR_CHANGES_RESET="change-reset" + export GIT_RADAR_COLOR_BRANCH_RESET="branch-reset" +} + +reset_env_vars() { + export GIT_RADAR_COLOR_REMOTE_AHEAD="" + export GIT_RADAR_COLOR_REMOTE_BEHIND="" + export GIT_RADAR_COLOR_REMOTE_DIVERGED="" + export GIT_RADAR_COLOR_REMOTE_NOT_UPSTREAM="" + + export GIT_RADAR_COLOR_LOCAL_AHEAD="" + export GIT_RADAR_COLOR_LOCAL_BEHIND="" + export GIT_RADAR_COLOR_LOCAL_DIVERGED="" + + export GIT_RADAR_COLOR_CHANGES_STAGED="" + export GIT_RADAR_COLOR_CHANGES_UNSTAGED="" + export GIT_RADAR_COLOR_CHANGES_CONFLICTED="" + export GIT_RADAR_COLOR_CHANGES_UNTRACKED="" + + export GIT_RADAR_COLOR_BRANCH="" + export GIT_RADAR_MASTER_SYMBOL="" + + export GIT_RADAR_COLOR_LOCAL_RESET="" + export GIT_RADAR_COLOR_REMOTE_RESET="" + export GIT_RADAR_COLOR_CHANGES_RESET="" + export GIT_RADAR_COLOR_BRANCH_RESET="" +} + +create_rc_file() { + echo 'GIT_RADAR_COLOR_REMOTE_AHEAD="remote-ahead"' >> .gitradarrc$1 + echo 'GIT_RADAR_COLOR_REMOTE_BEHIND="remote-behind"' >> .gitradarrc$1 + echo 'GIT_RADAR_COLOR_REMOTE_DIVERGED="remote-diverged"' >> .gitradarrc$1 + echo 'GIT_RADAR_COLOR_REMOTE_NOT_UPSTREAM="not-upstream"' >> .gitradarrc$1 + + echo 'GIT_RADAR_COLOR_LOCAL_AHEAD="local-ahead"' >> .gitradarrc$1 + echo 'GIT_RADAR_COLOR_LOCAL_BEHIND="local-behind"' >> .gitradarrc$1 + echo 'GIT_RADAR_COLOR_LOCAL_DIVERGED="local-diverged"' >> .gitradarrc$1 + + echo 'GIT_RADAR_COLOR_CHANGES_STAGED="changes-staged"' >> .gitradarrc$1 + echo 'GIT_RADAR_COLOR_CHANGES_UNSTAGED="changes-unstaged"' >> .gitradarrc$1 + echo 'GIT_RADAR_COLOR_CHANGES_CONFLICTED="changes-conflicted"' >> .gitradarrc$1 + echo 'GIT_RADAR_COLOR_CHANGES_UNTRACKED="changes-untracked"' >> .gitradarrc$1 + + echo 'export GIT_RADAR_COLOR_BRANCH="branch-color"' >> .gitradarrc$1 + echo 'export GIT_RADAR_MASTER_SYMBOL="m"' >> .gitradarrc$1 + + echo 'GIT_RADAR_COLOR_LOCAL_RESET="local-reset"' >> .gitradarrc$1 + echo 'GIT_RADAR_COLOR_REMOTE_RESET="remote-reset"' >> .gitradarrc$1 + echo 'GIT_RADAR_COLOR_CHANGES_RESET="change-reset"' >> .gitradarrc$1 + echo 'GIT_RADAR_COLOR_BRANCH_RESET="branch-reset"' >> .gitradarrc$1 +} + +test_with_rcfile_bash() { + reset_env_vars + cd_to_tmp + + rcfile_path="$(pwd)" + + create_rc_file ".bash" + prepare_bash_colors + + assertEquals "$COLOR_REMOTE_AHEAD" "\x01remote-ahead\x02" + assertEquals "$COLOR_REMOTE_BEHIND" "\x01remote-behind\x02" + assertEquals "$COLOR_REMOTE_DIVERGED" "\x01remote-diverged\x02" + assertEquals "$COLOR_REMOTE_NOT_UPSTREAM" "\x01not-upstream\x02" + + assertEquals "$COLOR_LOCAL_AHEAD" "\x01local-ahead\x02" + assertEquals "$COLOR_LOCAL_BEHIND" "\x01local-behind\x02" + assertEquals "$COLOR_LOCAL_DIVERGED" "\x01local-diverged\x02" + + assertEquals "$COLOR_CHANGES_STAGED" "\x01changes-staged\x02" + assertEquals "$COLOR_CHANGES_UNSTAGED" "\x01changes-unstaged\x02" + assertEquals "$COLOR_CHANGES_CONFLICTED" "\x01changes-conflicted\x02" + assertEquals "$COLOR_CHANGES_UNTRACKED" "\x01changes-untracked\x02" + + assertEquals "$COLOR_BRANCH" "\x01branch-color\x02" + assertEquals "$MASTER_SYMBOL" "m" + + assertEquals "$RESET_COLOR_LOCAL" "\x01local-reset\x02" + assertEquals "$RESET_COLOR_REMOTE" "\x01remote-reset\x02" + assertEquals "$RESET_COLOR_CHANGES" "\x01change-reset\x02" + assertEquals "$RESET_COLOR_BRANCH" "\x01branch-reset\x02" + + rm_tmp +} + +test_with_rcfile_zsh() { + reset_env_vars + cd_to_tmp + + rcfile_path="$(pwd)" + + create_rc_file ".zsh" + mock_zsh_colors + prepare_zsh_colors + + assertEquals "$COLOR_REMOTE_AHEAD" "%{remote-ahead%}" + assertEquals "$COLOR_REMOTE_BEHIND" "%{remote-behind%}" + assertEquals "$COLOR_REMOTE_DIVERGED" "%{remote-diverged%}" + assertEquals "$COLOR_REMOTE_NOT_UPSTREAM" "%{not-upstream%}" + + assertEquals "$COLOR_LOCAL_AHEAD" "%{local-ahead%}" + assertEquals "$COLOR_LOCAL_BEHIND" "%{local-behind%}" + assertEquals "$COLOR_LOCAL_DIVERGED" "%{local-diverged%}" + + assertEquals "$COLOR_CHANGES_STAGED" "%{changes-staged%}" + assertEquals "$COLOR_CHANGES_UNSTAGED" "%{changes-unstaged%}" + assertEquals "$COLOR_CHANGES_CONFLICTED" "%{changes-conflicted%}" + assertEquals "$COLOR_CHANGES_UNTRACKED" "%{changes-untracked%}" + + assertEquals "$COLOR_BRANCH" "%{branch-color%}" + assertEquals "$MASTER_SYMBOL" "m" + + assertEquals "$RESET_COLOR_LOCAL" "%{local-reset%}" + assertEquals "$RESET_COLOR_REMOTE" "%{remote-reset%}" + assertEquals "$RESET_COLOR_CHANGES" "%{change-reset%}" + assertEquals "$RESET_COLOR_BRANCH" "%{branch-reset%}" + + rm_tmp +} + +test_with_env_vars_bash() { + reset_env_vars + set_env_vars + prepare_bash_colors + + assertEquals "$COLOR_REMOTE_AHEAD" "\x01remote-ahead\x02" + assertEquals "$COLOR_REMOTE_BEHIND" "\x01remote-behind\x02" + assertEquals "$COLOR_REMOTE_DIVERGED" "\x01remote-diverged\x02" + assertEquals "$COLOR_REMOTE_NOT_UPSTREAM" "\x01not-upstream\x02" + + assertEquals "$COLOR_LOCAL_AHEAD" "\x01local-ahead\x02" + assertEquals "$COLOR_LOCAL_BEHIND" "\x01local-behind\x02" + assertEquals "$COLOR_LOCAL_DIVERGED" "\x01local-diverged\x02" + + assertEquals "$COLOR_CHANGES_STAGED" "\x01changes-staged\x02" + assertEquals "$COLOR_CHANGES_UNSTAGED" "\x01changes-unstaged\x02" + assertEquals "$COLOR_CHANGES_CONFLICTED" "\x01changes-conflicted\x02" + assertEquals "$COLOR_CHANGES_UNTRACKED" "\x01changes-untracked\x02" + + assertEquals "$COLOR_BRANCH" "\x01branch-color\x02" + assertEquals "$MASTER_SYMBOL" "m" + + assertEquals "$RESET_COLOR_LOCAL" "\x01local-reset\x02" + assertEquals "$RESET_COLOR_REMOTE" "\x01remote-reset\x02" + assertEquals "$RESET_COLOR_CHANGES" "\x01change-reset\x02" + assertEquals "$RESET_COLOR_BRANCH" "\x01branch-reset\x02" +} + +test_with_env_vars_zsh() { + reset_env_vars + set_env_vars + mock_zsh_colors + prepare_zsh_colors + + assertEquals "$COLOR_REMOTE_AHEAD" "%{remote-ahead%}" + assertEquals "$COLOR_REMOTE_BEHIND" "%{remote-behind%}" + assertEquals "$COLOR_REMOTE_DIVERGED" "%{remote-diverged%}" + assertEquals "$COLOR_REMOTE_NOT_UPSTREAM" "%{not-upstream%}" + + assertEquals "$COLOR_LOCAL_AHEAD" "%{local-ahead%}" + assertEquals "$COLOR_LOCAL_BEHIND" "%{local-behind%}" + assertEquals "$COLOR_LOCAL_DIVERGED" "%{local-diverged%}" + + assertEquals "$COLOR_CHANGES_STAGED" "%{changes-staged%}" + assertEquals "$COLOR_CHANGES_UNSTAGED" "%{changes-unstaged%}" + assertEquals "$COLOR_CHANGES_CONFLICTED" "%{changes-conflicted%}" + assertEquals "$COLOR_CHANGES_UNTRACKED" "%{changes-untracked%}" + + assertEquals "$COLOR_BRANCH" "%{branch-color%}" + assertEquals "$MASTER_SYMBOL" "m" + + assertEquals "$RESET_COLOR_LOCAL" "%{local-reset%}" + assertEquals "$RESET_COLOR_REMOTE" "%{remote-reset%}" + assertEquals "$RESET_COLOR_CHANGES" "%{change-reset%}" + assertEquals "$RESET_COLOR_BRANCH" "%{branch-reset%}" +} + +test_bash_colors_local() { + reset_env_vars + set_env_vars + prepare_bash_colors + + cd_to_tmp "remote" + git init --bare --quiet + remoteLocation="$(pwd)" + + cd_to_tmp "repo" + git init --quiet + git remote add origin $remoteLocation + git fetch origin --quiet + git checkout -b master --quiet + touch README + git add README + git commit -m "initial commit" --quiet + git push --quiet -u origin master >/dev/null + repoLocation="$(pwd)" + + echo "foo" > foo + git add . + git commit -m "test commit" --quiet + + printf -v expected " 1\x01local-ahead\x02↑\x01local-reset\x02" + assertEquals "$expected" "$(bash_color_local_commits)" + assertEquals "$expected" "$(color_local_commits)" + + git push --quiet >/dev/null + git reset --hard head^ --quiet >/dev/null + + printf -v expected " 1\x01local-behind\x02↓\x01local-reset\x02" + assertEquals "$expected" "$(bash_color_local_commits)" + assertEquals "$expected" "$(color_local_commits)" + + echo "foo" > foo + git add . + git commit -m "new commit" --quiet + + printf -v expected " 1\x01local-diverged\x02⇵\x01local-reset\x021" + assertEquals "$expected" "$(bash_color_local_commits)" + assertEquals "$expected" "$(color_local_commits)" + + rm_tmp +} + +test_zsh_colors_local() { + reset_env_vars + set_env_vars + prepare_zsh_colors + + cd_to_tmp "remote" + git init --bare --quiet + remoteLocation="$(pwd)" + + cd_to_tmp "repo" + git init --quiet + git remote add origin $remoteLocation + git fetch origin --quiet + git checkout -b master --quiet + touch README + git add README + git commit -m "initial commit" --quiet + git push --quiet -u origin master >/dev/null + repoLocation="$(pwd)" + + echo "foo" > foo + git add . + git commit -m "test commit" --quiet + + assertEquals " 1%{local-ahead%}↑%{local-reset%}" "$(zsh_color_local_commits)" + + git push --quiet >/dev/null + git reset --hard head^ --quiet >/dev/null + + assertEquals " 1%{local-behind%}↓%{local-reset%}" "$(zsh_color_local_commits)" + + echo "foo" > foo + git add . + git commit -m "new commit" --quiet + + assertEquals " 1%{local-diverged%}⇵%{local-reset%}1" "$(zsh_color_local_commits)" + + rm_tmp +} + +test_bash_colors_remote() { + reset_env_vars + set_env_vars + prepare_bash_colors + + cd_to_tmp "remote" + git init --bare --quiet + remoteLocation="$(pwd)" + + cd_to_tmp "repo" + git init --quiet + git remote add origin $remoteLocation + git fetch origin --quiet + git checkout -b master --quiet + touch README + git add README + git commit -m "initial commit" --quiet + echo "foo" > foo + git add . + git commit -m "test commit" --quiet + git push --quiet -u origin master >/dev/null + repoLocation="$(pwd)" + + git reset --hard head^ --quiet >/dev/null + git checkout -b mybranch --quiet + git push --quiet -u origin mybranch >/dev/null + + printf -v expected "m 1 \x01remote-behind\x02→\x01remote-reset\x02 " + assertEquals "$expected" "$(bash_color_remote_commits)" + assertEquals "$expected" "$(color_remote_commits)" + + echo "bar" > bar + git add . + git commit -m "new commit" --quiet + git push --quiet >/dev/null + + printf -v expected "m 1 \x01remote-diverged\x02⇄\x01remote-reset\x02 1 " + assertEquals "$expected" "$(bash_color_remote_commits)" + assertEquals "$expected" "$(color_remote_commits)" + + git pull origin master --quiet >/dev/null + git push --quiet >/dev/null + + printf -v expected "m \x01remote-ahead\x02←\x01remote-reset\x02 2 " + assertEquals "$expected" "$(bash_color_remote_commits)" + assertEquals "$expected" "$(color_remote_commits)" + + rm_tmp +} + +test_zsh_colors_remote() { + reset_env_vars + set_env_vars + prepare_zsh_colors + + cd_to_tmp "remote" + git init --bare --quiet + remoteLocation="$(pwd)" + + cd_to_tmp "repo" + git init --quiet + git remote add origin $remoteLocation + git fetch origin --quiet + git checkout -b master --quiet + touch README + git add README + git commit -m "initial commit" --quiet + echo "foo" > foo + git add . + git commit -m "test commit" --quiet + git push --quiet -u origin master >/dev/null + repoLocation="$(pwd)" + + git reset --hard head^ --quiet >/dev/null + git checkout -b mybranch --quiet + git push --quiet -u origin mybranch >/dev/null + + assertEquals "m 1 %{remote-behind%}→%{remote-reset%} " "$(zsh_color_remote_commits)" + + echo "bar" > bar + git add . + git commit -m "new commit" --quiet + git push --quiet >/dev/null + + assertEquals "m 1 %{remote-diverged%}⇄%{remote-reset%} 1 " "$(zsh_color_remote_commits)" + + git pull origin master --quiet >/dev/null + git push --quiet >/dev/null + + assertEquals "m %{remote-ahead%}←%{remote-reset%} 2 " "$(zsh_color_remote_commits)" + + rm_tmp +} + +test_bash_colors_changes() { + reset_env_vars + set_env_vars + prepare_bash_colors + + cd_to_tmp + git init --quiet + + touch foo + touch bar + git add bar + echo "bar" > bar + untracked="1\x01changes-untracked\x02A\x01change-reset\x02" + unstaged="1\x01changes-unstaged\x02M\x01change-reset\x02" + staged="1\x01changes-staged\x02A\x01change-reset\x02" + + printf -v expected " $staged $unstaged $untracked" + assertEquals "$expected" "$(bash_color_changes_status)" + assertEquals "$expected" "$(color_changes_status)" + rm_tmp +} + +test_zsh_colors_changes() { + reset_env_vars + set_env_vars + prepare_zsh_colors + + cd_to_tmp + git init --quiet + + touch foo + touch bar + git add bar + echo "bar" > bar + untracked="1%{changes-untracked%}A%{change-reset%}" + unstaged="1%{changes-unstaged%}M%{change-reset%}" + staged="1%{changes-staged%}A%{change-reset%}" + + assertEquals " $staged $unstaged $untracked" "$(zsh_color_changes_status)" + rm_tmp +} + +. ./shunit/shunit2 diff --git a/test-commits.sh b/test-commits.sh index 89a1ba1..8eecffc 100755 --- a/test-commits.sh +++ b/test-commits.sh @@ -367,19 +367,16 @@ test_quiet_if_no_remote_master() { rm_tmp } -test_zsh_and_bash_local_commits() { - local zsh_up="%{[green]%}↑%{%}" - local zsh_both="%{[yellow]%}⇵%{%}" - local zsh_down="%{[red]%}↓%{%}" - - printf -v bash_up "\x01\033[1;32m\x02↑\x01\033[0m\x02" - printf -v bash_both "\x01\033[1;33m\x02⇵\x01\033[0m\x02" - printf -v bash_down "\x01\033[1;31m\x02↓\x01\033[0m\x02" +test_local_commits() { + local up="↑" + local both="⇵" + local down="↓" cd_to_tmp "remote" assertEquals "" "$(zsh_color_local_commits)" assertEquals "" "$(bash_color_local_commits)" + assertEquals "" "$(color_local_commits)" git init --quiet touch README @@ -396,14 +393,16 @@ test_zsh_and_bash_local_commits() { assertEquals "" "$(zsh_color_local_commits)" assertEquals "" "$(bash_color_local_commits)" + assertEquals "" "$(color_local_commits)" cd "$repo" echo "bar" > bar git add . git commit -m "test commit" --quiet - assertEquals " 1$zsh_up" "$(zsh_color_local_commits)" - assertEquals " 1$bash_up" "$(bash_color_local_commits)" + assertEquals " 1$up" "$(zsh_color_local_commits)" + assertEquals " 1$up" "$(bash_color_local_commits)" + assertEquals " 1$up" "$(color_local_commits)" cd "$remote" echo "foo" > foo @@ -413,13 +412,15 @@ test_zsh_and_bash_local_commits() { cd "$repo" git fetch origin --quiet - assertEquals " 1${zsh_both}1" "$(zsh_color_local_commits)" - assertEquals " 1${bash_both}1" "$(bash_color_local_commits)" + assertEquals " 1${both}1" "$(zsh_color_local_commits)" + assertEquals " 1${both}1" "$(bash_color_local_commits)" + assertEquals " 1${both}1" "$(color_local_commits)" git reset --hard HEAD^ --quiet - assertEquals " 1$zsh_down" "$(zsh_color_local_commits)" - assertEquals " 1$bash_down" "$(bash_color_local_commits)" + assertEquals " 1$down" "$(zsh_color_local_commits)" + assertEquals " 1$down" "$(bash_color_local_commits)" + assertEquals " 1$down" "$(color_local_commits)" } . ./shunit/shunit2 diff --git a/test-radar-base.sh b/test-radar-base.sh new file mode 100755 index 0000000..9f7f8ee --- /dev/null +++ b/test-radar-base.sh @@ -0,0 +1,28 @@ +scriptDir="$(cd "$(dirname "$0")"; pwd)" + +source "$scriptDir/radar-base.sh" + +test_show_remote_status() { + show_remote_status + assertTrue $? + + show_remote_status --bash + assertTrue $? + + show_remote_status --bash --fetch + assertTrue $? + + show_remote_status --bash --no-remote-status --fetch + assertFalse $? + + show_remote_status --bash --fetch --no-remote-status + assertFalse $? + + show_remote_status --no-remote-status --bash --fetch + assertFalse $? + + show_remote_status --bash --fetch --minimal --no-remote-status + assertFalse $? +} + +. ./shunit/shunit2
\ No newline at end of file |