blob: 4433ca53de7eff82c29a191019f1b6477f53f3bb [file] [log] [blame]
Simon Hausmann86949ee2007-03-19 20:59:12 +01001#!/usr/bin/env python
2#
3# git-p4.py -- A tool for bidirectional operation between a Perforce depot and git.
4#
Simon Hausmannc8cbbee2007-05-28 14:43:25 +02005# Author: Simon Hausmann <simon@lst.de>
6# Copyright: 2007 Simon Hausmann <simon@lst.de>
Simon Hausmann83dce552007-03-19 22:26:36 +01007# 2007 Trolltech ASA
Simon Hausmann86949ee2007-03-19 20:59:12 +01008# License: MIT <http://www.opensource.org/licenses/mit-license.php>
9#
Luke Diamand4c1d5862020-01-29 11:12:43 +000010# pylint: disable=invalid-name,missing-docstring,too-many-arguments,broad-except
11# pylint: disable=no-self-use,wrong-import-position,consider-iterating-dictionary
12# pylint: disable=wrong-import-order,unused-import,too-few-public-methods
13# pylint: disable=too-many-lines,ungrouped-imports,fixme,too-many-locals
14# pylint: disable=line-too-long,bad-whitespace,superfluous-parens
15# pylint: disable=too-many-statements,too-many-instance-attributes
16# pylint: disable=too-many-branches,too-many-nested-blocks
17#
Eric S. Raymonda33faf22012-12-28 11:40:59 -050018import sys
Yang Zhao0b4396f2019-12-13 15:52:35 -080019if sys.version_info.major < 3 and sys.version_info.minor < 7:
20 sys.stderr.write("git-p4: requires Python 2.7 or later.\n")
Eric S. Raymonda33faf22012-12-28 11:40:59 -050021 sys.exit(1)
Pete Wyckofff629fa52013-01-26 22:11:05 -050022import os
23import optparse
Yang Zhaoa6b13062019-12-13 15:52:44 -080024import functools
Pete Wyckofff629fa52013-01-26 22:11:05 -050025import marshal
26import subprocess
27import tempfile
28import time
29import platform
30import re
31import shutil
Pete Wyckoffd20f0f82013-01-26 22:11:19 -050032import stat
Lars Schneidera5db4b12015-09-26 09:55:03 +020033import zipfile
34import zlib
Dennis Kaarsemaker4b07cd22015-10-20 21:31:46 +020035import ctypes
Luke Diamanddf8a9e82016-12-17 01:00:40 +000036import errno
Ben Keene9f59ca42020-02-11 18:57:59 +000037import glob
Han-Wen Nienhuys8b41a972007-05-23 18:20:53 -030038
Yang Zhao7575f4f2019-12-13 15:52:47 -080039# On python2.7 where raw_input() and input() are both availble,
40# we want raw_input's semantics, but aliased to input for python3
41# compatibility
Luke Diamandefdcc992018-06-19 09:04:09 +010042# support basestring in python3
43try:
Yang Zhao7575f4f2019-12-13 15:52:47 -080044 if raw_input and input:
45 input = raw_input
46except:
47 pass
Brandon Caseya235e852013-01-26 11:14:33 -080048
Han-Wen Nienhuys4addad22007-05-23 18:49:35 -030049verbose = False
Simon Hausmann86949ee2007-03-19 20:59:12 +010050
Luke Diamand06804c72012-04-11 17:21:24 +020051# Only labels/tags matching this will be imported/exported
Luke Diamandc8942a22012-04-11 17:21:24 +020052defaultLabelRegexp = r'[a-zA-Z0-9_\-.]+$'
Anand Kumria21a50752008-08-10 19:26:28 +010053
Luke Diamand3deed5e2018-06-08 21:32:48 +010054# The block size is reduced automatically if required
55defaultBlockSize = 1<<20
Luke Diamand1051ef02015-06-10 08:30:59 +010056
Luke Diamand0ef67ac2018-06-08 21:32:45 +010057p4_access_checked = False
Anand Kumria21a50752008-08-10 19:26:28 +010058
59def p4_build_cmd(cmd):
60 """Build a suitable p4 command line.
61
62 This consolidates building and returning a p4 command line into one
63 location. It means that hooking into the environment, or other configuration
64 can be done more easily.
65 """
Luke Diamand6de040d2011-10-16 10:47:52 -040066 real_cmd = ["p4"]
Anand Kumriaabcaf072008-08-10 19:26:31 +010067
68 user = gitConfig("git-p4.user")
69 if len(user) > 0:
Luke Diamand6de040d2011-10-16 10:47:52 -040070 real_cmd += ["-u",user]
Anand Kumriaabcaf072008-08-10 19:26:31 +010071
72 password = gitConfig("git-p4.password")
73 if len(password) > 0:
Luke Diamand6de040d2011-10-16 10:47:52 -040074 real_cmd += ["-P", password]
Anand Kumriaabcaf072008-08-10 19:26:31 +010075
76 port = gitConfig("git-p4.port")
77 if len(port) > 0:
Luke Diamand6de040d2011-10-16 10:47:52 -040078 real_cmd += ["-p", port]
Anand Kumriaabcaf072008-08-10 19:26:31 +010079
80 host = gitConfig("git-p4.host")
81 if len(host) > 0:
Russell Myers41799aa2012-02-22 11:16:05 -080082 real_cmd += ["-H", host]
Anand Kumriaabcaf072008-08-10 19:26:31 +010083
84 client = gitConfig("git-p4.client")
85 if len(client) > 0:
Luke Diamand6de040d2011-10-16 10:47:52 -040086 real_cmd += ["-c", client]
Anand Kumriaabcaf072008-08-10 19:26:31 +010087
Lars Schneider89a6ecc2016-12-04 15:03:11 +010088 retries = gitConfigInt("git-p4.retries")
89 if retries is None:
90 # Perform 3 retries by default
91 retries = 3
Igor Kushnirbc233522016-12-29 12:22:23 +020092 if retries > 0:
93 # Provide a way to not pass this option by setting git-p4.retries to 0
94 real_cmd += ["-r", str(retries)]
Luke Diamand6de040d2011-10-16 10:47:52 -040095
Ben Keene484d09c2019-12-13 15:52:36 -080096 if not isinstance(cmd, list):
Luke Diamand6de040d2011-10-16 10:47:52 -040097 real_cmd = ' '.join(real_cmd) + ' ' + cmd
98 else:
99 real_cmd += cmd
Luke Diamand0ef67ac2018-06-08 21:32:45 +0100100
101 # now check that we can actually talk to the server
102 global p4_access_checked
103 if not p4_access_checked:
104 p4_access_checked = True # suppress access checks in p4_check_access itself
105 p4_check_access()
106
Anand Kumria21a50752008-08-10 19:26:28 +0100107 return real_cmd
108
Luke Diamand378f7be2016-12-13 21:51:28 +0000109def git_dir(path):
110 """ Return TRUE if the given path is a git directory (/path/to/dir/.git).
111 This won't automatically add ".git" to a directory.
112 """
113 d = read_pipe(["git", "--git-dir", path, "rev-parse", "--git-dir"], True).strip()
114 if not d or len(d) == 0:
115 return None
116 else:
117 return d
118
Miklós Fazekasbbd84862013-03-11 17:45:29 -0400119def chdir(path, is_client_path=False):
120 """Do chdir to the given path, and set the PWD environment
121 variable for use by P4. It does not look at getcwd() output.
122 Since we're not using the shell, it is necessary to set the
123 PWD environment variable explicitly.
124
125 Normally, expand the path to force it to be absolute. This
126 addresses the use of relative path names inside P4 settings,
127 e.g. P4CONFIG=.p4config. P4 does not simply open the filename
128 as given; it looks for .p4config using PWD.
129
130 If is_client_path, the path was handed to us directly by p4,
131 and may be a symbolic link. Do not call os.getcwd() in this
132 case, because it will cause p4 to think that PWD is not inside
133 the client path.
134 """
135
136 os.chdir(path)
137 if not is_client_path:
138 path = os.getcwd()
139 os.environ['PWD'] = path
Robert Blum053fd0c2008-08-01 12:50:03 -0700140
Lars Schneider4d25dc42015-09-26 09:55:02 +0200141def calcDiskFree():
142 """Return free space in bytes on the disk of the given dirname."""
143 if platform.system() == 'Windows':
144 free_bytes = ctypes.c_ulonglong(0)
145 ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(os.getcwd()), None, None, ctypes.pointer(free_bytes))
146 return free_bytes.value
147 else:
148 st = os.statvfs(os.getcwd())
149 return st.f_bavail * st.f_frsize
150
Han-Wen Nienhuys86dff6b2007-05-23 18:49:35 -0300151def die(msg):
Luke Diamand6026aff2020-01-29 11:12:45 +0000152 """ Terminate execution. Make sure that any running child processes have been wait()ed for before
153 calling this.
154 """
Han-Wen Nienhuys86dff6b2007-05-23 18:49:35 -0300155 if verbose:
156 raise Exception(msg)
157 else:
158 sys.stderr.write(msg + "\n")
159 sys.exit(1)
160
Ben Keenee2aed5f2019-12-16 14:02:19 +0000161def prompt(prompt_text):
162 """ Prompt the user to choose one of the choices
163
164 Choices are identified in the prompt_text by square brackets around
165 a single letter option.
166 """
167 choices = set(m.group(1) for m in re.finditer(r"\[(.)\]", prompt_text))
168 while True:
Ben Keene6b602a22020-02-11 18:57:58 +0000169 sys.stderr.flush()
170 sys.stdout.write(prompt_text)
171 sys.stdout.flush()
172 response=sys.stdin.readline().strip().lower()
Ben Keenee2aed5f2019-12-16 14:02:19 +0000173 if not response:
174 continue
175 response = response[0]
176 if response in choices:
177 return response
178
Yang Zhao6cec21a2019-12-13 15:52:38 -0800179# We need different encoding/decoding strategies for text data being passed
180# around in pipes depending on python version
181if bytes is not str:
182 # For python3, always encode and decode as appropriate
183 def decode_text_stream(s):
184 return s.decode() if isinstance(s, bytes) else s
185 def encode_text_stream(s):
186 return s.encode() if isinstance(s, str) else s
187else:
188 # For python2.7, pass read strings as-is, but also allow writing unicode
189 def decode_text_stream(s):
190 return s
191 def encode_text_stream(s):
192 return s.encode('utf_8') if isinstance(s, unicode) else s
193
Yang Zhaod38208a2019-12-13 15:52:40 -0800194def decode_path(path):
195 """Decode a given string (bytes or otherwise) using configured path encoding options
196 """
197 encoding = gitConfig('git-p4.pathEncoding') or 'utf_8'
198 if bytes is not str:
199 return path.decode(encoding, errors='replace') if isinstance(path, bytes) else path
200 else:
201 try:
202 path.decode('ascii')
203 except:
204 path = path.decode(encoding, errors='replace')
205 if verbose:
206 print('Path with non-ASCII characters detected. Used {} to decode: {}'.format(encoding, path))
207 return path
208
Ben Keene9f59ca42020-02-11 18:57:59 +0000209def run_git_hook(cmd, param=[]):
210 """Execute a hook if the hook exists."""
211 if verbose:
212 sys.stderr.write("Looking for hook: %s\n" % cmd)
213 sys.stderr.flush()
214
215 hooks_path = gitConfig("core.hooksPath")
216 if len(hooks_path) <= 0:
217 hooks_path = os.path.join(os.environ["GIT_DIR"], "hooks")
218
219 if not isinstance(param, list):
220 param=[param]
221
222 # resolve hook file name, OS depdenent
223 hook_file = os.path.join(hooks_path, cmd)
224 if platform.system() == 'Windows':
225 if not os.path.isfile(hook_file):
226 # look for the file with an extension
227 files = glob.glob(hook_file + ".*")
228 if not files:
229 return True
230 files.sort()
231 hook_file = files.pop()
232 while hook_file.upper().endswith(".SAMPLE"):
233 # The file is a sample hook. We don't want it
234 if len(files) > 0:
235 hook_file = files.pop()
236 else:
237 return True
238
239 if not os.path.isfile(hook_file) or not os.access(hook_file, os.X_OK):
240 return True
241
242 return run_hook_command(hook_file, param) == 0
243
244def run_hook_command(cmd, param):
245 """Executes a git hook command
246 cmd = the command line file to be executed. This can be
247 a file that is run by OS association.
248
249 param = a list of parameters to pass to the cmd command
250
251 On windows, the extension is checked to see if it should
252 be run with the Git for Windows Bash shell. If there
253 is no file extension, the file is deemed a bash shell
254 and will be handed off to sh.exe. Otherwise, Windows
255 will be called with the shell to handle the file assocation.
256
257 For non Windows operating systems, the file is called
258 as an executable.
259 """
260 cli = [cmd] + param
261 use_shell = False
262 if platform.system() == 'Windows':
263 (root,ext) = os.path.splitext(cmd)
264 if ext == "":
265 exe_path = os.environ.get("EXEPATH")
266 if exe_path is None:
267 exe_path = ""
268 else:
269 exe_path = os.path.join(exe_path, "bin")
270 cli = [os.path.join(exe_path, "SH.EXE")] + cli
271 else:
272 use_shell = True
273 return subprocess.call(cli, shell=use_shell)
274
275
Luke Diamand6de040d2011-10-16 10:47:52 -0400276def write_pipe(c, stdin):
Han-Wen Nienhuys4addad22007-05-23 18:49:35 -0300277 if verbose:
Luke Diamand6de040d2011-10-16 10:47:52 -0400278 sys.stderr.write('Writing pipe: %s\n' % str(c))
Han-Wen Nienhuysb016d392007-05-23 17:10:46 -0300279
Ben Keene484d09c2019-12-13 15:52:36 -0800280 expand = not isinstance(c, list)
Luke Diamand6de040d2011-10-16 10:47:52 -0400281 p = subprocess.Popen(c, stdin=subprocess.PIPE, shell=expand)
282 pipe = p.stdin
283 val = pipe.write(stdin)
284 pipe.close()
285 if p.wait():
286 die('Command failed: %s' % str(c))
Han-Wen Nienhuysb016d392007-05-23 17:10:46 -0300287
288 return val
289
Luke Diamand6de040d2011-10-16 10:47:52 -0400290def p4_write_pipe(c, stdin):
Anand Kumriad9429192008-08-14 23:40:38 +0100291 real_cmd = p4_build_cmd(c)
Yang Zhao6cec21a2019-12-13 15:52:38 -0800292 if bytes is not str and isinstance(stdin, str):
293 stdin = encode_text_stream(stdin)
Luke Diamand6de040d2011-10-16 10:47:52 -0400294 return write_pipe(real_cmd, stdin)
Anand Kumriad9429192008-08-14 23:40:38 +0100295
Luke Diamand78871bf2017-04-15 11:36:08 +0100296def read_pipe_full(c):
297 """ Read output from command. Returns a tuple
298 of the return status, stdout text and stderr
299 text.
300 """
Han-Wen Nienhuys4addad22007-05-23 18:49:35 -0300301 if verbose:
Luke Diamand6de040d2011-10-16 10:47:52 -0400302 sys.stderr.write('Reading pipe: %s\n' % str(c))
Han-Wen Nienhuys8b41a972007-05-23 18:20:53 -0300303
Ben Keene484d09c2019-12-13 15:52:36 -0800304 expand = not isinstance(c, list)
Lars Schneider1f5f3902015-09-21 12:01:41 +0200305 p = subprocess.Popen(c, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=expand)
306 (out, err) = p.communicate()
Yang Zhao6cec21a2019-12-13 15:52:38 -0800307 return (p.returncode, out, decode_text_stream(err))
Luke Diamand78871bf2017-04-15 11:36:08 +0100308
Yang Zhao86dca242019-12-13 15:52:39 -0800309def read_pipe(c, ignore_error=False, raw=False):
Luke Diamand78871bf2017-04-15 11:36:08 +0100310 """ Read output from command. Returns the output text on
311 success. On failure, terminates execution, unless
312 ignore_error is True, when it returns an empty string.
Yang Zhao86dca242019-12-13 15:52:39 -0800313
314 If raw is True, do not attempt to decode output text.
Luke Diamand78871bf2017-04-15 11:36:08 +0100315 """
316 (retcode, out, err) = read_pipe_full(c)
317 if retcode != 0:
318 if ignore_error:
319 out = ""
320 else:
321 die('Command failed: %s\nError: %s' % (str(c), err))
Yang Zhao86dca242019-12-13 15:52:39 -0800322 if not raw:
323 out = decode_text_stream(out)
Lars Schneider1f5f3902015-09-21 12:01:41 +0200324 return out
Han-Wen Nienhuysb016d392007-05-23 17:10:46 -0300325
Luke Diamand78871bf2017-04-15 11:36:08 +0100326def read_pipe_text(c):
327 """ Read output from a command with trailing whitespace stripped.
328 On error, returns None.
329 """
330 (retcode, out, err) = read_pipe_full(c)
331 if retcode != 0:
332 return None
333 else:
Yang Zhao6cec21a2019-12-13 15:52:38 -0800334 return decode_text_stream(out).rstrip()
Luke Diamand78871bf2017-04-15 11:36:08 +0100335
Yang Zhao6cec21a2019-12-13 15:52:38 -0800336def p4_read_pipe(c, ignore_error=False, raw=False):
Anand Kumriad9429192008-08-14 23:40:38 +0100337 real_cmd = p4_build_cmd(c)
Yang Zhao6cec21a2019-12-13 15:52:38 -0800338 return read_pipe(real_cmd, ignore_error, raw=raw)
Han-Wen Nienhuysb016d392007-05-23 17:10:46 -0300339
Han-Wen Nienhuysbce4c5f2007-05-23 17:14:33 -0300340def read_pipe_lines(c):
Han-Wen Nienhuys4addad22007-05-23 18:49:35 -0300341 if verbose:
Luke Diamand6de040d2011-10-16 10:47:52 -0400342 sys.stderr.write('Reading pipe: %s\n' % str(c))
343
Ben Keene484d09c2019-12-13 15:52:36 -0800344 expand = not isinstance(c, list)
Luke Diamand6de040d2011-10-16 10:47:52 -0400345 p = subprocess.Popen(c, stdout=subprocess.PIPE, shell=expand)
346 pipe = p.stdout
Yang Zhao6cec21a2019-12-13 15:52:38 -0800347 val = [decode_text_stream(line) for line in pipe.readlines()]
Luke Diamand6de040d2011-10-16 10:47:52 -0400348 if pipe.close() or p.wait():
349 die('Command failed: %s' % str(c))
Han-Wen Nienhuysb016d392007-05-23 17:10:46 -0300350 return val
Simon Hausmanncaace112007-05-15 14:57:57 +0200351
Anand Kumria23181212008-08-10 19:26:24 +0100352def p4_read_pipe_lines(c):
353 """Specifically invoke p4 on the command supplied. """
Anand Kumria155af832008-08-10 19:26:30 +0100354 real_cmd = p4_build_cmd(c)
Anand Kumria23181212008-08-10 19:26:24 +0100355 return read_pipe_lines(real_cmd)
356
Gary Gibbons8e9497c2012-07-12 19:29:00 -0400357def p4_has_command(cmd):
358 """Ask p4 for help on this command. If it returns an error, the
359 command does not exist in this version of p4."""
360 real_cmd = p4_build_cmd(["help", cmd])
361 p = subprocess.Popen(real_cmd, stdout=subprocess.PIPE,
362 stderr=subprocess.PIPE)
363 p.communicate()
364 return p.returncode == 0
365
Pete Wyckoff249da4c2012-11-23 17:35:35 -0500366def p4_has_move_command():
367 """See if the move command exists, that it supports -k, and that
368 it has not been administratively disabled. The arguments
369 must be correct, but the filenames do not have to exist. Use
370 ones with wildcards so even if they exist, it will fail."""
371
372 if not p4_has_command("move"):
373 return False
374 cmd = p4_build_cmd(["move", "-k", "@from", "@to"])
375 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
376 (out, err) = p.communicate()
Yang Zhao6cec21a2019-12-13 15:52:38 -0800377 err = decode_text_stream(err)
Pete Wyckoff249da4c2012-11-23 17:35:35 -0500378 # return code will be 1 in either case
379 if err.find("Invalid option") >= 0:
380 return False
381 if err.find("disabled") >= 0:
382 return False
383 # assume it failed because @... was invalid changelist
384 return True
385
Luke Diamandcbff4b22015-11-21 09:54:40 +0000386def system(cmd, ignore_error=False):
Ben Keene484d09c2019-12-13 15:52:36 -0800387 expand = not isinstance(cmd, list)
Han-Wen Nienhuys4addad22007-05-23 18:49:35 -0300388 if verbose:
Luke Diamand6de040d2011-10-16 10:47:52 -0400389 sys.stderr.write("executing %s\n" % str(cmd))
Brandon Caseya235e852013-01-26 11:14:33 -0800390 retcode = subprocess.call(cmd, shell=expand)
Luke Diamandcbff4b22015-11-21 09:54:40 +0000391 if retcode and not ignore_error:
Brandon Caseya235e852013-01-26 11:14:33 -0800392 raise CalledProcessError(retcode, cmd)
Han-Wen Nienhuys6754a292007-05-23 17:41:50 -0300393
Luke Diamandcbff4b22015-11-21 09:54:40 +0000394 return retcode
395
Anand Kumriabf9320f2008-08-10 19:26:26 +0100396def p4_system(cmd):
397 """Specifically invoke p4 as the system command. """
Anand Kumria155af832008-08-10 19:26:30 +0100398 real_cmd = p4_build_cmd(cmd)
Ben Keene484d09c2019-12-13 15:52:36 -0800399 expand = not isinstance(real_cmd, list)
Brandon Caseya235e852013-01-26 11:14:33 -0800400 retcode = subprocess.call(real_cmd, shell=expand)
401 if retcode:
402 raise CalledProcessError(retcode, real_cmd)
Luke Diamand6de040d2011-10-16 10:47:52 -0400403
Luke Diamand0ef67ac2018-06-08 21:32:45 +0100404def die_bad_access(s):
405 die("failure accessing depot: {0}".format(s.rstrip()))
406
407def p4_check_access(min_expiration=1):
408 """ Check if we can access Perforce - account still logged in
409 """
410 results = p4CmdList(["login", "-s"])
411
412 if len(results) == 0:
413 # should never get here: always get either some results, or a p4ExitCode
414 assert("could not parse response from perforce")
415
416 result = results[0]
417
418 if 'p4ExitCode' in result:
419 # p4 returned non-zero status, e.g. P4PORT invalid, or p4 not in path
420 die_bad_access("could not run p4")
421
422 code = result.get("code")
423 if not code:
424 # we get here if we couldn't connect and there was nothing to unmarshal
425 die_bad_access("could not connect")
426
427 elif code == "stat":
428 expiry = result.get("TicketExpiration")
429 if expiry:
430 expiry = int(expiry)
431 if expiry > min_expiration:
432 # ok to carry on
433 return
434 else:
435 die_bad_access("perforce ticket expires in {0} seconds".format(expiry))
436
437 else:
438 # account without a timeout - all ok
439 return
440
441 elif code == "error":
442 data = result.get("data")
443 if data:
444 die_bad_access("p4 error: {0}".format(data))
445 else:
446 die_bad_access("unknown error")
Peter Osterlundd4990d52019-01-07 21:51:38 +0100447 elif code == "info":
448 return
Luke Diamand0ef67ac2018-06-08 21:32:45 +0100449 else:
450 die_bad_access("unknown error code {0}".format(code))
451
Pete Wyckoff7f0e5962013-01-26 22:11:13 -0500452_p4_version_string = None
453def p4_version_string():
454 """Read the version string, showing just the last line, which
455 hopefully is the interesting version bit.
456
457 $ p4 -V
458 Perforce - The Fast Software Configuration Management System.
459 Copyright 1995-2011 Perforce Software. All rights reserved.
460 Rev. P4/NTX86/2011.1/393975 (2011/12/16).
461 """
462 global _p4_version_string
463 if not _p4_version_string:
464 a = p4_read_pipe_lines(["-V"])
465 _p4_version_string = a[-1].rstrip()
466 return _p4_version_string
467
Luke Diamand6de040d2011-10-16 10:47:52 -0400468def p4_integrate(src, dest):
Pete Wyckoff9d7d4462012-04-29 20:57:17 -0400469 p4_system(["integrate", "-Dt", wildcard_encode(src), wildcard_encode(dest)])
Luke Diamand6de040d2011-10-16 10:47:52 -0400470
Pete Wyckoff8d7ec362012-04-29 20:57:14 -0400471def p4_sync(f, *options):
Pete Wyckoff9d7d4462012-04-29 20:57:17 -0400472 p4_system(["sync"] + list(options) + [wildcard_encode(f)])
Luke Diamand6de040d2011-10-16 10:47:52 -0400473
474def p4_add(f):
Pete Wyckoff9d7d4462012-04-29 20:57:17 -0400475 # forcibly add file names with wildcards
476 if wildcard_present(f):
477 p4_system(["add", "-f", f])
478 else:
479 p4_system(["add", f])
Luke Diamand6de040d2011-10-16 10:47:52 -0400480
481def p4_delete(f):
Pete Wyckoff9d7d4462012-04-29 20:57:17 -0400482 p4_system(["delete", wildcard_encode(f)])
Luke Diamand6de040d2011-10-16 10:47:52 -0400483
Romain Picarda02b8bc2016-01-12 13:43:47 +0100484def p4_edit(f, *options):
485 p4_system(["edit"] + list(options) + [wildcard_encode(f)])
Luke Diamand6de040d2011-10-16 10:47:52 -0400486
487def p4_revert(f):
Pete Wyckoff9d7d4462012-04-29 20:57:17 -0400488 p4_system(["revert", wildcard_encode(f)])
Luke Diamand6de040d2011-10-16 10:47:52 -0400489
Pete Wyckoff9d7d4462012-04-29 20:57:17 -0400490def p4_reopen(type, f):
491 p4_system(["reopen", "-t", type, wildcard_encode(f)])
Anand Kumriabf9320f2008-08-10 19:26:26 +0100492
Luke Diamand46c609e2016-12-02 22:43:19 +0000493def p4_reopen_in_change(changelist, files):
494 cmd = ["reopen", "-c", str(changelist)] + files
495 p4_system(cmd)
496
Gary Gibbons8e9497c2012-07-12 19:29:00 -0400497def p4_move(src, dest):
498 p4_system(["move", "-k", wildcard_encode(src), wildcard_encode(dest)])
499
Luke Diamand1051ef02015-06-10 08:30:59 +0100500def p4_last_change():
Miguel Torroja1997e912017-07-13 09:00:35 +0200501 results = p4CmdList(["changes", "-m", "1"], skip_info=True)
Luke Diamand1051ef02015-06-10 08:30:59 +0100502 return int(results[0]['change'])
503
Luke Diamand123f6312018-05-23 23:20:26 +0100504def p4_describe(change, shelved=False):
Pete Wyckoff18fa13d2012-11-23 17:35:34 -0500505 """Make sure it returns a valid result by checking for
506 the presence of field "time". Return a dict of the
507 results."""
508
Luke Diamand123f6312018-05-23 23:20:26 +0100509 cmd = ["describe", "-s"]
510 if shelved:
511 cmd += ["-S"]
512 cmd += [str(change)]
513
514 ds = p4CmdList(cmd, skip_info=True)
Pete Wyckoff18fa13d2012-11-23 17:35:34 -0500515 if len(ds) != 1:
516 die("p4 describe -s %d did not return 1 result: %s" % (change, str(ds)))
517
518 d = ds[0]
519
520 if "p4ExitCode" in d:
521 die("p4 describe -s %d exited with %d: %s" % (change, d["p4ExitCode"],
522 str(d)))
523 if "code" in d:
524 if d["code"] == "error":
525 die("p4 describe -s %d returned error code: %s" % (change, str(d)))
526
527 if "time" not in d:
528 die("p4 describe -s %d returned no \"time\": %s" % (change, str(d)))
529
530 return d
531
Pete Wyckoff9cffb8c2011-10-16 10:45:01 -0400532#
533# Canonicalize the p4 type and return a tuple of the
534# base type, plus any modifiers. See "p4 help filetypes"
535# for a list and explanation.
536#
537def split_p4_type(p4type):
David Brownb9fc6ea2007-09-19 13:12:48 -0700538
Pete Wyckoff9cffb8c2011-10-16 10:45:01 -0400539 p4_filetypes_historical = {
540 "ctempobj": "binary+Sw",
541 "ctext": "text+C",
542 "cxtext": "text+Cx",
543 "ktext": "text+k",
544 "kxtext": "text+kx",
545 "ltext": "text+F",
546 "tempobj": "binary+FSw",
547 "ubinary": "binary+F",
548 "uresource": "resource+F",
549 "uxbinary": "binary+Fx",
550 "xbinary": "binary+x",
551 "xltext": "text+Fx",
552 "xtempobj": "binary+Swx",
553 "xtext": "text+x",
554 "xunicode": "unicode+x",
555 "xutf16": "utf16+x",
556 }
557 if p4type in p4_filetypes_historical:
558 p4type = p4_filetypes_historical[p4type]
559 mods = ""
560 s = p4type.split("+")
561 base = s[0]
562 mods = ""
563 if len(s) > 1:
564 mods = s[1]
565 return (base, mods)
566
Luke Diamand60df0712012-02-23 07:51:30 +0000567#
568# return the raw p4 type of a file (text, text+ko, etc)
569#
Pete Wyckoff79467e62014-01-21 18:16:45 -0500570def p4_type(f):
571 results = p4CmdList(["fstat", "-T", "headType", wildcard_encode(f)])
Luke Diamand60df0712012-02-23 07:51:30 +0000572 return results[0]['headType']
573
574#
575# Given a type base and modifier, return a regexp matching
576# the keywords that can be expanded in the file
577#
578def p4_keywords_regexp_for_type(base, type_mods):
579 if base in ("text", "unicode", "binary"):
580 kwords = None
581 if "ko" in type_mods:
582 kwords = 'Id|Header'
583 elif "k" in type_mods:
584 kwords = 'Id|Header|Author|Date|DateTime|Change|File|Revision'
585 else:
586 return None
587 pattern = r"""
588 \$ # Starts with a dollar, followed by...
589 (%s) # one of the keywords, followed by...
Pete Wyckoff6b2bf412012-11-04 17:04:02 -0500590 (:[^$\n]+)? # possibly an old expansion, followed by...
Luke Diamand60df0712012-02-23 07:51:30 +0000591 \$ # another dollar
592 """ % kwords
593 return pattern
594 else:
595 return None
596
597#
598# Given a file, return a regexp matching the possible
599# RCS keywords that will be expanded, or None for files
600# with kw expansion turned off.
601#
602def p4_keywords_regexp_for_file(file):
603 if not os.path.exists(file):
604 return None
605 else:
606 (type_base, type_mods) = split_p4_type(p4_type(file))
607 return p4_keywords_regexp_for_type(type_base, type_mods)
David Brownb9fc6ea2007-09-19 13:12:48 -0700608
Chris Pettittc65b6702007-11-01 20:43:14 -0700609def setP4ExecBit(file, mode):
610 # Reopens an already open file and changes the execute bit to match
611 # the execute bit setting in the passed in mode.
612
613 p4Type = "+x"
614
615 if not isModeExec(mode):
616 p4Type = getP4OpenedType(file)
617 p4Type = re.sub('^([cku]?)x(.*)', '\\1\\2', p4Type)
618 p4Type = re.sub('(.*?\+.*?)x(.*?)', '\\1\\2', p4Type)
619 if p4Type[-1] == "+":
620 p4Type = p4Type[0:-1]
621
Luke Diamand6de040d2011-10-16 10:47:52 -0400622 p4_reopen(p4Type, file)
Chris Pettittc65b6702007-11-01 20:43:14 -0700623
624def getP4OpenedType(file):
625 # Returns the perforce file type for the given file.
626
Pete Wyckoff9d7d4462012-04-29 20:57:17 -0400627 result = p4_read_pipe(["opened", wildcard_encode(file)])
Blair Holloway34a0dbf2015-04-04 09:46:03 +0100628 match = re.match(".*\((.+)\)( \*exclusive\*)?\r?$", result)
Chris Pettittc65b6702007-11-01 20:43:14 -0700629 if match:
630 return match.group(1)
631 else:
Marius Storm-Olsenf3e5ae42008-03-28 15:40:40 +0100632 die("Could not determine file type for %s (result: '%s')" % (file, result))
Chris Pettittc65b6702007-11-01 20:43:14 -0700633
Luke Diamand06804c72012-04-11 17:21:24 +0200634# Return the set of all p4 labels
635def getP4Labels(depotPaths):
636 labels = set()
Ben Keene484d09c2019-12-13 15:52:36 -0800637 if not isinstance(depotPaths, list):
Luke Diamand06804c72012-04-11 17:21:24 +0200638 depotPaths = [depotPaths]
639
640 for l in p4CmdList(["labels"] + ["%s..." % p for p in depotPaths]):
641 label = l['label']
642 labels.add(label)
643
644 return labels
645
646# Return the set of all git tags
647def getGitTags():
648 gitTags = set()
649 for line in read_pipe_lines(["git", "tag"]):
650 tag = line.strip()
651 gitTags.add(tag)
652 return gitTags
653
Yang Zhaoce425eb2019-12-13 15:52:46 -0800654_diff_tree_pattern = None
Chris Pettittb43b0a32007-11-01 20:43:13 -0700655
656def parseDiffTreeEntry(entry):
657 """Parses a single diff tree entry into its component elements.
658
659 See git-diff-tree(1) manpage for details about the format of the diff
660 output. This method returns a dictionary with the following elements:
661
662 src_mode - The mode of the source file
663 dst_mode - The mode of the destination file
664 src_sha1 - The sha1 for the source file
665 dst_sha1 - The sha1 fr the destination file
666 status - The one letter status of the diff (i.e. 'A', 'M', 'D', etc)
667 status_score - The score for the status (applicable for 'C' and 'R'
668 statuses). This is None if there is no score.
669 src - The path for the source file.
670 dst - The path for the destination file. This is only present for
671 copy or renames. If it is not present, this is None.
672
673 If the pattern is not matched, None is returned."""
674
Yang Zhaoce425eb2019-12-13 15:52:46 -0800675 global _diff_tree_pattern
676 if not _diff_tree_pattern:
677 _diff_tree_pattern = re.compile(':(\d+) (\d+) (\w+) (\w+) ([A-Z])(\d+)?\t(.*?)((\t(.*))|$)')
678
679 match = _diff_tree_pattern.match(entry)
Chris Pettittb43b0a32007-11-01 20:43:13 -0700680 if match:
681 return {
682 'src_mode': match.group(1),
683 'dst_mode': match.group(2),
684 'src_sha1': match.group(3),
685 'dst_sha1': match.group(4),
686 'status': match.group(5),
687 'status_score': match.group(6),
688 'src': match.group(7),
689 'dst': match.group(10)
690 }
691 return None
692
Chris Pettittc65b6702007-11-01 20:43:14 -0700693def isModeExec(mode):
694 # Returns True if the given git mode represents an executable file,
695 # otherwise False.
696 return mode[-3:] == "755"
697
Luke Diamand55bb3e32018-06-08 21:32:46 +0100698class P4Exception(Exception):
699 """ Base class for exceptions from the p4 client """
700 def __init__(self, exit_code):
701 self.p4ExitCode = exit_code
702
703class P4ServerException(P4Exception):
704 """ Base class for exceptions where we get some kind of marshalled up result from the server """
705 def __init__(self, exit_code, p4_result):
706 super(P4ServerException, self).__init__(exit_code)
707 self.p4_result = p4_result
708 self.code = p4_result[0]['code']
709 self.data = p4_result[0]['data']
710
711class P4RequestSizeException(P4ServerException):
712 """ One of the maxresults or maxscanrows errors """
713 def __init__(self, exit_code, p4_result, limit):
714 super(P4RequestSizeException, self).__init__(exit_code, p4_result)
715 self.limit = limit
716
Luke Diamand5c3d5022020-01-29 11:12:42 +0000717class P4CommandException(P4Exception):
718 """ Something went wrong calling p4 which means we have to give up """
719 def __init__(self, msg):
720 self.msg = msg
721
722 def __str__(self):
723 return self.msg
724
Chris Pettittc65b6702007-11-01 20:43:14 -0700725def isModeExecChanged(src_mode, dst_mode):
726 return isModeExec(src_mode) != isModeExec(dst_mode)
727
Luke Diamand55bb3e32018-06-08 21:32:46 +0100728def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None, skip_info=False,
729 errors_as_exceptions=False):
Luke Diamand6de040d2011-10-16 10:47:52 -0400730
Ben Keene484d09c2019-12-13 15:52:36 -0800731 if not isinstance(cmd, list):
Luke Diamand6de040d2011-10-16 10:47:52 -0400732 cmd = "-G " + cmd
733 expand = True
734 else:
735 cmd = ["-G"] + cmd
736 expand = False
737
738 cmd = p4_build_cmd(cmd)
Han-Wen Nienhuys6a49f8e2007-05-23 18:49:35 -0300739 if verbose:
Luke Diamand6de040d2011-10-16 10:47:52 -0400740 sys.stderr.write("Opening pipe: %s\n" % str(cmd))
Scott Lamb9f90c732007-07-15 20:58:10 -0700741
742 # Use a temporary file to avoid deadlocks without
743 # subprocess.communicate(), which would put another copy
744 # of stdout into memory.
745 stdin_file = None
746 if stdin is not None:
747 stdin_file = tempfile.TemporaryFile(prefix='p4-stdin', mode=stdin_mode)
Ben Keene484d09c2019-12-13 15:52:36 -0800748 if not isinstance(stdin, list):
Luke Diamand6de040d2011-10-16 10:47:52 -0400749 stdin_file.write(stdin)
750 else:
751 for i in stdin:
Yang Zhao86dca242019-12-13 15:52:39 -0800752 stdin_file.write(encode_text_stream(i))
753 stdin_file.write(b'\n')
Scott Lamb9f90c732007-07-15 20:58:10 -0700754 stdin_file.flush()
755 stdin_file.seek(0)
756
Luke Diamand6de040d2011-10-16 10:47:52 -0400757 p4 = subprocess.Popen(cmd,
758 shell=expand,
Scott Lamb9f90c732007-07-15 20:58:10 -0700759 stdin=stdin_file,
760 stdout=subprocess.PIPE)
Simon Hausmann86949ee2007-03-19 20:59:12 +0100761
762 result = []
763 try:
764 while True:
Scott Lamb9f90c732007-07-15 20:58:10 -0700765 entry = marshal.load(p4.stdout)
Yang Zhao6cec21a2019-12-13 15:52:38 -0800766 if bytes is not str:
767 # Decode unmarshalled dict to use str keys and values, except for:
768 # - `data` which may contain arbitrary binary data
769 # - `depotFile[0-9]*`, `path`, or `clientFile` which may contain non-UTF8 encoded text
770 decoded_entry = {}
771 for key, value in entry.items():
772 key = key.decode()
773 if isinstance(value, bytes) and not (key in ('data', 'path', 'clientFile') or key.startswith('depotFile')):
774 value = value.decode()
775 decoded_entry[key] = value
776 # Parse out data if it's an error response
777 if decoded_entry.get('code') == 'error' and 'data' in decoded_entry:
778 decoded_entry['data'] = decoded_entry['data'].decode()
779 entry = decoded_entry
Miguel Torroja1997e912017-07-13 09:00:35 +0200780 if skip_info:
781 if 'code' in entry and entry['code'] == 'info':
782 continue
Andrew Garberc3f61632011-04-07 02:01:21 -0400783 if cb is not None:
784 cb(entry)
785 else:
786 result.append(entry)
Simon Hausmann86949ee2007-03-19 20:59:12 +0100787 except EOFError:
788 pass
Scott Lamb9f90c732007-07-15 20:58:10 -0700789 exitCode = p4.wait()
790 if exitCode != 0:
Luke Diamand55bb3e32018-06-08 21:32:46 +0100791 if errors_as_exceptions:
792 if len(result) > 0:
793 data = result[0].get('data')
794 if data:
795 m = re.search('Too many rows scanned \(over (\d+)\)', data)
796 if not m:
797 m = re.search('Request too large \(over (\d+)\)', data)
798
799 if m:
800 limit = int(m.group(1))
801 raise P4RequestSizeException(exitCode, result, limit)
802
803 raise P4ServerException(exitCode, result)
804 else:
805 raise P4Exception(exitCode)
806 else:
807 entry = {}
808 entry["p4ExitCode"] = exitCode
809 result.append(entry)
Simon Hausmann86949ee2007-03-19 20:59:12 +0100810
811 return result
812
813def p4Cmd(cmd):
814 list = p4CmdList(cmd)
815 result = {}
816 for entry in list:
817 result.update(entry)
818 return result;
819
Simon Hausmanncb2c9db2007-03-24 09:15:11 +0100820def p4Where(depotPath):
821 if not depotPath.endswith("/"):
822 depotPath += "/"
Vitor Antunescd884102015-04-21 23:49:30 +0100823 depotPathLong = depotPath + "..."
824 outputList = p4CmdList(["where", depotPathLong])
Tor Arvid Lund7f705dc2008-12-04 14:37:33 +0100825 output = None
826 for entry in outputList:
Tor Arvid Lund75bc9572008-12-09 16:41:50 +0100827 if "depotFile" in entry:
Vitor Antunescd884102015-04-21 23:49:30 +0100828 # Search for the base client side depot path, as long as it starts with the branch's P4 path.
829 # The base path always ends with "/...".
Yang Zhaod38208a2019-12-13 15:52:40 -0800830 entry_path = decode_path(entry['depotFile'])
831 if entry_path.find(depotPath) == 0 and entry_path[-4:] == "/...":
Tor Arvid Lund75bc9572008-12-09 16:41:50 +0100832 output = entry
833 break
834 elif "data" in entry:
835 data = entry.get("data")
836 space = data.find(" ")
837 if data[:space] == depotPath:
838 output = entry
839 break
Tor Arvid Lund7f705dc2008-12-04 14:37:33 +0100840 if output == None:
841 return ""
Simon Hausmanndc524032007-05-21 09:34:56 +0200842 if output["code"] == "error":
843 return ""
Simon Hausmanncb2c9db2007-03-24 09:15:11 +0100844 clientPath = ""
845 if "path" in output:
Yang Zhaod38208a2019-12-13 15:52:40 -0800846 clientPath = decode_path(output['path'])
Simon Hausmanncb2c9db2007-03-24 09:15:11 +0100847 elif "data" in output:
848 data = output.get("data")
Yang Zhaod38208a2019-12-13 15:52:40 -0800849 lastSpace = data.rfind(b" ")
850 clientPath = decode_path(data[lastSpace + 1:])
Simon Hausmanncb2c9db2007-03-24 09:15:11 +0100851
852 if clientPath.endswith("..."):
853 clientPath = clientPath[:-3]
854 return clientPath
855
Simon Hausmann86949ee2007-03-19 20:59:12 +0100856def currentGitBranch():
Luke Diamandeff45112017-04-15 11:36:09 +0100857 return read_pipe_text(["git", "symbolic-ref", "--short", "-q", "HEAD"])
Simon Hausmann86949ee2007-03-19 20:59:12 +0100858
Simon Hausmann4f5cf762007-03-19 22:25:17 +0100859def isValidGitDir(path):
Luke Diamand378f7be2016-12-13 21:51:28 +0000860 return git_dir(path) != None
Simon Hausmann4f5cf762007-03-19 22:25:17 +0100861
Simon Hausmann463e8af2007-05-17 09:13:54 +0200862def parseRevision(ref):
Han-Wen Nienhuysb25b2062007-05-23 18:49:35 -0300863 return read_pipe("git rev-parse %s" % ref).strip()
Simon Hausmann463e8af2007-05-17 09:13:54 +0200864
Pete Wyckoff28755db2011-12-24 21:07:40 -0500865def branchExists(ref):
866 rev = read_pipe(["git", "rev-parse", "-q", "--verify", ref],
867 ignore_error=True)
868 return len(rev) > 0
869
Simon Hausmann6ae8de82007-03-22 21:10:25 +0100870def extractLogMessageFromGitCommit(commit):
871 logMessage = ""
Han-Wen Nienhuysb016d392007-05-23 17:10:46 -0300872
873 ## fixme: title is first line of commit, not 1st paragraph.
Simon Hausmann6ae8de82007-03-22 21:10:25 +0100874 foundTitle = False
Mike Muellerc3f23582019-05-28 11:15:46 -0700875 for log in read_pipe_lines(["git", "cat-file", "commit", commit]):
Simon Hausmann6ae8de82007-03-22 21:10:25 +0100876 if not foundTitle:
877 if len(log) == 1:
Simon Hausmann1c094182007-05-01 23:15:48 +0200878 foundTitle = True
Simon Hausmann6ae8de82007-03-22 21:10:25 +0100879 continue
880
881 logMessage += log
882 return logMessage
883
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -0300884def extractSettingsGitLog(log):
Simon Hausmann6ae8de82007-03-22 21:10:25 +0100885 values = {}
886 for line in log.split("\n"):
887 line = line.strip()
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -0300888 m = re.search (r"^ *\[git-p4: (.*)\]$", line)
889 if not m:
890 continue
Simon Hausmann6ae8de82007-03-22 21:10:25 +0100891
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -0300892 assignments = m.group(1).split (':')
893 for a in assignments:
894 vals = a.split ('=')
895 key = vals[0].strip()
896 val = ('='.join (vals[1:])).strip()
897 if val.endswith ('\"') and val.startswith('"'):
898 val = val[1:-1]
899
900 values[key] = val
901
Simon Hausmann845b42c2007-06-07 09:19:34 +0200902 paths = values.get("depot-paths")
903 if not paths:
904 paths = values.get("depot-path")
Simon Hausmanna3fdd572007-06-07 22:54:32 +0200905 if paths:
906 values['depot-paths'] = paths.split(',')
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -0300907 return values
Simon Hausmann6ae8de82007-03-22 21:10:25 +0100908
Simon Hausmann8136a632007-03-22 21:27:14 +0100909def gitBranchExists(branch):
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -0300910 proc = subprocess.Popen(["git", "rev-parse", branch],
911 stderr=subprocess.PIPE, stdout=subprocess.PIPE);
Simon Hausmanncaace112007-05-15 14:57:57 +0200912 return proc.wait() == 0;
Simon Hausmann8136a632007-03-22 21:27:14 +0100913
Luke Diamand123f6312018-05-23 23:20:26 +0100914def gitUpdateRef(ref, newvalue):
915 subprocess.check_call(["git", "update-ref", ref, newvalue])
916
917def gitDeleteRef(ref):
918 subprocess.check_call(["git", "update-ref", "-d", ref])
919
John Chapman36bd8442008-11-08 14:22:49 +1100920_gitConfig = {}
Pete Wyckoffb345d6c2013-01-26 22:11:23 -0500921
Lars Schneider692e1792015-09-26 09:54:58 +0200922def gitConfig(key, typeSpecifier=None):
Luke Diamanddba1c9d2018-06-19 09:04:07 +0100923 if key not in _gitConfig:
Lars Schneider692e1792015-09-26 09:54:58 +0200924 cmd = [ "git", "config" ]
925 if typeSpecifier:
926 cmd += [ typeSpecifier ]
927 cmd += [ key ]
Pete Wyckoffb345d6c2013-01-26 22:11:23 -0500928 s = read_pipe(cmd, ignore_error=True)
929 _gitConfig[key] = s.strip()
John Chapman36bd8442008-11-08 14:22:49 +1100930 return _gitConfig[key]
Simon Hausmann01265102007-05-25 10:36:10 +0200931
Pete Wyckoff0d609032013-01-26 22:11:24 -0500932def gitConfigBool(key):
933 """Return a bool, using git config --bool. It is True only if the
934 variable is set to true, and False if set to false or not present
935 in the config."""
936
Luke Diamanddba1c9d2018-06-19 09:04:07 +0100937 if key not in _gitConfig:
Lars Schneider692e1792015-09-26 09:54:58 +0200938 _gitConfig[key] = gitConfig(key, '--bool') == "true"
Simon Hausmann062410b2007-07-18 10:56:31 +0200939 return _gitConfig[key]
940
Lars Schneidercb1dafd2015-09-26 09:54:59 +0200941def gitConfigInt(key):
Luke Diamanddba1c9d2018-06-19 09:04:07 +0100942 if key not in _gitConfig:
Lars Schneidercb1dafd2015-09-26 09:54:59 +0200943 cmd = [ "git", "config", "--int", key ]
Simon Hausmannb9847332007-03-20 20:54:23 +0100944 s = read_pipe(cmd, ignore_error=True)
Simon Hausmann062410b2007-07-18 10:56:31 +0200945 v = s.strip()
Lars Schneidercb1dafd2015-09-26 09:54:59 +0200946 try:
947 _gitConfig[key] = int(gitConfig(key, '--int'))
948 except ValueError:
949 _gitConfig[key] = None
Simon Hausmann062410b2007-07-18 10:56:31 +0200950 return _gitConfig[key]
951
Vitor Antunes7199cf12011-08-19 00:44:05 +0100952def gitConfigList(key):
Luke Diamanddba1c9d2018-06-19 09:04:07 +0100953 if key not in _gitConfig:
Pete Wyckoff2abba302013-01-26 22:11:22 -0500954 s = read_pipe(["git", "config", "--get-all", key], ignore_error=True)
George Vanburghc3c2b052017-01-25 09:17:29 +0000955 _gitConfig[key] = s.strip().splitlines()
Lars Schneider7960e702015-09-26 09:55:00 +0200956 if _gitConfig[key] == ['']:
957 _gitConfig[key] = []
Vitor Antunes7199cf12011-08-19 00:44:05 +0100958 return _gitConfig[key]
959
Pete Wyckoff2c8037e2013-01-14 19:46:57 -0500960def p4BranchesInGit(branchesAreInRemotes=True):
961 """Find all the branches whose names start with "p4/", looking
962 in remotes or heads as specified by the argument. Return
963 a dictionary of { branch: revision } for each one found.
964 The branch names are the short names, without any
965 "p4/" prefix."""
966
Simon Hausmann062410b2007-07-18 10:56:31 +0200967 branches = {}
968
969 cmdline = "git rev-parse --symbolic "
970 if branchesAreInRemotes:
Pete Wyckoff2c8037e2013-01-14 19:46:57 -0500971 cmdline += "--remotes"
Simon Hausmann062410b2007-07-18 10:56:31 +0200972 else:
Pete Wyckoff2c8037e2013-01-14 19:46:57 -0500973 cmdline += "--branches"
Simon Hausmann062410b2007-07-18 10:56:31 +0200974
975 for line in read_pipe_lines(cmdline):
976 line = line.strip()
977
Pete Wyckoff2c8037e2013-01-14 19:46:57 -0500978 # only import to p4/
979 if not line.startswith('p4/'):
Simon Hausmann062410b2007-07-18 10:56:31 +0200980 continue
Pete Wyckoff2c8037e2013-01-14 19:46:57 -0500981 # special symbolic ref to p4/master
982 if line == "p4/HEAD":
983 continue
Simon Hausmann062410b2007-07-18 10:56:31 +0200984
Pete Wyckoff2c8037e2013-01-14 19:46:57 -0500985 # strip off p4/ prefix
986 branch = line[len("p4/"):]
Simon Hausmann062410b2007-07-18 10:56:31 +0200987
988 branches[branch] = parseRevision(line)
Pete Wyckoff2c8037e2013-01-14 19:46:57 -0500989
Simon Hausmann062410b2007-07-18 10:56:31 +0200990 return branches
991
Pete Wyckoff5a8e84c2013-01-14 19:47:05 -0500992def branch_exists(branch):
993 """Make sure that the given ref name really exists."""
994
995 cmd = [ "git", "rev-parse", "--symbolic", "--verify", branch ]
996 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
997 out, _ = p.communicate()
Yang Zhao6cec21a2019-12-13 15:52:38 -0800998 out = decode_text_stream(out)
Pete Wyckoff5a8e84c2013-01-14 19:47:05 -0500999 if p.returncode:
1000 return False
1001 # expect exactly one line of output: the branch name
1002 return out.rstrip() == branch
1003
Simon Hausmann9ceab362007-06-22 00:01:57 +02001004def findUpstreamBranchPoint(head = "HEAD"):
Simon Hausmann86506fe2007-07-18 12:40:12 +02001005 branches = p4BranchesInGit()
1006 # map from depot-path to branch name
1007 branchByDepotPath = {}
1008 for branch in branches.keys():
1009 tip = branches[branch]
1010 log = extractLogMessageFromGitCommit(tip)
1011 settings = extractSettingsGitLog(log)
Luke Diamanddba1c9d2018-06-19 09:04:07 +01001012 if "depot-paths" in settings:
Simon Hausmann86506fe2007-07-18 12:40:12 +02001013 paths = ",".join(settings["depot-paths"])
1014 branchByDepotPath[paths] = "remotes/p4/" + branch
1015
Simon Hausmann27d2d812007-06-12 14:31:59 +02001016 settings = None
Simon Hausmann27d2d812007-06-12 14:31:59 +02001017 parent = 0
1018 while parent < 65535:
Simon Hausmann9ceab362007-06-22 00:01:57 +02001019 commit = head + "~%s" % parent
Simon Hausmann27d2d812007-06-12 14:31:59 +02001020 log = extractLogMessageFromGitCommit(commit)
1021 settings = extractSettingsGitLog(log)
Luke Diamanddba1c9d2018-06-19 09:04:07 +01001022 if "depot-paths" in settings:
Simon Hausmann86506fe2007-07-18 12:40:12 +02001023 paths = ",".join(settings["depot-paths"])
Luke Diamanddba1c9d2018-06-19 09:04:07 +01001024 if paths in branchByDepotPath:
Simon Hausmann86506fe2007-07-18 12:40:12 +02001025 return [branchByDepotPath[paths], settings]
Simon Hausmann27d2d812007-06-12 14:31:59 +02001026
Simon Hausmann86506fe2007-07-18 12:40:12 +02001027 parent = parent + 1
Simon Hausmann27d2d812007-06-12 14:31:59 +02001028
Simon Hausmann86506fe2007-07-18 12:40:12 +02001029 return ["", settings]
Simon Hausmann27d2d812007-06-12 14:31:59 +02001030
Simon Hausmann5ca44612007-08-24 17:44:16 +02001031def createOrUpdateBranchesFromOrigin(localRefPrefix = "refs/remotes/p4/", silent=True):
1032 if not silent:
Luke Diamandf2606b12018-06-19 09:04:10 +01001033 print("Creating/updating branch(es) in %s based on origin branch(es)"
Simon Hausmann5ca44612007-08-24 17:44:16 +02001034 % localRefPrefix)
1035
1036 originPrefix = "origin/p4/"
1037
1038 for line in read_pipe_lines("git rev-parse --symbolic --remotes"):
1039 line = line.strip()
1040 if (not line.startswith(originPrefix)) or line.endswith("HEAD"):
1041 continue
1042
1043 headName = line[len(originPrefix):]
1044 remoteHead = localRefPrefix + headName
1045 originHead = line
1046
1047 original = extractSettingsGitLog(extractLogMessageFromGitCommit(originHead))
Luke Diamanddba1c9d2018-06-19 09:04:07 +01001048 if ('depot-paths' not in original
1049 or 'change' not in original):
Simon Hausmann5ca44612007-08-24 17:44:16 +02001050 continue
1051
1052 update = False
1053 if not gitBranchExists(remoteHead):
1054 if verbose:
Luke Diamandf2606b12018-06-19 09:04:10 +01001055 print("creating %s" % remoteHead)
Simon Hausmann5ca44612007-08-24 17:44:16 +02001056 update = True
1057 else:
1058 settings = extractSettingsGitLog(extractLogMessageFromGitCommit(remoteHead))
Luke Diamanddba1c9d2018-06-19 09:04:07 +01001059 if 'change' in settings:
Simon Hausmann5ca44612007-08-24 17:44:16 +02001060 if settings['depot-paths'] == original['depot-paths']:
1061 originP4Change = int(original['change'])
1062 p4Change = int(settings['change'])
1063 if originP4Change > p4Change:
Luke Diamandf2606b12018-06-19 09:04:10 +01001064 print("%s (%s) is newer than %s (%s). "
Simon Hausmann5ca44612007-08-24 17:44:16 +02001065 "Updating p4 branch from origin."
1066 % (originHead, originP4Change,
1067 remoteHead, p4Change))
1068 update = True
1069 else:
Luke Diamandf2606b12018-06-19 09:04:10 +01001070 print("Ignoring: %s was imported from %s while "
Simon Hausmann5ca44612007-08-24 17:44:16 +02001071 "%s was imported from %s"
1072 % (originHead, ','.join(original['depot-paths']),
1073 remoteHead, ','.join(settings['depot-paths'])))
1074
1075 if update:
1076 system("git update-ref %s %s" % (remoteHead, originHead))
1077
1078def originP4BranchesExist():
1079 return gitBranchExists("origin") or gitBranchExists("origin/p4") or gitBranchExists("origin/p4/master")
1080
Simon Hausmann4f6432d2007-08-26 15:56:36 +02001081
Luke Diamand1051ef02015-06-10 08:30:59 +01001082def p4ParseNumericChangeRange(parts):
1083 changeStart = int(parts[0][1:])
1084 if parts[1] == '#head':
1085 changeEnd = p4_last_change()
1086 else:
1087 changeEnd = int(parts[1])
1088
1089 return (changeStart, changeEnd)
1090
1091def chooseBlockSize(blockSize):
1092 if blockSize:
1093 return blockSize
1094 else:
1095 return defaultBlockSize
1096
1097def p4ChangesForPaths(depotPaths, changeRange, requestedBlockSize):
1098 assert depotPaths
1099
1100 # Parse the change range into start and end. Try to find integer
1101 # revision ranges as these can be broken up into blocks to avoid
1102 # hitting server-side limits (maxrows, maxscanresults). But if
1103 # that doesn't work, fall back to using the raw revision specifier
1104 # strings, without using block mode.
1105
Lex Spoon96b2d542015-04-20 11:00:20 -04001106 if changeRange is None or changeRange == '':
Luke Diamand1051ef02015-06-10 08:30:59 +01001107 changeStart = 1
1108 changeEnd = p4_last_change()
1109 block_size = chooseBlockSize(requestedBlockSize)
Lex Spoon96b2d542015-04-20 11:00:20 -04001110 else:
1111 parts = changeRange.split(',')
1112 assert len(parts) == 2
Luke Diamand1051ef02015-06-10 08:30:59 +01001113 try:
1114 (changeStart, changeEnd) = p4ParseNumericChangeRange(parts)
1115 block_size = chooseBlockSize(requestedBlockSize)
Luke Diamand8fa0abf2018-06-08 21:32:47 +01001116 except ValueError:
Luke Diamand1051ef02015-06-10 08:30:59 +01001117 changeStart = parts[0][1:]
1118 changeEnd = parts[1]
1119 if requestedBlockSize:
1120 die("cannot use --changes-block-size with non-numeric revisions")
1121 block_size = None
Lex Spoon96b2d542015-04-20 11:00:20 -04001122
George Vanburgh9943e5b2016-12-17 22:11:23 +00001123 changes = set()
Lex Spoon96b2d542015-04-20 11:00:20 -04001124
Sam Hocevar1f90a642015-12-19 09:39:40 +00001125 # Retrieve changes a block at a time, to prevent running
Luke Diamand3deed5e2018-06-08 21:32:48 +01001126 # into a MaxResults/MaxScanRows error from the server. If
1127 # we _do_ hit one of those errors, turn down the block size
Luke Diamand1051ef02015-06-10 08:30:59 +01001128
Sam Hocevar1f90a642015-12-19 09:39:40 +00001129 while True:
1130 cmd = ['changes']
Luke Diamand1051ef02015-06-10 08:30:59 +01001131
Sam Hocevar1f90a642015-12-19 09:39:40 +00001132 if block_size:
1133 end = min(changeEnd, changeStart + block_size)
1134 revisionRange = "%d,%d" % (changeStart, end)
1135 else:
1136 revisionRange = "%s,%s" % (changeStart, changeEnd)
Luke Diamand1051ef02015-06-10 08:30:59 +01001137
Sam Hocevar1f90a642015-12-19 09:39:40 +00001138 for p in depotPaths:
Luke Diamand1051ef02015-06-10 08:30:59 +01001139 cmd += ["%s...@%s" % (p, revisionRange)]
1140
Luke Diamand3deed5e2018-06-08 21:32:48 +01001141 # fetch the changes
1142 try:
1143 result = p4CmdList(cmd, errors_as_exceptions=True)
1144 except P4RequestSizeException as e:
1145 if not block_size:
1146 block_size = e.limit
1147 elif block_size > e.limit:
1148 block_size = e.limit
1149 else:
1150 block_size = max(2, block_size // 2)
1151
1152 if verbose: print("block size error, retrying with block size {0}".format(block_size))
1153 continue
1154 except P4Exception as e:
1155 die('Error retrieving changes description ({0})'.format(e.p4ExitCode))
1156
Sam Hocevar1f90a642015-12-19 09:39:40 +00001157 # Insert changes in chronological order
Luke Diamand3deed5e2018-06-08 21:32:48 +01001158 for entry in reversed(result):
Luke Diamanddba1c9d2018-06-19 09:04:07 +01001159 if 'change' not in entry:
Miguel Torrojab596b3b2017-07-13 09:00:34 +02001160 continue
1161 changes.add(int(entry['change']))
Luke Diamand1051ef02015-06-10 08:30:59 +01001162
Sam Hocevar1f90a642015-12-19 09:39:40 +00001163 if not block_size:
1164 break
Luke Diamand1051ef02015-06-10 08:30:59 +01001165
Sam Hocevar1f90a642015-12-19 09:39:40 +00001166 if end >= changeEnd:
1167 break
Luke Diamand1051ef02015-06-10 08:30:59 +01001168
Sam Hocevar1f90a642015-12-19 09:39:40 +00001169 changeStart = end + 1
Simon Hausmann4f6432d2007-08-26 15:56:36 +02001170
Sam Hocevar1f90a642015-12-19 09:39:40 +00001171 changes = sorted(changes)
1172 return changes
Simon Hausmann4f6432d2007-08-26 15:56:36 +02001173
Tor Arvid Lundd53de8b2011-03-15 13:08:02 +01001174def p4PathStartsWith(path, prefix):
1175 # This method tries to remedy a potential mixed-case issue:
1176 #
1177 # If UserA adds //depot/DirA/file1
1178 # and UserB adds //depot/dira/file2
1179 #
1180 # we may or may not have a problem. If you have core.ignorecase=true,
1181 # we treat DirA and dira as the same directory
Pete Wyckoff0d609032013-01-26 22:11:24 -05001182 if gitConfigBool("core.ignorecase"):
Tor Arvid Lundd53de8b2011-03-15 13:08:02 +01001183 return path.lower().startswith(prefix.lower())
1184 return path.startswith(prefix)
1185
Pete Wyckoff543987b2012-02-25 20:06:25 -05001186def getClientSpec():
1187 """Look at the p4 client spec, create a View() object that contains
1188 all the mappings, and return it."""
1189
1190 specList = p4CmdList("client -o")
1191 if len(specList) != 1:
1192 die('Output from "client -o" is %d lines, expecting 1' %
1193 len(specList))
1194
1195 # dictionary of all client parameters
1196 entry = specList[0]
1197
Kazuki Saitoh9d57c4a2013-08-30 19:02:06 +09001198 # the //client/ name
1199 client_name = entry["Client"]
1200
Pete Wyckoff543987b2012-02-25 20:06:25 -05001201 # just the keys that start with "View"
1202 view_keys = [ k for k in entry.keys() if k.startswith("View") ]
1203
1204 # hold this new View
Kazuki Saitoh9d57c4a2013-08-30 19:02:06 +09001205 view = View(client_name)
Pete Wyckoff543987b2012-02-25 20:06:25 -05001206
1207 # append the lines, in order, to the view
1208 for view_num in range(len(view_keys)):
1209 k = "View%d" % view_num
1210 if k not in view_keys:
1211 die("Expected view key %s missing" % k)
1212 view.append(entry[k])
1213
1214 return view
1215
1216def getClientRoot():
1217 """Grab the client directory."""
1218
1219 output = p4CmdList("client -o")
1220 if len(output) != 1:
1221 die('Output from "client -o" is %d lines, expecting 1' % len(output))
1222
1223 entry = output[0]
1224 if "Root" not in entry:
1225 die('Client has no "Root"')
1226
1227 return entry["Root"]
1228
Pete Wyckoff9d7d4462012-04-29 20:57:17 -04001229#
1230# P4 wildcards are not allowed in filenames. P4 complains
1231# if you simply add them, but you can force it with "-f", in
1232# which case it translates them into %xx encoding internally.
1233#
1234def wildcard_decode(path):
1235 # Search for and fix just these four characters. Do % last so
1236 # that fixing it does not inadvertently create new %-escapes.
1237 # Cannot have * in a filename in windows; untested as to
1238 # what p4 would do in such a case.
1239 if not platform.system() == "Windows":
1240 path = path.replace("%2A", "*")
1241 path = path.replace("%23", "#") \
1242 .replace("%40", "@") \
1243 .replace("%25", "%")
1244 return path
1245
1246def wildcard_encode(path):
1247 # do % first to avoid double-encoding the %s introduced here
1248 path = path.replace("%", "%25") \
1249 .replace("*", "%2A") \
1250 .replace("#", "%23") \
1251 .replace("@", "%40")
1252 return path
1253
1254def wildcard_present(path):
Brandon Casey598354c2013-01-26 11:14:32 -08001255 m = re.search("[*#@%]", path)
1256 return m is not None
Pete Wyckoff9d7d4462012-04-29 20:57:17 -04001257
Lars Schneidera5db4b12015-09-26 09:55:03 +02001258class LargeFileSystem(object):
1259 """Base class for large file system support."""
1260
1261 def __init__(self, writeToGitStream):
1262 self.largeFiles = set()
1263 self.writeToGitStream = writeToGitStream
1264
1265 def generatePointer(self, cloneDestination, contentFile):
1266 """Return the content of a pointer file that is stored in Git instead of
1267 the actual content."""
1268 assert False, "Method 'generatePointer' required in " + self.__class__.__name__
1269
1270 def pushFile(self, localLargeFile):
1271 """Push the actual content which is not stored in the Git repository to
1272 a server."""
1273 assert False, "Method 'pushFile' required in " + self.__class__.__name__
1274
1275 def hasLargeFileExtension(self, relPath):
Yang Zhaoa6b13062019-12-13 15:52:44 -08001276 return functools.reduce(
Lars Schneidera5db4b12015-09-26 09:55:03 +02001277 lambda a, b: a or b,
1278 [relPath.endswith('.' + e) for e in gitConfigList('git-p4.largeFileExtensions')],
1279 False
1280 )
1281
1282 def generateTempFile(self, contents):
1283 contentFile = tempfile.NamedTemporaryFile(prefix='git-p4-large-file', delete=False)
1284 for d in contents:
1285 contentFile.write(d)
1286 contentFile.close()
1287 return contentFile.name
1288
1289 def exceedsLargeFileThreshold(self, relPath, contents):
1290 if gitConfigInt('git-p4.largeFileThreshold'):
1291 contentsSize = sum(len(d) for d in contents)
1292 if contentsSize > gitConfigInt('git-p4.largeFileThreshold'):
1293 return True
1294 if gitConfigInt('git-p4.largeFileCompressedThreshold'):
1295 contentsSize = sum(len(d) for d in contents)
1296 if contentsSize <= gitConfigInt('git-p4.largeFileCompressedThreshold'):
1297 return False
1298 contentTempFile = self.generateTempFile(contents)
Philip.McGrawde5abb52019-08-27 06:43:58 +03001299 compressedContentFile = tempfile.NamedTemporaryFile(prefix='git-p4-large-file', delete=True)
1300 with zipfile.ZipFile(compressedContentFile, mode='w') as zf:
1301 zf.write(contentTempFile, compress_type=zipfile.ZIP_DEFLATED)
1302 compressedContentsSize = zf.infolist()[0].compress_size
Lars Schneidera5db4b12015-09-26 09:55:03 +02001303 os.remove(contentTempFile)
Lars Schneidera5db4b12015-09-26 09:55:03 +02001304 if compressedContentsSize > gitConfigInt('git-p4.largeFileCompressedThreshold'):
1305 return True
1306 return False
1307
1308 def addLargeFile(self, relPath):
1309 self.largeFiles.add(relPath)
1310
1311 def removeLargeFile(self, relPath):
1312 self.largeFiles.remove(relPath)
1313
1314 def isLargeFile(self, relPath):
1315 return relPath in self.largeFiles
1316
1317 def processContent(self, git_mode, relPath, contents):
1318 """Processes the content of git fast import. This method decides if a
1319 file is stored in the large file system and handles all necessary
1320 steps."""
1321 if self.exceedsLargeFileThreshold(relPath, contents) or self.hasLargeFileExtension(relPath):
1322 contentTempFile = self.generateTempFile(contents)
Lars Schneiderd5eb3cf2016-12-04 17:03:37 +01001323 (pointer_git_mode, contents, localLargeFile) = self.generatePointer(contentTempFile)
1324 if pointer_git_mode:
1325 git_mode = pointer_git_mode
1326 if localLargeFile:
1327 # Move temp file to final location in large file system
1328 largeFileDir = os.path.dirname(localLargeFile)
1329 if not os.path.isdir(largeFileDir):
1330 os.makedirs(largeFileDir)
1331 shutil.move(contentTempFile, localLargeFile)
1332 self.addLargeFile(relPath)
1333 if gitConfigBool('git-p4.largeFilePush'):
1334 self.pushFile(localLargeFile)
1335 if verbose:
1336 sys.stderr.write("%s moved to large file system (%s)\n" % (relPath, localLargeFile))
Lars Schneidera5db4b12015-09-26 09:55:03 +02001337 return (git_mode, contents)
1338
1339class MockLFS(LargeFileSystem):
1340 """Mock large file system for testing."""
1341
1342 def generatePointer(self, contentFile):
1343 """The pointer content is the original content prefixed with "pointer-".
1344 The local filename of the large file storage is derived from the file content.
1345 """
1346 with open(contentFile, 'r') as f:
1347 content = next(f)
1348 gitMode = '100644'
1349 pointerContents = 'pointer-' + content
1350 localLargeFile = os.path.join(os.getcwd(), '.git', 'mock-storage', 'local', content[:-1])
1351 return (gitMode, pointerContents, localLargeFile)
1352
1353 def pushFile(self, localLargeFile):
1354 """The remote filename of the large file storage is the same as the local
1355 one but in a different directory.
1356 """
1357 remotePath = os.path.join(os.path.dirname(localLargeFile), '..', 'remote')
1358 if not os.path.exists(remotePath):
1359 os.makedirs(remotePath)
1360 shutil.copyfile(localLargeFile, os.path.join(remotePath, os.path.basename(localLargeFile)))
1361
Lars Schneiderb47d8072015-09-26 09:55:04 +02001362class GitLFS(LargeFileSystem):
1363 """Git LFS as backend for the git-p4 large file system.
1364 See https://git-lfs.github.com/ for details."""
1365
1366 def __init__(self, *args):
1367 LargeFileSystem.__init__(self, *args)
1368 self.baseGitAttributes = []
1369
1370 def generatePointer(self, contentFile):
1371 """Generate a Git LFS pointer for the content. Return LFS Pointer file
1372 mode and content which is stored in the Git repository instead of
1373 the actual content. Return also the new location of the actual
1374 content.
1375 """
Lars Schneiderd5eb3cf2016-12-04 17:03:37 +01001376 if os.path.getsize(contentFile) == 0:
1377 return (None, '', None)
1378
Lars Schneiderb47d8072015-09-26 09:55:04 +02001379 pointerProcess = subprocess.Popen(
1380 ['git', 'lfs', 'pointer', '--file=' + contentFile],
1381 stdout=subprocess.PIPE
1382 )
Yang Zhao86dca242019-12-13 15:52:39 -08001383 pointerFile = decode_text_stream(pointerProcess.stdout.read())
Lars Schneiderb47d8072015-09-26 09:55:04 +02001384 if pointerProcess.wait():
1385 os.remove(contentFile)
1386 die('git-lfs pointer command failed. Did you install the extension?')
Lars Schneider82f25672016-04-28 08:26:33 +02001387
1388 # Git LFS removed the preamble in the output of the 'pointer' command
1389 # starting from version 1.2.0. Check for the preamble here to support
1390 # earlier versions.
1391 # c.f. https://github.com/github/git-lfs/commit/da2935d9a739592bc775c98d8ef4df9c72ea3b43
1392 if pointerFile.startswith('Git LFS pointer for'):
1393 pointerFile = re.sub(r'Git LFS pointer for.*\n\n', '', pointerFile)
1394
1395 oid = re.search(r'^oid \w+:(\w+)', pointerFile, re.MULTILINE).group(1)
r.burenkovea94b162019-12-11 09:47:23 -08001396 # if someone use external lfs.storage ( not in local repo git )
1397 lfs_path = gitConfig('lfs.storage')
1398 if not lfs_path:
1399 lfs_path = 'lfs'
1400 if not os.path.isabs(lfs_path):
1401 lfs_path = os.path.join(os.getcwd(), '.git', lfs_path)
Lars Schneiderb47d8072015-09-26 09:55:04 +02001402 localLargeFile = os.path.join(
r.burenkovea94b162019-12-11 09:47:23 -08001403 lfs_path,
1404 'objects', oid[:2], oid[2:4],
Lars Schneiderb47d8072015-09-26 09:55:04 +02001405 oid,
1406 )
1407 # LFS Spec states that pointer files should not have the executable bit set.
1408 gitMode = '100644'
Lars Schneider82f25672016-04-28 08:26:33 +02001409 return (gitMode, pointerFile, localLargeFile)
Lars Schneiderb47d8072015-09-26 09:55:04 +02001410
1411 def pushFile(self, localLargeFile):
1412 uploadProcess = subprocess.Popen(
1413 ['git', 'lfs', 'push', '--object-id', 'origin', os.path.basename(localLargeFile)]
1414 )
1415 if uploadProcess.wait():
1416 die('git-lfs push command failed. Did you define a remote?')
1417
1418 def generateGitAttributes(self):
1419 return (
1420 self.baseGitAttributes +
1421 [
1422 '\n',
1423 '#\n',
1424 '# Git LFS (see https://git-lfs.github.com/)\n',
1425 '#\n',
1426 ] +
Lars Schneider862f9312016-12-18 20:01:40 +01001427 ['*.' + f.replace(' ', '[[:space:]]') + ' filter=lfs diff=lfs merge=lfs -text\n'
Lars Schneiderb47d8072015-09-26 09:55:04 +02001428 for f in sorted(gitConfigList('git-p4.largeFileExtensions'))
1429 ] +
Lars Schneider862f9312016-12-18 20:01:40 +01001430 ['/' + f.replace(' ', '[[:space:]]') + ' filter=lfs diff=lfs merge=lfs -text\n'
Lars Schneiderb47d8072015-09-26 09:55:04 +02001431 for f in sorted(self.largeFiles) if not self.hasLargeFileExtension(f)
1432 ]
1433 )
1434
1435 def addLargeFile(self, relPath):
1436 LargeFileSystem.addLargeFile(self, relPath)
1437 self.writeToGitStream('100644', '.gitattributes', self.generateGitAttributes())
1438
1439 def removeLargeFile(self, relPath):
1440 LargeFileSystem.removeLargeFile(self, relPath)
1441 self.writeToGitStream('100644', '.gitattributes', self.generateGitAttributes())
1442
1443 def processContent(self, git_mode, relPath, contents):
1444 if relPath == '.gitattributes':
1445 self.baseGitAttributes = contents
1446 return (git_mode, self.generateGitAttributes())
1447 else:
1448 return LargeFileSystem.processContent(self, git_mode, relPath, contents)
1449
Simon Hausmannb9847332007-03-20 20:54:23 +01001450class Command:
Luke Diamand89143ac2018-10-15 12:14:08 +01001451 delete_actions = ( "delete", "move/delete", "purge" )
Simon Williams0108f472019-05-22 07:21:20 +01001452 add_actions = ( "add", "branch", "move/add" )
Luke Diamand89143ac2018-10-15 12:14:08 +01001453
Simon Hausmannb9847332007-03-20 20:54:23 +01001454 def __init__(self):
1455 self.usage = "usage: %prog [options]"
Simon Hausmann8910ac02007-03-26 08:18:55 +02001456 self.needsGit = True
Luke Diamand6a10b6a2012-04-24 09:33:23 +01001457 self.verbose = False
Simon Hausmannb9847332007-03-20 20:54:23 +01001458
Mazo, Andreyff8c50e2019-04-01 18:02:26 +00001459 # This is required for the "append" update_shelve action
Luke Diamand8cf422d2017-12-21 11:06:14 +00001460 def ensure_value(self, attr, value):
1461 if not hasattr(self, attr) or getattr(self, attr) is None:
1462 setattr(self, attr, value)
1463 return getattr(self, attr)
1464
Luke Diamand3ea2cfd2011-04-21 20:50:23 +01001465class P4UserMap:
1466 def __init__(self):
1467 self.userMapFromPerforceServer = False
Luke Diamandaffb4742012-01-19 09:52:27 +00001468 self.myP4UserId = None
1469
1470 def p4UserId(self):
1471 if self.myP4UserId:
1472 return self.myP4UserId
1473
1474 results = p4CmdList("user -o")
1475 for r in results:
Luke Diamanddba1c9d2018-06-19 09:04:07 +01001476 if 'User' in r:
Luke Diamandaffb4742012-01-19 09:52:27 +00001477 self.myP4UserId = r['User']
1478 return r['User']
1479 die("Could not find your p4 user id")
1480
1481 def p4UserIsMe(self, p4User):
1482 # return True if the given p4 user is actually me
1483 me = self.p4UserId()
1484 if not p4User or p4User != me:
1485 return False
1486 else:
1487 return True
Luke Diamand3ea2cfd2011-04-21 20:50:23 +01001488
1489 def getUserCacheFilename(self):
1490 home = os.environ.get("HOME", os.environ.get("USERPROFILE"))
1491 return home + "/.gitp4-usercache.txt"
1492
1493 def getUserMapFromPerforceServer(self):
1494 if self.userMapFromPerforceServer:
1495 return
1496 self.users = {}
1497 self.emails = {}
1498
1499 for output in p4CmdList("users"):
Luke Diamanddba1c9d2018-06-19 09:04:07 +01001500 if "User" not in output:
Luke Diamand3ea2cfd2011-04-21 20:50:23 +01001501 continue
1502 self.users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">"
1503 self.emails[output["Email"]] = output["User"]
1504
Lars Schneider10d08a12016-03-01 11:49:56 +01001505 mapUserConfigRegex = re.compile(r"^\s*(\S+)\s*=\s*(.+)\s*<(\S+)>\s*$", re.VERBOSE)
1506 for mapUserConfig in gitConfigList("git-p4.mapUser"):
1507 mapUser = mapUserConfigRegex.findall(mapUserConfig)
1508 if mapUser and len(mapUser[0]) == 3:
1509 user = mapUser[0][0]
1510 fullname = mapUser[0][1]
1511 email = mapUser[0][2]
1512 self.users[user] = fullname + " <" + email + ">"
1513 self.emails[email] = user
Luke Diamand3ea2cfd2011-04-21 20:50:23 +01001514
1515 s = ''
1516 for (key, val) in self.users.items():
1517 s += "%s\t%s\n" % (key.expandtabs(1), val.expandtabs(1))
1518
Yang Zhao5a5577d2019-12-13 15:52:41 -08001519 open(self.getUserCacheFilename(), 'w').write(s)
Luke Diamand3ea2cfd2011-04-21 20:50:23 +01001520 self.userMapFromPerforceServer = True
1521
1522 def loadUserMapFromCache(self):
1523 self.users = {}
1524 self.userMapFromPerforceServer = False
1525 try:
Yang Zhao5a5577d2019-12-13 15:52:41 -08001526 cache = open(self.getUserCacheFilename(), 'r')
Luke Diamand3ea2cfd2011-04-21 20:50:23 +01001527 lines = cache.readlines()
1528 cache.close()
1529 for line in lines:
1530 entry = line.strip().split("\t")
1531 self.users[entry[0]] = entry[1]
1532 except IOError:
1533 self.getUserMapFromPerforceServer()
1534
Simon Hausmannb9847332007-03-20 20:54:23 +01001535class P4Debug(Command):
Simon Hausmann86949ee2007-03-19 20:59:12 +01001536 def __init__(self):
Simon Hausmann6ae8de82007-03-22 21:10:25 +01001537 Command.__init__(self)
Luke Diamand6a10b6a2012-04-24 09:33:23 +01001538 self.options = []
Simon Hausmannc8c39112007-03-19 21:02:30 +01001539 self.description = "A tool to debug the output of p4 -G."
Simon Hausmann8910ac02007-03-26 08:18:55 +02001540 self.needsGit = False
Simon Hausmann86949ee2007-03-19 20:59:12 +01001541
1542 def run(self, args):
Han-Wen Nienhuysb1ce9442007-05-23 18:49:35 -03001543 j = 0
Luke Diamand6de040d2011-10-16 10:47:52 -04001544 for output in p4CmdList(args):
Luke Diamandf2606b12018-06-19 09:04:10 +01001545 print('Element: %d' % j)
Han-Wen Nienhuysb1ce9442007-05-23 18:49:35 -03001546 j += 1
Luke Diamandf2606b12018-06-19 09:04:10 +01001547 print(output)
Simon Hausmannb9847332007-03-20 20:54:23 +01001548 return True
Simon Hausmann86949ee2007-03-19 20:59:12 +01001549
Simon Hausmann58346842007-05-21 22:57:06 +02001550class P4RollBack(Command):
1551 def __init__(self):
1552 Command.__init__(self)
1553 self.options = [
Simon Hausmann0c66a782007-05-23 20:07:57 +02001554 optparse.make_option("--local", dest="rollbackLocalBranches", action="store_true")
Simon Hausmann58346842007-05-21 22:57:06 +02001555 ]
1556 self.description = "A tool to debug the multi-branch import. Don't use :)"
Simon Hausmann0c66a782007-05-23 20:07:57 +02001557 self.rollbackLocalBranches = False
Simon Hausmann58346842007-05-21 22:57:06 +02001558
1559 def run(self, args):
1560 if len(args) != 1:
1561 return False
1562 maxChange = int(args[0])
Simon Hausmann0c66a782007-05-23 20:07:57 +02001563
Simon Hausmannad192f22007-05-23 23:44:19 +02001564 if "p4ExitCode" in p4Cmd("changes -m 1"):
Simon Hausmann66a2f522007-05-23 23:40:48 +02001565 die("Problems executing p4");
1566
Simon Hausmann0c66a782007-05-23 20:07:57 +02001567 if self.rollbackLocalBranches:
1568 refPrefix = "refs/heads/"
Han-Wen Nienhuysb016d392007-05-23 17:10:46 -03001569 lines = read_pipe_lines("git rev-parse --symbolic --branches")
Simon Hausmann0c66a782007-05-23 20:07:57 +02001570 else:
1571 refPrefix = "refs/remotes/"
Han-Wen Nienhuysb016d392007-05-23 17:10:46 -03001572 lines = read_pipe_lines("git rev-parse --symbolic --remotes")
Simon Hausmann0c66a782007-05-23 20:07:57 +02001573
1574 for line in lines:
1575 if self.rollbackLocalBranches or (line.startswith("p4/") and line != "p4/HEAD\n"):
Han-Wen Nienhuysb25b2062007-05-23 18:49:35 -03001576 line = line.strip()
1577 ref = refPrefix + line
Simon Hausmann58346842007-05-21 22:57:06 +02001578 log = extractLogMessageFromGitCommit(ref)
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03001579 settings = extractSettingsGitLog(log)
1580
1581 depotPaths = settings['depot-paths']
1582 change = settings['change']
1583
Simon Hausmann58346842007-05-21 22:57:06 +02001584 changed = False
Simon Hausmann52102d42007-05-21 23:44:24 +02001585
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03001586 if len(p4Cmd("changes -m 1 " + ' '.join (['%s...@%s' % (p, maxChange)
1587 for p in depotPaths]))) == 0:
Luke Diamandf2606b12018-06-19 09:04:10 +01001588 print("Branch %s did not exist at change %s, deleting." % (ref, maxChange))
Simon Hausmann52102d42007-05-21 23:44:24 +02001589 system("git update-ref -d %s `git rev-parse %s`" % (ref, ref))
1590 continue
1591
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03001592 while change and int(change) > maxChange:
Simon Hausmann58346842007-05-21 22:57:06 +02001593 changed = True
Simon Hausmann52102d42007-05-21 23:44:24 +02001594 if self.verbose:
Luke Diamandf2606b12018-06-19 09:04:10 +01001595 print("%s is at %s ; rewinding towards %s" % (ref, change, maxChange))
Simon Hausmann58346842007-05-21 22:57:06 +02001596 system("git update-ref %s \"%s^\"" % (ref, ref))
1597 log = extractLogMessageFromGitCommit(ref)
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03001598 settings = extractSettingsGitLog(log)
1599
1600
1601 depotPaths = settings['depot-paths']
1602 change = settings['change']
Simon Hausmann58346842007-05-21 22:57:06 +02001603
1604 if changed:
Luke Diamandf2606b12018-06-19 09:04:10 +01001605 print("%s rewound to %s" % (ref, change))
Simon Hausmann58346842007-05-21 22:57:06 +02001606
1607 return True
1608
Luke Diamand3ea2cfd2011-04-21 20:50:23 +01001609class P4Submit(Command, P4UserMap):
Pete Wyckoff6bbfd132012-09-09 16:16:13 -04001610
1611 conflict_behavior_choices = ("ask", "skip", "quit")
1612
Simon Hausmann4f5cf762007-03-19 22:25:17 +01001613 def __init__(self):
Simon Hausmannb9847332007-03-20 20:54:23 +01001614 Command.__init__(self)
Luke Diamand3ea2cfd2011-04-21 20:50:23 +01001615 P4UserMap.__init__(self)
Simon Hausmann4f5cf762007-03-19 22:25:17 +01001616 self.options = [
Simon Hausmann4f5cf762007-03-19 22:25:17 +01001617 optparse.make_option("--origin", dest="origin"),
Vitor Antunesae901092011-02-20 01:18:24 +00001618 optparse.make_option("-M", dest="detectRenames", action="store_true"),
Luke Diamand3ea2cfd2011-04-21 20:50:23 +01001619 # preserve the user, requires relevant p4 permissions
1620 optparse.make_option("--preserve-user", dest="preserveUser", action="store_true"),
Luke Diamand06804c72012-04-11 17:21:24 +02001621 optparse.make_option("--export-labels", dest="exportLabels", action="store_true"),
Pete Wyckoffef739f02012-09-09 16:16:11 -04001622 optparse.make_option("--dry-run", "-n", dest="dry_run", action="store_true"),
Pete Wyckoff728b7ad2012-09-09 16:16:12 -04001623 optparse.make_option("--prepare-p4-only", dest="prepare_p4_only", action="store_true"),
Pete Wyckoff6bbfd132012-09-09 16:16:13 -04001624 optparse.make_option("--conflict", dest="conflict_behavior",
Pete Wyckoff44e8d262013-01-14 19:47:08 -05001625 choices=self.conflict_behavior_choices),
1626 optparse.make_option("--branch", dest="branch"),
Vinicius Kursancewb34fa572016-11-28 09:33:18 +00001627 optparse.make_option("--shelve", dest="shelve", action="store_true",
1628 help="Shelve instead of submit. Shelved files are reverted, "
1629 "restoring the workspace to the state before the shelve"),
Luke Diamand8cf422d2017-12-21 11:06:14 +00001630 optparse.make_option("--update-shelve", dest="update_shelve", action="append", type="int",
Luke Diamand46c609e2016-12-02 22:43:19 +00001631 metavar="CHANGELIST",
Luke Diamand8cf422d2017-12-21 11:06:14 +00001632 help="update an existing shelved changelist, implies --shelve, "
Romain Merlandf55b87c2018-06-01 09:46:14 +02001633 "repeat in-order for multiple shelved changelists"),
1634 optparse.make_option("--commit", dest="commit", metavar="COMMIT",
1635 help="submit only the specified commit(s), one commit or xxx..xxx"),
1636 optparse.make_option("--disable-rebase", dest="disable_rebase", action="store_true",
1637 help="Disable rebase after submit is completed. Can be useful if you "
Luke Diamandb9d34db2018-06-08 21:32:44 +01001638 "work from a local git branch that is not master"),
1639 optparse.make_option("--disable-p4sync", dest="disable_p4sync", action="store_true",
1640 help="Skip Perforce sync of p4/master after submit or shelve"),
Ben Keene4935c452020-02-11 18:58:01 +00001641 optparse.make_option("--no-verify", dest="no_verify", action="store_true",
Ben Keene38ecf752020-02-14 14:44:45 +00001642 help="Bypass p4-pre-submit and p4-changelist hooks"),
Simon Hausmann4f5cf762007-03-19 22:25:17 +01001643 ]
Chen Bin251c8c52018-07-27 21:22:22 +10001644 self.description = """Submit changes from git to the perforce depot.\n
Ben Keene4935c452020-02-11 18:58:01 +00001645 The `p4-pre-submit` hook is executed if it exists and is executable. It
1646 can be bypassed with the `--no-verify` command line option. The hook takes
1647 no parameters and nothing from standard input. Exiting with a non-zero status
1648 from this script prevents `git-p4 submit` from launching.
Chen Bin251c8c52018-07-27 21:22:22 +10001649
Ben Keene4935c452020-02-11 18:58:01 +00001650 One usage scenario is to run unit tests in the hook.
Ben Keene38ecf752020-02-14 14:44:45 +00001651
1652 The `p4-prepare-changelist` hook is executed right after preparing the default
1653 changelist message and before the editor is started. It takes one parameter,
1654 the name of the file that contains the changelist text. Exiting with a non-zero
1655 status from the script will abort the process.
1656
1657 The purpose of the hook is to edit the message file in place, and it is not
1658 supressed by the `--no-verify` option. This hook is called even if
1659 `--prepare-p4-only` is set.
1660
1661 The `p4-changelist` hook is executed after the changelist message has been
1662 edited by the user. It can be bypassed with the `--no-verify` option. It
1663 takes a single parameter, the name of the file that holds the proposed
1664 changelist text. Exiting with a non-zero status causes the command to abort.
1665
1666 The hook is allowed to edit the changelist file and can be used to normalize
1667 the text into some project standard format. It can also be used to refuse the
1668 Submit after inspect the message file.
1669
1670 The `p4-post-changelist` hook is invoked after the submit has successfully
1671 occured in P4. It takes no parameters and is meant primarily for notification
1672 and cannot affect the outcome of the git p4 submit action.
Ben Keene4935c452020-02-11 18:58:01 +00001673 """
Chen Bin251c8c52018-07-27 21:22:22 +10001674
Simon Hausmannc9b50e62007-03-29 19:15:24 +02001675 self.usage += " [name of git branch to submit into perforce depot]"
Simon Hausmann95124972007-03-23 09:16:07 +01001676 self.origin = ""
Vitor Antunesae901092011-02-20 01:18:24 +00001677 self.detectRenames = False
Pete Wyckoff0d609032013-01-26 22:11:24 -05001678 self.preserveUser = gitConfigBool("git-p4.preserveUser")
Pete Wyckoffef739f02012-09-09 16:16:11 -04001679 self.dry_run = False
Vinicius Kursancewb34fa572016-11-28 09:33:18 +00001680 self.shelve = False
Luke Diamand8cf422d2017-12-21 11:06:14 +00001681 self.update_shelve = list()
Romain Merlandf55b87c2018-06-01 09:46:14 +02001682 self.commit = ""
Luke Diamand3b3477e2018-06-08 21:32:43 +01001683 self.disable_rebase = gitConfigBool("git-p4.disableRebase")
Luke Diamandb9d34db2018-06-08 21:32:44 +01001684 self.disable_p4sync = gitConfigBool("git-p4.disableP4Sync")
Pete Wyckoff728b7ad2012-09-09 16:16:12 -04001685 self.prepare_p4_only = False
Pete Wyckoff6bbfd132012-09-09 16:16:13 -04001686 self.conflict_behavior = None
Marius Storm-Olsenf7baba82007-06-07 14:07:01 +02001687 self.isWindows = (platform.system() == "Windows")
Luke Diamand06804c72012-04-11 17:21:24 +02001688 self.exportLabels = False
Pete Wyckoff249da4c2012-11-23 17:35:35 -05001689 self.p4HasMoveCommand = p4_has_move_command()
Pete Wyckoff44e8d262013-01-14 19:47:08 -05001690 self.branch = None
Ben Keene4935c452020-02-11 18:58:01 +00001691 self.no_verify = False
Simon Hausmann4f5cf762007-03-19 22:25:17 +01001692
Lars Schneidera5db4b12015-09-26 09:55:03 +02001693 if gitConfig('git-p4.largeFileSystem'):
1694 die("Large file system not supported for git-p4 submit command. Please remove it from config.")
1695
Simon Hausmann4f5cf762007-03-19 22:25:17 +01001696 def check(self):
1697 if len(p4CmdList("opened ...")) > 0:
1698 die("You have files opened with perforce! Close them before starting the sync.")
1699
Pete Wyckofff19cb0a2012-07-04 09:34:20 -04001700 def separate_jobs_from_description(self, message):
1701 """Extract and return a possible Jobs field in the commit
1702 message. It goes into a separate section in the p4 change
1703 specification.
1704
1705 A jobs line starts with "Jobs:" and looks like a new field
1706 in a form. Values are white-space separated on the same
1707 line or on following lines that start with a tab.
1708
1709 This does not parse and extract the full git commit message
1710 like a p4 form. It just sees the Jobs: line as a marker
1711 to pass everything from then on directly into the p4 form,
1712 but outside the description section.
1713
1714 Return a tuple (stripped log message, jobs string)."""
1715
1716 m = re.search(r'^Jobs:', message, re.MULTILINE)
1717 if m is None:
1718 return (message, None)
1719
1720 jobtext = message[m.start():]
1721 stripped_message = message[:m.start()].rstrip()
1722 return (stripped_message, jobtext)
1723
1724 def prepareLogMessage(self, template, message, jobs):
1725 """Edits the template returned from "p4 change -o" to insert
1726 the message in the Description field, and the jobs text in
1727 the Jobs field."""
Simon Hausmann4f5cf762007-03-19 22:25:17 +01001728 result = ""
1729
Simon Hausmannedae1e22008-02-19 09:29:06 +01001730 inDescriptionSection = False
1731
Simon Hausmann4f5cf762007-03-19 22:25:17 +01001732 for line in template.split("\n"):
1733 if line.startswith("#"):
1734 result += line + "\n"
1735 continue
1736
Simon Hausmannedae1e22008-02-19 09:29:06 +01001737 if inDescriptionSection:
Michael Horowitzc9dbab02011-02-25 21:31:13 -05001738 if line.startswith("Files:") or line.startswith("Jobs:"):
Simon Hausmannedae1e22008-02-19 09:29:06 +01001739 inDescriptionSection = False
Pete Wyckofff19cb0a2012-07-04 09:34:20 -04001740 # insert Jobs section
1741 if jobs:
1742 result += jobs + "\n"
Simon Hausmannedae1e22008-02-19 09:29:06 +01001743 else:
1744 continue
1745 else:
1746 if line.startswith("Description:"):
1747 inDescriptionSection = True
1748 line += "\n"
1749 for messageLine in message.split("\n"):
1750 line += "\t" + messageLine + "\n"
Simon Hausmann4f5cf762007-03-19 22:25:17 +01001751
Simon Hausmannedae1e22008-02-19 09:29:06 +01001752 result += line + "\n"
Simon Hausmann4f5cf762007-03-19 22:25:17 +01001753
1754 return result
1755
Luke Diamand60df0712012-02-23 07:51:30 +00001756 def patchRCSKeywords(self, file, pattern):
1757 # Attempt to zap the RCS keywords in a p4 controlled file matching the given pattern
1758 (handle, outFileName) = tempfile.mkstemp(dir='.')
1759 try:
1760 outFile = os.fdopen(handle, "w+")
1761 inFile = open(file, "r")
1762 regexp = re.compile(pattern, re.VERBOSE)
1763 for line in inFile.readlines():
1764 line = regexp.sub(r'$\1$', line)
1765 outFile.write(line)
1766 inFile.close()
1767 outFile.close()
1768 # Forcibly overwrite the original file
1769 os.unlink(file)
1770 shutil.move(outFileName, file)
1771 except:
1772 # cleanup our temporary file
1773 os.unlink(outFileName)
Luke Diamandf2606b12018-06-19 09:04:10 +01001774 print("Failed to strip RCS keywords in %s" % file)
Luke Diamand60df0712012-02-23 07:51:30 +00001775 raise
1776
Luke Diamandf2606b12018-06-19 09:04:10 +01001777 print("Patched up RCS keywords in %s" % file)
Luke Diamand60df0712012-02-23 07:51:30 +00001778
Luke Diamand3ea2cfd2011-04-21 20:50:23 +01001779 def p4UserForCommit(self,id):
1780 # Return the tuple (perforce user,git email) for a given git commit id
1781 self.getUserMapFromPerforceServer()
Pete Wyckoff9bf28852013-01-26 22:11:20 -05001782 gitEmail = read_pipe(["git", "log", "--max-count=1",
1783 "--format=%ae", id])
Luke Diamand3ea2cfd2011-04-21 20:50:23 +01001784 gitEmail = gitEmail.strip()
Luke Diamanddba1c9d2018-06-19 09:04:07 +01001785 if gitEmail not in self.emails:
Luke Diamand3ea2cfd2011-04-21 20:50:23 +01001786 return (None,gitEmail)
1787 else:
1788 return (self.emails[gitEmail],gitEmail)
1789
1790 def checkValidP4Users(self,commits):
1791 # check if any git authors cannot be mapped to p4 users
1792 for id in commits:
1793 (user,email) = self.p4UserForCommit(id)
1794 if not user:
1795 msg = "Cannot find p4 user for email %s in commit %s." % (email, id)
Pete Wyckoff0d609032013-01-26 22:11:24 -05001796 if gitConfigBool("git-p4.allowMissingP4Users"):
Luke Diamandf2606b12018-06-19 09:04:10 +01001797 print("%s" % msg)
Luke Diamand3ea2cfd2011-04-21 20:50:23 +01001798 else:
1799 die("Error: %s\nSet git-p4.allowMissingP4Users to true to allow this." % msg)
1800
1801 def lastP4Changelist(self):
1802 # Get back the last changelist number submitted in this client spec. This
1803 # then gets used to patch up the username in the change. If the same
1804 # client spec is being used by multiple processes then this might go
1805 # wrong.
1806 results = p4CmdList("client -o") # find the current client
1807 client = None
1808 for r in results:
Luke Diamanddba1c9d2018-06-19 09:04:07 +01001809 if 'Client' in r:
Luke Diamand3ea2cfd2011-04-21 20:50:23 +01001810 client = r['Client']
1811 break
1812 if not client:
1813 die("could not get client spec")
Luke Diamand6de040d2011-10-16 10:47:52 -04001814 results = p4CmdList(["changes", "-c", client, "-m", "1"])
Luke Diamand3ea2cfd2011-04-21 20:50:23 +01001815 for r in results:
Luke Diamanddba1c9d2018-06-19 09:04:07 +01001816 if 'change' in r:
Luke Diamand3ea2cfd2011-04-21 20:50:23 +01001817 return r['change']
1818 die("Could not get changelist number for last submit - cannot patch up user details")
1819
1820 def modifyChangelistUser(self, changelist, newUser):
1821 # fixup the user field of a changelist after it has been submitted.
1822 changes = p4CmdList("change -o %s" % changelist)
Luke Diamandecdba362011-05-07 11:19:43 +01001823 if len(changes) != 1:
1824 die("Bad output from p4 change modifying %s to user %s" %
1825 (changelist, newUser))
1826
1827 c = changes[0]
1828 if c['User'] == newUser: return # nothing to do
1829 c['User'] = newUser
Yang Zhao50da1e72019-12-13 15:52:42 -08001830 # p4 does not understand format version 3 and above
1831 input = marshal.dumps(c, 2)
Luke Diamandecdba362011-05-07 11:19:43 +01001832
Luke Diamand3ea2cfd2011-04-21 20:50:23 +01001833 result = p4CmdList("change -f -i", stdin=input)
1834 for r in result:
Luke Diamanddba1c9d2018-06-19 09:04:07 +01001835 if 'code' in r:
Luke Diamand3ea2cfd2011-04-21 20:50:23 +01001836 if r['code'] == 'error':
1837 die("Could not modify user field of changelist %s to %s:%s" % (changelist, newUser, r['data']))
Luke Diamanddba1c9d2018-06-19 09:04:07 +01001838 if 'data' in r:
Luke Diamand3ea2cfd2011-04-21 20:50:23 +01001839 print("Updated user field for changelist %s to %s" % (changelist, newUser))
1840 return
1841 die("Could not modify user field of changelist %s to %s" % (changelist, newUser))
1842
1843 def canChangeChangelists(self):
1844 # check to see if we have p4 admin or super-user permissions, either of
1845 # which are required to modify changelists.
Luke Diamand52a48802012-01-19 09:52:25 +00001846 results = p4CmdList(["protects", self.depotPath])
Luke Diamand3ea2cfd2011-04-21 20:50:23 +01001847 for r in results:
Luke Diamanddba1c9d2018-06-19 09:04:07 +01001848 if 'perm' in r:
Luke Diamand3ea2cfd2011-04-21 20:50:23 +01001849 if r['perm'] == 'admin':
1850 return 1
1851 if r['perm'] == 'super':
1852 return 1
1853 return 0
1854
Luke Diamand46c609e2016-12-02 22:43:19 +00001855 def prepareSubmitTemplate(self, changelist=None):
Pete Wyckofff19cb0a2012-07-04 09:34:20 -04001856 """Run "p4 change -o" to grab a change specification template.
1857 This does not use "p4 -G", as it is nice to keep the submission
1858 template in original order, since a human might edit it.
1859
1860 Remove lines in the Files section that show changes to files
1861 outside the depot path we're committing into."""
1862
Sam Hocevarcbc69242015-12-19 09:39:39 +00001863 [upstream, settings] = findUpstreamBranchPoint()
1864
Miguel Torrojab596b3b2017-07-13 09:00:34 +02001865 template = """\
1866# A Perforce Change Specification.
1867#
1868# Change: The change number. 'new' on a new changelist.
1869# Date: The date this specification was last modified.
1870# Client: The client on which the changelist was created. Read-only.
1871# User: The user who created the changelist.
1872# Status: Either 'pending' or 'submitted'. Read-only.
1873# Type: Either 'public' or 'restricted'. Default is 'public'.
1874# Description: Comments about the changelist. Required.
1875# Jobs: What opened jobs are to be closed by this changelist.
1876# You may delete jobs from this list. (New changelists only.)
1877# Files: What opened files from the default changelist are to be added
1878# to this changelist. You may delete files from this list.
1879# (New changelists only.)
1880"""
1881 files_list = []
Simon Hausmannea99c3a2007-08-08 17:06:55 +02001882 inFilesSection = False
Miguel Torrojab596b3b2017-07-13 09:00:34 +02001883 change_entry = None
Luke Diamand46c609e2016-12-02 22:43:19 +00001884 args = ['change', '-o']
1885 if changelist:
1886 args.append(str(changelist))
Miguel Torrojab596b3b2017-07-13 09:00:34 +02001887 for entry in p4CmdList(args):
Luke Diamanddba1c9d2018-06-19 09:04:07 +01001888 if 'code' not in entry:
Miguel Torrojab596b3b2017-07-13 09:00:34 +02001889 continue
1890 if entry['code'] == 'stat':
1891 change_entry = entry
1892 break
1893 if not change_entry:
1894 die('Failed to decode output of p4 change -o')
Yang Zhao2e2aa8d2019-12-13 15:52:45 -08001895 for key, value in change_entry.items():
Miguel Torrojab596b3b2017-07-13 09:00:34 +02001896 if key.startswith('File'):
Luke Diamanddba1c9d2018-06-19 09:04:07 +01001897 if 'depot-paths' in settings:
Miguel Torrojab596b3b2017-07-13 09:00:34 +02001898 if not [p for p in settings['depot-paths']
1899 if p4PathStartsWith(value, p)]:
1900 continue
Simon Hausmannea99c3a2007-08-08 17:06:55 +02001901 else:
Miguel Torrojab596b3b2017-07-13 09:00:34 +02001902 if not p4PathStartsWith(value, self.depotPath):
1903 continue
1904 files_list.append(value)
1905 continue
1906 # Output in the order expected by prepareLogMessage
1907 for key in ['Change', 'Client', 'User', 'Status', 'Description', 'Jobs']:
Luke Diamanddba1c9d2018-06-19 09:04:07 +01001908 if key not in change_entry:
Miguel Torrojab596b3b2017-07-13 09:00:34 +02001909 continue
1910 template += '\n'
1911 template += key + ':'
1912 if key == 'Description':
1913 template += '\n'
1914 for field_line in change_entry[key].splitlines():
1915 template += '\t'+field_line+'\n'
1916 if len(files_list) > 0:
1917 template += '\n'
1918 template += 'Files:\n'
1919 for path in files_list:
1920 template += '\t'+path+'\n'
Simon Hausmannea99c3a2007-08-08 17:06:55 +02001921 return template
1922
Pete Wyckoff7c766e52011-12-04 19:22:45 -05001923 def edit_template(self, template_file):
1924 """Invoke the editor to let the user change the submission
1925 message. Return true if okay to continue with the submit."""
1926
1927 # if configured to skip the editing part, just submit
Pete Wyckoff0d609032013-01-26 22:11:24 -05001928 if gitConfigBool("git-p4.skipSubmitEdit"):
Pete Wyckoff7c766e52011-12-04 19:22:45 -05001929 return True
1930
1931 # look at the modification time, to check later if the user saved
1932 # the file
1933 mtime = os.stat(template_file).st_mtime
1934
1935 # invoke the editor
Luke Diamanddba1c9d2018-06-19 09:04:07 +01001936 if "P4EDITOR" in os.environ and (os.environ.get("P4EDITOR") != ""):
Pete Wyckoff7c766e52011-12-04 19:22:45 -05001937 editor = os.environ.get("P4EDITOR")
1938 else:
1939 editor = read_pipe("git var GIT_EDITOR").strip()
Luke Diamand2dade7a2015-05-19 23:23:17 +01001940 system(["sh", "-c", ('%s "$@"' % editor), editor, template_file])
Pete Wyckoff7c766e52011-12-04 19:22:45 -05001941
1942 # If the file was not saved, prompt to see if this patch should
1943 # be skipped. But skip this verification step if configured so.
Pete Wyckoff0d609032013-01-26 22:11:24 -05001944 if gitConfigBool("git-p4.skipSubmitEditCheck"):
Pete Wyckoff7c766e52011-12-04 19:22:45 -05001945 return True
1946
Pete Wyckoffd1652042011-12-17 12:39:03 -05001947 # modification time updated means user saved the file
1948 if os.stat(template_file).st_mtime > mtime:
1949 return True
1950
Ben Keenee2aed5f2019-12-16 14:02:19 +00001951 response = prompt("Submit template unchanged. Submit anyway? [y]es, [n]o (skip this patch) ")
1952 if response == 'y':
1953 return True
1954 if response == 'n':
1955 return False
Pete Wyckoff7c766e52011-12-04 19:22:45 -05001956
Luke Diamanddf8a9e82016-12-17 01:00:40 +00001957 def get_diff_description(self, editedFiles, filesToAdd, symlinks):
Maxime Costeb4073bb2014-05-24 18:40:35 +01001958 # diff
Luke Diamanddba1c9d2018-06-19 09:04:07 +01001959 if "P4DIFF" in os.environ:
Maxime Costeb4073bb2014-05-24 18:40:35 +01001960 del(os.environ["P4DIFF"])
1961 diff = ""
1962 for editedFile in editedFiles:
1963 diff += p4_read_pipe(['diff', '-du',
1964 wildcard_encode(editedFile)])
1965
1966 # new file diff
1967 newdiff = ""
1968 for newFile in filesToAdd:
1969 newdiff += "==== new file ====\n"
1970 newdiff += "--- /dev/null\n"
1971 newdiff += "+++ %s\n" % newFile
Luke Diamanddf8a9e82016-12-17 01:00:40 +00001972
1973 is_link = os.path.islink(newFile)
1974 expect_link = newFile in symlinks
1975
1976 if is_link and expect_link:
1977 newdiff += "+%s\n" % os.readlink(newFile)
1978 else:
1979 f = open(newFile, "r")
1980 for line in f.readlines():
1981 newdiff += "+" + line
1982 f.close()
Maxime Costeb4073bb2014-05-24 18:40:35 +01001983
Maxime Costee2a892e2014-06-11 14:09:59 +01001984 return (diff + newdiff).replace('\r\n', '\n')
Maxime Costeb4073bb2014-05-24 18:40:35 +01001985
Han-Wen Nienhuys7cb5cbe2007-05-23 16:55:48 -03001986 def applyCommit(self, id):
Pete Wyckoff67b0fe22012-09-09 16:16:03 -04001987 """Apply one commit, return True if it succeeded."""
1988
Luke Diamandf2606b12018-06-19 09:04:10 +01001989 print("Applying", read_pipe(["git", "show", "-s",
1990 "--format=format:%h %s", id]))
Vitor Antunesae901092011-02-20 01:18:24 +00001991
Luke Diamand848de9c2011-05-13 20:46:00 +01001992 (p4User, gitEmail) = self.p4UserForCommit(id)
Luke Diamand3ea2cfd2011-04-21 20:50:23 +01001993
Gary Gibbons84cb0002012-07-04 09:40:19 -04001994 diff = read_pipe_lines("git diff-tree -r %s \"%s^\" \"%s\"" % (self.diffOpts, id, id))
Simon Hausmann4f5cf762007-03-19 22:25:17 +01001995 filesToAdd = set()
Romain Picarda02b8bc2016-01-12 13:43:47 +01001996 filesToChangeType = set()
Simon Hausmann4f5cf762007-03-19 22:25:17 +01001997 filesToDelete = set()
Simon Hausmannd336c152007-05-16 09:41:26 +02001998 editedFiles = set()
Pete Wyckoffb6ad6dc2012-04-29 20:57:16 -04001999 pureRenameCopy = set()
Luke Diamanddf8a9e82016-12-17 01:00:40 +00002000 symlinks = set()
Chris Pettittc65b6702007-11-01 20:43:14 -07002001 filesToChangeExecBit = {}
Luke Diamand46c609e2016-12-02 22:43:19 +00002002 all_files = list()
Luke Diamand60df0712012-02-23 07:51:30 +00002003
Simon Hausmann4f5cf762007-03-19 22:25:17 +01002004 for line in diff:
Chris Pettittb43b0a32007-11-01 20:43:13 -07002005 diff = parseDiffTreeEntry(line)
2006 modifier = diff['status']
2007 path = diff['src']
Luke Diamand46c609e2016-12-02 22:43:19 +00002008 all_files.append(path)
2009
Simon Hausmann4f5cf762007-03-19 22:25:17 +01002010 if modifier == "M":
Luke Diamand6de040d2011-10-16 10:47:52 -04002011 p4_edit(path)
Chris Pettittc65b6702007-11-01 20:43:14 -07002012 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
2013 filesToChangeExecBit[path] = diff['dst_mode']
Simon Hausmannd336c152007-05-16 09:41:26 +02002014 editedFiles.add(path)
Simon Hausmann4f5cf762007-03-19 22:25:17 +01002015 elif modifier == "A":
2016 filesToAdd.add(path)
Chris Pettittc65b6702007-11-01 20:43:14 -07002017 filesToChangeExecBit[path] = diff['dst_mode']
Simon Hausmann4f5cf762007-03-19 22:25:17 +01002018 if path in filesToDelete:
2019 filesToDelete.remove(path)
Luke Diamanddf8a9e82016-12-17 01:00:40 +00002020
2021 dst_mode = int(diff['dst_mode'], 8)
Luke Diamanddb2d9972018-06-19 09:04:11 +01002022 if dst_mode == 0o120000:
Luke Diamanddf8a9e82016-12-17 01:00:40 +00002023 symlinks.add(path)
2024
Simon Hausmann4f5cf762007-03-19 22:25:17 +01002025 elif modifier == "D":
2026 filesToDelete.add(path)
2027 if path in filesToAdd:
2028 filesToAdd.remove(path)
Vitor Antunes4fddb412011-02-20 01:18:25 +00002029 elif modifier == "C":
2030 src, dest = diff['src'], diff['dst']
Luke Diamand7a109462019-01-18 09:36:56 +00002031 all_files.append(dest)
Luke Diamand6de040d2011-10-16 10:47:52 -04002032 p4_integrate(src, dest)
Pete Wyckoffb6ad6dc2012-04-29 20:57:16 -04002033 pureRenameCopy.add(dest)
Vitor Antunes4fddb412011-02-20 01:18:25 +00002034 if diff['src_sha1'] != diff['dst_sha1']:
Luke Diamand6de040d2011-10-16 10:47:52 -04002035 p4_edit(dest)
Pete Wyckoffb6ad6dc2012-04-29 20:57:16 -04002036 pureRenameCopy.discard(dest)
Vitor Antunes4fddb412011-02-20 01:18:25 +00002037 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
Luke Diamand6de040d2011-10-16 10:47:52 -04002038 p4_edit(dest)
Pete Wyckoffb6ad6dc2012-04-29 20:57:16 -04002039 pureRenameCopy.discard(dest)
Vitor Antunes4fddb412011-02-20 01:18:25 +00002040 filesToChangeExecBit[dest] = diff['dst_mode']
Pete Wyckoffd20f0f82013-01-26 22:11:19 -05002041 if self.isWindows:
2042 # turn off read-only attribute
2043 os.chmod(dest, stat.S_IWRITE)
Vitor Antunes4fddb412011-02-20 01:18:25 +00002044 os.unlink(dest)
2045 editedFiles.add(dest)
Chris Pettittd9a5f252007-10-15 22:15:06 -07002046 elif modifier == "R":
Chris Pettittb43b0a32007-11-01 20:43:13 -07002047 src, dest = diff['src'], diff['dst']
Luke Diamand7a109462019-01-18 09:36:56 +00002048 all_files.append(dest)
Gary Gibbons8e9497c2012-07-12 19:29:00 -04002049 if self.p4HasMoveCommand:
2050 p4_edit(src) # src must be open before move
2051 p4_move(src, dest) # opens for (move/delete, move/add)
Pete Wyckoffb6ad6dc2012-04-29 20:57:16 -04002052 else:
Gary Gibbons8e9497c2012-07-12 19:29:00 -04002053 p4_integrate(src, dest)
2054 if diff['src_sha1'] != diff['dst_sha1']:
2055 p4_edit(dest)
2056 else:
2057 pureRenameCopy.add(dest)
Chris Pettittc65b6702007-11-01 20:43:14 -07002058 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
Gary Gibbons8e9497c2012-07-12 19:29:00 -04002059 if not self.p4HasMoveCommand:
2060 p4_edit(dest) # with move: already open, writable
Chris Pettittc65b6702007-11-01 20:43:14 -07002061 filesToChangeExecBit[dest] = diff['dst_mode']
Gary Gibbons8e9497c2012-07-12 19:29:00 -04002062 if not self.p4HasMoveCommand:
Pete Wyckoffd20f0f82013-01-26 22:11:19 -05002063 if self.isWindows:
2064 os.chmod(dest, stat.S_IWRITE)
Gary Gibbons8e9497c2012-07-12 19:29:00 -04002065 os.unlink(dest)
2066 filesToDelete.add(src)
Chris Pettittd9a5f252007-10-15 22:15:06 -07002067 editedFiles.add(dest)
Romain Picarda02b8bc2016-01-12 13:43:47 +01002068 elif modifier == "T":
2069 filesToChangeType.add(path)
Simon Hausmann4f5cf762007-03-19 22:25:17 +01002070 else:
2071 die("unknown modifier %s for %s" % (modifier, path))
2072
Tolga Ceylan749b6682014-05-06 22:48:54 -07002073 diffcmd = "git diff-tree --full-index -p \"%s\"" % (id)
Simon Hausmann47a130b2007-05-20 16:33:21 +02002074 patchcmd = diffcmd + " | git apply "
Simon Hausmannc1b296b2007-05-20 16:55:05 +02002075 tryPatchCmd = patchcmd + "--check -"
2076 applyPatchCmd = patchcmd + "--check --apply -"
Luke Diamand60df0712012-02-23 07:51:30 +00002077 patch_succeeded = True
Simon Hausmann51a26402007-04-15 09:59:56 +02002078
Ben Keene1ec4a0a2020-02-14 14:44:46 +00002079 if verbose:
2080 print("TryPatch: %s" % tryPatchCmd)
2081
Simon Hausmann47a130b2007-05-20 16:33:21 +02002082 if os.system(tryPatchCmd) != 0:
Luke Diamand60df0712012-02-23 07:51:30 +00002083 fixed_rcs_keywords = False
2084 patch_succeeded = False
Luke Diamandf2606b12018-06-19 09:04:10 +01002085 print("Unfortunately applying the change failed!")
Luke Diamand60df0712012-02-23 07:51:30 +00002086
2087 # Patch failed, maybe it's just RCS keyword woes. Look through
2088 # the patch to see if that's possible.
Pete Wyckoff0d609032013-01-26 22:11:24 -05002089 if gitConfigBool("git-p4.attemptRCSCleanup"):
Luke Diamand60df0712012-02-23 07:51:30 +00002090 file = None
2091 pattern = None
2092 kwfiles = {}
2093 for file in editedFiles | filesToDelete:
2094 # did this file's delta contain RCS keywords?
2095 pattern = p4_keywords_regexp_for_file(file)
2096
2097 if pattern:
2098 # this file is a possibility...look for RCS keywords.
2099 regexp = re.compile(pattern, re.VERBOSE)
2100 for line in read_pipe_lines(["git", "diff", "%s^..%s" % (id, id), file]):
2101 if regexp.search(line):
2102 if verbose:
Luke Diamandf2606b12018-06-19 09:04:10 +01002103 print("got keyword match on %s in %s in %s" % (pattern, line, file))
Luke Diamand60df0712012-02-23 07:51:30 +00002104 kwfiles[file] = pattern
2105 break
2106
2107 for file in kwfiles:
2108 if verbose:
Luke Diamandf2606b12018-06-19 09:04:10 +01002109 print("zapping %s with %s" % (line,pattern))
Pete Wyckoffd20f0f82013-01-26 22:11:19 -05002110 # File is being deleted, so not open in p4. Must
2111 # disable the read-only bit on windows.
2112 if self.isWindows and file not in editedFiles:
2113 os.chmod(file, stat.S_IWRITE)
Luke Diamand60df0712012-02-23 07:51:30 +00002114 self.patchRCSKeywords(file, kwfiles[file])
2115 fixed_rcs_keywords = True
2116
2117 if fixed_rcs_keywords:
Luke Diamandf2606b12018-06-19 09:04:10 +01002118 print("Retrying the patch with RCS keywords cleaned up")
Luke Diamand60df0712012-02-23 07:51:30 +00002119 if os.system(tryPatchCmd) == 0:
2120 patch_succeeded = True
Ben Keene1ec4a0a2020-02-14 14:44:46 +00002121 print("Patch succeesed this time with RCS keywords cleaned")
Luke Diamand60df0712012-02-23 07:51:30 +00002122
2123 if not patch_succeeded:
Pete Wyckoff7e5dd9f2012-09-09 16:16:05 -04002124 for f in editedFiles:
2125 p4_revert(f)
Pete Wyckoff7e5dd9f2012-09-09 16:16:05 -04002126 return False
Simon Hausmann51a26402007-04-15 09:59:56 +02002127
Pete Wyckoff55ac2ed2012-09-09 16:16:08 -04002128 #
2129 # Apply the patch for real, and do add/delete/+x handling.
2130 #
Simon Hausmann47a130b2007-05-20 16:33:21 +02002131 system(applyPatchCmd)
Simon Hausmann4f5cf762007-03-19 22:25:17 +01002132
Romain Picarda02b8bc2016-01-12 13:43:47 +01002133 for f in filesToChangeType:
2134 p4_edit(f, "-t", "auto")
Simon Hausmann4f5cf762007-03-19 22:25:17 +01002135 for f in filesToAdd:
Luke Diamand6de040d2011-10-16 10:47:52 -04002136 p4_add(f)
Simon Hausmann4f5cf762007-03-19 22:25:17 +01002137 for f in filesToDelete:
Luke Diamand6de040d2011-10-16 10:47:52 -04002138 p4_revert(f)
2139 p4_delete(f)
Simon Hausmann4f5cf762007-03-19 22:25:17 +01002140
Chris Pettittc65b6702007-11-01 20:43:14 -07002141 # Set/clear executable bits
2142 for f in filesToChangeExecBit.keys():
2143 mode = filesToChangeExecBit[f]
2144 setP4ExecBit(f, mode)
2145
Luke Diamand8cf422d2017-12-21 11:06:14 +00002146 update_shelve = 0
2147 if len(self.update_shelve) > 0:
2148 update_shelve = self.update_shelve.pop(0)
2149 p4_reopen_in_change(update_shelve, all_files)
Luke Diamand46c609e2016-12-02 22:43:19 +00002150
Pete Wyckoff55ac2ed2012-09-09 16:16:08 -04002151 #
2152 # Build p4 change description, starting with the contents
2153 # of the git commit message.
2154 #
Simon Hausmann0e36f2d2008-02-19 09:33:08 +01002155 logMessage = extractLogMessageFromGitCommit(id)
Simon Hausmann0e36f2d2008-02-19 09:33:08 +01002156 logMessage = logMessage.strip()
Pete Wyckofff19cb0a2012-07-04 09:34:20 -04002157 (logMessage, jobs) = self.separate_jobs_from_description(logMessage)
Simon Hausmann4f5cf762007-03-19 22:25:17 +01002158
Luke Diamand8cf422d2017-12-21 11:06:14 +00002159 template = self.prepareSubmitTemplate(update_shelve)
Pete Wyckofff19cb0a2012-07-04 09:34:20 -04002160 submitTemplate = self.prepareLogMessage(template, logMessage, jobs)
Pete Wyckoffc47178d2012-07-04 09:34:18 -04002161
2162 if self.preserveUser:
Pete Wyckoff55ac2ed2012-09-09 16:16:08 -04002163 submitTemplate += "\n######## Actual user %s, modified after commit\n" % p4User
Pete Wyckoffc47178d2012-07-04 09:34:18 -04002164
Pete Wyckoff55ac2ed2012-09-09 16:16:08 -04002165 if self.checkAuthorship and not self.p4UserIsMe(p4User):
2166 submitTemplate += "######## git author %s does not match your p4 account.\n" % gitEmail
2167 submitTemplate += "######## Use option --preserve-user to modify authorship.\n"
2168 submitTemplate += "######## Variable git-p4.skipUserNameCheck hides this message.\n"
2169
2170 separatorLine = "######## everything below this line is just the diff #######\n"
Maxime Costeb4073bb2014-05-24 18:40:35 +01002171 if not self.prepare_p4_only:
2172 submitTemplate += separatorLine
Luke Diamanddf8a9e82016-12-17 01:00:40 +00002173 submitTemplate += self.get_diff_description(editedFiles, filesToAdd, symlinks)
Pete Wyckoff55ac2ed2012-09-09 16:16:08 -04002174
Pete Wyckoffc47178d2012-07-04 09:34:18 -04002175 (handle, fileName) = tempfile.mkstemp()
Maxime Costee2a892e2014-06-11 14:09:59 +01002176 tmpFile = os.fdopen(handle, "w+b")
Pete Wyckoffc47178d2012-07-04 09:34:18 -04002177 if self.isWindows:
2178 submitTemplate = submitTemplate.replace("\n", "\r\n")
Yang Zhao6cec21a2019-12-13 15:52:38 -08002179 tmpFile.write(encode_text_stream(submitTemplate))
Pete Wyckoffc47178d2012-07-04 09:34:18 -04002180 tmpFile.close()
2181
GIRARD Etienneb7638fe2015-11-24 07:43:59 +00002182 submitted = False
Luke Diamandecdba362011-05-07 11:19:43 +01002183
GIRARD Etienneb7638fe2015-11-24 07:43:59 +00002184 try:
Ben Keene38ecf752020-02-14 14:44:45 +00002185 # Allow the hook to edit the changelist text before presenting it
2186 # to the user.
2187 if not run_git_hook("p4-prepare-changelist", [fileName]):
2188 return False
Ben Keenecd1e0dc2020-02-14 14:44:44 +00002189
2190 if self.prepare_p4_only:
2191 #
2192 # Leave the p4 tree prepared, and the submit template around
2193 # and let the user decide what to do next
2194 #
2195 submitted = True
2196 print("")
2197 print("P4 workspace prepared for submission.")
2198 print("To submit or revert, go to client workspace")
2199 print(" " + self.clientPath)
2200 print("")
2201 print("To submit, use \"p4 submit\" to write a new description,")
2202 print("or \"p4 submit -i <%s\" to use the one prepared by" \
2203 " \"git p4\"." % fileName)
2204 print("You can delete the file \"%s\" when finished." % fileName)
2205
2206 if self.preserveUser and p4User and not self.p4UserIsMe(p4User):
2207 print("To preserve change ownership by user %s, you must\n" \
2208 "do \"p4 change -f <change>\" after submitting and\n" \
2209 "edit the User field.")
2210 if pureRenameCopy:
2211 print("After submitting, renamed files must be re-synced.")
2212 print("Invoke \"p4 sync -f\" on each of these files:")
2213 for f in pureRenameCopy:
2214 print(" " + f)
2215
2216 print("")
2217 print("To revert the changes, use \"p4 revert ...\", and delete")
2218 print("the submit template file \"%s\"" % fileName)
2219 if filesToAdd:
2220 print("Since the commit adds new files, they must be deleted:")
2221 for f in filesToAdd:
2222 print(" " + f)
2223 print("")
2224 sys.stdout.flush()
2225 return True
2226
GIRARD Etienneb7638fe2015-11-24 07:43:59 +00002227 if self.edit_template(fileName):
Ben Keene38ecf752020-02-14 14:44:45 +00002228 if not self.no_verify:
2229 if not run_git_hook("p4-changelist", [fileName]):
2230 print("The p4-changelist hook failed.")
2231 sys.stdout.flush()
2232 return False
2233
GIRARD Etienneb7638fe2015-11-24 07:43:59 +00002234 # read the edited message and submit
2235 tmpFile = open(fileName, "rb")
Yang Zhao6cec21a2019-12-13 15:52:38 -08002236 message = decode_text_stream(tmpFile.read())
GIRARD Etienneb7638fe2015-11-24 07:43:59 +00002237 tmpFile.close()
2238 if self.isWindows:
2239 message = message.replace("\r\n", "\n")
Ben Keenecd1e0dc2020-02-14 14:44:44 +00002240 if message.find(separatorLine) != -1:
2241 submitTemplate = message[:message.index(separatorLine)]
2242 else:
2243 submitTemplate = message
2244
2245 if len(submitTemplate.strip()) == 0:
2246 print("Changelist is empty, aborting this changelist.")
2247 sys.stdout.flush()
2248 return False
Luke Diamand46c609e2016-12-02 22:43:19 +00002249
Luke Diamand8cf422d2017-12-21 11:06:14 +00002250 if update_shelve:
Luke Diamand46c609e2016-12-02 22:43:19 +00002251 p4_write_pipe(['shelve', '-r', '-i'], submitTemplate)
2252 elif self.shelve:
Vinicius Kursancewb34fa572016-11-28 09:33:18 +00002253 p4_write_pipe(['shelve', '-i'], submitTemplate)
2254 else:
2255 p4_write_pipe(['submit', '-i'], submitTemplate)
2256 # The rename/copy happened by applying a patch that created a
2257 # new file. This leaves it writable, which confuses p4.
2258 for f in pureRenameCopy:
2259 p4_sync(f, "-f")
Luke Diamandecdba362011-05-07 11:19:43 +01002260
GIRARD Etienneb7638fe2015-11-24 07:43:59 +00002261 if self.preserveUser:
2262 if p4User:
2263 # Get last changelist number. Cannot easily get it from
2264 # the submit command output as the output is
2265 # unmarshalled.
2266 changelist = self.lastP4Changelist()
2267 self.modifyChangelistUser(changelist, p4User)
Simon Hausmann4f5cf762007-03-19 22:25:17 +01002268
GIRARD Etienneb7638fe2015-11-24 07:43:59 +00002269 submitted = True
2270
Ben Keene38ecf752020-02-14 14:44:45 +00002271 run_git_hook("p4-post-changelist")
GIRARD Etienneb7638fe2015-11-24 07:43:59 +00002272 finally:
Ben Keenecd1e0dc2020-02-14 14:44:44 +00002273 # Revert changes if we skip this patch
Vinicius Kursancewb34fa572016-11-28 09:33:18 +00002274 if not submitted or self.shelve:
2275 if self.shelve:
2276 print ("Reverting shelved files.")
2277 else:
2278 print ("Submission cancelled, undoing p4 changes.")
Ben Keenecd1e0dc2020-02-14 14:44:44 +00002279 sys.stdout.flush()
Vinicius Kursancewb34fa572016-11-28 09:33:18 +00002280 for f in editedFiles | filesToDelete:
GIRARD Etienneb7638fe2015-11-24 07:43:59 +00002281 p4_revert(f)
2282 for f in filesToAdd:
2283 p4_revert(f)
2284 os.remove(f)
Pete Wyckoffc47178d2012-07-04 09:34:18 -04002285
Ben Keenecd1e0dc2020-02-14 14:44:44 +00002286 if not self.prepare_p4_only:
2287 os.remove(fileName)
GIRARD Etienneb7638fe2015-11-24 07:43:59 +00002288 return submitted
Simon Hausmann4f5cf762007-03-19 22:25:17 +01002289
Luke Diamand06804c72012-04-11 17:21:24 +02002290 # Export git tags as p4 labels. Create a p4 label and then tag
2291 # with that.
2292 def exportGitTags(self, gitTags):
Luke Diamandc8942a22012-04-11 17:21:24 +02002293 validLabelRegexp = gitConfig("git-p4.labelExportRegexp")
2294 if len(validLabelRegexp) == 0:
2295 validLabelRegexp = defaultLabelRegexp
2296 m = re.compile(validLabelRegexp)
Luke Diamand06804c72012-04-11 17:21:24 +02002297
2298 for name in gitTags:
2299
2300 if not m.match(name):
2301 if verbose:
Luke Diamandf2606b12018-06-19 09:04:10 +01002302 print("tag %s does not match regexp %s" % (name, validLabelRegexp))
Luke Diamand06804c72012-04-11 17:21:24 +02002303 continue
2304
2305 # Get the p4 commit this corresponds to
Luke Diamandc8942a22012-04-11 17:21:24 +02002306 logMessage = extractLogMessageFromGitCommit(name)
2307 values = extractSettingsGitLog(logMessage)
Luke Diamand06804c72012-04-11 17:21:24 +02002308
Luke Diamanddba1c9d2018-06-19 09:04:07 +01002309 if 'change' not in values:
Luke Diamand06804c72012-04-11 17:21:24 +02002310 # a tag pointing to something not sent to p4; ignore
2311 if verbose:
Luke Diamandf2606b12018-06-19 09:04:10 +01002312 print("git tag %s does not give a p4 commit" % name)
Luke Diamand06804c72012-04-11 17:21:24 +02002313 continue
Luke Diamandc8942a22012-04-11 17:21:24 +02002314 else:
2315 changelist = values['change']
Luke Diamand06804c72012-04-11 17:21:24 +02002316
2317 # Get the tag details.
2318 inHeader = True
2319 isAnnotated = False
2320 body = []
2321 for l in read_pipe_lines(["git", "cat-file", "-p", name]):
2322 l = l.strip()
2323 if inHeader:
2324 if re.match(r'tag\s+', l):
2325 isAnnotated = True
2326 elif re.match(r'\s*$', l):
2327 inHeader = False
2328 continue
2329 else:
2330 body.append(l)
2331
2332 if not isAnnotated:
2333 body = ["lightweight tag imported by git p4\n"]
2334
2335 # Create the label - use the same view as the client spec we are using
2336 clientSpec = getClientSpec()
2337
2338 labelTemplate = "Label: %s\n" % name
2339 labelTemplate += "Description:\n"
2340 for b in body:
2341 labelTemplate += "\t" + b + "\n"
2342 labelTemplate += "View:\n"
Kazuki Saitoh9d57c4a2013-08-30 19:02:06 +09002343 for depot_side in clientSpec.mappings:
2344 labelTemplate += "\t%s\n" % depot_side
Luke Diamand06804c72012-04-11 17:21:24 +02002345
Pete Wyckoffef739f02012-09-09 16:16:11 -04002346 if self.dry_run:
Luke Diamandf2606b12018-06-19 09:04:10 +01002347 print("Would create p4 label %s for tag" % name)
Pete Wyckoff728b7ad2012-09-09 16:16:12 -04002348 elif self.prepare_p4_only:
Luke Diamandf2606b12018-06-19 09:04:10 +01002349 print("Not creating p4 label %s for tag due to option" \
2350 " --prepare-p4-only" % name)
Pete Wyckoffef739f02012-09-09 16:16:11 -04002351 else:
2352 p4_write_pipe(["label", "-i"], labelTemplate)
Luke Diamand06804c72012-04-11 17:21:24 +02002353
Pete Wyckoffef739f02012-09-09 16:16:11 -04002354 # Use the label
2355 p4_system(["tag", "-l", name] +
Kazuki Saitoh9d57c4a2013-08-30 19:02:06 +09002356 ["%s@%s" % (depot_side, changelist) for depot_side in clientSpec.mappings])
Luke Diamand06804c72012-04-11 17:21:24 +02002357
Pete Wyckoffef739f02012-09-09 16:16:11 -04002358 if verbose:
Luke Diamandf2606b12018-06-19 09:04:10 +01002359 print("created p4 label for tag %s" % name)
Luke Diamand06804c72012-04-11 17:21:24 +02002360
Simon Hausmann4f5cf762007-03-19 22:25:17 +01002361 def run(self, args):
Simon Hausmannc9b50e62007-03-29 19:15:24 +02002362 if len(args) == 0:
2363 self.master = currentGitBranch()
Simon Hausmannc9b50e62007-03-29 19:15:24 +02002364 elif len(args) == 1:
2365 self.master = args[0]
Pete Wyckoff28755db2011-12-24 21:07:40 -05002366 if not branchExists(self.master):
2367 die("Branch %s does not exist" % self.master)
Simon Hausmannc9b50e62007-03-29 19:15:24 +02002368 else:
2369 return False
2370
Luke Diamand8cf422d2017-12-21 11:06:14 +00002371 for i in self.update_shelve:
2372 if i <= 0:
2373 sys.exit("invalid changelist %d" % i)
2374
Luke Diamand00ad6e32015-11-21 09:54:41 +00002375 if self.master:
2376 allowSubmit = gitConfig("git-p4.allowSubmit")
2377 if len(allowSubmit) > 0 and not self.master in allowSubmit.split(","):
2378 die("%s is not in git-p4.allowSubmit" % self.master)
Jing Xue4c2d5d72008-06-22 14:12:39 -04002379
Simon Hausmann27d2d812007-06-12 14:31:59 +02002380 [upstream, settings] = findUpstreamBranchPoint()
Simon Hausmannea99c3a2007-08-08 17:06:55 +02002381 self.depotPath = settings['depot-paths'][0]
Simon Hausmann27d2d812007-06-12 14:31:59 +02002382 if len(self.origin) == 0:
2383 self.origin = upstream
Simon Hausmanna3fdd572007-06-07 22:54:32 +02002384
Luke Diamand8cf422d2017-12-21 11:06:14 +00002385 if len(self.update_shelve) > 0:
Luke Diamand46c609e2016-12-02 22:43:19 +00002386 self.shelve = True
2387
Luke Diamand3ea2cfd2011-04-21 20:50:23 +01002388 if self.preserveUser:
2389 if not self.canChangeChangelists():
2390 die("Cannot preserve user names without p4 super-user or admin permissions")
2391
Pete Wyckoff6bbfd132012-09-09 16:16:13 -04002392 # if not set from the command line, try the config file
2393 if self.conflict_behavior is None:
2394 val = gitConfig("git-p4.conflict")
2395 if val:
2396 if val not in self.conflict_behavior_choices:
2397 die("Invalid value '%s' for config git-p4.conflict" % val)
2398 else:
2399 val = "ask"
2400 self.conflict_behavior = val
2401
Simon Hausmanna3fdd572007-06-07 22:54:32 +02002402 if self.verbose:
Luke Diamandf2606b12018-06-19 09:04:10 +01002403 print("Origin branch is " + self.origin)
Simon Hausmann95124972007-03-23 09:16:07 +01002404
Simon Hausmannea99c3a2007-08-08 17:06:55 +02002405 if len(self.depotPath) == 0:
Luke Diamandf2606b12018-06-19 09:04:10 +01002406 print("Internal error: cannot locate perforce depot path from existing branches")
Simon Hausmann95124972007-03-23 09:16:07 +01002407 sys.exit(128)
2408
Pete Wyckoff543987b2012-02-25 20:06:25 -05002409 self.useClientSpec = False
Pete Wyckoff0d609032013-01-26 22:11:24 -05002410 if gitConfigBool("git-p4.useclientspec"):
Pete Wyckoff543987b2012-02-25 20:06:25 -05002411 self.useClientSpec = True
2412 if self.useClientSpec:
2413 self.clientSpecDirs = getClientSpec()
Simon Hausmann95124972007-03-23 09:16:07 +01002414
Ville Skyttä2e3a16b2016-08-09 11:53:38 +03002415 # Check for the existence of P4 branches
Vitor Antunescd884102015-04-21 23:49:30 +01002416 branchesDetected = (len(p4BranchesInGit().keys()) > 1)
2417
2418 if self.useClientSpec and not branchesDetected:
Pete Wyckoff543987b2012-02-25 20:06:25 -05002419 # all files are relative to the client spec
2420 self.clientPath = getClientRoot()
2421 else:
2422 self.clientPath = p4Where(self.depotPath)
2423
2424 if self.clientPath == "":
2425 die("Error: Cannot locate perforce checkout of %s in client view" % self.depotPath)
Simon Hausmann95124972007-03-23 09:16:07 +01002426
Luke Diamandf2606b12018-06-19 09:04:10 +01002427 print("Perforce checkout for depot path %s located at %s" % (self.depotPath, self.clientPath))
Simon Hausmann7944f142007-05-21 11:04:26 +02002428 self.oldWorkingDirectory = os.getcwd()
Simon Hausmannc1b296b2007-05-20 16:55:05 +02002429
Gary Gibbons0591cfa2011-12-09 18:48:14 -05002430 # ensure the clientPath exists
Pete Wyckoff8d7ec362012-04-29 20:57:14 -04002431 new_client_dir = False
Gary Gibbons0591cfa2011-12-09 18:48:14 -05002432 if not os.path.exists(self.clientPath):
Pete Wyckoff8d7ec362012-04-29 20:57:14 -04002433 new_client_dir = True
Gary Gibbons0591cfa2011-12-09 18:48:14 -05002434 os.makedirs(self.clientPath)
2435
Miklós Fazekasbbd84862013-03-11 17:45:29 -04002436 chdir(self.clientPath, is_client_path=True)
Pete Wyckoffef739f02012-09-09 16:16:11 -04002437 if self.dry_run:
Luke Diamandf2606b12018-06-19 09:04:10 +01002438 print("Would synchronize p4 checkout in %s" % self.clientPath)
Pete Wyckoff8d7ec362012-04-29 20:57:14 -04002439 else:
Luke Diamandf2606b12018-06-19 09:04:10 +01002440 print("Synchronizing p4 checkout...")
Pete Wyckoffef739f02012-09-09 16:16:11 -04002441 if new_client_dir:
2442 # old one was destroyed, and maybe nobody told p4
2443 p4_sync("...", "-f")
2444 else:
2445 p4_sync("...")
Simon Hausmann4f5cf762007-03-19 22:25:17 +01002446 self.check()
Simon Hausmann4f5cf762007-03-19 22:25:17 +01002447
Simon Hausmann4c750c02008-02-19 09:37:16 +01002448 commits = []
Luke Diamand00ad6e32015-11-21 09:54:41 +00002449 if self.master:
Ævar Arnfjörð Bjarmason89f32a92018-05-10 12:43:00 +00002450 committish = self.master
Luke Diamand00ad6e32015-11-21 09:54:41 +00002451 else:
Ævar Arnfjörð Bjarmason89f32a92018-05-10 12:43:00 +00002452 committish = 'HEAD'
Luke Diamand00ad6e32015-11-21 09:54:41 +00002453
Romain Merlandf55b87c2018-06-01 09:46:14 +02002454 if self.commit != "":
2455 if self.commit.find("..") != -1:
2456 limits_ish = self.commit.split("..")
2457 for line in read_pipe_lines(["git", "rev-list", "--no-merges", "%s..%s" % (limits_ish[0], limits_ish[1])]):
2458 commits.append(line.strip())
2459 commits.reverse()
2460 else:
2461 commits.append(self.commit)
2462 else:
Junio C Hamanoe6388992018-06-18 10:18:41 -07002463 for line in read_pipe_lines(["git", "rev-list", "--no-merges", "%s..%s" % (self.origin, committish)]):
Romain Merlandf55b87c2018-06-01 09:46:14 +02002464 commits.append(line.strip())
2465 commits.reverse()
Simon Hausmann4f5cf762007-03-19 22:25:17 +01002466
Pete Wyckoff0d609032013-01-26 22:11:24 -05002467 if self.preserveUser or gitConfigBool("git-p4.skipUserNameCheck"):
Luke Diamand848de9c2011-05-13 20:46:00 +01002468 self.checkAuthorship = False
2469 else:
2470 self.checkAuthorship = True
2471
Luke Diamand3ea2cfd2011-04-21 20:50:23 +01002472 if self.preserveUser:
2473 self.checkValidP4Users(commits)
2474
Gary Gibbons84cb0002012-07-04 09:40:19 -04002475 #
2476 # Build up a set of options to be passed to diff when
2477 # submitting each commit to p4.
2478 #
2479 if self.detectRenames:
2480 # command-line -M arg
2481 self.diffOpts = "-M"
2482 else:
2483 # If not explicitly set check the config variable
2484 detectRenames = gitConfig("git-p4.detectRenames")
2485
2486 if detectRenames.lower() == "false" or detectRenames == "":
2487 self.diffOpts = ""
2488 elif detectRenames.lower() == "true":
2489 self.diffOpts = "-M"
2490 else:
2491 self.diffOpts = "-M%s" % detectRenames
2492
2493 # no command-line arg for -C or --find-copies-harder, just
2494 # config variables
2495 detectCopies = gitConfig("git-p4.detectCopies")
2496 if detectCopies.lower() == "false" or detectCopies == "":
2497 pass
2498 elif detectCopies.lower() == "true":
2499 self.diffOpts += " -C"
2500 else:
2501 self.diffOpts += " -C%s" % detectCopies
2502
Pete Wyckoff0d609032013-01-26 22:11:24 -05002503 if gitConfigBool("git-p4.detectCopiesHarder"):
Gary Gibbons84cb0002012-07-04 09:40:19 -04002504 self.diffOpts += " --find-copies-harder"
2505
Luke Diamand8cf422d2017-12-21 11:06:14 +00002506 num_shelves = len(self.update_shelve)
2507 if num_shelves > 0 and num_shelves != len(commits):
2508 sys.exit("number of commits (%d) must match number of shelved changelist (%d)" %
2509 (len(commits), num_shelves))
2510
Ben Keene4935c452020-02-11 18:58:01 +00002511 if not self.no_verify:
2512 try:
2513 if not run_git_hook("p4-pre-submit"):
2514 print("\nThe p4-pre-submit hook failed, aborting the submit.\n\nYou can skip " \
2515 "this pre-submission check by adding\nthe command line option '--no-verify', " \
2516 "however,\nthis will also skip the p4-changelist hook as well.")
2517 sys.exit(1)
2518 except Exception as e:
2519 print("\nThe p4-pre-submit hook failed, aborting the submit.\n\nThe hook failed "\
2520 "with the error '{0}'".format(e.message) )
Ben Keeneaa8b7662020-02-11 18:58:00 +00002521 sys.exit(1)
Chen Bin251c8c52018-07-27 21:22:22 +10002522
Pete Wyckoff7e5dd9f2012-09-09 16:16:05 -04002523 #
2524 # Apply the commits, one at a time. On failure, ask if should
2525 # continue to try the rest of the patches, or quit.
2526 #
Pete Wyckoffef739f02012-09-09 16:16:11 -04002527 if self.dry_run:
Luke Diamandf2606b12018-06-19 09:04:10 +01002528 print("Would apply")
Pete Wyckoff67b0fe22012-09-09 16:16:03 -04002529 applied = []
Pete Wyckoff7e5dd9f2012-09-09 16:16:05 -04002530 last = len(commits) - 1
2531 for i, commit in enumerate(commits):
Pete Wyckoffef739f02012-09-09 16:16:11 -04002532 if self.dry_run:
Luke Diamandf2606b12018-06-19 09:04:10 +01002533 print(" ", read_pipe(["git", "show", "-s",
2534 "--format=format:%h %s", commit]))
Pete Wyckoffef739f02012-09-09 16:16:11 -04002535 ok = True
2536 else:
2537 ok = self.applyCommit(commit)
Pete Wyckoff67b0fe22012-09-09 16:16:03 -04002538 if ok:
2539 applied.append(commit)
Ben Keene2dfdd702020-05-12 13:15:59 +00002540 if self.prepare_p4_only:
2541 if i < last:
2542 print("Processing only the first commit due to option" \
2543 " --prepare-p4-only")
Pete Wyckoff728b7ad2012-09-09 16:16:12 -04002544 break
Ben Keene2dfdd702020-05-12 13:15:59 +00002545 else:
Pete Wyckoff7e5dd9f2012-09-09 16:16:05 -04002546 if i < last:
Ben Keenee2aed5f2019-12-16 14:02:19 +00002547 # prompt for what to do, or use the option/variable
2548 if self.conflict_behavior == "ask":
2549 print("What do you want to do?")
2550 response = prompt("[s]kip this commit but apply the rest, or [q]uit? ")
2551 elif self.conflict_behavior == "skip":
2552 response = "s"
2553 elif self.conflict_behavior == "quit":
2554 response = "q"
2555 else:
2556 die("Unknown conflict_behavior '%s'" %
2557 self.conflict_behavior)
Simon Hausmann4f5cf762007-03-19 22:25:17 +01002558
Ben Keenee2aed5f2019-12-16 14:02:19 +00002559 if response == "s":
2560 print("Skipping this commit, but applying the rest")
2561 if response == "q":
2562 print("Quitting")
Pete Wyckoff7e5dd9f2012-09-09 16:16:05 -04002563 break
Simon Hausmann4f5cf762007-03-19 22:25:17 +01002564
Pete Wyckoff67b0fe22012-09-09 16:16:03 -04002565 chdir(self.oldWorkingDirectory)
Vinicius Kursancewb34fa572016-11-28 09:33:18 +00002566 shelved_applied = "shelved" if self.shelve else "applied"
Pete Wyckoffef739f02012-09-09 16:16:11 -04002567 if self.dry_run:
2568 pass
Pete Wyckoff728b7ad2012-09-09 16:16:12 -04002569 elif self.prepare_p4_only:
2570 pass
Pete Wyckoffef739f02012-09-09 16:16:11 -04002571 elif len(commits) == len(applied):
Luke Diamandf2606b12018-06-19 09:04:10 +01002572 print("All commits {0}!".format(shelved_applied))
Simon Hausmann14594f42007-08-22 09:07:15 +02002573
Simon Hausmann4c750c02008-02-19 09:37:16 +01002574 sync = P4Sync()
Pete Wyckoff44e8d262013-01-14 19:47:08 -05002575 if self.branch:
2576 sync.branch = self.branch
Luke Diamandb9d34db2018-06-08 21:32:44 +01002577 if self.disable_p4sync:
2578 sync.sync_origin_only()
2579 else:
2580 sync.run([])
Simon Hausmann14594f42007-08-22 09:07:15 +02002581
Luke Diamandb9d34db2018-06-08 21:32:44 +01002582 if not self.disable_rebase:
2583 rebase = P4Rebase()
2584 rebase.rebase()
Simon Hausmann4f5cf762007-03-19 22:25:17 +01002585
Pete Wyckoff67b0fe22012-09-09 16:16:03 -04002586 else:
2587 if len(applied) == 0:
Luke Diamandf2606b12018-06-19 09:04:10 +01002588 print("No commits {0}.".format(shelved_applied))
Pete Wyckoff67b0fe22012-09-09 16:16:03 -04002589 else:
Luke Diamandf2606b12018-06-19 09:04:10 +01002590 print("{0} only the commits marked with '*':".format(shelved_applied.capitalize()))
Pete Wyckoff67b0fe22012-09-09 16:16:03 -04002591 for c in commits:
2592 if c in applied:
2593 star = "*"
2594 else:
2595 star = " "
Luke Diamandf2606b12018-06-19 09:04:10 +01002596 print(star, read_pipe(["git", "show", "-s",
2597 "--format=format:%h %s", c]))
2598 print("You will have to do 'git p4 sync' and rebase.")
Pete Wyckoff67b0fe22012-09-09 16:16:03 -04002599
Pete Wyckoff0d609032013-01-26 22:11:24 -05002600 if gitConfigBool("git-p4.exportLabels"):
Luke Diamand06dcd152012-05-11 07:25:18 +01002601 self.exportLabels = True
Luke Diamand06804c72012-04-11 17:21:24 +02002602
2603 if self.exportLabels:
2604 p4Labels = getP4Labels(self.depotPath)
2605 gitTags = getGitTags()
2606
2607 missingGitTags = gitTags - p4Labels
2608 self.exportGitTags(missingGitTags)
2609
Ondřej Bílka98e023d2013-07-29 10:18:21 +02002610 # exit with error unless everything applied perfectly
Pete Wyckoff67b0fe22012-09-09 16:16:03 -04002611 if len(commits) != len(applied):
2612 sys.exit(1)
2613
Simon Hausmannb9847332007-03-20 20:54:23 +01002614 return True
2615
Pete Wyckoffecb7cf92012-01-02 18:05:53 -05002616class View(object):
2617 """Represent a p4 view ("p4 help views"), and map files in a
2618 repo according to the view."""
2619
Kazuki Saitoh9d57c4a2013-08-30 19:02:06 +09002620 def __init__(self, client_name):
Pete Wyckoffecb7cf92012-01-02 18:05:53 -05002621 self.mappings = []
Kazuki Saitoh9d57c4a2013-08-30 19:02:06 +09002622 self.client_prefix = "//%s/" % client_name
2623 # cache results of "p4 where" to lookup client file locations
2624 self.client_spec_path_cache = {}
Pete Wyckoffecb7cf92012-01-02 18:05:53 -05002625
2626 def append(self, view_line):
2627 """Parse a view line, splitting it into depot and client
Kazuki Saitoh9d57c4a2013-08-30 19:02:06 +09002628 sides. Append to self.mappings, preserving order. This
2629 is only needed for tag creation."""
Pete Wyckoffecb7cf92012-01-02 18:05:53 -05002630
2631 # Split the view line into exactly two words. P4 enforces
2632 # structure on these lines that simplifies this quite a bit.
2633 #
2634 # Either or both words may be double-quoted.
2635 # Single quotes do not matter.
2636 # Double-quote marks cannot occur inside the words.
2637 # A + or - prefix is also inside the quotes.
2638 # There are no quotes unless they contain a space.
2639 # The line is already white-space stripped.
2640 # The two words are separated by a single space.
2641 #
2642 if view_line[0] == '"':
2643 # First word is double quoted. Find its end.
2644 close_quote_index = view_line.find('"', 1)
2645 if close_quote_index <= 0:
2646 die("No first-word closing quote found: %s" % view_line)
2647 depot_side = view_line[1:close_quote_index]
2648 # skip closing quote and space
2649 rhs_index = close_quote_index + 1 + 1
2650 else:
2651 space_index = view_line.find(" ")
2652 if space_index <= 0:
2653 die("No word-splitting space found: %s" % view_line)
2654 depot_side = view_line[0:space_index]
2655 rhs_index = space_index + 1
2656
Pete Wyckoffecb7cf92012-01-02 18:05:53 -05002657 # prefix + means overlay on previous mapping
Pete Wyckoffecb7cf92012-01-02 18:05:53 -05002658 if depot_side.startswith("+"):
Pete Wyckoffecb7cf92012-01-02 18:05:53 -05002659 depot_side = depot_side[1:]
2660
Kazuki Saitoh9d57c4a2013-08-30 19:02:06 +09002661 # prefix - means exclude this path, leave out of mappings
Pete Wyckoffecb7cf92012-01-02 18:05:53 -05002662 exclude = False
2663 if depot_side.startswith("-"):
2664 exclude = True
2665 depot_side = depot_side[1:]
2666
Kazuki Saitoh9d57c4a2013-08-30 19:02:06 +09002667 if not exclude:
2668 self.mappings.append(depot_side)
2669
2670 def convert_client_path(self, clientFile):
2671 # chop off //client/ part to make it relative
Yang Zhaod38208a2019-12-13 15:52:40 -08002672 if not decode_path(clientFile).startswith(self.client_prefix):
Kazuki Saitoh9d57c4a2013-08-30 19:02:06 +09002673 die("No prefix '%s' on clientFile '%s'" %
2674 (self.client_prefix, clientFile))
2675 return clientFile[len(self.client_prefix):]
2676
2677 def update_client_spec_path_cache(self, files):
2678 """ Caching file paths by "p4 where" batch query """
2679
2680 # List depot file paths exclude that already cached
Yang Zhaod38208a2019-12-13 15:52:40 -08002681 fileArgs = [f['path'] for f in files if decode_path(f['path']) not in self.client_spec_path_cache]
Kazuki Saitoh9d57c4a2013-08-30 19:02:06 +09002682
2683 if len(fileArgs) == 0:
2684 return # All files in cache
2685
2686 where_result = p4CmdList(["-x", "-", "where"], stdin=fileArgs)
2687 for res in where_result:
2688 if "code" in res and res["code"] == "error":
2689 # assume error is "... file(s) not in client view"
2690 continue
2691 if "clientFile" not in res:
Pete Wyckoff20005442014-01-21 18:16:46 -05002692 die("No clientFile in 'p4 where' output")
Kazuki Saitoh9d57c4a2013-08-30 19:02:06 +09002693 if "unmap" in res:
2694 # it will list all of them, but only one not unmap-ped
2695 continue
Yang Zhaod38208a2019-12-13 15:52:40 -08002696 depot_path = decode_path(res['depotFile'])
Lars Schneidera0a50d82015-08-28 14:00:34 +02002697 if gitConfigBool("core.ignorecase"):
Yang Zhaod38208a2019-12-13 15:52:40 -08002698 depot_path = depot_path.lower()
2699 self.client_spec_path_cache[depot_path] = self.convert_client_path(res["clientFile"])
Kazuki Saitoh9d57c4a2013-08-30 19:02:06 +09002700
2701 # not found files or unmap files set to ""
2702 for depotFile in fileArgs:
Yang Zhaod38208a2019-12-13 15:52:40 -08002703 depotFile = decode_path(depotFile)
Lars Schneidera0a50d82015-08-28 14:00:34 +02002704 if gitConfigBool("core.ignorecase"):
2705 depotFile = depotFile.lower()
Kazuki Saitoh9d57c4a2013-08-30 19:02:06 +09002706 if depotFile not in self.client_spec_path_cache:
Yang Zhaod38208a2019-12-13 15:52:40 -08002707 self.client_spec_path_cache[depotFile] = b''
Pete Wyckoffecb7cf92012-01-02 18:05:53 -05002708
2709 def map_in_client(self, depot_path):
2710 """Return the relative location in the client where this
2711 depot file should live. Returns "" if the file should
2712 not be mapped in the client."""
2713
Lars Schneidera0a50d82015-08-28 14:00:34 +02002714 if gitConfigBool("core.ignorecase"):
2715 depot_path = depot_path.lower()
2716
Kazuki Saitoh9d57c4a2013-08-30 19:02:06 +09002717 if depot_path in self.client_spec_path_cache:
2718 return self.client_spec_path_cache[depot_path]
Pete Wyckoffecb7cf92012-01-02 18:05:53 -05002719
Kazuki Saitoh9d57c4a2013-08-30 19:02:06 +09002720 die( "Error: %s is not found in client spec path" % depot_path )
2721 return ""
Pete Wyckoffecb7cf92012-01-02 18:05:53 -05002722
Mazo, Andreyff8c50e2019-04-01 18:02:26 +00002723def cloneExcludeCallback(option, opt_str, value, parser):
2724 # prepend "/" because the first "/" was consumed as part of the option itself.
2725 # ("-//depot/A/..." becomes "/depot/A/..." after option parsing)
2726 parser.values.cloneExclude += ["/" + re.sub(r"\.\.\.$", "", value)]
2727
Luke Diamand3ea2cfd2011-04-21 20:50:23 +01002728class P4Sync(Command, P4UserMap):
Pete Wyckoff56c09342011-02-19 08:17:57 -05002729
Simon Hausmannb9847332007-03-20 20:54:23 +01002730 def __init__(self):
2731 Command.__init__(self)
Luke Diamand3ea2cfd2011-04-21 20:50:23 +01002732 P4UserMap.__init__(self)
Simon Hausmannb9847332007-03-20 20:54:23 +01002733 self.options = [
2734 optparse.make_option("--branch", dest="branch"),
2735 optparse.make_option("--detect-branches", dest="detectBranches", action="store_true"),
2736 optparse.make_option("--changesfile", dest="changesFile"),
2737 optparse.make_option("--silent", dest="silent", action="store_true"),
Simon Hausmannef48f902007-05-17 22:17:49 +02002738 optparse.make_option("--detect-labels", dest="detectLabels", action="store_true"),
Luke Diamand06804c72012-04-11 17:21:24 +02002739 optparse.make_option("--import-labels", dest="importLabels", action="store_true"),
Han-Wen Nienhuysd2c6dd32007-05-23 18:49:35 -03002740 optparse.make_option("--import-local", dest="importIntoRemotes", action="store_false",
2741 help="Import into refs/heads/ , not refs/remotes"),
Lex Spoon96b2d542015-04-20 11:00:20 -04002742 optparse.make_option("--max-changes", dest="maxChanges",
2743 help="Maximum number of changes to import"),
2744 optparse.make_option("--changes-block-size", dest="changes_block_size", type="int",
2745 help="Internal block size to use when iteratively calling p4 changes"),
Han-Wen Nienhuys86dff6b2007-05-23 18:49:35 -03002746 optparse.make_option("--keep-path", dest="keepRepoPath", action='store_true',
Tor Arvid Lund3a70cdf2008-02-18 15:22:08 +01002747 help="Keep entire BRANCH/DIR/SUBDIR prefix during import"),
2748 optparse.make_option("--use-client-spec", dest="useClientSpec", action='store_true',
Luke Diamand51334bb2015-01-17 20:56:38 +00002749 help="Only sync files that are included in the Perforce Client Spec"),
2750 optparse.make_option("-/", dest="cloneExclude",
Mazo, Andreyff8c50e2019-04-01 18:02:26 +00002751 action="callback", callback=cloneExcludeCallback, type="string",
Luke Diamand51334bb2015-01-17 20:56:38 +00002752 help="exclude depot path"),
Simon Hausmannb9847332007-03-20 20:54:23 +01002753 ]
2754 self.description = """Imports from Perforce into a git repository.\n
2755 example:
2756 //depot/my/project/ -- to import the current head
2757 //depot/my/project/@all -- to import everything
2758 //depot/my/project/@1,6 -- to import only from revision 1 to 6
2759
2760 (a ... is not needed in the path p4 specification, it's added implicitly)"""
2761
2762 self.usage += " //depot/path[@revRange]"
Simon Hausmannb9847332007-03-20 20:54:23 +01002763 self.silent = False
Reilly Grant1d7367d2009-09-10 00:02:38 -07002764 self.createdBranches = set()
2765 self.committedChanges = set()
Simon Hausmann569d1bd2007-03-22 21:34:16 +01002766 self.branch = ""
Simon Hausmannb9847332007-03-20 20:54:23 +01002767 self.detectBranches = False
Simon Hausmanncb53e1f2007-04-08 00:12:02 +02002768 self.detectLabels = False
Luke Diamand06804c72012-04-11 17:21:24 +02002769 self.importLabels = False
Simon Hausmannb9847332007-03-20 20:54:23 +01002770 self.changesFile = ""
Simon Hausmann01265102007-05-25 10:36:10 +02002771 self.syncWithOrigin = True
Simon Hausmanna028a982007-05-23 00:03:08 +02002772 self.importIntoRemotes = True
Simon Hausmann01a9c9c2007-05-23 00:07:35 +02002773 self.maxChanges = ""
Luke Diamand1051ef02015-06-10 08:30:59 +01002774 self.changes_block_size = None
Han-Wen Nienhuys8b41a972007-05-23 18:20:53 -03002775 self.keepRepoPath = False
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03002776 self.depotPaths = None
Simon Hausmann3c699642007-06-16 13:09:21 +02002777 self.p4BranchesInGit = []
Tommy Thorn354081d2008-02-03 10:38:51 -08002778 self.cloneExclude = []
Tor Arvid Lund3a70cdf2008-02-18 15:22:08 +01002779 self.useClientSpec = False
Pete Wyckoffa93d33e2012-02-25 20:06:24 -05002780 self.useClientSpec_from_options = False
Pete Wyckoffecb7cf92012-01-02 18:05:53 -05002781 self.clientSpecDirs = None
Vitor Antunesfed23692012-01-25 23:48:22 +00002782 self.tempBranches = []
Lars Schneiderd6041762016-06-29 09:35:27 +02002783 self.tempBranchLocation = "refs/git-p4-tmp"
Lars Schneidera5db4b12015-09-26 09:55:03 +02002784 self.largeFileSystem = None
Luke Diamand123f6312018-05-23 23:20:26 +01002785 self.suppress_meta_comment = False
Lars Schneidera5db4b12015-09-26 09:55:03 +02002786
2787 if gitConfig('git-p4.largeFileSystem'):
2788 largeFileSystemConstructor = globals()[gitConfig('git-p4.largeFileSystem')]
2789 self.largeFileSystem = largeFileSystemConstructor(
2790 lambda git_mode, relPath, contents: self.writeToGitStream(git_mode, relPath, contents)
2791 )
Simon Hausmannb9847332007-03-20 20:54:23 +01002792
Simon Hausmann01265102007-05-25 10:36:10 +02002793 if gitConfig("git-p4.syncFromOrigin") == "false":
2794 self.syncWithOrigin = False
2795
Luke Diamand123f6312018-05-23 23:20:26 +01002796 self.depotPaths = []
2797 self.changeRange = ""
2798 self.previousDepotPaths = []
2799 self.hasOrigin = False
2800
2801 # map from branch depot path to parent branch
2802 self.knownBranches = {}
2803 self.initialParents = {}
2804
2805 self.tz = "%+03d%02d" % (- time.timezone / 3600, ((- time.timezone % 3600) / 60))
2806 self.labels = {}
2807
Vitor Antunesfed23692012-01-25 23:48:22 +00002808 # Force a checkpoint in fast-import and wait for it to finish
2809 def checkpoint(self):
2810 self.gitStream.write("checkpoint\n\n")
2811 self.gitStream.write("progress checkpoint\n\n")
Yang Zhao4294d742019-12-13 15:52:43 -08002812 self.gitStream.flush()
Vitor Antunesfed23692012-01-25 23:48:22 +00002813 out = self.gitOutput.readline()
2814 if self.verbose:
Luke Diamandf2606b12018-06-19 09:04:10 +01002815 print("checkpoint finished: " + out)
Vitor Antunesfed23692012-01-25 23:48:22 +00002816
Mazo, Andreya2bee102019-04-01 18:02:32 +00002817 def isPathWanted(self, path):
2818 for p in self.cloneExclude:
2819 if p.endswith("/"):
2820 if p4PathStartsWith(path, p):
2821 return False
2822 # "-//depot/file1" without a trailing "/" should only exclude "file1", but not "file111" or "file1_dir/file2"
2823 elif path.lower() == p.lower():
2824 return False
2825 for p in self.depotPaths:
Yang Zhaod38208a2019-12-13 15:52:40 -08002826 if p4PathStartsWith(path, decode_path(p)):
Mazo, Andreya2bee102019-04-01 18:02:32 +00002827 return True
2828 return False
2829
Luke Diamand89143ac2018-10-15 12:14:08 +01002830 def extractFilesFromCommit(self, commit, shelved=False, shelved_cl = 0):
Simon Hausmannb9847332007-03-20 20:54:23 +01002831 files = []
2832 fnum = 0
Luke Diamanddba1c9d2018-06-19 09:04:07 +01002833 while "depotFile%s" % fnum in commit:
Simon Hausmannb9847332007-03-20 20:54:23 +01002834 path = commit["depotFile%s" % fnum]
Yang Zhaod38208a2019-12-13 15:52:40 -08002835 found = self.isPathWanted(decode_path(path))
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03002836 if not found:
Simon Hausmannb9847332007-03-20 20:54:23 +01002837 fnum = fnum + 1
2838 continue
2839
2840 file = {}
2841 file["path"] = path
2842 file["rev"] = commit["rev%s" % fnum]
2843 file["action"] = commit["action%s" % fnum]
2844 file["type"] = commit["type%s" % fnum]
Luke Diamand123f6312018-05-23 23:20:26 +01002845 if shelved:
2846 file["shelved_cl"] = int(shelved_cl)
Simon Hausmannb9847332007-03-20 20:54:23 +01002847 files.append(file)
2848 fnum = fnum + 1
2849 return files
2850
Jan Durovec26e6a272016-04-19 19:49:41 +00002851 def extractJobsFromCommit(self, commit):
2852 jobs = []
2853 jnum = 0
Luke Diamanddba1c9d2018-06-19 09:04:07 +01002854 while "job%s" % jnum in commit:
Jan Durovec26e6a272016-04-19 19:49:41 +00002855 job = commit["job%s" % jnum]
2856 jobs.append(job)
2857 jnum = jnum + 1
2858 return jobs
2859
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03002860 def stripRepoPath(self, path, prefixes):
Pete Wyckoff21ef5df2012-08-11 12:55:04 -04002861 """When streaming files, this is called to map a p4 depot path
2862 to where it should go in git. The prefixes are either
2863 self.depotPaths, or self.branchPrefixes in the case of
2864 branch detection."""
2865
Ian Wienand39527102011-02-11 16:33:48 -08002866 if self.useClientSpec:
Pete Wyckoff21ef5df2012-08-11 12:55:04 -04002867 # branch detection moves files up a level (the branch name)
2868 # from what client spec interpretation gives
Yang Zhaod38208a2019-12-13 15:52:40 -08002869 path = decode_path(self.clientSpecDirs.map_in_client(path))
Pete Wyckoff21ef5df2012-08-11 12:55:04 -04002870 if self.detectBranches:
2871 for b in self.knownBranches:
Mazo, Andreyf2768cb2019-04-01 18:02:24 +00002872 if p4PathStartsWith(path, b + "/"):
Pete Wyckoff21ef5df2012-08-11 12:55:04 -04002873 path = path[len(b)+1:]
2874
2875 elif self.keepRepoPath:
2876 # Preserve everything in relative path name except leading
2877 # //depot/; just look at first prefix as they all should
2878 # be in the same depot.
2879 depot = re.sub("^(//[^/]+/).*", r'\1', prefixes[0])
2880 if p4PathStartsWith(path, depot):
2881 path = path[len(depot):]
Ian Wienand39527102011-02-11 16:33:48 -08002882
Pete Wyckoff0d1696e2012-08-11 12:55:03 -04002883 else:
Pete Wyckoff0d1696e2012-08-11 12:55:03 -04002884 for p in prefixes:
2885 if p4PathStartsWith(path, p):
2886 path = path[len(p):]
Pete Wyckoff21ef5df2012-08-11 12:55:04 -04002887 break
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03002888
Pete Wyckoff0d1696e2012-08-11 12:55:03 -04002889 path = wildcard_decode(path)
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03002890 return path
Han-Wen Nienhuys6754a292007-05-23 17:41:50 -03002891
Simon Hausmann71b112d2007-05-19 11:54:11 +02002892 def splitFilesIntoBranches(self, commit):
Pete Wyckoff21ef5df2012-08-11 12:55:04 -04002893 """Look at each depotFile in the commit to figure out to what
2894 branch it belongs."""
2895
Kazuki Saitoh9d57c4a2013-08-30 19:02:06 +09002896 if self.clientSpecDirs:
2897 files = self.extractFilesFromCommit(commit)
2898 self.clientSpecDirs.update_client_spec_path_cache(files)
2899
Simon Hausmannd5904672007-05-19 11:07:32 +02002900 branches = {}
Simon Hausmann71b112d2007-05-19 11:54:11 +02002901 fnum = 0
Luke Diamanddba1c9d2018-06-19 09:04:07 +01002902 while "depotFile%s" % fnum in commit:
Yang Zhaod38208a2019-12-13 15:52:40 -08002903 raw_path = commit["depotFile%s" % fnum]
2904 path = decode_path(raw_path)
Mazo, Andreyd15068a2019-04-01 18:02:38 +00002905 found = self.isPathWanted(path)
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03002906 if not found:
Simon Hausmann71b112d2007-05-19 11:54:11 +02002907 fnum = fnum + 1
2908 continue
2909
2910 file = {}
Yang Zhaod38208a2019-12-13 15:52:40 -08002911 file["path"] = raw_path
Simon Hausmann71b112d2007-05-19 11:54:11 +02002912 file["rev"] = commit["rev%s" % fnum]
2913 file["action"] = commit["action%s" % fnum]
2914 file["type"] = commit["type%s" % fnum]
2915 fnum = fnum + 1
2916
Pete Wyckoff21ef5df2012-08-11 12:55:04 -04002917 # start with the full relative path where this file would
2918 # go in a p4 client
2919 if self.useClientSpec:
Yang Zhaod38208a2019-12-13 15:52:40 -08002920 relPath = decode_path(self.clientSpecDirs.map_in_client(path))
Pete Wyckoff21ef5df2012-08-11 12:55:04 -04002921 else:
2922 relPath = self.stripRepoPath(path, self.depotPaths)
Simon Hausmannb9847332007-03-20 20:54:23 +01002923
Simon Hausmann4b97ffb2007-05-18 21:45:23 +02002924 for branch in self.knownBranches.keys():
Pete Wyckoff21ef5df2012-08-11 12:55:04 -04002925 # add a trailing slash so that a commit into qt/4.2foo
2926 # doesn't end up in qt/4.2, e.g.
Mazo, Andreyf2768cb2019-04-01 18:02:24 +00002927 if p4PathStartsWith(relPath, branch + "/"):
Simon Hausmannd5904672007-05-19 11:07:32 +02002928 if branch not in branches:
2929 branches[branch] = []
Simon Hausmann71b112d2007-05-19 11:54:11 +02002930 branches[branch].append(file)
Simon Hausmann6555b2c2007-06-17 11:25:34 +02002931 break
Simon Hausmannb9847332007-03-20 20:54:23 +01002932
2933 return branches
2934
Lars Schneidera5db4b12015-09-26 09:55:03 +02002935 def writeToGitStream(self, gitMode, relPath, contents):
Yang Zhao6cec21a2019-12-13 15:52:38 -08002936 self.gitStream.write(encode_text_stream(u'M {} inline {}\n'.format(gitMode, relPath)))
Lars Schneidera5db4b12015-09-26 09:55:03 +02002937 self.gitStream.write('data %d\n' % sum(len(d) for d in contents))
2938 for d in contents:
2939 self.gitStream.write(d)
2940 self.gitStream.write('\n')
2941
Lars Schneidera8b05162017-02-09 16:06:56 +01002942 def encodeWithUTF8(self, path):
2943 try:
2944 path.decode('ascii')
2945 except:
2946 encoding = 'utf8'
2947 if gitConfig('git-p4.pathEncoding'):
2948 encoding = gitConfig('git-p4.pathEncoding')
2949 path = path.decode(encoding, 'replace').encode('utf8', 'replace')
2950 if self.verbose:
Luke Diamandf2606b12018-06-19 09:04:10 +01002951 print('Path with non-ASCII characters detected. Used %s to encode: %s ' % (encoding, path))
Lars Schneidera8b05162017-02-09 16:06:56 +01002952 return path
2953
Luke Diamandb9327052009-07-30 00:13:46 +01002954 # output one file from the P4 stream
2955 # - helper for streamP4Files
2956
2957 def streamOneP4File(self, file, contents):
Yang Zhaod38208a2019-12-13 15:52:40 -08002958 file_path = file['depotFile']
2959 relPath = self.stripRepoPath(decode_path(file_path), self.branchPrefixes)
2960
Luke Diamandb9327052009-07-30 00:13:46 +01002961 if verbose:
Luke Diamand0742b7c2018-10-12 06:28:31 +01002962 if 'fileSize' in self.stream_file:
2963 size = int(self.stream_file['fileSize'])
2964 else:
2965 size = 0 # deleted files don't get a fileSize apparently
Yang Zhaod38208a2019-12-13 15:52:40 -08002966 sys.stdout.write('\r%s --> %s (%i MB)\n' % (file_path, relPath, size/1024/1024))
Lars Schneiderd2176a52015-09-26 09:55:01 +02002967 sys.stdout.flush()
Luke Diamandb9327052009-07-30 00:13:46 +01002968
Pete Wyckoff9cffb8c2011-10-16 10:45:01 -04002969 (type_base, type_mods) = split_p4_type(file["type"])
2970
2971 git_mode = "100644"
2972 if "x" in type_mods:
2973 git_mode = "100755"
2974 if type_base == "symlink":
2975 git_mode = "120000"
Alexandru Juncu1292df12013-08-08 16:17:38 +03002976 # p4 print on a symlink sometimes contains "target\n";
2977 # if it does, remove the newline
Yang Zhao6cec21a2019-12-13 15:52:38 -08002978 data = ''.join(decode_text_stream(c) for c in contents)
Pete Wyckoff40f846c2014-01-21 18:16:40 -05002979 if not data:
2980 # Some version of p4 allowed creating a symlink that pointed
2981 # to nothing. This causes p4 errors when checking out such
2982 # a change, and errors here too. Work around it by ignoring
2983 # the bad symlink; hopefully a future change fixes it.
Yang Zhaod38208a2019-12-13 15:52:40 -08002984 print("\nIgnoring empty symlink in %s" % file_path)
Pete Wyckoff40f846c2014-01-21 18:16:40 -05002985 return
2986 elif data[-1] == '\n':
Alexandru Juncu1292df12013-08-08 16:17:38 +03002987 contents = [data[:-1]]
2988 else:
2989 contents = [data]
Luke Diamandb9327052009-07-30 00:13:46 +01002990
Pete Wyckoff9cffb8c2011-10-16 10:45:01 -04002991 if type_base == "utf16":
Pete Wyckoff55aa5712011-09-17 19:16:14 -04002992 # p4 delivers different text in the python output to -G
2993 # than it does when using "print -o", or normal p4 client
2994 # operations. utf16 is converted to ascii or utf8, perhaps.
2995 # But ascii text saved as -t utf16 is completely mangled.
2996 # Invoke print -o to get the real contents.
Pete Wyckoff7f0e5962013-01-26 22:11:13 -05002997 #
2998 # On windows, the newlines will always be mangled by print, so put
2999 # them back too. This is not needed to the cygwin windows version,
3000 # just the native "NT" type.
3001 #
Lars Schneider1f5f3902015-09-21 12:01:41 +02003002 try:
Yang Zhaod38208a2019-12-13 15:52:40 -08003003 text = p4_read_pipe(['print', '-q', '-o', '-', '%s@%s' % (decode_path(file['depotFile']), file['change'])], raw=True)
Lars Schneider1f5f3902015-09-21 12:01:41 +02003004 except Exception as e:
3005 if 'Translation of file content failed' in str(e):
3006 type_base = 'binary'
3007 else:
3008 raise e
3009 else:
3010 if p4_version_string().find('/NT') >= 0:
Yang Zhaod38208a2019-12-13 15:52:40 -08003011 text = text.replace(b'\r\n', b'\n')
Lars Schneider1f5f3902015-09-21 12:01:41 +02003012 contents = [ text ]
Pete Wyckoff55aa5712011-09-17 19:16:14 -04003013
Pete Wyckoff9f7ef0e2011-11-05 13:36:07 -04003014 if type_base == "apple":
3015 # Apple filetype files will be streamed as a concatenation of
3016 # its appledouble header and the contents. This is useless
3017 # on both macs and non-macs. If using "print -q -o xx", it
3018 # will create "xx" with the data, and "%xx" with the header.
3019 # This is also not very useful.
3020 #
3021 # Ideally, someday, this script can learn how to generate
3022 # appledouble files directly and import those to git, but
3023 # non-mac machines can never find a use for apple filetype.
Luke Diamandf2606b12018-06-19 09:04:10 +01003024 print("\nIgnoring apple filetype file %s" % file['depotFile'])
Pete Wyckoff9f7ef0e2011-11-05 13:36:07 -04003025 return
3026
Pete Wyckoff55aa5712011-09-17 19:16:14 -04003027 # Note that we do not try to de-mangle keywords on utf16 files,
3028 # even though in theory somebody may want that.
Luke Diamand60df0712012-02-23 07:51:30 +00003029 pattern = p4_keywords_regexp_for_type(type_base, type_mods)
3030 if pattern:
3031 regexp = re.compile(pattern, re.VERBOSE)
Yang Zhao6cec21a2019-12-13 15:52:38 -08003032 text = ''.join(decode_text_stream(c) for c in contents)
Luke Diamand60df0712012-02-23 07:51:30 +00003033 text = regexp.sub(r'$\1$', text)
3034 contents = [ text ]
Luke Diamandb9327052009-07-30 00:13:46 +01003035
Lars Schneidera5db4b12015-09-26 09:55:03 +02003036 if self.largeFileSystem:
3037 (git_mode, contents) = self.largeFileSystem.processContent(git_mode, relPath, contents)
Luke Diamandb9327052009-07-30 00:13:46 +01003038
Lars Schneidera5db4b12015-09-26 09:55:03 +02003039 self.writeToGitStream(git_mode, relPath, contents)
Luke Diamandb9327052009-07-30 00:13:46 +01003040
3041 def streamOneP4Deletion(self, file):
Yang Zhaod38208a2019-12-13 15:52:40 -08003042 relPath = self.stripRepoPath(decode_path(file['path']), self.branchPrefixes)
Luke Diamandb9327052009-07-30 00:13:46 +01003043 if verbose:
Lars Schneiderd2176a52015-09-26 09:55:01 +02003044 sys.stdout.write("delete %s\n" % relPath)
3045 sys.stdout.flush()
Yang Zhao6cec21a2019-12-13 15:52:38 -08003046 self.gitStream.write(encode_text_stream(u'D {}\n'.format(relPath)))
Luke Diamandb9327052009-07-30 00:13:46 +01003047
Lars Schneidera5db4b12015-09-26 09:55:03 +02003048 if self.largeFileSystem and self.largeFileSystem.isLargeFile(relPath):
3049 self.largeFileSystem.removeLargeFile(relPath)
3050
Luke Diamandb9327052009-07-30 00:13:46 +01003051 # handle another chunk of streaming data
3052 def streamP4FilesCb(self, marshalled):
3053
Pete Wyckoff78189be2012-11-23 17:35:36 -05003054 # catch p4 errors and complain
3055 err = None
3056 if "code" in marshalled:
3057 if marshalled["code"] == "error":
3058 if "data" in marshalled:
3059 err = marshalled["data"].rstrip()
Lars Schneider4d25dc42015-09-26 09:55:02 +02003060
3061 if not err and 'fileSize' in self.stream_file:
3062 required_bytes = int((4 * int(self.stream_file["fileSize"])) - calcDiskFree())
3063 if required_bytes > 0:
3064 err = 'Not enough space left on %s! Free at least %i MB.' % (
3065 os.getcwd(), required_bytes/1024/1024
3066 )
3067
Pete Wyckoff78189be2012-11-23 17:35:36 -05003068 if err:
3069 f = None
3070 if self.stream_have_file_info:
3071 if "depotFile" in self.stream_file:
3072 f = self.stream_file["depotFile"]
3073 # force a failure in fast-import, else an empty
3074 # commit will be made
3075 self.gitStream.write("\n")
3076 self.gitStream.write("die-now\n")
3077 self.gitStream.close()
3078 # ignore errors, but make sure it exits first
3079 self.importProcess.wait()
3080 if f:
3081 die("Error from p4 print for %s: %s" % (f, err))
3082 else:
3083 die("Error from p4 print: %s" % err)
3084
Luke Diamanddba1c9d2018-06-19 09:04:07 +01003085 if 'depotFile' in marshalled and self.stream_have_file_info:
Andrew Garberc3f61632011-04-07 02:01:21 -04003086 # start of a new file - output the old one first
3087 self.streamOneP4File(self.stream_file, self.stream_contents)
3088 self.stream_file = {}
3089 self.stream_contents = []
3090 self.stream_have_file_info = False
Luke Diamandb9327052009-07-30 00:13:46 +01003091
Andrew Garberc3f61632011-04-07 02:01:21 -04003092 # pick up the new file information... for the
3093 # 'data' field we need to append to our array
3094 for k in marshalled.keys():
3095 if k == 'data':
Lars Schneiderd2176a52015-09-26 09:55:01 +02003096 if 'streamContentSize' not in self.stream_file:
3097 self.stream_file['streamContentSize'] = 0
3098 self.stream_file['streamContentSize'] += len(marshalled['data'])
Andrew Garberc3f61632011-04-07 02:01:21 -04003099 self.stream_contents.append(marshalled['data'])
3100 else:
3101 self.stream_file[k] = marshalled[k]
Luke Diamandb9327052009-07-30 00:13:46 +01003102
Lars Schneiderd2176a52015-09-26 09:55:01 +02003103 if (verbose and
3104 'streamContentSize' in self.stream_file and
3105 'fileSize' in self.stream_file and
3106 'depotFile' in self.stream_file):
3107 size = int(self.stream_file["fileSize"])
3108 if size > 0:
3109 progress = 100*self.stream_file['streamContentSize']/size
3110 sys.stdout.write('\r%s %d%% (%i MB)' % (self.stream_file['depotFile'], progress, int(size/1024/1024)))
3111 sys.stdout.flush()
3112
Andrew Garberc3f61632011-04-07 02:01:21 -04003113 self.stream_have_file_info = True
Luke Diamandb9327052009-07-30 00:13:46 +01003114
3115 # Stream directly from "p4 files" into "git fast-import"
3116 def streamP4Files(self, files):
Simon Hausmann30b59402008-03-03 11:55:48 +01003117 filesForCommit = []
3118 filesToRead = []
Luke Diamandb9327052009-07-30 00:13:46 +01003119 filesToDelete = []
Simon Hausmann30b59402008-03-03 11:55:48 +01003120
Tor Arvid Lund3a70cdf2008-02-18 15:22:08 +01003121 for f in files:
Pete Wyckoffecb7cf92012-01-02 18:05:53 -05003122 filesForCommit.append(f)
3123 if f['action'] in self.delete_actions:
3124 filesToDelete.append(f)
3125 else:
3126 filesToRead.append(f)
Han-Wen Nienhuys6a49f8e2007-05-23 18:49:35 -03003127
Luke Diamandb9327052009-07-30 00:13:46 +01003128 # deleted files...
3129 for f in filesToDelete:
3130 self.streamOneP4Deletion(f)
3131
Simon Hausmann30b59402008-03-03 11:55:48 +01003132 if len(filesToRead) > 0:
Luke Diamandb9327052009-07-30 00:13:46 +01003133 self.stream_file = {}
3134 self.stream_contents = []
3135 self.stream_have_file_info = False
3136
Andrew Garberc3f61632011-04-07 02:01:21 -04003137 # curry self argument
3138 def streamP4FilesCbSelf(entry):
3139 self.streamP4FilesCb(entry)
Luke Diamandb9327052009-07-30 00:13:46 +01003140
Luke Diamand123f6312018-05-23 23:20:26 +01003141 fileArgs = []
3142 for f in filesToRead:
3143 if 'shelved_cl' in f:
3144 # Handle shelved CLs using the "p4 print file@=N" syntax to print
3145 # the contents
Yang Zhao6cec21a2019-12-13 15:52:38 -08003146 fileArg = f['path'] + encode_text_stream('@={}'.format(f['shelved_cl']))
Luke Diamand123f6312018-05-23 23:20:26 +01003147 else:
Yang Zhao6cec21a2019-12-13 15:52:38 -08003148 fileArg = f['path'] + encode_text_stream('#{}'.format(f['rev']))
Luke Diamand123f6312018-05-23 23:20:26 +01003149
3150 fileArgs.append(fileArg)
Luke Diamand6de040d2011-10-16 10:47:52 -04003151
3152 p4CmdList(["-x", "-", "print"],
3153 stdin=fileArgs,
3154 cb=streamP4FilesCbSelf)
Han-Wen Nienhuysf2eda792007-05-23 18:49:35 -03003155
Luke Diamandb9327052009-07-30 00:13:46 +01003156 # do the last chunk
Luke Diamanddba1c9d2018-06-19 09:04:07 +01003157 if 'depotFile' in self.stream_file:
Luke Diamandb9327052009-07-30 00:13:46 +01003158 self.streamOneP4File(self.stream_file, self.stream_contents)
Han-Wen Nienhuys6a49f8e2007-05-23 18:49:35 -03003159
Luke Diamandaffb4742012-01-19 09:52:27 +00003160 def make_email(self, userid):
3161 if userid in self.users:
3162 return self.users[userid]
3163 else:
3164 return "%s <a@b>" % userid
3165
Luke Diamand06804c72012-04-11 17:21:24 +02003166 def streamTag(self, gitStream, labelName, labelDetails, commit, epoch):
Luke Diamandb43702a2015-08-27 08:18:58 +01003167 """ Stream a p4 tag.
3168 commit is either a git commit, or a fast-import mark, ":<p4commit>"
3169 """
3170
Luke Diamand06804c72012-04-11 17:21:24 +02003171 if verbose:
Luke Diamandf2606b12018-06-19 09:04:10 +01003172 print("writing tag %s for commit %s" % (labelName, commit))
Luke Diamand06804c72012-04-11 17:21:24 +02003173 gitStream.write("tag %s\n" % labelName)
3174 gitStream.write("from %s\n" % commit)
3175
Luke Diamanddba1c9d2018-06-19 09:04:07 +01003176 if 'Owner' in labelDetails:
Luke Diamand06804c72012-04-11 17:21:24 +02003177 owner = labelDetails["Owner"]
3178 else:
3179 owner = None
3180
3181 # Try to use the owner of the p4 label, or failing that,
3182 # the current p4 user id.
3183 if owner:
3184 email = self.make_email(owner)
3185 else:
3186 email = self.make_email(self.p4UserId())
3187 tagger = "%s %s %s" % (email, epoch, self.tz)
3188
3189 gitStream.write("tagger %s\n" % tagger)
3190
Luke Diamandf2606b12018-06-19 09:04:10 +01003191 print("labelDetails=",labelDetails)
Luke Diamanddba1c9d2018-06-19 09:04:07 +01003192 if 'Description' in labelDetails:
Luke Diamand06804c72012-04-11 17:21:24 +02003193 description = labelDetails['Description']
3194 else:
3195 description = 'Label from git p4'
3196
3197 gitStream.write("data %d\n" % len(description))
3198 gitStream.write(description)
3199 gitStream.write("\n")
3200
Lars Schneider4ae048e2015-12-08 10:36:22 +01003201 def inClientSpec(self, path):
3202 if not self.clientSpecDirs:
3203 return True
3204 inClientSpec = self.clientSpecDirs.map_in_client(path)
3205 if not inClientSpec and self.verbose:
3206 print('Ignoring file outside of client spec: {0}'.format(path))
3207 return inClientSpec
3208
3209 def hasBranchPrefix(self, path):
3210 if not self.branchPrefixes:
3211 return True
3212 hasPrefix = [p for p in self.branchPrefixes
3213 if p4PathStartsWith(path, p)]
Andrew Oakley09667d02016-06-22 10:26:11 +01003214 if not hasPrefix and self.verbose:
Lars Schneider4ae048e2015-12-08 10:36:22 +01003215 print('Ignoring file outside of prefix: {0}'.format(path))
3216 return hasPrefix
3217
Andrew Oakley82e46d62020-05-10 11:16:50 +01003218 def findShadowedFiles(self, files, change):
3219 # Perforce allows you commit files and directories with the same name,
3220 # so you could have files //depot/foo and //depot/foo/bar both checked
3221 # in. A p4 sync of a repository in this state fails. Deleting one of
3222 # the files recovers the repository.
3223 #
3224 # Git will not allow the broken state to exist and only the most recent
3225 # of the conflicting names is left in the repository. When one of the
3226 # conflicting files is deleted we need to re-add the other one to make
3227 # sure the git repository recovers in the same way as perforce.
3228 deleted = [f for f in files if f['action'] in self.delete_actions]
3229 to_check = set()
3230 for f in deleted:
3231 path = decode_path(f['path'])
3232 to_check.add(path + '/...')
3233 while True:
3234 path = path.rsplit("/", 1)[0]
3235 if path == "/" or path in to_check:
3236 break
3237 to_check.add(path)
3238 to_check = ['%s@%s' % (wildcard_encode(p), change) for p in to_check
3239 if self.hasBranchPrefix(p)]
3240 if to_check:
3241 stat_result = p4CmdList(["-x", "-", "fstat", "-T",
3242 "depotFile,headAction,headRev,headType"], stdin=to_check)
3243 for record in stat_result:
3244 if record['code'] != 'stat':
3245 continue
3246 if record['headAction'] in self.delete_actions:
3247 continue
3248 files.append({
3249 'action': 'add',
3250 'path': record['depotFile'],
3251 'rev': record['headRev'],
3252 'type': record['headType']})
3253
Luke Diamand89143ac2018-10-15 12:14:08 +01003254 def commit(self, details, files, branch, parent = "", allow_empty=False):
Simon Hausmannb9847332007-03-20 20:54:23 +01003255 epoch = details["time"]
3256 author = details["user"]
Jan Durovec26e6a272016-04-19 19:49:41 +00003257 jobs = self.extractJobsFromCommit(details)
Simon Hausmannb9847332007-03-20 20:54:23 +01003258
Simon Hausmann4b97ffb2007-05-18 21:45:23 +02003259 if self.verbose:
Lars Schneider4ae048e2015-12-08 10:36:22 +01003260 print('commit into {0}'.format(branch))
Han-Wen Nienhuys96e07dd2007-05-23 18:49:35 -03003261
Andrew Oakley82e46d62020-05-10 11:16:50 +01003262 files = [f for f in files
3263 if self.hasBranchPrefix(decode_path(f['path']))]
3264 self.findShadowedFiles(files, details['change'])
3265
Kazuki Saitoh9d57c4a2013-08-30 19:02:06 +09003266 if self.clientSpecDirs:
3267 self.clientSpecDirs.update_client_spec_path_cache(files)
3268
Andrew Oakley82e46d62020-05-10 11:16:50 +01003269 files = [f for f in files if self.inClientSpec(decode_path(f['path']))]
Lars Schneider4ae048e2015-12-08 10:36:22 +01003270
Luke Diamand89143ac2018-10-15 12:14:08 +01003271 if gitConfigBool('git-p4.keepEmptyCommits'):
3272 allow_empty = True
3273
3274 if not files and not allow_empty:
Lars Schneider4ae048e2015-12-08 10:36:22 +01003275 print('Ignoring revision {0} as it would produce an empty commit.'
3276 .format(details['change']))
3277 return
3278
Simon Hausmannb9847332007-03-20 20:54:23 +01003279 self.gitStream.write("commit %s\n" % branch)
Luke Diamandb43702a2015-08-27 08:18:58 +01003280 self.gitStream.write("mark :%s\n" % details["change"])
Simon Hausmannb9847332007-03-20 20:54:23 +01003281 self.committedChanges.add(int(details["change"]))
3282 committer = ""
Simon Hausmannb607e712007-05-20 10:55:54 +02003283 if author not in self.users:
3284 self.getUserMapFromPerforceServer()
Luke Diamandaffb4742012-01-19 09:52:27 +00003285 committer = "%s %s %s" % (self.make_email(author), epoch, self.tz)
Simon Hausmannb9847332007-03-20 20:54:23 +01003286
3287 self.gitStream.write("committer %s\n" % committer)
3288
3289 self.gitStream.write("data <<EOT\n")
3290 self.gitStream.write(details["desc"])
Jan Durovec26e6a272016-04-19 19:49:41 +00003291 if len(jobs) > 0:
3292 self.gitStream.write("\nJobs: %s" % (' '.join(jobs)))
Luke Diamand123f6312018-05-23 23:20:26 +01003293
3294 if not self.suppress_meta_comment:
3295 self.gitStream.write("\n[git-p4: depot-paths = \"%s\": change = %s" %
3296 (','.join(self.branchPrefixes), details["change"]))
3297 if len(details['options']) > 0:
3298 self.gitStream.write(": options = %s" % details['options'])
3299 self.gitStream.write("]\n")
3300
3301 self.gitStream.write("EOT\n\n")
Simon Hausmannb9847332007-03-20 20:54:23 +01003302
3303 if len(parent) > 0:
Simon Hausmann4b97ffb2007-05-18 21:45:23 +02003304 if self.verbose:
Luke Diamandf2606b12018-06-19 09:04:10 +01003305 print("parent %s" % parent)
Simon Hausmannb9847332007-03-20 20:54:23 +01003306 self.gitStream.write("from %s\n" % parent)
3307
Lars Schneider4ae048e2015-12-08 10:36:22 +01003308 self.streamP4Files(files)
Simon Hausmannb9847332007-03-20 20:54:23 +01003309 self.gitStream.write("\n")
3310
Simon Hausmann1f4ba1c2007-03-26 22:34:34 +02003311 change = int(details["change"])
3312
Luke Diamanddba1c9d2018-06-19 09:04:07 +01003313 if change in self.labels:
Simon Hausmann1f4ba1c2007-03-26 22:34:34 +02003314 label = self.labels[change]
3315 labelDetails = label[0]
3316 labelRevisions = label[1]
Simon Hausmann71b112d2007-05-19 11:54:11 +02003317 if self.verbose:
Luke Diamandf2606b12018-06-19 09:04:10 +01003318 print("Change %s is labelled %s" % (change, labelDetails))
Simon Hausmann1f4ba1c2007-03-26 22:34:34 +02003319
Luke Diamand6de040d2011-10-16 10:47:52 -04003320 files = p4CmdList(["files"] + ["%s...@%s" % (p, change)
Pete Wyckoffe63231e2012-08-11 12:55:02 -04003321 for p in self.branchPrefixes])
Simon Hausmann1f4ba1c2007-03-26 22:34:34 +02003322
3323 if len(files) == len(labelRevisions):
3324
3325 cleanedFiles = {}
3326 for info in files:
Pete Wyckoff56c09342011-02-19 08:17:57 -05003327 if info["action"] in self.delete_actions:
Simon Hausmann1f4ba1c2007-03-26 22:34:34 +02003328 continue
3329 cleanedFiles[info["depotFile"]] = info["rev"]
3330
3331 if cleanedFiles == labelRevisions:
Luke Diamand06804c72012-04-11 17:21:24 +02003332 self.streamTag(self.gitStream, 'tag_%s' % labelDetails['label'], labelDetails, branch, epoch)
Simon Hausmann1f4ba1c2007-03-26 22:34:34 +02003333
3334 else:
Simon Hausmanna46668f2007-03-28 17:05:38 +02003335 if not self.silent:
Luke Diamandf2606b12018-06-19 09:04:10 +01003336 print("Tag %s does not match with change %s: files do not match."
Han-Wen Nienhuyscebdf5a2007-05-23 16:53:11 -03003337 % (labelDetails["label"], change))
Simon Hausmann1f4ba1c2007-03-26 22:34:34 +02003338
3339 else:
Simon Hausmanna46668f2007-03-28 17:05:38 +02003340 if not self.silent:
Luke Diamandf2606b12018-06-19 09:04:10 +01003341 print("Tag %s does not match with change %s: file count is different."
Han-Wen Nienhuyscebdf5a2007-05-23 16:53:11 -03003342 % (labelDetails["label"], change))
Simon Hausmannb9847332007-03-20 20:54:23 +01003343
Luke Diamand06804c72012-04-11 17:21:24 +02003344 # Build a dictionary of changelists and labels, for "detect-labels" option.
Simon Hausmann1f4ba1c2007-03-26 22:34:34 +02003345 def getLabels(self):
3346 self.labels = {}
3347
Luke Diamand52a48802012-01-19 09:52:25 +00003348 l = p4CmdList(["labels"] + ["%s..." % p for p in self.depotPaths])
Simon Hausmann10c32112007-04-08 10:15:47 +02003349 if len(l) > 0 and not self.silent:
Luke Diamand4d885192018-06-19 09:04:08 +01003350 print("Finding files belonging to labels in %s" % self.depotPaths)
Simon Hausmann01ce1fe2007-04-07 23:46:50 +02003351
3352 for output in l:
Simon Hausmann1f4ba1c2007-03-26 22:34:34 +02003353 label = output["label"]
3354 revisions = {}
3355 newestChange = 0
Simon Hausmann71b112d2007-05-19 11:54:11 +02003356 if self.verbose:
Luke Diamandf2606b12018-06-19 09:04:10 +01003357 print("Querying files for label %s" % label)
Luke Diamand6de040d2011-10-16 10:47:52 -04003358 for file in p4CmdList(["files"] +
3359 ["%s...@%s" % (p, label)
3360 for p in self.depotPaths]):
Simon Hausmann1f4ba1c2007-03-26 22:34:34 +02003361 revisions[file["depotFile"]] = file["rev"]
3362 change = int(file["change"])
3363 if change > newestChange:
3364 newestChange = change
3365
Simon Hausmann9bda3a82007-05-19 12:05:40 +02003366 self.labels[newestChange] = [output, revisions]
3367
3368 if self.verbose:
Luke Diamandf2606b12018-06-19 09:04:10 +01003369 print("Label changes: %s" % self.labels.keys())
Simon Hausmann1f4ba1c2007-03-26 22:34:34 +02003370
Luke Diamand06804c72012-04-11 17:21:24 +02003371 # Import p4 labels as git tags. A direct mapping does not
3372 # exist, so assume that if all the files are at the same revision
3373 # then we can use that, or it's something more complicated we should
3374 # just ignore.
3375 def importP4Labels(self, stream, p4Labels):
3376 if verbose:
Luke Diamandf2606b12018-06-19 09:04:10 +01003377 print("import p4 labels: " + ' '.join(p4Labels))
Luke Diamand06804c72012-04-11 17:21:24 +02003378
3379 ignoredP4Labels = gitConfigList("git-p4.ignoredP4Labels")
Luke Diamandc8942a22012-04-11 17:21:24 +02003380 validLabelRegexp = gitConfig("git-p4.labelImportRegexp")
Luke Diamand06804c72012-04-11 17:21:24 +02003381 if len(validLabelRegexp) == 0:
3382 validLabelRegexp = defaultLabelRegexp
3383 m = re.compile(validLabelRegexp)
3384
3385 for name in p4Labels:
3386 commitFound = False
3387
3388 if not m.match(name):
3389 if verbose:
Luke Diamandf2606b12018-06-19 09:04:10 +01003390 print("label %s does not match regexp %s" % (name,validLabelRegexp))
Luke Diamand06804c72012-04-11 17:21:24 +02003391 continue
3392
3393 if name in ignoredP4Labels:
3394 continue
3395
3396 labelDetails = p4CmdList(['label', "-o", name])[0]
3397
3398 # get the most recent changelist for each file in this label
3399 change = p4Cmd(["changes", "-m", "1"] + ["%s...@%s" % (p, name)
3400 for p in self.depotPaths])
3401
Luke Diamanddba1c9d2018-06-19 09:04:07 +01003402 if 'change' in change:
Luke Diamand06804c72012-04-11 17:21:24 +02003403 # find the corresponding git commit; take the oldest commit
3404 changelist = int(change['change'])
Luke Diamandb43702a2015-08-27 08:18:58 +01003405 if changelist in self.committedChanges:
3406 gitCommit = ":%d" % changelist # use a fast-import mark
Luke Diamand06804c72012-04-11 17:21:24 +02003407 commitFound = True
Luke Diamandb43702a2015-08-27 08:18:58 +01003408 else:
3409 gitCommit = read_pipe(["git", "rev-list", "--max-count=1",
3410 "--reverse", ":/\[git-p4:.*change = %d\]" % changelist], ignore_error=True)
3411 if len(gitCommit) == 0:
Luke Diamandf2606b12018-06-19 09:04:10 +01003412 print("importing label %s: could not find git commit for changelist %d" % (name, changelist))
Luke Diamandb43702a2015-08-27 08:18:58 +01003413 else:
3414 commitFound = True
3415 gitCommit = gitCommit.strip()
3416
3417 if commitFound:
Luke Diamand06804c72012-04-11 17:21:24 +02003418 # Convert from p4 time format
3419 try:
3420 tmwhen = time.strptime(labelDetails['Update'], "%Y/%m/%d %H:%M:%S")
3421 except ValueError:
Luke Diamandf2606b12018-06-19 09:04:10 +01003422 print("Could not convert label time %s" % labelDetails['Update'])
Luke Diamand06804c72012-04-11 17:21:24 +02003423 tmwhen = 1
3424
3425 when = int(time.mktime(tmwhen))
3426 self.streamTag(stream, name, labelDetails, gitCommit, when)
3427 if verbose:
Luke Diamandf2606b12018-06-19 09:04:10 +01003428 print("p4 label %s mapped to git commit %s" % (name, gitCommit))
Luke Diamand06804c72012-04-11 17:21:24 +02003429 else:
3430 if verbose:
Luke Diamandf2606b12018-06-19 09:04:10 +01003431 print("Label %s has no changelists - possibly deleted?" % name)
Luke Diamand06804c72012-04-11 17:21:24 +02003432
3433 if not commitFound:
3434 # We can't import this label; don't try again as it will get very
3435 # expensive repeatedly fetching all the files for labels that will
3436 # never be imported. If the label is moved in the future, the
3437 # ignore will need to be removed manually.
3438 system(["git", "config", "--add", "git-p4.ignoredP4Labels", name])
3439
Han-Wen Nienhuys86dff6b2007-05-23 18:49:35 -03003440 def guessProjectName(self):
3441 for p in self.depotPaths:
Simon Hausmann6e5295c2007-06-11 08:50:57 +02003442 if p.endswith("/"):
3443 p = p[:-1]
3444 p = p[p.strip().rfind("/") + 1:]
3445 if not p.endswith("/"):
3446 p += "/"
3447 return p
Han-Wen Nienhuys86dff6b2007-05-23 18:49:35 -03003448
Simon Hausmann4b97ffb2007-05-18 21:45:23 +02003449 def getBranchMapping(self):
Simon Hausmann6555b2c2007-06-17 11:25:34 +02003450 lostAndFoundBranches = set()
3451
Vitor Antunes8ace74c2011-08-19 00:44:04 +01003452 user = gitConfig("git-p4.branchUser")
3453 if len(user) > 0:
3454 command = "branches -u %s" % user
3455 else:
3456 command = "branches"
3457
3458 for info in p4CmdList(command):
Luke Diamand52a48802012-01-19 09:52:25 +00003459 details = p4Cmd(["branch", "-o", info["branch"]])
Simon Hausmann4b97ffb2007-05-18 21:45:23 +02003460 viewIdx = 0
Luke Diamanddba1c9d2018-06-19 09:04:07 +01003461 while "View%s" % viewIdx in details:
Simon Hausmann4b97ffb2007-05-18 21:45:23 +02003462 paths = details["View%s" % viewIdx].split(" ")
3463 viewIdx = viewIdx + 1
3464 # require standard //depot/foo/... //depot/bar/... mapping
3465 if len(paths) != 2 or not paths[0].endswith("/...") or not paths[1].endswith("/..."):
3466 continue
3467 source = paths[0]
3468 destination = paths[1]
Simon Hausmann6509e192007-06-07 09:41:53 +02003469 ## HACK
Tor Arvid Lundd53de8b2011-03-15 13:08:02 +01003470 if p4PathStartsWith(source, self.depotPaths[0]) and p4PathStartsWith(destination, self.depotPaths[0]):
Simon Hausmann6509e192007-06-07 09:41:53 +02003471 source = source[len(self.depotPaths[0]):-4]
3472 destination = destination[len(self.depotPaths[0]):-4]
Simon Hausmann6555b2c2007-06-17 11:25:34 +02003473
Simon Hausmann1a2edf42007-06-17 15:10:24 +02003474 if destination in self.knownBranches:
3475 if not self.silent:
Luke Diamandf2606b12018-06-19 09:04:10 +01003476 print("p4 branch %s defines a mapping from %s to %s" % (info["branch"], source, destination))
3477 print("but there exists another mapping from %s to %s already!" % (self.knownBranches[destination], destination))
Simon Hausmann1a2edf42007-06-17 15:10:24 +02003478 continue
3479
Simon Hausmann6555b2c2007-06-17 11:25:34 +02003480 self.knownBranches[destination] = source
3481
3482 lostAndFoundBranches.discard(destination)
3483
Simon Hausmann29bdbac2007-05-19 10:23:12 +02003484 if source not in self.knownBranches:
Simon Hausmann6555b2c2007-06-17 11:25:34 +02003485 lostAndFoundBranches.add(source)
3486
Vitor Antunes7199cf12011-08-19 00:44:05 +01003487 # Perforce does not strictly require branches to be defined, so we also
3488 # check git config for a branch list.
3489 #
3490 # Example of branch definition in git config file:
3491 # [git-p4]
3492 # branchList=main:branchA
3493 # branchList=main:branchB
3494 # branchList=branchA:branchC
3495 configBranches = gitConfigList("git-p4.branchList")
3496 for branch in configBranches:
3497 if branch:
3498 (source, destination) = branch.split(":")
3499 self.knownBranches[destination] = source
3500
3501 lostAndFoundBranches.discard(destination)
3502
3503 if source not in self.knownBranches:
3504 lostAndFoundBranches.add(source)
3505
Simon Hausmann6555b2c2007-06-17 11:25:34 +02003506
3507 for branch in lostAndFoundBranches:
3508 self.knownBranches[branch] = branch
Simon Hausmann29bdbac2007-05-19 10:23:12 +02003509
Simon Hausmann38f9f5e2007-11-15 10:38:45 +01003510 def getBranchMappingFromGitBranches(self):
3511 branches = p4BranchesInGit(self.importIntoRemotes)
3512 for branch in branches.keys():
3513 if branch == "master":
3514 branch = "main"
3515 else:
3516 branch = branch[len(self.projectName):]
3517 self.knownBranches[branch] = branch
3518
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03003519 def updateOptionDict(self, d):
3520 option_keys = {}
3521 if self.keepRepoPath:
3522 option_keys['keepRepoPath'] = 1
3523
3524 d["options"] = ' '.join(sorted(option_keys.keys()))
3525
3526 def readOptions(self, d):
Luke Diamanddba1c9d2018-06-19 09:04:07 +01003527 self.keepRepoPath = ('options' in d
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03003528 and ('keepRepoPath' in d['options']))
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03003529
Simon Hausmann8134f692007-08-26 16:44:55 +02003530 def gitRefForBranch(self, branch):
3531 if branch == "main":
3532 return self.refPrefix + "master"
3533
3534 if len(branch) <= 0:
3535 return branch
3536
3537 return self.refPrefix + self.projectName + branch
3538
Simon Hausmann1ca3d712007-08-26 17:36:55 +02003539 def gitCommitByP4Change(self, ref, change):
3540 if self.verbose:
Luke Diamandf2606b12018-06-19 09:04:10 +01003541 print("looking in ref " + ref + " for change %s using bisect..." % change)
Simon Hausmann1ca3d712007-08-26 17:36:55 +02003542
3543 earliestCommit = ""
3544 latestCommit = parseRevision(ref)
3545
3546 while True:
3547 if self.verbose:
Luke Diamandf2606b12018-06-19 09:04:10 +01003548 print("trying: earliest %s latest %s" % (earliestCommit, latestCommit))
Simon Hausmann1ca3d712007-08-26 17:36:55 +02003549 next = read_pipe("git rev-list --bisect %s %s" % (latestCommit, earliestCommit)).strip()
3550 if len(next) == 0:
3551 if self.verbose:
Luke Diamandf2606b12018-06-19 09:04:10 +01003552 print("argh")
Simon Hausmann1ca3d712007-08-26 17:36:55 +02003553 return ""
3554 log = extractLogMessageFromGitCommit(next)
3555 settings = extractSettingsGitLog(log)
3556 currentChange = int(settings['change'])
3557 if self.verbose:
Luke Diamandf2606b12018-06-19 09:04:10 +01003558 print("current change %s" % currentChange)
Simon Hausmann1ca3d712007-08-26 17:36:55 +02003559
3560 if currentChange == change:
3561 if self.verbose:
Luke Diamandf2606b12018-06-19 09:04:10 +01003562 print("found %s" % next)
Simon Hausmann1ca3d712007-08-26 17:36:55 +02003563 return next
3564
3565 if currentChange < change:
3566 earliestCommit = "^%s" % next
3567 else:
Mazo, Andrey2dda7412019-04-01 18:02:17 +00003568 if next == latestCommit:
3569 die("Infinite loop while looking in ref %s for change %s. Check your branch mappings" % (ref, change))
3570 latestCommit = "%s^@" % next
Simon Hausmann1ca3d712007-08-26 17:36:55 +02003571
3572 return ""
3573
3574 def importNewBranch(self, branch, maxChange):
3575 # make fast-import flush all changes to disk and update the refs using the checkpoint
3576 # command so that we can try to find the branch parent in the git history
3577 self.gitStream.write("checkpoint\n\n");
3578 self.gitStream.flush();
3579 branchPrefix = self.depotPaths[0] + branch + "/"
3580 range = "@1,%s" % maxChange
3581 #print "prefix" + branchPrefix
Lex Spoon96b2d542015-04-20 11:00:20 -04003582 changes = p4ChangesForPaths([branchPrefix], range, self.changes_block_size)
Simon Hausmann1ca3d712007-08-26 17:36:55 +02003583 if len(changes) <= 0:
3584 return False
3585 firstChange = changes[0]
3586 #print "first change in branch: %s" % firstChange
3587 sourceBranch = self.knownBranches[branch]
3588 sourceDepotPath = self.depotPaths[0] + sourceBranch
3589 sourceRef = self.gitRefForBranch(sourceBranch)
3590 #print "source " + sourceBranch
3591
Luke Diamand52a48802012-01-19 09:52:25 +00003592 branchParentChange = int(p4Cmd(["changes", "-m", "1", "%s...@1,%s" % (sourceDepotPath, firstChange)])["change"])
Simon Hausmann1ca3d712007-08-26 17:36:55 +02003593 #print "branch parent: %s" % branchParentChange
3594 gitParent = self.gitCommitByP4Change(sourceRef, branchParentChange)
3595 if len(gitParent) > 0:
3596 self.initialParents[self.gitRefForBranch(branch)] = gitParent
3597 #print "parent git commit: %s" % gitParent
3598
3599 self.importChanges(changes)
3600 return True
3601
Vitor Antunesfed23692012-01-25 23:48:22 +00003602 def searchParent(self, parent, branch, target):
3603 parentFound = False
Pete Wyckoffc7d34882013-01-26 22:11:21 -05003604 for blob in read_pipe_lines(["git", "rev-list", "--reverse",
3605 "--no-merges", parent]):
Vitor Antunesfed23692012-01-25 23:48:22 +00003606 blob = blob.strip()
3607 if len(read_pipe(["git", "diff-tree", blob, target])) == 0:
3608 parentFound = True
3609 if self.verbose:
Luke Diamandf2606b12018-06-19 09:04:10 +01003610 print("Found parent of %s in commit %s" % (branch, blob))
Vitor Antunesfed23692012-01-25 23:48:22 +00003611 break
3612 if parentFound:
3613 return blob
3614 else:
3615 return None
3616
Luke Diamand89143ac2018-10-15 12:14:08 +01003617 def importChanges(self, changes, origin_revision=0):
Simon Hausmanne87f37a2007-08-26 16:00:52 +02003618 cnt = 1
3619 for change in changes:
Luke Diamand89143ac2018-10-15 12:14:08 +01003620 description = p4_describe(change)
Simon Hausmanne87f37a2007-08-26 16:00:52 +02003621 self.updateOptionDict(description)
3622
3623 if not self.silent:
3624 sys.stdout.write("\rImporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
3625 sys.stdout.flush()
3626 cnt = cnt + 1
3627
3628 try:
3629 if self.detectBranches:
3630 branches = self.splitFilesIntoBranches(description)
3631 for branch in branches.keys():
3632 ## HACK --hwn
3633 branchPrefix = self.depotPaths[0] + branch + "/"
Pete Wyckoffe63231e2012-08-11 12:55:02 -04003634 self.branchPrefixes = [ branchPrefix ]
Simon Hausmanne87f37a2007-08-26 16:00:52 +02003635
3636 parent = ""
3637
3638 filesForCommit = branches[branch]
3639
3640 if self.verbose:
Luke Diamandf2606b12018-06-19 09:04:10 +01003641 print("branch is %s" % branch)
Simon Hausmanne87f37a2007-08-26 16:00:52 +02003642
3643 self.updatedBranches.add(branch)
3644
3645 if branch not in self.createdBranches:
3646 self.createdBranches.add(branch)
3647 parent = self.knownBranches[branch]
3648 if parent == branch:
3649 parent = ""
Simon Hausmann1ca3d712007-08-26 17:36:55 +02003650 else:
3651 fullBranch = self.projectName + branch
3652 if fullBranch not in self.p4BranchesInGit:
3653 if not self.silent:
3654 print("\n Importing new branch %s" % fullBranch);
3655 if self.importNewBranch(branch, change - 1):
3656 parent = ""
3657 self.p4BranchesInGit.append(fullBranch)
3658 if not self.silent:
3659 print("\n Resuming with change %s" % change);
3660
3661 if self.verbose:
Luke Diamandf2606b12018-06-19 09:04:10 +01003662 print("parent determined through known branches: %s" % parent)
Simon Hausmanne87f37a2007-08-26 16:00:52 +02003663
Simon Hausmann8134f692007-08-26 16:44:55 +02003664 branch = self.gitRefForBranch(branch)
3665 parent = self.gitRefForBranch(parent)
Simon Hausmanne87f37a2007-08-26 16:00:52 +02003666
3667 if self.verbose:
Luke Diamandf2606b12018-06-19 09:04:10 +01003668 print("looking for initial parent for %s; current parent is %s" % (branch, parent))
Simon Hausmanne87f37a2007-08-26 16:00:52 +02003669
3670 if len(parent) == 0 and branch in self.initialParents:
3671 parent = self.initialParents[branch]
3672 del self.initialParents[branch]
3673
Vitor Antunesfed23692012-01-25 23:48:22 +00003674 blob = None
3675 if len(parent) > 0:
Pete Wyckoff4f9273d2013-01-26 22:11:04 -05003676 tempBranch = "%s/%d" % (self.tempBranchLocation, change)
Vitor Antunesfed23692012-01-25 23:48:22 +00003677 if self.verbose:
Luke Diamandf2606b12018-06-19 09:04:10 +01003678 print("Creating temporary branch: " + tempBranch)
Pete Wyckoffe63231e2012-08-11 12:55:02 -04003679 self.commit(description, filesForCommit, tempBranch)
Vitor Antunesfed23692012-01-25 23:48:22 +00003680 self.tempBranches.append(tempBranch)
3681 self.checkpoint()
3682 blob = self.searchParent(parent, branch, tempBranch)
3683 if blob:
Pete Wyckoffe63231e2012-08-11 12:55:02 -04003684 self.commit(description, filesForCommit, branch, blob)
Vitor Antunesfed23692012-01-25 23:48:22 +00003685 else:
3686 if self.verbose:
Luke Diamandf2606b12018-06-19 09:04:10 +01003687 print("Parent of %s not found. Committing into head of %s" % (branch, parent))
Pete Wyckoffe63231e2012-08-11 12:55:02 -04003688 self.commit(description, filesForCommit, branch, parent)
Simon Hausmanne87f37a2007-08-26 16:00:52 +02003689 else:
Luke Diamand89143ac2018-10-15 12:14:08 +01003690 files = self.extractFilesFromCommit(description)
Pete Wyckoffe63231e2012-08-11 12:55:02 -04003691 self.commit(description, files, self.branch,
Simon Hausmanne87f37a2007-08-26 16:00:52 +02003692 self.initialParent)
Pete Wyckoff47497842013-01-14 19:47:04 -05003693 # only needed once, to connect to the previous commit
Simon Hausmanne87f37a2007-08-26 16:00:52 +02003694 self.initialParent = ""
3695 except IOError:
Luke Diamandf2606b12018-06-19 09:04:10 +01003696 print(self.gitError.read())
Simon Hausmanne87f37a2007-08-26 16:00:52 +02003697 sys.exit(1)
3698
Luke Diamandb9d34db2018-06-08 21:32:44 +01003699 def sync_origin_only(self):
3700 if self.syncWithOrigin:
3701 self.hasOrigin = originP4BranchesExist()
3702 if self.hasOrigin:
3703 if not self.silent:
Luke Diamandf2606b12018-06-19 09:04:10 +01003704 print('Syncing with origin first, using "git fetch origin"')
Luke Diamandb9d34db2018-06-08 21:32:44 +01003705 system("git fetch origin")
3706
Simon Hausmannc208a242007-08-26 16:07:18 +02003707 def importHeadRevision(self, revision):
Luke Diamandf2606b12018-06-19 09:04:10 +01003708 print("Doing initial import of %s from revision %s into %s" % (' '.join(self.depotPaths), revision, self.branch))
Simon Hausmannc208a242007-08-26 16:07:18 +02003709
Pete Wyckoff4e2e6ce2011-07-31 09:45:55 -04003710 details = {}
3711 details["user"] = "git perforce import user"
Pete Wyckoff1494fcb2011-02-19 08:17:56 -05003712 details["desc"] = ("Initial import of %s from the state at revision %s\n"
Simon Hausmannc208a242007-08-26 16:07:18 +02003713 % (' '.join(self.depotPaths), revision))
3714 details["change"] = revision
3715 newestRevision = 0
3716
3717 fileCnt = 0
Luke Diamand6de040d2011-10-16 10:47:52 -04003718 fileArgs = ["%s...%s" % (p,revision) for p in self.depotPaths]
3719
3720 for info in p4CmdList(["files"] + fileArgs):
Simon Hausmannc208a242007-08-26 16:07:18 +02003721
Pete Wyckoff68b28592011-02-19 08:17:55 -05003722 if 'code' in info and info['code'] == 'error':
Simon Hausmannc208a242007-08-26 16:07:18 +02003723 sys.stderr.write("p4 returned an error: %s\n"
3724 % info['data'])
Pete Wyckoffd88e7072011-02-19 08:17:58 -05003725 if info['data'].find("must refer to client") >= 0:
3726 sys.stderr.write("This particular p4 error is misleading.\n")
3727 sys.stderr.write("Perhaps the depot path was misspelled.\n");
3728 sys.stderr.write("Depot path: %s\n" % " ".join(self.depotPaths))
Simon Hausmannc208a242007-08-26 16:07:18 +02003729 sys.exit(1)
Pete Wyckoff68b28592011-02-19 08:17:55 -05003730 if 'p4ExitCode' in info:
3731 sys.stderr.write("p4 exitcode: %s\n" % info['p4ExitCode'])
Simon Hausmannc208a242007-08-26 16:07:18 +02003732 sys.exit(1)
3733
3734
3735 change = int(info["change"])
3736 if change > newestRevision:
3737 newestRevision = change
3738
Pete Wyckoff56c09342011-02-19 08:17:57 -05003739 if info["action"] in self.delete_actions:
Simon Hausmannc208a242007-08-26 16:07:18 +02003740 # don't increase the file cnt, otherwise details["depotFile123"] will have gaps!
3741 #fileCnt = fileCnt + 1
3742 continue
3743
3744 for prop in ["depotFile", "rev", "action", "type" ]:
3745 details["%s%s" % (prop, fileCnt)] = info[prop]
3746
3747 fileCnt = fileCnt + 1
3748
3749 details["change"] = newestRevision
Pete Wyckoff4e2e6ce2011-07-31 09:45:55 -04003750
Pete Wyckoff9dcb9f22012-04-08 20:18:01 -04003751 # Use time from top-most change so that all git p4 clones of
Pete Wyckoff4e2e6ce2011-07-31 09:45:55 -04003752 # the same p4 repo have the same commit SHA1s.
Pete Wyckoff18fa13d2012-11-23 17:35:34 -05003753 res = p4_describe(newestRevision)
3754 details["time"] = res["time"]
Pete Wyckoff4e2e6ce2011-07-31 09:45:55 -04003755
Simon Hausmannc208a242007-08-26 16:07:18 +02003756 self.updateOptionDict(details)
3757 try:
Pete Wyckoffe63231e2012-08-11 12:55:02 -04003758 self.commit(details, self.extractFilesFromCommit(details), self.branch)
Philip.McGrawde5abb52019-08-27 06:43:58 +03003759 except IOError as err:
Luke Diamandf2606b12018-06-19 09:04:10 +01003760 print("IO error with git fast-import. Is your git version recent enough?")
Philip.McGrawde5abb52019-08-27 06:43:58 +03003761 print("IO error details: {}".format(err))
Luke Diamandf2606b12018-06-19 09:04:10 +01003762 print(self.gitError.read())
Simon Hausmannc208a242007-08-26 16:07:18 +02003763
Luke Diamandca5b5cc2020-01-29 11:12:44 +00003764
3765 def importRevisions(self, args, branch_arg_given):
3766 changes = []
3767
3768 if len(self.changesFile) > 0:
Luke Diamand43f33e42020-01-30 11:50:34 +00003769 with open(self.changesFile) as f:
3770 output = f.readlines()
Luke Diamandca5b5cc2020-01-29 11:12:44 +00003771 changeSet = set()
3772 for line in output:
3773 changeSet.add(int(line))
3774
3775 for change in changeSet:
3776 changes.append(change)
3777
3778 changes.sort()
3779 else:
3780 # catch "git p4 sync" with no new branches, in a repo that
3781 # does not have any existing p4 branches
3782 if len(args) == 0:
3783 if not self.p4BranchesInGit:
Luke Diamand6026aff2020-01-29 11:12:45 +00003784 raise P4CommandException("No remote p4 branches. Perhaps you never did \"git p4 clone\" in here.")
Luke Diamandca5b5cc2020-01-29 11:12:44 +00003785
3786 # The default branch is master, unless --branch is used to
3787 # specify something else. Make sure it exists, or complain
3788 # nicely about how to use --branch.
3789 if not self.detectBranches:
3790 if not branch_exists(self.branch):
3791 if branch_arg_given:
Luke Diamand6026aff2020-01-29 11:12:45 +00003792 raise P4CommandException("Error: branch %s does not exist." % self.branch)
Luke Diamandca5b5cc2020-01-29 11:12:44 +00003793 else:
Luke Diamand6026aff2020-01-29 11:12:45 +00003794 raise P4CommandException("Error: no branch %s; perhaps specify one with --branch." %
Luke Diamandca5b5cc2020-01-29 11:12:44 +00003795 self.branch)
3796
3797 if self.verbose:
3798 print("Getting p4 changes for %s...%s" % (', '.join(self.depotPaths),
3799 self.changeRange))
3800 changes = p4ChangesForPaths(self.depotPaths, self.changeRange, self.changes_block_size)
3801
3802 if len(self.maxChanges) > 0:
3803 changes = changes[:min(int(self.maxChanges), len(changes))]
3804
3805 if len(changes) == 0:
3806 if not self.silent:
3807 print("No changes to import!")
3808 else:
3809 if not self.silent and not self.detectBranches:
3810 print("Import destination: %s" % self.branch)
3811
3812 self.updatedBranches = set()
3813
3814 if not self.detectBranches:
3815 if args:
3816 # start a new branch
3817 self.initialParent = ""
3818 else:
3819 # build on a previous revision
3820 self.initialParent = parseRevision(self.branch)
3821
3822 self.importChanges(changes)
3823
3824 if not self.silent:
3825 print("")
3826 if len(self.updatedBranches) > 0:
3827 sys.stdout.write("Updated branches: ")
3828 for b in self.updatedBranches:
3829 sys.stdout.write("%s " % b)
3830 sys.stdout.write("\n")
3831
Luke Diamand123f6312018-05-23 23:20:26 +01003832 def openStreams(self):
3833 self.importProcess = subprocess.Popen(["git", "fast-import"],
3834 stdin=subprocess.PIPE,
3835 stdout=subprocess.PIPE,
3836 stderr=subprocess.PIPE);
3837 self.gitOutput = self.importProcess.stdout
3838 self.gitStream = self.importProcess.stdin
3839 self.gitError = self.importProcess.stderr
3840
Yang Zhao86dca242019-12-13 15:52:39 -08003841 if bytes is not str:
3842 # Wrap gitStream.write() so that it can be called using `str` arguments
3843 def make_encoded_write(write):
3844 def encoded_write(s):
3845 return write(s.encode() if isinstance(s, str) else s)
3846 return encoded_write
3847
3848 self.gitStream.write = make_encoded_write(self.gitStream.write)
3849
Luke Diamand123f6312018-05-23 23:20:26 +01003850 def closeStreams(self):
Luke Diamand837b3a62020-01-29 11:12:41 +00003851 if self.gitStream is None:
3852 return
Luke Diamand123f6312018-05-23 23:20:26 +01003853 self.gitStream.close()
3854 if self.importProcess.wait() != 0:
3855 die("fast-import failed: %s" % self.gitError.read())
3856 self.gitOutput.close()
3857 self.gitError.close()
Luke Diamand837b3a62020-01-29 11:12:41 +00003858 self.gitStream = None
Simon Hausmannc208a242007-08-26 16:07:18 +02003859
Simon Hausmannb9847332007-03-20 20:54:23 +01003860 def run(self, args):
Simon Hausmanna028a982007-05-23 00:03:08 +02003861 if self.importIntoRemotes:
3862 self.refPrefix = "refs/remotes/p4/"
3863 else:
Marius Storm-Olsendb775552007-06-07 15:13:59 +02003864 self.refPrefix = "refs/heads/p4/"
Simon Hausmanna028a982007-05-23 00:03:08 +02003865
Luke Diamandb9d34db2018-06-08 21:32:44 +01003866 self.sync_origin_only()
Simon Hausmann10f880f2007-05-24 22:28:28 +02003867
Pete Wyckoff5a8e84c2013-01-14 19:47:05 -05003868 branch_arg_given = bool(self.branch)
Simon Hausmann569d1bd2007-03-22 21:34:16 +01003869 if len(self.branch) == 0:
Marius Storm-Olsendb775552007-06-07 15:13:59 +02003870 self.branch = self.refPrefix + "master"
Simon Hausmanna028a982007-05-23 00:03:08 +02003871 if gitBranchExists("refs/heads/p4") and self.importIntoRemotes:
Simon Hausmann48df6fd2007-05-17 21:18:53 +02003872 system("git update-ref %s refs/heads/p4" % self.branch)
Pete Wyckoff55d12432013-01-14 19:46:59 -05003873 system("git branch -D p4")
Simon Hausmann179caeb2007-03-22 22:17:42 +01003874
Pete Wyckoffa93d33e2012-02-25 20:06:24 -05003875 # accept either the command-line option, or the configuration variable
3876 if self.useClientSpec:
3877 # will use this after clone to set the variable
3878 self.useClientSpec_from_options = True
3879 else:
Pete Wyckoff0d609032013-01-26 22:11:24 -05003880 if gitConfigBool("git-p4.useclientspec"):
Pete Wyckoff09fca772011-12-24 21:07:39 -05003881 self.useClientSpec = True
3882 if self.useClientSpec:
Pete Wyckoff543987b2012-02-25 20:06:25 -05003883 self.clientSpecDirs = getClientSpec()
Tor Arvid Lund3a70cdf2008-02-18 15:22:08 +01003884
Han-Wen Nienhuys6a49f8e2007-05-23 18:49:35 -03003885 # TODO: should always look at previous commits,
3886 # merge with previous imports, if possible.
3887 if args == []:
Simon Hausmannd414c742007-05-25 11:36:42 +02003888 if self.hasOrigin:
Simon Hausmann5ca44612007-08-24 17:44:16 +02003889 createOrUpdateBranchesFromOrigin(self.refPrefix, self.silent)
Pete Wyckoff3b650fc2013-01-14 19:46:58 -05003890
3891 # branches holds mapping from branch name to sha1
3892 branches = p4BranchesInGit(self.importIntoRemotes)
Pete Wyckoff8c9e8b62013-01-14 19:47:06 -05003893
3894 # restrict to just this one, disabling detect-branches
3895 if branch_arg_given:
3896 short = self.branch.split("/")[-1]
3897 if short in branches:
3898 self.p4BranchesInGit = [ short ]
3899 else:
3900 self.p4BranchesInGit = branches.keys()
Simon Hausmannabcd7902007-05-24 22:25:36 +02003901
3902 if len(self.p4BranchesInGit) > 1:
3903 if not self.silent:
Luke Diamandf2606b12018-06-19 09:04:10 +01003904 print("Importing from/into multiple branches")
Simon Hausmannabcd7902007-05-24 22:25:36 +02003905 self.detectBranches = True
Pete Wyckoff8c9e8b62013-01-14 19:47:06 -05003906 for branch in branches.keys():
3907 self.initialParents[self.refPrefix + branch] = \
3908 branches[branch]
Simon Hausmann967f72e2007-03-23 09:30:41 +01003909
Simon Hausmann29bdbac2007-05-19 10:23:12 +02003910 if self.verbose:
Luke Diamandf2606b12018-06-19 09:04:10 +01003911 print("branches: %s" % self.p4BranchesInGit)
Simon Hausmann29bdbac2007-05-19 10:23:12 +02003912
3913 p4Change = 0
3914 for branch in self.p4BranchesInGit:
Han-Wen Nienhuyscebdf5a2007-05-23 16:53:11 -03003915 logMsg = extractLogMessageFromGitCommit(self.refPrefix + branch)
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03003916
3917 settings = extractSettingsGitLog(logMsg)
Simon Hausmann29bdbac2007-05-19 10:23:12 +02003918
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03003919 self.readOptions(settings)
Luke Diamanddba1c9d2018-06-19 09:04:07 +01003920 if ('depot-paths' in settings
3921 and 'change' in settings):
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03003922 change = int(settings['change']) + 1
Simon Hausmann29bdbac2007-05-19 10:23:12 +02003923 p4Change = max(p4Change, change)
3924
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03003925 depotPaths = sorted(settings['depot-paths'])
3926 if self.previousDepotPaths == []:
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03003927 self.previousDepotPaths = depotPaths
Simon Hausmann29bdbac2007-05-19 10:23:12 +02003928 else:
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03003929 paths = []
3930 for (prev, cur) in zip(self.previousDepotPaths, depotPaths):
Vitor Antunes04d277b2011-08-19 00:44:03 +01003931 prev_list = prev.split("/")
3932 cur_list = cur.split("/")
3933 for i in range(0, min(len(cur_list), len(prev_list))):
Luke Diamandfc35c9d2018-06-19 09:04:06 +01003934 if cur_list[i] != prev_list[i]:
Simon Hausmann583e1702007-06-07 09:37:13 +02003935 i = i - 1
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03003936 break
3937
Vitor Antunes04d277b2011-08-19 00:44:03 +01003938 paths.append ("/".join(cur_list[:i + 1]))
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03003939
3940 self.previousDepotPaths = paths
Simon Hausmann29bdbac2007-05-19 10:23:12 +02003941
3942 if p4Change > 0:
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03003943 self.depotPaths = sorted(self.previousDepotPaths)
Simon Hausmannd5904672007-05-19 11:07:32 +02003944 self.changeRange = "@%s,#head" % p4Change
Simon Hausmann341dc1c2007-05-21 00:39:16 +02003945 if not self.silent and not self.detectBranches:
Luke Diamandf2606b12018-06-19 09:04:10 +01003946 print("Performing incremental import into %s git branch" % self.branch)
Simon Hausmann569d1bd2007-03-22 21:34:16 +01003947
Pete Wyckoff40d69ac2013-01-14 19:47:03 -05003948 # accept multiple ref name abbreviations:
3949 # refs/foo/bar/branch -> use it exactly
3950 # p4/branch -> prepend refs/remotes/ or refs/heads/
3951 # branch -> prepend refs/remotes/p4/ or refs/heads/p4/
Simon Hausmannf9162f62007-05-17 09:02:45 +02003952 if not self.branch.startswith("refs/"):
Pete Wyckoff40d69ac2013-01-14 19:47:03 -05003953 if self.importIntoRemotes:
3954 prepend = "refs/remotes/"
3955 else:
3956 prepend = "refs/heads/"
3957 if not self.branch.startswith("p4/"):
3958 prepend += "p4/"
3959 self.branch = prepend + self.branch
Simon Hausmann179caeb2007-03-22 22:17:42 +01003960
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03003961 if len(args) == 0 and self.depotPaths:
Simon Hausmannb9847332007-03-20 20:54:23 +01003962 if not self.silent:
Luke Diamandf2606b12018-06-19 09:04:10 +01003963 print("Depot paths: %s" % ' '.join(self.depotPaths))
Simon Hausmannb9847332007-03-20 20:54:23 +01003964 else:
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03003965 if self.depotPaths and self.depotPaths != args:
Luke Diamandf2606b12018-06-19 09:04:10 +01003966 print("previous import used depot path %s and now %s was specified. "
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03003967 "This doesn't work!" % (' '.join (self.depotPaths),
3968 ' '.join (args)))
Simon Hausmannb9847332007-03-20 20:54:23 +01003969 sys.exit(1)
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03003970
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03003971 self.depotPaths = sorted(args)
Simon Hausmannb9847332007-03-20 20:54:23 +01003972
Simon Hausmann1c49fc12007-08-26 16:04:34 +02003973 revision = ""
Simon Hausmannb9847332007-03-20 20:54:23 +01003974 self.users = {}
Simon Hausmannb9847332007-03-20 20:54:23 +01003975
Pete Wyckoff58c8bc72011-12-24 21:07:35 -05003976 # Make sure no revision specifiers are used when --changesfile
3977 # is specified.
3978 bad_changesfile = False
3979 if len(self.changesFile) > 0:
3980 for p in self.depotPaths:
3981 if p.find("@") >= 0 or p.find("#") >= 0:
3982 bad_changesfile = True
3983 break
3984 if bad_changesfile:
3985 die("Option --changesfile is incompatible with revision specifiers")
3986
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03003987 newPaths = []
3988 for p in self.depotPaths:
3989 if p.find("@") != -1:
3990 atIdx = p.index("@")
3991 self.changeRange = p[atIdx:]
3992 if self.changeRange == "@all":
3993 self.changeRange = ""
Han-Wen Nienhuys6a49f8e2007-05-23 18:49:35 -03003994 elif ',' not in self.changeRange:
Simon Hausmann1c49fc12007-08-26 16:04:34 +02003995 revision = self.changeRange
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03003996 self.changeRange = ""
Han-Wen Nienhuys7fcff9d2007-07-23 15:56:37 -07003997 p = p[:atIdx]
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03003998 elif p.find("#") != -1:
3999 hashIdx = p.index("#")
Simon Hausmann1c49fc12007-08-26 16:04:34 +02004000 revision = p[hashIdx:]
Han-Wen Nienhuys7fcff9d2007-07-23 15:56:37 -07004001 p = p[:hashIdx]
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03004002 elif self.previousDepotPaths == []:
Pete Wyckoff58c8bc72011-12-24 21:07:35 -05004003 # pay attention to changesfile, if given, else import
4004 # the entire p4 tree at the head revision
4005 if len(self.changesFile) == 0:
4006 revision = "#head"
Simon Hausmannb9847332007-03-20 20:54:23 +01004007
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03004008 p = re.sub ("\.\.\.$", "", p)
4009 if not p.endswith("/"):
4010 p += "/"
4011
4012 newPaths.append(p)
4013
4014 self.depotPaths = newPaths
4015
Pete Wyckoffe63231e2012-08-11 12:55:02 -04004016 # --detect-branches may change this for each branch
4017 self.branchPrefixes = self.depotPaths
4018
Simon Hausmannb607e712007-05-20 10:55:54 +02004019 self.loadUserMapFromCache()
Simon Hausmanncb53e1f2007-04-08 00:12:02 +02004020 self.labels = {}
4021 if self.detectLabels:
4022 self.getLabels();
Simon Hausmannb9847332007-03-20 20:54:23 +01004023
Simon Hausmann4b97ffb2007-05-18 21:45:23 +02004024 if self.detectBranches:
Simon Hausmanndf450922007-06-08 08:49:22 +02004025 ## FIXME - what's a P4 projectName ?
4026 self.projectName = self.guessProjectName()
4027
Simon Hausmann38f9f5e2007-11-15 10:38:45 +01004028 if self.hasOrigin:
4029 self.getBranchMappingFromGitBranches()
4030 else:
4031 self.getBranchMapping()
Simon Hausmann29bdbac2007-05-19 10:23:12 +02004032 if self.verbose:
Luke Diamandf2606b12018-06-19 09:04:10 +01004033 print("p4-git branches: %s" % self.p4BranchesInGit)
4034 print("initial parents: %s" % self.initialParents)
Simon Hausmann29bdbac2007-05-19 10:23:12 +02004035 for b in self.p4BranchesInGit:
4036 if b != "master":
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03004037
4038 ## FIXME
Simon Hausmann29bdbac2007-05-19 10:23:12 +02004039 b = b[len(self.projectName):]
4040 self.createdBranches.add(b)
Simon Hausmann4b97ffb2007-05-18 21:45:23 +02004041
Luke Diamand19fa5ac2020-01-29 11:12:46 +00004042 p4_check_access()
4043
Luke Diamand123f6312018-05-23 23:20:26 +01004044 self.openStreams()
Simon Hausmannb9847332007-03-20 20:54:23 +01004045
Luke Diamand6026aff2020-01-29 11:12:45 +00004046 err = None
Simon Hausmannb9847332007-03-20 20:54:23 +01004047
Luke Diamand6026aff2020-01-29 11:12:45 +00004048 try:
4049 if revision:
4050 self.importHeadRevision(revision)
4051 else:
4052 self.importRevisions(args, branch_arg_given)
Luke Diamand06804c72012-04-11 17:21:24 +02004053
Luke Diamand6026aff2020-01-29 11:12:45 +00004054 if gitConfigBool("git-p4.importLabels"):
4055 self.importLabels = True
Luke Diamand06804c72012-04-11 17:21:24 +02004056
Luke Diamand6026aff2020-01-29 11:12:45 +00004057 if self.importLabels:
4058 p4Labels = getP4Labels(self.depotPaths)
4059 gitTags = getGitTags()
Simon Hausmannb9847332007-03-20 20:54:23 +01004060
Luke Diamand6026aff2020-01-29 11:12:45 +00004061 missingP4Labels = p4Labels - gitTags
4062 self.importP4Labels(self.gitStream, missingP4Labels)
4063
4064 except P4CommandException as e:
4065 err = e
4066
4067 finally:
4068 self.closeStreams()
4069
4070 if err:
4071 die(str(err))
Simon Hausmannb9847332007-03-20 20:54:23 +01004072
Vitor Antunesfed23692012-01-25 23:48:22 +00004073 # Cleanup temporary branches created during import
4074 if self.tempBranches != []:
4075 for branch in self.tempBranches:
4076 read_pipe("git update-ref -d %s" % branch)
4077 os.rmdir(os.path.join(os.environ.get("GIT_DIR", ".git"), self.tempBranchLocation))
4078
Pete Wyckoff55d12432013-01-14 19:46:59 -05004079 # Create a symbolic ref p4/HEAD pointing to p4/<branch> to allow
4080 # a convenient shortcut refname "p4".
4081 if self.importIntoRemotes:
4082 head_ref = self.refPrefix + "HEAD"
4083 if not gitBranchExists(head_ref) and gitBranchExists(self.branch):
4084 system(["git", "symbolic-ref", head_ref, self.branch])
4085
Simon Hausmannb9847332007-03-20 20:54:23 +01004086 return True
4087
Simon Hausmann01ce1fe2007-04-07 23:46:50 +02004088class P4Rebase(Command):
4089 def __init__(self):
4090 Command.__init__(self)
Luke Diamand06804c72012-04-11 17:21:24 +02004091 self.options = [
4092 optparse.make_option("--import-labels", dest="importLabels", action="store_true"),
Luke Diamand06804c72012-04-11 17:21:24 +02004093 ]
Luke Diamand06804c72012-04-11 17:21:24 +02004094 self.importLabels = False
Han-Wen Nienhuyscebdf5a2007-05-23 16:53:11 -03004095 self.description = ("Fetches the latest revision from perforce and "
4096 + "rebases the current work (branch) against it")
Simon Hausmann01ce1fe2007-04-07 23:46:50 +02004097
4098 def run(self, args):
4099 sync = P4Sync()
Luke Diamand06804c72012-04-11 17:21:24 +02004100 sync.importLabels = self.importLabels
Simon Hausmann01ce1fe2007-04-07 23:46:50 +02004101 sync.run([])
Simon Hausmannd7e38682007-06-12 14:34:46 +02004102
Simon Hausmann14594f42007-08-22 09:07:15 +02004103 return self.rebase()
4104
4105 def rebase(self):
Simon Hausmann36ee4ee2008-01-07 14:21:45 +01004106 if os.system("git update-index --refresh") != 0:
Martin Ågren7560f542017-08-23 19:49:35 +02004107 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.");
Simon Hausmann36ee4ee2008-01-07 14:21:45 +01004108 if len(read_pipe("git diff-index HEAD --")) > 0:
Veres Lajosf7e604e2013-06-19 07:37:24 +02004109 die("You have uncommitted changes. Please commit them before rebasing or stash them away with git stash.");
Simon Hausmann36ee4ee2008-01-07 14:21:45 +01004110
Simon Hausmannd7e38682007-06-12 14:34:46 +02004111 [upstream, settings] = findUpstreamBranchPoint()
4112 if len(upstream) == 0:
4113 die("Cannot find upstream branchpoint for rebase")
4114
4115 # the branchpoint may be p4/foo~3, so strip off the parent
4116 upstream = re.sub("~[0-9]+$", "", upstream)
4117
Luke Diamandf2606b12018-06-19 09:04:10 +01004118 print("Rebasing the current branch onto %s" % upstream)
Han-Wen Nienhuysb25b2062007-05-23 18:49:35 -03004119 oldHead = read_pipe("git rev-parse HEAD").strip()
Simon Hausmannd7e38682007-06-12 14:34:46 +02004120 system("git rebase %s" % upstream)
Vlad Dogaru4e49d952014-04-07 16:19:11 +03004121 system("git diff-tree --stat --summary -M %s HEAD --" % oldHead)
Simon Hausmann01ce1fe2007-04-07 23:46:50 +02004122 return True
4123
Simon Hausmannf9a3a4f2007-04-08 10:08:26 +02004124class P4Clone(P4Sync):
4125 def __init__(self):
4126 P4Sync.__init__(self)
4127 self.description = "Creates a new git repository and imports from Perforce into it"
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03004128 self.usage = "usage: %prog [options] //depot/path[@revRange]"
Tommy Thorn354081d2008-02-03 10:38:51 -08004129 self.options += [
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03004130 optparse.make_option("--destination", dest="cloneDestination",
4131 action='store', default=None,
Tommy Thorn354081d2008-02-03 10:38:51 -08004132 help="where to leave result of the clone"),
Pete Wyckoff38200072011-02-19 08:18:01 -05004133 optparse.make_option("--bare", dest="cloneBare",
4134 action="store_true", default=False),
Tommy Thorn354081d2008-02-03 10:38:51 -08004135 ]
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03004136 self.cloneDestination = None
Simon Hausmannf9a3a4f2007-04-08 10:08:26 +02004137 self.needsGit = False
Pete Wyckoff38200072011-02-19 08:18:01 -05004138 self.cloneBare = False
Simon Hausmannf9a3a4f2007-04-08 10:08:26 +02004139
Han-Wen Nienhuys6a49f8e2007-05-23 18:49:35 -03004140 def defaultDestination(self, args):
4141 ## TODO: use common prefix of args?
4142 depotPath = args[0]
4143 depotDir = re.sub("(@[^@]*)$", "", depotPath)
4144 depotDir = re.sub("(#[^#]*)$", "", depotDir)
Toby Allsopp053d9e42008-02-05 09:41:43 +13004145 depotDir = re.sub(r"\.\.\.$", "", depotDir)
Han-Wen Nienhuys6a49f8e2007-05-23 18:49:35 -03004146 depotDir = re.sub(r"/$", "", depotDir)
4147 return os.path.split(depotDir)[1]
4148
Simon Hausmannf9a3a4f2007-04-08 10:08:26 +02004149 def run(self, args):
4150 if len(args) < 1:
4151 return False
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03004152
4153 if self.keepRepoPath and not self.cloneDestination:
4154 sys.stderr.write("Must specify destination for --keep-path\n")
4155 sys.exit(1)
Simon Hausmannf9a3a4f2007-04-08 10:08:26 +02004156
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03004157 depotPaths = args
Simon Hausmann5e100b52007-06-07 21:12:25 +02004158
4159 if not self.cloneDestination and len(depotPaths) > 1:
4160 self.cloneDestination = depotPaths[-1]
4161 depotPaths = depotPaths[:-1]
4162
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03004163 for p in depotPaths:
4164 if not p.startswith("//"):
Pete Wyckoff0f487d32013-01-26 22:11:06 -05004165 sys.stderr.write('Depot paths must start with "//": %s\n' % p)
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03004166 return False
Simon Hausmannf9a3a4f2007-04-08 10:08:26 +02004167
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03004168 if not self.cloneDestination:
Marius Storm-Olsen98ad4fa2007-06-07 15:08:33 +02004169 self.cloneDestination = self.defaultDestination(args)
Simon Hausmannf9a3a4f2007-04-08 10:08:26 +02004170
Luke Diamandf2606b12018-06-19 09:04:10 +01004171 print("Importing from %s into %s" % (', '.join(depotPaths), self.cloneDestination))
Pete Wyckoff38200072011-02-19 08:18:01 -05004172
Kevin Greenc3bf3f12007-06-11 16:48:07 -04004173 if not os.path.exists(self.cloneDestination):
4174 os.makedirs(self.cloneDestination)
Robert Blum053fd0c2008-08-01 12:50:03 -07004175 chdir(self.cloneDestination)
Pete Wyckoff38200072011-02-19 08:18:01 -05004176
4177 init_cmd = [ "git", "init" ]
4178 if self.cloneBare:
4179 init_cmd.append("--bare")
Brandon Caseya235e852013-01-26 11:14:33 -08004180 retcode = subprocess.call(init_cmd)
4181 if retcode:
4182 raise CalledProcessError(retcode, init_cmd)
Pete Wyckoff38200072011-02-19 08:18:01 -05004183
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03004184 if not P4Sync.run(self, depotPaths):
Simon Hausmannf9a3a4f2007-04-08 10:08:26 +02004185 return False
Pete Wyckoffc5959562013-01-14 19:47:01 -05004186
4187 # create a master branch and check out a work tree
4188 if gitBranchExists(self.branch):
4189 system([ "git", "branch", "master", self.branch ])
4190 if not self.cloneBare:
4191 system([ "git", "checkout", "-f" ])
4192 else:
Luke Diamandf2606b12018-06-19 09:04:10 +01004193 print('Not checking out any branch, use ' \
4194 '"git checkout -q -b master <branch>"')
Han-Wen Nienhuys86dff6b2007-05-23 18:49:35 -03004195
Pete Wyckoffa93d33e2012-02-25 20:06:24 -05004196 # auto-set this variable if invoked with --use-client-spec
4197 if self.useClientSpec_from_options:
4198 system("git config --bool git-p4.useclientspec true")
4199
Simon Hausmannf9a3a4f2007-04-08 10:08:26 +02004200 return True
4201
Luke Diamand123f6312018-05-23 23:20:26 +01004202class P4Unshelve(Command):
4203 def __init__(self):
4204 Command.__init__(self)
4205 self.options = []
4206 self.origin = "HEAD"
4207 self.description = "Unshelve a P4 changelist into a git commit"
4208 self.usage = "usage: %prog [options] changelist"
4209 self.options += [
4210 optparse.make_option("--origin", dest="origin",
4211 help="Use this base revision instead of the default (%s)" % self.origin),
4212 ]
4213 self.verbose = False
4214 self.noCommit = False
Luke Diamand08813122018-10-15 12:14:07 +01004215 self.destbranch = "refs/remotes/p4-unshelved"
Luke Diamand123f6312018-05-23 23:20:26 +01004216
4217 def renameBranch(self, branch_name):
4218 """ Rename the existing branch to branch_name.N
4219 """
4220
4221 found = True
4222 for i in range(0,1000):
4223 backup_branch_name = "{0}.{1}".format(branch_name, i)
4224 if not gitBranchExists(backup_branch_name):
4225 gitUpdateRef(backup_branch_name, branch_name) # copy ref to backup
4226 gitDeleteRef(branch_name)
4227 found = True
4228 print("renamed old unshelve branch to {0}".format(backup_branch_name))
4229 break
4230
4231 if not found:
4232 sys.exit("gave up trying to rename existing branch {0}".format(sync.branch))
4233
4234 def findLastP4Revision(self, starting_point):
4235 """ Look back from starting_point for the first commit created by git-p4
4236 to find the P4 commit we are based on, and the depot-paths.
4237 """
4238
4239 for parent in (range(65535)):
Luke Diamand0acbf592020-09-19 09:54:41 +01004240 log = extractLogMessageFromGitCommit("{0}~{1}".format(starting_point, parent))
Luke Diamand123f6312018-05-23 23:20:26 +01004241 settings = extractSettingsGitLog(log)
Luke Diamanddba1c9d2018-06-19 09:04:07 +01004242 if 'change' in settings:
Luke Diamand123f6312018-05-23 23:20:26 +01004243 return settings
4244
4245 sys.exit("could not find git-p4 commits in {0}".format(self.origin))
4246
Luke Diamand89143ac2018-10-15 12:14:08 +01004247 def createShelveParent(self, change, branch_name, sync, origin):
4248 """ Create a commit matching the parent of the shelved changelist 'change'
4249 """
4250 parent_description = p4_describe(change, shelved=True)
4251 parent_description['desc'] = 'parent for shelved changelist {}\n'.format(change)
4252 files = sync.extractFilesFromCommit(parent_description, shelved=False, shelved_cl=change)
4253
4254 parent_files = []
4255 for f in files:
4256 # if it was added in the shelved changelist, it won't exist in the parent
4257 if f['action'] in self.add_actions:
4258 continue
4259
4260 # if it was deleted in the shelved changelist it must not be deleted
4261 # in the parent - we might even need to create it if the origin branch
4262 # does not have it
4263 if f['action'] in self.delete_actions:
4264 f['action'] = 'add'
4265
4266 parent_files.append(f)
4267
4268 sync.commit(parent_description, parent_files, branch_name,
4269 parent=origin, allow_empty=True)
4270 print("created parent commit for {0} based on {1} in {2}".format(
4271 change, self.origin, branch_name))
4272
Luke Diamand123f6312018-05-23 23:20:26 +01004273 def run(self, args):
4274 if len(args) != 1:
4275 return False
4276
4277 if not gitBranchExists(self.origin):
4278 sys.exit("origin branch {0} does not exist".format(self.origin))
4279
4280 sync = P4Sync()
4281 changes = args
Luke Diamand123f6312018-05-23 23:20:26 +01004282
Luke Diamand89143ac2018-10-15 12:14:08 +01004283 # only one change at a time
Luke Diamand123f6312018-05-23 23:20:26 +01004284 change = changes[0]
4285
4286 # if the target branch already exists, rename it
4287 branch_name = "{0}/{1}".format(self.destbranch, change)
4288 if gitBranchExists(branch_name):
4289 self.renameBranch(branch_name)
4290 sync.branch = branch_name
4291
4292 sync.verbose = self.verbose
4293 sync.suppress_meta_comment = True
4294
4295 settings = self.findLastP4Revision(self.origin)
Luke Diamand123f6312018-05-23 23:20:26 +01004296 sync.depotPaths = settings['depot-paths']
4297 sync.branchPrefixes = sync.depotPaths
4298
4299 sync.openStreams()
4300 sync.loadUserMapFromCache()
4301 sync.silent = True
Luke Diamand89143ac2018-10-15 12:14:08 +01004302
4303 # create a commit for the parent of the shelved changelist
4304 self.createShelveParent(change, branch_name, sync, self.origin)
4305
4306 # create the commit for the shelved changelist itself
4307 description = p4_describe(change, True)
4308 files = sync.extractFilesFromCommit(description, True, change)
4309
4310 sync.commit(description, files, branch_name, "")
Luke Diamand123f6312018-05-23 23:20:26 +01004311 sync.closeStreams()
4312
4313 print("unshelved changelist {0} into {1}".format(change, branch_name))
4314
4315 return True
4316
Simon Hausmann09d89de2007-06-20 23:10:28 +02004317class P4Branches(Command):
4318 def __init__(self):
4319 Command.__init__(self)
4320 self.options = [ ]
4321 self.description = ("Shows the git branches that hold imports and their "
4322 + "corresponding perforce depot paths")
4323 self.verbose = False
4324
4325 def run(self, args):
Simon Hausmann5ca44612007-08-24 17:44:16 +02004326 if originP4BranchesExist():
4327 createOrUpdateBranchesFromOrigin()
4328
Simon Hausmann09d89de2007-06-20 23:10:28 +02004329 cmdline = "git rev-parse --symbolic "
4330 cmdline += " --remotes"
4331
4332 for line in read_pipe_lines(cmdline):
4333 line = line.strip()
4334
4335 if not line.startswith('p4/') or line == "p4/HEAD":
4336 continue
4337 branch = line
4338
4339 log = extractLogMessageFromGitCommit("refs/remotes/%s" % branch)
4340 settings = extractSettingsGitLog(log)
4341
Luke Diamandf2606b12018-06-19 09:04:10 +01004342 print("%s <= %s (%s)" % (branch, ",".join(settings["depot-paths"]), settings["change"]))
Simon Hausmann09d89de2007-06-20 23:10:28 +02004343 return True
4344
Simon Hausmannb9847332007-03-20 20:54:23 +01004345class HelpFormatter(optparse.IndentedHelpFormatter):
4346 def __init__(self):
4347 optparse.IndentedHelpFormatter.__init__(self)
4348
4349 def format_description(self, description):
4350 if description:
4351 return description + "\n"
4352 else:
4353 return ""
Simon Hausmann4f5cf762007-03-19 22:25:17 +01004354
Simon Hausmann86949ee2007-03-19 20:59:12 +01004355def printUsage(commands):
Luke Diamandf2606b12018-06-19 09:04:10 +01004356 print("usage: %s <command> [options]" % sys.argv[0])
4357 print("")
4358 print("valid commands: %s" % ", ".join(commands))
4359 print("")
4360 print("Try %s <command> --help for command specific help." % sys.argv[0])
4361 print("")
Simon Hausmann86949ee2007-03-19 20:59:12 +01004362
4363commands = {
Han-Wen Nienhuysb86f7372007-05-23 18:49:35 -03004364 "debug" : P4Debug,
4365 "submit" : P4Submit,
Marius Storm-Olsena9834f52007-10-09 16:16:09 +02004366 "commit" : P4Submit,
Han-Wen Nienhuysb86f7372007-05-23 18:49:35 -03004367 "sync" : P4Sync,
4368 "rebase" : P4Rebase,
4369 "clone" : P4Clone,
Simon Hausmann09d89de2007-06-20 23:10:28 +02004370 "rollback" : P4RollBack,
Luke Diamand123f6312018-05-23 23:20:26 +01004371 "branches" : P4Branches,
4372 "unshelve" : P4Unshelve,
Simon Hausmann86949ee2007-03-19 20:59:12 +01004373}
4374
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03004375def main():
4376 if len(sys.argv[1:]) == 0:
4377 printUsage(commands.keys())
4378 sys.exit(2)
Simon Hausmann86949ee2007-03-19 20:59:12 +01004379
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03004380 cmdName = sys.argv[1]
4381 try:
Han-Wen Nienhuysb86f7372007-05-23 18:49:35 -03004382 klass = commands[cmdName]
4383 cmd = klass()
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03004384 except KeyError:
Luke Diamandf2606b12018-06-19 09:04:10 +01004385 print("unknown command %s" % cmdName)
4386 print("")
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03004387 printUsage(commands.keys())
4388 sys.exit(2)
Simon Hausmann4f5cf762007-03-19 22:25:17 +01004389
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03004390 options = cmd.options
Han-Wen Nienhuysb86f7372007-05-23 18:49:35 -03004391 cmd.gitdir = os.environ.get("GIT_DIR", None)
Simon Hausmann86949ee2007-03-19 20:59:12 +01004392
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03004393 args = sys.argv[2:]
Simon Hausmanne20a9e52007-03-26 00:13:51 +02004394
Pete Wyckoffb0ccc802012-09-09 16:16:10 -04004395 options.append(optparse.make_option("--verbose", "-v", dest="verbose", action="store_true"))
Luke Diamand6a10b6a2012-04-24 09:33:23 +01004396 if cmd.needsGit:
4397 options.append(optparse.make_option("--git-dir", dest="gitdir"))
Simon Hausmanne20a9e52007-03-26 00:13:51 +02004398
Luke Diamand6a10b6a2012-04-24 09:33:23 +01004399 parser = optparse.OptionParser(cmd.usage.replace("%prog", "%prog " + cmdName),
4400 options,
4401 description = cmd.description,
4402 formatter = HelpFormatter())
Simon Hausmann86949ee2007-03-19 20:59:12 +01004403
Ben Keene608e3802019-12-16 14:02:20 +00004404 try:
4405 (cmd, args) = parser.parse_args(sys.argv[2:], cmd);
4406 except:
4407 parser.print_help()
4408 raise
4409
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03004410 global verbose
4411 verbose = cmd.verbose
4412 if cmd.needsGit:
Han-Wen Nienhuysb86f7372007-05-23 18:49:35 -03004413 if cmd.gitdir == None:
4414 cmd.gitdir = os.path.abspath(".git")
4415 if not isValidGitDir(cmd.gitdir):
Luke Diamand378f7be2016-12-13 21:51:28 +00004416 # "rev-parse --git-dir" without arguments will try $PWD/.git
Han-Wen Nienhuysb86f7372007-05-23 18:49:35 -03004417 cmd.gitdir = read_pipe("git rev-parse --git-dir").strip()
4418 if os.path.exists(cmd.gitdir):
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03004419 cdup = read_pipe("git rev-parse --show-cdup").strip()
4420 if len(cdup) > 0:
Robert Blum053fd0c2008-08-01 12:50:03 -07004421 chdir(cdup);
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03004422
Han-Wen Nienhuysb86f7372007-05-23 18:49:35 -03004423 if not isValidGitDir(cmd.gitdir):
4424 if isValidGitDir(cmd.gitdir + "/.git"):
4425 cmd.gitdir += "/.git"
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03004426 else:
Han-Wen Nienhuysb86f7372007-05-23 18:49:35 -03004427 die("fatal: cannot locate git repository at %s" % cmd.gitdir)
Simon Hausmann8910ac02007-03-26 08:18:55 +02004428
Luke Diamand378f7be2016-12-13 21:51:28 +00004429 # so git commands invoked from the P4 workspace will succeed
Han-Wen Nienhuysb86f7372007-05-23 18:49:35 -03004430 os.environ["GIT_DIR"] = cmd.gitdir
Simon Hausmann4f5cf762007-03-19 22:25:17 +01004431
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03004432 if not cmd.run(args):
4433 parser.print_help()
Pete Wyckoff09fca772011-12-24 21:07:39 -05004434 sys.exit(2)
Simon Hausmann4f5cf762007-03-19 22:25:17 +01004435
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03004436
4437if __name__ == '__main__':
4438 main()