Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Copyright (C) 2015 Facebook, Inc
- # Maintained by Ryan McElroy <[email protected]>
- #
- # Inspiration and derivation from git-completion.bash by Shawn O. Pearce.
- #
- # Distributed under the GNU General Public License, version 2.0.
- #
- # ========================================================================
- #
- # Quickly determines the and emits some useful information about the state
- # of your current mercurial or git repository. Useful for PS1 prompts.
- #
- # Design goals:
- # * Useful for both git and mercurial
- # * Portable to both zsh and bash
- # * Portable to both Mac (BSD-based utils) and Linux (GNU-based utils)
- # * As fast as possible given the above constraints (few command invocations)
- # * Avoids invoking git or mercurial, which may be slow on large repositories
- #
- # To use from zsh:
- #
- # NOTE! the single quotes are important; if you use double quotes
- # then the prompt won't change when you chdir or checkout different
- # branches!
- #
- # setopt PROMPT_SUBST
- # source /path/to/scm-prompt
- # export PS1='$(_scm_prompt)$USER@%m:%~%% '
- #
- # To use from bash:
- #
- # source /path/to/scm-prompt
- # export PS1="\$(_scm_prompt)\u@\h:\W\$ "
- #
- # NOTE! You *EITHER* need to single-quote the whole thing *OR* back-slash
- # the $(...) (as above), but not both. Which one you use depends on if
- # you need the rest of your PS1 to interpolate variables.
- #
- # You may additionally pass a format-string to the scm_info command. This
- # allows you to control the format of the prompt string without interfering
- # with the prompt outside of a mercurial or git repository. For example:
- #
- # $(_scm_prompt "%s")
- #
- # The default format string is " (%s)" (note the space)
- #
- # Options: you may want to set these environment variables
- # * HOME_IS_NOT_A_REPO : Stop walking up the filesystem looking for a git or
- # hg repo at (but not including) /home instead of /. This helps when /home
- # is a remote or autofs mount point.
- # * WANT_OLD_SCM_PROMPT : Use '%s' as the formatting for the prompt instead
- # of ' (%s)'
- #
- # Notes to developers:
- #
- # * Aliases can screw up the default commands. To prevent this issue, use
- # the 'builtin' prefix for built-in shell commands (eg, 'cd' and 'echo')
- # and use the 'command' prefix for external commands that you do not want
- # to invoke aliases for (eg, 'grep', 'cut').
- #
- # =========================================================================
- #
- _find_most_relevant() {
- # We don't want to output all remote bookmarks because there can be many
- # of them. This function finds the most relevant remote bookmark using this
- # algorithm:
- # 1. If 'master' or '@' bookmark is available then output it
- # 2. Sort remote bookmarks and output the first in reverse sorted order (
- # it's a heuristic that tries to find the newest bookmark. It will work well
- # with bookmarks like 'release20160926' and 'release20161010').
- relevantbook="$(command grep -m1 -E -o "^[^/] /(master|@)$" <<< "$1")"
- if [[ -n $relevantbook ]]; then
- builtin echo $relevantbook
- return 0
- fi
- builtin echo "$(command sort -r <<< "$1" | command head -n 1)"
- }
- _hg_prompt() {
- local hg br extra
- hg="$1"
- if [[ -f "$hg/bisect.state" ]]; then
- extra="|BISECT"
- elif [[ -f "$hg/histedit-state" ]]; then
- extra="|HISTEDIT"
- elif [[ -f "$hg/graftstate" ]]; then
- extra="|GRAFT"
- elif [[ -f "$hg/unshelverebasestate" ]]; then
- extra="|UNSHELVE"
- elif [[ -f "$hg/rebasestate" ]]; then
- extra="|REBASE"
- elif [[ -d "$hg/merge" ]]; then
- extra="|MERGE"
- elif [[ -L "$hg/store/lock" ]]; then
- extra="|STORE-LOCKED"
- elif [[ -L "$hg/wlock" ]]; then
- extra="|WDIR-LOCKED"
- fi
- local dirstate="$( \
- ( [[ -f "$hg/dirstate" ]]
Add Comment
Please, Sign In to add comment