mercurial/bookmarks.py
changeset 14027 78ab705a8147
parent 14004 97ed99d1f419
child 14039 3fc7154396d2
equal deleted inserted replaced
14026:4f19242dac6c 14027:78ab705a8147
     5 # This software may be used and distributed according to the terms of the
     5 # This software may be used and distributed according to the terms of the
     6 # GNU General Public License version 2 or any later version.
     6 # GNU General Public License version 2 or any later version.
     7 
     7 
     8 from mercurial.i18n import _
     8 from mercurial.i18n import _
     9 from mercurial.node import nullid, nullrev, bin, hex, short
     9 from mercurial.node import nullid, nullrev, bin, hex, short
    10 from mercurial import encoding, util
    10 from mercurial import encoding, error, util
    11 import os
    11 import errno, os
    12 
    12 
    13 def valid(mark):
    13 def valid(mark):
    14     for c in (':', '\0', '\n', '\r'):
    14     for c in (':', '\0', '\n', '\r'):
    15         if c in mark:
    15         if c in mark:
    16             return False
    16             return False
    21 
    21 
    22     Bookmarks are stored as {HASH}\\s{NAME}\\n (localtags format) values
    22     Bookmarks are stored as {HASH}\\s{NAME}\\n (localtags format) values
    23     in the .hg/bookmarks file.
    23     in the .hg/bookmarks file.
    24     Read the file and return a (name=>nodeid) dictionary
    24     Read the file and return a (name=>nodeid) dictionary
    25     '''
    25     '''
    26     try:
    26     bookmarks = {}
    27         bookmarks = {}
    27     try:
    28         for line in repo.opener('bookmarks'):
    28         for line in repo.opener('bookmarks'):
    29             sha, refspec = line.strip().split(' ', 1)
    29             sha, refspec = line.strip().split(' ', 1)
    30             refspec = encoding.tolocal(refspec)
    30             refspec = encoding.tolocal(refspec)
    31             bookmarks[refspec] = repo.changelog.lookup(sha)
    31             try:
    32     except IOError:
    32                 bookmarks[refspec] = repo.changelog.lookup(sha)
    33         pass
    33             except error.RepoLookupError:
       
    34                 pass
       
    35     except IOError, inst:
       
    36         if inst.errno != errno.ENOENT:
       
    37             raise
    34     return bookmarks
    38     return bookmarks
    35 
    39 
    36 def readcurrent(repo):
    40 def readcurrent(repo):
    37     '''Get the current bookmark
    41     '''Get the current bookmark
    38 
    42 
    39     If we use gittishsh branches we have a current bookmark that
    43     If we use gittishsh branches we have a current bookmark that
    40     we are on. This function returns the name of the bookmark. It
    44     we are on. This function returns the name of the bookmark. It
    41     is stored in .hg/bookmarks.current
    45     is stored in .hg/bookmarks.current
    42     '''
    46     '''
    43     mark = None
    47     mark = None
    44     if os.path.exists(repo.join('bookmarks.current')):
    48     try:
    45         file = repo.opener('bookmarks.current')
    49         file = repo.opener('bookmarks.current')
       
    50     except IOError, inst:
       
    51         if inst.errno != errno.ENOENT:
       
    52             raise
       
    53         return None
       
    54     try:
    46         # No readline() in posixfile_nt, reading everything is cheap
    55         # No readline() in posixfile_nt, reading everything is cheap
    47         mark = encoding.tolocal((file.readlines() or [''])[0])
    56         mark = encoding.tolocal((file.readlines() or [''])[0])
    48         if mark == '' or mark not in repo._bookmarks:
    57         if mark == '' or mark not in repo._bookmarks:
    49             mark = None
    58             mark = None
       
    59     finally:
    50         file.close()
    60         file.close()
    51     return mark
    61     return mark
    52 
    62 
    53 def write(repo):
    63 def write(repo):
    54     '''Write bookmarks
    64     '''Write bookmarks