Skip to content

Commit 99df25a

Browse files
committed
Initial commit.
0 parents  commit 99df25a

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed

LICENSE

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright (c) 2009, David Logie
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without modification,
5+
are permitted provided that the following conditions are met:
6+
7+
1. Redistributions of source code must retain the above copyright notice,
8+
this list of conditions and the following disclaimer.
9+
10+
2. Redistributions in binary form must reproduce the above copyright
11+
notice, this list of conditions and the following disclaimer in the
12+
documentation and/or other materials provided with the distribution.
13+
14+
3. Neither the name of this project nor the names of its contributors may be
15+
used to endorse or promote products derived from this software without
16+
specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
===========
2+
vcprompt.py
3+
===========
4+
5+
A pure-Python implementation of vcprompt_
6+
7+
.. _vcprompt: http://vc.gerg.ca/hg/vcprompt/
8+
9+
10+
Installing
11+
----------
12+
13+
Place ``vcprompt.py`` somewhere in your ``$PATH``, then add it to your prompt.
14+
15+
For bash::
16+
17+
PS1='\u@\h:\w $(vcprompt.py)\$'
18+
19+
20+
For zsh you need to enable PROMPT_SUBST first::
21+
22+
setopt prompt_subst
23+
PS1='[%n@%m] [%~] $(vcprompt.py)'
24+
25+
26+
Adding systems
27+
--------------
28+
29+
To add a new VCS define a function which uses the 'vcs' decorator.
30+
The function should take a single argument, the path to be checked for.
31+
32+
::
33+
34+
@vcs
35+
def git(path):
36+
file = os.path.join(path, '.git/HEAD')
37+
if not os.path.exists(file):
38+
return None
39+
40+
with open(file, 'r') as f:
41+
line = f.read()
42+
if re.match('^ref: refs/heads/', line):
43+
return 'git:' + line.split('/')[-1]
44+
45+
46+
47+
TODO
48+
----
49+
50+
- Add support for CVS, Darcs and Fossil
51+
- Add option to show state of repository

vcprompt.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env python
2+
import os
3+
import re
4+
import sys
5+
from subprocess import Popen, PIPE
6+
7+
UNKNOWN = "(unknown)"
8+
SYSTEMS = []
9+
10+
def vcs(function):
11+
SYSTEMS.append(function)
12+
return function
13+
14+
@vcs
15+
def bzr(path):
16+
file = '.bzr/branch/last-revision'
17+
if not os.path.exists(os.path.join(path, file)):
18+
return None
19+
with open(file, 'r') as f:
20+
line = f.read().split(' ', 1)[0]
21+
return 'bzr:r' + (line or UNKNOWN)
22+
23+
@vcs
24+
def hg(path):
25+
file = '.hg/branch'
26+
if not os.path.exists(os.path.join(path, file)):
27+
return None
28+
with open(file, 'r') as f:
29+
line = f.read()
30+
return 'hg:' + (line or UNKNOWN)
31+
32+
@vcs
33+
def git(path):
34+
prompt = "git:"
35+
branch = UNKNOWN
36+
file = os.path.join(path, '.git/HEAD')
37+
if not os.path.exists(file):
38+
return None
39+
40+
with open(file, 'r') as f:
41+
line = f.read()
42+
if re.match('^ref: refs/heads/', line.strip()):
43+
branch = (line.split('/')[-1] or UNKNOWN)
44+
return prompt + branch
45+
46+
47+
@vcs
48+
def svn(path):
49+
# I'm not too keen on calling an external script
50+
# TODO find a way to do this in pure Python without the svn bindings
51+
if not os.path.exists(os.path.join(path, '.svn')):
52+
return None
53+
_p = Popen(['svnversion', path], stdout=PIPE)
54+
revision = _p.communicate()[0]
55+
if not revision:
56+
revision = UNKNOWN
57+
return 'svn:r' + revision
58+
59+
60+
def vcprompt(path=None):
61+
path = path or os.getcwd()
62+
count = 0
63+
while path:
64+
if count > 0:
65+
path = path.rsplit('/', 1)[0]
66+
if not path:
67+
path = '/'
68+
69+
# get vcs
70+
prompt = ''
71+
for vcs in SYSTEMS:
72+
prompt = vcs(path)
73+
if prompt:
74+
return prompt
75+
count += 1
76+
77+
if __name__ == '__main__':
78+
sys.stdout.write(vcprompt())

0 commit comments

Comments
 (0)