##// END OF EJS Templates
logexchange: use the proper accessors to get the remote url...
marmoute -
r50655:5bceea1a default
parent child Browse files
Show More
@@ -1,167 +1,167 b''
1 # logexchange.py
1 # logexchange.py
2 #
2 #
3 # Copyright 2017 Augie Fackler <raf@durin42.com>
3 # Copyright 2017 Augie Fackler <raf@durin42.com>
4 # Copyright 2017 Sean Farley <sean@farley.io>
4 # Copyright 2017 Sean Farley <sean@farley.io>
5 #
5 #
6 # This software may be used and distributed according to the terms of the
6 # This software may be used and distributed according to the terms of the
7 # GNU General Public License version 2 or any later version.
7 # GNU General Public License version 2 or any later version.
8
8
9
9
10 from .node import hex
10 from .node import hex
11
11
12 from . import (
12 from . import (
13 util,
13 util,
14 vfs as vfsmod,
14 vfs as vfsmod,
15 )
15 )
16 from .utils import (
16 from .utils import (
17 urlutil,
17 urlutil,
18 )
18 )
19
19
20 # directory name in .hg/ in which remotenames files will be present
20 # directory name in .hg/ in which remotenames files will be present
21 remotenamedir = b'logexchange'
21 remotenamedir = b'logexchange'
22
22
23
23
24 def readremotenamefile(repo, filename):
24 def readremotenamefile(repo, filename):
25 """
25 """
26 reads a file from .hg/logexchange/ directory and yields it's content
26 reads a file from .hg/logexchange/ directory and yields it's content
27 filename: the file to be read
27 filename: the file to be read
28 yield a tuple (node, remotepath, name)
28 yield a tuple (node, remotepath, name)
29 """
29 """
30
30
31 vfs = vfsmod.vfs(repo.vfs.join(remotenamedir))
31 vfs = vfsmod.vfs(repo.vfs.join(remotenamedir))
32 if not vfs.exists(filename):
32 if not vfs.exists(filename):
33 return
33 return
34 f = vfs(filename)
34 f = vfs(filename)
35 lineno = 0
35 lineno = 0
36 for line in f:
36 for line in f:
37 line = line.strip()
37 line = line.strip()
38 if not line:
38 if not line:
39 continue
39 continue
40 # contains the version number
40 # contains the version number
41 if lineno == 0:
41 if lineno == 0:
42 lineno += 1
42 lineno += 1
43 try:
43 try:
44 node, remote, rname = line.split(b'\0')
44 node, remote, rname = line.split(b'\0')
45 yield node, remote, rname
45 yield node, remote, rname
46 except ValueError:
46 except ValueError:
47 pass
47 pass
48
48
49 f.close()
49 f.close()
50
50
51
51
52 def readremotenames(repo):
52 def readremotenames(repo):
53 """
53 """
54 read the details about the remotenames stored in .hg/logexchange/ and
54 read the details about the remotenames stored in .hg/logexchange/ and
55 yields a tuple (node, remotepath, name). It does not yields information
55 yields a tuple (node, remotepath, name). It does not yields information
56 about whether an entry yielded is branch or bookmark. To get that
56 about whether an entry yielded is branch or bookmark. To get that
57 information, call the respective functions.
57 information, call the respective functions.
58 """
58 """
59
59
60 for bmentry in readremotenamefile(repo, b'bookmarks'):
60 for bmentry in readremotenamefile(repo, b'bookmarks'):
61 yield bmentry
61 yield bmentry
62 for branchentry in readremotenamefile(repo, b'branches'):
62 for branchentry in readremotenamefile(repo, b'branches'):
63 yield branchentry
63 yield branchentry
64
64
65
65
66 def writeremotenamefile(repo, remotepath, names, nametype):
66 def writeremotenamefile(repo, remotepath, names, nametype):
67 vfs = vfsmod.vfs(repo.vfs.join(remotenamedir))
67 vfs = vfsmod.vfs(repo.vfs.join(remotenamedir))
68 f = vfs(nametype, b'w', atomictemp=True)
68 f = vfs(nametype, b'w', atomictemp=True)
69 # write the storage version info on top of file
69 # write the storage version info on top of file
70 # version '0' represents the very initial version of the storage format
70 # version '0' represents the very initial version of the storage format
71 f.write(b'0\n\n')
71 f.write(b'0\n\n')
72
72
73 olddata = set(readremotenamefile(repo, nametype))
73 olddata = set(readremotenamefile(repo, nametype))
74 # re-save the data from a different remote than this one.
74 # re-save the data from a different remote than this one.
75 for node, oldpath, rname in sorted(olddata):
75 for node, oldpath, rname in sorted(olddata):
76 if oldpath != remotepath:
76 if oldpath != remotepath:
77 f.write(b'%s\0%s\0%s\n' % (node, oldpath, rname))
77 f.write(b'%s\0%s\0%s\n' % (node, oldpath, rname))
78
78
79 for name, node in sorted(names.items()):
79 for name, node in sorted(names.items()):
80 if nametype == b"branches":
80 if nametype == b"branches":
81 for n in node:
81 for n in node:
82 f.write(b'%s\0%s\0%s\n' % (n, remotepath, name))
82 f.write(b'%s\0%s\0%s\n' % (n, remotepath, name))
83 elif nametype == b"bookmarks":
83 elif nametype == b"bookmarks":
84 if node:
84 if node:
85 f.write(b'%s\0%s\0%s\n' % (node, remotepath, name))
85 f.write(b'%s\0%s\0%s\n' % (node, remotepath, name))
86
86
87 f.close()
87 f.close()
88
88
89
89
90 def saveremotenames(repo, remotepath, branches=None, bookmarks=None):
90 def saveremotenames(repo, remotepath, branches=None, bookmarks=None):
91 """
91 """
92 save remotenames i.e. remotebookmarks and remotebranches in their
92 save remotenames i.e. remotebookmarks and remotebranches in their
93 respective files under ".hg/logexchange/" directory.
93 respective files under ".hg/logexchange/" directory.
94 """
94 """
95 wlock = repo.wlock()
95 wlock = repo.wlock()
96 try:
96 try:
97 if bookmarks:
97 if bookmarks:
98 writeremotenamefile(repo, remotepath, bookmarks, b'bookmarks')
98 writeremotenamefile(repo, remotepath, bookmarks, b'bookmarks')
99 if branches:
99 if branches:
100 writeremotenamefile(repo, remotepath, branches, b'branches')
100 writeremotenamefile(repo, remotepath, branches, b'branches')
101 finally:
101 finally:
102 wlock.release()
102 wlock.release()
103
103
104
104
105 def activepath(repo, remote):
105 def activepath(repo, remote):
106 """returns remote path"""
106 """returns remote path"""
107 # is the remote a local peer
107 # is the remote a local peer
108 local = remote.local()
108 local = remote.local()
109
109
110 # determine the remote path from the repo, if possible; else just
110 # determine the remote path from the repo, if possible; else just
111 # use the string given to us
111 # use the string given to us
112 rpath = remote
112 rpath = remote
113 if local:
113 if local:
114 rpath = util.pconvert(remote._repo.root)
114 rpath = util.pconvert(remote._repo.root)
115 elif not isinstance(remote, bytes):
115 elif not isinstance(remote, bytes):
116 rpath = remote._url
116 rpath = remote.url()
117
117
118 # represent the remotepath with user defined path name if exists
118 # represent the remotepath with user defined path name if exists
119 for path, url in repo.ui.configitems(b'paths'):
119 for path, url in repo.ui.configitems(b'paths'):
120 # remove auth info from user defined url
120 # remove auth info from user defined url
121 noauthurl = urlutil.removeauth(url)
121 noauthurl = urlutil.removeauth(url)
122
122
123 # Standardize on unix style paths, otherwise some {remotenames} end up
123 # Standardize on unix style paths, otherwise some {remotenames} end up
124 # being an absolute path on Windows.
124 # being an absolute path on Windows.
125 url = util.pconvert(bytes(url))
125 url = util.pconvert(bytes(url))
126 noauthurl = util.pconvert(noauthurl)
126 noauthurl = util.pconvert(noauthurl)
127 if url == rpath or noauthurl == rpath:
127 if url == rpath or noauthurl == rpath:
128 rpath = path
128 rpath = path
129 break
129 break
130
130
131 return rpath
131 return rpath
132
132
133
133
134 def pullremotenames(localrepo, remoterepo):
134 def pullremotenames(localrepo, remoterepo):
135 """
135 """
136 pulls bookmarks and branches information of the remote repo during a
136 pulls bookmarks and branches information of the remote repo during a
137 pull or clone operation.
137 pull or clone operation.
138 localrepo is our local repository
138 localrepo is our local repository
139 remoterepo is the peer instance
139 remoterepo is the peer instance
140 """
140 """
141 remotepath = activepath(localrepo, remoterepo)
141 remotepath = activepath(localrepo, remoterepo)
142
142
143 with remoterepo.commandexecutor() as e:
143 with remoterepo.commandexecutor() as e:
144 bookmarks = e.callcommand(
144 bookmarks = e.callcommand(
145 b'listkeys',
145 b'listkeys',
146 {
146 {
147 b'namespace': b'bookmarks',
147 b'namespace': b'bookmarks',
148 },
148 },
149 ).result()
149 ).result()
150
150
151 # on a push, we don't want to keep obsolete heads since
151 # on a push, we don't want to keep obsolete heads since
152 # they won't show up as heads on the next pull, so we
152 # they won't show up as heads on the next pull, so we
153 # remove them here otherwise we would require the user
153 # remove them here otherwise we would require the user
154 # to issue a pull to refresh the storage
154 # to issue a pull to refresh the storage
155 bmap = {}
155 bmap = {}
156 repo = localrepo.unfiltered()
156 repo = localrepo.unfiltered()
157
157
158 with remoterepo.commandexecutor() as e:
158 with remoterepo.commandexecutor() as e:
159 branchmap = e.callcommand(b'branchmap', {}).result()
159 branchmap = e.callcommand(b'branchmap', {}).result()
160
160
161 for branch, nodes in branchmap.items():
161 for branch, nodes in branchmap.items():
162 bmap[branch] = []
162 bmap[branch] = []
163 for node in nodes:
163 for node in nodes:
164 if node in repo and not repo[node].obsolete():
164 if node in repo and not repo[node].obsolete():
165 bmap[branch].append(hex(node))
165 bmap[branch].append(hex(node))
166
166
167 saveremotenames(localrepo, remotepath, bmap, bookmarks)
167 saveremotenames(localrepo, remotepath, bmap, bookmarks)
General Comments 0
You need to be logged in to leave comments. Login now