3 # git-p4.py -- A tool for bidirectional operation between a Perforce depot and git.
5 # Author: Simon Hausmann <simon@lst.de>
6 # Copyright: 2007 Simon Hausmann <simon@lst.de>
8 # License: MIT <http://www.opensource.org/licenses/mit-license.php>
10 # pylint: disable=invalid-name,missing-docstring,too-many-arguments,broad-except
11 # pylint: disable=no-self-use,wrong-import-position,consider-iterating-dictionary
12 # pylint: disable=wrong-import-order,unused-import,too-few-public-methods
13 # pylint: disable=too-many-lines,ungrouped-imports,fixme,too-many-locals
14 # pylint: disable=line-too-long,bad-whitespace,superfluous-parens
15 # pylint: disable=too-many-statements,too-many-instance-attributes
16 # pylint: disable=too-many-branches,too-many-nested-blocks
19 if sys.hexversion < 0x02040000:
20 # The limiter is the subprocess module
21 sys.stderr.write("git-p4: requires Python 2.4 or later.\n")
38 # support basestring in python3
42 # 'unicode' is undefined, must be Python 3
46 basestring = (str,bytes)
48 # 'unicode' exists, must be Python 2
52 basestring = basestring
55 from subprocess import CalledProcessError
57 # from python2.7:subprocess.py
58 # Exception classes used by this module.
59 class CalledProcessError(Exception):
60 """This exception is raised when a process run by check_call() returns
61 a non-zero exit status. The exit status will be stored in the
62 returncode attribute."""
63 def __init__(self, returncode, cmd):
64 self.returncode = returncode
67 return "Command '%s' returned non-zero exit status %d" % (self.cmd, self.returncode)
71 # Only labels/tags matching this will be imported/exported
72 defaultLabelRegexp = r'[a-zA-Z0-9_\-.]+$'
74 # The block size is reduced automatically if required
75 defaultBlockSize = 1<<20
77 p4_access_checked = False
79 def p4_build_cmd(cmd):
80 """Build a suitable p4 command line.
82 This consolidates building and returning a p4 command line into one
83 location. It means that hooking into the environment, or other configuration
84 can be done more easily.
88 user = gitConfig("git-p4.user")
90 real_cmd += ["-u",user]
92 password = gitConfig("git-p4.password")
94 real_cmd += ["-P", password]
96 port = gitConfig("git-p4.port")
98 real_cmd += ["-p", port]
100 host = gitConfig("git-p4.host")
102 real_cmd += ["-H", host]
104 client = gitConfig("git-p4.client")
106 real_cmd += ["-c", client]
108 retries = gitConfigInt("git-p4.retries")
110 # Perform 3 retries by default
113 # Provide a way to not pass this option by setting git-p4.retries to 0
114 real_cmd += ["-r", str(retries)]
116 if isinstance(cmd,basestring):
117 real_cmd = ' '.join(real_cmd) + ' ' + cmd
121 # now check that we can actually talk to the server
122 global p4_access_checked
123 if not p4_access_checked:
124 p4_access_checked = True # suppress access checks in p4_check_access itself
130 """ Return TRUE if the given path is a git directory (/path/to/dir/.git).
131 This won't automatically add ".git" to a directory.
133 d = read_pipe(["git", "--git-dir", path, "rev-parse", "--git-dir"], True).strip()
134 if not d or len(d) == 0:
139 def chdir(path, is_client_path=False):
140 """Do chdir to the given path, and set the PWD environment
141 variable for use by P4. It does not look at getcwd() output.
142 Since we're not using the shell, it is necessary to set the
143 PWD environment variable explicitly.
145 Normally, expand the path to force it to be absolute. This
146 addresses the use of relative path names inside P4 settings,
147 e.g. P4CONFIG=.p4config. P4 does not simply open the filename
148 as given; it looks for .p4config using PWD.
150 If is_client_path, the path was handed to us directly by p4,
151 and may be a symbolic link. Do not call os.getcwd() in this
152 case, because it will cause p4 to think that PWD is not inside
157 if not is_client_path:
159 os.environ['PWD'] = path
162 """Return free space in bytes on the disk of the given dirname."""
163 if platform.system() == 'Windows':
164 free_bytes = ctypes.c_ulonglong(0)
165 ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(os.getcwd()), None, None, ctypes.pointer(free_bytes))
166 return free_bytes.value
168 st = os.statvfs(os.getcwd())
169 return st.f_bavail * st.f_frsize
175 sys.stderr.write(msg + "\n")
178 def prompt(prompt_text):
179 """ Prompt the user to choose one of the choices
181 Choices are identified in the prompt_text by square brackets around
182 a single letter option.
184 choices = set(m.group(1) for m in re.finditer(r"\[(.)\]", prompt_text))
186 response = raw_input(prompt_text).strip().lower()
189 response = response[0]
190 if response in choices:
193 def write_pipe(c, stdin):
195 sys.stderr.write('Writing pipe: %s\n' % str(c))
197 expand = isinstance(c,basestring)
198 p = subprocess.Popen(c, stdin=subprocess.PIPE, shell=expand)
200 val = pipe.write(stdin)
203 die('Command failed: %s' % str(c))
207 def p4_write_pipe(c, stdin):
208 real_cmd = p4_build_cmd(c)
209 return write_pipe(real_cmd, stdin)
211 def read_pipe_full(c):
212 """ Read output from command. Returns a tuple
213 of the return status, stdout text and stderr
217 sys.stderr.write('Reading pipe: %s\n' % str(c))
219 expand = isinstance(c,basestring)
220 p = subprocess.Popen(c, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=expand)
221 (out, err) = p.communicate()
222 return (p.returncode, out, err)
224 def read_pipe(c, ignore_error=False):
225 """ Read output from command. Returns the output text on
226 success. On failure, terminates execution, unless
227 ignore_error is True, when it returns an empty string.
229 (retcode, out, err) = read_pipe_full(c)
234 die('Command failed: %s\nError: %s' % (str(c), err))
237 def read_pipe_text(c):
238 """ Read output from a command with trailing whitespace stripped.
239 On error, returns None.
241 (retcode, out, err) = read_pipe_full(c)
247 def p4_read_pipe(c, ignore_error=False):
248 real_cmd = p4_build_cmd(c)
249 return read_pipe(real_cmd, ignore_error)
251 def read_pipe_lines(c):
253 sys.stderr.write('Reading pipe: %s\n' % str(c))
255 expand = isinstance(c, basestring)
256 p = subprocess.Popen(c, stdout=subprocess.PIPE, shell=expand)
258 val = pipe.readlines()
259 if pipe.close() or p.wait():
260 die('Command failed: %s' % str(c))
264 def p4_read_pipe_lines(c):
265 """Specifically invoke p4 on the command supplied. """
266 real_cmd = p4_build_cmd(c)
267 return read_pipe_lines(real_cmd)
269 def p4_has_command(cmd):
270 """Ask p4 for help on this command. If it returns an error, the
271 command does not exist in this version of p4."""
272 real_cmd = p4_build_cmd(["help", cmd])
273 p = subprocess.Popen(real_cmd, stdout=subprocess.PIPE,
274 stderr=subprocess.PIPE)
276 return p.returncode == 0
278 def p4_has_move_command():
279 """See if the move command exists, that it supports -k, and that
280 it has not been administratively disabled. The arguments
281 must be correct, but the filenames do not have to exist. Use
282 ones with wildcards so even if they exist, it will fail."""
284 if not p4_has_command("move"):
286 cmd = p4_build_cmd(["move", "-k", "@from", "@to"])
287 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
288 (out, err) = p.communicate()
289 # return code will be 1 in either case
290 if err.find("Invalid option") >= 0:
292 if err.find("disabled") >= 0:
294 # assume it failed because @... was invalid changelist
297 def system(cmd, ignore_error=False):
298 expand = isinstance(cmd,basestring)
300 sys.stderr.write("executing %s\n" % str(cmd))
301 retcode = subprocess.call(cmd, shell=expand)
302 if retcode and not ignore_error:
303 raise CalledProcessError(retcode, cmd)
308 """Specifically invoke p4 as the system command. """
309 real_cmd = p4_build_cmd(cmd)
310 expand = isinstance(real_cmd, basestring)
311 retcode = subprocess.call(real_cmd, shell=expand)
313 raise CalledProcessError(retcode, real_cmd)
315 def die_bad_access(s):
316 die("failure accessing depot: {0}".format(s.rstrip()))
318 def p4_check_access(min_expiration=1):
319 """ Check if we can access Perforce - account still logged in
321 results = p4CmdList(["login", "-s"])
323 if len(results) == 0:
324 # should never get here: always get either some results, or a p4ExitCode
325 assert("could not parse response from perforce")
329 if 'p4ExitCode' in result:
330 # p4 returned non-zero status, e.g. P4PORT invalid, or p4 not in path
331 die_bad_access("could not run p4")
333 code = result.get("code")
335 # we get here if we couldn't connect and there was nothing to unmarshal
336 die_bad_access("could not connect")
339 expiry = result.get("TicketExpiration")
342 if expiry > min_expiration:
346 die_bad_access("perforce ticket expires in {0} seconds".format(expiry))
349 # account without a timeout - all ok
352 elif code == "error":
353 data = result.get("data")
355 die_bad_access("p4 error: {0}".format(data))
357 die_bad_access("unknown error")
361 die_bad_access("unknown error code {0}".format(code))
363 _p4_version_string = None
364 def p4_version_string():
365 """Read the version string, showing just the last line, which
366 hopefully is the interesting version bit.
369 Perforce - The Fast Software Configuration Management System.
370 Copyright 1995-2011 Perforce Software. All rights reserved.
371 Rev. P4/NTX86/2011.1/393975 (2011/12/16).
373 global _p4_version_string
374 if not _p4_version_string:
375 a = p4_read_pipe_lines(["-V"])
376 _p4_version_string = a[-1].rstrip()
377 return _p4_version_string
379 def p4_integrate(src, dest):
380 p4_system(["integrate", "-Dt", wildcard_encode(src), wildcard_encode(dest)])
382 def p4_sync(f, *options):
383 p4_system(["sync"] + list(options) + [wildcard_encode(f)])
386 # forcibly add file names with wildcards
387 if wildcard_present(f):
388 p4_system(["add", "-f", f])
390 p4_system(["add", f])
393 p4_system(["delete", wildcard_encode(f)])
395 def p4_edit(f, *options):
396 p4_system(["edit"] + list(options) + [wildcard_encode(f)])
399 p4_system(["revert", wildcard_encode(f)])
401 def p4_reopen(type, f):
402 p4_system(["reopen", "-t", type, wildcard_encode(f)])
404 def p4_reopen_in_change(changelist, files):
405 cmd = ["reopen", "-c", str(changelist)] + files
408 def p4_move(src, dest):
409 p4_system(["move", "-k", wildcard_encode(src), wildcard_encode(dest)])
411 def p4_last_change():
412 results = p4CmdList(["changes", "-m", "1"], skip_info=True)
413 return int(results[0]['change'])
415 def p4_describe(change, shelved=False):
416 """Make sure it returns a valid result by checking for
417 the presence of field "time". Return a dict of the
420 cmd = ["describe", "-s"]
425 ds = p4CmdList(cmd, skip_info=True)
427 die("p4 describe -s %d did not return 1 result: %s" % (change, str(ds)))
431 if "p4ExitCode" in d:
432 die("p4 describe -s %d exited with %d: %s" % (change, d["p4ExitCode"],
435 if d["code"] == "error":
436 die("p4 describe -s %d returned error code: %s" % (change, str(d)))
439 die("p4 describe -s %d returned no \"time\": %s" % (change, str(d)))
444 # Canonicalize the p4 type and return a tuple of the
445 # base type, plus any modifiers. See "p4 help filetypes"
446 # for a list and explanation.
448 def split_p4_type(p4type):
450 p4_filetypes_historical = {
451 "ctempobj": "binary+Sw",
457 "tempobj": "binary+FSw",
458 "ubinary": "binary+F",
459 "uresource": "resource+F",
460 "uxbinary": "binary+Fx",
461 "xbinary": "binary+x",
463 "xtempobj": "binary+Swx",
465 "xunicode": "unicode+x",
468 if p4type in p4_filetypes_historical:
469 p4type = p4_filetypes_historical[p4type]
471 s = p4type.split("+")
479 # return the raw p4 type of a file (text, text+ko, etc)
482 results = p4CmdList(["fstat", "-T", "headType", wildcard_encode(f)])
483 return results[0]['headType']
486 # Given a type base and modifier, return a regexp matching
487 # the keywords that can be expanded in the file
489 def p4_keywords_regexp_for_type(base, type_mods):
490 if base in ("text", "unicode", "binary"):
492 if "ko" in type_mods:
494 elif "k" in type_mods:
495 kwords = 'Id|Header|Author|Date|DateTime|Change|File|Revision'
499 \$ # Starts with a dollar, followed by...
500 (%s) # one of the keywords, followed by...
501 (:[^$\n]+)? # possibly an old expansion, followed by...
509 # Given a file, return a regexp matching the possible
510 # RCS keywords that will be expanded, or None for files
511 # with kw expansion turned off.
513 def p4_keywords_regexp_for_file(file):
514 if not os.path.exists(file):
517 (type_base, type_mods) = split_p4_type(p4_type(file))
518 return p4_keywords_regexp_for_type(type_base, type_mods)
520 def setP4ExecBit(file, mode):
521 # Reopens an already open file and changes the execute bit to match
522 # the execute bit setting in the passed in mode.
526 if not isModeExec(mode):
527 p4Type = getP4OpenedType(file)
528 p4Type = re.sub('^([cku]?)x(.*)', '\\1\\2', p4Type)
529 p4Type = re.sub('(.*?\+.*?)x(.*?)', '\\1\\2', p4Type)
530 if p4Type[-1] == "+":
531 p4Type = p4Type[0:-1]
533 p4_reopen(p4Type, file)
535 def getP4OpenedType(file):
536 # Returns the perforce file type for the given file.
538 result = p4_read_pipe(["opened", wildcard_encode(file)])
539 match = re.match(".*\((.+)\)( \*exclusive\*)?\r?$", result)
541 return match.group(1)
543 die("Could not determine file type for %s (result: '%s')" % (file, result))
545 # Return the set of all p4 labels
546 def getP4Labels(depotPaths):
548 if isinstance(depotPaths,basestring):
549 depotPaths = [depotPaths]
551 for l in p4CmdList(["labels"] + ["%s..." % p for p in depotPaths]):
557 # Return the set of all git tags
560 for line in read_pipe_lines(["git", "tag"]):
565 def diffTreePattern():
566 # This is a simple generator for the diff tree regex pattern. This could be
567 # a class variable if this and parseDiffTreeEntry were a part of a class.
568 pattern = re.compile(':(\d+) (\d+) (\w+) (\w+) ([A-Z])(\d+)?\t(.*?)((\t(.*))|$)')
572 def parseDiffTreeEntry(entry):
573 """Parses a single diff tree entry into its component elements.
575 See git-diff-tree(1) manpage for details about the format of the diff
576 output. This method returns a dictionary with the following elements:
578 src_mode - The mode of the source file
579 dst_mode - The mode of the destination file
580 src_sha1 - The sha1 for the source file
581 dst_sha1 - The sha1 fr the destination file
582 status - The one letter status of the diff (i.e. 'A', 'M', 'D', etc)
583 status_score - The score for the status (applicable for 'C' and 'R'
584 statuses). This is None if there is no score.
585 src - The path for the source file.
586 dst - The path for the destination file. This is only present for
587 copy or renames. If it is not present, this is None.
589 If the pattern is not matched, None is returned."""
591 match = diffTreePattern().next().match(entry)
594 'src_mode': match.group(1),
595 'dst_mode': match.group(2),
596 'src_sha1': match.group(3),
597 'dst_sha1': match.group(4),
598 'status': match.group(5),
599 'status_score': match.group(6),
600 'src': match.group(7),
601 'dst': match.group(10)
605 def isModeExec(mode):
606 # Returns True if the given git mode represents an executable file,
608 return mode[-3:] == "755"
610 class P4Exception(Exception):
611 """ Base class for exceptions from the p4 client """
612 def __init__(self, exit_code):
613 self.p4ExitCode = exit_code
615 class P4ServerException(P4Exception):
616 """ Base class for exceptions where we get some kind of marshalled up result from the server """
617 def __init__(self, exit_code, p4_result):
618 super(P4ServerException, self).__init__(exit_code)
619 self.p4_result = p4_result
620 self.code = p4_result[0]['code']
621 self.data = p4_result[0]['data']
623 class P4RequestSizeException(P4ServerException):
624 """ One of the maxresults or maxscanrows errors """
625 def __init__(self, exit_code, p4_result, limit):
626 super(P4RequestSizeException, self).__init__(exit_code, p4_result)
629 class P4CommandException(P4Exception):
630 """ Something went wrong calling p4 which means we have to give up """
631 def __init__(self, msg):
637 def isModeExecChanged(src_mode, dst_mode):
638 return isModeExec(src_mode) != isModeExec(dst_mode)
640 def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None, skip_info=False,
641 errors_as_exceptions=False):
643 if isinstance(cmd,basestring):
650 cmd = p4_build_cmd(cmd)
652 sys.stderr.write("Opening pipe: %s\n" % str(cmd))
654 # Use a temporary file to avoid deadlocks without
655 # subprocess.communicate(), which would put another copy
656 # of stdout into memory.
658 if stdin is not None:
659 stdin_file = tempfile.TemporaryFile(prefix='p4-stdin', mode=stdin_mode)
660 if isinstance(stdin,basestring):
661 stdin_file.write(stdin)
664 stdin_file.write(i + '\n')
668 p4 = subprocess.Popen(cmd,
671 stdout=subprocess.PIPE)
676 entry = marshal.load(p4.stdout)
678 if 'code' in entry and entry['code'] == 'info':
688 if errors_as_exceptions:
690 data = result[0].get('data')
692 m = re.search('Too many rows scanned \(over (\d+)\)', data)
694 m = re.search('Request too large \(over (\d+)\)', data)
697 limit = int(m.group(1))
698 raise P4RequestSizeException(exitCode, result, limit)
700 raise P4ServerException(exitCode, result)
702 raise P4Exception(exitCode)
705 entry["p4ExitCode"] = exitCode
711 list = p4CmdList(cmd)
717 def p4Where(depotPath):
718 if not depotPath.endswith("/"):
720 depotPathLong = depotPath + "..."
721 outputList = p4CmdList(["where", depotPathLong])
723 for entry in outputList:
724 if "depotFile" in entry:
725 # Search for the base client side depot path, as long as it starts with the branch's P4 path.
726 # The base path always ends with "/...".
727 if entry["depotFile"].find(depotPath) == 0 and entry["depotFile"][-4:] == "/...":
730 elif "data" in entry:
731 data = entry.get("data")
732 space = data.find(" ")
733 if data[:space] == depotPath:
738 if output["code"] == "error":
742 clientPath = output.get("path")
743 elif "data" in output:
744 data = output.get("data")
745 lastSpace = data.rfind(" ")
746 clientPath = data[lastSpace + 1:]
748 if clientPath.endswith("..."):
749 clientPath = clientPath[:-3]
752 def currentGitBranch():
753 return read_pipe_text(["git", "symbolic-ref", "--short", "-q", "HEAD"])
755 def isValidGitDir(path):
756 return git_dir(path) != None
758 def parseRevision(ref):
759 return read_pipe("git rev-parse %s" % ref).strip()
761 def branchExists(ref):
762 rev = read_pipe(["git", "rev-parse", "-q", "--verify", ref],
766 def extractLogMessageFromGitCommit(commit):
769 ## fixme: title is first line of commit, not 1st paragraph.
771 for log in read_pipe_lines(["git", "cat-file", "commit", commit]):
780 def extractSettingsGitLog(log):
782 for line in log.split("\n"):
784 m = re.search (r"^ *\[git-p4: (.*)\]$", line)
788 assignments = m.group(1).split (':')
789 for a in assignments:
791 key = vals[0].strip()
792 val = ('='.join (vals[1:])).strip()
793 if val.endswith ('\"') and val.startswith('"'):
798 paths = values.get("depot-paths")
800 paths = values.get("depot-path")
802 values['depot-paths'] = paths.split(',')
805 def gitBranchExists(branch):
806 proc = subprocess.Popen(["git", "rev-parse", branch],
807 stderr=subprocess.PIPE, stdout=subprocess.PIPE);
808 return proc.wait() == 0;
810 def gitUpdateRef(ref, newvalue):
811 subprocess.check_call(["git", "update-ref", ref, newvalue])
813 def gitDeleteRef(ref):
814 subprocess.check_call(["git", "update-ref", "-d", ref])
818 def gitConfig(key, typeSpecifier=None):
819 if key not in _gitConfig:
820 cmd = [ "git", "config" ]
822 cmd += [ typeSpecifier ]
824 s = read_pipe(cmd, ignore_error=True)
825 _gitConfig[key] = s.strip()
826 return _gitConfig[key]
828 def gitConfigBool(key):
829 """Return a bool, using git config --bool. It is True only if the
830 variable is set to true, and False if set to false or not present
833 if key not in _gitConfig:
834 _gitConfig[key] = gitConfig(key, '--bool') == "true"
835 return _gitConfig[key]
837 def gitConfigInt(key):
838 if key not in _gitConfig:
839 cmd = [ "git", "config", "--int", key ]
840 s = read_pipe(cmd, ignore_error=True)
843 _gitConfig[key] = int(gitConfig(key, '--int'))
845 _gitConfig[key] = None
846 return _gitConfig[key]
848 def gitConfigList(key):
849 if key not in _gitConfig:
850 s = read_pipe(["git", "config", "--get-all", key], ignore_error=True)
851 _gitConfig[key] = s.strip().splitlines()
852 if _gitConfig[key] == ['']:
854 return _gitConfig[key]
856 def p4BranchesInGit(branchesAreInRemotes=True):
857 """Find all the branches whose names start with "p4/", looking
858 in remotes or heads as specified by the argument. Return
859 a dictionary of { branch: revision } for each one found.
860 The branch names are the short names, without any
865 cmdline = "git rev-parse --symbolic "
866 if branchesAreInRemotes:
867 cmdline += "--remotes"
869 cmdline += "--branches"
871 for line in read_pipe_lines(cmdline):
875 if not line.startswith('p4/'):
877 # special symbolic ref to p4/master
878 if line == "p4/HEAD":
881 # strip off p4/ prefix
882 branch = line[len("p4/"):]
884 branches[branch] = parseRevision(line)
888 def branch_exists(branch):
889 """Make sure that the given ref name really exists."""
891 cmd = [ "git", "rev-parse", "--symbolic", "--verify", branch ]
892 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
893 out, _ = p.communicate()
896 # expect exactly one line of output: the branch name
897 return out.rstrip() == branch
899 def findUpstreamBranchPoint(head = "HEAD"):
900 branches = p4BranchesInGit()
901 # map from depot-path to branch name
902 branchByDepotPath = {}
903 for branch in branches.keys():
904 tip = branches[branch]
905 log = extractLogMessageFromGitCommit(tip)
906 settings = extractSettingsGitLog(log)
907 if "depot-paths" in settings:
908 paths = ",".join(settings["depot-paths"])
909 branchByDepotPath[paths] = "remotes/p4/" + branch
913 while parent < 65535:
914 commit = head + "~%s" % parent
915 log = extractLogMessageFromGitCommit(commit)
916 settings = extractSettingsGitLog(log)
917 if "depot-paths" in settings:
918 paths = ",".join(settings["depot-paths"])
919 if paths in branchByDepotPath:
920 return [branchByDepotPath[paths], settings]
924 return ["", settings]
926 def createOrUpdateBranchesFromOrigin(localRefPrefix = "refs/remotes/p4/", silent=True):
928 print("Creating/updating branch(es) in %s based on origin branch(es)"
931 originPrefix = "origin/p4/"
933 for line in read_pipe_lines("git rev-parse --symbolic --remotes"):
935 if (not line.startswith(originPrefix)) or line.endswith("HEAD"):
938 headName = line[len(originPrefix):]
939 remoteHead = localRefPrefix + headName
942 original = extractSettingsGitLog(extractLogMessageFromGitCommit(originHead))
943 if ('depot-paths' not in original
944 or 'change' not in original):
948 if not gitBranchExists(remoteHead):
950 print("creating %s" % remoteHead)
953 settings = extractSettingsGitLog(extractLogMessageFromGitCommit(remoteHead))
954 if 'change' in settings:
955 if settings['depot-paths'] == original['depot-paths']:
956 originP4Change = int(original['change'])
957 p4Change = int(settings['change'])
958 if originP4Change > p4Change:
959 print("%s (%s) is newer than %s (%s). "
960 "Updating p4 branch from origin."
961 % (originHead, originP4Change,
962 remoteHead, p4Change))
965 print("Ignoring: %s was imported from %s while "
966 "%s was imported from %s"
967 % (originHead, ','.join(original['depot-paths']),
968 remoteHead, ','.join(settings['depot-paths'])))
971 system("git update-ref %s %s" % (remoteHead, originHead))
973 def originP4BranchesExist():
974 return gitBranchExists("origin") or gitBranchExists("origin/p4") or gitBranchExists("origin/p4/master")
977 def p4ParseNumericChangeRange(parts):
978 changeStart = int(parts[0][1:])
979 if parts[1] == '#head':
980 changeEnd = p4_last_change()
982 changeEnd = int(parts[1])
984 return (changeStart, changeEnd)
986 def chooseBlockSize(blockSize):
990 return defaultBlockSize
992 def p4ChangesForPaths(depotPaths, changeRange, requestedBlockSize):
995 # Parse the change range into start and end. Try to find integer
996 # revision ranges as these can be broken up into blocks to avoid
997 # hitting server-side limits (maxrows, maxscanresults). But if
998 # that doesn't work, fall back to using the raw revision specifier
999 # strings, without using block mode.
1001 if changeRange is None or changeRange == '':
1003 changeEnd = p4_last_change()
1004 block_size = chooseBlockSize(requestedBlockSize)
1006 parts = changeRange.split(',')
1007 assert len(parts) == 2
1009 (changeStart, changeEnd) = p4ParseNumericChangeRange(parts)
1010 block_size = chooseBlockSize(requestedBlockSize)
1012 changeStart = parts[0][1:]
1013 changeEnd = parts[1]
1014 if requestedBlockSize:
1015 die("cannot use --changes-block-size with non-numeric revisions")
1020 # Retrieve changes a block at a time, to prevent running
1021 # into a MaxResults/MaxScanRows error from the server. If
1022 # we _do_ hit one of those errors, turn down the block size
1028 end = min(changeEnd, changeStart + block_size)
1029 revisionRange = "%d,%d" % (changeStart, end)
1031 revisionRange = "%s,%s" % (changeStart, changeEnd)
1033 for p in depotPaths:
1034 cmd += ["%s...@%s" % (p, revisionRange)]
1038 result = p4CmdList(cmd, errors_as_exceptions=True)
1039 except P4RequestSizeException as e:
1041 block_size = e.limit
1042 elif block_size > e.limit:
1043 block_size = e.limit
1045 block_size = max(2, block_size // 2)
1047 if verbose: print("block size error, retrying with block size {0}".format(block_size))
1049 except P4Exception as e:
1050 die('Error retrieving changes description ({0})'.format(e.p4ExitCode))
1052 # Insert changes in chronological order
1053 for entry in reversed(result):
1054 if 'change' not in entry:
1056 changes.add(int(entry['change']))
1061 if end >= changeEnd:
1064 changeStart = end + 1
1066 changes = sorted(changes)
1069 def p4PathStartsWith(path, prefix):
1070 # This method tries to remedy a potential mixed-case issue:
1072 # If UserA adds //depot/DirA/file1
1073 # and UserB adds //depot/dira/file2
1075 # we may or may not have a problem. If you have core.ignorecase=true,
1076 # we treat DirA and dira as the same directory
1077 if gitConfigBool("core.ignorecase"):
1078 return path.lower().startswith(prefix.lower())
1079 return path.startswith(prefix)
1081 def getClientSpec():
1082 """Look at the p4 client spec, create a View() object that contains
1083 all the mappings, and return it."""
1085 specList = p4CmdList("client -o")
1086 if len(specList) != 1:
1087 die('Output from "client -o" is %d lines, expecting 1' %
1090 # dictionary of all client parameters
1093 # the //client/ name
1094 client_name = entry["Client"]
1096 # just the keys that start with "View"
1097 view_keys = [ k for k in entry.keys() if k.startswith("View") ]
1099 # hold this new View
1100 view = View(client_name)
1102 # append the lines, in order, to the view
1103 for view_num in range(len(view_keys)):
1104 k = "View%d" % view_num
1105 if k not in view_keys:
1106 die("Expected view key %s missing" % k)
1107 view.append(entry[k])
1111 def getClientRoot():
1112 """Grab the client directory."""
1114 output = p4CmdList("client -o")
1115 if len(output) != 1:
1116 die('Output from "client -o" is %d lines, expecting 1' % len(output))
1119 if "Root" not in entry:
1120 die('Client has no "Root"')
1122 return entry["Root"]
1125 # P4 wildcards are not allowed in filenames. P4 complains
1126 # if you simply add them, but you can force it with "-f", in
1127 # which case it translates them into %xx encoding internally.
1129 def wildcard_decode(path):
1130 # Search for and fix just these four characters. Do % last so
1131 # that fixing it does not inadvertently create new %-escapes.
1132 # Cannot have * in a filename in windows; untested as to
1133 # what p4 would do in such a case.
1134 if not platform.system() == "Windows":
1135 path = path.replace("%2A", "*")
1136 path = path.replace("%23", "#") \
1137 .replace("%40", "@") \
1138 .replace("%25", "%")
1141 def wildcard_encode(path):
1142 # do % first to avoid double-encoding the %s introduced here
1143 path = path.replace("%", "%25") \
1144 .replace("*", "%2A") \
1145 .replace("#", "%23") \
1146 .replace("@", "%40")
1149 def wildcard_present(path):
1150 m = re.search("[*#@%]", path)
1151 return m is not None
1153 class LargeFileSystem(object):
1154 """Base class for large file system support."""
1156 def __init__(self, writeToGitStream):
1157 self.largeFiles = set()
1158 self.writeToGitStream = writeToGitStream
1160 def generatePointer(self, cloneDestination, contentFile):
1161 """Return the content of a pointer file that is stored in Git instead of
1162 the actual content."""
1163 assert False, "Method 'generatePointer' required in " + self.__class__.__name__
1165 def pushFile(self, localLargeFile):
1166 """Push the actual content which is not stored in the Git repository to
1168 assert False, "Method 'pushFile' required in " + self.__class__.__name__
1170 def hasLargeFileExtension(self, relPath):
1172 lambda a, b: a or b,
1173 [relPath.endswith('.' + e) for e in gitConfigList('git-p4.largeFileExtensions')],
1177 def generateTempFile(self, contents):
1178 contentFile = tempfile.NamedTemporaryFile(prefix='git-p4-large-file', delete=False)
1180 contentFile.write(d)
1182 return contentFile.name
1184 def exceedsLargeFileThreshold(self, relPath, contents):
1185 if gitConfigInt('git-p4.largeFileThreshold'):
1186 contentsSize = sum(len(d) for d in contents)
1187 if contentsSize > gitConfigInt('git-p4.largeFileThreshold'):
1189 if gitConfigInt('git-p4.largeFileCompressedThreshold'):
1190 contentsSize = sum(len(d) for d in contents)
1191 if contentsSize <= gitConfigInt('git-p4.largeFileCompressedThreshold'):
1193 contentTempFile = self.generateTempFile(contents)
1194 compressedContentFile = tempfile.NamedTemporaryFile(prefix='git-p4-large-file', delete=True)
1195 with zipfile.ZipFile(compressedContentFile, mode='w') as zf:
1196 zf.write(contentTempFile, compress_type=zipfile.ZIP_DEFLATED)
1197 compressedContentsSize = zf.infolist()[0].compress_size
1198 os.remove(contentTempFile)
1199 if compressedContentsSize > gitConfigInt('git-p4.largeFileCompressedThreshold'):
1203 def addLargeFile(self, relPath):
1204 self.largeFiles.add(relPath)
1206 def removeLargeFile(self, relPath):
1207 self.largeFiles.remove(relPath)
1209 def isLargeFile(self, relPath):
1210 return relPath in self.largeFiles
1212 def processContent(self, git_mode, relPath, contents):
1213 """Processes the content of git fast import. This method decides if a
1214 file is stored in the large file system and handles all necessary
1216 if self.exceedsLargeFileThreshold(relPath, contents) or self.hasLargeFileExtension(relPath):
1217 contentTempFile = self.generateTempFile(contents)
1218 (pointer_git_mode, contents, localLargeFile) = self.generatePointer(contentTempFile)
1219 if pointer_git_mode:
1220 git_mode = pointer_git_mode
1222 # Move temp file to final location in large file system
1223 largeFileDir = os.path.dirname(localLargeFile)
1224 if not os.path.isdir(largeFileDir):
1225 os.makedirs(largeFileDir)
1226 shutil.move(contentTempFile, localLargeFile)
1227 self.addLargeFile(relPath)
1228 if gitConfigBool('git-p4.largeFilePush'):
1229 self.pushFile(localLargeFile)
1231 sys.stderr.write("%s moved to large file system (%s)\n" % (relPath, localLargeFile))
1232 return (git_mode, contents)
1234 class MockLFS(LargeFileSystem):
1235 """Mock large file system for testing."""
1237 def generatePointer(self, contentFile):
1238 """The pointer content is the original content prefixed with "pointer-".
1239 The local filename of the large file storage is derived from the file content.
1241 with open(contentFile, 'r') as f:
1244 pointerContents = 'pointer-' + content
1245 localLargeFile = os.path.join(os.getcwd(), '.git', 'mock-storage', 'local', content[:-1])
1246 return (gitMode, pointerContents, localLargeFile)
1248 def pushFile(self, localLargeFile):
1249 """The remote filename of the large file storage is the same as the local
1250 one but in a different directory.
1252 remotePath = os.path.join(os.path.dirname(localLargeFile), '..', 'remote')
1253 if not os.path.exists(remotePath):
1254 os.makedirs(remotePath)
1255 shutil.copyfile(localLargeFile, os.path.join(remotePath, os.path.basename(localLargeFile)))
1257 class GitLFS(LargeFileSystem):
1258 """Git LFS as backend for the git-p4 large file system.
1259 See https://git-lfs.github.com/ for details."""
1261 def __init__(self, *args):
1262 LargeFileSystem.__init__(self, *args)
1263 self.baseGitAttributes = []
1265 def generatePointer(self, contentFile):
1266 """Generate a Git LFS pointer for the content. Return LFS Pointer file
1267 mode and content which is stored in the Git repository instead of
1268 the actual content. Return also the new location of the actual
1271 if os.path.getsize(contentFile) == 0:
1272 return (None, '', None)
1274 pointerProcess = subprocess.Popen(
1275 ['git', 'lfs', 'pointer', '--file=' + contentFile],
1276 stdout=subprocess.PIPE
1278 pointerFile = pointerProcess.stdout.read()
1279 if pointerProcess.wait():
1280 os.remove(contentFile)
1281 die('git-lfs pointer command failed. Did you install the extension?')
1283 # Git LFS removed the preamble in the output of the 'pointer' command
1284 # starting from version 1.2.0. Check for the preamble here to support
1286 # c.f. https://github.com/github/git-lfs/commit/da2935d9a739592bc775c98d8ef4df9c72ea3b43
1287 if pointerFile.startswith('Git LFS pointer for'):
1288 pointerFile = re.sub(r'Git LFS pointer for.*\n\n', '', pointerFile)
1290 oid = re.search(r'^oid \w+:(\w+)', pointerFile, re.MULTILINE).group(1)
1291 # if someone use external lfs.storage ( not in local repo git )
1292 lfs_path = gitConfig('lfs.storage')
1295 if not os.path.isabs(lfs_path):
1296 lfs_path = os.path.join(os.getcwd(), '.git', lfs_path)
1297 localLargeFile = os.path.join(
1299 'objects', oid[:2], oid[2:4],
1302 # LFS Spec states that pointer files should not have the executable bit set.
1304 return (gitMode, pointerFile, localLargeFile)
1306 def pushFile(self, localLargeFile):
1307 uploadProcess = subprocess.Popen(
1308 ['git', 'lfs', 'push', '--object-id', 'origin', os.path.basename(localLargeFile)]
1310 if uploadProcess.wait():
1311 die('git-lfs push command failed. Did you define a remote?')
1313 def generateGitAttributes(self):
1315 self.baseGitAttributes +
1319 '# Git LFS (see https://git-lfs.github.com/)\n',
1322 ['*.' + f.replace(' ', '[[:space:]]') + ' filter=lfs diff=lfs merge=lfs -text\n'
1323 for f in sorted(gitConfigList('git-p4.largeFileExtensions'))
1325 ['/' + f.replace(' ', '[[:space:]]') + ' filter=lfs diff=lfs merge=lfs -text\n'
1326 for f in sorted(self.largeFiles) if not self.hasLargeFileExtension(f)
1330 def addLargeFile(self, relPath):
1331 LargeFileSystem.addLargeFile(self, relPath)
1332 self.writeToGitStream('100644', '.gitattributes', self.generateGitAttributes())
1334 def removeLargeFile(self, relPath):
1335 LargeFileSystem.removeLargeFile(self, relPath)
1336 self.writeToGitStream('100644', '.gitattributes', self.generateGitAttributes())
1338 def processContent(self, git_mode, relPath, contents):
1339 if relPath == '.gitattributes':
1340 self.baseGitAttributes = contents
1341 return (git_mode, self.generateGitAttributes())
1343 return LargeFileSystem.processContent(self, git_mode, relPath, contents)
1346 delete_actions = ( "delete", "move/delete", "purge" )
1347 add_actions = ( "add", "branch", "move/add" )
1350 self.usage = "usage: %prog [options]"
1351 self.needsGit = True
1352 self.verbose = False
1354 # This is required for the "append" update_shelve action
1355 def ensure_value(self, attr, value):
1356 if not hasattr(self, attr) or getattr(self, attr) is None:
1357 setattr(self, attr, value)
1358 return getattr(self, attr)
1362 self.userMapFromPerforceServer = False
1363 self.myP4UserId = None
1367 return self.myP4UserId
1369 results = p4CmdList("user -o")
1372 self.myP4UserId = r['User']
1374 die("Could not find your p4 user id")
1376 def p4UserIsMe(self, p4User):
1377 # return True if the given p4 user is actually me
1378 me = self.p4UserId()
1379 if not p4User or p4User != me:
1384 def getUserCacheFilename(self):
1385 home = os.environ.get("HOME", os.environ.get("USERPROFILE"))
1386 return home + "/.gitp4-usercache.txt"
1388 def getUserMapFromPerforceServer(self):
1389 if self.userMapFromPerforceServer:
1394 for output in p4CmdList("users"):
1395 if "User" not in output:
1397 self.users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">"
1398 self.emails[output["Email"]] = output["User"]
1400 mapUserConfigRegex = re.compile(r"^\s*(\S+)\s*=\s*(.+)\s*<(\S+)>\s*$", re.VERBOSE)
1401 for mapUserConfig in gitConfigList("git-p4.mapUser"):
1402 mapUser = mapUserConfigRegex.findall(mapUserConfig)
1403 if mapUser and len(mapUser[0]) == 3:
1404 user = mapUser[0][0]
1405 fullname = mapUser[0][1]
1406 email = mapUser[0][2]
1407 self.users[user] = fullname + " <" + email + ">"
1408 self.emails[email] = user
1411 for (key, val) in self.users.items():
1412 s += "%s\t%s\n" % (key.expandtabs(1), val.expandtabs(1))
1414 open(self.getUserCacheFilename(), "wb").write(s)
1415 self.userMapFromPerforceServer = True
1417 def loadUserMapFromCache(self):
1419 self.userMapFromPerforceServer = False
1421 cache = open(self.getUserCacheFilename(), "rb")
1422 lines = cache.readlines()
1425 entry = line.strip().split("\t")
1426 self.users[entry[0]] = entry[1]
1428 self.getUserMapFromPerforceServer()
1430 class P4Debug(Command):
1432 Command.__init__(self)
1434 self.description = "A tool to debug the output of p4 -G."
1435 self.needsGit = False
1437 def run(self, args):
1439 for output in p4CmdList(args):
1440 print('Element: %d' % j)
1445 class P4RollBack(Command):
1447 Command.__init__(self)
1449 optparse.make_option("--local", dest="rollbackLocalBranches", action="store_true")
1451 self.description = "A tool to debug the multi-branch import. Don't use :)"
1452 self.rollbackLocalBranches = False
1454 def run(self, args):
1457 maxChange = int(args[0])
1459 if "p4ExitCode" in p4Cmd("changes -m 1"):
1460 die("Problems executing p4");
1462 if self.rollbackLocalBranches:
1463 refPrefix = "refs/heads/"
1464 lines = read_pipe_lines("git rev-parse --symbolic --branches")
1466 refPrefix = "refs/remotes/"
1467 lines = read_pipe_lines("git rev-parse --symbolic --remotes")
1470 if self.rollbackLocalBranches or (line.startswith("p4/") and line != "p4/HEAD\n"):
1472 ref = refPrefix + line
1473 log = extractLogMessageFromGitCommit(ref)
1474 settings = extractSettingsGitLog(log)
1476 depotPaths = settings['depot-paths']
1477 change = settings['change']
1481 if len(p4Cmd("changes -m 1 " + ' '.join (['%s...@%s' % (p, maxChange)
1482 for p in depotPaths]))) == 0:
1483 print("Branch %s did not exist at change %s, deleting." % (ref, maxChange))
1484 system("git update-ref -d %s `git rev-parse %s`" % (ref, ref))
1487 while change and int(change) > maxChange:
1490 print("%s is at %s ; rewinding towards %s" % (ref, change, maxChange))
1491 system("git update-ref %s \"%s^\"" % (ref, ref))
1492 log = extractLogMessageFromGitCommit(ref)
1493 settings = extractSettingsGitLog(log)
1496 depotPaths = settings['depot-paths']
1497 change = settings['change']
1500 print("%s rewound to %s" % (ref, change))
1504 class P4Submit(Command, P4UserMap):
1506 conflict_behavior_choices = ("ask", "skip", "quit")
1509 Command.__init__(self)
1510 P4UserMap.__init__(self)
1512 optparse.make_option("--origin", dest="origin"),
1513 optparse.make_option("-M", dest="detectRenames", action="store_true"),
1514 # preserve the user, requires relevant p4 permissions
1515 optparse.make_option("--preserve-user", dest="preserveUser", action="store_true"),
1516 optparse.make_option("--export-labels", dest="exportLabels", action="store_true"),
1517 optparse.make_option("--dry-run", "-n", dest="dry_run", action="store_true"),
1518 optparse.make_option("--prepare-p4-only", dest="prepare_p4_only", action="store_true"),
1519 optparse.make_option("--conflict", dest="conflict_behavior",
1520 choices=self.conflict_behavior_choices),
1521 optparse.make_option("--branch", dest="branch"),
1522 optparse.make_option("--shelve", dest="shelve", action="store_true",
1523 help="Shelve instead of submit. Shelved files are reverted, "
1524 "restoring the workspace to the state before the shelve"),
1525 optparse.make_option("--update-shelve", dest="update_shelve", action="append", type="int",
1526 metavar="CHANGELIST",
1527 help="update an existing shelved changelist, implies --shelve, "
1528 "repeat in-order for multiple shelved changelists"),
1529 optparse.make_option("--commit", dest="commit", metavar="COMMIT",
1530 help="submit only the specified commit(s), one commit or xxx..xxx"),
1531 optparse.make_option("--disable-rebase", dest="disable_rebase", action="store_true",
1532 help="Disable rebase after submit is completed. Can be useful if you "
1533 "work from a local git branch that is not master"),
1534 optparse.make_option("--disable-p4sync", dest="disable_p4sync", action="store_true",
1535 help="Skip Perforce sync of p4/master after submit or shelve"),
1537 self.description = """Submit changes from git to the perforce depot.\n
1538 The `p4-pre-submit` hook is executed if it exists and is executable.
1539 The hook takes no parameters and nothing from standard input. Exiting with
1540 non-zero status from this script prevents `git-p4 submit` from launching.
1542 One usage scenario is to run unit tests in the hook."""
1544 self.usage += " [name of git branch to submit into perforce depot]"
1546 self.detectRenames = False
1547 self.preserveUser = gitConfigBool("git-p4.preserveUser")
1548 self.dry_run = False
1550 self.update_shelve = list()
1552 self.disable_rebase = gitConfigBool("git-p4.disableRebase")
1553 self.disable_p4sync = gitConfigBool("git-p4.disableP4Sync")
1554 self.prepare_p4_only = False
1555 self.conflict_behavior = None
1556 self.isWindows = (platform.system() == "Windows")
1557 self.exportLabels = False
1558 self.p4HasMoveCommand = p4_has_move_command()
1561 if gitConfig('git-p4.largeFileSystem'):
1562 die("Large file system not supported for git-p4 submit command. Please remove it from config.")
1565 if len(p4CmdList("opened ...")) > 0:
1566 die("You have files opened with perforce! Close them before starting the sync.")
1568 def separate_jobs_from_description(self, message):
1569 """Extract and return a possible Jobs field in the commit
1570 message. It goes into a separate section in the p4 change
1573 A jobs line starts with "Jobs:" and looks like a new field
1574 in a form. Values are white-space separated on the same
1575 line or on following lines that start with a tab.
1577 This does not parse and extract the full git commit message
1578 like a p4 form. It just sees the Jobs: line as a marker
1579 to pass everything from then on directly into the p4 form,
1580 but outside the description section.
1582 Return a tuple (stripped log message, jobs string)."""
1584 m = re.search(r'^Jobs:', message, re.MULTILINE)
1586 return (message, None)
1588 jobtext = message[m.start():]
1589 stripped_message = message[:m.start()].rstrip()
1590 return (stripped_message, jobtext)
1592 def prepareLogMessage(self, template, message, jobs):
1593 """Edits the template returned from "p4 change -o" to insert
1594 the message in the Description field, and the jobs text in
1598 inDescriptionSection = False
1600 for line in template.split("\n"):
1601 if line.startswith("#"):
1602 result += line + "\n"
1605 if inDescriptionSection:
1606 if line.startswith("Files:") or line.startswith("Jobs:"):
1607 inDescriptionSection = False
1608 # insert Jobs section
1610 result += jobs + "\n"
1614 if line.startswith("Description:"):
1615 inDescriptionSection = True
1617 for messageLine in message.split("\n"):
1618 line += "\t" + messageLine + "\n"
1620 result += line + "\n"
1624 def patchRCSKeywords(self, file, pattern):
1625 # Attempt to zap the RCS keywords in a p4 controlled file matching the given pattern
1626 (handle, outFileName) = tempfile.mkstemp(dir='.')
1628 outFile = os.fdopen(handle, "w+")
1629 inFile = open(file, "r")
1630 regexp = re.compile(pattern, re.VERBOSE)
1631 for line in inFile.readlines():
1632 line = regexp.sub(r'$\1$', line)
1636 # Forcibly overwrite the original file
1638 shutil.move(outFileName, file)
1640 # cleanup our temporary file
1641 os.unlink(outFileName)
1642 print("Failed to strip RCS keywords in %s" % file)
1645 print("Patched up RCS keywords in %s" % file)
1647 def p4UserForCommit(self,id):
1648 # Return the tuple (perforce user,git email) for a given git commit id
1649 self.getUserMapFromPerforceServer()
1650 gitEmail = read_pipe(["git", "log", "--max-count=1",
1651 "--format=%ae", id])
1652 gitEmail = gitEmail.strip()
1653 if gitEmail not in self.emails:
1654 return (None,gitEmail)
1656 return (self.emails[gitEmail],gitEmail)
1658 def checkValidP4Users(self,commits):
1659 # check if any git authors cannot be mapped to p4 users
1661 (user,email) = self.p4UserForCommit(id)
1663 msg = "Cannot find p4 user for email %s in commit %s." % (email, id)
1664 if gitConfigBool("git-p4.allowMissingP4Users"):
1667 die("Error: %s\nSet git-p4.allowMissingP4Users to true to allow this." % msg)
1669 def lastP4Changelist(self):
1670 # Get back the last changelist number submitted in this client spec. This
1671 # then gets used to patch up the username in the change. If the same
1672 # client spec is being used by multiple processes then this might go
1674 results = p4CmdList("client -o") # find the current client
1678 client = r['Client']
1681 die("could not get client spec")
1682 results = p4CmdList(["changes", "-c", client, "-m", "1"])
1686 die("Could not get changelist number for last submit - cannot patch up user details")
1688 def modifyChangelistUser(self, changelist, newUser):
1689 # fixup the user field of a changelist after it has been submitted.
1690 changes = p4CmdList("change -o %s" % changelist)
1691 if len(changes) != 1:
1692 die("Bad output from p4 change modifying %s to user %s" %
1693 (changelist, newUser))
1696 if c['User'] == newUser: return # nothing to do
1698 input = marshal.dumps(c)
1700 result = p4CmdList("change -f -i", stdin=input)
1703 if r['code'] == 'error':
1704 die("Could not modify user field of changelist %s to %s:%s" % (changelist, newUser, r['data']))
1706 print("Updated user field for changelist %s to %s" % (changelist, newUser))
1708 die("Could not modify user field of changelist %s to %s" % (changelist, newUser))
1710 def canChangeChangelists(self):
1711 # check to see if we have p4 admin or super-user permissions, either of
1712 # which are required to modify changelists.
1713 results = p4CmdList(["protects", self.depotPath])
1716 if r['perm'] == 'admin':
1718 if r['perm'] == 'super':
1722 def prepareSubmitTemplate(self, changelist=None):
1723 """Run "p4 change -o" to grab a change specification template.
1724 This does not use "p4 -G", as it is nice to keep the submission
1725 template in original order, since a human might edit it.
1727 Remove lines in the Files section that show changes to files
1728 outside the depot path we're committing into."""
1730 [upstream, settings] = findUpstreamBranchPoint()
1733 # A Perforce Change Specification.
1735 # Change: The change number. 'new' on a new changelist.
1736 # Date: The date this specification was last modified.
1737 # Client: The client on which the changelist was created. Read-only.
1738 # User: The user who created the changelist.
1739 # Status: Either 'pending' or 'submitted'. Read-only.
1740 # Type: Either 'public' or 'restricted'. Default is 'public'.
1741 # Description: Comments about the changelist. Required.
1742 # Jobs: What opened jobs are to be closed by this changelist.
1743 # You may delete jobs from this list. (New changelists only.)
1744 # Files: What opened files from the default changelist are to be added
1745 # to this changelist. You may delete files from this list.
1746 # (New changelists only.)
1749 inFilesSection = False
1751 args = ['change', '-o']
1753 args.append(str(changelist))
1754 for entry in p4CmdList(args):
1755 if 'code' not in entry:
1757 if entry['code'] == 'stat':
1758 change_entry = entry
1760 if not change_entry:
1761 die('Failed to decode output of p4 change -o')
1762 for key, value in change_entry.iteritems():
1763 if key.startswith('File'):
1764 if 'depot-paths' in settings:
1765 if not [p for p in settings['depot-paths']
1766 if p4PathStartsWith(value, p)]:
1769 if not p4PathStartsWith(value, self.depotPath):
1771 files_list.append(value)
1773 # Output in the order expected by prepareLogMessage
1774 for key in ['Change', 'Client', 'User', 'Status', 'Description', 'Jobs']:
1775 if key not in change_entry:
1778 template += key + ':'
1779 if key == 'Description':
1781 for field_line in change_entry[key].splitlines():
1782 template += '\t'+field_line+'\n'
1783 if len(files_list) > 0:
1785 template += 'Files:\n'
1786 for path in files_list:
1787 template += '\t'+path+'\n'
1790 def edit_template(self, template_file):
1791 """Invoke the editor to let the user change the submission
1792 message. Return true if okay to continue with the submit."""
1794 # if configured to skip the editing part, just submit
1795 if gitConfigBool("git-p4.skipSubmitEdit"):
1798 # look at the modification time, to check later if the user saved
1800 mtime = os.stat(template_file).st_mtime
1803 if "P4EDITOR" in os.environ and (os.environ.get("P4EDITOR") != ""):
1804 editor = os.environ.get("P4EDITOR")
1806 editor = read_pipe("git var GIT_EDITOR").strip()
1807 system(["sh", "-c", ('%s "$@"' % editor), editor, template_file])
1809 # If the file was not saved, prompt to see if this patch should
1810 # be skipped. But skip this verification step if configured so.
1811 if gitConfigBool("git-p4.skipSubmitEditCheck"):
1814 # modification time updated means user saved the file
1815 if os.stat(template_file).st_mtime > mtime:
1818 response = prompt("Submit template unchanged. Submit anyway? [y]es, [n]o (skip this patch) ")
1824 def get_diff_description(self, editedFiles, filesToAdd, symlinks):
1826 if "P4DIFF" in os.environ:
1827 del(os.environ["P4DIFF"])
1829 for editedFile in editedFiles:
1830 diff += p4_read_pipe(['diff', '-du',
1831 wildcard_encode(editedFile)])
1835 for newFile in filesToAdd:
1836 newdiff += "==== new file ====\n"
1837 newdiff += "--- /dev/null\n"
1838 newdiff += "+++ %s\n" % newFile
1840 is_link = os.path.islink(newFile)
1841 expect_link = newFile in symlinks
1843 if is_link and expect_link:
1844 newdiff += "+%s\n" % os.readlink(newFile)
1846 f = open(newFile, "r")
1847 for line in f.readlines():
1848 newdiff += "+" + line
1851 return (diff + newdiff).replace('\r\n', '\n')
1853 def applyCommit(self, id):
1854 """Apply one commit, return True if it succeeded."""
1856 print("Applying", read_pipe(["git", "show", "-s",
1857 "--format=format:%h %s", id]))
1859 (p4User, gitEmail) = self.p4UserForCommit(id)
1861 diff = read_pipe_lines("git diff-tree -r %s \"%s^\" \"%s\"" % (self.diffOpts, id, id))
1863 filesToChangeType = set()
1864 filesToDelete = set()
1866 pureRenameCopy = set()
1868 filesToChangeExecBit = {}
1872 diff = parseDiffTreeEntry(line)
1873 modifier = diff['status']
1875 all_files.append(path)
1879 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
1880 filesToChangeExecBit[path] = diff['dst_mode']
1881 editedFiles.add(path)
1882 elif modifier == "A":
1883 filesToAdd.add(path)
1884 filesToChangeExecBit[path] = diff['dst_mode']
1885 if path in filesToDelete:
1886 filesToDelete.remove(path)
1888 dst_mode = int(diff['dst_mode'], 8)
1889 if dst_mode == 0o120000:
1892 elif modifier == "D":
1893 filesToDelete.add(path)
1894 if path in filesToAdd:
1895 filesToAdd.remove(path)
1896 elif modifier == "C":
1897 src, dest = diff['src'], diff['dst']
1898 all_files.append(dest)
1899 p4_integrate(src, dest)
1900 pureRenameCopy.add(dest)
1901 if diff['src_sha1'] != diff['dst_sha1']:
1903 pureRenameCopy.discard(dest)
1904 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
1906 pureRenameCopy.discard(dest)
1907 filesToChangeExecBit[dest] = diff['dst_mode']
1909 # turn off read-only attribute
1910 os.chmod(dest, stat.S_IWRITE)
1912 editedFiles.add(dest)
1913 elif modifier == "R":
1914 src, dest = diff['src'], diff['dst']
1915 all_files.append(dest)
1916 if self.p4HasMoveCommand:
1917 p4_edit(src) # src must be open before move
1918 p4_move(src, dest) # opens for (move/delete, move/add)
1920 p4_integrate(src, dest)
1921 if diff['src_sha1'] != diff['dst_sha1']:
1924 pureRenameCopy.add(dest)
1925 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
1926 if not self.p4HasMoveCommand:
1927 p4_edit(dest) # with move: already open, writable
1928 filesToChangeExecBit[dest] = diff['dst_mode']
1929 if not self.p4HasMoveCommand:
1931 os.chmod(dest, stat.S_IWRITE)
1933 filesToDelete.add(src)
1934 editedFiles.add(dest)
1935 elif modifier == "T":
1936 filesToChangeType.add(path)
1938 die("unknown modifier %s for %s" % (modifier, path))
1940 diffcmd = "git diff-tree --full-index -p \"%s\"" % (id)
1941 patchcmd = diffcmd + " | git apply "
1942 tryPatchCmd = patchcmd + "--check -"
1943 applyPatchCmd = patchcmd + "--check --apply -"
1944 patch_succeeded = True
1946 if os.system(tryPatchCmd) != 0:
1947 fixed_rcs_keywords = False
1948 patch_succeeded = False
1949 print("Unfortunately applying the change failed!")
1951 # Patch failed, maybe it's just RCS keyword woes. Look through
1952 # the patch to see if that's possible.
1953 if gitConfigBool("git-p4.attemptRCSCleanup"):
1957 for file in editedFiles | filesToDelete:
1958 # did this file's delta contain RCS keywords?
1959 pattern = p4_keywords_regexp_for_file(file)
1962 # this file is a possibility...look for RCS keywords.
1963 regexp = re.compile(pattern, re.VERBOSE)
1964 for line in read_pipe_lines(["git", "diff", "%s^..%s" % (id, id), file]):
1965 if regexp.search(line):
1967 print("got keyword match on %s in %s in %s" % (pattern, line, file))
1968 kwfiles[file] = pattern
1971 for file in kwfiles:
1973 print("zapping %s with %s" % (line,pattern))
1974 # File is being deleted, so not open in p4. Must
1975 # disable the read-only bit on windows.
1976 if self.isWindows and file not in editedFiles:
1977 os.chmod(file, stat.S_IWRITE)
1978 self.patchRCSKeywords(file, kwfiles[file])
1979 fixed_rcs_keywords = True
1981 if fixed_rcs_keywords:
1982 print("Retrying the patch with RCS keywords cleaned up")
1983 if os.system(tryPatchCmd) == 0:
1984 patch_succeeded = True
1986 if not patch_succeeded:
1987 for f in editedFiles:
1992 # Apply the patch for real, and do add/delete/+x handling.
1994 system(applyPatchCmd)
1996 for f in filesToChangeType:
1997 p4_edit(f, "-t", "auto")
1998 for f in filesToAdd:
2000 for f in filesToDelete:
2004 # Set/clear executable bits
2005 for f in filesToChangeExecBit.keys():
2006 mode = filesToChangeExecBit[f]
2007 setP4ExecBit(f, mode)
2010 if len(self.update_shelve) > 0:
2011 update_shelve = self.update_shelve.pop(0)
2012 p4_reopen_in_change(update_shelve, all_files)
2015 # Build p4 change description, starting with the contents
2016 # of the git commit message.
2018 logMessage = extractLogMessageFromGitCommit(id)
2019 logMessage = logMessage.strip()
2020 (logMessage, jobs) = self.separate_jobs_from_description(logMessage)
2022 template = self.prepareSubmitTemplate(update_shelve)
2023 submitTemplate = self.prepareLogMessage(template, logMessage, jobs)
2025 if self.preserveUser:
2026 submitTemplate += "\n######## Actual user %s, modified after commit\n" % p4User
2028 if self.checkAuthorship and not self.p4UserIsMe(p4User):
2029 submitTemplate += "######## git author %s does not match your p4 account.\n" % gitEmail
2030 submitTemplate += "######## Use option --preserve-user to modify authorship.\n"
2031 submitTemplate += "######## Variable git-p4.skipUserNameCheck hides this message.\n"
2033 separatorLine = "######## everything below this line is just the diff #######\n"
2034 if not self.prepare_p4_only:
2035 submitTemplate += separatorLine
2036 submitTemplate += self.get_diff_description(editedFiles, filesToAdd, symlinks)
2038 (handle, fileName) = tempfile.mkstemp()
2039 tmpFile = os.fdopen(handle, "w+b")
2041 submitTemplate = submitTemplate.replace("\n", "\r\n")
2042 tmpFile.write(submitTemplate)
2045 if self.prepare_p4_only:
2047 # Leave the p4 tree prepared, and the submit template around
2048 # and let the user decide what to do next
2051 print("P4 workspace prepared for submission.")
2052 print("To submit or revert, go to client workspace")
2053 print(" " + self.clientPath)
2055 print("To submit, use \"p4 submit\" to write a new description,")
2056 print("or \"p4 submit -i <%s\" to use the one prepared by" \
2057 " \"git p4\"." % fileName)
2058 print("You can delete the file \"%s\" when finished." % fileName)
2060 if self.preserveUser and p4User and not self.p4UserIsMe(p4User):
2061 print("To preserve change ownership by user %s, you must\n" \
2062 "do \"p4 change -f <change>\" after submitting and\n" \
2063 "edit the User field.")
2065 print("After submitting, renamed files must be re-synced.")
2066 print("Invoke \"p4 sync -f\" on each of these files:")
2067 for f in pureRenameCopy:
2071 print("To revert the changes, use \"p4 revert ...\", and delete")
2072 print("the submit template file \"%s\"" % fileName)
2074 print("Since the commit adds new files, they must be deleted:")
2075 for f in filesToAdd:
2081 # Let the user edit the change description, then submit it.
2086 if self.edit_template(fileName):
2087 # read the edited message and submit
2088 tmpFile = open(fileName, "rb")
2089 message = tmpFile.read()
2092 message = message.replace("\r\n", "\n")
2093 submitTemplate = message[:message.index(separatorLine)]
2096 p4_write_pipe(['shelve', '-r', '-i'], submitTemplate)
2098 p4_write_pipe(['shelve', '-i'], submitTemplate)
2100 p4_write_pipe(['submit', '-i'], submitTemplate)
2101 # The rename/copy happened by applying a patch that created a
2102 # new file. This leaves it writable, which confuses p4.
2103 for f in pureRenameCopy:
2106 if self.preserveUser:
2108 # Get last changelist number. Cannot easily get it from
2109 # the submit command output as the output is
2111 changelist = self.lastP4Changelist()
2112 self.modifyChangelistUser(changelist, p4User)
2118 if not submitted or self.shelve:
2120 print ("Reverting shelved files.")
2122 print ("Submission cancelled, undoing p4 changes.")
2123 for f in editedFiles | filesToDelete:
2125 for f in filesToAdd:
2132 # Export git tags as p4 labels. Create a p4 label and then tag
2134 def exportGitTags(self, gitTags):
2135 validLabelRegexp = gitConfig("git-p4.labelExportRegexp")
2136 if len(validLabelRegexp) == 0:
2137 validLabelRegexp = defaultLabelRegexp
2138 m = re.compile(validLabelRegexp)
2140 for name in gitTags:
2142 if not m.match(name):
2144 print("tag %s does not match regexp %s" % (name, validLabelRegexp))
2147 # Get the p4 commit this corresponds to
2148 logMessage = extractLogMessageFromGitCommit(name)
2149 values = extractSettingsGitLog(logMessage)
2151 if 'change' not in values:
2152 # a tag pointing to something not sent to p4; ignore
2154 print("git tag %s does not give a p4 commit" % name)
2157 changelist = values['change']
2159 # Get the tag details.
2163 for l in read_pipe_lines(["git", "cat-file", "-p", name]):
2166 if re.match(r'tag\s+', l):
2168 elif re.match(r'\s*$', l):
2175 body = ["lightweight tag imported by git p4\n"]
2177 # Create the label - use the same view as the client spec we are using
2178 clientSpec = getClientSpec()
2180 labelTemplate = "Label: %s\n" % name
2181 labelTemplate += "Description:\n"
2183 labelTemplate += "\t" + b + "\n"
2184 labelTemplate += "View:\n"
2185 for depot_side in clientSpec.mappings:
2186 labelTemplate += "\t%s\n" % depot_side
2189 print("Would create p4 label %s for tag" % name)
2190 elif self.prepare_p4_only:
2191 print("Not creating p4 label %s for tag due to option" \
2192 " --prepare-p4-only" % name)
2194 p4_write_pipe(["label", "-i"], labelTemplate)
2197 p4_system(["tag", "-l", name] +
2198 ["%s@%s" % (depot_side, changelist) for depot_side in clientSpec.mappings])
2201 print("created p4 label for tag %s" % name)
2203 def run(self, args):
2205 self.master = currentGitBranch()
2206 elif len(args) == 1:
2207 self.master = args[0]
2208 if not branchExists(self.master):
2209 die("Branch %s does not exist" % self.master)
2213 for i in self.update_shelve:
2215 sys.exit("invalid changelist %d" % i)
2218 allowSubmit = gitConfig("git-p4.allowSubmit")
2219 if len(allowSubmit) > 0 and not self.master in allowSubmit.split(","):
2220 die("%s is not in git-p4.allowSubmit" % self.master)
2222 [upstream, settings] = findUpstreamBranchPoint()
2223 self.depotPath = settings['depot-paths'][0]
2224 if len(self.origin) == 0:
2225 self.origin = upstream
2227 if len(self.update_shelve) > 0:
2230 if self.preserveUser:
2231 if not self.canChangeChangelists():
2232 die("Cannot preserve user names without p4 super-user or admin permissions")
2234 # if not set from the command line, try the config file
2235 if self.conflict_behavior is None:
2236 val = gitConfig("git-p4.conflict")
2238 if val not in self.conflict_behavior_choices:
2239 die("Invalid value '%s' for config git-p4.conflict" % val)
2242 self.conflict_behavior = val
2245 print("Origin branch is " + self.origin)
2247 if len(self.depotPath) == 0:
2248 print("Internal error: cannot locate perforce depot path from existing branches")
2251 self.useClientSpec = False
2252 if gitConfigBool("git-p4.useclientspec"):
2253 self.useClientSpec = True
2254 if self.useClientSpec:
2255 self.clientSpecDirs = getClientSpec()
2257 # Check for the existence of P4 branches
2258 branchesDetected = (len(p4BranchesInGit().keys()) > 1)
2260 if self.useClientSpec and not branchesDetected:
2261 # all files are relative to the client spec
2262 self.clientPath = getClientRoot()
2264 self.clientPath = p4Where(self.depotPath)
2266 if self.clientPath == "":
2267 die("Error: Cannot locate perforce checkout of %s in client view" % self.depotPath)
2269 print("Perforce checkout for depot path %s located at %s" % (self.depotPath, self.clientPath))
2270 self.oldWorkingDirectory = os.getcwd()
2272 # ensure the clientPath exists
2273 new_client_dir = False
2274 if not os.path.exists(self.clientPath):
2275 new_client_dir = True
2276 os.makedirs(self.clientPath)
2278 chdir(self.clientPath, is_client_path=True)
2280 print("Would synchronize p4 checkout in %s" % self.clientPath)
2282 print("Synchronizing p4 checkout...")
2284 # old one was destroyed, and maybe nobody told p4
2285 p4_sync("...", "-f")
2292 committish = self.master
2296 if self.commit != "":
2297 if self.commit.find("..") != -1:
2298 limits_ish = self.commit.split("..")
2299 for line in read_pipe_lines(["git", "rev-list", "--no-merges", "%s..%s" % (limits_ish[0], limits_ish[1])]):
2300 commits.append(line.strip())
2303 commits.append(self.commit)
2305 for line in read_pipe_lines(["git", "rev-list", "--no-merges", "%s..%s" % (self.origin, committish)]):
2306 commits.append(line.strip())
2309 if self.preserveUser or gitConfigBool("git-p4.skipUserNameCheck"):
2310 self.checkAuthorship = False
2312 self.checkAuthorship = True
2314 if self.preserveUser:
2315 self.checkValidP4Users(commits)
2318 # Build up a set of options to be passed to diff when
2319 # submitting each commit to p4.
2321 if self.detectRenames:
2322 # command-line -M arg
2323 self.diffOpts = "-M"
2325 # If not explicitly set check the config variable
2326 detectRenames = gitConfig("git-p4.detectRenames")
2328 if detectRenames.lower() == "false" or detectRenames == "":
2330 elif detectRenames.lower() == "true":
2331 self.diffOpts = "-M"
2333 self.diffOpts = "-M%s" % detectRenames
2335 # no command-line arg for -C or --find-copies-harder, just
2337 detectCopies = gitConfig("git-p4.detectCopies")
2338 if detectCopies.lower() == "false" or detectCopies == "":
2340 elif detectCopies.lower() == "true":
2341 self.diffOpts += " -C"
2343 self.diffOpts += " -C%s" % detectCopies
2345 if gitConfigBool("git-p4.detectCopiesHarder"):
2346 self.diffOpts += " --find-copies-harder"
2348 num_shelves = len(self.update_shelve)
2349 if num_shelves > 0 and num_shelves != len(commits):
2350 sys.exit("number of commits (%d) must match number of shelved changelist (%d)" %
2351 (len(commits), num_shelves))
2353 hooks_path = gitConfig("core.hooksPath")
2354 if len(hooks_path) <= 0:
2355 hooks_path = os.path.join(os.environ.get("GIT_DIR", ".git"), "hooks")
2357 hook_file = os.path.join(hooks_path, "p4-pre-submit")
2358 if os.path.isfile(hook_file) and os.access(hook_file, os.X_OK) and subprocess.call([hook_file]) != 0:
2362 # Apply the commits, one at a time. On failure, ask if should
2363 # continue to try the rest of the patches, or quit.
2366 print("Would apply")
2368 last = len(commits) - 1
2369 for i, commit in enumerate(commits):
2371 print(" ", read_pipe(["git", "show", "-s",
2372 "--format=format:%h %s", commit]))
2375 ok = self.applyCommit(commit)
2377 applied.append(commit)
2379 if self.prepare_p4_only and i < last:
2380 print("Processing only the first commit due to option" \
2381 " --prepare-p4-only")
2384 # prompt for what to do, or use the option/variable
2385 if self.conflict_behavior == "ask":
2386 print("What do you want to do?")
2387 response = prompt("[s]kip this commit but apply the rest, or [q]uit? ")
2388 elif self.conflict_behavior == "skip":
2390 elif self.conflict_behavior == "quit":
2393 die("Unknown conflict_behavior '%s'" %
2394 self.conflict_behavior)
2397 print("Skipping this commit, but applying the rest")
2402 chdir(self.oldWorkingDirectory)
2403 shelved_applied = "shelved" if self.shelve else "applied"
2406 elif self.prepare_p4_only:
2408 elif len(commits) == len(applied):
2409 print("All commits {0}!".format(shelved_applied))
2413 sync.branch = self.branch
2414 if self.disable_p4sync:
2415 sync.sync_origin_only()
2419 if not self.disable_rebase:
2424 if len(applied) == 0:
2425 print("No commits {0}.".format(shelved_applied))
2427 print("{0} only the commits marked with '*':".format(shelved_applied.capitalize()))
2433 print(star, read_pipe(["git", "show", "-s",
2434 "--format=format:%h %s", c]))
2435 print("You will have to do 'git p4 sync' and rebase.")
2437 if gitConfigBool("git-p4.exportLabels"):
2438 self.exportLabels = True
2440 if self.exportLabels:
2441 p4Labels = getP4Labels(self.depotPath)
2442 gitTags = getGitTags()
2444 missingGitTags = gitTags - p4Labels
2445 self.exportGitTags(missingGitTags)
2447 # exit with error unless everything applied perfectly
2448 if len(commits) != len(applied):
2454 """Represent a p4 view ("p4 help views"), and map files in a
2455 repo according to the view."""
2457 def __init__(self, client_name):
2459 self.client_prefix = "//%s/" % client_name
2460 # cache results of "p4 where" to lookup client file locations
2461 self.client_spec_path_cache = {}
2463 def append(self, view_line):
2464 """Parse a view line, splitting it into depot and client
2465 sides. Append to self.mappings, preserving order. This
2466 is only needed for tag creation."""
2468 # Split the view line into exactly two words. P4 enforces
2469 # structure on these lines that simplifies this quite a bit.
2471 # Either or both words may be double-quoted.
2472 # Single quotes do not matter.
2473 # Double-quote marks cannot occur inside the words.
2474 # A + or - prefix is also inside the quotes.
2475 # There are no quotes unless they contain a space.
2476 # The line is already white-space stripped.
2477 # The two words are separated by a single space.
2479 if view_line[0] == '"':
2480 # First word is double quoted. Find its end.
2481 close_quote_index = view_line.find('"', 1)
2482 if close_quote_index <= 0:
2483 die("No first-word closing quote found: %s" % view_line)
2484 depot_side = view_line[1:close_quote_index]
2485 # skip closing quote and space
2486 rhs_index = close_quote_index + 1 + 1
2488 space_index = view_line.find(" ")
2489 if space_index <= 0:
2490 die("No word-splitting space found: %s" % view_line)
2491 depot_side = view_line[0:space_index]
2492 rhs_index = space_index + 1
2494 # prefix + means overlay on previous mapping
2495 if depot_side.startswith("+"):
2496 depot_side = depot_side[1:]
2498 # prefix - means exclude this path, leave out of mappings
2500 if depot_side.startswith("-"):
2502 depot_side = depot_side[1:]
2505 self.mappings.append(depot_side)
2507 def convert_client_path(self, clientFile):
2508 # chop off //client/ part to make it relative
2509 if not clientFile.startswith(self.client_prefix):
2510 die("No prefix '%s' on clientFile '%s'" %
2511 (self.client_prefix, clientFile))
2512 return clientFile[len(self.client_prefix):]
2514 def update_client_spec_path_cache(self, files):
2515 """ Caching file paths by "p4 where" batch query """
2517 # List depot file paths exclude that already cached
2518 fileArgs = [f['path'] for f in files if f['path'] not in self.client_spec_path_cache]
2520 if len(fileArgs) == 0:
2521 return # All files in cache
2523 where_result = p4CmdList(["-x", "-", "where"], stdin=fileArgs)
2524 for res in where_result:
2525 if "code" in res and res["code"] == "error":
2526 # assume error is "... file(s) not in client view"
2528 if "clientFile" not in res:
2529 die("No clientFile in 'p4 where' output")
2531 # it will list all of them, but only one not unmap-ped
2533 if gitConfigBool("core.ignorecase"):
2534 res['depotFile'] = res['depotFile'].lower()
2535 self.client_spec_path_cache[res['depotFile']] = self.convert_client_path(res["clientFile"])
2537 # not found files or unmap files set to ""
2538 for depotFile in fileArgs:
2539 if gitConfigBool("core.ignorecase"):
2540 depotFile = depotFile.lower()
2541 if depotFile not in self.client_spec_path_cache:
2542 self.client_spec_path_cache[depotFile] = ""
2544 def map_in_client(self, depot_path):
2545 """Return the relative location in the client where this
2546 depot file should live. Returns "" if the file should
2547 not be mapped in the client."""
2549 if gitConfigBool("core.ignorecase"):
2550 depot_path = depot_path.lower()
2552 if depot_path in self.client_spec_path_cache:
2553 return self.client_spec_path_cache[depot_path]
2555 die( "Error: %s is not found in client spec path" % depot_path )
2558 def cloneExcludeCallback(option, opt_str, value, parser):
2559 # prepend "/" because the first "/" was consumed as part of the option itself.
2560 # ("-//depot/A/..." becomes "/depot/A/..." after option parsing)
2561 parser.values.cloneExclude += ["/" + re.sub(r"\.\.\.$", "", value)]
2563 class P4Sync(Command, P4UserMap):
2566 Command.__init__(self)
2567 P4UserMap.__init__(self)
2569 optparse.make_option("--branch", dest="branch"),
2570 optparse.make_option("--detect-branches", dest="detectBranches", action="store_true"),
2571 optparse.make_option("--changesfile", dest="changesFile"),
2572 optparse.make_option("--silent", dest="silent", action="store_true"),
2573 optparse.make_option("--detect-labels", dest="detectLabels", action="store_true"),
2574 optparse.make_option("--import-labels", dest="importLabels", action="store_true"),
2575 optparse.make_option("--import-local", dest="importIntoRemotes", action="store_false",
2576 help="Import into refs/heads/ , not refs/remotes"),
2577 optparse.make_option("--max-changes", dest="maxChanges",
2578 help="Maximum number of changes to import"),
2579 optparse.make_option("--changes-block-size", dest="changes_block_size", type="int",
2580 help="Internal block size to use when iteratively calling p4 changes"),
2581 optparse.make_option("--keep-path", dest="keepRepoPath", action='store_true',
2582 help="Keep entire BRANCH/DIR/SUBDIR prefix during import"),
2583 optparse.make_option("--use-client-spec", dest="useClientSpec", action='store_true',
2584 help="Only sync files that are included in the Perforce Client Spec"),
2585 optparse.make_option("-/", dest="cloneExclude",
2586 action="callback", callback=cloneExcludeCallback, type="string",
2587 help="exclude depot path"),
2589 self.description = """Imports from Perforce into a git repository.\n
2591 //depot/my/project/ -- to import the current head
2592 //depot/my/project/@all -- to import everything
2593 //depot/my/project/@1,6 -- to import only from revision 1 to 6
2595 (a ... is not needed in the path p4 specification, it's added implicitly)"""
2597 self.usage += " //depot/path[@revRange]"
2599 self.createdBranches = set()
2600 self.committedChanges = set()
2602 self.detectBranches = False
2603 self.detectLabels = False
2604 self.importLabels = False
2605 self.changesFile = ""
2606 self.syncWithOrigin = True
2607 self.importIntoRemotes = True
2608 self.maxChanges = ""
2609 self.changes_block_size = None
2610 self.keepRepoPath = False
2611 self.depotPaths = None
2612 self.p4BranchesInGit = []
2613 self.cloneExclude = []
2614 self.useClientSpec = False
2615 self.useClientSpec_from_options = False
2616 self.clientSpecDirs = None
2617 self.tempBranches = []
2618 self.tempBranchLocation = "refs/git-p4-tmp"
2619 self.largeFileSystem = None
2620 self.suppress_meta_comment = False
2622 if gitConfig('git-p4.largeFileSystem'):
2623 largeFileSystemConstructor = globals()[gitConfig('git-p4.largeFileSystem')]
2624 self.largeFileSystem = largeFileSystemConstructor(
2625 lambda git_mode, relPath, contents: self.writeToGitStream(git_mode, relPath, contents)
2628 if gitConfig("git-p4.syncFromOrigin") == "false":
2629 self.syncWithOrigin = False
2631 self.depotPaths = []
2632 self.changeRange = ""
2633 self.previousDepotPaths = []
2634 self.hasOrigin = False
2636 # map from branch depot path to parent branch
2637 self.knownBranches = {}
2638 self.initialParents = {}
2640 self.tz = "%+03d%02d" % (- time.timezone / 3600, ((- time.timezone % 3600) / 60))
2643 # Force a checkpoint in fast-import and wait for it to finish
2644 def checkpoint(self):
2645 self.gitStream.write("checkpoint\n\n")
2646 self.gitStream.write("progress checkpoint\n\n")
2647 out = self.gitOutput.readline()
2649 print("checkpoint finished: " + out)
2651 def isPathWanted(self, path):
2652 for p in self.cloneExclude:
2654 if p4PathStartsWith(path, p):
2656 # "-//depot/file1" without a trailing "/" should only exclude "file1", but not "file111" or "file1_dir/file2"
2657 elif path.lower() == p.lower():
2659 for p in self.depotPaths:
2660 if p4PathStartsWith(path, p):
2664 def extractFilesFromCommit(self, commit, shelved=False, shelved_cl = 0):
2667 while "depotFile%s" % fnum in commit:
2668 path = commit["depotFile%s" % fnum]
2669 found = self.isPathWanted(path)
2676 file["rev"] = commit["rev%s" % fnum]
2677 file["action"] = commit["action%s" % fnum]
2678 file["type"] = commit["type%s" % fnum]
2680 file["shelved_cl"] = int(shelved_cl)
2685 def extractJobsFromCommit(self, commit):
2688 while "job%s" % jnum in commit:
2689 job = commit["job%s" % jnum]
2694 def stripRepoPath(self, path, prefixes):
2695 """When streaming files, this is called to map a p4 depot path
2696 to where it should go in git. The prefixes are either
2697 self.depotPaths, or self.branchPrefixes in the case of
2698 branch detection."""
2700 if self.useClientSpec:
2701 # branch detection moves files up a level (the branch name)
2702 # from what client spec interpretation gives
2703 path = self.clientSpecDirs.map_in_client(path)
2704 if self.detectBranches:
2705 for b in self.knownBranches:
2706 if p4PathStartsWith(path, b + "/"):
2707 path = path[len(b)+1:]
2709 elif self.keepRepoPath:
2710 # Preserve everything in relative path name except leading
2711 # //depot/; just look at first prefix as they all should
2712 # be in the same depot.
2713 depot = re.sub("^(//[^/]+/).*", r'\1', prefixes[0])
2714 if p4PathStartsWith(path, depot):
2715 path = path[len(depot):]
2719 if p4PathStartsWith(path, p):
2720 path = path[len(p):]
2723 path = wildcard_decode(path)
2726 def splitFilesIntoBranches(self, commit):
2727 """Look at each depotFile in the commit to figure out to what
2728 branch it belongs."""
2730 if self.clientSpecDirs:
2731 files = self.extractFilesFromCommit(commit)
2732 self.clientSpecDirs.update_client_spec_path_cache(files)
2736 while "depotFile%s" % fnum in commit:
2737 path = commit["depotFile%s" % fnum]
2738 found = self.isPathWanted(path)
2745 file["rev"] = commit["rev%s" % fnum]
2746 file["action"] = commit["action%s" % fnum]
2747 file["type"] = commit["type%s" % fnum]
2750 # start with the full relative path where this file would
2752 if self.useClientSpec:
2753 relPath = self.clientSpecDirs.map_in_client(path)
2755 relPath = self.stripRepoPath(path, self.depotPaths)
2757 for branch in self.knownBranches.keys():
2758 # add a trailing slash so that a commit into qt/4.2foo
2759 # doesn't end up in qt/4.2, e.g.
2760 if p4PathStartsWith(relPath, branch + "/"):
2761 if branch not in branches:
2762 branches[branch] = []
2763 branches[branch].append(file)
2768 def writeToGitStream(self, gitMode, relPath, contents):
2769 self.gitStream.write('M %s inline %s\n' % (gitMode, relPath))
2770 self.gitStream.write('data %d\n' % sum(len(d) for d in contents))
2772 self.gitStream.write(d)
2773 self.gitStream.write('\n')
2775 def encodeWithUTF8(self, path):
2777 path.decode('ascii')
2780 if gitConfig('git-p4.pathEncoding'):
2781 encoding = gitConfig('git-p4.pathEncoding')
2782 path = path.decode(encoding, 'replace').encode('utf8', 'replace')
2784 print('Path with non-ASCII characters detected. Used %s to encode: %s ' % (encoding, path))
2787 # output one file from the P4 stream
2788 # - helper for streamP4Files
2790 def streamOneP4File(self, file, contents):
2791 relPath = self.stripRepoPath(file['depotFile'], self.branchPrefixes)
2792 relPath = self.encodeWithUTF8(relPath)
2794 if 'fileSize' in self.stream_file:
2795 size = int(self.stream_file['fileSize'])
2797 size = 0 # deleted files don't get a fileSize apparently
2798 sys.stdout.write('\r%s --> %s (%i MB)\n' % (file['depotFile'], relPath, size/1024/1024))
2801 (type_base, type_mods) = split_p4_type(file["type"])
2804 if "x" in type_mods:
2806 if type_base == "symlink":
2808 # p4 print on a symlink sometimes contains "target\n";
2809 # if it does, remove the newline
2810 data = ''.join(contents)
2812 # Some version of p4 allowed creating a symlink that pointed
2813 # to nothing. This causes p4 errors when checking out such
2814 # a change, and errors here too. Work around it by ignoring
2815 # the bad symlink; hopefully a future change fixes it.
2816 print("\nIgnoring empty symlink in %s" % file['depotFile'])
2818 elif data[-1] == '\n':
2819 contents = [data[:-1]]
2823 if type_base == "utf16":
2824 # p4 delivers different text in the python output to -G
2825 # than it does when using "print -o", or normal p4 client
2826 # operations. utf16 is converted to ascii or utf8, perhaps.
2827 # But ascii text saved as -t utf16 is completely mangled.
2828 # Invoke print -o to get the real contents.
2830 # On windows, the newlines will always be mangled by print, so put
2831 # them back too. This is not needed to the cygwin windows version,
2832 # just the native "NT" type.
2835 text = p4_read_pipe(['print', '-q', '-o', '-', '%s@%s' % (file['depotFile'], file['change'])])
2836 except Exception as e:
2837 if 'Translation of file content failed' in str(e):
2838 type_base = 'binary'
2842 if p4_version_string().find('/NT') >= 0:
2843 text = text.replace('\r\n', '\n')
2846 if type_base == "apple":
2847 # Apple filetype files will be streamed as a concatenation of
2848 # its appledouble header and the contents. This is useless
2849 # on both macs and non-macs. If using "print -q -o xx", it
2850 # will create "xx" with the data, and "%xx" with the header.
2851 # This is also not very useful.
2853 # Ideally, someday, this script can learn how to generate
2854 # appledouble files directly and import those to git, but
2855 # non-mac machines can never find a use for apple filetype.
2856 print("\nIgnoring apple filetype file %s" % file['depotFile'])
2859 # Note that we do not try to de-mangle keywords on utf16 files,
2860 # even though in theory somebody may want that.
2861 pattern = p4_keywords_regexp_for_type(type_base, type_mods)
2863 regexp = re.compile(pattern, re.VERBOSE)
2864 text = ''.join(contents)
2865 text = regexp.sub(r'$\1$', text)
2868 if self.largeFileSystem:
2869 (git_mode, contents) = self.largeFileSystem.processContent(git_mode, relPath, contents)
2871 self.writeToGitStream(git_mode, relPath, contents)
2873 def streamOneP4Deletion(self, file):
2874 relPath = self.stripRepoPath(file['path'], self.branchPrefixes)
2875 relPath = self.encodeWithUTF8(relPath)
2877 sys.stdout.write("delete %s\n" % relPath)
2879 self.gitStream.write("D %s\n" % relPath)
2881 if self.largeFileSystem and self.largeFileSystem.isLargeFile(relPath):
2882 self.largeFileSystem.removeLargeFile(relPath)
2884 # handle another chunk of streaming data
2885 def streamP4FilesCb(self, marshalled):
2887 # catch p4 errors and complain
2889 if "code" in marshalled:
2890 if marshalled["code"] == "error":
2891 if "data" in marshalled:
2892 err = marshalled["data"].rstrip()
2894 if not err and 'fileSize' in self.stream_file:
2895 required_bytes = int((4 * int(self.stream_file["fileSize"])) - calcDiskFree())
2896 if required_bytes > 0:
2897 err = 'Not enough space left on %s! Free at least %i MB.' % (
2898 os.getcwd(), required_bytes/1024/1024
2903 if self.stream_have_file_info:
2904 if "depotFile" in self.stream_file:
2905 f = self.stream_file["depotFile"]
2906 # force a failure in fast-import, else an empty
2907 # commit will be made
2908 self.gitStream.write("\n")
2909 self.gitStream.write("die-now\n")
2910 self.gitStream.close()
2911 # ignore errors, but make sure it exits first
2912 self.importProcess.wait()
2914 die("Error from p4 print for %s: %s" % (f, err))
2916 die("Error from p4 print: %s" % err)
2918 if 'depotFile' in marshalled and self.stream_have_file_info:
2919 # start of a new file - output the old one first
2920 self.streamOneP4File(self.stream_file, self.stream_contents)
2921 self.stream_file = {}
2922 self.stream_contents = []
2923 self.stream_have_file_info = False
2925 # pick up the new file information... for the
2926 # 'data' field we need to append to our array
2927 for k in marshalled.keys():
2929 if 'streamContentSize' not in self.stream_file:
2930 self.stream_file['streamContentSize'] = 0
2931 self.stream_file['streamContentSize'] += len(marshalled['data'])
2932 self.stream_contents.append(marshalled['data'])
2934 self.stream_file[k] = marshalled[k]
2937 'streamContentSize' in self.stream_file and
2938 'fileSize' in self.stream_file and
2939 'depotFile' in self.stream_file):
2940 size = int(self.stream_file["fileSize"])
2942 progress = 100*self.stream_file['streamContentSize']/size
2943 sys.stdout.write('\r%s %d%% (%i MB)' % (self.stream_file['depotFile'], progress, int(size/1024/1024)))
2946 self.stream_have_file_info = True
2948 # Stream directly from "p4 files" into "git fast-import"
2949 def streamP4Files(self, files):
2955 filesForCommit.append(f)
2956 if f['action'] in self.delete_actions:
2957 filesToDelete.append(f)
2959 filesToRead.append(f)
2962 for f in filesToDelete:
2963 self.streamOneP4Deletion(f)
2965 if len(filesToRead) > 0:
2966 self.stream_file = {}
2967 self.stream_contents = []
2968 self.stream_have_file_info = False
2970 # curry self argument
2971 def streamP4FilesCbSelf(entry):
2972 self.streamP4FilesCb(entry)
2975 for f in filesToRead:
2976 if 'shelved_cl' in f:
2977 # Handle shelved CLs using the "p4 print file@=N" syntax to print
2979 fileArg = '%s@=%d' % (f['path'], f['shelved_cl'])
2981 fileArg = '%s#%s' % (f['path'], f['rev'])
2983 fileArgs.append(fileArg)
2985 p4CmdList(["-x", "-", "print"],
2987 cb=streamP4FilesCbSelf)
2990 if 'depotFile' in self.stream_file:
2991 self.streamOneP4File(self.stream_file, self.stream_contents)
2993 def make_email(self, userid):
2994 if userid in self.users:
2995 return self.users[userid]
2997 return "%s <a@b>" % userid
2999 def streamTag(self, gitStream, labelName, labelDetails, commit, epoch):
3000 """ Stream a p4 tag.
3001 commit is either a git commit, or a fast-import mark, ":<p4commit>"
3005 print("writing tag %s for commit %s" % (labelName, commit))
3006 gitStream.write("tag %s\n" % labelName)
3007 gitStream.write("from %s\n" % commit)
3009 if 'Owner' in labelDetails:
3010 owner = labelDetails["Owner"]
3014 # Try to use the owner of the p4 label, or failing that,
3015 # the current p4 user id.
3017 email = self.make_email(owner)
3019 email = self.make_email(self.p4UserId())
3020 tagger = "%s %s %s" % (email, epoch, self.tz)
3022 gitStream.write("tagger %s\n" % tagger)
3024 print("labelDetails=",labelDetails)
3025 if 'Description' in labelDetails:
3026 description = labelDetails['Description']
3028 description = 'Label from git p4'
3030 gitStream.write("data %d\n" % len(description))
3031 gitStream.write(description)
3032 gitStream.write("\n")
3034 def inClientSpec(self, path):
3035 if not self.clientSpecDirs:
3037 inClientSpec = self.clientSpecDirs.map_in_client(path)
3038 if not inClientSpec and self.verbose:
3039 print('Ignoring file outside of client spec: {0}'.format(path))
3042 def hasBranchPrefix(self, path):
3043 if not self.branchPrefixes:
3045 hasPrefix = [p for p in self.branchPrefixes
3046 if p4PathStartsWith(path, p)]
3047 if not hasPrefix and self.verbose:
3048 print('Ignoring file outside of prefix: {0}'.format(path))
3051 def commit(self, details, files, branch, parent = "", allow_empty=False):
3052 epoch = details["time"]
3053 author = details["user"]
3054 jobs = self.extractJobsFromCommit(details)
3057 print('commit into {0}'.format(branch))
3059 if self.clientSpecDirs:
3060 self.clientSpecDirs.update_client_spec_path_cache(files)
3062 files = [f for f in files
3063 if self.inClientSpec(f['path']) and self.hasBranchPrefix(f['path'])]
3065 if gitConfigBool('git-p4.keepEmptyCommits'):
3068 if not files and not allow_empty:
3069 print('Ignoring revision {0} as it would produce an empty commit.'
3070 .format(details['change']))
3073 self.gitStream.write("commit %s\n" % branch)
3074 self.gitStream.write("mark :%s\n" % details["change"])
3075 self.committedChanges.add(int(details["change"]))
3077 if author not in self.users:
3078 self.getUserMapFromPerforceServer()
3079 committer = "%s %s %s" % (self.make_email(author), epoch, self.tz)
3081 self.gitStream.write("committer %s\n" % committer)
3083 self.gitStream.write("data <<EOT\n")
3084 self.gitStream.write(details["desc"])
3086 self.gitStream.write("\nJobs: %s" % (' '.join(jobs)))
3088 if not self.suppress_meta_comment:
3089 self.gitStream.write("\n[git-p4: depot-paths = \"%s\": change = %s" %
3090 (','.join(self.branchPrefixes), details["change"]))
3091 if len(details['options']) > 0:
3092 self.gitStream.write(": options = %s" % details['options'])
3093 self.gitStream.write("]\n")
3095 self.gitStream.write("EOT\n\n")
3099 print("parent %s" % parent)
3100 self.gitStream.write("from %s\n" % parent)
3102 self.streamP4Files(files)
3103 self.gitStream.write("\n")
3105 change = int(details["change"])
3107 if change in self.labels:
3108 label = self.labels[change]
3109 labelDetails = label[0]
3110 labelRevisions = label[1]
3112 print("Change %s is labelled %s" % (change, labelDetails))
3114 files = p4CmdList(["files"] + ["%s...@%s" % (p, change)
3115 for p in self.branchPrefixes])
3117 if len(files) == len(labelRevisions):
3121 if info["action"] in self.delete_actions:
3123 cleanedFiles[info["depotFile"]] = info["rev"]
3125 if cleanedFiles == labelRevisions:
3126 self.streamTag(self.gitStream, 'tag_%s' % labelDetails['label'], labelDetails, branch, epoch)
3130 print("Tag %s does not match with change %s: files do not match."
3131 % (labelDetails["label"], change))
3135 print("Tag %s does not match with change %s: file count is different."
3136 % (labelDetails["label"], change))
3138 # Build a dictionary of changelists and labels, for "detect-labels" option.
3139 def getLabels(self):
3142 l = p4CmdList(["labels"] + ["%s..." % p for p in self.depotPaths])
3143 if len(l) > 0 and not self.silent:
3144 print("Finding files belonging to labels in %s" % self.depotPaths)
3147 label = output["label"]
3151 print("Querying files for label %s" % label)
3152 for file in p4CmdList(["files"] +
3153 ["%s...@%s" % (p, label)
3154 for p in self.depotPaths]):
3155 revisions[file["depotFile"]] = file["rev"]
3156 change = int(file["change"])
3157 if change > newestChange:
3158 newestChange = change
3160 self.labels[newestChange] = [output, revisions]
3163 print("Label changes: %s" % self.labels.keys())
3165 # Import p4 labels as git tags. A direct mapping does not
3166 # exist, so assume that if all the files are at the same revision
3167 # then we can use that, or it's something more complicated we should
3169 def importP4Labels(self, stream, p4Labels):
3171 print("import p4 labels: " + ' '.join(p4Labels))
3173 ignoredP4Labels = gitConfigList("git-p4.ignoredP4Labels")
3174 validLabelRegexp = gitConfig("git-p4.labelImportRegexp")
3175 if len(validLabelRegexp) == 0:
3176 validLabelRegexp = defaultLabelRegexp
3177 m = re.compile(validLabelRegexp)
3179 for name in p4Labels:
3182 if not m.match(name):
3184 print("label %s does not match regexp %s" % (name,validLabelRegexp))
3187 if name in ignoredP4Labels:
3190 labelDetails = p4CmdList(['label', "-o", name])[0]
3192 # get the most recent changelist for each file in this label
3193 change = p4Cmd(["changes", "-m", "1"] + ["%s...@%s" % (p, name)
3194 for p in self.depotPaths])
3196 if 'change' in change:
3197 # find the corresponding git commit; take the oldest commit
3198 changelist = int(change['change'])
3199 if changelist in self.committedChanges:
3200 gitCommit = ":%d" % changelist # use a fast-import mark
3203 gitCommit = read_pipe(["git", "rev-list", "--max-count=1",
3204 "--reverse", ":/\[git-p4:.*change = %d\]" % changelist], ignore_error=True)
3205 if len(gitCommit) == 0:
3206 print("importing label %s: could not find git commit for changelist %d" % (name, changelist))
3209 gitCommit = gitCommit.strip()
3212 # Convert from p4 time format
3214 tmwhen = time.strptime(labelDetails['Update'], "%Y/%m/%d %H:%M:%S")
3216 print("Could not convert label time %s" % labelDetails['Update'])
3219 when = int(time.mktime(tmwhen))
3220 self.streamTag(stream, name, labelDetails, gitCommit, when)
3222 print("p4 label %s mapped to git commit %s" % (name, gitCommit))
3225 print("Label %s has no changelists - possibly deleted?" % name)
3228 # We can't import this label; don't try again as it will get very
3229 # expensive repeatedly fetching all the files for labels that will
3230 # never be imported. If the label is moved in the future, the
3231 # ignore will need to be removed manually.
3232 system(["git", "config", "--add", "git-p4.ignoredP4Labels", name])
3234 def guessProjectName(self):
3235 for p in self.depotPaths:
3238 p = p[p.strip().rfind("/") + 1:]
3239 if not p.endswith("/"):
3243 def getBranchMapping(self):
3244 lostAndFoundBranches = set()
3246 user = gitConfig("git-p4.branchUser")
3248 command = "branches -u %s" % user
3250 command = "branches"
3252 for info in p4CmdList(command):
3253 details = p4Cmd(["branch", "-o", info["branch"]])
3255 while "View%s" % viewIdx in details:
3256 paths = details["View%s" % viewIdx].split(" ")
3257 viewIdx = viewIdx + 1
3258 # require standard //depot/foo/... //depot/bar/... mapping
3259 if len(paths) != 2 or not paths[0].endswith("/...") or not paths[1].endswith("/..."):
3262 destination = paths[1]
3264 if p4PathStartsWith(source, self.depotPaths[0]) and p4PathStartsWith(destination, self.depotPaths[0]):
3265 source = source[len(self.depotPaths[0]):-4]
3266 destination = destination[len(self.depotPaths[0]):-4]
3268 if destination in self.knownBranches:
3270 print("p4 branch %s defines a mapping from %s to %s" % (info["branch"], source, destination))
3271 print("but there exists another mapping from %s to %s already!" % (self.knownBranches[destination], destination))
3274 self.knownBranches[destination] = source
3276 lostAndFoundBranches.discard(destination)
3278 if source not in self.knownBranches:
3279 lostAndFoundBranches.add(source)
3281 # Perforce does not strictly require branches to be defined, so we also
3282 # check git config for a branch list.
3284 # Example of branch definition in git config file:
3286 # branchList=main:branchA
3287 # branchList=main:branchB
3288 # branchList=branchA:branchC
3289 configBranches = gitConfigList("git-p4.branchList")
3290 for branch in configBranches:
3292 (source, destination) = branch.split(":")
3293 self.knownBranches[destination] = source
3295 lostAndFoundBranches.discard(destination)
3297 if source not in self.knownBranches:
3298 lostAndFoundBranches.add(source)
3301 for branch in lostAndFoundBranches:
3302 self.knownBranches[branch] = branch
3304 def getBranchMappingFromGitBranches(self):
3305 branches = p4BranchesInGit(self.importIntoRemotes)
3306 for branch in branches.keys():
3307 if branch == "master":
3310 branch = branch[len(self.projectName):]
3311 self.knownBranches[branch] = branch
3313 def updateOptionDict(self, d):
3315 if self.keepRepoPath:
3316 option_keys['keepRepoPath'] = 1
3318 d["options"] = ' '.join(sorted(option_keys.keys()))
3320 def readOptions(self, d):
3321 self.keepRepoPath = ('options' in d
3322 and ('keepRepoPath' in d['options']))
3324 def gitRefForBranch(self, branch):
3325 if branch == "main":
3326 return self.refPrefix + "master"
3328 if len(branch) <= 0:
3331 return self.refPrefix + self.projectName + branch
3333 def gitCommitByP4Change(self, ref, change):
3335 print("looking in ref " + ref + " for change %s using bisect..." % change)
3338 latestCommit = parseRevision(ref)
3342 print("trying: earliest %s latest %s" % (earliestCommit, latestCommit))
3343 next = read_pipe("git rev-list --bisect %s %s" % (latestCommit, earliestCommit)).strip()
3348 log = extractLogMessageFromGitCommit(next)
3349 settings = extractSettingsGitLog(log)
3350 currentChange = int(settings['change'])
3352 print("current change %s" % currentChange)
3354 if currentChange == change:
3356 print("found %s" % next)
3359 if currentChange < change:
3360 earliestCommit = "^%s" % next
3362 if next == latestCommit:
3363 die("Infinite loop while looking in ref %s for change %s. Check your branch mappings" % (ref, change))
3364 latestCommit = "%s^@" % next
3368 def importNewBranch(self, branch, maxChange):
3369 # make fast-import flush all changes to disk and update the refs using the checkpoint
3370 # command so that we can try to find the branch parent in the git history
3371 self.gitStream.write("checkpoint\n\n");
3372 self.gitStream.flush();
3373 branchPrefix = self.depotPaths[0] + branch + "/"
3374 range = "@1,%s" % maxChange
3375 #print "prefix" + branchPrefix
3376 changes = p4ChangesForPaths([branchPrefix], range, self.changes_block_size)
3377 if len(changes) <= 0:
3379 firstChange = changes[0]
3380 #print "first change in branch: %s" % firstChange
3381 sourceBranch = self.knownBranches[branch]
3382 sourceDepotPath = self.depotPaths[0] + sourceBranch
3383 sourceRef = self.gitRefForBranch(sourceBranch)
3384 #print "source " + sourceBranch
3386 branchParentChange = int(p4Cmd(["changes", "-m", "1", "%s...@1,%s" % (sourceDepotPath, firstChange)])["change"])
3387 #print "branch parent: %s" % branchParentChange
3388 gitParent = self.gitCommitByP4Change(sourceRef, branchParentChange)
3389 if len(gitParent) > 0:
3390 self.initialParents[self.gitRefForBranch(branch)] = gitParent
3391 #print "parent git commit: %s" % gitParent
3393 self.importChanges(changes)
3396 def searchParent(self, parent, branch, target):
3398 for blob in read_pipe_lines(["git", "rev-list", "--reverse",
3399 "--no-merges", parent]):
3401 if len(read_pipe(["git", "diff-tree", blob, target])) == 0:
3404 print("Found parent of %s in commit %s" % (branch, blob))
3411 def importChanges(self, changes, origin_revision=0):
3413 for change in changes:
3414 description = p4_describe(change)
3415 self.updateOptionDict(description)
3418 sys.stdout.write("\rImporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
3423 if self.detectBranches:
3424 branches = self.splitFilesIntoBranches(description)
3425 for branch in branches.keys():
3427 branchPrefix = self.depotPaths[0] + branch + "/"
3428 self.branchPrefixes = [ branchPrefix ]
3432 filesForCommit = branches[branch]
3435 print("branch is %s" % branch)
3437 self.updatedBranches.add(branch)
3439 if branch not in self.createdBranches:
3440 self.createdBranches.add(branch)
3441 parent = self.knownBranches[branch]
3442 if parent == branch:
3445 fullBranch = self.projectName + branch
3446 if fullBranch not in self.p4BranchesInGit:
3448 print("\n Importing new branch %s" % fullBranch);
3449 if self.importNewBranch(branch, change - 1):
3451 self.p4BranchesInGit.append(fullBranch)
3453 print("\n Resuming with change %s" % change);
3456 print("parent determined through known branches: %s" % parent)
3458 branch = self.gitRefForBranch(branch)
3459 parent = self.gitRefForBranch(parent)
3462 print("looking for initial parent for %s; current parent is %s" % (branch, parent))
3464 if len(parent) == 0 and branch in self.initialParents:
3465 parent = self.initialParents[branch]
3466 del self.initialParents[branch]
3470 tempBranch = "%s/%d" % (self.tempBranchLocation, change)
3472 print("Creating temporary branch: " + tempBranch)
3473 self.commit(description, filesForCommit, tempBranch)
3474 self.tempBranches.append(tempBranch)
3476 blob = self.searchParent(parent, branch, tempBranch)
3478 self.commit(description, filesForCommit, branch, blob)
3481 print("Parent of %s not found. Committing into head of %s" % (branch, parent))
3482 self.commit(description, filesForCommit, branch, parent)
3484 files = self.extractFilesFromCommit(description)
3485 self.commit(description, files, self.branch,
3487 # only needed once, to connect to the previous commit
3488 self.initialParent = ""
3490 print(self.gitError.read())
3493 def sync_origin_only(self):
3494 if self.syncWithOrigin:
3495 self.hasOrigin = originP4BranchesExist()
3498 print('Syncing with origin first, using "git fetch origin"')
3499 system("git fetch origin")
3501 def importHeadRevision(self, revision):
3502 print("Doing initial import of %s from revision %s into %s" % (' '.join(self.depotPaths), revision, self.branch))
3505 details["user"] = "git perforce import user"
3506 details["desc"] = ("Initial import of %s from the state at revision %s\n"
3507 % (' '.join(self.depotPaths), revision))
3508 details["change"] = revision
3512 fileArgs = ["%s...%s" % (p,revision) for p in self.depotPaths]
3514 for info in p4CmdList(["files"] + fileArgs):
3516 if 'code' in info and info['code'] == 'error':
3517 sys.stderr.write("p4 returned an error: %s\n"
3519 if info['data'].find("must refer to client") >= 0:
3520 sys.stderr.write("This particular p4 error is misleading.\n")
3521 sys.stderr.write("Perhaps the depot path was misspelled.\n");
3522 sys.stderr.write("Depot path: %s\n" % " ".join(self.depotPaths))
3524 if 'p4ExitCode' in info:
3525 sys.stderr.write("p4 exitcode: %s\n" % info['p4ExitCode'])
3529 change = int(info["change"])
3530 if change > newestRevision:
3531 newestRevision = change
3533 if info["action"] in self.delete_actions:
3534 # don't increase the file cnt, otherwise details["depotFile123"] will have gaps!
3535 #fileCnt = fileCnt + 1
3538 for prop in ["depotFile", "rev", "action", "type" ]:
3539 details["%s%s" % (prop, fileCnt)] = info[prop]
3541 fileCnt = fileCnt + 1
3543 details["change"] = newestRevision
3545 # Use time from top-most change so that all git p4 clones of
3546 # the same p4 repo have the same commit SHA1s.
3547 res = p4_describe(newestRevision)
3548 details["time"] = res["time"]
3550 self.updateOptionDict(details)
3552 self.commit(details, self.extractFilesFromCommit(details), self.branch)
3553 except IOError as err:
3554 print("IO error with git fast-import. Is your git version recent enough?")
3555 print("IO error details: {}".format(err))
3556 print(self.gitError.read())
3558 def openStreams(self):
3559 self.importProcess = subprocess.Popen(["git", "fast-import"],
3560 stdin=subprocess.PIPE,
3561 stdout=subprocess.PIPE,
3562 stderr=subprocess.PIPE);
3563 self.gitOutput = self.importProcess.stdout
3564 self.gitStream = self.importProcess.stdin
3565 self.gitError = self.importProcess.stderr
3567 def closeStreams(self):
3568 if self.gitStream is None:
3570 self.gitStream.close()
3571 if self.importProcess.wait() != 0:
3572 die("fast-import failed: %s" % self.gitError.read())
3573 self.gitOutput.close()
3574 self.gitError.close()
3575 self.gitStream = None
3577 def run(self, args):
3578 if self.importIntoRemotes:
3579 self.refPrefix = "refs/remotes/p4/"
3581 self.refPrefix = "refs/heads/p4/"
3583 self.sync_origin_only()
3585 branch_arg_given = bool(self.branch)
3586 if len(self.branch) == 0:
3587 self.branch = self.refPrefix + "master"
3588 if gitBranchExists("refs/heads/p4") and self.importIntoRemotes:
3589 system("git update-ref %s refs/heads/p4" % self.branch)
3590 system("git branch -D p4")
3592 # accept either the command-line option, or the configuration variable
3593 if self.useClientSpec:
3594 # will use this after clone to set the variable
3595 self.useClientSpec_from_options = True
3597 if gitConfigBool("git-p4.useclientspec"):
3598 self.useClientSpec = True
3599 if self.useClientSpec:
3600 self.clientSpecDirs = getClientSpec()
3602 # TODO: should always look at previous commits,
3603 # merge with previous imports, if possible.
3606 createOrUpdateBranchesFromOrigin(self.refPrefix, self.silent)
3608 # branches holds mapping from branch name to sha1
3609 branches = p4BranchesInGit(self.importIntoRemotes)
3611 # restrict to just this one, disabling detect-branches
3612 if branch_arg_given:
3613 short = self.branch.split("/")[-1]
3614 if short in branches:
3615 self.p4BranchesInGit = [ short ]
3617 self.p4BranchesInGit = branches.keys()
3619 if len(self.p4BranchesInGit) > 1:
3621 print("Importing from/into multiple branches")
3622 self.detectBranches = True
3623 for branch in branches.keys():
3624 self.initialParents[self.refPrefix + branch] = \
3628 print("branches: %s" % self.p4BranchesInGit)
3631 for branch in self.p4BranchesInGit:
3632 logMsg = extractLogMessageFromGitCommit(self.refPrefix + branch)
3634 settings = extractSettingsGitLog(logMsg)
3636 self.readOptions(settings)
3637 if ('depot-paths' in settings
3638 and 'change' in settings):
3639 change = int(settings['change']) + 1
3640 p4Change = max(p4Change, change)
3642 depotPaths = sorted(settings['depot-paths'])
3643 if self.previousDepotPaths == []:
3644 self.previousDepotPaths = depotPaths
3647 for (prev, cur) in zip(self.previousDepotPaths, depotPaths):
3648 prev_list = prev.split("/")
3649 cur_list = cur.split("/")
3650 for i in range(0, min(len(cur_list), len(prev_list))):
3651 if cur_list[i] != prev_list[i]:
3655 paths.append ("/".join(cur_list[:i + 1]))
3657 self.previousDepotPaths = paths
3660 self.depotPaths = sorted(self.previousDepotPaths)
3661 self.changeRange = "@%s,#head" % p4Change
3662 if not self.silent and not self.detectBranches:
3663 print("Performing incremental import into %s git branch" % self.branch)
3665 # accept multiple ref name abbreviations:
3666 # refs/foo/bar/branch -> use it exactly
3667 # p4/branch -> prepend refs/remotes/ or refs/heads/
3668 # branch -> prepend refs/remotes/p4/ or refs/heads/p4/
3669 if not self.branch.startswith("refs/"):
3670 if self.importIntoRemotes:
3671 prepend = "refs/remotes/"
3673 prepend = "refs/heads/"
3674 if not self.branch.startswith("p4/"):
3676 self.branch = prepend + self.branch
3678 if len(args) == 0 and self.depotPaths:
3680 print("Depot paths: %s" % ' '.join(self.depotPaths))
3682 if self.depotPaths and self.depotPaths != args:
3683 print("previous import used depot path %s and now %s was specified. "
3684 "This doesn't work!" % (' '.join (self.depotPaths),
3688 self.depotPaths = sorted(args)
3693 # Make sure no revision specifiers are used when --changesfile
3695 bad_changesfile = False
3696 if len(self.changesFile) > 0:
3697 for p in self.depotPaths:
3698 if p.find("@") >= 0 or p.find("#") >= 0:
3699 bad_changesfile = True
3702 die("Option --changesfile is incompatible with revision specifiers")
3705 for p in self.depotPaths:
3706 if p.find("@") != -1:
3707 atIdx = p.index("@")
3708 self.changeRange = p[atIdx:]
3709 if self.changeRange == "@all":
3710 self.changeRange = ""
3711 elif ',' not in self.changeRange:
3712 revision = self.changeRange
3713 self.changeRange = ""
3715 elif p.find("#") != -1:
3716 hashIdx = p.index("#")
3717 revision = p[hashIdx:]
3719 elif self.previousDepotPaths == []:
3720 # pay attention to changesfile, if given, else import
3721 # the entire p4 tree at the head revision
3722 if len(self.changesFile) == 0:
3725 p = re.sub ("\.\.\.$", "", p)
3726 if not p.endswith("/"):
3731 self.depotPaths = newPaths
3733 # --detect-branches may change this for each branch
3734 self.branchPrefixes = self.depotPaths
3736 self.loadUserMapFromCache()
3738 if self.detectLabels:
3741 if self.detectBranches:
3742 ## FIXME - what's a P4 projectName ?
3743 self.projectName = self.guessProjectName()
3746 self.getBranchMappingFromGitBranches()
3748 self.getBranchMapping()
3750 print("p4-git branches: %s" % self.p4BranchesInGit)
3751 print("initial parents: %s" % self.initialParents)
3752 for b in self.p4BranchesInGit:
3756 b = b[len(self.projectName):]
3757 self.createdBranches.add(b)
3762 self.importHeadRevision(revision)
3766 if len(self.changesFile) > 0:
3767 output = open(self.changesFile).readlines()
3770 changeSet.add(int(line))
3772 for change in changeSet:
3773 changes.append(change)
3777 # catch "git p4 sync" with no new branches, in a repo that
3778 # does not have any existing p4 branches
3780 if not self.p4BranchesInGit:
3781 die("No remote p4 branches. Perhaps you never did \"git p4 clone\" in here.")
3783 # The default branch is master, unless --branch is used to
3784 # specify something else. Make sure it exists, or complain
3785 # nicely about how to use --branch.
3786 if not self.detectBranches:
3787 if not branch_exists(self.branch):
3788 if branch_arg_given:
3789 die("Error: branch %s does not exist." % self.branch)
3791 die("Error: no branch %s; perhaps specify one with --branch." %
3795 print("Getting p4 changes for %s...%s" % (', '.join(self.depotPaths),
3797 changes = p4ChangesForPaths(self.depotPaths, self.changeRange, self.changes_block_size)
3799 if len(self.maxChanges) > 0:
3800 changes = changes[:min(int(self.maxChanges), len(changes))]
3802 if len(changes) == 0:
3804 print("No changes to import!")
3806 if not self.silent and not self.detectBranches:
3807 print("Import destination: %s" % self.branch)
3809 self.updatedBranches = set()
3811 if not self.detectBranches:
3813 # start a new branch
3814 self.initialParent = ""
3816 # build on a previous revision
3817 self.initialParent = parseRevision(self.branch)
3819 self.importChanges(changes)
3823 if len(self.updatedBranches) > 0:
3824 sys.stdout.write("Updated branches: ")
3825 for b in self.updatedBranches:
3826 sys.stdout.write("%s " % b)
3827 sys.stdout.write("\n")
3829 if gitConfigBool("git-p4.importLabels"):
3830 self.importLabels = True
3832 if self.importLabels:
3833 p4Labels = getP4Labels(self.depotPaths)
3834 gitTags = getGitTags()
3836 missingP4Labels = p4Labels - gitTags
3837 self.importP4Labels(self.gitStream, missingP4Labels)
3841 # Cleanup temporary branches created during import
3842 if self.tempBranches != []:
3843 for branch in self.tempBranches:
3844 read_pipe("git update-ref -d %s" % branch)
3845 os.rmdir(os.path.join(os.environ.get("GIT_DIR", ".git"), self.tempBranchLocation))
3847 # Create a symbolic ref p4/HEAD pointing to p4/<branch> to allow
3848 # a convenient shortcut refname "p4".
3849 if self.importIntoRemotes:
3850 head_ref = self.refPrefix + "HEAD"
3851 if not gitBranchExists(head_ref) and gitBranchExists(self.branch):
3852 system(["git", "symbolic-ref", head_ref, self.branch])
3856 class P4Rebase(Command):
3858 Command.__init__(self)
3860 optparse.make_option("--import-labels", dest="importLabels", action="store_true"),
3862 self.importLabels = False
3863 self.description = ("Fetches the latest revision from perforce and "
3864 + "rebases the current work (branch) against it")
3866 def run(self, args):
3868 sync.importLabels = self.importLabels
3871 return self.rebase()
3874 if os.system("git update-index --refresh") != 0:
3875 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.");
3876 if len(read_pipe("git diff-index HEAD --")) > 0:
3877 die("You have uncommitted changes. Please commit them before rebasing or stash them away with git stash.");
3879 [upstream, settings] = findUpstreamBranchPoint()
3880 if len(upstream) == 0:
3881 die("Cannot find upstream branchpoint for rebase")
3883 # the branchpoint may be p4/foo~3, so strip off the parent
3884 upstream = re.sub("~[0-9]+$", "", upstream)
3886 print("Rebasing the current branch onto %s" % upstream)
3887 oldHead = read_pipe("git rev-parse HEAD").strip()
3888 system("git rebase %s" % upstream)
3889 system("git diff-tree --stat --summary -M %s HEAD --" % oldHead)
3892 class P4Clone(P4Sync):
3894 P4Sync.__init__(self)
3895 self.description = "Creates a new git repository and imports from Perforce into it"
3896 self.usage = "usage: %prog [options] //depot/path[@revRange]"
3898 optparse.make_option("--destination", dest="cloneDestination",
3899 action='store', default=None,
3900 help="where to leave result of the clone"),
3901 optparse.make_option("--bare", dest="cloneBare",
3902 action="store_true", default=False),
3904 self.cloneDestination = None
3905 self.needsGit = False
3906 self.cloneBare = False
3908 def defaultDestination(self, args):
3909 ## TODO: use common prefix of args?
3911 depotDir = re.sub("(@[^@]*)$", "", depotPath)
3912 depotDir = re.sub("(#[^#]*)$", "", depotDir)
3913 depotDir = re.sub(r"\.\.\.$", "", depotDir)
3914 depotDir = re.sub(r"/$", "", depotDir)
3915 return os.path.split(depotDir)[1]
3917 def run(self, args):
3921 if self.keepRepoPath and not self.cloneDestination:
3922 sys.stderr.write("Must specify destination for --keep-path\n")
3927 if not self.cloneDestination and len(depotPaths) > 1:
3928 self.cloneDestination = depotPaths[-1]
3929 depotPaths = depotPaths[:-1]
3931 for p in depotPaths:
3932 if not p.startswith("//"):
3933 sys.stderr.write('Depot paths must start with "//": %s\n' % p)
3936 if not self.cloneDestination:
3937 self.cloneDestination = self.defaultDestination(args)
3939 print("Importing from %s into %s" % (', '.join(depotPaths), self.cloneDestination))
3941 if not os.path.exists(self.cloneDestination):
3942 os.makedirs(self.cloneDestination)
3943 chdir(self.cloneDestination)
3945 init_cmd = [ "git", "init" ]
3947 init_cmd.append("--bare")
3948 retcode = subprocess.call(init_cmd)
3950 raise CalledProcessError(retcode, init_cmd)
3952 if not P4Sync.run(self, depotPaths):
3955 # create a master branch and check out a work tree
3956 if gitBranchExists(self.branch):
3957 system([ "git", "branch", "master", self.branch ])
3958 if not self.cloneBare:
3959 system([ "git", "checkout", "-f" ])
3961 print('Not checking out any branch, use ' \
3962 '"git checkout -q -b master <branch>"')
3964 # auto-set this variable if invoked with --use-client-spec
3965 if self.useClientSpec_from_options:
3966 system("git config --bool git-p4.useclientspec true")
3970 class P4Unshelve(Command):
3972 Command.__init__(self)
3974 self.origin = "HEAD"
3975 self.description = "Unshelve a P4 changelist into a git commit"
3976 self.usage = "usage: %prog [options] changelist"
3978 optparse.make_option("--origin", dest="origin",
3979 help="Use this base revision instead of the default (%s)" % self.origin),
3981 self.verbose = False
3982 self.noCommit = False
3983 self.destbranch = "refs/remotes/p4-unshelved"
3985 def renameBranch(self, branch_name):
3986 """ Rename the existing branch to branch_name.N
3990 for i in range(0,1000):
3991 backup_branch_name = "{0}.{1}".format(branch_name, i)
3992 if not gitBranchExists(backup_branch_name):
3993 gitUpdateRef(backup_branch_name, branch_name) # copy ref to backup
3994 gitDeleteRef(branch_name)
3996 print("renamed old unshelve branch to {0}".format(backup_branch_name))
4000 sys.exit("gave up trying to rename existing branch {0}".format(sync.branch))
4002 def findLastP4Revision(self, starting_point):
4003 """ Look back from starting_point for the first commit created by git-p4
4004 to find the P4 commit we are based on, and the depot-paths.
4007 for parent in (range(65535)):
4008 log = extractLogMessageFromGitCommit("{0}^{1}".format(starting_point, parent))
4009 settings = extractSettingsGitLog(log)
4010 if 'change' in settings:
4013 sys.exit("could not find git-p4 commits in {0}".format(self.origin))
4015 def createShelveParent(self, change, branch_name, sync, origin):
4016 """ Create a commit matching the parent of the shelved changelist 'change'
4018 parent_description = p4_describe(change, shelved=True)
4019 parent_description['desc'] = 'parent for shelved changelist {}\n'.format(change)
4020 files = sync.extractFilesFromCommit(parent_description, shelved=False, shelved_cl=change)
4024 # if it was added in the shelved changelist, it won't exist in the parent
4025 if f['action'] in self.add_actions:
4028 # if it was deleted in the shelved changelist it must not be deleted
4029 # in the parent - we might even need to create it if the origin branch
4031 if f['action'] in self.delete_actions:
4034 parent_files.append(f)
4036 sync.commit(parent_description, parent_files, branch_name,
4037 parent=origin, allow_empty=True)
4038 print("created parent commit for {0} based on {1} in {2}".format(
4039 change, self.origin, branch_name))
4041 def run(self, args):
4045 if not gitBranchExists(self.origin):
4046 sys.exit("origin branch {0} does not exist".format(self.origin))
4051 # only one change at a time
4054 # if the target branch already exists, rename it
4055 branch_name = "{0}/{1}".format(self.destbranch, change)
4056 if gitBranchExists(branch_name):
4057 self.renameBranch(branch_name)
4058 sync.branch = branch_name
4060 sync.verbose = self.verbose
4061 sync.suppress_meta_comment = True
4063 settings = self.findLastP4Revision(self.origin)
4064 sync.depotPaths = settings['depot-paths']
4065 sync.branchPrefixes = sync.depotPaths
4068 sync.loadUserMapFromCache()
4071 # create a commit for the parent of the shelved changelist
4072 self.createShelveParent(change, branch_name, sync, self.origin)
4074 # create the commit for the shelved changelist itself
4075 description = p4_describe(change, True)
4076 files = sync.extractFilesFromCommit(description, True, change)
4078 sync.commit(description, files, branch_name, "")
4081 print("unshelved changelist {0} into {1}".format(change, branch_name))
4085 class P4Branches(Command):
4087 Command.__init__(self)
4089 self.description = ("Shows the git branches that hold imports and their "
4090 + "corresponding perforce depot paths")
4091 self.verbose = False
4093 def run(self, args):
4094 if originP4BranchesExist():
4095 createOrUpdateBranchesFromOrigin()
4097 cmdline = "git rev-parse --symbolic "
4098 cmdline += " --remotes"
4100 for line in read_pipe_lines(cmdline):
4103 if not line.startswith('p4/') or line == "p4/HEAD":
4107 log = extractLogMessageFromGitCommit("refs/remotes/%s" % branch)
4108 settings = extractSettingsGitLog(log)
4110 print("%s <= %s (%s)" % (branch, ",".join(settings["depot-paths"]), settings["change"]))
4113 class HelpFormatter(optparse.IndentedHelpFormatter):
4115 optparse.IndentedHelpFormatter.__init__(self)
4117 def format_description(self, description):
4119 return description + "\n"
4123 def printUsage(commands):
4124 print("usage: %s <command> [options]" % sys.argv[0])
4126 print("valid commands: %s" % ", ".join(commands))
4128 print("Try %s <command> --help for command specific help." % sys.argv[0])
4133 "submit" : P4Submit,
4134 "commit" : P4Submit,
4136 "rebase" : P4Rebase,
4138 "rollback" : P4RollBack,
4139 "branches" : P4Branches,
4140 "unshelve" : P4Unshelve,
4145 if len(sys.argv[1:]) == 0:
4146 printUsage(commands.keys())
4149 cmdName = sys.argv[1]
4151 klass = commands[cmdName]
4154 print("unknown command %s" % cmdName)
4156 printUsage(commands.keys())
4159 options = cmd.options
4160 cmd.gitdir = os.environ.get("GIT_DIR", None)
4164 options.append(optparse.make_option("--verbose", "-v", dest="verbose", action="store_true"))
4166 options.append(optparse.make_option("--git-dir", dest="gitdir"))
4168 parser = optparse.OptionParser(cmd.usage.replace("%prog", "%prog " + cmdName),
4170 description = cmd.description,
4171 formatter = HelpFormatter())
4174 (cmd, args) = parser.parse_args(sys.argv[2:], cmd);
4180 verbose = cmd.verbose
4182 if cmd.gitdir == None:
4183 cmd.gitdir = os.path.abspath(".git")
4184 if not isValidGitDir(cmd.gitdir):
4185 # "rev-parse --git-dir" without arguments will try $PWD/.git
4186 cmd.gitdir = read_pipe("git rev-parse --git-dir").strip()
4187 if os.path.exists(cmd.gitdir):
4188 cdup = read_pipe("git rev-parse --show-cdup").strip()
4192 if not isValidGitDir(cmd.gitdir):
4193 if isValidGitDir(cmd.gitdir + "/.git"):
4194 cmd.gitdir += "/.git"
4196 die("fatal: cannot locate git repository at %s" % cmd.gitdir)
4198 # so git commands invoked from the P4 workspace will succeed
4199 os.environ["GIT_DIR"] = cmd.gitdir
4201 if not cmd.run(args):
4206 if __name__ == '__main__':