简体   繁体   中英

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

pysvn中 ,如何测试文件是否受版本控制?

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 . Eg

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM