3 # git-p4.py -- A tool for bidirectional operation between a Perforce depot and git.
5 # Author: Simon Hausmann <simon@lst.de>
6 # Copyright: 2007 Simon Hausmann <simon@lst.de>
8 # License: MIT <http://www.opensource.org/licenses/mit-license.php>
10 # pylint: disable=invalid-name,missing-docstring,too-many-arguments,broad-except
11 # pylint: disable=no-self-use,wrong-import-position,consider-iterating-dictionary
12 # pylint: disable=wrong-import-order,unused-import,too-few-public-methods
13 # pylint: disable=too-many-lines,ungrouped-imports,fixme,too-many-locals
14 # pylint: disable=line-too-long,bad-whitespace,superfluous-parens
15 # pylint: disable=too-many-statements,too-many-instance-attributes
16 # pylint: disable=too-many-branches,too-many-nested-blocks
19 if sys.version_info.major < 3 and sys.version_info.minor < 7:
20 sys.stderr.write("git-p4: requires Python 2.7 or later.\n")
39 # On python2.7 where raw_input() and input() are both availble,
40 # we want raw_input's semantics, but aliased to input for python3
42 # support basestring in python3
44 if raw_input and input:
51 # Only labels/tags matching this will be imported/exported
52 defaultLabelRegexp = r'[a-zA-Z0-9_\-.]+$'
54 # The block size is reduced automatically if required
55 defaultBlockSize = 1<<20
57 p4_access_checked = False
59 def p4_build_cmd(cmd):
60 """Build a suitable p4 command line.
62 This consolidates building and returning a p4 command line into one
63 location. It means that hooking into the environment, or other configuration
64 can be done more easily.
68 user = gitConfig("git-p4.user")
70 real_cmd += ["-u",user]
72 password = gitConfig("git-p4.password")
74 real_cmd += ["-P", password]
76 port = gitConfig("git-p4.port")
78 real_cmd += ["-p", port]
80 host = gitConfig("git-p4.host")
82 real_cmd += ["-H", host]
84 client = gitConfig("git-p4.client")
86 real_cmd += ["-c", client]
88 retries = gitConfigInt("git-p4.retries")
90 # Perform 3 retries by default
93 # Provide a way to not pass this option by setting git-p4.retries to 0
94 real_cmd += ["-r", str(retries)]
96 if not isinstance(cmd, list):
97 real_cmd = ' '.join(real_cmd) + ' ' + cmd
101 # now check that we can actually talk to the server
102 global p4_access_checked
103 if not p4_access_checked:
104 p4_access_checked = True # suppress access checks in p4_check_access itself
110 """ Return TRUE if the given path is a git directory (/path/to/dir/.git).
111 This won't automatically add ".git" to a directory.
113 d = read_pipe(["git", "--git-dir", path, "rev-parse", "--git-dir"], True).strip()
114 if not d or len(d) == 0:
119 def chdir(path, is_client_path=False):
120 """Do chdir to the given path, and set the PWD environment
121 variable for use by P4. It does not look at getcwd() output.
122 Since we're not using the shell, it is necessary to set the
123 PWD environment variable explicitly.
125 Normally, expand the path to force it to be absolute. This
126 addresses the use of relative path names inside P4 settings,
127 e.g. P4CONFIG=.p4config. P4 does not simply open the filename
128 as given; it looks for .p4config using PWD.
130 If is_client_path, the path was handed to us directly by p4,
131 and may be a symbolic link. Do not call os.getcwd() in this
132 case, because it will cause p4 to think that PWD is not inside
137 if not is_client_path:
139 os.environ['PWD'] = path
142 """Return free space in bytes on the disk of the given dirname."""
143 if platform.system() == 'Windows':
144 free_bytes = ctypes.c_ulonglong(0)
145 ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(os.getcwd()), None, None, ctypes.pointer(free_bytes))
146 return free_bytes.value
148 st = os.statvfs(os.getcwd())
149 return st.f_bavail * st.f_frsize
152 """ Terminate execution. Make sure that any running child processes have been wait()ed for before
158 sys.stderr.write(msg + "\n")
161 def prompt(prompt_text):
162 """ Prompt the user to choose one of the choices
164 Choices are identified in the prompt_text by square brackets around
165 a single letter option.
167 choices = set(m.group(1) for m in re.finditer(r"\[(.)\]", prompt_text))
170 sys.stdout.write(prompt_text)
172 response=sys.stdin.readline().strip().lower()
175 response = response[0]
176 if response in choices:
179 # We need different encoding/decoding strategies for text data being passed
180 # around in pipes depending on python version
182 # For python3, always encode and decode as appropriate
183 def decode_text_stream(s):
184 return s.decode() if isinstance(s, bytes) else s
185 def encode_text_stream(s):
186 return s.encode() if isinstance(s, str) else s
188 # For python2.7, pass read strings as-is, but also allow writing unicode
189 def decode_text_stream(s):
191 def encode_text_stream(s):
192 return s.encode('utf_8') if isinstance(s, unicode) else s
194 def decode_path(path):
195 """Decode a given string (bytes or otherwise) using configured path encoding options
197 encoding = gitConfig('git-p4.pathEncoding') or 'utf_8'
199 return path.decode(encoding, errors='replace') if isinstance(path, bytes) else path
204 path = path.decode(encoding, errors='replace')
206 print('Path with non-ASCII characters detected. Used {} to decode: {}'.format(encoding, path))
209 def run_git_hook(cmd, param=[]):
210 """Execute a hook if the hook exists."""
212 sys.stderr.write("Looking for hook: %s\n" % cmd)
215 hooks_path = gitConfig("core.hooksPath")
216 if len(hooks_path) <= 0:
217 hooks_path = os.path.join(os.environ["GIT_DIR"], "hooks")
219 if not isinstance(param, list):
222 # resolve hook file name, OS depdenent
223 hook_file = os.path.join(hooks_path, cmd)
224 if platform.system() == 'Windows':
225 if not os.path.isfile(hook_file):
226 # look for the file with an extension
227 files = glob.glob(hook_file + ".*")
231 hook_file = files.pop()
232 while hook_file.upper().endswith(".SAMPLE"):
233 # The file is a sample hook. We don't want it
235 hook_file = files.pop()
239 if not os.path.isfile(hook_file) or not os.access(hook_file, os.X_OK):
242 return run_hook_command(hook_file, param) == 0
244 def run_hook_command(cmd, param):
245 """Executes a git hook command
246 cmd = the command line file to be executed. This can be
247 a file that is run by OS association.
249 param = a list of parameters to pass to the cmd command
251 On windows, the extension is checked to see if it should
252 be run with the Git for Windows Bash shell. If there
253 is no file extension, the file is deemed a bash shell
254 and will be handed off to sh.exe. Otherwise, Windows
255 will be called with the shell to handle the file assocation.
257 For non Windows operating systems, the file is called
262 if platform.system() == 'Windows':
263 (root,ext) = os.path.splitext(cmd)
265 exe_path = os.environ.get("EXEPATH")
269 exe_path = os.path.join(exe_path, "bin")
270 cli = [os.path.join(exe_path, "SH.EXE")] + cli
273 return subprocess.call(cli, shell=use_shell)
276 def write_pipe(c, stdin):
278 sys.stderr.write('Writing pipe: %s\n' % str(c))
280 expand = not isinstance(c, list)
281 p = subprocess.Popen(c, stdin=subprocess.PIPE, shell=expand)
283 val = pipe.write(stdin)
286 die('Command failed: %s' % str(c))
290 def p4_write_pipe(c, stdin):
291 real_cmd = p4_build_cmd(c)
292 if bytes is not str and isinstance(stdin, str):
293 stdin = encode_text_stream(stdin)
294 return write_pipe(real_cmd, stdin)
296 def read_pipe_full(c):
297 """ Read output from command. Returns a tuple
298 of the return status, stdout text and stderr
302 sys.stderr.write('Reading pipe: %s\n' % str(c))
304 expand = not isinstance(c, list)
305 p = subprocess.Popen(c, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=expand)
306 (out, err) = p.communicate()
307 return (p.returncode, out, decode_text_stream(err))
309 def read_pipe(c, ignore_error=False, raw=False):
310 """ Read output from command. Returns the output text on
311 success. On failure, terminates execution, unless
312 ignore_error is True, when it returns an empty string.
314 If raw is True, do not attempt to decode output text.
316 (retcode, out, err) = read_pipe_full(c)
321 die('Command failed: %s\nError: %s' % (str(c), err))
323 out = decode_text_stream(out)
326 def read_pipe_text(c):
327 """ Read output from a command with trailing whitespace stripped.
328 On error, returns None.
330 (retcode, out, err) = read_pipe_full(c)
334 return decode_text_stream(out).rstrip()
336 def p4_read_pipe(c, ignore_error=False, raw=False):
337 real_cmd = p4_build_cmd(c)
338 return read_pipe(real_cmd, ignore_error, raw=raw)
340 def read_pipe_lines(c):
342 sys.stderr.write('Reading pipe: %s\n' % str(c))
344 expand = not isinstance(c, list)
345 p = subprocess.Popen(c, stdout=subprocess.PIPE, shell=expand)
347 val = [decode_text_stream(line) for line in pipe.readlines()]
348 if pipe.close() or p.wait():
349 die('Command failed: %s' % str(c))
352 def p4_read_pipe_lines(c):
353 """Specifically invoke p4 on the command supplied. """
354 real_cmd = p4_build_cmd(c)
355 return read_pipe_lines(real_cmd)
357 def p4_has_command(cmd):
358 """Ask p4 for help on this command. If it returns an error, the
359 command does not exist in this version of p4."""
360 real_cmd = p4_build_cmd(["help", cmd])
361 p = subprocess.Popen(real_cmd, stdout=subprocess.PIPE,
362 stderr=subprocess.PIPE)
364 return p.returncode == 0
366 def p4_has_move_command():
367 """See if the move command exists, that it supports -k, and that
368 it has not been administratively disabled. The arguments
369 must be correct, but the filenames do not have to exist. Use
370 ones with wildcards so even if they exist, it will fail."""
372 if not p4_has_command("move"):
374 cmd = p4_build_cmd(["move", "-k", "@from", "@to"])
375 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
376 (out, err) = p.communicate()
377 err = decode_text_stream(err)
378 # return code will be 1 in either case
379 if err.find("Invalid option") >= 0:
381 if err.find("disabled") >= 0:
383 # assume it failed because @... was invalid changelist
386 def system(cmd, ignore_error=False):
387 expand = not isinstance(cmd, list)
389 sys.stderr.write("executing %s\n" % str(cmd))
390 retcode = subprocess.call(cmd, shell=expand)
391 if retcode and not ignore_error:
392 raise CalledProcessError(retcode, cmd)
397 """Specifically invoke p4 as the system command. """
398 real_cmd = p4_build_cmd(cmd)
399 expand = not isinstance(real_cmd, list)
400 retcode = subprocess.call(real_cmd, shell=expand)
402 raise CalledProcessError(retcode, real_cmd)
404 def die_bad_access(s):
405 die("failure accessing depot: {0}".format(s.rstrip()))
407 def p4_check_access(min_expiration=1):
408 """ Check if we can access Perforce - account still logged in
410 results = p4CmdList(["login", "-s"])
412 if len(results) == 0:
413 # should never get here: always get either some results, or a p4ExitCode
414 assert("could not parse response from perforce")
418 if 'p4ExitCode' in result:
419 # p4 returned non-zero status, e.g. P4PORT invalid, or p4 not in path
420 die_bad_access("could not run p4")
422 code = result.get("code")
424 # we get here if we couldn't connect and there was nothing to unmarshal
425 die_bad_access("could not connect")
428 expiry = result.get("TicketExpiration")
431 if expiry > min_expiration:
435 die_bad_access("perforce ticket expires in {0} seconds".format(expiry))
438 # account without a timeout - all ok
441 elif code == "error":
442 data = result.get("data")
444 die_bad_access("p4 error: {0}".format(data))
446 die_bad_access("unknown error")
450 die_bad_access("unknown error code {0}".format(code))
452 _p4_version_string = None
453 def p4_version_string():
454 """Read the version string, showing just the last line, which
455 hopefully is the interesting version bit.
458 Perforce - The Fast Software Configuration Management System.
459 Copyright 1995-2011 Perforce Software. All rights reserved.
460 Rev. P4/NTX86/2011.1/393975 (2011/12/16).
462 global _p4_version_string
463 if not _p4_version_string:
464 a = p4_read_pipe_lines(["-V"])
465 _p4_version_string = a[-1].rstrip()
466 return _p4_version_string
468 def p4_integrate(src, dest):
469 p4_system(["integrate", "-Dt", wildcard_encode(src), wildcard_encode(dest)])
471 def p4_sync(f, *options):
472 p4_system(["sync"] + list(options) + [wildcard_encode(f)])
475 # forcibly add file names with wildcards
476 if wildcard_present(f):
477 p4_system(["add", "-f", f])
479 p4_system(["add", f])
482 p4_system(["delete", wildcard_encode(f)])
484 def p4_edit(f, *options):
485 p4_system(["edit"] + list(options) + [wildcard_encode(f)])
488 p4_system(["revert", wildcard_encode(f)])
490 def p4_reopen(type, f):
491 p4_system(["reopen", "-t", type, wildcard_encode(f)])
493 def p4_reopen_in_change(changelist, files):
494 cmd = ["reopen", "-c", str(changelist)] + files
497 def p4_move(src, dest):
498 p4_system(["move", "-k", wildcard_encode(src), wildcard_encode(dest)])
500 def p4_last_change():
501 results = p4CmdList(["changes", "-m", "1"], skip_info=True)
502 return int(results[0]['change'])
504 def p4_describe(change, shelved=False):
505 """Make sure it returns a valid result by checking for
506 the presence of field "time". Return a dict of the
509 cmd = ["describe", "-s"]
514 ds = p4CmdList(cmd, skip_info=True)
516 die("p4 describe -s %d did not return 1 result: %s" % (change, str(ds)))
520 if "p4ExitCode" in d:
521 die("p4 describe -s %d exited with %d: %s" % (change, d["p4ExitCode"],
524 if d["code"] == "error":
525 die("p4 describe -s %d returned error code: %s" % (change, str(d)))
528 die("p4 describe -s %d returned no \"time\": %s" % (change, str(d)))
533 # Canonicalize the p4 type and return a tuple of the
534 # base type, plus any modifiers. See "p4 help filetypes"
535 # for a list and explanation.
537 def split_p4_type(p4type):
539 p4_filetypes_historical = {
540 "ctempobj": "binary+Sw",
546 "tempobj": "binary+FSw",
547 "ubinary": "binary+F",
548 "uresource": "resource+F",
549 "uxbinary": "binary+Fx",
550 "xbinary": "binary+x",
552 "xtempobj": "binary+Swx",
554 "xunicode": "unicode+x",
557 if p4type in p4_filetypes_historical:
558 p4type = p4_filetypes_historical[p4type]
560 s = p4type.split("+")
568 # return the raw p4 type of a file (text, text+ko, etc)
571 results = p4CmdList(["fstat", "-T", "headType", wildcard_encode(f)])
572 return results[0]['headType']
575 # Given a type base and modifier, return a regexp matching
576 # the keywords that can be expanded in the file
578 def p4_keywords_regexp_for_type(base, type_mods):
579 if base in ("text", "unicode", "binary"):
581 if "ko" in type_mods:
583 elif "k" in type_mods:
584 kwords = 'Id|Header|Author|Date|DateTime|Change|File|Revision'
588 \$ # Starts with a dollar, followed by...
589 (%s) # one of the keywords, followed by...
590 (:[^$\n]+)? # possibly an old expansion, followed by...
598 # Given a file, return a regexp matching the possible
599 # RCS keywords that will be expanded, or None for files
600 # with kw expansion turned off.
602 def p4_keywords_regexp_for_file(file):
603 if not os.path.exists(file):
606 (type_base, type_mods) = split_p4_type(p4_type(file))
607 return p4_keywords_regexp_for_type(type_base, type_mods)
609 def setP4ExecBit(file, mode):
610 # Reopens an already open file and changes the execute bit to match
611 # the execute bit setting in the passed in mode.
615 if not isModeExec(mode):
616 p4Type = getP4OpenedType(file)
617 p4Type = re.sub('^([cku]?)x(.*)', '\\1\\2', p4Type)
618 p4Type = re.sub('(.*?\+.*?)x(.*?)', '\\1\\2', p4Type)
619 if p4Type[-1] == "+":
620 p4Type = p4Type[0:-1]
622 p4_reopen(p4Type, file)
624 def getP4OpenedType(file):
625 # Returns the perforce file type for the given file.
627 result = p4_read_pipe(["opened", wildcard_encode(file)])
628 match = re.match(".*\((.+)\)( \*exclusive\*)?\r?$", result)
630 return match.group(1)
632 die("Could not determine file type for %s (result: '%s')" % (file, result))
634 # Return the set of all p4 labels
635 def getP4Labels(depotPaths):
637 if not isinstance(depotPaths, list):
638 depotPaths = [depotPaths]
640 for l in p4CmdList(["labels"] + ["%s..." % p for p in depotPaths]):
646 # Return the set of all git tags
649 for line in read_pipe_lines(["git", "tag"]):
654 _diff_tree_pattern = None
656 def parseDiffTreeEntry(entry):
657 """Parses a single diff tree entry into its component elements.
659 See git-diff-tree(1) manpage for details about the format of the diff
660 output. This method returns a dictionary with the following elements:
662 src_mode - The mode of the source file
663 dst_mode - The mode of the destination file
664 src_sha1 - The sha1 for the source file
665 dst_sha1 - The sha1 fr the destination file
666 status - The one letter status of the diff (i.e. 'A', 'M', 'D', etc)
667 status_score - The score for the status (applicable for 'C' and 'R'
668 statuses). This is None if there is no score.
669 src - The path for the source file.
670 dst - The path for the destination file. This is only present for
671 copy or renames. If it is not present, this is None.
673 If the pattern is not matched, None is returned."""
675 global _diff_tree_pattern
676 if not _diff_tree_pattern:
677 _diff_tree_pattern = re.compile(':(\d+) (\d+) (\w+) (\w+) ([A-Z])(\d+)?\t(.*?)((\t(.*))|$)')
679 match = _diff_tree_pattern.match(entry)
682 'src_mode': match.group(1),
683 'dst_mode': match.group(2),
684 'src_sha1': match.group(3),
685 'dst_sha1': match.group(4),
686 'status': match.group(5),
687 'status_score': match.group(6),
688 'src': match.group(7),
689 'dst': match.group(10)
693 def isModeExec(mode):
694 # Returns True if the given git mode represents an executable file,
696 return mode[-3:] == "755"
698 class P4Exception(Exception):
699 """ Base class for exceptions from the p4 client """
700 def __init__(self, exit_code):
701 self.p4ExitCode = exit_code
703 class P4ServerException(P4Exception):
704 """ Base class for exceptions where we get some kind of marshalled up result from the server """
705 def __init__(self, exit_code, p4_result):
706 super(P4ServerException, self).__init__(exit_code)
707 self.p4_result = p4_result
708 self.code = p4_result[0]['code']
709 self.data = p4_result[0]['data']
711 class P4RequestSizeException(P4ServerException):
712 """ One of the maxresults or maxscanrows errors """
713 def __init__(self, exit_code, p4_result, limit):
714 super(P4RequestSizeException, self).__init__(exit_code, p4_result)
717 class P4CommandException(P4Exception):
718 """ Something went wrong calling p4 which means we have to give up """
719 def __init__(self, msg):
725 def isModeExecChanged(src_mode, dst_mode):
726 return isModeExec(src_mode) != isModeExec(dst_mode)
728 def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None, skip_info=False,
729 errors_as_exceptions=False):
731 if not isinstance(cmd, list):
738 cmd = p4_build_cmd(cmd)
740 sys.stderr.write("Opening pipe: %s\n" % str(cmd))
742 # Use a temporary file to avoid deadlocks without
743 # subprocess.communicate(), which would put another copy
744 # of stdout into memory.
746 if stdin is not None:
747 stdin_file = tempfile.TemporaryFile(prefix='p4-stdin', mode=stdin_mode)
748 if not isinstance(stdin, list):
749 stdin_file.write(stdin)
752 stdin_file.write(encode_text_stream(i))
753 stdin_file.write(b'\n')
757 p4 = subprocess.Popen(cmd,
760 stdout=subprocess.PIPE)
765 entry = marshal.load(p4.stdout)
767 # Decode unmarshalled dict to use str keys and values, except
768 # for cases where the values may not be valid UTF-8.
769 binary_keys = ('data', 'path', 'clientFile', 'Description',
770 'desc', 'Email', 'FullName', 'Owner', 'time',
773 for key, value in entry.items():
775 if isinstance(value, bytes) and not (key in binary_keys or key.startswith('depotFile')):
776 value = value.decode()
777 decoded_entry[key] = value
778 # Parse out data if it's an error response
779 if decoded_entry.get('code') == 'error' and 'data' in decoded_entry:
780 decoded_entry['data'] = decoded_entry['data'].decode()
781 entry = decoded_entry
783 if 'code' in entry and entry['code'] == 'info':
793 if errors_as_exceptions:
795 data = result[0].get('data')
797 m = re.search('Too many rows scanned \(over (\d+)\)', data)
799 m = re.search('Request too large \(over (\d+)\)', data)
802 limit = int(m.group(1))
803 raise P4RequestSizeException(exitCode, result, limit)
805 raise P4ServerException(exitCode, result)
807 raise P4Exception(exitCode)
810 entry["p4ExitCode"] = exitCode
816 list = p4CmdList(cmd)
822 def p4Where(depotPath):
823 if not depotPath.endswith("/"):
825 depotPathLong = depotPath + "..."
826 outputList = p4CmdList(["where", depotPathLong])
828 for entry in outputList:
829 if "depotFile" in entry:
830 # Search for the base client side depot path, as long as it starts with the branch's P4 path.
831 # The base path always ends with "/...".
832 entry_path = decode_path(entry['depotFile'])
833 if entry_path.find(depotPath) == 0 and entry_path[-4:] == "/...":
836 elif "data" in entry:
837 data = entry.get("data")
838 space = data.find(" ")
839 if data[:space] == depotPath:
844 if output["code"] == "error":
848 clientPath = decode_path(output['path'])
849 elif "data" in output:
850 data = output.get("data")
851 lastSpace = data.rfind(b" ")
852 clientPath = decode_path(data[lastSpace + 1:])
854 if clientPath.endswith("..."):
855 clientPath = clientPath[:-3]
858 def currentGitBranch():
859 return read_pipe_text(["git", "symbolic-ref", "--short", "-q", "HEAD"])
861 def isValidGitDir(path):
862 return git_dir(path) != None
864 def parseRevision(ref):
865 return read_pipe("git rev-parse %s" % ref).strip()
867 def branchExists(ref):
868 rev = read_pipe(["git", "rev-parse", "-q", "--verify", ref],
872 def extractLogMessageFromGitCommit(commit):
875 ## fixme: title is first line of commit, not 1st paragraph.
877 for log in read_pipe_lines(["git", "cat-file", "commit", commit]):
886 def extractSettingsGitLog(log):
888 for line in log.split("\n"):
890 m = re.search (r"^ *\[git-p4: (.*)\]$", line)
894 assignments = m.group(1).split (':')
895 for a in assignments:
897 key = vals[0].strip()
898 val = ('='.join (vals[1:])).strip()
899 if val.endswith ('\"') and val.startswith('"'):
904 paths = values.get("depot-paths")
906 paths = values.get("depot-path")
908 values['depot-paths'] = paths.split(',')
911 def gitBranchExists(branch):
912 proc = subprocess.Popen(["git", "rev-parse", branch],
913 stderr=subprocess.PIPE, stdout=subprocess.PIPE);
914 return proc.wait() == 0;
916 def gitUpdateRef(ref, newvalue):
917 subprocess.check_call(["git", "update-ref", ref, newvalue])
919 def gitDeleteRef(ref):
920 subprocess.check_call(["git", "update-ref", "-d", ref])
924 def gitConfig(key, typeSpecifier=None):
925 if key not in _gitConfig:
926 cmd = [ "git", "config" ]
928 cmd += [ typeSpecifier ]
930 s = read_pipe(cmd, ignore_error=True)
931 _gitConfig[key] = s.strip()
932 return _gitConfig[key]
934 def gitConfigBool(key):
935 """Return a bool, using git config --bool. It is True only if the
936 variable is set to true, and False if set to false or not present
939 if key not in _gitConfig:
940 _gitConfig[key] = gitConfig(key, '--bool') == "true"
941 return _gitConfig[key]
943 def gitConfigInt(key):
944 if key not in _gitConfig:
945 cmd = [ "git", "config", "--int", key ]
946 s = read_pipe(cmd, ignore_error=True)
949 _gitConfig[key] = int(gitConfig(key, '--int'))
951 _gitConfig[key] = None
952 return _gitConfig[key]
954 def gitConfigList(key, raw=False):
955 if key not in _gitConfig:
956 s = read_pipe(["git", "config", "--get-all", key], ignore_error=True, raw=raw)
957 _gitConfig[key] = s.strip().splitlines()
958 if _gitConfig[key] == [''] or _gitConfig[key] == [b'']:
960 return _gitConfig[key]
962 def p4BranchesInGit(branchesAreInRemotes=True):
963 """Find all the branches whose names start with "p4/", looking
964 in remotes or heads as specified by the argument. Return
965 a dictionary of { branch: revision } for each one found.
966 The branch names are the short names, without any
971 cmdline = "git rev-parse --symbolic "
972 if branchesAreInRemotes:
973 cmdline += "--remotes"
975 cmdline += "--branches"
977 for line in read_pipe_lines(cmdline):
981 if not line.startswith('p4/'):
983 # special symbolic ref to p4/master
984 if line == "p4/HEAD":
987 # strip off p4/ prefix
988 branch = line[len("p4/"):]
990 branches[branch] = parseRevision(line)
994 def branch_exists(branch):
995 """Make sure that the given ref name really exists."""
997 cmd = [ "git", "rev-parse", "--symbolic", "--verify", branch ]
998 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
999 out, _ = p.communicate()
1000 out = decode_text_stream(out)
1003 # expect exactly one line of output: the branch name
1004 return out.rstrip() == branch
1006 def findUpstreamBranchPoint(head = "HEAD"):
1007 branches = p4BranchesInGit()
1008 # map from depot-path to branch name
1009 branchByDepotPath = {}
1010 for branch in branches.keys():
1011 tip = branches[branch]
1012 log = extractLogMessageFromGitCommit(tip)
1013 settings = extractSettingsGitLog(log)
1014 if "depot-paths" in settings:
1015 paths = ",".join(settings["depot-paths"])
1016 branchByDepotPath[paths] = "remotes/p4/" + branch
1020 while parent < 65535:
1021 commit = head + "~%s" % parent
1022 log = extractLogMessageFromGitCommit(commit)
1023 settings = extractSettingsGitLog(log)
1024 if "depot-paths" in settings:
1025 paths = ",".join(settings["depot-paths"])
1026 if paths in branchByDepotPath:
1027 return [branchByDepotPath[paths], settings]
1031 return ["", settings]
1033 def createOrUpdateBranchesFromOrigin(localRefPrefix = "refs/remotes/p4/", silent=True):
1035 print("Creating/updating branch(es) in %s based on origin branch(es)"
1038 originPrefix = "origin/p4/"
1040 for line in read_pipe_lines("git rev-parse --symbolic --remotes"):
1042 if (not line.startswith(originPrefix)) or line.endswith("HEAD"):
1045 headName = line[len(originPrefix):]
1046 remoteHead = localRefPrefix + headName
1049 original = extractSettingsGitLog(extractLogMessageFromGitCommit(originHead))
1050 if ('depot-paths' not in original
1051 or 'change' not in original):
1055 if not gitBranchExists(remoteHead):
1057 print("creating %s" % remoteHead)
1060 settings = extractSettingsGitLog(extractLogMessageFromGitCommit(remoteHead))
1061 if 'change' in settings:
1062 if settings['depot-paths'] == original['depot-paths']:
1063 originP4Change = int(original['change'])
1064 p4Change = int(settings['change'])
1065 if originP4Change > p4Change:
1066 print("%s (%s) is newer than %s (%s). "
1067 "Updating p4 branch from origin."
1068 % (originHead, originP4Change,
1069 remoteHead, p4Change))
1072 print("Ignoring: %s was imported from %s while "
1073 "%s was imported from %s"
1074 % (originHead, ','.join(original['depot-paths']),
1075 remoteHead, ','.join(settings['depot-paths'])))
1078 system("git update-ref %s %s" % (remoteHead, originHead))
1080 def originP4BranchesExist():
1081 return gitBranchExists("origin") or gitBranchExists("origin/p4") or gitBranchExists("origin/p4/master")
1084 def p4ParseNumericChangeRange(parts):
1085 changeStart = int(parts[0][1:])
1086 if parts[1] == '#head':
1087 changeEnd = p4_last_change()
1089 changeEnd = int(parts[1])
1091 return (changeStart, changeEnd)
1093 def chooseBlockSize(blockSize):
1097 return defaultBlockSize
1099 def p4ChangesForPaths(depotPaths, changeRange, requestedBlockSize):
1102 # Parse the change range into start and end. Try to find integer
1103 # revision ranges as these can be broken up into blocks to avoid
1104 # hitting server-side limits (maxrows, maxscanresults). But if
1105 # that doesn't work, fall back to using the raw revision specifier
1106 # strings, without using block mode.
1108 if changeRange is None or changeRange == '':
1110 changeEnd = p4_last_change()
1111 block_size = chooseBlockSize(requestedBlockSize)
1113 parts = changeRange.split(',')
1114 assert len(parts) == 2
1116 (changeStart, changeEnd) = p4ParseNumericChangeRange(parts)
1117 block_size = chooseBlockSize(requestedBlockSize)
1119 changeStart = parts[0][1:]
1120 changeEnd = parts[1]
1121 if requestedBlockSize:
1122 die("cannot use --changes-block-size with non-numeric revisions")
1127 # Retrieve changes a block at a time, to prevent running
1128 # into a MaxResults/MaxScanRows error from the server. If
1129 # we _do_ hit one of those errors, turn down the block size
1135 end = min(changeEnd, changeStart + block_size)
1136 revisionRange = "%d,%d" % (changeStart, end)
1138 revisionRange = "%s,%s" % (changeStart, changeEnd)
1140 for p in depotPaths:
1141 cmd += ["%s...@%s" % (p, revisionRange)]
1145 result = p4CmdList(cmd, errors_as_exceptions=True)
1146 except P4RequestSizeException as e:
1148 block_size = e.limit
1149 elif block_size > e.limit:
1150 block_size = e.limit
1152 block_size = max(2, block_size // 2)
1154 if verbose: print("block size error, retrying with block size {0}".format(block_size))
1156 except P4Exception as e:
1157 die('Error retrieving changes description ({0})'.format(e.p4ExitCode))
1159 # Insert changes in chronological order
1160 for entry in reversed(result):
1161 if 'change' not in entry:
1163 changes.add(int(entry['change']))
1168 if end >= changeEnd:
1171 changeStart = end + 1
1173 changes = sorted(changes)
1176 def p4PathStartsWith(path, prefix):
1177 # This method tries to remedy a potential mixed-case issue:
1179 # If UserA adds //depot/DirA/file1
1180 # and UserB adds //depot/dira/file2
1182 # we may or may not have a problem. If you have core.ignorecase=true,
1183 # we treat DirA and dira as the same directory
1184 if gitConfigBool("core.ignorecase"):
1185 return path.lower().startswith(prefix.lower())
1186 return path.startswith(prefix)
1188 def getClientSpec():
1189 """Look at the p4 client spec, create a View() object that contains
1190 all the mappings, and return it."""
1192 specList = p4CmdList("client -o")
1193 if len(specList) != 1:
1194 die('Output from "client -o" is %d lines, expecting 1' %
1197 # dictionary of all client parameters
1200 # the //client/ name
1201 client_name = entry["Client"]
1203 # just the keys that start with "View"
1204 view_keys = [ k for k in entry.keys() if k.startswith("View") ]
1206 # hold this new View
1207 view = View(client_name)
1209 # append the lines, in order, to the view
1210 for view_num in range(len(view_keys)):
1211 k = "View%d" % view_num
1212 if k not in view_keys:
1213 die("Expected view key %s missing" % k)
1214 view.append(entry[k])
1218 def getClientRoot():
1219 """Grab the client directory."""
1221 output = p4CmdList("client -o")
1222 if len(output) != 1:
1223 die('Output from "client -o" is %d lines, expecting 1' % len(output))
1226 if "Root" not in entry:
1227 die('Client has no "Root"')
1229 return entry["Root"]
1232 # P4 wildcards are not allowed in filenames. P4 complains
1233 # if you simply add them, but you can force it with "-f", in
1234 # which case it translates them into %xx encoding internally.
1236 def wildcard_decode(path):
1237 # Search for and fix just these four characters. Do % last so
1238 # that fixing it does not inadvertently create new %-escapes.
1239 # Cannot have * in a filename in windows; untested as to
1240 # what p4 would do in such a case.
1241 if not platform.system() == "Windows":
1242 path = path.replace("%2A", "*")
1243 path = path.replace("%23", "#") \
1244 .replace("%40", "@") \
1245 .replace("%25", "%")
1248 def wildcard_encode(path):
1249 # do % first to avoid double-encoding the %s introduced here
1250 path = path.replace("%", "%25") \
1251 .replace("*", "%2A") \
1252 .replace("#", "%23") \
1253 .replace("@", "%40")
1256 def wildcard_present(path):
1257 m = re.search("[*#@%]", path)
1258 return m is not None
1260 class LargeFileSystem(object):
1261 """Base class for large file system support."""
1263 def __init__(self, writeToGitStream):
1264 self.largeFiles = set()
1265 self.writeToGitStream = writeToGitStream
1267 def generatePointer(self, cloneDestination, contentFile):
1268 """Return the content of a pointer file that is stored in Git instead of
1269 the actual content."""
1270 assert False, "Method 'generatePointer' required in " + self.__class__.__name__
1272 def pushFile(self, localLargeFile):
1273 """Push the actual content which is not stored in the Git repository to
1275 assert False, "Method 'pushFile' required in " + self.__class__.__name__
1277 def hasLargeFileExtension(self, relPath):
1278 return functools.reduce(
1279 lambda a, b: a or b,
1280 [relPath.endswith('.' + e) for e in gitConfigList('git-p4.largeFileExtensions')],
1284 def generateTempFile(self, contents):
1285 contentFile = tempfile.NamedTemporaryFile(prefix='git-p4-large-file', delete=False)
1287 contentFile.write(d)
1289 return contentFile.name
1291 def exceedsLargeFileThreshold(self, relPath, contents):
1292 if gitConfigInt('git-p4.largeFileThreshold'):
1293 contentsSize = sum(len(d) for d in contents)
1294 if contentsSize > gitConfigInt('git-p4.largeFileThreshold'):
1296 if gitConfigInt('git-p4.largeFileCompressedThreshold'):
1297 contentsSize = sum(len(d) for d in contents)
1298 if contentsSize <= gitConfigInt('git-p4.largeFileCompressedThreshold'):
1300 contentTempFile = self.generateTempFile(contents)
1301 compressedContentFile = tempfile.NamedTemporaryFile(prefix='git-p4-large-file', delete=True)
1302 with zipfile.ZipFile(compressedContentFile, mode='w') as zf:
1303 zf.write(contentTempFile, compress_type=zipfile.ZIP_DEFLATED)
1304 compressedContentsSize = zf.infolist()[0].compress_size
1305 os.remove(contentTempFile)
1306 if compressedContentsSize > gitConfigInt('git-p4.largeFileCompressedThreshold'):
1310 def addLargeFile(self, relPath):
1311 self.largeFiles.add(relPath)
1313 def removeLargeFile(self, relPath):
1314 self.largeFiles.remove(relPath)
1316 def isLargeFile(self, relPath):
1317 return relPath in self.largeFiles
1319 def processContent(self, git_mode, relPath, contents):
1320 """Processes the content of git fast import. This method decides if a
1321 file is stored in the large file system and handles all necessary
1323 if self.exceedsLargeFileThreshold(relPath, contents) or self.hasLargeFileExtension(relPath):
1324 contentTempFile = self.generateTempFile(contents)
1325 (pointer_git_mode, contents, localLargeFile) = self.generatePointer(contentTempFile)
1326 if pointer_git_mode:
1327 git_mode = pointer_git_mode
1329 # Move temp file to final location in large file system
1330 largeFileDir = os.path.dirname(localLargeFile)
1331 if not os.path.isdir(largeFileDir):
1332 os.makedirs(largeFileDir)
1333 shutil.move(contentTempFile, localLargeFile)
1334 self.addLargeFile(relPath)
1335 if gitConfigBool('git-p4.largeFilePush'):
1336 self.pushFile(localLargeFile)
1338 sys.stderr.write("%s moved to large file system (%s)\n" % (relPath, localLargeFile))
1339 return (git_mode, contents)
1341 class MockLFS(LargeFileSystem):
1342 """Mock large file system for testing."""
1344 def generatePointer(self, contentFile):
1345 """The pointer content is the original content prefixed with "pointer-".
1346 The local filename of the large file storage is derived from the file content.
1348 with open(contentFile, 'r') as f:
1351 pointerContents = 'pointer-' + content
1352 localLargeFile = os.path.join(os.getcwd(), '.git', 'mock-storage', 'local', content[:-1])
1353 return (gitMode, pointerContents, localLargeFile)
1355 def pushFile(self, localLargeFile):
1356 """The remote filename of the large file storage is the same as the local
1357 one but in a different directory.
1359 remotePath = os.path.join(os.path.dirname(localLargeFile), '..', 'remote')
1360 if not os.path.exists(remotePath):
1361 os.makedirs(remotePath)
1362 shutil.copyfile(localLargeFile, os.path.join(remotePath, os.path.basename(localLargeFile)))
1364 class GitLFS(LargeFileSystem):
1365 """Git LFS as backend for the git-p4 large file system.
1366 See https://git-lfs.github.com/ for details."""
1368 def __init__(self, *args):
1369 LargeFileSystem.__init__(self, *args)
1370 self.baseGitAttributes = []
1372 def generatePointer(self, contentFile):
1373 """Generate a Git LFS pointer for the content. Return LFS Pointer file
1374 mode and content which is stored in the Git repository instead of
1375 the actual content. Return also the new location of the actual
1378 if os.path.getsize(contentFile) == 0:
1379 return (None, '', None)
1381 pointerProcess = subprocess.Popen(
1382 ['git', 'lfs', 'pointer', '--file=' + contentFile],
1383 stdout=subprocess.PIPE
1385 pointerFile = decode_text_stream(pointerProcess.stdout.read())
1386 if pointerProcess.wait():
1387 os.remove(contentFile)
1388 die('git-lfs pointer command failed. Did you install the extension?')
1390 # Git LFS removed the preamble in the output of the 'pointer' command
1391 # starting from version 1.2.0. Check for the preamble here to support
1393 # c.f. https://github.com/github/git-lfs/commit/da2935d9a739592bc775c98d8ef4df9c72ea3b43
1394 if pointerFile.startswith('Git LFS pointer for'):
1395 pointerFile = re.sub(r'Git LFS pointer for.*\n\n', '', pointerFile)
1397 oid = re.search(r'^oid \w+:(\w+)', pointerFile, re.MULTILINE).group(1)
1398 # if someone use external lfs.storage ( not in local repo git )
1399 lfs_path = gitConfig('lfs.storage')
1402 if not os.path.isabs(lfs_path):
1403 lfs_path = os.path.join(os.getcwd(), '.git', lfs_path)
1404 localLargeFile = os.path.join(
1406 'objects', oid[:2], oid[2:4],
1409 # LFS Spec states that pointer files should not have the executable bit set.
1411 return (gitMode, pointerFile, localLargeFile)
1413 def pushFile(self, localLargeFile):
1414 uploadProcess = subprocess.Popen(
1415 ['git', 'lfs', 'push', '--object-id', 'origin', os.path.basename(localLargeFile)]
1417 if uploadProcess.wait():
1418 die('git-lfs push command failed. Did you define a remote?')
1420 def generateGitAttributes(self):
1422 self.baseGitAttributes +
1426 '# Git LFS (see https://git-lfs.github.com/)\n',
1429 ['*.' + f.replace(' ', '[[:space:]]') + ' filter=lfs diff=lfs merge=lfs -text\n'
1430 for f in sorted(gitConfigList('git-p4.largeFileExtensions'))
1432 ['/' + f.replace(' ', '[[:space:]]') + ' filter=lfs diff=lfs merge=lfs -text\n'
1433 for f in sorted(self.largeFiles) if not self.hasLargeFileExtension(f)
1437 def addLargeFile(self, relPath):
1438 LargeFileSystem.addLargeFile(self, relPath)
1439 self.writeToGitStream('100644', '.gitattributes', self.generateGitAttributes())
1441 def removeLargeFile(self, relPath):
1442 LargeFileSystem.removeLargeFile(self, relPath)
1443 self.writeToGitStream('100644', '.gitattributes', self.generateGitAttributes())
1445 def processContent(self, git_mode, relPath, contents):
1446 if relPath == '.gitattributes':
1447 self.baseGitAttributes = contents
1448 return (git_mode, self.generateGitAttributes())
1450 return LargeFileSystem.processContent(self, git_mode, relPath, contents)
1453 delete_actions = ( "delete", "move/delete", "purge" )
1454 add_actions = ( "add", "branch", "move/add" )
1457 self.usage = "usage: %prog [options]"
1458 self.needsGit = True
1459 self.verbose = False
1461 # This is required for the "append" update_shelve action
1462 def ensure_value(self, attr, value):
1463 if not hasattr(self, attr) or getattr(self, attr) is None:
1464 setattr(self, attr, value)
1465 return getattr(self, attr)
1469 self.userMapFromPerforceServer = False
1470 self.myP4UserId = None
1474 return self.myP4UserId
1476 results = p4CmdList("user -o")
1479 self.myP4UserId = r['User']
1481 die("Could not find your p4 user id")
1483 def p4UserIsMe(self, p4User):
1484 # return True if the given p4 user is actually me
1485 me = self.p4UserId()
1486 if not p4User or p4User != me:
1491 def getUserCacheFilename(self):
1492 home = os.environ.get("HOME", os.environ.get("USERPROFILE"))
1493 return home + "/.gitp4-usercache.txt"
1495 def getUserMapFromPerforceServer(self):
1496 if self.userMapFromPerforceServer:
1501 for output in p4CmdList("users"):
1502 if "User" not in output:
1504 self.users[output["User"]] = output["FullName"] + b" <" + output["Email"] + b">"
1505 self.emails[output["Email"]] = output["User"]
1507 mapUserConfigRegex = re.compile(br"^\s*(\S+)\s*=\s*(.+)\s*<(\S+)>\s*$", re.VERBOSE)
1508 for mapUserConfig in gitConfigList("git-p4.mapUser", raw=True):
1509 mapUser = mapUserConfigRegex.findall(mapUserConfig)
1510 if mapUser and len(mapUser[0]) == 3:
1511 user = mapUser[0][0]
1512 fullname = mapUser[0][1]
1513 email = mapUser[0][2]
1514 self.users[user] = fullname + b" <" + email + b">"
1515 self.emails[email] = user
1518 for (key, val) in self.users.items():
1519 s += b"%s\t%s\n" % (key.expandtabs(1), val.expandtabs(1))
1521 open(self.getUserCacheFilename(), 'wb').write(s)
1522 self.userMapFromPerforceServer = True
1524 def loadUserMapFromCache(self):
1526 self.userMapFromPerforceServer = False
1528 cache = open(self.getUserCacheFilename(), 'rb')
1529 lines = cache.readlines()
1532 entry = line.strip().split(b"\t")
1533 self.users[entry[0]] = entry[1]
1535 self.getUserMapFromPerforceServer()
1537 class P4Debug(Command):
1539 Command.__init__(self)
1541 self.description = "A tool to debug the output of p4 -G."
1542 self.needsGit = False
1544 def run(self, args):
1546 for output in p4CmdList(args):
1547 print('Element: %d' % j)
1552 class P4RollBack(Command):
1554 Command.__init__(self)
1556 optparse.make_option("--local", dest="rollbackLocalBranches", action="store_true")
1558 self.description = "A tool to debug the multi-branch import. Don't use :)"
1559 self.rollbackLocalBranches = False
1561 def run(self, args):
1564 maxChange = int(args[0])
1566 if "p4ExitCode" in p4Cmd("changes -m 1"):
1567 die("Problems executing p4");
1569 if self.rollbackLocalBranches:
1570 refPrefix = "refs/heads/"
1571 lines = read_pipe_lines("git rev-parse --symbolic --branches")
1573 refPrefix = "refs/remotes/"
1574 lines = read_pipe_lines("git rev-parse --symbolic --remotes")
1577 if self.rollbackLocalBranches or (line.startswith("p4/") and line != "p4/HEAD\n"):
1579 ref = refPrefix + line
1580 log = extractLogMessageFromGitCommit(ref)
1581 settings = extractSettingsGitLog(log)
1583 depotPaths = settings['depot-paths']
1584 change = settings['change']
1588 if len(p4Cmd("changes -m 1 " + ' '.join (['%s...@%s' % (p, maxChange)
1589 for p in depotPaths]))) == 0:
1590 print("Branch %s did not exist at change %s, deleting." % (ref, maxChange))
1591 system("git update-ref -d %s `git rev-parse %s`" % (ref, ref))
1594 while change and int(change) > maxChange:
1597 print("%s is at %s ; rewinding towards %s" % (ref, change, maxChange))
1598 system("git update-ref %s \"%s^\"" % (ref, ref))
1599 log = extractLogMessageFromGitCommit(ref)
1600 settings = extractSettingsGitLog(log)
1603 depotPaths = settings['depot-paths']
1604 change = settings['change']
1607 print("%s rewound to %s" % (ref, change))
1611 class P4Submit(Command, P4UserMap):
1613 conflict_behavior_choices = ("ask", "skip", "quit")
1616 Command.__init__(self)
1617 P4UserMap.__init__(self)
1619 optparse.make_option("--origin", dest="origin"),
1620 optparse.make_option("-M", dest="detectRenames", action="store_true"),
1621 # preserve the user, requires relevant p4 permissions
1622 optparse.make_option("--preserve-user", dest="preserveUser", action="store_true"),
1623 optparse.make_option("--export-labels", dest="exportLabels", action="store_true"),
1624 optparse.make_option("--dry-run", "-n", dest="dry_run", action="store_true"),
1625 optparse.make_option("--prepare-p4-only", dest="prepare_p4_only", action="store_true"),
1626 optparse.make_option("--conflict", dest="conflict_behavior",
1627 choices=self.conflict_behavior_choices),
1628 optparse.make_option("--branch", dest="branch"),
1629 optparse.make_option("--shelve", dest="shelve", action="store_true",
1630 help="Shelve instead of submit. Shelved files are reverted, "
1631 "restoring the workspace to the state before the shelve"),
1632 optparse.make_option("--update-shelve", dest="update_shelve", action="append", type="int",
1633 metavar="CHANGELIST",
1634 help="update an existing shelved changelist, implies --shelve, "
1635 "repeat in-order for multiple shelved changelists"),
1636 optparse.make_option("--commit", dest="commit", metavar="COMMIT",
1637 help="submit only the specified commit(s), one commit or xxx..xxx"),
1638 optparse.make_option("--disable-rebase", dest="disable_rebase", action="store_true",
1639 help="Disable rebase after submit is completed. Can be useful if you "
1640 "work from a local git branch that is not master"),
1641 optparse.make_option("--disable-p4sync", dest="disable_p4sync", action="store_true",
1642 help="Skip Perforce sync of p4/master after submit or shelve"),
1643 optparse.make_option("--no-verify", dest="no_verify", action="store_true",
1644 help="Bypass p4-pre-submit and p4-changelist hooks"),
1646 self.description = """Submit changes from git to the perforce depot.\n
1647 The `p4-pre-submit` hook is executed if it exists and is executable. It
1648 can be bypassed with the `--no-verify` command line option. The hook takes
1649 no parameters and nothing from standard input. Exiting with a non-zero status
1650 from this script prevents `git-p4 submit` from launching.
1652 One usage scenario is to run unit tests in the hook.
1654 The `p4-prepare-changelist` hook is executed right after preparing the default
1655 changelist message and before the editor is started. It takes one parameter,
1656 the name of the file that contains the changelist text. Exiting with a non-zero
1657 status from the script will abort the process.
1659 The purpose of the hook is to edit the message file in place, and it is not
1660 supressed by the `--no-verify` option. This hook is called even if
1661 `--prepare-p4-only` is set.
1663 The `p4-changelist` hook is executed after the changelist message has been
1664 edited by the user. It can be bypassed with the `--no-verify` option. It
1665 takes a single parameter, the name of the file that holds the proposed
1666 changelist text. Exiting with a non-zero status causes the command to abort.
1668 The hook is allowed to edit the changelist file and can be used to normalize
1669 the text into some project standard format. It can also be used to refuse the
1670 Submit after inspect the message file.
1672 The `p4-post-changelist` hook is invoked after the submit has successfully
1673 occurred in P4. It takes no parameters and is meant primarily for notification
1674 and cannot affect the outcome of the git p4 submit action.
1677 self.usage += " [name of git branch to submit into perforce depot]"
1679 self.detectRenames = False
1680 self.preserveUser = gitConfigBool("git-p4.preserveUser")
1681 self.dry_run = False
1683 self.update_shelve = list()
1685 self.disable_rebase = gitConfigBool("git-p4.disableRebase")
1686 self.disable_p4sync = gitConfigBool("git-p4.disableP4Sync")
1687 self.prepare_p4_only = False
1688 self.conflict_behavior = None
1689 self.isWindows = (platform.system() == "Windows")
1690 self.exportLabels = False
1691 self.p4HasMoveCommand = p4_has_move_command()
1693 self.no_verify = False
1695 if gitConfig('git-p4.largeFileSystem'):
1696 die("Large file system not supported for git-p4 submit command. Please remove it from config.")
1699 if len(p4CmdList("opened ...")) > 0:
1700 die("You have files opened with perforce! Close them before starting the sync.")
1702 def separate_jobs_from_description(self, message):
1703 """Extract and return a possible Jobs field in the commit
1704 message. It goes into a separate section in the p4 change
1707 A jobs line starts with "Jobs:" and looks like a new field
1708 in a form. Values are white-space separated on the same
1709 line or on following lines that start with a tab.
1711 This does not parse and extract the full git commit message
1712 like a p4 form. It just sees the Jobs: line as a marker
1713 to pass everything from then on directly into the p4 form,
1714 but outside the description section.
1716 Return a tuple (stripped log message, jobs string)."""
1718 m = re.search(r'^Jobs:', message, re.MULTILINE)
1720 return (message, None)
1722 jobtext = message[m.start():]
1723 stripped_message = message[:m.start()].rstrip()
1724 return (stripped_message, jobtext)
1726 def prepareLogMessage(self, template, message, jobs):
1727 """Edits the template returned from "p4 change -o" to insert
1728 the message in the Description field, and the jobs text in
1732 inDescriptionSection = False
1734 for line in template.split("\n"):
1735 if line.startswith("#"):
1736 result += line + "\n"
1739 if inDescriptionSection:
1740 if line.startswith("Files:") or line.startswith("Jobs:"):
1741 inDescriptionSection = False
1742 # insert Jobs section
1744 result += jobs + "\n"
1748 if line.startswith("Description:"):
1749 inDescriptionSection = True
1751 for messageLine in message.split("\n"):
1752 line += "\t" + messageLine + "\n"
1754 result += line + "\n"
1758 def patchRCSKeywords(self, file, pattern):
1759 # Attempt to zap the RCS keywords in a p4 controlled file matching the given pattern
1760 (handle, outFileName) = tempfile.mkstemp(dir='.')
1762 outFile = os.fdopen(handle, "w+")
1763 inFile = open(file, "r")
1764 regexp = re.compile(pattern, re.VERBOSE)
1765 for line in inFile.readlines():
1766 line = regexp.sub(r'$\1$', line)
1770 # Forcibly overwrite the original file
1772 shutil.move(outFileName, file)
1774 # cleanup our temporary file
1775 os.unlink(outFileName)
1776 print("Failed to strip RCS keywords in %s" % file)
1779 print("Patched up RCS keywords in %s" % file)
1781 def p4UserForCommit(self,id):
1782 # Return the tuple (perforce user,git email) for a given git commit id
1783 self.getUserMapFromPerforceServer()
1784 gitEmail = read_pipe(["git", "log", "--max-count=1",
1785 "--format=%ae", id], raw=True)
1786 gitEmail = gitEmail.strip()
1787 if gitEmail not in self.emails:
1788 return (None,gitEmail)
1790 return (self.emails[gitEmail],gitEmail)
1792 def checkValidP4Users(self,commits):
1793 # check if any git authors cannot be mapped to p4 users
1795 (user,email) = self.p4UserForCommit(id)
1797 msg = "Cannot find p4 user for email %s in commit %s." % (email, id)
1798 if gitConfigBool("git-p4.allowMissingP4Users"):
1801 die("Error: %s\nSet git-p4.allowMissingP4Users to true to allow this." % msg)
1803 def lastP4Changelist(self):
1804 # Get back the last changelist number submitted in this client spec. This
1805 # then gets used to patch up the username in the change. If the same
1806 # client spec is being used by multiple processes then this might go
1808 results = p4CmdList("client -o") # find the current client
1812 client = r['Client']
1815 die("could not get client spec")
1816 results = p4CmdList(["changes", "-c", client, "-m", "1"])
1820 die("Could not get changelist number for last submit - cannot patch up user details")
1822 def modifyChangelistUser(self, changelist, newUser):
1823 # fixup the user field of a changelist after it has been submitted.
1824 changes = p4CmdList("change -o %s" % changelist)
1825 if len(changes) != 1:
1826 die("Bad output from p4 change modifying %s to user %s" %
1827 (changelist, newUser))
1830 if c['User'] == newUser: return # nothing to do
1832 # p4 does not understand format version 3 and above
1833 input = marshal.dumps(c, 2)
1835 result = p4CmdList("change -f -i", stdin=input)
1838 if r['code'] == 'error':
1839 die("Could not modify user field of changelist %s to %s:%s" % (changelist, newUser, r['data']))
1841 print("Updated user field for changelist %s to %s" % (changelist, newUser))
1843 die("Could not modify user field of changelist %s to %s" % (changelist, newUser))
1845 def canChangeChangelists(self):
1846 # check to see if we have p4 admin or super-user permissions, either of
1847 # which are required to modify changelists.
1848 results = p4CmdList(["protects", self.depotPath])
1851 if r['perm'] == 'admin':
1853 if r['perm'] == 'super':
1857 def prepareSubmitTemplate(self, changelist=None):
1858 """Run "p4 change -o" to grab a change specification template.
1859 This does not use "p4 -G", as it is nice to keep the submission
1860 template in original order, since a human might edit it.
1862 Remove lines in the Files section that show changes to files
1863 outside the depot path we're committing into."""
1865 [upstream, settings] = findUpstreamBranchPoint()
1868 # A Perforce Change Specification.
1870 # Change: The change number. 'new' on a new changelist.
1871 # Date: The date this specification was last modified.
1872 # Client: The client on which the changelist was created. Read-only.
1873 # User: The user who created the changelist.
1874 # Status: Either 'pending' or 'submitted'. Read-only.
1875 # Type: Either 'public' or 'restricted'. Default is 'public'.
1876 # Description: Comments about the changelist. Required.
1877 # Jobs: What opened jobs are to be closed by this changelist.
1878 # You may delete jobs from this list. (New changelists only.)
1879 # Files: What opened files from the default changelist are to be added
1880 # to this changelist. You may delete files from this list.
1881 # (New changelists only.)
1884 inFilesSection = False
1886 args = ['change', '-o']
1888 args.append(str(changelist))
1889 for entry in p4CmdList(args):
1890 if 'code' not in entry:
1892 if entry['code'] == 'stat':
1893 change_entry = entry
1895 if not change_entry:
1896 die('Failed to decode output of p4 change -o')
1897 for key, value in change_entry.items():
1898 if key.startswith('File'):
1899 if 'depot-paths' in settings:
1900 if not [p for p in settings['depot-paths']
1901 if p4PathStartsWith(value, p)]:
1904 if not p4PathStartsWith(value, self.depotPath):
1906 files_list.append(value)
1908 # Output in the order expected by prepareLogMessage
1909 for key in ['Change', 'Client', 'User', 'Status', 'Description', 'Jobs']:
1910 if key not in change_entry:
1913 template += key + ':'
1914 if key == 'Description':
1916 for field_line in decode_text_stream(change_entry[key]).splitlines():
1917 template += '\t'+field_line+'\n'
1918 if len(files_list) > 0:
1920 template += 'Files:\n'
1921 for path in files_list:
1922 template += '\t'+path+'\n'
1925 def edit_template(self, template_file):
1926 """Invoke the editor to let the user change the submission
1927 message. Return true if okay to continue with the submit."""
1929 # if configured to skip the editing part, just submit
1930 if gitConfigBool("git-p4.skipSubmitEdit"):
1933 # look at the modification time, to check later if the user saved
1935 mtime = os.stat(template_file).st_mtime
1938 if "P4EDITOR" in os.environ and (os.environ.get("P4EDITOR") != ""):
1939 editor = os.environ.get("P4EDITOR")
1941 editor = read_pipe("git var GIT_EDITOR").strip()
1942 system(["sh", "-c", ('%s "$@"' % editor), editor, template_file])
1944 # If the file was not saved, prompt to see if this patch should
1945 # be skipped. But skip this verification step if configured so.
1946 if gitConfigBool("git-p4.skipSubmitEditCheck"):
1949 # modification time updated means user saved the file
1950 if os.stat(template_file).st_mtime > mtime:
1953 response = prompt("Submit template unchanged. Submit anyway? [y]es, [n]o (skip this patch) ")
1959 def get_diff_description(self, editedFiles, filesToAdd, symlinks):
1961 if "P4DIFF" in os.environ:
1962 del(os.environ["P4DIFF"])
1964 for editedFile in editedFiles:
1965 diff += p4_read_pipe(['diff', '-du',
1966 wildcard_encode(editedFile)])
1970 for newFile in filesToAdd:
1971 newdiff += "==== new file ====\n"
1972 newdiff += "--- /dev/null\n"
1973 newdiff += "+++ %s\n" % newFile
1975 is_link = os.path.islink(newFile)
1976 expect_link = newFile in symlinks
1978 if is_link and expect_link:
1979 newdiff += "+%s\n" % os.readlink(newFile)
1981 f = open(newFile, "r")
1982 for line in f.readlines():
1983 newdiff += "+" + line
1986 return (diff + newdiff).replace('\r\n', '\n')
1988 def applyCommit(self, id):
1989 """Apply one commit, return True if it succeeded."""
1991 print("Applying", read_pipe(["git", "show", "-s",
1992 "--format=format:%h %s", id]))
1994 (p4User, gitEmail) = self.p4UserForCommit(id)
1996 diff = read_pipe_lines("git diff-tree -r %s \"%s^\" \"%s\"" % (self.diffOpts, id, id))
1998 filesToChangeType = set()
1999 filesToDelete = set()
2001 pureRenameCopy = set()
2003 filesToChangeExecBit = {}
2007 diff = parseDiffTreeEntry(line)
2008 modifier = diff['status']
2010 all_files.append(path)
2014 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
2015 filesToChangeExecBit[path] = diff['dst_mode']
2016 editedFiles.add(path)
2017 elif modifier == "A":
2018 filesToAdd.add(path)
2019 filesToChangeExecBit[path] = diff['dst_mode']
2020 if path in filesToDelete:
2021 filesToDelete.remove(path)
2023 dst_mode = int(diff['dst_mode'], 8)
2024 if dst_mode == 0o120000:
2027 elif modifier == "D":
2028 filesToDelete.add(path)
2029 if path in filesToAdd:
2030 filesToAdd.remove(path)
2031 elif modifier == "C":
2032 src, dest = diff['src'], diff['dst']
2033 all_files.append(dest)
2034 p4_integrate(src, dest)
2035 pureRenameCopy.add(dest)
2036 if diff['src_sha1'] != diff['dst_sha1']:
2038 pureRenameCopy.discard(dest)
2039 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
2041 pureRenameCopy.discard(dest)
2042 filesToChangeExecBit[dest] = diff['dst_mode']
2044 # turn off read-only attribute
2045 os.chmod(dest, stat.S_IWRITE)
2047 editedFiles.add(dest)
2048 elif modifier == "R":
2049 src, dest = diff['src'], diff['dst']
2050 all_files.append(dest)
2051 if self.p4HasMoveCommand:
2052 p4_edit(src) # src must be open before move
2053 p4_move(src, dest) # opens for (move/delete, move/add)
2055 p4_integrate(src, dest)
2056 if diff['src_sha1'] != diff['dst_sha1']:
2059 pureRenameCopy.add(dest)
2060 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
2061 if not self.p4HasMoveCommand:
2062 p4_edit(dest) # with move: already open, writable
2063 filesToChangeExecBit[dest] = diff['dst_mode']
2064 if not self.p4HasMoveCommand:
2066 os.chmod(dest, stat.S_IWRITE)
2068 filesToDelete.add(src)
2069 editedFiles.add(dest)
2070 elif modifier == "T":
2071 filesToChangeType.add(path)
2073 die("unknown modifier %s for %s" % (modifier, path))
2075 diffcmd = "git diff-tree --full-index -p \"%s\"" % (id)
2076 patchcmd = diffcmd + " | git apply "
2077 tryPatchCmd = patchcmd + "--check -"
2078 applyPatchCmd = patchcmd + "--check --apply -"
2079 patch_succeeded = True
2082 print("TryPatch: %s" % tryPatchCmd)
2084 if os.system(tryPatchCmd) != 0:
2085 fixed_rcs_keywords = False
2086 patch_succeeded = False
2087 print("Unfortunately applying the change failed!")
2089 # Patch failed, maybe it's just RCS keyword woes. Look through
2090 # the patch to see if that's possible.
2091 if gitConfigBool("git-p4.attemptRCSCleanup"):
2095 for file in editedFiles | filesToDelete:
2096 # did this file's delta contain RCS keywords?
2097 pattern = p4_keywords_regexp_for_file(file)
2100 # this file is a possibility...look for RCS keywords.
2101 regexp = re.compile(pattern, re.VERBOSE)
2102 for line in read_pipe_lines(["git", "diff", "%s^..%s" % (id, id), file]):
2103 if regexp.search(line):
2105 print("got keyword match on %s in %s in %s" % (pattern, line, file))
2106 kwfiles[file] = pattern
2109 for file in kwfiles:
2111 print("zapping %s with %s" % (line,pattern))
2112 # File is being deleted, so not open in p4. Must
2113 # disable the read-only bit on windows.
2114 if self.isWindows and file not in editedFiles:
2115 os.chmod(file, stat.S_IWRITE)
2116 self.patchRCSKeywords(file, kwfiles[file])
2117 fixed_rcs_keywords = True
2119 if fixed_rcs_keywords:
2120 print("Retrying the patch with RCS keywords cleaned up")
2121 if os.system(tryPatchCmd) == 0:
2122 patch_succeeded = True
2123 print("Patch succeesed this time with RCS keywords cleaned")
2125 if not patch_succeeded:
2126 for f in editedFiles:
2131 # Apply the patch for real, and do add/delete/+x handling.
2133 system(applyPatchCmd)
2135 for f in filesToChangeType:
2136 p4_edit(f, "-t", "auto")
2137 for f in filesToAdd:
2139 for f in filesToDelete:
2143 # Set/clear executable bits
2144 for f in filesToChangeExecBit.keys():
2145 mode = filesToChangeExecBit[f]
2146 setP4ExecBit(f, mode)
2149 if len(self.update_shelve) > 0:
2150 update_shelve = self.update_shelve.pop(0)
2151 p4_reopen_in_change(update_shelve, all_files)
2154 # Build p4 change description, starting with the contents
2155 # of the git commit message.
2157 logMessage = extractLogMessageFromGitCommit(id)
2158 logMessage = logMessage.strip()
2159 (logMessage, jobs) = self.separate_jobs_from_description(logMessage)
2161 template = self.prepareSubmitTemplate(update_shelve)
2162 submitTemplate = self.prepareLogMessage(template, logMessage, jobs)
2164 if self.preserveUser:
2165 submitTemplate += "\n######## Actual user %s, modified after commit\n" % p4User
2167 if self.checkAuthorship and not self.p4UserIsMe(p4User):
2168 submitTemplate += "######## git author %s does not match your p4 account.\n" % decode_text_stream(gitEmail)
2169 submitTemplate += "######## Use option --preserve-user to modify authorship.\n"
2170 submitTemplate += "######## Variable git-p4.skipUserNameCheck hides this message.\n"
2172 separatorLine = "######## everything below this line is just the diff #######\n"
2173 if not self.prepare_p4_only:
2174 submitTemplate += separatorLine
2175 submitTemplate += self.get_diff_description(editedFiles, filesToAdd, symlinks)
2177 (handle, fileName) = tempfile.mkstemp()
2178 tmpFile = os.fdopen(handle, "w+b")
2180 submitTemplate = submitTemplate.replace("\n", "\r\n")
2181 tmpFile.write(encode_text_stream(submitTemplate))
2187 # Allow the hook to edit the changelist text before presenting it
2189 if not run_git_hook("p4-prepare-changelist", [fileName]):
2192 if self.prepare_p4_only:
2194 # Leave the p4 tree prepared, and the submit template around
2195 # and let the user decide what to do next
2199 print("P4 workspace prepared for submission.")
2200 print("To submit or revert, go to client workspace")
2201 print(" " + self.clientPath)
2203 print("To submit, use \"p4 submit\" to write a new description,")
2204 print("or \"p4 submit -i <%s\" to use the one prepared by" \
2205 " \"git p4\"." % fileName)
2206 print("You can delete the file \"%s\" when finished." % fileName)
2208 if self.preserveUser and p4User and not self.p4UserIsMe(p4User):
2209 print("To preserve change ownership by user %s, you must\n" \
2210 "do \"p4 change -f <change>\" after submitting and\n" \
2211 "edit the User field.")
2213 print("After submitting, renamed files must be re-synced.")
2214 print("Invoke \"p4 sync -f\" on each of these files:")
2215 for f in pureRenameCopy:
2219 print("To revert the changes, use \"p4 revert ...\", and delete")
2220 print("the submit template file \"%s\"" % fileName)
2222 print("Since the commit adds new files, they must be deleted:")
2223 for f in filesToAdd:
2229 if self.edit_template(fileName):
2230 if not self.no_verify:
2231 if not run_git_hook("p4-changelist", [fileName]):
2232 print("The p4-changelist hook failed.")
2236 # read the edited message and submit
2237 tmpFile = open(fileName, "rb")
2238 message = decode_text_stream(tmpFile.read())
2241 message = message.replace("\r\n", "\n")
2242 if message.find(separatorLine) != -1:
2243 submitTemplate = message[:message.index(separatorLine)]
2245 submitTemplate = message
2247 if len(submitTemplate.strip()) == 0:
2248 print("Changelist is empty, aborting this changelist.")
2253 p4_write_pipe(['shelve', '-r', '-i'], submitTemplate)
2255 p4_write_pipe(['shelve', '-i'], submitTemplate)
2257 p4_write_pipe(['submit', '-i'], submitTemplate)
2258 # The rename/copy happened by applying a patch that created a
2259 # new file. This leaves it writable, which confuses p4.
2260 for f in pureRenameCopy:
2263 if self.preserveUser:
2265 # Get last changelist number. Cannot easily get it from
2266 # the submit command output as the output is
2268 changelist = self.lastP4Changelist()
2269 self.modifyChangelistUser(changelist, p4User)
2273 run_git_hook("p4-post-changelist")
2275 # Revert changes if we skip this patch
2276 if not submitted or self.shelve:
2278 print ("Reverting shelved files.")
2280 print ("Submission cancelled, undoing p4 changes.")
2282 for f in editedFiles | filesToDelete:
2284 for f in filesToAdd:
2288 if not self.prepare_p4_only:
2292 # Export git tags as p4 labels. Create a p4 label and then tag
2294 def exportGitTags(self, gitTags):
2295 validLabelRegexp = gitConfig("git-p4.labelExportRegexp")
2296 if len(validLabelRegexp) == 0:
2297 validLabelRegexp = defaultLabelRegexp
2298 m = re.compile(validLabelRegexp)
2300 for name in gitTags:
2302 if not m.match(name):
2304 print("tag %s does not match regexp %s" % (name, validLabelRegexp))
2307 # Get the p4 commit this corresponds to
2308 logMessage = extractLogMessageFromGitCommit(name)
2309 values = extractSettingsGitLog(logMessage)
2311 if 'change' not in values:
2312 # a tag pointing to something not sent to p4; ignore
2314 print("git tag %s does not give a p4 commit" % name)
2317 changelist = values['change']
2319 # Get the tag details.
2323 for l in read_pipe_lines(["git", "cat-file", "-p", name]):
2326 if re.match(r'tag\s+', l):
2328 elif re.match(r'\s*$', l):
2335 body = ["lightweight tag imported by git p4\n"]
2337 # Create the label - use the same view as the client spec we are using
2338 clientSpec = getClientSpec()
2340 labelTemplate = "Label: %s\n" % name
2341 labelTemplate += "Description:\n"
2343 labelTemplate += "\t" + b + "\n"
2344 labelTemplate += "View:\n"
2345 for depot_side in clientSpec.mappings:
2346 labelTemplate += "\t%s\n" % depot_side
2349 print("Would create p4 label %s for tag" % name)
2350 elif self.prepare_p4_only:
2351 print("Not creating p4 label %s for tag due to option" \
2352 " --prepare-p4-only" % name)
2354 p4_write_pipe(["label", "-i"], labelTemplate)
2357 p4_system(["tag", "-l", name] +
2358 ["%s@%s" % (depot_side, changelist) for depot_side in clientSpec.mappings])
2361 print("created p4 label for tag %s" % name)
2363 def run(self, args):
2365 self.master = currentGitBranch()
2366 elif len(args) == 1:
2367 self.master = args[0]
2368 if not branchExists(self.master):
2369 die("Branch %s does not exist" % self.master)
2373 for i in self.update_shelve:
2375 sys.exit("invalid changelist %d" % i)
2378 allowSubmit = gitConfig("git-p4.allowSubmit")
2379 if len(allowSubmit) > 0 and not self.master in allowSubmit.split(","):
2380 die("%s is not in git-p4.allowSubmit" % self.master)
2382 [upstream, settings] = findUpstreamBranchPoint()
2383 self.depotPath = settings['depot-paths'][0]
2384 if len(self.origin) == 0:
2385 self.origin = upstream
2387 if len(self.update_shelve) > 0:
2390 if self.preserveUser:
2391 if not self.canChangeChangelists():
2392 die("Cannot preserve user names without p4 super-user or admin permissions")
2394 # if not set from the command line, try the config file
2395 if self.conflict_behavior is None:
2396 val = gitConfig("git-p4.conflict")
2398 if val not in self.conflict_behavior_choices:
2399 die("Invalid value '%s' for config git-p4.conflict" % val)
2402 self.conflict_behavior = val
2405 print("Origin branch is " + self.origin)
2407 if len(self.depotPath) == 0:
2408 print("Internal error: cannot locate perforce depot path from existing branches")
2411 self.useClientSpec = False
2412 if gitConfigBool("git-p4.useclientspec"):
2413 self.useClientSpec = True
2414 if self.useClientSpec:
2415 self.clientSpecDirs = getClientSpec()
2417 # Check for the existence of P4 branches
2418 branchesDetected = (len(p4BranchesInGit().keys()) > 1)
2420 if self.useClientSpec and not branchesDetected:
2421 # all files are relative to the client spec
2422 self.clientPath = getClientRoot()
2424 self.clientPath = p4Where(self.depotPath)
2426 if self.clientPath == "":
2427 die("Error: Cannot locate perforce checkout of %s in client view" % self.depotPath)
2429 print("Perforce checkout for depot path %s located at %s" % (self.depotPath, self.clientPath))
2430 self.oldWorkingDirectory = os.getcwd()
2432 # ensure the clientPath exists
2433 new_client_dir = False
2434 if not os.path.exists(self.clientPath):
2435 new_client_dir = True
2436 os.makedirs(self.clientPath)
2438 chdir(self.clientPath, is_client_path=True)
2440 print("Would synchronize p4 checkout in %s" % self.clientPath)
2442 print("Synchronizing p4 checkout...")
2444 # old one was destroyed, and maybe nobody told p4
2445 p4_sync("...", "-f")
2452 committish = self.master
2456 if self.commit != "":
2457 if self.commit.find("..") != -1:
2458 limits_ish = self.commit.split("..")
2459 for line in read_pipe_lines(["git", "rev-list", "--no-merges", "%s..%s" % (limits_ish[0], limits_ish[1])]):
2460 commits.append(line.strip())
2463 commits.append(self.commit)
2465 for line in read_pipe_lines(["git", "rev-list", "--no-merges", "%s..%s" % (self.origin, committish)]):
2466 commits.append(line.strip())
2469 if self.preserveUser or gitConfigBool("git-p4.skipUserNameCheck"):
2470 self.checkAuthorship = False
2472 self.checkAuthorship = True
2474 if self.preserveUser:
2475 self.checkValidP4Users(commits)
2478 # Build up a set of options to be passed to diff when
2479 # submitting each commit to p4.
2481 if self.detectRenames:
2482 # command-line -M arg
2483 self.diffOpts = "-M"
2485 # If not explicitly set check the config variable
2486 detectRenames = gitConfig("git-p4.detectRenames")
2488 if detectRenames.lower() == "false" or detectRenames == "":
2490 elif detectRenames.lower() == "true":
2491 self.diffOpts = "-M"
2493 self.diffOpts = "-M%s" % detectRenames
2495 # no command-line arg for -C or --find-copies-harder, just
2497 detectCopies = gitConfig("git-p4.detectCopies")
2498 if detectCopies.lower() == "false" or detectCopies == "":
2500 elif detectCopies.lower() == "true":
2501 self.diffOpts += " -C"
2503 self.diffOpts += " -C%s" % detectCopies
2505 if gitConfigBool("git-p4.detectCopiesHarder"):
2506 self.diffOpts += " --find-copies-harder"
2508 num_shelves = len(self.update_shelve)
2509 if num_shelves > 0 and num_shelves != len(commits):
2510 sys.exit("number of commits (%d) must match number of shelved changelist (%d)" %
2511 (len(commits), num_shelves))
2513 if not self.no_verify:
2515 if not run_git_hook("p4-pre-submit"):
2516 print("\nThe p4-pre-submit hook failed, aborting the submit.\n\nYou can skip " \
2517 "this pre-submission check by adding\nthe command line option '--no-verify', " \
2518 "however,\nthis will also skip the p4-changelist hook as well.")
2520 except Exception as e:
2521 print("\nThe p4-pre-submit hook failed, aborting the submit.\n\nThe hook failed "\
2522 "with the error '{0}'".format(e.message) )
2526 # Apply the commits, one at a time. On failure, ask if should
2527 # continue to try the rest of the patches, or quit.
2530 print("Would apply")
2532 last = len(commits) - 1
2533 for i, commit in enumerate(commits):
2535 print(" ", read_pipe(["git", "show", "-s",
2536 "--format=format:%h %s", commit]))
2539 ok = self.applyCommit(commit)
2541 applied.append(commit)
2542 if self.prepare_p4_only:
2544 print("Processing only the first commit due to option" \
2545 " --prepare-p4-only")
2549 # prompt for what to do, or use the option/variable
2550 if self.conflict_behavior == "ask":
2551 print("What do you want to do?")
2552 response = prompt("[s]kip this commit but apply the rest, or [q]uit? ")
2553 elif self.conflict_behavior == "skip":
2555 elif self.conflict_behavior == "quit":
2558 die("Unknown conflict_behavior '%s'" %
2559 self.conflict_behavior)
2562 print("Skipping this commit, but applying the rest")
2567 chdir(self.oldWorkingDirectory)
2568 shelved_applied = "shelved" if self.shelve else "applied"
2571 elif self.prepare_p4_only:
2573 elif len(commits) == len(applied):
2574 print("All commits {0}!".format(shelved_applied))
2578 sync.branch = self.branch
2579 if self.disable_p4sync:
2580 sync.sync_origin_only()
2584 if not self.disable_rebase:
2589 if len(applied) == 0:
2590 print("No commits {0}.".format(shelved_applied))
2592 print("{0} only the commits marked with '*':".format(shelved_applied.capitalize()))
2598 print(star, read_pipe(["git", "show", "-s",
2599 "--format=format:%h %s", c]))
2600 print("You will have to do 'git p4 sync' and rebase.")
2602 if gitConfigBool("git-p4.exportLabels"):
2603 self.exportLabels = True
2605 if self.exportLabels:
2606 p4Labels = getP4Labels(self.depotPath)
2607 gitTags = getGitTags()
2609 missingGitTags = gitTags - p4Labels
2610 self.exportGitTags(missingGitTags)
2612 # exit with error unless everything applied perfectly
2613 if len(commits) != len(applied):
2619 """Represent a p4 view ("p4 help views"), and map files in a
2620 repo according to the view."""
2622 def __init__(self, client_name):
2624 self.client_prefix = "//%s/" % client_name
2625 # cache results of "p4 where" to lookup client file locations
2626 self.client_spec_path_cache = {}
2628 def append(self, view_line):
2629 """Parse a view line, splitting it into depot and client
2630 sides. Append to self.mappings, preserving order. This
2631 is only needed for tag creation."""
2633 # Split the view line into exactly two words. P4 enforces
2634 # structure on these lines that simplifies this quite a bit.
2636 # Either or both words may be double-quoted.
2637 # Single quotes do not matter.
2638 # Double-quote marks cannot occur inside the words.
2639 # A + or - prefix is also inside the quotes.
2640 # There are no quotes unless they contain a space.
2641 # The line is already white-space stripped.
2642 # The two words are separated by a single space.
2644 if view_line[0] == '"':
2645 # First word is double quoted. Find its end.
2646 close_quote_index = view_line.find('"', 1)
2647 if close_quote_index <= 0:
2648 die("No first-word closing quote found: %s" % view_line)
2649 depot_side = view_line[1:close_quote_index]
2650 # skip closing quote and space
2651 rhs_index = close_quote_index + 1 + 1
2653 space_index = view_line.find(" ")
2654 if space_index <= 0:
2655 die("No word-splitting space found: %s" % view_line)
2656 depot_side = view_line[0:space_index]
2657 rhs_index = space_index + 1
2659 # prefix + means overlay on previous mapping
2660 if depot_side.startswith("+"):
2661 depot_side = depot_side[1:]
2663 # prefix - means exclude this path, leave out of mappings
2665 if depot_side.startswith("-"):
2667 depot_side = depot_side[1:]
2670 self.mappings.append(depot_side)
2672 def convert_client_path(self, clientFile):
2673 # chop off //client/ part to make it relative
2674 if not decode_path(clientFile).startswith(self.client_prefix):
2675 die("No prefix '%s' on clientFile '%s'" %
2676 (self.client_prefix, clientFile))
2677 return clientFile[len(self.client_prefix):]
2679 def update_client_spec_path_cache(self, files):
2680 """ Caching file paths by "p4 where" batch query """
2682 # List depot file paths exclude that already cached
2683 fileArgs = [f['path'] for f in files if decode_path(f['path']) not in self.client_spec_path_cache]
2685 if len(fileArgs) == 0:
2686 return # All files in cache
2688 where_result = p4CmdList(["-x", "-", "where"], stdin=fileArgs)
2689 for res in where_result:
2690 if "code" in res and res["code"] == "error":
2691 # assume error is "... file(s) not in client view"
2693 if "clientFile" not in res:
2694 die("No clientFile in 'p4 where' output")
2696 # it will list all of them, but only one not unmap-ped
2698 depot_path = decode_path(res['depotFile'])
2699 if gitConfigBool("core.ignorecase"):
2700 depot_path = depot_path.lower()
2701 self.client_spec_path_cache[depot_path] = self.convert_client_path(res["clientFile"])
2703 # not found files or unmap files set to ""
2704 for depotFile in fileArgs:
2705 depotFile = decode_path(depotFile)
2706 if gitConfigBool("core.ignorecase"):
2707 depotFile = depotFile.lower()
2708 if depotFile not in self.client_spec_path_cache:
2709 self.client_spec_path_cache[depotFile] = b''
2711 def map_in_client(self, depot_path):
2712 """Return the relative location in the client where this
2713 depot file should live. Returns "" if the file should
2714 not be mapped in the client."""
2716 if gitConfigBool("core.ignorecase"):
2717 depot_path = depot_path.lower()
2719 if depot_path in self.client_spec_path_cache:
2720 return self.client_spec_path_cache[depot_path]
2722 die( "Error: %s is not found in client spec path" % depot_path )
2725 def cloneExcludeCallback(option, opt_str, value, parser):
2726 # prepend "/" because the first "/" was consumed as part of the option itself.
2727 # ("-//depot/A/..." becomes "/depot/A/..." after option parsing)
2728 parser.values.cloneExclude += ["/" + re.sub(r"\.\.\.$", "", value)]
2730 class P4Sync(Command, P4UserMap):
2733 Command.__init__(self)
2734 P4UserMap.__init__(self)
2736 optparse.make_option("--branch", dest="branch"),
2737 optparse.make_option("--detect-branches", dest="detectBranches", action="store_true"),
2738 optparse.make_option("--changesfile", dest="changesFile"),
2739 optparse.make_option("--silent", dest="silent", action="store_true"),
2740 optparse.make_option("--detect-labels", dest="detectLabels", action="store_true"),
2741 optparse.make_option("--import-labels", dest="importLabels", action="store_true"),
2742 optparse.make_option("--import-local", dest="importIntoRemotes", action="store_false",
2743 help="Import into refs/heads/ , not refs/remotes"),
2744 optparse.make_option("--max-changes", dest="maxChanges",
2745 help="Maximum number of changes to import"),
2746 optparse.make_option("--changes-block-size", dest="changes_block_size", type="int",
2747 help="Internal block size to use when iteratively calling p4 changes"),
2748 optparse.make_option("--keep-path", dest="keepRepoPath", action='store_true',
2749 help="Keep entire BRANCH/DIR/SUBDIR prefix during import"),
2750 optparse.make_option("--use-client-spec", dest="useClientSpec", action='store_true',
2751 help="Only sync files that are included in the Perforce Client Spec"),
2752 optparse.make_option("-/", dest="cloneExclude",
2753 action="callback", callback=cloneExcludeCallback, type="string",
2754 help="exclude depot path"),
2756 self.description = """Imports from Perforce into a git repository.\n
2758 //depot/my/project/ -- to import the current head
2759 //depot/my/project/@all -- to import everything
2760 //depot/my/project/@1,6 -- to import only from revision 1 to 6
2762 (a ... is not needed in the path p4 specification, it's added implicitly)"""
2764 self.usage += " //depot/path[@revRange]"
2766 self.createdBranches = set()
2767 self.committedChanges = set()
2769 self.detectBranches = False
2770 self.detectLabels = False
2771 self.importLabels = False
2772 self.changesFile = ""
2773 self.syncWithOrigin = True
2774 self.importIntoRemotes = True
2775 self.maxChanges = ""
2776 self.changes_block_size = None
2777 self.keepRepoPath = False
2778 self.depotPaths = None
2779 self.p4BranchesInGit = []
2780 self.cloneExclude = []
2781 self.useClientSpec = False
2782 self.useClientSpec_from_options = False
2783 self.clientSpecDirs = None
2784 self.tempBranches = []
2785 self.tempBranchLocation = "refs/git-p4-tmp"
2786 self.largeFileSystem = None
2787 self.suppress_meta_comment = False
2789 if gitConfig('git-p4.largeFileSystem'):
2790 largeFileSystemConstructor = globals()[gitConfig('git-p4.largeFileSystem')]
2791 self.largeFileSystem = largeFileSystemConstructor(
2792 lambda git_mode, relPath, contents: self.writeToGitStream(git_mode, relPath, contents)
2795 if gitConfig("git-p4.syncFromOrigin") == "false":
2796 self.syncWithOrigin = False
2798 self.depotPaths = []
2799 self.changeRange = ""
2800 self.previousDepotPaths = []
2801 self.hasOrigin = False
2803 # map from branch depot path to parent branch
2804 self.knownBranches = {}
2805 self.initialParents = {}
2807 self.tz = b"%+03d%02d" % (- time.timezone / 3600, ((- time.timezone % 3600) / 60))
2810 # Force a checkpoint in fast-import and wait for it to finish
2811 def checkpoint(self):
2812 self.gitStream.write("checkpoint\n\n")
2813 self.gitStream.write("progress checkpoint\n\n")
2814 self.gitStream.flush()
2815 out = self.gitOutput.readline()
2817 print("checkpoint finished: " + out)
2819 def isPathWanted(self, path):
2820 for p in self.cloneExclude:
2822 if p4PathStartsWith(path, p):
2824 # "-//depot/file1" without a trailing "/" should only exclude "file1", but not "file111" or "file1_dir/file2"
2825 elif path.lower() == p.lower():
2827 for p in self.depotPaths:
2828 if p4PathStartsWith(path, decode_path(p)):
2832 def extractFilesFromCommit(self, commit, shelved=False, shelved_cl = 0):
2835 while "depotFile%s" % fnum in commit:
2836 path = commit["depotFile%s" % fnum]
2837 found = self.isPathWanted(decode_path(path))
2844 file["rev"] = commit["rev%s" % fnum]
2845 file["action"] = commit["action%s" % fnum]
2846 file["type"] = commit["type%s" % fnum]
2848 file["shelved_cl"] = int(shelved_cl)
2853 def extractJobsFromCommit(self, commit):
2856 while "job%s" % jnum in commit:
2857 job = commit["job%s" % jnum]
2862 def stripRepoPath(self, path, prefixes):
2863 """When streaming files, this is called to map a p4 depot path
2864 to where it should go in git. The prefixes are either
2865 self.depotPaths, or self.branchPrefixes in the case of
2866 branch detection."""
2868 if self.useClientSpec:
2869 # branch detection moves files up a level (the branch name)
2870 # from what client spec interpretation gives
2871 path = decode_path(self.clientSpecDirs.map_in_client(path))
2872 if self.detectBranches:
2873 for b in self.knownBranches:
2874 if p4PathStartsWith(path, b + "/"):
2875 path = path[len(b)+1:]
2877 elif self.keepRepoPath:
2878 # Preserve everything in relative path name except leading
2879 # //depot/; just look at first prefix as they all should
2880 # be in the same depot.
2881 depot = re.sub("^(//[^/]+/).*", r'\1', prefixes[0])
2882 if p4PathStartsWith(path, depot):
2883 path = path[len(depot):]
2887 if p4PathStartsWith(path, p):
2888 path = path[len(p):]
2891 path = wildcard_decode(path)
2894 def splitFilesIntoBranches(self, commit):
2895 """Look at each depotFile in the commit to figure out to what
2896 branch it belongs."""
2898 if self.clientSpecDirs:
2899 files = self.extractFilesFromCommit(commit)
2900 self.clientSpecDirs.update_client_spec_path_cache(files)
2904 while "depotFile%s" % fnum in commit:
2905 raw_path = commit["depotFile%s" % fnum]
2906 path = decode_path(raw_path)
2907 found = self.isPathWanted(path)
2913 file["path"] = raw_path
2914 file["rev"] = commit["rev%s" % fnum]
2915 file["action"] = commit["action%s" % fnum]
2916 file["type"] = commit["type%s" % fnum]
2919 # start with the full relative path where this file would
2921 if self.useClientSpec:
2922 relPath = decode_path(self.clientSpecDirs.map_in_client(path))
2924 relPath = self.stripRepoPath(path, self.depotPaths)
2926 for branch in self.knownBranches.keys():
2927 # add a trailing slash so that a commit into qt/4.2foo
2928 # doesn't end up in qt/4.2, e.g.
2929 if p4PathStartsWith(relPath, branch + "/"):
2930 if branch not in branches:
2931 branches[branch] = []
2932 branches[branch].append(file)
2937 def writeToGitStream(self, gitMode, relPath, contents):
2938 self.gitStream.write(encode_text_stream(u'M {} inline {}\n'.format(gitMode, relPath)))
2939 self.gitStream.write('data %d\n' % sum(len(d) for d in contents))
2941 self.gitStream.write(d)
2942 self.gitStream.write('\n')
2944 def encodeWithUTF8(self, path):
2946 path.decode('ascii')
2949 if gitConfig('git-p4.pathEncoding'):
2950 encoding = gitConfig('git-p4.pathEncoding')
2951 path = path.decode(encoding, 'replace').encode('utf8', 'replace')
2953 print('Path with non-ASCII characters detected. Used %s to encode: %s ' % (encoding, path))
2956 # output one file from the P4 stream
2957 # - helper for streamP4Files
2959 def streamOneP4File(self, file, contents):
2960 file_path = file['depotFile']
2961 relPath = self.stripRepoPath(decode_path(file_path), self.branchPrefixes)
2964 if 'fileSize' in self.stream_file:
2965 size = int(self.stream_file['fileSize'])
2967 size = 0 # deleted files don't get a fileSize apparently
2968 sys.stdout.write('\r%s --> %s (%i MB)\n' % (file_path, relPath, size/1024/1024))
2971 (type_base, type_mods) = split_p4_type(file["type"])
2974 if "x" in type_mods:
2976 if type_base == "symlink":
2978 # p4 print on a symlink sometimes contains "target\n";
2979 # if it does, remove the newline
2980 data = ''.join(decode_text_stream(c) for c in contents)
2982 # Some version of p4 allowed creating a symlink that pointed
2983 # to nothing. This causes p4 errors when checking out such
2984 # a change, and errors here too. Work around it by ignoring
2985 # the bad symlink; hopefully a future change fixes it.
2986 print("\nIgnoring empty symlink in %s" % file_path)
2988 elif data[-1] == '\n':
2989 contents = [data[:-1]]
2993 if type_base == "utf16":
2994 # p4 delivers different text in the python output to -G
2995 # than it does when using "print -o", or normal p4 client
2996 # operations. utf16 is converted to ascii or utf8, perhaps.
2997 # But ascii text saved as -t utf16 is completely mangled.
2998 # Invoke print -o to get the real contents.
3000 # On windows, the newlines will always be mangled by print, so put
3001 # them back too. This is not needed to the cygwin windows version,
3002 # just the native "NT" type.
3005 text = p4_read_pipe(['print', '-q', '-o', '-', '%s@%s' % (decode_path(file['depotFile']), file['change'])], raw=True)
3006 except Exception as e:
3007 if 'Translation of file content failed' in str(e):
3008 type_base = 'binary'
3012 if p4_version_string().find('/NT') >= 0:
3013 text = text.replace(b'\r\n', b'\n')
3016 if type_base == "apple":
3017 # Apple filetype files will be streamed as a concatenation of
3018 # its appledouble header and the contents. This is useless
3019 # on both macs and non-macs. If using "print -q -o xx", it
3020 # will create "xx" with the data, and "%xx" with the header.
3021 # This is also not very useful.
3023 # Ideally, someday, this script can learn how to generate
3024 # appledouble files directly and import those to git, but
3025 # non-mac machines can never find a use for apple filetype.
3026 print("\nIgnoring apple filetype file %s" % file['depotFile'])
3029 # Note that we do not try to de-mangle keywords on utf16 files,
3030 # even though in theory somebody may want that.
3031 pattern = p4_keywords_regexp_for_type(type_base, type_mods)
3033 regexp = re.compile(pattern, re.VERBOSE)
3034 text = ''.join(decode_text_stream(c) for c in contents)
3035 text = regexp.sub(r'$\1$', text)
3036 contents = [ encode_text_stream(text) ]
3038 if self.largeFileSystem:
3039 (git_mode, contents) = self.largeFileSystem.processContent(git_mode, relPath, contents)
3041 self.writeToGitStream(git_mode, relPath, contents)
3043 def streamOneP4Deletion(self, file):
3044 relPath = self.stripRepoPath(decode_path(file['path']), self.branchPrefixes)
3046 sys.stdout.write("delete %s\n" % relPath)
3048 self.gitStream.write(encode_text_stream(u'D {}\n'.format(relPath)))
3050 if self.largeFileSystem and self.largeFileSystem.isLargeFile(relPath):
3051 self.largeFileSystem.removeLargeFile(relPath)
3053 # handle another chunk of streaming data
3054 def streamP4FilesCb(self, marshalled):
3056 # catch p4 errors and complain
3058 if "code" in marshalled:
3059 if marshalled["code"] == "error":
3060 if "data" in marshalled:
3061 err = marshalled["data"].rstrip()
3063 if not err and 'fileSize' in self.stream_file:
3064 required_bytes = int((4 * int(self.stream_file["fileSize"])) - calcDiskFree())
3065 if required_bytes > 0:
3066 err = 'Not enough space left on %s! Free at least %i MB.' % (
3067 os.getcwd(), required_bytes/1024/1024
3072 if self.stream_have_file_info:
3073 if "depotFile" in self.stream_file:
3074 f = self.stream_file["depotFile"]
3075 # force a failure in fast-import, else an empty
3076 # commit will be made
3077 self.gitStream.write("\n")
3078 self.gitStream.write("die-now\n")
3079 self.gitStream.close()
3080 # ignore errors, but make sure it exits first
3081 self.importProcess.wait()
3083 die("Error from p4 print for %s: %s" % (f, err))
3085 die("Error from p4 print: %s" % err)
3087 if 'depotFile' in marshalled and self.stream_have_file_info:
3088 # start of a new file - output the old one first
3089 self.streamOneP4File(self.stream_file, self.stream_contents)
3090 self.stream_file = {}
3091 self.stream_contents = []
3092 self.stream_have_file_info = False
3094 # pick up the new file information... for the
3095 # 'data' field we need to append to our array
3096 for k in marshalled.keys():
3098 if 'streamContentSize' not in self.stream_file:
3099 self.stream_file['streamContentSize'] = 0
3100 self.stream_file['streamContentSize'] += len(marshalled['data'])
3101 self.stream_contents.append(marshalled['data'])
3103 self.stream_file[k] = marshalled[k]
3106 'streamContentSize' in self.stream_file and
3107 'fileSize' in self.stream_file and
3108 'depotFile' in self.stream_file):
3109 size = int(self.stream_file["fileSize"])
3111 progress = 100*self.stream_file['streamContentSize']/size
3112 sys.stdout.write('\r%s %d%% (%i MB)' % (self.stream_file['depotFile'], progress, int(size/1024/1024)))
3115 self.stream_have_file_info = True
3117 # Stream directly from "p4 files" into "git fast-import"
3118 def streamP4Files(self, files):
3124 filesForCommit.append(f)
3125 if f['action'] in self.delete_actions:
3126 filesToDelete.append(f)
3128 filesToRead.append(f)
3131 for f in filesToDelete:
3132 self.streamOneP4Deletion(f)
3134 if len(filesToRead) > 0:
3135 self.stream_file = {}
3136 self.stream_contents = []
3137 self.stream_have_file_info = False
3139 # curry self argument
3140 def streamP4FilesCbSelf(entry):
3141 self.streamP4FilesCb(entry)
3144 for f in filesToRead:
3145 if 'shelved_cl' in f:
3146 # Handle shelved CLs using the "p4 print file@=N" syntax to print
3148 fileArg = f['path'] + encode_text_stream('@={}'.format(f['shelved_cl']))
3150 fileArg = f['path'] + encode_text_stream('#{}'.format(f['rev']))
3152 fileArgs.append(fileArg)
3154 p4CmdList(["-x", "-", "print"],
3156 cb=streamP4FilesCbSelf)
3159 if 'depotFile' in self.stream_file:
3160 self.streamOneP4File(self.stream_file, self.stream_contents)
3162 def make_email(self, userid):
3163 if userid in self.users:
3164 return self.users[userid]
3166 return b"%s <a@b>" % userid
3168 def streamTag(self, gitStream, labelName, labelDetails, commit, epoch):
3169 """ Stream a p4 tag.
3170 commit is either a git commit, or a fast-import mark, ":<p4commit>"
3174 print("writing tag %s for commit %s" % (labelName, commit))
3175 gitStream.write("tag %s\n" % labelName)
3176 gitStream.write("from %s\n" % commit)
3178 if 'Owner' in labelDetails:
3179 owner = labelDetails["Owner"]
3183 # Try to use the owner of the p4 label, or failing that,
3184 # the current p4 user id.
3186 email = self.make_email(owner)
3188 email = self.make_email(self.p4UserId())
3189 tagger = b"%s %s %s" % (email, epoch, self.tz)
3191 gitStream.write(b"tagger %s\n" % tagger)
3193 print("labelDetails=",labelDetails)
3194 if 'Description' in labelDetails:
3195 description = labelDetails['Description']
3197 description = 'Label from git p4'
3199 gitStream.write("data %d\n" % len(description))
3200 gitStream.write(description)
3201 gitStream.write("\n")
3203 def inClientSpec(self, path):
3204 if not self.clientSpecDirs:
3206 inClientSpec = self.clientSpecDirs.map_in_client(path)
3207 if not inClientSpec and self.verbose:
3208 print('Ignoring file outside of client spec: {0}'.format(path))
3211 def hasBranchPrefix(self, path):
3212 if not self.branchPrefixes:
3214 hasPrefix = [p for p in self.branchPrefixes
3215 if p4PathStartsWith(path, p)]
3216 if not hasPrefix and self.verbose:
3217 print('Ignoring file outside of prefix: {0}'.format(path))
3220 def findShadowedFiles(self, files, change):
3221 # Perforce allows you commit files and directories with the same name,
3222 # so you could have files //depot/foo and //depot/foo/bar both checked
3223 # in. A p4 sync of a repository in this state fails. Deleting one of
3224 # the files recovers the repository.
3226 # Git will not allow the broken state to exist and only the most recent
3227 # of the conflicting names is left in the repository. When one of the
3228 # conflicting files is deleted we need to re-add the other one to make
3229 # sure the git repository recovers in the same way as perforce.
3230 deleted = [f for f in files if f['action'] in self.delete_actions]
3233 path = decode_path(f['path'])
3234 to_check.add(path + '/...')
3236 path = path.rsplit("/", 1)[0]
3237 if path == "/" or path in to_check:
3240 to_check = ['%s@%s' % (wildcard_encode(p), change) for p in to_check
3241 if self.hasBranchPrefix(p)]
3243 stat_result = p4CmdList(["-x", "-", "fstat", "-T",
3244 "depotFile,headAction,headRev,headType"], stdin=to_check)
3245 for record in stat_result:
3246 if record['code'] != 'stat':
3248 if record['headAction'] in self.delete_actions:
3252 'path': record['depotFile'],
3253 'rev': record['headRev'],
3254 'type': record['headType']})
3256 def commit(self, details, files, branch, parent = "", allow_empty=False):
3257 epoch = details["time"]
3258 author = details["user"]
3259 jobs = self.extractJobsFromCommit(details)
3262 print('commit into {0}'.format(branch))
3264 files = [f for f in files
3265 if self.hasBranchPrefix(decode_path(f['path']))]
3266 self.findShadowedFiles(files, details['change'])
3268 if self.clientSpecDirs:
3269 self.clientSpecDirs.update_client_spec_path_cache(files)
3271 files = [f for f in files if self.inClientSpec(decode_path(f['path']))]
3273 if gitConfigBool('git-p4.keepEmptyCommits'):
3276 if not files and not allow_empty:
3277 print('Ignoring revision {0} as it would produce an empty commit.'
3278 .format(details['change']))
3281 self.gitStream.write("commit %s\n" % branch)
3282 self.gitStream.write("mark :%s\n" % details["change"])
3283 self.committedChanges.add(int(details["change"]))
3284 if author not in self.users:
3285 self.getUserMapFromPerforceServer()
3286 committer = b"%s %s %s" % (self.make_email(author), epoch, self.tz)
3288 self.gitStream.write(b"committer %s\n" % committer)
3290 self.gitStream.write("data <<EOT\n")
3291 self.gitStream.write(details["desc"])
3293 self.gitStream.write("\nJobs: %s" % (' '.join(jobs)))
3295 if not self.suppress_meta_comment:
3296 self.gitStream.write("\n[git-p4: depot-paths = \"%s\": change = %s" %
3297 (','.join(self.branchPrefixes), details["change"]))
3298 if len(details['options']) > 0:
3299 self.gitStream.write(": options = %s" % details['options'])
3300 self.gitStream.write("]\n")
3302 self.gitStream.write("EOT\n\n")
3306 print("parent %s" % parent)
3307 self.gitStream.write("from %s\n" % parent)
3309 self.streamP4Files(files)
3310 self.gitStream.write("\n")
3312 change = int(details["change"])
3314 if change in self.labels:
3315 label = self.labels[change]
3316 labelDetails = label[0]
3317 labelRevisions = label[1]
3319 print("Change %s is labelled %s" % (change, labelDetails))
3321 files = p4CmdList(["files"] + ["%s...@%s" % (p, change)
3322 for p in self.branchPrefixes])
3324 if len(files) == len(labelRevisions):
3328 if info["action"] in self.delete_actions:
3330 cleanedFiles[info["depotFile"]] = info["rev"]
3332 if cleanedFiles == labelRevisions:
3333 self.streamTag(self.gitStream, 'tag_%s' % labelDetails['label'], labelDetails, branch, epoch)
3337 print("Tag %s does not match with change %s: files do not match."
3338 % (labelDetails["label"], change))
3342 print("Tag %s does not match with change %s: file count is different."
3343 % (labelDetails["label"], change))
3345 # Build a dictionary of changelists and labels, for "detect-labels" option.
3346 def getLabels(self):
3349 l = p4CmdList(["labels"] + ["%s..." % p for p in self.depotPaths])
3350 if len(l) > 0 and not self.silent:
3351 print("Finding files belonging to labels in %s" % self.depotPaths)
3354 label = output["label"]
3358 print("Querying files for label %s" % label)
3359 for file in p4CmdList(["files"] +
3360 ["%s...@%s" % (p, label)
3361 for p in self.depotPaths]):
3362 revisions[file["depotFile"]] = file["rev"]
3363 change = int(file["change"])
3364 if change > newestChange:
3365 newestChange = change
3367 self.labels[newestChange] = [output, revisions]
3370 print("Label changes: %s" % self.labels.keys())
3372 # Import p4 labels as git tags. A direct mapping does not
3373 # exist, so assume that if all the files are at the same revision
3374 # then we can use that, or it's something more complicated we should
3376 def importP4Labels(self, stream, p4Labels):
3378 print("import p4 labels: " + ' '.join(p4Labels))
3380 ignoredP4Labels = gitConfigList("git-p4.ignoredP4Labels")
3381 validLabelRegexp = gitConfig("git-p4.labelImportRegexp")
3382 if len(validLabelRegexp) == 0:
3383 validLabelRegexp = defaultLabelRegexp
3384 m = re.compile(validLabelRegexp)
3386 for name in p4Labels:
3389 if not m.match(name):
3391 print("label %s does not match regexp %s" % (name,validLabelRegexp))
3394 if name in ignoredP4Labels:
3397 labelDetails = p4CmdList(['label', "-o", name])[0]
3399 # get the most recent changelist for each file in this label
3400 change = p4Cmd(["changes", "-m", "1"] + ["%s...@%s" % (p, name)
3401 for p in self.depotPaths])
3403 if 'change' in change:
3404 # find the corresponding git commit; take the oldest commit
3405 changelist = int(change['change'])
3406 if changelist in self.committedChanges:
3407 gitCommit = ":%d" % changelist # use a fast-import mark
3410 gitCommit = read_pipe(["git", "rev-list", "--max-count=1",
3411 "--reverse", ":/\[git-p4:.*change = %d\]" % changelist], ignore_error=True)
3412 if len(gitCommit) == 0:
3413 print("importing label %s: could not find git commit for changelist %d" % (name, changelist))
3416 gitCommit = gitCommit.strip()
3419 # Convert from p4 time format
3421 tmwhen = time.strptime(labelDetails['Update'], "%Y/%m/%d %H:%M:%S")
3423 print("Could not convert label time %s" % labelDetails['Update'])
3426 when = b"%i" % int(time.mktime(tmwhen))
3427 self.streamTag(stream, name, labelDetails, gitCommit, when)
3429 print("p4 label %s mapped to git commit %s" % (name, gitCommit))
3432 print("Label %s has no changelists - possibly deleted?" % name)
3435 # We can't import this label; don't try again as it will get very
3436 # expensive repeatedly fetching all the files for labels that will
3437 # never be imported. If the label is moved in the future, the
3438 # ignore will need to be removed manually.
3439 system(["git", "config", "--add", "git-p4.ignoredP4Labels", name])
3441 def guessProjectName(self):
3442 for p in self.depotPaths:
3445 p = p[p.strip().rfind("/") + 1:]
3446 if not p.endswith("/"):
3450 def getBranchMapping(self):
3451 lostAndFoundBranches = set()
3453 user = gitConfig("git-p4.branchUser")
3455 command = "branches -u %s" % user
3457 command = "branches"
3459 for info in p4CmdList(command):
3460 details = p4Cmd(["branch", "-o", info["branch"]])
3462 while "View%s" % viewIdx in details:
3463 paths = details["View%s" % viewIdx].split(" ")
3464 viewIdx = viewIdx + 1
3465 # require standard //depot/foo/... //depot/bar/... mapping
3466 if len(paths) != 2 or not paths[0].endswith("/...") or not paths[1].endswith("/..."):
3469 destination = paths[1]
3471 if p4PathStartsWith(source, self.depotPaths[0]) and p4PathStartsWith(destination, self.depotPaths[0]):
3472 source = source[len(self.depotPaths[0]):-4]
3473 destination = destination[len(self.depotPaths[0]):-4]
3475 if destination in self.knownBranches:
3477 print("p4 branch %s defines a mapping from %s to %s" % (info["branch"], source, destination))
3478 print("but there exists another mapping from %s to %s already!" % (self.knownBranches[destination], destination))
3481 self.knownBranches[destination] = source
3483 lostAndFoundBranches.discard(destination)
3485 if source not in self.knownBranches:
3486 lostAndFoundBranches.add(source)
3488 # Perforce does not strictly require branches to be defined, so we also
3489 # check git config for a branch list.
3491 # Example of branch definition in git config file:
3493 # branchList=main:branchA
3494 # branchList=main:branchB
3495 # branchList=branchA:branchC
3496 configBranches = gitConfigList("git-p4.branchList")
3497 for branch in configBranches:
3499 (source, destination) = branch.split(":")
3500 self.knownBranches[destination] = source
3502 lostAndFoundBranches.discard(destination)
3504 if source not in self.knownBranches:
3505 lostAndFoundBranches.add(source)
3508 for branch in lostAndFoundBranches:
3509 self.knownBranches[branch] = branch
3511 def getBranchMappingFromGitBranches(self):
3512 branches = p4BranchesInGit(self.importIntoRemotes)
3513 for branch in branches.keys():
3514 if branch == "master":
3517 branch = branch[len(self.projectName):]
3518 self.knownBranches[branch] = branch
3520 def updateOptionDict(self, d):
3522 if self.keepRepoPath:
3523 option_keys['keepRepoPath'] = 1
3525 d["options"] = ' '.join(sorted(option_keys.keys()))
3527 def readOptions(self, d):
3528 self.keepRepoPath = ('options' in d
3529 and ('keepRepoPath' in d['options']))
3531 def gitRefForBranch(self, branch):
3532 if branch == "main":
3533 return self.refPrefix + "master"
3535 if len(branch) <= 0:
3538 return self.refPrefix + self.projectName + branch
3540 def gitCommitByP4Change(self, ref, change):
3542 print("looking in ref " + ref + " for change %s using bisect..." % change)
3545 latestCommit = parseRevision(ref)
3549 print("trying: earliest %s latest %s" % (earliestCommit, latestCommit))
3550 next = read_pipe("git rev-list --bisect %s %s" % (latestCommit, earliestCommit)).strip()
3555 log = extractLogMessageFromGitCommit(next)
3556 settings = extractSettingsGitLog(log)
3557 currentChange = int(settings['change'])
3559 print("current change %s" % currentChange)
3561 if currentChange == change:
3563 print("found %s" % next)
3566 if currentChange < change:
3567 earliestCommit = "^%s" % next
3569 if next == latestCommit:
3570 die("Infinite loop while looking in ref %s for change %s. Check your branch mappings" % (ref, change))
3571 latestCommit = "%s^@" % next
3575 def importNewBranch(self, branch, maxChange):
3576 # make fast-import flush all changes to disk and update the refs using the checkpoint
3577 # command so that we can try to find the branch parent in the git history
3578 self.gitStream.write("checkpoint\n\n");
3579 self.gitStream.flush();
3580 branchPrefix = self.depotPaths[0] + branch + "/"
3581 range = "@1,%s" % maxChange
3582 #print "prefix" + branchPrefix
3583 changes = p4ChangesForPaths([branchPrefix], range, self.changes_block_size)
3584 if len(changes) <= 0:
3586 firstChange = changes[0]
3587 #print "first change in branch: %s" % firstChange
3588 sourceBranch = self.knownBranches[branch]
3589 sourceDepotPath = self.depotPaths[0] + sourceBranch
3590 sourceRef = self.gitRefForBranch(sourceBranch)
3591 #print "source " + sourceBranch
3593 branchParentChange = int(p4Cmd(["changes", "-m", "1", "%s...@1,%s" % (sourceDepotPath, firstChange)])["change"])
3594 #print "branch parent: %s" % branchParentChange
3595 gitParent = self.gitCommitByP4Change(sourceRef, branchParentChange)
3596 if len(gitParent) > 0:
3597 self.initialParents[self.gitRefForBranch(branch)] = gitParent
3598 #print "parent git commit: %s" % gitParent
3600 self.importChanges(changes)
3603 def searchParent(self, parent, branch, target):
3605 for blob in read_pipe_lines(["git", "rev-list", "--reverse",
3606 "--no-merges", parent]):
3608 if len(read_pipe(["git", "diff-tree", blob, target])) == 0:
3611 print("Found parent of %s in commit %s" % (branch, blob))
3618 def importChanges(self, changes, origin_revision=0):
3620 for change in changes:
3621 description = p4_describe(change)
3622 self.updateOptionDict(description)
3625 sys.stdout.write("\rImporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
3630 if self.detectBranches:
3631 branches = self.splitFilesIntoBranches(description)
3632 for branch in branches.keys():
3634 branchPrefix = self.depotPaths[0] + branch + "/"
3635 self.branchPrefixes = [ branchPrefix ]
3639 filesForCommit = branches[branch]
3642 print("branch is %s" % branch)
3644 self.updatedBranches.add(branch)
3646 if branch not in self.createdBranches:
3647 self.createdBranches.add(branch)
3648 parent = self.knownBranches[branch]
3649 if parent == branch:
3652 fullBranch = self.projectName + branch
3653 if fullBranch not in self.p4BranchesInGit:
3655 print("\n Importing new branch %s" % fullBranch);
3656 if self.importNewBranch(branch, change - 1):
3658 self.p4BranchesInGit.append(fullBranch)
3660 print("\n Resuming with change %s" % change);
3663 print("parent determined through known branches: %s" % parent)
3665 branch = self.gitRefForBranch(branch)
3666 parent = self.gitRefForBranch(parent)
3669 print("looking for initial parent for %s; current parent is %s" % (branch, parent))
3671 if len(parent) == 0 and branch in self.initialParents:
3672 parent = self.initialParents[branch]
3673 del self.initialParents[branch]
3677 tempBranch = "%s/%d" % (self.tempBranchLocation, change)
3679 print("Creating temporary branch: " + tempBranch)
3680 self.commit(description, filesForCommit, tempBranch)
3681 self.tempBranches.append(tempBranch)
3683 blob = self.searchParent(parent, branch, tempBranch)
3685 self.commit(description, filesForCommit, branch, blob)
3688 print("Parent of %s not found. Committing into head of %s" % (branch, parent))
3689 self.commit(description, filesForCommit, branch, parent)
3691 files = self.extractFilesFromCommit(description)
3692 self.commit(description, files, self.branch,
3694 # only needed once, to connect to the previous commit
3695 self.initialParent = ""
3697 print(self.gitError.read())
3700 def sync_origin_only(self):
3701 if self.syncWithOrigin:
3702 self.hasOrigin = originP4BranchesExist()
3705 print('Syncing with origin first, using "git fetch origin"')
3706 system("git fetch origin")
3708 def importHeadRevision(self, revision):
3709 print("Doing initial import of %s from revision %s into %s" % (' '.join(self.depotPaths), revision, self.branch))
3712 details["user"] = b"git perforce import user"
3713 details["desc"] = ("Initial import of %s from the state at revision %s\n"
3714 % (' '.join(self.depotPaths), revision))
3715 details["change"] = revision
3719 fileArgs = ["%s...%s" % (p,revision) for p in self.depotPaths]
3721 for info in p4CmdList(["files"] + fileArgs):
3723 if 'code' in info and info['code'] == 'error':
3724 sys.stderr.write("p4 returned an error: %s\n"
3726 if info['data'].find("must refer to client") >= 0:
3727 sys.stderr.write("This particular p4 error is misleading.\n")
3728 sys.stderr.write("Perhaps the depot path was misspelled.\n");
3729 sys.stderr.write("Depot path: %s\n" % " ".join(self.depotPaths))
3731 if 'p4ExitCode' in info:
3732 sys.stderr.write("p4 exitcode: %s\n" % info['p4ExitCode'])
3736 change = int(info["change"])
3737 if change > newestRevision:
3738 newestRevision = change
3740 if info["action"] in self.delete_actions:
3741 # don't increase the file cnt, otherwise details["depotFile123"] will have gaps!
3742 #fileCnt = fileCnt + 1
3745 for prop in ["depotFile", "rev", "action", "type" ]:
3746 details["%s%s" % (prop, fileCnt)] = info[prop]
3748 fileCnt = fileCnt + 1
3750 details["change"] = newestRevision
3752 # Use time from top-most change so that all git p4 clones of
3753 # the same p4 repo have the same commit SHA1s.
3754 res = p4_describe(newestRevision)
3755 details["time"] = res["time"]
3757 self.updateOptionDict(details)
3759 self.commit(details, self.extractFilesFromCommit(details), self.branch)
3760 except IOError as err:
3761 print("IO error with git fast-import. Is your git version recent enough?")
3762 print("IO error details: {}".format(err))
3763 print(self.gitError.read())
3766 def importRevisions(self, args, branch_arg_given):
3769 if len(self.changesFile) > 0:
3770 with open(self.changesFile) as f:
3771 output = f.readlines()
3774 changeSet.add(int(line))
3776 for change in changeSet:
3777 changes.append(change)
3781 # catch "git p4 sync" with no new branches, in a repo that
3782 # does not have any existing p4 branches
3784 if not self.p4BranchesInGit:
3785 raise P4CommandException("No remote p4 branches. Perhaps you never did \"git p4 clone\" in here.")
3787 # The default branch is master, unless --branch is used to
3788 # specify something else. Make sure it exists, or complain
3789 # nicely about how to use --branch.
3790 if not self.detectBranches:
3791 if not branch_exists(self.branch):
3792 if branch_arg_given:
3793 raise P4CommandException("Error: branch %s does not exist." % self.branch)
3795 raise P4CommandException("Error: no branch %s; perhaps specify one with --branch." %
3799 print("Getting p4 changes for %s...%s" % (', '.join(self.depotPaths),
3801 changes = p4ChangesForPaths(self.depotPaths, self.changeRange, self.changes_block_size)
3803 if len(self.maxChanges) > 0:
3804 changes = changes[:min(int(self.maxChanges), len(changes))]
3806 if len(changes) == 0:
3808 print("No changes to import!")
3810 if not self.silent and not self.detectBranches:
3811 print("Import destination: %s" % self.branch)
3813 self.updatedBranches = set()
3815 if not self.detectBranches:
3817 # start a new branch
3818 self.initialParent = ""
3820 # build on a previous revision
3821 self.initialParent = parseRevision(self.branch)
3823 self.importChanges(changes)
3827 if len(self.updatedBranches) > 0:
3828 sys.stdout.write("Updated branches: ")
3829 for b in self.updatedBranches:
3830 sys.stdout.write("%s " % b)
3831 sys.stdout.write("\n")
3833 def openStreams(self):
3834 self.importProcess = subprocess.Popen(["git", "fast-import"],
3835 stdin=subprocess.PIPE,
3836 stdout=subprocess.PIPE,
3837 stderr=subprocess.PIPE);
3838 self.gitOutput = self.importProcess.stdout
3839 self.gitStream = self.importProcess.stdin
3840 self.gitError = self.importProcess.stderr
3842 if bytes is not str:
3843 # Wrap gitStream.write() so that it can be called using `str` arguments
3844 def make_encoded_write(write):
3845 def encoded_write(s):
3846 return write(s.encode() if isinstance(s, str) else s)
3847 return encoded_write
3849 self.gitStream.write = make_encoded_write(self.gitStream.write)
3851 def closeStreams(self):
3852 if self.gitStream is None:
3854 self.gitStream.close()
3855 if self.importProcess.wait() != 0:
3856 die("fast-import failed: %s" % self.gitError.read())
3857 self.gitOutput.close()
3858 self.gitError.close()
3859 self.gitStream = None
3861 def run(self, args):
3862 if self.importIntoRemotes:
3863 self.refPrefix = "refs/remotes/p4/"
3865 self.refPrefix = "refs/heads/p4/"
3867 self.sync_origin_only()
3869 branch_arg_given = bool(self.branch)
3870 if len(self.branch) == 0:
3871 self.branch = self.refPrefix + "master"
3872 if gitBranchExists("refs/heads/p4") and self.importIntoRemotes:
3873 system("git update-ref %s refs/heads/p4" % self.branch)
3874 system("git branch -D p4")
3876 # accept either the command-line option, or the configuration variable
3877 if self.useClientSpec:
3878 # will use this after clone to set the variable
3879 self.useClientSpec_from_options = True
3881 if gitConfigBool("git-p4.useclientspec"):
3882 self.useClientSpec = True
3883 if self.useClientSpec:
3884 self.clientSpecDirs = getClientSpec()
3886 # TODO: should always look at previous commits,
3887 # merge with previous imports, if possible.
3890 createOrUpdateBranchesFromOrigin(self.refPrefix, self.silent)
3892 # branches holds mapping from branch name to sha1
3893 branches = p4BranchesInGit(self.importIntoRemotes)
3895 # restrict to just this one, disabling detect-branches
3896 if branch_arg_given:
3897 short = self.branch.split("/")[-1]
3898 if short in branches:
3899 self.p4BranchesInGit = [ short ]
3901 self.p4BranchesInGit = branches.keys()
3903 if len(self.p4BranchesInGit) > 1:
3905 print("Importing from/into multiple branches")
3906 self.detectBranches = True
3907 for branch in branches.keys():
3908 self.initialParents[self.refPrefix + branch] = \
3912 print("branches: %s" % self.p4BranchesInGit)
3915 for branch in self.p4BranchesInGit:
3916 logMsg = extractLogMessageFromGitCommit(self.refPrefix + branch)
3918 settings = extractSettingsGitLog(logMsg)
3920 self.readOptions(settings)
3921 if ('depot-paths' in settings
3922 and 'change' in settings):
3923 change = int(settings['change']) + 1
3924 p4Change = max(p4Change, change)
3926 depotPaths = sorted(settings['depot-paths'])
3927 if self.previousDepotPaths == []:
3928 self.previousDepotPaths = depotPaths
3931 for (prev, cur) in zip(self.previousDepotPaths, depotPaths):
3932 prev_list = prev.split("/")
3933 cur_list = cur.split("/")
3934 for i in range(0, min(len(cur_list), len(prev_list))):
3935 if cur_list[i] != prev_list[i]:
3939 paths.append ("/".join(cur_list[:i + 1]))
3941 self.previousDepotPaths = paths
3944 self.depotPaths = sorted(self.previousDepotPaths)
3945 self.changeRange = "@%s,#head" % p4Change
3946 if not self.silent and not self.detectBranches:
3947 print("Performing incremental import into %s git branch" % self.branch)
3949 # accept multiple ref name abbreviations:
3950 # refs/foo/bar/branch -> use it exactly
3951 # p4/branch -> prepend refs/remotes/ or refs/heads/
3952 # branch -> prepend refs/remotes/p4/ or refs/heads/p4/
3953 if not self.branch.startswith("refs/"):
3954 if self.importIntoRemotes:
3955 prepend = "refs/remotes/"
3957 prepend = "refs/heads/"
3958 if not self.branch.startswith("p4/"):
3960 self.branch = prepend + self.branch
3962 if len(args) == 0 and self.depotPaths:
3964 print("Depot paths: %s" % ' '.join(self.depotPaths))
3966 if self.depotPaths and self.depotPaths != args:
3967 print("previous import used depot path %s and now %s was specified. "
3968 "This doesn't work!" % (' '.join (self.depotPaths),
3972 self.depotPaths = sorted(args)
3977 # Make sure no revision specifiers are used when --changesfile
3979 bad_changesfile = False
3980 if len(self.changesFile) > 0:
3981 for p in self.depotPaths:
3982 if p.find("@") >= 0 or p.find("#") >= 0:
3983 bad_changesfile = True
3986 die("Option --changesfile is incompatible with revision specifiers")
3989 for p in self.depotPaths:
3990 if p.find("@") != -1:
3991 atIdx = p.index("@")
3992 self.changeRange = p[atIdx:]
3993 if self.changeRange == "@all":
3994 self.changeRange = ""
3995 elif ',' not in self.changeRange:
3996 revision = self.changeRange
3997 self.changeRange = ""
3999 elif p.find("#") != -1:
4000 hashIdx = p.index("#")
4001 revision = p[hashIdx:]
4003 elif self.previousDepotPaths == []:
4004 # pay attention to changesfile, if given, else import
4005 # the entire p4 tree at the head revision
4006 if len(self.changesFile) == 0:
4009 p = re.sub ("\.\.\.$", "", p)
4010 if not p.endswith("/"):
4015 self.depotPaths = newPaths
4017 # --detect-branches may change this for each branch
4018 self.branchPrefixes = self.depotPaths
4020 self.loadUserMapFromCache()
4022 if self.detectLabels:
4025 if self.detectBranches:
4026 ## FIXME - what's a P4 projectName ?
4027 self.projectName = self.guessProjectName()
4030 self.getBranchMappingFromGitBranches()
4032 self.getBranchMapping()
4034 print("p4-git branches: %s" % self.p4BranchesInGit)
4035 print("initial parents: %s" % self.initialParents)
4036 for b in self.p4BranchesInGit:
4040 b = b[len(self.projectName):]
4041 self.createdBranches.add(b)
4051 self.importHeadRevision(revision)
4053 self.importRevisions(args, branch_arg_given)
4055 if gitConfigBool("git-p4.importLabels"):
4056 self.importLabels = True
4058 if self.importLabels:
4059 p4Labels = getP4Labels(self.depotPaths)
4060 gitTags = getGitTags()
4062 missingP4Labels = p4Labels - gitTags
4063 self.importP4Labels(self.gitStream, missingP4Labels)
4065 except P4CommandException as e:
4074 # Cleanup temporary branches created during import
4075 if self.tempBranches != []:
4076 for branch in self.tempBranches:
4077 read_pipe("git update-ref -d %s" % branch)
4078 os.rmdir(os.path.join(os.environ.get("GIT_DIR", ".git"), self.tempBranchLocation))
4080 # Create a symbolic ref p4/HEAD pointing to p4/<branch> to allow
4081 # a convenient shortcut refname "p4".
4082 if self.importIntoRemotes:
4083 head_ref = self.refPrefix + "HEAD"
4084 if not gitBranchExists(head_ref) and gitBranchExists(self.branch):
4085 system(["git", "symbolic-ref", head_ref, self.branch])
4089 class P4Rebase(Command):
4091 Command.__init__(self)
4093 optparse.make_option("--import-labels", dest="importLabels", action="store_true"),
4095 self.importLabels = False
4096 self.description = ("Fetches the latest revision from perforce and "
4097 + "rebases the current work (branch) against it")
4099 def run(self, args):
4101 sync.importLabels = self.importLabels
4104 return self.rebase()
4107 if os.system("git update-index --refresh") != 0:
4108 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.");
4109 if len(read_pipe("git diff-index HEAD --")) > 0:
4110 die("You have uncommitted changes. Please commit them before rebasing or stash them away with git stash.");
4112 [upstream, settings] = findUpstreamBranchPoint()
4113 if len(upstream) == 0:
4114 die("Cannot find upstream branchpoint for rebase")
4116 # the branchpoint may be p4/foo~3, so strip off the parent
4117 upstream = re.sub("~[0-9]+$", "", upstream)
4119 print("Rebasing the current branch onto %s" % upstream)
4120 oldHead = read_pipe("git rev-parse HEAD").strip()
4121 system("git rebase %s" % upstream)
4122 system("git diff-tree --stat --summary -M %s HEAD --" % oldHead)
4125 class P4Clone(P4Sync):
4127 P4Sync.__init__(self)
4128 self.description = "Creates a new git repository and imports from Perforce into it"
4129 self.usage = "usage: %prog [options] //depot/path[@revRange]"
4131 optparse.make_option("--destination", dest="cloneDestination",
4132 action='store', default=None,
4133 help="where to leave result of the clone"),
4134 optparse.make_option("--bare", dest="cloneBare",
4135 action="store_true", default=False),
4137 self.cloneDestination = None
4138 self.needsGit = False
4139 self.cloneBare = False
4141 def defaultDestination(self, args):
4142 ## TODO: use common prefix of args?
4144 depotDir = re.sub("(@[^@]*)$", "", depotPath)
4145 depotDir = re.sub("(#[^#]*)$", "", depotDir)
4146 depotDir = re.sub(r"\.\.\.$", "", depotDir)
4147 depotDir = re.sub(r"/$", "", depotDir)
4148 return os.path.split(depotDir)[1]
4150 def run(self, args):
4154 if self.keepRepoPath and not self.cloneDestination:
4155 sys.stderr.write("Must specify destination for --keep-path\n")
4160 if not self.cloneDestination and len(depotPaths) > 1:
4161 self.cloneDestination = depotPaths[-1]
4162 depotPaths = depotPaths[:-1]
4164 for p in depotPaths:
4165 if not p.startswith("//"):
4166 sys.stderr.write('Depot paths must start with "//": %s\n' % p)
4169 if not self.cloneDestination:
4170 self.cloneDestination = self.defaultDestination(args)
4172 print("Importing from %s into %s" % (', '.join(depotPaths), self.cloneDestination))
4174 if not os.path.exists(self.cloneDestination):
4175 os.makedirs(self.cloneDestination)
4176 chdir(self.cloneDestination)
4178 init_cmd = [ "git", "init" ]
4180 init_cmd.append("--bare")
4181 retcode = subprocess.call(init_cmd)
4183 raise CalledProcessError(retcode, init_cmd)
4185 if not P4Sync.run(self, depotPaths):
4188 # create a master branch and check out a work tree
4189 if gitBranchExists(self.branch):
4190 system([ "git", "branch", currentGitBranch(), self.branch ])
4191 if not self.cloneBare:
4192 system([ "git", "checkout", "-f" ])
4194 print('Not checking out any branch, use ' \
4195 '"git checkout -q -b master <branch>"')
4197 # auto-set this variable if invoked with --use-client-spec
4198 if self.useClientSpec_from_options:
4199 system("git config --bool git-p4.useclientspec true")
4203 class P4Unshelve(Command):
4205 Command.__init__(self)
4207 self.origin = "HEAD"
4208 self.description = "Unshelve a P4 changelist into a git commit"
4209 self.usage = "usage: %prog [options] changelist"
4211 optparse.make_option("--origin", dest="origin",
4212 help="Use this base revision instead of the default (%s)" % self.origin),
4214 self.verbose = False
4215 self.noCommit = False
4216 self.destbranch = "refs/remotes/p4-unshelved"
4218 def renameBranch(self, branch_name):
4219 """ Rename the existing branch to branch_name.N
4223 for i in range(0,1000):
4224 backup_branch_name = "{0}.{1}".format(branch_name, i)
4225 if not gitBranchExists(backup_branch_name):
4226 gitUpdateRef(backup_branch_name, branch_name) # copy ref to backup
4227 gitDeleteRef(branch_name)
4229 print("renamed old unshelve branch to {0}".format(backup_branch_name))
4233 sys.exit("gave up trying to rename existing branch {0}".format(sync.branch))
4235 def findLastP4Revision(self, starting_point):
4236 """ Look back from starting_point for the first commit created by git-p4
4237 to find the P4 commit we are based on, and the depot-paths.
4240 for parent in (range(65535)):
4241 log = extractLogMessageFromGitCommit("{0}~{1}".format(starting_point, parent))
4242 settings = extractSettingsGitLog(log)
4243 if 'change' in settings:
4246 sys.exit("could not find git-p4 commits in {0}".format(self.origin))
4248 def createShelveParent(self, change, branch_name, sync, origin):
4249 """ Create a commit matching the parent of the shelved changelist 'change'
4251 parent_description = p4_describe(change, shelved=True)
4252 parent_description['desc'] = 'parent for shelved changelist {}\n'.format(change)
4253 files = sync.extractFilesFromCommit(parent_description, shelved=False, shelved_cl=change)
4257 # if it was added in the shelved changelist, it won't exist in the parent
4258 if f['action'] in self.add_actions:
4261 # if it was deleted in the shelved changelist it must not be deleted
4262 # in the parent - we might even need to create it if the origin branch
4264 if f['action'] in self.delete_actions:
4267 parent_files.append(f)
4269 sync.commit(parent_description, parent_files, branch_name,
4270 parent=origin, allow_empty=True)
4271 print("created parent commit for {0} based on {1} in {2}".format(
4272 change, self.origin, branch_name))
4274 def run(self, args):
4278 if not gitBranchExists(self.origin):
4279 sys.exit("origin branch {0} does not exist".format(self.origin))
4284 # only one change at a time
4287 # if the target branch already exists, rename it
4288 branch_name = "{0}/{1}".format(self.destbranch, change)
4289 if gitBranchExists(branch_name):
4290 self.renameBranch(branch_name)
4291 sync.branch = branch_name
4293 sync.verbose = self.verbose
4294 sync.suppress_meta_comment = True
4296 settings = self.findLastP4Revision(self.origin)
4297 sync.depotPaths = settings['depot-paths']
4298 sync.branchPrefixes = sync.depotPaths
4301 sync.loadUserMapFromCache()
4304 # create a commit for the parent of the shelved changelist
4305 self.createShelveParent(change, branch_name, sync, self.origin)
4307 # create the commit for the shelved changelist itself
4308 description = p4_describe(change, True)
4309 files = sync.extractFilesFromCommit(description, True, change)
4311 sync.commit(description, files, branch_name, "")
4314 print("unshelved changelist {0} into {1}".format(change, branch_name))
4318 class P4Branches(Command):
4320 Command.__init__(self)
4322 self.description = ("Shows the git branches that hold imports and their "
4323 + "corresponding perforce depot paths")
4324 self.verbose = False
4326 def run(self, args):
4327 if originP4BranchesExist():
4328 createOrUpdateBranchesFromOrigin()
4330 cmdline = "git rev-parse --symbolic "
4331 cmdline += " --remotes"
4333 for line in read_pipe_lines(cmdline):
4336 if not line.startswith('p4/') or line == "p4/HEAD":
4340 log = extractLogMessageFromGitCommit("refs/remotes/%s" % branch)
4341 settings = extractSettingsGitLog(log)
4343 print("%s <= %s (%s)" % (branch, ",".join(settings["depot-paths"]), settings["change"]))
4346 class HelpFormatter(optparse.IndentedHelpFormatter):
4348 optparse.IndentedHelpFormatter.__init__(self)
4350 def format_description(self, description):
4352 return description + "\n"
4356 def printUsage(commands):
4357 print("usage: %s <command> [options]" % sys.argv[0])
4359 print("valid commands: %s" % ", ".join(commands))
4361 print("Try %s <command> --help for command specific help." % sys.argv[0])
4366 "submit" : P4Submit,
4367 "commit" : P4Submit,
4369 "rebase" : P4Rebase,
4371 "rollback" : P4RollBack,
4372 "branches" : P4Branches,
4373 "unshelve" : P4Unshelve,
4377 if len(sys.argv[1:]) == 0:
4378 printUsage(commands.keys())
4381 cmdName = sys.argv[1]
4383 klass = commands[cmdName]
4386 print("unknown command %s" % cmdName)
4388 printUsage(commands.keys())
4391 options = cmd.options
4392 cmd.gitdir = os.environ.get("GIT_DIR", None)
4396 options.append(optparse.make_option("--verbose", "-v", dest="verbose", action="store_true"))
4398 options.append(optparse.make_option("--git-dir", dest="gitdir"))
4400 parser = optparse.OptionParser(cmd.usage.replace("%prog", "%prog " + cmdName),
4402 description = cmd.description,
4403 formatter = HelpFormatter())
4406 (cmd, args) = parser.parse_args(sys.argv[2:], cmd);
4412 verbose = cmd.verbose
4414 if cmd.gitdir == None:
4415 cmd.gitdir = os.path.abspath(".git")
4416 if not isValidGitDir(cmd.gitdir):
4417 # "rev-parse --git-dir" without arguments will try $PWD/.git
4418 cmd.gitdir = read_pipe("git rev-parse --git-dir").strip()
4419 if os.path.exists(cmd.gitdir):
4420 cdup = read_pipe("git rev-parse --show-cdup").strip()
4424 if not isValidGitDir(cmd.gitdir):
4425 if isValidGitDir(cmd.gitdir + "/.git"):
4426 cmd.gitdir += "/.git"
4428 die("fatal: cannot locate git repository at %s" % cmd.gitdir)
4430 # so git commands invoked from the P4 workspace will succeed
4431 os.environ["GIT_DIR"] = cmd.gitdir
4433 if not cmd.run(args):
4438 if __name__ == '__main__':