]> git.ipfire.org Git - thirdparty/git.git/blob - git-p4.py
git-p4: remove "rollback" verb
[thirdparty/git.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 for:
768 # - `data` which may contain arbitrary binary data
769 # - `depotFile[0-9]*`, `path`, or `clientFile` which may contain non-UTF8 encoded text
770 decoded_entry = {}
771 for key, value in entry.items():
772 key = key.decode()
773 if isinstance(value, bytes) and not (key in ('data', 'path', 'clientFile') or key.startswith('depotFile')):
774 value = value.decode()
775 decoded_entry[key] = value
776 # Parse out data if it's an error response
777 if decoded_entry.get('code') == 'error' and 'data' in decoded_entry:
778 decoded_entry['data'] = decoded_entry['data'].decode()
779 entry = decoded_entry
780 if skip_info:
781 if 'code' in entry and entry['code'] == 'info':
782 continue
783 if cb is not None:
784 cb(entry)
785 else:
786 result.append(entry)
787 except EOFError:
788 pass
789 exitCode = p4.wait()
790 if exitCode != 0:
791 if errors_as_exceptions:
792 if len(result) > 0:
793 data = result[0].get('data')
794 if data:
795 m = re.search('Too many rows scanned \(over (\d+)\)', data)
796 if not m:
797 m = re.search('Request too large \(over (\d+)\)', data)
798
799 if m:
800 limit = int(m.group(1))
801 raise P4RequestSizeException(exitCode, result, limit)
802
803 raise P4ServerException(exitCode, result)
804 else:
805 raise P4Exception(exitCode)
806 else:
807 entry = {}
808 entry["p4ExitCode"] = exitCode
809 result.append(entry)
810
811 return result
812
813 def p4Cmd(cmd):
814 list = p4CmdList(cmd)
815 result = {}
816 for entry in list:
817 result.update(entry)
818 return result;
819
820 def p4Where(depotPath):
821 if not depotPath.endswith("/"):
822 depotPath += "/"
823 depotPathLong = depotPath + "..."
824 outputList = p4CmdList(["where", depotPathLong])
825 output = None
826 for entry in outputList:
827 if "depotFile" in entry:
828 # Search for the base client side depot path, as long as it starts with the branch's P4 path.
829 # The base path always ends with "/...".
830 entry_path = decode_path(entry['depotFile'])
831 if entry_path.find(depotPath) == 0 and entry_path[-4:] == "/...":
832 output = entry
833 break
834 elif "data" in entry:
835 data = entry.get("data")
836 space = data.find(" ")
837 if data[:space] == depotPath:
838 output = entry
839 break
840 if output == None:
841 return ""
842 if output["code"] == "error":
843 return ""
844 clientPath = ""
845 if "path" in output:
846 clientPath = decode_path(output['path'])
847 elif "data" in output:
848 data = output.get("data")
849 lastSpace = data.rfind(b" ")
850 clientPath = decode_path(data[lastSpace + 1:])
851
852 if clientPath.endswith("..."):
853 clientPath = clientPath[:-3]
854 return clientPath
855
856 def currentGitBranch():
857 return read_pipe_text(["git", "symbolic-ref", "--short", "-q", "HEAD"])
858
859 def isValidGitDir(path):
860 return git_dir(path) != None
861
862 def parseRevision(ref):
863 return read_pipe("git rev-parse %s" % ref).strip()
864
865 def branchExists(ref):
866 rev = read_pipe(["git", "rev-parse", "-q", "--verify", ref],
867 ignore_error=True)
868 return len(rev) > 0
869
870 def extractLogMessageFromGitCommit(commit):
871 logMessage = ""
872
873 ## fixme: title is first line of commit, not 1st paragraph.
874 foundTitle = False
875 for log in read_pipe_lines(["git", "cat-file", "commit", commit]):
876 if not foundTitle:
877 if len(log) == 1:
878 foundTitle = True
879 continue
880
881 logMessage += log
882 return logMessage
883
884 def extractSettingsGitLog(log):
885 values = {}
886 for line in log.split("\n"):
887 line = line.strip()
888 m = re.search (r"^ *\[git-p4: (.*)\]$", line)
889 if not m:
890 continue
891
892 assignments = m.group(1).split (':')
893 for a in assignments:
894 vals = a.split ('=')
895 key = vals[0].strip()
896 val = ('='.join (vals[1:])).strip()
897 if val.endswith ('\"') and val.startswith('"'):
898 val = val[1:-1]
899
900 values[key] = val
901
902 paths = values.get("depot-paths")
903 if not paths:
904 paths = values.get("depot-path")
905 if paths:
906 values['depot-paths'] = paths.split(',')
907 return values
908
909 def gitBranchExists(branch):
910 proc = subprocess.Popen(["git", "rev-parse", branch],
911 stderr=subprocess.PIPE, stdout=subprocess.PIPE);
912 return proc.wait() == 0;
913
914 def gitUpdateRef(ref, newvalue):
915 subprocess.check_call(["git", "update-ref", ref, newvalue])
916
917 def gitDeleteRef(ref):
918 subprocess.check_call(["git", "update-ref", "-d", ref])
919
920 _gitConfig = {}
921
922 def gitConfig(key, typeSpecifier=None):
923 if key not in _gitConfig:
924 cmd = [ "git", "config" ]
925 if typeSpecifier:
926 cmd += [ typeSpecifier ]
927 cmd += [ key ]
928 s = read_pipe(cmd, ignore_error=True)
929 _gitConfig[key] = s.strip()
930 return _gitConfig[key]
931
932 def gitConfigBool(key):
933 """Return a bool, using git config --bool. It is True only if the
934 variable is set to true, and False if set to false or not present
935 in the config."""
936
937 if key not in _gitConfig:
938 _gitConfig[key] = gitConfig(key, '--bool') == "true"
939 return _gitConfig[key]
940
941 def gitConfigInt(key):
942 if key not in _gitConfig:
943 cmd = [ "git", "config", "--int", key ]
944 s = read_pipe(cmd, ignore_error=True)
945 v = s.strip()
946 try:
947 _gitConfig[key] = int(gitConfig(key, '--int'))
948 except ValueError:
949 _gitConfig[key] = None
950 return _gitConfig[key]
951
952 def gitConfigList(key):
953 if key not in _gitConfig:
954 s = read_pipe(["git", "config", "--get-all", key], ignore_error=True)
955 _gitConfig[key] = s.strip().splitlines()
956 if _gitConfig[key] == ['']:
957 _gitConfig[key] = []
958 return _gitConfig[key]
959
960 def p4BranchesInGit(branchesAreInRemotes=True):
961 """Find all the branches whose names start with "p4/", looking
962 in remotes or heads as specified by the argument. Return
963 a dictionary of { branch: revision } for each one found.
964 The branch names are the short names, without any
965 "p4/" prefix."""
966
967 branches = {}
968
969 cmdline = "git rev-parse --symbolic "
970 if branchesAreInRemotes:
971 cmdline += "--remotes"
972 else:
973 cmdline += "--branches"
974
975 for line in read_pipe_lines(cmdline):
976 line = line.strip()
977
978 # only import to p4/
979 if not line.startswith('p4/'):
980 continue
981 # special symbolic ref to p4/master
982 if line == "p4/HEAD":
983 continue
984
985 # strip off p4/ prefix
986 branch = line[len("p4/"):]
987
988 branches[branch] = parseRevision(line)
989
990 return branches
991
992 def branch_exists(branch):
993 """Make sure that the given ref name really exists."""
994
995 cmd = [ "git", "rev-parse", "--symbolic", "--verify", branch ]
996 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
997 out, _ = p.communicate()
998 out = decode_text_stream(out)
999 if p.returncode:
1000 return False
1001 # expect exactly one line of output: the branch name
1002 return out.rstrip() == branch
1003
1004 def findUpstreamBranchPoint(head = "HEAD"):
1005 branches = p4BranchesInGit()
1006 # map from depot-path to branch name
1007 branchByDepotPath = {}
1008 for branch in branches.keys():
1009 tip = branches[branch]
1010 log = extractLogMessageFromGitCommit(tip)
1011 settings = extractSettingsGitLog(log)
1012 if "depot-paths" in settings:
1013 paths = ",".join(settings["depot-paths"])
1014 branchByDepotPath[paths] = "remotes/p4/" + branch
1015
1016 settings = None
1017 parent = 0
1018 while parent < 65535:
1019 commit = head + "~%s" % parent
1020 log = extractLogMessageFromGitCommit(commit)
1021 settings = extractSettingsGitLog(log)
1022 if "depot-paths" in settings:
1023 paths = ",".join(settings["depot-paths"])
1024 if paths in branchByDepotPath:
1025 return [branchByDepotPath[paths], settings]
1026
1027 parent = parent + 1
1028
1029 return ["", settings]
1030
1031 def createOrUpdateBranchesFromOrigin(localRefPrefix = "refs/remotes/p4/", silent=True):
1032 if not silent:
1033 print("Creating/updating branch(es) in %s based on origin branch(es)"
1034 % localRefPrefix)
1035
1036 originPrefix = "origin/p4/"
1037
1038 for line in read_pipe_lines("git rev-parse --symbolic --remotes"):
1039 line = line.strip()
1040 if (not line.startswith(originPrefix)) or line.endswith("HEAD"):
1041 continue
1042
1043 headName = line[len(originPrefix):]
1044 remoteHead = localRefPrefix + headName
1045 originHead = line
1046
1047 original = extractSettingsGitLog(extractLogMessageFromGitCommit(originHead))
1048 if ('depot-paths' not in original
1049 or 'change' not in original):
1050 continue
1051
1052 update = False
1053 if not gitBranchExists(remoteHead):
1054 if verbose:
1055 print("creating %s" % remoteHead)
1056 update = True
1057 else:
1058 settings = extractSettingsGitLog(extractLogMessageFromGitCommit(remoteHead))
1059 if 'change' in settings:
1060 if settings['depot-paths'] == original['depot-paths']:
1061 originP4Change = int(original['change'])
1062 p4Change = int(settings['change'])
1063 if originP4Change > p4Change:
1064 print("%s (%s) is newer than %s (%s). "
1065 "Updating p4 branch from origin."
1066 % (originHead, originP4Change,
1067 remoteHead, p4Change))
1068 update = True
1069 else:
1070 print("Ignoring: %s was imported from %s while "
1071 "%s was imported from %s"
1072 % (originHead, ','.join(original['depot-paths']),
1073 remoteHead, ','.join(settings['depot-paths'])))
1074
1075 if update:
1076 system("git update-ref %s %s" % (remoteHead, originHead))
1077
1078 def originP4BranchesExist():
1079 return gitBranchExists("origin") or gitBranchExists("origin/p4") or gitBranchExists("origin/p4/master")
1080
1081
1082 def p4ParseNumericChangeRange(parts):
1083 changeStart = int(parts[0][1:])
1084 if parts[1] == '#head':
1085 changeEnd = p4_last_change()
1086 else:
1087 changeEnd = int(parts[1])
1088
1089 return (changeStart, changeEnd)
1090
1091 def chooseBlockSize(blockSize):
1092 if blockSize:
1093 return blockSize
1094 else:
1095 return defaultBlockSize
1096
1097 def p4ChangesForPaths(depotPaths, changeRange, requestedBlockSize):
1098 assert depotPaths
1099
1100 # Parse the change range into start and end. Try to find integer
1101 # revision ranges as these can be broken up into blocks to avoid
1102 # hitting server-side limits (maxrows, maxscanresults). But if
1103 # that doesn't work, fall back to using the raw revision specifier
1104 # strings, without using block mode.
1105
1106 if changeRange is None or changeRange == '':
1107 changeStart = 1
1108 changeEnd = p4_last_change()
1109 block_size = chooseBlockSize(requestedBlockSize)
1110 else:
1111 parts = changeRange.split(',')
1112 assert len(parts) == 2
1113 try:
1114 (changeStart, changeEnd) = p4ParseNumericChangeRange(parts)
1115 block_size = chooseBlockSize(requestedBlockSize)
1116 except ValueError:
1117 changeStart = parts[0][1:]
1118 changeEnd = parts[1]
1119 if requestedBlockSize:
1120 die("cannot use --changes-block-size with non-numeric revisions")
1121 block_size = None
1122
1123 changes = set()
1124
1125 # Retrieve changes a block at a time, to prevent running
1126 # into a MaxResults/MaxScanRows error from the server. If
1127 # we _do_ hit one of those errors, turn down the block size
1128
1129 while True:
1130 cmd = ['changes']
1131
1132 if block_size:
1133 end = min(changeEnd, changeStart + block_size)
1134 revisionRange = "%d,%d" % (changeStart, end)
1135 else:
1136 revisionRange = "%s,%s" % (changeStart, changeEnd)
1137
1138 for p in depotPaths:
1139 cmd += ["%s...@%s" % (p, revisionRange)]
1140
1141 # fetch the changes
1142 try:
1143 result = p4CmdList(cmd, errors_as_exceptions=True)
1144 except P4RequestSizeException as e:
1145 if not block_size:
1146 block_size = e.limit
1147 elif block_size > e.limit:
1148 block_size = e.limit
1149 else:
1150 block_size = max(2, block_size // 2)
1151
1152 if verbose: print("block size error, retrying with block size {0}".format(block_size))
1153 continue
1154 except P4Exception as e:
1155 die('Error retrieving changes description ({0})'.format(e.p4ExitCode))
1156
1157 # Insert changes in chronological order
1158 for entry in reversed(result):
1159 if 'change' not in entry:
1160 continue
1161 changes.add(int(entry['change']))
1162
1163 if not block_size:
1164 break
1165
1166 if end >= changeEnd:
1167 break
1168
1169 changeStart = end + 1
1170
1171 changes = sorted(changes)
1172 return changes
1173
1174 def p4PathStartsWith(path, prefix):
1175 # This method tries to remedy a potential mixed-case issue:
1176 #
1177 # If UserA adds //depot/DirA/file1
1178 # and UserB adds //depot/dira/file2
1179 #
1180 # we may or may not have a problem. If you have core.ignorecase=true,
1181 # we treat DirA and dira as the same directory
1182 if gitConfigBool("core.ignorecase"):
1183 return path.lower().startswith(prefix.lower())
1184 return path.startswith(prefix)
1185
1186 def getClientSpec():
1187 """Look at the p4 client spec, create a View() object that contains
1188 all the mappings, and return it."""
1189
1190 specList = p4CmdList("client -o")
1191 if len(specList) != 1:
1192 die('Output from "client -o" is %d lines, expecting 1' %
1193 len(specList))
1194
1195 # dictionary of all client parameters
1196 entry = specList[0]
1197
1198 # the //client/ name
1199 client_name = entry["Client"]
1200
1201 # just the keys that start with "View"
1202 view_keys = [ k for k in entry.keys() if k.startswith("View") ]
1203
1204 # hold this new View
1205 view = View(client_name)
1206
1207 # append the lines, in order, to the view
1208 for view_num in range(len(view_keys)):
1209 k = "View%d" % view_num
1210 if k not in view_keys:
1211 die("Expected view key %s missing" % k)
1212 view.append(entry[k])
1213
1214 return view
1215
1216 def getClientRoot():
1217 """Grab the client directory."""
1218
1219 output = p4CmdList("client -o")
1220 if len(output) != 1:
1221 die('Output from "client -o" is %d lines, expecting 1' % len(output))
1222
1223 entry = output[0]
1224 if "Root" not in entry:
1225 die('Client has no "Root"')
1226
1227 return entry["Root"]
1228
1229 #
1230 # P4 wildcards are not allowed in filenames. P4 complains
1231 # if you simply add them, but you can force it with "-f", in
1232 # which case it translates them into %xx encoding internally.
1233 #
1234 def wildcard_decode(path):
1235 # Search for and fix just these four characters. Do % last so
1236 # that fixing it does not inadvertently create new %-escapes.
1237 # Cannot have * in a filename in windows; untested as to
1238 # what p4 would do in such a case.
1239 if not platform.system() == "Windows":
1240 path = path.replace("%2A", "*")
1241 path = path.replace("%23", "#") \
1242 .replace("%40", "@") \
1243 .replace("%25", "%")
1244 return path
1245
1246 def wildcard_encode(path):
1247 # do % first to avoid double-encoding the %s introduced here
1248 path = path.replace("%", "%25") \
1249 .replace("*", "%2A") \
1250 .replace("#", "%23") \
1251 .replace("@", "%40")
1252 return path
1253
1254 def wildcard_present(path):
1255 m = re.search("[*#@%]", path)
1256 return m is not None
1257
1258 class LargeFileSystem(object):
1259 """Base class for large file system support."""
1260
1261 def __init__(self, writeToGitStream):
1262 self.largeFiles = set()
1263 self.writeToGitStream = writeToGitStream
1264
1265 def generatePointer(self, cloneDestination, contentFile):
1266 """Return the content of a pointer file that is stored in Git instead of
1267 the actual content."""
1268 assert False, "Method 'generatePointer' required in " + self.__class__.__name__
1269
1270 def pushFile(self, localLargeFile):
1271 """Push the actual content which is not stored in the Git repository to
1272 a server."""
1273 assert False, "Method 'pushFile' required in " + self.__class__.__name__
1274
1275 def hasLargeFileExtension(self, relPath):
1276 return functools.reduce(
1277 lambda a, b: a or b,
1278 [relPath.endswith('.' + e) for e in gitConfigList('git-p4.largeFileExtensions')],
1279 False
1280 )
1281
1282 def generateTempFile(self, contents):
1283 contentFile = tempfile.NamedTemporaryFile(prefix='git-p4-large-file', delete=False)
1284 for d in contents:
1285 contentFile.write(d)
1286 contentFile.close()
1287 return contentFile.name
1288
1289 def exceedsLargeFileThreshold(self, relPath, contents):
1290 if gitConfigInt('git-p4.largeFileThreshold'):
1291 contentsSize = sum(len(d) for d in contents)
1292 if contentsSize > gitConfigInt('git-p4.largeFileThreshold'):
1293 return True
1294 if gitConfigInt('git-p4.largeFileCompressedThreshold'):
1295 contentsSize = sum(len(d) for d in contents)
1296 if contentsSize <= gitConfigInt('git-p4.largeFileCompressedThreshold'):
1297 return False
1298 contentTempFile = self.generateTempFile(contents)
1299 compressedContentFile = tempfile.NamedTemporaryFile(prefix='git-p4-large-file', delete=True)
1300 with zipfile.ZipFile(compressedContentFile, mode='w') as zf:
1301 zf.write(contentTempFile, compress_type=zipfile.ZIP_DEFLATED)
1302 compressedContentsSize = zf.infolist()[0].compress_size
1303 os.remove(contentTempFile)
1304 if compressedContentsSize > gitConfigInt('git-p4.largeFileCompressedThreshold'):
1305 return True
1306 return False
1307
1308 def addLargeFile(self, relPath):
1309 self.largeFiles.add(relPath)
1310
1311 def removeLargeFile(self, relPath):
1312 self.largeFiles.remove(relPath)
1313
1314 def isLargeFile(self, relPath):
1315 return relPath in self.largeFiles
1316
1317 def processContent(self, git_mode, relPath, contents):
1318 """Processes the content of git fast import. This method decides if a
1319 file is stored in the large file system and handles all necessary
1320 steps."""
1321 if self.exceedsLargeFileThreshold(relPath, contents) or self.hasLargeFileExtension(relPath):
1322 contentTempFile = self.generateTempFile(contents)
1323 (pointer_git_mode, contents, localLargeFile) = self.generatePointer(contentTempFile)
1324 if pointer_git_mode:
1325 git_mode = pointer_git_mode
1326 if localLargeFile:
1327 # Move temp file to final location in large file system
1328 largeFileDir = os.path.dirname(localLargeFile)
1329 if not os.path.isdir(largeFileDir):
1330 os.makedirs(largeFileDir)
1331 shutil.move(contentTempFile, localLargeFile)
1332 self.addLargeFile(relPath)
1333 if gitConfigBool('git-p4.largeFilePush'):
1334 self.pushFile(localLargeFile)
1335 if verbose:
1336 sys.stderr.write("%s moved to large file system (%s)\n" % (relPath, localLargeFile))
1337 return (git_mode, contents)
1338
1339 class MockLFS(LargeFileSystem):
1340 """Mock large file system for testing."""
1341
1342 def generatePointer(self, contentFile):
1343 """The pointer content is the original content prefixed with "pointer-".
1344 The local filename of the large file storage is derived from the file content.
1345 """
1346 with open(contentFile, 'r') as f:
1347 content = next(f)
1348 gitMode = '100644'
1349 pointerContents = 'pointer-' + content
1350 localLargeFile = os.path.join(os.getcwd(), '.git', 'mock-storage', 'local', content[:-1])
1351 return (gitMode, pointerContents, localLargeFile)
1352
1353 def pushFile(self, localLargeFile):
1354 """The remote filename of the large file storage is the same as the local
1355 one but in a different directory.
1356 """
1357 remotePath = os.path.join(os.path.dirname(localLargeFile), '..', 'remote')
1358 if not os.path.exists(remotePath):
1359 os.makedirs(remotePath)
1360 shutil.copyfile(localLargeFile, os.path.join(remotePath, os.path.basename(localLargeFile)))
1361
1362 class GitLFS(LargeFileSystem):
1363 """Git LFS as backend for the git-p4 large file system.
1364 See https://git-lfs.github.com/ for details."""
1365
1366 def __init__(self, *args):
1367 LargeFileSystem.__init__(self, *args)
1368 self.baseGitAttributes = []
1369
1370 def generatePointer(self, contentFile):
1371 """Generate a Git LFS pointer for the content. Return LFS Pointer file
1372 mode and content which is stored in the Git repository instead of
1373 the actual content. Return also the new location of the actual
1374 content.
1375 """
1376 if os.path.getsize(contentFile) == 0:
1377 return (None, '', None)
1378
1379 pointerProcess = subprocess.Popen(
1380 ['git', 'lfs', 'pointer', '--file=' + contentFile],
1381 stdout=subprocess.PIPE
1382 )
1383 pointerFile = decode_text_stream(pointerProcess.stdout.read())
1384 if pointerProcess.wait():
1385 os.remove(contentFile)
1386 die('git-lfs pointer command failed. Did you install the extension?')
1387
1388 # Git LFS removed the preamble in the output of the 'pointer' command
1389 # starting from version 1.2.0. Check for the preamble here to support
1390 # earlier versions.
1391 # c.f. https://github.com/github/git-lfs/commit/da2935d9a739592bc775c98d8ef4df9c72ea3b43
1392 if pointerFile.startswith('Git LFS pointer for'):
1393 pointerFile = re.sub(r'Git LFS pointer for.*\n\n', '', pointerFile)
1394
1395 oid = re.search(r'^oid \w+:(\w+)', pointerFile, re.MULTILINE).group(1)
1396 # if someone use external lfs.storage ( not in local repo git )
1397 lfs_path = gitConfig('lfs.storage')
1398 if not lfs_path:
1399 lfs_path = 'lfs'
1400 if not os.path.isabs(lfs_path):
1401 lfs_path = os.path.join(os.getcwd(), '.git', lfs_path)
1402 localLargeFile = os.path.join(
1403 lfs_path,
1404 'objects', oid[:2], oid[2:4],
1405 oid,
1406 )
1407 # LFS Spec states that pointer files should not have the executable bit set.
1408 gitMode = '100644'
1409 return (gitMode, pointerFile, localLargeFile)
1410
1411 def pushFile(self, localLargeFile):
1412 uploadProcess = subprocess.Popen(
1413 ['git', 'lfs', 'push', '--object-id', 'origin', os.path.basename(localLargeFile)]
1414 )
1415 if uploadProcess.wait():
1416 die('git-lfs push command failed. Did you define a remote?')
1417
1418 def generateGitAttributes(self):
1419 return (
1420 self.baseGitAttributes +
1421 [
1422 '\n',
1423 '#\n',
1424 '# Git LFS (see https://git-lfs.github.com/)\n',
1425 '#\n',
1426 ] +
1427 ['*.' + f.replace(' ', '[[:space:]]') + ' filter=lfs diff=lfs merge=lfs -text\n'
1428 for f in sorted(gitConfigList('git-p4.largeFileExtensions'))
1429 ] +
1430 ['/' + f.replace(' ', '[[:space:]]') + ' filter=lfs diff=lfs merge=lfs -text\n'
1431 for f in sorted(self.largeFiles) if not self.hasLargeFileExtension(f)
1432 ]
1433 )
1434
1435 def addLargeFile(self, relPath):
1436 LargeFileSystem.addLargeFile(self, relPath)
1437 self.writeToGitStream('100644', '.gitattributes', self.generateGitAttributes())
1438
1439 def removeLargeFile(self, relPath):
1440 LargeFileSystem.removeLargeFile(self, relPath)
1441 self.writeToGitStream('100644', '.gitattributes', self.generateGitAttributes())
1442
1443 def processContent(self, git_mode, relPath, contents):
1444 if relPath == '.gitattributes':
1445 self.baseGitAttributes = contents
1446 return (git_mode, self.generateGitAttributes())
1447 else:
1448 return LargeFileSystem.processContent(self, git_mode, relPath, contents)
1449
1450 class Command:
1451 delete_actions = ( "delete", "move/delete", "purge" )
1452 add_actions = ( "add", "branch", "move/add" )
1453
1454 def __init__(self):
1455 self.usage = "usage: %prog [options]"
1456 self.needsGit = True
1457 self.verbose = False
1458
1459 # This is required for the "append" update_shelve action
1460 def ensure_value(self, attr, value):
1461 if not hasattr(self, attr) or getattr(self, attr) is None:
1462 setattr(self, attr, value)
1463 return getattr(self, attr)
1464
1465 class P4UserMap:
1466 def __init__(self):
1467 self.userMapFromPerforceServer = False
1468 self.myP4UserId = None
1469
1470 def p4UserId(self):
1471 if self.myP4UserId:
1472 return self.myP4UserId
1473
1474 results = p4CmdList("user -o")
1475 for r in results:
1476 if 'User' in r:
1477 self.myP4UserId = r['User']
1478 return r['User']
1479 die("Could not find your p4 user id")
1480
1481 def p4UserIsMe(self, p4User):
1482 # return True if the given p4 user is actually me
1483 me = self.p4UserId()
1484 if not p4User or p4User != me:
1485 return False
1486 else:
1487 return True
1488
1489 def getUserCacheFilename(self):
1490 home = os.environ.get("HOME", os.environ.get("USERPROFILE"))
1491 return home + "/.gitp4-usercache.txt"
1492
1493 def getUserMapFromPerforceServer(self):
1494 if self.userMapFromPerforceServer:
1495 return
1496 self.users = {}
1497 self.emails = {}
1498
1499 for output in p4CmdList("users"):
1500 if "User" not in output:
1501 continue
1502 self.users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">"
1503 self.emails[output["Email"]] = output["User"]
1504
1505 mapUserConfigRegex = re.compile(r"^\s*(\S+)\s*=\s*(.+)\s*<(\S+)>\s*$", re.VERBOSE)
1506 for mapUserConfig in gitConfigList("git-p4.mapUser"):
1507 mapUser = mapUserConfigRegex.findall(mapUserConfig)
1508 if mapUser and len(mapUser[0]) == 3:
1509 user = mapUser[0][0]
1510 fullname = mapUser[0][1]
1511 email = mapUser[0][2]
1512 self.users[user] = fullname + " <" + email + ">"
1513 self.emails[email] = user
1514
1515 s = ''
1516 for (key, val) in self.users.items():
1517 s += "%s\t%s\n" % (key.expandtabs(1), val.expandtabs(1))
1518
1519 open(self.getUserCacheFilename(), 'w').write(s)
1520 self.userMapFromPerforceServer = True
1521
1522 def loadUserMapFromCache(self):
1523 self.users = {}
1524 self.userMapFromPerforceServer = False
1525 try:
1526 cache = open(self.getUserCacheFilename(), 'r')
1527 lines = cache.readlines()
1528 cache.close()
1529 for line in lines:
1530 entry = line.strip().split("\t")
1531 self.users[entry[0]] = entry[1]
1532 except IOError:
1533 self.getUserMapFromPerforceServer()
1534
1535 class P4Submit(Command, P4UserMap):
1536
1537 conflict_behavior_choices = ("ask", "skip", "quit")
1538
1539 def __init__(self):
1540 Command.__init__(self)
1541 P4UserMap.__init__(self)
1542 self.options = [
1543 optparse.make_option("--origin", dest="origin"),
1544 optparse.make_option("-M", dest="detectRenames", action="store_true"),
1545 # preserve the user, requires relevant p4 permissions
1546 optparse.make_option("--preserve-user", dest="preserveUser", action="store_true"),
1547 optparse.make_option("--export-labels", dest="exportLabels", action="store_true"),
1548 optparse.make_option("--dry-run", "-n", dest="dry_run", action="store_true"),
1549 optparse.make_option("--prepare-p4-only", dest="prepare_p4_only", action="store_true"),
1550 optparse.make_option("--conflict", dest="conflict_behavior",
1551 choices=self.conflict_behavior_choices),
1552 optparse.make_option("--branch", dest="branch"),
1553 optparse.make_option("--shelve", dest="shelve", action="store_true",
1554 help="Shelve instead of submit. Shelved files are reverted, "
1555 "restoring the workspace to the state before the shelve"),
1556 optparse.make_option("--update-shelve", dest="update_shelve", action="append", type="int",
1557 metavar="CHANGELIST",
1558 help="update an existing shelved changelist, implies --shelve, "
1559 "repeat in-order for multiple shelved changelists"),
1560 optparse.make_option("--commit", dest="commit", metavar="COMMIT",
1561 help="submit only the specified commit(s), one commit or xxx..xxx"),
1562 optparse.make_option("--disable-rebase", dest="disable_rebase", action="store_true",
1563 help="Disable rebase after submit is completed. Can be useful if you "
1564 "work from a local git branch that is not master"),
1565 optparse.make_option("--disable-p4sync", dest="disable_p4sync", action="store_true",
1566 help="Skip Perforce sync of p4/master after submit or shelve"),
1567 optparse.make_option("--no-verify", dest="no_verify", action="store_true",
1568 help="Bypass p4-pre-submit and p4-changelist hooks"),
1569 ]
1570 self.description = """Submit changes from git to the perforce depot.\n
1571 The `p4-pre-submit` hook is executed if it exists and is executable. It
1572 can be bypassed with the `--no-verify` command line option. The hook takes
1573 no parameters and nothing from standard input. Exiting with a non-zero status
1574 from this script prevents `git-p4 submit` from launching.
1575
1576 One usage scenario is to run unit tests in the hook.
1577
1578 The `p4-prepare-changelist` hook is executed right after preparing the default
1579 changelist message and before the editor is started. It takes one parameter,
1580 the name of the file that contains the changelist text. Exiting with a non-zero
1581 status from the script will abort the process.
1582
1583 The purpose of the hook is to edit the message file in place, and it is not
1584 supressed by the `--no-verify` option. This hook is called even if
1585 `--prepare-p4-only` is set.
1586
1587 The `p4-changelist` hook is executed after the changelist message has been
1588 edited by the user. It can be bypassed with the `--no-verify` option. It
1589 takes a single parameter, the name of the file that holds the proposed
1590 changelist text. Exiting with a non-zero status causes the command to abort.
1591
1592 The hook is allowed to edit the changelist file and can be used to normalize
1593 the text into some project standard format. It can also be used to refuse the
1594 Submit after inspect the message file.
1595
1596 The `p4-post-changelist` hook is invoked after the submit has successfully
1597 occurred in P4. It takes no parameters and is meant primarily for notification
1598 and cannot affect the outcome of the git p4 submit action.
1599 """
1600
1601 self.usage += " [name of git branch to submit into perforce depot]"
1602 self.origin = ""
1603 self.detectRenames = False
1604 self.preserveUser = gitConfigBool("git-p4.preserveUser")
1605 self.dry_run = False
1606 self.shelve = False
1607 self.update_shelve = list()
1608 self.commit = ""
1609 self.disable_rebase = gitConfigBool("git-p4.disableRebase")
1610 self.disable_p4sync = gitConfigBool("git-p4.disableP4Sync")
1611 self.prepare_p4_only = False
1612 self.conflict_behavior = None
1613 self.isWindows = (platform.system() == "Windows")
1614 self.exportLabels = False
1615 self.p4HasMoveCommand = p4_has_move_command()
1616 self.branch = None
1617 self.no_verify = False
1618
1619 if gitConfig('git-p4.largeFileSystem'):
1620 die("Large file system not supported for git-p4 submit command. Please remove it from config.")
1621
1622 def check(self):
1623 if len(p4CmdList("opened ...")) > 0:
1624 die("You have files opened with perforce! Close them before starting the sync.")
1625
1626 def separate_jobs_from_description(self, message):
1627 """Extract and return a possible Jobs field in the commit
1628 message. It goes into a separate section in the p4 change
1629 specification.
1630
1631 A jobs line starts with "Jobs:" and looks like a new field
1632 in a form. Values are white-space separated on the same
1633 line or on following lines that start with a tab.
1634
1635 This does not parse and extract the full git commit message
1636 like a p4 form. It just sees the Jobs: line as a marker
1637 to pass everything from then on directly into the p4 form,
1638 but outside the description section.
1639
1640 Return a tuple (stripped log message, jobs string)."""
1641
1642 m = re.search(r'^Jobs:', message, re.MULTILINE)
1643 if m is None:
1644 return (message, None)
1645
1646 jobtext = message[m.start():]
1647 stripped_message = message[:m.start()].rstrip()
1648 return (stripped_message, jobtext)
1649
1650 def prepareLogMessage(self, template, message, jobs):
1651 """Edits the template returned from "p4 change -o" to insert
1652 the message in the Description field, and the jobs text in
1653 the Jobs field."""
1654 result = ""
1655
1656 inDescriptionSection = False
1657
1658 for line in template.split("\n"):
1659 if line.startswith("#"):
1660 result += line + "\n"
1661 continue
1662
1663 if inDescriptionSection:
1664 if line.startswith("Files:") or line.startswith("Jobs:"):
1665 inDescriptionSection = False
1666 # insert Jobs section
1667 if jobs:
1668 result += jobs + "\n"
1669 else:
1670 continue
1671 else:
1672 if line.startswith("Description:"):
1673 inDescriptionSection = True
1674 line += "\n"
1675 for messageLine in message.split("\n"):
1676 line += "\t" + messageLine + "\n"
1677
1678 result += line + "\n"
1679
1680 return result
1681
1682 def patchRCSKeywords(self, file, pattern):
1683 # Attempt to zap the RCS keywords in a p4 controlled file matching the given pattern
1684 (handle, outFileName) = tempfile.mkstemp(dir='.')
1685 try:
1686 outFile = os.fdopen(handle, "w+")
1687 inFile = open(file, "r")
1688 regexp = re.compile(pattern, re.VERBOSE)
1689 for line in inFile.readlines():
1690 line = regexp.sub(r'$\1$', line)
1691 outFile.write(line)
1692 inFile.close()
1693 outFile.close()
1694 # Forcibly overwrite the original file
1695 os.unlink(file)
1696 shutil.move(outFileName, file)
1697 except:
1698 # cleanup our temporary file
1699 os.unlink(outFileName)
1700 print("Failed to strip RCS keywords in %s" % file)
1701 raise
1702
1703 print("Patched up RCS keywords in %s" % file)
1704
1705 def p4UserForCommit(self,id):
1706 # Return the tuple (perforce user,git email) for a given git commit id
1707 self.getUserMapFromPerforceServer()
1708 gitEmail = read_pipe(["git", "log", "--max-count=1",
1709 "--format=%ae", id])
1710 gitEmail = gitEmail.strip()
1711 if gitEmail not in self.emails:
1712 return (None,gitEmail)
1713 else:
1714 return (self.emails[gitEmail],gitEmail)
1715
1716 def checkValidP4Users(self,commits):
1717 # check if any git authors cannot be mapped to p4 users
1718 for id in commits:
1719 (user,email) = self.p4UserForCommit(id)
1720 if not user:
1721 msg = "Cannot find p4 user for email %s in commit %s." % (email, id)
1722 if gitConfigBool("git-p4.allowMissingP4Users"):
1723 print("%s" % msg)
1724 else:
1725 die("Error: %s\nSet git-p4.allowMissingP4Users to true to allow this." % msg)
1726
1727 def lastP4Changelist(self):
1728 # Get back the last changelist number submitted in this client spec. This
1729 # then gets used to patch up the username in the change. If the same
1730 # client spec is being used by multiple processes then this might go
1731 # wrong.
1732 results = p4CmdList("client -o") # find the current client
1733 client = None
1734 for r in results:
1735 if 'Client' in r:
1736 client = r['Client']
1737 break
1738 if not client:
1739 die("could not get client spec")
1740 results = p4CmdList(["changes", "-c", client, "-m", "1"])
1741 for r in results:
1742 if 'change' in r:
1743 return r['change']
1744 die("Could not get changelist number for last submit - cannot patch up user details")
1745
1746 def modifyChangelistUser(self, changelist, newUser):
1747 # fixup the user field of a changelist after it has been submitted.
1748 changes = p4CmdList("change -o %s" % changelist)
1749 if len(changes) != 1:
1750 die("Bad output from p4 change modifying %s to user %s" %
1751 (changelist, newUser))
1752
1753 c = changes[0]
1754 if c['User'] == newUser: return # nothing to do
1755 c['User'] = newUser
1756 # p4 does not understand format version 3 and above
1757 input = marshal.dumps(c, 2)
1758
1759 result = p4CmdList("change -f -i", stdin=input)
1760 for r in result:
1761 if 'code' in r:
1762 if r['code'] == 'error':
1763 die("Could not modify user field of changelist %s to %s:%s" % (changelist, newUser, r['data']))
1764 if 'data' in r:
1765 print("Updated user field for changelist %s to %s" % (changelist, newUser))
1766 return
1767 die("Could not modify user field of changelist %s to %s" % (changelist, newUser))
1768
1769 def canChangeChangelists(self):
1770 # check to see if we have p4 admin or super-user permissions, either of
1771 # which are required to modify changelists.
1772 results = p4CmdList(["protects", self.depotPath])
1773 for r in results:
1774 if 'perm' in r:
1775 if r['perm'] == 'admin':
1776 return 1
1777 if r['perm'] == 'super':
1778 return 1
1779 return 0
1780
1781 def prepareSubmitTemplate(self, changelist=None):
1782 """Run "p4 change -o" to grab a change specification template.
1783 This does not use "p4 -G", as it is nice to keep the submission
1784 template in original order, since a human might edit it.
1785
1786 Remove lines in the Files section that show changes to files
1787 outside the depot path we're committing into."""
1788
1789 [upstream, settings] = findUpstreamBranchPoint()
1790
1791 template = """\
1792 # A Perforce Change Specification.
1793 #
1794 # Change: The change number. 'new' on a new changelist.
1795 # Date: The date this specification was last modified.
1796 # Client: The client on which the changelist was created. Read-only.
1797 # User: The user who created the changelist.
1798 # Status: Either 'pending' or 'submitted'. Read-only.
1799 # Type: Either 'public' or 'restricted'. Default is 'public'.
1800 # Description: Comments about the changelist. Required.
1801 # Jobs: What opened jobs are to be closed by this changelist.
1802 # You may delete jobs from this list. (New changelists only.)
1803 # Files: What opened files from the default changelist are to be added
1804 # to this changelist. You may delete files from this list.
1805 # (New changelists only.)
1806 """
1807 files_list = []
1808 inFilesSection = False
1809 change_entry = None
1810 args = ['change', '-o']
1811 if changelist:
1812 args.append(str(changelist))
1813 for entry in p4CmdList(args):
1814 if 'code' not in entry:
1815 continue
1816 if entry['code'] == 'stat':
1817 change_entry = entry
1818 break
1819 if not change_entry:
1820 die('Failed to decode output of p4 change -o')
1821 for key, value in change_entry.items():
1822 if key.startswith('File'):
1823 if 'depot-paths' in settings:
1824 if not [p for p in settings['depot-paths']
1825 if p4PathStartsWith(value, p)]:
1826 continue
1827 else:
1828 if not p4PathStartsWith(value, self.depotPath):
1829 continue
1830 files_list.append(value)
1831 continue
1832 # Output in the order expected by prepareLogMessage
1833 for key in ['Change', 'Client', 'User', 'Status', 'Description', 'Jobs']:
1834 if key not in change_entry:
1835 continue
1836 template += '\n'
1837 template += key + ':'
1838 if key == 'Description':
1839 template += '\n'
1840 for field_line in change_entry[key].splitlines():
1841 template += '\t'+field_line+'\n'
1842 if len(files_list) > 0:
1843 template += '\n'
1844 template += 'Files:\n'
1845 for path in files_list:
1846 template += '\t'+path+'\n'
1847 return template
1848
1849 def edit_template(self, template_file):
1850 """Invoke the editor to let the user change the submission
1851 message. Return true if okay to continue with the submit."""
1852
1853 # if configured to skip the editing part, just submit
1854 if gitConfigBool("git-p4.skipSubmitEdit"):
1855 return True
1856
1857 # look at the modification time, to check later if the user saved
1858 # the file
1859 mtime = os.stat(template_file).st_mtime
1860
1861 # invoke the editor
1862 if "P4EDITOR" in os.environ and (os.environ.get("P4EDITOR") != ""):
1863 editor = os.environ.get("P4EDITOR")
1864 else:
1865 editor = read_pipe("git var GIT_EDITOR").strip()
1866 system(["sh", "-c", ('%s "$@"' % editor), editor, template_file])
1867
1868 # If the file was not saved, prompt to see if this patch should
1869 # be skipped. But skip this verification step if configured so.
1870 if gitConfigBool("git-p4.skipSubmitEditCheck"):
1871 return True
1872
1873 # modification time updated means user saved the file
1874 if os.stat(template_file).st_mtime > mtime:
1875 return True
1876
1877 response = prompt("Submit template unchanged. Submit anyway? [y]es, [n]o (skip this patch) ")
1878 if response == 'y':
1879 return True
1880 if response == 'n':
1881 return False
1882
1883 def get_diff_description(self, editedFiles, filesToAdd, symlinks):
1884 # diff
1885 if "P4DIFF" in os.environ:
1886 del(os.environ["P4DIFF"])
1887 diff = ""
1888 for editedFile in editedFiles:
1889 diff += p4_read_pipe(['diff', '-du',
1890 wildcard_encode(editedFile)])
1891
1892 # new file diff
1893 newdiff = ""
1894 for newFile in filesToAdd:
1895 newdiff += "==== new file ====\n"
1896 newdiff += "--- /dev/null\n"
1897 newdiff += "+++ %s\n" % newFile
1898
1899 is_link = os.path.islink(newFile)
1900 expect_link = newFile in symlinks
1901
1902 if is_link and expect_link:
1903 newdiff += "+%s\n" % os.readlink(newFile)
1904 else:
1905 f = open(newFile, "r")
1906 try:
1907 for line in f.readlines():
1908 newdiff += "+" + line
1909 except UnicodeDecodeError:
1910 pass # Found non-text data and skip, since diff description should only include text
1911 f.close()
1912
1913 return (diff + newdiff).replace('\r\n', '\n')
1914
1915 def applyCommit(self, id):
1916 """Apply one commit, return True if it succeeded."""
1917
1918 print("Applying", read_pipe(["git", "show", "-s",
1919 "--format=format:%h %s", id]))
1920
1921 (p4User, gitEmail) = self.p4UserForCommit(id)
1922
1923 diff = read_pipe_lines("git diff-tree -r %s \"%s^\" \"%s\"" % (self.diffOpts, id, id))
1924 filesToAdd = set()
1925 filesToChangeType = set()
1926 filesToDelete = set()
1927 editedFiles = set()
1928 pureRenameCopy = set()
1929 symlinks = set()
1930 filesToChangeExecBit = {}
1931 all_files = list()
1932
1933 for line in diff:
1934 diff = parseDiffTreeEntry(line)
1935 modifier = diff['status']
1936 path = diff['src']
1937 all_files.append(path)
1938
1939 if modifier == "M":
1940 p4_edit(path)
1941 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
1942 filesToChangeExecBit[path] = diff['dst_mode']
1943 editedFiles.add(path)
1944 elif modifier == "A":
1945 filesToAdd.add(path)
1946 filesToChangeExecBit[path] = diff['dst_mode']
1947 if path in filesToDelete:
1948 filesToDelete.remove(path)
1949
1950 dst_mode = int(diff['dst_mode'], 8)
1951 if dst_mode == 0o120000:
1952 symlinks.add(path)
1953
1954 elif modifier == "D":
1955 filesToDelete.add(path)
1956 if path in filesToAdd:
1957 filesToAdd.remove(path)
1958 elif modifier == "C":
1959 src, dest = diff['src'], diff['dst']
1960 all_files.append(dest)
1961 p4_integrate(src, dest)
1962 pureRenameCopy.add(dest)
1963 if diff['src_sha1'] != diff['dst_sha1']:
1964 p4_edit(dest)
1965 pureRenameCopy.discard(dest)
1966 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
1967 p4_edit(dest)
1968 pureRenameCopy.discard(dest)
1969 filesToChangeExecBit[dest] = diff['dst_mode']
1970 if self.isWindows:
1971 # turn off read-only attribute
1972 os.chmod(dest, stat.S_IWRITE)
1973 os.unlink(dest)
1974 editedFiles.add(dest)
1975 elif modifier == "R":
1976 src, dest = diff['src'], diff['dst']
1977 all_files.append(dest)
1978 if self.p4HasMoveCommand:
1979 p4_edit(src) # src must be open before move
1980 p4_move(src, dest) # opens for (move/delete, move/add)
1981 else:
1982 p4_integrate(src, dest)
1983 if diff['src_sha1'] != diff['dst_sha1']:
1984 p4_edit(dest)
1985 else:
1986 pureRenameCopy.add(dest)
1987 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
1988 if not self.p4HasMoveCommand:
1989 p4_edit(dest) # with move: already open, writable
1990 filesToChangeExecBit[dest] = diff['dst_mode']
1991 if not self.p4HasMoveCommand:
1992 if self.isWindows:
1993 os.chmod(dest, stat.S_IWRITE)
1994 os.unlink(dest)
1995 filesToDelete.add(src)
1996 editedFiles.add(dest)
1997 elif modifier == "T":
1998 filesToChangeType.add(path)
1999 else:
2000 die("unknown modifier %s for %s" % (modifier, path))
2001
2002 diffcmd = "git diff-tree --full-index -p \"%s\"" % (id)
2003 patchcmd = diffcmd + " | git apply "
2004 tryPatchCmd = patchcmd + "--check -"
2005 applyPatchCmd = patchcmd + "--check --apply -"
2006 patch_succeeded = True
2007
2008 if verbose:
2009 print("TryPatch: %s" % tryPatchCmd)
2010
2011 if os.system(tryPatchCmd) != 0:
2012 fixed_rcs_keywords = False
2013 patch_succeeded = False
2014 print("Unfortunately applying the change failed!")
2015
2016 # Patch failed, maybe it's just RCS keyword woes. Look through
2017 # the patch to see if that's possible.
2018 if gitConfigBool("git-p4.attemptRCSCleanup"):
2019 file = None
2020 pattern = None
2021 kwfiles = {}
2022 for file in editedFiles | filesToDelete:
2023 # did this file's delta contain RCS keywords?
2024 pattern = p4_keywords_regexp_for_file(file)
2025
2026 if pattern:
2027 # this file is a possibility...look for RCS keywords.
2028 regexp = re.compile(pattern, re.VERBOSE)
2029 for line in read_pipe_lines(["git", "diff", "%s^..%s" % (id, id), file]):
2030 if regexp.search(line):
2031 if verbose:
2032 print("got keyword match on %s in %s in %s" % (pattern, line, file))
2033 kwfiles[file] = pattern
2034 break
2035
2036 for file in kwfiles:
2037 if verbose:
2038 print("zapping %s with %s" % (line,pattern))
2039 # File is being deleted, so not open in p4. Must
2040 # disable the read-only bit on windows.
2041 if self.isWindows and file not in editedFiles:
2042 os.chmod(file, stat.S_IWRITE)
2043 self.patchRCSKeywords(file, kwfiles[file])
2044 fixed_rcs_keywords = True
2045
2046 if fixed_rcs_keywords:
2047 print("Retrying the patch with RCS keywords cleaned up")
2048 if os.system(tryPatchCmd) == 0:
2049 patch_succeeded = True
2050 print("Patch succeesed this time with RCS keywords cleaned")
2051
2052 if not patch_succeeded:
2053 for f in editedFiles:
2054 p4_revert(f)
2055 return False
2056
2057 #
2058 # Apply the patch for real, and do add/delete/+x handling.
2059 #
2060 system(applyPatchCmd)
2061
2062 for f in filesToChangeType:
2063 p4_edit(f, "-t", "auto")
2064 for f in filesToAdd:
2065 p4_add(f)
2066 for f in filesToDelete:
2067 p4_revert(f)
2068 p4_delete(f)
2069
2070 # Set/clear executable bits
2071 for f in filesToChangeExecBit.keys():
2072 mode = filesToChangeExecBit[f]
2073 setP4ExecBit(f, mode)
2074
2075 update_shelve = 0
2076 if len(self.update_shelve) > 0:
2077 update_shelve = self.update_shelve.pop(0)
2078 p4_reopen_in_change(update_shelve, all_files)
2079
2080 #
2081 # Build p4 change description, starting with the contents
2082 # of the git commit message.
2083 #
2084 logMessage = extractLogMessageFromGitCommit(id)
2085 logMessage = logMessage.strip()
2086 (logMessage, jobs) = self.separate_jobs_from_description(logMessage)
2087
2088 template = self.prepareSubmitTemplate(update_shelve)
2089 submitTemplate = self.prepareLogMessage(template, logMessage, jobs)
2090
2091 if self.preserveUser:
2092 submitTemplate += "\n######## Actual user %s, modified after commit\n" % p4User
2093
2094 if self.checkAuthorship and not self.p4UserIsMe(p4User):
2095 submitTemplate += "######## git author %s does not match your p4 account.\n" % gitEmail
2096 submitTemplate += "######## Use option --preserve-user to modify authorship.\n"
2097 submitTemplate += "######## Variable git-p4.skipUserNameCheck hides this message.\n"
2098
2099 separatorLine = "######## everything below this line is just the diff #######\n"
2100 if not self.prepare_p4_only:
2101 submitTemplate += separatorLine
2102 submitTemplate += self.get_diff_description(editedFiles, filesToAdd, symlinks)
2103
2104 (handle, fileName) = tempfile.mkstemp()
2105 tmpFile = os.fdopen(handle, "w+b")
2106 if self.isWindows:
2107 submitTemplate = submitTemplate.replace("\n", "\r\n")
2108 tmpFile.write(encode_text_stream(submitTemplate))
2109 tmpFile.close()
2110
2111 submitted = False
2112
2113 try:
2114 # Allow the hook to edit the changelist text before presenting it
2115 # to the user.
2116 if not run_git_hook("p4-prepare-changelist", [fileName]):
2117 return False
2118
2119 if self.prepare_p4_only:
2120 #
2121 # Leave the p4 tree prepared, and the submit template around
2122 # and let the user decide what to do next
2123 #
2124 submitted = True
2125 print("")
2126 print("P4 workspace prepared for submission.")
2127 print("To submit or revert, go to client workspace")
2128 print(" " + self.clientPath)
2129 print("")
2130 print("To submit, use \"p4 submit\" to write a new description,")
2131 print("or \"p4 submit -i <%s\" to use the one prepared by" \
2132 " \"git p4\"." % fileName)
2133 print("You can delete the file \"%s\" when finished." % fileName)
2134
2135 if self.preserveUser and p4User and not self.p4UserIsMe(p4User):
2136 print("To preserve change ownership by user %s, you must\n" \
2137 "do \"p4 change -f <change>\" after submitting and\n" \
2138 "edit the User field.")
2139 if pureRenameCopy:
2140 print("After submitting, renamed files must be re-synced.")
2141 print("Invoke \"p4 sync -f\" on each of these files:")
2142 for f in pureRenameCopy:
2143 print(" " + f)
2144
2145 print("")
2146 print("To revert the changes, use \"p4 revert ...\", and delete")
2147 print("the submit template file \"%s\"" % fileName)
2148 if filesToAdd:
2149 print("Since the commit adds new files, they must be deleted:")
2150 for f in filesToAdd:
2151 print(" " + f)
2152 print("")
2153 sys.stdout.flush()
2154 return True
2155
2156 if self.edit_template(fileName):
2157 if not self.no_verify:
2158 if not run_git_hook("p4-changelist", [fileName]):
2159 print("The p4-changelist hook failed.")
2160 sys.stdout.flush()
2161 return False
2162
2163 # read the edited message and submit
2164 tmpFile = open(fileName, "rb")
2165 message = decode_text_stream(tmpFile.read())
2166 tmpFile.close()
2167 if self.isWindows:
2168 message = message.replace("\r\n", "\n")
2169 if message.find(separatorLine) != -1:
2170 submitTemplate = message[:message.index(separatorLine)]
2171 else:
2172 submitTemplate = message
2173
2174 if len(submitTemplate.strip()) == 0:
2175 print("Changelist is empty, aborting this changelist.")
2176 sys.stdout.flush()
2177 return False
2178
2179 if update_shelve:
2180 p4_write_pipe(['shelve', '-r', '-i'], submitTemplate)
2181 elif self.shelve:
2182 p4_write_pipe(['shelve', '-i'], submitTemplate)
2183 else:
2184 p4_write_pipe(['submit', '-i'], submitTemplate)
2185 # The rename/copy happened by applying a patch that created a
2186 # new file. This leaves it writable, which confuses p4.
2187 for f in pureRenameCopy:
2188 p4_sync(f, "-f")
2189
2190 if self.preserveUser:
2191 if p4User:
2192 # Get last changelist number. Cannot easily get it from
2193 # the submit command output as the output is
2194 # unmarshalled.
2195 changelist = self.lastP4Changelist()
2196 self.modifyChangelistUser(changelist, p4User)
2197
2198 submitted = True
2199
2200 run_git_hook("p4-post-changelist")
2201 finally:
2202 # Revert changes if we skip this patch
2203 if not submitted or self.shelve:
2204 if self.shelve:
2205 print ("Reverting shelved files.")
2206 else:
2207 print ("Submission cancelled, undoing p4 changes.")
2208 sys.stdout.flush()
2209 for f in editedFiles | filesToDelete:
2210 p4_revert(f)
2211 for f in filesToAdd:
2212 p4_revert(f)
2213 os.remove(f)
2214
2215 if not self.prepare_p4_only:
2216 os.remove(fileName)
2217 return submitted
2218
2219 # Export git tags as p4 labels. Create a p4 label and then tag
2220 # with that.
2221 def exportGitTags(self, gitTags):
2222 validLabelRegexp = gitConfig("git-p4.labelExportRegexp")
2223 if len(validLabelRegexp) == 0:
2224 validLabelRegexp = defaultLabelRegexp
2225 m = re.compile(validLabelRegexp)
2226
2227 for name in gitTags:
2228
2229 if not m.match(name):
2230 if verbose:
2231 print("tag %s does not match regexp %s" % (name, validLabelRegexp))
2232 continue
2233
2234 # Get the p4 commit this corresponds to
2235 logMessage = extractLogMessageFromGitCommit(name)
2236 values = extractSettingsGitLog(logMessage)
2237
2238 if 'change' not in values:
2239 # a tag pointing to something not sent to p4; ignore
2240 if verbose:
2241 print("git tag %s does not give a p4 commit" % name)
2242 continue
2243 else:
2244 changelist = values['change']
2245
2246 # Get the tag details.
2247 inHeader = True
2248 isAnnotated = False
2249 body = []
2250 for l in read_pipe_lines(["git", "cat-file", "-p", name]):
2251 l = l.strip()
2252 if inHeader:
2253 if re.match(r'tag\s+', l):
2254 isAnnotated = True
2255 elif re.match(r'\s*$', l):
2256 inHeader = False
2257 continue
2258 else:
2259 body.append(l)
2260
2261 if not isAnnotated:
2262 body = ["lightweight tag imported by git p4\n"]
2263
2264 # Create the label - use the same view as the client spec we are using
2265 clientSpec = getClientSpec()
2266
2267 labelTemplate = "Label: %s\n" % name
2268 labelTemplate += "Description:\n"
2269 for b in body:
2270 labelTemplate += "\t" + b + "\n"
2271 labelTemplate += "View:\n"
2272 for depot_side in clientSpec.mappings:
2273 labelTemplate += "\t%s\n" % depot_side
2274
2275 if self.dry_run:
2276 print("Would create p4 label %s for tag" % name)
2277 elif self.prepare_p4_only:
2278 print("Not creating p4 label %s for tag due to option" \
2279 " --prepare-p4-only" % name)
2280 else:
2281 p4_write_pipe(["label", "-i"], labelTemplate)
2282
2283 # Use the label
2284 p4_system(["tag", "-l", name] +
2285 ["%s@%s" % (depot_side, changelist) for depot_side in clientSpec.mappings])
2286
2287 if verbose:
2288 print("created p4 label for tag %s" % name)
2289
2290 def run(self, args):
2291 if len(args) == 0:
2292 self.master = currentGitBranch()
2293 elif len(args) == 1:
2294 self.master = args[0]
2295 if not branchExists(self.master):
2296 die("Branch %s does not exist" % self.master)
2297 else:
2298 return False
2299
2300 for i in self.update_shelve:
2301 if i <= 0:
2302 sys.exit("invalid changelist %d" % i)
2303
2304 if self.master:
2305 allowSubmit = gitConfig("git-p4.allowSubmit")
2306 if len(allowSubmit) > 0 and not self.master in allowSubmit.split(","):
2307 die("%s is not in git-p4.allowSubmit" % self.master)
2308
2309 [upstream, settings] = findUpstreamBranchPoint()
2310 self.depotPath = settings['depot-paths'][0]
2311 if len(self.origin) == 0:
2312 self.origin = upstream
2313
2314 if len(self.update_shelve) > 0:
2315 self.shelve = True
2316
2317 if self.preserveUser:
2318 if not self.canChangeChangelists():
2319 die("Cannot preserve user names without p4 super-user or admin permissions")
2320
2321 # if not set from the command line, try the config file
2322 if self.conflict_behavior is None:
2323 val = gitConfig("git-p4.conflict")
2324 if val:
2325 if val not in self.conflict_behavior_choices:
2326 die("Invalid value '%s' for config git-p4.conflict" % val)
2327 else:
2328 val = "ask"
2329 self.conflict_behavior = val
2330
2331 if self.verbose:
2332 print("Origin branch is " + self.origin)
2333
2334 if len(self.depotPath) == 0:
2335 print("Internal error: cannot locate perforce depot path from existing branches")
2336 sys.exit(128)
2337
2338 self.useClientSpec = False
2339 if gitConfigBool("git-p4.useclientspec"):
2340 self.useClientSpec = True
2341 if self.useClientSpec:
2342 self.clientSpecDirs = getClientSpec()
2343
2344 # Check for the existence of P4 branches
2345 branchesDetected = (len(p4BranchesInGit().keys()) > 1)
2346
2347 if self.useClientSpec and not branchesDetected:
2348 # all files are relative to the client spec
2349 self.clientPath = getClientRoot()
2350 else:
2351 self.clientPath = p4Where(self.depotPath)
2352
2353 if self.clientPath == "":
2354 die("Error: Cannot locate perforce checkout of %s in client view" % self.depotPath)
2355
2356 print("Perforce checkout for depot path %s located at %s" % (self.depotPath, self.clientPath))
2357 self.oldWorkingDirectory = os.getcwd()
2358
2359 # ensure the clientPath exists
2360 new_client_dir = False
2361 if not os.path.exists(self.clientPath):
2362 new_client_dir = True
2363 os.makedirs(self.clientPath)
2364
2365 chdir(self.clientPath, is_client_path=True)
2366 if self.dry_run:
2367 print("Would synchronize p4 checkout in %s" % self.clientPath)
2368 else:
2369 print("Synchronizing p4 checkout...")
2370 if new_client_dir:
2371 # old one was destroyed, and maybe nobody told p4
2372 p4_sync("...", "-f")
2373 else:
2374 p4_sync("...")
2375 self.check()
2376
2377 commits = []
2378 if self.master:
2379 committish = self.master
2380 else:
2381 committish = 'HEAD'
2382
2383 if self.commit != "":
2384 if self.commit.find("..") != -1:
2385 limits_ish = self.commit.split("..")
2386 for line in read_pipe_lines(["git", "rev-list", "--no-merges", "%s..%s" % (limits_ish[0], limits_ish[1])]):
2387 commits.append(line.strip())
2388 commits.reverse()
2389 else:
2390 commits.append(self.commit)
2391 else:
2392 for line in read_pipe_lines(["git", "rev-list", "--no-merges", "%s..%s" % (self.origin, committish)]):
2393 commits.append(line.strip())
2394 commits.reverse()
2395
2396 if self.preserveUser or gitConfigBool("git-p4.skipUserNameCheck"):
2397 self.checkAuthorship = False
2398 else:
2399 self.checkAuthorship = True
2400
2401 if self.preserveUser:
2402 self.checkValidP4Users(commits)
2403
2404 #
2405 # Build up a set of options to be passed to diff when
2406 # submitting each commit to p4.
2407 #
2408 if self.detectRenames:
2409 # command-line -M arg
2410 self.diffOpts = "-M"
2411 else:
2412 # If not explicitly set check the config variable
2413 detectRenames = gitConfig("git-p4.detectRenames")
2414
2415 if detectRenames.lower() == "false" or detectRenames == "":
2416 self.diffOpts = ""
2417 elif detectRenames.lower() == "true":
2418 self.diffOpts = "-M"
2419 else:
2420 self.diffOpts = "-M%s" % detectRenames
2421
2422 # no command-line arg for -C or --find-copies-harder, just
2423 # config variables
2424 detectCopies = gitConfig("git-p4.detectCopies")
2425 if detectCopies.lower() == "false" or detectCopies == "":
2426 pass
2427 elif detectCopies.lower() == "true":
2428 self.diffOpts += " -C"
2429 else:
2430 self.diffOpts += " -C%s" % detectCopies
2431
2432 if gitConfigBool("git-p4.detectCopiesHarder"):
2433 self.diffOpts += " --find-copies-harder"
2434
2435 num_shelves = len(self.update_shelve)
2436 if num_shelves > 0 and num_shelves != len(commits):
2437 sys.exit("number of commits (%d) must match number of shelved changelist (%d)" %
2438 (len(commits), num_shelves))
2439
2440 if not self.no_verify:
2441 try:
2442 if not run_git_hook("p4-pre-submit"):
2443 print("\nThe p4-pre-submit hook failed, aborting the submit.\n\nYou can skip " \
2444 "this pre-submission check by adding\nthe command line option '--no-verify', " \
2445 "however,\nthis will also skip the p4-changelist hook as well.")
2446 sys.exit(1)
2447 except Exception as e:
2448 print("\nThe p4-pre-submit hook failed, aborting the submit.\n\nThe hook failed "\
2449 "with the error '{0}'".format(e.message) )
2450 sys.exit(1)
2451
2452 #
2453 # Apply the commits, one at a time. On failure, ask if should
2454 # continue to try the rest of the patches, or quit.
2455 #
2456 if self.dry_run:
2457 print("Would apply")
2458 applied = []
2459 last = len(commits) - 1
2460 for i, commit in enumerate(commits):
2461 if self.dry_run:
2462 print(" ", read_pipe(["git", "show", "-s",
2463 "--format=format:%h %s", commit]))
2464 ok = True
2465 else:
2466 ok = self.applyCommit(commit)
2467 if ok:
2468 applied.append(commit)
2469 if self.prepare_p4_only:
2470 if i < last:
2471 print("Processing only the first commit due to option" \
2472 " --prepare-p4-only")
2473 break
2474 else:
2475 if i < last:
2476 # prompt for what to do, or use the option/variable
2477 if self.conflict_behavior == "ask":
2478 print("What do you want to do?")
2479 response = prompt("[s]kip this commit but apply the rest, or [q]uit? ")
2480 elif self.conflict_behavior == "skip":
2481 response = "s"
2482 elif self.conflict_behavior == "quit":
2483 response = "q"
2484 else:
2485 die("Unknown conflict_behavior '%s'" %
2486 self.conflict_behavior)
2487
2488 if response == "s":
2489 print("Skipping this commit, but applying the rest")
2490 if response == "q":
2491 print("Quitting")
2492 break
2493
2494 chdir(self.oldWorkingDirectory)
2495 shelved_applied = "shelved" if self.shelve else "applied"
2496 if self.dry_run:
2497 pass
2498 elif self.prepare_p4_only:
2499 pass
2500 elif len(commits) == len(applied):
2501 print("All commits {0}!".format(shelved_applied))
2502
2503 sync = P4Sync()
2504 if self.branch:
2505 sync.branch = self.branch
2506 if self.disable_p4sync:
2507 sync.sync_origin_only()
2508 else:
2509 sync.run([])
2510
2511 if not self.disable_rebase:
2512 rebase = P4Rebase()
2513 rebase.rebase()
2514
2515 else:
2516 if len(applied) == 0:
2517 print("No commits {0}.".format(shelved_applied))
2518 else:
2519 print("{0} only the commits marked with '*':".format(shelved_applied.capitalize()))
2520 for c in commits:
2521 if c in applied:
2522 star = "*"
2523 else:
2524 star = " "
2525 print(star, read_pipe(["git", "show", "-s",
2526 "--format=format:%h %s", c]))
2527 print("You will have to do 'git p4 sync' and rebase.")
2528
2529 if gitConfigBool("git-p4.exportLabels"):
2530 self.exportLabels = True
2531
2532 if self.exportLabels:
2533 p4Labels = getP4Labels(self.depotPath)
2534 gitTags = getGitTags()
2535
2536 missingGitTags = gitTags - p4Labels
2537 self.exportGitTags(missingGitTags)
2538
2539 # exit with error unless everything applied perfectly
2540 if len(commits) != len(applied):
2541 sys.exit(1)
2542
2543 return True
2544
2545 class View(object):
2546 """Represent a p4 view ("p4 help views"), and map files in a
2547 repo according to the view."""
2548
2549 def __init__(self, client_name):
2550 self.mappings = []
2551 self.client_prefix = "//%s/" % client_name
2552 # cache results of "p4 where" to lookup client file locations
2553 self.client_spec_path_cache = {}
2554
2555 def append(self, view_line):
2556 """Parse a view line, splitting it into depot and client
2557 sides. Append to self.mappings, preserving order. This
2558 is only needed for tag creation."""
2559
2560 # Split the view line into exactly two words. P4 enforces
2561 # structure on these lines that simplifies this quite a bit.
2562 #
2563 # Either or both words may be double-quoted.
2564 # Single quotes do not matter.
2565 # Double-quote marks cannot occur inside the words.
2566 # A + or - prefix is also inside the quotes.
2567 # There are no quotes unless they contain a space.
2568 # The line is already white-space stripped.
2569 # The two words are separated by a single space.
2570 #
2571 if view_line[0] == '"':
2572 # First word is double quoted. Find its end.
2573 close_quote_index = view_line.find('"', 1)
2574 if close_quote_index <= 0:
2575 die("No first-word closing quote found: %s" % view_line)
2576 depot_side = view_line[1:close_quote_index]
2577 # skip closing quote and space
2578 rhs_index = close_quote_index + 1 + 1
2579 else:
2580 space_index = view_line.find(" ")
2581 if space_index <= 0:
2582 die("No word-splitting space found: %s" % view_line)
2583 depot_side = view_line[0:space_index]
2584 rhs_index = space_index + 1
2585
2586 # prefix + means overlay on previous mapping
2587 if depot_side.startswith("+"):
2588 depot_side = depot_side[1:]
2589
2590 # prefix - means exclude this path, leave out of mappings
2591 exclude = False
2592 if depot_side.startswith("-"):
2593 exclude = True
2594 depot_side = depot_side[1:]
2595
2596 if not exclude:
2597 self.mappings.append(depot_side)
2598
2599 def convert_client_path(self, clientFile):
2600 # chop off //client/ part to make it relative
2601 if not decode_path(clientFile).startswith(self.client_prefix):
2602 die("No prefix '%s' on clientFile '%s'" %
2603 (self.client_prefix, clientFile))
2604 return clientFile[len(self.client_prefix):]
2605
2606 def update_client_spec_path_cache(self, files):
2607 """ Caching file paths by "p4 where" batch query """
2608
2609 # List depot file paths exclude that already cached
2610 fileArgs = [f['path'] for f in files if decode_path(f['path']) not in self.client_spec_path_cache]
2611
2612 if len(fileArgs) == 0:
2613 return # All files in cache
2614
2615 where_result = p4CmdList(["-x", "-", "where"], stdin=fileArgs)
2616 for res in where_result:
2617 if "code" in res and res["code"] == "error":
2618 # assume error is "... file(s) not in client view"
2619 continue
2620 if "clientFile" not in res:
2621 die("No clientFile in 'p4 where' output")
2622 if "unmap" in res:
2623 # it will list all of them, but only one not unmap-ped
2624 continue
2625 depot_path = decode_path(res['depotFile'])
2626 if gitConfigBool("core.ignorecase"):
2627 depot_path = depot_path.lower()
2628 self.client_spec_path_cache[depot_path] = self.convert_client_path(res["clientFile"])
2629
2630 # not found files or unmap files set to ""
2631 for depotFile in fileArgs:
2632 depotFile = decode_path(depotFile)
2633 if gitConfigBool("core.ignorecase"):
2634 depotFile = depotFile.lower()
2635 if depotFile not in self.client_spec_path_cache:
2636 self.client_spec_path_cache[depotFile] = b''
2637
2638 def map_in_client(self, depot_path):
2639 """Return the relative location in the client where this
2640 depot file should live. Returns "" if the file should
2641 not be mapped in the client."""
2642
2643 if gitConfigBool("core.ignorecase"):
2644 depot_path = depot_path.lower()
2645
2646 if depot_path in self.client_spec_path_cache:
2647 return self.client_spec_path_cache[depot_path]
2648
2649 die( "Error: %s is not found in client spec path" % depot_path )
2650 return ""
2651
2652 def cloneExcludeCallback(option, opt_str, value, parser):
2653 # prepend "/" because the first "/" was consumed as part of the option itself.
2654 # ("-//depot/A/..." becomes "/depot/A/..." after option parsing)
2655 parser.values.cloneExclude += ["/" + re.sub(r"\.\.\.$", "", value)]
2656
2657 class P4Sync(Command, P4UserMap):
2658
2659 def __init__(self):
2660 Command.__init__(self)
2661 P4UserMap.__init__(self)
2662 self.options = [
2663 optparse.make_option("--branch", dest="branch"),
2664 optparse.make_option("--detect-branches", dest="detectBranches", action="store_true"),
2665 optparse.make_option("--changesfile", dest="changesFile"),
2666 optparse.make_option("--silent", dest="silent", action="store_true"),
2667 optparse.make_option("--detect-labels", dest="detectLabels", action="store_true"),
2668 optparse.make_option("--import-labels", dest="importLabels", action="store_true"),
2669 optparse.make_option("--import-local", dest="importIntoRemotes", action="store_false",
2670 help="Import into refs/heads/ , not refs/remotes"),
2671 optparse.make_option("--max-changes", dest="maxChanges",
2672 help="Maximum number of changes to import"),
2673 optparse.make_option("--changes-block-size", dest="changes_block_size", type="int",
2674 help="Internal block size to use when iteratively calling p4 changes"),
2675 optparse.make_option("--keep-path", dest="keepRepoPath", action='store_true',
2676 help="Keep entire BRANCH/DIR/SUBDIR prefix during import"),
2677 optparse.make_option("--use-client-spec", dest="useClientSpec", action='store_true',
2678 help="Only sync files that are included in the Perforce Client Spec"),
2679 optparse.make_option("-/", dest="cloneExclude",
2680 action="callback", callback=cloneExcludeCallback, type="string",
2681 help="exclude depot path"),
2682 ]
2683 self.description = """Imports from Perforce into a git repository.\n
2684 example:
2685 //depot/my/project/ -- to import the current head
2686 //depot/my/project/@all -- to import everything
2687 //depot/my/project/@1,6 -- to import only from revision 1 to 6
2688
2689 (a ... is not needed in the path p4 specification, it's added implicitly)"""
2690
2691 self.usage += " //depot/path[@revRange]"
2692 self.silent = False
2693 self.createdBranches = set()
2694 self.committedChanges = set()
2695 self.branch = ""
2696 self.detectBranches = False
2697 self.detectLabels = False
2698 self.importLabels = False
2699 self.changesFile = ""
2700 self.syncWithOrigin = True
2701 self.importIntoRemotes = True
2702 self.maxChanges = ""
2703 self.changes_block_size = None
2704 self.keepRepoPath = False
2705 self.depotPaths = None
2706 self.p4BranchesInGit = []
2707 self.cloneExclude = []
2708 self.useClientSpec = False
2709 self.useClientSpec_from_options = False
2710 self.clientSpecDirs = None
2711 self.tempBranches = []
2712 self.tempBranchLocation = "refs/git-p4-tmp"
2713 self.largeFileSystem = None
2714 self.suppress_meta_comment = False
2715
2716 if gitConfig('git-p4.largeFileSystem'):
2717 largeFileSystemConstructor = globals()[gitConfig('git-p4.largeFileSystem')]
2718 self.largeFileSystem = largeFileSystemConstructor(
2719 lambda git_mode, relPath, contents: self.writeToGitStream(git_mode, relPath, contents)
2720 )
2721
2722 if gitConfig("git-p4.syncFromOrigin") == "false":
2723 self.syncWithOrigin = False
2724
2725 self.depotPaths = []
2726 self.changeRange = ""
2727 self.previousDepotPaths = []
2728 self.hasOrigin = False
2729
2730 # map from branch depot path to parent branch
2731 self.knownBranches = {}
2732 self.initialParents = {}
2733
2734 self.tz = "%+03d%02d" % (- time.timezone / 3600, ((- time.timezone % 3600) / 60))
2735 self.labels = {}
2736
2737 # Force a checkpoint in fast-import and wait for it to finish
2738 def checkpoint(self):
2739 self.gitStream.write("checkpoint\n\n")
2740 self.gitStream.write("progress checkpoint\n\n")
2741 self.gitStream.flush()
2742 out = self.gitOutput.readline()
2743 if self.verbose:
2744 print("checkpoint finished: " + out)
2745
2746 def isPathWanted(self, path):
2747 for p in self.cloneExclude:
2748 if p.endswith("/"):
2749 if p4PathStartsWith(path, p):
2750 return False
2751 # "-//depot/file1" without a trailing "/" should only exclude "file1", but not "file111" or "file1_dir/file2"
2752 elif path.lower() == p.lower():
2753 return False
2754 for p in self.depotPaths:
2755 if p4PathStartsWith(path, decode_path(p)):
2756 return True
2757 return False
2758
2759 def extractFilesFromCommit(self, commit, shelved=False, shelved_cl = 0):
2760 files = []
2761 fnum = 0
2762 while "depotFile%s" % fnum in commit:
2763 path = commit["depotFile%s" % fnum]
2764 found = self.isPathWanted(decode_path(path))
2765 if not found:
2766 fnum = fnum + 1
2767 continue
2768
2769 file = {}
2770 file["path"] = path
2771 file["rev"] = commit["rev%s" % fnum]
2772 file["action"] = commit["action%s" % fnum]
2773 file["type"] = commit["type%s" % fnum]
2774 if shelved:
2775 file["shelved_cl"] = int(shelved_cl)
2776 files.append(file)
2777 fnum = fnum + 1
2778 return files
2779
2780 def extractJobsFromCommit(self, commit):
2781 jobs = []
2782 jnum = 0
2783 while "job%s" % jnum in commit:
2784 job = commit["job%s" % jnum]
2785 jobs.append(job)
2786 jnum = jnum + 1
2787 return jobs
2788
2789 def stripRepoPath(self, path, prefixes):
2790 """When streaming files, this is called to map a p4 depot path
2791 to where it should go in git. The prefixes are either
2792 self.depotPaths, or self.branchPrefixes in the case of
2793 branch detection."""
2794
2795 if self.useClientSpec:
2796 # branch detection moves files up a level (the branch name)
2797 # from what client spec interpretation gives
2798 path = decode_path(self.clientSpecDirs.map_in_client(path))
2799 if self.detectBranches:
2800 for b in self.knownBranches:
2801 if p4PathStartsWith(path, b + "/"):
2802 path = path[len(b)+1:]
2803
2804 elif self.keepRepoPath:
2805 # Preserve everything in relative path name except leading
2806 # //depot/; just look at first prefix as they all should
2807 # be in the same depot.
2808 depot = re.sub("^(//[^/]+/).*", r'\1', prefixes[0])
2809 if p4PathStartsWith(path, depot):
2810 path = path[len(depot):]
2811
2812 else:
2813 for p in prefixes:
2814 if p4PathStartsWith(path, p):
2815 path = path[len(p):]
2816 break
2817
2818 path = wildcard_decode(path)
2819 return path
2820
2821 def splitFilesIntoBranches(self, commit):
2822 """Look at each depotFile in the commit to figure out to what
2823 branch it belongs."""
2824
2825 if self.clientSpecDirs:
2826 files = self.extractFilesFromCommit(commit)
2827 self.clientSpecDirs.update_client_spec_path_cache(files)
2828
2829 branches = {}
2830 fnum = 0
2831 while "depotFile%s" % fnum in commit:
2832 raw_path = commit["depotFile%s" % fnum]
2833 path = decode_path(raw_path)
2834 found = self.isPathWanted(path)
2835 if not found:
2836 fnum = fnum + 1
2837 continue
2838
2839 file = {}
2840 file["path"] = raw_path
2841 file["rev"] = commit["rev%s" % fnum]
2842 file["action"] = commit["action%s" % fnum]
2843 file["type"] = commit["type%s" % fnum]
2844 fnum = fnum + 1
2845
2846 # start with the full relative path where this file would
2847 # go in a p4 client
2848 if self.useClientSpec:
2849 relPath = decode_path(self.clientSpecDirs.map_in_client(path))
2850 else:
2851 relPath = self.stripRepoPath(path, self.depotPaths)
2852
2853 for branch in self.knownBranches.keys():
2854 # add a trailing slash so that a commit into qt/4.2foo
2855 # doesn't end up in qt/4.2, e.g.
2856 if p4PathStartsWith(relPath, branch + "/"):
2857 if branch not in branches:
2858 branches[branch] = []
2859 branches[branch].append(file)
2860 break
2861
2862 return branches
2863
2864 def writeToGitStream(self, gitMode, relPath, contents):
2865 self.gitStream.write(encode_text_stream(u'M {} inline {}\n'.format(gitMode, relPath)))
2866 self.gitStream.write('data %d\n' % sum(len(d) for d in contents))
2867 for d in contents:
2868 self.gitStream.write(d)
2869 self.gitStream.write('\n')
2870
2871 def encodeWithUTF8(self, path):
2872 try:
2873 path.decode('ascii')
2874 except:
2875 encoding = 'utf8'
2876 if gitConfig('git-p4.pathEncoding'):
2877 encoding = gitConfig('git-p4.pathEncoding')
2878 path = path.decode(encoding, 'replace').encode('utf8', 'replace')
2879 if self.verbose:
2880 print('Path with non-ASCII characters detected. Used %s to encode: %s ' % (encoding, path))
2881 return path
2882
2883 # output one file from the P4 stream
2884 # - helper for streamP4Files
2885
2886 def streamOneP4File(self, file, contents):
2887 file_path = file['depotFile']
2888 relPath = self.stripRepoPath(decode_path(file_path), self.branchPrefixes)
2889
2890 if verbose:
2891 if 'fileSize' in self.stream_file:
2892 size = int(self.stream_file['fileSize'])
2893 else:
2894 size = 0 # deleted files don't get a fileSize apparently
2895 sys.stdout.write('\r%s --> %s (%i MB)\n' % (file_path, relPath, size/1024/1024))
2896 sys.stdout.flush()
2897
2898 (type_base, type_mods) = split_p4_type(file["type"])
2899
2900 git_mode = "100644"
2901 if "x" in type_mods:
2902 git_mode = "100755"
2903 if type_base == "symlink":
2904 git_mode = "120000"
2905 # p4 print on a symlink sometimes contains "target\n";
2906 # if it does, remove the newline
2907 data = ''.join(decode_text_stream(c) for c in contents)
2908 if not data:
2909 # Some version of p4 allowed creating a symlink that pointed
2910 # to nothing. This causes p4 errors when checking out such
2911 # a change, and errors here too. Work around it by ignoring
2912 # the bad symlink; hopefully a future change fixes it.
2913 print("\nIgnoring empty symlink in %s" % file_path)
2914 return
2915 elif data[-1] == '\n':
2916 contents = [data[:-1]]
2917 else:
2918 contents = [data]
2919
2920 if type_base == "utf16":
2921 # p4 delivers different text in the python output to -G
2922 # than it does when using "print -o", or normal p4 client
2923 # operations. utf16 is converted to ascii or utf8, perhaps.
2924 # But ascii text saved as -t utf16 is completely mangled.
2925 # Invoke print -o to get the real contents.
2926 #
2927 # On windows, the newlines will always be mangled by print, so put
2928 # them back too. This is not needed to the cygwin windows version,
2929 # just the native "NT" type.
2930 #
2931 try:
2932 text = p4_read_pipe(['print', '-q', '-o', '-', '%s@%s' % (decode_path(file['depotFile']), file['change'])], raw=True)
2933 except Exception as e:
2934 if 'Translation of file content failed' in str(e):
2935 type_base = 'binary'
2936 else:
2937 raise e
2938 else:
2939 if p4_version_string().find('/NT') >= 0:
2940 text = text.replace(b'\r\n', b'\n')
2941 contents = [ text ]
2942
2943 if type_base == "apple":
2944 # Apple filetype files will be streamed as a concatenation of
2945 # its appledouble header and the contents. This is useless
2946 # on both macs and non-macs. If using "print -q -o xx", it
2947 # will create "xx" with the data, and "%xx" with the header.
2948 # This is also not very useful.
2949 #
2950 # Ideally, someday, this script can learn how to generate
2951 # appledouble files directly and import those to git, but
2952 # non-mac machines can never find a use for apple filetype.
2953 print("\nIgnoring apple filetype file %s" % file['depotFile'])
2954 return
2955
2956 # Note that we do not try to de-mangle keywords on utf16 files,
2957 # even though in theory somebody may want that.
2958 pattern = p4_keywords_regexp_for_type(type_base, type_mods)
2959 if pattern:
2960 regexp = re.compile(pattern, re.VERBOSE)
2961 text = ''.join(decode_text_stream(c) for c in contents)
2962 text = regexp.sub(r'$\1$', text)
2963 contents = [ encode_text_stream(text) ]
2964
2965 if self.largeFileSystem:
2966 (git_mode, contents) = self.largeFileSystem.processContent(git_mode, relPath, contents)
2967
2968 self.writeToGitStream(git_mode, relPath, contents)
2969
2970 def streamOneP4Deletion(self, file):
2971 relPath = self.stripRepoPath(decode_path(file['path']), self.branchPrefixes)
2972 if verbose:
2973 sys.stdout.write("delete %s\n" % relPath)
2974 sys.stdout.flush()
2975 self.gitStream.write(encode_text_stream(u'D {}\n'.format(relPath)))
2976
2977 if self.largeFileSystem and self.largeFileSystem.isLargeFile(relPath):
2978 self.largeFileSystem.removeLargeFile(relPath)
2979
2980 # handle another chunk of streaming data
2981 def streamP4FilesCb(self, marshalled):
2982
2983 # catch p4 errors and complain
2984 err = None
2985 if "code" in marshalled:
2986 if marshalled["code"] == "error":
2987 if "data" in marshalled:
2988 err = marshalled["data"].rstrip()
2989
2990 if not err and 'fileSize' in self.stream_file:
2991 required_bytes = int((4 * int(self.stream_file["fileSize"])) - calcDiskFree())
2992 if required_bytes > 0:
2993 err = 'Not enough space left on %s! Free at least %i MB.' % (
2994 os.getcwd(), required_bytes/1024/1024
2995 )
2996
2997 if err:
2998 f = None
2999 if self.stream_have_file_info:
3000 if "depotFile" in self.stream_file:
3001 f = self.stream_file["depotFile"]
3002 # force a failure in fast-import, else an empty
3003 # commit will be made
3004 self.gitStream.write("\n")
3005 self.gitStream.write("die-now\n")
3006 self.gitStream.close()
3007 # ignore errors, but make sure it exits first
3008 self.importProcess.wait()
3009 if f:
3010 die("Error from p4 print for %s: %s" % (f, err))
3011 else:
3012 die("Error from p4 print: %s" % err)
3013
3014 if 'depotFile' in marshalled and self.stream_have_file_info:
3015 # start of a new file - output the old one first
3016 self.streamOneP4File(self.stream_file, self.stream_contents)
3017 self.stream_file = {}
3018 self.stream_contents = []
3019 self.stream_have_file_info = False
3020
3021 # pick up the new file information... for the
3022 # 'data' field we need to append to our array
3023 for k in marshalled.keys():
3024 if k == 'data':
3025 if 'streamContentSize' not in self.stream_file:
3026 self.stream_file['streamContentSize'] = 0
3027 self.stream_file['streamContentSize'] += len(marshalled['data'])
3028 self.stream_contents.append(marshalled['data'])
3029 else:
3030 self.stream_file[k] = marshalled[k]
3031
3032 if (verbose and
3033 'streamContentSize' in self.stream_file and
3034 'fileSize' in self.stream_file and
3035 'depotFile' in self.stream_file):
3036 size = int(self.stream_file["fileSize"])
3037 if size > 0:
3038 progress = 100*self.stream_file['streamContentSize']/size
3039 sys.stdout.write('\r%s %d%% (%i MB)' % (self.stream_file['depotFile'], progress, int(size/1024/1024)))
3040 sys.stdout.flush()
3041
3042 self.stream_have_file_info = True
3043
3044 # Stream directly from "p4 files" into "git fast-import"
3045 def streamP4Files(self, files):
3046 filesForCommit = []
3047 filesToRead = []
3048 filesToDelete = []
3049
3050 for f in files:
3051 filesForCommit.append(f)
3052 if f['action'] in self.delete_actions:
3053 filesToDelete.append(f)
3054 else:
3055 filesToRead.append(f)
3056
3057 # deleted files...
3058 for f in filesToDelete:
3059 self.streamOneP4Deletion(f)
3060
3061 if len(filesToRead) > 0:
3062 self.stream_file = {}
3063 self.stream_contents = []
3064 self.stream_have_file_info = False
3065
3066 # curry self argument
3067 def streamP4FilesCbSelf(entry):
3068 self.streamP4FilesCb(entry)
3069
3070 fileArgs = []
3071 for f in filesToRead:
3072 if 'shelved_cl' in f:
3073 # Handle shelved CLs using the "p4 print file@=N" syntax to print
3074 # the contents
3075 fileArg = f['path'] + encode_text_stream('@={}'.format(f['shelved_cl']))
3076 else:
3077 fileArg = f['path'] + encode_text_stream('#{}'.format(f['rev']))
3078
3079 fileArgs.append(fileArg)
3080
3081 p4CmdList(["-x", "-", "print"],
3082 stdin=fileArgs,
3083 cb=streamP4FilesCbSelf)
3084
3085 # do the last chunk
3086 if 'depotFile' in self.stream_file:
3087 self.streamOneP4File(self.stream_file, self.stream_contents)
3088
3089 def make_email(self, userid):
3090 if userid in self.users:
3091 return self.users[userid]
3092 else:
3093 return "%s <a@b>" % userid
3094
3095 def streamTag(self, gitStream, labelName, labelDetails, commit, epoch):
3096 """ Stream a p4 tag.
3097 commit is either a git commit, or a fast-import mark, ":<p4commit>"
3098 """
3099
3100 if verbose:
3101 print("writing tag %s for commit %s" % (labelName, commit))
3102 gitStream.write("tag %s\n" % labelName)
3103 gitStream.write("from %s\n" % commit)
3104
3105 if 'Owner' in labelDetails:
3106 owner = labelDetails["Owner"]
3107 else:
3108 owner = None
3109
3110 # Try to use the owner of the p4 label, or failing that,
3111 # the current p4 user id.
3112 if owner:
3113 email = self.make_email(owner)
3114 else:
3115 email = self.make_email(self.p4UserId())
3116 tagger = "%s %s %s" % (email, epoch, self.tz)
3117
3118 gitStream.write("tagger %s\n" % tagger)
3119
3120 print("labelDetails=",labelDetails)
3121 if 'Description' in labelDetails:
3122 description = labelDetails['Description']
3123 else:
3124 description = 'Label from git p4'
3125
3126 gitStream.write("data %d\n" % len(description))
3127 gitStream.write(description)
3128 gitStream.write("\n")
3129
3130 def inClientSpec(self, path):
3131 if not self.clientSpecDirs:
3132 return True
3133 inClientSpec = self.clientSpecDirs.map_in_client(path)
3134 if not inClientSpec and self.verbose:
3135 print('Ignoring file outside of client spec: {0}'.format(path))
3136 return inClientSpec
3137
3138 def hasBranchPrefix(self, path):
3139 if not self.branchPrefixes:
3140 return True
3141 hasPrefix = [p for p in self.branchPrefixes
3142 if p4PathStartsWith(path, p)]
3143 if not hasPrefix and self.verbose:
3144 print('Ignoring file outside of prefix: {0}'.format(path))
3145 return hasPrefix
3146
3147 def findShadowedFiles(self, files, change):
3148 # Perforce allows you commit files and directories with the same name,
3149 # so you could have files //depot/foo and //depot/foo/bar both checked
3150 # in. A p4 sync of a repository in this state fails. Deleting one of
3151 # the files recovers the repository.
3152 #
3153 # Git will not allow the broken state to exist and only the most recent
3154 # of the conflicting names is left in the repository. When one of the
3155 # conflicting files is deleted we need to re-add the other one to make
3156 # sure the git repository recovers in the same way as perforce.
3157 deleted = [f for f in files if f['action'] in self.delete_actions]
3158 to_check = set()
3159 for f in deleted:
3160 path = decode_path(f['path'])
3161 to_check.add(path + '/...')
3162 while True:
3163 path = path.rsplit("/", 1)[0]
3164 if path == "/" or path in to_check:
3165 break
3166 to_check.add(path)
3167 to_check = ['%s@%s' % (wildcard_encode(p), change) for p in to_check
3168 if self.hasBranchPrefix(p)]
3169 if to_check:
3170 stat_result = p4CmdList(["-x", "-", "fstat", "-T",
3171 "depotFile,headAction,headRev,headType"], stdin=to_check)
3172 for record in stat_result:
3173 if record['code'] != 'stat':
3174 continue
3175 if record['headAction'] in self.delete_actions:
3176 continue
3177 files.append({
3178 'action': 'add',
3179 'path': record['depotFile'],
3180 'rev': record['headRev'],
3181 'type': record['headType']})
3182
3183 def commit(self, details, files, branch, parent = "", allow_empty=False):
3184 epoch = details["time"]
3185 author = details["user"]
3186 jobs = self.extractJobsFromCommit(details)
3187
3188 if self.verbose:
3189 print('commit into {0}'.format(branch))
3190
3191 files = [f for f in files
3192 if self.hasBranchPrefix(decode_path(f['path']))]
3193 self.findShadowedFiles(files, details['change'])
3194
3195 if self.clientSpecDirs:
3196 self.clientSpecDirs.update_client_spec_path_cache(files)
3197
3198 files = [f for f in files if self.inClientSpec(decode_path(f['path']))]
3199
3200 if gitConfigBool('git-p4.keepEmptyCommits'):
3201 allow_empty = True
3202
3203 if not files and not allow_empty:
3204 print('Ignoring revision {0} as it would produce an empty commit.'
3205 .format(details['change']))
3206 return
3207
3208 self.gitStream.write("commit %s\n" % branch)
3209 self.gitStream.write("mark :%s\n" % details["change"])
3210 self.committedChanges.add(int(details["change"]))
3211 committer = ""
3212 if author not in self.users:
3213 self.getUserMapFromPerforceServer()
3214 committer = "%s %s %s" % (self.make_email(author), epoch, self.tz)
3215
3216 self.gitStream.write("committer %s\n" % committer)
3217
3218 self.gitStream.write("data <<EOT\n")
3219 self.gitStream.write(details["desc"])
3220 if len(jobs) > 0:
3221 self.gitStream.write("\nJobs: %s" % (' '.join(jobs)))
3222
3223 if not self.suppress_meta_comment:
3224 self.gitStream.write("\n[git-p4: depot-paths = \"%s\": change = %s" %
3225 (','.join(self.branchPrefixes), details["change"]))
3226 if len(details['options']) > 0:
3227 self.gitStream.write(": options = %s" % details['options'])
3228 self.gitStream.write("]\n")
3229
3230 self.gitStream.write("EOT\n\n")
3231
3232 if len(parent) > 0:
3233 if self.verbose:
3234 print("parent %s" % parent)
3235 self.gitStream.write("from %s\n" % parent)
3236
3237 self.streamP4Files(files)
3238 self.gitStream.write("\n")
3239
3240 change = int(details["change"])
3241
3242 if change in self.labels:
3243 label = self.labels[change]
3244 labelDetails = label[0]
3245 labelRevisions = label[1]
3246 if self.verbose:
3247 print("Change %s is labelled %s" % (change, labelDetails))
3248
3249 files = p4CmdList(["files"] + ["%s...@%s" % (p, change)
3250 for p in self.branchPrefixes])
3251
3252 if len(files) == len(labelRevisions):
3253
3254 cleanedFiles = {}
3255 for info in files:
3256 if info["action"] in self.delete_actions:
3257 continue
3258 cleanedFiles[info["depotFile"]] = info["rev"]
3259
3260 if cleanedFiles == labelRevisions:
3261 self.streamTag(self.gitStream, 'tag_%s' % labelDetails['label'], labelDetails, branch, epoch)
3262
3263 else:
3264 if not self.silent:
3265 print("Tag %s does not match with change %s: files do not match."
3266 % (labelDetails["label"], change))
3267
3268 else:
3269 if not self.silent:
3270 print("Tag %s does not match with change %s: file count is different."
3271 % (labelDetails["label"], change))
3272
3273 # Build a dictionary of changelists and labels, for "detect-labels" option.
3274 def getLabels(self):
3275 self.labels = {}
3276
3277 l = p4CmdList(["labels"] + ["%s..." % p for p in self.depotPaths])
3278 if len(l) > 0 and not self.silent:
3279 print("Finding files belonging to labels in %s" % self.depotPaths)
3280
3281 for output in l:
3282 label = output["label"]
3283 revisions = {}
3284 newestChange = 0
3285 if self.verbose:
3286 print("Querying files for label %s" % label)
3287 for file in p4CmdList(["files"] +
3288 ["%s...@%s" % (p, label)
3289 for p in self.depotPaths]):
3290 revisions[file["depotFile"]] = file["rev"]
3291 change = int(file["change"])
3292 if change > newestChange:
3293 newestChange = change
3294
3295 self.labels[newestChange] = [output, revisions]
3296
3297 if self.verbose:
3298 print("Label changes: %s" % self.labels.keys())
3299
3300 # Import p4 labels as git tags. A direct mapping does not
3301 # exist, so assume that if all the files are at the same revision
3302 # then we can use that, or it's something more complicated we should
3303 # just ignore.
3304 def importP4Labels(self, stream, p4Labels):
3305 if verbose:
3306 print("import p4 labels: " + ' '.join(p4Labels))
3307
3308 ignoredP4Labels = gitConfigList("git-p4.ignoredP4Labels")
3309 validLabelRegexp = gitConfig("git-p4.labelImportRegexp")
3310 if len(validLabelRegexp) == 0:
3311 validLabelRegexp = defaultLabelRegexp
3312 m = re.compile(validLabelRegexp)
3313
3314 for name in p4Labels:
3315 commitFound = False
3316
3317 if not m.match(name):
3318 if verbose:
3319 print("label %s does not match regexp %s" % (name,validLabelRegexp))
3320 continue
3321
3322 if name in ignoredP4Labels:
3323 continue
3324
3325 labelDetails = p4CmdList(['label', "-o", name])[0]
3326
3327 # get the most recent changelist for each file in this label
3328 change = p4Cmd(["changes", "-m", "1"] + ["%s...@%s" % (p, name)
3329 for p in self.depotPaths])
3330
3331 if 'change' in change:
3332 # find the corresponding git commit; take the oldest commit
3333 changelist = int(change['change'])
3334 if changelist in self.committedChanges:
3335 gitCommit = ":%d" % changelist # use a fast-import mark
3336 commitFound = True
3337 else:
3338 gitCommit = read_pipe(["git", "rev-list", "--max-count=1",
3339 "--reverse", ":/\[git-p4:.*change = %d\]" % changelist], ignore_error=True)
3340 if len(gitCommit) == 0:
3341 print("importing label %s: could not find git commit for changelist %d" % (name, changelist))
3342 else:
3343 commitFound = True
3344 gitCommit = gitCommit.strip()
3345
3346 if commitFound:
3347 # Convert from p4 time format
3348 try:
3349 tmwhen = time.strptime(labelDetails['Update'], "%Y/%m/%d %H:%M:%S")
3350 except ValueError:
3351 print("Could not convert label time %s" % labelDetails['Update'])
3352 tmwhen = 1
3353
3354 when = int(time.mktime(tmwhen))
3355 self.streamTag(stream, name, labelDetails, gitCommit, when)
3356 if verbose:
3357 print("p4 label %s mapped to git commit %s" % (name, gitCommit))
3358 else:
3359 if verbose:
3360 print("Label %s has no changelists - possibly deleted?" % name)
3361
3362 if not commitFound:
3363 # We can't import this label; don't try again as it will get very
3364 # expensive repeatedly fetching all the files for labels that will
3365 # never be imported. If the label is moved in the future, the
3366 # ignore will need to be removed manually.
3367 system(["git", "config", "--add", "git-p4.ignoredP4Labels", name])
3368
3369 def guessProjectName(self):
3370 for p in self.depotPaths:
3371 if p.endswith("/"):
3372 p = p[:-1]
3373 p = p[p.strip().rfind("/") + 1:]
3374 if not p.endswith("/"):
3375 p += "/"
3376 return p
3377
3378 def getBranchMapping(self):
3379 lostAndFoundBranches = set()
3380
3381 user = gitConfig("git-p4.branchUser")
3382 if len(user) > 0:
3383 command = "branches -u %s" % user
3384 else:
3385 command = "branches"
3386
3387 for info in p4CmdList(command):
3388 details = p4Cmd(["branch", "-o", info["branch"]])
3389 viewIdx = 0
3390 while "View%s" % viewIdx in details:
3391 paths = details["View%s" % viewIdx].split(" ")
3392 viewIdx = viewIdx + 1
3393 # require standard //depot/foo/... //depot/bar/... mapping
3394 if len(paths) != 2 or not paths[0].endswith("/...") or not paths[1].endswith("/..."):
3395 continue
3396 source = paths[0]
3397 destination = paths[1]
3398 ## HACK
3399 if p4PathStartsWith(source, self.depotPaths[0]) and p4PathStartsWith(destination, self.depotPaths[0]):
3400 source = source[len(self.depotPaths[0]):-4]
3401 destination = destination[len(self.depotPaths[0]):-4]
3402
3403 if destination in self.knownBranches:
3404 if not self.silent:
3405 print("p4 branch %s defines a mapping from %s to %s" % (info["branch"], source, destination))
3406 print("but there exists another mapping from %s to %s already!" % (self.knownBranches[destination], destination))
3407 continue
3408
3409 self.knownBranches[destination] = source
3410
3411 lostAndFoundBranches.discard(destination)
3412
3413 if source not in self.knownBranches:
3414 lostAndFoundBranches.add(source)
3415
3416 # Perforce does not strictly require branches to be defined, so we also
3417 # check git config for a branch list.
3418 #
3419 # Example of branch definition in git config file:
3420 # [git-p4]
3421 # branchList=main:branchA
3422 # branchList=main:branchB
3423 # branchList=branchA:branchC
3424 configBranches = gitConfigList("git-p4.branchList")
3425 for branch in configBranches:
3426 if branch:
3427 (source, destination) = branch.split(":")
3428 self.knownBranches[destination] = source
3429
3430 lostAndFoundBranches.discard(destination)
3431
3432 if source not in self.knownBranches:
3433 lostAndFoundBranches.add(source)
3434
3435
3436 for branch in lostAndFoundBranches:
3437 self.knownBranches[branch] = branch
3438
3439 def getBranchMappingFromGitBranches(self):
3440 branches = p4BranchesInGit(self.importIntoRemotes)
3441 for branch in branches.keys():
3442 if branch == "master":
3443 branch = "main"
3444 else:
3445 branch = branch[len(self.projectName):]
3446 self.knownBranches[branch] = branch
3447
3448 def updateOptionDict(self, d):
3449 option_keys = {}
3450 if self.keepRepoPath:
3451 option_keys['keepRepoPath'] = 1
3452
3453 d["options"] = ' '.join(sorted(option_keys.keys()))
3454
3455 def readOptions(self, d):
3456 self.keepRepoPath = ('options' in d
3457 and ('keepRepoPath' in d['options']))
3458
3459 def gitRefForBranch(self, branch):
3460 if branch == "main":
3461 return self.refPrefix + "master"
3462
3463 if len(branch) <= 0:
3464 return branch
3465
3466 return self.refPrefix + self.projectName + branch
3467
3468 def gitCommitByP4Change(self, ref, change):
3469 if self.verbose:
3470 print("looking in ref " + ref + " for change %s using bisect..." % change)
3471
3472 earliestCommit = ""
3473 latestCommit = parseRevision(ref)
3474
3475 while True:
3476 if self.verbose:
3477 print("trying: earliest %s latest %s" % (earliestCommit, latestCommit))
3478 next = read_pipe("git rev-list --bisect %s %s" % (latestCommit, earliestCommit)).strip()
3479 if len(next) == 0:
3480 if self.verbose:
3481 print("argh")
3482 return ""
3483 log = extractLogMessageFromGitCommit(next)
3484 settings = extractSettingsGitLog(log)
3485 currentChange = int(settings['change'])
3486 if self.verbose:
3487 print("current change %s" % currentChange)
3488
3489 if currentChange == change:
3490 if self.verbose:
3491 print("found %s" % next)
3492 return next
3493
3494 if currentChange < change:
3495 earliestCommit = "^%s" % next
3496 else:
3497 if next == latestCommit:
3498 die("Infinite loop while looking in ref %s for change %s. Check your branch mappings" % (ref, change))
3499 latestCommit = "%s^@" % next
3500
3501 return ""
3502
3503 def importNewBranch(self, branch, maxChange):
3504 # make fast-import flush all changes to disk and update the refs using the checkpoint
3505 # command so that we can try to find the branch parent in the git history
3506 self.gitStream.write("checkpoint\n\n");
3507 self.gitStream.flush();
3508 branchPrefix = self.depotPaths[0] + branch + "/"
3509 range = "@1,%s" % maxChange
3510 #print "prefix" + branchPrefix
3511 changes = p4ChangesForPaths([branchPrefix], range, self.changes_block_size)
3512 if len(changes) <= 0:
3513 return False
3514 firstChange = changes[0]
3515 #print "first change in branch: %s" % firstChange
3516 sourceBranch = self.knownBranches[branch]
3517 sourceDepotPath = self.depotPaths[0] + sourceBranch
3518 sourceRef = self.gitRefForBranch(sourceBranch)
3519 #print "source " + sourceBranch
3520
3521 branchParentChange = int(p4Cmd(["changes", "-m", "1", "%s...@1,%s" % (sourceDepotPath, firstChange)])["change"])
3522 #print "branch parent: %s" % branchParentChange
3523 gitParent = self.gitCommitByP4Change(sourceRef, branchParentChange)
3524 if len(gitParent) > 0:
3525 self.initialParents[self.gitRefForBranch(branch)] = gitParent
3526 #print "parent git commit: %s" % gitParent
3527
3528 self.importChanges(changes)
3529 return True
3530
3531 def searchParent(self, parent, branch, target):
3532 targetTree = read_pipe(["git", "rev-parse",
3533 "{}^{{tree}}".format(target)]).strip()
3534 for line in read_pipe_lines(["git", "rev-list", "--format=%H %T",
3535 "--no-merges", parent]):
3536 if line.startswith("commit "):
3537 continue
3538 commit, tree = line.strip().split(" ")
3539 if tree == targetTree:
3540 if self.verbose:
3541 print("Found parent of %s in commit %s" % (branch, commit))
3542 return commit
3543 return None
3544
3545 def importChanges(self, changes, origin_revision=0):
3546 cnt = 1
3547 for change in changes:
3548 description = p4_describe(change)
3549 self.updateOptionDict(description)
3550
3551 if not self.silent:
3552 sys.stdout.write("\rImporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
3553 sys.stdout.flush()
3554 cnt = cnt + 1
3555
3556 try:
3557 if self.detectBranches:
3558 branches = self.splitFilesIntoBranches(description)
3559 for branch in branches.keys():
3560 ## HACK --hwn
3561 branchPrefix = self.depotPaths[0] + branch + "/"
3562 self.branchPrefixes = [ branchPrefix ]
3563
3564 parent = ""
3565
3566 filesForCommit = branches[branch]
3567
3568 if self.verbose:
3569 print("branch is %s" % branch)
3570
3571 self.updatedBranches.add(branch)
3572
3573 if branch not in self.createdBranches:
3574 self.createdBranches.add(branch)
3575 parent = self.knownBranches[branch]
3576 if parent == branch:
3577 parent = ""
3578 else:
3579 fullBranch = self.projectName + branch
3580 if fullBranch not in self.p4BranchesInGit:
3581 if not self.silent:
3582 print("\n Importing new branch %s" % fullBranch);
3583 if self.importNewBranch(branch, change - 1):
3584 parent = ""
3585 self.p4BranchesInGit.append(fullBranch)
3586 if not self.silent:
3587 print("\n Resuming with change %s" % change);
3588
3589 if self.verbose:
3590 print("parent determined through known branches: %s" % parent)
3591
3592 branch = self.gitRefForBranch(branch)
3593 parent = self.gitRefForBranch(parent)
3594
3595 if self.verbose:
3596 print("looking for initial parent for %s; current parent is %s" % (branch, parent))
3597
3598 if len(parent) == 0 and branch in self.initialParents:
3599 parent = self.initialParents[branch]
3600 del self.initialParents[branch]
3601
3602 blob = None
3603 if len(parent) > 0:
3604 tempBranch = "%s/%d" % (self.tempBranchLocation, change)
3605 if self.verbose:
3606 print("Creating temporary branch: " + tempBranch)
3607 self.commit(description, filesForCommit, tempBranch)
3608 self.tempBranches.append(tempBranch)
3609 self.checkpoint()
3610 blob = self.searchParent(parent, branch, tempBranch)
3611 if blob:
3612 self.commit(description, filesForCommit, branch, blob)
3613 else:
3614 if self.verbose:
3615 print("Parent of %s not found. Committing into head of %s" % (branch, parent))
3616 self.commit(description, filesForCommit, branch, parent)
3617 else:
3618 files = self.extractFilesFromCommit(description)
3619 self.commit(description, files, self.branch,
3620 self.initialParent)
3621 # only needed once, to connect to the previous commit
3622 self.initialParent = ""
3623 except IOError:
3624 print(self.gitError.read())
3625 sys.exit(1)
3626
3627 def sync_origin_only(self):
3628 if self.syncWithOrigin:
3629 self.hasOrigin = originP4BranchesExist()
3630 if self.hasOrigin:
3631 if not self.silent:
3632 print('Syncing with origin first, using "git fetch origin"')
3633 system("git fetch origin")
3634
3635 def importHeadRevision(self, revision):
3636 print("Doing initial import of %s from revision %s into %s" % (' '.join(self.depotPaths), revision, self.branch))
3637
3638 details = {}
3639 details["user"] = "git perforce import user"
3640 details["desc"] = ("Initial import of %s from the state at revision %s\n"
3641 % (' '.join(self.depotPaths), revision))
3642 details["change"] = revision
3643 newestRevision = 0
3644
3645 fileCnt = 0
3646 fileArgs = ["%s...%s" % (p,revision) for p in self.depotPaths]
3647
3648 for info in p4CmdList(["files"] + fileArgs):
3649
3650 if 'code' in info and info['code'] == 'error':
3651 sys.stderr.write("p4 returned an error: %s\n"
3652 % info['data'])
3653 if info['data'].find("must refer to client") >= 0:
3654 sys.stderr.write("This particular p4 error is misleading.\n")
3655 sys.stderr.write("Perhaps the depot path was misspelled.\n");
3656 sys.stderr.write("Depot path: %s\n" % " ".join(self.depotPaths))
3657 sys.exit(1)
3658 if 'p4ExitCode' in info:
3659 sys.stderr.write("p4 exitcode: %s\n" % info['p4ExitCode'])
3660 sys.exit(1)
3661
3662
3663 change = int(info["change"])
3664 if change > newestRevision:
3665 newestRevision = change
3666
3667 if info["action"] in self.delete_actions:
3668 # don't increase the file cnt, otherwise details["depotFile123"] will have gaps!
3669 #fileCnt = fileCnt + 1
3670 continue
3671
3672 for prop in ["depotFile", "rev", "action", "type" ]:
3673 details["%s%s" % (prop, fileCnt)] = info[prop]
3674
3675 fileCnt = fileCnt + 1
3676
3677 details["change"] = newestRevision
3678
3679 # Use time from top-most change so that all git p4 clones of
3680 # the same p4 repo have the same commit SHA1s.
3681 res = p4_describe(newestRevision)
3682 details["time"] = res["time"]
3683
3684 self.updateOptionDict(details)
3685 try:
3686 self.commit(details, self.extractFilesFromCommit(details), self.branch)
3687 except IOError as err:
3688 print("IO error with git fast-import. Is your git version recent enough?")
3689 print("IO error details: {}".format(err))
3690 print(self.gitError.read())
3691
3692
3693 def importRevisions(self, args, branch_arg_given):
3694 changes = []
3695
3696 if len(self.changesFile) > 0:
3697 with open(self.changesFile) as f:
3698 output = f.readlines()
3699 changeSet = set()
3700 for line in output:
3701 changeSet.add(int(line))
3702
3703 for change in changeSet:
3704 changes.append(change)
3705
3706 changes.sort()
3707 else:
3708 # catch "git p4 sync" with no new branches, in a repo that
3709 # does not have any existing p4 branches
3710 if len(args) == 0:
3711 if not self.p4BranchesInGit:
3712 raise P4CommandException("No remote p4 branches. Perhaps you never did \"git p4 clone\" in here.")
3713
3714 # The default branch is master, unless --branch is used to
3715 # specify something else. Make sure it exists, or complain
3716 # nicely about how to use --branch.
3717 if not self.detectBranches:
3718 if not branch_exists(self.branch):
3719 if branch_arg_given:
3720 raise P4CommandException("Error: branch %s does not exist." % self.branch)
3721 else:
3722 raise P4CommandException("Error: no branch %s; perhaps specify one with --branch." %
3723 self.branch)
3724
3725 if self.verbose:
3726 print("Getting p4 changes for %s...%s" % (', '.join(self.depotPaths),
3727 self.changeRange))
3728 changes = p4ChangesForPaths(self.depotPaths, self.changeRange, self.changes_block_size)
3729
3730 if len(self.maxChanges) > 0:
3731 changes = changes[:min(int(self.maxChanges), len(changes))]
3732
3733 if len(changes) == 0:
3734 if not self.silent:
3735 print("No changes to import!")
3736 else:
3737 if not self.silent and not self.detectBranches:
3738 print("Import destination: %s" % self.branch)
3739
3740 self.updatedBranches = set()
3741
3742 if not self.detectBranches:
3743 if args:
3744 # start a new branch
3745 self.initialParent = ""
3746 else:
3747 # build on a previous revision
3748 self.initialParent = parseRevision(self.branch)
3749
3750 self.importChanges(changes)
3751
3752 if not self.silent:
3753 print("")
3754 if len(self.updatedBranches) > 0:
3755 sys.stdout.write("Updated branches: ")
3756 for b in self.updatedBranches:
3757 sys.stdout.write("%s " % b)
3758 sys.stdout.write("\n")
3759
3760 def openStreams(self):
3761 self.importProcess = subprocess.Popen(["git", "fast-import"],
3762 stdin=subprocess.PIPE,
3763 stdout=subprocess.PIPE,
3764 stderr=subprocess.PIPE);
3765 self.gitOutput = self.importProcess.stdout
3766 self.gitStream = self.importProcess.stdin
3767 self.gitError = self.importProcess.stderr
3768
3769 if bytes is not str:
3770 # Wrap gitStream.write() so that it can be called using `str` arguments
3771 def make_encoded_write(write):
3772 def encoded_write(s):
3773 return write(s.encode() if isinstance(s, str) else s)
3774 return encoded_write
3775
3776 self.gitStream.write = make_encoded_write(self.gitStream.write)
3777
3778 def closeStreams(self):
3779 if self.gitStream is None:
3780 return
3781 self.gitStream.close()
3782 if self.importProcess.wait() != 0:
3783 die("fast-import failed: %s" % self.gitError.read())
3784 self.gitOutput.close()
3785 self.gitError.close()
3786 self.gitStream = None
3787
3788 def run(self, args):
3789 if self.importIntoRemotes:
3790 self.refPrefix = "refs/remotes/p4/"
3791 else:
3792 self.refPrefix = "refs/heads/p4/"
3793
3794 self.sync_origin_only()
3795
3796 branch_arg_given = bool(self.branch)
3797 if len(self.branch) == 0:
3798 self.branch = self.refPrefix + "master"
3799 if gitBranchExists("refs/heads/p4") and self.importIntoRemotes:
3800 system("git update-ref %s refs/heads/p4" % self.branch)
3801 system("git branch -D p4")
3802
3803 # accept either the command-line option, or the configuration variable
3804 if self.useClientSpec:
3805 # will use this after clone to set the variable
3806 self.useClientSpec_from_options = True
3807 else:
3808 if gitConfigBool("git-p4.useclientspec"):
3809 self.useClientSpec = True
3810 if self.useClientSpec:
3811 self.clientSpecDirs = getClientSpec()
3812
3813 # TODO: should always look at previous commits,
3814 # merge with previous imports, if possible.
3815 if args == []:
3816 if self.hasOrigin:
3817 createOrUpdateBranchesFromOrigin(self.refPrefix, self.silent)
3818
3819 # branches holds mapping from branch name to sha1
3820 branches = p4BranchesInGit(self.importIntoRemotes)
3821
3822 # restrict to just this one, disabling detect-branches
3823 if branch_arg_given:
3824 short = self.branch.split("/")[-1]
3825 if short in branches:
3826 self.p4BranchesInGit = [ short ]
3827 else:
3828 self.p4BranchesInGit = branches.keys()
3829
3830 if len(self.p4BranchesInGit) > 1:
3831 if not self.silent:
3832 print("Importing from/into multiple branches")
3833 self.detectBranches = True
3834 for branch in branches.keys():
3835 self.initialParents[self.refPrefix + branch] = \
3836 branches[branch]
3837
3838 if self.verbose:
3839 print("branches: %s" % self.p4BranchesInGit)
3840
3841 p4Change = 0
3842 for branch in self.p4BranchesInGit:
3843 logMsg = extractLogMessageFromGitCommit(self.refPrefix + branch)
3844
3845 settings = extractSettingsGitLog(logMsg)
3846
3847 self.readOptions(settings)
3848 if ('depot-paths' in settings
3849 and 'change' in settings):
3850 change = int(settings['change']) + 1
3851 p4Change = max(p4Change, change)
3852
3853 depotPaths = sorted(settings['depot-paths'])
3854 if self.previousDepotPaths == []:
3855 self.previousDepotPaths = depotPaths
3856 else:
3857 paths = []
3858 for (prev, cur) in zip(self.previousDepotPaths, depotPaths):
3859 prev_list = prev.split("/")
3860 cur_list = cur.split("/")
3861 for i in range(0, min(len(cur_list), len(prev_list))):
3862 if cur_list[i] != prev_list[i]:
3863 i = i - 1
3864 break
3865
3866 paths.append ("/".join(cur_list[:i + 1]))
3867
3868 self.previousDepotPaths = paths
3869
3870 if p4Change > 0:
3871 self.depotPaths = sorted(self.previousDepotPaths)
3872 self.changeRange = "@%s,#head" % p4Change
3873 if not self.silent and not self.detectBranches:
3874 print("Performing incremental import into %s git branch" % self.branch)
3875
3876 # accept multiple ref name abbreviations:
3877 # refs/foo/bar/branch -> use it exactly
3878 # p4/branch -> prepend refs/remotes/ or refs/heads/
3879 # branch -> prepend refs/remotes/p4/ or refs/heads/p4/
3880 if not self.branch.startswith("refs/"):
3881 if self.importIntoRemotes:
3882 prepend = "refs/remotes/"
3883 else:
3884 prepend = "refs/heads/"
3885 if not self.branch.startswith("p4/"):
3886 prepend += "p4/"
3887 self.branch = prepend + self.branch
3888
3889 if len(args) == 0 and self.depotPaths:
3890 if not self.silent:
3891 print("Depot paths: %s" % ' '.join(self.depotPaths))
3892 else:
3893 if self.depotPaths and self.depotPaths != args:
3894 print("previous import used depot path %s and now %s was specified. "
3895 "This doesn't work!" % (' '.join (self.depotPaths),
3896 ' '.join (args)))
3897 sys.exit(1)
3898
3899 self.depotPaths = sorted(args)
3900
3901 revision = ""
3902 self.users = {}
3903
3904 # Make sure no revision specifiers are used when --changesfile
3905 # is specified.
3906 bad_changesfile = False
3907 if len(self.changesFile) > 0:
3908 for p in self.depotPaths:
3909 if p.find("@") >= 0 or p.find("#") >= 0:
3910 bad_changesfile = True
3911 break
3912 if bad_changesfile:
3913 die("Option --changesfile is incompatible with revision specifiers")
3914
3915 newPaths = []
3916 for p in self.depotPaths:
3917 if p.find("@") != -1:
3918 atIdx = p.index("@")
3919 self.changeRange = p[atIdx:]
3920 if self.changeRange == "@all":
3921 self.changeRange = ""
3922 elif ',' not in self.changeRange:
3923 revision = self.changeRange
3924 self.changeRange = ""
3925 p = p[:atIdx]
3926 elif p.find("#") != -1:
3927 hashIdx = p.index("#")
3928 revision = p[hashIdx:]
3929 p = p[:hashIdx]
3930 elif self.previousDepotPaths == []:
3931 # pay attention to changesfile, if given, else import
3932 # the entire p4 tree at the head revision
3933 if len(self.changesFile) == 0:
3934 revision = "#head"
3935
3936 p = re.sub ("\.\.\.$", "", p)
3937 if not p.endswith("/"):
3938 p += "/"
3939
3940 newPaths.append(p)
3941
3942 self.depotPaths = newPaths
3943
3944 # --detect-branches may change this for each branch
3945 self.branchPrefixes = self.depotPaths
3946
3947 self.loadUserMapFromCache()
3948 self.labels = {}
3949 if self.detectLabels:
3950 self.getLabels();
3951
3952 if self.detectBranches:
3953 ## FIXME - what's a P4 projectName ?
3954 self.projectName = self.guessProjectName()
3955
3956 if self.hasOrigin:
3957 self.getBranchMappingFromGitBranches()
3958 else:
3959 self.getBranchMapping()
3960 if self.verbose:
3961 print("p4-git branches: %s" % self.p4BranchesInGit)
3962 print("initial parents: %s" % self.initialParents)
3963 for b in self.p4BranchesInGit:
3964 if b != "master":
3965
3966 ## FIXME
3967 b = b[len(self.projectName):]
3968 self.createdBranches.add(b)
3969
3970 p4_check_access()
3971
3972 self.openStreams()
3973
3974 err = None
3975
3976 try:
3977 if revision:
3978 self.importHeadRevision(revision)
3979 else:
3980 self.importRevisions(args, branch_arg_given)
3981
3982 if gitConfigBool("git-p4.importLabels"):
3983 self.importLabels = True
3984
3985 if self.importLabels:
3986 p4Labels = getP4Labels(self.depotPaths)
3987 gitTags = getGitTags()
3988
3989 missingP4Labels = p4Labels - gitTags
3990 self.importP4Labels(self.gitStream, missingP4Labels)
3991
3992 except P4CommandException as e:
3993 err = e
3994
3995 finally:
3996 self.closeStreams()
3997
3998 if err:
3999 die(str(err))
4000
4001 # Cleanup temporary branches created during import
4002 if self.tempBranches != []:
4003 for branch in self.tempBranches:
4004 read_pipe("git update-ref -d %s" % branch)
4005 os.rmdir(os.path.join(os.environ.get("GIT_DIR", ".git"), self.tempBranchLocation))
4006
4007 # Create a symbolic ref p4/HEAD pointing to p4/<branch> to allow
4008 # a convenient shortcut refname "p4".
4009 if self.importIntoRemotes:
4010 head_ref = self.refPrefix + "HEAD"
4011 if not gitBranchExists(head_ref) and gitBranchExists(self.branch):
4012 system(["git", "symbolic-ref", head_ref, self.branch])
4013
4014 return True
4015
4016 class P4Rebase(Command):
4017 def __init__(self):
4018 Command.__init__(self)
4019 self.options = [
4020 optparse.make_option("--import-labels", dest="importLabels", action="store_true"),
4021 ]
4022 self.importLabels = False
4023 self.description = ("Fetches the latest revision from perforce and "
4024 + "rebases the current work (branch) against it")
4025
4026 def run(self, args):
4027 sync = P4Sync()
4028 sync.importLabels = self.importLabels
4029 sync.run([])
4030
4031 return self.rebase()
4032
4033 def rebase(self):
4034 if os.system("git update-index --refresh") != 0:
4035 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.");
4036 if len(read_pipe("git diff-index HEAD --")) > 0:
4037 die("You have uncommitted changes. Please commit them before rebasing or stash them away with git stash.");
4038
4039 [upstream, settings] = findUpstreamBranchPoint()
4040 if len(upstream) == 0:
4041 die("Cannot find upstream branchpoint for rebase")
4042
4043 # the branchpoint may be p4/foo~3, so strip off the parent
4044 upstream = re.sub("~[0-9]+$", "", upstream)
4045
4046 print("Rebasing the current branch onto %s" % upstream)
4047 oldHead = read_pipe("git rev-parse HEAD").strip()
4048 system("git rebase %s" % upstream)
4049 system("git diff-tree --stat --summary -M %s HEAD --" % oldHead)
4050 return True
4051
4052 class P4Clone(P4Sync):
4053 def __init__(self):
4054 P4Sync.__init__(self)
4055 self.description = "Creates a new git repository and imports from Perforce into it"
4056 self.usage = "usage: %prog [options] //depot/path[@revRange]"
4057 self.options += [
4058 optparse.make_option("--destination", dest="cloneDestination",
4059 action='store', default=None,
4060 help="where to leave result of the clone"),
4061 optparse.make_option("--bare", dest="cloneBare",
4062 action="store_true", default=False),
4063 ]
4064 self.cloneDestination = None
4065 self.needsGit = False
4066 self.cloneBare = False
4067
4068 def defaultDestination(self, args):
4069 ## TODO: use common prefix of args?
4070 depotPath = args[0]
4071 depotDir = re.sub("(@[^@]*)$", "", depotPath)
4072 depotDir = re.sub("(#[^#]*)$", "", depotDir)
4073 depotDir = re.sub(r"\.\.\.$", "", depotDir)
4074 depotDir = re.sub(r"/$", "", depotDir)
4075 return os.path.split(depotDir)[1]
4076
4077 def run(self, args):
4078 if len(args) < 1:
4079 return False
4080
4081 if self.keepRepoPath and not self.cloneDestination:
4082 sys.stderr.write("Must specify destination for --keep-path\n")
4083 sys.exit(1)
4084
4085 depotPaths = args
4086
4087 if not self.cloneDestination and len(depotPaths) > 1:
4088 self.cloneDestination = depotPaths[-1]
4089 depotPaths = depotPaths[:-1]
4090
4091 for p in depotPaths:
4092 if not p.startswith("//"):
4093 sys.stderr.write('Depot paths must start with "//": %s\n' % p)
4094 return False
4095
4096 if not self.cloneDestination:
4097 self.cloneDestination = self.defaultDestination(args)
4098
4099 print("Importing from %s into %s" % (', '.join(depotPaths), self.cloneDestination))
4100
4101 if not os.path.exists(self.cloneDestination):
4102 os.makedirs(self.cloneDestination)
4103 chdir(self.cloneDestination)
4104
4105 init_cmd = [ "git", "init" ]
4106 if self.cloneBare:
4107 init_cmd.append("--bare")
4108 retcode = subprocess.call(init_cmd)
4109 if retcode:
4110 raise CalledProcessError(retcode, init_cmd)
4111
4112 if not P4Sync.run(self, depotPaths):
4113 return False
4114
4115 # create a master branch and check out a work tree
4116 if gitBranchExists(self.branch):
4117 system([ "git", "branch", currentGitBranch(), self.branch ])
4118 if not self.cloneBare:
4119 system([ "git", "checkout", "-f" ])
4120 else:
4121 print('Not checking out any branch, use ' \
4122 '"git checkout -q -b master <branch>"')
4123
4124 # auto-set this variable if invoked with --use-client-spec
4125 if self.useClientSpec_from_options:
4126 system("git config --bool git-p4.useclientspec true")
4127
4128 return True
4129
4130 class P4Unshelve(Command):
4131 def __init__(self):
4132 Command.__init__(self)
4133 self.options = []
4134 self.origin = "HEAD"
4135 self.description = "Unshelve a P4 changelist into a git commit"
4136 self.usage = "usage: %prog [options] changelist"
4137 self.options += [
4138 optparse.make_option("--origin", dest="origin",
4139 help="Use this base revision instead of the default (%s)" % self.origin),
4140 ]
4141 self.verbose = False
4142 self.noCommit = False
4143 self.destbranch = "refs/remotes/p4-unshelved"
4144
4145 def renameBranch(self, branch_name):
4146 """ Rename the existing branch to branch_name.N
4147 """
4148
4149 found = True
4150 for i in range(0,1000):
4151 backup_branch_name = "{0}.{1}".format(branch_name, i)
4152 if not gitBranchExists(backup_branch_name):
4153 gitUpdateRef(backup_branch_name, branch_name) # copy ref to backup
4154 gitDeleteRef(branch_name)
4155 found = True
4156 print("renamed old unshelve branch to {0}".format(backup_branch_name))
4157 break
4158
4159 if not found:
4160 sys.exit("gave up trying to rename existing branch {0}".format(sync.branch))
4161
4162 def findLastP4Revision(self, starting_point):
4163 """ Look back from starting_point for the first commit created by git-p4
4164 to find the P4 commit we are based on, and the depot-paths.
4165 """
4166
4167 for parent in (range(65535)):
4168 log = extractLogMessageFromGitCommit("{0}~{1}".format(starting_point, parent))
4169 settings = extractSettingsGitLog(log)
4170 if 'change' in settings:
4171 return settings
4172
4173 sys.exit("could not find git-p4 commits in {0}".format(self.origin))
4174
4175 def createShelveParent(self, change, branch_name, sync, origin):
4176 """ Create a commit matching the parent of the shelved changelist 'change'
4177 """
4178 parent_description = p4_describe(change, shelved=True)
4179 parent_description['desc'] = 'parent for shelved changelist {}\n'.format(change)
4180 files = sync.extractFilesFromCommit(parent_description, shelved=False, shelved_cl=change)
4181
4182 parent_files = []
4183 for f in files:
4184 # if it was added in the shelved changelist, it won't exist in the parent
4185 if f['action'] in self.add_actions:
4186 continue
4187
4188 # if it was deleted in the shelved changelist it must not be deleted
4189 # in the parent - we might even need to create it if the origin branch
4190 # does not have it
4191 if f['action'] in self.delete_actions:
4192 f['action'] = 'add'
4193
4194 parent_files.append(f)
4195
4196 sync.commit(parent_description, parent_files, branch_name,
4197 parent=origin, allow_empty=True)
4198 print("created parent commit for {0} based on {1} in {2}".format(
4199 change, self.origin, branch_name))
4200
4201 def run(self, args):
4202 if len(args) != 1:
4203 return False
4204
4205 if not gitBranchExists(self.origin):
4206 sys.exit("origin branch {0} does not exist".format(self.origin))
4207
4208 sync = P4Sync()
4209 changes = args
4210
4211 # only one change at a time
4212 change = changes[0]
4213
4214 # if the target branch already exists, rename it
4215 branch_name = "{0}/{1}".format(self.destbranch, change)
4216 if gitBranchExists(branch_name):
4217 self.renameBranch(branch_name)
4218 sync.branch = branch_name
4219
4220 sync.verbose = self.verbose
4221 sync.suppress_meta_comment = True
4222
4223 settings = self.findLastP4Revision(self.origin)
4224 sync.depotPaths = settings['depot-paths']
4225 sync.branchPrefixes = sync.depotPaths
4226
4227 sync.openStreams()
4228 sync.loadUserMapFromCache()
4229 sync.silent = True
4230
4231 # create a commit for the parent of the shelved changelist
4232 self.createShelveParent(change, branch_name, sync, self.origin)
4233
4234 # create the commit for the shelved changelist itself
4235 description = p4_describe(change, True)
4236 files = sync.extractFilesFromCommit(description, True, change)
4237
4238 sync.commit(description, files, branch_name, "")
4239 sync.closeStreams()
4240
4241 print("unshelved changelist {0} into {1}".format(change, branch_name))
4242
4243 return True
4244
4245 class P4Branches(Command):
4246 def __init__(self):
4247 Command.__init__(self)
4248 self.options = [ ]
4249 self.description = ("Shows the git branches that hold imports and their "
4250 + "corresponding perforce depot paths")
4251 self.verbose = False
4252
4253 def run(self, args):
4254 if originP4BranchesExist():
4255 createOrUpdateBranchesFromOrigin()
4256
4257 cmdline = "git rev-parse --symbolic "
4258 cmdline += " --remotes"
4259
4260 for line in read_pipe_lines(cmdline):
4261 line = line.strip()
4262
4263 if not line.startswith('p4/') or line == "p4/HEAD":
4264 continue
4265 branch = line
4266
4267 log = extractLogMessageFromGitCommit("refs/remotes/%s" % branch)
4268 settings = extractSettingsGitLog(log)
4269
4270 print("%s <= %s (%s)" % (branch, ",".join(settings["depot-paths"]), settings["change"]))
4271 return True
4272
4273 class HelpFormatter(optparse.IndentedHelpFormatter):
4274 def __init__(self):
4275 optparse.IndentedHelpFormatter.__init__(self)
4276
4277 def format_description(self, description):
4278 if description:
4279 return description + "\n"
4280 else:
4281 return ""
4282
4283 def printUsage(commands):
4284 print("usage: %s <command> [options]" % sys.argv[0])
4285 print("")
4286 print("valid commands: %s" % ", ".join(commands))
4287 print("")
4288 print("Try %s <command> --help for command specific help." % sys.argv[0])
4289 print("")
4290
4291 commands = {
4292 "submit" : P4Submit,
4293 "commit" : P4Submit,
4294 "sync" : P4Sync,
4295 "rebase" : P4Rebase,
4296 "clone" : P4Clone,
4297 "branches" : P4Branches,
4298 "unshelve" : P4Unshelve,
4299 }
4300
4301 def main():
4302 if len(sys.argv[1:]) == 0:
4303 printUsage(commands.keys())
4304 sys.exit(2)
4305
4306 cmdName = sys.argv[1]
4307 try:
4308 klass = commands[cmdName]
4309 cmd = klass()
4310 except KeyError:
4311 print("unknown command %s" % cmdName)
4312 print("")
4313 printUsage(commands.keys())
4314 sys.exit(2)
4315
4316 options = cmd.options
4317 cmd.gitdir = os.environ.get("GIT_DIR", None)
4318
4319 args = sys.argv[2:]
4320
4321 options.append(optparse.make_option("--verbose", "-v", dest="verbose", action="store_true"))
4322 if cmd.needsGit:
4323 options.append(optparse.make_option("--git-dir", dest="gitdir"))
4324
4325 parser = optparse.OptionParser(cmd.usage.replace("%prog", "%prog " + cmdName),
4326 options,
4327 description = cmd.description,
4328 formatter = HelpFormatter())
4329
4330 try:
4331 (cmd, args) = parser.parse_args(sys.argv[2:], cmd);
4332 except:
4333 parser.print_help()
4334 raise
4335
4336 global verbose
4337 verbose = cmd.verbose
4338 if cmd.needsGit:
4339 if cmd.gitdir == None:
4340 cmd.gitdir = os.path.abspath(".git")
4341 if not isValidGitDir(cmd.gitdir):
4342 # "rev-parse --git-dir" without arguments will try $PWD/.git
4343 cmd.gitdir = read_pipe("git rev-parse --git-dir").strip()
4344 if os.path.exists(cmd.gitdir):
4345 cdup = read_pipe("git rev-parse --show-cdup").strip()
4346 if len(cdup) > 0:
4347 chdir(cdup);
4348
4349 if not isValidGitDir(cmd.gitdir):
4350 if isValidGitDir(cmd.gitdir + "/.git"):
4351 cmd.gitdir += "/.git"
4352 else:
4353 die("fatal: cannot locate git repository at %s" % cmd.gitdir)
4354
4355 # so git commands invoked from the P4 workspace will succeed
4356 os.environ["GIT_DIR"] = cmd.gitdir
4357
4358 if not cmd.run(args):
4359 parser.print_help()
4360 sys.exit(2)
4361
4362
4363 if __name__ == '__main__':
4364 main()