2019-03-31 17:05:09 +08:00
.. _cmd-fish_git_prompt:
2019-02-25 08:20:25 +08:00
fish_git_prompt - output git information for use in a prompt
============================================================
2019-09-17 17:59:04 +08:00
Synopsis
--------
docs synopsis: add HTML highlighing and automate manpage markup
Recent synopsis changes move from literal code blocks to
[RST line blocks]. This does not translate well to HTML: it's not
rendered in monospace, so aligment is lost. Additionally, we don't
get syntax highlighting in HTML, which adds differences to our code
samples which are highlighted.
We hard-wrap synopsis lines (like code blocks). To align continuation
lines in manpages we need [backslashes in weird places]. Combined with
the **, *, and `` markup, it's a bit hard to get the alignment right.
Fix these by moving synopsis sources back to code blocks and compute
HTML syntax highlighting and manpage markup with a custom Sphinx
extension.
The new Pygments lexer can tokenize a synopsis and assign the various
highlighting roles, which closely matches fish's syntax highlighing:
- command/keyword (dark blue)
- parameter (light blue)
- operator like and/or/not/&&/|| (cyan)
- grammar metacharacter (black)
For manpage output, we don't project the fish syntax highlighting
but follow the markup convention in GNU's man(1):
bold text type exactly as shown.
italic text replace with appropriate argument.
To make it easy to separate these two automatically, formalize that
(italic) placeholders must be uppercase; while all lowercase text is
interpreted literally (so rendered bold).
This makes manpages more consistent, see string-join(1) and and(1).
Implementation notes:
Since we want manpage formatting but Sphinx's Pygments highlighing
plugin does not support manpage output, add our custom "synopsis"
directive. This directive parses differently when manpage output is
specified. This means that the HTML and manpage build processes must
not share a cache, because the parsed doctrees are cached. Work around
this by using separate cache locations for build targets "sphinx-docs"
(which creates HTML) and "sphinx-manpages". A better solution would
be to only override Sphinx's ManualPageBuilder but that would take a
bit more code (ideally we could override ManualPageWriter but Sphinx
4.3.2 doesn't really support that).
---
Alternative solution: stick with line blocks but use roles like
:command: or :option: (or custom ones). While this would make it
possible to produce HTML that is consistent with code blocks (by adding
a bit of CSS), the source would look uglier and is harder to maintain.
(Let's say we want to add custom formatting to the [|] metacharacters
in HTML. This is much easier with the proposed patch.)
---
[RST line blocks]: https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#line-blocks
[backslashes in weird places]: https://github.com/fish-shell/fish-shell/pull/8626#discussion_r782837750
2022-01-09 22:09:46 +08:00
.. synopsis ::
fish_git_prompt
2019-09-17 17:59:04 +08:00
::
docs synopsis: add HTML highlighing and automate manpage markup
Recent synopsis changes move from literal code blocks to
[RST line blocks]. This does not translate well to HTML: it's not
rendered in monospace, so aligment is lost. Additionally, we don't
get syntax highlighting in HTML, which adds differences to our code
samples which are highlighted.
We hard-wrap synopsis lines (like code blocks). To align continuation
lines in manpages we need [backslashes in weird places]. Combined with
the **, *, and `` markup, it's a bit hard to get the alignment right.
Fix these by moving synopsis sources back to code blocks and compute
HTML syntax highlighting and manpage markup with a custom Sphinx
extension.
The new Pygments lexer can tokenize a synopsis and assign the various
highlighting roles, which closely matches fish's syntax highlighing:
- command/keyword (dark blue)
- parameter (light blue)
- operator like and/or/not/&&/|| (cyan)
- grammar metacharacter (black)
For manpage output, we don't project the fish syntax highlighting
but follow the markup convention in GNU's man(1):
bold text type exactly as shown.
italic text replace with appropriate argument.
To make it easy to separate these two automatically, formalize that
(italic) placeholders must be uppercase; while all lowercase text is
interpreted literally (so rendered bold).
This makes manpages more consistent, see string-join(1) and and(1).
Implementation notes:
Since we want manpage formatting but Sphinx's Pygments highlighing
plugin does not support manpage output, add our custom "synopsis"
directive. This directive parses differently when manpage output is
specified. This means that the HTML and manpage build processes must
not share a cache, because the parsed doctrees are cached. Work around
this by using separate cache locations for build targets "sphinx-docs"
(which creates HTML) and "sphinx-manpages". A better solution would
be to only override Sphinx's ManualPageBuilder but that would take a
bit more code (ideally we could override ManualPageWriter but Sphinx
4.3.2 doesn't really support that).
---
Alternative solution: stick with line blocks but use roles like
:command: or :option: (or custom ones). While this would make it
possible to produce HTML that is consistent with code blocks (by adding
a bit of CSS), the source would look uglier and is harder to maintain.
(Let's say we want to add custom formatting to the [|] metacharacters
in HTML. This is much easier with the proposed patch.)
---
[RST line blocks]: https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#line-blocks
[backslashes in weird places]: https://github.com/fish-shell/fish-shell/pull/8626#discussion_r782837750
2022-01-09 22:09:46 +08:00
function fish_prompt
printf '%s' $PWD (fish_git_prompt) ' $ '
end
2019-09-17 17:59:04 +08:00
2021-12-18 07:16:47 +08:00
2019-02-25 08:20:25 +08:00
Description
-----------
2019-04-11 23:51:00 +08:00
The `` fish_git_prompt `` function displays information about the current git repository, if any.
2019-02-25 08:20:25 +08:00
2019-04-11 23:51:00 +08:00
`Git <https://git-scm.com> `_ must be installed.
2019-02-25 08:20:25 +08:00
2022-09-24 01:57:49 +08:00
There are numerous customization options, which can be controlled with git options or fish variables. git options, where available, take precedence over the fish variable with the same function. git options can be set on a per-repository or global basis. git options can be set with the `` git config `` command, while fish variables can be set as usual with the :doc: `set <set>` command.
2019-02-25 08:20:25 +08:00
2022-10-22 02:22:20 +08:00
Boolean options (those which enable or disable something) understand "1", "yes" or "true" to mean true and every other value to mean false.
- `` $__fish_git_prompt_show_informative_status `` or the git option `` bash.showInformativeStatus `` can be set to 1, true or yes to enable the "informative" display, which will show a large amount of information - the number of dirty files, unpushed/unpulled commits, and more.
2022-09-17 19:38:33 +08:00
In large repositories, this can take a lot of time, so you may wish to disable it in these repositories with `` git config --local bash.showInformativeStatus false `` . It also changes the characters the prompt uses to less plain ones (`` ✚ `` instead of `` * `` for the dirty state for example) , and if you are only interested in that, set `` $__fish_git_prompt_use_informative_chars `` instead.
2022-05-30 23:36:41 +08:00
Because counting untracked files requires a lot of time, the number of untracked files is only shown if enabled via `` $__fish_git_prompt_showuntrackedfiles `` or the git option `` bash.showUntrackedFiles `` .
2019-03-20 04:07:49 +08:00
2022-10-22 02:22:20 +08:00
- `` $__fish_git_prompt_showdirtystate `` or the git option `` bash.showDirtyState `` can be set to 1, true or yes to show if the repository is "dirty", i.e. has uncommitted changes.
2019-02-25 08:20:25 +08:00
2022-10-22 02:22:20 +08:00
- `` $__fish_git_prompt_showuntrackedfiles `` or the git option `` bash.showUntrackedFiles `` can be set to 1, true or yes to show if the repository has untracked files (that aren't ignored).
2019-02-25 08:20:25 +08:00
2019-08-24 01:34:33 +08:00
- `` $__fish_git_prompt_showupstream `` can be set to a list of values to determine how changes between HEAD and upstream are shown:
2019-02-25 08:20:25 +08:00
2019-08-20 09:56:23 +08:00
`` auto ``
summarize the difference between HEAD and its upstream
2019-04-11 23:51:00 +08:00
`` verbose ``
show number of commits ahead/behind (+/-) upstream
`` name ``
if verbose, then also show the upstream abbrev name
`` informative ``
similar to verbose, but shows nothing when equal - this is the default if informative status is enabled.
`` git ``
always compare HEAD to @{upstream}
`` svn ``
always compare HEAD to your SVN upstream
`` none ``
disables (useful with informative status)
2019-02-25 08:20:25 +08:00
2022-10-22 02:22:20 +08:00
- `` $__fish_git_prompt_showstashstate `` can be set to 1, true or yes to display the state of the stash.
2019-02-25 08:20:25 +08:00
2019-03-29 05:11:28 +08:00
- `` $__fish_git_prompt_shorten_branch_len `` can be set to the number of characters that the branch name will be shortened to.
2019-02-25 08:20:25 +08:00
2019-08-20 10:03:22 +08:00
- `` $__fish_git_prompt_describe_style `` can be set to one of the following styles to describe the current HEAD:
2019-02-25 08:20:25 +08:00
2019-04-11 23:51:00 +08:00
`` contains ``
relative to newer annotated tag, such as `` (v1.6.3.2~35) ``
`` branch ``
relative to newer tag or branch, such as `` (master~4) ``
`` describe ``
relative to older annotated tag, such as `` (v1.6.3.1-13-gdd42c2f) ``
`` default ``
2020-09-18 22:32:28 +08:00
an exactly matching tag (`` (develop) `` )
If none of these apply, the commit SHA shortened to 8 characters is used.
2019-02-25 08:20:25 +08:00
2022-10-22 02:22:20 +08:00
- `` $__fish_git_prompt_showcolorhints `` can be set to 1, true or yes to enable coloring for the branch name and status symbols.
2019-02-25 08:20:25 +08:00
2019-06-25 21:06:26 +08:00
A number of variables set characters and color used as indicators. Many of these have a different default if used with informative status enabled, or `` $__fish_git_prompt_use_informative_chars `` set. The usual default is given first, then the informative default (if it is different). If no default for the colors is given, they default to `` $__fish_git_prompt_color `` .
2019-03-20 04:07:49 +08:00
2020-10-27 02:19:05 +08:00
- `` $__fish_git_prompt_char_stateseparator `` (' ', `` | `` ) - the character to be used between the state characters
2020-09-18 22:32:28 +08:00
- `` $__fish_git_prompt_color `` (no default)
- `` $__fish_git_prompt_color_prefix `` - the color of the `` ( `` prefix
- `` $__fish_git_prompt_color_suffix `` - the color of the `` ) `` suffix
- `` $__fish_git_prompt_color_bare `` - the color to use for a bare repository - one without a working tree
- `` $__fish_git_prompt_color_merging `` - the color when a merge/rebase/revert/bisect or cherry-pick is in progress
2019-02-25 08:20:25 +08:00
2021-04-04 22:46:14 +08:00
- `` $__fish_git_prompt_char_cleanstate `` (✔ in informative mode) - the character to be used when nothing else applies
- `` $__fish_git_prompt_color_cleanstate `` (no default)
2019-03-20 04:07:49 +08:00
2019-04-11 23:51:00 +08:00
Variables used with `` showdirtystate `` :
2019-03-29 05:11:28 +08:00
2020-09-18 22:32:28 +08:00
- `` $__fish_git_prompt_char_dirtystate `` (`*` , ✚) - the number of "dirty" changes, i.e. unstaged files with changes
- `` $__fish_git_prompt_char_invalidstate `` (#, ✖) - the number of "unmerged" changes, e.g. additional changes to already added files
- `` $__fish_git_prompt_char_stagedstate `` (+, ●) - the number of staged files without additional changes
2019-03-29 05:11:28 +08:00
- `` $__fish_git_prompt_color_dirtystate `` (red with showcolorhints, same as color_flags otherwise)
- `` $__fish_git_prompt_color_invalidstate ``
- `` $__fish_git_prompt_color_stagedstate `` (green with showcolorhints, color_flags otherwise)
2019-03-20 04:07:49 +08:00
2019-04-11 23:51:00 +08:00
Variables used with `` showstashstate `` :
2019-03-29 05:11:28 +08:00
- `` $__fish_git_prompt_char_stashstate `` (`` $ `` , ⚑)
- `` $__fish_git_prompt_color_stashstate `` (same as color_flags)
2019-03-20 04:07:49 +08:00
2019-04-11 23:51:00 +08:00
Variables used with `` showuntrackedfiles `` :
2019-03-29 05:11:28 +08:00
2020-09-18 22:32:28 +08:00
- `` $__fish_git_prompt_char_untrackedfiles `` (%, …) - the symbol for untracked files
2019-03-29 05:11:28 +08:00
- `` $__fish_git_prompt_color_untrackedfiles `` (same as color_flags)
2019-03-20 04:07:49 +08:00
2019-04-11 23:51:00 +08:00
Variables used with `` showupstream `` (also implied by informative status):
2019-03-29 05:11:28 +08:00
2020-09-18 22:32:28 +08:00
- `` $__fish_git_prompt_char_upstream_ahead `` (>, ↑) - the character for the commits this repository is ahead of upstream
- `` $__fish_git_prompt_char_upstream_behind `` (<, ↓) - the character for the commits this repository is behind upstream
- `` $__fish_git_prompt_char_upstream_diverged `` (<>) - the symbol if this repository is both ahead and behind upstream
- `` $__fish_git_prompt_char_upstream_equal `` (=) - the symbol if this repo is equal to upstream
2019-03-29 05:11:28 +08:00
- `` $__fish_git_prompt_char_upstream_prefix `` ('')
- `` $__fish_git_prompt_color_upstream ``
2019-02-25 08:20:25 +08:00
2019-04-11 23:51:00 +08:00
Colors used with `` showcolorhints `` :
2019-02-25 08:20:25 +08:00
2021-04-04 22:44:01 +08:00
- `` $__fish_git_prompt_color_branch `` (green) - the color of the branch if nothing else applies
2020-09-18 22:32:28 +08:00
- `` $__fish_git_prompt_color_branch_detached `` (red) the color of the branch if it's detached (e.g. a commit is checked out)
2021-04-04 22:44:01 +08:00
- `` $__fish_git_prompt_color_branch_dirty `` (no default) the color of the branch if it's dirty and not detached
- `` $__fish_git_prompt_color_branch_staged `` (no default) the color of the branch if it just has something staged and is otherwise clean
2020-09-18 22:32:28 +08:00
- `` $__fish_git_prompt_color_flags `` (--bold blue) - the default color for dirty/staged/stashed/untracked state
2019-03-29 05:11:28 +08:00
2019-04-11 23:51:00 +08:00
Note that all colors can also have a corresponding `` _done `` color. For example, the contents of `` $__fish_git_prompt_color_upstream_done `` is printed right _after_ the upstream.
2019-02-25 08:20:25 +08:00
2022-09-24 01:57:49 +08:00
See also :doc: `fish_vcs_prompt <fish_vcs_prompt>` , which will call all supported version control prompt functions, including git, Mercurial and Subversion.
2019-02-25 08:20:25 +08:00
Example
--------
A simple prompt that displays git info::
function fish_prompt
2019-03-20 04:07:49 +08:00
# ...
2019-02-25 08:20:25 +08:00
set -g __fish_git_prompt_showupstream auto
printf '%s %s$' $PWD (fish_git_prompt)
end