git-p4: avoid decoding more data from perforce
[git] / git-p4.py
1 #!/usr/bin/env python
2 #
3 # git-p4.py -- A tool for bidirectional operation between a Perforce depot and git.
4 #
5 # Author: Simon Hausmann <simon@lst.de>
6 # Copyright: 2007 Simon Hausmann <simon@lst.de>
7 #            2007 Trolltech ASA
8 # License: MIT <http://www.opensource.org/licenses/mit-license.php>
9 #
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
17 #
18 import sys
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")
21     sys.exit(1)
22 import os
23 import optparse
24 import functools
25 import marshal
26 import subprocess
27 import tempfile
28 import time
29 import platform
30 import re
31 import shutil
32 import stat
33 import zipfile
34 import zlib
35 import ctypes
36 import errno
37 import glob
38
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
41 # compatibility
42 # support basestring in python3
43 try:
44     if raw_input and input:
45         input = raw_input
46 except:
47     pass
48
49 verbose = False
50
51 # Only labels/tags matching this will be imported/exported
52 defaultLabelRegexp = r'[a-zA-Z0-9_\-.]+$'
53
54 # The block size is reduced automatically if required
55 defaultBlockSize = 1<<20
56
57 p4_access_checked = False
58
59 def p4_build_cmd(cmd):
60     """Build a suitable p4 command line.
61
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.
65     """
66     real_cmd = ["p4"]
67
68     user = gitConfig("git-p4.user")
69     if len(user) > 0:
70         real_cmd += ["-u",user]
71
72     password = gitConfig("git-p4.password")
73     if len(password) > 0:
74         real_cmd += ["-P", password]
75
76     port = gitConfig("git-p4.port")
77     if len(port) > 0:
78         real_cmd += ["-p", port]
79
80     host = gitConfig("git-p4.host")
81     if len(host) > 0:
82         real_cmd += ["-H", host]
83
84     client = gitConfig("git-p4.client")
85     if len(client) > 0:
86         real_cmd += ["-c", client]
87
88     retries = gitConfigInt("git-p4.retries")
89     if retries is None:
90         # Perform 3 retries by default
91         retries = 3
92     if retries > 0:
93         # Provide a way to not pass this option by setting git-p4.retries to 0
94         real_cmd += ["-r", str(retries)]
95
96     if not isinstance(cmd, list):
97         real_cmd = ' '.join(real_cmd) + ' ' + cmd
98     else:
99         real_cmd += cmd
100
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
105         p4_check_access()
106
107     return real_cmd
108
109 def git_dir(path):
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.
112     """
113     d = read_pipe(["git", "--git-dir", path, "rev-parse", "--git-dir"], True).strip()
114     if not d or len(d) == 0:
115         return None
116     else:
117         return d
118
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.
124
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.
129
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
133        the client path.
134        """
135
136     os.chdir(path)
137     if not is_client_path:
138         path = os.getcwd()
139     os.environ['PWD'] = path
140
141 def calcDiskFree():
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
147     else:
148         st = os.statvfs(os.getcwd())
149         return st.f_bavail * st.f_frsize
150
151 def die(msg):
152     """ Terminate execution. Make sure that any running child processes have been wait()ed for before
153         calling this.
154     """
155     if verbose:
156         raise Exception(msg)
157     else:
158         sys.stderr.write(msg + "\n")
159         sys.exit(1)
160
161 def prompt(prompt_text):
162     """ Prompt the user to choose one of the choices
163
164     Choices are identified in the prompt_text by square brackets around
165     a single letter option.
166     """
167     choices = set(m.group(1) for m in re.finditer(r"\[(.)\]", prompt_text))
168     while True:
169         sys.stderr.flush()
170         sys.stdout.write(prompt_text)
171         sys.stdout.flush()
172         response=sys.stdin.readline().strip().lower()
173         if not response:
174             continue
175         response = response[0]
176         if response in choices:
177             return response
178
179 # We need different encoding/decoding strategies for text data being passed
180 # around in pipes depending on python version
181 if bytes is not str:
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
187 else:
188     # For python2.7, pass read strings as-is, but also allow writing unicode
189     def decode_text_stream(s):
190         return s
191     def encode_text_stream(s):
192         return s.encode('utf_8') if isinstance(s, unicode) else s
193
194 def decode_path(path):
195     """Decode a given string (bytes or otherwise) using configured path encoding options
196     """
197     encoding = gitConfig('git-p4.pathEncoding') or 'utf_8'
198     if bytes is not str:
199         return path.decode(encoding, errors='replace') if isinstance(path, bytes) else path
200     else:
201         try:
202             path.decode('ascii')
203         except:
204             path = path.decode(encoding, errors='replace')
205             if verbose:
206                 print('Path with non-ASCII characters detected. Used {} to decode: {}'.format(encoding, path))
207         return path
208
209 def run_git_hook(cmd, param=[]):
210     """Execute a hook if the hook exists."""
211     if verbose:
212         sys.stderr.write("Looking for hook: %s\n" % cmd)
213         sys.stderr.flush()
214
215     hooks_path = gitConfig("core.hooksPath")
216     if len(hooks_path) <= 0:
217         hooks_path = os.path.join(os.environ["GIT_DIR"], "hooks")
218
219     if not isinstance(param, list):
220         param=[param]
221
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 + ".*")
228             if not files:
229                 return True
230             files.sort()
231             hook_file = files.pop()
232             while hook_file.upper().endswith(".SAMPLE"):
233                 # The file is a sample hook. We don't want it
234                 if len(files) > 0:
235                     hook_file = files.pop()
236                 else:
237                     return True
238
239     if not os.path.isfile(hook_file) or not os.access(hook_file, os.X_OK):
240         return True
241
242     return run_hook_command(hook_file, param) == 0
243
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.
248
249        param = a list of parameters to pass to the cmd command
250
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.
256
257        For non Windows operating systems, the file is called
258        as an executable.
259     """
260     cli = [cmd] + param
261     use_shell = False
262     if platform.system() == 'Windows':
263         (root,ext) = os.path.splitext(cmd)
264         if ext == "":
265             exe_path = os.environ.get("EXEPATH")
266             if exe_path is None:
267                 exe_path = ""
268             else:
269                 exe_path = os.path.join(exe_path, "bin")
270             cli = [os.path.join(exe_path, "SH.EXE")] + cli
271         else:
272             use_shell = True
273     return subprocess.call(cli, shell=use_shell)
274
275
276 def write_pipe(c, stdin):
277     if verbose:
278         sys.stderr.write('Writing pipe: %s\n' % str(c))
279
280     expand = not isinstance(c, list)
281     p = subprocess.Popen(c, stdin=subprocess.PIPE, shell=expand)
282     pipe = p.stdin
283     val = pipe.write(stdin)
284     pipe.close()
285     if p.wait():
286         die('Command failed: %s' % str(c))
287
288     return val
289
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)
295
296 def read_pipe_full(c):
297     """ Read output from  command. Returns a tuple
298         of the return status, stdout text and stderr
299         text.
300     """
301     if verbose:
302         sys.stderr.write('Reading pipe: %s\n' % str(c))
303
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))
308
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.
313
314         If raw is True, do not attempt to decode output text.
315     """
316     (retcode, out, err) = read_pipe_full(c)
317     if retcode != 0:
318         if ignore_error:
319             out = ""
320         else:
321             die('Command failed: %s\nError: %s' % (str(c), err))
322     if not raw:
323         out = decode_text_stream(out)
324     return out
325
326 def read_pipe_text(c):
327     """ Read output from a command with trailing whitespace stripped.
328         On error, returns None.
329     """
330     (retcode, out, err) = read_pipe_full(c)
331     if retcode != 0:
332         return None
333     else:
334         return decode_text_stream(out).rstrip()
335
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)
339
340 def read_pipe_lines(c):
341     if verbose:
342         sys.stderr.write('Reading pipe: %s\n' % str(c))
343
344     expand = not isinstance(c, list)
345     p = subprocess.Popen(c, stdout=subprocess.PIPE, shell=expand)
346     pipe = p.stdout
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))
350     return val
351
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)
356
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)
363     p.communicate()
364     return p.returncode == 0
365
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."""
371
372     if not p4_has_command("move"):
373         return False
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:
380         return False
381     if err.find("disabled") >= 0:
382         return False
383     # assume it failed because @... was invalid changelist
384     return True
385
386 def system(cmd, ignore_error=False):
387     expand = not isinstance(cmd, list)
388     if verbose:
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)
393
394     return retcode
395
396 def p4_system(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)
401     if retcode:
402         raise CalledProcessError(retcode, real_cmd)
403
404 def die_bad_access(s):
405     die("failure accessing depot: {0}".format(s.rstrip()))
406
407 def p4_check_access(min_expiration=1):
408     """ Check if we can access Perforce - account still logged in
409     """
410     results = p4CmdList(["login", "-s"])
411
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")
415
416     result = results[0]
417
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")
421
422     code = result.get("code")
423     if not code:
424         # we get here if we couldn't connect and there was nothing to unmarshal
425         die_bad_access("could not connect")
426
427     elif code == "stat":
428         expiry = result.get("TicketExpiration")
429         if expiry:
430             expiry = int(expiry)
431             if expiry > min_expiration:
432                 # ok to carry on
433                 return
434             else:
435                 die_bad_access("perforce ticket expires in {0} seconds".format(expiry))
436
437         else:
438             # account without a timeout - all ok
439             return
440
441     elif code == "error":
442         data = result.get("data")
443         if data:
444             die_bad_access("p4 error: {0}".format(data))
445         else:
446             die_bad_access("unknown error")
447     elif code == "info":
448         return
449     else:
450         die_bad_access("unknown error code {0}".format(code))
451
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.
456
457        $ p4 -V
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).
461     """
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
467
468 def p4_integrate(src, dest):
469     p4_system(["integrate", "-Dt", wildcard_encode(src), wildcard_encode(dest)])
470
471 def p4_sync(f, *options):
472     p4_system(["sync"] + list(options) + [wildcard_encode(f)])
473
474 def p4_add(f):
475     # forcibly add file names with wildcards
476     if wildcard_present(f):
477         p4_system(["add", "-f", f])
478     else:
479         p4_system(["add", f])
480
481 def p4_delete(f):
482     p4_system(["delete", wildcard_encode(f)])
483
484 def p4_edit(f, *options):
485     p4_system(["edit"] + list(options) + [wildcard_encode(f)])
486
487 def p4_revert(f):
488     p4_system(["revert", wildcard_encode(f)])
489
490 def p4_reopen(type, f):
491     p4_system(["reopen", "-t", type, wildcard_encode(f)])
492
493 def p4_reopen_in_change(changelist, files):
494     cmd = ["reopen", "-c", str(changelist)] + files
495     p4_system(cmd)
496
497 def p4_move(src, dest):
498     p4_system(["move", "-k", wildcard_encode(src), wildcard_encode(dest)])
499
500 def p4_last_change():
501     results = p4CmdList(["changes", "-m", "1"], skip_info=True)
502     return int(results[0]['change'])
503
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
507        results."""
508
509     cmd = ["describe", "-s"]
510     if shelved:
511         cmd += ["-S"]
512     cmd += [str(change)]
513
514     ds = p4CmdList(cmd, skip_info=True)
515     if len(ds) != 1:
516         die("p4 describe -s %d did not return 1 result: %s" % (change, str(ds)))
517
518     d = ds[0]
519
520     if "p4ExitCode" in d:
521         die("p4 describe -s %d exited with %d: %s" % (change, d["p4ExitCode"],
522                                                       str(d)))
523     if "code" in d:
524         if d["code"] == "error":
525             die("p4 describe -s %d returned error code: %s" % (change, str(d)))
526
527     if "time" not in d:
528         die("p4 describe -s %d returned no \"time\": %s" % (change, str(d)))
529
530     return d
531
532 #
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.
536 #
537 def split_p4_type(p4type):
538
539     p4_filetypes_historical = {
540         "ctempobj": "binary+Sw",
541         "ctext": "text+C",
542         "cxtext": "text+Cx",
543         "ktext": "text+k",
544         "kxtext": "text+kx",
545         "ltext": "text+F",
546         "tempobj": "binary+FSw",
547         "ubinary": "binary+F",
548         "uresource": "resource+F",
549         "uxbinary": "binary+Fx",
550         "xbinary": "binary+x",
551         "xltext": "text+Fx",
552         "xtempobj": "binary+Swx",
553         "xtext": "text+x",
554         "xunicode": "unicode+x",
555         "xutf16": "utf16+x",
556     }
557     if p4type in p4_filetypes_historical:
558         p4type = p4_filetypes_historical[p4type]
559     mods = ""
560     s = p4type.split("+")
561     base = s[0]
562     mods = ""
563     if len(s) > 1:
564         mods = s[1]
565     return (base, mods)
566
567 #
568 # return the raw p4 type of a file (text, text+ko, etc)
569 #
570 def p4_type(f):
571     results = p4CmdList(["fstat", "-T", "headType", wildcard_encode(f)])
572     return results[0]['headType']
573
574 #
575 # Given a type base and modifier, return a regexp matching
576 # the keywords that can be expanded in the file
577 #
578 def p4_keywords_regexp_for_type(base, type_mods):
579     if base in ("text", "unicode", "binary"):
580         kwords = None
581         if "ko" in type_mods:
582             kwords = 'Id|Header'
583         elif "k" in type_mods:
584             kwords = 'Id|Header|Author|Date|DateTime|Change|File|Revision'
585         else:
586             return None
587         pattern = r"""
588             \$              # Starts with a dollar, followed by...
589             (%s)            # one of the keywords, followed by...
590             (:[^$\n]+)?     # possibly an old expansion, followed by...
591             \$              # another dollar
592             """ % kwords
593         return pattern
594     else:
595         return None
596
597 #
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.
601 #
602 def p4_keywords_regexp_for_file(file):
603     if not os.path.exists(file):
604         return None
605     else:
606         (type_base, type_mods) = split_p4_type(p4_type(file))
607         return p4_keywords_regexp_for_type(type_base, type_mods)
608
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.
612
613     p4Type = "+x"
614
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]
621
622     p4_reopen(p4Type, file)
623
624 def getP4OpenedType(file):
625     # Returns the perforce file type for the given file.
626
627     result = p4_read_pipe(["opened", wildcard_encode(file)])
628     match = re.match(".*\((.+)\)( \*exclusive\*)?\r?$", result)
629     if match:
630         return match.group(1)
631     else:
632         die("Could not determine file type for %s (result: '%s')" % (file, result))
633
634 # Return the set of all p4 labels
635 def getP4Labels(depotPaths):
636     labels = set()
637     if not isinstance(depotPaths, list):
638         depotPaths = [depotPaths]
639
640     for l in p4CmdList(["labels"] + ["%s..." % p for p in depotPaths]):
641         label = l['label']
642         labels.add(label)
643
644     return labels
645
646 # Return the set of all git tags
647 def getGitTags():
648     gitTags = set()
649     for line in read_pipe_lines(["git", "tag"]):
650         tag = line.strip()
651         gitTags.add(tag)
652     return gitTags
653
654 _diff_tree_pattern = None
655
656 def parseDiffTreeEntry(entry):
657     """Parses a single diff tree entry into its component elements.
658
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:
661
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.
672
673     If the pattern is not matched, None is returned."""
674
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(.*))|$)')
678
679     match = _diff_tree_pattern.match(entry)
680     if match:
681         return {
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)
690         }
691     return None
692
693 def isModeExec(mode):
694     # Returns True if the given git mode represents an executable file,
695     # otherwise False.
696     return mode[-3:] == "755"
697
698 class P4Exception(Exception):
699     """ Base class for exceptions from the p4 client """
700     def __init__(self, exit_code):
701         self.p4ExitCode = exit_code
702
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']
710
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)
715         self.limit = limit
716
717 class P4CommandException(P4Exception):
718     """ Something went wrong calling p4 which means we have to give up """
719     def __init__(self, msg):
720         self.msg = msg
721
722     def __str__(self):
723         return self.msg
724
725 def isModeExecChanged(src_mode, dst_mode):
726     return isModeExec(src_mode) != isModeExec(dst_mode)
727
728 def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None, skip_info=False,
729         errors_as_exceptions=False):
730
731     if not isinstance(cmd, list):
732         cmd = "-G " + cmd
733         expand = True
734     else:
735         cmd = ["-G"] + cmd
736         expand = False
737
738     cmd = p4_build_cmd(cmd)
739     if verbose:
740         sys.stderr.write("Opening pipe: %s\n" % str(cmd))
741
742     # Use a temporary file to avoid deadlocks without
743     # subprocess.communicate(), which would put another copy
744     # of stdout into memory.
745     stdin_file = None
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)
750         else:
751             for i in stdin:
752                 stdin_file.write(encode_text_stream(i))
753                 stdin_file.write(b'\n')
754         stdin_file.flush()
755         stdin_file.seek(0)
756
757     p4 = subprocess.Popen(cmd,
758                           shell=expand,
759                           stdin=stdin_file,
760                           stdout=subprocess.PIPE)
761
762     result = []
763     try:
764         while True:
765             entry = marshal.load(p4.stdout)
766             if bytes is not str:
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',
771                                'user', 'User')
772                 decoded_entry = {}
773                 for key, value in entry.items():
774                     key = key.decode()
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
782             if skip_info:
783                 if 'code' in entry and entry['code'] == 'info':
784                     continue
785             if cb is not None:
786                 cb(entry)
787             else:
788                 result.append(entry)
789     except EOFError:
790         pass
791     exitCode = p4.wait()
792     if exitCode != 0:
793         if errors_as_exceptions:
794             if len(result) > 0:
795                 data = result[0].get('data')
796                 if data:
797                     m = re.search('Too many rows scanned \(over (\d+)\)', data)
798                     if not m:
799                         m = re.search('Request too large \(over (\d+)\)', data)
800
801                     if m:
802                         limit = int(m.group(1))
803                         raise P4RequestSizeException(exitCode, result, limit)
804
805                 raise P4ServerException(exitCode, result)
806             else:
807                 raise P4Exception(exitCode)
808         else:
809             entry = {}
810             entry["p4ExitCode"] = exitCode
811             result.append(entry)
812
813     return result
814
815 def p4Cmd(cmd):
816     list = p4CmdList(cmd)
817     result = {}
818     for entry in list:
819         result.update(entry)
820     return result;
821
822 def p4Where(depotPath):
823     if not depotPath.endswith("/"):
824         depotPath += "/"
825     depotPathLong = depotPath + "..."
826     outputList = p4CmdList(["where", depotPathLong])
827     output = None
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:] == "/...":
834                 output = entry
835                 break
836         elif "data" in entry:
837             data = entry.get("data")
838             space = data.find(" ")
839             if data[:space] == depotPath:
840                 output = entry
841                 break
842     if output == None:
843         return ""
844     if output["code"] == "error":
845         return ""
846     clientPath = ""
847     if "path" in output:
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:])
853
854     if clientPath.endswith("..."):
855         clientPath = clientPath[:-3]
856     return clientPath
857
858 def currentGitBranch():
859     return read_pipe_text(["git", "symbolic-ref", "--short", "-q", "HEAD"])
860
861 def isValidGitDir(path):
862     return git_dir(path) != None
863
864 def parseRevision(ref):
865     return read_pipe("git rev-parse %s" % ref).strip()
866
867 def branchExists(ref):
868     rev = read_pipe(["git", "rev-parse", "-q", "--verify", ref],
869                      ignore_error=True)
870     return len(rev) > 0
871
872 def extractLogMessageFromGitCommit(commit):
873     logMessage = ""
874
875     ## fixme: title is first line of commit, not 1st paragraph.
876     foundTitle = False
877     for log in read_pipe_lines(["git", "cat-file", "commit", commit]):
878        if not foundTitle:
879            if len(log) == 1:
880                foundTitle = True
881            continue
882
883        logMessage += log
884     return logMessage
885
886 def extractSettingsGitLog(log):
887     values = {}
888     for line in log.split("\n"):
889         line = line.strip()
890         m = re.search (r"^ *\[git-p4: (.*)\]$", line)
891         if not m:
892             continue
893
894         assignments = m.group(1).split (':')
895         for a in assignments:
896             vals = a.split ('=')
897             key = vals[0].strip()
898             val = ('='.join (vals[1:])).strip()
899             if val.endswith ('\"') and val.startswith('"'):
900                 val = val[1:-1]
901
902             values[key] = val
903
904     paths = values.get("depot-paths")
905     if not paths:
906         paths = values.get("depot-path")
907     if paths:
908         values['depot-paths'] = paths.split(',')
909     return values
910
911 def gitBranchExists(branch):
912     proc = subprocess.Popen(["git", "rev-parse", branch],
913                             stderr=subprocess.PIPE, stdout=subprocess.PIPE);
914     return proc.wait() == 0;
915
916 def gitUpdateRef(ref, newvalue):
917     subprocess.check_call(["git", "update-ref", ref, newvalue])
918
919 def gitDeleteRef(ref):
920     subprocess.check_call(["git", "update-ref", "-d", ref])
921
922 _gitConfig = {}
923
924 def gitConfig(key, typeSpecifier=None):
925     if key not in _gitConfig:
926         cmd = [ "git", "config" ]
927         if typeSpecifier:
928             cmd += [ typeSpecifier ]
929         cmd += [ key ]
930         s = read_pipe(cmd, ignore_error=True)
931         _gitConfig[key] = s.strip()
932     return _gitConfig[key]
933
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
937        in the config."""
938
939     if key not in _gitConfig:
940         _gitConfig[key] = gitConfig(key, '--bool') == "true"
941     return _gitConfig[key]
942
943 def gitConfigInt(key):
944     if key not in _gitConfig:
945         cmd = [ "git", "config", "--int", key ]
946         s = read_pipe(cmd, ignore_error=True)
947         v = s.strip()
948         try:
949             _gitConfig[key] = int(gitConfig(key, '--int'))
950         except ValueError:
951             _gitConfig[key] = None
952     return _gitConfig[key]
953
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'']:
959             _gitConfig[key] = []
960     return _gitConfig[key]
961
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
967        "p4/" prefix."""
968
969     branches = {}
970
971     cmdline = "git rev-parse --symbolic "
972     if branchesAreInRemotes:
973         cmdline += "--remotes"
974     else:
975         cmdline += "--branches"
976
977     for line in read_pipe_lines(cmdline):
978         line = line.strip()
979
980         # only import to p4/
981         if not line.startswith('p4/'):
982             continue
983         # special symbolic ref to p4/master
984         if line == "p4/HEAD":
985             continue
986
987         # strip off p4/ prefix
988         branch = line[len("p4/"):]
989
990         branches[branch] = parseRevision(line)
991
992     return branches
993
994 def branch_exists(branch):
995     """Make sure that the given ref name really exists."""
996
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)
1001     if p.returncode:
1002         return False
1003     # expect exactly one line of output: the branch name
1004     return out.rstrip() == branch
1005
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
1017
1018     settings = None
1019     parent = 0
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]
1028
1029         parent = parent + 1
1030
1031     return ["", settings]
1032
1033 def createOrUpdateBranchesFromOrigin(localRefPrefix = "refs/remotes/p4/", silent=True):
1034     if not silent:
1035         print("Creating/updating branch(es) in %s based on origin branch(es)"
1036                % localRefPrefix)
1037
1038     originPrefix = "origin/p4/"
1039
1040     for line in read_pipe_lines("git rev-parse --symbolic --remotes"):
1041         line = line.strip()
1042         if (not line.startswith(originPrefix)) or line.endswith("HEAD"):
1043             continue
1044
1045         headName = line[len(originPrefix):]
1046         remoteHead = localRefPrefix + headName
1047         originHead = line
1048
1049         original = extractSettingsGitLog(extractLogMessageFromGitCommit(originHead))
1050         if ('depot-paths' not in original
1051             or 'change' not in original):
1052             continue
1053
1054         update = False
1055         if not gitBranchExists(remoteHead):
1056             if verbose:
1057                 print("creating %s" % remoteHead)
1058             update = True
1059         else:
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))
1070                         update = True
1071                 else:
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'])))
1076
1077         if update:
1078             system("git update-ref %s %s" % (remoteHead, originHead))
1079
1080 def originP4BranchesExist():
1081         return gitBranchExists("origin") or gitBranchExists("origin/p4") or gitBranchExists("origin/p4/master")
1082
1083
1084 def p4ParseNumericChangeRange(parts):
1085     changeStart = int(parts[0][1:])
1086     if parts[1] == '#head':
1087         changeEnd = p4_last_change()
1088     else:
1089         changeEnd = int(parts[1])
1090
1091     return (changeStart, changeEnd)
1092
1093 def chooseBlockSize(blockSize):
1094     if blockSize:
1095         return blockSize
1096     else:
1097         return defaultBlockSize
1098
1099 def p4ChangesForPaths(depotPaths, changeRange, requestedBlockSize):
1100     assert depotPaths
1101
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.
1107
1108     if changeRange is None or changeRange == '':
1109         changeStart = 1
1110         changeEnd = p4_last_change()
1111         block_size = chooseBlockSize(requestedBlockSize)
1112     else:
1113         parts = changeRange.split(',')
1114         assert len(parts) == 2
1115         try:
1116             (changeStart, changeEnd) = p4ParseNumericChangeRange(parts)
1117             block_size = chooseBlockSize(requestedBlockSize)
1118         except ValueError:
1119             changeStart = parts[0][1:]
1120             changeEnd = parts[1]
1121             if requestedBlockSize:
1122                 die("cannot use --changes-block-size with non-numeric revisions")
1123             block_size = None
1124
1125     changes = set()
1126
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
1130
1131     while True:
1132         cmd = ['changes']
1133
1134         if block_size:
1135             end = min(changeEnd, changeStart + block_size)
1136             revisionRange = "%d,%d" % (changeStart, end)
1137         else:
1138             revisionRange = "%s,%s" % (changeStart, changeEnd)
1139
1140         for p in depotPaths:
1141             cmd += ["%s...@%s" % (p, revisionRange)]
1142
1143         # fetch the changes
1144         try:
1145             result = p4CmdList(cmd, errors_as_exceptions=True)
1146         except P4RequestSizeException as e:
1147             if not block_size:
1148                 block_size = e.limit
1149             elif block_size > e.limit:
1150                 block_size = e.limit
1151             else:
1152                 block_size = max(2, block_size // 2)
1153
1154             if verbose: print("block size error, retrying with block size {0}".format(block_size))
1155             continue
1156         except P4Exception as e:
1157             die('Error retrieving changes description ({0})'.format(e.p4ExitCode))
1158
1159         # Insert changes in chronological order
1160         for entry in reversed(result):
1161             if 'change' not in entry:
1162                 continue
1163             changes.add(int(entry['change']))
1164
1165         if not block_size:
1166             break
1167
1168         if end >= changeEnd:
1169             break
1170
1171         changeStart = end + 1
1172
1173     changes = sorted(changes)
1174     return changes
1175
1176 def p4PathStartsWith(path, prefix):
1177     # This method tries to remedy a potential mixed-case issue:
1178     #
1179     # If UserA adds  //depot/DirA/file1
1180     # and UserB adds //depot/dira/file2
1181     #
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)
1187
1188 def getClientSpec():
1189     """Look at the p4 client spec, create a View() object that contains
1190        all the mappings, and return it."""
1191
1192     specList = p4CmdList("client -o")
1193     if len(specList) != 1:
1194         die('Output from "client -o" is %d lines, expecting 1' %
1195             len(specList))
1196
1197     # dictionary of all client parameters
1198     entry = specList[0]
1199
1200     # the //client/ name
1201     client_name = entry["Client"]
1202
1203     # just the keys that start with "View"
1204     view_keys = [ k for k in entry.keys() if k.startswith("View") ]
1205
1206     # hold this new View
1207     view = View(client_name)
1208
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])
1215
1216     return view
1217
1218 def getClientRoot():
1219     """Grab the client directory."""
1220
1221     output = p4CmdList("client -o")
1222     if len(output) != 1:
1223         die('Output from "client -o" is %d lines, expecting 1' % len(output))
1224
1225     entry = output[0]
1226     if "Root" not in entry:
1227         die('Client has no "Root"')
1228
1229     return entry["Root"]
1230
1231 #
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.
1235 #
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", "%")
1246     return path
1247
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")
1254     return path
1255
1256 def wildcard_present(path):
1257     m = re.search("[*#@%]", path)
1258     return m is not None
1259
1260 class LargeFileSystem(object):
1261     """Base class for large file system support."""
1262
1263     def __init__(self, writeToGitStream):
1264         self.largeFiles = set()
1265         self.writeToGitStream = writeToGitStream
1266
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__
1271
1272     def pushFile(self, localLargeFile):
1273         """Push the actual content which is not stored in the Git repository to
1274            a server."""
1275         assert False, "Method 'pushFile' required in " + self.__class__.__name__
1276
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')],
1281             False
1282         )
1283
1284     def generateTempFile(self, contents):
1285         contentFile = tempfile.NamedTemporaryFile(prefix='git-p4-large-file', delete=False)
1286         for d in contents:
1287             contentFile.write(d)
1288         contentFile.close()
1289         return contentFile.name
1290
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'):
1295                 return True
1296         if gitConfigInt('git-p4.largeFileCompressedThreshold'):
1297             contentsSize = sum(len(d) for d in contents)
1298             if contentsSize <= gitConfigInt('git-p4.largeFileCompressedThreshold'):
1299                 return False
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'):
1307                 return True
1308         return False
1309
1310     def addLargeFile(self, relPath):
1311         self.largeFiles.add(relPath)
1312
1313     def removeLargeFile(self, relPath):
1314         self.largeFiles.remove(relPath)
1315
1316     def isLargeFile(self, relPath):
1317         return relPath in self.largeFiles
1318
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
1322            steps."""
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
1328             if localLargeFile:
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)
1337                 if verbose:
1338                     sys.stderr.write("%s moved to large file system (%s)\n" % (relPath, localLargeFile))
1339         return (git_mode, contents)
1340
1341 class MockLFS(LargeFileSystem):
1342     """Mock large file system for testing."""
1343
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.
1347            """
1348         with open(contentFile, 'r') as f:
1349             content = next(f)
1350             gitMode = '100644'
1351             pointerContents = 'pointer-' + content
1352             localLargeFile = os.path.join(os.getcwd(), '.git', 'mock-storage', 'local', content[:-1])
1353             return (gitMode, pointerContents, localLargeFile)
1354
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.
1358            """
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)))
1363
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."""
1367
1368     def __init__(self, *args):
1369         LargeFileSystem.__init__(self, *args)
1370         self.baseGitAttributes = []
1371
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
1376            content.
1377            """
1378         if os.path.getsize(contentFile) == 0:
1379             return (None, '', None)
1380
1381         pointerProcess = subprocess.Popen(
1382             ['git', 'lfs', 'pointer', '--file=' + contentFile],
1383             stdout=subprocess.PIPE
1384         )
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?')
1389
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
1392         # earlier versions.
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)
1396
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')
1400         if not lfs_path:
1401             lfs_path = 'lfs'
1402         if not os.path.isabs(lfs_path):
1403             lfs_path = os.path.join(os.getcwd(), '.git', lfs_path)
1404         localLargeFile = os.path.join(
1405             lfs_path,
1406             'objects', oid[:2], oid[2:4],
1407             oid,
1408         )
1409         # LFS Spec states that pointer files should not have the executable bit set.
1410         gitMode = '100644'
1411         return (gitMode, pointerFile, localLargeFile)
1412
1413     def pushFile(self, localLargeFile):
1414         uploadProcess = subprocess.Popen(
1415             ['git', 'lfs', 'push', '--object-id', 'origin', os.path.basename(localLargeFile)]
1416         )
1417         if uploadProcess.wait():
1418             die('git-lfs push command failed. Did you define a remote?')
1419
1420     def generateGitAttributes(self):
1421         return (
1422             self.baseGitAttributes +
1423             [
1424                 '\n',
1425                 '#\n',
1426                 '# Git LFS (see https://git-lfs.github.com/)\n',
1427                 '#\n',
1428             ] +
1429             ['*.' + f.replace(' ', '[[:space:]]') + ' filter=lfs diff=lfs merge=lfs -text\n'
1430                 for f in sorted(gitConfigList('git-p4.largeFileExtensions'))
1431             ] +
1432             ['/' + f.replace(' ', '[[:space:]]') + ' filter=lfs diff=lfs merge=lfs -text\n'
1433                 for f in sorted(self.largeFiles) if not self.hasLargeFileExtension(f)
1434             ]
1435         )
1436
1437     def addLargeFile(self, relPath):
1438         LargeFileSystem.addLargeFile(self, relPath)
1439         self.writeToGitStream('100644', '.gitattributes', self.generateGitAttributes())
1440
1441     def removeLargeFile(self, relPath):
1442         LargeFileSystem.removeLargeFile(self, relPath)
1443         self.writeToGitStream('100644', '.gitattributes', self.generateGitAttributes())
1444
1445     def processContent(self, git_mode, relPath, contents):
1446         if relPath == '.gitattributes':
1447             self.baseGitAttributes = contents
1448             return (git_mode, self.generateGitAttributes())
1449         else:
1450             return LargeFileSystem.processContent(self, git_mode, relPath, contents)
1451
1452 class Command:
1453     delete_actions = ( "delete", "move/delete", "purge" )
1454     add_actions = ( "add", "branch", "move/add" )
1455
1456     def __init__(self):
1457         self.usage = "usage: %prog [options]"
1458         self.needsGit = True
1459         self.verbose = False
1460
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)
1466
1467 class P4UserMap:
1468     def __init__(self):
1469         self.userMapFromPerforceServer = False
1470         self.myP4UserId = None
1471
1472     def p4UserId(self):
1473         if self.myP4UserId:
1474             return self.myP4UserId
1475
1476         results = p4CmdList("user -o")
1477         for r in results:
1478             if 'User' in r:
1479                 self.myP4UserId = r['User']
1480                 return r['User']
1481         die("Could not find your p4 user id")
1482
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:
1487             return False
1488         else:
1489             return True
1490
1491     def getUserCacheFilename(self):
1492         home = os.environ.get("HOME", os.environ.get("USERPROFILE"))
1493         return home + "/.gitp4-usercache.txt"
1494
1495     def getUserMapFromPerforceServer(self):
1496         if self.userMapFromPerforceServer:
1497             return
1498         self.users = {}
1499         self.emails = {}
1500
1501         for output in p4CmdList("users"):
1502             if "User" not in output:
1503                 continue
1504             self.users[output["User"]] = output["FullName"] + b" <" + output["Email"] + b">"
1505             self.emails[output["Email"]] = output["User"]
1506
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
1516
1517         s = b''
1518         for (key, val) in self.users.items():
1519             s += b"%s\t%s\n" % (key.expandtabs(1), val.expandtabs(1))
1520
1521         open(self.getUserCacheFilename(), 'wb').write(s)
1522         self.userMapFromPerforceServer = True
1523
1524     def loadUserMapFromCache(self):
1525         self.users = {}
1526         self.userMapFromPerforceServer = False
1527         try:
1528             cache = open(self.getUserCacheFilename(), 'rb')
1529             lines = cache.readlines()
1530             cache.close()
1531             for line in lines:
1532                 entry = line.strip().split(b"\t")
1533                 self.users[entry[0]] = entry[1]
1534         except IOError:
1535             self.getUserMapFromPerforceServer()
1536
1537 class P4Debug(Command):
1538     def __init__(self):
1539         Command.__init__(self)
1540         self.options = []
1541         self.description = "A tool to debug the output of p4 -G."
1542         self.needsGit = False
1543
1544     def run(self, args):
1545         j = 0
1546         for output in p4CmdList(args):
1547             print('Element: %d' % j)
1548             j += 1
1549             print(output)
1550         return True
1551
1552 class P4RollBack(Command):
1553     def __init__(self):
1554         Command.__init__(self)
1555         self.options = [
1556             optparse.make_option("--local", dest="rollbackLocalBranches", action="store_true")
1557         ]
1558         self.description = "A tool to debug the multi-branch import. Don't use :)"
1559         self.rollbackLocalBranches = False
1560
1561     def run(self, args):
1562         if len(args) != 1:
1563             return False
1564         maxChange = int(args[0])
1565
1566         if "p4ExitCode" in p4Cmd("changes -m 1"):
1567             die("Problems executing p4");
1568
1569         if self.rollbackLocalBranches:
1570             refPrefix = "refs/heads/"
1571             lines = read_pipe_lines("git rev-parse --symbolic --branches")
1572         else:
1573             refPrefix = "refs/remotes/"
1574             lines = read_pipe_lines("git rev-parse --symbolic --remotes")
1575
1576         for line in lines:
1577             if self.rollbackLocalBranches or (line.startswith("p4/") and line != "p4/HEAD\n"):
1578                 line = line.strip()
1579                 ref = refPrefix + line
1580                 log = extractLogMessageFromGitCommit(ref)
1581                 settings = extractSettingsGitLog(log)
1582
1583                 depotPaths = settings['depot-paths']
1584                 change = settings['change']
1585
1586                 changed = False
1587
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))
1592                     continue
1593
1594                 while change and int(change) > maxChange:
1595                     changed = True
1596                     if self.verbose:
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)
1601
1602
1603                     depotPaths = settings['depot-paths']
1604                     change = settings['change']
1605
1606                 if changed:
1607                     print("%s rewound to %s" % (ref, change))
1608
1609         return True
1610
1611 class P4Submit(Command, P4UserMap):
1612
1613     conflict_behavior_choices = ("ask", "skip", "quit")
1614
1615     def __init__(self):
1616         Command.__init__(self)
1617         P4UserMap.__init__(self)
1618         self.options = [
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"),
1645         ]
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.
1651
1652     One usage scenario is to run unit tests in the hook.
1653
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.
1658
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.
1662
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.
1667
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.
1671
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.
1675     """
1676
1677         self.usage += " [name of git branch to submit into perforce depot]"
1678         self.origin = ""
1679         self.detectRenames = False
1680         self.preserveUser = gitConfigBool("git-p4.preserveUser")
1681         self.dry_run = False
1682         self.shelve = False
1683         self.update_shelve = list()
1684         self.commit = ""
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()
1692         self.branch = None
1693         self.no_verify = False
1694
1695         if gitConfig('git-p4.largeFileSystem'):
1696             die("Large file system not supported for git-p4 submit command. Please remove it from config.")
1697
1698     def check(self):
1699         if len(p4CmdList("opened ...")) > 0:
1700             die("You have files opened with perforce! Close them before starting the sync.")
1701
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
1705            specification.
1706
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.
1710
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.
1715
1716            Return a tuple (stripped log message, jobs string)."""
1717
1718         m = re.search(r'^Jobs:', message, re.MULTILINE)
1719         if m is None:
1720             return (message, None)
1721
1722         jobtext = message[m.start():]
1723         stripped_message = message[:m.start()].rstrip()
1724         return (stripped_message, jobtext)
1725
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
1729            the Jobs field."""
1730         result = ""
1731
1732         inDescriptionSection = False
1733
1734         for line in template.split("\n"):
1735             if line.startswith("#"):
1736                 result += line + "\n"
1737                 continue
1738
1739             if inDescriptionSection:
1740                 if line.startswith("Files:") or line.startswith("Jobs:"):
1741                     inDescriptionSection = False
1742                     # insert Jobs section
1743                     if jobs:
1744                         result += jobs + "\n"
1745                 else:
1746                     continue
1747             else:
1748                 if line.startswith("Description:"):
1749                     inDescriptionSection = True
1750                     line += "\n"
1751                     for messageLine in message.split("\n"):
1752                         line += "\t" + messageLine + "\n"
1753
1754             result += line + "\n"
1755
1756         return result
1757
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='.')
1761         try:
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)
1767                 outFile.write(line)
1768             inFile.close()
1769             outFile.close()
1770             # Forcibly overwrite the original file
1771             os.unlink(file)
1772             shutil.move(outFileName, file)
1773         except:
1774             # cleanup our temporary file
1775             os.unlink(outFileName)
1776             print("Failed to strip RCS keywords in %s" % file)
1777             raise
1778
1779         print("Patched up RCS keywords in %s" % file)
1780
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)
1789         else:
1790             return (self.emails[gitEmail],gitEmail)
1791
1792     def checkValidP4Users(self,commits):
1793         # check if any git authors cannot be mapped to p4 users
1794         for id in commits:
1795             (user,email) = self.p4UserForCommit(id)
1796             if not user:
1797                 msg = "Cannot find p4 user for email %s in commit %s." % (email, id)
1798                 if gitConfigBool("git-p4.allowMissingP4Users"):
1799                     print("%s" % msg)
1800                 else:
1801                     die("Error: %s\nSet git-p4.allowMissingP4Users to true to allow this." % msg)
1802
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
1807         # wrong.
1808         results = p4CmdList("client -o")        # find the current client
1809         client = None
1810         for r in results:
1811             if 'Client' in r:
1812                 client = r['Client']
1813                 break
1814         if not client:
1815             die("could not get client spec")
1816         results = p4CmdList(["changes", "-c", client, "-m", "1"])
1817         for r in results:
1818             if 'change' in r:
1819                 return r['change']
1820         die("Could not get changelist number for last submit - cannot patch up user details")
1821
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))
1828
1829         c = changes[0]
1830         if c['User'] == newUser: return   # nothing to do
1831         c['User'] = newUser
1832         # p4 does not understand format version 3 and above
1833         input = marshal.dumps(c, 2)
1834
1835         result = p4CmdList("change -f -i", stdin=input)
1836         for r in result:
1837             if 'code' in r:
1838                 if r['code'] == 'error':
1839                     die("Could not modify user field of changelist %s to %s:%s" % (changelist, newUser, r['data']))
1840             if 'data' in r:
1841                 print("Updated user field for changelist %s to %s" % (changelist, newUser))
1842                 return
1843         die("Could not modify user field of changelist %s to %s" % (changelist, newUser))
1844
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])
1849         for r in results:
1850             if 'perm' in r:
1851                 if r['perm'] == 'admin':
1852                     return 1
1853                 if r['perm'] == 'super':
1854                     return 1
1855         return 0
1856
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.
1861
1862            Remove lines in the Files section that show changes to files
1863            outside the depot path we're committing into."""
1864
1865         [upstream, settings] = findUpstreamBranchPoint()
1866
1867         template = """\
1868 # A Perforce Change Specification.
1869 #
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.)
1882 """
1883         files_list = []
1884         inFilesSection = False
1885         change_entry = None
1886         args = ['change', '-o']
1887         if changelist:
1888             args.append(str(changelist))
1889         for entry in p4CmdList(args):
1890             if 'code' not in entry:
1891                 continue
1892             if entry['code'] == 'stat':
1893                 change_entry = entry
1894                 break
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)]:
1902                         continue
1903                 else:
1904                     if not p4PathStartsWith(value, self.depotPath):
1905                         continue
1906                 files_list.append(value)
1907                 continue
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:
1911                 continue
1912             template += '\n'
1913             template += key + ':'
1914             if key == 'Description':
1915                 template += '\n'
1916             for field_line in decode_text_stream(change_entry[key]).splitlines():
1917                 template += '\t'+field_line+'\n'
1918         if len(files_list) > 0:
1919             template += '\n'
1920             template += 'Files:\n'
1921         for path in files_list:
1922             template += '\t'+path+'\n'
1923         return template
1924
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."""
1928
1929         # if configured to skip the editing part, just submit
1930         if gitConfigBool("git-p4.skipSubmitEdit"):
1931             return True
1932
1933         # look at the modification time, to check later if the user saved
1934         # the file
1935         mtime = os.stat(template_file).st_mtime
1936
1937         # invoke the editor
1938         if "P4EDITOR" in os.environ and (os.environ.get("P4EDITOR") != ""):
1939             editor = os.environ.get("P4EDITOR")
1940         else:
1941             editor = read_pipe("git var GIT_EDITOR").strip()
1942         system(["sh", "-c", ('%s "$@"' % editor), editor, template_file])
1943
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"):
1947             return True
1948
1949         # modification time updated means user saved the file
1950         if os.stat(template_file).st_mtime > mtime:
1951             return True
1952
1953         response = prompt("Submit template unchanged. Submit anyway? [y]es, [n]o (skip this patch) ")
1954         if response == 'y':
1955             return True
1956         if response == 'n':
1957             return False
1958
1959     def get_diff_description(self, editedFiles, filesToAdd, symlinks):
1960         # diff
1961         if "P4DIFF" in os.environ:
1962             del(os.environ["P4DIFF"])
1963         diff = ""
1964         for editedFile in editedFiles:
1965             diff += p4_read_pipe(['diff', '-du',
1966                                   wildcard_encode(editedFile)])
1967
1968         # new file diff
1969         newdiff = ""
1970         for newFile in filesToAdd:
1971             newdiff += "==== new file ====\n"
1972             newdiff += "--- /dev/null\n"
1973             newdiff += "+++ %s\n" % newFile
1974
1975             is_link = os.path.islink(newFile)
1976             expect_link = newFile in symlinks
1977
1978             if is_link and expect_link:
1979                 newdiff += "+%s\n" % os.readlink(newFile)
1980             else:
1981                 f = open(newFile, "r")
1982                 for line in f.readlines():
1983                     newdiff += "+" + line
1984                 f.close()
1985
1986         return (diff + newdiff).replace('\r\n', '\n')
1987
1988     def applyCommit(self, id):
1989         """Apply one commit, return True if it succeeded."""
1990
1991         print("Applying", read_pipe(["git", "show", "-s",
1992                                      "--format=format:%h %s", id]))
1993
1994         (p4User, gitEmail) = self.p4UserForCommit(id)
1995
1996         diff = read_pipe_lines("git diff-tree -r %s \"%s^\" \"%s\"" % (self.diffOpts, id, id))
1997         filesToAdd = set()
1998         filesToChangeType = set()
1999         filesToDelete = set()
2000         editedFiles = set()
2001         pureRenameCopy = set()
2002         symlinks = set()
2003         filesToChangeExecBit = {}
2004         all_files = list()
2005
2006         for line in diff:
2007             diff = parseDiffTreeEntry(line)
2008             modifier = diff['status']
2009             path = diff['src']
2010             all_files.append(path)
2011
2012             if modifier == "M":
2013                 p4_edit(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)
2022
2023                 dst_mode = int(diff['dst_mode'], 8)
2024                 if dst_mode == 0o120000:
2025                     symlinks.add(path)
2026
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']:
2037                     p4_edit(dest)
2038                     pureRenameCopy.discard(dest)
2039                 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
2040                     p4_edit(dest)
2041                     pureRenameCopy.discard(dest)
2042                     filesToChangeExecBit[dest] = diff['dst_mode']
2043                 if self.isWindows:
2044                     # turn off read-only attribute
2045                     os.chmod(dest, stat.S_IWRITE)
2046                 os.unlink(dest)
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)
2054                 else:
2055                     p4_integrate(src, dest)
2056                     if diff['src_sha1'] != diff['dst_sha1']:
2057                         p4_edit(dest)
2058                     else:
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:
2065                     if self.isWindows:
2066                         os.chmod(dest, stat.S_IWRITE)
2067                     os.unlink(dest)
2068                     filesToDelete.add(src)
2069                 editedFiles.add(dest)
2070             elif modifier == "T":
2071                 filesToChangeType.add(path)
2072             else:
2073                 die("unknown modifier %s for %s" % (modifier, path))
2074
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
2080
2081         if verbose:
2082             print("TryPatch: %s" % tryPatchCmd)
2083
2084         if os.system(tryPatchCmd) != 0:
2085             fixed_rcs_keywords = False
2086             patch_succeeded = False
2087             print("Unfortunately applying the change failed!")
2088
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"):
2092                 file = None
2093                 pattern = None
2094                 kwfiles = {}
2095                 for file in editedFiles | filesToDelete:
2096                     # did this file's delta contain RCS keywords?
2097                     pattern = p4_keywords_regexp_for_file(file)
2098
2099                     if pattern:
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):
2104                                 if verbose:
2105                                     print("got keyword match on %s in %s in %s" % (pattern, line, file))
2106                                 kwfiles[file] = pattern
2107                                 break
2108
2109                 for file in kwfiles:
2110                     if verbose:
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
2118
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")
2124
2125         if not patch_succeeded:
2126             for f in editedFiles:
2127                 p4_revert(f)
2128             return False
2129
2130         #
2131         # Apply the patch for real, and do add/delete/+x handling.
2132         #
2133         system(applyPatchCmd)
2134
2135         for f in filesToChangeType:
2136             p4_edit(f, "-t", "auto")
2137         for f in filesToAdd:
2138             p4_add(f)
2139         for f in filesToDelete:
2140             p4_revert(f)
2141             p4_delete(f)
2142
2143         # Set/clear executable bits
2144         for f in filesToChangeExecBit.keys():
2145             mode = filesToChangeExecBit[f]
2146             setP4ExecBit(f, mode)
2147
2148         update_shelve = 0
2149         if len(self.update_shelve) > 0:
2150             update_shelve = self.update_shelve.pop(0)
2151             p4_reopen_in_change(update_shelve, all_files)
2152
2153         #
2154         # Build p4 change description, starting with the contents
2155         # of the git commit message.
2156         #
2157         logMessage = extractLogMessageFromGitCommit(id)
2158         logMessage = logMessage.strip()
2159         (logMessage, jobs) = self.separate_jobs_from_description(logMessage)
2160
2161         template = self.prepareSubmitTemplate(update_shelve)
2162         submitTemplate = self.prepareLogMessage(template, logMessage, jobs)
2163
2164         if self.preserveUser:
2165            submitTemplate += "\n######## Actual user %s, modified after commit\n" % p4User
2166
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"
2171
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)
2176
2177         (handle, fileName) = tempfile.mkstemp()
2178         tmpFile = os.fdopen(handle, "w+b")
2179         if self.isWindows:
2180             submitTemplate = submitTemplate.replace("\n", "\r\n")
2181         tmpFile.write(encode_text_stream(submitTemplate))
2182         tmpFile.close()
2183
2184         submitted = False
2185
2186         try:
2187             # Allow the hook to edit the changelist text before presenting it
2188             # to the user.
2189             if not run_git_hook("p4-prepare-changelist", [fileName]):
2190                 return False
2191
2192             if self.prepare_p4_only:
2193                 #
2194                 # Leave the p4 tree prepared, and the submit template around
2195                 # and let the user decide what to do next
2196                 #
2197                 submitted = True
2198                 print("")
2199                 print("P4 workspace prepared for submission.")
2200                 print("To submit or revert, go to client workspace")
2201                 print("  " + self.clientPath)
2202                 print("")
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)
2207
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.")
2212                 if pureRenameCopy:
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:
2216                         print("  " + f)
2217
2218                 print("")
2219                 print("To revert the changes, use \"p4 revert ...\", and delete")
2220                 print("the submit template file \"%s\"" % fileName)
2221                 if filesToAdd:
2222                     print("Since the commit adds new files, they must be deleted:")
2223                     for f in filesToAdd:
2224                         print("  " + f)
2225                 print("")
2226                 sys.stdout.flush()
2227                 return True
2228
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.")
2233                         sys.stdout.flush()
2234                         return False
2235
2236                 # read the edited message and submit
2237                 tmpFile = open(fileName, "rb")
2238                 message = decode_text_stream(tmpFile.read())
2239                 tmpFile.close()
2240                 if self.isWindows:
2241                     message = message.replace("\r\n", "\n")
2242                 if message.find(separatorLine) != -1:
2243                     submitTemplate = message[:message.index(separatorLine)]
2244                 else:
2245                     submitTemplate = message
2246
2247                 if len(submitTemplate.strip()) == 0:
2248                     print("Changelist is empty, aborting this changelist.")
2249                     sys.stdout.flush()
2250                     return False
2251
2252                 if update_shelve:
2253                     p4_write_pipe(['shelve', '-r', '-i'], submitTemplate)
2254                 elif self.shelve:
2255                     p4_write_pipe(['shelve', '-i'], submitTemplate)
2256                 else:
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:
2261                         p4_sync(f, "-f")
2262
2263                 if self.preserveUser:
2264                     if p4User:
2265                         # Get last changelist number. Cannot easily get it from
2266                         # the submit command output as the output is
2267                         # unmarshalled.
2268                         changelist = self.lastP4Changelist()
2269                         self.modifyChangelistUser(changelist, p4User)
2270
2271                 submitted = True
2272
2273                 run_git_hook("p4-post-changelist")
2274         finally:
2275             # Revert changes if we skip this patch
2276             if not submitted or self.shelve:
2277                 if self.shelve:
2278                     print ("Reverting shelved files.")
2279                 else:
2280                     print ("Submission cancelled, undoing p4 changes.")
2281                 sys.stdout.flush()
2282                 for f in editedFiles | filesToDelete:
2283                     p4_revert(f)
2284                 for f in filesToAdd:
2285                     p4_revert(f)
2286                     os.remove(f)
2287
2288             if not self.prepare_p4_only:
2289                 os.remove(fileName)
2290         return submitted
2291
2292     # Export git tags as p4 labels. Create a p4 label and then tag
2293     # with that.
2294     def exportGitTags(self, gitTags):
2295         validLabelRegexp = gitConfig("git-p4.labelExportRegexp")
2296         if len(validLabelRegexp) == 0:
2297             validLabelRegexp = defaultLabelRegexp
2298         m = re.compile(validLabelRegexp)
2299
2300         for name in gitTags:
2301
2302             if not m.match(name):
2303                 if verbose:
2304                     print("tag %s does not match regexp %s" % (name, validLabelRegexp))
2305                 continue
2306
2307             # Get the p4 commit this corresponds to
2308             logMessage = extractLogMessageFromGitCommit(name)
2309             values = extractSettingsGitLog(logMessage)
2310
2311             if 'change' not in values:
2312                 # a tag pointing to something not sent to p4; ignore
2313                 if verbose:
2314                     print("git tag %s does not give a p4 commit" % name)
2315                 continue
2316             else:
2317                 changelist = values['change']
2318
2319             # Get the tag details.
2320             inHeader = True
2321             isAnnotated = False
2322             body = []
2323             for l in read_pipe_lines(["git", "cat-file", "-p", name]):
2324                 l = l.strip()
2325                 if inHeader:
2326                     if re.match(r'tag\s+', l):
2327                         isAnnotated = True
2328                     elif re.match(r'\s*$', l):
2329                         inHeader = False
2330                         continue
2331                 else:
2332                     body.append(l)
2333
2334             if not isAnnotated:
2335                 body = ["lightweight tag imported by git p4\n"]
2336
2337             # Create the label - use the same view as the client spec we are using
2338             clientSpec = getClientSpec()
2339
2340             labelTemplate  = "Label: %s\n" % name
2341             labelTemplate += "Description:\n"
2342             for b in body:
2343                 labelTemplate += "\t" + b + "\n"
2344             labelTemplate += "View:\n"
2345             for depot_side in clientSpec.mappings:
2346                 labelTemplate += "\t%s\n" % depot_side
2347
2348             if self.dry_run:
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)
2353             else:
2354                 p4_write_pipe(["label", "-i"], labelTemplate)
2355
2356                 # Use the label
2357                 p4_system(["tag", "-l", name] +
2358                           ["%s@%s" % (depot_side, changelist) for depot_side in clientSpec.mappings])
2359
2360                 if verbose:
2361                     print("created p4 label for tag %s" % name)
2362
2363     def run(self, args):
2364         if len(args) == 0:
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)
2370         else:
2371             return False
2372
2373         for i in self.update_shelve:
2374             if i <= 0:
2375                 sys.exit("invalid changelist %d" % i)
2376
2377         if self.master:
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)
2381
2382         [upstream, settings] = findUpstreamBranchPoint()
2383         self.depotPath = settings['depot-paths'][0]
2384         if len(self.origin) == 0:
2385             self.origin = upstream
2386
2387         if len(self.update_shelve) > 0:
2388             self.shelve = True
2389
2390         if self.preserveUser:
2391             if not self.canChangeChangelists():
2392                 die("Cannot preserve user names without p4 super-user or admin permissions")
2393
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")
2397             if val:
2398                 if val not in self.conflict_behavior_choices:
2399                     die("Invalid value '%s' for config git-p4.conflict" % val)
2400             else:
2401                 val = "ask"
2402             self.conflict_behavior = val
2403
2404         if self.verbose:
2405             print("Origin branch is " + self.origin)
2406
2407         if len(self.depotPath) == 0:
2408             print("Internal error: cannot locate perforce depot path from existing branches")
2409             sys.exit(128)
2410
2411         self.useClientSpec = False
2412         if gitConfigBool("git-p4.useclientspec"):
2413             self.useClientSpec = True
2414         if self.useClientSpec:
2415             self.clientSpecDirs = getClientSpec()
2416
2417         # Check for the existence of P4 branches
2418         branchesDetected = (len(p4BranchesInGit().keys()) > 1)
2419
2420         if self.useClientSpec and not branchesDetected:
2421             # all files are relative to the client spec
2422             self.clientPath = getClientRoot()
2423         else:
2424             self.clientPath = p4Where(self.depotPath)
2425
2426         if self.clientPath == "":
2427             die("Error: Cannot locate perforce checkout of %s in client view" % self.depotPath)
2428
2429         print("Perforce checkout for depot path %s located at %s" % (self.depotPath, self.clientPath))
2430         self.oldWorkingDirectory = os.getcwd()
2431
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)
2437
2438         chdir(self.clientPath, is_client_path=True)
2439         if self.dry_run:
2440             print("Would synchronize p4 checkout in %s" % self.clientPath)
2441         else:
2442             print("Synchronizing p4 checkout...")
2443             if new_client_dir:
2444                 # old one was destroyed, and maybe nobody told p4
2445                 p4_sync("...", "-f")
2446             else:
2447                 p4_sync("...")
2448         self.check()
2449
2450         commits = []
2451         if self.master:
2452             committish = self.master
2453         else:
2454             committish = 'HEAD'
2455
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())
2461                 commits.reverse()
2462             else:
2463                 commits.append(self.commit)
2464         else:
2465             for line in read_pipe_lines(["git", "rev-list", "--no-merges", "%s..%s" % (self.origin, committish)]):
2466                 commits.append(line.strip())
2467             commits.reverse()
2468
2469         if self.preserveUser or gitConfigBool("git-p4.skipUserNameCheck"):
2470             self.checkAuthorship = False
2471         else:
2472             self.checkAuthorship = True
2473
2474         if self.preserveUser:
2475             self.checkValidP4Users(commits)
2476
2477         #
2478         # Build up a set of options to be passed to diff when
2479         # submitting each commit to p4.
2480         #
2481         if self.detectRenames:
2482             # command-line -M arg
2483             self.diffOpts = "-M"
2484         else:
2485             # If not explicitly set check the config variable
2486             detectRenames = gitConfig("git-p4.detectRenames")
2487
2488             if detectRenames.lower() == "false" or detectRenames == "":
2489                 self.diffOpts = ""
2490             elif detectRenames.lower() == "true":
2491                 self.diffOpts = "-M"
2492             else:
2493                 self.diffOpts = "-M%s" % detectRenames
2494
2495         # no command-line arg for -C or --find-copies-harder, just
2496         # config variables
2497         detectCopies = gitConfig("git-p4.detectCopies")
2498         if detectCopies.lower() == "false" or detectCopies == "":
2499             pass
2500         elif detectCopies.lower() == "true":
2501             self.diffOpts += " -C"
2502         else:
2503             self.diffOpts += " -C%s" % detectCopies
2504
2505         if gitConfigBool("git-p4.detectCopiesHarder"):
2506             self.diffOpts += " --find-copies-harder"
2507
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))
2512
2513         if not self.no_verify:
2514             try:
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.")
2519                     sys.exit(1)
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) )
2523                 sys.exit(1)
2524
2525         #
2526         # Apply the commits, one at a time.  On failure, ask if should
2527         # continue to try the rest of the patches, or quit.
2528         #
2529         if self.dry_run:
2530             print("Would apply")
2531         applied = []
2532         last = len(commits) - 1
2533         for i, commit in enumerate(commits):
2534             if self.dry_run:
2535                 print(" ", read_pipe(["git", "show", "-s",
2536                                       "--format=format:%h %s", commit]))
2537                 ok = True
2538             else:
2539                 ok = self.applyCommit(commit)
2540             if ok:
2541                 applied.append(commit)
2542                 if self.prepare_p4_only:
2543                     if i < last:
2544                         print("Processing only the first commit due to option" \
2545                                 " --prepare-p4-only")
2546                     break
2547             else:
2548                 if i < last:
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":
2554                         response = "s"
2555                     elif self.conflict_behavior == "quit":
2556                         response = "q"
2557                     else:
2558                         die("Unknown conflict_behavior '%s'" %
2559                             self.conflict_behavior)
2560
2561                     if response == "s":
2562                         print("Skipping this commit, but applying the rest")
2563                     if response == "q":
2564                         print("Quitting")
2565                         break
2566
2567         chdir(self.oldWorkingDirectory)
2568         shelved_applied = "shelved" if self.shelve else "applied"
2569         if self.dry_run:
2570             pass
2571         elif self.prepare_p4_only:
2572             pass
2573         elif len(commits) == len(applied):
2574             print("All commits {0}!".format(shelved_applied))
2575
2576             sync = P4Sync()
2577             if self.branch:
2578                 sync.branch = self.branch
2579             if self.disable_p4sync:
2580                 sync.sync_origin_only()
2581             else:
2582                 sync.run([])
2583
2584                 if not self.disable_rebase:
2585                     rebase = P4Rebase()
2586                     rebase.rebase()
2587
2588         else:
2589             if len(applied) == 0:
2590                 print("No commits {0}.".format(shelved_applied))
2591             else:
2592                 print("{0} only the commits marked with '*':".format(shelved_applied.capitalize()))
2593                 for c in commits:
2594                     if c in applied:
2595                         star = "*"
2596                     else:
2597                         star = " "
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.")
2601
2602         if gitConfigBool("git-p4.exportLabels"):
2603             self.exportLabels = True
2604
2605         if self.exportLabels:
2606             p4Labels = getP4Labels(self.depotPath)
2607             gitTags = getGitTags()
2608
2609             missingGitTags = gitTags - p4Labels
2610             self.exportGitTags(missingGitTags)
2611
2612         # exit with error unless everything applied perfectly
2613         if len(commits) != len(applied):
2614                 sys.exit(1)
2615
2616         return True
2617
2618 class View(object):
2619     """Represent a p4 view ("p4 help views"), and map files in a
2620        repo according to the view."""
2621
2622     def __init__(self, client_name):
2623         self.mappings = []
2624         self.client_prefix = "//%s/" % client_name
2625         # cache results of "p4 where" to lookup client file locations
2626         self.client_spec_path_cache = {}
2627
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."""
2632
2633         # Split the view line into exactly two words.  P4 enforces
2634         # structure on these lines that simplifies this quite a bit.
2635         #
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.
2643         #
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
2652         else:
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
2658
2659         # prefix + means overlay on previous mapping
2660         if depot_side.startswith("+"):
2661             depot_side = depot_side[1:]
2662
2663         # prefix - means exclude this path, leave out of mappings
2664         exclude = False
2665         if depot_side.startswith("-"):
2666             exclude = True
2667             depot_side = depot_side[1:]
2668
2669         if not exclude:
2670             self.mappings.append(depot_side)
2671
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):]
2678
2679     def update_client_spec_path_cache(self, files):
2680         """ Caching file paths by "p4 where" batch query """
2681
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]
2684
2685         if len(fileArgs) == 0:
2686             return  # All files in cache
2687
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"
2692                 continue
2693             if "clientFile" not in res:
2694                 die("No clientFile in 'p4 where' output")
2695             if "unmap" in res:
2696                 # it will list all of them, but only one not unmap-ped
2697                 continue
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"])
2702
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''
2710
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."""
2715
2716         if gitConfigBool("core.ignorecase"):
2717             depot_path = depot_path.lower()
2718
2719         if depot_path in self.client_spec_path_cache:
2720             return self.client_spec_path_cache[depot_path]
2721
2722         die( "Error: %s is not found in client spec path" % depot_path )
2723         return ""
2724
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)]
2729
2730 class P4Sync(Command, P4UserMap):
2731
2732     def __init__(self):
2733         Command.__init__(self)
2734         P4UserMap.__init__(self)
2735         self.options = [
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"),
2755         ]
2756         self.description = """Imports from Perforce into a git repository.\n
2757     example:
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
2761
2762     (a ... is not needed in the path p4 specification, it's added implicitly)"""
2763
2764         self.usage += " //depot/path[@revRange]"
2765         self.silent = False
2766         self.createdBranches = set()
2767         self.committedChanges = set()
2768         self.branch = ""
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
2788
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)
2793             )
2794
2795         if gitConfig("git-p4.syncFromOrigin") == "false":
2796             self.syncWithOrigin = False
2797
2798         self.depotPaths = []
2799         self.changeRange = ""
2800         self.previousDepotPaths = []
2801         self.hasOrigin = False
2802
2803         # map from branch depot path to parent branch
2804         self.knownBranches = {}
2805         self.initialParents = {}
2806
2807         self.tz = b"%+03d%02d" % (- time.timezone / 3600, ((- time.timezone % 3600) / 60))
2808         self.labels = {}
2809
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()
2816         if self.verbose:
2817             print("checkpoint finished: " + out)
2818
2819     def isPathWanted(self, path):
2820         for p in self.cloneExclude:
2821             if p.endswith("/"):
2822                 if p4PathStartsWith(path, p):
2823                     return False
2824             # "-//depot/file1" without a trailing "/" should only exclude "file1", but not "file111" or "file1_dir/file2"
2825             elif path.lower() == p.lower():
2826                 return False
2827         for p in self.depotPaths:
2828             if p4PathStartsWith(path, decode_path(p)):
2829                 return True
2830         return False
2831
2832     def extractFilesFromCommit(self, commit, shelved=False, shelved_cl = 0):
2833         files = []
2834         fnum = 0
2835         while "depotFile%s" % fnum in commit:
2836             path =  commit["depotFile%s" % fnum]
2837             found = self.isPathWanted(decode_path(path))
2838             if not found:
2839                 fnum = fnum + 1
2840                 continue
2841
2842             file = {}
2843             file["path"] = path
2844             file["rev"] = commit["rev%s" % fnum]
2845             file["action"] = commit["action%s" % fnum]
2846             file["type"] = commit["type%s" % fnum]
2847             if shelved:
2848                 file["shelved_cl"] = int(shelved_cl)
2849             files.append(file)
2850             fnum = fnum + 1
2851         return files
2852
2853     def extractJobsFromCommit(self, commit):
2854         jobs = []
2855         jnum = 0
2856         while "job%s" % jnum in commit:
2857             job = commit["job%s" % jnum]
2858             jobs.append(job)
2859             jnum = jnum + 1
2860         return jobs
2861
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."""
2867
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:]
2876
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):]
2884
2885         else:
2886             for p in prefixes:
2887                 if p4PathStartsWith(path, p):
2888                     path = path[len(p):]
2889                     break
2890
2891         path = wildcard_decode(path)
2892         return path
2893
2894     def splitFilesIntoBranches(self, commit):
2895         """Look at each depotFile in the commit to figure out to what
2896            branch it belongs."""
2897
2898         if self.clientSpecDirs:
2899             files = self.extractFilesFromCommit(commit)
2900             self.clientSpecDirs.update_client_spec_path_cache(files)
2901
2902         branches = {}
2903         fnum = 0
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)
2908             if not found:
2909                 fnum = fnum + 1
2910                 continue
2911
2912             file = {}
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]
2917             fnum = fnum + 1
2918
2919             # start with the full relative path where this file would
2920             # go in a p4 client
2921             if self.useClientSpec:
2922                 relPath = decode_path(self.clientSpecDirs.map_in_client(path))
2923             else:
2924                 relPath = self.stripRepoPath(path, self.depotPaths)
2925
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)
2933                     break
2934
2935         return branches
2936
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))
2940         for d in contents:
2941             self.gitStream.write(d)
2942         self.gitStream.write('\n')
2943
2944     def encodeWithUTF8(self, path):
2945         try:
2946             path.decode('ascii')
2947         except:
2948             encoding = 'utf8'
2949             if gitConfig('git-p4.pathEncoding'):
2950                 encoding = gitConfig('git-p4.pathEncoding')
2951             path = path.decode(encoding, 'replace').encode('utf8', 'replace')
2952             if self.verbose:
2953                 print('Path with non-ASCII characters detected. Used %s to encode: %s ' % (encoding, path))
2954         return path
2955
2956     # output one file from the P4 stream
2957     # - helper for streamP4Files
2958
2959     def streamOneP4File(self, file, contents):
2960         file_path = file['depotFile']
2961         relPath = self.stripRepoPath(decode_path(file_path), self.branchPrefixes)
2962
2963         if verbose:
2964             if 'fileSize' in self.stream_file:
2965                 size = int(self.stream_file['fileSize'])
2966             else:
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))
2969             sys.stdout.flush()
2970
2971         (type_base, type_mods) = split_p4_type(file["type"])
2972
2973         git_mode = "100644"
2974         if "x" in type_mods:
2975             git_mode = "100755"
2976         if type_base == "symlink":
2977             git_mode = "120000"
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)
2981             if not data:
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)
2987                 return
2988             elif data[-1] == '\n':
2989                 contents = [data[:-1]]
2990             else:
2991                 contents = [data]
2992
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.
2999             #
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.
3003             #
3004             try:
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'
3009                 else:
3010                     raise e
3011             else:
3012                 if p4_version_string().find('/NT') >= 0:
3013                     text = text.replace(b'\r\n', b'\n')
3014                 contents = [ text ]
3015
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.
3022             #
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'])
3027             return
3028
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)
3032         if pattern:
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) ]
3037
3038         if self.largeFileSystem:
3039             (git_mode, contents) = self.largeFileSystem.processContent(git_mode, relPath, contents)
3040
3041         self.writeToGitStream(git_mode, relPath, contents)
3042
3043     def streamOneP4Deletion(self, file):
3044         relPath = self.stripRepoPath(decode_path(file['path']), self.branchPrefixes)
3045         if verbose:
3046             sys.stdout.write("delete %s\n" % relPath)
3047             sys.stdout.flush()
3048         self.gitStream.write(encode_text_stream(u'D {}\n'.format(relPath)))
3049
3050         if self.largeFileSystem and self.largeFileSystem.isLargeFile(relPath):
3051             self.largeFileSystem.removeLargeFile(relPath)
3052
3053     # handle another chunk of streaming data
3054     def streamP4FilesCb(self, marshalled):
3055
3056         # catch p4 errors and complain
3057         err = None
3058         if "code" in marshalled:
3059             if marshalled["code"] == "error":
3060                 if "data" in marshalled:
3061                     err = marshalled["data"].rstrip()
3062
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
3068                 )
3069
3070         if err:
3071             f = None
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()
3082             if f:
3083                 die("Error from p4 print for %s: %s" % (f, err))
3084             else:
3085                 die("Error from p4 print: %s" % err)
3086
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
3093
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():
3097             if k == 'data':
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'])
3102             else:
3103                 self.stream_file[k] = marshalled[k]
3104
3105         if (verbose and
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"])
3110             if size > 0:
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)))
3113                 sys.stdout.flush()
3114
3115         self.stream_have_file_info = True
3116
3117     # Stream directly from "p4 files" into "git fast-import"
3118     def streamP4Files(self, files):
3119         filesForCommit = []
3120         filesToRead = []
3121         filesToDelete = []
3122
3123         for f in files:
3124             filesForCommit.append(f)
3125             if f['action'] in self.delete_actions:
3126                 filesToDelete.append(f)
3127             else:
3128                 filesToRead.append(f)
3129
3130         # deleted files...
3131         for f in filesToDelete:
3132             self.streamOneP4Deletion(f)
3133
3134         if len(filesToRead) > 0:
3135             self.stream_file = {}
3136             self.stream_contents = []
3137             self.stream_have_file_info = False
3138
3139             # curry self argument
3140             def streamP4FilesCbSelf(entry):
3141                 self.streamP4FilesCb(entry)
3142
3143             fileArgs = []
3144             for f in filesToRead:
3145                 if 'shelved_cl' in f:
3146                     # Handle shelved CLs using the "p4 print file@=N" syntax to print
3147                     # the contents
3148                     fileArg = f['path'] + encode_text_stream('@={}'.format(f['shelved_cl']))
3149                 else:
3150                     fileArg = f['path'] + encode_text_stream('#{}'.format(f['rev']))
3151
3152                 fileArgs.append(fileArg)
3153
3154             p4CmdList(["-x", "-", "print"],
3155                       stdin=fileArgs,
3156                       cb=streamP4FilesCbSelf)
3157
3158             # do the last chunk
3159             if 'depotFile' in self.stream_file:
3160                 self.streamOneP4File(self.stream_file, self.stream_contents)
3161
3162     def make_email(self, userid):
3163         if userid in self.users:
3164             return self.users[userid]
3165         else:
3166             return b"%s <a@b>" % userid
3167
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>"
3171         """
3172
3173         if verbose:
3174             print("writing tag %s for commit %s" % (labelName, commit))
3175         gitStream.write("tag %s\n" % labelName)
3176         gitStream.write("from %s\n" % commit)
3177
3178         if 'Owner' in labelDetails:
3179             owner = labelDetails["Owner"]
3180         else:
3181             owner = None
3182
3183         # Try to use the owner of the p4 label, or failing that,
3184         # the current p4 user id.
3185         if owner:
3186             email = self.make_email(owner)
3187         else:
3188             email = self.make_email(self.p4UserId())
3189         tagger = b"%s %s %s" % (email, epoch, self.tz)
3190
3191         gitStream.write(b"tagger %s\n" % tagger)
3192
3193         print("labelDetails=",labelDetails)
3194         if 'Description' in labelDetails:
3195             description = labelDetails['Description']
3196         else:
3197             description = 'Label from git p4'
3198
3199         gitStream.write("data %d\n" % len(description))
3200         gitStream.write(description)
3201         gitStream.write("\n")
3202
3203     def inClientSpec(self, path):
3204         if not self.clientSpecDirs:
3205             return True
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))
3209         return inClientSpec
3210
3211     def hasBranchPrefix(self, path):
3212         if not self.branchPrefixes:
3213             return True
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))
3218         return hasPrefix
3219
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.
3225         #
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]
3231         to_check = set()
3232         for f in deleted:
3233             path = decode_path(f['path'])
3234             to_check.add(path + '/...')
3235             while True:
3236                 path = path.rsplit("/", 1)[0]
3237                 if path == "/" or path in to_check:
3238                     break
3239                 to_check.add(path)
3240         to_check = ['%s@%s' % (wildcard_encode(p), change) for p in to_check
3241             if self.hasBranchPrefix(p)]
3242         if to_check:
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':
3247                     continue
3248                 if record['headAction'] in self.delete_actions:
3249                     continue
3250                 files.append({
3251                     'action': 'add',
3252                     'path': record['depotFile'],
3253                     'rev': record['headRev'],
3254                     'type': record['headType']})
3255
3256     def commit(self, details, files, branch, parent = "", allow_empty=False):
3257         epoch = details["time"]
3258         author = details["user"]
3259         jobs = self.extractJobsFromCommit(details)
3260
3261         if self.verbose:
3262             print('commit into {0}'.format(branch))
3263
3264         files = [f for f in files
3265             if self.hasBranchPrefix(decode_path(f['path']))]
3266         self.findShadowedFiles(files, details['change'])
3267
3268         if self.clientSpecDirs:
3269             self.clientSpecDirs.update_client_spec_path_cache(files)
3270
3271         files = [f for f in files if self.inClientSpec(decode_path(f['path']))]
3272
3273         if gitConfigBool('git-p4.keepEmptyCommits'):
3274             allow_empty = True
3275
3276         if not files and not allow_empty:
3277             print('Ignoring revision {0} as it would produce an empty commit.'
3278                 .format(details['change']))
3279             return
3280
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)
3287
3288         self.gitStream.write(b"committer %s\n" % committer)
3289
3290         self.gitStream.write("data <<EOT\n")
3291         self.gitStream.write(details["desc"])
3292         if len(jobs) > 0:
3293             self.gitStream.write("\nJobs: %s" % (' '.join(jobs)))
3294
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")
3301
3302         self.gitStream.write("EOT\n\n")
3303
3304         if len(parent) > 0:
3305             if self.verbose:
3306                 print("parent %s" % parent)
3307             self.gitStream.write("from %s\n" % parent)
3308
3309         self.streamP4Files(files)
3310         self.gitStream.write("\n")
3311
3312         change = int(details["change"])
3313
3314         if change in self.labels:
3315             label = self.labels[change]
3316             labelDetails = label[0]
3317             labelRevisions = label[1]
3318             if self.verbose:
3319                 print("Change %s is labelled %s" % (change, labelDetails))
3320
3321             files = p4CmdList(["files"] + ["%s...@%s" % (p, change)
3322                                                 for p in self.branchPrefixes])
3323
3324             if len(files) == len(labelRevisions):
3325
3326                 cleanedFiles = {}
3327                 for info in files:
3328                     if info["action"] in self.delete_actions:
3329                         continue
3330                     cleanedFiles[info["depotFile"]] = info["rev"]
3331
3332                 if cleanedFiles == labelRevisions:
3333                     self.streamTag(self.gitStream, 'tag_%s' % labelDetails['label'], labelDetails, branch, epoch)
3334
3335                 else:
3336                     if not self.silent:
3337                         print("Tag %s does not match with change %s: files do not match."
3338                                % (labelDetails["label"], change))
3339
3340             else:
3341                 if not self.silent:
3342                     print("Tag %s does not match with change %s: file count is different."
3343                            % (labelDetails["label"], change))
3344
3345     # Build a dictionary of changelists and labels, for "detect-labels" option.
3346     def getLabels(self):
3347         self.labels = {}
3348
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)
3352
3353         for output in l:
3354             label = output["label"]
3355             revisions = {}
3356             newestChange = 0
3357             if self.verbose:
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
3366
3367             self.labels[newestChange] = [output, revisions]
3368
3369         if self.verbose:
3370             print("Label changes: %s" % self.labels.keys())
3371
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
3375     # just ignore.
3376     def importP4Labels(self, stream, p4Labels):
3377         if verbose:
3378             print("import p4 labels: " + ' '.join(p4Labels))
3379
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)
3385
3386         for name in p4Labels:
3387             commitFound = False
3388
3389             if not m.match(name):
3390                 if verbose:
3391                     print("label %s does not match regexp %s" % (name,validLabelRegexp))
3392                 continue
3393
3394             if name in ignoredP4Labels:
3395                 continue
3396
3397             labelDetails = p4CmdList(['label', "-o", name])[0]
3398
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])
3402
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
3408                     commitFound = True
3409                 else:
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))
3414                     else:
3415                         commitFound = True
3416                         gitCommit = gitCommit.strip()
3417
3418                 if commitFound:
3419                     # Convert from p4 time format
3420                     try:
3421                         tmwhen = time.strptime(labelDetails['Update'], "%Y/%m/%d %H:%M:%S")
3422                     except ValueError:
3423                         print("Could not convert label time %s" % labelDetails['Update'])
3424                         tmwhen = 1
3425
3426                     when = b"%i" % int(time.mktime(tmwhen))
3427                     self.streamTag(stream, name, labelDetails, gitCommit, when)
3428                     if verbose:
3429                         print("p4 label %s mapped to git commit %s" % (name, gitCommit))
3430             else:
3431                 if verbose:
3432                     print("Label %s has no changelists - possibly deleted?" % name)
3433
3434             if not commitFound:
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])
3440
3441     def guessProjectName(self):
3442         for p in self.depotPaths:
3443             if p.endswith("/"):
3444                 p = p[:-1]
3445             p = p[p.strip().rfind("/") + 1:]
3446             if not p.endswith("/"):
3447                p += "/"
3448             return p
3449
3450     def getBranchMapping(self):
3451         lostAndFoundBranches = set()
3452
3453         user = gitConfig("git-p4.branchUser")
3454         if len(user) > 0:
3455             command = "branches -u %s" % user
3456         else:
3457             command = "branches"
3458
3459         for info in p4CmdList(command):
3460             details = p4Cmd(["branch", "-o", info["branch"]])
3461             viewIdx = 0
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("/..."):
3467                     continue
3468                 source = paths[0]
3469                 destination = paths[1]
3470                 ## HACK
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]
3474
3475                     if destination in self.knownBranches:
3476                         if not self.silent:
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))
3479                         continue
3480
3481                     self.knownBranches[destination] = source
3482
3483                     lostAndFoundBranches.discard(destination)
3484
3485                     if source not in self.knownBranches:
3486                         lostAndFoundBranches.add(source)
3487
3488         # Perforce does not strictly require branches to be defined, so we also
3489         # check git config for a branch list.
3490         #
3491         # Example of branch definition in git config file:
3492         # [git-p4]
3493         #   branchList=main:branchA
3494         #   branchList=main:branchB
3495         #   branchList=branchA:branchC
3496         configBranches = gitConfigList("git-p4.branchList")
3497         for branch in configBranches:
3498             if branch:
3499                 (source, destination) = branch.split(":")
3500                 self.knownBranches[destination] = source
3501
3502                 lostAndFoundBranches.discard(destination)
3503
3504                 if source not in self.knownBranches:
3505                     lostAndFoundBranches.add(source)
3506
3507
3508         for branch in lostAndFoundBranches:
3509             self.knownBranches[branch] = branch
3510
3511     def getBranchMappingFromGitBranches(self):
3512         branches = p4BranchesInGit(self.importIntoRemotes)
3513         for branch in branches.keys():
3514             if branch == "master":
3515                 branch = "main"
3516             else:
3517                 branch = branch[len(self.projectName):]
3518             self.knownBranches[branch] = branch
3519
3520     def updateOptionDict(self, d):
3521         option_keys = {}
3522         if self.keepRepoPath:
3523             option_keys['keepRepoPath'] = 1
3524
3525         d["options"] = ' '.join(sorted(option_keys.keys()))
3526
3527     def readOptions(self, d):
3528         self.keepRepoPath = ('options' in d
3529                              and ('keepRepoPath' in d['options']))
3530
3531     def gitRefForBranch(self, branch):
3532         if branch == "main":
3533             return self.refPrefix + "master"
3534
3535         if len(branch) <= 0:
3536             return branch
3537
3538         return self.refPrefix + self.projectName + branch
3539
3540     def gitCommitByP4Change(self, ref, change):
3541         if self.verbose:
3542             print("looking in ref " + ref + " for change %s using bisect..." % change)
3543
3544         earliestCommit = ""
3545         latestCommit = parseRevision(ref)
3546
3547         while True:
3548             if self.verbose:
3549                 print("trying: earliest %s latest %s" % (earliestCommit, latestCommit))
3550             next = read_pipe("git rev-list --bisect %s %s" % (latestCommit, earliestCommit)).strip()
3551             if len(next) == 0:
3552                 if self.verbose:
3553                     print("argh")
3554                 return ""
3555             log = extractLogMessageFromGitCommit(next)
3556             settings = extractSettingsGitLog(log)
3557             currentChange = int(settings['change'])
3558             if self.verbose:
3559                 print("current change %s" % currentChange)
3560
3561             if currentChange == change:
3562                 if self.verbose:
3563                     print("found %s" % next)
3564                 return next
3565
3566             if currentChange < change:
3567                 earliestCommit = "^%s" % next
3568             else:
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
3572
3573         return ""
3574
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:
3585             return False
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
3592
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
3599
3600         self.importChanges(changes)
3601         return True
3602
3603     def searchParent(self, parent, branch, target):
3604         parentFound = False
3605         for blob in read_pipe_lines(["git", "rev-list", "--reverse",
3606                                      "--no-merges", parent]):
3607             blob = blob.strip()
3608             if len(read_pipe(["git", "diff-tree", blob, target])) == 0:
3609                 parentFound = True
3610                 if self.verbose:
3611                     print("Found parent of %s in commit %s" % (branch, blob))
3612                 break
3613         if parentFound:
3614             return blob
3615         else:
3616             return None
3617
3618     def importChanges(self, changes, origin_revision=0):
3619         cnt = 1
3620         for change in changes:
3621             description = p4_describe(change)
3622             self.updateOptionDict(description)
3623
3624             if not self.silent:
3625                 sys.stdout.write("\rImporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
3626                 sys.stdout.flush()
3627             cnt = cnt + 1
3628
3629             try:
3630                 if self.detectBranches:
3631                     branches = self.splitFilesIntoBranches(description)
3632                     for branch in branches.keys():
3633                         ## HACK  --hwn
3634                         branchPrefix = self.depotPaths[0] + branch + "/"
3635                         self.branchPrefixes = [ branchPrefix ]
3636
3637                         parent = ""
3638
3639                         filesForCommit = branches[branch]
3640
3641                         if self.verbose:
3642                             print("branch is %s" % branch)
3643
3644                         self.updatedBranches.add(branch)
3645
3646                         if branch not in self.createdBranches:
3647                             self.createdBranches.add(branch)
3648                             parent = self.knownBranches[branch]
3649                             if parent == branch:
3650                                 parent = ""
3651                             else:
3652                                 fullBranch = self.projectName + branch
3653                                 if fullBranch not in self.p4BranchesInGit:
3654                                     if not self.silent:
3655                                         print("\n    Importing new branch %s" % fullBranch);
3656                                     if self.importNewBranch(branch, change - 1):
3657                                         parent = ""
3658                                         self.p4BranchesInGit.append(fullBranch)
3659                                     if not self.silent:
3660                                         print("\n    Resuming with change %s" % change);
3661
3662                                 if self.verbose:
3663                                     print("parent determined through known branches: %s" % parent)
3664
3665                         branch = self.gitRefForBranch(branch)
3666                         parent = self.gitRefForBranch(parent)
3667
3668                         if self.verbose:
3669                             print("looking for initial parent for %s; current parent is %s" % (branch, parent))
3670
3671                         if len(parent) == 0 and branch in self.initialParents:
3672                             parent = self.initialParents[branch]
3673                             del self.initialParents[branch]
3674
3675                         blob = None
3676                         if len(parent) > 0:
3677                             tempBranch = "%s/%d" % (self.tempBranchLocation, change)
3678                             if self.verbose:
3679                                 print("Creating temporary branch: " + tempBranch)
3680                             self.commit(description, filesForCommit, tempBranch)
3681                             self.tempBranches.append(tempBranch)
3682                             self.checkpoint()
3683                             blob = self.searchParent(parent, branch, tempBranch)
3684                         if blob:
3685                             self.commit(description, filesForCommit, branch, blob)
3686                         else:
3687                             if self.verbose:
3688                                 print("Parent of %s not found. Committing into head of %s" % (branch, parent))
3689                             self.commit(description, filesForCommit, branch, parent)
3690                 else:
3691                     files = self.extractFilesFromCommit(description)
3692                     self.commit(description, files, self.branch,
3693                                 self.initialParent)
3694                     # only needed once, to connect to the previous commit
3695                     self.initialParent = ""
3696             except IOError:
3697                 print(self.gitError.read())
3698                 sys.exit(1)
3699
3700     def sync_origin_only(self):
3701         if self.syncWithOrigin:
3702             self.hasOrigin = originP4BranchesExist()
3703             if self.hasOrigin:
3704                 if not self.silent:
3705                     print('Syncing with origin first, using "git fetch origin"')
3706                 system("git fetch origin")
3707
3708     def importHeadRevision(self, revision):
3709         print("Doing initial import of %s from revision %s into %s" % (' '.join(self.depotPaths), revision, self.branch))
3710
3711         details = {}
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
3716         newestRevision = 0
3717
3718         fileCnt = 0
3719         fileArgs = ["%s...%s" % (p,revision) for p in self.depotPaths]
3720
3721         for info in p4CmdList(["files"] + fileArgs):
3722
3723             if 'code' in info and info['code'] == 'error':
3724                 sys.stderr.write("p4 returned an error: %s\n"
3725                                  % info['data'])
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))
3730                 sys.exit(1)
3731             if 'p4ExitCode' in info:
3732                 sys.stderr.write("p4 exitcode: %s\n" % info['p4ExitCode'])
3733                 sys.exit(1)
3734
3735
3736             change = int(info["change"])
3737             if change > newestRevision:
3738                 newestRevision = change
3739
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
3743                 continue
3744
3745             for prop in ["depotFile", "rev", "action", "type" ]:
3746                 details["%s%s" % (prop, fileCnt)] = info[prop]
3747
3748             fileCnt = fileCnt + 1
3749
3750         details["change"] = newestRevision
3751
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"]
3756
3757         self.updateOptionDict(details)
3758         try:
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())
3764
3765
3766     def importRevisions(self, args, branch_arg_given):
3767         changes = []
3768
3769         if len(self.changesFile) > 0:
3770             with open(self.changesFile) as f:
3771                 output = f.readlines()
3772             changeSet = set()
3773             for line in output:
3774                 changeSet.add(int(line))
3775
3776             for change in changeSet:
3777                 changes.append(change)
3778
3779             changes.sort()
3780         else:
3781             # catch "git p4 sync" with no new branches, in a repo that
3782             # does not have any existing p4 branches
3783             if len(args) == 0:
3784                 if not self.p4BranchesInGit:
3785                     raise P4CommandException("No remote p4 branches.  Perhaps you never did \"git p4 clone\" in here.")
3786
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)
3794                         else:
3795                             raise P4CommandException("Error: no branch %s; perhaps specify one with --branch." %
3796                                 self.branch)
3797
3798             if self.verbose:
3799                 print("Getting p4 changes for %s...%s" % (', '.join(self.depotPaths),
3800                                                           self.changeRange))
3801             changes = p4ChangesForPaths(self.depotPaths, self.changeRange, self.changes_block_size)
3802
3803             if len(self.maxChanges) > 0:
3804                 changes = changes[:min(int(self.maxChanges), len(changes))]
3805
3806         if len(changes) == 0:
3807             if not self.silent:
3808                 print("No changes to import!")
3809         else:
3810             if not self.silent and not self.detectBranches:
3811                 print("Import destination: %s" % self.branch)
3812
3813             self.updatedBranches = set()
3814
3815             if not self.detectBranches:
3816                 if args:
3817                     # start a new branch
3818                     self.initialParent = ""
3819                 else:
3820                     # build on a previous revision
3821                     self.initialParent = parseRevision(self.branch)
3822
3823             self.importChanges(changes)
3824
3825             if not self.silent:
3826                 print("")
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")
3832
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
3841
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
3848
3849             self.gitStream.write = make_encoded_write(self.gitStream.write)
3850
3851     def closeStreams(self):
3852         if self.gitStream is None:
3853             return
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
3860
3861     def run(self, args):
3862         if self.importIntoRemotes:
3863             self.refPrefix = "refs/remotes/p4/"
3864         else:
3865             self.refPrefix = "refs/heads/p4/"
3866
3867         self.sync_origin_only()
3868
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")
3875
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
3880         else:
3881             if gitConfigBool("git-p4.useclientspec"):
3882                 self.useClientSpec = True
3883         if self.useClientSpec:
3884             self.clientSpecDirs = getClientSpec()
3885
3886         # TODO: should always look at previous commits,
3887         # merge with previous imports, if possible.
3888         if args == []:
3889             if self.hasOrigin:
3890                 createOrUpdateBranchesFromOrigin(self.refPrefix, self.silent)
3891
3892             # branches holds mapping from branch name to sha1
3893             branches = p4BranchesInGit(self.importIntoRemotes)
3894
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 ]
3900             else:
3901                 self.p4BranchesInGit = branches.keys()
3902
3903             if len(self.p4BranchesInGit) > 1:
3904                 if not self.silent:
3905                     print("Importing from/into multiple branches")
3906                 self.detectBranches = True
3907                 for branch in branches.keys():
3908                     self.initialParents[self.refPrefix + branch] = \
3909                         branches[branch]
3910
3911             if self.verbose:
3912                 print("branches: %s" % self.p4BranchesInGit)
3913
3914             p4Change = 0
3915             for branch in self.p4BranchesInGit:
3916                 logMsg =  extractLogMessageFromGitCommit(self.refPrefix + branch)
3917
3918                 settings = extractSettingsGitLog(logMsg)
3919
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)
3925
3926                     depotPaths = sorted(settings['depot-paths'])
3927                     if self.previousDepotPaths == []:
3928                         self.previousDepotPaths = depotPaths
3929                     else:
3930                         paths = []
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]:
3936                                     i = i - 1
3937                                     break
3938
3939                             paths.append ("/".join(cur_list[:i + 1]))
3940
3941                         self.previousDepotPaths = paths
3942
3943             if p4Change > 0:
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)
3948
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/"
3956             else:
3957                 prepend = "refs/heads/"
3958             if not self.branch.startswith("p4/"):
3959                 prepend += "p4/"
3960             self.branch = prepend + self.branch
3961
3962         if len(args) == 0 and self.depotPaths:
3963             if not self.silent:
3964                 print("Depot paths: %s" % ' '.join(self.depotPaths))
3965         else:
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),
3969                                                ' '.join (args)))
3970                 sys.exit(1)
3971
3972             self.depotPaths = sorted(args)
3973
3974         revision = ""
3975         self.users = {}
3976
3977         # Make sure no revision specifiers are used when --changesfile
3978         # is specified.
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
3984                     break
3985         if bad_changesfile:
3986             die("Option --changesfile is incompatible with revision specifiers")
3987
3988         newPaths = []
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 = ""
3998                 p = p[:atIdx]
3999             elif p.find("#") != -1:
4000                 hashIdx = p.index("#")
4001                 revision = p[hashIdx:]
4002                 p = 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:
4007                     revision = "#head"
4008
4009             p = re.sub ("\.\.\.$", "", p)
4010             if not p.endswith("/"):
4011                 p += "/"
4012
4013             newPaths.append(p)
4014
4015         self.depotPaths = newPaths
4016
4017         # --detect-branches may change this for each branch
4018         self.branchPrefixes = self.depotPaths
4019
4020         self.loadUserMapFromCache()
4021         self.labels = {}
4022         if self.detectLabels:
4023             self.getLabels();
4024
4025         if self.detectBranches:
4026             ## FIXME - what's a P4 projectName ?
4027             self.projectName = self.guessProjectName()
4028
4029             if self.hasOrigin:
4030                 self.getBranchMappingFromGitBranches()
4031             else:
4032                 self.getBranchMapping()
4033             if self.verbose:
4034                 print("p4-git branches: %s" % self.p4BranchesInGit)
4035                 print("initial parents: %s" % self.initialParents)
4036             for b in self.p4BranchesInGit:
4037                 if b != "master":
4038
4039                     ## FIXME
4040                     b = b[len(self.projectName):]
4041                 self.createdBranches.add(b)
4042
4043         p4_check_access()
4044
4045         self.openStreams()
4046
4047         err = None
4048
4049         try:
4050             if revision:
4051                 self.importHeadRevision(revision)
4052             else:
4053                 self.importRevisions(args, branch_arg_given)
4054
4055             if gitConfigBool("git-p4.importLabels"):
4056                 self.importLabels = True
4057
4058             if self.importLabels:
4059                 p4Labels = getP4Labels(self.depotPaths)
4060                 gitTags = getGitTags()
4061
4062                 missingP4Labels = p4Labels - gitTags
4063                 self.importP4Labels(self.gitStream, missingP4Labels)
4064
4065         except P4CommandException as e:
4066             err = e
4067
4068         finally:
4069             self.closeStreams()
4070
4071         if err:
4072             die(str(err))
4073
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))
4079
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])
4086
4087         return True
4088
4089 class P4Rebase(Command):
4090     def __init__(self):
4091         Command.__init__(self)
4092         self.options = [
4093                 optparse.make_option("--import-labels", dest="importLabels", action="store_true"),
4094         ]
4095         self.importLabels = False
4096         self.description = ("Fetches the latest revision from perforce and "
4097                             + "rebases the current work (branch) against it")
4098
4099     def run(self, args):
4100         sync = P4Sync()
4101         sync.importLabels = self.importLabels
4102         sync.run([])
4103
4104         return self.rebase()
4105
4106     def rebase(self):
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.");
4111
4112         [upstream, settings] = findUpstreamBranchPoint()
4113         if len(upstream) == 0:
4114             die("Cannot find upstream branchpoint for rebase")
4115
4116         # the branchpoint may be p4/foo~3, so strip off the parent
4117         upstream = re.sub("~[0-9]+$", "", upstream)
4118
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)
4123         return True
4124
4125 class P4Clone(P4Sync):
4126     def __init__(self):
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]"
4130         self.options += [
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),
4136         ]
4137         self.cloneDestination = None
4138         self.needsGit = False
4139         self.cloneBare = False
4140
4141     def defaultDestination(self, args):
4142         ## TODO: use common prefix of args?
4143         depotPath = args[0]
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]
4149
4150     def run(self, args):
4151         if len(args) < 1:
4152             return False
4153
4154         if self.keepRepoPath and not self.cloneDestination:
4155             sys.stderr.write("Must specify destination for --keep-path\n")
4156             sys.exit(1)
4157
4158         depotPaths = args
4159
4160         if not self.cloneDestination and len(depotPaths) > 1:
4161             self.cloneDestination = depotPaths[-1]
4162             depotPaths = depotPaths[:-1]
4163
4164         for p in depotPaths:
4165             if not p.startswith("//"):
4166                 sys.stderr.write('Depot paths must start with "//": %s\n' % p)
4167                 return False
4168
4169         if not self.cloneDestination:
4170             self.cloneDestination = self.defaultDestination(args)
4171
4172         print("Importing from %s into %s" % (', '.join(depotPaths), self.cloneDestination))
4173
4174         if not os.path.exists(self.cloneDestination):
4175             os.makedirs(self.cloneDestination)
4176         chdir(self.cloneDestination)
4177
4178         init_cmd = [ "git", "init" ]
4179         if self.cloneBare:
4180             init_cmd.append("--bare")
4181         retcode = subprocess.call(init_cmd)
4182         if retcode:
4183             raise CalledProcessError(retcode, init_cmd)
4184
4185         if not P4Sync.run(self, depotPaths):
4186             return False
4187
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" ])
4193         else:
4194             print('Not checking out any branch, use ' \
4195                   '"git checkout -q -b master <branch>"')
4196
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")
4200
4201         return True
4202
4203 class P4Unshelve(Command):
4204     def __init__(self):
4205         Command.__init__(self)
4206         self.options = []
4207         self.origin = "HEAD"
4208         self.description = "Unshelve a P4 changelist into a git commit"
4209         self.usage = "usage: %prog [options] changelist"
4210         self.options += [
4211                 optparse.make_option("--origin", dest="origin",
4212                     help="Use this base revision instead of the default (%s)" % self.origin),
4213         ]
4214         self.verbose = False
4215         self.noCommit = False
4216         self.destbranch = "refs/remotes/p4-unshelved"
4217
4218     def renameBranch(self, branch_name):
4219         """ Rename the existing branch to branch_name.N
4220         """
4221
4222         found = True
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)
4228                 found = True
4229                 print("renamed old unshelve branch to {0}".format(backup_branch_name))
4230                 break
4231
4232         if not found:
4233             sys.exit("gave up trying to rename existing branch {0}".format(sync.branch))
4234
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.
4238         """
4239
4240         for parent in (range(65535)):
4241             log = extractLogMessageFromGitCommit("{0}~{1}".format(starting_point, parent))
4242             settings = extractSettingsGitLog(log)
4243             if 'change' in settings:
4244                 return settings
4245
4246         sys.exit("could not find git-p4 commits in {0}".format(self.origin))
4247
4248     def createShelveParent(self, change, branch_name, sync, origin):
4249         """ Create a commit matching the parent of the shelved changelist 'change'
4250         """
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)
4254
4255         parent_files = []
4256         for f in files:
4257             # if it was added in the shelved changelist, it won't exist in the parent
4258             if f['action'] in self.add_actions:
4259                 continue
4260
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
4263             # does not have it
4264             if f['action'] in self.delete_actions:
4265                 f['action'] = 'add'
4266
4267             parent_files.append(f)
4268
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))
4273
4274     def run(self, args):
4275         if len(args) != 1:
4276             return False
4277
4278         if not gitBranchExists(self.origin):
4279             sys.exit("origin branch {0} does not exist".format(self.origin))
4280
4281         sync = P4Sync()
4282         changes = args
4283
4284         # only one change at a time
4285         change = changes[0]
4286
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
4292
4293         sync.verbose = self.verbose
4294         sync.suppress_meta_comment = True
4295
4296         settings = self.findLastP4Revision(self.origin)
4297         sync.depotPaths = settings['depot-paths']
4298         sync.branchPrefixes = sync.depotPaths
4299
4300         sync.openStreams()
4301         sync.loadUserMapFromCache()
4302         sync.silent = True
4303
4304         # create a commit for the parent of the shelved changelist
4305         self.createShelveParent(change, branch_name, sync, self.origin)
4306
4307         # create the commit for the shelved changelist itself
4308         description = p4_describe(change, True)
4309         files = sync.extractFilesFromCommit(description, True, change)
4310
4311         sync.commit(description, files, branch_name, "")
4312         sync.closeStreams()
4313
4314         print("unshelved changelist {0} into {1}".format(change, branch_name))
4315
4316         return True
4317
4318 class P4Branches(Command):
4319     def __init__(self):
4320         Command.__init__(self)
4321         self.options = [ ]
4322         self.description = ("Shows the git branches that hold imports and their "
4323                             + "corresponding perforce depot paths")
4324         self.verbose = False
4325
4326     def run(self, args):
4327         if originP4BranchesExist():
4328             createOrUpdateBranchesFromOrigin()
4329
4330         cmdline = "git rev-parse --symbolic "
4331         cmdline += " --remotes"
4332
4333         for line in read_pipe_lines(cmdline):
4334             line = line.strip()
4335
4336             if not line.startswith('p4/') or line == "p4/HEAD":
4337                 continue
4338             branch = line
4339
4340             log = extractLogMessageFromGitCommit("refs/remotes/%s" % branch)
4341             settings = extractSettingsGitLog(log)
4342
4343             print("%s <= %s (%s)" % (branch, ",".join(settings["depot-paths"]), settings["change"]))
4344         return True
4345
4346 class HelpFormatter(optparse.IndentedHelpFormatter):
4347     def __init__(self):
4348         optparse.IndentedHelpFormatter.__init__(self)
4349
4350     def format_description(self, description):
4351         if description:
4352             return description + "\n"
4353         else:
4354             return ""
4355
4356 def printUsage(commands):
4357     print("usage: %s <command> [options]" % sys.argv[0])
4358     print("")
4359     print("valid commands: %s" % ", ".join(commands))
4360     print("")
4361     print("Try %s <command> --help for command specific help." % sys.argv[0])
4362     print("")
4363
4364 commands = {
4365     "debug" : P4Debug,
4366     "submit" : P4Submit,
4367     "commit" : P4Submit,
4368     "sync" : P4Sync,
4369     "rebase" : P4Rebase,
4370     "clone" : P4Clone,
4371     "rollback" : P4RollBack,
4372     "branches" : P4Branches,
4373     "unshelve" : P4Unshelve,
4374 }
4375
4376 def main():
4377     if len(sys.argv[1:]) == 0:
4378         printUsage(commands.keys())
4379         sys.exit(2)
4380
4381     cmdName = sys.argv[1]
4382     try:
4383         klass = commands[cmdName]
4384         cmd = klass()
4385     except KeyError:
4386         print("unknown command %s" % cmdName)
4387         print("")
4388         printUsage(commands.keys())
4389         sys.exit(2)
4390
4391     options = cmd.options
4392     cmd.gitdir = os.environ.get("GIT_DIR", None)
4393
4394     args = sys.argv[2:]
4395
4396     options.append(optparse.make_option("--verbose", "-v", dest="verbose", action="store_true"))
4397     if cmd.needsGit:
4398         options.append(optparse.make_option("--git-dir", dest="gitdir"))
4399
4400     parser = optparse.OptionParser(cmd.usage.replace("%prog", "%prog " + cmdName),
4401                                    options,
4402                                    description = cmd.description,
4403                                    formatter = HelpFormatter())
4404
4405     try:
4406         (cmd, args) = parser.parse_args(sys.argv[2:], cmd);
4407     except:
4408         parser.print_help()
4409         raise
4410
4411     global verbose
4412     verbose = cmd.verbose
4413     if cmd.needsGit:
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()
4421                     if len(cdup) > 0:
4422                         chdir(cdup);
4423
4424         if not isValidGitDir(cmd.gitdir):
4425             if isValidGitDir(cmd.gitdir + "/.git"):
4426                 cmd.gitdir += "/.git"
4427             else:
4428                 die("fatal: cannot locate git repository at %s" % cmd.gitdir)
4429
4430         # so git commands invoked from the P4 workspace will succeed
4431         os.environ["GIT_DIR"] = cmd.gitdir
4432
4433     if not cmd.run(args):
4434         parser.print_help()
4435         sys.exit(2)
4436
4437
4438 if __name__ == '__main__':
4439     main()