2>/dev/null

Show subversion status in shell prompt

Summary:

Adding detailed svn status info to the Bash prompt

A recent convert to version control, I’m always on the lookout for tools that diminish its attendant pitfalls. A simple one is scripting the shell prompt to include working-copy status info. A while back I picked up a simple “show Git branch” script somewhere, I forget where. More recently I’ve started using Subversion, and added this script from the ever-helpful Marc Liyanage. It covers both Git and Subversion.

Marc’s script shows the current Git branch or Subversion revision number, and adds the nice touch of adding an asterisk if there are modifications in the working copy. This wasn’t helpful for my current project, though, because it has external dependencies: svn status always shows this and hence my working copy would always show “dirty”.

So I made a new version that adds all the first-column flags from svn status to the prompt. (You can find parse_git_branch() in Marc’s article.)

parse_svn_status() {
    local REV=$(                # get svn revision number
        svn info 2>/dev/null | grep Revision | sed -e 's/Revision: //'
    )
    [ "$REV" ] || return        # stop now if not a working copy

    local STATUS=(              # create an array

        # svn status items (second column is always ' ', 'C', or 'M')
        $( svn st | grep '^[^ ][ CM]' | \

        # first column only, filter duplicates
        sed -Ee 's/^(.).*$/\1/' | awk 'x[$0]++ == 0' ) 
    )
    echo "(r$REV ${STATUS[*]})"
}

export PS1='\n\W $(parse_git_branch)$(parse_svn_status) \$ '

The result is a prompt like this:

src (r3317 X M ?) $ _

I.e., the working directory (‘src’), is at revision 3317, and has one or more external dependencies, modified files, and unversioned files.

I use Bash; I don’t think the script has any Bashisms, but YMMV.

Marc’s article includes still another alternative, based on svnversion. This would work for me also, as svnversion seems to ignore externals. It also shows a revision range for a mixed-revision working copy. As Marc points out, for a large tree it is faster. But for now I’m keeping my own version, as I like the more detailed status info, and don’t particularly want to see the revision range.

Posted 2010-03-05 (last modified 2017-03-14) under Subversion, Unix