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 |
10 from mercurial import encoding |
11 import os |
11 import os |
|
12 |
|
13 def read(repo): |
|
14 '''Parse .hg/bookmarks file and return a dictionary |
|
15 |
|
16 Bookmarks are stored as {HASH}\\s{NAME}\\n (localtags format) values |
|
17 in the .hg/bookmarks file. |
|
18 Read the file and return a (name=>nodeid) dictionary |
|
19 ''' |
|
20 try: |
|
21 bookmarks = {} |
|
22 for line in repo.opener('bookmarks'): |
|
23 sha, refspec = line.strip().split(' ', 1) |
|
24 refspec = encoding.tolocal(refspec) |
|
25 bookmarks[refspec] = repo.changelog.lookup(sha) |
|
26 except: |
|
27 pass |
|
28 return bookmarks |
|
29 |
|
30 def readcurrent(repo): |
|
31 '''Get the current bookmark |
|
32 |
|
33 If we use gittishsh branches we have a current bookmark that |
|
34 we are on. This function returns the name of the bookmark. It |
|
35 is stored in .hg/bookmarks.current |
|
36 ''' |
|
37 mark = None |
|
38 if os.path.exists(repo.join('bookmarks.current')): |
|
39 file = repo.opener('bookmarks.current') |
|
40 # No readline() in posixfile_nt, reading everything is cheap |
|
41 mark = (file.readlines() or [''])[0] |
|
42 if mark == '': |
|
43 mark = None |
|
44 file.close() |
|
45 return mark |
12 |
46 |
13 def write(repo): |
47 def write(repo): |
14 '''Write bookmarks |
48 '''Write bookmarks |
15 |
49 |
16 Write the given bookmark => hash dictionary to the .hg/bookmarks file |
50 Write the given bookmark => hash dictionary to the .hg/bookmarks file |