Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: enhance git section #1260

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 73 additions & 15 deletions docs/sections/git.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,48 @@

!!! important "This section is rendered asynchronously by default"

The `git` section consists of [`git_branch`](#git-branch-git_branch) and [`git_status`](#git-status-git_status) subsections.
The `git` section consists of [`git_branch`](#git-branch-git_branch), [`git_tag`](#git-tag-git_tag), [`git_status`](#git-status-git_status), [`git_diff`](#git-diff-git_diff), [`git_hash`](#git-hash-git_hash) and
[`git_commit_msg`](#git-commit_msg-git_commit_msg),subsections.

## Options

| Variable | Default | Meaning |
| :--------------------- | :--------------------------------: | ----------------------------------------------------------------------------------------------------------- |
| `SPACESHIP_GIT_SHOW` | `true` | Show section |
| `SPACESHIP_GIT_ASYNC` | `true` | Render section asynchronously |
| `SPACESHIP_GIT_PREFIX` | `on·` | Section's prefix |
| `SPACESHIP_GIT_SUFFIX` | `$SPACESHIP_PROMPT_DEFAULT_SUFFIX` | Section's suffix |
| `SPACESHIP_GIT_SYMBOL` | `` | Symbol displayed before the section (requires [powerline patched font](https://github.com/powerline/fonts)) |
| `SPACESHIP_GIT_ORDER` | `(git_branch git_status)` | Order of git subsection rendering |
| Variable | Default | Meaning |
|:-----------------------|:------------------------------------------------------------------:|-------------------------------------------------------------------------------------------------------------|
| `SPACESHIP_GIT_SHOW` | `true` | Show section |
| `SPACESHIP_GIT_ASYNC` | `true` | Render section asynchronously |
| `SPACESHIP_GIT_PREFIX` | `on·` | Section's prefix |
| `SPACESHIP_GIT_SUFFIX` | `$SPACESHIP_PROMPT_DEFAULT_SUFFIX` | Section's suffix |
| `SPACESHIP_GIT_SYMBOL` | `` | Symbol displayed before the section (requires [powerline patched font](https://github.com/powerline/fonts)) |
| `SPACESHIP_GIT_ORDER` | `(git_branch git_tag git_status git_diff git_hash git_commit_msg)` | Order of git subsection rendering |
| | | |

## Git branch `git_branch`

The `git_branch` section displays the current branch of the Git repository.

### Options

| Variable | Default | Meaning |
| :---------------------------- | :---------------------: | ----------------------------- |
| `SPACESHIP_GIT_BRANCH_SHOW` | `true` | Show section |
| `SPACESHIP_GIT_BRANCH_ASYNC` | `false` | Render section asynchronously |
| Variable | Default | Meaning |
|:------------------------------|:-----------------------:|-------------------------------|
| `SPACESHIP_GIT_BRANCH_SHOW` | `true` | Show section |
| `SPACESHIP_GIT_BRANCH_ASYNC` | `true` | Render section asynchronously |
| `SPACESHIP_GIT_BRANCH_PREFIX` | `$SPACESHIP_GIT_SYMBOL` | Section's prefix |
| `SPACESHIP_GIT_BRANCH_SUFFIX` | ` ` | Section's suffix |
| `SPACESHIP_GIT_BRANCH_COLOR` | `magenta` | Section's color |
| `SPACESHIP_GIT_BRANCH_SUFFIX` | ` ` | Section's suffix |
| `SPACESHIP_GIT_BRANCH_COLOR` | `magenta` | Section's color |

## Git branch `git_tag`

The `git_tag` section displays the current branch tag of the Git repository.

### Options

| Variable | Default | Meaning |
|:---------------------------|:-------:|-------------------------------|
| `SPACESHIP_GIT_TAG_SHOW` | `true` | Show section |
| `SPACESHIP_GIT_TAG_ASYNC` | `true` | Render section asynchronously |
| `SPACESHIP_GIT_TAG_PREFIX` | ` @` | Section's prefix |
| `SPACESHIP_GIT_TAG_SUFFIX` | ` ` | Section's suffix |
| `SPACESHIP_GIT_TAG_COLOR` | `blue` | Section's color |

## Git status `git_status`

Expand All @@ -52,3 +68,45 @@ The `git_status` subsection displays indicators only when you have a dirty Git r
| `SPACESHIP_GIT_STATUS_AHEAD` | `⇡` | Indicator for unpushed changes (ahead of remote branch) |
| `SPACESHIP_GIT_STATUS_BEHIND` | `⇣` | Indicator for unpulled changes (behind of remote branch) |
| `SPACESHIP_GIT_STATUS_DIVERGED` | `⇕` | Indicator for diverged changes (diverged with remote branch) |

## Git branch `git_diff`

The `git_diff` section displays the count of diff (added and removed) for the current branch with the upstream Git repository.

### Options

| Variable | Default | Meaning |
|:--------------------------------|:-------:|-------------------------------|
| `SPACESHIP_GIT_DIFFSTAT_SHOW` | `true` | Show section |
| `SPACESHIP_GIT_DIFFSTAT_ASYNC` | `true` | Render section asynchronously |
| `SPACESHIP_GIT_DIFFSTAT_PREFIX` | | Section's prefix |
| `SPACESHIP_GIT_DIFFSTAT_SUFFIX` | ` ` | Section's suffix |

## Git branch `git_hash`

The `git_hash` section displays the current branch commit hash of the Git repository.

### Options

| Variable | Default | Meaning |
|:--------------------------------|:---------:|-------------------------------|
| `SPACESHIP_GIT_GIT_HASH_SHOW` | `true` | Show section |
| `SPACESHIP_GIT_GIT_HASH_ASYNC` | `true` | Render section asynchronously |
| `SPACESHIP_GIT_GIT_HASH_PREFIX` | ` ※ ` | Section's prefix |
| `SPACESHIP_GIT_GIT_HASH_SUFFIX` | ` ` | Section's suffix |
| `SPACESHIP_GIT_GIT_HASH_COLOR` | `magenta` | Section's collor |
| | | |

## Git branch `git_commit_msg`

The `git_commit_msg` section displays the current branch commit message of the Git repository.

### Options

| Variable | Default | Meaning |
|:--------------------------------------|:-------:|-------------------------------|
| `SPACESHIP_GIT_GIT_COMMIT_MSG_SHOW` | `true` | Show section |
| `SPACESHIP_GIT_GIT_COMMIT_MSG_ASYNC` | `true` | Render section asynchronously |
| `SPACESHIP_GIT_GIT_COMMIT_MSG_PREFIX` | ` 📓️ ` | Section's prefix |
| `SPACESHIP_GIT_GIT_COMMIT_MSG_SUFFIX` | ` ` | Section's suffix |
| `SPACESHIP_GIT_GIT_COMMIT_MSG_COLOR` | `black` | Section's collor |
11 changes: 10 additions & 1 deletion sections/git.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,27 @@ SPACESHIP_GIT_SUFFIX="${SPACESHIP_GIT_SUFFIX="$SPACESHIP_PROMPT_DEFAULT_SUFFIX"}
SPACESHIP_GIT_SYMBOL="${SPACESHIP_GIT_SYMBOL=" "}"

if [ -z "$SPACESHIP_GIT_ORDER" ]; then
SPACESHIP_GIT_ORDER=(git_branch git_status)
SPACESHIP_GIT_ORDER=(git_branch git_tag git_status git_diff git_hash git_commit_msg)
fi

# ------------------------------------------------------------------------------
# Dependencies
# ------------------------------------------------------------------------------

source "$SPACESHIP_ROOT/sections/git_branch.zsh"
source "$SPACESHIP_ROOT/sections/git_tag.zsh"
source "$SPACESHIP_ROOT/sections/git_status.zsh"
source "$SPACESHIP_ROOT/sections/git_diff.zsh"
source "$SPACESHIP_ROOT/sections/git_hash.zsh"
source "$SPACESHIP_ROOT/sections/git_commit_msg.zsh"


spaceship::precompile "$SPACESHIP_ROOT/sections/git_branch.zsh"
spaceship::precompile "$SPACESHIP_ROOT/sections/git_tag.zsh"
spaceship::precompile "$SPACESHIP_ROOT/sections/git_status.zsh"
spaceship::precompile "$SPACESHIP_ROOT/sections/git_diff.zsh"
spaceship::precompile "$SPACESHIP_ROOT/sections/git_hash.zsh"
spaceship::precompile "$SPACESHIP_ROOT/sections/git_commit_msg.zsh"

# ------------------------------------------------------------------------------
# Section
Expand Down
35 changes: 35 additions & 0 deletions sections/git_commit_msg.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#
# Git commit message
#

# ------------------------------------------------------------------------------
# Configuration
# ------------------------------------------------------------------------------

SPACESHIP_GIT_COMMIT_MSG_SHOW="${SPACESHIP_GIT_COMMIT_MSG_SHOW=true}"
SPACESHIP_GIT_COMMIT_MSG_ASYNC="${SPACESHIP_GIT_COMMIT_MSG_ASYNC=true}"
SPACESHIP_GIT_COMMIT_MSG_PREFIX="${SPACESHIP_GIT_COMMIT_MSG_PREFIX=" 📓️ "}"
SPACESHIP_GIT_COMMIT_MSG_SUFFIX="${SPACESHIP_GIT_COMMIT_MSG_SUFFIX=""}"
SPACESHIP_GIT_COMMIT_MSG_COLOR="${SPACESHIP_GIT_COMMIT_MSG_COLOR="black"}"

# ------------------------------------------------------------------------------
# Section
# ------------------------------------------------------------------------------

spaceship_git_commit_msg() {
[[ $SPACESHIP_GIT_COMMIT_MSG_SHOW == false ]] && return

spaceship::is_git || return

# vcs_info

local git_commit_msg=$(command git show-branch --no-name HEAD)

# Get first line of last commit message
if [[ -n $git_commit_msg ]]; then
# Commit prefixes are colorized
spaceship::section \
--color "$SPACESHIP_GIT_COMMIT_MSG_COLOR" \
"$SPACESHIP_GIT_COMMIT_MSG_PREFIX$git_commit_msg$SPACESHIP_GIT_COMMIT_MSG_SUFFIX"
fi
}
35 changes: 35 additions & 0 deletions sections/git_diff.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#
# Prints line counts of diff (added and removed)
#

# ------------------------------------------------------------------------------
# Configuration
# ------------------------------------------------------------------------------

SPACESHIP_GIT_DIFFSTAT_SHOW="${SPACESHIP_GIT_DIFFSTAT_SHOW=true}"
SPACESHIP_GIT_DIFFSTAT_ASYNC="${SPACESHIP_GIT_DIFFSTAT_ASYNC=false}"
SPACESHIP_GIT_DIFFSTAT_PREFIX="${SPACESHIP_GIT_DIFFSTAT_PREFIX=""}"
SPACESHIP_GIT_DIFFSTAT_SUFFIX="${SPACESHIP_GIT_DIFFSTAT_SUFFIX=""}"

# ------------------------------------------------------------------------------
# Section
# ------------------------------------------------------------------------------

spaceship_git_diff() {
[[ $SPACESHIP_GIT_DIFFSTAT_SHOW == false ]] && return

spaceship::is_git || return

# Shows count of lines added in green (with '+' prefix) and lines removed in red (with '-' prefix)
# e.g. "+21 -7"

git_diffstat=$(command git diff --staged --numstat HEAD | awk 'NF==3 {added+=$1; removed+=$2} END {printf("\x1B[32m+%d\x1B[0m \x1B[31m-%d\x1B[0m\n", added, removed)}')

# Only show the diffstat if there are files staged.
if ! git diff-index --quiet HEAD; then
# Diffstats are colorized, so no color is necessary.
spaceship::section \
'' \
"$SPACESHIP_GIT_DIFFSTAT_PREFIX$git_diffstat$SPACESHIP_GIT_DIFFSTAT_SUFFIX"
fi
}
31 changes: 31 additions & 0 deletions sections/git_hash.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#
# Git hash
#
# Show last git hash

# ------------------------------------------------------------------------------
# Configuration
# ------------------------------------------------------------------------------


SPACESHIP_GIT_HASH_SHOW="${SPACESHIP_GIT_HASH_SHOW=true}"
SPACESHIP_GIT_HASH_ASYNC="${SPACESHIP_GIT_HASH_ASYNC=true}"
SPACESHIP_GIT_HASH_PREFIX="${SPACESHIP_GIT_HASH_PREFIX=" ※ "}"
SPACESHIP_GIT_HASH_SUFFIX="${SPACESHIP_GIT_HASH_SUFFIX=""}"
SPACESHIP_GIT_HASH_COLOR="${SPACESHIP_GIT_HASH_COLOR="magenta"}"

# ------------------------------------------------------------------------------
# Section
# ------------------------------------------------------------------------------

spaceship_git_hash() {
[[ $SPACESHIP_GIT_HASH_SHOW == false ]] && return

spaceship::is_git || return

git_hash=$(command git rev-parse --short HEAD)

spaceship::section \
--color "$SPACESHIP_GIT_HASH_COLOR" \
"$SPACESHIP_GIT_HASH_PREFIX$git_hash$SPACESHIP_GIT_HASH_SUFFIX"
}
33 changes: 33 additions & 0 deletions sections/git_tag.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#
# Git tag
#
# Show last git tag

# ------------------------------------------------------------------------------
# Configuration
# ------------------------------------------------------------------------------


SPACESHIP_GIT_TAG_SHOW="${SPACESHIP_GIT_TAG_SHOW=true}"
SPACESHIP_GIT_TAG_ASYNC="${SPACESHIP_GIT_TAG_ASYNC=true}"
SPACESHIP_GIT_TAG_PREFIX="${SPACESHIP_GIT_TAG_PREFIX=" @"}"
SPACESHIP_GIT_TAG_SUFFIX="${SPACESHIP_GIT_TAG_SUFFIX=""}"
SPACESHIP_GIT_TAG_COLOR="${SPACESHIP_GIT_TAG_COLOR="blue"}"

# ------------------------------------------------------------------------------
# Section
# ------------------------------------------------------------------------------

spaceship_git_tag() {
[[ $SPACESHIP_GIT_TAG_SHOW == false ]] && return

spaceship::is_git || return

if [[ -n $(git tag) ]]; then
git_tag=$(command git describe --tags --abbrev=0)

spaceship::section \
--color "$SPACESHIP_GIT_TAG_COLOR" \
"$SPACESHIP_GIT_TAG_PREFIX$git_tag$SPACESHIP_GIT_TAG_SUFFIX"
fi
}