##// END OF EJS Templates
narrow: drop checkambig=True when restoring backup...
Martin von Zweigbergk -
r38907:204e074c default
parent child Browse files
Show More
@@ -1,198 +1,198 b''
1 1 # narrowspec.py - methods for working with a narrow view of a repository
2 2 #
3 3 # Copyright 2017 Google, Inc.
4 4 #
5 5 # This software may be used and distributed according to the terms of the
6 6 # GNU General Public License version 2 or any later version.
7 7
8 8 from __future__ import absolute_import
9 9
10 10 import errno
11 11
12 12 from .i18n import _
13 13 from . import (
14 14 error,
15 15 match as matchmod,
16 16 repository,
17 17 sparse,
18 18 util,
19 19 )
20 20
21 21 FILENAME = 'narrowspec'
22 22
23 23 def parseserverpatterns(text):
24 24 """Parses the narrowspec format that's returned by the server."""
25 25 includepats = set()
26 26 excludepats = set()
27 27
28 28 # We get one entry per line, in the format "<key> <value>".
29 29 # It's OK for value to contain other spaces.
30 30 for kp in (l.split(' ', 1) for l in text.splitlines()):
31 31 if len(kp) != 2:
32 32 raise error.Abort(_('Invalid narrowspec pattern line: "%s"') % kp)
33 33 key = kp[0]
34 34 pat = kp[1]
35 35 if key == 'include':
36 36 includepats.add(pat)
37 37 elif key == 'exclude':
38 38 excludepats.add(pat)
39 39 else:
40 40 raise error.Abort(_('Invalid key "%s" in server response') % key)
41 41
42 42 return includepats, excludepats
43 43
44 44 def normalizesplitpattern(kind, pat):
45 45 """Returns the normalized version of a pattern and kind.
46 46
47 47 Returns a tuple with the normalized kind and normalized pattern.
48 48 """
49 49 pat = pat.rstrip('/')
50 50 _validatepattern(pat)
51 51 return kind, pat
52 52
53 53 def _numlines(s):
54 54 """Returns the number of lines in s, including ending empty lines."""
55 55 # We use splitlines because it is Unicode-friendly and thus Python 3
56 56 # compatible. However, it does not count empty lines at the end, so trick
57 57 # it by adding a character at the end.
58 58 return len((s + 'x').splitlines())
59 59
60 60 def _validatepattern(pat):
61 61 """Validates the pattern and aborts if it is invalid.
62 62
63 63 Patterns are stored in the narrowspec as newline-separated
64 64 POSIX-style bytestring paths. There's no escaping.
65 65 """
66 66
67 67 # We use newlines as separators in the narrowspec file, so don't allow them
68 68 # in patterns.
69 69 if _numlines(pat) > 1:
70 70 raise error.Abort(_('newlines are not allowed in narrowspec paths'))
71 71
72 72 components = pat.split('/')
73 73 if '.' in components or '..' in components:
74 74 raise error.Abort(_('"." and ".." are not allowed in narrowspec paths'))
75 75
76 76 def normalizepattern(pattern, defaultkind='path'):
77 77 """Returns the normalized version of a text-format pattern.
78 78
79 79 If the pattern has no kind, the default will be added.
80 80 """
81 81 kind, pat = matchmod._patsplit(pattern, defaultkind)
82 82 return '%s:%s' % normalizesplitpattern(kind, pat)
83 83
84 84 def parsepatterns(pats):
85 85 """Parses a list of patterns into a typed pattern set."""
86 86 return set(normalizepattern(p) for p in pats)
87 87
88 88 def format(includes, excludes):
89 89 output = '[include]\n'
90 90 for i in sorted(includes - excludes):
91 91 output += i + '\n'
92 92 output += '[exclude]\n'
93 93 for e in sorted(excludes):
94 94 output += e + '\n'
95 95 return output
96 96
97 97 def match(root, include=None, exclude=None):
98 98 if not include:
99 99 # Passing empty include and empty exclude to matchmod.match()
100 100 # gives a matcher that matches everything, so explicitly use
101 101 # the nevermatcher.
102 102 return matchmod.never(root, '')
103 103 return matchmod.match(root, '', [], include=include or [],
104 104 exclude=exclude or [])
105 105
106 106 def needsexpansion(includes):
107 107 return [i for i in includes if i.startswith('include:')]
108 108
109 109 def load(repo):
110 110 try:
111 111 spec = repo.vfs.read(FILENAME)
112 112 except IOError as e:
113 113 # Treat "narrowspec does not exist" the same as "narrowspec file exists
114 114 # and is empty".
115 115 if e.errno == errno.ENOENT:
116 116 return set(), set()
117 117 raise
118 118 # maybe we should care about the profiles returned too
119 119 includepats, excludepats, profiles = sparse.parseconfig(repo.ui, spec,
120 120 'narrow')
121 121 if profiles:
122 122 raise error.Abort(_("including other spec files using '%include' is not"
123 123 " suported in narrowspec"))
124 124 return includepats, excludepats
125 125
126 126 def save(repo, includepats, excludepats):
127 127 spec = format(includepats, excludepats)
128 128 repo.vfs.write(FILENAME, spec)
129 129
130 130 def savebackup(repo, backupname):
131 131 if repository.NARROW_REQUIREMENT not in repo.requirements:
132 132 return
133 133 vfs = repo.vfs
134 134 vfs.tryunlink(backupname)
135 135 util.copyfile(vfs.join(FILENAME), vfs.join(backupname), hardlink=True)
136 136
137 137 def restorebackup(repo, backupname):
138 138 if repository.NARROW_REQUIREMENT not in repo.requirements:
139 139 return
140 repo.vfs.rename(backupname, FILENAME, checkambig=True)
140 repo.vfs.rename(backupname, FILENAME)
141 141
142 142 def clearbackup(repo, backupname):
143 143 if repository.NARROW_REQUIREMENT not in repo.requirements:
144 144 return
145 145 repo.vfs.unlink(backupname)
146 146
147 147 def restrictpatterns(req_includes, req_excludes, repo_includes, repo_excludes):
148 148 r""" Restricts the patterns according to repo settings,
149 149 results in a logical AND operation
150 150
151 151 :param req_includes: requested includes
152 152 :param req_excludes: requested excludes
153 153 :param repo_includes: repo includes
154 154 :param repo_excludes: repo excludes
155 155 :return: include patterns, exclude patterns, and invalid include patterns.
156 156
157 157 >>> restrictpatterns({'f1','f2'}, {}, ['f1'], [])
158 158 (set(['f1']), {}, [])
159 159 >>> restrictpatterns({'f1'}, {}, ['f1','f2'], [])
160 160 (set(['f1']), {}, [])
161 161 >>> restrictpatterns({'f1/fc1', 'f3/fc3'}, {}, ['f1','f2'], [])
162 162 (set(['f1/fc1']), {}, [])
163 163 >>> restrictpatterns({'f1_fc1'}, {}, ['f1','f2'], [])
164 164 ([], set(['path:.']), [])
165 165 >>> restrictpatterns({'f1/../f2/fc2'}, {}, ['f1','f2'], [])
166 166 (set(['f2/fc2']), {}, [])
167 167 >>> restrictpatterns({'f1/../f3/fc3'}, {}, ['f1','f2'], [])
168 168 ([], set(['path:.']), [])
169 169 >>> restrictpatterns({'f1/$non_exitent_var'}, {}, ['f1','f2'], [])
170 170 (set(['f1/$non_exitent_var']), {}, [])
171 171 """
172 172 res_excludes = set(req_excludes)
173 173 res_excludes.update(repo_excludes)
174 174 invalid_includes = []
175 175 if not req_includes:
176 176 res_includes = set(repo_includes)
177 177 elif 'path:.' not in repo_includes:
178 178 res_includes = []
179 179 for req_include in req_includes:
180 180 req_include = util.expandpath(util.normpath(req_include))
181 181 if req_include in repo_includes:
182 182 res_includes.append(req_include)
183 183 continue
184 184 valid = False
185 185 for repo_include in repo_includes:
186 186 if req_include.startswith(repo_include + '/'):
187 187 valid = True
188 188 res_includes.append(req_include)
189 189 break
190 190 if not valid:
191 191 invalid_includes.append(req_include)
192 192 if len(res_includes) == 0:
193 193 res_excludes = {'path:.'}
194 194 else:
195 195 res_includes = set(res_includes)
196 196 else:
197 197 res_includes = set(req_includes)
198 198 return res_includes, res_excludes, invalid_includes
General Comments 0
You need to be logged in to leave comments. Login now