Skip to content

Commit

Permalink
Add git_staged_files() and git_staged_lines()
Browse files Browse the repository at this point in the history
I considered making these when I did the overall VCS rework, but thought
that since the default theme does not use it, it was pointless. But now
that a user has requested it, why not.

Fixes #644
  • Loading branch information
Rycieos committed Jan 11, 2021
1 parent 8bf1772 commit 9038ec8
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
36 changes: 36 additions & 0 deletions docs/functions/data/vcs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,26 @@ this.

.. versionadded:: 2.0

.. function:: _lp_vcs_staged_files() -> var:lp_vcs_staged_files

Returns ``true`` if any staged files exist in the repository. In other words,
tracked files that contain staged changes. Returns the number of staged
files.

Many VCS providers do not support staging.

.. versionadded:: 2.0

.. function:: _lp_vcs_staged_lines() -> var:lp_vcs_staged_i_lines, var:lp_vcs_staged_d_lines

Returns ``true`` if any staged lines exist in the repository. In other words,
tracked files that contain staged changes. Returns the number of staged
lines.

Many VCS providers do not support staging.

.. versionadded:: 2.0

.. function:: _lp_vcs_stash_count() -> var:lp_vcs_stash_count

Returns ``true`` if there are stashes the repository. Returns the
Expand Down Expand Up @@ -438,6 +458,22 @@ Git

.. versionadded:: 2.0

.. function:: _lp_git_staged_files() -> var:lp_vcs_staged_files

Returns ``true`` if any staged files exist in the repository. In other words,
tracked files that contain staged changes. Returns the number of staged
files.

.. versionadded:: 2.0

.. function:: _lp_git_staged_lines() -> var:lp_vcs_staged_i_lines, var:lp_vcs_staged_d_lines

Returns ``true`` if any staged lines exist in the repository. In other words,
tracked files that contain staged changes. Returns the number of staged
lines.

.. versionadded:: 2.0

.. function:: _lp_git_stash_count() -> var:lp_vcs_stash_count

Returns ``true`` if there are stashes the repository. Returns the
Expand Down
10 changes: 10 additions & 0 deletions docs/functions/internal.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ Git

.. versionadded:: 2.0

.. function:: __lp_git_diff_shortstat_staged() -> var:_lp_git_diff_shortstat_staged

Returns the output of a ``git diff --shortstat`` command, comparing the
staging area to the HEAD commit.

The return variable is supposed to be a cache, set as local in
:func:`__lp_set_prompt`, preventing duplicate calls to ``git``.

.. versionadded:: 2.0

.. function:: __lp_git_diff_shortstat_uncommitted() -> var:_lp_git_diff_shortstat_uncommitted

Returns the output of a ``git diff --shortstat`` command, comparing the
Expand Down
39 changes: 38 additions & 1 deletion liquidprompt
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,16 @@ _lp_vcs_unstaged_lines() {
"_lp_${lp_vcs_type}_unstaged_lines" 2>/dev/null
}

# Get the number of changed files in staging compared to the last or checked out commit.
_lp_vcs_staged_files() {
"_lp_${lp_vcs_type}_staged_files" 2>/dev/null
}

# Get the number of changed lines in staging compared to the last or checked out commit.
_lp_vcs_staged_lines() {
"_lp_${lp_vcs_type}_staged_lines" 2>/dev/null
}

# GIT #

# Check if Git is enabled in Liquidprompt and the current directory is a valid
Expand Down Expand Up @@ -1576,6 +1586,33 @@ _lp_git_unstaged_lines() {
lp_vcs_unstaged_d_lines=$lp_git_diff_shortstat_d_lines
}

__lp_git_diff_shortstat_staged() {
if [[ -z ${_lp_git_diff_shortstat_staged+x} ]]; then
_lp_git_diff_shortstat_staged="$(LC_ALL=C \git diff --shortstat --cached 2>/dev/null)"
fi
}

# Get the number of changed files in staging compared to HEAD.
_lp_git_staged_files() {
__lp_git_diff_shortstat_staged

local lp_git_diff_shortstat_files
__lp_git_diff_shortstat_files "$_lp_git_diff_shortstat_staged" || return "$?"

lp_vcs_staged_files=$lp_git_diff_shortstat_files
}

# Get the number of changed lines in staging compared to HEAD.
_lp_git_staged_lines() {
__lp_git_diff_shortstat_staged

local lp_git_diff_shortstat_i_lines lp_git_diff_shortstat_d_lines
__lp_git_diff_shortstat_lines "$_lp_git_diff_shortstat_staged" || return "$?"

lp_vcs_staged_i_lines=$lp_git_diff_shortstat_i_lines
lp_vcs_staged_d_lines=$lp_git_diff_shortstat_d_lines
}

# MERCURIAL #
# Note that Mercurial has no staging area

Expand Down Expand Up @@ -2554,7 +2591,7 @@ __lp_set_prompt() {
fi

# Localize cache data variables
local _lp_git_diff_shortstat_uncommitted _lp_git_diff_shortstat_unstaged
local _lp_git_diff_shortstat_uncommitted _lp_git_diff_shortstat_unstaged _lp_git_diff_shortstat_staged

# if change of working directory
if [[ "${LP_OLD_PWD-}" != "LP:$PWD" ]]; then
Expand Down

0 comments on commit 9038ec8

Please sign in to comment.