##// END OF EJS Templates
bookmarks: move basic io to core
Matt Mackall -
r13350:a7376b92 default
parent child Browse files
Show More
@@ -0,0 +1,73
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
@@ -32,70 +32,9 from mercurial.i18n import _
32 32 from mercurial.node import nullid, nullrev, bin, hex, short
33 33 from mercurial import util, commands, repair, extensions, pushkey, hg, url
34 34 from mercurial import revset, encoding
35 from mercurial import bookmarks
35 36 import os
36 37
37 def write(repo):
38 '''Write bookmarks
39
40 Write the given bookmark => hash dictionary to the .hg/bookmarks file
41 in a format equal to those of localtags.
42
43 We also store a backup of the previous state in undo.bookmarks that
44 can be copied back on rollback.
45 '''
46 refs = repo._bookmarks
47
48 try:
49 bms = repo.opener('bookmarks').read()
50 except IOError:
51 bms = ''
52 repo.opener('undo.bookmarks', 'w').write(bms)
53
54 if repo._bookmarkcurrent not in refs:
55 setcurrent(repo, None)
56 wlock = repo.wlock()
57 try:
58 file = repo.opener('bookmarks', 'w', atomictemp=True)
59 for refspec, node in refs.iteritems():
60 file.write("%s %s\n" % (hex(node), encoding.fromlocal(refspec)))
61 file.rename()
62
63 # touch 00changelog.i so hgweb reloads bookmarks (no lock needed)
64 try:
65 os.utime(repo.sjoin('00changelog.i'), None)
66 except OSError:
67 pass
68
69 finally:
70 wlock.release()
71
72 def setcurrent(repo, mark):
73 '''Set the name of the bookmark that we are currently on
74
75 Set the name of the bookmark that we are on (hg update <bookmark>).
76 The name is recorded in .hg/bookmarks.current
77 '''
78 current = repo._bookmarkcurrent
79 if current == mark:
80 return
81
82 refs = repo._bookmarks
83
84 # do not update if we do update to a rev equal to the current bookmark
85 if (mark and mark not in refs and
86 current and refs[current] == repo.changectx('.').node()):
87 return
88 if mark not in refs:
89 mark = ''
90 wlock = repo.wlock()
91 try:
92 file = repo.opener('bookmarks.current', 'w', atomictemp=True)
93 file.write(mark)
94 file.rename()
95 finally:
96 wlock.release()
97 repo._bookmarkcurrent = mark
98
99 38 def bookmark(ui, repo, mark=None, rev=None, force=False, delete=False, rename=None):
100 39 '''track a line of development with movable markers
101 40
@@ -127,8 +66,8 def bookmark(ui, repo, mark=None, rev=No
127 66 marks[mark] = marks[rename]
128 67 del marks[rename]
129 68 if repo._bookmarkcurrent == rename:
130 setcurrent(repo, mark)
131 write(repo)
69 bookmarks.setcurrent(repo, mark)
70 bookmarks.write(repo)
132 71 return
133 72
134 73 if delete:
@@ -137,9 +76,9 def bookmark(ui, repo, mark=None, rev=No
137 76 if mark not in marks:
138 77 raise util.Abort(_("a bookmark of this name does not exist"))
139 78 if mark == repo._bookmarkcurrent:
140 setcurrent(repo, None)
79 bookmarks.setcurrent(repo, None)
141 80 del marks[mark]
142 write(repo)
81 bookmarks.write(repo)
143 82 return
144 83
145 84 if mark is not None:
@@ -159,8 +98,8 def bookmark(ui, repo, mark=None, rev=No
159 98 marks[mark] = repo.lookup(rev)
160 99 else:
161 100 marks[mark] = repo.changectx('.').node()
162 setcurrent(repo, mark)
163 write(repo)
101 bookmarks.setcurrent(repo, mark)
102 bookmarks.write(repo)
164 103 return
165 104
166 105 if mark is None:
@@ -218,7 +157,7 def strip(oldstrip, ui, repo, node, back
218 157 if len(update) > 0:
219 158 for m in update:
220 159 marks[m] = repo.changectx('.').node()
221 write(repo)
160 bookmarks.write(repo)
222 161
223 162 def reposetup(ui, repo):
224 163 if not repo.local():
@@ -290,7 +229,7 def reposetup(ui, repo):
290 229 marks[mark] = node
291 230 update = True
292 231 if update:
293 write(self)
232 bookmarks.write(self)
294 233
295 234 def commitctx(self, ctx, error=False):
296 235 """Add a revision to the repository and
@@ -331,7 +270,7 def reposetup(ui, repo):
331 270 self.ui.warn(_("not updating divergent"
332 271 " bookmark %s\n") % k)
333 272 if changed:
334 write(repo)
273 bookmarks.write(repo)
335 274
336 275 return result
337 276
@@ -405,7 +344,7 def pushbookmark(repo, key, old, new):
405 344 if new not in repo:
406 345 return False
407 346 marks[key] = repo[new].node()
408 write(repo)
347 bookmarks.write(repo)
409 348 return True
410 349 finally:
411 350 w.release()
@@ -432,7 +371,7 def pull(oldpull, ui, repo, source="defa
432 371 # explicit pull overrides local bookmark if any
433 372 ui.status(_("importing bookmark %s\n") % b)
434 373 repo._bookmarks[b] = repo[rb[b]].node()
435 write(repo)
374 bookmarks.write(repo)
436 375
437 376 return result
438 377
@@ -542,7 +481,7 def updatecurbookmark(orig, ui, repo, *a
542 481 rev = opts['rev']
543 482 if not rev and len(args) > 0:
544 483 rev = args[0]
545 setcurrent(repo, rev)
484 bookmarks.setcurrent(repo, rev)
546 485 return res
547 486
548 487 def bmrevset(repo, subset, x):
General Comments 0
You need to be logged in to leave comments. Login now