# HG changeset patch # User Matt Mackall # Date 2011-02-10 19:46:27 # Node ID 6c5368cd2df98498bbd9f7780c01bb0f69cf3aaf # Parent a7376b92caaae67a59d6875bdfb44744de4f3b5e bookmarks: move read methods to core diff --git a/hgext/bookmarks.py b/hgext/bookmarks.py --- a/hgext/bookmarks.py +++ b/hgext/bookmarks.py @@ -167,39 +167,11 @@ def reposetup(ui, repo): @util.propertycache def _bookmarks(self): - '''Parse .hg/bookmarks file and return a dictionary - - Bookmarks are stored as {HASH}\\s{NAME}\\n (localtags format) values - in the .hg/bookmarks file. - Read the file and return a (name=>nodeid) dictionary - ''' - try: - bookmarks = {} - for line in self.opener('bookmarks'): - sha, refspec = line.strip().split(' ', 1) - refspec = encoding.tolocal(refspec) - bookmarks[refspec] = self.changelog.lookup(sha) - except: - pass - return bookmarks + return bookmarks.read(self) @util.propertycache def _bookmarkcurrent(self): - '''Get the current bookmark - - If we use gittishsh branches we have a current bookmark that - we are on. This function returns the name of the bookmark. It - is stored in .hg/bookmarks.current - ''' - mark = None - if os.path.exists(self.join('bookmarks.current')): - file = self.opener('bookmarks.current') - # No readline() in posixfile_nt, reading everything is cheap - mark = (file.readlines() or [''])[0] - if mark == '': - mark = None - file.close() - return mark + return bookmarks.readcurrent(self) def rollback(self, dryrun=False): if os.path.exists(self.join('undo.bookmarks')): diff --git a/mercurial/bookmarks.py b/mercurial/bookmarks.py --- a/mercurial/bookmarks.py +++ b/mercurial/bookmarks.py @@ -10,6 +10,40 @@ from mercurial.node import nullid, nullr from mercurial import encoding import os +def read(repo): + '''Parse .hg/bookmarks file and return a dictionary + + Bookmarks are stored as {HASH}\\s{NAME}\\n (localtags format) values + in the .hg/bookmarks file. + Read the file and return a (name=>nodeid) dictionary + ''' + try: + bookmarks = {} + for line in repo.opener('bookmarks'): + sha, refspec = line.strip().split(' ', 1) + refspec = encoding.tolocal(refspec) + bookmarks[refspec] = repo.changelog.lookup(sha) + except: + pass + return bookmarks + +def readcurrent(repo): + '''Get the current bookmark + + If we use gittishsh branches we have a current bookmark that + we are on. This function returns the name of the bookmark. It + is stored in .hg/bookmarks.current + ''' + mark = None + if os.path.exists(repo.join('bookmarks.current')): + file = repo.opener('bookmarks.current') + # No readline() in posixfile_nt, reading everything is cheap + mark = (file.readlines() or [''])[0] + if mark == '': + mark = None + file.close() + return mark + def write(repo): '''Write bookmarks