Script for easier adding subversion ignore rules

4 July, 2008 - 11:16
Categories:

It's possible to make Subversion ignore unversioned files, so they don't show up in the status overview and you get a better signal to noise ratio. The command line interface to add ignore rules this is however a bit clunky. First, you need to remember following command:

svn propedit svn:ignore .

(which can be quite confusing, especially with the slightly similar looking but different propset command).
Then you get a file editor where you have to add the rule (make sure you remembered the file name or pattern you want to ignore), save the file and exit the editor.
A bit too much hoops to jump through for something that could be just one command like svn-ignore filename.

I wrote this simple python script to get this functionality:

#!/usr/bin/env python
 
"""
Script to make it easier to add svn-ignore rules.
"""
 
import sys
import os
import subprocess
 
def svn_propget_svnignore(path):
    '''fetch the svn:ignore property of given path'''
    p = subprocess.Popen(['svn', 'propget', 'svn:ignore', path], stdout=subprocess.PIPE)
    p.wait()
    data = p.stdout.read().strip()
    return data
 
def svn_propset_svnignore(path, value):
    '''set the svn:ignore property of the given path'''
    p = subprocess.Popen(['svn', 'propset', 'svn:ignore', value, path])
    p.wait()
 
 
def main():
 
    if len(sys.argv) < 2:
        print 'Usage: %s filenames' % sys.argv[0]
        sys.exit()
 
    for path in sys.argv[1:]:
        print path
 
        dirpath, filename = os.path.split(path)
        svnignore_data = svn_propget_svnignore(dirpath)
 
        if filename not in svnignore_data.split('\n'):
            svnignore_data += '\n' + filename
            svn_propset_svnignore(dirpath, svnignore_data)
 
if __name__ == '__main__':
    main()

I saved it as svn-ignore.py, made it executable, put it somewhere in my PATH and now I can just do

svn-ignore.py foo.txt bar.zzs "*.backup"

Note that it also supports wildcard rules, but these have to be put in quotes, so the shell does not expands them.

1 July, 2010 - 10:55

script

Magic Tricks Tutorial (not verified)

nice script it help alot!

23 August, 2009 - 03:40

Works great for me, no bugs

aumento de pene (not verified)

Works great for me, no bugs showed.

2 July, 2009 - 08:18

good and useful scripts

tamiflu (not verified)

good and useful scripts

26 March, 2009 - 19:50

little bug!

acalando (not verified)

Files that partialy match a filename already added are not
added, so I propose a little correction:

if '\n'+filename+'\n' not in '\n'+svnignore_data+'\n':

Thanks for this nice script anyway!

16 December, 2009 - 13:47

fixed

slippens

Yep, I noticed it too.
Should be fixed now

26 March, 2009 - 19:48

Bug when similiar name already in proplist

calandoa (not verified)

Files that partialy matches filename already added are not
added, so I add a little correction:

if '\n'+filename+'\n' not in '\n'+svnignore_data+'\n':

Thanks for this nice script

29 September, 2008 - 11:12

Nice script ...

pjbaeck (not verified)

... but the subprocess module is only available for Python >= 2.4. Anyway, you can replace the subprocess.Popen function with os.Popen.

Cheers
Pieter-Jan

6 September, 2008 - 22:56

Got confused also

Joe S (not verified)

nice script, but a little confusing where python said. got it right though

15 August, 2008 - 20:24

Blacktackle

Roel De Meester (not verified)

I just discovered that i already had a similar script ...
Have a look at http://www.cubewano.org/blacktackle

Greetz

15 August, 2008 - 20:20

Handy script

Roel De Meester (not verified)

That's indeed a lot handier than the propedit thing.
In fact i even managed to get confused because i was adding multiple ignore patterns in a row like
/> svn propset svn:ignore files
/> svn propset svn:ignore .project
/> svn propset svn:ignore .cache
...

It took me some minutes to discover that this command does not ADD extra ignore patterns, it just sets an new one, so only the last pattern was retained.. Hence the need for
/> svn propedit svn:ignore

I just added your script to my growing list of handy (drupal) scripts.
Thanks.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • No HTML tags allowed
  • Lines and paragraphs break automatically.

More information about formatting options