Skip to content

Instantly share code, notes, and snippets.

@vivien
Created June 5, 2012 01:39
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vivien/2871909 to your computer and use it in GitHub Desktop.
Save vivien/2871909 to your computer and use it in GitHub Desktop.
A bash_completion file for Fossil SCM, as /etc/bash_completion.d/git works for Git, providing a __fossil_ps1 bash function, and other tools.
# Bash completion support for Fossil.
# It is based on the Git Bash completion file.
# It best fits in /etc/bash_completion.d/fossil
#
# Copyright (C) 2011 Vivien Didelot <vivien@didelot.org>
# This file is distributed under the same terms as the license of the Fossil project.
#
# This file contains routine to change your PS1, and helper functions.
#
# For instance, add those options to your bashrc to customize PS1:
#
# # Customize PS1 with Fossil routines
# FOSSIL_PS1_SHOWDIRTYSTATE=1 # Add Fossil dirty state mark to PS1
# FOSSIL_PS1_SHOWSTASHSTATE=1 # Show if something is stashed
# FOSSIL_PS1_SHOWUNTRACKEDFILES=1 # Show if there're untracked files
#
# # Customize the prompt
# PS1='$(date +%R) \W$(__fossil_ps1 " $(__fossil_proj) (%s)") > '
# Tests if it is an opened Fossil repository.
__fossil_is_opened ()
{
test -e ./_FOSSIL_
}
# Returns specific info from the Fossil info command according to the first argument.
__fossil_info ()
{
(($#)) || return
if __fossil_is_opened ; then
while read key value ; do
if [[ "$key" == "$1:" ]] ; then
echo "$value"
return
fi
done < <(fossil info)
fi
}
# Returns the Fossil project name.
__fossil_proj ()
{
__fossil_info "project-name"
}
# Returns the Fossil repository file.
__fossil_repo ()
{
__fossil_info "repository"
}
# Checks if the source tree contains changes.
__fossil_is_dirty ()
{
if __fossil_is_opened ; then
[[ -n `fossil changes` ]]
fi
}
# Returns the list of untracked files.
__fossil_untracked_files ()
{
if __fossil_is_opened ; then
local r e
r="`__fossil_repo`"
e="_FOSSIL_"
if [[ "`pwd`" == "`dirname "$r"`" ]] ; then
e="$e|`basename "$r"`"
fi
comm -13 <(fossil ls | sort) <(find -type f | sed 's!^\./!!' | sort) | grep -vE "$e"
fi
}
# Checks if the source tree have untracked files.
__fossil_has_untracked_files ()
{
[[ -n `__fossil_untracked_files` ]]
}
# Checks if the source tree has stashes.
__fossil_has_stashes ()
{
[[ "`fossil stash list`" != "empty stash" ]]
}
# Returns the current branch name.
__fossil_current_branch ()
{
#fossil branch | grep '*' | sed 's/^\* //' # only the branch name
__fossil_info "tags" # branch name and tags
}
# Returns a formatted string for the prompt.
#
# If $FOSSIL_PS1_SHOWDIRTYSTATE is set to a non-empty value
# and if the source tree has changes, it will display a *.
# If $FOSSIL_PS1_SHOWDIRTYSTATE is set to a non-empty value
# and if the source tree has untracked files, it will display a %.
# If $FOSSIL_PS1_SHOWDIRTYSTATE is set to a non-empty value
# and if the source tree has stashes, it will display a $.
#
# The string format can be changed with the first argument.
__fossil_ps1 ()
{
if __fossil_is_opened ; then
local b d u s
b="`__fossil_current_branch`"
d=""
u=""
s=""
if [[ -n "${FOSSIL_PS1_SHOWDIRTYSTATE-}" ]] ; then
if __fossil_is_dirty ; then
d="*"
fi
fi
if [[ -n "${FOSSIL_PS1_SHOWUNTRACKEDFILES-}" ]] ; then
if __fossil_has_untracked_files ; then
u="%"
fi
fi
if [[ -n "${FOSSIL_PS1_SHOWSTASHSTATE-}" ]] ; then
if __fossil_has_stashes ; then
s='$'
fi
fi
local f="$d$u$s"
printf "${1:- (%s)}" "$b${f:+ $f}"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment