Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Colorize filename according to svn status

Tags:

bash

svn

colors

ls

When invoking ls, I would like to have file names with a different color depending on their subversion status. For example, an added file will be cyan, a modified file red and so on. Is it possible with the bare power of bash? Is there something ready on this regard ?

like image 641
Stefano Borini Avatar asked Jan 12 '11 10:01

Stefano Borini


People also ask

What is svn status s?

Status S means "switched", according to svn h st .

What does svn status l mean?

This means the file was scheduled for deletion, and then a new file with the same name was scheduled for addition in its place. L : Item is locked. E: Item existed, as it would have been created, by an svn update.

What does exclamation mark mean svn?

A Red exclamation mark tells you that you have modified these files in your working copy, so to get rid of the exclamation marks, you have to either commit the files (update the version in the repository with your modified version) or revert them (throwing away your changes).


2 Answers

As far as I know, it is not possible to achieve that with pure bash (scripting aside).

You can quite easily get colorised file listing using scripts (bash, python, perl, whatever your poison). Here's a rather crude proof-of-concept implementation written in python : https://gist.github.com/776093

#!/usr/bin/env python
import re
from subprocess import Popen, PIPE

colormap = {
    "M" : "31", # red
    "?" : "37;41", # grey
    "A" : "32", # green
    "X" : "33", # yellow
    "C" : "30;41", # black on red
    "-" : "31", # red
    "D" : "31;1", # bold red
    "+" : "32", # green
}
re_svnout = re.compile(r'(.)\s+(.+)$')
file_status = {}


def colorise(line, key):
    if key in colormap.keys():
        return "\001\033[%sm%s\033[m\002" % (colormap[key], line)
    else:
        return line

def get_svn_status():
    cmd = "svn status"
    output = Popen(cmd, shell=True, stdout=PIPE)
    for line in output.stdout:
        match = re_svnout.match(line)
        if match:
            status, f = match.group(1), match.group(2)

            # if sub directory has changes, mark it as modified
            if "/" in f:
                f = f.split("/")[0]
                status = "M"

            file_status[f] = status

if __name__ == "__main__":
    get_svn_status()
    for L in Popen("ls", shell=True, stdout=PIPE).stdout:
        line = L.strip()
        status = file_status.get(line, False)
        print colorise(line, status)
like image 198
Shawn Chin Avatar answered Sep 21 '22 12:09

Shawn Chin


Here's a Gist with the 3rd generation of a small script to colorize SVN output. It works perfectly for svn status commands. I just added alias svns="/path/to/svn-color.py status" to my .bash_profile and now I can type svns and see the color-coded output. The author recommends making svn default to his script.

like image 34
jerclarke Avatar answered Sep 17 '22 12:09

jerclarke