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