]>
Commit | Line | Data |
---|---|---|
86949eef SH |
1 | #!/usr/bin/env python |
2 | # | |
3 | # git-p4.py -- A tool for bidirectional operation between a Perforce depot and git. | |
4 | # | |
c8cbbee9 SH |
5 | # Author: Simon Hausmann <simon@lst.de> |
6 | # Copyright: 2007 Simon Hausmann <simon@lst.de> | |
83dce55a | 7 | # 2007 Trolltech ASA |
86949eef SH |
8 | # License: MIT <http://www.opensource.org/licenses/mit-license.php> |
9 | # | |
10 | ||
08483580 | 11 | import optparse, sys, os, marshal, popen2, subprocess, shelve |
25df95cc | 12 | import tempfile, getopt, sha, os.path, time, platform |
ce6f33c8 | 13 | import re |
8b41a97f | 14 | |
b984733c | 15 | from sets import Set; |
4f5cf76a | 16 | |
4addad22 | 17 | verbose = False |
86949eef | 18 | |
86dff6b6 HWN |
19 | def die(msg): |
20 | if verbose: | |
21 | raise Exception(msg) | |
22 | else: | |
23 | sys.stderr.write(msg + "\n") | |
24 | sys.exit(1) | |
25 | ||
bce4c5fc | 26 | def write_pipe(c, str): |
4addad22 | 27 | if verbose: |
86dff6b6 | 28 | sys.stderr.write('Writing pipe: %s\n' % c) |
b016d397 | 29 | |
bce4c5fc | 30 | pipe = os.popen(c, 'w') |
b016d397 | 31 | val = pipe.write(str) |
bce4c5fc | 32 | if pipe.close(): |
86dff6b6 | 33 | die('Command failed: %s' % c) |
b016d397 HWN |
34 | |
35 | return val | |
36 | ||
4addad22 HWN |
37 | def read_pipe(c, ignore_error=False): |
38 | if verbose: | |
86dff6b6 | 39 | sys.stderr.write('Reading pipe: %s\n' % c) |
8b41a97f | 40 | |
bce4c5fc | 41 | pipe = os.popen(c, 'rb') |
b016d397 | 42 | val = pipe.read() |
4addad22 | 43 | if pipe.close() and not ignore_error: |
86dff6b6 | 44 | die('Command failed: %s' % c) |
b016d397 HWN |
45 | |
46 | return val | |
47 | ||
48 | ||
bce4c5fc | 49 | def read_pipe_lines(c): |
4addad22 | 50 | if verbose: |
86dff6b6 | 51 | sys.stderr.write('Reading pipe: %s\n' % c) |
b016d397 | 52 | ## todo: check return status |
bce4c5fc | 53 | pipe = os.popen(c, 'rb') |
b016d397 | 54 | val = pipe.readlines() |
bce4c5fc | 55 | if pipe.close(): |
86dff6b6 | 56 | die('Command failed: %s' % c) |
b016d397 HWN |
57 | |
58 | return val | |
caace111 | 59 | |
6754a299 | 60 | def system(cmd): |
4addad22 | 61 | if verbose: |
bb6e09b2 | 62 | sys.stderr.write("executing %s\n" % cmd) |
6754a299 HWN |
63 | if os.system(cmd) != 0: |
64 | die("command failed: %s" % cmd) | |
65 | ||
b9fc6ea9 DB |
66 | def isP4Exec(kind): |
67 | """Determine if a Perforce 'kind' should have execute permission | |
68 | ||
69 | 'p4 help filetypes' gives a list of the types. If it starts with 'x', | |
70 | or x follows one of a few letters. Otherwise, if there is an 'x' after | |
71 | a plus sign, it is also executable""" | |
72 | return (re.search(r"(^[cku]?x)|\+.*x", kind) != None) | |
73 | ||
c65b670e CP |
74 | def setP4ExecBit(file, mode): |
75 | # Reopens an already open file and changes the execute bit to match | |
76 | # the execute bit setting in the passed in mode. | |
77 | ||
78 | p4Type = "+x" | |
79 | ||
80 | if not isModeExec(mode): | |
81 | p4Type = getP4OpenedType(file) | |
82 | p4Type = re.sub('^([cku]?)x(.*)', '\\1\\2', p4Type) | |
83 | p4Type = re.sub('(.*?\+.*?)x(.*?)', '\\1\\2', p4Type) | |
84 | if p4Type[-1] == "+": | |
85 | p4Type = p4Type[0:-1] | |
86 | ||
87 | system("p4 reopen -t %s %s" % (p4Type, file)) | |
88 | ||
89 | def getP4OpenedType(file): | |
90 | # Returns the perforce file type for the given file. | |
91 | ||
92 | result = read_pipe("p4 opened %s" % file) | |
f3e5ae4f | 93 | match = re.match(".*\((.+)\)\r?$", result) |
c65b670e CP |
94 | if match: |
95 | return match.group(1) | |
96 | else: | |
f3e5ae4f | 97 | die("Could not determine file type for %s (result: '%s')" % (file, result)) |
c65b670e | 98 | |
b43b0a3c CP |
99 | def diffTreePattern(): |
100 | # This is a simple generator for the diff tree regex pattern. This could be | |
101 | # a class variable if this and parseDiffTreeEntry were a part of a class. | |
102 | pattern = re.compile(':(\d+) (\d+) (\w+) (\w+) ([A-Z])(\d+)?\t(.*?)((\t(.*))|$)') | |
103 | while True: | |
104 | yield pattern | |
105 | ||
106 | def parseDiffTreeEntry(entry): | |
107 | """Parses a single diff tree entry into its component elements. | |
108 | ||
109 | See git-diff-tree(1) manpage for details about the format of the diff | |
110 | output. This method returns a dictionary with the following elements: | |
111 | ||
112 | src_mode - The mode of the source file | |
113 | dst_mode - The mode of the destination file | |
114 | src_sha1 - The sha1 for the source file | |
115 | dst_sha1 - The sha1 fr the destination file | |
116 | status - The one letter status of the diff (i.e. 'A', 'M', 'D', etc) | |
117 | status_score - The score for the status (applicable for 'C' and 'R' | |
118 | statuses). This is None if there is no score. | |
119 | src - The path for the source file. | |
120 | dst - The path for the destination file. This is only present for | |
121 | copy or renames. If it is not present, this is None. | |
122 | ||
123 | If the pattern is not matched, None is returned.""" | |
124 | ||
125 | match = diffTreePattern().next().match(entry) | |
126 | if match: | |
127 | return { | |
128 | 'src_mode': match.group(1), | |
129 | 'dst_mode': match.group(2), | |
130 | 'src_sha1': match.group(3), | |
131 | 'dst_sha1': match.group(4), | |
132 | 'status': match.group(5), | |
133 | 'status_score': match.group(6), | |
134 | 'src': match.group(7), | |
135 | 'dst': match.group(10) | |
136 | } | |
137 | return None | |
138 | ||
c65b670e CP |
139 | def isModeExec(mode): |
140 | # Returns True if the given git mode represents an executable file, | |
141 | # otherwise False. | |
142 | return mode[-3:] == "755" | |
143 | ||
144 | def isModeExecChanged(src_mode, dst_mode): | |
145 | return isModeExec(src_mode) != isModeExec(dst_mode) | |
146 | ||
9f90c733 | 147 | def p4CmdList(cmd, stdin=None, stdin_mode='w+b'): |
86949eef | 148 | cmd = "p4 -G %s" % cmd |
6a49f8e2 HWN |
149 | if verbose: |
150 | sys.stderr.write("Opening pipe: %s\n" % cmd) | |
9f90c733 SL |
151 | |
152 | # Use a temporary file to avoid deadlocks without | |
153 | # subprocess.communicate(), which would put another copy | |
154 | # of stdout into memory. | |
155 | stdin_file = None | |
156 | if stdin is not None: | |
157 | stdin_file = tempfile.TemporaryFile(prefix='p4-stdin', mode=stdin_mode) | |
158 | stdin_file.write(stdin) | |
159 | stdin_file.flush() | |
160 | stdin_file.seek(0) | |
161 | ||
162 | p4 = subprocess.Popen(cmd, shell=True, | |
163 | stdin=stdin_file, | |
164 | stdout=subprocess.PIPE) | |
86949eef SH |
165 | |
166 | result = [] | |
167 | try: | |
168 | while True: | |
9f90c733 | 169 | entry = marshal.load(p4.stdout) |
86949eef SH |
170 | result.append(entry) |
171 | except EOFError: | |
172 | pass | |
9f90c733 SL |
173 | exitCode = p4.wait() |
174 | if exitCode != 0: | |
ac3e0d79 SH |
175 | entry = {} |
176 | entry["p4ExitCode"] = exitCode | |
177 | result.append(entry) | |
86949eef SH |
178 | |
179 | return result | |
180 | ||
181 | def p4Cmd(cmd): | |
182 | list = p4CmdList(cmd) | |
183 | result = {} | |
184 | for entry in list: | |
185 | result.update(entry) | |
186 | return result; | |
187 | ||
cb2c9db5 SH |
188 | def p4Where(depotPath): |
189 | if not depotPath.endswith("/"): | |
190 | depotPath += "/" | |
191 | output = p4Cmd("where %s..." % depotPath) | |
dc524036 SH |
192 | if output["code"] == "error": |
193 | return "" | |
cb2c9db5 SH |
194 | clientPath = "" |
195 | if "path" in output: | |
196 | clientPath = output.get("path") | |
197 | elif "data" in output: | |
198 | data = output.get("data") | |
199 | lastSpace = data.rfind(" ") | |
200 | clientPath = data[lastSpace + 1:] | |
201 | ||
202 | if clientPath.endswith("..."): | |
203 | clientPath = clientPath[:-3] | |
204 | return clientPath | |
205 | ||
86949eef | 206 | def currentGitBranch(): |
b25b2065 | 207 | return read_pipe("git name-rev HEAD").split(" ")[1].strip() |
86949eef | 208 | |
4f5cf76a | 209 | def isValidGitDir(path): |
bb6e09b2 HWN |
210 | if (os.path.exists(path + "/HEAD") |
211 | and os.path.exists(path + "/refs") and os.path.exists(path + "/objects")): | |
4f5cf76a SH |
212 | return True; |
213 | return False | |
214 | ||
463e8af6 | 215 | def parseRevision(ref): |
b25b2065 | 216 | return read_pipe("git rev-parse %s" % ref).strip() |
463e8af6 | 217 | |
6ae8de88 SH |
218 | def extractLogMessageFromGitCommit(commit): |
219 | logMessage = "" | |
b016d397 HWN |
220 | |
221 | ## fixme: title is first line of commit, not 1st paragraph. | |
6ae8de88 | 222 | foundTitle = False |
b016d397 | 223 | for log in read_pipe_lines("git cat-file commit %s" % commit): |
6ae8de88 SH |
224 | if not foundTitle: |
225 | if len(log) == 1: | |
1c094184 | 226 | foundTitle = True |
6ae8de88 SH |
227 | continue |
228 | ||
229 | logMessage += log | |
230 | return logMessage | |
231 | ||
bb6e09b2 | 232 | def extractSettingsGitLog(log): |
6ae8de88 SH |
233 | values = {} |
234 | for line in log.split("\n"): | |
235 | line = line.strip() | |
6326aa58 HWN |
236 | m = re.search (r"^ *\[git-p4: (.*)\]$", line) |
237 | if not m: | |
238 | continue | |
239 | ||
240 | assignments = m.group(1).split (':') | |
241 | for a in assignments: | |
242 | vals = a.split ('=') | |
243 | key = vals[0].strip() | |
244 | val = ('='.join (vals[1:])).strip() | |
245 | if val.endswith ('\"') and val.startswith('"'): | |
246 | val = val[1:-1] | |
247 | ||
248 | values[key] = val | |
249 | ||
845b42cb SH |
250 | paths = values.get("depot-paths") |
251 | if not paths: | |
252 | paths = values.get("depot-path") | |
a3fdd579 SH |
253 | if paths: |
254 | values['depot-paths'] = paths.split(',') | |
bb6e09b2 | 255 | return values |
6ae8de88 | 256 | |
8136a639 | 257 | def gitBranchExists(branch): |
bb6e09b2 HWN |
258 | proc = subprocess.Popen(["git", "rev-parse", branch], |
259 | stderr=subprocess.PIPE, stdout=subprocess.PIPE); | |
caace111 | 260 | return proc.wait() == 0; |
8136a639 | 261 | |
01265103 | 262 | def gitConfig(key): |
4addad22 | 263 | return read_pipe("git config %s" % key, ignore_error=True).strip() |
01265103 | 264 | |
062410bb SH |
265 | def p4BranchesInGit(branchesAreInRemotes = True): |
266 | branches = {} | |
267 | ||
268 | cmdline = "git rev-parse --symbolic " | |
269 | if branchesAreInRemotes: | |
270 | cmdline += " --remotes" | |
271 | else: | |
272 | cmdline += " --branches" | |
273 | ||
274 | for line in read_pipe_lines(cmdline): | |
275 | line = line.strip() | |
276 | ||
277 | ## only import to p4/ | |
278 | if not line.startswith('p4/') or line == "p4/HEAD": | |
279 | continue | |
280 | branch = line | |
281 | ||
282 | # strip off p4 | |
283 | branch = re.sub ("^p4/", "", line) | |
284 | ||
285 | branches[branch] = parseRevision(line) | |
286 | return branches | |
287 | ||
9ceab363 | 288 | def findUpstreamBranchPoint(head = "HEAD"): |
86506fe5 SH |
289 | branches = p4BranchesInGit() |
290 | # map from depot-path to branch name | |
291 | branchByDepotPath = {} | |
292 | for branch in branches.keys(): | |
293 | tip = branches[branch] | |
294 | log = extractLogMessageFromGitCommit(tip) | |
295 | settings = extractSettingsGitLog(log) | |
296 | if settings.has_key("depot-paths"): | |
297 | paths = ",".join(settings["depot-paths"]) | |
298 | branchByDepotPath[paths] = "remotes/p4/" + branch | |
299 | ||
27d2d811 | 300 | settings = None |
27d2d811 SH |
301 | parent = 0 |
302 | while parent < 65535: | |
9ceab363 | 303 | commit = head + "~%s" % parent |
27d2d811 SH |
304 | log = extractLogMessageFromGitCommit(commit) |
305 | settings = extractSettingsGitLog(log) | |
86506fe5 SH |
306 | if settings.has_key("depot-paths"): |
307 | paths = ",".join(settings["depot-paths"]) | |
308 | if branchByDepotPath.has_key(paths): | |
309 | return [branchByDepotPath[paths], settings] | |
27d2d811 | 310 | |
86506fe5 | 311 | parent = parent + 1 |
27d2d811 | 312 | |
86506fe5 | 313 | return ["", settings] |
27d2d811 | 314 | |
5ca44617 SH |
315 | def createOrUpdateBranchesFromOrigin(localRefPrefix = "refs/remotes/p4/", silent=True): |
316 | if not silent: | |
317 | print ("Creating/updating branch(es) in %s based on origin branch(es)" | |
318 | % localRefPrefix) | |
319 | ||
320 | originPrefix = "origin/p4/" | |
321 | ||
322 | for line in read_pipe_lines("git rev-parse --symbolic --remotes"): | |
323 | line = line.strip() | |
324 | if (not line.startswith(originPrefix)) or line.endswith("HEAD"): | |
325 | continue | |
326 | ||
327 | headName = line[len(originPrefix):] | |
328 | remoteHead = localRefPrefix + headName | |
329 | originHead = line | |
330 | ||
331 | original = extractSettingsGitLog(extractLogMessageFromGitCommit(originHead)) | |
332 | if (not original.has_key('depot-paths') | |
333 | or not original.has_key('change')): | |
334 | continue | |
335 | ||
336 | update = False | |
337 | if not gitBranchExists(remoteHead): | |
338 | if verbose: | |
339 | print "creating %s" % remoteHead | |
340 | update = True | |
341 | else: | |
342 | settings = extractSettingsGitLog(extractLogMessageFromGitCommit(remoteHead)) | |
343 | if settings.has_key('change') > 0: | |
344 | if settings['depot-paths'] == original['depot-paths']: | |
345 | originP4Change = int(original['change']) | |
346 | p4Change = int(settings['change']) | |
347 | if originP4Change > p4Change: | |
348 | print ("%s (%s) is newer than %s (%s). " | |
349 | "Updating p4 branch from origin." | |
350 | % (originHead, originP4Change, | |
351 | remoteHead, p4Change)) | |
352 | update = True | |
353 | else: | |
354 | print ("Ignoring: %s was imported from %s while " | |
355 | "%s was imported from %s" | |
356 | % (originHead, ','.join(original['depot-paths']), | |
357 | remoteHead, ','.join(settings['depot-paths']))) | |
358 | ||
359 | if update: | |
360 | system("git update-ref %s %s" % (remoteHead, originHead)) | |
361 | ||
362 | def originP4BranchesExist(): | |
363 | return gitBranchExists("origin") or gitBranchExists("origin/p4") or gitBranchExists("origin/p4/master") | |
364 | ||
4f6432d8 SH |
365 | def p4ChangesForPaths(depotPaths, changeRange): |
366 | assert depotPaths | |
367 | output = read_pipe_lines("p4 changes " + ' '.join (["%s...%s" % (p, changeRange) | |
368 | for p in depotPaths])) | |
369 | ||
370 | changes = [] | |
371 | for line in output: | |
372 | changeNum = line.split(" ")[1] | |
373 | changes.append(int(changeNum)) | |
374 | ||
375 | changes.sort() | |
376 | return changes | |
377 | ||
b984733c SH |
378 | class Command: |
379 | def __init__(self): | |
380 | self.usage = "usage: %prog [options]" | |
8910ac0e | 381 | self.needsGit = True |
b984733c SH |
382 | |
383 | class P4Debug(Command): | |
86949eef | 384 | def __init__(self): |
6ae8de88 | 385 | Command.__init__(self) |
86949eef | 386 | self.options = [ |
b1ce9447 HWN |
387 | optparse.make_option("--verbose", dest="verbose", action="store_true", |
388 | default=False), | |
4addad22 | 389 | ] |
c8c39116 | 390 | self.description = "A tool to debug the output of p4 -G." |
8910ac0e | 391 | self.needsGit = False |
b1ce9447 | 392 | self.verbose = False |
86949eef SH |
393 | |
394 | def run(self, args): | |
b1ce9447 | 395 | j = 0 |
86949eef | 396 | for output in p4CmdList(" ".join(args)): |
b1ce9447 HWN |
397 | print 'Element: %d' % j |
398 | j += 1 | |
86949eef | 399 | print output |
b984733c | 400 | return True |
86949eef | 401 | |
5834684d SH |
402 | class P4RollBack(Command): |
403 | def __init__(self): | |
404 | Command.__init__(self) | |
405 | self.options = [ | |
0c66a783 SH |
406 | optparse.make_option("--verbose", dest="verbose", action="store_true"), |
407 | optparse.make_option("--local", dest="rollbackLocalBranches", action="store_true") | |
5834684d SH |
408 | ] |
409 | self.description = "A tool to debug the multi-branch import. Don't use :)" | |
52102d47 | 410 | self.verbose = False |
0c66a783 | 411 | self.rollbackLocalBranches = False |
5834684d SH |
412 | |
413 | def run(self, args): | |
414 | if len(args) != 1: | |
415 | return False | |
416 | maxChange = int(args[0]) | |
0c66a783 | 417 | |
ad192f28 | 418 | if "p4ExitCode" in p4Cmd("changes -m 1"): |
66a2f523 SH |
419 | die("Problems executing p4"); |
420 | ||
0c66a783 SH |
421 | if self.rollbackLocalBranches: |
422 | refPrefix = "refs/heads/" | |
b016d397 | 423 | lines = read_pipe_lines("git rev-parse --symbolic --branches") |
0c66a783 SH |
424 | else: |
425 | refPrefix = "refs/remotes/" | |
b016d397 | 426 | lines = read_pipe_lines("git rev-parse --symbolic --remotes") |
0c66a783 SH |
427 | |
428 | for line in lines: | |
429 | if self.rollbackLocalBranches or (line.startswith("p4/") and line != "p4/HEAD\n"): | |
b25b2065 HWN |
430 | line = line.strip() |
431 | ref = refPrefix + line | |
5834684d | 432 | log = extractLogMessageFromGitCommit(ref) |
bb6e09b2 HWN |
433 | settings = extractSettingsGitLog(log) |
434 | ||
435 | depotPaths = settings['depot-paths'] | |
436 | change = settings['change'] | |
437 | ||
5834684d | 438 | changed = False |
52102d47 | 439 | |
6326aa58 HWN |
440 | if len(p4Cmd("changes -m 1 " + ' '.join (['%s...@%s' % (p, maxChange) |
441 | for p in depotPaths]))) == 0: | |
52102d47 SH |
442 | print "Branch %s did not exist at change %s, deleting." % (ref, maxChange) |
443 | system("git update-ref -d %s `git rev-parse %s`" % (ref, ref)) | |
444 | continue | |
445 | ||
bb6e09b2 | 446 | while change and int(change) > maxChange: |
5834684d | 447 | changed = True |
52102d47 SH |
448 | if self.verbose: |
449 | print "%s is at %s ; rewinding towards %s" % (ref, change, maxChange) | |
5834684d SH |
450 | system("git update-ref %s \"%s^\"" % (ref, ref)) |
451 | log = extractLogMessageFromGitCommit(ref) | |
bb6e09b2 HWN |
452 | settings = extractSettingsGitLog(log) |
453 | ||
454 | ||
455 | depotPaths = settings['depot-paths'] | |
456 | change = settings['change'] | |
5834684d SH |
457 | |
458 | if changed: | |
52102d47 | 459 | print "%s rewound to %s" % (ref, change) |
5834684d SH |
460 | |
461 | return True | |
462 | ||
711544b0 | 463 | class P4Submit(Command): |
4f5cf76a | 464 | def __init__(self): |
b984733c | 465 | Command.__init__(self) |
4f5cf76a | 466 | self.options = [ |
4addad22 | 467 | optparse.make_option("--verbose", dest="verbose", action="store_true"), |
4f5cf76a | 468 | optparse.make_option("--origin", dest="origin"), |
d9a5f25b | 469 | optparse.make_option("-M", dest="detectRename", action="store_true"), |
4f5cf76a SH |
470 | ] |
471 | self.description = "Submit changes from git to the perforce depot." | |
c9b50e63 | 472 | self.usage += " [name of git branch to submit into perforce depot]" |
4f5cf76a | 473 | self.interactive = True |
9512497b | 474 | self.origin = "" |
d9a5f25b | 475 | self.detectRename = False |
b0d10df7 | 476 | self.verbose = False |
f7baba8b | 477 | self.isWindows = (platform.system() == "Windows") |
4f5cf76a | 478 | |
4f5cf76a SH |
479 | def check(self): |
480 | if len(p4CmdList("opened ...")) > 0: | |
481 | die("You have files opened with perforce! Close them before starting the sync.") | |
482 | ||
edae1e2f SH |
483 | # replaces everything between 'Description:' and the next P4 submit template field with the |
484 | # commit message | |
4f5cf76a SH |
485 | def prepareLogMessage(self, template, message): |
486 | result = "" | |
487 | ||
edae1e2f SH |
488 | inDescriptionSection = False |
489 | ||
4f5cf76a SH |
490 | for line in template.split("\n"): |
491 | if line.startswith("#"): | |
492 | result += line + "\n" | |
493 | continue | |
494 | ||
edae1e2f SH |
495 | if inDescriptionSection: |
496 | if line.startswith("Files:"): | |
497 | inDescriptionSection = False | |
498 | else: | |
499 | continue | |
500 | else: | |
501 | if line.startswith("Description:"): | |
502 | inDescriptionSection = True | |
503 | line += "\n" | |
504 | for messageLine in message.split("\n"): | |
505 | line += "\t" + messageLine + "\n" | |
506 | ||
507 | result += line + "\n" | |
4f5cf76a SH |
508 | |
509 | return result | |
510 | ||
ea99c3ae SH |
511 | def prepareSubmitTemplate(self): |
512 | # remove lines in the Files section that show changes to files outside the depot path we're committing into | |
513 | template = "" | |
514 | inFilesSection = False | |
515 | for line in read_pipe_lines("p4 change -o"): | |
f3e5ae4f MSO |
516 | if line.endswith("\r\n"): |
517 | line = line[:-2] + "\n" | |
ea99c3ae SH |
518 | if inFilesSection: |
519 | if line.startswith("\t"): | |
520 | # path starts and ends with a tab | |
521 | path = line[1:] | |
522 | lastTab = path.rfind("\t") | |
523 | if lastTab != -1: | |
524 | path = path[:lastTab] | |
525 | if not path.startswith(self.depotPath): | |
526 | continue | |
527 | else: | |
528 | inFilesSection = False | |
529 | else: | |
530 | if line.startswith("Files:"): | |
531 | inFilesSection = True | |
532 | ||
533 | template += line | |
534 | ||
535 | return template | |
536 | ||
7cb5cbef | 537 | def applyCommit(self, id): |
0e36f2d7 SH |
538 | print "Applying %s" % (read_pipe("git log --max-count=1 --pretty=oneline %s" % id)) |
539 | diffOpts = ("", "-M")[self.detectRename] | |
540 | diff = read_pipe_lines("git diff-tree -r %s \"%s^\" \"%s\"" % (diffOpts, id, id)) | |
4f5cf76a SH |
541 | filesToAdd = set() |
542 | filesToDelete = set() | |
d336c158 | 543 | editedFiles = set() |
c65b670e | 544 | filesToChangeExecBit = {} |
4f5cf76a | 545 | for line in diff: |
b43b0a3c CP |
546 | diff = parseDiffTreeEntry(line) |
547 | modifier = diff['status'] | |
548 | path = diff['src'] | |
4f5cf76a | 549 | if modifier == "M": |
d336c158 | 550 | system("p4 edit \"%s\"" % path) |
c65b670e CP |
551 | if isModeExecChanged(diff['src_mode'], diff['dst_mode']): |
552 | filesToChangeExecBit[path] = diff['dst_mode'] | |
d336c158 | 553 | editedFiles.add(path) |
4f5cf76a SH |
554 | elif modifier == "A": |
555 | filesToAdd.add(path) | |
c65b670e | 556 | filesToChangeExecBit[path] = diff['dst_mode'] |
4f5cf76a SH |
557 | if path in filesToDelete: |
558 | filesToDelete.remove(path) | |
559 | elif modifier == "D": | |
560 | filesToDelete.add(path) | |
561 | if path in filesToAdd: | |
562 | filesToAdd.remove(path) | |
d9a5f25b | 563 | elif modifier == "R": |
b43b0a3c | 564 | src, dest = diff['src'], diff['dst'] |
d9a5f25b CP |
565 | system("p4 integrate -Dt \"%s\" \"%s\"" % (src, dest)) |
566 | system("p4 edit \"%s\"" % (dest)) | |
c65b670e CP |
567 | if isModeExecChanged(diff['src_mode'], diff['dst_mode']): |
568 | filesToChangeExecBit[dest] = diff['dst_mode'] | |
d9a5f25b CP |
569 | os.unlink(dest) |
570 | editedFiles.add(dest) | |
571 | filesToDelete.add(src) | |
4f5cf76a SH |
572 | else: |
573 | die("unknown modifier %s for %s" % (modifier, path)) | |
574 | ||
0e36f2d7 | 575 | diffcmd = "git format-patch -k --stdout \"%s^\"..\"%s\"" % (id, id) |
47a130b7 | 576 | patchcmd = diffcmd + " | git apply " |
c1b296b9 SH |
577 | tryPatchCmd = patchcmd + "--check -" |
578 | applyPatchCmd = patchcmd + "--check --apply -" | |
51a2640a | 579 | |
47a130b7 | 580 | if os.system(tryPatchCmd) != 0: |
51a2640a SH |
581 | print "Unfortunately applying the change failed!" |
582 | print "What do you want to do?" | |
583 | response = "x" | |
584 | while response != "s" and response != "a" and response != "w": | |
cebdf5af HWN |
585 | response = raw_input("[s]kip this patch / [a]pply the patch forcibly " |
586 | "and with .rej files / [w]rite the patch to a file (patch.txt) ") | |
51a2640a SH |
587 | if response == "s": |
588 | print "Skipping! Good luck with the next patches..." | |
20947149 SH |
589 | for f in editedFiles: |
590 | system("p4 revert \"%s\"" % f); | |
591 | for f in filesToAdd: | |
592 | system("rm %s" %f) | |
51a2640a SH |
593 | return |
594 | elif response == "a": | |
47a130b7 | 595 | os.system(applyPatchCmd) |
51a2640a SH |
596 | if len(filesToAdd) > 0: |
597 | print "You may also want to call p4 add on the following files:" | |
598 | print " ".join(filesToAdd) | |
599 | if len(filesToDelete): | |
600 | print "The following files should be scheduled for deletion with p4 delete:" | |
601 | print " ".join(filesToDelete) | |
cebdf5af HWN |
602 | die("Please resolve and submit the conflict manually and " |
603 | + "continue afterwards with git-p4 submit --continue") | |
51a2640a SH |
604 | elif response == "w": |
605 | system(diffcmd + " > patch.txt") | |
606 | print "Patch saved to patch.txt in %s !" % self.clientPath | |
cebdf5af HWN |
607 | die("Please resolve and submit the conflict manually and " |
608 | "continue afterwards with git-p4 submit --continue") | |
51a2640a | 609 | |
47a130b7 | 610 | system(applyPatchCmd) |
4f5cf76a SH |
611 | |
612 | for f in filesToAdd: | |
e6b711f0 | 613 | system("p4 add \"%s\"" % f) |
4f5cf76a | 614 | for f in filesToDelete: |
e6b711f0 SH |
615 | system("p4 revert \"%s\"" % f) |
616 | system("p4 delete \"%s\"" % f) | |
4f5cf76a | 617 | |
c65b670e CP |
618 | # Set/clear executable bits |
619 | for f in filesToChangeExecBit.keys(): | |
620 | mode = filesToChangeExecBit[f] | |
621 | setP4ExecBit(f, mode) | |
622 | ||
0e36f2d7 | 623 | logMessage = extractLogMessageFromGitCommit(id) |
0e36f2d7 | 624 | logMessage = logMessage.strip() |
4f5cf76a | 625 | |
ea99c3ae | 626 | template = self.prepareSubmitTemplate() |
4f5cf76a SH |
627 | |
628 | if self.interactive: | |
629 | submitTemplate = self.prepareLogMessage(template, logMessage) | |
67abd417 SB |
630 | if os.environ.has_key("P4DIFF"): |
631 | del(os.environ["P4DIFF"]) | |
b016d397 | 632 | diff = read_pipe("p4 diff -du ...") |
4f5cf76a | 633 | |
f3e5ae4f | 634 | newdiff = "" |
4f5cf76a | 635 | for newFile in filesToAdd: |
f3e5ae4f MSO |
636 | newdiff += "==== new file ====\n" |
637 | newdiff += "--- /dev/null\n" | |
638 | newdiff += "+++ %s\n" % newFile | |
4f5cf76a SH |
639 | f = open(newFile, "r") |
640 | for line in f.readlines(): | |
f3e5ae4f | 641 | newdiff += "+" + line |
4f5cf76a SH |
642 | f.close() |
643 | ||
f3e5ae4f | 644 | separatorLine = "######## everything below this line is just the diff #######\n" |
4f5cf76a | 645 | |
e96e400f SH |
646 | [handle, fileName] = tempfile.mkstemp() |
647 | tmpFile = os.fdopen(handle, "w+") | |
f3e5ae4f MSO |
648 | if self.isWindows: |
649 | submitTemplate = submitTemplate.replace("\n", "\r\n") | |
650 | separatorLine = separatorLine.replace("\n", "\r\n") | |
651 | newdiff = newdiff.replace("\n", "\r\n") | |
652 | tmpFile.write(submitTemplate + separatorLine + diff + newdiff) | |
e96e400f SH |
653 | tmpFile.close() |
654 | defaultEditor = "vi" | |
655 | if platform.system() == "Windows": | |
656 | defaultEditor = "notepad" | |
82cea9ff SB |
657 | if os.environ.has_key("P4EDITOR"): |
658 | editor = os.environ.get("P4EDITOR") | |
659 | else: | |
660 | editor = os.environ.get("EDITOR", defaultEditor); | |
e96e400f SH |
661 | system(editor + " " + fileName) |
662 | tmpFile = open(fileName, "rb") | |
663 | message = tmpFile.read() | |
664 | tmpFile.close() | |
665 | os.remove(fileName) | |
666 | submitTemplate = message[:message.index(separatorLine)] | |
667 | if self.isWindows: | |
668 | submitTemplate = submitTemplate.replace("\r\n", "\n") | |
669 | ||
e96e400f | 670 | write_pipe("p4 submit -i", submitTemplate) |
4f5cf76a SH |
671 | else: |
672 | fileName = "submit.txt" | |
673 | file = open(fileName, "w+") | |
674 | file.write(self.prepareLogMessage(template, logMessage)) | |
675 | file.close() | |
cebdf5af HWN |
676 | print ("Perforce submit template written as %s. " |
677 | + "Please review/edit and then use p4 submit -i < %s to submit directly!" | |
678 | % (fileName, fileName)) | |
4f5cf76a SH |
679 | |
680 | def run(self, args): | |
c9b50e63 SH |
681 | if len(args) == 0: |
682 | self.master = currentGitBranch() | |
4280e533 | 683 | if len(self.master) == 0 or not gitBranchExists("refs/heads/%s" % self.master): |
c9b50e63 SH |
684 | die("Detecting current git branch failed!") |
685 | elif len(args) == 1: | |
686 | self.master = args[0] | |
687 | else: | |
688 | return False | |
689 | ||
4c2d5d72 JX |
690 | allowSubmit = gitConfig("git-p4.allowSubmit") |
691 | if len(allowSubmit) > 0 and not self.master in allowSubmit.split(","): | |
692 | die("%s is not in git-p4.allowSubmit" % self.master) | |
693 | ||
27d2d811 | 694 | [upstream, settings] = findUpstreamBranchPoint() |
ea99c3ae | 695 | self.depotPath = settings['depot-paths'][0] |
27d2d811 SH |
696 | if len(self.origin) == 0: |
697 | self.origin = upstream | |
a3fdd579 SH |
698 | |
699 | if self.verbose: | |
700 | print "Origin branch is " + self.origin | |
9512497b | 701 | |
ea99c3ae | 702 | if len(self.depotPath) == 0: |
9512497b SH |
703 | print "Internal error: cannot locate perforce depot path from existing branches" |
704 | sys.exit(128) | |
705 | ||
ea99c3ae | 706 | self.clientPath = p4Where(self.depotPath) |
9512497b | 707 | |
51a2640a | 708 | if len(self.clientPath) == 0: |
ea99c3ae | 709 | print "Error: Cannot locate perforce checkout of %s in client view" % self.depotPath |
9512497b SH |
710 | sys.exit(128) |
711 | ||
ea99c3ae | 712 | print "Perforce checkout for depot path %s located at %s" % (self.depotPath, self.clientPath) |
7944f142 | 713 | self.oldWorkingDirectory = os.getcwd() |
c1b296b9 | 714 | |
51a2640a | 715 | os.chdir(self.clientPath) |
31f9ec12 SH |
716 | print "Syncronizing p4 checkout..." |
717 | system("p4 sync ...") | |
9512497b | 718 | |
4f5cf76a | 719 | self.check() |
4f5cf76a | 720 | |
4c750c0d SH |
721 | commits = [] |
722 | for line in read_pipe_lines("git rev-list --no-merges %s..%s" % (self.origin, self.master)): | |
723 | commits.append(line.strip()) | |
724 | commits.reverse() | |
4f5cf76a SH |
725 | |
726 | while len(commits) > 0: | |
4f5cf76a SH |
727 | commit = commits[0] |
728 | commits = commits[1:] | |
7cb5cbef | 729 | self.applyCommit(commit) |
4f5cf76a SH |
730 | if not self.interactive: |
731 | break | |
732 | ||
4f5cf76a | 733 | if len(commits) == 0: |
4c750c0d SH |
734 | print "All changes applied!" |
735 | os.chdir(self.oldWorkingDirectory) | |
14594f4b | 736 | |
4c750c0d SH |
737 | sync = P4Sync() |
738 | sync.run([]) | |
14594f4b | 739 | |
4c750c0d SH |
740 | rebase = P4Rebase() |
741 | rebase.rebase() | |
4f5cf76a | 742 | |
b984733c SH |
743 | return True |
744 | ||
711544b0 | 745 | class P4Sync(Command): |
b984733c SH |
746 | def __init__(self): |
747 | Command.__init__(self) | |
748 | self.options = [ | |
749 | optparse.make_option("--branch", dest="branch"), | |
750 | optparse.make_option("--detect-branches", dest="detectBranches", action="store_true"), | |
751 | optparse.make_option("--changesfile", dest="changesFile"), | |
752 | optparse.make_option("--silent", dest="silent", action="store_true"), | |
ef48f909 | 753 | optparse.make_option("--detect-labels", dest="detectLabels", action="store_true"), |
a028a98e | 754 | optparse.make_option("--verbose", dest="verbose", action="store_true"), |
d2c6dd30 HWN |
755 | optparse.make_option("--import-local", dest="importIntoRemotes", action="store_false", |
756 | help="Import into refs/heads/ , not refs/remotes"), | |
8b41a97f | 757 | optparse.make_option("--max-changes", dest="maxChanges"), |
86dff6b6 | 758 | optparse.make_option("--keep-path", dest="keepRepoPath", action='store_true', |
3a70cdfa TAL |
759 | help="Keep entire BRANCH/DIR/SUBDIR prefix during import"), |
760 | optparse.make_option("--use-client-spec", dest="useClientSpec", action='store_true', | |
761 | help="Only sync files that are included in the Perforce Client Spec") | |
b984733c SH |
762 | ] |
763 | self.description = """Imports from Perforce into a git repository.\n | |
764 | example: | |
765 | //depot/my/project/ -- to import the current head | |
766 | //depot/my/project/@all -- to import everything | |
767 | //depot/my/project/@1,6 -- to import only from revision 1 to 6 | |
768 | ||
769 | (a ... is not needed in the path p4 specification, it's added implicitly)""" | |
770 | ||
771 | self.usage += " //depot/path[@revRange]" | |
b984733c | 772 | self.silent = False |
b984733c SH |
773 | self.createdBranches = Set() |
774 | self.committedChanges = Set() | |
569d1bd4 | 775 | self.branch = "" |
b984733c | 776 | self.detectBranches = False |
cb53e1f8 | 777 | self.detectLabels = False |
b984733c | 778 | self.changesFile = "" |
01265103 | 779 | self.syncWithOrigin = True |
4b97ffb1 | 780 | self.verbose = False |
a028a98e | 781 | self.importIntoRemotes = True |
01a9c9c5 | 782 | self.maxChanges = "" |
c1f9197f | 783 | self.isWindows = (platform.system() == "Windows") |
8b41a97f | 784 | self.keepRepoPath = False |
6326aa58 | 785 | self.depotPaths = None |
3c699645 | 786 | self.p4BranchesInGit = [] |
354081d5 | 787 | self.cloneExclude = [] |
3a70cdfa TAL |
788 | self.useClientSpec = False |
789 | self.clientSpecDirs = [] | |
b984733c | 790 | |
01265103 SH |
791 | if gitConfig("git-p4.syncFromOrigin") == "false": |
792 | self.syncWithOrigin = False | |
793 | ||
b984733c | 794 | def extractFilesFromCommit(self, commit): |
354081d5 TT |
795 | self.cloneExclude = [re.sub(r"\.\.\.$", "", path) |
796 | for path in self.cloneExclude] | |
b984733c SH |
797 | files = [] |
798 | fnum = 0 | |
799 | while commit.has_key("depotFile%s" % fnum): | |
800 | path = commit["depotFile%s" % fnum] | |
6326aa58 | 801 | |
354081d5 TT |
802 | if [p for p in self.cloneExclude |
803 | if path.startswith (p)]: | |
804 | found = False | |
805 | else: | |
806 | found = [p for p in self.depotPaths | |
807 | if path.startswith (p)] | |
6326aa58 | 808 | if not found: |
b984733c SH |
809 | fnum = fnum + 1 |
810 | continue | |
811 | ||
812 | file = {} | |
813 | file["path"] = path | |
814 | file["rev"] = commit["rev%s" % fnum] | |
815 | file["action"] = commit["action%s" % fnum] | |
816 | file["type"] = commit["type%s" % fnum] | |
817 | files.append(file) | |
818 | fnum = fnum + 1 | |
819 | return files | |
820 | ||
6326aa58 | 821 | def stripRepoPath(self, path, prefixes): |
8b41a97f | 822 | if self.keepRepoPath: |
6326aa58 HWN |
823 | prefixes = [re.sub("^(//[^/]+/).*", r'\1', prefixes[0])] |
824 | ||
825 | for p in prefixes: | |
826 | if path.startswith(p): | |
827 | path = path[len(p):] | |
8b41a97f | 828 | |
6326aa58 | 829 | return path |
6754a299 | 830 | |
71b112d4 | 831 | def splitFilesIntoBranches(self, commit): |
d5904674 | 832 | branches = {} |
71b112d4 SH |
833 | fnum = 0 |
834 | while commit.has_key("depotFile%s" % fnum): | |
835 | path = commit["depotFile%s" % fnum] | |
6326aa58 HWN |
836 | found = [p for p in self.depotPaths |
837 | if path.startswith (p)] | |
838 | if not found: | |
71b112d4 SH |
839 | fnum = fnum + 1 |
840 | continue | |
841 | ||
842 | file = {} | |
843 | file["path"] = path | |
844 | file["rev"] = commit["rev%s" % fnum] | |
845 | file["action"] = commit["action%s" % fnum] | |
846 | file["type"] = commit["type%s" % fnum] | |
847 | fnum = fnum + 1 | |
848 | ||
6326aa58 | 849 | relPath = self.stripRepoPath(path, self.depotPaths) |
b984733c | 850 | |
4b97ffb1 | 851 | for branch in self.knownBranches.keys(): |
6754a299 HWN |
852 | |
853 | # add a trailing slash so that a commit into qt/4.2foo doesn't end up in qt/4.2 | |
854 | if relPath.startswith(branch + "/"): | |
d5904674 SH |
855 | if branch not in branches: |
856 | branches[branch] = [] | |
71b112d4 | 857 | branches[branch].append(file) |
6555b2cc | 858 | break |
b984733c SH |
859 | |
860 | return branches | |
861 | ||
6a49f8e2 HWN |
862 | ## Should move this out, doesn't use SELF. |
863 | def readP4Files(self, files): | |
30b5940b SH |
864 | filesForCommit = [] |
865 | filesToRead = [] | |
866 | ||
3a70cdfa | 867 | for f in files: |
30b5940b | 868 | includeFile = True |
3a70cdfa TAL |
869 | for val in self.clientSpecDirs: |
870 | if f['path'].startswith(val[0]): | |
30b5940b SH |
871 | if val[1] <= 0: |
872 | includeFile = False | |
3a70cdfa TAL |
873 | break |
874 | ||
30b5940b SH |
875 | if includeFile: |
876 | filesForCommit.append(f) | |
877 | if f['action'] != 'delete': | |
878 | filesToRead.append(f) | |
6a49f8e2 | 879 | |
30b5940b SH |
880 | filedata = [] |
881 | if len(filesToRead) > 0: | |
882 | filedata = p4CmdList('-x - print', | |
883 | stdin='\n'.join(['%s#%s' % (f['path'], f['rev']) | |
884 | for f in filesToRead]), | |
885 | stdin_mode='w+') | |
f2eda79f | 886 | |
30b5940b SH |
887 | if "p4ExitCode" in filedata[0]: |
888 | die("Problems executing p4. Error: [%d]." | |
889 | % (filedata[0]['p4ExitCode'])); | |
6a49f8e2 | 890 | |
d2c6dd30 HWN |
891 | j = 0; |
892 | contents = {} | |
b1ce9447 | 893 | while j < len(filedata): |
d2c6dd30 | 894 | stat = filedata[j] |
b1ce9447 | 895 | j += 1 |
8ff45f2a | 896 | text = []; |
f3e9512b | 897 | while j < len(filedata) and filedata[j]['code'] in ('text', 'unicode', 'binary'): |
8ff45f2a | 898 | text.append(filedata[j]['data']) |
b1ce9447 | 899 | j += 1 |
8ff45f2a | 900 | text = ''.join(text) |
1b9a4684 HWN |
901 | |
902 | if not stat.has_key('depotFile'): | |
903 | sys.stderr.write("p4 print fails with: %s\n" % repr(stat)) | |
904 | continue | |
905 | ||
8ff45f2a MSO |
906 | if stat['type'] in ('text+ko', 'unicode+ko', 'binary+ko'): |
907 | text = re.sub(r'(?i)\$(Id|Header):[^$]*\$',r'$\1$', text) | |
908 | elif stat['type'] in ('text+k', 'ktext', 'kxtext', 'unicode+k', 'binary+k'): | |
2d71bde2 | 909 | text = re.sub(r'\$(Id|Header|Author|Date|DateTime|Change|File|Revision):[^$]*\$',r'$\1$', text) |
8ff45f2a | 910 | |
b1ce9447 | 911 | contents[stat['depotFile']] = text |
6a49f8e2 | 912 | |
30b5940b SH |
913 | for f in filesForCommit: |
914 | path = f['path'] | |
915 | if contents.has_key(path): | |
916 | f['data'] = contents[path] | |
917 | ||
918 | return filesForCommit | |
6a49f8e2 | 919 | |
6326aa58 | 920 | def commit(self, details, files, branch, branchPrefixes, parent = ""): |
b984733c SH |
921 | epoch = details["time"] |
922 | author = details["user"] | |
923 | ||
4b97ffb1 SH |
924 | if self.verbose: |
925 | print "commit into %s" % branch | |
926 | ||
96e07dd2 HWN |
927 | # start with reading files; if that fails, we should not |
928 | # create a commit. | |
929 | new_files = [] | |
930 | for f in files: | |
931 | if [p for p in branchPrefixes if f['path'].startswith(p)]: | |
932 | new_files.append (f) | |
933 | else: | |
934 | sys.stderr.write("Ignoring file outside of prefix: %s\n" % path) | |
3a70cdfa | 935 | files = self.readP4Files(new_files) |
96e07dd2 | 936 | |
b984733c | 937 | self.gitStream.write("commit %s\n" % branch) |
6a49f8e2 | 938 | # gitStream.write("mark :%s\n" % details["change"]) |
b984733c SH |
939 | self.committedChanges.add(int(details["change"])) |
940 | committer = "" | |
b607e71e SH |
941 | if author not in self.users: |
942 | self.getUserMapFromPerforceServer() | |
b984733c | 943 | if author in self.users: |
0828ab14 | 944 | committer = "%s %s %s" % (self.users[author], epoch, self.tz) |
b984733c | 945 | else: |
0828ab14 | 946 | committer = "%s <a@b> %s %s" % (author, epoch, self.tz) |
b984733c SH |
947 | |
948 | self.gitStream.write("committer %s\n" % committer) | |
949 | ||
950 | self.gitStream.write("data <<EOT\n") | |
951 | self.gitStream.write(details["desc"]) | |
6581de09 SH |
952 | self.gitStream.write("\n[git-p4: depot-paths = \"%s\": change = %s" |
953 | % (','.join (branchPrefixes), details["change"])) | |
954 | if len(details['options']) > 0: | |
955 | self.gitStream.write(": options = %s" % details['options']) | |
956 | self.gitStream.write("]\nEOT\n\n") | |
b984733c SH |
957 | |
958 | if len(parent) > 0: | |
4b97ffb1 SH |
959 | if self.verbose: |
960 | print "parent %s" % parent | |
b984733c SH |
961 | self.gitStream.write("from %s\n" % parent) |
962 | ||
6a49f8e2 | 963 | for file in files: |
b984733c | 964 | if file["type"] == "apple": |
6a49f8e2 | 965 | print "\nfile %s is a strange apple file that forks. Ignoring!" % file['path'] |
b984733c SH |
966 | continue |
967 | ||
6a49f8e2 HWN |
968 | relPath = self.stripRepoPath(file['path'], branchPrefixes) |
969 | if file["action"] == "delete": | |
b984733c SH |
970 | self.gitStream.write("D %s\n" % relPath) |
971 | else: | |
6a49f8e2 | 972 | data = file['data'] |
b984733c | 973 | |
74276ec6 | 974 | mode = "644" |
b9fc6ea9 | 975 | if isP4Exec(file["type"]): |
74276ec6 SH |
976 | mode = "755" |
977 | elif file["type"] == "symlink": | |
978 | mode = "120000" | |
979 | # p4 print on a symlink contains "target\n", so strip it off | |
980 | data = data[:-1] | |
981 | ||
c1f9197f MSO |
982 | if self.isWindows and file["type"].endswith("text"): |
983 | data = data.replace("\r\n", "\n") | |
984 | ||
74276ec6 | 985 | self.gitStream.write("M %s inline %s\n" % (mode, relPath)) |
b984733c SH |
986 | self.gitStream.write("data %s\n" % len(data)) |
987 | self.gitStream.write(data) | |
988 | self.gitStream.write("\n") | |
989 | ||
990 | self.gitStream.write("\n") | |
991 | ||
1f4ba1cb SH |
992 | change = int(details["change"]) |
993 | ||
9bda3a85 | 994 | if self.labels.has_key(change): |
1f4ba1cb SH |
995 | label = self.labels[change] |
996 | labelDetails = label[0] | |
997 | labelRevisions = label[1] | |
71b112d4 SH |
998 | if self.verbose: |
999 | print "Change %s is labelled %s" % (change, labelDetails) | |
1f4ba1cb | 1000 | |
6326aa58 HWN |
1001 | files = p4CmdList("files " + ' '.join (["%s...@%s" % (p, change) |
1002 | for p in branchPrefixes])) | |
1f4ba1cb SH |
1003 | |
1004 | if len(files) == len(labelRevisions): | |
1005 | ||
1006 | cleanedFiles = {} | |
1007 | for info in files: | |
1008 | if info["action"] == "delete": | |
1009 | continue | |
1010 | cleanedFiles[info["depotFile"]] = info["rev"] | |
1011 | ||
1012 | if cleanedFiles == labelRevisions: | |
1013 | self.gitStream.write("tag tag_%s\n" % labelDetails["label"]) | |
1014 | self.gitStream.write("from %s\n" % branch) | |
1015 | ||
1016 | owner = labelDetails["Owner"] | |
1017 | tagger = "" | |
1018 | if author in self.users: | |
1019 | tagger = "%s %s %s" % (self.users[owner], epoch, self.tz) | |
1020 | else: | |
1021 | tagger = "%s <a@b> %s %s" % (owner, epoch, self.tz) | |
1022 | self.gitStream.write("tagger %s\n" % tagger) | |
1023 | self.gitStream.write("data <<EOT\n") | |
1024 | self.gitStream.write(labelDetails["Description"]) | |
1025 | self.gitStream.write("EOT\n\n") | |
1026 | ||
1027 | else: | |
a46668fa | 1028 | if not self.silent: |
cebdf5af HWN |
1029 | print ("Tag %s does not match with change %s: files do not match." |
1030 | % (labelDetails["label"], change)) | |
1f4ba1cb SH |
1031 | |
1032 | else: | |
a46668fa | 1033 | if not self.silent: |
cebdf5af HWN |
1034 | print ("Tag %s does not match with change %s: file count is different." |
1035 | % (labelDetails["label"], change)) | |
b984733c | 1036 | |
183b8ef8 | 1037 | def getUserCacheFilename(self): |
b2d2d16a SH |
1038 | home = os.environ.get("HOME", os.environ.get("USERPROFILE")) |
1039 | return home + "/.gitp4-usercache.txt" | |
183b8ef8 | 1040 | |
b607e71e | 1041 | def getUserMapFromPerforceServer(self): |
ebd81168 SH |
1042 | if self.userMapFromPerforceServer: |
1043 | return | |
b984733c SH |
1044 | self.users = {} |
1045 | ||
1046 | for output in p4CmdList("users"): | |
1047 | if not output.has_key("User"): | |
1048 | continue | |
1049 | self.users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">" | |
1050 | ||
183b8ef8 HWN |
1051 | |
1052 | s = '' | |
1053 | for (key, val) in self.users.items(): | |
1054 | s += "%s\t%s\n" % (key, val) | |
1055 | ||
1056 | open(self.getUserCacheFilename(), "wb").write(s) | |
ebd81168 | 1057 | self.userMapFromPerforceServer = True |
b607e71e SH |
1058 | |
1059 | def loadUserMapFromCache(self): | |
1060 | self.users = {} | |
ebd81168 | 1061 | self.userMapFromPerforceServer = False |
b607e71e | 1062 | try: |
183b8ef8 | 1063 | cache = open(self.getUserCacheFilename(), "rb") |
b607e71e SH |
1064 | lines = cache.readlines() |
1065 | cache.close() | |
1066 | for line in lines: | |
b25b2065 | 1067 | entry = line.strip().split("\t") |
b607e71e SH |
1068 | self.users[entry[0]] = entry[1] |
1069 | except IOError: | |
1070 | self.getUserMapFromPerforceServer() | |
1071 | ||
1f4ba1cb SH |
1072 | def getLabels(self): |
1073 | self.labels = {} | |
1074 | ||
6326aa58 | 1075 | l = p4CmdList("labels %s..." % ' '.join (self.depotPaths)) |
10c3211b | 1076 | if len(l) > 0 and not self.silent: |
183f8436 | 1077 | print "Finding files belonging to labels in %s" % `self.depotPaths` |
01ce1fe9 SH |
1078 | |
1079 | for output in l: | |
1f4ba1cb SH |
1080 | label = output["label"] |
1081 | revisions = {} | |
1082 | newestChange = 0 | |
71b112d4 SH |
1083 | if self.verbose: |
1084 | print "Querying files for label %s" % label | |
6326aa58 HWN |
1085 | for file in p4CmdList("files " |
1086 | + ' '.join (["%s...@%s" % (p, label) | |
1087 | for p in self.depotPaths])): | |
1f4ba1cb SH |
1088 | revisions[file["depotFile"]] = file["rev"] |
1089 | change = int(file["change"]) | |
1090 | if change > newestChange: | |
1091 | newestChange = change | |
1092 | ||
9bda3a85 SH |
1093 | self.labels[newestChange] = [output, revisions] |
1094 | ||
1095 | if self.verbose: | |
1096 | print "Label changes: %s" % self.labels.keys() | |
1f4ba1cb | 1097 | |
86dff6b6 HWN |
1098 | def guessProjectName(self): |
1099 | for p in self.depotPaths: | |
6e5295c4 SH |
1100 | if p.endswith("/"): |
1101 | p = p[:-1] | |
1102 | p = p[p.strip().rfind("/") + 1:] | |
1103 | if not p.endswith("/"): | |
1104 | p += "/" | |
1105 | return p | |
86dff6b6 | 1106 | |
4b97ffb1 | 1107 | def getBranchMapping(self): |
6555b2cc SH |
1108 | lostAndFoundBranches = set() |
1109 | ||
4b97ffb1 SH |
1110 | for info in p4CmdList("branches"): |
1111 | details = p4Cmd("branch -o %s" % info["branch"]) | |
1112 | viewIdx = 0 | |
1113 | while details.has_key("View%s" % viewIdx): | |
1114 | paths = details["View%s" % viewIdx].split(" ") | |
1115 | viewIdx = viewIdx + 1 | |
1116 | # require standard //depot/foo/... //depot/bar/... mapping | |
1117 | if len(paths) != 2 or not paths[0].endswith("/...") or not paths[1].endswith("/..."): | |
1118 | continue | |
1119 | source = paths[0] | |
1120 | destination = paths[1] | |
6509e19c SH |
1121 | ## HACK |
1122 | if source.startswith(self.depotPaths[0]) and destination.startswith(self.depotPaths[0]): | |
1123 | source = source[len(self.depotPaths[0]):-4] | |
1124 | destination = destination[len(self.depotPaths[0]):-4] | |
6555b2cc | 1125 | |
1a2edf4e SH |
1126 | if destination in self.knownBranches: |
1127 | if not self.silent: | |
1128 | print "p4 branch %s defines a mapping from %s to %s" % (info["branch"], source, destination) | |
1129 | print "but there exists another mapping from %s to %s already!" % (self.knownBranches[destination], destination) | |
1130 | continue | |
1131 | ||
6555b2cc SH |
1132 | self.knownBranches[destination] = source |
1133 | ||
1134 | lostAndFoundBranches.discard(destination) | |
1135 | ||
29bdbac1 | 1136 | if source not in self.knownBranches: |
6555b2cc SH |
1137 | lostAndFoundBranches.add(source) |
1138 | ||
1139 | ||
1140 | for branch in lostAndFoundBranches: | |
1141 | self.knownBranches[branch] = branch | |
29bdbac1 | 1142 | |
38f9f5ec SH |
1143 | def getBranchMappingFromGitBranches(self): |
1144 | branches = p4BranchesInGit(self.importIntoRemotes) | |
1145 | for branch in branches.keys(): | |
1146 | if branch == "master": | |
1147 | branch = "main" | |
1148 | else: | |
1149 | branch = branch[len(self.projectName):] | |
1150 | self.knownBranches[branch] = branch | |
1151 | ||
29bdbac1 | 1152 | def listExistingP4GitBranches(self): |
144ff46b SH |
1153 | # branches holds mapping from name to commit |
1154 | branches = p4BranchesInGit(self.importIntoRemotes) | |
1155 | self.p4BranchesInGit = branches.keys() | |
1156 | for branch in branches.keys(): | |
1157 | self.initialParents[self.refPrefix + branch] = branches[branch] | |
4b97ffb1 | 1158 | |
bb6e09b2 HWN |
1159 | def updateOptionDict(self, d): |
1160 | option_keys = {} | |
1161 | if self.keepRepoPath: | |
1162 | option_keys['keepRepoPath'] = 1 | |
1163 | ||
1164 | d["options"] = ' '.join(sorted(option_keys.keys())) | |
1165 | ||
1166 | def readOptions(self, d): | |
1167 | self.keepRepoPath = (d.has_key('options') | |
1168 | and ('keepRepoPath' in d['options'])) | |
6326aa58 | 1169 | |
8134f69c SH |
1170 | def gitRefForBranch(self, branch): |
1171 | if branch == "main": | |
1172 | return self.refPrefix + "master" | |
1173 | ||
1174 | if len(branch) <= 0: | |
1175 | return branch | |
1176 | ||
1177 | return self.refPrefix + self.projectName + branch | |
1178 | ||
1ca3d710 SH |
1179 | def gitCommitByP4Change(self, ref, change): |
1180 | if self.verbose: | |
1181 | print "looking in ref " + ref + " for change %s using bisect..." % change | |
1182 | ||
1183 | earliestCommit = "" | |
1184 | latestCommit = parseRevision(ref) | |
1185 | ||
1186 | while True: | |
1187 | if self.verbose: | |
1188 | print "trying: earliest %s latest %s" % (earliestCommit, latestCommit) | |
1189 | next = read_pipe("git rev-list --bisect %s %s" % (latestCommit, earliestCommit)).strip() | |
1190 | if len(next) == 0: | |
1191 | if self.verbose: | |
1192 | print "argh" | |
1193 | return "" | |
1194 | log = extractLogMessageFromGitCommit(next) | |
1195 | settings = extractSettingsGitLog(log) | |
1196 | currentChange = int(settings['change']) | |
1197 | if self.verbose: | |
1198 | print "current change %s" % currentChange | |
1199 | ||
1200 | if currentChange == change: | |
1201 | if self.verbose: | |
1202 | print "found %s" % next | |
1203 | return next | |
1204 | ||
1205 | if currentChange < change: | |
1206 | earliestCommit = "^%s" % next | |
1207 | else: | |
1208 | latestCommit = "%s" % next | |
1209 | ||
1210 | return "" | |
1211 | ||
1212 | def importNewBranch(self, branch, maxChange): | |
1213 | # make fast-import flush all changes to disk and update the refs using the checkpoint | |
1214 | # command so that we can try to find the branch parent in the git history | |
1215 | self.gitStream.write("checkpoint\n\n"); | |
1216 | self.gitStream.flush(); | |
1217 | branchPrefix = self.depotPaths[0] + branch + "/" | |
1218 | range = "@1,%s" % maxChange | |
1219 | #print "prefix" + branchPrefix | |
1220 | changes = p4ChangesForPaths([branchPrefix], range) | |
1221 | if len(changes) <= 0: | |
1222 | return False | |
1223 | firstChange = changes[0] | |
1224 | #print "first change in branch: %s" % firstChange | |
1225 | sourceBranch = self.knownBranches[branch] | |
1226 | sourceDepotPath = self.depotPaths[0] + sourceBranch | |
1227 | sourceRef = self.gitRefForBranch(sourceBranch) | |
1228 | #print "source " + sourceBranch | |
1229 | ||
1230 | branchParentChange = int(p4Cmd("changes -m 1 %s...@1,%s" % (sourceDepotPath, firstChange))["change"]) | |
1231 | #print "branch parent: %s" % branchParentChange | |
1232 | gitParent = self.gitCommitByP4Change(sourceRef, branchParentChange) | |
1233 | if len(gitParent) > 0: | |
1234 | self.initialParents[self.gitRefForBranch(branch)] = gitParent | |
1235 | #print "parent git commit: %s" % gitParent | |
1236 | ||
1237 | self.importChanges(changes) | |
1238 | return True | |
1239 | ||
e87f37ae SH |
1240 | def importChanges(self, changes): |
1241 | cnt = 1 | |
1242 | for change in changes: | |
1243 | description = p4Cmd("describe %s" % change) | |
1244 | self.updateOptionDict(description) | |
1245 | ||
1246 | if not self.silent: | |
1247 | sys.stdout.write("\rImporting revision %s (%s%%)" % (change, cnt * 100 / len(changes))) | |
1248 | sys.stdout.flush() | |
1249 | cnt = cnt + 1 | |
1250 | ||
1251 | try: | |
1252 | if self.detectBranches: | |
1253 | branches = self.splitFilesIntoBranches(description) | |
1254 | for branch in branches.keys(): | |
1255 | ## HACK --hwn | |
1256 | branchPrefix = self.depotPaths[0] + branch + "/" | |
1257 | ||
1258 | parent = "" | |
1259 | ||
1260 | filesForCommit = branches[branch] | |
1261 | ||
1262 | if self.verbose: | |
1263 | print "branch is %s" % branch | |
1264 | ||
1265 | self.updatedBranches.add(branch) | |
1266 | ||
1267 | if branch not in self.createdBranches: | |
1268 | self.createdBranches.add(branch) | |
1269 | parent = self.knownBranches[branch] | |
1270 | if parent == branch: | |
1271 | parent = "" | |
1ca3d710 SH |
1272 | else: |
1273 | fullBranch = self.projectName + branch | |
1274 | if fullBranch not in self.p4BranchesInGit: | |
1275 | if not self.silent: | |
1276 | print("\n Importing new branch %s" % fullBranch); | |
1277 | if self.importNewBranch(branch, change - 1): | |
1278 | parent = "" | |
1279 | self.p4BranchesInGit.append(fullBranch) | |
1280 | if not self.silent: | |
1281 | print("\n Resuming with change %s" % change); | |
1282 | ||
1283 | if self.verbose: | |
1284 | print "parent determined through known branches: %s" % parent | |
e87f37ae | 1285 | |
8134f69c SH |
1286 | branch = self.gitRefForBranch(branch) |
1287 | parent = self.gitRefForBranch(parent) | |
e87f37ae SH |
1288 | |
1289 | if self.verbose: | |
1290 | print "looking for initial parent for %s; current parent is %s" % (branch, parent) | |
1291 | ||
1292 | if len(parent) == 0 and branch in self.initialParents: | |
1293 | parent = self.initialParents[branch] | |
1294 | del self.initialParents[branch] | |
1295 | ||
1296 | self.commit(description, filesForCommit, branch, [branchPrefix], parent) | |
1297 | else: | |
1298 | files = self.extractFilesFromCommit(description) | |
1299 | self.commit(description, files, self.branch, self.depotPaths, | |
1300 | self.initialParent) | |
1301 | self.initialParent = "" | |
1302 | except IOError: | |
1303 | print self.gitError.read() | |
1304 | sys.exit(1) | |
1305 | ||
c208a243 SH |
1306 | def importHeadRevision(self, revision): |
1307 | print "Doing initial import of %s from revision %s into %s" % (' '.join(self.depotPaths), revision, self.branch) | |
1308 | ||
1309 | details = { "user" : "git perforce import user", "time" : int(time.time()) } | |
1310 | details["desc"] = ("Initial import of %s from the state at revision %s" | |
1311 | % (' '.join(self.depotPaths), revision)) | |
1312 | details["change"] = revision | |
1313 | newestRevision = 0 | |
1314 | ||
1315 | fileCnt = 0 | |
1316 | for info in p4CmdList("files " | |
1317 | + ' '.join(["%s...%s" | |
1318 | % (p, revision) | |
1319 | for p in self.depotPaths])): | |
1320 | ||
1321 | if info['code'] == 'error': | |
1322 | sys.stderr.write("p4 returned an error: %s\n" | |
1323 | % info['data']) | |
1324 | sys.exit(1) | |
1325 | ||
1326 | ||
1327 | change = int(info["change"]) | |
1328 | if change > newestRevision: | |
1329 | newestRevision = change | |
1330 | ||
1331 | if info["action"] == "delete": | |
1332 | # don't increase the file cnt, otherwise details["depotFile123"] will have gaps! | |
1333 | #fileCnt = fileCnt + 1 | |
1334 | continue | |
1335 | ||
1336 | for prop in ["depotFile", "rev", "action", "type" ]: | |
1337 | details["%s%s" % (prop, fileCnt)] = info[prop] | |
1338 | ||
1339 | fileCnt = fileCnt + 1 | |
1340 | ||
1341 | details["change"] = newestRevision | |
1342 | self.updateOptionDict(details) | |
1343 | try: | |
1344 | self.commit(details, self.extractFilesFromCommit(details), self.branch, self.depotPaths) | |
1345 | except IOError: | |
1346 | print "IO error with git fast-import. Is your git version recent enough?" | |
1347 | print self.gitError.read() | |
1348 | ||
1349 | ||
3a70cdfa TAL |
1350 | def getClientSpec(self): |
1351 | specList = p4CmdList( "client -o" ) | |
1352 | temp = {} | |
1353 | for entry in specList: | |
1354 | for k,v in entry.iteritems(): | |
1355 | if k.startswith("View"): | |
1356 | if v.startswith('"'): | |
1357 | start = 1 | |
1358 | else: | |
1359 | start = 0 | |
1360 | index = v.find("...") | |
1361 | v = v[start:index] | |
1362 | if v.startswith("-"): | |
1363 | v = v[1:] | |
1364 | temp[v] = -len(v) | |
1365 | else: | |
1366 | temp[v] = len(v) | |
1367 | self.clientSpecDirs = temp.items() | |
1368 | self.clientSpecDirs.sort( lambda x, y: abs( y[1] ) - abs( x[1] ) ) | |
1369 | ||
b984733c | 1370 | def run(self, args): |
6326aa58 | 1371 | self.depotPaths = [] |
179caebf SH |
1372 | self.changeRange = "" |
1373 | self.initialParent = "" | |
6326aa58 | 1374 | self.previousDepotPaths = [] |
ce6f33c8 | 1375 | |
29bdbac1 SH |
1376 | # map from branch depot path to parent branch |
1377 | self.knownBranches = {} | |
1378 | self.initialParents = {} | |
5ca44617 | 1379 | self.hasOrigin = originP4BranchesExist() |
a43ff00c SH |
1380 | if not self.syncWithOrigin: |
1381 | self.hasOrigin = False | |
29bdbac1 | 1382 | |
a028a98e SH |
1383 | if self.importIntoRemotes: |
1384 | self.refPrefix = "refs/remotes/p4/" | |
1385 | else: | |
db775559 | 1386 | self.refPrefix = "refs/heads/p4/" |
a028a98e | 1387 | |
cebdf5af HWN |
1388 | if self.syncWithOrigin and self.hasOrigin: |
1389 | if not self.silent: | |
1390 | print "Syncing with origin first by calling git fetch origin" | |
1391 | system("git fetch origin") | |
10f880f8 | 1392 | |
569d1bd4 | 1393 | if len(self.branch) == 0: |
db775559 | 1394 | self.branch = self.refPrefix + "master" |
a028a98e | 1395 | if gitBranchExists("refs/heads/p4") and self.importIntoRemotes: |
48df6fd8 | 1396 | system("git update-ref %s refs/heads/p4" % self.branch) |
48df6fd8 | 1397 | system("git branch -D p4"); |
faf1bd20 | 1398 | # create it /after/ importing, when master exists |
0058a33a | 1399 | if not gitBranchExists(self.refPrefix + "HEAD") and self.importIntoRemotes and gitBranchExists(self.branch): |
a3c55c09 | 1400 | system("git symbolic-ref %sHEAD %s" % (self.refPrefix, self.branch)) |
967f72e2 | 1401 | |
3a70cdfa TAL |
1402 | if self.useClientSpec or gitConfig("p4.useclientspec") == "true": |
1403 | self.getClientSpec() | |
1404 | ||
6a49f8e2 HWN |
1405 | # TODO: should always look at previous commits, |
1406 | # merge with previous imports, if possible. | |
1407 | if args == []: | |
d414c74a | 1408 | if self.hasOrigin: |
5ca44617 | 1409 | createOrUpdateBranchesFromOrigin(self.refPrefix, self.silent) |
abcd790f SH |
1410 | self.listExistingP4GitBranches() |
1411 | ||
1412 | if len(self.p4BranchesInGit) > 1: | |
1413 | if not self.silent: | |
1414 | print "Importing from/into multiple branches" | |
1415 | self.detectBranches = True | |
967f72e2 | 1416 | |
29bdbac1 SH |
1417 | if self.verbose: |
1418 | print "branches: %s" % self.p4BranchesInGit | |
1419 | ||
1420 | p4Change = 0 | |
1421 | for branch in self.p4BranchesInGit: | |
cebdf5af | 1422 | logMsg = extractLogMessageFromGitCommit(self.refPrefix + branch) |
bb6e09b2 HWN |
1423 | |
1424 | settings = extractSettingsGitLog(logMsg) | |
29bdbac1 | 1425 | |
bb6e09b2 HWN |
1426 | self.readOptions(settings) |
1427 | if (settings.has_key('depot-paths') | |
1428 | and settings.has_key ('change')): | |
1429 | change = int(settings['change']) + 1 | |
29bdbac1 SH |
1430 | p4Change = max(p4Change, change) |
1431 | ||
bb6e09b2 HWN |
1432 | depotPaths = sorted(settings['depot-paths']) |
1433 | if self.previousDepotPaths == []: | |
6326aa58 | 1434 | self.previousDepotPaths = depotPaths |
29bdbac1 | 1435 | else: |
6326aa58 HWN |
1436 | paths = [] |
1437 | for (prev, cur) in zip(self.previousDepotPaths, depotPaths): | |
583e1707 | 1438 | for i in range(0, min(len(cur), len(prev))): |
6326aa58 | 1439 | if cur[i] <> prev[i]: |
583e1707 | 1440 | i = i - 1 |
6326aa58 HWN |
1441 | break |
1442 | ||
583e1707 | 1443 | paths.append (cur[:i + 1]) |
6326aa58 HWN |
1444 | |
1445 | self.previousDepotPaths = paths | |
29bdbac1 SH |
1446 | |
1447 | if p4Change > 0: | |
bb6e09b2 | 1448 | self.depotPaths = sorted(self.previousDepotPaths) |
d5904674 | 1449 | self.changeRange = "@%s,#head" % p4Change |
330f53b8 SH |
1450 | if not self.detectBranches: |
1451 | self.initialParent = parseRevision(self.branch) | |
341dc1c1 | 1452 | if not self.silent and not self.detectBranches: |
967f72e2 | 1453 | print "Performing incremental import into %s git branch" % self.branch |
569d1bd4 | 1454 | |
f9162f6a SH |
1455 | if not self.branch.startswith("refs/"): |
1456 | self.branch = "refs/heads/" + self.branch | |
179caebf | 1457 | |
6326aa58 | 1458 | if len(args) == 0 and self.depotPaths: |
b984733c | 1459 | if not self.silent: |
6326aa58 | 1460 | print "Depot paths: %s" % ' '.join(self.depotPaths) |
b984733c | 1461 | else: |
6326aa58 | 1462 | if self.depotPaths and self.depotPaths != args: |
cebdf5af | 1463 | print ("previous import used depot path %s and now %s was specified. " |
6326aa58 HWN |
1464 | "This doesn't work!" % (' '.join (self.depotPaths), |
1465 | ' '.join (args))) | |
b984733c | 1466 | sys.exit(1) |
6326aa58 | 1467 | |
bb6e09b2 | 1468 | self.depotPaths = sorted(args) |
b984733c | 1469 | |
1c49fc19 | 1470 | revision = "" |
b984733c | 1471 | self.users = {} |
b984733c | 1472 | |
6326aa58 HWN |
1473 | newPaths = [] |
1474 | for p in self.depotPaths: | |
1475 | if p.find("@") != -1: | |
1476 | atIdx = p.index("@") | |
1477 | self.changeRange = p[atIdx:] | |
1478 | if self.changeRange == "@all": | |
1479 | self.changeRange = "" | |
6a49f8e2 | 1480 | elif ',' not in self.changeRange: |
1c49fc19 | 1481 | revision = self.changeRange |
6326aa58 | 1482 | self.changeRange = "" |
7fcff9de | 1483 | p = p[:atIdx] |
6326aa58 HWN |
1484 | elif p.find("#") != -1: |
1485 | hashIdx = p.index("#") | |
1c49fc19 | 1486 | revision = p[hashIdx:] |
7fcff9de | 1487 | p = p[:hashIdx] |
6326aa58 | 1488 | elif self.previousDepotPaths == []: |
1c49fc19 | 1489 | revision = "#head" |
6326aa58 HWN |
1490 | |
1491 | p = re.sub ("\.\.\.$", "", p) | |
1492 | if not p.endswith("/"): | |
1493 | p += "/" | |
1494 | ||
1495 | newPaths.append(p) | |
1496 | ||
1497 | self.depotPaths = newPaths | |
1498 | ||
b984733c | 1499 | |
b607e71e | 1500 | self.loadUserMapFromCache() |
cb53e1f8 SH |
1501 | self.labels = {} |
1502 | if self.detectLabels: | |
1503 | self.getLabels(); | |
b984733c | 1504 | |
4b97ffb1 | 1505 | if self.detectBranches: |
df450923 SH |
1506 | ## FIXME - what's a P4 projectName ? |
1507 | self.projectName = self.guessProjectName() | |
1508 | ||
38f9f5ec SH |
1509 | if self.hasOrigin: |
1510 | self.getBranchMappingFromGitBranches() | |
1511 | else: | |
1512 | self.getBranchMapping() | |
29bdbac1 SH |
1513 | if self.verbose: |
1514 | print "p4-git branches: %s" % self.p4BranchesInGit | |
1515 | print "initial parents: %s" % self.initialParents | |
1516 | for b in self.p4BranchesInGit: | |
1517 | if b != "master": | |
6326aa58 HWN |
1518 | |
1519 | ## FIXME | |
29bdbac1 SH |
1520 | b = b[len(self.projectName):] |
1521 | self.createdBranches.add(b) | |
4b97ffb1 | 1522 | |
f291b4e3 | 1523 | self.tz = "%+03d%02d" % (- time.timezone / 3600, ((- time.timezone % 3600) / 60)) |
b984733c | 1524 | |
cebdf5af | 1525 | importProcess = subprocess.Popen(["git", "fast-import"], |
6326aa58 HWN |
1526 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
1527 | stderr=subprocess.PIPE); | |
08483580 SH |
1528 | self.gitOutput = importProcess.stdout |
1529 | self.gitStream = importProcess.stdin | |
1530 | self.gitError = importProcess.stderr | |
b984733c | 1531 | |
1c49fc19 | 1532 | if revision: |
c208a243 | 1533 | self.importHeadRevision(revision) |
b984733c SH |
1534 | else: |
1535 | changes = [] | |
1536 | ||
0828ab14 | 1537 | if len(self.changesFile) > 0: |
b984733c SH |
1538 | output = open(self.changesFile).readlines() |
1539 | changeSet = Set() | |
1540 | for line in output: | |
1541 | changeSet.add(int(line)) | |
1542 | ||
1543 | for change in changeSet: | |
1544 | changes.append(change) | |
1545 | ||
1546 | changes.sort() | |
1547 | else: | |
29bdbac1 | 1548 | if self.verbose: |
86dff6b6 | 1549 | print "Getting p4 changes for %s...%s" % (', '.join(self.depotPaths), |
6326aa58 | 1550 | self.changeRange) |
4f6432d8 | 1551 | changes = p4ChangesForPaths(self.depotPaths, self.changeRange) |
b984733c | 1552 | |
01a9c9c5 | 1553 | if len(self.maxChanges) > 0: |
7fcff9de | 1554 | changes = changes[:min(int(self.maxChanges), len(changes))] |
01a9c9c5 | 1555 | |
b984733c | 1556 | if len(changes) == 0: |
0828ab14 | 1557 | if not self.silent: |
341dc1c1 | 1558 | print "No changes to import!" |
1f52af6c | 1559 | return True |
b984733c | 1560 | |
a9d1a27a SH |
1561 | if not self.silent and not self.detectBranches: |
1562 | print "Import destination: %s" % self.branch | |
1563 | ||
341dc1c1 SH |
1564 | self.updatedBranches = set() |
1565 | ||
e87f37ae | 1566 | self.importChanges(changes) |
b984733c | 1567 | |
341dc1c1 SH |
1568 | if not self.silent: |
1569 | print "" | |
1570 | if len(self.updatedBranches) > 0: | |
1571 | sys.stdout.write("Updated branches: ") | |
1572 | for b in self.updatedBranches: | |
1573 | sys.stdout.write("%s " % b) | |
1574 | sys.stdout.write("\n") | |
b984733c | 1575 | |
b984733c | 1576 | self.gitStream.close() |
29bdbac1 SH |
1577 | if importProcess.wait() != 0: |
1578 | die("fast-import failed: %s" % self.gitError.read()) | |
b984733c SH |
1579 | self.gitOutput.close() |
1580 | self.gitError.close() | |
1581 | ||
b984733c SH |
1582 | return True |
1583 | ||
01ce1fe9 SH |
1584 | class P4Rebase(Command): |
1585 | def __init__(self): | |
1586 | Command.__init__(self) | |
01265103 | 1587 | self.options = [ ] |
cebdf5af HWN |
1588 | self.description = ("Fetches the latest revision from perforce and " |
1589 | + "rebases the current work (branch) against it") | |
68c42153 | 1590 | self.verbose = False |
01ce1fe9 SH |
1591 | |
1592 | def run(self, args): | |
1593 | sync = P4Sync() | |
1594 | sync.run([]) | |
d7e3868c | 1595 | |
14594f4b SH |
1596 | return self.rebase() |
1597 | ||
1598 | def rebase(self): | |
36ee4ee4 SH |
1599 | if os.system("git update-index --refresh") != 0: |
1600 | die("Some files in your working directory are modified and different than what is in your index. You can use git update-index <filename> to bring the index up-to-date or stash away all your changes with git stash."); | |
1601 | if len(read_pipe("git diff-index HEAD --")) > 0: | |
1602 | die("You have uncommited changes. Please commit them before rebasing or stash them away with git stash."); | |
1603 | ||
d7e3868c SH |
1604 | [upstream, settings] = findUpstreamBranchPoint() |
1605 | if len(upstream) == 0: | |
1606 | die("Cannot find upstream branchpoint for rebase") | |
1607 | ||
1608 | # the branchpoint may be p4/foo~3, so strip off the parent | |
1609 | upstream = re.sub("~[0-9]+$", "", upstream) | |
1610 | ||
1611 | print "Rebasing the current branch onto %s" % upstream | |
b25b2065 | 1612 | oldHead = read_pipe("git rev-parse HEAD").strip() |
d7e3868c | 1613 | system("git rebase %s" % upstream) |
1f52af6c | 1614 | system("git diff-tree --stat --summary -M %s HEAD" % oldHead) |
01ce1fe9 SH |
1615 | return True |
1616 | ||
f9a3a4f7 SH |
1617 | class P4Clone(P4Sync): |
1618 | def __init__(self): | |
1619 | P4Sync.__init__(self) | |
1620 | self.description = "Creates a new git repository and imports from Perforce into it" | |
bb6e09b2 | 1621 | self.usage = "usage: %prog [options] //depot/path[@revRange]" |
354081d5 | 1622 | self.options += [ |
bb6e09b2 HWN |
1623 | optparse.make_option("--destination", dest="cloneDestination", |
1624 | action='store', default=None, | |
354081d5 TT |
1625 | help="where to leave result of the clone"), |
1626 | optparse.make_option("-/", dest="cloneExclude", | |
1627 | action="append", type="string", | |
1628 | help="exclude depot path") | |
1629 | ] | |
bb6e09b2 | 1630 | self.cloneDestination = None |
f9a3a4f7 | 1631 | self.needsGit = False |
f9a3a4f7 | 1632 | |
354081d5 TT |
1633 | # This is required for the "append" cloneExclude action |
1634 | def ensure_value(self, attr, value): | |
1635 | if not hasattr(self, attr) or getattr(self, attr) is None: | |
1636 | setattr(self, attr, value) | |
1637 | return getattr(self, attr) | |
1638 | ||
6a49f8e2 HWN |
1639 | def defaultDestination(self, args): |
1640 | ## TODO: use common prefix of args? | |
1641 | depotPath = args[0] | |
1642 | depotDir = re.sub("(@[^@]*)$", "", depotPath) | |
1643 | depotDir = re.sub("(#[^#]*)$", "", depotDir) | |
053d9e43 | 1644 | depotDir = re.sub(r"\.\.\.$", "", depotDir) |
6a49f8e2 HWN |
1645 | depotDir = re.sub(r"/$", "", depotDir) |
1646 | return os.path.split(depotDir)[1] | |
1647 | ||
f9a3a4f7 SH |
1648 | def run(self, args): |
1649 | if len(args) < 1: | |
1650 | return False | |
bb6e09b2 HWN |
1651 | |
1652 | if self.keepRepoPath and not self.cloneDestination: | |
1653 | sys.stderr.write("Must specify destination for --keep-path\n") | |
1654 | sys.exit(1) | |
f9a3a4f7 | 1655 | |
6326aa58 | 1656 | depotPaths = args |
5e100b5c SH |
1657 | |
1658 | if not self.cloneDestination and len(depotPaths) > 1: | |
1659 | self.cloneDestination = depotPaths[-1] | |
1660 | depotPaths = depotPaths[:-1] | |
1661 | ||
354081d5 | 1662 | self.cloneExclude = ["/"+p for p in self.cloneExclude] |
6326aa58 HWN |
1663 | for p in depotPaths: |
1664 | if not p.startswith("//"): | |
1665 | return False | |
f9a3a4f7 | 1666 | |
bb6e09b2 | 1667 | if not self.cloneDestination: |
98ad4faf | 1668 | self.cloneDestination = self.defaultDestination(args) |
f9a3a4f7 | 1669 | |
86dff6b6 | 1670 | print "Importing from %s into %s" % (', '.join(depotPaths), self.cloneDestination) |
c3bf3f13 KG |
1671 | if not os.path.exists(self.cloneDestination): |
1672 | os.makedirs(self.cloneDestination) | |
bb6e09b2 | 1673 | os.chdir(self.cloneDestination) |
f9a3a4f7 | 1674 | system("git init") |
b86f7378 | 1675 | self.gitdir = os.getcwd() + "/.git" |
6326aa58 | 1676 | if not P4Sync.run(self, depotPaths): |
f9a3a4f7 | 1677 | return False |
f9a3a4f7 | 1678 | if self.branch != "master": |
8f9b2e08 SH |
1679 | if gitBranchExists("refs/remotes/p4/master"): |
1680 | system("git branch master refs/remotes/p4/master") | |
1681 | system("git checkout -f") | |
1682 | else: | |
1683 | print "Could not detect main branch. No checkout/master branch created." | |
86dff6b6 | 1684 | |
f9a3a4f7 SH |
1685 | return True |
1686 | ||
09d89de2 SH |
1687 | class P4Branches(Command): |
1688 | def __init__(self): | |
1689 | Command.__init__(self) | |
1690 | self.options = [ ] | |
1691 | self.description = ("Shows the git branches that hold imports and their " | |
1692 | + "corresponding perforce depot paths") | |
1693 | self.verbose = False | |
1694 | ||
1695 | def run(self, args): | |
5ca44617 SH |
1696 | if originP4BranchesExist(): |
1697 | createOrUpdateBranchesFromOrigin() | |
1698 | ||
09d89de2 SH |
1699 | cmdline = "git rev-parse --symbolic " |
1700 | cmdline += " --remotes" | |
1701 | ||
1702 | for line in read_pipe_lines(cmdline): | |
1703 | line = line.strip() | |
1704 | ||
1705 | if not line.startswith('p4/') or line == "p4/HEAD": | |
1706 | continue | |
1707 | branch = line | |
1708 | ||
1709 | log = extractLogMessageFromGitCommit("refs/remotes/%s" % branch) | |
1710 | settings = extractSettingsGitLog(log) | |
1711 | ||
1712 | print "%s <= %s (%s)" % (branch, ",".join(settings["depot-paths"]), settings["change"]) | |
1713 | return True | |
1714 | ||
b984733c SH |
1715 | class HelpFormatter(optparse.IndentedHelpFormatter): |
1716 | def __init__(self): | |
1717 | optparse.IndentedHelpFormatter.__init__(self) | |
1718 | ||
1719 | def format_description(self, description): | |
1720 | if description: | |
1721 | return description + "\n" | |
1722 | else: | |
1723 | return "" | |
4f5cf76a | 1724 | |
86949eef SH |
1725 | def printUsage(commands): |
1726 | print "usage: %s <command> [options]" % sys.argv[0] | |
1727 | print "" | |
1728 | print "valid commands: %s" % ", ".join(commands) | |
1729 | print "" | |
1730 | print "Try %s <command> --help for command specific help." % sys.argv[0] | |
1731 | print "" | |
1732 | ||
1733 | commands = { | |
b86f7378 HWN |
1734 | "debug" : P4Debug, |
1735 | "submit" : P4Submit, | |
a9834f58 | 1736 | "commit" : P4Submit, |
b86f7378 HWN |
1737 | "sync" : P4Sync, |
1738 | "rebase" : P4Rebase, | |
1739 | "clone" : P4Clone, | |
09d89de2 SH |
1740 | "rollback" : P4RollBack, |
1741 | "branches" : P4Branches | |
86949eef SH |
1742 | } |
1743 | ||
86949eef | 1744 | |
bb6e09b2 HWN |
1745 | def main(): |
1746 | if len(sys.argv[1:]) == 0: | |
1747 | printUsage(commands.keys()) | |
1748 | sys.exit(2) | |
4f5cf76a | 1749 | |
bb6e09b2 HWN |
1750 | cmd = "" |
1751 | cmdName = sys.argv[1] | |
1752 | try: | |
b86f7378 HWN |
1753 | klass = commands[cmdName] |
1754 | cmd = klass() | |
bb6e09b2 HWN |
1755 | except KeyError: |
1756 | print "unknown command %s" % cmdName | |
1757 | print "" | |
1758 | printUsage(commands.keys()) | |
1759 | sys.exit(2) | |
1760 | ||
1761 | options = cmd.options | |
b86f7378 | 1762 | cmd.gitdir = os.environ.get("GIT_DIR", None) |
bb6e09b2 HWN |
1763 | |
1764 | args = sys.argv[2:] | |
1765 | ||
1766 | if len(options) > 0: | |
1767 | options.append(optparse.make_option("--git-dir", dest="gitdir")) | |
1768 | ||
1769 | parser = optparse.OptionParser(cmd.usage.replace("%prog", "%prog " + cmdName), | |
1770 | options, | |
1771 | description = cmd.description, | |
1772 | formatter = HelpFormatter()) | |
1773 | ||
1774 | (cmd, args) = parser.parse_args(sys.argv[2:], cmd); | |
1775 | global verbose | |
1776 | verbose = cmd.verbose | |
1777 | if cmd.needsGit: | |
b86f7378 HWN |
1778 | if cmd.gitdir == None: |
1779 | cmd.gitdir = os.path.abspath(".git") | |
1780 | if not isValidGitDir(cmd.gitdir): | |
1781 | cmd.gitdir = read_pipe("git rev-parse --git-dir").strip() | |
1782 | if os.path.exists(cmd.gitdir): | |
bb6e09b2 HWN |
1783 | cdup = read_pipe("git rev-parse --show-cdup").strip() |
1784 | if len(cdup) > 0: | |
1785 | os.chdir(cdup); | |
e20a9e53 | 1786 | |
b86f7378 HWN |
1787 | if not isValidGitDir(cmd.gitdir): |
1788 | if isValidGitDir(cmd.gitdir + "/.git"): | |
1789 | cmd.gitdir += "/.git" | |
bb6e09b2 | 1790 | else: |
b86f7378 | 1791 | die("fatal: cannot locate git repository at %s" % cmd.gitdir) |
e20a9e53 | 1792 | |
b86f7378 | 1793 | os.environ["GIT_DIR"] = cmd.gitdir |
86949eef | 1794 | |
bb6e09b2 HWN |
1795 | if not cmd.run(args): |
1796 | parser.print_help() | |
4f5cf76a | 1797 | |
4f5cf76a | 1798 | |
bb6e09b2 HWN |
1799 | if __name__ == '__main__': |
1800 | main() |