Skip to content
This repository has been archived by the owner on Dec 10, 2019. It is now read-only.

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
djl committed Dec 2, 2009
0 parents commit 99df25a
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 0 deletions.
27 changes: 27 additions & 0 deletions LICENSE
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,27 @@
Copyright (c) 2009, David Logie
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3. Neither the name of this project nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51 changes: 51 additions & 0 deletions README
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,51 @@
===========
vcprompt.py
===========

A pure-Python implementation of vcprompt_

.. _vcprompt: http://vc.gerg.ca/hg/vcprompt/


Installing
----------

Place ``vcprompt.py`` somewhere in your ``$PATH``, then add it to your prompt.

For bash::

PS1='\u@\h:\w $(vcprompt.py)\$'


For zsh you need to enable PROMPT_SUBST first::

setopt prompt_subst
PS1='[%n@%m] [%~] $(vcprompt.py)'


Adding systems
--------------

To add a new VCS define a function which uses the 'vcs' decorator.
The function should take a single argument, the path to be checked for.

::

@vcs
def git(path):
file = os.path.join(path, '.git/HEAD')
if not os.path.exists(file):
return None

with open(file, 'r') as f:
line = f.read()
if re.match('^ref: refs/heads/', line):
return 'git:' + line.split('/')[-1]



TODO
----

- Add support for CVS, Darcs and Fossil
- Add option to show state of repository
78 changes: 78 additions & 0 deletions vcprompt.py
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env python
import os
import re
import sys
from subprocess import Popen, PIPE

UNKNOWN = "(unknown)"
SYSTEMS = []

def vcs(function):
SYSTEMS.append(function)
return function

@vcs
def bzr(path):
file = '.bzr/branch/last-revision'
if not os.path.exists(os.path.join(path, file)):
return None
with open(file, 'r') as f:
line = f.read().split(' ', 1)[0]
return 'bzr:r' + (line or UNKNOWN)

@vcs
def hg(path):
file = '.hg/branch'
if not os.path.exists(os.path.join(path, file)):
return None
with open(file, 'r') as f:
line = f.read()
return 'hg:' + (line or UNKNOWN)

@vcs
def git(path):
prompt = "git:"
branch = UNKNOWN
file = os.path.join(path, '.git/HEAD')
if not os.path.exists(file):
return None

with open(file, 'r') as f:
line = f.read()
if re.match('^ref: refs/heads/', line.strip()):
branch = (line.split('/')[-1] or UNKNOWN)
return prompt + branch


@vcs
def svn(path):
# I'm not too keen on calling an external script
# TODO find a way to do this in pure Python without the svn bindings
if not os.path.exists(os.path.join(path, '.svn')):
return None
_p = Popen(['svnversion', path], stdout=PIPE)
revision = _p.communicate()[0]
if not revision:
revision = UNKNOWN
return 'svn:r' + revision


def vcprompt(path=None):
path = path or os.getcwd()
count = 0
while path:
if count > 0:
path = path.rsplit('/', 1)[0]
if not path:
path = '/'

# get vcs
prompt = ''
for vcs in SYSTEMS:
prompt = vcs(path)
if prompt:
return prompt
count += 1

if __name__ == '__main__':
sys.stdout.write(vcprompt())

0 comments on commit 99df25a

Please sign in to comment.