fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Tristan Partinfish_git_prompt: rename single-letter variables for clarity4e8d2c706

master
29.9 KiB710 linesraw
1# based off of the git-prompt script that ships with git
2# hence licensed under GPL version 2 (like the rest of fish).
3#
4# Written by Lily Ballard and updated by Brian Gernhardt and fish contributors
5#
6# This is based on git's git-prompt.bash script, Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>.
7# The act of porting the code, along with any new code, are Copyright (C) 2012 Lily Ballard.
8
9function __fish_git_prompt_show_upstream --description "Helper function for fish_git_prompt"
10    set -l show_upstream $__fish_git_prompt_showupstream
11    set -l svn_prefix # For better SVN upstream information
12    set -l informative
13
14    set -l svn_url_pattern
15    set -l count
16    set -l upstream git
17    set -l verbose
18    set -l name
19
20    # Default to informative if __fish_git_prompt_show_informative_status is set
21    if contains -- "$__fish_git_prompt_show_informative_status" yes true 1
22        set informative 1
23    end
24
25    set -l svn_remote
26    # get some config options from git-config
27    command git config -z --get-regexp '^(svn-remote\..*\.url|bash\.showupstream)$' 2>/dev/null | while read -lz key value
28        switch $key
29            case bash.showupstream
30                set show_upstream $value
31                test -n "$show_upstream"
32                or return
33            case svn-remote.'*'.url
34                set svn_remote $svn_remote $value
35                # Avoid adding \| to the beginning to avoid needing #?? later
36                if test -n "$svn_url_pattern"
37                    set svn_url_pattern $svn_url_pattern"|$value"
38                else
39                    set svn_url_pattern $value
40                end
41                set upstream svn+git # default upstream is SVN if available, else git
42
43                # Save the config key (without .url) for later use
44                set -l remote_prefix (string replace -r '\.url$' '' -- $key)
45                set svn_prefix $svn_prefix $remote_prefix
46        end
47    end
48
49    # parse configuration variables
50    # and clear informative default when needed
51    for option in $show_upstream
52        switch $option
53            case git svn
54                set upstream $option
55                set -e informative
56            case verbose
57                set verbose 1
58                set -e informative
59            case informative
60                set informative 1
61            case name
62                set name 1
63            case none
64                return
65        end
66    end
67
68    # Find our upstream
69    switch $upstream
70        case git
71            set upstream '@{upstream}'
72        case svn\*
73            # get the upstream from the 'git-svn-id: …' in a commit message
74            # (git-svn uses essentially the same procedure internally)
75            set -l svn_upstream (git log --first-parent -1 --grep="^git-svn-id: \($svn_url_pattern\)" 2>/dev/null)
76            if test (count $svn_upstream) -ne 0
77                echo $svn_upstream[-1] | read -l __ svn_upstream __
78                set svn_upstream (string replace -r '@.*' '' -- $svn_upstream)
79                set -l cur_prefix
80
81                for i in (seq (count $svn_remote))
82                    set -l remote $svn_remote[$i]
83                    set -l mod_upstream (string replace "$remote" "" -- $svn_upstream)
84                    if test "$svn_upstream" != "$mod_upstream"
85                        # we found a valid remote
86                        set svn_upstream $mod_upstream
87                        set cur_prefix $svn_prefix[$i]
88                        break
89                    end
90                end
91
92                if test -z "$svn_upstream"
93                    # default branch name for checkouts with no layout:
94                    if test -n "$GIT_SVN_ID"
95                        set upstream $GIT_SVN_ID
96                    else
97                        set upstream git-svn
98                    end
99                else
100                    set upstream (string replace '/branches' '' -- $svn_upstream | string replace -a '/' '')
101
102                    # Use fetch config to fix upstream
103                    set -l fetch_val (command git config "$cur_prefix".fetch)
104                    if test -n "$fetch_val"
105                        string split -m1 : -- "$fetch_val" | read -l trunk pattern
106                        set upstream (string replace -r -- "/$trunk\$" '' $pattern) /$upstream
107                    end
108                end
109            else if test $upstream = svn+git
110                set upstream '@{upstream}'
111            end
112    end
113
114    # Find how many commits we are ahead/behind our upstream
115    set count (command git rev-list --count --left-right $upstream...HEAD 2>/dev/null | string replace \t " ")
116
117    # calculate the result
118    if test -n "$verbose"
119        # Verbose has a space by default
120        set -l prefix "$___fish_git_prompt_char_upstream_prefix"
121        # Using two underscore version to check if user explicitly set to nothing
122        if not set -q __fish_git_prompt_char_upstream_prefix
123            set prefix " "
124        end
125
126        echo $count | read -l behind ahead
127        switch "$count"
128            case '' # no upstream
129            case "0 0" # equal to upstream
130                echo "$prefix$___fish_git_prompt_char_upstream_equal"
131            case "0 *" # ahead of upstream
132                echo "$prefix$___fish_git_prompt_char_upstream_ahead$ahead"
133            case "* 0" # behind upstream
134                echo "$prefix$___fish_git_prompt_char_upstream_behind$behind"
135            case '*' # diverged from upstream
136                echo "$prefix$___fish_git_prompt_char_upstream_diverged$ahead-$behind"
137        end
138        if test -n "$count" -a -n "$name"
139            echo " "(command git rev-parse --abbrev-ref "$upstream" 2>/dev/null)
140        end
141    else if test -n "$informative"
142        echo $count | read -l behind ahead
143        switch "$count"
144            case '' # no upstream
145            case "0 0" # equal to upstream
146            case "0 *" # ahead of upstream
147                echo "$___fish_git_prompt_char_upstream_prefix$___fish_git_prompt_char_upstream_ahead$ahead"
148            case "* 0" # behind upstream
149                echo "$___fish_git_prompt_char_upstream_prefix$___fish_git_prompt_char_upstream_behind$behind"
150            case '*' # diverged from upstream
151                echo "$___fish_git_prompt_char_upstream_prefix$___fish_git_prompt_char_upstream_ahead$ahead$___fish_git_prompt_char_upstream_behind$behind"
152        end
153    else
154        switch "$count"
155            case '' # no upstream
156            case "0 0" # equal to upstream
157                echo "$___fish_git_prompt_char_upstream_prefix$___fish_git_prompt_char_upstream_equal"
158            case "0 *" # ahead of upstream
159                echo "$___fish_git_prompt_char_upstream_prefix$___fish_git_prompt_char_upstream_ahead"
160            case "* 0" # behind upstream
161                echo "$___fish_git_prompt_char_upstream_prefix$___fish_git_prompt_char_upstream_behind"
162            case '*' # diverged from upstream
163                echo "$___fish_git_prompt_char_upstream_prefix$___fish_git_prompt_char_upstream_diverged"
164        end
165    end
166
167    # For the return status
168    test "$count" = "0 0"
169end
170
171# Decide if git is safe to run.
172# On Darwin, git is pre-installed as a stub, which will pop a dialog if you run it.
173if string match -q Darwin -- (__fish_uname) && string match -q /usr/bin/git -- (command -s git) && type -q xcode-select && type -q xcrun
174    if not xcode-select --print-path &>/dev/null
175        # Only the stub git is installed.
176        # Do not try to run it.
177        function __fish_git_prompt_ready
178            return 1
179        end
180    else
181        # git is installed, but on the first run it may be very slow as xcrun needs to populate the cache.
182        # Kick it off in the background to populate the cache.
183        set -l sh (__fish_posix_shell)
184        $sh -c '( /usr/bin/git --version; touch /tmp/__fish_git_ready ) >/dev/null 2>&1 &'
185        function __fish_git_prompt_ready
186            path is /tmp/__fish_git_ready || return 1
187            # git is ready, erase the function.
188            functions -e __fish_git_prompt_ready
189            return 0
190        end
191    end
192end
193
194function fish_git_prompt --description "Prompt function for Git"
195    # If git isn't installed, there's nothing we can do
196    # Return 1 so the calling prompt can deal with it
197    if not command -sq git
198        return 1
199    end
200    # Fail if __fish_git_prompt_ready is defined and fails.
201    if functions -q __fish_git_prompt_ready && not __fish_git_prompt_ready
202        return 1
203    end
204    set -l repo_info (command git rev-parse --git-dir --git-common-dir --is-inside-git-dir --is-bare-repository --is-inside-work-tree HEAD 2>/dev/null)
205    test -n "$repo_info"
206    or return
207
208    set -l git_dir $repo_info[1]
209    set -l git_common_dir $repo_info[2]
210    set -l inside_gitdir $repo_info[3]
211    set -l bare_repo $repo_info[4]
212    set -l inside_worktree $repo_info[5]
213    set -q repo_info[6]
214    and set -l sha $repo_info[6]
215
216    set -l branch_state (__fish_git_prompt_operation_branch_bare $repo_info)
217    set -l operation $branch_state[1]
218    set -l branch $branch_state[2]
219    set -l detached $branch_state[3]
220    set -l dirtystate
221    set -l stagedstate
222    set -l invalidstate
223    set -l stashstate
224    set -l untrackedfiles
225    set -l bare_prefix $branch_state[4]
226    set -l upstream
227    set -l informative_status
228
229    set -q __fish_git_prompt_status_order
230    or set -g __fish_git_prompt_status_order stagedstate invalidstate dirtystate untrackedfiles stashstate
231
232    if not set -q ___fish_git_prompt_init
233        # This takes a while, so it only needs to be done once,
234        # and then whenever the configuration changes.
235        __fish_git_prompt_validate_chars
236        __fish_git_prompt_validate_colors
237        set -g ___fish_git_prompt_init
238    end
239
240    set -l space "$___fish_git_prompt_color$___fish_git_prompt_char_stateseparator$___fish_git_prompt_color_done"
241
242    # Use our variables as defaults, but allow overrides via the local git config.
243    # That means if neither is set, this stays empty.
244    #
245    # So "!= true" or "!= false" are useful tests if you want to do something by default.
246    set -l informative
247    set -l dirty
248    set -l untracked
249    command git config -z --get-regexp 'bash\.(showInformativeStatus|showDirtyState|showUntrackedFiles)' 2>/dev/null | while read -lz key value
250        switch $key
251            case bash.showinformativestatus
252                set informative $value
253            case bash.showdirtystate
254                set dirty $value
255            case bash.showuntrackedfiles
256                set untracked $value
257        end
258    end
259
260    # If we don't print these, there is no need to compute them. Note: For now, staged and dirty are coupled.
261    if not set -q dirty[1]
262        contains -- "$__fish_git_prompt_showdirtystate" yes true 1
263        and set dirty true
264    end
265    contains dirtystate $__fish_git_prompt_status_order || contains stagedstate $__fish_git_prompt_status_order
266    or set dirty false
267
268    if not set -q untracked[1]
269        contains -- "$__fish_git_prompt_showuntrackedfiles" yes true 1
270        and set untracked true
271    end
272    contains untrackedfiles $__fish_git_prompt_status_order
273    or set untracked false
274
275    if test true = $inside_worktree
276        # Use informative status if it has been enabled locally, or it has been
277        # enabled globally (via the fish variable) and dirty or untracked are not false.
278        #
279        # This is to allow overrides for the repository.
280        if test "$informative" = true
281            or begin
282                contains -- "$__fish_git_prompt_show_informative_status" yes true 1
283                and test "$dirty" != false
284            end
285            set informative_status (untracked=$untracked __fish_git_prompt_informative_status $git_common_dir)
286            if test -n "$informative_status"
287                set informative_status "$space$informative_status"
288            end
289        else
290            if not test "$dirty" = true; and test "$untracked" = true
291                # Only untracked, ls-files is faster.
292                command git -c core.fsmonitor= ls-files --others --exclude-standard --directory --no-empty-directory --error-unmatch -- :/ >/dev/null 2>&1
293                and set untrackedfiles 1
294            else if test "$dirty" = true
295                # With both dirty and untracked, git status is ~10% faster.
296                # With just dirty, it's ~20%.
297                set -l opt -uno
298                test "$untracked" = true; and set opt -unormal
299                # Don't use `--ignored=no`; it was introduced in Git 2.16, from January 2018
300                # Ignored files are omitted by default
301                # Renames and copies in porcelain -z output have an extra NUL-delimited
302                # field for the source path. Filter to entries starting with a valid
303                # two-char status code followed by a space to skip those bare paths.
304                set -l stat (__fish_git_prompt_status_porcelain_modulo_rename_source $opt)
305
306                set dirtystate (string match -qr '^.[ACDMRTU]' -- $stat; and echo 1)
307                if test -n "$sha"
308                    set stagedstate (string match -qr '^[ACDMRTU].' -- $stat; and echo 1)
309                else
310                    set invalidstate 1
311                end
312
313                test "$untracked" = true
314                and set untrackedfiles (string match -qr '\?\?' -- $stat; and echo 1)
315            end
316
317            if contains -- "$__fish_git_prompt_showstashstate" yes true 1
318                and test -r $git_common_dir/logs/refs/stash
319                # If we have informative status but don't want to actually
320                # *compute* the informative status, we might still count the stash.
321                if contains -- "$__fish_git_prompt_show_informative_status" yes true 1
322                    set stashstate (count < $git_common_dir/logs/refs/stash)
323                else if test -s $git_common_dir/logs/refs/stash
324                    set stashstate 1
325                end
326            end
327        end
328
329        # (showupstream has a variety of options, not just bool)
330        if set -q __fish_git_prompt_showupstream
331            or contains -- "$__fish_git_prompt_show_informative_status" yes true 1
332            set upstream (__fish_git_prompt_show_upstream)
333        end
334    end
335
336    set -l branch_color $___fish_git_prompt_color_branch
337    set -l branch_done $___fish_git_prompt_color_branch_done
338    if contains -- "$__fish_git_prompt_showcolorhints" yes true 1
339        if test $detached = yes
340            set branch_color $___fish_git_prompt_color_branch_detached
341            set branch_done $___fish_git_prompt_color_branch_detached_done
342        else if test -n "$dirtystate$untrackedfiles"; and set -q __fish_git_prompt_color_branch_dirty
343            set branch_color (set_color $__fish_git_prompt_color_branch_dirty)
344            set branch_done (set_color $__fish_git_prompt_color_branch_dirty_done)
345        else if test -n "$stagedstate"; and set -q __fish_git_prompt_color_branch_staged
346            set branch_color (set_color $__fish_git_prompt_color_branch_staged)
347            set branch_done (set_color $__fish_git_prompt_color_branch_staged_done)
348        end
349    end
350
351    set -l flags ""
352    for i in $__fish_git_prompt_status_order
353        if test -n "$$i"
354            set -l color_var ___fish_git_prompt_color_$i
355            set -l color_done_var ___fish_git_prompt_color_{$i}_done
356            set -l symbol_var ___fish_git_prompt_char_$i
357
358            set -l color $$color_var
359            set -l color_done $$color_done_var
360            set -l symbol $$symbol_var
361
362            # If we count some things, print the number
363            # This won't be done if we actually do the full informative status
364            # because that does the printing.
365            contains -- "$__fish_git_prompt_show_informative_status" yes true 1
366            and set flags "$flags$color$symbol$$i$color_done"
367            or set flags "$flags$color$symbol$color_done"
368        end
369    end
370
371    set branch (string replace refs/heads/ '' -- $branch)
372    # Strip control characters before display.
373    set branch (string replace -ra '[[:cntrl:]]' '' -- $branch)
374    set operation (string replace -ra '[[:cntrl:]]' '' -- $operation)
375    if string match -qr '^\d+$' "$__fish_git_prompt_shorten_branch_len"
376        set -q __fish_git_prompt_shorten_branch_char_suffix
377        and set -l char -c "$__fish_git_prompt_shorten_branch_char_suffix"
378        set branch (string shorten -m "$__fish_git_prompt_shorten_branch_len" $char -- "$branch")
379    end
380    if test -n "$branch"
381        set branch "$branch_color$branch$branch_done"
382        if test -z "$dirtystate$untrackedfiles$stagedstate"; and test -n "$___fish_git_prompt_char_cleanstate"
383            and not contains -- "$__fish_git_prompt_show_informative_status" yes true 1
384            set branch "$branch$___fish_git_prompt_color_cleanstate$___fish_git_prompt_char_cleanstate$___fish_git_prompt_color_cleanstate_done"
385        end
386    end
387    if test -n "$bare_prefix"
388        set bare_prefix "$___fish_git_prompt_color_bare$bare_prefix$___fish_git_prompt_color_bare_done"
389    end
390    if test -n "$operation"
391        set operation "$___fish_git_prompt_color_merging$operation$___fish_git_prompt_color_merging_done"
392    end
393    if test -n "$upstream"
394        set upstream "$___fish_git_prompt_color_upstream$upstream$___fish_git_prompt_color_upstream_done"
395    end
396
397    # Formatting
398    # If we have state, a bare repo or upstream difference, add a separator.
399    # merging is already separate.
400    if test -n "$flags$bare_prefix$upstream"
401        set flags "$space$flags"
402    end
403    set -l format $argv[1]
404    if test -z "$format"
405        set format " (%s)"
406    end
407
408    printf "%s$format%s" "$___fish_git_prompt_color_prefix" "$___fish_git_prompt_color_prefix_done$bare_prefix$branch$flags$operation$upstream$informative_status$___fish_git_prompt_color_suffix" "$___fish_git_prompt_color_suffix_done"
409end
410
411### helper functions
412
413function __fish_git_prompt_status_porcelain_modulo_rename_source
414    set -l skip false
415    for line in (git -c core.fsmonitor= status --porcelain -z $argv | string split0)
416        if $skip
417            set skip false
418            continue
419        end
420        printf %s\n $line
421        if string match -rq -- ^[RC] $line
422            set skip true
423        end
424    end
425end
426
427function __fish_git_prompt_informative_status
428    set -l stashstate 0
429    set -l stashfile "$argv[1]/logs/refs/stash"
430    if contains -- "$__fish_git_prompt_showstashstate" yes true 1; and test -e "$stashfile"
431        set stashstate (count < $stashfile)
432    end
433
434    # If we're not told to show untracked files, we don't.
435    # If we are, we still use the "normal" mode because it's a lot faster,
436    # and it's unlikely anyone cares about the number of files if it's *all* of the files
437    # in that directory.
438    set -l untr -uno
439    test "$untracked" = true
440    and set untr -unormal
441
442    # Use git status --porcelain.
443    # The v2 format is better, but we don't actually care in this case.
444    # Renames and copies in porcelain -z output have an extra NUL-delimited
445    # field for the source path. Filter to entries starting with a valid
446    # two-char status code followed by a space to skip those bare paths.
447    set -l stats (__fish_git_prompt_status_porcelain_modulo_rename_source $untr)
448    set -l invalidstate (string match -r '^UU' $stats | count)
449    set -l stagedstate (string match -r '^[ACDMRT].' $stats | count)
450    set -l dirtystate (string match -r '^.[ACDMRT]' $stats | count)
451    set -l untrackedfiles (string match -r '^\?\?' $stats | count)
452
453    set -l info
454
455    if test "$dirtystate$invalidstate$stagedstate$untrackedfiles$stashstate" = 00000
456        if test -n "$___fish_git_prompt_char_cleanstate"
457            set info $___fish_git_prompt_color_cleanstate$___fish_git_prompt_char_cleanstate$___fish_git_prompt_color_cleanstate_done
458        end
459    else
460        for i in $__fish_git_prompt_status_order
461            if test $$i != 0
462                set -l color_var ___fish_git_prompt_color_$i
463                set -l color_done_var ___fish_git_prompt_color_{$i}_done
464                set -l symbol_var ___fish_git_prompt_char_$i
465
466                set -l color $$color_var
467                set -l color_done $$color_done_var
468                set -l symbol $$symbol_var
469
470                set -l count
471
472                if not set -q __fish_git_prompt_hide_$i
473                    set count $$i
474                end
475
476                set info "$info$color$symbol$count$color_done"
477            end
478        end
479    end
480
481    echo $info
482
483end
484
485# Keeping these together avoids many duplicated checks
486function __fish_git_prompt_operation_branch_bare --description "fish_git_prompt helper, returns the current Git operation and branch"
487    # This function is passed the full repo_info array
488    set -l git_dir $argv[1]
489    set -l inside_gitdir $argv[3]
490    set -l bare_repo $argv[4]
491    set -q argv[6]
492    and set -l sha $argv[6]
493
494    set -l branch
495    set -l operation
496    set -l detached no
497    set -l bare
498    set -l step
499    set -l total
500
501    if test -d $git_dir/rebase-merge
502        set branch (cat $git_dir/rebase-merge/head-name 2>/dev/null)
503        set step (cat $git_dir/rebase-merge/msgnum 2>/dev/null)
504        set total (cat $git_dir/rebase-merge/end 2>/dev/null)
505        if test -f $git_dir/rebase-merge/interactive
506            set operation "|REBASE-i"
507        else
508            set operation "|REBASE-m"
509        end
510    else
511        if test -d $git_dir/rebase-apply
512            set step (cat $git_dir/rebase-apply/next 2>/dev/null)
513            set total (cat $git_dir/rebase-apply/last 2>/dev/null)
514            if test -f $git_dir/rebase-apply/rebasing
515                set branch (cat $git_dir/rebase-apply/head-name 2>/dev/null)
516                set operation "|REBASE"
517            else if test -f $git_dir/rebase-apply/applying
518                set operation "|AM"
519            else
520                set operation "|AM/REBASE"
521            end
522        else if test -f $git_dir/MERGE_HEAD
523            set operation "|MERGING"
524        else if test -f $git_dir/CHERRY_PICK_HEAD
525            set operation "|CHERRY-PICKING"
526        else if test -f $git_dir/REVERT_HEAD
527            set operation "|REVERTING"
528        else if test -f $git_dir/BISECT_LOG
529            set operation "|BISECTING"
530        end
531    end
532
533    if test -n "$step" -a -n "$total"
534        set operation "$operation $step/$total"
535    end
536
537    if test -z "$branch"
538        if not set branch (command git symbolic-ref HEAD 2>/dev/null)
539            set detached yes
540            set branch (switch "$__fish_git_prompt_describe_style"
541                        case contains
542                            command git describe --contains HEAD
543                        case branch
544                            command git describe --contains --all HEAD
545                        case describe
546                            command git describe HEAD
547                        case default '*'
548                            command git describe --tags --exact-match HEAD
549                        end 2>/dev/null)
550            if test $status -ne 0
551                # Shorten the sha ourselves to 8 characters - this should be good for most repositories,
552                # and even for large ones it should be good for most commits
553                # No need for an ellipsis.
554                if set -q sha
555                    set branch (string shorten -m8 -c "" -- $sha)
556                else
557                    set branch unknown
558                end
559            end
560            set branch "($branch)"
561        end
562    end
563
564    if test true = $inside_gitdir
565        if test true = $bare_repo
566            set bare "BARE:"
567        else
568            # Let user know they're inside the git dir of a non-bare repo
569            set branch "GIT_DIR!"
570        end
571    end
572
573    echo $operation
574    echo $branch
575    echo $detached
576    echo $bare
577end
578
579function __fish_git_prompt_set_char
580    set -l user_variable_name "$argv[1]"
581    set -l char $argv[2]
582
583    if set -q argv[3]
584        and begin
585            contains -- "$__fish_git_prompt_show_informative_status" yes true 1
586            or contains -- "$__fish_git_prompt_use_informative_chars" yes true 1
587        end
588        set char $argv[3]
589    end
590
591    set -l variable _$user_variable_name
592    set -l variable_done "$variable"_done
593
594    if not set -q $variable
595        set -g $variable (set -q $user_variable_name; and echo $$user_variable_name; or echo $char)
596    end
597end
598
599function __fish_git_prompt_validate_chars --description "fish_git_prompt helper, checks char variables"
600    # cleanstate is only defined with actual informative status.
601    contains -- "$__fish_git_prompt_show_informative_status" yes true 1
602    and __fish_git_prompt_set_char __fish_git_prompt_char_cleanstate '✔'
603    or __fish_git_prompt_set_char __fish_git_prompt_char_cleanstate ''
604
605    __fish_git_prompt_set_char __fish_git_prompt_char_dirtystate '*' '✚'
606    __fish_git_prompt_set_char __fish_git_prompt_char_invalidstate '#' '✖'
607    __fish_git_prompt_set_char __fish_git_prompt_char_stagedstate '+' '●'
608    __fish_git_prompt_set_char __fish_git_prompt_char_stashstate '$' '⚑'
609    __fish_git_prompt_set_char __fish_git_prompt_char_stateseparator ' ' '|'
610    __fish_git_prompt_set_char __fish_git_prompt_char_untrackedfiles '%' '…'
611    __fish_git_prompt_set_char __fish_git_prompt_char_upstream_ahead '>' '↑'
612    __fish_git_prompt_set_char __fish_git_prompt_char_upstream_behind '<' '↓'
613    __fish_git_prompt_set_char __fish_git_prompt_char_upstream_diverged '<>' '↓↑'
614    __fish_git_prompt_set_char __fish_git_prompt_char_upstream_equal '='
615    __fish_git_prompt_set_char __fish_git_prompt_char_upstream_prefix ''
616
617end
618
619function __fish_git_prompt_set_color
620    set -l user_variable_name "$argv[1]"
621
622    set -l default
623    set -l default_done
624
625    switch (count $argv)
626        case 1 # No defaults given, use prompt color
627            set default $___fish_git_prompt_color
628            set default_done $___fish_git_prompt_color_done
629        case 2 # One default given, use "--reset" for done
630            set default "$argv[2]"
631            set default_done (set_color --reset)
632        case 3 # Both defaults given
633            set default "$argv[2]"
634            set default_done "$argv[3]"
635    end
636
637    set -l variable _$user_variable_name
638    set -l variable_done "$variable"_done
639
640    if not set -q $variable
641        if test -n "$$user_variable_name"
642            set -g $variable (set_color $$user_variable_name)
643            set -g $variable_done (set_color --reset)
644        else
645            set -g $variable $default
646            set -g $variable_done $default_done
647        end
648    end
649end
650
651function __fish_git_prompt_validate_colors --description "fish_git_prompt helper, checks color variables"
652
653    # Base color defaults to nothing (must be done first)
654    __fish_git_prompt_set_color __fish_git_prompt_color '' ''
655
656    # Normal colors
657    __fish_git_prompt_set_color __fish_git_prompt_color_prefix
658    __fish_git_prompt_set_color __fish_git_prompt_color_suffix
659    __fish_git_prompt_set_color __fish_git_prompt_color_bare
660    __fish_git_prompt_set_color __fish_git_prompt_color_merging
661    __fish_git_prompt_set_color __fish_git_prompt_color_cleanstate
662    __fish_git_prompt_set_color __fish_git_prompt_color_invalidstate
663    __fish_git_prompt_set_color __fish_git_prompt_color_upstream
664
665    # Colors with defaults with showcolorhints
666    if contains -- "$__fish_git_prompt_showcolorhints" yes true 1
667        __fish_git_prompt_set_color __fish_git_prompt_color_flags (set_color --bold blue)
668        __fish_git_prompt_set_color __fish_git_prompt_color_branch (set_color green)
669        __fish_git_prompt_set_color __fish_git_prompt_color_dirtystate (set_color red)
670        __fish_git_prompt_set_color __fish_git_prompt_color_stagedstate (set_color green)
671    else
672        __fish_git_prompt_set_color __fish_git_prompt_color_flags
673        __fish_git_prompt_set_color __fish_git_prompt_color_branch
674        __fish_git_prompt_set_color __fish_git_prompt_color_dirtystate $___fish_git_prompt_color_flags $___fish_git_prompt_color_flags_done
675        __fish_git_prompt_set_color __fish_git_prompt_color_stagedstate $___fish_git_prompt_color_flags $___fish_git_prompt_color_flags_done
676    end
677
678    # Branch_detached has a default, but is only used with showcolorhints
679    __fish_git_prompt_set_color __fish_git_prompt_color_branch_detached (set_color red)
680
681    # Colors that depend on flags color
682    __fish_git_prompt_set_color __fish_git_prompt_color_stashstate $___fish_git_prompt_color_flags $___fish_git_prompt_color_flags_done
683    __fish_git_prompt_set_color __fish_git_prompt_color_untrackedfiles $___fish_git_prompt_color_flags $___fish_git_prompt_color_flags_done
684
685end
686
687function __fish_git_prompt_reset -a type -a op -a var --description "Event handler, resets prompt when functionality changes" \
688    --on-variable=__fish_git_prompt_{show_informative_status,use_informative_chars}
689    # Clear characters that have different defaults with/without informative status
690    set -e ___fish_git_prompt_char_{name,cleanstate,dirtystate,invalidstate,stagedstate,stashstate,stateseparator,untrackedfiles,upstream_ahead,upstream_behind}
691    # Clear init so we reset the chars next time.
692    set -e ___fish_git_prompt_init
693end
694
695function __fish_git_prompt_reset_color -a type -a op -a var --description "Event handler, resets prompt when any color changes" \
696    --on-variable=__fish_git_prompt_color{'',_prefix,_suffix,_bare,_merging,_cleanstate,_invalidstate,_upstream,_flags,_branch,_dirtystate,_stagedstate,_branch_detached,_stashstate,_untrackedfiles} --on-variable=__fish_git_prompt_showcolorhints
697    set -e _$var
698    set -e _{$var}_done
699    set -e ___fish_git_prompt_init
700    if contains -- $var __fish_git_prompt_color __fish_git_prompt_color_flags __fish_git_prompt_showcolorhints
701        # reset all the other colors too
702        set -e ___fish_git_prompt_color_{prefix,suffix,bare,merging,branch,dirtystate,stagedstate,invalidstate,stashstate,untrackedfiles,upstream,flags}{,_done}
703    end
704end
705
706function __fish_git_prompt_reset_char -a type -a op -a var --description "Event handler, resets prompt when any char changes" \
707    --on-variable=__fish_git_prompt_char_{cleanstate,dirtystate,invalidstate,stagedstate,stashstate,stateseparator,untrackedfiles,upstream_ahead,upstream_behind,upstream_diverged,upstream_equal,upstream_prefix}
708    set -e ___fish_git_prompt_init
709    set -e _$var
710end