Check if the file is under version control in pysvn (python disruptive operation)

In pysvn , how to check if a file is under version control?

+4
source share
1 answer

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> 

This indicates that 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)

So you can wrap this with 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 also want to access files outside of the svn working directory, you need to catch and handle ClientError . For instance.

 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) 
+7
source

Source: https://habr.com/ru/post/1412816/


All Articles