Skip to content

Commit

Permalink
Fix slowdown from undefined VCS functions
Browse files Browse the repository at this point in the history
Without these functions defined, each prompt invocation would force the
shell to check if the command was valid, and generate an error message
if it was not. We know that it won't be, so we can shortcut that by
setting it as such.

More importantly, if the Bash function command_not_found_handle() is
set, it will be executed for each function that is not set. For Git or
HG, this is only one. But for SVN, it is enough to lock the prompt for a
while.

Fixes #696
  • Loading branch information
Rycieos committed Feb 8, 2022
1 parent 8778261 commit 1a0fc6e
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 31 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [2.0.4] - 2022-02-07
### Changed
- **hg**: `_lp_hg_commits_off_remote()` returns `2` instead of `3` ([#696])
- **vcs**: Unsupported VCS functions defined as empty functions ([#696])

## [2.0.3] - 2021-05-30
### Fixed
- **hg**: Stash count erroring with no function defined ([#671])
Expand Down Expand Up @@ -489,7 +494,9 @@ for help.

## [1.0] - 2012-08-10 - nojhan

[Unreleased]: https://github.com/nojhan/liquidprompt/compare/v2.0.2...master
[Unreleased]: https://github.com/nojhan/liquidprompt/compare/v2.0.4...master
[2.0.4]: https://github.com/nojhan/liquidprompt/releases/tag/v2.0.4
[2.0.3]: https://github.com/nojhan/liquidprompt/releases/tag/v2.0.3
[2.0.2]: https://github.com/nojhan/liquidprompt/releases/tag/v2.0.2
[2.0.1]: https://github.com/nojhan/liquidprompt/releases/tag/v2.0.1
[2.0.0]: https://github.com/nojhan/liquidprompt/releases/tag/v2.0.0
Expand Down Expand Up @@ -640,6 +647,7 @@ for help.
[#658]: https://github.com/nojhan/liquidprompt/issues/658
[#670]: https://github.com/nojhan/liquidprompt/issues/670
[#671]: https://github.com/nojhan/liquidprompt/pull/671
[#696]: https://github.com/nojhan/liquidprompt/issues/696

[0200b99]: https://github.com/nojhan/liquidprompt/commit/0200b99ebd8485ba8ba2c91da7703e87c40ec15d
[0234a58]: https://github.com/nojhan/liquidprompt/commit/0234a581d023fb6c40e5339f6dcbd619a33b4553
Expand Down
10 changes: 2 additions & 8 deletions docs/functions/data/vcs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,8 @@ this.

.. note::

All following generic functions can exit with return codes higher than their
normal disabled exit code. If the active VCS does not support a feature, the
data function will not be defined, and therefore the shell will return an
error code much higher than ``2``. Compare using greater-or-equal if checking
for not supported error codes.

Unless otherwise documented, the following functions return ``0`` for good
data, ``1`` for no data, and ``2`` or higher for unsupported function.
data, ``1`` for no data, and ``2`` for unsupported function.

.. function:: _lp_vcs_bookmark() -> var:lp_vcs_bookmark

Expand Down Expand Up @@ -582,7 +576,7 @@ Mercurial

.. function:: _lp_hg_commits_off_remote()

Returns ``3`` (disabled).
Returns ``2`` (disabled).

Mercurial does not keep a local copy of the remote state, so checking this
will require a connection to the remote server. This means it is often
Expand Down
108 changes: 86 additions & 22 deletions liquidprompt
Original file line number Diff line number Diff line change
Expand Up @@ -1502,78 +1502,78 @@ _lp_vcs_details_color() {
# Check if the detected VCS is enabled in Liquidprompt and the current
# directory is a valid repository of that type.
_lp_vcs_active() {
"_lp_${lp_vcs_type}_active" 2>/dev/null
"_lp_${lp_vcs_type}_active"
}

# Get the branch name of the repo in the current directory.
_lp_vcs_branch() {
"_lp_${lp_vcs_type}_branch" 2>/dev/null
"_lp_${lp_vcs_type}_branch"
}

# Get the bookmark name of the repo in the current directory.
_lp_vcs_bookmark() {
"_lp_${lp_vcs_type}_bookmark" 2>/dev/null
"_lp_${lp_vcs_type}_bookmark"
}

# Get a tag name of the repo in the current directory.
_lp_vcs_tag() {
"_lp_${lp_vcs_type}_tag" 2>/dev/null
"_lp_${lp_vcs_type}_tag"
}

# Get the current commit string for the repo in the current directory.
_lp_vcs_commit_id() {
"_lp_${lp_vcs_type}_commit_id" 2>/dev/null
"_lp_${lp_vcs_type}_commit_id"
}

# Get additional information if the repo is in a special or unusual state.
_lp_vcs_head_status() {
"_lp_${lp_vcs_type}_head_status" 2>/dev/null
"_lp_${lp_vcs_type}_head_status"
# TODO: set lp_vcs_head_details if not set?
}

# Get the number of stashes in the repo.
_lp_vcs_stash_count() {
"_lp_${lp_vcs_type}_stash_count" 2>/dev/null
"_lp_${lp_vcs_type}_stash_count"
}

# Get the number of commits ahead and behind the upstream branch.
_lp_vcs_commits_off_remote() {
"_lp_${lp_vcs_type}_commits_off_remote" 2>/dev/null
"_lp_${lp_vcs_type}_commits_off_remote"
}

# Get the number of untracked aka extra files in the repo.
_lp_vcs_untracked_files() {
"_lp_${lp_vcs_type}_untracked_files" 2>/dev/null
"_lp_${lp_vcs_type}_untracked_files"
}

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

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

# Get the number of changed files compared to staging.
_lp_vcs_unstaged_files() {
"_lp_${lp_vcs_type}_unstaged_files" 2>/dev/null
"_lp_${lp_vcs_type}_unstaged_files"
}

# Get the number of changed lines compared to staging.
_lp_vcs_unstaged_lines() {
"_lp_${lp_vcs_type}_unstaged_lines" 2>/dev/null
"_lp_${lp_vcs_type}_unstaged_lines"
}

# 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
"_lp_${lp_vcs_type}_staged_files"
}

# 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
"_lp_${lp_vcs_type}_staged_lines"
}

# GIT #
Expand All @@ -1598,6 +1598,9 @@ _lp_git_branch() {
fi
}

# Git does not support bookmarks.
_lp_git_bookmark() { return 2 ; }

# Get a tag name of the Git repo in the current directory.
_lp_git_tag() {
local tag ret
Expand Down Expand Up @@ -1793,7 +1796,6 @@ _lp_git_staged_lines() {
}

# MERCURIAL #
# Note that Mercurial has no staging area

# Check if Mercurial is enabled in Liquidprompt and the current directory is a
# valid Mercurial repository.
Expand Down Expand Up @@ -1875,10 +1877,10 @@ _lp_hg_stash_count() {
}

# https://github.com/nojhan/liquidprompt/issues/217
# return: always false (3: disabled).
# return: always false (2: disabled).
_lp_hg_commits_off_remote() {
#commits=$(\hg outgoing --no-merges 2>/dev/null | \grep -c '\(^changeset\:\)')
return 3
return 2
}

# Get the number of untracked files in the Mercurial repo.
Expand All @@ -1905,9 +1907,13 @@ _lp_hg_uncommitted_lines() {
(( lp_vcs_uncommitted_i_lines || lp_vcs_uncommitted_d_lines ))
}

# Mercurial does not support a staging area.
_lp_hg_unstaged_files() { return 2 ; }
_lp_hg_unstaged_lines() { return 2 ; }
_lp_hg_staged_files() { return 2 ; }
_lp_hg_staged_lines() { return 2 ; }

# SUBVERSION #
# Note that Subversion has no tags, stashes, or staging area. It also has no
# concept of a remote, since it is not distributed and always must be in sync.

# Check if Subversion is enabled in Liquidprompt and the current directory is a
# valid Subversion repository.
Expand Down Expand Up @@ -1940,11 +1946,29 @@ _lp_svn_branch() {
fi
}

# Subversion does not support bookmarks.
_lp_svn_bookmark() { return 2 ; }

# Subversion does not support tags. What are generally agreed upon as
# being tags are internally branches. These are returned by _lp_svn_branch().
_lp_svn_tag() { return 2 ; }

# Get the current revision number for the repo in the current directory.
_lp_svn_commit_id() {
lp_vcs_commit_id="$(\svn info --show-item revision 2>/dev/null)"
}

# Subversion does not have extra head statuses. A Subversion merge is no different
# than a manual file change, so the repository has no extra state to track.
_lp_svn_head_status() { return 2 ; }

# Subversion does not support stashes.
_lp_svn_stash_count() { return 2 ; }

# Subversion does not support remote tracking branches (as it is not a
# distributed version control system).
_lp_svn_commits_off_remote() { return 2 ; }

# Get the number of untracked files in the Subversion repo.
_lp_svn_untracked_files() {
lp_vcs_untracked_files="$(LC_ALL=C \svn status 2>/dev/null | \grep -c '^?')"
Expand Down Expand Up @@ -1976,8 +2000,13 @@ _lp_svn_uncommitted_lines() {
(( lp_vcs_uncommitted_i_lines || lp_vcs_uncommitted_d_lines ))
}

# Subversion does not support a staging area.
_lp_svn_unstaged_files() { return 2 ; }
_lp_svn_unstaged_lines() { return 2 ; }
_lp_svn_staged_files() { return 2 ; }
_lp_svn_staged_lines() { return 2 ; }

# FOSSIL #
# Note that Fossil has no staging area, bookmarks, or unique tags.

# Check if Fossil is enabled in Liquidprompt and the current directory is a
# valid Fossil repository.
Expand Down Expand Up @@ -2013,6 +2042,13 @@ _lp_fossil_branch() {
fi
}

# Fossil does not support bookmarks.
_lp_fossil_bookmark() { return 2 ; }

# Fossil does not support unique tags. Fossil tags can refer to multiple checkin IDs,
# so a matching tag is not a useful unique ID.
_lp_fossil_tag() { return 2 ; }

# Get the current full commit hash of the Fossil repo in the current directory.
_lp_fossil_commit_id() {
lp_vcs_commit_id="$(LC_ALL=C \fossil status 2>/dev/null | sed -n 's/^checkout:[[:space:]]*\([^[:space:]]*\).*/\1/p')"
Expand Down Expand Up @@ -2040,6 +2076,11 @@ _lp_fossil_stash_count() {
(( lp_vcs_stash_count ))
}

# Fossil does not support remote tracking branches. Fossil by default keeps the local
# repository in sync with the remote. Even if a user disables that, it is not possible
# to have a local and remote branch named the same not in sync.
_lp_fossil_commits_off_remote() { return 2 ; }

# Get the number of extra files in the Fossil repo.
_lp_fossil_untracked_files() {
local extras count
Expand Down Expand Up @@ -2072,8 +2113,13 @@ _lp_fossil_uncommitted_lines() {
(( lp_vcs_uncommitted_i_lines || lp_vcs_uncommitted_d_lines ))
}

# Fossil does not support a staging area.
_lp_fossil_unstaged_files() { return 2 ; }
_lp_fossil_unstaged_lines() { return 2 ; }
_lp_fossil_staged_files() { return 2 ; }
_lp_fossil_staged_lines() { return 2 ; }

# Bazaar #
# Note that Bazaar has no staging area, bookmarks, remote tracking branches, or extra statuses.

# Check if Bazaar is enabled in Liquidprompt and the current directory is a
# valid Bazaar repository. This check should be done before running any other
Expand All @@ -2094,6 +2140,10 @@ _lp_bzr_branch() {
fi
}

# Bazaar does not support bookmarks. A nick is somewhat like a bookmark, but there is
# no command to view a naked branch name, so the nick command is used for branches.
_lp_bzr_bookmark() { return 2 ; }

# Get the most recent tag that refers to the current revision.
_lp_bzr_tag() {
local tag ret eol
Expand All @@ -2111,6 +2161,10 @@ _lp_bzr_commit_id() {
lp_vcs_commit_id="$(\bzr revno 2>/dev/null)"
}

# Bazaar does not have extra head statuses. A Bazaar merge can be partially complete,
# but there is no command to test for it.
_lp_bzr_head_status() { return 2 ; }

# Get the number of Bazaar shelves in the repo.
_lp_bzr_stash_count() {
local shelves count
Expand All @@ -2132,6 +2186,10 @@ _lp_bzr_stash_count() {
(( lp_vcs_stash_count ))
}

# Bazaar does not support getting details of remote tracking branches. Bazaar does not
# keep a local copy of the remote state, so checking this would be impossible anyway.
_lp_bzr_commits_off_remote() { return 2 ; }

# Get the number of unknown files in the repo.
_lp_bzr_untracked_files() {
lp_vcs_untracked_files="$(LC_ALL=C \bzr status --short 2>/dev/null | \grep -c '^?')"
Expand Down Expand Up @@ -2160,6 +2218,12 @@ _lp_bzr_uncommitted_lines() {
(( lp_vcs_uncommitted_i_lines || lp_vcs_uncommitted_d_lines ))
}

# Bazaar does not support a staging area.
_lp_fossil_unstaged_files() { return 2 ; }
_lp_fossil_unstaged_lines() { return 2 ; }
_lp_fossil_staged_files() { return 2 ; }
_lp_fossil_staged_lines() { return 2 ; }

####################
# Wifi link status #
####################
Expand Down

0 comments on commit 1a0fc6e

Please sign in to comment.