annotate mercurial/remotenames.py @ 35239:744d1c874a59

remotenames: add functions to read remotenames data from .hg/remotenames/ This patch functions which can be used to read remotenames data from .hg/remotenames/. The logic for the function which reads the remotenames file is taken from the remotenames extension. Previously reviewed as D940. Differential Revision: https://phab.mercurial-scm.org/D1550
author Pulkit Goyal <7895pulkit@gmail.com>
date Thu, 05 Oct 2017 01:31:53 +0530
parents 8df8ce2cc5dd
children 2ea6e42ed15e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
35236
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
1 # remotenames.py
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
2 #
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
3 # Copyright 2017 Augie Fackler <raf@durin42.com>
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
4 # Copyright 2017 Sean Farley <sean@farley.io>
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
5 #
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
6 # This software may be used and distributed according to the terms of the
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
7 # GNU General Public License version 2 or any later version.
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
8
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
9 from __future__ import absolute_import
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
10
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
11 from .node import hex
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
12
35237
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
13 from . import (
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
14 vfs as vfsmod,
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
15 )
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
16
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
17 # directory name in .hg/ in which remotenames files will be present
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
18 remotenamedir = 'remotenames'
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
19
35239
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
20 def readremotenamefile(repo, filename):
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
21 """
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
22 reads a file from .hg/remotenames/ directory and yields it's content
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
23 filename: the file to be read
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
24 yield a tuple (node, remotepath, name)
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
25 """
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
26
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
27 vfs = vfsmod.vfs(repo.vfs.join(remotenamedir))
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
28 if not vfs.exists(filename):
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
29 return
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
30 f = vfs(filename)
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
31 lineno = 0
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
32 for line in f:
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
33 line = line.strip()
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
34 if not line:
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
35 continue
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
36 # contains the version number
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
37 if lineno == 0:
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
38 lineno += 1
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
39 try:
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
40 node, remote, rname = line.split('\0')
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
41 yield node, remote, rname
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
42 except ValueError:
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
43 pass
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
44
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
45 f.close()
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
46
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
47 def readremotenames(repo):
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
48 """
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
49 read the details about the remotenames stored in .hg/remotenames/ and
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
50 yields a tuple (node, remotepath, name). It does not yields information
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
51 about whether an entry yielded is branch or bookmark. To get that
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
52 information, call the respective functions.
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
53 """
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
54
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
55 for bmentry in readremotenamefile(repo, 'bookmarks'):
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
56 yield bmentry
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
57 for branchentry in readremotenamefile(repo, 'branches'):
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
58 yield branchentry
744d1c874a59 remotenames: add functions to read remotenames data from .hg/remotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35237
diff changeset
59
35237
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
60 def writeremotenamefile(repo, remotepath, names, nametype):
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
61 vfs = vfsmod.vfs(repo.vfs.join(remotenamedir))
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
62 f = vfs(nametype, 'w', atomictemp=True)
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
63 # write the storage version info on top of file
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
64 # version '0' represents the very initial version of the storage format
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
65 f.write('0\n\n')
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
66
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
67 for name, node in sorted(names.iteritems()):
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
68 if nametype == "branches":
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
69 for n in node:
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
70 f.write('%s\0%s\0%s\n' % (n, remotepath, name))
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
71 elif nametype == "bookmarks":
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
72 if node:
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
73 f.write('%s\0%s\0%s\n' % (node, remotepath, name))
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
74
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
75 f.close()
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
76
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
77 def saveremotenames(repo, remotepath, branches=None, bookmarks=None):
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
78 """
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
79 save remotenames i.e. remotebookmarks and remotebranches in their
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
80 respective files under ".hg/remotenames/" directory.
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
81 """
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
82 wlock = repo.wlock()
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
83 try:
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
84 if bookmarks:
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
85 writeremotenamefile(repo, remotepath, bookmarks, 'bookmarks')
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
86 if branches:
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
87 writeremotenamefile(repo, remotepath, branches, 'branches')
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
88 finally:
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
89 wlock.release()
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
90
35236
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
91 def pullremotenames(localrepo, remoterepo):
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
92 """
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
93 pulls bookmarks and branches information of the remote repo during a
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
94 pull or clone operation.
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
95 localrepo is our local repository
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
96 remoterepo is the peer instance
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
97 """
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
98 remotepath = remoterepo.url()
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
99 bookmarks = remoterepo.listkeys('bookmarks')
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
100 # on a push, we don't want to keep obsolete heads since
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
101 # they won't show up as heads on the next pull, so we
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
102 # remove them here otherwise we would require the user
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
103 # to issue a pull to refresh the storage
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
104 bmap = {}
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
105 repo = localrepo.unfiltered()
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
106 for branch, nodes in remoterepo.branchmap().iteritems():
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
107 bmap[branch] = []
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
108 for node in nodes:
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
109 if node in repo and not repo[node].obsolete():
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
110 bmap[branch].append(hex(node))
5a62910948d2 remotenames: move function to pull remotenames from the remoterepo to core
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
111
35237
8df8ce2cc5dd remotenames: add functionality to store remotenames under .hg/hgremotenames/
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35236
diff changeset
112 saveremotenames(localrepo, remotepath, bmap, bookmarks)