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>
11 if sys.hexversion < 0x02040000:
12 # The limiter is the subprocess module
13 sys.stderr.write("git-p4: requires Python 2.4 or later.\n")
30 # support basestring in python3
34 # 'unicode' is undefined, must be Python 3
38 basestring = (str,bytes)
40 # 'unicode' exists, must be Python 2
44 basestring = basestring
47 from subprocess import CalledProcessError
49 # from python2.7:subprocess.py
50 # Exception classes used by this module.
51 class CalledProcessError(Exception):
52 """This exception is raised when a process run by check_call() returns
53 a non-zero exit status. The exit status will be stored in the
54 returncode attribute."""
55 def __init__(self, returncode, cmd):
56 self.returncode = returncode
59 return "Command '%s' returned non-zero exit status %d" % (self.cmd, self.returncode)
63 # Only labels/tags matching this will be imported/exported
64 defaultLabelRegexp = r'[a-zA-Z0-9_\-.]+$'
66 # The block size is reduced automatically if required
67 defaultBlockSize = 1<<20
69 p4_access_checked = False
71 def p4_build_cmd(cmd):
72 """Build a suitable p4 command line.
74 This consolidates building and returning a p4 command line into one
75 location. It means that hooking into the environment, or other configuration
76 can be done more easily.
80 user = gitConfig("git-p4.user")
82 real_cmd += ["-u",user]
84 password = gitConfig("git-p4.password")
86 real_cmd += ["-P", password]
88 port = gitConfig("git-p4.port")
90 real_cmd += ["-p", port]
92 host = gitConfig("git-p4.host")
94 real_cmd += ["-H", host]
96 client = gitConfig("git-p4.client")
98 real_cmd += ["-c", client]
100 retries = gitConfigInt("git-p4.retries")
102 # Perform 3 retries by default
105 # Provide a way to not pass this option by setting git-p4.retries to 0
106 real_cmd += ["-r", str(retries)]
108 if isinstance(cmd,basestring):
109 real_cmd = ' '.join(real_cmd) + ' ' + cmd
113 # now check that we can actually talk to the server
114 global p4_access_checked
115 if not p4_access_checked:
116 p4_access_checked = True # suppress access checks in p4_check_access itself
122 """ Return TRUE if the given path is a git directory (/path/to/dir/.git).
123 This won't automatically add ".git" to a directory.
125 d = read_pipe(["git", "--git-dir", path, "rev-parse", "--git-dir"], True).strip()
126 if not d or len(d) == 0:
131 def chdir(path, is_client_path=False):
132 """Do chdir to the given path, and set the PWD environment
133 variable for use by P4. It does not look at getcwd() output.
134 Since we're not using the shell, it is necessary to set the
135 PWD environment variable explicitly.
137 Normally, expand the path to force it to be absolute. This
138 addresses the use of relative path names inside P4 settings,
139 e.g. P4CONFIG=.p4config. P4 does not simply open the filename
140 as given; it looks for .p4config using PWD.
142 If is_client_path, the path was handed to us directly by p4,
143 and may be a symbolic link. Do not call os.getcwd() in this
144 case, because it will cause p4 to think that PWD is not inside
149 if not is_client_path:
151 os.environ['PWD'] = path
154 """Return free space in bytes on the disk of the given dirname."""
155 if platform.system() == 'Windows':
156 free_bytes = ctypes.c_ulonglong(0)
157 ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(os.getcwd()), None, None, ctypes.pointer(free_bytes))
158 return free_bytes.value
160 st = os.statvfs(os.getcwd())
161 return st.f_bavail * st.f_frsize
167 sys.stderr.write(msg + "\n")
170 def prompt(prompt_text):
171 """ Prompt the user to choose one of the choices
173 Choices are identified in the prompt_text by square brackets around
174 a single letter option.
176 choices = set(m.group(1) for m in re.finditer(r"\[(.)\]", prompt_text))
178 response = raw_input(prompt_text).strip().lower()
181 response = response[0]
182 if response in choices:
185 def write_pipe(c, stdin):
187 sys.stderr.write('Writing pipe: %s\n' % str(c))
189 expand = isinstance(c,basestring)
190 p = subprocess.Popen(c, stdin=subprocess.PIPE, shell=expand)
192 val = pipe.write(stdin)
195 die('Command failed: %s' % str(c))
199 def p4_write_pipe(c, stdin):
200 real_cmd = p4_build_cmd(c)
201 return write_pipe(real_cmd, stdin)
203 def read_pipe_full(c):
204 """ Read output from command. Returns a tuple
205 of the return status, stdout text and stderr
209 sys.stderr.write('Reading pipe: %s\n' % str(c))
211 expand = isinstance(c,basestring)
212 p = subprocess.Popen(c, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=expand)
213 (out, err) = p.communicate()
214 return (p.returncode, out, err)
216 def read_pipe(c, ignore_error=False):
217 """ Read output from command. Returns the output text on
218 success. On failure, terminates execution, unless
219 ignore_error is True, when it returns an empty string.
221 (retcode, out, err) = read_pipe_full(c)
226 die('Command failed: %s\nError: %s' % (str(c), err))
229 def read_pipe_text(c):
230 """ Read output from a command with trailing whitespace stripped.
231 On error, returns None.
233 (retcode, out, err) = read_pipe_full(c)
239 def p4_read_pipe(c, ignore_error=False):
240 real_cmd = p4_build_cmd(c)
241 return read_pipe(real_cmd, ignore_error)
243 def read_pipe_lines(c):
245 sys.stderr.write('Reading pipe: %s\n' % str(c))
247 expand = isinstance(c, basestring)
248 p = subprocess.Popen(c, stdout=subprocess.PIPE, shell=expand)
250 val = pipe.readlines()
251 if pipe.close() or p.wait():
252 die('Command failed: %s' % str(c))
256 def p4_read_pipe_lines(c):
257 """Specifically invoke p4 on the command supplied. """
258 real_cmd = p4_build_cmd(c)
259 return read_pipe_lines(real_cmd)
261 def p4_has_command(cmd):
262 """Ask p4 for help on this command. If it returns an error, the
263 command does not exist in this version of p4."""
264 real_cmd = p4_build_cmd(["help", cmd])
265 p = subprocess.Popen(real_cmd, stdout=subprocess.PIPE,
266 stderr=subprocess.PIPE)
268 return p.returncode == 0
270 def p4_has_move_command():
271 """See if the move command exists, that it supports -k, and that
272 it has not been administratively disabled. The arguments
273 must be correct, but the filenames do not have to exist. Use
274 ones with wildcards so even if they exist, it will fail."""
276 if not p4_has_command("move"):
278 cmd = p4_build_cmd(["move", "-k", "@from", "@to"])
279 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
280 (out, err) = p.communicate()
281 # return code will be 1 in either case
282 if err.find("Invalid option") >= 0:
284 if err.find("disabled") >= 0:
286 # assume it failed because @... was invalid changelist
289 def system(cmd, ignore_error=False):
290 expand = isinstance(cmd,basestring)
292 sys.stderr.write("executing %s\n" % str(cmd))
293 retcode = subprocess.call(cmd, shell=expand)
294 if retcode and not ignore_error:
295 raise CalledProcessError(retcode, cmd)
300 """Specifically invoke p4 as the system command. """
301 real_cmd = p4_build_cmd(cmd)
302 expand = isinstance(real_cmd, basestring)
303 retcode = subprocess.call(real_cmd, shell=expand)
305 raise CalledProcessError(retcode, real_cmd)
307 def die_bad_access(s):
308 die("failure accessing depot: {0}".format(s.rstrip()))
310 def p4_check_access(min_expiration=1):
311 """ Check if we can access Perforce - account still logged in
313 results = p4CmdList(["login", "-s"])
315 if len(results) == 0:
316 # should never get here: always get either some results, or a p4ExitCode
317 assert("could not parse response from perforce")
321 if 'p4ExitCode' in result:
322 # p4 returned non-zero status, e.g. P4PORT invalid, or p4 not in path
323 die_bad_access("could not run p4")
325 code = result.get("code")
327 # we get here if we couldn't connect and there was nothing to unmarshal
328 die_bad_access("could not connect")
331 expiry = result.get("TicketExpiration")
334 if expiry > min_expiration:
338 die_bad_access("perforce ticket expires in {0} seconds".format(expiry))
341 # account without a timeout - all ok
344 elif code == "error":
345 data = result.get("data")
347 die_bad_access("p4 error: {0}".format(data))
349 die_bad_access("unknown error")
353 die_bad_access("unknown error code {0}".format(code))
355 _p4_version_string = None
356 def p4_version_string():
357 """Read the version string, showing just the last line, which
358 hopefully is the interesting version bit.
361 Perforce - The Fast Software Configuration Management System.
362 Copyright 1995-2011 Perforce Software. All rights reserved.
363 Rev. P4/NTX86/2011.1/393975 (2011/12/16).
365 global _p4_version_string
366 if not _p4_version_string:
367 a = p4_read_pipe_lines(["-V"])
368 _p4_version_string = a[-1].rstrip()
369 return _p4_version_string
371 def p4_integrate(src, dest):
372 p4_system(["integrate", "-Dt", wildcard_encode(src), wildcard_encode(dest)])
374 def p4_sync(f, *options):
375 p4_system(["sync"] + list(options) + [wildcard_encode(f)])
378 # forcibly add file names with wildcards
379 if wildcard_present(f):
380 p4_system(["add", "-f", f])
382 p4_system(["add", f])
385 p4_system(["delete", wildcard_encode(f)])
387 def p4_edit(f, *options):
388 p4_system(["edit"] + list(options) + [wildcard_encode(f)])
391 p4_system(["revert", wildcard_encode(f)])
393 def p4_reopen(type, f):
394 p4_system(["reopen", "-t", type, wildcard_encode(f)])
396 def p4_reopen_in_change(changelist, files):
397 cmd = ["reopen", "-c", str(changelist)] + files
400 def p4_move(src, dest):
401 p4_system(["move", "-k", wildcard_encode(src), wildcard_encode(dest)])
403 def p4_last_change():
404 results = p4CmdList(["changes", "-m", "1"], skip_info=True)
405 return int(results[0]['change'])
407 def p4_describe(change, shelved=False):
408 """Make sure it returns a valid result by checking for
409 the presence of field "time". Return a dict of the
412 cmd = ["describe", "-s"]
417 ds = p4CmdList(cmd, skip_info=True)
419 die("p4 describe -s %d did not return 1 result: %s" % (change, str(ds)))
423 if "p4ExitCode" in d:
424 die("p4 describe -s %d exited with %d: %s" % (change, d["p4ExitCode"],
427 if d["code"] == "error":
428 die("p4 describe -s %d returned error code: %s" % (change, str(d)))
431 die("p4 describe -s %d returned no \"time\": %s" % (change, str(d)))
436 # Canonicalize the p4 type and return a tuple of the
437 # base type, plus any modifiers. See "p4 help filetypes"
438 # for a list and explanation.
440 def split_p4_type(p4type):
442 p4_filetypes_historical = {
443 "ctempobj": "binary+Sw",
449 "tempobj": "binary+FSw",
450 "ubinary": "binary+F",
451 "uresource": "resource+F",
452 "uxbinary": "binary+Fx",
453 "xbinary": "binary+x",
455 "xtempobj": "binary+Swx",
457 "xunicode": "unicode+x",
460 if p4type in p4_filetypes_historical:
461 p4type = p4_filetypes_historical[p4type]
463 s = p4type.split("+")
471 # return the raw p4 type of a file (text, text+ko, etc)
474 results = p4CmdList(["fstat", "-T", "headType", wildcard_encode(f)])
475 return results[0]['headType']
478 # Given a type base and modifier, return a regexp matching
479 # the keywords that can be expanded in the file
481 def p4_keywords_regexp_for_type(base, type_mods):
482 if base in ("text", "unicode", "binary"):
484 if "ko" in type_mods:
486 elif "k" in type_mods:
487 kwords = 'Id|Header|Author|Date|DateTime|Change|File|Revision'
491 \$ # Starts with a dollar, followed by...
492 (%s) # one of the keywords, followed by...
493 (:[^$\n]+)? # possibly an old expansion, followed by...
501 # Given a file, return a regexp matching the possible
502 # RCS keywords that will be expanded, or None for files
503 # with kw expansion turned off.
505 def p4_keywords_regexp_for_file(file):
506 if not os.path.exists(file):
509 (type_base, type_mods) = split_p4_type(p4_type(file))
510 return p4_keywords_regexp_for_type(type_base, type_mods)
512 def setP4ExecBit(file, mode):
513 # Reopens an already open file and changes the execute bit to match
514 # the execute bit setting in the passed in mode.
518 if not isModeExec(mode):
519 p4Type = getP4OpenedType(file)
520 p4Type = re.sub('^([cku]?)x(.*)', '\\1\\2', p4Type)
521 p4Type = re.sub('(.*?\+.*?)x(.*?)', '\\1\\2', p4Type)
522 if p4Type[-1] == "+":
523 p4Type = p4Type[0:-1]
525 p4_reopen(p4Type, file)
527 def getP4OpenedType(file):
528 # Returns the perforce file type for the given file.
530 result = p4_read_pipe(["opened", wildcard_encode(file)])
531 match = re.match(".*\((.+)\)( \*exclusive\*)?\r?$", result)
533 return match.group(1)
535 die("Could not determine file type for %s (result: '%s')" % (file, result))
537 # Return the set of all p4 labels
538 def getP4Labels(depotPaths):
540 if isinstance(depotPaths,basestring):
541 depotPaths = [depotPaths]
543 for l in p4CmdList(["labels"] + ["%s..." % p for p in depotPaths]):
549 # Return the set of all git tags
552 for line in read_pipe_lines(["git", "tag"]):
557 def diffTreePattern():
558 # This is a simple generator for the diff tree regex pattern. This could be
559 # a class variable if this and parseDiffTreeEntry were a part of a class.
560 pattern = re.compile(':(\d+) (\d+) (\w+) (\w+) ([A-Z])(\d+)?\t(.*?)((\t(.*))|$)')
564 def parseDiffTreeEntry(entry):
565 """Parses a single diff tree entry into its component elements.
567 See git-diff-tree(1) manpage for details about the format of the diff
568 output. This method returns a dictionary with the following elements:
570 src_mode - The mode of the source file
571 dst_mode - The mode of the destination file
572 src_sha1 - The sha1 for the source file
573 dst_sha1 - The sha1 fr the destination file
574 status - The one letter status of the diff (i.e. 'A', 'M', 'D', etc)
575 status_score - The score for the status (applicable for 'C' and 'R'
576 statuses). This is None if there is no score.
577 src - The path for the source file.
578 dst - The path for the destination file. This is only present for
579 copy or renames. If it is not present, this is None.
581 If the pattern is not matched, None is returned."""
583 match = diffTreePattern().next().match(entry)
586 'src_mode': match.group(1),
587 'dst_mode': match.group(2),
588 'src_sha1': match.group(3),
589 'dst_sha1': match.group(4),
590 'status': match.group(5),
591 'status_score': match.group(6),
592 'src': match.group(7),
593 'dst': match.group(10)
597 def isModeExec(mode):
598 # Returns True if the given git mode represents an executable file,
600 return mode[-3:] == "755"
602 class P4Exception(Exception):
603 """ Base class for exceptions from the p4 client """
604 def __init__(self, exit_code):
605 self.p4ExitCode = exit_code
607 class P4ServerException(P4Exception):
608 """ Base class for exceptions where we get some kind of marshalled up result from the server """
609 def __init__(self, exit_code, p4_result):
610 super(P4ServerException, self).__init__(exit_code)
611 self.p4_result = p4_result
612 self.code = p4_result[0]['code']
613 self.data = p4_result[0]['data']
615 class P4RequestSizeException(P4ServerException):
616 """ One of the maxresults or maxscanrows errors """
617 def __init__(self, exit_code, p4_result, limit):
618 super(P4RequestSizeException, self).__init__(exit_code, p4_result)
621 def isModeExecChanged(src_mode, dst_mode):
622 return isModeExec(src_mode) != isModeExec(dst_mode)
624 def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None, skip_info=False,
625 errors_as_exceptions=False):
627 if isinstance(cmd,basestring):
634 cmd = p4_build_cmd(cmd)
636 sys.stderr.write("Opening pipe: %s\n" % str(cmd))
638 # Use a temporary file to avoid deadlocks without
639 # subprocess.communicate(), which would put another copy
640 # of stdout into memory.
642 if stdin is not None:
643 stdin_file = tempfile.TemporaryFile(prefix='p4-stdin', mode=stdin_mode)
644 if isinstance(stdin,basestring):
645 stdin_file.write(stdin)
648 stdin_file.write(i + '\n')
652 p4 = subprocess.Popen(cmd,
655 stdout=subprocess.PIPE)
660 entry = marshal.load(p4.stdout)
662 if 'code' in entry and entry['code'] == 'info':
672 if errors_as_exceptions:
674 data = result[0].get('data')
676 m = re.search('Too many rows scanned \(over (\d+)\)', data)
678 m = re.search('Request too large \(over (\d+)\)', data)
681 limit = int(m.group(1))
682 raise P4RequestSizeException(exitCode, result, limit)
684 raise P4ServerException(exitCode, result)
686 raise P4Exception(exitCode)
689 entry["p4ExitCode"] = exitCode
695 list = p4CmdList(cmd)
701 def p4Where(depotPath):
702 if not depotPath.endswith("/"):
704 depotPathLong = depotPath + "..."
705 outputList = p4CmdList(["where", depotPathLong])
707 for entry in outputList:
708 if "depotFile" in entry:
709 # Search for the base client side depot path, as long as it starts with the branch's P4 path.
710 # The base path always ends with "/...".
711 if entry["depotFile"].find(depotPath) == 0 and entry["depotFile"][-4:] == "/...":
714 elif "data" in entry:
715 data = entry.get("data")
716 space = data.find(" ")
717 if data[:space] == depotPath:
722 if output["code"] == "error":
726 clientPath = output.get("path")
727 elif "data" in output:
728 data = output.get("data")
729 lastSpace = data.rfind(" ")
730 clientPath = data[lastSpace + 1:]
732 if clientPath.endswith("..."):
733 clientPath = clientPath[:-3]
736 def currentGitBranch():
737 return read_pipe_text(["git", "symbolic-ref", "--short", "-q", "HEAD"])
739 def isValidGitDir(path):
740 return git_dir(path) != None
742 def parseRevision(ref):
743 return read_pipe("git rev-parse %s" % ref).strip()
745 def branchExists(ref):
746 rev = read_pipe(["git", "rev-parse", "-q", "--verify", ref],
750 def extractLogMessageFromGitCommit(commit):
753 ## fixme: title is first line of commit, not 1st paragraph.
755 for log in read_pipe_lines(["git", "cat-file", "commit", commit]):
764 def extractSettingsGitLog(log):
766 for line in log.split("\n"):
768 m = re.search (r"^ *\[git-p4: (.*)\]$", line)
772 assignments = m.group(1).split (':')
773 for a in assignments:
775 key = vals[0].strip()
776 val = ('='.join (vals[1:])).strip()
777 if val.endswith ('\"') and val.startswith('"'):
782 paths = values.get("depot-paths")
784 paths = values.get("depot-path")
786 values['depot-paths'] = paths.split(',')
789 def gitBranchExists(branch):
790 proc = subprocess.Popen(["git", "rev-parse", branch],
791 stderr=subprocess.PIPE, stdout=subprocess.PIPE);
792 return proc.wait() == 0;
794 def gitUpdateRef(ref, newvalue):
795 subprocess.check_call(["git", "update-ref", ref, newvalue])
797 def gitDeleteRef(ref):
798 subprocess.check_call(["git", "update-ref", "-d", ref])
802 def gitConfig(key, typeSpecifier=None):
803 if key not in _gitConfig:
804 cmd = [ "git", "config" ]
806 cmd += [ typeSpecifier ]
808 s = read_pipe(cmd, ignore_error=True)
809 _gitConfig[key] = s.strip()
810 return _gitConfig[key]
812 def gitConfigBool(key):
813 """Return a bool, using git config --bool. It is True only if the
814 variable is set to true, and False if set to false or not present
817 if key not in _gitConfig:
818 _gitConfig[key] = gitConfig(key, '--bool') == "true"
819 return _gitConfig[key]
821 def gitConfigInt(key):
822 if key not in _gitConfig:
823 cmd = [ "git", "config", "--int", key ]
824 s = read_pipe(cmd, ignore_error=True)
827 _gitConfig[key] = int(gitConfig(key, '--int'))
829 _gitConfig[key] = None
830 return _gitConfig[key]
832 def gitConfigList(key):
833 if key not in _gitConfig:
834 s = read_pipe(["git", "config", "--get-all", key], ignore_error=True)
835 _gitConfig[key] = s.strip().splitlines()
836 if _gitConfig[key] == ['']:
838 return _gitConfig[key]
840 def p4BranchesInGit(branchesAreInRemotes=True):
841 """Find all the branches whose names start with "p4/", looking
842 in remotes or heads as specified by the argument. Return
843 a dictionary of { branch: revision } for each one found.
844 The branch names are the short names, without any
849 cmdline = "git rev-parse --symbolic "
850 if branchesAreInRemotes:
851 cmdline += "--remotes"
853 cmdline += "--branches"
855 for line in read_pipe_lines(cmdline):
859 if not line.startswith('p4/'):
861 # special symbolic ref to p4/master
862 if line == "p4/HEAD":
865 # strip off p4/ prefix
866 branch = line[len("p4/"):]
868 branches[branch] = parseRevision(line)
872 def branch_exists(branch):
873 """Make sure that the given ref name really exists."""
875 cmd = [ "git", "rev-parse", "--symbolic", "--verify", branch ]
876 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
877 out, _ = p.communicate()
880 # expect exactly one line of output: the branch name
881 return out.rstrip() == branch
883 def findUpstreamBranchPoint(head = "HEAD"):
884 branches = p4BranchesInGit()
885 # map from depot-path to branch name
886 branchByDepotPath = {}
887 for branch in branches.keys():
888 tip = branches[branch]
889 log = extractLogMessageFromGitCommit(tip)
890 settings = extractSettingsGitLog(log)
891 if "depot-paths" in settings:
892 paths = ",".join(settings["depot-paths"])
893 branchByDepotPath[paths] = "remotes/p4/" + branch
897 while parent < 65535:
898 commit = head + "~%s" % parent
899 log = extractLogMessageFromGitCommit(commit)
900 settings = extractSettingsGitLog(log)
901 if "depot-paths" in settings:
902 paths = ",".join(settings["depot-paths"])
903 if paths in branchByDepotPath:
904 return [branchByDepotPath[paths], settings]
908 return ["", settings]
910 def createOrUpdateBranchesFromOrigin(localRefPrefix = "refs/remotes/p4/", silent=True):
912 print("Creating/updating branch(es) in %s based on origin branch(es)"
915 originPrefix = "origin/p4/"
917 for line in read_pipe_lines("git rev-parse --symbolic --remotes"):
919 if (not line.startswith(originPrefix)) or line.endswith("HEAD"):
922 headName = line[len(originPrefix):]
923 remoteHead = localRefPrefix + headName
926 original = extractSettingsGitLog(extractLogMessageFromGitCommit(originHead))
927 if ('depot-paths' not in original
928 or 'change' not in original):
932 if not gitBranchExists(remoteHead):
934 print("creating %s" % remoteHead)
937 settings = extractSettingsGitLog(extractLogMessageFromGitCommit(remoteHead))
938 if 'change' in settings:
939 if settings['depot-paths'] == original['depot-paths']:
940 originP4Change = int(original['change'])
941 p4Change = int(settings['change'])
942 if originP4Change > p4Change:
943 print("%s (%s) is newer than %s (%s). "
944 "Updating p4 branch from origin."
945 % (originHead, originP4Change,
946 remoteHead, p4Change))
949 print("Ignoring: %s was imported from %s while "
950 "%s was imported from %s"
951 % (originHead, ','.join(original['depot-paths']),
952 remoteHead, ','.join(settings['depot-paths'])))
955 system("git update-ref %s %s" % (remoteHead, originHead))
957 def originP4BranchesExist():
958 return gitBranchExists("origin") or gitBranchExists("origin/p4") or gitBranchExists("origin/p4/master")
961 def p4ParseNumericChangeRange(parts):
962 changeStart = int(parts[0][1:])
963 if parts[1] == '#head':
964 changeEnd = p4_last_change()
966 changeEnd = int(parts[1])
968 return (changeStart, changeEnd)
970 def chooseBlockSize(blockSize):
974 return defaultBlockSize
976 def p4ChangesForPaths(depotPaths, changeRange, requestedBlockSize):
979 # Parse the change range into start and end. Try to find integer
980 # revision ranges as these can be broken up into blocks to avoid
981 # hitting server-side limits (maxrows, maxscanresults). But if
982 # that doesn't work, fall back to using the raw revision specifier
983 # strings, without using block mode.
985 if changeRange is None or changeRange == '':
987 changeEnd = p4_last_change()
988 block_size = chooseBlockSize(requestedBlockSize)
990 parts = changeRange.split(',')
991 assert len(parts) == 2
993 (changeStart, changeEnd) = p4ParseNumericChangeRange(parts)
994 block_size = chooseBlockSize(requestedBlockSize)
996 changeStart = parts[0][1:]
998 if requestedBlockSize:
999 die("cannot use --changes-block-size with non-numeric revisions")
1004 # Retrieve changes a block at a time, to prevent running
1005 # into a MaxResults/MaxScanRows error from the server. If
1006 # we _do_ hit one of those errors, turn down the block size
1012 end = min(changeEnd, changeStart + block_size)
1013 revisionRange = "%d,%d" % (changeStart, end)
1015 revisionRange = "%s,%s" % (changeStart, changeEnd)
1017 for p in depotPaths:
1018 cmd += ["%s...@%s" % (p, revisionRange)]
1022 result = p4CmdList(cmd, errors_as_exceptions=True)
1023 except P4RequestSizeException as e:
1025 block_size = e.limit
1026 elif block_size > e.limit:
1027 block_size = e.limit
1029 block_size = max(2, block_size // 2)
1031 if verbose: print("block size error, retrying with block size {0}".format(block_size))
1033 except P4Exception as e:
1034 die('Error retrieving changes description ({0})'.format(e.p4ExitCode))
1036 # Insert changes in chronological order
1037 for entry in reversed(result):
1038 if 'change' not in entry:
1040 changes.add(int(entry['change']))
1045 if end >= changeEnd:
1048 changeStart = end + 1
1050 changes = sorted(changes)
1053 def p4PathStartsWith(path, prefix):
1054 # This method tries to remedy a potential mixed-case issue:
1056 # If UserA adds //depot/DirA/file1
1057 # and UserB adds //depot/dira/file2
1059 # we may or may not have a problem. If you have core.ignorecase=true,
1060 # we treat DirA and dira as the same directory
1061 if gitConfigBool("core.ignorecase"):
1062 return path.lower().startswith(prefix.lower())
1063 return path.startswith(prefix)
1065 def getClientSpec():
1066 """Look at the p4 client spec, create a View() object that contains
1067 all the mappings, and return it."""
1069 specList = p4CmdList("client -o")
1070 if len(specList) != 1:
1071 die('Output from "client -o" is %d lines, expecting 1' %
1074 # dictionary of all client parameters
1077 # the //client/ name
1078 client_name = entry["Client"]
1080 # just the keys that start with "View"
1081 view_keys = [ k for k in entry.keys() if k.startswith("View") ]
1083 # hold this new View
1084 view = View(client_name)
1086 # append the lines, in order, to the view
1087 for view_num in range(len(view_keys)):
1088 k = "View%d" % view_num
1089 if k not in view_keys:
1090 die("Expected view key %s missing" % k)
1091 view.append(entry[k])
1095 def getClientRoot():
1096 """Grab the client directory."""
1098 output = p4CmdList("client -o")
1099 if len(output) != 1:
1100 die('Output from "client -o" is %d lines, expecting 1' % len(output))
1103 if "Root" not in entry:
1104 die('Client has no "Root"')
1106 return entry["Root"]
1109 # P4 wildcards are not allowed in filenames. P4 complains
1110 # if you simply add them, but you can force it with "-f", in
1111 # which case it translates them into %xx encoding internally.
1113 def wildcard_decode(path):
1114 # Search for and fix just these four characters. Do % last so
1115 # that fixing it does not inadvertently create new %-escapes.
1116 # Cannot have * in a filename in windows; untested as to
1117 # what p4 would do in such a case.
1118 if not platform.system() == "Windows":
1119 path = path.replace("%2A", "*")
1120 path = path.replace("%23", "#") \
1121 .replace("%40", "@") \
1122 .replace("%25", "%")
1125 def wildcard_encode(path):
1126 # do % first to avoid double-encoding the %s introduced here
1127 path = path.replace("%", "%25") \
1128 .replace("*", "%2A") \
1129 .replace("#", "%23") \
1130 .replace("@", "%40")
1133 def wildcard_present(path):
1134 m = re.search("[*#@%]", path)
1135 return m is not None
1137 class LargeFileSystem(object):
1138 """Base class for large file system support."""
1140 def __init__(self, writeToGitStream):
1141 self.largeFiles = set()
1142 self.writeToGitStream = writeToGitStream
1144 def generatePointer(self, cloneDestination, contentFile):
1145 """Return the content of a pointer file that is stored in Git instead of
1146 the actual content."""
1147 assert False, "Method 'generatePointer' required in " + self.__class__.__name__
1149 def pushFile(self, localLargeFile):
1150 """Push the actual content which is not stored in the Git repository to
1152 assert False, "Method 'pushFile' required in " + self.__class__.__name__
1154 def hasLargeFileExtension(self, relPath):
1156 lambda a, b: a or b,
1157 [relPath.endswith('.' + e) for e in gitConfigList('git-p4.largeFileExtensions')],
1161 def generateTempFile(self, contents):
1162 contentFile = tempfile.NamedTemporaryFile(prefix='git-p4-large-file', delete=False)
1164 contentFile.write(d)
1166 return contentFile.name
1168 def exceedsLargeFileThreshold(self, relPath, contents):
1169 if gitConfigInt('git-p4.largeFileThreshold'):
1170 contentsSize = sum(len(d) for d in contents)
1171 if contentsSize > gitConfigInt('git-p4.largeFileThreshold'):
1173 if gitConfigInt('git-p4.largeFileCompressedThreshold'):
1174 contentsSize = sum(len(d) for d in contents)
1175 if contentsSize <= gitConfigInt('git-p4.largeFileCompressedThreshold'):
1177 contentTempFile = self.generateTempFile(contents)
1178 compressedContentFile = tempfile.NamedTemporaryFile(prefix='git-p4-large-file', delete=True)
1179 with zipfile.ZipFile(compressedContentFile, mode='w') as zf:
1180 zf.write(contentTempFile, compress_type=zipfile.ZIP_DEFLATED)
1181 compressedContentsSize = zf.infolist()[0].compress_size
1182 os.remove(contentTempFile)
1183 if compressedContentsSize > gitConfigInt('git-p4.largeFileCompressedThreshold'):
1187 def addLargeFile(self, relPath):
1188 self.largeFiles.add(relPath)
1190 def removeLargeFile(self, relPath):
1191 self.largeFiles.remove(relPath)
1193 def isLargeFile(self, relPath):
1194 return relPath in self.largeFiles
1196 def processContent(self, git_mode, relPath, contents):
1197 """Processes the content of git fast import. This method decides if a
1198 file is stored in the large file system and handles all necessary
1200 if self.exceedsLargeFileThreshold(relPath, contents) or self.hasLargeFileExtension(relPath):
1201 contentTempFile = self.generateTempFile(contents)
1202 (pointer_git_mode, contents, localLargeFile) = self.generatePointer(contentTempFile)
1203 if pointer_git_mode:
1204 git_mode = pointer_git_mode
1206 # Move temp file to final location in large file system
1207 largeFileDir = os.path.dirname(localLargeFile)
1208 if not os.path.isdir(largeFileDir):
1209 os.makedirs(largeFileDir)
1210 shutil.move(contentTempFile, localLargeFile)
1211 self.addLargeFile(relPath)
1212 if gitConfigBool('git-p4.largeFilePush'):
1213 self.pushFile(localLargeFile)
1215 sys.stderr.write("%s moved to large file system (%s)\n" % (relPath, localLargeFile))
1216 return (git_mode, contents)
1218 class MockLFS(LargeFileSystem):
1219 """Mock large file system for testing."""
1221 def generatePointer(self, contentFile):
1222 """The pointer content is the original content prefixed with "pointer-".
1223 The local filename of the large file storage is derived from the file content.
1225 with open(contentFile, 'r') as f:
1228 pointerContents = 'pointer-' + content
1229 localLargeFile = os.path.join(os.getcwd(), '.git', 'mock-storage', 'local', content[:-1])
1230 return (gitMode, pointerContents, localLargeFile)
1232 def pushFile(self, localLargeFile):
1233 """The remote filename of the large file storage is the same as the local
1234 one but in a different directory.
1236 remotePath = os.path.join(os.path.dirname(localLargeFile), '..', 'remote')
1237 if not os.path.exists(remotePath):
1238 os.makedirs(remotePath)
1239 shutil.copyfile(localLargeFile, os.path.join(remotePath, os.path.basename(localLargeFile)))
1241 class GitLFS(LargeFileSystem):
1242 """Git LFS as backend for the git-p4 large file system.
1243 See https://git-lfs.github.com/ for details."""
1245 def __init__(self, *args):
1246 LargeFileSystem.__init__(self, *args)
1247 self.baseGitAttributes = []
1249 def generatePointer(self, contentFile):
1250 """Generate a Git LFS pointer for the content. Return LFS Pointer file
1251 mode and content which is stored in the Git repository instead of
1252 the actual content. Return also the new location of the actual
1255 if os.path.getsize(contentFile) == 0:
1256 return (None, '', None)
1258 pointerProcess = subprocess.Popen(
1259 ['git', 'lfs', 'pointer', '--file=' + contentFile],
1260 stdout=subprocess.PIPE
1262 pointerFile = pointerProcess.stdout.read()
1263 if pointerProcess.wait():
1264 os.remove(contentFile)
1265 die('git-lfs pointer command failed. Did you install the extension?')
1267 # Git LFS removed the preamble in the output of the 'pointer' command
1268 # starting from version 1.2.0. Check for the preamble here to support
1270 # c.f. https://github.com/github/git-lfs/commit/da2935d9a739592bc775c98d8ef4df9c72ea3b43
1271 if pointerFile.startswith('Git LFS pointer for'):
1272 pointerFile = re.sub(r'Git LFS pointer for.*\n\n', '', pointerFile)
1274 oid = re.search(r'^oid \w+:(\w+)', pointerFile, re.MULTILINE).group(1)
1275 localLargeFile = os.path.join(
1277 '.git', 'lfs', 'objects', oid[:2], oid[2:4],
1280 # LFS Spec states that pointer files should not have the executable bit set.
1282 return (gitMode, pointerFile, localLargeFile)
1284 def pushFile(self, localLargeFile):
1285 uploadProcess = subprocess.Popen(
1286 ['git', 'lfs', 'push', '--object-id', 'origin', os.path.basename(localLargeFile)]
1288 if uploadProcess.wait():
1289 die('git-lfs push command failed. Did you define a remote?')
1291 def generateGitAttributes(self):
1293 self.baseGitAttributes +
1297 '# Git LFS (see https://git-lfs.github.com/)\n',
1300 ['*.' + f.replace(' ', '[[:space:]]') + ' filter=lfs diff=lfs merge=lfs -text\n'
1301 for f in sorted(gitConfigList('git-p4.largeFileExtensions'))
1303 ['/' + f.replace(' ', '[[:space:]]') + ' filter=lfs diff=lfs merge=lfs -text\n'
1304 for f in sorted(self.largeFiles) if not self.hasLargeFileExtension(f)
1308 def addLargeFile(self, relPath):
1309 LargeFileSystem.addLargeFile(self, relPath)
1310 self.writeToGitStream('100644', '.gitattributes', self.generateGitAttributes())
1312 def removeLargeFile(self, relPath):
1313 LargeFileSystem.removeLargeFile(self, relPath)
1314 self.writeToGitStream('100644', '.gitattributes', self.generateGitAttributes())
1316 def processContent(self, git_mode, relPath, contents):
1317 if relPath == '.gitattributes':
1318 self.baseGitAttributes = contents
1319 return (git_mode, self.generateGitAttributes())
1321 return LargeFileSystem.processContent(self, git_mode, relPath, contents)
1324 delete_actions = ( "delete", "move/delete", "purge" )
1325 add_actions = ( "add", "branch", "move/add" )
1328 self.usage = "usage: %prog [options]"
1329 self.needsGit = True
1330 self.verbose = False
1332 # This is required for the "append" update_shelve action
1333 def ensure_value(self, attr, value):
1334 if not hasattr(self, attr) or getattr(self, attr) is None:
1335 setattr(self, attr, value)
1336 return getattr(self, attr)
1340 self.userMapFromPerforceServer = False
1341 self.myP4UserId = None
1345 return self.myP4UserId
1347 results = p4CmdList("user -o")
1350 self.myP4UserId = r['User']
1352 die("Could not find your p4 user id")
1354 def p4UserIsMe(self, p4User):
1355 # return True if the given p4 user is actually me
1356 me = self.p4UserId()
1357 if not p4User or p4User != me:
1362 def getUserCacheFilename(self):
1363 home = os.environ.get("HOME", os.environ.get("USERPROFILE"))
1364 return home + "/.gitp4-usercache.txt"
1366 def getUserMapFromPerforceServer(self):
1367 if self.userMapFromPerforceServer:
1372 for output in p4CmdList("users"):
1373 if "User" not in output:
1375 self.users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">"
1376 self.emails[output["Email"]] = output["User"]
1378 mapUserConfigRegex = re.compile(r"^\s*(\S+)\s*=\s*(.+)\s*<(\S+)>\s*$", re.VERBOSE)
1379 for mapUserConfig in gitConfigList("git-p4.mapUser"):
1380 mapUser = mapUserConfigRegex.findall(mapUserConfig)
1381 if mapUser and len(mapUser[0]) == 3:
1382 user = mapUser[0][0]
1383 fullname = mapUser[0][1]
1384 email = mapUser[0][2]
1385 self.users[user] = fullname + " <" + email + ">"
1386 self.emails[email] = user
1389 for (key, val) in self.users.items():
1390 s += "%s\t%s\n" % (key.expandtabs(1), val.expandtabs(1))
1392 open(self.getUserCacheFilename(), "wb").write(s)
1393 self.userMapFromPerforceServer = True
1395 def loadUserMapFromCache(self):
1397 self.userMapFromPerforceServer = False
1399 cache = open(self.getUserCacheFilename(), "rb")
1400 lines = cache.readlines()
1403 entry = line.strip().split("\t")
1404 self.users[entry[0]] = entry[1]
1406 self.getUserMapFromPerforceServer()
1408 class P4Debug(Command):
1410 Command.__init__(self)
1412 self.description = "A tool to debug the output of p4 -G."
1413 self.needsGit = False
1415 def run(self, args):
1417 for output in p4CmdList(args):
1418 print('Element: %d' % j)
1423 class P4RollBack(Command):
1425 Command.__init__(self)
1427 optparse.make_option("--local", dest="rollbackLocalBranches", action="store_true")
1429 self.description = "A tool to debug the multi-branch import. Don't use :)"
1430 self.rollbackLocalBranches = False
1432 def run(self, args):
1435 maxChange = int(args[0])
1437 if "p4ExitCode" in p4Cmd("changes -m 1"):
1438 die("Problems executing p4");
1440 if self.rollbackLocalBranches:
1441 refPrefix = "refs/heads/"
1442 lines = read_pipe_lines("git rev-parse --symbolic --branches")
1444 refPrefix = "refs/remotes/"
1445 lines = read_pipe_lines("git rev-parse --symbolic --remotes")
1448 if self.rollbackLocalBranches or (line.startswith("p4/") and line != "p4/HEAD\n"):
1450 ref = refPrefix + line
1451 log = extractLogMessageFromGitCommit(ref)
1452 settings = extractSettingsGitLog(log)
1454 depotPaths = settings['depot-paths']
1455 change = settings['change']
1459 if len(p4Cmd("changes -m 1 " + ' '.join (['%s...@%s' % (p, maxChange)
1460 for p in depotPaths]))) == 0:
1461 print("Branch %s did not exist at change %s, deleting." % (ref, maxChange))
1462 system("git update-ref -d %s `git rev-parse %s`" % (ref, ref))
1465 while change and int(change) > maxChange:
1468 print("%s is at %s ; rewinding towards %s" % (ref, change, maxChange))
1469 system("git update-ref %s \"%s^\"" % (ref, ref))
1470 log = extractLogMessageFromGitCommit(ref)
1471 settings = extractSettingsGitLog(log)
1474 depotPaths = settings['depot-paths']
1475 change = settings['change']
1478 print("%s rewound to %s" % (ref, change))
1482 class P4Submit(Command, P4UserMap):
1484 conflict_behavior_choices = ("ask", "skip", "quit")
1487 Command.__init__(self)
1488 P4UserMap.__init__(self)
1490 optparse.make_option("--origin", dest="origin"),
1491 optparse.make_option("-M", dest="detectRenames", action="store_true"),
1492 # preserve the user, requires relevant p4 permissions
1493 optparse.make_option("--preserve-user", dest="preserveUser", action="store_true"),
1494 optparse.make_option("--export-labels", dest="exportLabels", action="store_true"),
1495 optparse.make_option("--dry-run", "-n", dest="dry_run", action="store_true"),
1496 optparse.make_option("--prepare-p4-only", dest="prepare_p4_only", action="store_true"),
1497 optparse.make_option("--conflict", dest="conflict_behavior",
1498 choices=self.conflict_behavior_choices),
1499 optparse.make_option("--branch", dest="branch"),
1500 optparse.make_option("--shelve", dest="shelve", action="store_true",
1501 help="Shelve instead of submit. Shelved files are reverted, "
1502 "restoring the workspace to the state before the shelve"),
1503 optparse.make_option("--update-shelve", dest="update_shelve", action="append", type="int",
1504 metavar="CHANGELIST",
1505 help="update an existing shelved changelist, implies --shelve, "
1506 "repeat in-order for multiple shelved changelists"),
1507 optparse.make_option("--commit", dest="commit", metavar="COMMIT",
1508 help="submit only the specified commit(s), one commit or xxx..xxx"),
1509 optparse.make_option("--disable-rebase", dest="disable_rebase", action="store_true",
1510 help="Disable rebase after submit is completed. Can be useful if you "
1511 "work from a local git branch that is not master"),
1512 optparse.make_option("--disable-p4sync", dest="disable_p4sync", action="store_true",
1513 help="Skip Perforce sync of p4/master after submit or shelve"),
1515 self.description = """Submit changes from git to the perforce depot.\n
1516 The `p4-pre-submit` hook is executed if it exists and is executable.
1517 The hook takes no parameters and nothing from standard input. Exiting with
1518 non-zero status from this script prevents `git-p4 submit` from launching.
1520 One usage scenario is to run unit tests in the hook."""
1522 self.usage += " [name of git branch to submit into perforce depot]"
1524 self.detectRenames = False
1525 self.preserveUser = gitConfigBool("git-p4.preserveUser")
1526 self.dry_run = False
1528 self.update_shelve = list()
1530 self.disable_rebase = gitConfigBool("git-p4.disableRebase")
1531 self.disable_p4sync = gitConfigBool("git-p4.disableP4Sync")
1532 self.prepare_p4_only = False
1533 self.conflict_behavior = None
1534 self.isWindows = (platform.system() == "Windows")
1535 self.exportLabels = False
1536 self.p4HasMoveCommand = p4_has_move_command()
1539 if gitConfig('git-p4.largeFileSystem'):
1540 die("Large file system not supported for git-p4 submit command. Please remove it from config.")
1543 if len(p4CmdList("opened ...")) > 0:
1544 die("You have files opened with perforce! Close them before starting the sync.")
1546 def separate_jobs_from_description(self, message):
1547 """Extract and return a possible Jobs field in the commit
1548 message. It goes into a separate section in the p4 change
1551 A jobs line starts with "Jobs:" and looks like a new field
1552 in a form. Values are white-space separated on the same
1553 line or on following lines that start with a tab.
1555 This does not parse and extract the full git commit message
1556 like a p4 form. It just sees the Jobs: line as a marker
1557 to pass everything from then on directly into the p4 form,
1558 but outside the description section.
1560 Return a tuple (stripped log message, jobs string)."""
1562 m = re.search(r'^Jobs:', message, re.MULTILINE)
1564 return (message, None)
1566 jobtext = message[m.start():]
1567 stripped_message = message[:m.start()].rstrip()
1568 return (stripped_message, jobtext)
1570 def prepareLogMessage(self, template, message, jobs):
1571 """Edits the template returned from "p4 change -o" to insert
1572 the message in the Description field, and the jobs text in
1576 inDescriptionSection = False
1578 for line in template.split("\n"):
1579 if line.startswith("#"):
1580 result += line + "\n"
1583 if inDescriptionSection:
1584 if line.startswith("Files:") or line.startswith("Jobs:"):
1585 inDescriptionSection = False
1586 # insert Jobs section
1588 result += jobs + "\n"
1592 if line.startswith("Description:"):
1593 inDescriptionSection = True
1595 for messageLine in message.split("\n"):
1596 line += "\t" + messageLine + "\n"
1598 result += line + "\n"
1602 def patchRCSKeywords(self, file, pattern):
1603 # Attempt to zap the RCS keywords in a p4 controlled file matching the given pattern
1604 (handle, outFileName) = tempfile.mkstemp(dir='.')
1606 outFile = os.fdopen(handle, "w+")
1607 inFile = open(file, "r")
1608 regexp = re.compile(pattern, re.VERBOSE)
1609 for line in inFile.readlines():
1610 line = regexp.sub(r'$\1$', line)
1614 # Forcibly overwrite the original file
1616 shutil.move(outFileName, file)
1618 # cleanup our temporary file
1619 os.unlink(outFileName)
1620 print("Failed to strip RCS keywords in %s" % file)
1623 print("Patched up RCS keywords in %s" % file)
1625 def p4UserForCommit(self,id):
1626 # Return the tuple (perforce user,git email) for a given git commit id
1627 self.getUserMapFromPerforceServer()
1628 gitEmail = read_pipe(["git", "log", "--max-count=1",
1629 "--format=%ae", id])
1630 gitEmail = gitEmail.strip()
1631 if gitEmail not in self.emails:
1632 return (None,gitEmail)
1634 return (self.emails[gitEmail],gitEmail)
1636 def checkValidP4Users(self,commits):
1637 # check if any git authors cannot be mapped to p4 users
1639 (user,email) = self.p4UserForCommit(id)
1641 msg = "Cannot find p4 user for email %s in commit %s." % (email, id)
1642 if gitConfigBool("git-p4.allowMissingP4Users"):
1645 die("Error: %s\nSet git-p4.allowMissingP4Users to true to allow this." % msg)
1647 def lastP4Changelist(self):
1648 # Get back the last changelist number submitted in this client spec. This
1649 # then gets used to patch up the username in the change. If the same
1650 # client spec is being used by multiple processes then this might go
1652 results = p4CmdList("client -o") # find the current client
1656 client = r['Client']
1659 die("could not get client spec")
1660 results = p4CmdList(["changes", "-c", client, "-m", "1"])
1664 die("Could not get changelist number for last submit - cannot patch up user details")
1666 def modifyChangelistUser(self, changelist, newUser):
1667 # fixup the user field of a changelist after it has been submitted.
1668 changes = p4CmdList("change -o %s" % changelist)
1669 if len(changes) != 1:
1670 die("Bad output from p4 change modifying %s to user %s" %
1671 (changelist, newUser))
1674 if c['User'] == newUser: return # nothing to do
1676 input = marshal.dumps(c)
1678 result = p4CmdList("change -f -i", stdin=input)
1681 if r['code'] == 'error':
1682 die("Could not modify user field of changelist %s to %s:%s" % (changelist, newUser, r['data']))
1684 print("Updated user field for changelist %s to %s" % (changelist, newUser))
1686 die("Could not modify user field of changelist %s to %s" % (changelist, newUser))
1688 def canChangeChangelists(self):
1689 # check to see if we have p4 admin or super-user permissions, either of
1690 # which are required to modify changelists.
1691 results = p4CmdList(["protects", self.depotPath])
1694 if r['perm'] == 'admin':
1696 if r['perm'] == 'super':
1700 def prepareSubmitTemplate(self, changelist=None):
1701 """Run "p4 change -o" to grab a change specification template.
1702 This does not use "p4 -G", as it is nice to keep the submission
1703 template in original order, since a human might edit it.
1705 Remove lines in the Files section that show changes to files
1706 outside the depot path we're committing into."""
1708 [upstream, settings] = findUpstreamBranchPoint()
1711 # A Perforce Change Specification.
1713 # Change: The change number. 'new' on a new changelist.
1714 # Date: The date this specification was last modified.
1715 # Client: The client on which the changelist was created. Read-only.
1716 # User: The user who created the changelist.
1717 # Status: Either 'pending' or 'submitted'. Read-only.
1718 # Type: Either 'public' or 'restricted'. Default is 'public'.
1719 # Description: Comments about the changelist. Required.
1720 # Jobs: What opened jobs are to be closed by this changelist.
1721 # You may delete jobs from this list. (New changelists only.)
1722 # Files: What opened files from the default changelist are to be added
1723 # to this changelist. You may delete files from this list.
1724 # (New changelists only.)
1727 inFilesSection = False
1729 args = ['change', '-o']
1731 args.append(str(changelist))
1732 for entry in p4CmdList(args):
1733 if 'code' not in entry:
1735 if entry['code'] == 'stat':
1736 change_entry = entry
1738 if not change_entry:
1739 die('Failed to decode output of p4 change -o')
1740 for key, value in change_entry.iteritems():
1741 if key.startswith('File'):
1742 if 'depot-paths' in settings:
1743 if not [p for p in settings['depot-paths']
1744 if p4PathStartsWith(value, p)]:
1747 if not p4PathStartsWith(value, self.depotPath):
1749 files_list.append(value)
1751 # Output in the order expected by prepareLogMessage
1752 for key in ['Change', 'Client', 'User', 'Status', 'Description', 'Jobs']:
1753 if key not in change_entry:
1756 template += key + ':'
1757 if key == 'Description':
1759 for field_line in change_entry[key].splitlines():
1760 template += '\t'+field_line+'\n'
1761 if len(files_list) > 0:
1763 template += 'Files:\n'
1764 for path in files_list:
1765 template += '\t'+path+'\n'
1768 def edit_template(self, template_file):
1769 """Invoke the editor to let the user change the submission
1770 message. Return true if okay to continue with the submit."""
1772 # if configured to skip the editing part, just submit
1773 if gitConfigBool("git-p4.skipSubmitEdit"):
1776 # look at the modification time, to check later if the user saved
1778 mtime = os.stat(template_file).st_mtime
1781 if "P4EDITOR" in os.environ and (os.environ.get("P4EDITOR") != ""):
1782 editor = os.environ.get("P4EDITOR")
1784 editor = read_pipe("git var GIT_EDITOR").strip()
1785 system(["sh", "-c", ('%s "$@"' % editor), editor, template_file])
1787 # If the file was not saved, prompt to see if this patch should
1788 # be skipped. But skip this verification step if configured so.
1789 if gitConfigBool("git-p4.skipSubmitEditCheck"):
1792 # modification time updated means user saved the file
1793 if os.stat(template_file).st_mtime > mtime:
1796 response = prompt("Submit template unchanged. Submit anyway? [y]es, [n]o (skip this patch) ")
1802 def get_diff_description(self, editedFiles, filesToAdd, symlinks):
1804 if "P4DIFF" in os.environ:
1805 del(os.environ["P4DIFF"])
1807 for editedFile in editedFiles:
1808 diff += p4_read_pipe(['diff', '-du',
1809 wildcard_encode(editedFile)])
1813 for newFile in filesToAdd:
1814 newdiff += "==== new file ====\n"
1815 newdiff += "--- /dev/null\n"
1816 newdiff += "+++ %s\n" % newFile
1818 is_link = os.path.islink(newFile)
1819 expect_link = newFile in symlinks
1821 if is_link and expect_link:
1822 newdiff += "+%s\n" % os.readlink(newFile)
1824 f = open(newFile, "r")
1825 for line in f.readlines():
1826 newdiff += "+" + line
1829 return (diff + newdiff).replace('\r\n', '\n')
1831 def applyCommit(self, id):
1832 """Apply one commit, return True if it succeeded."""
1834 print("Applying", read_pipe(["git", "show", "-s",
1835 "--format=format:%h %s", id]))
1837 (p4User, gitEmail) = self.p4UserForCommit(id)
1839 diff = read_pipe_lines("git diff-tree -r %s \"%s^\" \"%s\"" % (self.diffOpts, id, id))
1841 filesToChangeType = set()
1842 filesToDelete = set()
1844 pureRenameCopy = set()
1846 filesToChangeExecBit = {}
1850 diff = parseDiffTreeEntry(line)
1851 modifier = diff['status']
1853 all_files.append(path)
1857 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
1858 filesToChangeExecBit[path] = diff['dst_mode']
1859 editedFiles.add(path)
1860 elif modifier == "A":
1861 filesToAdd.add(path)
1862 filesToChangeExecBit[path] = diff['dst_mode']
1863 if path in filesToDelete:
1864 filesToDelete.remove(path)
1866 dst_mode = int(diff['dst_mode'], 8)
1867 if dst_mode == 0o120000:
1870 elif modifier == "D":
1871 filesToDelete.add(path)
1872 if path in filesToAdd:
1873 filesToAdd.remove(path)
1874 elif modifier == "C":
1875 src, dest = diff['src'], diff['dst']
1876 all_files.append(dest)
1877 p4_integrate(src, dest)
1878 pureRenameCopy.add(dest)
1879 if diff['src_sha1'] != diff['dst_sha1']:
1881 pureRenameCopy.discard(dest)
1882 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
1884 pureRenameCopy.discard(dest)
1885 filesToChangeExecBit[dest] = diff['dst_mode']
1887 # turn off read-only attribute
1888 os.chmod(dest, stat.S_IWRITE)
1890 editedFiles.add(dest)
1891 elif modifier == "R":
1892 src, dest = diff['src'], diff['dst']
1893 all_files.append(dest)
1894 if self.p4HasMoveCommand:
1895 p4_edit(src) # src must be open before move
1896 p4_move(src, dest) # opens for (move/delete, move/add)
1898 p4_integrate(src, dest)
1899 if diff['src_sha1'] != diff['dst_sha1']:
1902 pureRenameCopy.add(dest)
1903 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
1904 if not self.p4HasMoveCommand:
1905 p4_edit(dest) # with move: already open, writable
1906 filesToChangeExecBit[dest] = diff['dst_mode']
1907 if not self.p4HasMoveCommand:
1909 os.chmod(dest, stat.S_IWRITE)
1911 filesToDelete.add(src)
1912 editedFiles.add(dest)
1913 elif modifier == "T":
1914 filesToChangeType.add(path)
1916 die("unknown modifier %s for %s" % (modifier, path))
1918 diffcmd = "git diff-tree --full-index -p \"%s\"" % (id)
1919 patchcmd = diffcmd + " | git apply "
1920 tryPatchCmd = patchcmd + "--check -"
1921 applyPatchCmd = patchcmd + "--check --apply -"
1922 patch_succeeded = True
1924 if os.system(tryPatchCmd) != 0:
1925 fixed_rcs_keywords = False
1926 patch_succeeded = False
1927 print("Unfortunately applying the change failed!")
1929 # Patch failed, maybe it's just RCS keyword woes. Look through
1930 # the patch to see if that's possible.
1931 if gitConfigBool("git-p4.attemptRCSCleanup"):
1935 for file in editedFiles | filesToDelete:
1936 # did this file's delta contain RCS keywords?
1937 pattern = p4_keywords_regexp_for_file(file)
1940 # this file is a possibility...look for RCS keywords.
1941 regexp = re.compile(pattern, re.VERBOSE)
1942 for line in read_pipe_lines(["git", "diff", "%s^..%s" % (id, id), file]):
1943 if regexp.search(line):
1945 print("got keyword match on %s in %s in %s" % (pattern, line, file))
1946 kwfiles[file] = pattern
1949 for file in kwfiles:
1951 print("zapping %s with %s" % (line,pattern))
1952 # File is being deleted, so not open in p4. Must
1953 # disable the read-only bit on windows.
1954 if self.isWindows and file not in editedFiles:
1955 os.chmod(file, stat.S_IWRITE)
1956 self.patchRCSKeywords(file, kwfiles[file])
1957 fixed_rcs_keywords = True
1959 if fixed_rcs_keywords:
1960 print("Retrying the patch with RCS keywords cleaned up")
1961 if os.system(tryPatchCmd) == 0:
1962 patch_succeeded = True
1964 if not patch_succeeded:
1965 for f in editedFiles:
1970 # Apply the patch for real, and do add/delete/+x handling.
1972 system(applyPatchCmd)
1974 for f in filesToChangeType:
1975 p4_edit(f, "-t", "auto")
1976 for f in filesToAdd:
1978 for f in filesToDelete:
1982 # Set/clear executable bits
1983 for f in filesToChangeExecBit.keys():
1984 mode = filesToChangeExecBit[f]
1985 setP4ExecBit(f, mode)
1988 if len(self.update_shelve) > 0:
1989 update_shelve = self.update_shelve.pop(0)
1990 p4_reopen_in_change(update_shelve, all_files)
1993 # Build p4 change description, starting with the contents
1994 # of the git commit message.
1996 logMessage = extractLogMessageFromGitCommit(id)
1997 logMessage = logMessage.strip()
1998 (logMessage, jobs) = self.separate_jobs_from_description(logMessage)
2000 template = self.prepareSubmitTemplate(update_shelve)
2001 submitTemplate = self.prepareLogMessage(template, logMessage, jobs)
2003 if self.preserveUser:
2004 submitTemplate += "\n######## Actual user %s, modified after commit\n" % p4User
2006 if self.checkAuthorship and not self.p4UserIsMe(p4User):
2007 submitTemplate += "######## git author %s does not match your p4 account.\n" % gitEmail
2008 submitTemplate += "######## Use option --preserve-user to modify authorship.\n"
2009 submitTemplate += "######## Variable git-p4.skipUserNameCheck hides this message.\n"
2011 separatorLine = "######## everything below this line is just the diff #######\n"
2012 if not self.prepare_p4_only:
2013 submitTemplate += separatorLine
2014 submitTemplate += self.get_diff_description(editedFiles, filesToAdd, symlinks)
2016 (handle, fileName) = tempfile.mkstemp()
2017 tmpFile = os.fdopen(handle, "w+b")
2019 submitTemplate = submitTemplate.replace("\n", "\r\n")
2020 tmpFile.write(submitTemplate)
2023 if self.prepare_p4_only:
2025 # Leave the p4 tree prepared, and the submit template around
2026 # and let the user decide what to do next
2029 print("P4 workspace prepared for submission.")
2030 print("To submit or revert, go to client workspace")
2031 print(" " + self.clientPath)
2033 print("To submit, use \"p4 submit\" to write a new description,")
2034 print("or \"p4 submit -i <%s\" to use the one prepared by" \
2035 " \"git p4\"." % fileName)
2036 print("You can delete the file \"%s\" when finished." % fileName)
2038 if self.preserveUser and p4User and not self.p4UserIsMe(p4User):
2039 print("To preserve change ownership by user %s, you must\n" \
2040 "do \"p4 change -f <change>\" after submitting and\n" \
2041 "edit the User field.")
2043 print("After submitting, renamed files must be re-synced.")
2044 print("Invoke \"p4 sync -f\" on each of these files:")
2045 for f in pureRenameCopy:
2049 print("To revert the changes, use \"p4 revert ...\", and delete")
2050 print("the submit template file \"%s\"" % fileName)
2052 print("Since the commit adds new files, they must be deleted:")
2053 for f in filesToAdd:
2059 # Let the user edit the change description, then submit it.
2064 if self.edit_template(fileName):
2065 # read the edited message and submit
2066 tmpFile = open(fileName, "rb")
2067 message = tmpFile.read()
2070 message = message.replace("\r\n", "\n")
2071 submitTemplate = message[:message.index(separatorLine)]
2074 p4_write_pipe(['shelve', '-r', '-i'], submitTemplate)
2076 p4_write_pipe(['shelve', '-i'], submitTemplate)
2078 p4_write_pipe(['submit', '-i'], submitTemplate)
2079 # The rename/copy happened by applying a patch that created a
2080 # new file. This leaves it writable, which confuses p4.
2081 for f in pureRenameCopy:
2084 if self.preserveUser:
2086 # Get last changelist number. Cannot easily get it from
2087 # the submit command output as the output is
2089 changelist = self.lastP4Changelist()
2090 self.modifyChangelistUser(changelist, p4User)
2096 if not submitted or self.shelve:
2098 print ("Reverting shelved files.")
2100 print ("Submission cancelled, undoing p4 changes.")
2101 for f in editedFiles | filesToDelete:
2103 for f in filesToAdd:
2110 # Export git tags as p4 labels. Create a p4 label and then tag
2112 def exportGitTags(self, gitTags):
2113 validLabelRegexp = gitConfig("git-p4.labelExportRegexp")
2114 if len(validLabelRegexp) == 0:
2115 validLabelRegexp = defaultLabelRegexp
2116 m = re.compile(validLabelRegexp)
2118 for name in gitTags:
2120 if not m.match(name):
2122 print("tag %s does not match regexp %s" % (name, validLabelRegexp))
2125 # Get the p4 commit this corresponds to
2126 logMessage = extractLogMessageFromGitCommit(name)
2127 values = extractSettingsGitLog(logMessage)
2129 if 'change' not in values:
2130 # a tag pointing to something not sent to p4; ignore
2132 print("git tag %s does not give a p4 commit" % name)
2135 changelist = values['change']
2137 # Get the tag details.
2141 for l in read_pipe_lines(["git", "cat-file", "-p", name]):
2144 if re.match(r'tag\s+', l):
2146 elif re.match(r'\s*$', l):
2153 body = ["lightweight tag imported by git p4\n"]
2155 # Create the label - use the same view as the client spec we are using
2156 clientSpec = getClientSpec()
2158 labelTemplate = "Label: %s\n" % name
2159 labelTemplate += "Description:\n"
2161 labelTemplate += "\t" + b + "\n"
2162 labelTemplate += "View:\n"
2163 for depot_side in clientSpec.mappings:
2164 labelTemplate += "\t%s\n" % depot_side
2167 print("Would create p4 label %s for tag" % name)
2168 elif self.prepare_p4_only:
2169 print("Not creating p4 label %s for tag due to option" \
2170 " --prepare-p4-only" % name)
2172 p4_write_pipe(["label", "-i"], labelTemplate)
2175 p4_system(["tag", "-l", name] +
2176 ["%s@%s" % (depot_side, changelist) for depot_side in clientSpec.mappings])
2179 print("created p4 label for tag %s" % name)
2181 def run(self, args):
2183 self.master = currentGitBranch()
2184 elif len(args) == 1:
2185 self.master = args[0]
2186 if not branchExists(self.master):
2187 die("Branch %s does not exist" % self.master)
2191 for i in self.update_shelve:
2193 sys.exit("invalid changelist %d" % i)
2196 allowSubmit = gitConfig("git-p4.allowSubmit")
2197 if len(allowSubmit) > 0 and not self.master in allowSubmit.split(","):
2198 die("%s is not in git-p4.allowSubmit" % self.master)
2200 [upstream, settings] = findUpstreamBranchPoint()
2201 self.depotPath = settings['depot-paths'][0]
2202 if len(self.origin) == 0:
2203 self.origin = upstream
2205 if len(self.update_shelve) > 0:
2208 if self.preserveUser:
2209 if not self.canChangeChangelists():
2210 die("Cannot preserve user names without p4 super-user or admin permissions")
2212 # if not set from the command line, try the config file
2213 if self.conflict_behavior is None:
2214 val = gitConfig("git-p4.conflict")
2216 if val not in self.conflict_behavior_choices:
2217 die("Invalid value '%s' for config git-p4.conflict" % val)
2220 self.conflict_behavior = val
2223 print("Origin branch is " + self.origin)
2225 if len(self.depotPath) == 0:
2226 print("Internal error: cannot locate perforce depot path from existing branches")
2229 self.useClientSpec = False
2230 if gitConfigBool("git-p4.useclientspec"):
2231 self.useClientSpec = True
2232 if self.useClientSpec:
2233 self.clientSpecDirs = getClientSpec()
2235 # Check for the existence of P4 branches
2236 branchesDetected = (len(p4BranchesInGit().keys()) > 1)
2238 if self.useClientSpec and not branchesDetected:
2239 # all files are relative to the client spec
2240 self.clientPath = getClientRoot()
2242 self.clientPath = p4Where(self.depotPath)
2244 if self.clientPath == "":
2245 die("Error: Cannot locate perforce checkout of %s in client view" % self.depotPath)
2247 print("Perforce checkout for depot path %s located at %s" % (self.depotPath, self.clientPath))
2248 self.oldWorkingDirectory = os.getcwd()
2250 # ensure the clientPath exists
2251 new_client_dir = False
2252 if not os.path.exists(self.clientPath):
2253 new_client_dir = True
2254 os.makedirs(self.clientPath)
2256 chdir(self.clientPath, is_client_path=True)
2258 print("Would synchronize p4 checkout in %s" % self.clientPath)
2260 print("Synchronizing p4 checkout...")
2262 # old one was destroyed, and maybe nobody told p4
2263 p4_sync("...", "-f")
2270 committish = self.master
2274 if self.commit != "":
2275 if self.commit.find("..") != -1:
2276 limits_ish = self.commit.split("..")
2277 for line in read_pipe_lines(["git", "rev-list", "--no-merges", "%s..%s" % (limits_ish[0], limits_ish[1])]):
2278 commits.append(line.strip())
2281 commits.append(self.commit)
2283 for line in read_pipe_lines(["git", "rev-list", "--no-merges", "%s..%s" % (self.origin, committish)]):
2284 commits.append(line.strip())
2287 if self.preserveUser or gitConfigBool("git-p4.skipUserNameCheck"):
2288 self.checkAuthorship = False
2290 self.checkAuthorship = True
2292 if self.preserveUser:
2293 self.checkValidP4Users(commits)
2296 # Build up a set of options to be passed to diff when
2297 # submitting each commit to p4.
2299 if self.detectRenames:
2300 # command-line -M arg
2301 self.diffOpts = "-M"
2303 # If not explicitly set check the config variable
2304 detectRenames = gitConfig("git-p4.detectRenames")
2306 if detectRenames.lower() == "false" or detectRenames == "":
2308 elif detectRenames.lower() == "true":
2309 self.diffOpts = "-M"
2311 self.diffOpts = "-M%s" % detectRenames
2313 # no command-line arg for -C or --find-copies-harder, just
2315 detectCopies = gitConfig("git-p4.detectCopies")
2316 if detectCopies.lower() == "false" or detectCopies == "":
2318 elif detectCopies.lower() == "true":
2319 self.diffOpts += " -C"
2321 self.diffOpts += " -C%s" % detectCopies
2323 if gitConfigBool("git-p4.detectCopiesHarder"):
2324 self.diffOpts += " --find-copies-harder"
2326 num_shelves = len(self.update_shelve)
2327 if num_shelves > 0 and num_shelves != len(commits):
2328 sys.exit("number of commits (%d) must match number of shelved changelist (%d)" %
2329 (len(commits), num_shelves))
2331 hooks_path = gitConfig("core.hooksPath")
2332 if len(hooks_path) <= 0:
2333 hooks_path = os.path.join(os.environ.get("GIT_DIR", ".git"), "hooks")
2335 hook_file = os.path.join(hooks_path, "p4-pre-submit")
2336 if os.path.isfile(hook_file) and os.access(hook_file, os.X_OK) and subprocess.call([hook_file]) != 0:
2340 # Apply the commits, one at a time. On failure, ask if should
2341 # continue to try the rest of the patches, or quit.
2344 print("Would apply")
2346 last = len(commits) - 1
2347 for i, commit in enumerate(commits):
2349 print(" ", read_pipe(["git", "show", "-s",
2350 "--format=format:%h %s", commit]))
2353 ok = self.applyCommit(commit)
2355 applied.append(commit)
2357 if self.prepare_p4_only and i < last:
2358 print("Processing only the first commit due to option" \
2359 " --prepare-p4-only")
2362 # prompt for what to do, or use the option/variable
2363 if self.conflict_behavior == "ask":
2364 print("What do you want to do?")
2365 response = prompt("[s]kip this commit but apply the rest, or [q]uit? ")
2366 elif self.conflict_behavior == "skip":
2368 elif self.conflict_behavior == "quit":
2371 die("Unknown conflict_behavior '%s'" %
2372 self.conflict_behavior)
2375 print("Skipping this commit, but applying the rest")
2380 chdir(self.oldWorkingDirectory)
2381 shelved_applied = "shelved" if self.shelve else "applied"
2384 elif self.prepare_p4_only:
2386 elif len(commits) == len(applied):
2387 print("All commits {0}!".format(shelved_applied))
2391 sync.branch = self.branch
2392 if self.disable_p4sync:
2393 sync.sync_origin_only()
2397 if not self.disable_rebase:
2402 if len(applied) == 0:
2403 print("No commits {0}.".format(shelved_applied))
2405 print("{0} only the commits marked with '*':".format(shelved_applied.capitalize()))
2411 print(star, read_pipe(["git", "show", "-s",
2412 "--format=format:%h %s", c]))
2413 print("You will have to do 'git p4 sync' and rebase.")
2415 if gitConfigBool("git-p4.exportLabels"):
2416 self.exportLabels = True
2418 if self.exportLabels:
2419 p4Labels = getP4Labels(self.depotPath)
2420 gitTags = getGitTags()
2422 missingGitTags = gitTags - p4Labels
2423 self.exportGitTags(missingGitTags)
2425 # exit with error unless everything applied perfectly
2426 if len(commits) != len(applied):
2432 """Represent a p4 view ("p4 help views"), and map files in a
2433 repo according to the view."""
2435 def __init__(self, client_name):
2437 self.client_prefix = "//%s/" % client_name
2438 # cache results of "p4 where" to lookup client file locations
2439 self.client_spec_path_cache = {}
2441 def append(self, view_line):
2442 """Parse a view line, splitting it into depot and client
2443 sides. Append to self.mappings, preserving order. This
2444 is only needed for tag creation."""
2446 # Split the view line into exactly two words. P4 enforces
2447 # structure on these lines that simplifies this quite a bit.
2449 # Either or both words may be double-quoted.
2450 # Single quotes do not matter.
2451 # Double-quote marks cannot occur inside the words.
2452 # A + or - prefix is also inside the quotes.
2453 # There are no quotes unless they contain a space.
2454 # The line is already white-space stripped.
2455 # The two words are separated by a single space.
2457 if view_line[0] == '"':
2458 # First word is double quoted. Find its end.
2459 close_quote_index = view_line.find('"', 1)
2460 if close_quote_index <= 0:
2461 die("No first-word closing quote found: %s" % view_line)
2462 depot_side = view_line[1:close_quote_index]
2463 # skip closing quote and space
2464 rhs_index = close_quote_index + 1 + 1
2466 space_index = view_line.find(" ")
2467 if space_index <= 0:
2468 die("No word-splitting space found: %s" % view_line)
2469 depot_side = view_line[0:space_index]
2470 rhs_index = space_index + 1
2472 # prefix + means overlay on previous mapping
2473 if depot_side.startswith("+"):
2474 depot_side = depot_side[1:]
2476 # prefix - means exclude this path, leave out of mappings
2478 if depot_side.startswith("-"):
2480 depot_side = depot_side[1:]
2483 self.mappings.append(depot_side)
2485 def convert_client_path(self, clientFile):
2486 # chop off //client/ part to make it relative
2487 if not clientFile.startswith(self.client_prefix):
2488 die("No prefix '%s' on clientFile '%s'" %
2489 (self.client_prefix, clientFile))
2490 return clientFile[len(self.client_prefix):]
2492 def update_client_spec_path_cache(self, files):
2493 """ Caching file paths by "p4 where" batch query """
2495 # List depot file paths exclude that already cached
2496 fileArgs = [f['path'] for f in files if f['path'] not in self.client_spec_path_cache]
2498 if len(fileArgs) == 0:
2499 return # All files in cache
2501 where_result = p4CmdList(["-x", "-", "where"], stdin=fileArgs)
2502 for res in where_result:
2503 if "code" in res and res["code"] == "error":
2504 # assume error is "... file(s) not in client view"
2506 if "clientFile" not in res:
2507 die("No clientFile in 'p4 where' output")
2509 # it will list all of them, but only one not unmap-ped
2511 if gitConfigBool("core.ignorecase"):
2512 res['depotFile'] = res['depotFile'].lower()
2513 self.client_spec_path_cache[res['depotFile']] = self.convert_client_path(res["clientFile"])
2515 # not found files or unmap files set to ""
2516 for depotFile in fileArgs:
2517 if gitConfigBool("core.ignorecase"):
2518 depotFile = depotFile.lower()
2519 if depotFile not in self.client_spec_path_cache:
2520 self.client_spec_path_cache[depotFile] = ""
2522 def map_in_client(self, depot_path):
2523 """Return the relative location in the client where this
2524 depot file should live. Returns "" if the file should
2525 not be mapped in the client."""
2527 if gitConfigBool("core.ignorecase"):
2528 depot_path = depot_path.lower()
2530 if depot_path in self.client_spec_path_cache:
2531 return self.client_spec_path_cache[depot_path]
2533 die( "Error: %s is not found in client spec path" % depot_path )
2536 def cloneExcludeCallback(option, opt_str, value, parser):
2537 # prepend "/" because the first "/" was consumed as part of the option itself.
2538 # ("-//depot/A/..." becomes "/depot/A/..." after option parsing)
2539 parser.values.cloneExclude += ["/" + re.sub(r"\.\.\.$", "", value)]
2541 class P4Sync(Command, P4UserMap):
2544 Command.__init__(self)
2545 P4UserMap.__init__(self)
2547 optparse.make_option("--branch", dest="branch"),
2548 optparse.make_option("--detect-branches", dest="detectBranches", action="store_true"),
2549 optparse.make_option("--changesfile", dest="changesFile"),
2550 optparse.make_option("--silent", dest="silent", action="store_true"),
2551 optparse.make_option("--detect-labels", dest="detectLabels", action="store_true"),
2552 optparse.make_option("--import-labels", dest="importLabels", action="store_true"),
2553 optparse.make_option("--import-local", dest="importIntoRemotes", action="store_false",
2554 help="Import into refs/heads/ , not refs/remotes"),
2555 optparse.make_option("--max-changes", dest="maxChanges",
2556 help="Maximum number of changes to import"),
2557 optparse.make_option("--changes-block-size", dest="changes_block_size", type="int",
2558 help="Internal block size to use when iteratively calling p4 changes"),
2559 optparse.make_option("--keep-path", dest="keepRepoPath", action='store_true',
2560 help="Keep entire BRANCH/DIR/SUBDIR prefix during import"),
2561 optparse.make_option("--use-client-spec", dest="useClientSpec", action='store_true',
2562 help="Only sync files that are included in the Perforce Client Spec"),
2563 optparse.make_option("-/", dest="cloneExclude",
2564 action="callback", callback=cloneExcludeCallback, type="string",
2565 help="exclude depot path"),
2567 self.description = """Imports from Perforce into a git repository.\n
2569 //depot/my/project/ -- to import the current head
2570 //depot/my/project/@all -- to import everything
2571 //depot/my/project/@1,6 -- to import only from revision 1 to 6
2573 (a ... is not needed in the path p4 specification, it's added implicitly)"""
2575 self.usage += " //depot/path[@revRange]"
2577 self.createdBranches = set()
2578 self.committedChanges = set()
2580 self.detectBranches = False
2581 self.detectLabels = False
2582 self.importLabels = False
2583 self.changesFile = ""
2584 self.syncWithOrigin = True
2585 self.importIntoRemotes = True
2586 self.maxChanges = ""
2587 self.changes_block_size = None
2588 self.keepRepoPath = False
2589 self.depotPaths = None
2590 self.p4BranchesInGit = []
2591 self.cloneExclude = []
2592 self.useClientSpec = False
2593 self.useClientSpec_from_options = False
2594 self.clientSpecDirs = None
2595 self.tempBranches = []
2596 self.tempBranchLocation = "refs/git-p4-tmp"
2597 self.largeFileSystem = None
2598 self.suppress_meta_comment = False
2600 if gitConfig('git-p4.largeFileSystem'):
2601 largeFileSystemConstructor = globals()[gitConfig('git-p4.largeFileSystem')]
2602 self.largeFileSystem = largeFileSystemConstructor(
2603 lambda git_mode, relPath, contents: self.writeToGitStream(git_mode, relPath, contents)
2606 if gitConfig("git-p4.syncFromOrigin") == "false":
2607 self.syncWithOrigin = False
2609 self.depotPaths = []
2610 self.changeRange = ""
2611 self.previousDepotPaths = []
2612 self.hasOrigin = False
2614 # map from branch depot path to parent branch
2615 self.knownBranches = {}
2616 self.initialParents = {}
2618 self.tz = "%+03d%02d" % (- time.timezone / 3600, ((- time.timezone % 3600) / 60))
2621 # Force a checkpoint in fast-import and wait for it to finish
2622 def checkpoint(self):
2623 self.gitStream.write("checkpoint\n\n")
2624 self.gitStream.write("progress checkpoint\n\n")
2625 out = self.gitOutput.readline()
2627 print("checkpoint finished: " + out)
2629 def isPathWanted(self, path):
2630 for p in self.cloneExclude:
2632 if p4PathStartsWith(path, p):
2634 # "-//depot/file1" without a trailing "/" should only exclude "file1", but not "file111" or "file1_dir/file2"
2635 elif path.lower() == p.lower():
2637 for p in self.depotPaths:
2638 if p4PathStartsWith(path, p):
2642 def extractFilesFromCommit(self, commit, shelved=False, shelved_cl = 0):
2645 while "depotFile%s" % fnum in commit:
2646 path = commit["depotFile%s" % fnum]
2647 found = self.isPathWanted(path)
2654 file["rev"] = commit["rev%s" % fnum]
2655 file["action"] = commit["action%s" % fnum]
2656 file["type"] = commit["type%s" % fnum]
2658 file["shelved_cl"] = int(shelved_cl)
2663 def extractJobsFromCommit(self, commit):
2666 while "job%s" % jnum in commit:
2667 job = commit["job%s" % jnum]
2672 def stripRepoPath(self, path, prefixes):
2673 """When streaming files, this is called to map a p4 depot path
2674 to where it should go in git. The prefixes are either
2675 self.depotPaths, or self.branchPrefixes in the case of
2676 branch detection."""
2678 if self.useClientSpec:
2679 # branch detection moves files up a level (the branch name)
2680 # from what client spec interpretation gives
2681 path = self.clientSpecDirs.map_in_client(path)
2682 if self.detectBranches:
2683 for b in self.knownBranches:
2684 if p4PathStartsWith(path, b + "/"):
2685 path = path[len(b)+1:]
2687 elif self.keepRepoPath:
2688 # Preserve everything in relative path name except leading
2689 # //depot/; just look at first prefix as they all should
2690 # be in the same depot.
2691 depot = re.sub("^(//[^/]+/).*", r'\1', prefixes[0])
2692 if p4PathStartsWith(path, depot):
2693 path = path[len(depot):]
2697 if p4PathStartsWith(path, p):
2698 path = path[len(p):]
2701 path = wildcard_decode(path)
2704 def splitFilesIntoBranches(self, commit):
2705 """Look at each depotFile in the commit to figure out to what
2706 branch it belongs."""
2708 if self.clientSpecDirs:
2709 files = self.extractFilesFromCommit(commit)
2710 self.clientSpecDirs.update_client_spec_path_cache(files)
2714 while "depotFile%s" % fnum in commit:
2715 path = commit["depotFile%s" % fnum]
2716 found = self.isPathWanted(path)
2723 file["rev"] = commit["rev%s" % fnum]
2724 file["action"] = commit["action%s" % fnum]
2725 file["type"] = commit["type%s" % fnum]
2728 # start with the full relative path where this file would
2730 if self.useClientSpec:
2731 relPath = self.clientSpecDirs.map_in_client(path)
2733 relPath = self.stripRepoPath(path, self.depotPaths)
2735 for branch in self.knownBranches.keys():
2736 # add a trailing slash so that a commit into qt/4.2foo
2737 # doesn't end up in qt/4.2, e.g.
2738 if p4PathStartsWith(relPath, branch + "/"):
2739 if branch not in branches:
2740 branches[branch] = []
2741 branches[branch].append(file)
2746 def writeToGitStream(self, gitMode, relPath, contents):
2747 self.gitStream.write('M %s inline %s\n' % (gitMode, relPath))
2748 self.gitStream.write('data %d\n' % sum(len(d) for d in contents))
2750 self.gitStream.write(d)
2751 self.gitStream.write('\n')
2753 def encodeWithUTF8(self, path):
2755 path.decode('ascii')
2758 if gitConfig('git-p4.pathEncoding'):
2759 encoding = gitConfig('git-p4.pathEncoding')
2760 path = path.decode(encoding, 'replace').encode('utf8', 'replace')
2762 print('Path with non-ASCII characters detected. Used %s to encode: %s ' % (encoding, path))
2765 # output one file from the P4 stream
2766 # - helper for streamP4Files
2768 def streamOneP4File(self, file, contents):
2769 relPath = self.stripRepoPath(file['depotFile'], self.branchPrefixes)
2770 relPath = self.encodeWithUTF8(relPath)
2772 if 'fileSize' in self.stream_file:
2773 size = int(self.stream_file['fileSize'])
2775 size = 0 # deleted files don't get a fileSize apparently
2776 sys.stdout.write('\r%s --> %s (%i MB)\n' % (file['depotFile'], relPath, size/1024/1024))
2779 (type_base, type_mods) = split_p4_type(file["type"])
2782 if "x" in type_mods:
2784 if type_base == "symlink":
2786 # p4 print on a symlink sometimes contains "target\n";
2787 # if it does, remove the newline
2788 data = ''.join(contents)
2790 # Some version of p4 allowed creating a symlink that pointed
2791 # to nothing. This causes p4 errors when checking out such
2792 # a change, and errors here too. Work around it by ignoring
2793 # the bad symlink; hopefully a future change fixes it.
2794 print("\nIgnoring empty symlink in %s" % file['depotFile'])
2796 elif data[-1] == '\n':
2797 contents = [data[:-1]]
2801 if type_base == "utf16":
2802 # p4 delivers different text in the python output to -G
2803 # than it does when using "print -o", or normal p4 client
2804 # operations. utf16 is converted to ascii or utf8, perhaps.
2805 # But ascii text saved as -t utf16 is completely mangled.
2806 # Invoke print -o to get the real contents.
2808 # On windows, the newlines will always be mangled by print, so put
2809 # them back too. This is not needed to the cygwin windows version,
2810 # just the native "NT" type.
2813 text = p4_read_pipe(['print', '-q', '-o', '-', '%s@%s' % (file['depotFile'], file['change'])])
2814 except Exception as e:
2815 if 'Translation of file content failed' in str(e):
2816 type_base = 'binary'
2820 if p4_version_string().find('/NT') >= 0:
2821 text = text.replace('\r\n', '\n')
2824 if type_base == "apple":
2825 # Apple filetype files will be streamed as a concatenation of
2826 # its appledouble header and the contents. This is useless
2827 # on both macs and non-macs. If using "print -q -o xx", it
2828 # will create "xx" with the data, and "%xx" with the header.
2829 # This is also not very useful.
2831 # Ideally, someday, this script can learn how to generate
2832 # appledouble files directly and import those to git, but
2833 # non-mac machines can never find a use for apple filetype.
2834 print("\nIgnoring apple filetype file %s" % file['depotFile'])
2837 # Note that we do not try to de-mangle keywords on utf16 files,
2838 # even though in theory somebody may want that.
2839 pattern = p4_keywords_regexp_for_type(type_base, type_mods)
2841 regexp = re.compile(pattern, re.VERBOSE)
2842 text = ''.join(contents)
2843 text = regexp.sub(r'$\1$', text)
2846 if self.largeFileSystem:
2847 (git_mode, contents) = self.largeFileSystem.processContent(git_mode, relPath, contents)
2849 self.writeToGitStream(git_mode, relPath, contents)
2851 def streamOneP4Deletion(self, file):
2852 relPath = self.stripRepoPath(file['path'], self.branchPrefixes)
2853 relPath = self.encodeWithUTF8(relPath)
2855 sys.stdout.write("delete %s\n" % relPath)
2857 self.gitStream.write("D %s\n" % relPath)
2859 if self.largeFileSystem and self.largeFileSystem.isLargeFile(relPath):
2860 self.largeFileSystem.removeLargeFile(relPath)
2862 # handle another chunk of streaming data
2863 def streamP4FilesCb(self, marshalled):
2865 # catch p4 errors and complain
2867 if "code" in marshalled:
2868 if marshalled["code"] == "error":
2869 if "data" in marshalled:
2870 err = marshalled["data"].rstrip()
2872 if not err and 'fileSize' in self.stream_file:
2873 required_bytes = int((4 * int(self.stream_file["fileSize"])) - calcDiskFree())
2874 if required_bytes > 0:
2875 err = 'Not enough space left on %s! Free at least %i MB.' % (
2876 os.getcwd(), required_bytes/1024/1024
2881 if self.stream_have_file_info:
2882 if "depotFile" in self.stream_file:
2883 f = self.stream_file["depotFile"]
2884 # force a failure in fast-import, else an empty
2885 # commit will be made
2886 self.gitStream.write("\n")
2887 self.gitStream.write("die-now\n")
2888 self.gitStream.close()
2889 # ignore errors, but make sure it exits first
2890 self.importProcess.wait()
2892 die("Error from p4 print for %s: %s" % (f, err))
2894 die("Error from p4 print: %s" % err)
2896 if 'depotFile' in marshalled and self.stream_have_file_info:
2897 # start of a new file - output the old one first
2898 self.streamOneP4File(self.stream_file, self.stream_contents)
2899 self.stream_file = {}
2900 self.stream_contents = []
2901 self.stream_have_file_info = False
2903 # pick up the new file information... for the
2904 # 'data' field we need to append to our array
2905 for k in marshalled.keys():
2907 if 'streamContentSize' not in self.stream_file:
2908 self.stream_file['streamContentSize'] = 0
2909 self.stream_file['streamContentSize'] += len(marshalled['data'])
2910 self.stream_contents.append(marshalled['data'])
2912 self.stream_file[k] = marshalled[k]
2915 'streamContentSize' in self.stream_file and
2916 'fileSize' in self.stream_file and
2917 'depotFile' in self.stream_file):
2918 size = int(self.stream_file["fileSize"])
2920 progress = 100*self.stream_file['streamContentSize']/size
2921 sys.stdout.write('\r%s %d%% (%i MB)' % (self.stream_file['depotFile'], progress, int(size/1024/1024)))
2924 self.stream_have_file_info = True
2926 # Stream directly from "p4 files" into "git fast-import"
2927 def streamP4Files(self, files):
2933 filesForCommit.append(f)
2934 if f['action'] in self.delete_actions:
2935 filesToDelete.append(f)
2937 filesToRead.append(f)
2940 for f in filesToDelete:
2941 self.streamOneP4Deletion(f)
2943 if len(filesToRead) > 0:
2944 self.stream_file = {}
2945 self.stream_contents = []
2946 self.stream_have_file_info = False
2948 # curry self argument
2949 def streamP4FilesCbSelf(entry):
2950 self.streamP4FilesCb(entry)
2953 for f in filesToRead:
2954 if 'shelved_cl' in f:
2955 # Handle shelved CLs using the "p4 print file@=N" syntax to print
2957 fileArg = '%s@=%d' % (f['path'], f['shelved_cl'])
2959 fileArg = '%s#%s' % (f['path'], f['rev'])
2961 fileArgs.append(fileArg)
2963 p4CmdList(["-x", "-", "print"],
2965 cb=streamP4FilesCbSelf)
2968 if 'depotFile' in self.stream_file:
2969 self.streamOneP4File(self.stream_file, self.stream_contents)
2971 def make_email(self, userid):
2972 if userid in self.users:
2973 return self.users[userid]
2975 return "%s <a@b>" % userid
2977 def streamTag(self, gitStream, labelName, labelDetails, commit, epoch):
2978 """ Stream a p4 tag.
2979 commit is either a git commit, or a fast-import mark, ":<p4commit>"
2983 print("writing tag %s for commit %s" % (labelName, commit))
2984 gitStream.write("tag %s\n" % labelName)
2985 gitStream.write("from %s\n" % commit)
2987 if 'Owner' in labelDetails:
2988 owner = labelDetails["Owner"]
2992 # Try to use the owner of the p4 label, or failing that,
2993 # the current p4 user id.
2995 email = self.make_email(owner)
2997 email = self.make_email(self.p4UserId())
2998 tagger = "%s %s %s" % (email, epoch, self.tz)
3000 gitStream.write("tagger %s\n" % tagger)
3002 print("labelDetails=",labelDetails)
3003 if 'Description' in labelDetails:
3004 description = labelDetails['Description']
3006 description = 'Label from git p4'
3008 gitStream.write("data %d\n" % len(description))
3009 gitStream.write(description)
3010 gitStream.write("\n")
3012 def inClientSpec(self, path):
3013 if not self.clientSpecDirs:
3015 inClientSpec = self.clientSpecDirs.map_in_client(path)
3016 if not inClientSpec and self.verbose:
3017 print('Ignoring file outside of client spec: {0}'.format(path))
3020 def hasBranchPrefix(self, path):
3021 if not self.branchPrefixes:
3023 hasPrefix = [p for p in self.branchPrefixes
3024 if p4PathStartsWith(path, p)]
3025 if not hasPrefix and self.verbose:
3026 print('Ignoring file outside of prefix: {0}'.format(path))
3029 def commit(self, details, files, branch, parent = "", allow_empty=False):
3030 epoch = details["time"]
3031 author = details["user"]
3032 jobs = self.extractJobsFromCommit(details)
3035 print('commit into {0}'.format(branch))
3037 if self.clientSpecDirs:
3038 self.clientSpecDirs.update_client_spec_path_cache(files)
3040 files = [f for f in files
3041 if self.inClientSpec(f['path']) and self.hasBranchPrefix(f['path'])]
3043 if gitConfigBool('git-p4.keepEmptyCommits'):
3046 if not files and not allow_empty:
3047 print('Ignoring revision {0} as it would produce an empty commit.'
3048 .format(details['change']))
3051 self.gitStream.write("commit %s\n" % branch)
3052 self.gitStream.write("mark :%s\n" % details["change"])
3053 self.committedChanges.add(int(details["change"]))
3055 if author not in self.users:
3056 self.getUserMapFromPerforceServer()
3057 committer = "%s %s %s" % (self.make_email(author), epoch, self.tz)
3059 self.gitStream.write("committer %s\n" % committer)
3061 self.gitStream.write("data <<EOT\n")
3062 self.gitStream.write(details["desc"])
3064 self.gitStream.write("\nJobs: %s" % (' '.join(jobs)))
3066 if not self.suppress_meta_comment:
3067 self.gitStream.write("\n[git-p4: depot-paths = \"%s\": change = %s" %
3068 (','.join(self.branchPrefixes), details["change"]))
3069 if len(details['options']) > 0:
3070 self.gitStream.write(": options = %s" % details['options'])
3071 self.gitStream.write("]\n")
3073 self.gitStream.write("EOT\n\n")
3077 print("parent %s" % parent)
3078 self.gitStream.write("from %s\n" % parent)
3080 self.streamP4Files(files)
3081 self.gitStream.write("\n")
3083 change = int(details["change"])
3085 if change in self.labels:
3086 label = self.labels[change]
3087 labelDetails = label[0]
3088 labelRevisions = label[1]
3090 print("Change %s is labelled %s" % (change, labelDetails))
3092 files = p4CmdList(["files"] + ["%s...@%s" % (p, change)
3093 for p in self.branchPrefixes])
3095 if len(files) == len(labelRevisions):
3099 if info["action"] in self.delete_actions:
3101 cleanedFiles[info["depotFile"]] = info["rev"]
3103 if cleanedFiles == labelRevisions:
3104 self.streamTag(self.gitStream, 'tag_%s' % labelDetails['label'], labelDetails, branch, epoch)
3108 print("Tag %s does not match with change %s: files do not match."
3109 % (labelDetails["label"], change))
3113 print("Tag %s does not match with change %s: file count is different."
3114 % (labelDetails["label"], change))
3116 # Build a dictionary of changelists and labels, for "detect-labels" option.
3117 def getLabels(self):
3120 l = p4CmdList(["labels"] + ["%s..." % p for p in self.depotPaths])
3121 if len(l) > 0 and not self.silent:
3122 print("Finding files belonging to labels in %s" % self.depotPaths)
3125 label = output["label"]
3129 print("Querying files for label %s" % label)
3130 for file in p4CmdList(["files"] +
3131 ["%s...@%s" % (p, label)
3132 for p in self.depotPaths]):
3133 revisions[file["depotFile"]] = file["rev"]
3134 change = int(file["change"])
3135 if change > newestChange:
3136 newestChange = change
3138 self.labels[newestChange] = [output, revisions]
3141 print("Label changes: %s" % self.labels.keys())
3143 # Import p4 labels as git tags. A direct mapping does not
3144 # exist, so assume that if all the files are at the same revision
3145 # then we can use that, or it's something more complicated we should
3147 def importP4Labels(self, stream, p4Labels):
3149 print("import p4 labels: " + ' '.join(p4Labels))
3151 ignoredP4Labels = gitConfigList("git-p4.ignoredP4Labels")
3152 validLabelRegexp = gitConfig("git-p4.labelImportRegexp")
3153 if len(validLabelRegexp) == 0:
3154 validLabelRegexp = defaultLabelRegexp
3155 m = re.compile(validLabelRegexp)
3157 for name in p4Labels:
3160 if not m.match(name):
3162 print("label %s does not match regexp %s" % (name,validLabelRegexp))
3165 if name in ignoredP4Labels:
3168 labelDetails = p4CmdList(['label', "-o", name])[0]
3170 # get the most recent changelist for each file in this label
3171 change = p4Cmd(["changes", "-m", "1"] + ["%s...@%s" % (p, name)
3172 for p in self.depotPaths])
3174 if 'change' in change:
3175 # find the corresponding git commit; take the oldest commit
3176 changelist = int(change['change'])
3177 if changelist in self.committedChanges:
3178 gitCommit = ":%d" % changelist # use a fast-import mark
3181 gitCommit = read_pipe(["git", "rev-list", "--max-count=1",
3182 "--reverse", ":/\[git-p4:.*change = %d\]" % changelist], ignore_error=True)
3183 if len(gitCommit) == 0:
3184 print("importing label %s: could not find git commit for changelist %d" % (name, changelist))
3187 gitCommit = gitCommit.strip()
3190 # Convert from p4 time format
3192 tmwhen = time.strptime(labelDetails['Update'], "%Y/%m/%d %H:%M:%S")
3194 print("Could not convert label time %s" % labelDetails['Update'])
3197 when = int(time.mktime(tmwhen))
3198 self.streamTag(stream, name, labelDetails, gitCommit, when)
3200 print("p4 label %s mapped to git commit %s" % (name, gitCommit))
3203 print("Label %s has no changelists - possibly deleted?" % name)
3206 # We can't import this label; don't try again as it will get very
3207 # expensive repeatedly fetching all the files for labels that will
3208 # never be imported. If the label is moved in the future, the
3209 # ignore will need to be removed manually.
3210 system(["git", "config", "--add", "git-p4.ignoredP4Labels", name])
3212 def guessProjectName(self):
3213 for p in self.depotPaths:
3216 p = p[p.strip().rfind("/") + 1:]
3217 if not p.endswith("/"):
3221 def getBranchMapping(self):
3222 lostAndFoundBranches = set()
3224 user = gitConfig("git-p4.branchUser")
3226 command = "branches -u %s" % user
3228 command = "branches"
3230 for info in p4CmdList(command):
3231 details = p4Cmd(["branch", "-o", info["branch"]])
3233 while "View%s" % viewIdx in details:
3234 paths = details["View%s" % viewIdx].split(" ")
3235 viewIdx = viewIdx + 1
3236 # require standard //depot/foo/... //depot/bar/... mapping
3237 if len(paths) != 2 or not paths[0].endswith("/...") or not paths[1].endswith("/..."):
3240 destination = paths[1]
3242 if p4PathStartsWith(source, self.depotPaths[0]) and p4PathStartsWith(destination, self.depotPaths[0]):
3243 source = source[len(self.depotPaths[0]):-4]
3244 destination = destination[len(self.depotPaths[0]):-4]
3246 if destination in self.knownBranches:
3248 print("p4 branch %s defines a mapping from %s to %s" % (info["branch"], source, destination))
3249 print("but there exists another mapping from %s to %s already!" % (self.knownBranches[destination], destination))
3252 self.knownBranches[destination] = source
3254 lostAndFoundBranches.discard(destination)
3256 if source not in self.knownBranches:
3257 lostAndFoundBranches.add(source)
3259 # Perforce does not strictly require branches to be defined, so we also
3260 # check git config for a branch list.
3262 # Example of branch definition in git config file:
3264 # branchList=main:branchA
3265 # branchList=main:branchB
3266 # branchList=branchA:branchC
3267 configBranches = gitConfigList("git-p4.branchList")
3268 for branch in configBranches:
3270 (source, destination) = branch.split(":")
3271 self.knownBranches[destination] = source
3273 lostAndFoundBranches.discard(destination)
3275 if source not in self.knownBranches:
3276 lostAndFoundBranches.add(source)
3279 for branch in lostAndFoundBranches:
3280 self.knownBranches[branch] = branch
3282 def getBranchMappingFromGitBranches(self):
3283 branches = p4BranchesInGit(self.importIntoRemotes)
3284 for branch in branches.keys():
3285 if branch == "master":
3288 branch = branch[len(self.projectName):]
3289 self.knownBranches[branch] = branch
3291 def updateOptionDict(self, d):
3293 if self.keepRepoPath:
3294 option_keys['keepRepoPath'] = 1
3296 d["options"] = ' '.join(sorted(option_keys.keys()))
3298 def readOptions(self, d):
3299 self.keepRepoPath = ('options' in d
3300 and ('keepRepoPath' in d['options']))
3302 def gitRefForBranch(self, branch):
3303 if branch == "main":
3304 return self.refPrefix + "master"
3306 if len(branch) <= 0:
3309 return self.refPrefix + self.projectName + branch
3311 def gitCommitByP4Change(self, ref, change):
3313 print("looking in ref " + ref + " for change %s using bisect..." % change)
3316 latestCommit = parseRevision(ref)
3320 print("trying: earliest %s latest %s" % (earliestCommit, latestCommit))
3321 next = read_pipe("git rev-list --bisect %s %s" % (latestCommit, earliestCommit)).strip()
3326 log = extractLogMessageFromGitCommit(next)
3327 settings = extractSettingsGitLog(log)
3328 currentChange = int(settings['change'])
3330 print("current change %s" % currentChange)
3332 if currentChange == change:
3334 print("found %s" % next)
3337 if currentChange < change:
3338 earliestCommit = "^%s" % next
3340 if next == latestCommit:
3341 die("Infinite loop while looking in ref %s for change %s. Check your branch mappings" % (ref, change))
3342 latestCommit = "%s^@" % next
3346 def importNewBranch(self, branch, maxChange):
3347 # make fast-import flush all changes to disk and update the refs using the checkpoint
3348 # command so that we can try to find the branch parent in the git history
3349 self.gitStream.write("checkpoint\n\n");
3350 self.gitStream.flush();
3351 branchPrefix = self.depotPaths[0] + branch + "/"
3352 range = "@1,%s" % maxChange
3353 #print "prefix" + branchPrefix
3354 changes = p4ChangesForPaths([branchPrefix], range, self.changes_block_size)
3355 if len(changes) <= 0:
3357 firstChange = changes[0]
3358 #print "first change in branch: %s" % firstChange
3359 sourceBranch = self.knownBranches[branch]
3360 sourceDepotPath = self.depotPaths[0] + sourceBranch
3361 sourceRef = self.gitRefForBranch(sourceBranch)
3362 #print "source " + sourceBranch
3364 branchParentChange = int(p4Cmd(["changes", "-m", "1", "%s...@1,%s" % (sourceDepotPath, firstChange)])["change"])
3365 #print "branch parent: %s" % branchParentChange
3366 gitParent = self.gitCommitByP4Change(sourceRef, branchParentChange)
3367 if len(gitParent) > 0:
3368 self.initialParents[self.gitRefForBranch(branch)] = gitParent
3369 #print "parent git commit: %s" % gitParent
3371 self.importChanges(changes)
3374 def searchParent(self, parent, branch, target):
3376 for blob in read_pipe_lines(["git", "rev-list", "--reverse",
3377 "--no-merges", parent]):
3379 if len(read_pipe(["git", "diff-tree", blob, target])) == 0:
3382 print("Found parent of %s in commit %s" % (branch, blob))
3389 def importChanges(self, changes, origin_revision=0):
3391 for change in changes:
3392 description = p4_describe(change)
3393 self.updateOptionDict(description)
3396 sys.stdout.write("\rImporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
3401 if self.detectBranches:
3402 branches = self.splitFilesIntoBranches(description)
3403 for branch in branches.keys():
3405 branchPrefix = self.depotPaths[0] + branch + "/"
3406 self.branchPrefixes = [ branchPrefix ]
3410 filesForCommit = branches[branch]
3413 print("branch is %s" % branch)
3415 self.updatedBranches.add(branch)
3417 if branch not in self.createdBranches:
3418 self.createdBranches.add(branch)
3419 parent = self.knownBranches[branch]
3420 if parent == branch:
3423 fullBranch = self.projectName + branch
3424 if fullBranch not in self.p4BranchesInGit:
3426 print("\n Importing new branch %s" % fullBranch);
3427 if self.importNewBranch(branch, change - 1):
3429 self.p4BranchesInGit.append(fullBranch)
3431 print("\n Resuming with change %s" % change);
3434 print("parent determined through known branches: %s" % parent)
3436 branch = self.gitRefForBranch(branch)
3437 parent = self.gitRefForBranch(parent)
3440 print("looking for initial parent for %s; current parent is %s" % (branch, parent))
3442 if len(parent) == 0 and branch in self.initialParents:
3443 parent = self.initialParents[branch]
3444 del self.initialParents[branch]
3448 tempBranch = "%s/%d" % (self.tempBranchLocation, change)
3450 print("Creating temporary branch: " + tempBranch)
3451 self.commit(description, filesForCommit, tempBranch)
3452 self.tempBranches.append(tempBranch)
3454 blob = self.searchParent(parent, branch, tempBranch)
3456 self.commit(description, filesForCommit, branch, blob)
3459 print("Parent of %s not found. Committing into head of %s" % (branch, parent))
3460 self.commit(description, filesForCommit, branch, parent)
3462 files = self.extractFilesFromCommit(description)
3463 self.commit(description, files, self.branch,
3465 # only needed once, to connect to the previous commit
3466 self.initialParent = ""
3468 print(self.gitError.read())
3471 def sync_origin_only(self):
3472 if self.syncWithOrigin:
3473 self.hasOrigin = originP4BranchesExist()
3476 print('Syncing with origin first, using "git fetch origin"')
3477 system("git fetch origin")
3479 def importHeadRevision(self, revision):
3480 print("Doing initial import of %s from revision %s into %s" % (' '.join(self.depotPaths), revision, self.branch))
3483 details["user"] = "git perforce import user"
3484 details["desc"] = ("Initial import of %s from the state at revision %s\n"
3485 % (' '.join(self.depotPaths), revision))
3486 details["change"] = revision
3490 fileArgs = ["%s...%s" % (p,revision) for p in self.depotPaths]
3492 for info in p4CmdList(["files"] + fileArgs):
3494 if 'code' in info and info['code'] == 'error':
3495 sys.stderr.write("p4 returned an error: %s\n"
3497 if info['data'].find("must refer to client") >= 0:
3498 sys.stderr.write("This particular p4 error is misleading.\n")
3499 sys.stderr.write("Perhaps the depot path was misspelled.\n");
3500 sys.stderr.write("Depot path: %s\n" % " ".join(self.depotPaths))
3502 if 'p4ExitCode' in info:
3503 sys.stderr.write("p4 exitcode: %s\n" % info['p4ExitCode'])
3507 change = int(info["change"])
3508 if change > newestRevision:
3509 newestRevision = change
3511 if info["action"] in self.delete_actions:
3512 # don't increase the file cnt, otherwise details["depotFile123"] will have gaps!
3513 #fileCnt = fileCnt + 1
3516 for prop in ["depotFile", "rev", "action", "type" ]:
3517 details["%s%s" % (prop, fileCnt)] = info[prop]
3519 fileCnt = fileCnt + 1
3521 details["change"] = newestRevision
3523 # Use time from top-most change so that all git p4 clones of
3524 # the same p4 repo have the same commit SHA1s.
3525 res = p4_describe(newestRevision)
3526 details["time"] = res["time"]
3528 self.updateOptionDict(details)
3530 self.commit(details, self.extractFilesFromCommit(details), self.branch)
3531 except IOError as err:
3532 print("IO error with git fast-import. Is your git version recent enough?")
3533 print("IO error details: {}".format(err))
3534 print(self.gitError.read())
3536 def openStreams(self):
3537 self.importProcess = subprocess.Popen(["git", "fast-import"],
3538 stdin=subprocess.PIPE,
3539 stdout=subprocess.PIPE,
3540 stderr=subprocess.PIPE);
3541 self.gitOutput = self.importProcess.stdout
3542 self.gitStream = self.importProcess.stdin
3543 self.gitError = self.importProcess.stderr
3545 def closeStreams(self):
3546 self.gitStream.close()
3547 if self.importProcess.wait() != 0:
3548 die("fast-import failed: %s" % self.gitError.read())
3549 self.gitOutput.close()
3550 self.gitError.close()
3552 def run(self, args):
3553 if self.importIntoRemotes:
3554 self.refPrefix = "refs/remotes/p4/"
3556 self.refPrefix = "refs/heads/p4/"
3558 self.sync_origin_only()
3560 branch_arg_given = bool(self.branch)
3561 if len(self.branch) == 0:
3562 self.branch = self.refPrefix + "master"
3563 if gitBranchExists("refs/heads/p4") and self.importIntoRemotes:
3564 system("git update-ref %s refs/heads/p4" % self.branch)
3565 system("git branch -D p4")
3567 # accept either the command-line option, or the configuration variable
3568 if self.useClientSpec:
3569 # will use this after clone to set the variable
3570 self.useClientSpec_from_options = True
3572 if gitConfigBool("git-p4.useclientspec"):
3573 self.useClientSpec = True
3574 if self.useClientSpec:
3575 self.clientSpecDirs = getClientSpec()
3577 # TODO: should always look at previous commits,
3578 # merge with previous imports, if possible.
3581 createOrUpdateBranchesFromOrigin(self.refPrefix, self.silent)
3583 # branches holds mapping from branch name to sha1
3584 branches = p4BranchesInGit(self.importIntoRemotes)
3586 # restrict to just this one, disabling detect-branches
3587 if branch_arg_given:
3588 short = self.branch.split("/")[-1]
3589 if short in branches:
3590 self.p4BranchesInGit = [ short ]
3592 self.p4BranchesInGit = branches.keys()
3594 if len(self.p4BranchesInGit) > 1:
3596 print("Importing from/into multiple branches")
3597 self.detectBranches = True
3598 for branch in branches.keys():
3599 self.initialParents[self.refPrefix + branch] = \
3603 print("branches: %s" % self.p4BranchesInGit)
3606 for branch in self.p4BranchesInGit:
3607 logMsg = extractLogMessageFromGitCommit(self.refPrefix + branch)
3609 settings = extractSettingsGitLog(logMsg)
3611 self.readOptions(settings)
3612 if ('depot-paths' in settings
3613 and 'change' in settings):
3614 change = int(settings['change']) + 1
3615 p4Change = max(p4Change, change)
3617 depotPaths = sorted(settings['depot-paths'])
3618 if self.previousDepotPaths == []:
3619 self.previousDepotPaths = depotPaths
3622 for (prev, cur) in zip(self.previousDepotPaths, depotPaths):
3623 prev_list = prev.split("/")
3624 cur_list = cur.split("/")
3625 for i in range(0, min(len(cur_list), len(prev_list))):
3626 if cur_list[i] != prev_list[i]:
3630 paths.append ("/".join(cur_list[:i + 1]))
3632 self.previousDepotPaths = paths
3635 self.depotPaths = sorted(self.previousDepotPaths)
3636 self.changeRange = "@%s,#head" % p4Change
3637 if not self.silent and not self.detectBranches:
3638 print("Performing incremental import into %s git branch" % self.branch)
3640 # accept multiple ref name abbreviations:
3641 # refs/foo/bar/branch -> use it exactly
3642 # p4/branch -> prepend refs/remotes/ or refs/heads/
3643 # branch -> prepend refs/remotes/p4/ or refs/heads/p4/
3644 if not self.branch.startswith("refs/"):
3645 if self.importIntoRemotes:
3646 prepend = "refs/remotes/"
3648 prepend = "refs/heads/"
3649 if not self.branch.startswith("p4/"):
3651 self.branch = prepend + self.branch
3653 if len(args) == 0 and self.depotPaths:
3655 print("Depot paths: %s" % ' '.join(self.depotPaths))
3657 if self.depotPaths and self.depotPaths != args:
3658 print("previous import used depot path %s and now %s was specified. "
3659 "This doesn't work!" % (' '.join (self.depotPaths),
3663 self.depotPaths = sorted(args)
3668 # Make sure no revision specifiers are used when --changesfile
3670 bad_changesfile = False
3671 if len(self.changesFile) > 0:
3672 for p in self.depotPaths:
3673 if p.find("@") >= 0 or p.find("#") >= 0:
3674 bad_changesfile = True
3677 die("Option --changesfile is incompatible with revision specifiers")
3680 for p in self.depotPaths:
3681 if p.find("@") != -1:
3682 atIdx = p.index("@")
3683 self.changeRange = p[atIdx:]
3684 if self.changeRange == "@all":
3685 self.changeRange = ""
3686 elif ',' not in self.changeRange:
3687 revision = self.changeRange
3688 self.changeRange = ""
3690 elif p.find("#") != -1:
3691 hashIdx = p.index("#")
3692 revision = p[hashIdx:]
3694 elif self.previousDepotPaths == []:
3695 # pay attention to changesfile, if given, else import
3696 # the entire p4 tree at the head revision
3697 if len(self.changesFile) == 0:
3700 p = re.sub ("\.\.\.$", "", p)
3701 if not p.endswith("/"):
3706 self.depotPaths = newPaths
3708 # --detect-branches may change this for each branch
3709 self.branchPrefixes = self.depotPaths
3711 self.loadUserMapFromCache()
3713 if self.detectLabels:
3716 if self.detectBranches:
3717 ## FIXME - what's a P4 projectName ?
3718 self.projectName = self.guessProjectName()
3721 self.getBranchMappingFromGitBranches()
3723 self.getBranchMapping()
3725 print("p4-git branches: %s" % self.p4BranchesInGit)
3726 print("initial parents: %s" % self.initialParents)
3727 for b in self.p4BranchesInGit:
3731 b = b[len(self.projectName):]
3732 self.createdBranches.add(b)
3737 self.importHeadRevision(revision)
3741 if len(self.changesFile) > 0:
3742 output = open(self.changesFile).readlines()
3745 changeSet.add(int(line))
3747 for change in changeSet:
3748 changes.append(change)
3752 # catch "git p4 sync" with no new branches, in a repo that
3753 # does not have any existing p4 branches
3755 if not self.p4BranchesInGit:
3756 die("No remote p4 branches. Perhaps you never did \"git p4 clone\" in here.")
3758 # The default branch is master, unless --branch is used to
3759 # specify something else. Make sure it exists, or complain
3760 # nicely about how to use --branch.
3761 if not self.detectBranches:
3762 if not branch_exists(self.branch):
3763 if branch_arg_given:
3764 die("Error: branch %s does not exist." % self.branch)
3766 die("Error: no branch %s; perhaps specify one with --branch." %
3770 print("Getting p4 changes for %s...%s" % (', '.join(self.depotPaths),
3772 changes = p4ChangesForPaths(self.depotPaths, self.changeRange, self.changes_block_size)
3774 if len(self.maxChanges) > 0:
3775 changes = changes[:min(int(self.maxChanges), len(changes))]
3777 if len(changes) == 0:
3779 print("No changes to import!")
3781 if not self.silent and not self.detectBranches:
3782 print("Import destination: %s" % self.branch)
3784 self.updatedBranches = set()
3786 if not self.detectBranches:
3788 # start a new branch
3789 self.initialParent = ""
3791 # build on a previous revision
3792 self.initialParent = parseRevision(self.branch)
3794 self.importChanges(changes)
3798 if len(self.updatedBranches) > 0:
3799 sys.stdout.write("Updated branches: ")
3800 for b in self.updatedBranches:
3801 sys.stdout.write("%s " % b)
3802 sys.stdout.write("\n")
3804 if gitConfigBool("git-p4.importLabels"):
3805 self.importLabels = True
3807 if self.importLabels:
3808 p4Labels = getP4Labels(self.depotPaths)
3809 gitTags = getGitTags()
3811 missingP4Labels = p4Labels - gitTags
3812 self.importP4Labels(self.gitStream, missingP4Labels)
3816 # Cleanup temporary branches created during import
3817 if self.tempBranches != []:
3818 for branch in self.tempBranches:
3819 read_pipe("git update-ref -d %s" % branch)
3820 os.rmdir(os.path.join(os.environ.get("GIT_DIR", ".git"), self.tempBranchLocation))
3822 # Create a symbolic ref p4/HEAD pointing to p4/<branch> to allow
3823 # a convenient shortcut refname "p4".
3824 if self.importIntoRemotes:
3825 head_ref = self.refPrefix + "HEAD"
3826 if not gitBranchExists(head_ref) and gitBranchExists(self.branch):
3827 system(["git", "symbolic-ref", head_ref, self.branch])
3831 class P4Rebase(Command):
3833 Command.__init__(self)
3835 optparse.make_option("--import-labels", dest="importLabels", action="store_true"),
3837 self.importLabels = False
3838 self.description = ("Fetches the latest revision from perforce and "
3839 + "rebases the current work (branch) against it")
3841 def run(self, args):
3843 sync.importLabels = self.importLabels
3846 return self.rebase()
3849 if os.system("git update-index --refresh") != 0:
3850 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.");
3851 if len(read_pipe("git diff-index HEAD --")) > 0:
3852 die("You have uncommitted changes. Please commit them before rebasing or stash them away with git stash.");
3854 [upstream, settings] = findUpstreamBranchPoint()
3855 if len(upstream) == 0:
3856 die("Cannot find upstream branchpoint for rebase")
3858 # the branchpoint may be p4/foo~3, so strip off the parent
3859 upstream = re.sub("~[0-9]+$", "", upstream)
3861 print("Rebasing the current branch onto %s" % upstream)
3862 oldHead = read_pipe("git rev-parse HEAD").strip()
3863 system("git rebase %s" % upstream)
3864 system("git diff-tree --stat --summary -M %s HEAD --" % oldHead)
3867 class P4Clone(P4Sync):
3869 P4Sync.__init__(self)
3870 self.description = "Creates a new git repository and imports from Perforce into it"
3871 self.usage = "usage: %prog [options] //depot/path[@revRange]"
3873 optparse.make_option("--destination", dest="cloneDestination",
3874 action='store', default=None,
3875 help="where to leave result of the clone"),
3876 optparse.make_option("--bare", dest="cloneBare",
3877 action="store_true", default=False),
3879 self.cloneDestination = None
3880 self.needsGit = False
3881 self.cloneBare = False
3883 def defaultDestination(self, args):
3884 ## TODO: use common prefix of args?
3886 depotDir = re.sub("(@[^@]*)$", "", depotPath)
3887 depotDir = re.sub("(#[^#]*)$", "", depotDir)
3888 depotDir = re.sub(r"\.\.\.$", "", depotDir)
3889 depotDir = re.sub(r"/$", "", depotDir)
3890 return os.path.split(depotDir)[1]
3892 def run(self, args):
3896 if self.keepRepoPath and not self.cloneDestination:
3897 sys.stderr.write("Must specify destination for --keep-path\n")
3902 if not self.cloneDestination and len(depotPaths) > 1:
3903 self.cloneDestination = depotPaths[-1]
3904 depotPaths = depotPaths[:-1]
3906 for p in depotPaths:
3907 if not p.startswith("//"):
3908 sys.stderr.write('Depot paths must start with "//": %s\n' % p)
3911 if not self.cloneDestination:
3912 self.cloneDestination = self.defaultDestination(args)
3914 print("Importing from %s into %s" % (', '.join(depotPaths), self.cloneDestination))
3916 if not os.path.exists(self.cloneDestination):
3917 os.makedirs(self.cloneDestination)
3918 chdir(self.cloneDestination)
3920 init_cmd = [ "git", "init" ]
3922 init_cmd.append("--bare")
3923 retcode = subprocess.call(init_cmd)
3925 raise CalledProcessError(retcode, init_cmd)
3927 if not P4Sync.run(self, depotPaths):
3930 # create a master branch and check out a work tree
3931 if gitBranchExists(self.branch):
3932 system([ "git", "branch", "master", self.branch ])
3933 if not self.cloneBare:
3934 system([ "git", "checkout", "-f" ])
3936 print('Not checking out any branch, use ' \
3937 '"git checkout -q -b master <branch>"')
3939 # auto-set this variable if invoked with --use-client-spec
3940 if self.useClientSpec_from_options:
3941 system("git config --bool git-p4.useclientspec true")
3945 class P4Unshelve(Command):
3947 Command.__init__(self)
3949 self.origin = "HEAD"
3950 self.description = "Unshelve a P4 changelist into a git commit"
3951 self.usage = "usage: %prog [options] changelist"
3953 optparse.make_option("--origin", dest="origin",
3954 help="Use this base revision instead of the default (%s)" % self.origin),
3956 self.verbose = False
3957 self.noCommit = False
3958 self.destbranch = "refs/remotes/p4-unshelved"
3960 def renameBranch(self, branch_name):
3961 """ Rename the existing branch to branch_name.N
3965 for i in range(0,1000):
3966 backup_branch_name = "{0}.{1}".format(branch_name, i)
3967 if not gitBranchExists(backup_branch_name):
3968 gitUpdateRef(backup_branch_name, branch_name) # copy ref to backup
3969 gitDeleteRef(branch_name)
3971 print("renamed old unshelve branch to {0}".format(backup_branch_name))
3975 sys.exit("gave up trying to rename existing branch {0}".format(sync.branch))
3977 def findLastP4Revision(self, starting_point):
3978 """ Look back from starting_point for the first commit created by git-p4
3979 to find the P4 commit we are based on, and the depot-paths.
3982 for parent in (range(65535)):
3983 log = extractLogMessageFromGitCommit("{0}^{1}".format(starting_point, parent))
3984 settings = extractSettingsGitLog(log)
3985 if 'change' in settings:
3988 sys.exit("could not find git-p4 commits in {0}".format(self.origin))
3990 def createShelveParent(self, change, branch_name, sync, origin):
3991 """ Create a commit matching the parent of the shelved changelist 'change'
3993 parent_description = p4_describe(change, shelved=True)
3994 parent_description['desc'] = 'parent for shelved changelist {}\n'.format(change)
3995 files = sync.extractFilesFromCommit(parent_description, shelved=False, shelved_cl=change)
3999 # if it was added in the shelved changelist, it won't exist in the parent
4000 if f['action'] in self.add_actions:
4003 # if it was deleted in the shelved changelist it must not be deleted
4004 # in the parent - we might even need to create it if the origin branch
4006 if f['action'] in self.delete_actions:
4009 parent_files.append(f)
4011 sync.commit(parent_description, parent_files, branch_name,
4012 parent=origin, allow_empty=True)
4013 print("created parent commit for {0} based on {1} in {2}".format(
4014 change, self.origin, branch_name))
4016 def run(self, args):
4020 if not gitBranchExists(self.origin):
4021 sys.exit("origin branch {0} does not exist".format(self.origin))
4026 # only one change at a time
4029 # if the target branch already exists, rename it
4030 branch_name = "{0}/{1}".format(self.destbranch, change)
4031 if gitBranchExists(branch_name):
4032 self.renameBranch(branch_name)
4033 sync.branch = branch_name
4035 sync.verbose = self.verbose
4036 sync.suppress_meta_comment = True
4038 settings = self.findLastP4Revision(self.origin)
4039 sync.depotPaths = settings['depot-paths']
4040 sync.branchPrefixes = sync.depotPaths
4043 sync.loadUserMapFromCache()
4046 # create a commit for the parent of the shelved changelist
4047 self.createShelveParent(change, branch_name, sync, self.origin)
4049 # create the commit for the shelved changelist itself
4050 description = p4_describe(change, True)
4051 files = sync.extractFilesFromCommit(description, True, change)
4053 sync.commit(description, files, branch_name, "")
4056 print("unshelved changelist {0} into {1}".format(change, branch_name))
4060 class P4Branches(Command):
4062 Command.__init__(self)
4064 self.description = ("Shows the git branches that hold imports and their "
4065 + "corresponding perforce depot paths")
4066 self.verbose = False
4068 def run(self, args):
4069 if originP4BranchesExist():
4070 createOrUpdateBranchesFromOrigin()
4072 cmdline = "git rev-parse --symbolic "
4073 cmdline += " --remotes"
4075 for line in read_pipe_lines(cmdline):
4078 if not line.startswith('p4/') or line == "p4/HEAD":
4082 log = extractLogMessageFromGitCommit("refs/remotes/%s" % branch)
4083 settings = extractSettingsGitLog(log)
4085 print("%s <= %s (%s)" % (branch, ",".join(settings["depot-paths"]), settings["change"]))
4088 class HelpFormatter(optparse.IndentedHelpFormatter):
4090 optparse.IndentedHelpFormatter.__init__(self)
4092 def format_description(self, description):
4094 return description + "\n"
4098 def printUsage(commands):
4099 print("usage: %s <command> [options]" % sys.argv[0])
4101 print("valid commands: %s" % ", ".join(commands))
4103 print("Try %s <command> --help for command specific help." % sys.argv[0])
4108 "submit" : P4Submit,
4109 "commit" : P4Submit,
4111 "rebase" : P4Rebase,
4113 "rollback" : P4RollBack,
4114 "branches" : P4Branches,
4115 "unshelve" : P4Unshelve,
4120 if len(sys.argv[1:]) == 0:
4121 printUsage(commands.keys())
4124 cmdName = sys.argv[1]
4126 klass = commands[cmdName]
4129 print("unknown command %s" % cmdName)
4131 printUsage(commands.keys())
4134 options = cmd.options
4135 cmd.gitdir = os.environ.get("GIT_DIR", None)
4139 options.append(optparse.make_option("--verbose", "-v", dest="verbose", action="store_true"))
4141 options.append(optparse.make_option("--git-dir", dest="gitdir"))
4143 parser = optparse.OptionParser(cmd.usage.replace("%prog", "%prog " + cmdName),
4145 description = cmd.description,
4146 formatter = HelpFormatter())
4148 (cmd, args) = parser.parse_args(sys.argv[2:], cmd);
4150 verbose = cmd.verbose
4152 if cmd.gitdir == None:
4153 cmd.gitdir = os.path.abspath(".git")
4154 if not isValidGitDir(cmd.gitdir):
4155 # "rev-parse --git-dir" without arguments will try $PWD/.git
4156 cmd.gitdir = read_pipe("git rev-parse --git-dir").strip()
4157 if os.path.exists(cmd.gitdir):
4158 cdup = read_pipe("git rev-parse --show-cdup").strip()
4162 if not isValidGitDir(cmd.gitdir):
4163 if isValidGitDir(cmd.gitdir + "/.git"):
4164 cmd.gitdir += "/.git"
4166 die("fatal: cannot locate git repository at %s" % cmd.gitdir)
4168 # so git commands invoked from the P4 workspace will succeed
4169 os.environ["GIT_DIR"] = cmd.gitdir
4171 if not cmd.run(args):
4176 if __name__ == '__main__':