5

I'm using pysvn to monitor the changes in a Subversion directory. This is how i get informations from revisions:

(...)
svn_root = "http://svn/"
client = pysvn.Client()
from_revision = pysvn.Revision(pysvn.opt_revision_kind.number, 1500)
to_revision = pysvn.Revision( pysvn.opt_revision_kind.head )

revisions = client.log(svn_root, to_revision, to_revision, discover_changed_paths=True)

Now I want to get the changes not from a specific revision, like in my example, but the changes within the last 5 revisions (from head - 5 to head). How can I accomplish that? How can i get the NUMBER of the head revision?

I could do it by calling the Shell from Python. But I guess that there is a "Pythonic" way for that using pysvn.

Wolkenarchitekt
  • 20,170
  • 29
  • 111
  • 174
  • If an exception is raised `svn_client.info2(svn_url)[0][1].rev.number` is executed, then you can use the following try .. except construct: headrev = svn_client.info2(svn_url)[0][1].rev.number except Exception as e: print "Error while getting head revision number: {}".format(e) – khattam Feb 14 '12 at 10:25

3 Answers3

7

Got it. When providing the path to the checked out SVN source, i can ask for the HEAD revision like this:

headrev = client.info(svnroot).get("revision").number

An alternative would be this:

headrev = pysvn.Revision( pysvn.opt_revision_kind.head )            
revlog = svnclient.log( url, revision_start=headrev, revision_end=headrev, discover_changed_paths=False)
headrev = revlog[0].revision.number

(Attention, the latter only works if you use the root of an SVN repository as the url. revlog will be empty if you provide a sub-url of the repo if it's not HEAD itself)

Wolkenarchitekt
  • 20,170
  • 29
  • 111
  • 174
6

A better (and faster) method is this:

client.revpropget("revision", url=svn_url)[0].number
user1988536
  • 61
  • 1
  • 1
  • This solution works as expected. It retrieves the actual HEAD revision from the server. The other solutions with client.info do not work as i expected. Client.info only takes the path to a checked out copy as a parameter. In my case it only gives me the current revision i checked out. Not the HEAD revision from the server. – MoJo2600 Mar 26 '13 at 12:18
3

You can get the number of the head revision like follows:

client = pysvn.Client()
headrev = client.info(svn_url).revision.number

Then, you need to define the revisions according to the way you want:

from_revision = pysvn.Revision(pysvn.opt_revision_kind.number, headrev -5)
to_revision = pysvn.Revision( pysvn.opt_revision_kind.head )

Finally you can use the log output (which is a list) inside a loop and extract the info you want:

for l in log:
    print i.date
    print i.author   
Yunus Nedim Mehel
  • 12,089
  • 4
  • 50
  • 56