]> git.ipfire.org Git - thirdparty/git.git/blame - git-p4.py
git p4: avoid shell when mapping users
[thirdparty/git.git] / git-p4.py
CommitLineData
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#
a33faf28
ER
10import sys
11if sys.hexversion < 0x02040000:
12 # The limiter is the subprocess module
13 sys.stderr.write("git-p4: requires Python 2.4 or later.\n")
14 sys.exit(1)
f629fa59
PW
15import os
16import optparse
17import marshal
18import subprocess
19import tempfile
20import time
21import platform
22import re
23import shutil
d20f0f8e 24import stat
8b41a97f 25
4addad22 26verbose = False
86949eef 27
06804c76 28# Only labels/tags matching this will be imported/exported
c8942a22 29defaultLabelRegexp = r'[a-zA-Z0-9_\-.]+$'
21a50753
AK
30
31def p4_build_cmd(cmd):
32 """Build a suitable p4 command line.
33
34 This consolidates building and returning a p4 command line into one
35 location. It means that hooking into the environment, or other configuration
36 can be done more easily.
37 """
6de040df 38 real_cmd = ["p4"]
abcaf073
AK
39
40 user = gitConfig("git-p4.user")
41 if len(user) > 0:
6de040df 42 real_cmd += ["-u",user]
abcaf073
AK
43
44 password = gitConfig("git-p4.password")
45 if len(password) > 0:
6de040df 46 real_cmd += ["-P", password]
abcaf073
AK
47
48 port = gitConfig("git-p4.port")
49 if len(port) > 0:
6de040df 50 real_cmd += ["-p", port]
abcaf073
AK
51
52 host = gitConfig("git-p4.host")
53 if len(host) > 0:
41799aa2 54 real_cmd += ["-H", host]
abcaf073
AK
55
56 client = gitConfig("git-p4.client")
57 if len(client) > 0:
6de040df 58 real_cmd += ["-c", client]
abcaf073 59
6de040df
LD
60
61 if isinstance(cmd,basestring):
62 real_cmd = ' '.join(real_cmd) + ' ' + cmd
63 else:
64 real_cmd += cmd
21a50753
AK
65 return real_cmd
66
053fd0c1 67def chdir(dir):
6de040df 68 # P4 uses the PWD environment variable rather than getcwd(). Since we're
bf1d68ff
GG
69 # not using the shell, we have to set it ourselves. This path could
70 # be relative, so go there first, then figure out where we ended up.
053fd0c1 71 os.chdir(dir)
bf1d68ff 72 os.environ['PWD'] = os.getcwd()
053fd0c1 73
86dff6b6
HWN
74def die(msg):
75 if verbose:
76 raise Exception(msg)
77 else:
78 sys.stderr.write(msg + "\n")
79 sys.exit(1)
80
6de040df 81def write_pipe(c, stdin):
4addad22 82 if verbose:
6de040df 83 sys.stderr.write('Writing pipe: %s\n' % str(c))
b016d397 84
6de040df
LD
85 expand = isinstance(c,basestring)
86 p = subprocess.Popen(c, stdin=subprocess.PIPE, shell=expand)
87 pipe = p.stdin
88 val = pipe.write(stdin)
89 pipe.close()
90 if p.wait():
91 die('Command failed: %s' % str(c))
b016d397
HWN
92
93 return val
94
6de040df 95def p4_write_pipe(c, stdin):
d9429194 96 real_cmd = p4_build_cmd(c)
6de040df 97 return write_pipe(real_cmd, stdin)
d9429194 98
4addad22
HWN
99def read_pipe(c, ignore_error=False):
100 if verbose:
6de040df 101 sys.stderr.write('Reading pipe: %s\n' % str(c))
8b41a97f 102
6de040df
LD
103 expand = isinstance(c,basestring)
104 p = subprocess.Popen(c, stdout=subprocess.PIPE, shell=expand)
105 pipe = p.stdout
b016d397 106 val = pipe.read()
6de040df
LD
107 if p.wait() and not ignore_error:
108 die('Command failed: %s' % str(c))
b016d397
HWN
109
110 return val
111
d9429194
AK
112def p4_read_pipe(c, ignore_error=False):
113 real_cmd = p4_build_cmd(c)
114 return read_pipe(real_cmd, ignore_error)
b016d397 115
bce4c5fc 116def read_pipe_lines(c):
4addad22 117 if verbose:
6de040df
LD
118 sys.stderr.write('Reading pipe: %s\n' % str(c))
119
120 expand = isinstance(c, basestring)
121 p = subprocess.Popen(c, stdout=subprocess.PIPE, shell=expand)
122 pipe = p.stdout
b016d397 123 val = pipe.readlines()
6de040df
LD
124 if pipe.close() or p.wait():
125 die('Command failed: %s' % str(c))
b016d397
HWN
126
127 return val
caace111 128
2318121b
AK
129def p4_read_pipe_lines(c):
130 """Specifically invoke p4 on the command supplied. """
155af834 131 real_cmd = p4_build_cmd(c)
2318121b
AK
132 return read_pipe_lines(real_cmd)
133
8e9497c2
GG
134def p4_has_command(cmd):
135 """Ask p4 for help on this command. If it returns an error, the
136 command does not exist in this version of p4."""
137 real_cmd = p4_build_cmd(["help", cmd])
138 p = subprocess.Popen(real_cmd, stdout=subprocess.PIPE,
139 stderr=subprocess.PIPE)
140 p.communicate()
141 return p.returncode == 0
142
249da4c0
PW
143def p4_has_move_command():
144 """See if the move command exists, that it supports -k, and that
145 it has not been administratively disabled. The arguments
146 must be correct, but the filenames do not have to exist. Use
147 ones with wildcards so even if they exist, it will fail."""
148
149 if not p4_has_command("move"):
150 return False
151 cmd = p4_build_cmd(["move", "-k", "@from", "@to"])
152 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
153 (out, err) = p.communicate()
154 # return code will be 1 in either case
155 if err.find("Invalid option") >= 0:
156 return False
157 if err.find("disabled") >= 0:
158 return False
159 # assume it failed because @... was invalid changelist
160 return True
161
6754a299 162def system(cmd):
6de040df 163 expand = isinstance(cmd,basestring)
4addad22 164 if verbose:
6de040df
LD
165 sys.stderr.write("executing %s\n" % str(cmd))
166 subprocess.check_call(cmd, shell=expand)
6754a299 167
bf9320f1
AK
168def p4_system(cmd):
169 """Specifically invoke p4 as the system command. """
155af834 170 real_cmd = p4_build_cmd(cmd)
6de040df
LD
171 expand = isinstance(real_cmd, basestring)
172 subprocess.check_call(real_cmd, shell=expand)
173
7f0e5962
PW
174_p4_version_string = None
175def p4_version_string():
176 """Read the version string, showing just the last line, which
177 hopefully is the interesting version bit.
178
179 $ p4 -V
180 Perforce - The Fast Software Configuration Management System.
181 Copyright 1995-2011 Perforce Software. All rights reserved.
182 Rev. P4/NTX86/2011.1/393975 (2011/12/16).
183 """
184 global _p4_version_string
185 if not _p4_version_string:
186 a = p4_read_pipe_lines(["-V"])
187 _p4_version_string = a[-1].rstrip()
188 return _p4_version_string
189
6de040df 190def p4_integrate(src, dest):
9d7d446a 191 p4_system(["integrate", "-Dt", wildcard_encode(src), wildcard_encode(dest)])
6de040df 192
8d7ec362 193def p4_sync(f, *options):
9d7d446a 194 p4_system(["sync"] + list(options) + [wildcard_encode(f)])
6de040df
LD
195
196def p4_add(f):
9d7d446a
PW
197 # forcibly add file names with wildcards
198 if wildcard_present(f):
199 p4_system(["add", "-f", f])
200 else:
201 p4_system(["add", f])
6de040df
LD
202
203def p4_delete(f):
9d7d446a 204 p4_system(["delete", wildcard_encode(f)])
6de040df
LD
205
206def p4_edit(f):
9d7d446a 207 p4_system(["edit", wildcard_encode(f)])
6de040df
LD
208
209def p4_revert(f):
9d7d446a 210 p4_system(["revert", wildcard_encode(f)])
6de040df 211
9d7d446a
PW
212def p4_reopen(type, f):
213 p4_system(["reopen", "-t", type, wildcard_encode(f)])
bf9320f1 214
8e9497c2
GG
215def p4_move(src, dest):
216 p4_system(["move", "-k", wildcard_encode(src), wildcard_encode(dest)])
217
18fa13d0
PW
218def p4_describe(change):
219 """Make sure it returns a valid result by checking for
220 the presence of field "time". Return a dict of the
221 results."""
222
223 ds = p4CmdList(["describe", "-s", str(change)])
224 if len(ds) != 1:
225 die("p4 describe -s %d did not return 1 result: %s" % (change, str(ds)))
226
227 d = ds[0]
228
229 if "p4ExitCode" in d:
230 die("p4 describe -s %d exited with %d: %s" % (change, d["p4ExitCode"],
231 str(d)))
232 if "code" in d:
233 if d["code"] == "error":
234 die("p4 describe -s %d returned error code: %s" % (change, str(d)))
235
236 if "time" not in d:
237 die("p4 describe -s %d returned no \"time\": %s" % (change, str(d)))
238
239 return d
240
9cffb8c8
PW
241#
242# Canonicalize the p4 type and return a tuple of the
243# base type, plus any modifiers. See "p4 help filetypes"
244# for a list and explanation.
245#
246def split_p4_type(p4type):
247
248 p4_filetypes_historical = {
249 "ctempobj": "binary+Sw",
250 "ctext": "text+C",
251 "cxtext": "text+Cx",
252 "ktext": "text+k",
253 "kxtext": "text+kx",
254 "ltext": "text+F",
255 "tempobj": "binary+FSw",
256 "ubinary": "binary+F",
257 "uresource": "resource+F",
258 "uxbinary": "binary+Fx",
259 "xbinary": "binary+x",
260 "xltext": "text+Fx",
261 "xtempobj": "binary+Swx",
262 "xtext": "text+x",
263 "xunicode": "unicode+x",
264 "xutf16": "utf16+x",
265 }
266 if p4type in p4_filetypes_historical:
267 p4type = p4_filetypes_historical[p4type]
268 mods = ""
269 s = p4type.split("+")
270 base = s[0]
271 mods = ""
272 if len(s) > 1:
273 mods = s[1]
274 return (base, mods)
b9fc6ea9 275
60df071c
LD
276#
277# return the raw p4 type of a file (text, text+ko, etc)
278#
279def p4_type(file):
280 results = p4CmdList(["fstat", "-T", "headType", file])
281 return results[0]['headType']
282
283#
284# Given a type base and modifier, return a regexp matching
285# the keywords that can be expanded in the file
286#
287def p4_keywords_regexp_for_type(base, type_mods):
288 if base in ("text", "unicode", "binary"):
289 kwords = None
290 if "ko" in type_mods:
291 kwords = 'Id|Header'
292 elif "k" in type_mods:
293 kwords = 'Id|Header|Author|Date|DateTime|Change|File|Revision'
294 else:
295 return None
296 pattern = r"""
297 \$ # Starts with a dollar, followed by...
298 (%s) # one of the keywords, followed by...
6b2bf41e 299 (:[^$\n]+)? # possibly an old expansion, followed by...
60df071c
LD
300 \$ # another dollar
301 """ % kwords
302 return pattern
303 else:
304 return None
305
306#
307# Given a file, return a regexp matching the possible
308# RCS keywords that will be expanded, or None for files
309# with kw expansion turned off.
310#
311def p4_keywords_regexp_for_file(file):
312 if not os.path.exists(file):
313 return None
314 else:
315 (type_base, type_mods) = split_p4_type(p4_type(file))
316 return p4_keywords_regexp_for_type(type_base, type_mods)
b9fc6ea9 317
c65b670e
CP
318def setP4ExecBit(file, mode):
319 # Reopens an already open file and changes the execute bit to match
320 # the execute bit setting in the passed in mode.
321
322 p4Type = "+x"
323
324 if not isModeExec(mode):
325 p4Type = getP4OpenedType(file)
326 p4Type = re.sub('^([cku]?)x(.*)', '\\1\\2', p4Type)
327 p4Type = re.sub('(.*?\+.*?)x(.*?)', '\\1\\2', p4Type)
328 if p4Type[-1] == "+":
329 p4Type = p4Type[0:-1]
330
6de040df 331 p4_reopen(p4Type, file)
c65b670e
CP
332
333def getP4OpenedType(file):
334 # Returns the perforce file type for the given file.
335
9d7d446a 336 result = p4_read_pipe(["opened", wildcard_encode(file)])
f3e5ae4f 337 match = re.match(".*\((.+)\)\r?$", result)
c65b670e
CP
338 if match:
339 return match.group(1)
340 else:
f3e5ae4f 341 die("Could not determine file type for %s (result: '%s')" % (file, result))
c65b670e 342
06804c76
LD
343# Return the set of all p4 labels
344def getP4Labels(depotPaths):
345 labels = set()
346 if isinstance(depotPaths,basestring):
347 depotPaths = [depotPaths]
348
349 for l in p4CmdList(["labels"] + ["%s..." % p for p in depotPaths]):
350 label = l['label']
351 labels.add(label)
352
353 return labels
354
355# Return the set of all git tags
356def getGitTags():
357 gitTags = set()
358 for line in read_pipe_lines(["git", "tag"]):
359 tag = line.strip()
360 gitTags.add(tag)
361 return gitTags
362
b43b0a3c
CP
363def diffTreePattern():
364 # This is a simple generator for the diff tree regex pattern. This could be
365 # a class variable if this and parseDiffTreeEntry were a part of a class.
366 pattern = re.compile(':(\d+) (\d+) (\w+) (\w+) ([A-Z])(\d+)?\t(.*?)((\t(.*))|$)')
367 while True:
368 yield pattern
369
370def parseDiffTreeEntry(entry):
371 """Parses a single diff tree entry into its component elements.
372
373 See git-diff-tree(1) manpage for details about the format of the diff
374 output. This method returns a dictionary with the following elements:
375
376 src_mode - The mode of the source file
377 dst_mode - The mode of the destination file
378 src_sha1 - The sha1 for the source file
379 dst_sha1 - The sha1 fr the destination file
380 status - The one letter status of the diff (i.e. 'A', 'M', 'D', etc)
381 status_score - The score for the status (applicable for 'C' and 'R'
382 statuses). This is None if there is no score.
383 src - The path for the source file.
384 dst - The path for the destination file. This is only present for
385 copy or renames. If it is not present, this is None.
386
387 If the pattern is not matched, None is returned."""
388
389 match = diffTreePattern().next().match(entry)
390 if match:
391 return {
392 'src_mode': match.group(1),
393 'dst_mode': match.group(2),
394 'src_sha1': match.group(3),
395 'dst_sha1': match.group(4),
396 'status': match.group(5),
397 'status_score': match.group(6),
398 'src': match.group(7),
399 'dst': match.group(10)
400 }
401 return None
402
c65b670e
CP
403def isModeExec(mode):
404 # Returns True if the given git mode represents an executable file,
405 # otherwise False.
406 return mode[-3:] == "755"
407
408def isModeExecChanged(src_mode, dst_mode):
409 return isModeExec(src_mode) != isModeExec(dst_mode)
410
b932705b 411def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None):
6de040df
LD
412
413 if isinstance(cmd,basestring):
414 cmd = "-G " + cmd
415 expand = True
416 else:
417 cmd = ["-G"] + cmd
418 expand = False
419
420 cmd = p4_build_cmd(cmd)
6a49f8e2 421 if verbose:
6de040df 422 sys.stderr.write("Opening pipe: %s\n" % str(cmd))
9f90c733
SL
423
424 # Use a temporary file to avoid deadlocks without
425 # subprocess.communicate(), which would put another copy
426 # of stdout into memory.
427 stdin_file = None
428 if stdin is not None:
429 stdin_file = tempfile.TemporaryFile(prefix='p4-stdin', mode=stdin_mode)
6de040df
LD
430 if isinstance(stdin,basestring):
431 stdin_file.write(stdin)
432 else:
433 for i in stdin:
434 stdin_file.write(i + '\n')
9f90c733
SL
435 stdin_file.flush()
436 stdin_file.seek(0)
437
6de040df
LD
438 p4 = subprocess.Popen(cmd,
439 shell=expand,
9f90c733
SL
440 stdin=stdin_file,
441 stdout=subprocess.PIPE)
86949eef
SH
442
443 result = []
444 try:
445 while True:
9f90c733 446 entry = marshal.load(p4.stdout)
c3f6163b
AG
447 if cb is not None:
448 cb(entry)
449 else:
450 result.append(entry)
86949eef
SH
451 except EOFError:
452 pass
9f90c733
SL
453 exitCode = p4.wait()
454 if exitCode != 0:
ac3e0d79
SH
455 entry = {}
456 entry["p4ExitCode"] = exitCode
457 result.append(entry)
86949eef
SH
458
459 return result
460
461def p4Cmd(cmd):
462 list = p4CmdList(cmd)
463 result = {}
464 for entry in list:
465 result.update(entry)
466 return result;
467
cb2c9db5
SH
468def p4Where(depotPath):
469 if not depotPath.endswith("/"):
470 depotPath += "/"
7f705dc3 471 depotPath = depotPath + "..."
6de040df 472 outputList = p4CmdList(["where", depotPath])
7f705dc3
TAL
473 output = None
474 for entry in outputList:
75bc9573
TAL
475 if "depotFile" in entry:
476 if entry["depotFile"] == depotPath:
477 output = entry
478 break
479 elif "data" in entry:
480 data = entry.get("data")
481 space = data.find(" ")
482 if data[:space] == depotPath:
483 output = entry
484 break
7f705dc3
TAL
485 if output == None:
486 return ""
dc524036
SH
487 if output["code"] == "error":
488 return ""
cb2c9db5
SH
489 clientPath = ""
490 if "path" in output:
491 clientPath = output.get("path")
492 elif "data" in output:
493 data = output.get("data")
494 lastSpace = data.rfind(" ")
495 clientPath = data[lastSpace + 1:]
496
497 if clientPath.endswith("..."):
498 clientPath = clientPath[:-3]
499 return clientPath
500
86949eef 501def currentGitBranch():
b25b2065 502 return read_pipe("git name-rev HEAD").split(" ")[1].strip()
86949eef 503
4f5cf76a 504def isValidGitDir(path):
bb6e09b2
HWN
505 if (os.path.exists(path + "/HEAD")
506 and os.path.exists(path + "/refs") and os.path.exists(path + "/objects")):
4f5cf76a
SH
507 return True;
508 return False
509
463e8af6 510def parseRevision(ref):
b25b2065 511 return read_pipe("git rev-parse %s" % ref).strip()
463e8af6 512
28755dba
PW
513def branchExists(ref):
514 rev = read_pipe(["git", "rev-parse", "-q", "--verify", ref],
515 ignore_error=True)
516 return len(rev) > 0
517
6ae8de88
SH
518def extractLogMessageFromGitCommit(commit):
519 logMessage = ""
b016d397
HWN
520
521 ## fixme: title is first line of commit, not 1st paragraph.
6ae8de88 522 foundTitle = False
b016d397 523 for log in read_pipe_lines("git cat-file commit %s" % commit):
6ae8de88
SH
524 if not foundTitle:
525 if len(log) == 1:
1c094184 526 foundTitle = True
6ae8de88
SH
527 continue
528
529 logMessage += log
530 return logMessage
531
bb6e09b2 532def extractSettingsGitLog(log):
6ae8de88
SH
533 values = {}
534 for line in log.split("\n"):
535 line = line.strip()
6326aa58
HWN
536 m = re.search (r"^ *\[git-p4: (.*)\]$", line)
537 if not m:
538 continue
539
540 assignments = m.group(1).split (':')
541 for a in assignments:
542 vals = a.split ('=')
543 key = vals[0].strip()
544 val = ('='.join (vals[1:])).strip()
545 if val.endswith ('\"') and val.startswith('"'):
546 val = val[1:-1]
547
548 values[key] = val
549
845b42cb
SH
550 paths = values.get("depot-paths")
551 if not paths:
552 paths = values.get("depot-path")
a3fdd579
SH
553 if paths:
554 values['depot-paths'] = paths.split(',')
bb6e09b2 555 return values
6ae8de88 556
8136a639 557def gitBranchExists(branch):
bb6e09b2
HWN
558 proc = subprocess.Popen(["git", "rev-parse", branch],
559 stderr=subprocess.PIPE, stdout=subprocess.PIPE);
caace111 560 return proc.wait() == 0;
8136a639 561
36bd8446 562_gitConfig = {}
99f790f2 563def gitConfig(key, args = None): # set args to "--bool", for instance
36bd8446 564 if not _gitConfig.has_key(key):
99f790f2
TAL
565 argsFilter = ""
566 if args != None:
567 argsFilter = "%s " % args
568 cmd = "git config %s%s" % (argsFilter, key)
569 _gitConfig[key] = read_pipe(cmd, ignore_error=True).strip()
36bd8446 570 return _gitConfig[key]
01265103 571
7199cf13
VA
572def gitConfigList(key):
573 if not _gitConfig.has_key(key):
574 _gitConfig[key] = read_pipe("git config --get-all %s" % key, ignore_error=True).strip().split(os.linesep)
575 return _gitConfig[key]
576
2c8037ed
PW
577def p4BranchesInGit(branchesAreInRemotes=True):
578 """Find all the branches whose names start with "p4/", looking
579 in remotes or heads as specified by the argument. Return
580 a dictionary of { branch: revision } for each one found.
581 The branch names are the short names, without any
582 "p4/" prefix."""
583
062410bb
SH
584 branches = {}
585
586 cmdline = "git rev-parse --symbolic "
587 if branchesAreInRemotes:
2c8037ed 588 cmdline += "--remotes"
062410bb 589 else:
2c8037ed 590 cmdline += "--branches"
062410bb
SH
591
592 for line in read_pipe_lines(cmdline):
593 line = line.strip()
594
2c8037ed
PW
595 # only import to p4/
596 if not line.startswith('p4/'):
597 continue
598 # special symbolic ref to p4/master
599 if line == "p4/HEAD":
062410bb 600 continue
062410bb 601
2c8037ed
PW
602 # strip off p4/ prefix
603 branch = line[len("p4/"):]
062410bb
SH
604
605 branches[branch] = parseRevision(line)
2c8037ed 606
062410bb
SH
607 return branches
608
5a8e84cd
PW
609def branch_exists(branch):
610 """Make sure that the given ref name really exists."""
611
612 cmd = [ "git", "rev-parse", "--symbolic", "--verify", branch ]
613 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
614 out, _ = p.communicate()
615 if p.returncode:
616 return False
617 # expect exactly one line of output: the branch name
618 return out.rstrip() == branch
619
9ceab363 620def findUpstreamBranchPoint(head = "HEAD"):
86506fe5
SH
621 branches = p4BranchesInGit()
622 # map from depot-path to branch name
623 branchByDepotPath = {}
624 for branch in branches.keys():
625 tip = branches[branch]
626 log = extractLogMessageFromGitCommit(tip)
627 settings = extractSettingsGitLog(log)
628 if settings.has_key("depot-paths"):
629 paths = ",".join(settings["depot-paths"])
630 branchByDepotPath[paths] = "remotes/p4/" + branch
631
27d2d811 632 settings = None
27d2d811
SH
633 parent = 0
634 while parent < 65535:
9ceab363 635 commit = head + "~%s" % parent
27d2d811
SH
636 log = extractLogMessageFromGitCommit(commit)
637 settings = extractSettingsGitLog(log)
86506fe5
SH
638 if settings.has_key("depot-paths"):
639 paths = ",".join(settings["depot-paths"])
640 if branchByDepotPath.has_key(paths):
641 return [branchByDepotPath[paths], settings]
27d2d811 642
86506fe5 643 parent = parent + 1
27d2d811 644
86506fe5 645 return ["", settings]
27d2d811 646
5ca44617
SH
647def createOrUpdateBranchesFromOrigin(localRefPrefix = "refs/remotes/p4/", silent=True):
648 if not silent:
649 print ("Creating/updating branch(es) in %s based on origin branch(es)"
650 % localRefPrefix)
651
652 originPrefix = "origin/p4/"
653
654 for line in read_pipe_lines("git rev-parse --symbolic --remotes"):
655 line = line.strip()
656 if (not line.startswith(originPrefix)) or line.endswith("HEAD"):
657 continue
658
659 headName = line[len(originPrefix):]
660 remoteHead = localRefPrefix + headName
661 originHead = line
662
663 original = extractSettingsGitLog(extractLogMessageFromGitCommit(originHead))
664 if (not original.has_key('depot-paths')
665 or not original.has_key('change')):
666 continue
667
668 update = False
669 if not gitBranchExists(remoteHead):
670 if verbose:
671 print "creating %s" % remoteHead
672 update = True
673 else:
674 settings = extractSettingsGitLog(extractLogMessageFromGitCommit(remoteHead))
675 if settings.has_key('change') > 0:
676 if settings['depot-paths'] == original['depot-paths']:
677 originP4Change = int(original['change'])
678 p4Change = int(settings['change'])
679 if originP4Change > p4Change:
680 print ("%s (%s) is newer than %s (%s). "
681 "Updating p4 branch from origin."
682 % (originHead, originP4Change,
683 remoteHead, p4Change))
684 update = True
685 else:
686 print ("Ignoring: %s was imported from %s while "
687 "%s was imported from %s"
688 % (originHead, ','.join(original['depot-paths']),
689 remoteHead, ','.join(settings['depot-paths'])))
690
691 if update:
692 system("git update-ref %s %s" % (remoteHead, originHead))
693
694def originP4BranchesExist():
695 return gitBranchExists("origin") or gitBranchExists("origin/p4") or gitBranchExists("origin/p4/master")
696
4f6432d8
SH
697def p4ChangesForPaths(depotPaths, changeRange):
698 assert depotPaths
6de040df
LD
699 cmd = ['changes']
700 for p in depotPaths:
701 cmd += ["%s...%s" % (p, changeRange)]
702 output = p4_read_pipe_lines(cmd)
4f6432d8 703
b4b0ba06 704 changes = {}
4f6432d8 705 for line in output:
c3f6163b
AG
706 changeNum = int(line.split(" ")[1])
707 changes[changeNum] = True
4f6432d8 708
b4b0ba06
PW
709 changelist = changes.keys()
710 changelist.sort()
711 return changelist
4f6432d8 712
d53de8b9
TAL
713def p4PathStartsWith(path, prefix):
714 # This method tries to remedy a potential mixed-case issue:
715 #
716 # If UserA adds //depot/DirA/file1
717 # and UserB adds //depot/dira/file2
718 #
719 # we may or may not have a problem. If you have core.ignorecase=true,
720 # we treat DirA and dira as the same directory
721 ignorecase = gitConfig("core.ignorecase", "--bool") == "true"
722 if ignorecase:
723 return path.lower().startswith(prefix.lower())
724 return path.startswith(prefix)
725
543987bd
PW
726def getClientSpec():
727 """Look at the p4 client spec, create a View() object that contains
728 all the mappings, and return it."""
729
730 specList = p4CmdList("client -o")
731 if len(specList) != 1:
732 die('Output from "client -o" is %d lines, expecting 1' %
733 len(specList))
734
735 # dictionary of all client parameters
736 entry = specList[0]
737
738 # just the keys that start with "View"
739 view_keys = [ k for k in entry.keys() if k.startswith("View") ]
740
741 # hold this new View
742 view = View()
743
744 # append the lines, in order, to the view
745 for view_num in range(len(view_keys)):
746 k = "View%d" % view_num
747 if k not in view_keys:
748 die("Expected view key %s missing" % k)
749 view.append(entry[k])
750
751 return view
752
753def getClientRoot():
754 """Grab the client directory."""
755
756 output = p4CmdList("client -o")
757 if len(output) != 1:
758 die('Output from "client -o" is %d lines, expecting 1' % len(output))
759
760 entry = output[0]
761 if "Root" not in entry:
762 die('Client has no "Root"')
763
764 return entry["Root"]
765
9d7d446a
PW
766#
767# P4 wildcards are not allowed in filenames. P4 complains
768# if you simply add them, but you can force it with "-f", in
769# which case it translates them into %xx encoding internally.
770#
771def wildcard_decode(path):
772 # Search for and fix just these four characters. Do % last so
773 # that fixing it does not inadvertently create new %-escapes.
774 # Cannot have * in a filename in windows; untested as to
775 # what p4 would do in such a case.
776 if not platform.system() == "Windows":
777 path = path.replace("%2A", "*")
778 path = path.replace("%23", "#") \
779 .replace("%40", "@") \
780 .replace("%25", "%")
781 return path
782
783def wildcard_encode(path):
784 # do % first to avoid double-encoding the %s introduced here
785 path = path.replace("%", "%25") \
786 .replace("*", "%2A") \
787 .replace("#", "%23") \
788 .replace("@", "%40")
789 return path
790
791def wildcard_present(path):
792 return path.translate(None, "*#@%") != path
793
b984733c
SH
794class Command:
795 def __init__(self):
796 self.usage = "usage: %prog [options]"
8910ac0e 797 self.needsGit = True
6a10b6aa 798 self.verbose = False
b984733c 799
3ea2cfd4
LD
800class P4UserMap:
801 def __init__(self):
802 self.userMapFromPerforceServer = False
affb474f
LD
803 self.myP4UserId = None
804
805 def p4UserId(self):
806 if self.myP4UserId:
807 return self.myP4UserId
808
809 results = p4CmdList("user -o")
810 for r in results:
811 if r.has_key('User'):
812 self.myP4UserId = r['User']
813 return r['User']
814 die("Could not find your p4 user id")
815
816 def p4UserIsMe(self, p4User):
817 # return True if the given p4 user is actually me
818 me = self.p4UserId()
819 if not p4User or p4User != me:
820 return False
821 else:
822 return True
3ea2cfd4
LD
823
824 def getUserCacheFilename(self):
825 home = os.environ.get("HOME", os.environ.get("USERPROFILE"))
826 return home + "/.gitp4-usercache.txt"
827
828 def getUserMapFromPerforceServer(self):
829 if self.userMapFromPerforceServer:
830 return
831 self.users = {}
832 self.emails = {}
833
834 for output in p4CmdList("users"):
835 if not output.has_key("User"):
836 continue
837 self.users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">"
838 self.emails[output["Email"]] = output["User"]
839
840
841 s = ''
842 for (key, val) in self.users.items():
843 s += "%s\t%s\n" % (key.expandtabs(1), val.expandtabs(1))
844
845 open(self.getUserCacheFilename(), "wb").write(s)
846 self.userMapFromPerforceServer = True
847
848 def loadUserMapFromCache(self):
849 self.users = {}
850 self.userMapFromPerforceServer = False
851 try:
852 cache = open(self.getUserCacheFilename(), "rb")
853 lines = cache.readlines()
854 cache.close()
855 for line in lines:
856 entry = line.strip().split("\t")
857 self.users[entry[0]] = entry[1]
858 except IOError:
859 self.getUserMapFromPerforceServer()
860
b984733c 861class P4Debug(Command):
86949eef 862 def __init__(self):
6ae8de88 863 Command.__init__(self)
6a10b6aa 864 self.options = []
c8c39116 865 self.description = "A tool to debug the output of p4 -G."
8910ac0e 866 self.needsGit = False
86949eef
SH
867
868 def run(self, args):
b1ce9447 869 j = 0
6de040df 870 for output in p4CmdList(args):
b1ce9447
HWN
871 print 'Element: %d' % j
872 j += 1
86949eef 873 print output
b984733c 874 return True
86949eef 875
5834684d
SH
876class P4RollBack(Command):
877 def __init__(self):
878 Command.__init__(self)
879 self.options = [
0c66a783 880 optparse.make_option("--local", dest="rollbackLocalBranches", action="store_true")
5834684d
SH
881 ]
882 self.description = "A tool to debug the multi-branch import. Don't use :)"
0c66a783 883 self.rollbackLocalBranches = False
5834684d
SH
884
885 def run(self, args):
886 if len(args) != 1:
887 return False
888 maxChange = int(args[0])
0c66a783 889
ad192f28 890 if "p4ExitCode" in p4Cmd("changes -m 1"):
66a2f523
SH
891 die("Problems executing p4");
892
0c66a783
SH
893 if self.rollbackLocalBranches:
894 refPrefix = "refs/heads/"
b016d397 895 lines = read_pipe_lines("git rev-parse --symbolic --branches")
0c66a783
SH
896 else:
897 refPrefix = "refs/remotes/"
b016d397 898 lines = read_pipe_lines("git rev-parse --symbolic --remotes")
0c66a783
SH
899
900 for line in lines:
901 if self.rollbackLocalBranches or (line.startswith("p4/") and line != "p4/HEAD\n"):
b25b2065
HWN
902 line = line.strip()
903 ref = refPrefix + line
5834684d 904 log = extractLogMessageFromGitCommit(ref)
bb6e09b2
HWN
905 settings = extractSettingsGitLog(log)
906
907 depotPaths = settings['depot-paths']
908 change = settings['change']
909
5834684d 910 changed = False
52102d47 911
6326aa58
HWN
912 if len(p4Cmd("changes -m 1 " + ' '.join (['%s...@%s' % (p, maxChange)
913 for p in depotPaths]))) == 0:
52102d47
SH
914 print "Branch %s did not exist at change %s, deleting." % (ref, maxChange)
915 system("git update-ref -d %s `git rev-parse %s`" % (ref, ref))
916 continue
917
bb6e09b2 918 while change and int(change) > maxChange:
5834684d 919 changed = True
52102d47
SH
920 if self.verbose:
921 print "%s is at %s ; rewinding towards %s" % (ref, change, maxChange)
5834684d
SH
922 system("git update-ref %s \"%s^\"" % (ref, ref))
923 log = extractLogMessageFromGitCommit(ref)
bb6e09b2
HWN
924 settings = extractSettingsGitLog(log)
925
926
927 depotPaths = settings['depot-paths']
928 change = settings['change']
5834684d
SH
929
930 if changed:
52102d47 931 print "%s rewound to %s" % (ref, change)
5834684d
SH
932
933 return True
934
3ea2cfd4 935class P4Submit(Command, P4UserMap):
6bbfd137
PW
936
937 conflict_behavior_choices = ("ask", "skip", "quit")
938
4f5cf76a 939 def __init__(self):
b984733c 940 Command.__init__(self)
3ea2cfd4 941 P4UserMap.__init__(self)
4f5cf76a 942 self.options = [
4f5cf76a 943 optparse.make_option("--origin", dest="origin"),
ae901090 944 optparse.make_option("-M", dest="detectRenames", action="store_true"),
3ea2cfd4
LD
945 # preserve the user, requires relevant p4 permissions
946 optparse.make_option("--preserve-user", dest="preserveUser", action="store_true"),
06804c76 947 optparse.make_option("--export-labels", dest="exportLabels", action="store_true"),
ef739f08 948 optparse.make_option("--dry-run", "-n", dest="dry_run", action="store_true"),
728b7ad8 949 optparse.make_option("--prepare-p4-only", dest="prepare_p4_only", action="store_true"),
6bbfd137 950 optparse.make_option("--conflict", dest="conflict_behavior",
44e8d26c
PW
951 choices=self.conflict_behavior_choices),
952 optparse.make_option("--branch", dest="branch"),
4f5cf76a
SH
953 ]
954 self.description = "Submit changes from git to the perforce depot."
c9b50e63 955 self.usage += " [name of git branch to submit into perforce depot]"
9512497b 956 self.origin = ""
ae901090 957 self.detectRenames = False
3ea2cfd4 958 self.preserveUser = gitConfig("git-p4.preserveUser").lower() == "true"
ef739f08 959 self.dry_run = False
728b7ad8 960 self.prepare_p4_only = False
6bbfd137 961 self.conflict_behavior = None
f7baba8b 962 self.isWindows = (platform.system() == "Windows")
06804c76 963 self.exportLabels = False
249da4c0 964 self.p4HasMoveCommand = p4_has_move_command()
44e8d26c 965 self.branch = None
4f5cf76a 966
4f5cf76a
SH
967 def check(self):
968 if len(p4CmdList("opened ...")) > 0:
969 die("You have files opened with perforce! Close them before starting the sync.")
970
f19cb0a0
PW
971 def separate_jobs_from_description(self, message):
972 """Extract and return a possible Jobs field in the commit
973 message. It goes into a separate section in the p4 change
974 specification.
975
976 A jobs line starts with "Jobs:" and looks like a new field
977 in a form. Values are white-space separated on the same
978 line or on following lines that start with a tab.
979
980 This does not parse and extract the full git commit message
981 like a p4 form. It just sees the Jobs: line as a marker
982 to pass everything from then on directly into the p4 form,
983 but outside the description section.
984
985 Return a tuple (stripped log message, jobs string)."""
986
987 m = re.search(r'^Jobs:', message, re.MULTILINE)
988 if m is None:
989 return (message, None)
990
991 jobtext = message[m.start():]
992 stripped_message = message[:m.start()].rstrip()
993 return (stripped_message, jobtext)
994
995 def prepareLogMessage(self, template, message, jobs):
996 """Edits the template returned from "p4 change -o" to insert
997 the message in the Description field, and the jobs text in
998 the Jobs field."""
4f5cf76a
SH
999 result = ""
1000
edae1e2f
SH
1001 inDescriptionSection = False
1002
4f5cf76a
SH
1003 for line in template.split("\n"):
1004 if line.startswith("#"):
1005 result += line + "\n"
1006 continue
1007
edae1e2f 1008 if inDescriptionSection:
c9dbab04 1009 if line.startswith("Files:") or line.startswith("Jobs:"):
edae1e2f 1010 inDescriptionSection = False
f19cb0a0
PW
1011 # insert Jobs section
1012 if jobs:
1013 result += jobs + "\n"
edae1e2f
SH
1014 else:
1015 continue
1016 else:
1017 if line.startswith("Description:"):
1018 inDescriptionSection = True
1019 line += "\n"
1020 for messageLine in message.split("\n"):
1021 line += "\t" + messageLine + "\n"
1022
1023 result += line + "\n"
4f5cf76a
SH
1024
1025 return result
1026
60df071c
LD
1027 def patchRCSKeywords(self, file, pattern):
1028 # Attempt to zap the RCS keywords in a p4 controlled file matching the given pattern
1029 (handle, outFileName) = tempfile.mkstemp(dir='.')
1030 try:
1031 outFile = os.fdopen(handle, "w+")
1032 inFile = open(file, "r")
1033 regexp = re.compile(pattern, re.VERBOSE)
1034 for line in inFile.readlines():
1035 line = regexp.sub(r'$\1$', line)
1036 outFile.write(line)
1037 inFile.close()
1038 outFile.close()
1039 # Forcibly overwrite the original file
1040 os.unlink(file)
1041 shutil.move(outFileName, file)
1042 except:
1043 # cleanup our temporary file
1044 os.unlink(outFileName)
1045 print "Failed to strip RCS keywords in %s" % file
1046 raise
1047
1048 print "Patched up RCS keywords in %s" % file
1049
3ea2cfd4
LD
1050 def p4UserForCommit(self,id):
1051 # Return the tuple (perforce user,git email) for a given git commit id
1052 self.getUserMapFromPerforceServer()
9bf28855
PW
1053 gitEmail = read_pipe(["git", "log", "--max-count=1",
1054 "--format=%ae", id])
3ea2cfd4
LD
1055 gitEmail = gitEmail.strip()
1056 if not self.emails.has_key(gitEmail):
1057 return (None,gitEmail)
1058 else:
1059 return (self.emails[gitEmail],gitEmail)
1060
1061 def checkValidP4Users(self,commits):
1062 # check if any git authors cannot be mapped to p4 users
1063 for id in commits:
1064 (user,email) = self.p4UserForCommit(id)
1065 if not user:
1066 msg = "Cannot find p4 user for email %s in commit %s." % (email, id)
1067 if gitConfig('git-p4.allowMissingP4Users').lower() == "true":
1068 print "%s" % msg
1069 else:
1070 die("Error: %s\nSet git-p4.allowMissingP4Users to true to allow this." % msg)
1071
1072 def lastP4Changelist(self):
1073 # Get back the last changelist number submitted in this client spec. This
1074 # then gets used to patch up the username in the change. If the same
1075 # client spec is being used by multiple processes then this might go
1076 # wrong.
1077 results = p4CmdList("client -o") # find the current client
1078 client = None
1079 for r in results:
1080 if r.has_key('Client'):
1081 client = r['Client']
1082 break
1083 if not client:
1084 die("could not get client spec")
6de040df 1085 results = p4CmdList(["changes", "-c", client, "-m", "1"])
3ea2cfd4
LD
1086 for r in results:
1087 if r.has_key('change'):
1088 return r['change']
1089 die("Could not get changelist number for last submit - cannot patch up user details")
1090
1091 def modifyChangelistUser(self, changelist, newUser):
1092 # fixup the user field of a changelist after it has been submitted.
1093 changes = p4CmdList("change -o %s" % changelist)
ecdba36d
LD
1094 if len(changes) != 1:
1095 die("Bad output from p4 change modifying %s to user %s" %
1096 (changelist, newUser))
1097
1098 c = changes[0]
1099 if c['User'] == newUser: return # nothing to do
1100 c['User'] = newUser
1101 input = marshal.dumps(c)
1102
3ea2cfd4
LD
1103 result = p4CmdList("change -f -i", stdin=input)
1104 for r in result:
1105 if r.has_key('code'):
1106 if r['code'] == 'error':
1107 die("Could not modify user field of changelist %s to %s:%s" % (changelist, newUser, r['data']))
1108 if r.has_key('data'):
1109 print("Updated user field for changelist %s to %s" % (changelist, newUser))
1110 return
1111 die("Could not modify user field of changelist %s to %s" % (changelist, newUser))
1112
1113 def canChangeChangelists(self):
1114 # check to see if we have p4 admin or super-user permissions, either of
1115 # which are required to modify changelists.
52a4880b 1116 results = p4CmdList(["protects", self.depotPath])
3ea2cfd4
LD
1117 for r in results:
1118 if r.has_key('perm'):
1119 if r['perm'] == 'admin':
1120 return 1
1121 if r['perm'] == 'super':
1122 return 1
1123 return 0
1124
ea99c3ae 1125 def prepareSubmitTemplate(self):
f19cb0a0
PW
1126 """Run "p4 change -o" to grab a change specification template.
1127 This does not use "p4 -G", as it is nice to keep the submission
1128 template in original order, since a human might edit it.
1129
1130 Remove lines in the Files section that show changes to files
1131 outside the depot path we're committing into."""
1132
ea99c3ae
SH
1133 template = ""
1134 inFilesSection = False
6de040df 1135 for line in p4_read_pipe_lines(['change', '-o']):
f3e5ae4f
MSO
1136 if line.endswith("\r\n"):
1137 line = line[:-2] + "\n"
ea99c3ae
SH
1138 if inFilesSection:
1139 if line.startswith("\t"):
1140 # path starts and ends with a tab
1141 path = line[1:]
1142 lastTab = path.rfind("\t")
1143 if lastTab != -1:
1144 path = path[:lastTab]
d53de8b9 1145 if not p4PathStartsWith(path, self.depotPath):
ea99c3ae
SH
1146 continue
1147 else:
1148 inFilesSection = False
1149 else:
1150 if line.startswith("Files:"):
1151 inFilesSection = True
1152
1153 template += line
1154
1155 return template
1156
7c766e57
PW
1157 def edit_template(self, template_file):
1158 """Invoke the editor to let the user change the submission
1159 message. Return true if okay to continue with the submit."""
1160
1161 # if configured to skip the editing part, just submit
1162 if gitConfig("git-p4.skipSubmitEdit") == "true":
1163 return True
1164
1165 # look at the modification time, to check later if the user saved
1166 # the file
1167 mtime = os.stat(template_file).st_mtime
1168
1169 # invoke the editor
f95ceaf0 1170 if os.environ.has_key("P4EDITOR") and (os.environ.get("P4EDITOR") != ""):
7c766e57
PW
1171 editor = os.environ.get("P4EDITOR")
1172 else:
1173 editor = read_pipe("git var GIT_EDITOR").strip()
1174 system(editor + " " + template_file)
1175
1176 # If the file was not saved, prompt to see if this patch should
1177 # be skipped. But skip this verification step if configured so.
1178 if gitConfig("git-p4.skipSubmitEditCheck") == "true":
1179 return True
1180
d1652049
PW
1181 # modification time updated means user saved the file
1182 if os.stat(template_file).st_mtime > mtime:
1183 return True
1184
1185 while True:
1186 response = raw_input("Submit template unchanged. Submit anyway? [y]es, [n]o (skip this patch) ")
1187 if response == 'y':
1188 return True
1189 if response == 'n':
1190 return False
7c766e57 1191
7cb5cbef 1192 def applyCommit(self, id):
67b0fe2e
PW
1193 """Apply one commit, return True if it succeeded."""
1194
1195 print "Applying", read_pipe(["git", "show", "-s",
1196 "--format=format:%h %s", id])
ae901090 1197
848de9c3 1198 (p4User, gitEmail) = self.p4UserForCommit(id)
3ea2cfd4 1199
84cb0003 1200 diff = read_pipe_lines("git diff-tree -r %s \"%s^\" \"%s\"" % (self.diffOpts, id, id))
4f5cf76a
SH
1201 filesToAdd = set()
1202 filesToDelete = set()
d336c158 1203 editedFiles = set()
b6ad6dcc 1204 pureRenameCopy = set()
c65b670e 1205 filesToChangeExecBit = {}
60df071c 1206
4f5cf76a 1207 for line in diff:
b43b0a3c
CP
1208 diff = parseDiffTreeEntry(line)
1209 modifier = diff['status']
1210 path = diff['src']
4f5cf76a 1211 if modifier == "M":
6de040df 1212 p4_edit(path)
c65b670e
CP
1213 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
1214 filesToChangeExecBit[path] = diff['dst_mode']
d336c158 1215 editedFiles.add(path)
4f5cf76a
SH
1216 elif modifier == "A":
1217 filesToAdd.add(path)
c65b670e 1218 filesToChangeExecBit[path] = diff['dst_mode']
4f5cf76a
SH
1219 if path in filesToDelete:
1220 filesToDelete.remove(path)
1221 elif modifier == "D":
1222 filesToDelete.add(path)
1223 if path in filesToAdd:
1224 filesToAdd.remove(path)
4fddb41b
VA
1225 elif modifier == "C":
1226 src, dest = diff['src'], diff['dst']
6de040df 1227 p4_integrate(src, dest)
b6ad6dcc 1228 pureRenameCopy.add(dest)
4fddb41b 1229 if diff['src_sha1'] != diff['dst_sha1']:
6de040df 1230 p4_edit(dest)
b6ad6dcc 1231 pureRenameCopy.discard(dest)
4fddb41b 1232 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
6de040df 1233 p4_edit(dest)
b6ad6dcc 1234 pureRenameCopy.discard(dest)
4fddb41b 1235 filesToChangeExecBit[dest] = diff['dst_mode']
d20f0f8e
PW
1236 if self.isWindows:
1237 # turn off read-only attribute
1238 os.chmod(dest, stat.S_IWRITE)
4fddb41b
VA
1239 os.unlink(dest)
1240 editedFiles.add(dest)
d9a5f25b 1241 elif modifier == "R":
b43b0a3c 1242 src, dest = diff['src'], diff['dst']
8e9497c2
GG
1243 if self.p4HasMoveCommand:
1244 p4_edit(src) # src must be open before move
1245 p4_move(src, dest) # opens for (move/delete, move/add)
b6ad6dcc 1246 else:
8e9497c2
GG
1247 p4_integrate(src, dest)
1248 if diff['src_sha1'] != diff['dst_sha1']:
1249 p4_edit(dest)
1250 else:
1251 pureRenameCopy.add(dest)
c65b670e 1252 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
8e9497c2
GG
1253 if not self.p4HasMoveCommand:
1254 p4_edit(dest) # with move: already open, writable
c65b670e 1255 filesToChangeExecBit[dest] = diff['dst_mode']
8e9497c2 1256 if not self.p4HasMoveCommand:
d20f0f8e
PW
1257 if self.isWindows:
1258 os.chmod(dest, stat.S_IWRITE)
8e9497c2
GG
1259 os.unlink(dest)
1260 filesToDelete.add(src)
d9a5f25b 1261 editedFiles.add(dest)
4f5cf76a
SH
1262 else:
1263 die("unknown modifier %s for %s" % (modifier, path))
1264
0e36f2d7 1265 diffcmd = "git format-patch -k --stdout \"%s^\"..\"%s\"" % (id, id)
47a130b7 1266 patchcmd = diffcmd + " | git apply "
c1b296b9
SH
1267 tryPatchCmd = patchcmd + "--check -"
1268 applyPatchCmd = patchcmd + "--check --apply -"
60df071c 1269 patch_succeeded = True
51a2640a 1270
47a130b7 1271 if os.system(tryPatchCmd) != 0:
60df071c
LD
1272 fixed_rcs_keywords = False
1273 patch_succeeded = False
51a2640a 1274 print "Unfortunately applying the change failed!"
60df071c
LD
1275
1276 # Patch failed, maybe it's just RCS keyword woes. Look through
1277 # the patch to see if that's possible.
1278 if gitConfig("git-p4.attemptRCSCleanup","--bool") == "true":
1279 file = None
1280 pattern = None
1281 kwfiles = {}
1282 for file in editedFiles | filesToDelete:
1283 # did this file's delta contain RCS keywords?
1284 pattern = p4_keywords_regexp_for_file(file)
1285
1286 if pattern:
1287 # this file is a possibility...look for RCS keywords.
1288 regexp = re.compile(pattern, re.VERBOSE)
1289 for line in read_pipe_lines(["git", "diff", "%s^..%s" % (id, id), file]):
1290 if regexp.search(line):
1291 if verbose:
1292 print "got keyword match on %s in %s in %s" % (pattern, line, file)
1293 kwfiles[file] = pattern
1294 break
1295
1296 for file in kwfiles:
1297 if verbose:
1298 print "zapping %s with %s" % (line,pattern)
d20f0f8e
PW
1299 # File is being deleted, so not open in p4. Must
1300 # disable the read-only bit on windows.
1301 if self.isWindows and file not in editedFiles:
1302 os.chmod(file, stat.S_IWRITE)
60df071c
LD
1303 self.patchRCSKeywords(file, kwfiles[file])
1304 fixed_rcs_keywords = True
1305
1306 if fixed_rcs_keywords:
1307 print "Retrying the patch with RCS keywords cleaned up"
1308 if os.system(tryPatchCmd) == 0:
1309 patch_succeeded = True
1310
1311 if not patch_succeeded:
7e5dd9f2
PW
1312 for f in editedFiles:
1313 p4_revert(f)
7e5dd9f2 1314 return False
51a2640a 1315
55ac2ed6
PW
1316 #
1317 # Apply the patch for real, and do add/delete/+x handling.
1318 #
47a130b7 1319 system(applyPatchCmd)
4f5cf76a
SH
1320
1321 for f in filesToAdd:
6de040df 1322 p4_add(f)
4f5cf76a 1323 for f in filesToDelete:
6de040df
LD
1324 p4_revert(f)
1325 p4_delete(f)
4f5cf76a 1326
c65b670e
CP
1327 # Set/clear executable bits
1328 for f in filesToChangeExecBit.keys():
1329 mode = filesToChangeExecBit[f]
1330 setP4ExecBit(f, mode)
1331
55ac2ed6
PW
1332 #
1333 # Build p4 change description, starting with the contents
1334 # of the git commit message.
1335 #
0e36f2d7 1336 logMessage = extractLogMessageFromGitCommit(id)
0e36f2d7 1337 logMessage = logMessage.strip()
f19cb0a0 1338 (logMessage, jobs) = self.separate_jobs_from_description(logMessage)
4f5cf76a 1339
ea99c3ae 1340 template = self.prepareSubmitTemplate()
f19cb0a0 1341 submitTemplate = self.prepareLogMessage(template, logMessage, jobs)
ecdba36d 1342
c47178d4 1343 if self.preserveUser:
55ac2ed6 1344 submitTemplate += "\n######## Actual user %s, modified after commit\n" % p4User
c47178d4 1345
55ac2ed6
PW
1346 if self.checkAuthorship and not self.p4UserIsMe(p4User):
1347 submitTemplate += "######## git author %s does not match your p4 account.\n" % gitEmail
1348 submitTemplate += "######## Use option --preserve-user to modify authorship.\n"
1349 submitTemplate += "######## Variable git-p4.skipUserNameCheck hides this message.\n"
c47178d4 1350
55ac2ed6
PW
1351 separatorLine = "######## everything below this line is just the diff #######\n"
1352
1353 # diff
c47178d4
PW
1354 if os.environ.has_key("P4DIFF"):
1355 del(os.environ["P4DIFF"])
1356 diff = ""
1357 for editedFile in editedFiles:
1358 diff += p4_read_pipe(['diff', '-du',
1359 wildcard_encode(editedFile)])
1360
55ac2ed6 1361 # new file diff
c47178d4
PW
1362 newdiff = ""
1363 for newFile in filesToAdd:
1364 newdiff += "==== new file ====\n"
1365 newdiff += "--- /dev/null\n"
1366 newdiff += "+++ %s\n" % newFile
1367 f = open(newFile, "r")
1368 for line in f.readlines():
1369 newdiff += "+" + line
1370 f.close()
1371
55ac2ed6 1372 # change description file: submitTemplate, separatorLine, diff, newdiff
c47178d4
PW
1373 (handle, fileName) = tempfile.mkstemp()
1374 tmpFile = os.fdopen(handle, "w+")
1375 if self.isWindows:
1376 submitTemplate = submitTemplate.replace("\n", "\r\n")
1377 separatorLine = separatorLine.replace("\n", "\r\n")
1378 newdiff = newdiff.replace("\n", "\r\n")
1379 tmpFile.write(submitTemplate + separatorLine + diff + newdiff)
1380 tmpFile.close()
1381
728b7ad8
PW
1382 if self.prepare_p4_only:
1383 #
1384 # Leave the p4 tree prepared, and the submit template around
1385 # and let the user decide what to do next
1386 #
1387 print
1388 print "P4 workspace prepared for submission."
1389 print "To submit or revert, go to client workspace"
1390 print " " + self.clientPath
1391 print
1392 print "To submit, use \"p4 submit\" to write a new description,"
1393 print "or \"p4 submit -i %s\" to use the one prepared by" \
1394 " \"git p4\"." % fileName
1395 print "You can delete the file \"%s\" when finished." % fileName
1396
1397 if self.preserveUser and p4User and not self.p4UserIsMe(p4User):
1398 print "To preserve change ownership by user %s, you must\n" \
1399 "do \"p4 change -f <change>\" after submitting and\n" \
1400 "edit the User field."
1401 if pureRenameCopy:
1402 print "After submitting, renamed files must be re-synced."
1403 print "Invoke \"p4 sync -f\" on each of these files:"
1404 for f in pureRenameCopy:
1405 print " " + f
1406
1407 print
1408 print "To revert the changes, use \"p4 revert ...\", and delete"
1409 print "the submit template file \"%s\"" % fileName
1410 if filesToAdd:
1411 print "Since the commit adds new files, they must be deleted:"
1412 for f in filesToAdd:
1413 print " " + f
1414 print
1415 return True
1416
55ac2ed6
PW
1417 #
1418 # Let the user edit the change description, then submit it.
1419 #
c47178d4
PW
1420 if self.edit_template(fileName):
1421 # read the edited message and submit
5a41c16a 1422 ret = True
c47178d4
PW
1423 tmpFile = open(fileName, "rb")
1424 message = tmpFile.read()
e96e400f 1425 tmpFile.close()
c47178d4
PW
1426 submitTemplate = message[:message.index(separatorLine)]
1427 if self.isWindows:
1428 submitTemplate = submitTemplate.replace("\r\n", "\n")
1429 p4_write_pipe(['submit', '-i'], submitTemplate)
e96e400f 1430
c47178d4
PW
1431 if self.preserveUser:
1432 if p4User:
1433 # Get last changelist number. Cannot easily get it from
1434 # the submit command output as the output is
1435 # unmarshalled.
1436 changelist = self.lastP4Changelist()
1437 self.modifyChangelistUser(changelist, p4User)
1438
1439 # The rename/copy happened by applying a patch that created a
1440 # new file. This leaves it writable, which confuses p4.
1441 for f in pureRenameCopy:
1442 p4_sync(f, "-f")
cdc7e388 1443
4f5cf76a 1444 else:
c47178d4 1445 # skip this patch
5a41c16a 1446 ret = False
c47178d4
PW
1447 print "Submission cancelled, undoing p4 changes."
1448 for f in editedFiles:
1449 p4_revert(f)
1450 for f in filesToAdd:
1451 p4_revert(f)
1452 os.remove(f)
df9c5453
PW
1453 for f in filesToDelete:
1454 p4_revert(f)
c47178d4
PW
1455
1456 os.remove(fileName)
5a41c16a 1457 return ret
4f5cf76a 1458
06804c76
LD
1459 # Export git tags as p4 labels. Create a p4 label and then tag
1460 # with that.
1461 def exportGitTags(self, gitTags):
c8942a22
LD
1462 validLabelRegexp = gitConfig("git-p4.labelExportRegexp")
1463 if len(validLabelRegexp) == 0:
1464 validLabelRegexp = defaultLabelRegexp
1465 m = re.compile(validLabelRegexp)
06804c76
LD
1466
1467 for name in gitTags:
1468
1469 if not m.match(name):
1470 if verbose:
05a3cec5 1471 print "tag %s does not match regexp %s" % (name, validLabelRegexp)
06804c76
LD
1472 continue
1473
1474 # Get the p4 commit this corresponds to
c8942a22
LD
1475 logMessage = extractLogMessageFromGitCommit(name)
1476 values = extractSettingsGitLog(logMessage)
06804c76 1477
c8942a22 1478 if not values.has_key('change'):
06804c76
LD
1479 # a tag pointing to something not sent to p4; ignore
1480 if verbose:
1481 print "git tag %s does not give a p4 commit" % name
1482 continue
c8942a22
LD
1483 else:
1484 changelist = values['change']
06804c76
LD
1485
1486 # Get the tag details.
1487 inHeader = True
1488 isAnnotated = False
1489 body = []
1490 for l in read_pipe_lines(["git", "cat-file", "-p", name]):
1491 l = l.strip()
1492 if inHeader:
1493 if re.match(r'tag\s+', l):
1494 isAnnotated = True
1495 elif re.match(r'\s*$', l):
1496 inHeader = False
1497 continue
1498 else:
1499 body.append(l)
1500
1501 if not isAnnotated:
1502 body = ["lightweight tag imported by git p4\n"]
1503
1504 # Create the label - use the same view as the client spec we are using
1505 clientSpec = getClientSpec()
1506
1507 labelTemplate = "Label: %s\n" % name
1508 labelTemplate += "Description:\n"
1509 for b in body:
1510 labelTemplate += "\t" + b + "\n"
1511 labelTemplate += "View:\n"
1512 for mapping in clientSpec.mappings:
1513 labelTemplate += "\t%s\n" % mapping.depot_side.path
1514
ef739f08
PW
1515 if self.dry_run:
1516 print "Would create p4 label %s for tag" % name
728b7ad8
PW
1517 elif self.prepare_p4_only:
1518 print "Not creating p4 label %s for tag due to option" \
1519 " --prepare-p4-only" % name
ef739f08
PW
1520 else:
1521 p4_write_pipe(["label", "-i"], labelTemplate)
06804c76 1522
ef739f08
PW
1523 # Use the label
1524 p4_system(["tag", "-l", name] +
1525 ["%s@%s" % (mapping.depot_side.path, changelist) for mapping in clientSpec.mappings])
06804c76 1526
ef739f08
PW
1527 if verbose:
1528 print "created p4 label for tag %s" % name
06804c76 1529
4f5cf76a 1530 def run(self, args):
c9b50e63
SH
1531 if len(args) == 0:
1532 self.master = currentGitBranch()
4280e533 1533 if len(self.master) == 0 or not gitBranchExists("refs/heads/%s" % self.master):
c9b50e63
SH
1534 die("Detecting current git branch failed!")
1535 elif len(args) == 1:
1536 self.master = args[0]
28755dba
PW
1537 if not branchExists(self.master):
1538 die("Branch %s does not exist" % self.master)
c9b50e63
SH
1539 else:
1540 return False
1541
4c2d5d72
JX
1542 allowSubmit = gitConfig("git-p4.allowSubmit")
1543 if len(allowSubmit) > 0 and not self.master in allowSubmit.split(","):
1544 die("%s is not in git-p4.allowSubmit" % self.master)
1545
27d2d811 1546 [upstream, settings] = findUpstreamBranchPoint()
ea99c3ae 1547 self.depotPath = settings['depot-paths'][0]
27d2d811
SH
1548 if len(self.origin) == 0:
1549 self.origin = upstream
a3fdd579 1550
3ea2cfd4
LD
1551 if self.preserveUser:
1552 if not self.canChangeChangelists():
1553 die("Cannot preserve user names without p4 super-user or admin permissions")
1554
6bbfd137
PW
1555 # if not set from the command line, try the config file
1556 if self.conflict_behavior is None:
1557 val = gitConfig("git-p4.conflict")
1558 if val:
1559 if val not in self.conflict_behavior_choices:
1560 die("Invalid value '%s' for config git-p4.conflict" % val)
1561 else:
1562 val = "ask"
1563 self.conflict_behavior = val
1564
a3fdd579
SH
1565 if self.verbose:
1566 print "Origin branch is " + self.origin
9512497b 1567
ea99c3ae 1568 if len(self.depotPath) == 0:
9512497b
SH
1569 print "Internal error: cannot locate perforce depot path from existing branches"
1570 sys.exit(128)
1571
543987bd
PW
1572 self.useClientSpec = False
1573 if gitConfig("git-p4.useclientspec", "--bool") == "true":
1574 self.useClientSpec = True
1575 if self.useClientSpec:
1576 self.clientSpecDirs = getClientSpec()
9512497b 1577
543987bd
PW
1578 if self.useClientSpec:
1579 # all files are relative to the client spec
1580 self.clientPath = getClientRoot()
1581 else:
1582 self.clientPath = p4Where(self.depotPath)
9512497b 1583
543987bd
PW
1584 if self.clientPath == "":
1585 die("Error: Cannot locate perforce checkout of %s in client view" % self.depotPath)
9512497b 1586
ea99c3ae 1587 print "Perforce checkout for depot path %s located at %s" % (self.depotPath, self.clientPath)
7944f142 1588 self.oldWorkingDirectory = os.getcwd()
c1b296b9 1589
0591cfa8 1590 # ensure the clientPath exists
8d7ec362 1591 new_client_dir = False
0591cfa8 1592 if not os.path.exists(self.clientPath):
8d7ec362 1593 new_client_dir = True
0591cfa8
GG
1594 os.makedirs(self.clientPath)
1595
053fd0c1 1596 chdir(self.clientPath)
ef739f08
PW
1597 if self.dry_run:
1598 print "Would synchronize p4 checkout in %s" % self.clientPath
8d7ec362 1599 else:
ef739f08
PW
1600 print "Synchronizing p4 checkout..."
1601 if new_client_dir:
1602 # old one was destroyed, and maybe nobody told p4
1603 p4_sync("...", "-f")
1604 else:
1605 p4_sync("...")
4f5cf76a 1606 self.check()
4f5cf76a 1607
4c750c0d
SH
1608 commits = []
1609 for line in read_pipe_lines("git rev-list --no-merges %s..%s" % (self.origin, self.master)):
1610 commits.append(line.strip())
1611 commits.reverse()
4f5cf76a 1612
848de9c3
LD
1613 if self.preserveUser or (gitConfig("git-p4.skipUserNameCheck") == "true"):
1614 self.checkAuthorship = False
1615 else:
1616 self.checkAuthorship = True
1617
3ea2cfd4
LD
1618 if self.preserveUser:
1619 self.checkValidP4Users(commits)
1620
84cb0003
GG
1621 #
1622 # Build up a set of options to be passed to diff when
1623 # submitting each commit to p4.
1624 #
1625 if self.detectRenames:
1626 # command-line -M arg
1627 self.diffOpts = "-M"
1628 else:
1629 # If not explicitly set check the config variable
1630 detectRenames = gitConfig("git-p4.detectRenames")
1631
1632 if detectRenames.lower() == "false" or detectRenames == "":
1633 self.diffOpts = ""
1634 elif detectRenames.lower() == "true":
1635 self.diffOpts = "-M"
1636 else:
1637 self.diffOpts = "-M%s" % detectRenames
1638
1639 # no command-line arg for -C or --find-copies-harder, just
1640 # config variables
1641 detectCopies = gitConfig("git-p4.detectCopies")
1642 if detectCopies.lower() == "false" or detectCopies == "":
1643 pass
1644 elif detectCopies.lower() == "true":
1645 self.diffOpts += " -C"
1646 else:
1647 self.diffOpts += " -C%s" % detectCopies
1648
1649 if gitConfig("git-p4.detectCopiesHarder", "--bool") == "true":
1650 self.diffOpts += " --find-copies-harder"
1651
7e5dd9f2
PW
1652 #
1653 # Apply the commits, one at a time. On failure, ask if should
1654 # continue to try the rest of the patches, or quit.
1655 #
ef739f08
PW
1656 if self.dry_run:
1657 print "Would apply"
67b0fe2e 1658 applied = []
7e5dd9f2
PW
1659 last = len(commits) - 1
1660 for i, commit in enumerate(commits):
ef739f08
PW
1661 if self.dry_run:
1662 print " ", read_pipe(["git", "show", "-s",
1663 "--format=format:%h %s", commit])
1664 ok = True
1665 else:
1666 ok = self.applyCommit(commit)
67b0fe2e
PW
1667 if ok:
1668 applied.append(commit)
7e5dd9f2 1669 else:
728b7ad8
PW
1670 if self.prepare_p4_only and i < last:
1671 print "Processing only the first commit due to option" \
1672 " --prepare-p4-only"
1673 break
7e5dd9f2
PW
1674 if i < last:
1675 quit = False
1676 while True:
6bbfd137
PW
1677 # prompt for what to do, or use the option/variable
1678 if self.conflict_behavior == "ask":
1679 print "What do you want to do?"
1680 response = raw_input("[s]kip this commit but apply"
1681 " the rest, or [q]uit? ")
1682 if not response:
1683 continue
1684 elif self.conflict_behavior == "skip":
1685 response = "s"
1686 elif self.conflict_behavior == "quit":
1687 response = "q"
1688 else:
1689 die("Unknown conflict_behavior '%s'" %
1690 self.conflict_behavior)
1691
7e5dd9f2
PW
1692 if response[0] == "s":
1693 print "Skipping this commit, but applying the rest"
1694 break
1695 if response[0] == "q":
1696 print "Quitting"
1697 quit = True
1698 break
1699 if quit:
1700 break
4f5cf76a 1701
67b0fe2e 1702 chdir(self.oldWorkingDirectory)
4f5cf76a 1703
ef739f08
PW
1704 if self.dry_run:
1705 pass
728b7ad8
PW
1706 elif self.prepare_p4_only:
1707 pass
ef739f08 1708 elif len(commits) == len(applied):
67b0fe2e 1709 print "All commits applied!"
14594f4b 1710
4c750c0d 1711 sync = P4Sync()
44e8d26c
PW
1712 if self.branch:
1713 sync.branch = self.branch
4c750c0d 1714 sync.run([])
14594f4b 1715
4c750c0d
SH
1716 rebase = P4Rebase()
1717 rebase.rebase()
4f5cf76a 1718
67b0fe2e
PW
1719 else:
1720 if len(applied) == 0:
1721 print "No commits applied."
1722 else:
1723 print "Applied only the commits marked with '*':"
1724 for c in commits:
1725 if c in applied:
1726 star = "*"
1727 else:
1728 star = " "
1729 print star, read_pipe(["git", "show", "-s",
1730 "--format=format:%h %s", c])
1731 print "You will have to do 'git p4 sync' and rebase."
1732
06804c76 1733 if gitConfig("git-p4.exportLabels", "--bool") == "true":
06dcd152 1734 self.exportLabels = True
06804c76
LD
1735
1736 if self.exportLabels:
1737 p4Labels = getP4Labels(self.depotPath)
1738 gitTags = getGitTags()
1739
1740 missingGitTags = gitTags - p4Labels
1741 self.exportGitTags(missingGitTags)
1742
67b0fe2e
PW
1743 # exit with error unless everything applied perfecly
1744 if len(commits) != len(applied):
1745 sys.exit(1)
1746
b984733c
SH
1747 return True
1748
ecb7cf98
PW
1749class View(object):
1750 """Represent a p4 view ("p4 help views"), and map files in a
1751 repo according to the view."""
1752
1753 class Path(object):
1754 """A depot or client path, possibly containing wildcards.
1755 The only one supported is ... at the end, currently.
1756 Initialize with the full path, with //depot or //client."""
1757
1758 def __init__(self, path, is_depot):
1759 self.path = path
1760 self.is_depot = is_depot
1761 self.find_wildcards()
1762 # remember the prefix bit, useful for relative mappings
1763 m = re.match("(//[^/]+/)", self.path)
1764 if not m:
1765 die("Path %s does not start with //prefix/" % self.path)
1766 prefix = m.group(1)
1767 if not self.is_depot:
1768 # strip //client/ on client paths
1769 self.path = self.path[len(prefix):]
1770
1771 def find_wildcards(self):
1772 """Make sure wildcards are valid, and set up internal
1773 variables."""
1774
1775 self.ends_triple_dot = False
1776 # There are three wildcards allowed in p4 views
1777 # (see "p4 help views"). This code knows how to
1778 # handle "..." (only at the end), but cannot deal with
1779 # "%%n" or "*". Only check the depot_side, as p4 should
1780 # validate that the client_side matches too.
1781 if re.search(r'%%[1-9]', self.path):
1782 die("Can't handle %%n wildcards in view: %s" % self.path)
1783 if self.path.find("*") >= 0:
1784 die("Can't handle * wildcards in view: %s" % self.path)
1785 triple_dot_index = self.path.find("...")
1786 if triple_dot_index >= 0:
43b82bd9
PW
1787 if triple_dot_index != len(self.path) - 3:
1788 die("Can handle only single ... wildcard, at end: %s" %
ecb7cf98
PW
1789 self.path)
1790 self.ends_triple_dot = True
1791
1792 def ensure_compatible(self, other_path):
1793 """Make sure the wildcards agree."""
1794 if self.ends_triple_dot != other_path.ends_triple_dot:
1795 die("Both paths must end with ... if either does;\n" +
1796 "paths: %s %s" % (self.path, other_path.path))
1797
1798 def match_wildcards(self, test_path):
1799 """See if this test_path matches us, and fill in the value
1800 of the wildcards if so. Returns a tuple of
1801 (True|False, wildcards[]). For now, only the ... at end
1802 is supported, so at most one wildcard."""
1803 if self.ends_triple_dot:
1804 dotless = self.path[:-3]
1805 if test_path.startswith(dotless):
1806 wildcard = test_path[len(dotless):]
1807 return (True, [ wildcard ])
1808 else:
1809 if test_path == self.path:
1810 return (True, [])
1811 return (False, [])
1812
1813 def match(self, test_path):
1814 """Just return if it matches; don't bother with the wildcards."""
1815 b, _ = self.match_wildcards(test_path)
1816 return b
1817
1818 def fill_in_wildcards(self, wildcards):
1819 """Return the relative path, with the wildcards filled in
1820 if there are any."""
1821 if self.ends_triple_dot:
1822 return self.path[:-3] + wildcards[0]
1823 else:
1824 return self.path
1825
1826 class Mapping(object):
1827 def __init__(self, depot_side, client_side, overlay, exclude):
1828 # depot_side is without the trailing /... if it had one
1829 self.depot_side = View.Path(depot_side, is_depot=True)
1830 self.client_side = View.Path(client_side, is_depot=False)
1831 self.overlay = overlay # started with "+"
1832 self.exclude = exclude # started with "-"
1833 assert not (self.overlay and self.exclude)
1834 self.depot_side.ensure_compatible(self.client_side)
1835
1836 def __str__(self):
1837 c = " "
1838 if self.overlay:
1839 c = "+"
1840 if self.exclude:
1841 c = "-"
1842 return "View.Mapping: %s%s -> %s" % \
329afb8e 1843 (c, self.depot_side.path, self.client_side.path)
ecb7cf98
PW
1844
1845 def map_depot_to_client(self, depot_path):
1846 """Calculate the client path if using this mapping on the
1847 given depot path; does not consider the effect of other
1848 mappings in a view. Even excluded mappings are returned."""
1849 matches, wildcards = self.depot_side.match_wildcards(depot_path)
1850 if not matches:
1851 return ""
1852 client_path = self.client_side.fill_in_wildcards(wildcards)
1853 return client_path
1854
1855 #
1856 # View methods
1857 #
1858 def __init__(self):
1859 self.mappings = []
1860
1861 def append(self, view_line):
1862 """Parse a view line, splitting it into depot and client
1863 sides. Append to self.mappings, preserving order."""
1864
1865 # Split the view line into exactly two words. P4 enforces
1866 # structure on these lines that simplifies this quite a bit.
1867 #
1868 # Either or both words may be double-quoted.
1869 # Single quotes do not matter.
1870 # Double-quote marks cannot occur inside the words.
1871 # A + or - prefix is also inside the quotes.
1872 # There are no quotes unless they contain a space.
1873 # The line is already white-space stripped.
1874 # The two words are separated by a single space.
1875 #
1876 if view_line[0] == '"':
1877 # First word is double quoted. Find its end.
1878 close_quote_index = view_line.find('"', 1)
1879 if close_quote_index <= 0:
1880 die("No first-word closing quote found: %s" % view_line)
1881 depot_side = view_line[1:close_quote_index]
1882 # skip closing quote and space
1883 rhs_index = close_quote_index + 1 + 1
1884 else:
1885 space_index = view_line.find(" ")
1886 if space_index <= 0:
1887 die("No word-splitting space found: %s" % view_line)
1888 depot_side = view_line[0:space_index]
1889 rhs_index = space_index + 1
1890
1891 if view_line[rhs_index] == '"':
1892 # Second word is double quoted. Make sure there is a
1893 # double quote at the end too.
1894 if not view_line.endswith('"'):
1895 die("View line with rhs quote should end with one: %s" %
1896 view_line)
1897 # skip the quotes
1898 client_side = view_line[rhs_index+1:-1]
1899 else:
1900 client_side = view_line[rhs_index:]
1901
1902 # prefix + means overlay on previous mapping
1903 overlay = False
1904 if depot_side.startswith("+"):
1905 overlay = True
1906 depot_side = depot_side[1:]
1907
1908 # prefix - means exclude this path
1909 exclude = False
1910 if depot_side.startswith("-"):
1911 exclude = True
1912 depot_side = depot_side[1:]
1913
1914 m = View.Mapping(depot_side, client_side, overlay, exclude)
1915 self.mappings.append(m)
1916
1917 def map_in_client(self, depot_path):
1918 """Return the relative location in the client where this
1919 depot file should live. Returns "" if the file should
1920 not be mapped in the client."""
1921
1922 paths_filled = []
1923 client_path = ""
1924
1925 # look at later entries first
1926 for m in self.mappings[::-1]:
1927
1928 # see where will this path end up in the client
1929 p = m.map_depot_to_client(depot_path)
1930
1931 if p == "":
1932 # Depot path does not belong in client. Must remember
1933 # this, as previous items should not cause files to
1934 # exist in this path either. Remember that the list is
1935 # being walked from the end, which has higher precedence.
1936 # Overlap mappings do not exclude previous mappings.
1937 if not m.overlay:
1938 paths_filled.append(m.client_side)
1939
1940 else:
1941 # This mapping matched; no need to search any further.
1942 # But, the mapping could be rejected if the client path
6ee9a999
PW
1943 # has already been claimed by an earlier mapping (i.e.
1944 # one later in the list, which we are walking backwards).
ecb7cf98
PW
1945 already_mapped_in_client = False
1946 for f in paths_filled:
1947 # this is View.Path.match
1948 if f.match(p):
1949 already_mapped_in_client = True
1950 break
1951 if not already_mapped_in_client:
1952 # Include this file, unless it is from a line that
1953 # explicitly said to exclude it.
1954 if not m.exclude:
1955 client_path = p
1956
1957 # a match, even if rejected, always stops the search
1958 break
1959
1960 return client_path
1961
3ea2cfd4 1962class P4Sync(Command, P4UserMap):
56c09345
PW
1963 delete_actions = ( "delete", "move/delete", "purge" )
1964
b984733c
SH
1965 def __init__(self):
1966 Command.__init__(self)
3ea2cfd4 1967 P4UserMap.__init__(self)
b984733c
SH
1968 self.options = [
1969 optparse.make_option("--branch", dest="branch"),
1970 optparse.make_option("--detect-branches", dest="detectBranches", action="store_true"),
1971 optparse.make_option("--changesfile", dest="changesFile"),
1972 optparse.make_option("--silent", dest="silent", action="store_true"),
ef48f909 1973 optparse.make_option("--detect-labels", dest="detectLabels", action="store_true"),
06804c76 1974 optparse.make_option("--import-labels", dest="importLabels", action="store_true"),
d2c6dd30
HWN
1975 optparse.make_option("--import-local", dest="importIntoRemotes", action="store_false",
1976 help="Import into refs/heads/ , not refs/remotes"),
8b41a97f 1977 optparse.make_option("--max-changes", dest="maxChanges"),
86dff6b6 1978 optparse.make_option("--keep-path", dest="keepRepoPath", action='store_true',
3a70cdfa
TAL
1979 help="Keep entire BRANCH/DIR/SUBDIR prefix during import"),
1980 optparse.make_option("--use-client-spec", dest="useClientSpec", action='store_true',
1981 help="Only sync files that are included in the Perforce Client Spec")
b984733c
SH
1982 ]
1983 self.description = """Imports from Perforce into a git repository.\n
1984 example:
1985 //depot/my/project/ -- to import the current head
1986 //depot/my/project/@all -- to import everything
1987 //depot/my/project/@1,6 -- to import only from revision 1 to 6
1988
1989 (a ... is not needed in the path p4 specification, it's added implicitly)"""
1990
1991 self.usage += " //depot/path[@revRange]"
b984733c 1992 self.silent = False
1d7367dc
RG
1993 self.createdBranches = set()
1994 self.committedChanges = set()
569d1bd4 1995 self.branch = ""
b984733c 1996 self.detectBranches = False
cb53e1f8 1997 self.detectLabels = False
06804c76 1998 self.importLabels = False
b984733c 1999 self.changesFile = ""
01265103 2000 self.syncWithOrigin = True
a028a98e 2001 self.importIntoRemotes = True
01a9c9c5 2002 self.maxChanges = ""
8b41a97f 2003 self.keepRepoPath = False
6326aa58 2004 self.depotPaths = None
3c699645 2005 self.p4BranchesInGit = []
354081d5 2006 self.cloneExclude = []
3a70cdfa 2007 self.useClientSpec = False
a93d33ee 2008 self.useClientSpec_from_options = False
ecb7cf98 2009 self.clientSpecDirs = None
fed23693
VA
2010 self.tempBranches = []
2011 self.tempBranchLocation = "git-p4-tmp"
b984733c 2012
01265103
SH
2013 if gitConfig("git-p4.syncFromOrigin") == "false":
2014 self.syncWithOrigin = False
2015
fed23693
VA
2016 # Force a checkpoint in fast-import and wait for it to finish
2017 def checkpoint(self):
2018 self.gitStream.write("checkpoint\n\n")
2019 self.gitStream.write("progress checkpoint\n\n")
2020 out = self.gitOutput.readline()
2021 if self.verbose:
2022 print "checkpoint finished: " + out
2023
b984733c 2024 def extractFilesFromCommit(self, commit):
354081d5
TT
2025 self.cloneExclude = [re.sub(r"\.\.\.$", "", path)
2026 for path in self.cloneExclude]
b984733c
SH
2027 files = []
2028 fnum = 0
2029 while commit.has_key("depotFile%s" % fnum):
2030 path = commit["depotFile%s" % fnum]
6326aa58 2031
354081d5 2032 if [p for p in self.cloneExclude
d53de8b9 2033 if p4PathStartsWith(path, p)]:
354081d5
TT
2034 found = False
2035 else:
2036 found = [p for p in self.depotPaths
d53de8b9 2037 if p4PathStartsWith(path, p)]
6326aa58 2038 if not found:
b984733c
SH
2039 fnum = fnum + 1
2040 continue
2041
2042 file = {}
2043 file["path"] = path
2044 file["rev"] = commit["rev%s" % fnum]
2045 file["action"] = commit["action%s" % fnum]
2046 file["type"] = commit["type%s" % fnum]
2047 files.append(file)
2048 fnum = fnum + 1
2049 return files
2050
6326aa58 2051 def stripRepoPath(self, path, prefixes):
21ef5df4
PW
2052 """When streaming files, this is called to map a p4 depot path
2053 to where it should go in git. The prefixes are either
2054 self.depotPaths, or self.branchPrefixes in the case of
2055 branch detection."""
2056
3952710b 2057 if self.useClientSpec:
21ef5df4
PW
2058 # branch detection moves files up a level (the branch name)
2059 # from what client spec interpretation gives
0d1696ef 2060 path = self.clientSpecDirs.map_in_client(path)
21ef5df4
PW
2061 if self.detectBranches:
2062 for b in self.knownBranches:
2063 if path.startswith(b + "/"):
2064 path = path[len(b)+1:]
2065
2066 elif self.keepRepoPath:
2067 # Preserve everything in relative path name except leading
2068 # //depot/; just look at first prefix as they all should
2069 # be in the same depot.
2070 depot = re.sub("^(//[^/]+/).*", r'\1', prefixes[0])
2071 if p4PathStartsWith(path, depot):
2072 path = path[len(depot):]
3952710b 2073
0d1696ef 2074 else:
0d1696ef
PW
2075 for p in prefixes:
2076 if p4PathStartsWith(path, p):
2077 path = path[len(p):]
21ef5df4 2078 break
8b41a97f 2079
0d1696ef 2080 path = wildcard_decode(path)
6326aa58 2081 return path
6754a299 2082
71b112d4 2083 def splitFilesIntoBranches(self, commit):
21ef5df4
PW
2084 """Look at each depotFile in the commit to figure out to what
2085 branch it belongs."""
2086
d5904674 2087 branches = {}
71b112d4
SH
2088 fnum = 0
2089 while commit.has_key("depotFile%s" % fnum):
2090 path = commit["depotFile%s" % fnum]
6326aa58 2091 found = [p for p in self.depotPaths
d53de8b9 2092 if p4PathStartsWith(path, p)]
6326aa58 2093 if not found:
71b112d4
SH
2094 fnum = fnum + 1
2095 continue
2096
2097 file = {}
2098 file["path"] = path
2099 file["rev"] = commit["rev%s" % fnum]
2100 file["action"] = commit["action%s" % fnum]
2101 file["type"] = commit["type%s" % fnum]
2102 fnum = fnum + 1
2103
21ef5df4
PW
2104 # start with the full relative path where this file would
2105 # go in a p4 client
2106 if self.useClientSpec:
2107 relPath = self.clientSpecDirs.map_in_client(path)
2108 else:
2109 relPath = self.stripRepoPath(path, self.depotPaths)
b984733c 2110
4b97ffb1 2111 for branch in self.knownBranches.keys():
21ef5df4
PW
2112 # add a trailing slash so that a commit into qt/4.2foo
2113 # doesn't end up in qt/4.2, e.g.
6754a299 2114 if relPath.startswith(branch + "/"):
d5904674
SH
2115 if branch not in branches:
2116 branches[branch] = []
71b112d4 2117 branches[branch].append(file)
6555b2cc 2118 break
b984733c
SH
2119
2120 return branches
2121
b932705b
LD
2122 # output one file from the P4 stream
2123 # - helper for streamP4Files
2124
2125 def streamOneP4File(self, file, contents):
b932705b
LD
2126 relPath = self.stripRepoPath(file['depotFile'], self.branchPrefixes)
2127 if verbose:
2128 sys.stderr.write("%s\n" % relPath)
2129
9cffb8c8
PW
2130 (type_base, type_mods) = split_p4_type(file["type"])
2131
2132 git_mode = "100644"
2133 if "x" in type_mods:
2134 git_mode = "100755"
2135 if type_base == "symlink":
2136 git_mode = "120000"
2137 # p4 print on a symlink contains "target\n"; remove the newline
b39c3612
EP
2138 data = ''.join(contents)
2139 contents = [data[:-1]]
b932705b 2140
9cffb8c8 2141 if type_base == "utf16":
55aa5714
PW
2142 # p4 delivers different text in the python output to -G
2143 # than it does when using "print -o", or normal p4 client
2144 # operations. utf16 is converted to ascii or utf8, perhaps.
2145 # But ascii text saved as -t utf16 is completely mangled.
2146 # Invoke print -o to get the real contents.
7f0e5962
PW
2147 #
2148 # On windows, the newlines will always be mangled by print, so put
2149 # them back too. This is not needed to the cygwin windows version,
2150 # just the native "NT" type.
2151 #
6de040df 2152 text = p4_read_pipe(['print', '-q', '-o', '-', file['depotFile']])
7f0e5962
PW
2153 if p4_version_string().find("/NT") >= 0:
2154 text = text.replace("\r\n", "\n")
55aa5714
PW
2155 contents = [ text ]
2156
9f7ef0ea
PW
2157 if type_base == "apple":
2158 # Apple filetype files will be streamed as a concatenation of
2159 # its appledouble header and the contents. This is useless
2160 # on both macs and non-macs. If using "print -q -o xx", it
2161 # will create "xx" with the data, and "%xx" with the header.
2162 # This is also not very useful.
2163 #
2164 # Ideally, someday, this script can learn how to generate
2165 # appledouble files directly and import those to git, but
2166 # non-mac machines can never find a use for apple filetype.
2167 print "\nIgnoring apple filetype file %s" % file['depotFile']
2168 return
2169
55aa5714
PW
2170 # Note that we do not try to de-mangle keywords on utf16 files,
2171 # even though in theory somebody may want that.
60df071c
LD
2172 pattern = p4_keywords_regexp_for_type(type_base, type_mods)
2173 if pattern:
2174 regexp = re.compile(pattern, re.VERBOSE)
2175 text = ''.join(contents)
2176 text = regexp.sub(r'$\1$', text)
2177 contents = [ text ]
b932705b 2178
9cffb8c8 2179 self.gitStream.write("M %s inline %s\n" % (git_mode, relPath))
b932705b
LD
2180
2181 # total length...
2182 length = 0
2183 for d in contents:
2184 length = length + len(d)
2185
2186 self.gitStream.write("data %d\n" % length)
2187 for d in contents:
2188 self.gitStream.write(d)
2189 self.gitStream.write("\n")
2190
2191 def streamOneP4Deletion(self, file):
2192 relPath = self.stripRepoPath(file['path'], self.branchPrefixes)
2193 if verbose:
2194 sys.stderr.write("delete %s\n" % relPath)
2195 self.gitStream.write("D %s\n" % relPath)
2196
2197 # handle another chunk of streaming data
2198 def streamP4FilesCb(self, marshalled):
2199
78189bea
PW
2200 # catch p4 errors and complain
2201 err = None
2202 if "code" in marshalled:
2203 if marshalled["code"] == "error":
2204 if "data" in marshalled:
2205 err = marshalled["data"].rstrip()
2206 if err:
2207 f = None
2208 if self.stream_have_file_info:
2209 if "depotFile" in self.stream_file:
2210 f = self.stream_file["depotFile"]
2211 # force a failure in fast-import, else an empty
2212 # commit will be made
2213 self.gitStream.write("\n")
2214 self.gitStream.write("die-now\n")
2215 self.gitStream.close()
2216 # ignore errors, but make sure it exits first
2217 self.importProcess.wait()
2218 if f:
2219 die("Error from p4 print for %s: %s" % (f, err))
2220 else:
2221 die("Error from p4 print: %s" % err)
2222
c3f6163b
AG
2223 if marshalled.has_key('depotFile') and self.stream_have_file_info:
2224 # start of a new file - output the old one first
2225 self.streamOneP4File(self.stream_file, self.stream_contents)
2226 self.stream_file = {}
2227 self.stream_contents = []
2228 self.stream_have_file_info = False
b932705b 2229
c3f6163b
AG
2230 # pick up the new file information... for the
2231 # 'data' field we need to append to our array
2232 for k in marshalled.keys():
2233 if k == 'data':
2234 self.stream_contents.append(marshalled['data'])
2235 else:
2236 self.stream_file[k] = marshalled[k]
b932705b 2237
c3f6163b 2238 self.stream_have_file_info = True
b932705b
LD
2239
2240 # Stream directly from "p4 files" into "git fast-import"
2241 def streamP4Files(self, files):
30b5940b
SH
2242 filesForCommit = []
2243 filesToRead = []
b932705b 2244 filesToDelete = []
30b5940b 2245
3a70cdfa 2246 for f in files:
ecb7cf98
PW
2247 # if using a client spec, only add the files that have
2248 # a path in the client
2249 if self.clientSpecDirs:
2250 if self.clientSpecDirs.map_in_client(f['path']) == "":
2251 continue
3a70cdfa 2252
ecb7cf98
PW
2253 filesForCommit.append(f)
2254 if f['action'] in self.delete_actions:
2255 filesToDelete.append(f)
2256 else:
2257 filesToRead.append(f)
6a49f8e2 2258
b932705b
LD
2259 # deleted files...
2260 for f in filesToDelete:
2261 self.streamOneP4Deletion(f)
1b9a4684 2262
b932705b
LD
2263 if len(filesToRead) > 0:
2264 self.stream_file = {}
2265 self.stream_contents = []
2266 self.stream_have_file_info = False
8ff45f2a 2267
c3f6163b
AG
2268 # curry self argument
2269 def streamP4FilesCbSelf(entry):
2270 self.streamP4FilesCb(entry)
6a49f8e2 2271
6de040df
LD
2272 fileArgs = ['%s#%s' % (f['path'], f['rev']) for f in filesToRead]
2273
2274 p4CmdList(["-x", "-", "print"],
2275 stdin=fileArgs,
2276 cb=streamP4FilesCbSelf)
30b5940b 2277
b932705b
LD
2278 # do the last chunk
2279 if self.stream_file.has_key('depotFile'):
2280 self.streamOneP4File(self.stream_file, self.stream_contents)
6a49f8e2 2281
affb474f
LD
2282 def make_email(self, userid):
2283 if userid in self.users:
2284 return self.users[userid]
2285 else:
2286 return "%s <a@b>" % userid
2287
06804c76
LD
2288 # Stream a p4 tag
2289 def streamTag(self, gitStream, labelName, labelDetails, commit, epoch):
2290 if verbose:
2291 print "writing tag %s for commit %s" % (labelName, commit)
2292 gitStream.write("tag %s\n" % labelName)
2293 gitStream.write("from %s\n" % commit)
2294
2295 if labelDetails.has_key('Owner'):
2296 owner = labelDetails["Owner"]
2297 else:
2298 owner = None
2299
2300 # Try to use the owner of the p4 label, or failing that,
2301 # the current p4 user id.
2302 if owner:
2303 email = self.make_email(owner)
2304 else:
2305 email = self.make_email(self.p4UserId())
2306 tagger = "%s %s %s" % (email, epoch, self.tz)
2307
2308 gitStream.write("tagger %s\n" % tagger)
2309
2310 print "labelDetails=",labelDetails
2311 if labelDetails.has_key('Description'):
2312 description = labelDetails['Description']
2313 else:
2314 description = 'Label from git p4'
2315
2316 gitStream.write("data %d\n" % len(description))
2317 gitStream.write(description)
2318 gitStream.write("\n")
2319
e63231e5 2320 def commit(self, details, files, branch, parent = ""):
b984733c
SH
2321 epoch = details["time"]
2322 author = details["user"]
2323
4b97ffb1
SH
2324 if self.verbose:
2325 print "commit into %s" % branch
2326
96e07dd2
HWN
2327 # start with reading files; if that fails, we should not
2328 # create a commit.
2329 new_files = []
2330 for f in files:
e63231e5 2331 if [p for p in self.branchPrefixes if p4PathStartsWith(f['path'], p)]:
96e07dd2
HWN
2332 new_files.append (f)
2333 else:
afa1dd9a 2334 sys.stderr.write("Ignoring file outside of prefix: %s\n" % f['path'])
96e07dd2 2335
b984733c 2336 self.gitStream.write("commit %s\n" % branch)
6a49f8e2 2337# gitStream.write("mark :%s\n" % details["change"])
b984733c
SH
2338 self.committedChanges.add(int(details["change"]))
2339 committer = ""
b607e71e
SH
2340 if author not in self.users:
2341 self.getUserMapFromPerforceServer()
affb474f 2342 committer = "%s %s %s" % (self.make_email(author), epoch, self.tz)
b984733c
SH
2343
2344 self.gitStream.write("committer %s\n" % committer)
2345
2346 self.gitStream.write("data <<EOT\n")
2347 self.gitStream.write(details["desc"])
e63231e5
PW
2348 self.gitStream.write("\n[git-p4: depot-paths = \"%s\": change = %s" %
2349 (','.join(self.branchPrefixes), details["change"]))
6581de09
SH
2350 if len(details['options']) > 0:
2351 self.gitStream.write(": options = %s" % details['options'])
2352 self.gitStream.write("]\nEOT\n\n")
b984733c
SH
2353
2354 if len(parent) > 0:
4b97ffb1
SH
2355 if self.verbose:
2356 print "parent %s" % parent
b984733c
SH
2357 self.gitStream.write("from %s\n" % parent)
2358
b932705b 2359 self.streamP4Files(new_files)
b984733c
SH
2360 self.gitStream.write("\n")
2361
1f4ba1cb
SH
2362 change = int(details["change"])
2363
9bda3a85 2364 if self.labels.has_key(change):
1f4ba1cb
SH
2365 label = self.labels[change]
2366 labelDetails = label[0]
2367 labelRevisions = label[1]
71b112d4
SH
2368 if self.verbose:
2369 print "Change %s is labelled %s" % (change, labelDetails)
1f4ba1cb 2370
6de040df 2371 files = p4CmdList(["files"] + ["%s...@%s" % (p, change)
e63231e5 2372 for p in self.branchPrefixes])
1f4ba1cb
SH
2373
2374 if len(files) == len(labelRevisions):
2375
2376 cleanedFiles = {}
2377 for info in files:
56c09345 2378 if info["action"] in self.delete_actions:
1f4ba1cb
SH
2379 continue
2380 cleanedFiles[info["depotFile"]] = info["rev"]
2381
2382 if cleanedFiles == labelRevisions:
06804c76 2383 self.streamTag(self.gitStream, 'tag_%s' % labelDetails['label'], labelDetails, branch, epoch)
1f4ba1cb
SH
2384
2385 else:
a46668fa 2386 if not self.silent:
cebdf5af
HWN
2387 print ("Tag %s does not match with change %s: files do not match."
2388 % (labelDetails["label"], change))
1f4ba1cb
SH
2389
2390 else:
a46668fa 2391 if not self.silent:
cebdf5af
HWN
2392 print ("Tag %s does not match with change %s: file count is different."
2393 % (labelDetails["label"], change))
b984733c 2394
06804c76 2395 # Build a dictionary of changelists and labels, for "detect-labels" option.
1f4ba1cb
SH
2396 def getLabels(self):
2397 self.labels = {}
2398
52a4880b 2399 l = p4CmdList(["labels"] + ["%s..." % p for p in self.depotPaths])
10c3211b 2400 if len(l) > 0 and not self.silent:
183f8436 2401 print "Finding files belonging to labels in %s" % `self.depotPaths`
01ce1fe9
SH
2402
2403 for output in l:
1f4ba1cb
SH
2404 label = output["label"]
2405 revisions = {}
2406 newestChange = 0
71b112d4
SH
2407 if self.verbose:
2408 print "Querying files for label %s" % label
6de040df
LD
2409 for file in p4CmdList(["files"] +
2410 ["%s...@%s" % (p, label)
2411 for p in self.depotPaths]):
1f4ba1cb
SH
2412 revisions[file["depotFile"]] = file["rev"]
2413 change = int(file["change"])
2414 if change > newestChange:
2415 newestChange = change
2416
9bda3a85
SH
2417 self.labels[newestChange] = [output, revisions]
2418
2419 if self.verbose:
2420 print "Label changes: %s" % self.labels.keys()
1f4ba1cb 2421
06804c76
LD
2422 # Import p4 labels as git tags. A direct mapping does not
2423 # exist, so assume that if all the files are at the same revision
2424 # then we can use that, or it's something more complicated we should
2425 # just ignore.
2426 def importP4Labels(self, stream, p4Labels):
2427 if verbose:
2428 print "import p4 labels: " + ' '.join(p4Labels)
2429
2430 ignoredP4Labels = gitConfigList("git-p4.ignoredP4Labels")
c8942a22 2431 validLabelRegexp = gitConfig("git-p4.labelImportRegexp")
06804c76
LD
2432 if len(validLabelRegexp) == 0:
2433 validLabelRegexp = defaultLabelRegexp
2434 m = re.compile(validLabelRegexp)
2435
2436 for name in p4Labels:
2437 commitFound = False
2438
2439 if not m.match(name):
2440 if verbose:
2441 print "label %s does not match regexp %s" % (name,validLabelRegexp)
2442 continue
2443
2444 if name in ignoredP4Labels:
2445 continue
2446
2447 labelDetails = p4CmdList(['label', "-o", name])[0]
2448
2449 # get the most recent changelist for each file in this label
2450 change = p4Cmd(["changes", "-m", "1"] + ["%s...@%s" % (p, name)
2451 for p in self.depotPaths])
2452
2453 if change.has_key('change'):
2454 # find the corresponding git commit; take the oldest commit
2455 changelist = int(change['change'])
2456 gitCommit = read_pipe(["git", "rev-list", "--max-count=1",
2457 "--reverse", ":/\[git-p4:.*change = %d\]" % changelist])
2458 if len(gitCommit) == 0:
2459 print "could not find git commit for changelist %d" % changelist
2460 else:
2461 gitCommit = gitCommit.strip()
2462 commitFound = True
2463 # Convert from p4 time format
2464 try:
2465 tmwhen = time.strptime(labelDetails['Update'], "%Y/%m/%d %H:%M:%S")
2466 except ValueError:
a4e9054c 2467 print "Could not convert label time %s" % labelDetails['Update']
06804c76
LD
2468 tmwhen = 1
2469
2470 when = int(time.mktime(tmwhen))
2471 self.streamTag(stream, name, labelDetails, gitCommit, when)
2472 if verbose:
2473 print "p4 label %s mapped to git commit %s" % (name, gitCommit)
2474 else:
2475 if verbose:
2476 print "Label %s has no changelists - possibly deleted?" % name
2477
2478 if not commitFound:
2479 # We can't import this label; don't try again as it will get very
2480 # expensive repeatedly fetching all the files for labels that will
2481 # never be imported. If the label is moved in the future, the
2482 # ignore will need to be removed manually.
2483 system(["git", "config", "--add", "git-p4.ignoredP4Labels", name])
2484
86dff6b6
HWN
2485 def guessProjectName(self):
2486 for p in self.depotPaths:
6e5295c4
SH
2487 if p.endswith("/"):
2488 p = p[:-1]
2489 p = p[p.strip().rfind("/") + 1:]
2490 if not p.endswith("/"):
2491 p += "/"
2492 return p
86dff6b6 2493
4b97ffb1 2494 def getBranchMapping(self):
6555b2cc
SH
2495 lostAndFoundBranches = set()
2496
8ace74c0
VA
2497 user = gitConfig("git-p4.branchUser")
2498 if len(user) > 0:
2499 command = "branches -u %s" % user
2500 else:
2501 command = "branches"
2502
2503 for info in p4CmdList(command):
52a4880b 2504 details = p4Cmd(["branch", "-o", info["branch"]])
4b97ffb1
SH
2505 viewIdx = 0
2506 while details.has_key("View%s" % viewIdx):
2507 paths = details["View%s" % viewIdx].split(" ")
2508 viewIdx = viewIdx + 1
2509 # require standard //depot/foo/... //depot/bar/... mapping
2510 if len(paths) != 2 or not paths[0].endswith("/...") or not paths[1].endswith("/..."):
2511 continue
2512 source = paths[0]
2513 destination = paths[1]
6509e19c 2514 ## HACK
d53de8b9 2515 if p4PathStartsWith(source, self.depotPaths[0]) and p4PathStartsWith(destination, self.depotPaths[0]):
6509e19c
SH
2516 source = source[len(self.depotPaths[0]):-4]
2517 destination = destination[len(self.depotPaths[0]):-4]
6555b2cc 2518
1a2edf4e
SH
2519 if destination in self.knownBranches:
2520 if not self.silent:
2521 print "p4 branch %s defines a mapping from %s to %s" % (info["branch"], source, destination)
2522 print "but there exists another mapping from %s to %s already!" % (self.knownBranches[destination], destination)
2523 continue
2524
6555b2cc
SH
2525 self.knownBranches[destination] = source
2526
2527 lostAndFoundBranches.discard(destination)
2528
29bdbac1 2529 if source not in self.knownBranches:
6555b2cc
SH
2530 lostAndFoundBranches.add(source)
2531
7199cf13
VA
2532 # Perforce does not strictly require branches to be defined, so we also
2533 # check git config for a branch list.
2534 #
2535 # Example of branch definition in git config file:
2536 # [git-p4]
2537 # branchList=main:branchA
2538 # branchList=main:branchB
2539 # branchList=branchA:branchC
2540 configBranches = gitConfigList("git-p4.branchList")
2541 for branch in configBranches:
2542 if branch:
2543 (source, destination) = branch.split(":")
2544 self.knownBranches[destination] = source
2545
2546 lostAndFoundBranches.discard(destination)
2547
2548 if source not in self.knownBranches:
2549 lostAndFoundBranches.add(source)
2550
6555b2cc
SH
2551
2552 for branch in lostAndFoundBranches:
2553 self.knownBranches[branch] = branch
29bdbac1 2554
38f9f5ec
SH
2555 def getBranchMappingFromGitBranches(self):
2556 branches = p4BranchesInGit(self.importIntoRemotes)
2557 for branch in branches.keys():
2558 if branch == "master":
2559 branch = "main"
2560 else:
2561 branch = branch[len(self.projectName):]
2562 self.knownBranches[branch] = branch
2563
bb6e09b2
HWN
2564 def updateOptionDict(self, d):
2565 option_keys = {}
2566 if self.keepRepoPath:
2567 option_keys['keepRepoPath'] = 1
2568
2569 d["options"] = ' '.join(sorted(option_keys.keys()))
2570
2571 def readOptions(self, d):
2572 self.keepRepoPath = (d.has_key('options')
2573 and ('keepRepoPath' in d['options']))
6326aa58 2574
8134f69c
SH
2575 def gitRefForBranch(self, branch):
2576 if branch == "main":
2577 return self.refPrefix + "master"
2578
2579 if len(branch) <= 0:
2580 return branch
2581
2582 return self.refPrefix + self.projectName + branch
2583
1ca3d710
SH
2584 def gitCommitByP4Change(self, ref, change):
2585 if self.verbose:
2586 print "looking in ref " + ref + " for change %s using bisect..." % change
2587
2588 earliestCommit = ""
2589 latestCommit = parseRevision(ref)
2590
2591 while True:
2592 if self.verbose:
2593 print "trying: earliest %s latest %s" % (earliestCommit, latestCommit)
2594 next = read_pipe("git rev-list --bisect %s %s" % (latestCommit, earliestCommit)).strip()
2595 if len(next) == 0:
2596 if self.verbose:
2597 print "argh"
2598 return ""
2599 log = extractLogMessageFromGitCommit(next)
2600 settings = extractSettingsGitLog(log)
2601 currentChange = int(settings['change'])
2602 if self.verbose:
2603 print "current change %s" % currentChange
2604
2605 if currentChange == change:
2606 if self.verbose:
2607 print "found %s" % next
2608 return next
2609
2610 if currentChange < change:
2611 earliestCommit = "^%s" % next
2612 else:
2613 latestCommit = "%s" % next
2614
2615 return ""
2616
2617 def importNewBranch(self, branch, maxChange):
2618 # make fast-import flush all changes to disk and update the refs using the checkpoint
2619 # command so that we can try to find the branch parent in the git history
2620 self.gitStream.write("checkpoint\n\n");
2621 self.gitStream.flush();
2622 branchPrefix = self.depotPaths[0] + branch + "/"
2623 range = "@1,%s" % maxChange
2624 #print "prefix" + branchPrefix
2625 changes = p4ChangesForPaths([branchPrefix], range)
2626 if len(changes) <= 0:
2627 return False
2628 firstChange = changes[0]
2629 #print "first change in branch: %s" % firstChange
2630 sourceBranch = self.knownBranches[branch]
2631 sourceDepotPath = self.depotPaths[0] + sourceBranch
2632 sourceRef = self.gitRefForBranch(sourceBranch)
2633 #print "source " + sourceBranch
2634
52a4880b 2635 branchParentChange = int(p4Cmd(["changes", "-m", "1", "%s...@1,%s" % (sourceDepotPath, firstChange)])["change"])
1ca3d710
SH
2636 #print "branch parent: %s" % branchParentChange
2637 gitParent = self.gitCommitByP4Change(sourceRef, branchParentChange)
2638 if len(gitParent) > 0:
2639 self.initialParents[self.gitRefForBranch(branch)] = gitParent
2640 #print "parent git commit: %s" % gitParent
2641
2642 self.importChanges(changes)
2643 return True
2644
fed23693
VA
2645 def searchParent(self, parent, branch, target):
2646 parentFound = False
2647 for blob in read_pipe_lines(["git", "rev-list", "--reverse", "--no-merges", parent]):
2648 blob = blob.strip()
2649 if len(read_pipe(["git", "diff-tree", blob, target])) == 0:
2650 parentFound = True
2651 if self.verbose:
2652 print "Found parent of %s in commit %s" % (branch, blob)
2653 break
2654 if parentFound:
2655 return blob
2656 else:
2657 return None
2658
e87f37ae
SH
2659 def importChanges(self, changes):
2660 cnt = 1
2661 for change in changes:
18fa13d0 2662 description = p4_describe(change)
e87f37ae
SH
2663 self.updateOptionDict(description)
2664
2665 if not self.silent:
2666 sys.stdout.write("\rImporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
2667 sys.stdout.flush()
2668 cnt = cnt + 1
2669
2670 try:
2671 if self.detectBranches:
2672 branches = self.splitFilesIntoBranches(description)
2673 for branch in branches.keys():
2674 ## HACK --hwn
2675 branchPrefix = self.depotPaths[0] + branch + "/"
e63231e5 2676 self.branchPrefixes = [ branchPrefix ]
e87f37ae
SH
2677
2678 parent = ""
2679
2680 filesForCommit = branches[branch]
2681
2682 if self.verbose:
2683 print "branch is %s" % branch
2684
2685 self.updatedBranches.add(branch)
2686
2687 if branch not in self.createdBranches:
2688 self.createdBranches.add(branch)
2689 parent = self.knownBranches[branch]
2690 if parent == branch:
2691 parent = ""
1ca3d710
SH
2692 else:
2693 fullBranch = self.projectName + branch
2694 if fullBranch not in self.p4BranchesInGit:
2695 if not self.silent:
2696 print("\n Importing new branch %s" % fullBranch);
2697 if self.importNewBranch(branch, change - 1):
2698 parent = ""
2699 self.p4BranchesInGit.append(fullBranch)
2700 if not self.silent:
2701 print("\n Resuming with change %s" % change);
2702
2703 if self.verbose:
2704 print "parent determined through known branches: %s" % parent
e87f37ae 2705
8134f69c
SH
2706 branch = self.gitRefForBranch(branch)
2707 parent = self.gitRefForBranch(parent)
e87f37ae
SH
2708
2709 if self.verbose:
2710 print "looking for initial parent for %s; current parent is %s" % (branch, parent)
2711
2712 if len(parent) == 0 and branch in self.initialParents:
2713 parent = self.initialParents[branch]
2714 del self.initialParents[branch]
2715
fed23693
VA
2716 blob = None
2717 if len(parent) > 0:
4f9273d2 2718 tempBranch = "%s/%d" % (self.tempBranchLocation, change)
fed23693
VA
2719 if self.verbose:
2720 print "Creating temporary branch: " + tempBranch
e63231e5 2721 self.commit(description, filesForCommit, tempBranch)
fed23693
VA
2722 self.tempBranches.append(tempBranch)
2723 self.checkpoint()
2724 blob = self.searchParent(parent, branch, tempBranch)
2725 if blob:
e63231e5 2726 self.commit(description, filesForCommit, branch, blob)
fed23693
VA
2727 else:
2728 if self.verbose:
2729 print "Parent of %s not found. Committing into head of %s" % (branch, parent)
e63231e5 2730 self.commit(description, filesForCommit, branch, parent)
e87f37ae
SH
2731 else:
2732 files = self.extractFilesFromCommit(description)
e63231e5 2733 self.commit(description, files, self.branch,
e87f37ae 2734 self.initialParent)
47497844 2735 # only needed once, to connect to the previous commit
e87f37ae
SH
2736 self.initialParent = ""
2737 except IOError:
2738 print self.gitError.read()
2739 sys.exit(1)
2740
c208a243
SH
2741 def importHeadRevision(self, revision):
2742 print "Doing initial import of %s from revision %s into %s" % (' '.join(self.depotPaths), revision, self.branch)
2743
4e2e6ce4
PW
2744 details = {}
2745 details["user"] = "git perforce import user"
1494fcbb 2746 details["desc"] = ("Initial import of %s from the state at revision %s\n"
c208a243
SH
2747 % (' '.join(self.depotPaths), revision))
2748 details["change"] = revision
2749 newestRevision = 0
2750
2751 fileCnt = 0
6de040df
LD
2752 fileArgs = ["%s...%s" % (p,revision) for p in self.depotPaths]
2753
2754 for info in p4CmdList(["files"] + fileArgs):
c208a243 2755
68b28593 2756 if 'code' in info and info['code'] == 'error':
c208a243
SH
2757 sys.stderr.write("p4 returned an error: %s\n"
2758 % info['data'])
d88e707f
PW
2759 if info['data'].find("must refer to client") >= 0:
2760 sys.stderr.write("This particular p4 error is misleading.\n")
2761 sys.stderr.write("Perhaps the depot path was misspelled.\n");
2762 sys.stderr.write("Depot path: %s\n" % " ".join(self.depotPaths))
c208a243 2763 sys.exit(1)
68b28593
PW
2764 if 'p4ExitCode' in info:
2765 sys.stderr.write("p4 exitcode: %s\n" % info['p4ExitCode'])
c208a243
SH
2766 sys.exit(1)
2767
2768
2769 change = int(info["change"])
2770 if change > newestRevision:
2771 newestRevision = change
2772
56c09345 2773 if info["action"] in self.delete_actions:
c208a243
SH
2774 # don't increase the file cnt, otherwise details["depotFile123"] will have gaps!
2775 #fileCnt = fileCnt + 1
2776 continue
2777
2778 for prop in ["depotFile", "rev", "action", "type" ]:
2779 details["%s%s" % (prop, fileCnt)] = info[prop]
2780
2781 fileCnt = fileCnt + 1
2782
2783 details["change"] = newestRevision
4e2e6ce4 2784
9dcb9f24 2785 # Use time from top-most change so that all git p4 clones of
4e2e6ce4 2786 # the same p4 repo have the same commit SHA1s.
18fa13d0
PW
2787 res = p4_describe(newestRevision)
2788 details["time"] = res["time"]
4e2e6ce4 2789
c208a243
SH
2790 self.updateOptionDict(details)
2791 try:
e63231e5 2792 self.commit(details, self.extractFilesFromCommit(details), self.branch)
c208a243
SH
2793 except IOError:
2794 print "IO error with git fast-import. Is your git version recent enough?"
2795 print self.gitError.read()
2796
2797
b984733c 2798 def run(self, args):
6326aa58 2799 self.depotPaths = []
179caebf 2800 self.changeRange = ""
6326aa58 2801 self.previousDepotPaths = []
991a2de4 2802 self.hasOrigin = False
ce6f33c8 2803
29bdbac1
SH
2804 # map from branch depot path to parent branch
2805 self.knownBranches = {}
2806 self.initialParents = {}
2807
a028a98e
SH
2808 if self.importIntoRemotes:
2809 self.refPrefix = "refs/remotes/p4/"
2810 else:
db775559 2811 self.refPrefix = "refs/heads/p4/"
a028a98e 2812
991a2de4
PW
2813 if self.syncWithOrigin:
2814 self.hasOrigin = originP4BranchesExist()
2815 if self.hasOrigin:
2816 if not self.silent:
2817 print 'Syncing with origin first, using "git fetch origin"'
2818 system("git fetch origin")
10f880f8 2819
5a8e84cd 2820 branch_arg_given = bool(self.branch)
569d1bd4 2821 if len(self.branch) == 0:
db775559 2822 self.branch = self.refPrefix + "master"
a028a98e 2823 if gitBranchExists("refs/heads/p4") and self.importIntoRemotes:
48df6fd8 2824 system("git update-ref %s refs/heads/p4" % self.branch)
55d12437 2825 system("git branch -D p4")
967f72e2 2826
a93d33ee
PW
2827 # accept either the command-line option, or the configuration variable
2828 if self.useClientSpec:
2829 # will use this after clone to set the variable
2830 self.useClientSpec_from_options = True
2831 else:
09fca77b
PW
2832 if gitConfig("git-p4.useclientspec", "--bool") == "true":
2833 self.useClientSpec = True
2834 if self.useClientSpec:
543987bd 2835 self.clientSpecDirs = getClientSpec()
3a70cdfa 2836
6a49f8e2
HWN
2837 # TODO: should always look at previous commits,
2838 # merge with previous imports, if possible.
2839 if args == []:
d414c74a 2840 if self.hasOrigin:
5ca44617 2841 createOrUpdateBranchesFromOrigin(self.refPrefix, self.silent)
3b650fc9
PW
2842
2843 # branches holds mapping from branch name to sha1
2844 branches = p4BranchesInGit(self.importIntoRemotes)
8c9e8b6e
PW
2845
2846 # restrict to just this one, disabling detect-branches
2847 if branch_arg_given:
2848 short = self.branch.split("/")[-1]
2849 if short in branches:
2850 self.p4BranchesInGit = [ short ]
2851 else:
2852 self.p4BranchesInGit = branches.keys()
abcd790f
SH
2853
2854 if len(self.p4BranchesInGit) > 1:
2855 if not self.silent:
2856 print "Importing from/into multiple branches"
2857 self.detectBranches = True
8c9e8b6e
PW
2858 for branch in branches.keys():
2859 self.initialParents[self.refPrefix + branch] = \
2860 branches[branch]
967f72e2 2861
29bdbac1
SH
2862 if self.verbose:
2863 print "branches: %s" % self.p4BranchesInGit
2864
2865 p4Change = 0
2866 for branch in self.p4BranchesInGit:
cebdf5af 2867 logMsg = extractLogMessageFromGitCommit(self.refPrefix + branch)
bb6e09b2
HWN
2868
2869 settings = extractSettingsGitLog(logMsg)
29bdbac1 2870
bb6e09b2
HWN
2871 self.readOptions(settings)
2872 if (settings.has_key('depot-paths')
2873 and settings.has_key ('change')):
2874 change = int(settings['change']) + 1
29bdbac1
SH
2875 p4Change = max(p4Change, change)
2876
bb6e09b2
HWN
2877 depotPaths = sorted(settings['depot-paths'])
2878 if self.previousDepotPaths == []:
6326aa58 2879 self.previousDepotPaths = depotPaths
29bdbac1 2880 else:
6326aa58
HWN
2881 paths = []
2882 for (prev, cur) in zip(self.previousDepotPaths, depotPaths):
04d277b3
VA
2883 prev_list = prev.split("/")
2884 cur_list = cur.split("/")
2885 for i in range(0, min(len(cur_list), len(prev_list))):
2886 if cur_list[i] <> prev_list[i]:
583e1707 2887 i = i - 1
6326aa58
HWN
2888 break
2889
04d277b3 2890 paths.append ("/".join(cur_list[:i + 1]))
6326aa58
HWN
2891
2892 self.previousDepotPaths = paths
29bdbac1
SH
2893
2894 if p4Change > 0:
bb6e09b2 2895 self.depotPaths = sorted(self.previousDepotPaths)
d5904674 2896 self.changeRange = "@%s,#head" % p4Change
341dc1c1 2897 if not self.silent and not self.detectBranches:
967f72e2 2898 print "Performing incremental import into %s git branch" % self.branch
569d1bd4 2899
40d69ac3
PW
2900 # accept multiple ref name abbreviations:
2901 # refs/foo/bar/branch -> use it exactly
2902 # p4/branch -> prepend refs/remotes/ or refs/heads/
2903 # branch -> prepend refs/remotes/p4/ or refs/heads/p4/
f9162f6a 2904 if not self.branch.startswith("refs/"):
40d69ac3
PW
2905 if self.importIntoRemotes:
2906 prepend = "refs/remotes/"
2907 else:
2908 prepend = "refs/heads/"
2909 if not self.branch.startswith("p4/"):
2910 prepend += "p4/"
2911 self.branch = prepend + self.branch
179caebf 2912
6326aa58 2913 if len(args) == 0 and self.depotPaths:
b984733c 2914 if not self.silent:
6326aa58 2915 print "Depot paths: %s" % ' '.join(self.depotPaths)
b984733c 2916 else:
6326aa58 2917 if self.depotPaths and self.depotPaths != args:
cebdf5af 2918 print ("previous import used depot path %s and now %s was specified. "
6326aa58
HWN
2919 "This doesn't work!" % (' '.join (self.depotPaths),
2920 ' '.join (args)))
b984733c 2921 sys.exit(1)
6326aa58 2922
bb6e09b2 2923 self.depotPaths = sorted(args)
b984733c 2924
1c49fc19 2925 revision = ""
b984733c 2926 self.users = {}
b984733c 2927
58c8bc7c
PW
2928 # Make sure no revision specifiers are used when --changesfile
2929 # is specified.
2930 bad_changesfile = False
2931 if len(self.changesFile) > 0:
2932 for p in self.depotPaths:
2933 if p.find("@") >= 0 or p.find("#") >= 0:
2934 bad_changesfile = True
2935 break
2936 if bad_changesfile:
2937 die("Option --changesfile is incompatible with revision specifiers")
2938
6326aa58
HWN
2939 newPaths = []
2940 for p in self.depotPaths:
2941 if p.find("@") != -1:
2942 atIdx = p.index("@")
2943 self.changeRange = p[atIdx:]
2944 if self.changeRange == "@all":
2945 self.changeRange = ""
6a49f8e2 2946 elif ',' not in self.changeRange:
1c49fc19 2947 revision = self.changeRange
6326aa58 2948 self.changeRange = ""
7fcff9de 2949 p = p[:atIdx]
6326aa58
HWN
2950 elif p.find("#") != -1:
2951 hashIdx = p.index("#")
1c49fc19 2952 revision = p[hashIdx:]
7fcff9de 2953 p = p[:hashIdx]
6326aa58 2954 elif self.previousDepotPaths == []:
58c8bc7c
PW
2955 # pay attention to changesfile, if given, else import
2956 # the entire p4 tree at the head revision
2957 if len(self.changesFile) == 0:
2958 revision = "#head"
6326aa58
HWN
2959
2960 p = re.sub ("\.\.\.$", "", p)
2961 if not p.endswith("/"):
2962 p += "/"
2963
2964 newPaths.append(p)
2965
2966 self.depotPaths = newPaths
2967
e63231e5
PW
2968 # --detect-branches may change this for each branch
2969 self.branchPrefixes = self.depotPaths
2970
b607e71e 2971 self.loadUserMapFromCache()
cb53e1f8
SH
2972 self.labels = {}
2973 if self.detectLabels:
2974 self.getLabels();
b984733c 2975
4b97ffb1 2976 if self.detectBranches:
df450923
SH
2977 ## FIXME - what's a P4 projectName ?
2978 self.projectName = self.guessProjectName()
2979
38f9f5ec
SH
2980 if self.hasOrigin:
2981 self.getBranchMappingFromGitBranches()
2982 else:
2983 self.getBranchMapping()
29bdbac1
SH
2984 if self.verbose:
2985 print "p4-git branches: %s" % self.p4BranchesInGit
2986 print "initial parents: %s" % self.initialParents
2987 for b in self.p4BranchesInGit:
2988 if b != "master":
6326aa58
HWN
2989
2990 ## FIXME
29bdbac1
SH
2991 b = b[len(self.projectName):]
2992 self.createdBranches.add(b)
4b97ffb1 2993
f291b4e3 2994 self.tz = "%+03d%02d" % (- time.timezone / 3600, ((- time.timezone % 3600) / 60))
b984733c 2995
78189bea
PW
2996 self.importProcess = subprocess.Popen(["git", "fast-import"],
2997 stdin=subprocess.PIPE,
2998 stdout=subprocess.PIPE,
2999 stderr=subprocess.PIPE);
3000 self.gitOutput = self.importProcess.stdout
3001 self.gitStream = self.importProcess.stdin
3002 self.gitError = self.importProcess.stderr
b984733c 3003
1c49fc19 3004 if revision:
c208a243 3005 self.importHeadRevision(revision)
b984733c
SH
3006 else:
3007 changes = []
3008
0828ab14 3009 if len(self.changesFile) > 0:
b984733c 3010 output = open(self.changesFile).readlines()
1d7367dc 3011 changeSet = set()
b984733c
SH
3012 for line in output:
3013 changeSet.add(int(line))
3014
3015 for change in changeSet:
3016 changes.append(change)
3017
3018 changes.sort()
3019 else:
9dcb9f24
PW
3020 # catch "git p4 sync" with no new branches, in a repo that
3021 # does not have any existing p4 branches
5a8e84cd
PW
3022 if len(args) == 0:
3023 if not self.p4BranchesInGit:
3024 die("No remote p4 branches. Perhaps you never did \"git p4 clone\" in here.")
3025
3026 # The default branch is master, unless --branch is used to
3027 # specify something else. Make sure it exists, or complain
3028 # nicely about how to use --branch.
3029 if not self.detectBranches:
3030 if not branch_exists(self.branch):
3031 if branch_arg_given:
3032 die("Error: branch %s does not exist." % self.branch)
3033 else:
3034 die("Error: no branch %s; perhaps specify one with --branch." %
3035 self.branch)
3036
29bdbac1 3037 if self.verbose:
86dff6b6 3038 print "Getting p4 changes for %s...%s" % (', '.join(self.depotPaths),
6326aa58 3039 self.changeRange)
4f6432d8 3040 changes = p4ChangesForPaths(self.depotPaths, self.changeRange)
b984733c 3041
01a9c9c5 3042 if len(self.maxChanges) > 0:
7fcff9de 3043 changes = changes[:min(int(self.maxChanges), len(changes))]
01a9c9c5 3044
b984733c 3045 if len(changes) == 0:
0828ab14 3046 if not self.silent:
341dc1c1 3047 print "No changes to import!"
06804c76
LD
3048 else:
3049 if not self.silent and not self.detectBranches:
3050 print "Import destination: %s" % self.branch
3051
3052 self.updatedBranches = set()
b984733c 3053
47497844
PW
3054 if not self.detectBranches:
3055 if args:
3056 # start a new branch
3057 self.initialParent = ""
3058 else:
3059 # build on a previous revision
3060 self.initialParent = parseRevision(self.branch)
3061
06804c76 3062 self.importChanges(changes)
a9d1a27a 3063
06804c76
LD
3064 if not self.silent:
3065 print ""
3066 if len(self.updatedBranches) > 0:
3067 sys.stdout.write("Updated branches: ")
3068 for b in self.updatedBranches:
3069 sys.stdout.write("%s " % b)
3070 sys.stdout.write("\n")
341dc1c1 3071
06804c76 3072 if gitConfig("git-p4.importLabels", "--bool") == "true":
06dcd152 3073 self.importLabels = True
b984733c 3074
06804c76
LD
3075 if self.importLabels:
3076 p4Labels = getP4Labels(self.depotPaths)
3077 gitTags = getGitTags()
3078
3079 missingP4Labels = p4Labels - gitTags
3080 self.importP4Labels(self.gitStream, missingP4Labels)
b984733c 3081
b984733c 3082 self.gitStream.close()
78189bea 3083 if self.importProcess.wait() != 0:
29bdbac1 3084 die("fast-import failed: %s" % self.gitError.read())
b984733c
SH
3085 self.gitOutput.close()
3086 self.gitError.close()
3087
fed23693
VA
3088 # Cleanup temporary branches created during import
3089 if self.tempBranches != []:
3090 for branch in self.tempBranches:
3091 read_pipe("git update-ref -d %s" % branch)
3092 os.rmdir(os.path.join(os.environ.get("GIT_DIR", ".git"), self.tempBranchLocation))
3093
55d12437
PW
3094 # Create a symbolic ref p4/HEAD pointing to p4/<branch> to allow
3095 # a convenient shortcut refname "p4".
3096 if self.importIntoRemotes:
3097 head_ref = self.refPrefix + "HEAD"
3098 if not gitBranchExists(head_ref) and gitBranchExists(self.branch):
3099 system(["git", "symbolic-ref", head_ref, self.branch])
3100
b984733c
SH
3101 return True
3102
01ce1fe9
SH
3103class P4Rebase(Command):
3104 def __init__(self):
3105 Command.__init__(self)
06804c76
LD
3106 self.options = [
3107 optparse.make_option("--import-labels", dest="importLabels", action="store_true"),
06804c76 3108 ]
06804c76 3109 self.importLabels = False
cebdf5af
HWN
3110 self.description = ("Fetches the latest revision from perforce and "
3111 + "rebases the current work (branch) against it")
01ce1fe9
SH
3112
3113 def run(self, args):
3114 sync = P4Sync()
06804c76 3115 sync.importLabels = self.importLabels
01ce1fe9 3116 sync.run([])
d7e3868c 3117
14594f4b
SH
3118 return self.rebase()
3119
3120 def rebase(self):
36ee4ee4
SH
3121 if os.system("git update-index --refresh") != 0:
3122 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.");
3123 if len(read_pipe("git diff-index HEAD --")) > 0:
3124 die("You have uncommited changes. Please commit them before rebasing or stash them away with git stash.");
3125
d7e3868c
SH
3126 [upstream, settings] = findUpstreamBranchPoint()
3127 if len(upstream) == 0:
3128 die("Cannot find upstream branchpoint for rebase")
3129
3130 # the branchpoint may be p4/foo~3, so strip off the parent
3131 upstream = re.sub("~[0-9]+$", "", upstream)
3132
3133 print "Rebasing the current branch onto %s" % upstream
b25b2065 3134 oldHead = read_pipe("git rev-parse HEAD").strip()
d7e3868c 3135 system("git rebase %s" % upstream)
1f52af6c 3136 system("git diff-tree --stat --summary -M %s HEAD" % oldHead)
01ce1fe9
SH
3137 return True
3138
f9a3a4f7
SH
3139class P4Clone(P4Sync):
3140 def __init__(self):
3141 P4Sync.__init__(self)
3142 self.description = "Creates a new git repository and imports from Perforce into it"
bb6e09b2 3143 self.usage = "usage: %prog [options] //depot/path[@revRange]"
354081d5 3144 self.options += [
bb6e09b2
HWN
3145 optparse.make_option("--destination", dest="cloneDestination",
3146 action='store', default=None,
354081d5
TT
3147 help="where to leave result of the clone"),
3148 optparse.make_option("-/", dest="cloneExclude",
3149 action="append", type="string",
38200076
PW
3150 help="exclude depot path"),
3151 optparse.make_option("--bare", dest="cloneBare",
3152 action="store_true", default=False),
354081d5 3153 ]
bb6e09b2 3154 self.cloneDestination = None
f9a3a4f7 3155 self.needsGit = False
38200076 3156 self.cloneBare = False
f9a3a4f7 3157
354081d5
TT
3158 # This is required for the "append" cloneExclude action
3159 def ensure_value(self, attr, value):
3160 if not hasattr(self, attr) or getattr(self, attr) is None:
3161 setattr(self, attr, value)
3162 return getattr(self, attr)
3163
6a49f8e2
HWN
3164 def defaultDestination(self, args):
3165 ## TODO: use common prefix of args?
3166 depotPath = args[0]
3167 depotDir = re.sub("(@[^@]*)$", "", depotPath)
3168 depotDir = re.sub("(#[^#]*)$", "", depotDir)
053d9e43 3169 depotDir = re.sub(r"\.\.\.$", "", depotDir)
6a49f8e2
HWN
3170 depotDir = re.sub(r"/$", "", depotDir)
3171 return os.path.split(depotDir)[1]
3172
f9a3a4f7
SH
3173 def run(self, args):
3174 if len(args) < 1:
3175 return False
bb6e09b2
HWN
3176
3177 if self.keepRepoPath and not self.cloneDestination:
3178 sys.stderr.write("Must specify destination for --keep-path\n")
3179 sys.exit(1)
f9a3a4f7 3180
6326aa58 3181 depotPaths = args
5e100b5c
SH
3182
3183 if not self.cloneDestination and len(depotPaths) > 1:
3184 self.cloneDestination = depotPaths[-1]
3185 depotPaths = depotPaths[:-1]
3186
354081d5 3187 self.cloneExclude = ["/"+p for p in self.cloneExclude]
6326aa58
HWN
3188 for p in depotPaths:
3189 if not p.startswith("//"):
0f487d30 3190 sys.stderr.write('Depot paths must start with "//": %s\n' % p)
6326aa58 3191 return False
f9a3a4f7 3192
bb6e09b2 3193 if not self.cloneDestination:
98ad4faf 3194 self.cloneDestination = self.defaultDestination(args)
f9a3a4f7 3195
86dff6b6 3196 print "Importing from %s into %s" % (', '.join(depotPaths), self.cloneDestination)
38200076 3197
c3bf3f13
KG
3198 if not os.path.exists(self.cloneDestination):
3199 os.makedirs(self.cloneDestination)
053fd0c1 3200 chdir(self.cloneDestination)
38200076
PW
3201
3202 init_cmd = [ "git", "init" ]
3203 if self.cloneBare:
3204 init_cmd.append("--bare")
3205 subprocess.check_call(init_cmd)
3206
6326aa58 3207 if not P4Sync.run(self, depotPaths):
f9a3a4f7 3208 return False
c595956d
PW
3209
3210 # create a master branch and check out a work tree
3211 if gitBranchExists(self.branch):
3212 system([ "git", "branch", "master", self.branch ])
3213 if not self.cloneBare:
3214 system([ "git", "checkout", "-f" ])
3215 else:
3216 print 'Not checking out any branch, use ' \
3217 '"git checkout -q -b master <branch>"'
86dff6b6 3218
a93d33ee
PW
3219 # auto-set this variable if invoked with --use-client-spec
3220 if self.useClientSpec_from_options:
3221 system("git config --bool git-p4.useclientspec true")
3222
f9a3a4f7
SH
3223 return True
3224
09d89de2
SH
3225class P4Branches(Command):
3226 def __init__(self):
3227 Command.__init__(self)
3228 self.options = [ ]
3229 self.description = ("Shows the git branches that hold imports and their "
3230 + "corresponding perforce depot paths")
3231 self.verbose = False
3232
3233 def run(self, args):
5ca44617
SH
3234 if originP4BranchesExist():
3235 createOrUpdateBranchesFromOrigin()
3236
09d89de2
SH
3237 cmdline = "git rev-parse --symbolic "
3238 cmdline += " --remotes"
3239
3240 for line in read_pipe_lines(cmdline):
3241 line = line.strip()
3242
3243 if not line.startswith('p4/') or line == "p4/HEAD":
3244 continue
3245 branch = line
3246
3247 log = extractLogMessageFromGitCommit("refs/remotes/%s" % branch)
3248 settings = extractSettingsGitLog(log)
3249
3250 print "%s <= %s (%s)" % (branch, ",".join(settings["depot-paths"]), settings["change"])
3251 return True
3252
b984733c
SH
3253class HelpFormatter(optparse.IndentedHelpFormatter):
3254 def __init__(self):
3255 optparse.IndentedHelpFormatter.__init__(self)
3256
3257 def format_description(self, description):
3258 if description:
3259 return description + "\n"
3260 else:
3261 return ""
4f5cf76a 3262
86949eef
SH
3263def printUsage(commands):
3264 print "usage: %s <command> [options]" % sys.argv[0]
3265 print ""
3266 print "valid commands: %s" % ", ".join(commands)
3267 print ""
3268 print "Try %s <command> --help for command specific help." % sys.argv[0]
3269 print ""
3270
3271commands = {
b86f7378
HWN
3272 "debug" : P4Debug,
3273 "submit" : P4Submit,
a9834f58 3274 "commit" : P4Submit,
b86f7378
HWN
3275 "sync" : P4Sync,
3276 "rebase" : P4Rebase,
3277 "clone" : P4Clone,
09d89de2
SH
3278 "rollback" : P4RollBack,
3279 "branches" : P4Branches
86949eef
SH
3280}
3281
86949eef 3282
bb6e09b2
HWN
3283def main():
3284 if len(sys.argv[1:]) == 0:
3285 printUsage(commands.keys())
3286 sys.exit(2)
4f5cf76a 3287
bb6e09b2
HWN
3288 cmdName = sys.argv[1]
3289 try:
b86f7378
HWN
3290 klass = commands[cmdName]
3291 cmd = klass()
bb6e09b2
HWN
3292 except KeyError:
3293 print "unknown command %s" % cmdName
3294 print ""
3295 printUsage(commands.keys())
3296 sys.exit(2)
3297
3298 options = cmd.options
b86f7378 3299 cmd.gitdir = os.environ.get("GIT_DIR", None)
bb6e09b2
HWN
3300
3301 args = sys.argv[2:]
3302
b0ccc80d 3303 options.append(optparse.make_option("--verbose", "-v", dest="verbose", action="store_true"))
6a10b6aa
LD
3304 if cmd.needsGit:
3305 options.append(optparse.make_option("--git-dir", dest="gitdir"))
bb6e09b2 3306
6a10b6aa
LD
3307 parser = optparse.OptionParser(cmd.usage.replace("%prog", "%prog " + cmdName),
3308 options,
3309 description = cmd.description,
3310 formatter = HelpFormatter())
bb6e09b2 3311
6a10b6aa 3312 (cmd, args) = parser.parse_args(sys.argv[2:], cmd);
bb6e09b2
HWN
3313 global verbose
3314 verbose = cmd.verbose
3315 if cmd.needsGit:
b86f7378
HWN
3316 if cmd.gitdir == None:
3317 cmd.gitdir = os.path.abspath(".git")
3318 if not isValidGitDir(cmd.gitdir):
3319 cmd.gitdir = read_pipe("git rev-parse --git-dir").strip()
3320 if os.path.exists(cmd.gitdir):
bb6e09b2
HWN
3321 cdup = read_pipe("git rev-parse --show-cdup").strip()
3322 if len(cdup) > 0:
053fd0c1 3323 chdir(cdup);
e20a9e53 3324
b86f7378
HWN
3325 if not isValidGitDir(cmd.gitdir):
3326 if isValidGitDir(cmd.gitdir + "/.git"):
3327 cmd.gitdir += "/.git"
bb6e09b2 3328 else:
b86f7378 3329 die("fatal: cannot locate git repository at %s" % cmd.gitdir)
e20a9e53 3330
b86f7378 3331 os.environ["GIT_DIR"] = cmd.gitdir
86949eef 3332
bb6e09b2
HWN
3333 if not cmd.run(args):
3334 parser.print_help()
09fca77b 3335 sys.exit(2)
4f5cf76a 3336
4f5cf76a 3337
bb6e09b2
HWN
3338if __name__ == '__main__':
3339 main()