]> git.ipfire.org Git - thirdparty/git.git/blame - git-p4.py
submodule: make zero-oid comparison hash function agnostic
[thirdparty/git.git] / git-p4.py
CommitLineData
86949eef
SH
1#!/usr/bin/env python
2#
3# git-p4.py -- A tool for bidirectional operation between a Perforce depot and git.
4#
c8cbbee9
SH
5# Author: Simon Hausmann <simon@lst.de>
6# Copyright: 2007 Simon Hausmann <simon@lst.de>
83dce55a 7# 2007 Trolltech ASA
86949eef
SH
8# License: MIT <http://www.opensource.org/licenses/mit-license.php>
9#
a33faf28
ER
10import sys
11if sys.hexversion < 0x02040000:
12 # The limiter is the subprocess module
13 sys.stderr.write("git-p4: requires Python 2.4 or later.\n")
14 sys.exit(1)
f629fa59
PW
15import os
16import optparse
17import marshal
18import subprocess
19import tempfile
20import time
21import platform
22import re
23import shutil
d20f0f8e 24import stat
a5db4b12
LS
25import zipfile
26import zlib
4b07cd23 27import ctypes
df8a9e86 28import errno
8b41a97f 29
efdcc992
LD
30# support basestring in python3
31try:
32 unicode = unicode
33except NameError:
34 # 'unicode' is undefined, must be Python 3
35 str = str
36 unicode = str
37 bytes = bytes
38 basestring = (str,bytes)
39else:
40 # 'unicode' exists, must be Python 2
41 str = str
42 unicode = unicode
43 bytes = str
44 basestring = basestring
45
a235e85c
BC
46try:
47 from subprocess import CalledProcessError
48except ImportError:
49 # from python2.7:subprocess.py
50 # Exception classes used by this module.
51 class CalledProcessError(Exception):
52 """This exception is raised when a process run by check_call() returns
53 a non-zero exit status. The exit status will be stored in the
54 returncode attribute."""
55 def __init__(self, returncode, cmd):
56 self.returncode = returncode
57 self.cmd = cmd
58 def __str__(self):
59 return "Command '%s' returned non-zero exit status %d" % (self.cmd, self.returncode)
60
4addad22 61verbose = False
86949eef 62
06804c76 63# Only labels/tags matching this will be imported/exported
c8942a22 64defaultLabelRegexp = r'[a-zA-Z0-9_\-.]+$'
21a50753 65
3deed5e0
LD
66# The block size is reduced automatically if required
67defaultBlockSize = 1<<20
1051ef00 68
0ef67acd 69p4_access_checked = False
1051ef00 70
21a50753
AK
71def p4_build_cmd(cmd):
72 """Build a suitable p4 command line.
73
74 This consolidates building and returning a p4 command line into one
75 location. It means that hooking into the environment, or other configuration
76 can be done more easily.
77 """
6de040df 78 real_cmd = ["p4"]
abcaf073
AK
79
80 user = gitConfig("git-p4.user")
81 if len(user) > 0:
6de040df 82 real_cmd += ["-u",user]
abcaf073
AK
83
84 password = gitConfig("git-p4.password")
85 if len(password) > 0:
6de040df 86 real_cmd += ["-P", password]
abcaf073
AK
87
88 port = gitConfig("git-p4.port")
89 if len(port) > 0:
6de040df 90 real_cmd += ["-p", port]
abcaf073
AK
91
92 host = gitConfig("git-p4.host")
93 if len(host) > 0:
41799aa2 94 real_cmd += ["-H", host]
abcaf073
AK
95
96 client = gitConfig("git-p4.client")
97 if len(client) > 0:
6de040df 98 real_cmd += ["-c", client]
abcaf073 99
89a6ecc5
LS
100 retries = gitConfigInt("git-p4.retries")
101 if retries is None:
102 # Perform 3 retries by default
103 retries = 3
bc233524
IK
104 if retries > 0:
105 # Provide a way to not pass this option by setting git-p4.retries to 0
106 real_cmd += ["-r", str(retries)]
6de040df
LD
107
108 if isinstance(cmd,basestring):
109 real_cmd = ' '.join(real_cmd) + ' ' + cmd
110 else:
111 real_cmd += cmd
0ef67acd
LD
112
113 # now check that we can actually talk to the server
114 global p4_access_checked
115 if not p4_access_checked:
116 p4_access_checked = True # suppress access checks in p4_check_access itself
117 p4_check_access()
118
21a50753
AK
119 return real_cmd
120
378f7be1
LD
121def git_dir(path):
122 """ Return TRUE if the given path is a git directory (/path/to/dir/.git).
123 This won't automatically add ".git" to a directory.
124 """
125 d = read_pipe(["git", "--git-dir", path, "rev-parse", "--git-dir"], True).strip()
126 if not d or len(d) == 0:
127 return None
128 else:
129 return d
130
bbd84863
MF
131def chdir(path, is_client_path=False):
132 """Do chdir to the given path, and set the PWD environment
133 variable for use by P4. It does not look at getcwd() output.
134 Since we're not using the shell, it is necessary to set the
135 PWD environment variable explicitly.
136
137 Normally, expand the path to force it to be absolute. This
138 addresses the use of relative path names inside P4 settings,
139 e.g. P4CONFIG=.p4config. P4 does not simply open the filename
140 as given; it looks for .p4config using PWD.
141
142 If is_client_path, the path was handed to us directly by p4,
143 and may be a symbolic link. Do not call os.getcwd() in this
144 case, because it will cause p4 to think that PWD is not inside
145 the client path.
146 """
147
148 os.chdir(path)
149 if not is_client_path:
150 path = os.getcwd()
151 os.environ['PWD'] = path
053fd0c1 152
4d25dc44
LS
153def calcDiskFree():
154 """Return free space in bytes on the disk of the given dirname."""
155 if platform.system() == 'Windows':
156 free_bytes = ctypes.c_ulonglong(0)
157 ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(os.getcwd()), None, None, ctypes.pointer(free_bytes))
158 return free_bytes.value
159 else:
160 st = os.statvfs(os.getcwd())
161 return st.f_bavail * st.f_frsize
162
86dff6b6
HWN
163def die(msg):
164 if verbose:
165 raise Exception(msg)
166 else:
167 sys.stderr.write(msg + "\n")
168 sys.exit(1)
169
6de040df 170def write_pipe(c, stdin):
4addad22 171 if verbose:
6de040df 172 sys.stderr.write('Writing pipe: %s\n' % str(c))
b016d397 173
6de040df
LD
174 expand = isinstance(c,basestring)
175 p = subprocess.Popen(c, stdin=subprocess.PIPE, shell=expand)
176 pipe = p.stdin
177 val = pipe.write(stdin)
178 pipe.close()
179 if p.wait():
180 die('Command failed: %s' % str(c))
b016d397
HWN
181
182 return val
183
6de040df 184def p4_write_pipe(c, stdin):
d9429194 185 real_cmd = p4_build_cmd(c)
6de040df 186 return write_pipe(real_cmd, stdin)
d9429194 187
78871bf4
LD
188def read_pipe_full(c):
189 """ Read output from command. Returns a tuple
190 of the return status, stdout text and stderr
191 text.
192 """
4addad22 193 if verbose:
6de040df 194 sys.stderr.write('Reading pipe: %s\n' % str(c))
8b41a97f 195
6de040df 196 expand = isinstance(c,basestring)
1f5f3907
LS
197 p = subprocess.Popen(c, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=expand)
198 (out, err) = p.communicate()
78871bf4
LD
199 return (p.returncode, out, err)
200
201def read_pipe(c, ignore_error=False):
202 """ Read output from command. Returns the output text on
203 success. On failure, terminates execution, unless
204 ignore_error is True, when it returns an empty string.
205 """
206 (retcode, out, err) = read_pipe_full(c)
207 if retcode != 0:
208 if ignore_error:
209 out = ""
210 else:
211 die('Command failed: %s\nError: %s' % (str(c), err))
1f5f3907 212 return out
b016d397 213
78871bf4
LD
214def read_pipe_text(c):
215 """ Read output from a command with trailing whitespace stripped.
216 On error, returns None.
217 """
218 (retcode, out, err) = read_pipe_full(c)
219 if retcode != 0:
220 return None
221 else:
222 return out.rstrip()
223
d9429194
AK
224def p4_read_pipe(c, ignore_error=False):
225 real_cmd = p4_build_cmd(c)
226 return read_pipe(real_cmd, ignore_error)
b016d397 227
bce4c5fc 228def read_pipe_lines(c):
4addad22 229 if verbose:
6de040df
LD
230 sys.stderr.write('Reading pipe: %s\n' % str(c))
231
232 expand = isinstance(c, basestring)
233 p = subprocess.Popen(c, stdout=subprocess.PIPE, shell=expand)
234 pipe = p.stdout
b016d397 235 val = pipe.readlines()
6de040df
LD
236 if pipe.close() or p.wait():
237 die('Command failed: %s' % str(c))
b016d397
HWN
238
239 return val
caace111 240
2318121b
AK
241def p4_read_pipe_lines(c):
242 """Specifically invoke p4 on the command supplied. """
155af834 243 real_cmd = p4_build_cmd(c)
2318121b
AK
244 return read_pipe_lines(real_cmd)
245
8e9497c2
GG
246def p4_has_command(cmd):
247 """Ask p4 for help on this command. If it returns an error, the
248 command does not exist in this version of p4."""
249 real_cmd = p4_build_cmd(["help", cmd])
250 p = subprocess.Popen(real_cmd, stdout=subprocess.PIPE,
251 stderr=subprocess.PIPE)
252 p.communicate()
253 return p.returncode == 0
254
249da4c0
PW
255def p4_has_move_command():
256 """See if the move command exists, that it supports -k, and that
257 it has not been administratively disabled. The arguments
258 must be correct, but the filenames do not have to exist. Use
259 ones with wildcards so even if they exist, it will fail."""
260
261 if not p4_has_command("move"):
262 return False
263 cmd = p4_build_cmd(["move", "-k", "@from", "@to"])
264 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
265 (out, err) = p.communicate()
266 # return code will be 1 in either case
267 if err.find("Invalid option") >= 0:
268 return False
269 if err.find("disabled") >= 0:
270 return False
271 # assume it failed because @... was invalid changelist
272 return True
273
cbff4b25 274def system(cmd, ignore_error=False):
6de040df 275 expand = isinstance(cmd,basestring)
4addad22 276 if verbose:
6de040df 277 sys.stderr.write("executing %s\n" % str(cmd))
a235e85c 278 retcode = subprocess.call(cmd, shell=expand)
cbff4b25 279 if retcode and not ignore_error:
a235e85c 280 raise CalledProcessError(retcode, cmd)
6754a299 281
cbff4b25
LD
282 return retcode
283
bf9320f1
AK
284def p4_system(cmd):
285 """Specifically invoke p4 as the system command. """
155af834 286 real_cmd = p4_build_cmd(cmd)
6de040df 287 expand = isinstance(real_cmd, basestring)
a235e85c
BC
288 retcode = subprocess.call(real_cmd, shell=expand)
289 if retcode:
290 raise CalledProcessError(retcode, real_cmd)
6de040df 291
0ef67acd
LD
292def die_bad_access(s):
293 die("failure accessing depot: {0}".format(s.rstrip()))
294
295def p4_check_access(min_expiration=1):
296 """ Check if we can access Perforce - account still logged in
297 """
298 results = p4CmdList(["login", "-s"])
299
300 if len(results) == 0:
301 # should never get here: always get either some results, or a p4ExitCode
302 assert("could not parse response from perforce")
303
304 result = results[0]
305
306 if 'p4ExitCode' in result:
307 # p4 returned non-zero status, e.g. P4PORT invalid, or p4 not in path
308 die_bad_access("could not run p4")
309
310 code = result.get("code")
311 if not code:
312 # we get here if we couldn't connect and there was nothing to unmarshal
313 die_bad_access("could not connect")
314
315 elif code == "stat":
316 expiry = result.get("TicketExpiration")
317 if expiry:
318 expiry = int(expiry)
319 if expiry > min_expiration:
320 # ok to carry on
321 return
322 else:
323 die_bad_access("perforce ticket expires in {0} seconds".format(expiry))
324
325 else:
326 # account without a timeout - all ok
327 return
328
329 elif code == "error":
330 data = result.get("data")
331 if data:
332 die_bad_access("p4 error: {0}".format(data))
333 else:
334 die_bad_access("unknown error")
335 else:
336 die_bad_access("unknown error code {0}".format(code))
337
7f0e5962
PW
338_p4_version_string = None
339def p4_version_string():
340 """Read the version string, showing just the last line, which
341 hopefully is the interesting version bit.
342
343 $ p4 -V
344 Perforce - The Fast Software Configuration Management System.
345 Copyright 1995-2011 Perforce Software. All rights reserved.
346 Rev. P4/NTX86/2011.1/393975 (2011/12/16).
347 """
348 global _p4_version_string
349 if not _p4_version_string:
350 a = p4_read_pipe_lines(["-V"])
351 _p4_version_string = a[-1].rstrip()
352 return _p4_version_string
353
6de040df 354def p4_integrate(src, dest):
9d7d446a 355 p4_system(["integrate", "-Dt", wildcard_encode(src), wildcard_encode(dest)])
6de040df 356
8d7ec362 357def p4_sync(f, *options):
9d7d446a 358 p4_system(["sync"] + list(options) + [wildcard_encode(f)])
6de040df
LD
359
360def p4_add(f):
9d7d446a
PW
361 # forcibly add file names with wildcards
362 if wildcard_present(f):
363 p4_system(["add", "-f", f])
364 else:
365 p4_system(["add", f])
6de040df
LD
366
367def p4_delete(f):
9d7d446a 368 p4_system(["delete", wildcard_encode(f)])
6de040df 369
a02b8bc4
RP
370def p4_edit(f, *options):
371 p4_system(["edit"] + list(options) + [wildcard_encode(f)])
6de040df
LD
372
373def p4_revert(f):
9d7d446a 374 p4_system(["revert", wildcard_encode(f)])
6de040df 375
9d7d446a
PW
376def p4_reopen(type, f):
377 p4_system(["reopen", "-t", type, wildcard_encode(f)])
bf9320f1 378
46c609e9
LD
379def p4_reopen_in_change(changelist, files):
380 cmd = ["reopen", "-c", str(changelist)] + files
381 p4_system(cmd)
382
8e9497c2
GG
383def p4_move(src, dest):
384 p4_system(["move", "-k", wildcard_encode(src), wildcard_encode(dest)])
385
1051ef00 386def p4_last_change():
1997e91f 387 results = p4CmdList(["changes", "-m", "1"], skip_info=True)
1051ef00
LD
388 return int(results[0]['change'])
389
123f6317 390def p4_describe(change, shelved=False):
18fa13d0
PW
391 """Make sure it returns a valid result by checking for
392 the presence of field "time". Return a dict of the
393 results."""
394
123f6317
LD
395 cmd = ["describe", "-s"]
396 if shelved:
397 cmd += ["-S"]
398 cmd += [str(change)]
399
400 ds = p4CmdList(cmd, skip_info=True)
18fa13d0
PW
401 if len(ds) != 1:
402 die("p4 describe -s %d did not return 1 result: %s" % (change, str(ds)))
403
404 d = ds[0]
405
406 if "p4ExitCode" in d:
407 die("p4 describe -s %d exited with %d: %s" % (change, d["p4ExitCode"],
408 str(d)))
409 if "code" in d:
410 if d["code"] == "error":
411 die("p4 describe -s %d returned error code: %s" % (change, str(d)))
412
413 if "time" not in d:
414 die("p4 describe -s %d returned no \"time\": %s" % (change, str(d)))
415
416 return d
417
9cffb8c8
PW
418#
419# Canonicalize the p4 type and return a tuple of the
420# base type, plus any modifiers. See "p4 help filetypes"
421# for a list and explanation.
422#
423def split_p4_type(p4type):
424
425 p4_filetypes_historical = {
426 "ctempobj": "binary+Sw",
427 "ctext": "text+C",
428 "cxtext": "text+Cx",
429 "ktext": "text+k",
430 "kxtext": "text+kx",
431 "ltext": "text+F",
432 "tempobj": "binary+FSw",
433 "ubinary": "binary+F",
434 "uresource": "resource+F",
435 "uxbinary": "binary+Fx",
436 "xbinary": "binary+x",
437 "xltext": "text+Fx",
438 "xtempobj": "binary+Swx",
439 "xtext": "text+x",
440 "xunicode": "unicode+x",
441 "xutf16": "utf16+x",
442 }
443 if p4type in p4_filetypes_historical:
444 p4type = p4_filetypes_historical[p4type]
445 mods = ""
446 s = p4type.split("+")
447 base = s[0]
448 mods = ""
449 if len(s) > 1:
450 mods = s[1]
451 return (base, mods)
b9fc6ea9 452
60df071c
LD
453#
454# return the raw p4 type of a file (text, text+ko, etc)
455#
79467e61
PW
456def p4_type(f):
457 results = p4CmdList(["fstat", "-T", "headType", wildcard_encode(f)])
60df071c
LD
458 return results[0]['headType']
459
460#
461# Given a type base and modifier, return a regexp matching
462# the keywords that can be expanded in the file
463#
464def p4_keywords_regexp_for_type(base, type_mods):
465 if base in ("text", "unicode", "binary"):
466 kwords = None
467 if "ko" in type_mods:
468 kwords = 'Id|Header'
469 elif "k" in type_mods:
470 kwords = 'Id|Header|Author|Date|DateTime|Change|File|Revision'
471 else:
472 return None
473 pattern = r"""
474 \$ # Starts with a dollar, followed by...
475 (%s) # one of the keywords, followed by...
6b2bf41e 476 (:[^$\n]+)? # possibly an old expansion, followed by...
60df071c
LD
477 \$ # another dollar
478 """ % kwords
479 return pattern
480 else:
481 return None
482
483#
484# Given a file, return a regexp matching the possible
485# RCS keywords that will be expanded, or None for files
486# with kw expansion turned off.
487#
488def p4_keywords_regexp_for_file(file):
489 if not os.path.exists(file):
490 return None
491 else:
492 (type_base, type_mods) = split_p4_type(p4_type(file))
493 return p4_keywords_regexp_for_type(type_base, type_mods)
b9fc6ea9 494
c65b670e
CP
495def setP4ExecBit(file, mode):
496 # Reopens an already open file and changes the execute bit to match
497 # the execute bit setting in the passed in mode.
498
499 p4Type = "+x"
500
501 if not isModeExec(mode):
502 p4Type = getP4OpenedType(file)
503 p4Type = re.sub('^([cku]?)x(.*)', '\\1\\2', p4Type)
504 p4Type = re.sub('(.*?\+.*?)x(.*?)', '\\1\\2', p4Type)
505 if p4Type[-1] == "+":
506 p4Type = p4Type[0:-1]
507
6de040df 508 p4_reopen(p4Type, file)
c65b670e
CP
509
510def getP4OpenedType(file):
511 # Returns the perforce file type for the given file.
512
9d7d446a 513 result = p4_read_pipe(["opened", wildcard_encode(file)])
34a0dbfc 514 match = re.match(".*\((.+)\)( \*exclusive\*)?\r?$", result)
c65b670e
CP
515 if match:
516 return match.group(1)
517 else:
f3e5ae4f 518 die("Could not determine file type for %s (result: '%s')" % (file, result))
c65b670e 519
06804c76
LD
520# Return the set of all p4 labels
521def getP4Labels(depotPaths):
522 labels = set()
523 if isinstance(depotPaths,basestring):
524 depotPaths = [depotPaths]
525
526 for l in p4CmdList(["labels"] + ["%s..." % p for p in depotPaths]):
527 label = l['label']
528 labels.add(label)
529
530 return labels
531
532# Return the set of all git tags
533def getGitTags():
534 gitTags = set()
535 for line in read_pipe_lines(["git", "tag"]):
536 tag = line.strip()
537 gitTags.add(tag)
538 return gitTags
539
b43b0a3c
CP
540def diffTreePattern():
541 # This is a simple generator for the diff tree regex pattern. This could be
542 # a class variable if this and parseDiffTreeEntry were a part of a class.
543 pattern = re.compile(':(\d+) (\d+) (\w+) (\w+) ([A-Z])(\d+)?\t(.*?)((\t(.*))|$)')
544 while True:
545 yield pattern
546
547def parseDiffTreeEntry(entry):
548 """Parses a single diff tree entry into its component elements.
549
550 See git-diff-tree(1) manpage for details about the format of the diff
551 output. This method returns a dictionary with the following elements:
552
553 src_mode - The mode of the source file
554 dst_mode - The mode of the destination file
555 src_sha1 - The sha1 for the source file
556 dst_sha1 - The sha1 fr the destination file
557 status - The one letter status of the diff (i.e. 'A', 'M', 'D', etc)
558 status_score - The score for the status (applicable for 'C' and 'R'
559 statuses). This is None if there is no score.
560 src - The path for the source file.
561 dst - The path for the destination file. This is only present for
562 copy or renames. If it is not present, this is None.
563
564 If the pattern is not matched, None is returned."""
565
566 match = diffTreePattern().next().match(entry)
567 if match:
568 return {
569 'src_mode': match.group(1),
570 'dst_mode': match.group(2),
571 'src_sha1': match.group(3),
572 'dst_sha1': match.group(4),
573 'status': match.group(5),
574 'status_score': match.group(6),
575 'src': match.group(7),
576 'dst': match.group(10)
577 }
578 return None
579
c65b670e
CP
580def isModeExec(mode):
581 # Returns True if the given git mode represents an executable file,
582 # otherwise False.
583 return mode[-3:] == "755"
584
55bb3e36
LD
585class P4Exception(Exception):
586 """ Base class for exceptions from the p4 client """
587 def __init__(self, exit_code):
588 self.p4ExitCode = exit_code
589
590class P4ServerException(P4Exception):
591 """ Base class for exceptions where we get some kind of marshalled up result from the server """
592 def __init__(self, exit_code, p4_result):
593 super(P4ServerException, self).__init__(exit_code)
594 self.p4_result = p4_result
595 self.code = p4_result[0]['code']
596 self.data = p4_result[0]['data']
597
598class P4RequestSizeException(P4ServerException):
599 """ One of the maxresults or maxscanrows errors """
600 def __init__(self, exit_code, p4_result, limit):
601 super(P4RequestSizeException, self).__init__(exit_code, p4_result)
602 self.limit = limit
603
c65b670e
CP
604def isModeExecChanged(src_mode, dst_mode):
605 return isModeExec(src_mode) != isModeExec(dst_mode)
606
55bb3e36
LD
607def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None, skip_info=False,
608 errors_as_exceptions=False):
6de040df
LD
609
610 if isinstance(cmd,basestring):
611 cmd = "-G " + cmd
612 expand = True
613 else:
614 cmd = ["-G"] + cmd
615 expand = False
616
617 cmd = p4_build_cmd(cmd)
6a49f8e2 618 if verbose:
6de040df 619 sys.stderr.write("Opening pipe: %s\n" % str(cmd))
9f90c733
SL
620
621 # Use a temporary file to avoid deadlocks without
622 # subprocess.communicate(), which would put another copy
623 # of stdout into memory.
624 stdin_file = None
625 if stdin is not None:
626 stdin_file = tempfile.TemporaryFile(prefix='p4-stdin', mode=stdin_mode)
6de040df
LD
627 if isinstance(stdin,basestring):
628 stdin_file.write(stdin)
629 else:
630 for i in stdin:
631 stdin_file.write(i + '\n')
9f90c733
SL
632 stdin_file.flush()
633 stdin_file.seek(0)
634
6de040df
LD
635 p4 = subprocess.Popen(cmd,
636 shell=expand,
9f90c733
SL
637 stdin=stdin_file,
638 stdout=subprocess.PIPE)
86949eef
SH
639
640 result = []
641 try:
642 while True:
9f90c733 643 entry = marshal.load(p4.stdout)
1997e91f
MT
644 if skip_info:
645 if 'code' in entry and entry['code'] == 'info':
646 continue
c3f6163b
AG
647 if cb is not None:
648 cb(entry)
649 else:
650 result.append(entry)
86949eef
SH
651 except EOFError:
652 pass
9f90c733
SL
653 exitCode = p4.wait()
654 if exitCode != 0:
55bb3e36
LD
655 if errors_as_exceptions:
656 if len(result) > 0:
657 data = result[0].get('data')
658 if data:
659 m = re.search('Too many rows scanned \(over (\d+)\)', data)
660 if not m:
661 m = re.search('Request too large \(over (\d+)\)', data)
662
663 if m:
664 limit = int(m.group(1))
665 raise P4RequestSizeException(exitCode, result, limit)
666
667 raise P4ServerException(exitCode, result)
668 else:
669 raise P4Exception(exitCode)
670 else:
671 entry = {}
672 entry["p4ExitCode"] = exitCode
673 result.append(entry)
86949eef
SH
674
675 return result
676
677def p4Cmd(cmd):
678 list = p4CmdList(cmd)
679 result = {}
680 for entry in list:
681 result.update(entry)
682 return result;
683
cb2c9db5
SH
684def p4Where(depotPath):
685 if not depotPath.endswith("/"):
686 depotPath += "/"
cd884106
VA
687 depotPathLong = depotPath + "..."
688 outputList = p4CmdList(["where", depotPathLong])
7f705dc3
TAL
689 output = None
690 for entry in outputList:
75bc9573 691 if "depotFile" in entry:
cd884106
VA
692 # Search for the base client side depot path, as long as it starts with the branch's P4 path.
693 # The base path always ends with "/...".
694 if entry["depotFile"].find(depotPath) == 0 and entry["depotFile"][-4:] == "/...":
75bc9573
TAL
695 output = entry
696 break
697 elif "data" in entry:
698 data = entry.get("data")
699 space = data.find(" ")
700 if data[:space] == depotPath:
701 output = entry
702 break
7f705dc3
TAL
703 if output == None:
704 return ""
dc524036
SH
705 if output["code"] == "error":
706 return ""
cb2c9db5
SH
707 clientPath = ""
708 if "path" in output:
709 clientPath = output.get("path")
710 elif "data" in output:
711 data = output.get("data")
712 lastSpace = data.rfind(" ")
713 clientPath = data[lastSpace + 1:]
714
715 if clientPath.endswith("..."):
716 clientPath = clientPath[:-3]
717 return clientPath
718
86949eef 719def currentGitBranch():
eff45110 720 return read_pipe_text(["git", "symbolic-ref", "--short", "-q", "HEAD"])
86949eef 721
4f5cf76a 722def isValidGitDir(path):
378f7be1 723 return git_dir(path) != None
4f5cf76a 724
463e8af6 725def parseRevision(ref):
b25b2065 726 return read_pipe("git rev-parse %s" % ref).strip()
463e8af6 727
28755dba
PW
728def branchExists(ref):
729 rev = read_pipe(["git", "rev-parse", "-q", "--verify", ref],
730 ignore_error=True)
731 return len(rev) > 0
732
6ae8de88
SH
733def extractLogMessageFromGitCommit(commit):
734 logMessage = ""
b016d397
HWN
735
736 ## fixme: title is first line of commit, not 1st paragraph.
6ae8de88 737 foundTitle = False
b016d397 738 for log in read_pipe_lines("git cat-file commit %s" % commit):
6ae8de88
SH
739 if not foundTitle:
740 if len(log) == 1:
1c094184 741 foundTitle = True
6ae8de88
SH
742 continue
743
744 logMessage += log
745 return logMessage
746
bb6e09b2 747def extractSettingsGitLog(log):
6ae8de88
SH
748 values = {}
749 for line in log.split("\n"):
750 line = line.strip()
6326aa58
HWN
751 m = re.search (r"^ *\[git-p4: (.*)\]$", line)
752 if not m:
753 continue
754
755 assignments = m.group(1).split (':')
756 for a in assignments:
757 vals = a.split ('=')
758 key = vals[0].strip()
759 val = ('='.join (vals[1:])).strip()
760 if val.endswith ('\"') and val.startswith('"'):
761 val = val[1:-1]
762
763 values[key] = val
764
845b42cb
SH
765 paths = values.get("depot-paths")
766 if not paths:
767 paths = values.get("depot-path")
a3fdd579
SH
768 if paths:
769 values['depot-paths'] = paths.split(',')
bb6e09b2 770 return values
6ae8de88 771
8136a639 772def gitBranchExists(branch):
bb6e09b2
HWN
773 proc = subprocess.Popen(["git", "rev-parse", branch],
774 stderr=subprocess.PIPE, stdout=subprocess.PIPE);
caace111 775 return proc.wait() == 0;
8136a639 776
123f6317
LD
777def gitUpdateRef(ref, newvalue):
778 subprocess.check_call(["git", "update-ref", ref, newvalue])
779
780def gitDeleteRef(ref):
781 subprocess.check_call(["git", "update-ref", "-d", ref])
782
36bd8446 783_gitConfig = {}
b345d6c3 784
692e1796 785def gitConfig(key, typeSpecifier=None):
dba1c9d9 786 if key not in _gitConfig:
692e1796
LS
787 cmd = [ "git", "config" ]
788 if typeSpecifier:
789 cmd += [ typeSpecifier ]
790 cmd += [ key ]
b345d6c3
PW
791 s = read_pipe(cmd, ignore_error=True)
792 _gitConfig[key] = s.strip()
36bd8446 793 return _gitConfig[key]
01265103 794
0d609032
PW
795def gitConfigBool(key):
796 """Return a bool, using git config --bool. It is True only if the
797 variable is set to true, and False if set to false or not present
798 in the config."""
799
dba1c9d9 800 if key not in _gitConfig:
692e1796 801 _gitConfig[key] = gitConfig(key, '--bool') == "true"
36bd8446 802 return _gitConfig[key]
01265103 803
cb1dafdf 804def gitConfigInt(key):
dba1c9d9 805 if key not in _gitConfig:
cb1dafdf 806 cmd = [ "git", "config", "--int", key ]
0d609032
PW
807 s = read_pipe(cmd, ignore_error=True)
808 v = s.strip()
cb1dafdf
LS
809 try:
810 _gitConfig[key] = int(gitConfig(key, '--int'))
811 except ValueError:
812 _gitConfig[key] = None
36bd8446 813 return _gitConfig[key]
01265103 814
7199cf13 815def gitConfigList(key):
dba1c9d9 816 if key not in _gitConfig:
2abba301 817 s = read_pipe(["git", "config", "--get-all", key], ignore_error=True)
c3c2b057 818 _gitConfig[key] = s.strip().splitlines()
7960e707
LS
819 if _gitConfig[key] == ['']:
820 _gitConfig[key] = []
7199cf13
VA
821 return _gitConfig[key]
822
2c8037ed
PW
823def p4BranchesInGit(branchesAreInRemotes=True):
824 """Find all the branches whose names start with "p4/", looking
825 in remotes or heads as specified by the argument. Return
826 a dictionary of { branch: revision } for each one found.
827 The branch names are the short names, without any
828 "p4/" prefix."""
829
062410bb
SH
830 branches = {}
831
832 cmdline = "git rev-parse --symbolic "
833 if branchesAreInRemotes:
2c8037ed 834 cmdline += "--remotes"
062410bb 835 else:
2c8037ed 836 cmdline += "--branches"
062410bb
SH
837
838 for line in read_pipe_lines(cmdline):
839 line = line.strip()
840
2c8037ed
PW
841 # only import to p4/
842 if not line.startswith('p4/'):
843 continue
844 # special symbolic ref to p4/master
845 if line == "p4/HEAD":
062410bb 846 continue
062410bb 847
2c8037ed
PW
848 # strip off p4/ prefix
849 branch = line[len("p4/"):]
062410bb
SH
850
851 branches[branch] = parseRevision(line)
2c8037ed 852
062410bb
SH
853 return branches
854
5a8e84cd
PW
855def branch_exists(branch):
856 """Make sure that the given ref name really exists."""
857
858 cmd = [ "git", "rev-parse", "--symbolic", "--verify", branch ]
859 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
860 out, _ = p.communicate()
861 if p.returncode:
862 return False
863 # expect exactly one line of output: the branch name
864 return out.rstrip() == branch
865
9ceab363 866def findUpstreamBranchPoint(head = "HEAD"):
86506fe5
SH
867 branches = p4BranchesInGit()
868 # map from depot-path to branch name
869 branchByDepotPath = {}
870 for branch in branches.keys():
871 tip = branches[branch]
872 log = extractLogMessageFromGitCommit(tip)
873 settings = extractSettingsGitLog(log)
dba1c9d9 874 if "depot-paths" in settings:
86506fe5
SH
875 paths = ",".join(settings["depot-paths"])
876 branchByDepotPath[paths] = "remotes/p4/" + branch
877
27d2d811 878 settings = None
27d2d811
SH
879 parent = 0
880 while parent < 65535:
9ceab363 881 commit = head + "~%s" % parent
27d2d811
SH
882 log = extractLogMessageFromGitCommit(commit)
883 settings = extractSettingsGitLog(log)
dba1c9d9 884 if "depot-paths" in settings:
86506fe5 885 paths = ",".join(settings["depot-paths"])
dba1c9d9 886 if paths in branchByDepotPath:
86506fe5 887 return [branchByDepotPath[paths], settings]
27d2d811 888
86506fe5 889 parent = parent + 1
27d2d811 890
86506fe5 891 return ["", settings]
27d2d811 892
5ca44617
SH
893def createOrUpdateBranchesFromOrigin(localRefPrefix = "refs/remotes/p4/", silent=True):
894 if not silent:
f2606b17 895 print("Creating/updating branch(es) in %s based on origin branch(es)"
5ca44617
SH
896 % localRefPrefix)
897
898 originPrefix = "origin/p4/"
899
900 for line in read_pipe_lines("git rev-parse --symbolic --remotes"):
901 line = line.strip()
902 if (not line.startswith(originPrefix)) or line.endswith("HEAD"):
903 continue
904
905 headName = line[len(originPrefix):]
906 remoteHead = localRefPrefix + headName
907 originHead = line
908
909 original = extractSettingsGitLog(extractLogMessageFromGitCommit(originHead))
dba1c9d9
LD
910 if ('depot-paths' not in original
911 or 'change' not in original):
5ca44617
SH
912 continue
913
914 update = False
915 if not gitBranchExists(remoteHead):
916 if verbose:
f2606b17 917 print("creating %s" % remoteHead)
5ca44617
SH
918 update = True
919 else:
920 settings = extractSettingsGitLog(extractLogMessageFromGitCommit(remoteHead))
dba1c9d9 921 if 'change' in settings:
5ca44617
SH
922 if settings['depot-paths'] == original['depot-paths']:
923 originP4Change = int(original['change'])
924 p4Change = int(settings['change'])
925 if originP4Change > p4Change:
f2606b17 926 print("%s (%s) is newer than %s (%s). "
5ca44617
SH
927 "Updating p4 branch from origin."
928 % (originHead, originP4Change,
929 remoteHead, p4Change))
930 update = True
931 else:
f2606b17 932 print("Ignoring: %s was imported from %s while "
5ca44617
SH
933 "%s was imported from %s"
934 % (originHead, ','.join(original['depot-paths']),
935 remoteHead, ','.join(settings['depot-paths'])))
936
937 if update:
938 system("git update-ref %s %s" % (remoteHead, originHead))
939
940def originP4BranchesExist():
941 return gitBranchExists("origin") or gitBranchExists("origin/p4") or gitBranchExists("origin/p4/master")
942
1051ef00
LD
943
944def p4ParseNumericChangeRange(parts):
945 changeStart = int(parts[0][1:])
946 if parts[1] == '#head':
947 changeEnd = p4_last_change()
948 else:
949 changeEnd = int(parts[1])
950
951 return (changeStart, changeEnd)
952
953def chooseBlockSize(blockSize):
954 if blockSize:
955 return blockSize
956 else:
957 return defaultBlockSize
958
959def p4ChangesForPaths(depotPaths, changeRange, requestedBlockSize):
4f6432d8 960 assert depotPaths
96b2d54a 961
1051ef00
LD
962 # Parse the change range into start and end. Try to find integer
963 # revision ranges as these can be broken up into blocks to avoid
964 # hitting server-side limits (maxrows, maxscanresults). But if
965 # that doesn't work, fall back to using the raw revision specifier
966 # strings, without using block mode.
967
96b2d54a 968 if changeRange is None or changeRange == '':
1051ef00
LD
969 changeStart = 1
970 changeEnd = p4_last_change()
971 block_size = chooseBlockSize(requestedBlockSize)
96b2d54a
LS
972 else:
973 parts = changeRange.split(',')
974 assert len(parts) == 2
1051ef00
LD
975 try:
976 (changeStart, changeEnd) = p4ParseNumericChangeRange(parts)
977 block_size = chooseBlockSize(requestedBlockSize)
8fa0abf8 978 except ValueError:
1051ef00
LD
979 changeStart = parts[0][1:]
980 changeEnd = parts[1]
981 if requestedBlockSize:
982 die("cannot use --changes-block-size with non-numeric revisions")
983 block_size = None
4f6432d8 984
9943e5b9 985 changes = set()
96b2d54a 986
1f90a648 987 # Retrieve changes a block at a time, to prevent running
3deed5e0
LD
988 # into a MaxResults/MaxScanRows error from the server. If
989 # we _do_ hit one of those errors, turn down the block size
1051ef00 990
1f90a648
SH
991 while True:
992 cmd = ['changes']
1051ef00 993
1f90a648
SH
994 if block_size:
995 end = min(changeEnd, changeStart + block_size)
996 revisionRange = "%d,%d" % (changeStart, end)
997 else:
998 revisionRange = "%s,%s" % (changeStart, changeEnd)
1051ef00 999
1f90a648 1000 for p in depotPaths:
1051ef00
LD
1001 cmd += ["%s...@%s" % (p, revisionRange)]
1002
3deed5e0
LD
1003 # fetch the changes
1004 try:
1005 result = p4CmdList(cmd, errors_as_exceptions=True)
1006 except P4RequestSizeException as e:
1007 if not block_size:
1008 block_size = e.limit
1009 elif block_size > e.limit:
1010 block_size = e.limit
1011 else:
1012 block_size = max(2, block_size // 2)
1013
1014 if verbose: print("block size error, retrying with block size {0}".format(block_size))
1015 continue
1016 except P4Exception as e:
1017 die('Error retrieving changes description ({0})'.format(e.p4ExitCode))
1018
1f90a648 1019 # Insert changes in chronological order
3deed5e0 1020 for entry in reversed(result):
dba1c9d9 1021 if 'change' not in entry:
b596b3b9
MT
1022 continue
1023 changes.add(int(entry['change']))
1051ef00 1024
1f90a648
SH
1025 if not block_size:
1026 break
1051ef00 1027
1f90a648
SH
1028 if end >= changeEnd:
1029 break
1051ef00 1030
1f90a648 1031 changeStart = end + 1
4f6432d8 1032
1f90a648
SH
1033 changes = sorted(changes)
1034 return changes
4f6432d8 1035
d53de8b9
TAL
1036def p4PathStartsWith(path, prefix):
1037 # This method tries to remedy a potential mixed-case issue:
1038 #
1039 # If UserA adds //depot/DirA/file1
1040 # and UserB adds //depot/dira/file2
1041 #
1042 # we may or may not have a problem. If you have core.ignorecase=true,
1043 # we treat DirA and dira as the same directory
0d609032 1044 if gitConfigBool("core.ignorecase"):
d53de8b9
TAL
1045 return path.lower().startswith(prefix.lower())
1046 return path.startswith(prefix)
1047
543987bd
PW
1048def getClientSpec():
1049 """Look at the p4 client spec, create a View() object that contains
1050 all the mappings, and return it."""
1051
1052 specList = p4CmdList("client -o")
1053 if len(specList) != 1:
1054 die('Output from "client -o" is %d lines, expecting 1' %
1055 len(specList))
1056
1057 # dictionary of all client parameters
1058 entry = specList[0]
1059
9d57c4a6
KS
1060 # the //client/ name
1061 client_name = entry["Client"]
1062
543987bd
PW
1063 # just the keys that start with "View"
1064 view_keys = [ k for k in entry.keys() if k.startswith("View") ]
1065
1066 # hold this new View
9d57c4a6 1067 view = View(client_name)
543987bd
PW
1068
1069 # append the lines, in order, to the view
1070 for view_num in range(len(view_keys)):
1071 k = "View%d" % view_num
1072 if k not in view_keys:
1073 die("Expected view key %s missing" % k)
1074 view.append(entry[k])
1075
1076 return view
1077
1078def getClientRoot():
1079 """Grab the client directory."""
1080
1081 output = p4CmdList("client -o")
1082 if len(output) != 1:
1083 die('Output from "client -o" is %d lines, expecting 1' % len(output))
1084
1085 entry = output[0]
1086 if "Root" not in entry:
1087 die('Client has no "Root"')
1088
1089 return entry["Root"]
1090
9d7d446a
PW
1091#
1092# P4 wildcards are not allowed in filenames. P4 complains
1093# if you simply add them, but you can force it with "-f", in
1094# which case it translates them into %xx encoding internally.
1095#
1096def wildcard_decode(path):
1097 # Search for and fix just these four characters. Do % last so
1098 # that fixing it does not inadvertently create new %-escapes.
1099 # Cannot have * in a filename in windows; untested as to
1100 # what p4 would do in such a case.
1101 if not platform.system() == "Windows":
1102 path = path.replace("%2A", "*")
1103 path = path.replace("%23", "#") \
1104 .replace("%40", "@") \
1105 .replace("%25", "%")
1106 return path
1107
1108def wildcard_encode(path):
1109 # do % first to avoid double-encoding the %s introduced here
1110 path = path.replace("%", "%25") \
1111 .replace("*", "%2A") \
1112 .replace("#", "%23") \
1113 .replace("@", "%40")
1114 return path
1115
1116def wildcard_present(path):
598354c0
BC
1117 m = re.search("[*#@%]", path)
1118 return m is not None
9d7d446a 1119
a5db4b12
LS
1120class LargeFileSystem(object):
1121 """Base class for large file system support."""
1122
1123 def __init__(self, writeToGitStream):
1124 self.largeFiles = set()
1125 self.writeToGitStream = writeToGitStream
1126
1127 def generatePointer(self, cloneDestination, contentFile):
1128 """Return the content of a pointer file that is stored in Git instead of
1129 the actual content."""
1130 assert False, "Method 'generatePointer' required in " + self.__class__.__name__
1131
1132 def pushFile(self, localLargeFile):
1133 """Push the actual content which is not stored in the Git repository to
1134 a server."""
1135 assert False, "Method 'pushFile' required in " + self.__class__.__name__
1136
1137 def hasLargeFileExtension(self, relPath):
1138 return reduce(
1139 lambda a, b: a or b,
1140 [relPath.endswith('.' + e) for e in gitConfigList('git-p4.largeFileExtensions')],
1141 False
1142 )
1143
1144 def generateTempFile(self, contents):
1145 contentFile = tempfile.NamedTemporaryFile(prefix='git-p4-large-file', delete=False)
1146 for d in contents:
1147 contentFile.write(d)
1148 contentFile.close()
1149 return contentFile.name
1150
1151 def exceedsLargeFileThreshold(self, relPath, contents):
1152 if gitConfigInt('git-p4.largeFileThreshold'):
1153 contentsSize = sum(len(d) for d in contents)
1154 if contentsSize > gitConfigInt('git-p4.largeFileThreshold'):
1155 return True
1156 if gitConfigInt('git-p4.largeFileCompressedThreshold'):
1157 contentsSize = sum(len(d) for d in contents)
1158 if contentsSize <= gitConfigInt('git-p4.largeFileCompressedThreshold'):
1159 return False
1160 contentTempFile = self.generateTempFile(contents)
1161 compressedContentFile = tempfile.NamedTemporaryFile(prefix='git-p4-large-file', delete=False)
1162 zf = zipfile.ZipFile(compressedContentFile.name, mode='w')
1163 zf.write(contentTempFile, compress_type=zipfile.ZIP_DEFLATED)
1164 zf.close()
1165 compressedContentsSize = zf.infolist()[0].compress_size
1166 os.remove(contentTempFile)
1167 os.remove(compressedContentFile.name)
1168 if compressedContentsSize > gitConfigInt('git-p4.largeFileCompressedThreshold'):
1169 return True
1170 return False
1171
1172 def addLargeFile(self, relPath):
1173 self.largeFiles.add(relPath)
1174
1175 def removeLargeFile(self, relPath):
1176 self.largeFiles.remove(relPath)
1177
1178 def isLargeFile(self, relPath):
1179 return relPath in self.largeFiles
1180
1181 def processContent(self, git_mode, relPath, contents):
1182 """Processes the content of git fast import. This method decides if a
1183 file is stored in the large file system and handles all necessary
1184 steps."""
1185 if self.exceedsLargeFileThreshold(relPath, contents) or self.hasLargeFileExtension(relPath):
1186 contentTempFile = self.generateTempFile(contents)
d5eb3cf5
LS
1187 (pointer_git_mode, contents, localLargeFile) = self.generatePointer(contentTempFile)
1188 if pointer_git_mode:
1189 git_mode = pointer_git_mode
1190 if localLargeFile:
1191 # Move temp file to final location in large file system
1192 largeFileDir = os.path.dirname(localLargeFile)
1193 if not os.path.isdir(largeFileDir):
1194 os.makedirs(largeFileDir)
1195 shutil.move(contentTempFile, localLargeFile)
1196 self.addLargeFile(relPath)
1197 if gitConfigBool('git-p4.largeFilePush'):
1198 self.pushFile(localLargeFile)
1199 if verbose:
1200 sys.stderr.write("%s moved to large file system (%s)\n" % (relPath, localLargeFile))
a5db4b12
LS
1201 return (git_mode, contents)
1202
1203class MockLFS(LargeFileSystem):
1204 """Mock large file system for testing."""
1205
1206 def generatePointer(self, contentFile):
1207 """The pointer content is the original content prefixed with "pointer-".
1208 The local filename of the large file storage is derived from the file content.
1209 """
1210 with open(contentFile, 'r') as f:
1211 content = next(f)
1212 gitMode = '100644'
1213 pointerContents = 'pointer-' + content
1214 localLargeFile = os.path.join(os.getcwd(), '.git', 'mock-storage', 'local', content[:-1])
1215 return (gitMode, pointerContents, localLargeFile)
1216
1217 def pushFile(self, localLargeFile):
1218 """The remote filename of the large file storage is the same as the local
1219 one but in a different directory.
1220 """
1221 remotePath = os.path.join(os.path.dirname(localLargeFile), '..', 'remote')
1222 if not os.path.exists(remotePath):
1223 os.makedirs(remotePath)
1224 shutil.copyfile(localLargeFile, os.path.join(remotePath, os.path.basename(localLargeFile)))
1225
b47d807d
LS
1226class GitLFS(LargeFileSystem):
1227 """Git LFS as backend for the git-p4 large file system.
1228 See https://git-lfs.github.com/ for details."""
1229
1230 def __init__(self, *args):
1231 LargeFileSystem.__init__(self, *args)
1232 self.baseGitAttributes = []
1233
1234 def generatePointer(self, contentFile):
1235 """Generate a Git LFS pointer for the content. Return LFS Pointer file
1236 mode and content which is stored in the Git repository instead of
1237 the actual content. Return also the new location of the actual
1238 content.
1239 """
d5eb3cf5
LS
1240 if os.path.getsize(contentFile) == 0:
1241 return (None, '', None)
1242
b47d807d
LS
1243 pointerProcess = subprocess.Popen(
1244 ['git', 'lfs', 'pointer', '--file=' + contentFile],
1245 stdout=subprocess.PIPE
1246 )
1247 pointerFile = pointerProcess.stdout.read()
1248 if pointerProcess.wait():
1249 os.remove(contentFile)
1250 die('git-lfs pointer command failed. Did you install the extension?')
82f2567e
LS
1251
1252 # Git LFS removed the preamble in the output of the 'pointer' command
1253 # starting from version 1.2.0. Check for the preamble here to support
1254 # earlier versions.
1255 # c.f. https://github.com/github/git-lfs/commit/da2935d9a739592bc775c98d8ef4df9c72ea3b43
1256 if pointerFile.startswith('Git LFS pointer for'):
1257 pointerFile = re.sub(r'Git LFS pointer for.*\n\n', '', pointerFile)
1258
1259 oid = re.search(r'^oid \w+:(\w+)', pointerFile, re.MULTILINE).group(1)
b47d807d
LS
1260 localLargeFile = os.path.join(
1261 os.getcwd(),
1262 '.git', 'lfs', 'objects', oid[:2], oid[2:4],
1263 oid,
1264 )
1265 # LFS Spec states that pointer files should not have the executable bit set.
1266 gitMode = '100644'
82f2567e 1267 return (gitMode, pointerFile, localLargeFile)
b47d807d
LS
1268
1269 def pushFile(self, localLargeFile):
1270 uploadProcess = subprocess.Popen(
1271 ['git', 'lfs', 'push', '--object-id', 'origin', os.path.basename(localLargeFile)]
1272 )
1273 if uploadProcess.wait():
1274 die('git-lfs push command failed. Did you define a remote?')
1275
1276 def generateGitAttributes(self):
1277 return (
1278 self.baseGitAttributes +
1279 [
1280 '\n',
1281 '#\n',
1282 '# Git LFS (see https://git-lfs.github.com/)\n',
1283 '#\n',
1284 ] +
862f9312 1285 ['*.' + f.replace(' ', '[[:space:]]') + ' filter=lfs diff=lfs merge=lfs -text\n'
b47d807d
LS
1286 for f in sorted(gitConfigList('git-p4.largeFileExtensions'))
1287 ] +
862f9312 1288 ['/' + f.replace(' ', '[[:space:]]') + ' filter=lfs diff=lfs merge=lfs -text\n'
b47d807d
LS
1289 for f in sorted(self.largeFiles) if not self.hasLargeFileExtension(f)
1290 ]
1291 )
1292
1293 def addLargeFile(self, relPath):
1294 LargeFileSystem.addLargeFile(self, relPath)
1295 self.writeToGitStream('100644', '.gitattributes', self.generateGitAttributes())
1296
1297 def removeLargeFile(self, relPath):
1298 LargeFileSystem.removeLargeFile(self, relPath)
1299 self.writeToGitStream('100644', '.gitattributes', self.generateGitAttributes())
1300
1301 def processContent(self, git_mode, relPath, contents):
1302 if relPath == '.gitattributes':
1303 self.baseGitAttributes = contents
1304 return (git_mode, self.generateGitAttributes())
1305 else:
1306 return LargeFileSystem.processContent(self, git_mode, relPath, contents)
1307
b984733c
SH
1308class Command:
1309 def __init__(self):
1310 self.usage = "usage: %prog [options]"
8910ac0e 1311 self.needsGit = True
6a10b6aa 1312 self.verbose = False
b984733c 1313
8cf422db
LD
1314 # This is required for the "append" cloneExclude action
1315 def ensure_value(self, attr, value):
1316 if not hasattr(self, attr) or getattr(self, attr) is None:
1317 setattr(self, attr, value)
1318 return getattr(self, attr)
1319
3ea2cfd4
LD
1320class P4UserMap:
1321 def __init__(self):
1322 self.userMapFromPerforceServer = False
affb474f
LD
1323 self.myP4UserId = None
1324
1325 def p4UserId(self):
1326 if self.myP4UserId:
1327 return self.myP4UserId
1328
1329 results = p4CmdList("user -o")
1330 for r in results:
dba1c9d9 1331 if 'User' in r:
affb474f
LD
1332 self.myP4UserId = r['User']
1333 return r['User']
1334 die("Could not find your p4 user id")
1335
1336 def p4UserIsMe(self, p4User):
1337 # return True if the given p4 user is actually me
1338 me = self.p4UserId()
1339 if not p4User or p4User != me:
1340 return False
1341 else:
1342 return True
3ea2cfd4
LD
1343
1344 def getUserCacheFilename(self):
1345 home = os.environ.get("HOME", os.environ.get("USERPROFILE"))
1346 return home + "/.gitp4-usercache.txt"
1347
1348 def getUserMapFromPerforceServer(self):
1349 if self.userMapFromPerforceServer:
1350 return
1351 self.users = {}
1352 self.emails = {}
1353
1354 for output in p4CmdList("users"):
dba1c9d9 1355 if "User" not in output:
3ea2cfd4
LD
1356 continue
1357 self.users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">"
1358 self.emails[output["Email"]] = output["User"]
1359
10d08a14
LS
1360 mapUserConfigRegex = re.compile(r"^\s*(\S+)\s*=\s*(.+)\s*<(\S+)>\s*$", re.VERBOSE)
1361 for mapUserConfig in gitConfigList("git-p4.mapUser"):
1362 mapUser = mapUserConfigRegex.findall(mapUserConfig)
1363 if mapUser and len(mapUser[0]) == 3:
1364 user = mapUser[0][0]
1365 fullname = mapUser[0][1]
1366 email = mapUser[0][2]
1367 self.users[user] = fullname + " <" + email + ">"
1368 self.emails[email] = user
3ea2cfd4
LD
1369
1370 s = ''
1371 for (key, val) in self.users.items():
1372 s += "%s\t%s\n" % (key.expandtabs(1), val.expandtabs(1))
1373
1374 open(self.getUserCacheFilename(), "wb").write(s)
1375 self.userMapFromPerforceServer = True
1376
1377 def loadUserMapFromCache(self):
1378 self.users = {}
1379 self.userMapFromPerforceServer = False
1380 try:
1381 cache = open(self.getUserCacheFilename(), "rb")
1382 lines = cache.readlines()
1383 cache.close()
1384 for line in lines:
1385 entry = line.strip().split("\t")
1386 self.users[entry[0]] = entry[1]
1387 except IOError:
1388 self.getUserMapFromPerforceServer()
1389
b984733c 1390class P4Debug(Command):
86949eef 1391 def __init__(self):
6ae8de88 1392 Command.__init__(self)
6a10b6aa 1393 self.options = []
c8c39116 1394 self.description = "A tool to debug the output of p4 -G."
8910ac0e 1395 self.needsGit = False
86949eef
SH
1396
1397 def run(self, args):
b1ce9447 1398 j = 0
6de040df 1399 for output in p4CmdList(args):
f2606b17 1400 print('Element: %d' % j)
b1ce9447 1401 j += 1
f2606b17 1402 print(output)
b984733c 1403 return True
86949eef 1404
5834684d
SH
1405class P4RollBack(Command):
1406 def __init__(self):
1407 Command.__init__(self)
1408 self.options = [
0c66a783 1409 optparse.make_option("--local", dest="rollbackLocalBranches", action="store_true")
5834684d
SH
1410 ]
1411 self.description = "A tool to debug the multi-branch import. Don't use :)"
0c66a783 1412 self.rollbackLocalBranches = False
5834684d
SH
1413
1414 def run(self, args):
1415 if len(args) != 1:
1416 return False
1417 maxChange = int(args[0])
0c66a783 1418
ad192f28 1419 if "p4ExitCode" in p4Cmd("changes -m 1"):
66a2f523
SH
1420 die("Problems executing p4");
1421
0c66a783
SH
1422 if self.rollbackLocalBranches:
1423 refPrefix = "refs/heads/"
b016d397 1424 lines = read_pipe_lines("git rev-parse --symbolic --branches")
0c66a783
SH
1425 else:
1426 refPrefix = "refs/remotes/"
b016d397 1427 lines = read_pipe_lines("git rev-parse --symbolic --remotes")
0c66a783
SH
1428
1429 for line in lines:
1430 if self.rollbackLocalBranches or (line.startswith("p4/") and line != "p4/HEAD\n"):
b25b2065
HWN
1431 line = line.strip()
1432 ref = refPrefix + line
5834684d 1433 log = extractLogMessageFromGitCommit(ref)
bb6e09b2
HWN
1434 settings = extractSettingsGitLog(log)
1435
1436 depotPaths = settings['depot-paths']
1437 change = settings['change']
1438
5834684d 1439 changed = False
52102d47 1440
6326aa58
HWN
1441 if len(p4Cmd("changes -m 1 " + ' '.join (['%s...@%s' % (p, maxChange)
1442 for p in depotPaths]))) == 0:
f2606b17 1443 print("Branch %s did not exist at change %s, deleting." % (ref, maxChange))
52102d47
SH
1444 system("git update-ref -d %s `git rev-parse %s`" % (ref, ref))
1445 continue
1446
bb6e09b2 1447 while change and int(change) > maxChange:
5834684d 1448 changed = True
52102d47 1449 if self.verbose:
f2606b17 1450 print("%s is at %s ; rewinding towards %s" % (ref, change, maxChange))
5834684d
SH
1451 system("git update-ref %s \"%s^\"" % (ref, ref))
1452 log = extractLogMessageFromGitCommit(ref)
bb6e09b2
HWN
1453 settings = extractSettingsGitLog(log)
1454
1455
1456 depotPaths = settings['depot-paths']
1457 change = settings['change']
5834684d
SH
1458
1459 if changed:
f2606b17 1460 print("%s rewound to %s" % (ref, change))
5834684d
SH
1461
1462 return True
1463
3ea2cfd4 1464class P4Submit(Command, P4UserMap):
6bbfd137
PW
1465
1466 conflict_behavior_choices = ("ask", "skip", "quit")
1467
4f5cf76a 1468 def __init__(self):
b984733c 1469 Command.__init__(self)
3ea2cfd4 1470 P4UserMap.__init__(self)
4f5cf76a 1471 self.options = [
4f5cf76a 1472 optparse.make_option("--origin", dest="origin"),
ae901090 1473 optparse.make_option("-M", dest="detectRenames", action="store_true"),
3ea2cfd4
LD
1474 # preserve the user, requires relevant p4 permissions
1475 optparse.make_option("--preserve-user", dest="preserveUser", action="store_true"),
06804c76 1476 optparse.make_option("--export-labels", dest="exportLabels", action="store_true"),
ef739f08 1477 optparse.make_option("--dry-run", "-n", dest="dry_run", action="store_true"),
728b7ad8 1478 optparse.make_option("--prepare-p4-only", dest="prepare_p4_only", action="store_true"),
6bbfd137 1479 optparse.make_option("--conflict", dest="conflict_behavior",
44e8d26c
PW
1480 choices=self.conflict_behavior_choices),
1481 optparse.make_option("--branch", dest="branch"),
b34fa577
VK
1482 optparse.make_option("--shelve", dest="shelve", action="store_true",
1483 help="Shelve instead of submit. Shelved files are reverted, "
1484 "restoring the workspace to the state before the shelve"),
8cf422db 1485 optparse.make_option("--update-shelve", dest="update_shelve", action="append", type="int",
46c609e9 1486 metavar="CHANGELIST",
8cf422db 1487 help="update an existing shelved changelist, implies --shelve, "
f55b87c1
RM
1488 "repeat in-order for multiple shelved changelists"),
1489 optparse.make_option("--commit", dest="commit", metavar="COMMIT",
1490 help="submit only the specified commit(s), one commit or xxx..xxx"),
1491 optparse.make_option("--disable-rebase", dest="disable_rebase", action="store_true",
1492 help="Disable rebase after submit is completed. Can be useful if you "
b9d34db9
LD
1493 "work from a local git branch that is not master"),
1494 optparse.make_option("--disable-p4sync", dest="disable_p4sync", action="store_true",
1495 help="Skip Perforce sync of p4/master after submit or shelve"),
4f5cf76a 1496 ]
251c8c50
CB
1497 self.description = """Submit changes from git to the perforce depot.\n
1498 The `p4-pre-submit` hook is executed if it exists and is executable.
1499 The hook takes no parameters and nothing from standard input. Exiting with
1500 non-zero status from this script prevents `git-p4 submit` from launching.
1501
1502 One usage scenario is to run unit tests in the hook."""
1503
c9b50e63 1504 self.usage += " [name of git branch to submit into perforce depot]"
9512497b 1505 self.origin = ""
ae901090 1506 self.detectRenames = False
0d609032 1507 self.preserveUser = gitConfigBool("git-p4.preserveUser")
ef739f08 1508 self.dry_run = False
b34fa577 1509 self.shelve = False
8cf422db 1510 self.update_shelve = list()
f55b87c1 1511 self.commit = ""
3b3477ea 1512 self.disable_rebase = gitConfigBool("git-p4.disableRebase")
b9d34db9 1513 self.disable_p4sync = gitConfigBool("git-p4.disableP4Sync")
728b7ad8 1514 self.prepare_p4_only = False
6bbfd137 1515 self.conflict_behavior = None
f7baba8b 1516 self.isWindows = (platform.system() == "Windows")
06804c76 1517 self.exportLabels = False
249da4c0 1518 self.p4HasMoveCommand = p4_has_move_command()
44e8d26c 1519 self.branch = None
4f5cf76a 1520
a5db4b12
LS
1521 if gitConfig('git-p4.largeFileSystem'):
1522 die("Large file system not supported for git-p4 submit command. Please remove it from config.")
1523
4f5cf76a
SH
1524 def check(self):
1525 if len(p4CmdList("opened ...")) > 0:
1526 die("You have files opened with perforce! Close them before starting the sync.")
1527
f19cb0a0
PW
1528 def separate_jobs_from_description(self, message):
1529 """Extract and return a possible Jobs field in the commit
1530 message. It goes into a separate section in the p4 change
1531 specification.
1532
1533 A jobs line starts with "Jobs:" and looks like a new field
1534 in a form. Values are white-space separated on the same
1535 line or on following lines that start with a tab.
1536
1537 This does not parse and extract the full git commit message
1538 like a p4 form. It just sees the Jobs: line as a marker
1539 to pass everything from then on directly into the p4 form,
1540 but outside the description section.
1541
1542 Return a tuple (stripped log message, jobs string)."""
1543
1544 m = re.search(r'^Jobs:', message, re.MULTILINE)
1545 if m is None:
1546 return (message, None)
1547
1548 jobtext = message[m.start():]
1549 stripped_message = message[:m.start()].rstrip()
1550 return (stripped_message, jobtext)
1551
1552 def prepareLogMessage(self, template, message, jobs):
1553 """Edits the template returned from "p4 change -o" to insert
1554 the message in the Description field, and the jobs text in
1555 the Jobs field."""
4f5cf76a
SH
1556 result = ""
1557
edae1e2f
SH
1558 inDescriptionSection = False
1559
4f5cf76a
SH
1560 for line in template.split("\n"):
1561 if line.startswith("#"):
1562 result += line + "\n"
1563 continue
1564
edae1e2f 1565 if inDescriptionSection:
c9dbab04 1566 if line.startswith("Files:") or line.startswith("Jobs:"):
edae1e2f 1567 inDescriptionSection = False
f19cb0a0
PW
1568 # insert Jobs section
1569 if jobs:
1570 result += jobs + "\n"
edae1e2f
SH
1571 else:
1572 continue
1573 else:
1574 if line.startswith("Description:"):
1575 inDescriptionSection = True
1576 line += "\n"
1577 for messageLine in message.split("\n"):
1578 line += "\t" + messageLine + "\n"
1579
1580 result += line + "\n"
4f5cf76a
SH
1581
1582 return result
1583
60df071c
LD
1584 def patchRCSKeywords(self, file, pattern):
1585 # Attempt to zap the RCS keywords in a p4 controlled file matching the given pattern
1586 (handle, outFileName) = tempfile.mkstemp(dir='.')
1587 try:
1588 outFile = os.fdopen(handle, "w+")
1589 inFile = open(file, "r")
1590 regexp = re.compile(pattern, re.VERBOSE)
1591 for line in inFile.readlines():
1592 line = regexp.sub(r'$\1$', line)
1593 outFile.write(line)
1594 inFile.close()
1595 outFile.close()
1596 # Forcibly overwrite the original file
1597 os.unlink(file)
1598 shutil.move(outFileName, file)
1599 except:
1600 # cleanup our temporary file
1601 os.unlink(outFileName)
f2606b17 1602 print("Failed to strip RCS keywords in %s" % file)
60df071c
LD
1603 raise
1604
f2606b17 1605 print("Patched up RCS keywords in %s" % file)
60df071c 1606
3ea2cfd4
LD
1607 def p4UserForCommit(self,id):
1608 # Return the tuple (perforce user,git email) for a given git commit id
1609 self.getUserMapFromPerforceServer()
9bf28855
PW
1610 gitEmail = read_pipe(["git", "log", "--max-count=1",
1611 "--format=%ae", id])
3ea2cfd4 1612 gitEmail = gitEmail.strip()
dba1c9d9 1613 if gitEmail not in self.emails:
3ea2cfd4
LD
1614 return (None,gitEmail)
1615 else:
1616 return (self.emails[gitEmail],gitEmail)
1617
1618 def checkValidP4Users(self,commits):
1619 # check if any git authors cannot be mapped to p4 users
1620 for id in commits:
1621 (user,email) = self.p4UserForCommit(id)
1622 if not user:
1623 msg = "Cannot find p4 user for email %s in commit %s." % (email, id)
0d609032 1624 if gitConfigBool("git-p4.allowMissingP4Users"):
f2606b17 1625 print("%s" % msg)
3ea2cfd4
LD
1626 else:
1627 die("Error: %s\nSet git-p4.allowMissingP4Users to true to allow this." % msg)
1628
1629 def lastP4Changelist(self):
1630 # Get back the last changelist number submitted in this client spec. This
1631 # then gets used to patch up the username in the change. If the same
1632 # client spec is being used by multiple processes then this might go
1633 # wrong.
1634 results = p4CmdList("client -o") # find the current client
1635 client = None
1636 for r in results:
dba1c9d9 1637 if 'Client' in r:
3ea2cfd4
LD
1638 client = r['Client']
1639 break
1640 if not client:
1641 die("could not get client spec")
6de040df 1642 results = p4CmdList(["changes", "-c", client, "-m", "1"])
3ea2cfd4 1643 for r in results:
dba1c9d9 1644 if 'change' in r:
3ea2cfd4
LD
1645 return r['change']
1646 die("Could not get changelist number for last submit - cannot patch up user details")
1647
1648 def modifyChangelistUser(self, changelist, newUser):
1649 # fixup the user field of a changelist after it has been submitted.
1650 changes = p4CmdList("change -o %s" % changelist)
ecdba36d
LD
1651 if len(changes) != 1:
1652 die("Bad output from p4 change modifying %s to user %s" %
1653 (changelist, newUser))
1654
1655 c = changes[0]
1656 if c['User'] == newUser: return # nothing to do
1657 c['User'] = newUser
1658 input = marshal.dumps(c)
1659
3ea2cfd4
LD
1660 result = p4CmdList("change -f -i", stdin=input)
1661 for r in result:
dba1c9d9 1662 if 'code' in r:
3ea2cfd4
LD
1663 if r['code'] == 'error':
1664 die("Could not modify user field of changelist %s to %s:%s" % (changelist, newUser, r['data']))
dba1c9d9 1665 if 'data' in r:
3ea2cfd4
LD
1666 print("Updated user field for changelist %s to %s" % (changelist, newUser))
1667 return
1668 die("Could not modify user field of changelist %s to %s" % (changelist, newUser))
1669
1670 def canChangeChangelists(self):
1671 # check to see if we have p4 admin or super-user permissions, either of
1672 # which are required to modify changelists.
52a4880b 1673 results = p4CmdList(["protects", self.depotPath])
3ea2cfd4 1674 for r in results:
dba1c9d9 1675 if 'perm' in r:
3ea2cfd4
LD
1676 if r['perm'] == 'admin':
1677 return 1
1678 if r['perm'] == 'super':
1679 return 1
1680 return 0
1681
46c609e9 1682 def prepareSubmitTemplate(self, changelist=None):
f19cb0a0
PW
1683 """Run "p4 change -o" to grab a change specification template.
1684 This does not use "p4 -G", as it is nice to keep the submission
1685 template in original order, since a human might edit it.
1686
1687 Remove lines in the Files section that show changes to files
1688 outside the depot path we're committing into."""
1689
cbc69242
SH
1690 [upstream, settings] = findUpstreamBranchPoint()
1691
b596b3b9
MT
1692 template = """\
1693# A Perforce Change Specification.
1694#
1695# Change: The change number. 'new' on a new changelist.
1696# Date: The date this specification was last modified.
1697# Client: The client on which the changelist was created. Read-only.
1698# User: The user who created the changelist.
1699# Status: Either 'pending' or 'submitted'. Read-only.
1700# Type: Either 'public' or 'restricted'. Default is 'public'.
1701# Description: Comments about the changelist. Required.
1702# Jobs: What opened jobs are to be closed by this changelist.
1703# You may delete jobs from this list. (New changelists only.)
1704# Files: What opened files from the default changelist are to be added
1705# to this changelist. You may delete files from this list.
1706# (New changelists only.)
1707"""
1708 files_list = []
ea99c3ae 1709 inFilesSection = False
b596b3b9 1710 change_entry = None
46c609e9
LD
1711 args = ['change', '-o']
1712 if changelist:
1713 args.append(str(changelist))
b596b3b9 1714 for entry in p4CmdList(args):
dba1c9d9 1715 if 'code' not in entry:
b596b3b9
MT
1716 continue
1717 if entry['code'] == 'stat':
1718 change_entry = entry
1719 break
1720 if not change_entry:
1721 die('Failed to decode output of p4 change -o')
1722 for key, value in change_entry.iteritems():
1723 if key.startswith('File'):
dba1c9d9 1724 if 'depot-paths' in settings:
b596b3b9
MT
1725 if not [p for p in settings['depot-paths']
1726 if p4PathStartsWith(value, p)]:
1727 continue
ea99c3ae 1728 else:
b596b3b9
MT
1729 if not p4PathStartsWith(value, self.depotPath):
1730 continue
1731 files_list.append(value)
1732 continue
1733 # Output in the order expected by prepareLogMessage
1734 for key in ['Change', 'Client', 'User', 'Status', 'Description', 'Jobs']:
dba1c9d9 1735 if key not in change_entry:
b596b3b9
MT
1736 continue
1737 template += '\n'
1738 template += key + ':'
1739 if key == 'Description':
1740 template += '\n'
1741 for field_line in change_entry[key].splitlines():
1742 template += '\t'+field_line+'\n'
1743 if len(files_list) > 0:
1744 template += '\n'
1745 template += 'Files:\n'
1746 for path in files_list:
1747 template += '\t'+path+'\n'
ea99c3ae
SH
1748 return template
1749
7c766e57
PW
1750 def edit_template(self, template_file):
1751 """Invoke the editor to let the user change the submission
1752 message. Return true if okay to continue with the submit."""
1753
1754 # if configured to skip the editing part, just submit
0d609032 1755 if gitConfigBool("git-p4.skipSubmitEdit"):
7c766e57
PW
1756 return True
1757
1758 # look at the modification time, to check later if the user saved
1759 # the file
1760 mtime = os.stat(template_file).st_mtime
1761
1762 # invoke the editor
dba1c9d9 1763 if "P4EDITOR" in os.environ and (os.environ.get("P4EDITOR") != ""):
7c766e57
PW
1764 editor = os.environ.get("P4EDITOR")
1765 else:
1766 editor = read_pipe("git var GIT_EDITOR").strip()
2dade7a7 1767 system(["sh", "-c", ('%s "$@"' % editor), editor, template_file])
7c766e57
PW
1768
1769 # If the file was not saved, prompt to see if this patch should
1770 # be skipped. But skip this verification step if configured so.
0d609032 1771 if gitConfigBool("git-p4.skipSubmitEditCheck"):
7c766e57
PW
1772 return True
1773
d1652049
PW
1774 # modification time updated means user saved the file
1775 if os.stat(template_file).st_mtime > mtime:
1776 return True
1777
1778 while True:
1779 response = raw_input("Submit template unchanged. Submit anyway? [y]es, [n]o (skip this patch) ")
1780 if response == 'y':
1781 return True
1782 if response == 'n':
1783 return False
7c766e57 1784
df8a9e86 1785 def get_diff_description(self, editedFiles, filesToAdd, symlinks):
b4073bb3 1786 # diff
dba1c9d9 1787 if "P4DIFF" in os.environ:
b4073bb3
MC
1788 del(os.environ["P4DIFF"])
1789 diff = ""
1790 for editedFile in editedFiles:
1791 diff += p4_read_pipe(['diff', '-du',
1792 wildcard_encode(editedFile)])
1793
1794 # new file diff
1795 newdiff = ""
1796 for newFile in filesToAdd:
1797 newdiff += "==== new file ====\n"
1798 newdiff += "--- /dev/null\n"
1799 newdiff += "+++ %s\n" % newFile
df8a9e86
LD
1800
1801 is_link = os.path.islink(newFile)
1802 expect_link = newFile in symlinks
1803
1804 if is_link and expect_link:
1805 newdiff += "+%s\n" % os.readlink(newFile)
1806 else:
1807 f = open(newFile, "r")
1808 for line in f.readlines():
1809 newdiff += "+" + line
1810 f.close()
b4073bb3 1811
e2a892ee 1812 return (diff + newdiff).replace('\r\n', '\n')
b4073bb3 1813
7cb5cbef 1814 def applyCommit(self, id):
67b0fe2e
PW
1815 """Apply one commit, return True if it succeeded."""
1816
f2606b17
LD
1817 print("Applying", read_pipe(["git", "show", "-s",
1818 "--format=format:%h %s", id]))
ae901090 1819
848de9c3 1820 (p4User, gitEmail) = self.p4UserForCommit(id)
3ea2cfd4 1821
84cb0003 1822 diff = read_pipe_lines("git diff-tree -r %s \"%s^\" \"%s\"" % (self.diffOpts, id, id))
4f5cf76a 1823 filesToAdd = set()
a02b8bc4 1824 filesToChangeType = set()
4f5cf76a 1825 filesToDelete = set()
d336c158 1826 editedFiles = set()
b6ad6dcc 1827 pureRenameCopy = set()
df8a9e86 1828 symlinks = set()
c65b670e 1829 filesToChangeExecBit = {}
46c609e9 1830 all_files = list()
60df071c 1831
4f5cf76a 1832 for line in diff:
b43b0a3c
CP
1833 diff = parseDiffTreeEntry(line)
1834 modifier = diff['status']
1835 path = diff['src']
46c609e9
LD
1836 all_files.append(path)
1837
4f5cf76a 1838 if modifier == "M":
6de040df 1839 p4_edit(path)
c65b670e
CP
1840 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
1841 filesToChangeExecBit[path] = diff['dst_mode']
d336c158 1842 editedFiles.add(path)
4f5cf76a
SH
1843 elif modifier == "A":
1844 filesToAdd.add(path)
c65b670e 1845 filesToChangeExecBit[path] = diff['dst_mode']
4f5cf76a
SH
1846 if path in filesToDelete:
1847 filesToDelete.remove(path)
df8a9e86
LD
1848
1849 dst_mode = int(diff['dst_mode'], 8)
db2d997e 1850 if dst_mode == 0o120000:
df8a9e86
LD
1851 symlinks.add(path)
1852
4f5cf76a
SH
1853 elif modifier == "D":
1854 filesToDelete.add(path)
1855 if path in filesToAdd:
1856 filesToAdd.remove(path)
4fddb41b
VA
1857 elif modifier == "C":
1858 src, dest = diff['src'], diff['dst']
6de040df 1859 p4_integrate(src, dest)
b6ad6dcc 1860 pureRenameCopy.add(dest)
4fddb41b 1861 if diff['src_sha1'] != diff['dst_sha1']:
6de040df 1862 p4_edit(dest)
b6ad6dcc 1863 pureRenameCopy.discard(dest)
4fddb41b 1864 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
6de040df 1865 p4_edit(dest)
b6ad6dcc 1866 pureRenameCopy.discard(dest)
4fddb41b 1867 filesToChangeExecBit[dest] = diff['dst_mode']
d20f0f8e
PW
1868 if self.isWindows:
1869 # turn off read-only attribute
1870 os.chmod(dest, stat.S_IWRITE)
4fddb41b
VA
1871 os.unlink(dest)
1872 editedFiles.add(dest)
d9a5f25b 1873 elif modifier == "R":
b43b0a3c 1874 src, dest = diff['src'], diff['dst']
8e9497c2
GG
1875 if self.p4HasMoveCommand:
1876 p4_edit(src) # src must be open before move
1877 p4_move(src, dest) # opens for (move/delete, move/add)
b6ad6dcc 1878 else:
8e9497c2
GG
1879 p4_integrate(src, dest)
1880 if diff['src_sha1'] != diff['dst_sha1']:
1881 p4_edit(dest)
1882 else:
1883 pureRenameCopy.add(dest)
c65b670e 1884 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
8e9497c2
GG
1885 if not self.p4HasMoveCommand:
1886 p4_edit(dest) # with move: already open, writable
c65b670e 1887 filesToChangeExecBit[dest] = diff['dst_mode']
8e9497c2 1888 if not self.p4HasMoveCommand:
d20f0f8e
PW
1889 if self.isWindows:
1890 os.chmod(dest, stat.S_IWRITE)
8e9497c2
GG
1891 os.unlink(dest)
1892 filesToDelete.add(src)
d9a5f25b 1893 editedFiles.add(dest)
a02b8bc4
RP
1894 elif modifier == "T":
1895 filesToChangeType.add(path)
4f5cf76a
SH
1896 else:
1897 die("unknown modifier %s for %s" % (modifier, path))
1898
749b668c 1899 diffcmd = "git diff-tree --full-index -p \"%s\"" % (id)
47a130b7 1900 patchcmd = diffcmd + " | git apply "
c1b296b9
SH
1901 tryPatchCmd = patchcmd + "--check -"
1902 applyPatchCmd = patchcmd + "--check --apply -"
60df071c 1903 patch_succeeded = True
51a2640a 1904
47a130b7 1905 if os.system(tryPatchCmd) != 0:
60df071c
LD
1906 fixed_rcs_keywords = False
1907 patch_succeeded = False
f2606b17 1908 print("Unfortunately applying the change failed!")
60df071c
LD
1909
1910 # Patch failed, maybe it's just RCS keyword woes. Look through
1911 # the patch to see if that's possible.
0d609032 1912 if gitConfigBool("git-p4.attemptRCSCleanup"):
60df071c
LD
1913 file = None
1914 pattern = None
1915 kwfiles = {}
1916 for file in editedFiles | filesToDelete:
1917 # did this file's delta contain RCS keywords?
1918 pattern = p4_keywords_regexp_for_file(file)
1919
1920 if pattern:
1921 # this file is a possibility...look for RCS keywords.
1922 regexp = re.compile(pattern, re.VERBOSE)
1923 for line in read_pipe_lines(["git", "diff", "%s^..%s" % (id, id), file]):
1924 if regexp.search(line):
1925 if verbose:
f2606b17 1926 print("got keyword match on %s in %s in %s" % (pattern, line, file))
60df071c
LD
1927 kwfiles[file] = pattern
1928 break
1929
1930 for file in kwfiles:
1931 if verbose:
f2606b17 1932 print("zapping %s with %s" % (line,pattern))
d20f0f8e
PW
1933 # File is being deleted, so not open in p4. Must
1934 # disable the read-only bit on windows.
1935 if self.isWindows and file not in editedFiles:
1936 os.chmod(file, stat.S_IWRITE)
60df071c
LD
1937 self.patchRCSKeywords(file, kwfiles[file])
1938 fixed_rcs_keywords = True
1939
1940 if fixed_rcs_keywords:
f2606b17 1941 print("Retrying the patch with RCS keywords cleaned up")
60df071c
LD
1942 if os.system(tryPatchCmd) == 0:
1943 patch_succeeded = True
1944
1945 if not patch_succeeded:
7e5dd9f2
PW
1946 for f in editedFiles:
1947 p4_revert(f)
7e5dd9f2 1948 return False
51a2640a 1949
55ac2ed6
PW
1950 #
1951 # Apply the patch for real, and do add/delete/+x handling.
1952 #
47a130b7 1953 system(applyPatchCmd)
4f5cf76a 1954
a02b8bc4
RP
1955 for f in filesToChangeType:
1956 p4_edit(f, "-t", "auto")
4f5cf76a 1957 for f in filesToAdd:
6de040df 1958 p4_add(f)
4f5cf76a 1959 for f in filesToDelete:
6de040df
LD
1960 p4_revert(f)
1961 p4_delete(f)
4f5cf76a 1962
c65b670e
CP
1963 # Set/clear executable bits
1964 for f in filesToChangeExecBit.keys():
1965 mode = filesToChangeExecBit[f]
1966 setP4ExecBit(f, mode)
1967
8cf422db
LD
1968 update_shelve = 0
1969 if len(self.update_shelve) > 0:
1970 update_shelve = self.update_shelve.pop(0)
1971 p4_reopen_in_change(update_shelve, all_files)
46c609e9 1972
55ac2ed6
PW
1973 #
1974 # Build p4 change description, starting with the contents
1975 # of the git commit message.
1976 #
0e36f2d7 1977 logMessage = extractLogMessageFromGitCommit(id)
0e36f2d7 1978 logMessage = logMessage.strip()
f19cb0a0 1979 (logMessage, jobs) = self.separate_jobs_from_description(logMessage)
4f5cf76a 1980
8cf422db 1981 template = self.prepareSubmitTemplate(update_shelve)
f19cb0a0 1982 submitTemplate = self.prepareLogMessage(template, logMessage, jobs)
ecdba36d 1983
c47178d4 1984 if self.preserveUser:
55ac2ed6 1985 submitTemplate += "\n######## Actual user %s, modified after commit\n" % p4User
c47178d4 1986
55ac2ed6
PW
1987 if self.checkAuthorship and not self.p4UserIsMe(p4User):
1988 submitTemplate += "######## git author %s does not match your p4 account.\n" % gitEmail
1989 submitTemplate += "######## Use option --preserve-user to modify authorship.\n"
1990 submitTemplate += "######## Variable git-p4.skipUserNameCheck hides this message.\n"
c47178d4 1991
55ac2ed6 1992 separatorLine = "######## everything below this line is just the diff #######\n"
b4073bb3
MC
1993 if not self.prepare_p4_only:
1994 submitTemplate += separatorLine
df8a9e86 1995 submitTemplate += self.get_diff_description(editedFiles, filesToAdd, symlinks)
55ac2ed6 1996
c47178d4 1997 (handle, fileName) = tempfile.mkstemp()
e2a892ee 1998 tmpFile = os.fdopen(handle, "w+b")
c47178d4
PW
1999 if self.isWindows:
2000 submitTemplate = submitTemplate.replace("\n", "\r\n")
b4073bb3 2001 tmpFile.write(submitTemplate)
c47178d4
PW
2002 tmpFile.close()
2003
728b7ad8
PW
2004 if self.prepare_p4_only:
2005 #
2006 # Leave the p4 tree prepared, and the submit template around
2007 # and let the user decide what to do next
2008 #
f2606b17
LD
2009 print()
2010 print("P4 workspace prepared for submission.")
2011 print("To submit or revert, go to client workspace")
2012 print(" " + self.clientPath)
2013 print()
2014 print("To submit, use \"p4 submit\" to write a new description,")
2015 print("or \"p4 submit -i <%s\" to use the one prepared by" \
2016 " \"git p4\"." % fileName)
2017 print("You can delete the file \"%s\" when finished." % fileName)
728b7ad8
PW
2018
2019 if self.preserveUser and p4User and not self.p4UserIsMe(p4User):
f2606b17 2020 print("To preserve change ownership by user %s, you must\n" \
728b7ad8 2021 "do \"p4 change -f <change>\" after submitting and\n" \
f2606b17 2022 "edit the User field.")
728b7ad8 2023 if pureRenameCopy:
f2606b17
LD
2024 print("After submitting, renamed files must be re-synced.")
2025 print("Invoke \"p4 sync -f\" on each of these files:")
728b7ad8 2026 for f in pureRenameCopy:
f2606b17 2027 print(" " + f)
728b7ad8 2028
f2606b17
LD
2029 print()
2030 print("To revert the changes, use \"p4 revert ...\", and delete")
2031 print("the submit template file \"%s\"" % fileName)
728b7ad8 2032 if filesToAdd:
f2606b17 2033 print("Since the commit adds new files, they must be deleted:")
728b7ad8 2034 for f in filesToAdd:
f2606b17
LD
2035 print(" " + f)
2036 print()
728b7ad8
PW
2037 return True
2038
55ac2ed6
PW
2039 #
2040 # Let the user edit the change description, then submit it.
2041 #
b7638fed 2042 submitted = False
cdc7e388 2043
b7638fed
GE
2044 try:
2045 if self.edit_template(fileName):
2046 # read the edited message and submit
2047 tmpFile = open(fileName, "rb")
2048 message = tmpFile.read()
2049 tmpFile.close()
2050 if self.isWindows:
2051 message = message.replace("\r\n", "\n")
2052 submitTemplate = message[:message.index(separatorLine)]
46c609e9 2053
8cf422db 2054 if update_shelve:
46c609e9
LD
2055 p4_write_pipe(['shelve', '-r', '-i'], submitTemplate)
2056 elif self.shelve:
b34fa577
VK
2057 p4_write_pipe(['shelve', '-i'], submitTemplate)
2058 else:
2059 p4_write_pipe(['submit', '-i'], submitTemplate)
2060 # The rename/copy happened by applying a patch that created a
2061 # new file. This leaves it writable, which confuses p4.
2062 for f in pureRenameCopy:
2063 p4_sync(f, "-f")
b7638fed
GE
2064
2065 if self.preserveUser:
2066 if p4User:
2067 # Get last changelist number. Cannot easily get it from
2068 # the submit command output as the output is
2069 # unmarshalled.
2070 changelist = self.lastP4Changelist()
2071 self.modifyChangelistUser(changelist, p4User)
2072
b7638fed
GE
2073 submitted = True
2074
2075 finally:
c47178d4 2076 # skip this patch
b34fa577
VK
2077 if not submitted or self.shelve:
2078 if self.shelve:
2079 print ("Reverting shelved files.")
2080 else:
2081 print ("Submission cancelled, undoing p4 changes.")
2082 for f in editedFiles | filesToDelete:
b7638fed
GE
2083 p4_revert(f)
2084 for f in filesToAdd:
2085 p4_revert(f)
2086 os.remove(f)
c47178d4
PW
2087
2088 os.remove(fileName)
b7638fed 2089 return submitted
4f5cf76a 2090
06804c76
LD
2091 # Export git tags as p4 labels. Create a p4 label and then tag
2092 # with that.
2093 def exportGitTags(self, gitTags):
c8942a22
LD
2094 validLabelRegexp = gitConfig("git-p4.labelExportRegexp")
2095 if len(validLabelRegexp) == 0:
2096 validLabelRegexp = defaultLabelRegexp
2097 m = re.compile(validLabelRegexp)
06804c76
LD
2098
2099 for name in gitTags:
2100
2101 if not m.match(name):
2102 if verbose:
f2606b17 2103 print("tag %s does not match regexp %s" % (name, validLabelRegexp))
06804c76
LD
2104 continue
2105
2106 # Get the p4 commit this corresponds to
c8942a22
LD
2107 logMessage = extractLogMessageFromGitCommit(name)
2108 values = extractSettingsGitLog(logMessage)
06804c76 2109
dba1c9d9 2110 if 'change' not in values:
06804c76
LD
2111 # a tag pointing to something not sent to p4; ignore
2112 if verbose:
f2606b17 2113 print("git tag %s does not give a p4 commit" % name)
06804c76 2114 continue
c8942a22
LD
2115 else:
2116 changelist = values['change']
06804c76
LD
2117
2118 # Get the tag details.
2119 inHeader = True
2120 isAnnotated = False
2121 body = []
2122 for l in read_pipe_lines(["git", "cat-file", "-p", name]):
2123 l = l.strip()
2124 if inHeader:
2125 if re.match(r'tag\s+', l):
2126 isAnnotated = True
2127 elif re.match(r'\s*$', l):
2128 inHeader = False
2129 continue
2130 else:
2131 body.append(l)
2132
2133 if not isAnnotated:
2134 body = ["lightweight tag imported by git p4\n"]
2135
2136 # Create the label - use the same view as the client spec we are using
2137 clientSpec = getClientSpec()
2138
2139 labelTemplate = "Label: %s\n" % name
2140 labelTemplate += "Description:\n"
2141 for b in body:
2142 labelTemplate += "\t" + b + "\n"
2143 labelTemplate += "View:\n"
9d57c4a6
KS
2144 for depot_side in clientSpec.mappings:
2145 labelTemplate += "\t%s\n" % depot_side
06804c76 2146
ef739f08 2147 if self.dry_run:
f2606b17 2148 print("Would create p4 label %s for tag" % name)
728b7ad8 2149 elif self.prepare_p4_only:
f2606b17
LD
2150 print("Not creating p4 label %s for tag due to option" \
2151 " --prepare-p4-only" % name)
ef739f08
PW
2152 else:
2153 p4_write_pipe(["label", "-i"], labelTemplate)
06804c76 2154
ef739f08
PW
2155 # Use the label
2156 p4_system(["tag", "-l", name] +
9d57c4a6 2157 ["%s@%s" % (depot_side, changelist) for depot_side in clientSpec.mappings])
06804c76 2158
ef739f08 2159 if verbose:
f2606b17 2160 print("created p4 label for tag %s" % name)
06804c76 2161
4f5cf76a 2162 def run(self, args):
c9b50e63
SH
2163 if len(args) == 0:
2164 self.master = currentGitBranch()
c9b50e63
SH
2165 elif len(args) == 1:
2166 self.master = args[0]
28755dba
PW
2167 if not branchExists(self.master):
2168 die("Branch %s does not exist" % self.master)
c9b50e63
SH
2169 else:
2170 return False
2171
8cf422db
LD
2172 for i in self.update_shelve:
2173 if i <= 0:
2174 sys.exit("invalid changelist %d" % i)
2175
00ad6e31
LD
2176 if self.master:
2177 allowSubmit = gitConfig("git-p4.allowSubmit")
2178 if len(allowSubmit) > 0 and not self.master in allowSubmit.split(","):
2179 die("%s is not in git-p4.allowSubmit" % self.master)
4c2d5d72 2180
27d2d811 2181 [upstream, settings] = findUpstreamBranchPoint()
ea99c3ae 2182 self.depotPath = settings['depot-paths'][0]
27d2d811
SH
2183 if len(self.origin) == 0:
2184 self.origin = upstream
a3fdd579 2185
8cf422db 2186 if len(self.update_shelve) > 0:
46c609e9
LD
2187 self.shelve = True
2188
3ea2cfd4
LD
2189 if self.preserveUser:
2190 if not self.canChangeChangelists():
2191 die("Cannot preserve user names without p4 super-user or admin permissions")
2192
6bbfd137
PW
2193 # if not set from the command line, try the config file
2194 if self.conflict_behavior is None:
2195 val = gitConfig("git-p4.conflict")
2196 if val:
2197 if val not in self.conflict_behavior_choices:
2198 die("Invalid value '%s' for config git-p4.conflict" % val)
2199 else:
2200 val = "ask"
2201 self.conflict_behavior = val
2202
a3fdd579 2203 if self.verbose:
f2606b17 2204 print("Origin branch is " + self.origin)
9512497b 2205
ea99c3ae 2206 if len(self.depotPath) == 0:
f2606b17 2207 print("Internal error: cannot locate perforce depot path from existing branches")
9512497b
SH
2208 sys.exit(128)
2209
543987bd 2210 self.useClientSpec = False
0d609032 2211 if gitConfigBool("git-p4.useclientspec"):
543987bd
PW
2212 self.useClientSpec = True
2213 if self.useClientSpec:
2214 self.clientSpecDirs = getClientSpec()
9512497b 2215
2e3a16b2 2216 # Check for the existence of P4 branches
cd884106
VA
2217 branchesDetected = (len(p4BranchesInGit().keys()) > 1)
2218
2219 if self.useClientSpec and not branchesDetected:
543987bd
PW
2220 # all files are relative to the client spec
2221 self.clientPath = getClientRoot()
2222 else:
2223 self.clientPath = p4Where(self.depotPath)
9512497b 2224
543987bd
PW
2225 if self.clientPath == "":
2226 die("Error: Cannot locate perforce checkout of %s in client view" % self.depotPath)
9512497b 2227
f2606b17 2228 print("Perforce checkout for depot path %s located at %s" % (self.depotPath, self.clientPath))
7944f142 2229 self.oldWorkingDirectory = os.getcwd()
c1b296b9 2230
0591cfa8 2231 # ensure the clientPath exists
8d7ec362 2232 new_client_dir = False
0591cfa8 2233 if not os.path.exists(self.clientPath):
8d7ec362 2234 new_client_dir = True
0591cfa8
GG
2235 os.makedirs(self.clientPath)
2236
bbd84863 2237 chdir(self.clientPath, is_client_path=True)
ef739f08 2238 if self.dry_run:
f2606b17 2239 print("Would synchronize p4 checkout in %s" % self.clientPath)
8d7ec362 2240 else:
f2606b17 2241 print("Synchronizing p4 checkout...")
ef739f08
PW
2242 if new_client_dir:
2243 # old one was destroyed, and maybe nobody told p4
2244 p4_sync("...", "-f")
2245 else:
2246 p4_sync("...")
4f5cf76a 2247 self.check()
4f5cf76a 2248
4c750c0d 2249 commits = []
00ad6e31 2250 if self.master:
89f32a92 2251 committish = self.master
00ad6e31 2252 else:
89f32a92 2253 committish = 'HEAD'
00ad6e31 2254
f55b87c1
RM
2255 if self.commit != "":
2256 if self.commit.find("..") != -1:
2257 limits_ish = self.commit.split("..")
2258 for line in read_pipe_lines(["git", "rev-list", "--no-merges", "%s..%s" % (limits_ish[0], limits_ish[1])]):
2259 commits.append(line.strip())
2260 commits.reverse()
2261 else:
2262 commits.append(self.commit)
2263 else:
e6388994 2264 for line in read_pipe_lines(["git", "rev-list", "--no-merges", "%s..%s" % (self.origin, committish)]):
f55b87c1
RM
2265 commits.append(line.strip())
2266 commits.reverse()
4f5cf76a 2267
0d609032 2268 if self.preserveUser or gitConfigBool("git-p4.skipUserNameCheck"):
848de9c3
LD
2269 self.checkAuthorship = False
2270 else:
2271 self.checkAuthorship = True
2272
3ea2cfd4
LD
2273 if self.preserveUser:
2274 self.checkValidP4Users(commits)
2275
84cb0003
GG
2276 #
2277 # Build up a set of options to be passed to diff when
2278 # submitting each commit to p4.
2279 #
2280 if self.detectRenames:
2281 # command-line -M arg
2282 self.diffOpts = "-M"
2283 else:
2284 # If not explicitly set check the config variable
2285 detectRenames = gitConfig("git-p4.detectRenames")
2286
2287 if detectRenames.lower() == "false" or detectRenames == "":
2288 self.diffOpts = ""
2289 elif detectRenames.lower() == "true":
2290 self.diffOpts = "-M"
2291 else:
2292 self.diffOpts = "-M%s" % detectRenames
2293
2294 # no command-line arg for -C or --find-copies-harder, just
2295 # config variables
2296 detectCopies = gitConfig("git-p4.detectCopies")
2297 if detectCopies.lower() == "false" or detectCopies == "":
2298 pass
2299 elif detectCopies.lower() == "true":
2300 self.diffOpts += " -C"
2301 else:
2302 self.diffOpts += " -C%s" % detectCopies
2303
0d609032 2304 if gitConfigBool("git-p4.detectCopiesHarder"):
84cb0003
GG
2305 self.diffOpts += " --find-copies-harder"
2306
8cf422db
LD
2307 num_shelves = len(self.update_shelve)
2308 if num_shelves > 0 and num_shelves != len(commits):
2309 sys.exit("number of commits (%d) must match number of shelved changelist (%d)" %
2310 (len(commits), num_shelves))
2311
251c8c50
CB
2312 hooks_path = gitConfig("core.hooksPath")
2313 if len(hooks_path) <= 0:
2314 hooks_path = os.path.join(os.environ.get("GIT_DIR", ".git"), "hooks")
2315
2316 hook_file = os.path.join(hooks_path, "p4-pre-submit")
2317 if os.path.isfile(hook_file) and os.access(hook_file, os.X_OK) and subprocess.call([hook_file]) != 0:
2318 sys.exit(1)
2319
7e5dd9f2
PW
2320 #
2321 # Apply the commits, one at a time. On failure, ask if should
2322 # continue to try the rest of the patches, or quit.
2323 #
ef739f08 2324 if self.dry_run:
f2606b17 2325 print("Would apply")
67b0fe2e 2326 applied = []
7e5dd9f2
PW
2327 last = len(commits) - 1
2328 for i, commit in enumerate(commits):
ef739f08 2329 if self.dry_run:
f2606b17
LD
2330 print(" ", read_pipe(["git", "show", "-s",
2331 "--format=format:%h %s", commit]))
ef739f08
PW
2332 ok = True
2333 else:
2334 ok = self.applyCommit(commit)
67b0fe2e
PW
2335 if ok:
2336 applied.append(commit)
7e5dd9f2 2337 else:
728b7ad8 2338 if self.prepare_p4_only and i < last:
f2606b17
LD
2339 print("Processing only the first commit due to option" \
2340 " --prepare-p4-only")
728b7ad8 2341 break
7e5dd9f2
PW
2342 if i < last:
2343 quit = False
2344 while True:
6bbfd137
PW
2345 # prompt for what to do, or use the option/variable
2346 if self.conflict_behavior == "ask":
f2606b17 2347 print("What do you want to do?")
6bbfd137
PW
2348 response = raw_input("[s]kip this commit but apply"
2349 " the rest, or [q]uit? ")
2350 if not response:
2351 continue
2352 elif self.conflict_behavior == "skip":
2353 response = "s"
2354 elif self.conflict_behavior == "quit":
2355 response = "q"
2356 else:
2357 die("Unknown conflict_behavior '%s'" %
2358 self.conflict_behavior)
2359
7e5dd9f2 2360 if response[0] == "s":
f2606b17 2361 print("Skipping this commit, but applying the rest")
7e5dd9f2
PW
2362 break
2363 if response[0] == "q":
f2606b17 2364 print("Quitting")
7e5dd9f2
PW
2365 quit = True
2366 break
2367 if quit:
2368 break
4f5cf76a 2369
67b0fe2e 2370 chdir(self.oldWorkingDirectory)
b34fa577 2371 shelved_applied = "shelved" if self.shelve else "applied"
ef739f08
PW
2372 if self.dry_run:
2373 pass
728b7ad8
PW
2374 elif self.prepare_p4_only:
2375 pass
ef739f08 2376 elif len(commits) == len(applied):
f2606b17 2377 print("All commits {0}!".format(shelved_applied))
14594f4b 2378
4c750c0d 2379 sync = P4Sync()
44e8d26c
PW
2380 if self.branch:
2381 sync.branch = self.branch
b9d34db9
LD
2382 if self.disable_p4sync:
2383 sync.sync_origin_only()
2384 else:
2385 sync.run([])
14594f4b 2386
b9d34db9
LD
2387 if not self.disable_rebase:
2388 rebase = P4Rebase()
2389 rebase.rebase()
4f5cf76a 2390
67b0fe2e
PW
2391 else:
2392 if len(applied) == 0:
f2606b17 2393 print("No commits {0}.".format(shelved_applied))
67b0fe2e 2394 else:
f2606b17 2395 print("{0} only the commits marked with '*':".format(shelved_applied.capitalize()))
67b0fe2e
PW
2396 for c in commits:
2397 if c in applied:
2398 star = "*"
2399 else:
2400 star = " "
f2606b17
LD
2401 print(star, read_pipe(["git", "show", "-s",
2402 "--format=format:%h %s", c]))
2403 print("You will have to do 'git p4 sync' and rebase.")
67b0fe2e 2404
0d609032 2405 if gitConfigBool("git-p4.exportLabels"):
06dcd152 2406 self.exportLabels = True
06804c76
LD
2407
2408 if self.exportLabels:
2409 p4Labels = getP4Labels(self.depotPath)
2410 gitTags = getGitTags()
2411
2412 missingGitTags = gitTags - p4Labels
2413 self.exportGitTags(missingGitTags)
2414
98e023de 2415 # exit with error unless everything applied perfectly
67b0fe2e
PW
2416 if len(commits) != len(applied):
2417 sys.exit(1)
2418
b984733c
SH
2419 return True
2420
ecb7cf98
PW
2421class View(object):
2422 """Represent a p4 view ("p4 help views"), and map files in a
2423 repo according to the view."""
2424
9d57c4a6 2425 def __init__(self, client_name):
ecb7cf98 2426 self.mappings = []
9d57c4a6
KS
2427 self.client_prefix = "//%s/" % client_name
2428 # cache results of "p4 where" to lookup client file locations
2429 self.client_spec_path_cache = {}
ecb7cf98
PW
2430
2431 def append(self, view_line):
2432 """Parse a view line, splitting it into depot and client
9d57c4a6
KS
2433 sides. Append to self.mappings, preserving order. This
2434 is only needed for tag creation."""
ecb7cf98
PW
2435
2436 # Split the view line into exactly two words. P4 enforces
2437 # structure on these lines that simplifies this quite a bit.
2438 #
2439 # Either or both words may be double-quoted.
2440 # Single quotes do not matter.
2441 # Double-quote marks cannot occur inside the words.
2442 # A + or - prefix is also inside the quotes.
2443 # There are no quotes unless they contain a space.
2444 # The line is already white-space stripped.
2445 # The two words are separated by a single space.
2446 #
2447 if view_line[0] == '"':
2448 # First word is double quoted. Find its end.
2449 close_quote_index = view_line.find('"', 1)
2450 if close_quote_index <= 0:
2451 die("No first-word closing quote found: %s" % view_line)
2452 depot_side = view_line[1:close_quote_index]
2453 # skip closing quote and space
2454 rhs_index = close_quote_index + 1 + 1
2455 else:
2456 space_index = view_line.find(" ")
2457 if space_index <= 0:
2458 die("No word-splitting space found: %s" % view_line)
2459 depot_side = view_line[0:space_index]
2460 rhs_index = space_index + 1
2461
ecb7cf98 2462 # prefix + means overlay on previous mapping
ecb7cf98 2463 if depot_side.startswith("+"):
ecb7cf98
PW
2464 depot_side = depot_side[1:]
2465
9d57c4a6 2466 # prefix - means exclude this path, leave out of mappings
ecb7cf98
PW
2467 exclude = False
2468 if depot_side.startswith("-"):
2469 exclude = True
2470 depot_side = depot_side[1:]
2471
9d57c4a6
KS
2472 if not exclude:
2473 self.mappings.append(depot_side)
ecb7cf98 2474
9d57c4a6
KS
2475 def convert_client_path(self, clientFile):
2476 # chop off //client/ part to make it relative
2477 if not clientFile.startswith(self.client_prefix):
2478 die("No prefix '%s' on clientFile '%s'" %
2479 (self.client_prefix, clientFile))
2480 return clientFile[len(self.client_prefix):]
ecb7cf98 2481
9d57c4a6
KS
2482 def update_client_spec_path_cache(self, files):
2483 """ Caching file paths by "p4 where" batch query """
ecb7cf98 2484
9d57c4a6
KS
2485 # List depot file paths exclude that already cached
2486 fileArgs = [f['path'] for f in files if f['path'] not in self.client_spec_path_cache]
ecb7cf98 2487
9d57c4a6
KS
2488 if len(fileArgs) == 0:
2489 return # All files in cache
ecb7cf98 2490
9d57c4a6
KS
2491 where_result = p4CmdList(["-x", "-", "where"], stdin=fileArgs)
2492 for res in where_result:
2493 if "code" in res and res["code"] == "error":
2494 # assume error is "... file(s) not in client view"
2495 continue
2496 if "clientFile" not in res:
20005443 2497 die("No clientFile in 'p4 where' output")
9d57c4a6
KS
2498 if "unmap" in res:
2499 # it will list all of them, but only one not unmap-ped
2500 continue
a0a50d87
LS
2501 if gitConfigBool("core.ignorecase"):
2502 res['depotFile'] = res['depotFile'].lower()
9d57c4a6 2503 self.client_spec_path_cache[res['depotFile']] = self.convert_client_path(res["clientFile"])
ecb7cf98 2504
9d57c4a6
KS
2505 # not found files or unmap files set to ""
2506 for depotFile in fileArgs:
a0a50d87
LS
2507 if gitConfigBool("core.ignorecase"):
2508 depotFile = depotFile.lower()
9d57c4a6
KS
2509 if depotFile not in self.client_spec_path_cache:
2510 self.client_spec_path_cache[depotFile] = ""
ecb7cf98 2511
9d57c4a6
KS
2512 def map_in_client(self, depot_path):
2513 """Return the relative location in the client where this
2514 depot file should live. Returns "" if the file should
2515 not be mapped in the client."""
ecb7cf98 2516
a0a50d87
LS
2517 if gitConfigBool("core.ignorecase"):
2518 depot_path = depot_path.lower()
2519
9d57c4a6
KS
2520 if depot_path in self.client_spec_path_cache:
2521 return self.client_spec_path_cache[depot_path]
2522
2523 die( "Error: %s is not found in client spec path" % depot_path )
2524 return ""
ecb7cf98 2525
3ea2cfd4 2526class P4Sync(Command, P4UserMap):
56c09345
PW
2527 delete_actions = ( "delete", "move/delete", "purge" )
2528
b984733c
SH
2529 def __init__(self):
2530 Command.__init__(self)
3ea2cfd4 2531 P4UserMap.__init__(self)
b984733c
SH
2532 self.options = [
2533 optparse.make_option("--branch", dest="branch"),
2534 optparse.make_option("--detect-branches", dest="detectBranches", action="store_true"),
2535 optparse.make_option("--changesfile", dest="changesFile"),
2536 optparse.make_option("--silent", dest="silent", action="store_true"),
ef48f909 2537 optparse.make_option("--detect-labels", dest="detectLabels", action="store_true"),
06804c76 2538 optparse.make_option("--import-labels", dest="importLabels", action="store_true"),
d2c6dd30
HWN
2539 optparse.make_option("--import-local", dest="importIntoRemotes", action="store_false",
2540 help="Import into refs/heads/ , not refs/remotes"),
96b2d54a
LS
2541 optparse.make_option("--max-changes", dest="maxChanges",
2542 help="Maximum number of changes to import"),
2543 optparse.make_option("--changes-block-size", dest="changes_block_size", type="int",
2544 help="Internal block size to use when iteratively calling p4 changes"),
86dff6b6 2545 optparse.make_option("--keep-path", dest="keepRepoPath", action='store_true',
3a70cdfa
TAL
2546 help="Keep entire BRANCH/DIR/SUBDIR prefix during import"),
2547 optparse.make_option("--use-client-spec", dest="useClientSpec", action='store_true',
51334bb0
LD
2548 help="Only sync files that are included in the Perforce Client Spec"),
2549 optparse.make_option("-/", dest="cloneExclude",
2550 action="append", type="string",
2551 help="exclude depot path"),
b984733c
SH
2552 ]
2553 self.description = """Imports from Perforce into a git repository.\n
2554 example:
2555 //depot/my/project/ -- to import the current head
2556 //depot/my/project/@all -- to import everything
2557 //depot/my/project/@1,6 -- to import only from revision 1 to 6
2558
2559 (a ... is not needed in the path p4 specification, it's added implicitly)"""
2560
2561 self.usage += " //depot/path[@revRange]"
b984733c 2562 self.silent = False
1d7367dc
RG
2563 self.createdBranches = set()
2564 self.committedChanges = set()
569d1bd4 2565 self.branch = ""
b984733c 2566 self.detectBranches = False
cb53e1f8 2567 self.detectLabels = False
06804c76 2568 self.importLabels = False
b984733c 2569 self.changesFile = ""
01265103 2570 self.syncWithOrigin = True
a028a98e 2571 self.importIntoRemotes = True
01a9c9c5 2572 self.maxChanges = ""
1051ef00 2573 self.changes_block_size = None
8b41a97f 2574 self.keepRepoPath = False
6326aa58 2575 self.depotPaths = None
3c699645 2576 self.p4BranchesInGit = []
354081d5 2577 self.cloneExclude = []
3a70cdfa 2578 self.useClientSpec = False
a93d33ee 2579 self.useClientSpec_from_options = False
ecb7cf98 2580 self.clientSpecDirs = None
fed23693 2581 self.tempBranches = []
d604176d 2582 self.tempBranchLocation = "refs/git-p4-tmp"
a5db4b12 2583 self.largeFileSystem = None
123f6317 2584 self.suppress_meta_comment = False
a5db4b12
LS
2585
2586 if gitConfig('git-p4.largeFileSystem'):
2587 largeFileSystemConstructor = globals()[gitConfig('git-p4.largeFileSystem')]
2588 self.largeFileSystem = largeFileSystemConstructor(
2589 lambda git_mode, relPath, contents: self.writeToGitStream(git_mode, relPath, contents)
2590 )
b984733c 2591
01265103
SH
2592 if gitConfig("git-p4.syncFromOrigin") == "false":
2593 self.syncWithOrigin = False
2594
123f6317
LD
2595 self.depotPaths = []
2596 self.changeRange = ""
2597 self.previousDepotPaths = []
2598 self.hasOrigin = False
2599
2600 # map from branch depot path to parent branch
2601 self.knownBranches = {}
2602 self.initialParents = {}
2603
2604 self.tz = "%+03d%02d" % (- time.timezone / 3600, ((- time.timezone % 3600) / 60))
2605 self.labels = {}
2606
fed23693
VA
2607 # Force a checkpoint in fast-import and wait for it to finish
2608 def checkpoint(self):
2609 self.gitStream.write("checkpoint\n\n")
2610 self.gitStream.write("progress checkpoint\n\n")
2611 out = self.gitOutput.readline()
2612 if self.verbose:
f2606b17 2613 print("checkpoint finished: " + out)
fed23693 2614
123f6317
LD
2615 def cmp_shelved(self, path, filerev, revision):
2616 """ Determine if a path at revision #filerev is the same as the file
2617 at revision @revision for a shelved changelist. If they don't match,
2618 unshelving won't be safe (we will get other changes mixed in).
2619
2620 This is comparing the revision that the shelved changelist is *based* on, not
2621 the shelved changelist itself.
2622 """
2623 ret = p4Cmd(["diff2", "{0}#{1}".format(path, filerev), "{0}@{1}".format(path, revision)])
2624 if verbose:
2625 print("p4 diff2 path %s filerev %s revision %s => %s" % (path, filerev, revision, ret))
2626 return ret["status"] == "identical"
2627
2628 def extractFilesFromCommit(self, commit, shelved=False, shelved_cl = 0, origin_revision = 0):
354081d5
TT
2629 self.cloneExclude = [re.sub(r"\.\.\.$", "", path)
2630 for path in self.cloneExclude]
b984733c
SH
2631 files = []
2632 fnum = 0
dba1c9d9 2633 while "depotFile%s" % fnum in commit:
b984733c 2634 path = commit["depotFile%s" % fnum]
6326aa58 2635
354081d5 2636 if [p for p in self.cloneExclude
d53de8b9 2637 if p4PathStartsWith(path, p)]:
354081d5
TT
2638 found = False
2639 else:
2640 found = [p for p in self.depotPaths
d53de8b9 2641 if p4PathStartsWith(path, p)]
6326aa58 2642 if not found:
b984733c
SH
2643 fnum = fnum + 1
2644 continue
2645
2646 file = {}
2647 file["path"] = path
2648 file["rev"] = commit["rev%s" % fnum]
2649 file["action"] = commit["action%s" % fnum]
2650 file["type"] = commit["type%s" % fnum]
123f6317
LD
2651 if shelved:
2652 file["shelved_cl"] = int(shelved_cl)
2653
2654 # For shelved changelists, check that the revision of each file that the
2655 # shelve was based on matches the revision that we are using for the
2656 # starting point for git-fast-import (self.initialParent). Otherwise
2657 # the resulting diff will contain deltas from multiple commits.
2658
2659 if file["action"] != "add" and \
2660 not self.cmp_shelved(path, file["rev"], origin_revision):
2661 sys.exit("change {0} not based on {1} for {2}, cannot unshelve".format(
2662 commit["change"], self.initialParent, path))
2663
b984733c
SH
2664 files.append(file)
2665 fnum = fnum + 1
2666 return files
2667
26e6a27d
JD
2668 def extractJobsFromCommit(self, commit):
2669 jobs = []
2670 jnum = 0
dba1c9d9 2671 while "job%s" % jnum in commit:
26e6a27d
JD
2672 job = commit["job%s" % jnum]
2673 jobs.append(job)
2674 jnum = jnum + 1
2675 return jobs
2676
6326aa58 2677 def stripRepoPath(self, path, prefixes):
21ef5df4
PW
2678 """When streaming files, this is called to map a p4 depot path
2679 to where it should go in git. The prefixes are either
2680 self.depotPaths, or self.branchPrefixes in the case of
2681 branch detection."""
2682
3952710b 2683 if self.useClientSpec:
21ef5df4
PW
2684 # branch detection moves files up a level (the branch name)
2685 # from what client spec interpretation gives
0d1696ef 2686 path = self.clientSpecDirs.map_in_client(path)
21ef5df4
PW
2687 if self.detectBranches:
2688 for b in self.knownBranches:
2689 if path.startswith(b + "/"):
2690 path = path[len(b)+1:]
2691
2692 elif self.keepRepoPath:
2693 # Preserve everything in relative path name except leading
2694 # //depot/; just look at first prefix as they all should
2695 # be in the same depot.
2696 depot = re.sub("^(//[^/]+/).*", r'\1', prefixes[0])
2697 if p4PathStartsWith(path, depot):
2698 path = path[len(depot):]
3952710b 2699
0d1696ef 2700 else:
0d1696ef
PW
2701 for p in prefixes:
2702 if p4PathStartsWith(path, p):
2703 path = path[len(p):]
21ef5df4 2704 break
8b41a97f 2705
0d1696ef 2706 path = wildcard_decode(path)
6326aa58 2707 return path
6754a299 2708
71b112d4 2709 def splitFilesIntoBranches(self, commit):
21ef5df4
PW
2710 """Look at each depotFile in the commit to figure out to what
2711 branch it belongs."""
2712
9d57c4a6
KS
2713 if self.clientSpecDirs:
2714 files = self.extractFilesFromCommit(commit)
2715 self.clientSpecDirs.update_client_spec_path_cache(files)
2716
d5904674 2717 branches = {}
71b112d4 2718 fnum = 0
dba1c9d9 2719 while "depotFile%s" % fnum in commit:
71b112d4 2720 path = commit["depotFile%s" % fnum]
6326aa58 2721 found = [p for p in self.depotPaths
d53de8b9 2722 if p4PathStartsWith(path, p)]
6326aa58 2723 if not found:
71b112d4
SH
2724 fnum = fnum + 1
2725 continue
2726
2727 file = {}
2728 file["path"] = path
2729 file["rev"] = commit["rev%s" % fnum]
2730 file["action"] = commit["action%s" % fnum]
2731 file["type"] = commit["type%s" % fnum]
2732 fnum = fnum + 1
2733
21ef5df4
PW
2734 # start with the full relative path where this file would
2735 # go in a p4 client
2736 if self.useClientSpec:
2737 relPath = self.clientSpecDirs.map_in_client(path)
2738 else:
2739 relPath = self.stripRepoPath(path, self.depotPaths)
b984733c 2740
4b97ffb1 2741 for branch in self.knownBranches.keys():
21ef5df4
PW
2742 # add a trailing slash so that a commit into qt/4.2foo
2743 # doesn't end up in qt/4.2, e.g.
6754a299 2744 if relPath.startswith(branch + "/"):
d5904674
SH
2745 if branch not in branches:
2746 branches[branch] = []
71b112d4 2747 branches[branch].append(file)
6555b2cc 2748 break
b984733c
SH
2749
2750 return branches
2751
a5db4b12
LS
2752 def writeToGitStream(self, gitMode, relPath, contents):
2753 self.gitStream.write('M %s inline %s\n' % (gitMode, relPath))
2754 self.gitStream.write('data %d\n' % sum(len(d) for d in contents))
2755 for d in contents:
2756 self.gitStream.write(d)
2757 self.gitStream.write('\n')
2758
a8b05162
LS
2759 def encodeWithUTF8(self, path):
2760 try:
2761 path.decode('ascii')
2762 except:
2763 encoding = 'utf8'
2764 if gitConfig('git-p4.pathEncoding'):
2765 encoding = gitConfig('git-p4.pathEncoding')
2766 path = path.decode(encoding, 'replace').encode('utf8', 'replace')
2767 if self.verbose:
f2606b17 2768 print('Path with non-ASCII characters detected. Used %s to encode: %s ' % (encoding, path))
a8b05162
LS
2769 return path
2770
b932705b
LD
2771 # output one file from the P4 stream
2772 # - helper for streamP4Files
2773
2774 def streamOneP4File(self, file, contents):
b932705b 2775 relPath = self.stripRepoPath(file['depotFile'], self.branchPrefixes)
a8b05162 2776 relPath = self.encodeWithUTF8(relPath)
b932705b 2777 if verbose:
d2176a50
LS
2778 size = int(self.stream_file['fileSize'])
2779 sys.stdout.write('\r%s --> %s (%i MB)\n' % (file['depotFile'], relPath, size/1024/1024))
2780 sys.stdout.flush()
b932705b 2781
9cffb8c8
PW
2782 (type_base, type_mods) = split_p4_type(file["type"])
2783
2784 git_mode = "100644"
2785 if "x" in type_mods:
2786 git_mode = "100755"
2787 if type_base == "symlink":
2788 git_mode = "120000"
1292df11
AJ
2789 # p4 print on a symlink sometimes contains "target\n";
2790 # if it does, remove the newline
b39c3612 2791 data = ''.join(contents)
40f846c3
PW
2792 if not data:
2793 # Some version of p4 allowed creating a symlink that pointed
2794 # to nothing. This causes p4 errors when checking out such
2795 # a change, and errors here too. Work around it by ignoring
2796 # the bad symlink; hopefully a future change fixes it.
f2606b17 2797 print("\nIgnoring empty symlink in %s" % file['depotFile'])
40f846c3
PW
2798 return
2799 elif data[-1] == '\n':
1292df11
AJ
2800 contents = [data[:-1]]
2801 else:
2802 contents = [data]
b932705b 2803
9cffb8c8 2804 if type_base == "utf16":
55aa5714
PW
2805 # p4 delivers different text in the python output to -G
2806 # than it does when using "print -o", or normal p4 client
2807 # operations. utf16 is converted to ascii or utf8, perhaps.
2808 # But ascii text saved as -t utf16 is completely mangled.
2809 # Invoke print -o to get the real contents.
7f0e5962
PW
2810 #
2811 # On windows, the newlines will always be mangled by print, so put
2812 # them back too. This is not needed to the cygwin windows version,
2813 # just the native "NT" type.
2814 #
1f5f3907
LS
2815 try:
2816 text = p4_read_pipe(['print', '-q', '-o', '-', '%s@%s' % (file['depotFile'], file['change'])])
2817 except Exception as e:
2818 if 'Translation of file content failed' in str(e):
2819 type_base = 'binary'
2820 else:
2821 raise e
2822 else:
2823 if p4_version_string().find('/NT') >= 0:
2824 text = text.replace('\r\n', '\n')
2825 contents = [ text ]
55aa5714 2826
9f7ef0ea
PW
2827 if type_base == "apple":
2828 # Apple filetype files will be streamed as a concatenation of
2829 # its appledouble header and the contents. This is useless
2830 # on both macs and non-macs. If using "print -q -o xx", it
2831 # will create "xx" with the data, and "%xx" with the header.
2832 # This is also not very useful.
2833 #
2834 # Ideally, someday, this script can learn how to generate
2835 # appledouble files directly and import those to git, but
2836 # non-mac machines can never find a use for apple filetype.
f2606b17 2837 print("\nIgnoring apple filetype file %s" % file['depotFile'])
9f7ef0ea
PW
2838 return
2839
55aa5714
PW
2840 # Note that we do not try to de-mangle keywords on utf16 files,
2841 # even though in theory somebody may want that.
60df071c
LD
2842 pattern = p4_keywords_regexp_for_type(type_base, type_mods)
2843 if pattern:
2844 regexp = re.compile(pattern, re.VERBOSE)
2845 text = ''.join(contents)
2846 text = regexp.sub(r'$\1$', text)
2847 contents = [ text ]
b932705b 2848
a5db4b12
LS
2849 if self.largeFileSystem:
2850 (git_mode, contents) = self.largeFileSystem.processContent(git_mode, relPath, contents)
b932705b 2851
a5db4b12 2852 self.writeToGitStream(git_mode, relPath, contents)
b932705b
LD
2853
2854 def streamOneP4Deletion(self, file):
2855 relPath = self.stripRepoPath(file['path'], self.branchPrefixes)
a8b05162 2856 relPath = self.encodeWithUTF8(relPath)
b932705b 2857 if verbose:
d2176a50
LS
2858 sys.stdout.write("delete %s\n" % relPath)
2859 sys.stdout.flush()
b932705b
LD
2860 self.gitStream.write("D %s\n" % relPath)
2861
a5db4b12
LS
2862 if self.largeFileSystem and self.largeFileSystem.isLargeFile(relPath):
2863 self.largeFileSystem.removeLargeFile(relPath)
2864
b932705b
LD
2865 # handle another chunk of streaming data
2866 def streamP4FilesCb(self, marshalled):
2867
78189bea
PW
2868 # catch p4 errors and complain
2869 err = None
2870 if "code" in marshalled:
2871 if marshalled["code"] == "error":
2872 if "data" in marshalled:
2873 err = marshalled["data"].rstrip()
4d25dc44
LS
2874
2875 if not err and 'fileSize' in self.stream_file:
2876 required_bytes = int((4 * int(self.stream_file["fileSize"])) - calcDiskFree())
2877 if required_bytes > 0:
2878 err = 'Not enough space left on %s! Free at least %i MB.' % (
2879 os.getcwd(), required_bytes/1024/1024
2880 )
2881
78189bea
PW
2882 if err:
2883 f = None
2884 if self.stream_have_file_info:
2885 if "depotFile" in self.stream_file:
2886 f = self.stream_file["depotFile"]
2887 # force a failure in fast-import, else an empty
2888 # commit will be made
2889 self.gitStream.write("\n")
2890 self.gitStream.write("die-now\n")
2891 self.gitStream.close()
2892 # ignore errors, but make sure it exits first
2893 self.importProcess.wait()
2894 if f:
2895 die("Error from p4 print for %s: %s" % (f, err))
2896 else:
2897 die("Error from p4 print: %s" % err)
2898
dba1c9d9 2899 if 'depotFile' in marshalled and self.stream_have_file_info:
c3f6163b
AG
2900 # start of a new file - output the old one first
2901 self.streamOneP4File(self.stream_file, self.stream_contents)
2902 self.stream_file = {}
2903 self.stream_contents = []
2904 self.stream_have_file_info = False
b932705b 2905
c3f6163b
AG
2906 # pick up the new file information... for the
2907 # 'data' field we need to append to our array
2908 for k in marshalled.keys():
2909 if k == 'data':
d2176a50
LS
2910 if 'streamContentSize' not in self.stream_file:
2911 self.stream_file['streamContentSize'] = 0
2912 self.stream_file['streamContentSize'] += len(marshalled['data'])
c3f6163b
AG
2913 self.stream_contents.append(marshalled['data'])
2914 else:
2915 self.stream_file[k] = marshalled[k]
b932705b 2916
d2176a50
LS
2917 if (verbose and
2918 'streamContentSize' in self.stream_file and
2919 'fileSize' in self.stream_file and
2920 'depotFile' in self.stream_file):
2921 size = int(self.stream_file["fileSize"])
2922 if size > 0:
2923 progress = 100*self.stream_file['streamContentSize']/size
2924 sys.stdout.write('\r%s %d%% (%i MB)' % (self.stream_file['depotFile'], progress, int(size/1024/1024)))
2925 sys.stdout.flush()
2926
c3f6163b 2927 self.stream_have_file_info = True
b932705b
LD
2928
2929 # Stream directly from "p4 files" into "git fast-import"
2930 def streamP4Files(self, files):
30b5940b
SH
2931 filesForCommit = []
2932 filesToRead = []
b932705b 2933 filesToDelete = []
30b5940b 2934
3a70cdfa 2935 for f in files:
ecb7cf98
PW
2936 filesForCommit.append(f)
2937 if f['action'] in self.delete_actions:
2938 filesToDelete.append(f)
2939 else:
2940 filesToRead.append(f)
6a49f8e2 2941
b932705b
LD
2942 # deleted files...
2943 for f in filesToDelete:
2944 self.streamOneP4Deletion(f)
1b9a4684 2945
b932705b
LD
2946 if len(filesToRead) > 0:
2947 self.stream_file = {}
2948 self.stream_contents = []
2949 self.stream_have_file_info = False
8ff45f2a 2950
c3f6163b
AG
2951 # curry self argument
2952 def streamP4FilesCbSelf(entry):
2953 self.streamP4FilesCb(entry)
6a49f8e2 2954
123f6317
LD
2955 fileArgs = []
2956 for f in filesToRead:
2957 if 'shelved_cl' in f:
2958 # Handle shelved CLs using the "p4 print file@=N" syntax to print
2959 # the contents
2960 fileArg = '%s@=%d' % (f['path'], f['shelved_cl'])
2961 else:
2962 fileArg = '%s#%s' % (f['path'], f['rev'])
2963
2964 fileArgs.append(fileArg)
6de040df
LD
2965
2966 p4CmdList(["-x", "-", "print"],
2967 stdin=fileArgs,
2968 cb=streamP4FilesCbSelf)
30b5940b 2969
b932705b 2970 # do the last chunk
dba1c9d9 2971 if 'depotFile' in self.stream_file:
b932705b 2972 self.streamOneP4File(self.stream_file, self.stream_contents)
6a49f8e2 2973
affb474f
LD
2974 def make_email(self, userid):
2975 if userid in self.users:
2976 return self.users[userid]
2977 else:
2978 return "%s <a@b>" % userid
2979
06804c76 2980 def streamTag(self, gitStream, labelName, labelDetails, commit, epoch):
b43702ac
LD
2981 """ Stream a p4 tag.
2982 commit is either a git commit, or a fast-import mark, ":<p4commit>"
2983 """
2984
06804c76 2985 if verbose:
f2606b17 2986 print("writing tag %s for commit %s" % (labelName, commit))
06804c76
LD
2987 gitStream.write("tag %s\n" % labelName)
2988 gitStream.write("from %s\n" % commit)
2989
dba1c9d9 2990 if 'Owner' in labelDetails:
06804c76
LD
2991 owner = labelDetails["Owner"]
2992 else:
2993 owner = None
2994
2995 # Try to use the owner of the p4 label, or failing that,
2996 # the current p4 user id.
2997 if owner:
2998 email = self.make_email(owner)
2999 else:
3000 email = self.make_email(self.p4UserId())
3001 tagger = "%s %s %s" % (email, epoch, self.tz)
3002
3003 gitStream.write("tagger %s\n" % tagger)
3004
f2606b17 3005 print("labelDetails=",labelDetails)
dba1c9d9 3006 if 'Description' in labelDetails:
06804c76
LD
3007 description = labelDetails['Description']
3008 else:
3009 description = 'Label from git p4'
3010
3011 gitStream.write("data %d\n" % len(description))
3012 gitStream.write(description)
3013 gitStream.write("\n")
3014
4ae048e6
LS
3015 def inClientSpec(self, path):
3016 if not self.clientSpecDirs:
3017 return True
3018 inClientSpec = self.clientSpecDirs.map_in_client(path)
3019 if not inClientSpec and self.verbose:
3020 print('Ignoring file outside of client spec: {0}'.format(path))
3021 return inClientSpec
3022
3023 def hasBranchPrefix(self, path):
3024 if not self.branchPrefixes:
3025 return True
3026 hasPrefix = [p for p in self.branchPrefixes
3027 if p4PathStartsWith(path, p)]
09667d01 3028 if not hasPrefix and self.verbose:
4ae048e6
LS
3029 print('Ignoring file outside of prefix: {0}'.format(path))
3030 return hasPrefix
3031
e63231e5 3032 def commit(self, details, files, branch, parent = ""):
b984733c
SH
3033 epoch = details["time"]
3034 author = details["user"]
26e6a27d 3035 jobs = self.extractJobsFromCommit(details)
b984733c 3036
4b97ffb1 3037 if self.verbose:
4ae048e6 3038 print('commit into {0}'.format(branch))
96e07dd2 3039
9d57c4a6
KS
3040 if self.clientSpecDirs:
3041 self.clientSpecDirs.update_client_spec_path_cache(files)
3042
4ae048e6
LS
3043 files = [f for f in files
3044 if self.inClientSpec(f['path']) and self.hasBranchPrefix(f['path'])]
3045
3046 if not files and not gitConfigBool('git-p4.keepEmptyCommits'):
3047 print('Ignoring revision {0} as it would produce an empty commit.'
3048 .format(details['change']))
3049 return
3050
b984733c 3051 self.gitStream.write("commit %s\n" % branch)
b43702ac 3052 self.gitStream.write("mark :%s\n" % details["change"])
b984733c
SH
3053 self.committedChanges.add(int(details["change"]))
3054 committer = ""
b607e71e
SH
3055 if author not in self.users:
3056 self.getUserMapFromPerforceServer()
affb474f 3057 committer = "%s %s %s" % (self.make_email(author), epoch, self.tz)
b984733c
SH
3058
3059 self.gitStream.write("committer %s\n" % committer)
3060
3061 self.gitStream.write("data <<EOT\n")
3062 self.gitStream.write(details["desc"])
26e6a27d
JD
3063 if len(jobs) > 0:
3064 self.gitStream.write("\nJobs: %s" % (' '.join(jobs)))
123f6317
LD
3065
3066 if not self.suppress_meta_comment:
3067 self.gitStream.write("\n[git-p4: depot-paths = \"%s\": change = %s" %
3068 (','.join(self.branchPrefixes), details["change"]))
3069 if len(details['options']) > 0:
3070 self.gitStream.write(": options = %s" % details['options'])
3071 self.gitStream.write("]\n")
3072
3073 self.gitStream.write("EOT\n\n")
b984733c
SH
3074
3075 if len(parent) > 0:
4b97ffb1 3076 if self.verbose:
f2606b17 3077 print("parent %s" % parent)
b984733c
SH
3078 self.gitStream.write("from %s\n" % parent)
3079
4ae048e6 3080 self.streamP4Files(files)
b984733c
SH
3081 self.gitStream.write("\n")
3082
1f4ba1cb
SH
3083 change = int(details["change"])
3084
dba1c9d9 3085 if change in self.labels:
1f4ba1cb
SH
3086 label = self.labels[change]
3087 labelDetails = label[0]
3088 labelRevisions = label[1]
71b112d4 3089 if self.verbose:
f2606b17 3090 print("Change %s is labelled %s" % (change, labelDetails))
1f4ba1cb 3091
6de040df 3092 files = p4CmdList(["files"] + ["%s...@%s" % (p, change)
e63231e5 3093 for p in self.branchPrefixes])
1f4ba1cb
SH
3094
3095 if len(files) == len(labelRevisions):
3096
3097 cleanedFiles = {}
3098 for info in files:
56c09345 3099 if info["action"] in self.delete_actions:
1f4ba1cb
SH
3100 continue
3101 cleanedFiles[info["depotFile"]] = info["rev"]
3102
3103 if cleanedFiles == labelRevisions:
06804c76 3104 self.streamTag(self.gitStream, 'tag_%s' % labelDetails['label'], labelDetails, branch, epoch)
1f4ba1cb
SH
3105
3106 else:
a46668fa 3107 if not self.silent:
f2606b17 3108 print("Tag %s does not match with change %s: files do not match."
cebdf5af 3109 % (labelDetails["label"], change))
1f4ba1cb
SH
3110
3111 else:
a46668fa 3112 if not self.silent:
f2606b17 3113 print("Tag %s does not match with change %s: file count is different."
cebdf5af 3114 % (labelDetails["label"], change))
b984733c 3115
06804c76 3116 # Build a dictionary of changelists and labels, for "detect-labels" option.
1f4ba1cb
SH
3117 def getLabels(self):
3118 self.labels = {}
3119
52a4880b 3120 l = p4CmdList(["labels"] + ["%s..." % p for p in self.depotPaths])
10c3211b 3121 if len(l) > 0 and not self.silent:
4d88519f 3122 print("Finding files belonging to labels in %s" % self.depotPaths)
01ce1fe9
SH
3123
3124 for output in l:
1f4ba1cb
SH
3125 label = output["label"]
3126 revisions = {}
3127 newestChange = 0
71b112d4 3128 if self.verbose:
f2606b17 3129 print("Querying files for label %s" % label)
6de040df
LD
3130 for file in p4CmdList(["files"] +
3131 ["%s...@%s" % (p, label)
3132 for p in self.depotPaths]):
1f4ba1cb
SH
3133 revisions[file["depotFile"]] = file["rev"]
3134 change = int(file["change"])
3135 if change > newestChange:
3136 newestChange = change
3137
9bda3a85
SH
3138 self.labels[newestChange] = [output, revisions]
3139
3140 if self.verbose:
f2606b17 3141 print("Label changes: %s" % self.labels.keys())
1f4ba1cb 3142
06804c76
LD
3143 # Import p4 labels as git tags. A direct mapping does not
3144 # exist, so assume that if all the files are at the same revision
3145 # then we can use that, or it's something more complicated we should
3146 # just ignore.
3147 def importP4Labels(self, stream, p4Labels):
3148 if verbose:
f2606b17 3149 print("import p4 labels: " + ' '.join(p4Labels))
06804c76
LD
3150
3151 ignoredP4Labels = gitConfigList("git-p4.ignoredP4Labels")
c8942a22 3152 validLabelRegexp = gitConfig("git-p4.labelImportRegexp")
06804c76
LD
3153 if len(validLabelRegexp) == 0:
3154 validLabelRegexp = defaultLabelRegexp
3155 m = re.compile(validLabelRegexp)
3156
3157 for name in p4Labels:
3158 commitFound = False
3159
3160 if not m.match(name):
3161 if verbose:
f2606b17 3162 print("label %s does not match regexp %s" % (name,validLabelRegexp))
06804c76
LD
3163 continue
3164
3165 if name in ignoredP4Labels:
3166 continue
3167
3168 labelDetails = p4CmdList(['label', "-o", name])[0]
3169
3170 # get the most recent changelist for each file in this label
3171 change = p4Cmd(["changes", "-m", "1"] + ["%s...@%s" % (p, name)
3172 for p in self.depotPaths])
3173
dba1c9d9 3174 if 'change' in change:
06804c76
LD
3175 # find the corresponding git commit; take the oldest commit
3176 changelist = int(change['change'])
b43702ac
LD
3177 if changelist in self.committedChanges:
3178 gitCommit = ":%d" % changelist # use a fast-import mark
06804c76 3179 commitFound = True
b43702ac
LD
3180 else:
3181 gitCommit = read_pipe(["git", "rev-list", "--max-count=1",
3182 "--reverse", ":/\[git-p4:.*change = %d\]" % changelist], ignore_error=True)
3183 if len(gitCommit) == 0:
f2606b17 3184 print("importing label %s: could not find git commit for changelist %d" % (name, changelist))
b43702ac
LD
3185 else:
3186 commitFound = True
3187 gitCommit = gitCommit.strip()
3188
3189 if commitFound:
06804c76
LD
3190 # Convert from p4 time format
3191 try:
3192 tmwhen = time.strptime(labelDetails['Update'], "%Y/%m/%d %H:%M:%S")
3193 except ValueError:
f2606b17 3194 print("Could not convert label time %s" % labelDetails['Update'])
06804c76
LD
3195 tmwhen = 1
3196
3197 when = int(time.mktime(tmwhen))
3198 self.streamTag(stream, name, labelDetails, gitCommit, when)
3199 if verbose:
f2606b17 3200 print("p4 label %s mapped to git commit %s" % (name, gitCommit))
06804c76
LD
3201 else:
3202 if verbose:
f2606b17 3203 print("Label %s has no changelists - possibly deleted?" % name)
06804c76
LD
3204
3205 if not commitFound:
3206 # We can't import this label; don't try again as it will get very
3207 # expensive repeatedly fetching all the files for labels that will
3208 # never be imported. If the label is moved in the future, the
3209 # ignore will need to be removed manually.
3210 system(["git", "config", "--add", "git-p4.ignoredP4Labels", name])
3211
86dff6b6
HWN
3212 def guessProjectName(self):
3213 for p in self.depotPaths:
6e5295c4
SH
3214 if p.endswith("/"):
3215 p = p[:-1]
3216 p = p[p.strip().rfind("/") + 1:]
3217 if not p.endswith("/"):
3218 p += "/"
3219 return p
86dff6b6 3220
4b97ffb1 3221 def getBranchMapping(self):
6555b2cc
SH
3222 lostAndFoundBranches = set()
3223
8ace74c0
VA
3224 user = gitConfig("git-p4.branchUser")
3225 if len(user) > 0:
3226 command = "branches -u %s" % user
3227 else:
3228 command = "branches"
3229
3230 for info in p4CmdList(command):
52a4880b 3231 details = p4Cmd(["branch", "-o", info["branch"]])
4b97ffb1 3232 viewIdx = 0
dba1c9d9 3233 while "View%s" % viewIdx in details:
4b97ffb1
SH
3234 paths = details["View%s" % viewIdx].split(" ")
3235 viewIdx = viewIdx + 1
3236 # require standard //depot/foo/... //depot/bar/... mapping
3237 if len(paths) != 2 or not paths[0].endswith("/...") or not paths[1].endswith("/..."):
3238 continue
3239 source = paths[0]
3240 destination = paths[1]
6509e19c 3241 ## HACK
d53de8b9 3242 if p4PathStartsWith(source, self.depotPaths[0]) and p4PathStartsWith(destination, self.depotPaths[0]):
6509e19c
SH
3243 source = source[len(self.depotPaths[0]):-4]
3244 destination = destination[len(self.depotPaths[0]):-4]
6555b2cc 3245
1a2edf4e
SH
3246 if destination in self.knownBranches:
3247 if not self.silent:
f2606b17
LD
3248 print("p4 branch %s defines a mapping from %s to %s" % (info["branch"], source, destination))
3249 print("but there exists another mapping from %s to %s already!" % (self.knownBranches[destination], destination))
1a2edf4e
SH
3250 continue
3251
6555b2cc
SH
3252 self.knownBranches[destination] = source
3253
3254 lostAndFoundBranches.discard(destination)
3255
29bdbac1 3256 if source not in self.knownBranches:
6555b2cc
SH
3257 lostAndFoundBranches.add(source)
3258
7199cf13
VA
3259 # Perforce does not strictly require branches to be defined, so we also
3260 # check git config for a branch list.
3261 #
3262 # Example of branch definition in git config file:
3263 # [git-p4]
3264 # branchList=main:branchA
3265 # branchList=main:branchB
3266 # branchList=branchA:branchC
3267 configBranches = gitConfigList("git-p4.branchList")
3268 for branch in configBranches:
3269 if branch:
3270 (source, destination) = branch.split(":")
3271 self.knownBranches[destination] = source
3272
3273 lostAndFoundBranches.discard(destination)
3274
3275 if source not in self.knownBranches:
3276 lostAndFoundBranches.add(source)
3277
6555b2cc
SH
3278
3279 for branch in lostAndFoundBranches:
3280 self.knownBranches[branch] = branch
29bdbac1 3281
38f9f5ec
SH
3282 def getBranchMappingFromGitBranches(self):
3283 branches = p4BranchesInGit(self.importIntoRemotes)
3284 for branch in branches.keys():
3285 if branch == "master":
3286 branch = "main"
3287 else:
3288 branch = branch[len(self.projectName):]
3289 self.knownBranches[branch] = branch
3290
bb6e09b2
HWN
3291 def updateOptionDict(self, d):
3292 option_keys = {}
3293 if self.keepRepoPath:
3294 option_keys['keepRepoPath'] = 1
3295
3296 d["options"] = ' '.join(sorted(option_keys.keys()))
3297
3298 def readOptions(self, d):
dba1c9d9 3299 self.keepRepoPath = ('options' in d
bb6e09b2 3300 and ('keepRepoPath' in d['options']))
6326aa58 3301
8134f69c
SH
3302 def gitRefForBranch(self, branch):
3303 if branch == "main":
3304 return self.refPrefix + "master"
3305
3306 if len(branch) <= 0:
3307 return branch
3308
3309 return self.refPrefix + self.projectName + branch
3310
1ca3d710
SH
3311 def gitCommitByP4Change(self, ref, change):
3312 if self.verbose:
f2606b17 3313 print("looking in ref " + ref + " for change %s using bisect..." % change)
1ca3d710
SH
3314
3315 earliestCommit = ""
3316 latestCommit = parseRevision(ref)
3317
3318 while True:
3319 if self.verbose:
f2606b17 3320 print("trying: earliest %s latest %s" % (earliestCommit, latestCommit))
1ca3d710
SH
3321 next = read_pipe("git rev-list --bisect %s %s" % (latestCommit, earliestCommit)).strip()
3322 if len(next) == 0:
3323 if self.verbose:
f2606b17 3324 print("argh")
1ca3d710
SH
3325 return ""
3326 log = extractLogMessageFromGitCommit(next)
3327 settings = extractSettingsGitLog(log)
3328 currentChange = int(settings['change'])
3329 if self.verbose:
f2606b17 3330 print("current change %s" % currentChange)
1ca3d710
SH
3331
3332 if currentChange == change:
3333 if self.verbose:
f2606b17 3334 print("found %s" % next)
1ca3d710
SH
3335 return next
3336
3337 if currentChange < change:
3338 earliestCommit = "^%s" % next
3339 else:
3340 latestCommit = "%s" % next
3341
3342 return ""
3343
3344 def importNewBranch(self, branch, maxChange):
3345 # make fast-import flush all changes to disk and update the refs using the checkpoint
3346 # command so that we can try to find the branch parent in the git history
3347 self.gitStream.write("checkpoint\n\n");
3348 self.gitStream.flush();
3349 branchPrefix = self.depotPaths[0] + branch + "/"
3350 range = "@1,%s" % maxChange
3351 #print "prefix" + branchPrefix
96b2d54a 3352 changes = p4ChangesForPaths([branchPrefix], range, self.changes_block_size)
1ca3d710
SH
3353 if len(changes) <= 0:
3354 return False
3355 firstChange = changes[0]
3356 #print "first change in branch: %s" % firstChange
3357 sourceBranch = self.knownBranches[branch]
3358 sourceDepotPath = self.depotPaths[0] + sourceBranch
3359 sourceRef = self.gitRefForBranch(sourceBranch)
3360 #print "source " + sourceBranch
3361
52a4880b 3362 branchParentChange = int(p4Cmd(["changes", "-m", "1", "%s...@1,%s" % (sourceDepotPath, firstChange)])["change"])
1ca3d710
SH
3363 #print "branch parent: %s" % branchParentChange
3364 gitParent = self.gitCommitByP4Change(sourceRef, branchParentChange)
3365 if len(gitParent) > 0:
3366 self.initialParents[self.gitRefForBranch(branch)] = gitParent
3367 #print "parent git commit: %s" % gitParent
3368
3369 self.importChanges(changes)
3370 return True
3371
fed23693
VA
3372 def searchParent(self, parent, branch, target):
3373 parentFound = False
c7d34884
PW
3374 for blob in read_pipe_lines(["git", "rev-list", "--reverse",
3375 "--no-merges", parent]):
fed23693
VA
3376 blob = blob.strip()
3377 if len(read_pipe(["git", "diff-tree", blob, target])) == 0:
3378 parentFound = True
3379 if self.verbose:
f2606b17 3380 print("Found parent of %s in commit %s" % (branch, blob))
fed23693
VA
3381 break
3382 if parentFound:
3383 return blob
3384 else:
3385 return None
3386
123f6317 3387 def importChanges(self, changes, shelved=False, origin_revision=0):
e87f37ae
SH
3388 cnt = 1
3389 for change in changes:
123f6317 3390 description = p4_describe(change, shelved)
e87f37ae
SH
3391 self.updateOptionDict(description)
3392
3393 if not self.silent:
3394 sys.stdout.write("\rImporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
3395 sys.stdout.flush()
3396 cnt = cnt + 1
3397
3398 try:
3399 if self.detectBranches:
3400 branches = self.splitFilesIntoBranches(description)
3401 for branch in branches.keys():
3402 ## HACK --hwn
3403 branchPrefix = self.depotPaths[0] + branch + "/"
e63231e5 3404 self.branchPrefixes = [ branchPrefix ]
e87f37ae
SH
3405
3406 parent = ""
3407
3408 filesForCommit = branches[branch]
3409
3410 if self.verbose:
f2606b17 3411 print("branch is %s" % branch)
e87f37ae
SH
3412
3413 self.updatedBranches.add(branch)
3414
3415 if branch not in self.createdBranches:
3416 self.createdBranches.add(branch)
3417 parent = self.knownBranches[branch]
3418 if parent == branch:
3419 parent = ""
1ca3d710
SH
3420 else:
3421 fullBranch = self.projectName + branch
3422 if fullBranch not in self.p4BranchesInGit:
3423 if not self.silent:
3424 print("\n Importing new branch %s" % fullBranch);
3425 if self.importNewBranch(branch, change - 1):
3426 parent = ""
3427 self.p4BranchesInGit.append(fullBranch)
3428 if not self.silent:
3429 print("\n Resuming with change %s" % change);
3430
3431 if self.verbose:
f2606b17 3432 print("parent determined through known branches: %s" % parent)
e87f37ae 3433
8134f69c
SH
3434 branch = self.gitRefForBranch(branch)
3435 parent = self.gitRefForBranch(parent)
e87f37ae
SH
3436
3437 if self.verbose:
f2606b17 3438 print("looking for initial parent for %s; current parent is %s" % (branch, parent))
e87f37ae
SH
3439
3440 if len(parent) == 0 and branch in self.initialParents:
3441 parent = self.initialParents[branch]
3442 del self.initialParents[branch]
3443
fed23693
VA
3444 blob = None
3445 if len(parent) > 0:
4f9273d2 3446 tempBranch = "%s/%d" % (self.tempBranchLocation, change)
fed23693 3447 if self.verbose:
f2606b17 3448 print("Creating temporary branch: " + tempBranch)
e63231e5 3449 self.commit(description, filesForCommit, tempBranch)
fed23693
VA
3450 self.tempBranches.append(tempBranch)
3451 self.checkpoint()
3452 blob = self.searchParent(parent, branch, tempBranch)
3453 if blob:
e63231e5 3454 self.commit(description, filesForCommit, branch, blob)
fed23693
VA
3455 else:
3456 if self.verbose:
f2606b17 3457 print("Parent of %s not found. Committing into head of %s" % (branch, parent))
e63231e5 3458 self.commit(description, filesForCommit, branch, parent)
e87f37ae 3459 else:
123f6317 3460 files = self.extractFilesFromCommit(description, shelved, change, origin_revision)
e63231e5 3461 self.commit(description, files, self.branch,
e87f37ae 3462 self.initialParent)
47497844 3463 # only needed once, to connect to the previous commit
e87f37ae
SH
3464 self.initialParent = ""
3465 except IOError:
f2606b17 3466 print(self.gitError.read())
e87f37ae
SH
3467 sys.exit(1)
3468
b9d34db9
LD
3469 def sync_origin_only(self):
3470 if self.syncWithOrigin:
3471 self.hasOrigin = originP4BranchesExist()
3472 if self.hasOrigin:
3473 if not self.silent:
f2606b17 3474 print('Syncing with origin first, using "git fetch origin"')
b9d34db9
LD
3475 system("git fetch origin")
3476
c208a243 3477 def importHeadRevision(self, revision):
f2606b17 3478 print("Doing initial import of %s from revision %s into %s" % (' '.join(self.depotPaths), revision, self.branch))
c208a243 3479
4e2e6ce4
PW
3480 details = {}
3481 details["user"] = "git perforce import user"
1494fcbb 3482 details["desc"] = ("Initial import of %s from the state at revision %s\n"
c208a243
SH
3483 % (' '.join(self.depotPaths), revision))
3484 details["change"] = revision
3485 newestRevision = 0
3486
3487 fileCnt = 0
6de040df
LD
3488 fileArgs = ["%s...%s" % (p,revision) for p in self.depotPaths]
3489
3490 for info in p4CmdList(["files"] + fileArgs):
c208a243 3491
68b28593 3492 if 'code' in info and info['code'] == 'error':
c208a243
SH
3493 sys.stderr.write("p4 returned an error: %s\n"
3494 % info['data'])
d88e707f
PW
3495 if info['data'].find("must refer to client") >= 0:
3496 sys.stderr.write("This particular p4 error is misleading.\n")
3497 sys.stderr.write("Perhaps the depot path was misspelled.\n");
3498 sys.stderr.write("Depot path: %s\n" % " ".join(self.depotPaths))
c208a243 3499 sys.exit(1)
68b28593
PW
3500 if 'p4ExitCode' in info:
3501 sys.stderr.write("p4 exitcode: %s\n" % info['p4ExitCode'])
c208a243
SH
3502 sys.exit(1)
3503
3504
3505 change = int(info["change"])
3506 if change > newestRevision:
3507 newestRevision = change
3508
56c09345 3509 if info["action"] in self.delete_actions:
c208a243
SH
3510 # don't increase the file cnt, otherwise details["depotFile123"] will have gaps!
3511 #fileCnt = fileCnt + 1
3512 continue
3513
3514 for prop in ["depotFile", "rev", "action", "type" ]:
3515 details["%s%s" % (prop, fileCnt)] = info[prop]
3516
3517 fileCnt = fileCnt + 1
3518
3519 details["change"] = newestRevision
4e2e6ce4 3520
9dcb9f24 3521 # Use time from top-most change so that all git p4 clones of
4e2e6ce4 3522 # the same p4 repo have the same commit SHA1s.
18fa13d0
PW
3523 res = p4_describe(newestRevision)
3524 details["time"] = res["time"]
4e2e6ce4 3525
c208a243
SH
3526 self.updateOptionDict(details)
3527 try:
e63231e5 3528 self.commit(details, self.extractFilesFromCommit(details), self.branch)
c208a243 3529 except IOError:
f2606b17
LD
3530 print("IO error with git fast-import. Is your git version recent enough?")
3531 print(self.gitError.read())
c208a243 3532
123f6317
LD
3533 def openStreams(self):
3534 self.importProcess = subprocess.Popen(["git", "fast-import"],
3535 stdin=subprocess.PIPE,
3536 stdout=subprocess.PIPE,
3537 stderr=subprocess.PIPE);
3538 self.gitOutput = self.importProcess.stdout
3539 self.gitStream = self.importProcess.stdin
3540 self.gitError = self.importProcess.stderr
c208a243 3541
123f6317
LD
3542 def closeStreams(self):
3543 self.gitStream.close()
3544 if self.importProcess.wait() != 0:
3545 die("fast-import failed: %s" % self.gitError.read())
3546 self.gitOutput.close()
3547 self.gitError.close()
29bdbac1 3548
123f6317 3549 def run(self, args):
a028a98e
SH
3550 if self.importIntoRemotes:
3551 self.refPrefix = "refs/remotes/p4/"
3552 else:
db775559 3553 self.refPrefix = "refs/heads/p4/"
a028a98e 3554
b9d34db9 3555 self.sync_origin_only()
10f880f8 3556
5a8e84cd 3557 branch_arg_given = bool(self.branch)
569d1bd4 3558 if len(self.branch) == 0:
db775559 3559 self.branch = self.refPrefix + "master"
a028a98e 3560 if gitBranchExists("refs/heads/p4") and self.importIntoRemotes:
48df6fd8 3561 system("git update-ref %s refs/heads/p4" % self.branch)
55d12437 3562 system("git branch -D p4")
967f72e2 3563
a93d33ee
PW
3564 # accept either the command-line option, or the configuration variable
3565 if self.useClientSpec:
3566 # will use this after clone to set the variable
3567 self.useClientSpec_from_options = True
3568 else:
0d609032 3569 if gitConfigBool("git-p4.useclientspec"):
09fca77b
PW
3570 self.useClientSpec = True
3571 if self.useClientSpec:
543987bd 3572 self.clientSpecDirs = getClientSpec()
3a70cdfa 3573
6a49f8e2
HWN
3574 # TODO: should always look at previous commits,
3575 # merge with previous imports, if possible.
3576 if args == []:
d414c74a 3577 if self.hasOrigin:
5ca44617 3578 createOrUpdateBranchesFromOrigin(self.refPrefix, self.silent)
3b650fc9
PW
3579
3580 # branches holds mapping from branch name to sha1
3581 branches = p4BranchesInGit(self.importIntoRemotes)
8c9e8b6e
PW
3582
3583 # restrict to just this one, disabling detect-branches
3584 if branch_arg_given:
3585 short = self.branch.split("/")[-1]
3586 if short in branches:
3587 self.p4BranchesInGit = [ short ]
3588 else:
3589 self.p4BranchesInGit = branches.keys()
abcd790f
SH
3590
3591 if len(self.p4BranchesInGit) > 1:
3592 if not self.silent:
f2606b17 3593 print("Importing from/into multiple branches")
abcd790f 3594 self.detectBranches = True
8c9e8b6e
PW
3595 for branch in branches.keys():
3596 self.initialParents[self.refPrefix + branch] = \
3597 branches[branch]
967f72e2 3598
29bdbac1 3599 if self.verbose:
f2606b17 3600 print("branches: %s" % self.p4BranchesInGit)
29bdbac1
SH
3601
3602 p4Change = 0
3603 for branch in self.p4BranchesInGit:
cebdf5af 3604 logMsg = extractLogMessageFromGitCommit(self.refPrefix + branch)
bb6e09b2
HWN
3605
3606 settings = extractSettingsGitLog(logMsg)
29bdbac1 3607
bb6e09b2 3608 self.readOptions(settings)
dba1c9d9
LD
3609 if ('depot-paths' in settings
3610 and 'change' in settings):
bb6e09b2 3611 change = int(settings['change']) + 1
29bdbac1
SH
3612 p4Change = max(p4Change, change)
3613
bb6e09b2
HWN
3614 depotPaths = sorted(settings['depot-paths'])
3615 if self.previousDepotPaths == []:
6326aa58 3616 self.previousDepotPaths = depotPaths
29bdbac1 3617 else:
6326aa58
HWN
3618 paths = []
3619 for (prev, cur) in zip(self.previousDepotPaths, depotPaths):
04d277b3
VA
3620 prev_list = prev.split("/")
3621 cur_list = cur.split("/")
3622 for i in range(0, min(len(cur_list), len(prev_list))):
fc35c9d5 3623 if cur_list[i] != prev_list[i]:
583e1707 3624 i = i - 1
6326aa58
HWN
3625 break
3626
04d277b3 3627 paths.append ("/".join(cur_list[:i + 1]))
6326aa58
HWN
3628
3629 self.previousDepotPaths = paths
29bdbac1
SH
3630
3631 if p4Change > 0:
bb6e09b2 3632 self.depotPaths = sorted(self.previousDepotPaths)
d5904674 3633 self.changeRange = "@%s,#head" % p4Change
341dc1c1 3634 if not self.silent and not self.detectBranches:
f2606b17 3635 print("Performing incremental import into %s git branch" % self.branch)
569d1bd4 3636
40d69ac3
PW
3637 # accept multiple ref name abbreviations:
3638 # refs/foo/bar/branch -> use it exactly
3639 # p4/branch -> prepend refs/remotes/ or refs/heads/
3640 # branch -> prepend refs/remotes/p4/ or refs/heads/p4/
f9162f6a 3641 if not self.branch.startswith("refs/"):
40d69ac3
PW
3642 if self.importIntoRemotes:
3643 prepend = "refs/remotes/"
3644 else:
3645 prepend = "refs/heads/"
3646 if not self.branch.startswith("p4/"):
3647 prepend += "p4/"
3648 self.branch = prepend + self.branch
179caebf 3649
6326aa58 3650 if len(args) == 0 and self.depotPaths:
b984733c 3651 if not self.silent:
f2606b17 3652 print("Depot paths: %s" % ' '.join(self.depotPaths))
b984733c 3653 else:
6326aa58 3654 if self.depotPaths and self.depotPaths != args:
f2606b17 3655 print("previous import used depot path %s and now %s was specified. "
6326aa58
HWN
3656 "This doesn't work!" % (' '.join (self.depotPaths),
3657 ' '.join (args)))
b984733c 3658 sys.exit(1)
6326aa58 3659
bb6e09b2 3660 self.depotPaths = sorted(args)
b984733c 3661
1c49fc19 3662 revision = ""
b984733c 3663 self.users = {}
b984733c 3664
58c8bc7c
PW
3665 # Make sure no revision specifiers are used when --changesfile
3666 # is specified.
3667 bad_changesfile = False
3668 if len(self.changesFile) > 0:
3669 for p in self.depotPaths:
3670 if p.find("@") >= 0 or p.find("#") >= 0:
3671 bad_changesfile = True
3672 break
3673 if bad_changesfile:
3674 die("Option --changesfile is incompatible with revision specifiers")
3675
6326aa58
HWN
3676 newPaths = []
3677 for p in self.depotPaths:
3678 if p.find("@") != -1:
3679 atIdx = p.index("@")
3680 self.changeRange = p[atIdx:]
3681 if self.changeRange == "@all":
3682 self.changeRange = ""
6a49f8e2 3683 elif ',' not in self.changeRange:
1c49fc19 3684 revision = self.changeRange
6326aa58 3685 self.changeRange = ""
7fcff9de 3686 p = p[:atIdx]
6326aa58
HWN
3687 elif p.find("#") != -1:
3688 hashIdx = p.index("#")
1c49fc19 3689 revision = p[hashIdx:]
7fcff9de 3690 p = p[:hashIdx]
6326aa58 3691 elif self.previousDepotPaths == []:
58c8bc7c
PW
3692 # pay attention to changesfile, if given, else import
3693 # the entire p4 tree at the head revision
3694 if len(self.changesFile) == 0:
3695 revision = "#head"
6326aa58
HWN
3696
3697 p = re.sub ("\.\.\.$", "", p)
3698 if not p.endswith("/"):
3699 p += "/"
3700
3701 newPaths.append(p)
3702
3703 self.depotPaths = newPaths
3704
e63231e5
PW
3705 # --detect-branches may change this for each branch
3706 self.branchPrefixes = self.depotPaths
3707
b607e71e 3708 self.loadUserMapFromCache()
cb53e1f8
SH
3709 self.labels = {}
3710 if self.detectLabels:
3711 self.getLabels();
b984733c 3712
4b97ffb1 3713 if self.detectBranches:
df450923
SH
3714 ## FIXME - what's a P4 projectName ?
3715 self.projectName = self.guessProjectName()
3716
38f9f5ec
SH
3717 if self.hasOrigin:
3718 self.getBranchMappingFromGitBranches()
3719 else:
3720 self.getBranchMapping()
29bdbac1 3721 if self.verbose:
f2606b17
LD
3722 print("p4-git branches: %s" % self.p4BranchesInGit)
3723 print("initial parents: %s" % self.initialParents)
29bdbac1
SH
3724 for b in self.p4BranchesInGit:
3725 if b != "master":
6326aa58
HWN
3726
3727 ## FIXME
29bdbac1
SH
3728 b = b[len(self.projectName):]
3729 self.createdBranches.add(b)
4b97ffb1 3730
123f6317 3731 self.openStreams()
b984733c 3732
1c49fc19 3733 if revision:
c208a243 3734 self.importHeadRevision(revision)
b984733c
SH
3735 else:
3736 changes = []
3737
0828ab14 3738 if len(self.changesFile) > 0:
b984733c 3739 output = open(self.changesFile).readlines()
1d7367dc 3740 changeSet = set()
b984733c
SH
3741 for line in output:
3742 changeSet.add(int(line))
3743
3744 for change in changeSet:
3745 changes.append(change)
3746
3747 changes.sort()
3748 else:
9dcb9f24
PW
3749 # catch "git p4 sync" with no new branches, in a repo that
3750 # does not have any existing p4 branches
5a8e84cd
PW
3751 if len(args) == 0:
3752 if not self.p4BranchesInGit:
3753 die("No remote p4 branches. Perhaps you never did \"git p4 clone\" in here.")
3754
3755 # The default branch is master, unless --branch is used to
3756 # specify something else. Make sure it exists, or complain
3757 # nicely about how to use --branch.
3758 if not self.detectBranches:
3759 if not branch_exists(self.branch):
3760 if branch_arg_given:
3761 die("Error: branch %s does not exist." % self.branch)
3762 else:
3763 die("Error: no branch %s; perhaps specify one with --branch." %
3764 self.branch)
3765
29bdbac1 3766 if self.verbose:
f2606b17
LD
3767 print("Getting p4 changes for %s...%s" % (', '.join(self.depotPaths),
3768 self.changeRange))
96b2d54a 3769 changes = p4ChangesForPaths(self.depotPaths, self.changeRange, self.changes_block_size)
b984733c 3770
01a9c9c5 3771 if len(self.maxChanges) > 0:
7fcff9de 3772 changes = changes[:min(int(self.maxChanges), len(changes))]
01a9c9c5 3773
b984733c 3774 if len(changes) == 0:
0828ab14 3775 if not self.silent:
f2606b17 3776 print("No changes to import!")
06804c76
LD
3777 else:
3778 if not self.silent and not self.detectBranches:
f2606b17 3779 print("Import destination: %s" % self.branch)
06804c76
LD
3780
3781 self.updatedBranches = set()
b984733c 3782
47497844
PW
3783 if not self.detectBranches:
3784 if args:
3785 # start a new branch
3786 self.initialParent = ""
3787 else:
3788 # build on a previous revision
3789 self.initialParent = parseRevision(self.branch)
3790
06804c76 3791 self.importChanges(changes)
a9d1a27a 3792
06804c76 3793 if not self.silent:
f2606b17 3794 print("")
06804c76
LD
3795 if len(self.updatedBranches) > 0:
3796 sys.stdout.write("Updated branches: ")
3797 for b in self.updatedBranches:
3798 sys.stdout.write("%s " % b)
3799 sys.stdout.write("\n")
341dc1c1 3800
0d609032 3801 if gitConfigBool("git-p4.importLabels"):
06dcd152 3802 self.importLabels = True
b984733c 3803
06804c76
LD
3804 if self.importLabels:
3805 p4Labels = getP4Labels(self.depotPaths)
3806 gitTags = getGitTags()
3807
3808 missingP4Labels = p4Labels - gitTags
3809 self.importP4Labels(self.gitStream, missingP4Labels)
b984733c 3810
123f6317 3811 self.closeStreams()
b984733c 3812
fed23693
VA
3813 # Cleanup temporary branches created during import
3814 if self.tempBranches != []:
3815 for branch in self.tempBranches:
3816 read_pipe("git update-ref -d %s" % branch)
3817 os.rmdir(os.path.join(os.environ.get("GIT_DIR", ".git"), self.tempBranchLocation))
3818
55d12437
PW
3819 # Create a symbolic ref p4/HEAD pointing to p4/<branch> to allow
3820 # a convenient shortcut refname "p4".
3821 if self.importIntoRemotes:
3822 head_ref = self.refPrefix + "HEAD"
3823 if not gitBranchExists(head_ref) and gitBranchExists(self.branch):
3824 system(["git", "symbolic-ref", head_ref, self.branch])
3825
b984733c
SH
3826 return True
3827
01ce1fe9
SH
3828class P4Rebase(Command):
3829 def __init__(self):
3830 Command.__init__(self)
06804c76
LD
3831 self.options = [
3832 optparse.make_option("--import-labels", dest="importLabels", action="store_true"),
06804c76 3833 ]
06804c76 3834 self.importLabels = False
cebdf5af
HWN
3835 self.description = ("Fetches the latest revision from perforce and "
3836 + "rebases the current work (branch) against it")
01ce1fe9
SH
3837
3838 def run(self, args):
3839 sync = P4Sync()
06804c76 3840 sync.importLabels = self.importLabels
01ce1fe9 3841 sync.run([])
d7e3868c 3842
14594f4b
SH
3843 return self.rebase()
3844
3845 def rebase(self):
36ee4ee4 3846 if os.system("git update-index --refresh") != 0:
7560f547 3847 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.");
36ee4ee4 3848 if len(read_pipe("git diff-index HEAD --")) > 0:
f7e604ed 3849 die("You have uncommitted changes. Please commit them before rebasing or stash them away with git stash.");
36ee4ee4 3850
d7e3868c
SH
3851 [upstream, settings] = findUpstreamBranchPoint()
3852 if len(upstream) == 0:
3853 die("Cannot find upstream branchpoint for rebase")
3854
3855 # the branchpoint may be p4/foo~3, so strip off the parent
3856 upstream = re.sub("~[0-9]+$", "", upstream)
3857
f2606b17 3858 print("Rebasing the current branch onto %s" % upstream)
b25b2065 3859 oldHead = read_pipe("git rev-parse HEAD").strip()
d7e3868c 3860 system("git rebase %s" % upstream)
4e49d95e 3861 system("git diff-tree --stat --summary -M %s HEAD --" % oldHead)
01ce1fe9
SH
3862 return True
3863
f9a3a4f7
SH
3864class P4Clone(P4Sync):
3865 def __init__(self):
3866 P4Sync.__init__(self)
3867 self.description = "Creates a new git repository and imports from Perforce into it"
bb6e09b2 3868 self.usage = "usage: %prog [options] //depot/path[@revRange]"
354081d5 3869 self.options += [
bb6e09b2
HWN
3870 optparse.make_option("--destination", dest="cloneDestination",
3871 action='store', default=None,
354081d5 3872 help="where to leave result of the clone"),
38200076
PW
3873 optparse.make_option("--bare", dest="cloneBare",
3874 action="store_true", default=False),
354081d5 3875 ]
bb6e09b2 3876 self.cloneDestination = None
f9a3a4f7 3877 self.needsGit = False
38200076 3878 self.cloneBare = False
f9a3a4f7 3879
6a49f8e2
HWN
3880 def defaultDestination(self, args):
3881 ## TODO: use common prefix of args?
3882 depotPath = args[0]
3883 depotDir = re.sub("(@[^@]*)$", "", depotPath)
3884 depotDir = re.sub("(#[^#]*)$", "", depotDir)
053d9e43 3885 depotDir = re.sub(r"\.\.\.$", "", depotDir)
6a49f8e2
HWN
3886 depotDir = re.sub(r"/$", "", depotDir)
3887 return os.path.split(depotDir)[1]
3888
f9a3a4f7
SH
3889 def run(self, args):
3890 if len(args) < 1:
3891 return False
bb6e09b2
HWN
3892
3893 if self.keepRepoPath and not self.cloneDestination:
3894 sys.stderr.write("Must specify destination for --keep-path\n")
3895 sys.exit(1)
f9a3a4f7 3896
6326aa58 3897 depotPaths = args
5e100b5c
SH
3898
3899 if not self.cloneDestination and len(depotPaths) > 1:
3900 self.cloneDestination = depotPaths[-1]
3901 depotPaths = depotPaths[:-1]
3902
354081d5 3903 self.cloneExclude = ["/"+p for p in self.cloneExclude]
6326aa58
HWN
3904 for p in depotPaths:
3905 if not p.startswith("//"):
0f487d30 3906 sys.stderr.write('Depot paths must start with "//": %s\n' % p)
6326aa58 3907 return False
f9a3a4f7 3908
bb6e09b2 3909 if not self.cloneDestination:
98ad4faf 3910 self.cloneDestination = self.defaultDestination(args)
f9a3a4f7 3911
f2606b17 3912 print("Importing from %s into %s" % (', '.join(depotPaths), self.cloneDestination))
38200076 3913
c3bf3f13
KG
3914 if not os.path.exists(self.cloneDestination):
3915 os.makedirs(self.cloneDestination)
053fd0c1 3916 chdir(self.cloneDestination)
38200076
PW
3917
3918 init_cmd = [ "git", "init" ]
3919 if self.cloneBare:
3920 init_cmd.append("--bare")
a235e85c
BC
3921 retcode = subprocess.call(init_cmd)
3922 if retcode:
3923 raise CalledProcessError(retcode, init_cmd)
38200076 3924
6326aa58 3925 if not P4Sync.run(self, depotPaths):
f9a3a4f7 3926 return False
c595956d
PW
3927
3928 # create a master branch and check out a work tree
3929 if gitBranchExists(self.branch):
3930 system([ "git", "branch", "master", self.branch ])
3931 if not self.cloneBare:
3932 system([ "git", "checkout", "-f" ])
3933 else:
f2606b17
LD
3934 print('Not checking out any branch, use ' \
3935 '"git checkout -q -b master <branch>"')
86dff6b6 3936
a93d33ee
PW
3937 # auto-set this variable if invoked with --use-client-spec
3938 if self.useClientSpec_from_options:
3939 system("git config --bool git-p4.useclientspec true")
3940
f9a3a4f7
SH
3941 return True
3942
123f6317
LD
3943class P4Unshelve(Command):
3944 def __init__(self):
3945 Command.__init__(self)
3946 self.options = []
3947 self.origin = "HEAD"
3948 self.description = "Unshelve a P4 changelist into a git commit"
3949 self.usage = "usage: %prog [options] changelist"
3950 self.options += [
3951 optparse.make_option("--origin", dest="origin",
3952 help="Use this base revision instead of the default (%s)" % self.origin),
3953 ]
3954 self.verbose = False
3955 self.noCommit = False
3956 self.destbranch = "refs/remotes/p4/unshelved"
3957
3958 def renameBranch(self, branch_name):
3959 """ Rename the existing branch to branch_name.N
3960 """
3961
3962 found = True
3963 for i in range(0,1000):
3964 backup_branch_name = "{0}.{1}".format(branch_name, i)
3965 if not gitBranchExists(backup_branch_name):
3966 gitUpdateRef(backup_branch_name, branch_name) # copy ref to backup
3967 gitDeleteRef(branch_name)
3968 found = True
3969 print("renamed old unshelve branch to {0}".format(backup_branch_name))
3970 break
3971
3972 if not found:
3973 sys.exit("gave up trying to rename existing branch {0}".format(sync.branch))
3974
3975 def findLastP4Revision(self, starting_point):
3976 """ Look back from starting_point for the first commit created by git-p4
3977 to find the P4 commit we are based on, and the depot-paths.
3978 """
3979
3980 for parent in (range(65535)):
3981 log = extractLogMessageFromGitCommit("{0}^{1}".format(starting_point, parent))
3982 settings = extractSettingsGitLog(log)
dba1c9d9 3983 if 'change' in settings:
123f6317
LD
3984 return settings
3985
3986 sys.exit("could not find git-p4 commits in {0}".format(self.origin))
3987
3988 def run(self, args):
3989 if len(args) != 1:
3990 return False
3991
3992 if not gitBranchExists(self.origin):
3993 sys.exit("origin branch {0} does not exist".format(self.origin))
3994
3995 sync = P4Sync()
3996 changes = args
3997 sync.initialParent = self.origin
3998
3999 # use the first change in the list to construct the branch to unshelve into
4000 change = changes[0]
4001
4002 # if the target branch already exists, rename it
4003 branch_name = "{0}/{1}".format(self.destbranch, change)
4004 if gitBranchExists(branch_name):
4005 self.renameBranch(branch_name)
4006 sync.branch = branch_name
4007
4008 sync.verbose = self.verbose
4009 sync.suppress_meta_comment = True
4010
4011 settings = self.findLastP4Revision(self.origin)
4012 origin_revision = settings['change']
4013 sync.depotPaths = settings['depot-paths']
4014 sync.branchPrefixes = sync.depotPaths
4015
4016 sync.openStreams()
4017 sync.loadUserMapFromCache()
4018 sync.silent = True
4019 sync.importChanges(changes, shelved=True, origin_revision=origin_revision)
4020 sync.closeStreams()
4021
4022 print("unshelved changelist {0} into {1}".format(change, branch_name))
4023
4024 return True
4025
09d89de2
SH
4026class P4Branches(Command):
4027 def __init__(self):
4028 Command.__init__(self)
4029 self.options = [ ]
4030 self.description = ("Shows the git branches that hold imports and their "
4031 + "corresponding perforce depot paths")
4032 self.verbose = False
4033
4034 def run(self, args):
5ca44617
SH
4035 if originP4BranchesExist():
4036 createOrUpdateBranchesFromOrigin()
4037
09d89de2
SH
4038 cmdline = "git rev-parse --symbolic "
4039 cmdline += " --remotes"
4040
4041 for line in read_pipe_lines(cmdline):
4042 line = line.strip()
4043
4044 if not line.startswith('p4/') or line == "p4/HEAD":
4045 continue
4046 branch = line
4047
4048 log = extractLogMessageFromGitCommit("refs/remotes/%s" % branch)
4049 settings = extractSettingsGitLog(log)
4050
f2606b17 4051 print("%s <= %s (%s)" % (branch, ",".join(settings["depot-paths"]), settings["change"]))
09d89de2
SH
4052 return True
4053
b984733c
SH
4054class HelpFormatter(optparse.IndentedHelpFormatter):
4055 def __init__(self):
4056 optparse.IndentedHelpFormatter.__init__(self)
4057
4058 def format_description(self, description):
4059 if description:
4060 return description + "\n"
4061 else:
4062 return ""
4f5cf76a 4063
86949eef 4064def printUsage(commands):
f2606b17
LD
4065 print("usage: %s <command> [options]" % sys.argv[0])
4066 print("")
4067 print("valid commands: %s" % ", ".join(commands))
4068 print("")
4069 print("Try %s <command> --help for command specific help." % sys.argv[0])
4070 print("")
86949eef
SH
4071
4072commands = {
b86f7378
HWN
4073 "debug" : P4Debug,
4074 "submit" : P4Submit,
a9834f58 4075 "commit" : P4Submit,
b86f7378
HWN
4076 "sync" : P4Sync,
4077 "rebase" : P4Rebase,
4078 "clone" : P4Clone,
09d89de2 4079 "rollback" : P4RollBack,
123f6317
LD
4080 "branches" : P4Branches,
4081 "unshelve" : P4Unshelve,
86949eef
SH
4082}
4083
86949eef 4084
bb6e09b2
HWN
4085def main():
4086 if len(sys.argv[1:]) == 0:
4087 printUsage(commands.keys())
4088 sys.exit(2)
4f5cf76a 4089
bb6e09b2
HWN
4090 cmdName = sys.argv[1]
4091 try:
b86f7378
HWN
4092 klass = commands[cmdName]
4093 cmd = klass()
bb6e09b2 4094 except KeyError:
f2606b17
LD
4095 print("unknown command %s" % cmdName)
4096 print("")
bb6e09b2
HWN
4097 printUsage(commands.keys())
4098 sys.exit(2)
4099
4100 options = cmd.options
b86f7378 4101 cmd.gitdir = os.environ.get("GIT_DIR", None)
bb6e09b2
HWN
4102
4103 args = sys.argv[2:]
4104
b0ccc80d 4105 options.append(optparse.make_option("--verbose", "-v", dest="verbose", action="store_true"))
6a10b6aa
LD
4106 if cmd.needsGit:
4107 options.append(optparse.make_option("--git-dir", dest="gitdir"))
bb6e09b2 4108
6a10b6aa
LD
4109 parser = optparse.OptionParser(cmd.usage.replace("%prog", "%prog " + cmdName),
4110 options,
4111 description = cmd.description,
4112 formatter = HelpFormatter())
bb6e09b2 4113
6a10b6aa 4114 (cmd, args) = parser.parse_args(sys.argv[2:], cmd);
bb6e09b2
HWN
4115 global verbose
4116 verbose = cmd.verbose
4117 if cmd.needsGit:
b86f7378
HWN
4118 if cmd.gitdir == None:
4119 cmd.gitdir = os.path.abspath(".git")
4120 if not isValidGitDir(cmd.gitdir):
378f7be1 4121 # "rev-parse --git-dir" without arguments will try $PWD/.git
b86f7378
HWN
4122 cmd.gitdir = read_pipe("git rev-parse --git-dir").strip()
4123 if os.path.exists(cmd.gitdir):
bb6e09b2
HWN
4124 cdup = read_pipe("git rev-parse --show-cdup").strip()
4125 if len(cdup) > 0:
053fd0c1 4126 chdir(cdup);
e20a9e53 4127
b86f7378
HWN
4128 if not isValidGitDir(cmd.gitdir):
4129 if isValidGitDir(cmd.gitdir + "/.git"):
4130 cmd.gitdir += "/.git"
bb6e09b2 4131 else:
b86f7378 4132 die("fatal: cannot locate git repository at %s" % cmd.gitdir)
e20a9e53 4133
378f7be1 4134 # so git commands invoked from the P4 workspace will succeed
b86f7378 4135 os.environ["GIT_DIR"] = cmd.gitdir
86949eef 4136
bb6e09b2
HWN
4137 if not cmd.run(args):
4138 parser.print_help()
09fca77b 4139 sys.exit(2)
4f5cf76a 4140
4f5cf76a 4141
bb6e09b2
HWN
4142if __name__ == '__main__':
4143 main()