comparison mercurial/bookmarks.py @ 13350:a7376b92caaa

bookmarks: move basic io to core
author Matt Mackall <mpm@selenic.com>
date Thu, 10 Feb 2011 13:46:27 -0600
parents
children 6c5368cd2df9
comparison
equal deleted inserted replaced
13349:0d3f35394af4 13350:a7376b92caaa
1 # Mercurial bookmark support code
2 #
3 # Copyright 2008 David Soria Parra <dsp@php.net>
4 #
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.
7
8 from mercurial.i18n import _
9 from mercurial.node import nullid, nullrev, bin, hex, short
10 from mercurial import encoding
11 import os
12
13 def write(repo):
14 '''Write bookmarks
15
16 Write the given bookmark => hash dictionary to the .hg/bookmarks file
17 in a format equal to those of localtags.
18
19 We also store a backup of the previous state in undo.bookmarks that
20 can be copied back on rollback.
21 '''
22 refs = repo._bookmarks
23
24 try:
25 bms = repo.opener('bookmarks').read()
26 except IOError:
27 bms = ''
28 repo.opener('undo.bookmarks', 'w').write(bms)
29
30 if repo._bookmarkcurrent not in refs:
31 setcurrent(repo, None)
32 wlock = repo.wlock()
33 try:
34 file = repo.opener('bookmarks', 'w', atomictemp=True)
35 for refspec, node in refs.iteritems():
36 file.write("%s %s\n" % (hex(node), encoding.fromlocal(refspec)))
37 file.rename()
38
39 # touch 00changelog.i so hgweb reloads bookmarks (no lock needed)
40 try:
41 os.utime(repo.sjoin('00changelog.i'), None)
42 except OSError:
43 pass
44
45 finally:
46 wlock.release()
47
48 def setcurrent(repo, mark):
49 '''Set the name of the bookmark that we are currently on
50
51 Set the name of the bookmark that we are on (hg update <bookmark>).
52 The name is recorded in .hg/bookmarks.current
53 '''
54 current = repo._bookmarkcurrent
55 if current == mark:
56 return
57
58 refs = repo._bookmarks
59
60 # do not update if we do update to a rev equal to the current bookmark
61 if (mark and mark not in refs and
62 current and refs[current] == repo.changectx('.').node()):
63 return
64 if mark not in refs:
65 mark = ''
66 wlock = repo.wlock()
67 try:
68 file = repo.opener('bookmarks.current', 'w', atomictemp=True)
69 file.write(mark)
70 file.rename()
71 finally:
72 wlock.release()
73 repo._bookmarkcurrent = mark