Show More
@@ -1,203 +1,202 b'' | |||||
1 | # Perforce source for convert extension. |
|
1 | # Perforce source for convert extension. | |
2 | # |
|
2 | # | |
3 | # Copyright 2009, Frank Kingswood <frank@kingswood-consulting.co.uk> |
|
3 | # Copyright 2009, Frank Kingswood <frank@kingswood-consulting.co.uk> | |
4 | # |
|
4 | # | |
5 | # This software may be used and distributed according to the terms of the |
|
5 | # This software may be used and distributed according to the terms of the | |
6 | # GNU General Public License version 2 or any later version. |
|
6 | # GNU General Public License version 2 or any later version. | |
7 |
|
7 | |||
8 | from mercurial import util |
|
8 | from mercurial import util | |
9 | from mercurial.i18n import _ |
|
9 | from mercurial.i18n import _ | |
10 |
|
10 | |||
11 | from common import commit, converter_source, checktool, NoRepo |
|
11 | from common import commit, converter_source, checktool, NoRepo | |
12 | import marshal |
|
12 | import marshal | |
13 | import re |
|
13 | import re | |
14 |
|
14 | |||
15 | def loaditer(f): |
|
15 | def loaditer(f): | |
16 | "Yield the dictionary objects generated by p4" |
|
16 | "Yield the dictionary objects generated by p4" | |
17 | try: |
|
17 | try: | |
18 | while True: |
|
18 | while True: | |
19 | d = marshal.load(f) |
|
19 | d = marshal.load(f) | |
20 | if not d: |
|
20 | if not d: | |
21 | break |
|
21 | break | |
22 | yield d |
|
22 | yield d | |
23 | except EOFError: |
|
23 | except EOFError: | |
24 | pass |
|
24 | pass | |
25 |
|
25 | |||
26 | class p4_source(converter_source): |
|
26 | class p4_source(converter_source): | |
27 | def __init__(self, ui, path, rev=None): |
|
27 | def __init__(self, ui, path, rev=None): | |
28 | super(p4_source, self).__init__(ui, path, rev=rev) |
|
28 | super(p4_source, self).__init__(ui, path, rev=rev) | |
29 |
|
29 | |||
30 | if "/" in path and not path.startswith('//'): |
|
30 | if "/" in path and not path.startswith('//'): | |
31 | raise NoRepo(_('%s does not look like a P4 repository') % path) |
|
31 | raise NoRepo(_('%s does not look like a P4 repository') % path) | |
32 |
|
32 | |||
33 | checktool('p4', abort=False) |
|
33 | checktool('p4', abort=False) | |
34 |
|
34 | |||
35 | self.p4changes = {} |
|
35 | self.p4changes = {} | |
36 | self.heads = {} |
|
36 | self.heads = {} | |
37 | self.changeset = {} |
|
37 | self.changeset = {} | |
38 | self.files = {} |
|
38 | self.files = {} | |
39 | self.tags = {} |
|
39 | self.tags = {} | |
40 | self.lastbranch = {} |
|
40 | self.lastbranch = {} | |
41 | self.parent = {} |
|
41 | self.parent = {} | |
42 | self.encoding = "latin_1" |
|
42 | self.encoding = "latin_1" | |
43 | self.depotname = {} # mapping from local name to depot name |
|
43 | self.depotname = {} # mapping from local name to depot name | |
44 | self.re_type = re.compile( |
|
44 | self.re_type = re.compile( | |
45 | "([a-z]+)?(text|binary|symlink|apple|resource|unicode|utf\d+)" |
|
45 | "([a-z]+)?(text|binary|symlink|apple|resource|unicode|utf\d+)" | |
46 | "(\+\w+)?$") |
|
46 | "(\+\w+)?$") | |
47 | self.re_keywords = re.compile( |
|
47 | self.re_keywords = re.compile( | |
48 | r"\$(Id|Header|Date|DateTime|Change|File|Revision|Author)" |
|
48 | r"\$(Id|Header|Date|DateTime|Change|File|Revision|Author)" | |
49 | r":[^$\n]*\$") |
|
49 | r":[^$\n]*\$") | |
50 | self.re_keywords_old = re.compile("\$(Id|Header):[^$\n]*\$") |
|
50 | self.re_keywords_old = re.compile("\$(Id|Header):[^$\n]*\$") | |
51 |
|
51 | |||
52 | self._parse(ui, path) |
|
52 | self._parse(ui, path) | |
53 |
|
53 | |||
54 | def _parse_view(self, path): |
|
54 | def _parse_view(self, path): | |
55 | "Read changes affecting the path" |
|
55 | "Read changes affecting the path" | |
56 | cmd = 'p4 -G changes -s submitted %s' % util.shellquote(path) |
|
56 | cmd = 'p4 -G changes -s submitted %s' % util.shellquote(path) | |
57 | stdout = util.popen(cmd, mode='rb') |
|
57 | stdout = util.popen(cmd, mode='rb') | |
58 | for d in loaditer(stdout): |
|
58 | for d in loaditer(stdout): | |
59 | c = d.get("change", None) |
|
59 | c = d.get("change", None) | |
60 | if c: |
|
60 | if c: | |
61 | self.p4changes[c] = True |
|
61 | self.p4changes[c] = True | |
62 |
|
62 | |||
63 | def _parse(self, ui, path): |
|
63 | def _parse(self, ui, path): | |
64 | "Prepare list of P4 filenames and revisions to import" |
|
64 | "Prepare list of P4 filenames and revisions to import" | |
65 | ui.status(_('reading p4 views\n')) |
|
65 | ui.status(_('reading p4 views\n')) | |
66 |
|
66 | |||
67 | # read client spec or view |
|
67 | # read client spec or view | |
68 | if "/" in path: |
|
68 | if "/" in path: | |
69 | self._parse_view(path) |
|
69 | self._parse_view(path) | |
70 | if path.startswith("//") and path.endswith("/..."): |
|
70 | if path.startswith("//") and path.endswith("/..."): | |
71 | views = {path[:-3]:""} |
|
71 | views = {path[:-3]:""} | |
72 | else: |
|
72 | else: | |
73 | views = {"//": ""} |
|
73 | views = {"//": ""} | |
74 | else: |
|
74 | else: | |
75 | cmd = 'p4 -G client -o %s' % util.shellquote(path) |
|
75 | cmd = 'p4 -G client -o %s' % util.shellquote(path) | |
76 | clientspec = marshal.load(util.popen(cmd, mode='rb')) |
|
76 | clientspec = marshal.load(util.popen(cmd, mode='rb')) | |
77 |
|
77 | |||
78 | views = {} |
|
78 | views = {} | |
79 | for client in clientspec: |
|
79 | for client in clientspec: | |
80 | if client.startswith("View"): |
|
80 | if client.startswith("View"): | |
81 | sview, cview = clientspec[client].split() |
|
81 | sview, cview = clientspec[client].split() | |
82 | self._parse_view(sview) |
|
82 | self._parse_view(sview) | |
83 | if sview.endswith("...") and cview.endswith("..."): |
|
83 | if sview.endswith("...") and cview.endswith("..."): | |
84 | sview = sview[:-3] |
|
84 | sview = sview[:-3] | |
85 | cview = cview[:-3] |
|
85 | cview = cview[:-3] | |
86 | cview = cview[2:] |
|
86 | cview = cview[2:] | |
87 | cview = cview[cview.find("/") + 1:] |
|
87 | cview = cview[cview.find("/") + 1:] | |
88 | views[sview] = cview |
|
88 | views[sview] = cview | |
89 |
|
89 | |||
90 | # list of changes that affect our source files |
|
90 | # list of changes that affect our source files | |
91 | self.p4changes = self.p4changes.keys() |
|
91 | self.p4changes = self.p4changes.keys() | |
92 | self.p4changes.sort(key=int) |
|
92 | self.p4changes.sort(key=int) | |
93 |
|
93 | |||
94 | # list with depot pathnames, longest first |
|
94 | # list with depot pathnames, longest first | |
95 | vieworder = views.keys() |
|
95 | vieworder = views.keys() | |
96 | vieworder.sort(key=len, reverse=True) |
|
96 | vieworder.sort(key=len, reverse=True) | |
97 |
|
97 | |||
98 | # handle revision limiting |
|
98 | # handle revision limiting | |
99 | startrev = self.ui.config('convert', 'p4.startrev', default=0) |
|
99 | startrev = self.ui.config('convert', 'p4.startrev', default=0) | |
100 | self.p4changes = [x for x in self.p4changes |
|
100 | self.p4changes = [x for x in self.p4changes | |
101 | if ((not startrev or int(x) >= int(startrev)) and |
|
101 | if ((not startrev or int(x) >= int(startrev)) and | |
102 | (not self.rev or int(x) <= int(self.rev)))] |
|
102 | (not self.rev or int(x) <= int(self.rev)))] | |
103 |
|
103 | |||
104 | # now read the full changelists to get the list of file revisions |
|
104 | # now read the full changelists to get the list of file revisions | |
105 | ui.status(_('collecting p4 changelists\n')) |
|
105 | ui.status(_('collecting p4 changelists\n')) | |
106 | lastid = None |
|
106 | lastid = None | |
107 | for change in self.p4changes: |
|
107 | for change in self.p4changes: | |
108 | cmd = "p4 -G describe %s" % change |
|
108 | cmd = "p4 -G describe -s %s" % change | |
109 | stdout = util.popen(cmd, mode='rb') |
|
109 | stdout = util.popen(cmd, mode='rb') | |
110 | d = marshal.load(stdout) |
|
110 | d = marshal.load(stdout) | |
111 |
|
||||
112 | desc = self.recode(d["desc"]) |
|
111 | desc = self.recode(d["desc"]) | |
113 | shortdesc = desc.split("\n", 1)[0] |
|
112 | shortdesc = desc.split("\n", 1)[0] | |
114 | t = '%s %s' % (d["change"], repr(shortdesc)[1:-1]) |
|
113 | t = '%s %s' % (d["change"], repr(shortdesc)[1:-1]) | |
115 | ui.status(util.ellipsis(t, 80) + '\n') |
|
114 | ui.status(util.ellipsis(t, 80) + '\n') | |
116 |
|
115 | |||
117 | if lastid: |
|
116 | if lastid: | |
118 | parents = [lastid] |
|
117 | parents = [lastid] | |
119 | else: |
|
118 | else: | |
120 | parents = [] |
|
119 | parents = [] | |
121 |
|
120 | |||
122 | date = (int(d["time"]), 0) # timezone not set |
|
121 | date = (int(d["time"]), 0) # timezone not set | |
123 | c = commit(author=self.recode(d["user"]), date=util.datestr(date), |
|
122 | c = commit(author=self.recode(d["user"]), date=util.datestr(date), | |
124 | parents=parents, desc=desc, branch='', |
|
123 | parents=parents, desc=desc, branch='', | |
125 | extra={"p4": change}) |
|
124 | extra={"p4": change}) | |
126 |
|
125 | |||
127 | files = [] |
|
126 | files = [] | |
128 | i = 0 |
|
127 | i = 0 | |
129 | while ("depotFile%d" % i) in d and ("rev%d" % i) in d: |
|
128 | while ("depotFile%d" % i) in d and ("rev%d" % i) in d: | |
130 | oldname = d["depotFile%d" % i] |
|
129 | oldname = d["depotFile%d" % i] | |
131 | filename = None |
|
130 | filename = None | |
132 | for v in vieworder: |
|
131 | for v in vieworder: | |
133 | if oldname.startswith(v): |
|
132 | if oldname.startswith(v): | |
134 | filename = views[v] + oldname[len(v):] |
|
133 | filename = views[v] + oldname[len(v):] | |
135 | break |
|
134 | break | |
136 | if filename: |
|
135 | if filename: | |
137 | files.append((filename, d["rev%d" % i])) |
|
136 | files.append((filename, d["rev%d" % i])) | |
138 | self.depotname[filename] = oldname |
|
137 | self.depotname[filename] = oldname | |
139 | i += 1 |
|
138 | i += 1 | |
140 | self.changeset[change] = c |
|
139 | self.changeset[change] = c | |
141 | self.files[change] = files |
|
140 | self.files[change] = files | |
142 | lastid = change |
|
141 | lastid = change | |
143 |
|
142 | |||
144 | if lastid: |
|
143 | if lastid: | |
145 | self.heads = [lastid] |
|
144 | self.heads = [lastid] | |
146 |
|
145 | |||
147 | def getheads(self): |
|
146 | def getheads(self): | |
148 | return self.heads |
|
147 | return self.heads | |
149 |
|
148 | |||
150 | def getfile(self, name, rev): |
|
149 | def getfile(self, name, rev): | |
151 | cmd = 'p4 -G print %s' \ |
|
150 | cmd = 'p4 -G print %s' \ | |
152 | % util.shellquote("%s#%s" % (self.depotname[name], rev)) |
|
151 | % util.shellquote("%s#%s" % (self.depotname[name], rev)) | |
153 | stdout = util.popen(cmd, mode='rb') |
|
152 | stdout = util.popen(cmd, mode='rb') | |
154 |
|
153 | |||
155 | mode = None |
|
154 | mode = None | |
156 | contents = "" |
|
155 | contents = "" | |
157 | keywords = None |
|
156 | keywords = None | |
158 |
|
157 | |||
159 | for d in loaditer(stdout): |
|
158 | for d in loaditer(stdout): | |
160 | code = d["code"] |
|
159 | code = d["code"] | |
161 | data = d.get("data") |
|
160 | data = d.get("data") | |
162 |
|
161 | |||
163 | if code == "error": |
|
162 | if code == "error": | |
164 | raise IOError(d["generic"], data) |
|
163 | raise IOError(d["generic"], data) | |
165 |
|
164 | |||
166 | elif code == "stat": |
|
165 | elif code == "stat": | |
167 | p4type = self.re_type.match(d["type"]) |
|
166 | p4type = self.re_type.match(d["type"]) | |
168 | if p4type: |
|
167 | if p4type: | |
169 | mode = "" |
|
168 | mode = "" | |
170 | flags = (p4type.group(1) or "") + (p4type.group(3) or "") |
|
169 | flags = (p4type.group(1) or "") + (p4type.group(3) or "") | |
171 | if "x" in flags: |
|
170 | if "x" in flags: | |
172 | mode = "x" |
|
171 | mode = "x" | |
173 | if p4type.group(2) == "symlink": |
|
172 | if p4type.group(2) == "symlink": | |
174 | mode = "l" |
|
173 | mode = "l" | |
175 | if "ko" in flags: |
|
174 | if "ko" in flags: | |
176 | keywords = self.re_keywords_old |
|
175 | keywords = self.re_keywords_old | |
177 | elif "k" in flags: |
|
176 | elif "k" in flags: | |
178 | keywords = self.re_keywords |
|
177 | keywords = self.re_keywords | |
179 |
|
178 | |||
180 | elif code == "text" or code == "binary": |
|
179 | elif code == "text" or code == "binary": | |
181 | contents += data |
|
180 | contents += data | |
182 |
|
181 | |||
183 | if mode is None: |
|
182 | if mode is None: | |
184 | raise IOError(0, "bad stat") |
|
183 | raise IOError(0, "bad stat") | |
185 |
|
184 | |||
186 | if keywords: |
|
185 | if keywords: | |
187 | contents = keywords.sub("$\\1$", contents) |
|
186 | contents = keywords.sub("$\\1$", contents) | |
188 | if mode == "l" and contents.endswith("\n"): |
|
187 | if mode == "l" and contents.endswith("\n"): | |
189 | contents = contents[:-1] |
|
188 | contents = contents[:-1] | |
190 |
|
189 | |||
191 | return contents, mode |
|
190 | return contents, mode | |
192 |
|
191 | |||
193 | def getchanges(self, rev): |
|
192 | def getchanges(self, rev): | |
194 | return self.files[rev], {} |
|
193 | return self.files[rev], {} | |
195 |
|
194 | |||
196 | def getcommit(self, rev): |
|
195 | def getcommit(self, rev): | |
197 | return self.changeset[rev] |
|
196 | return self.changeset[rev] | |
198 |
|
197 | |||
199 | def gettags(self): |
|
198 | def gettags(self): | |
200 | return self.tags |
|
199 | return self.tags | |
201 |
|
200 | |||
202 | def getchangedfiles(self, rev, i): |
|
201 | def getchangedfiles(self, rev, i): | |
203 | return sorted([x[0] for x in self.files[rev]]) |
|
202 | return sorted([x[0] for x in self.files[rev]]) |
General Comments 0
You need to be logged in to leave comments.
Login now