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