Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if file under version control in pysvn (python subversion)

Tags:

python

svn

pysvn

In pysvn, how do I test if a file is under version control?

like image 222
Colonel Panic Avatar asked May 16 '12 15:05

Colonel Panic


1 Answers

Use client.status() and check the text_status attribute of the returned status object. Example:

>>> import pysvn
>>> c = pysvn.Client()
>>> out = c.status("versioned.cpp")[0]  # .status() returns a list
>>> out.text_status
<wc_status_kind.normal>

That shows the file is versioned and unmodified.

>>> c.status("added.cpp")[0].text_status  # added file
<wc_status_kind.added>
>>> c.status("unversioned.cpp")[0].text_status  # unversioned file
<wc_status_kind.unversioned>

You can explore other possible statuses using dir (pysvn.wc_status_kind)

You can therefore wrap that up in something like:

def under_version_control(filename):
    "returns true if file is unversioned"
    c = pysvn.Client()
    s = c.status(filename)[0].text_status
    return s not in (
        pysvn.wc_status_kind.added, 
        pysvn.wc_status_kind.unversioned,
        pysvn.wc_status_kind.ignored)

If you wish to also address files outside an svn working directory, you'll need to catch and handle ClientError. E.g.

def under_version_control(filename):
    "returns true if file is unversioned"
    c = pysvn.Client()
    try:
        s = c.status(filename)[0].text_status
    catch pysvn.ClientError:
        return False
    else:
        return s not in (
            pysvn.wc_status_kind.added, 
            pysvn.wc_status_kind.unversioned,
            pysvn.wc_status_kind.ignored)
like image 120
Shawn Chin Avatar answered Oct 24 '22 10:10

Shawn Chin