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