Frage

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

War es hilfreich?

Lösung

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)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top