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 | # |
| 10 | |
Reilly Grant | 1d7367d | 2009-09-10 00:02:38 -0700 | [diff] [blame] | 11 | import optparse, sys, os, marshal, subprocess, shelve |
| 12 | import tempfile, getopt, os.path, time, platform |
Han-Wen Nienhuys | ce6f33c | 2007-05-23 16:46:29 -0300 | [diff] [blame] | 13 | import re |
Han-Wen Nienhuys | 8b41a97 | 2007-05-23 18:20:53 -0300 | [diff] [blame] | 14 | |
Han-Wen Nienhuys | 4addad2 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 15 | verbose = False |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 16 | |
Anand Kumria | 21a5075 | 2008-08-10 19:26:28 +0100 | [diff] [blame] | 17 | |
| 18 | def p4_build_cmd(cmd): |
| 19 | """Build a suitable p4 command line. |
| 20 | |
| 21 | This consolidates building and returning a p4 command line into one |
| 22 | location. It means that hooking into the environment, or other configuration |
| 23 | can be done more easily. |
| 24 | """ |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 25 | real_cmd = ["p4"] |
Anand Kumria | abcaf07 | 2008-08-10 19:26:31 +0100 | [diff] [blame] | 26 | |
| 27 | user = gitConfig("git-p4.user") |
| 28 | if len(user) > 0: |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 29 | real_cmd += ["-u",user] |
Anand Kumria | abcaf07 | 2008-08-10 19:26:31 +0100 | [diff] [blame] | 30 | |
| 31 | password = gitConfig("git-p4.password") |
| 32 | if len(password) > 0: |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 33 | real_cmd += ["-P", password] |
Anand Kumria | abcaf07 | 2008-08-10 19:26:31 +0100 | [diff] [blame] | 34 | |
| 35 | port = gitConfig("git-p4.port") |
| 36 | if len(port) > 0: |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 37 | real_cmd += ["-p", port] |
Anand Kumria | abcaf07 | 2008-08-10 19:26:31 +0100 | [diff] [blame] | 38 | |
| 39 | host = gitConfig("git-p4.host") |
| 40 | if len(host) > 0: |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 41 | real_cmd += ["-h", host] |
Anand Kumria | abcaf07 | 2008-08-10 19:26:31 +0100 | [diff] [blame] | 42 | |
| 43 | client = gitConfig("git-p4.client") |
| 44 | if len(client) > 0: |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 45 | real_cmd += ["-c", client] |
Anand Kumria | abcaf07 | 2008-08-10 19:26:31 +0100 | [diff] [blame] | 46 | |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 47 | |
| 48 | if isinstance(cmd,basestring): |
| 49 | real_cmd = ' '.join(real_cmd) + ' ' + cmd |
| 50 | else: |
| 51 | real_cmd += cmd |
Anand Kumria | 21a5075 | 2008-08-10 19:26:28 +0100 | [diff] [blame] | 52 | return real_cmd |
| 53 | |
Robert Blum | 053fd0c | 2008-08-01 12:50:03 -0700 | [diff] [blame] | 54 | def chdir(dir): |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 55 | # P4 uses the PWD environment variable rather than getcwd(). Since we're |
Gary Gibbons | bf1d68f | 2011-12-09 18:48:16 -0500 | [diff] [blame] | 56 | # not using the shell, we have to set it ourselves. This path could |
| 57 | # be relative, so go there first, then figure out where we ended up. |
Robert Blum | 053fd0c | 2008-08-01 12:50:03 -0700 | [diff] [blame] | 58 | os.chdir(dir) |
Gary Gibbons | bf1d68f | 2011-12-09 18:48:16 -0500 | [diff] [blame] | 59 | os.environ['PWD'] = os.getcwd() |
Robert Blum | 053fd0c | 2008-08-01 12:50:03 -0700 | [diff] [blame] | 60 | |
Han-Wen Nienhuys | 86dff6b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 61 | def die(msg): |
| 62 | if verbose: |
| 63 | raise Exception(msg) |
| 64 | else: |
| 65 | sys.stderr.write(msg + "\n") |
| 66 | sys.exit(1) |
| 67 | |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 68 | def write_pipe(c, stdin): |
Han-Wen Nienhuys | 4addad2 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 69 | if verbose: |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 70 | sys.stderr.write('Writing pipe: %s\n' % str(c)) |
Han-Wen Nienhuys | b016d39 | 2007-05-23 17:10:46 -0300 | [diff] [blame] | 71 | |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 72 | expand = isinstance(c,basestring) |
| 73 | p = subprocess.Popen(c, stdin=subprocess.PIPE, shell=expand) |
| 74 | pipe = p.stdin |
| 75 | val = pipe.write(stdin) |
| 76 | pipe.close() |
| 77 | if p.wait(): |
| 78 | die('Command failed: %s' % str(c)) |
Han-Wen Nienhuys | b016d39 | 2007-05-23 17:10:46 -0300 | [diff] [blame] | 79 | |
| 80 | return val |
| 81 | |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 82 | def p4_write_pipe(c, stdin): |
Anand Kumria | d942919 | 2008-08-14 23:40:38 +0100 | [diff] [blame] | 83 | real_cmd = p4_build_cmd(c) |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 84 | return write_pipe(real_cmd, stdin) |
Anand Kumria | d942919 | 2008-08-14 23:40:38 +0100 | [diff] [blame] | 85 | |
Han-Wen Nienhuys | 4addad2 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 86 | def read_pipe(c, ignore_error=False): |
| 87 | if verbose: |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 88 | sys.stderr.write('Reading pipe: %s\n' % str(c)) |
Han-Wen Nienhuys | 8b41a97 | 2007-05-23 18:20:53 -0300 | [diff] [blame] | 89 | |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 90 | expand = isinstance(c,basestring) |
| 91 | p = subprocess.Popen(c, stdout=subprocess.PIPE, shell=expand) |
| 92 | pipe = p.stdout |
Han-Wen Nienhuys | b016d39 | 2007-05-23 17:10:46 -0300 | [diff] [blame] | 93 | val = pipe.read() |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 94 | if p.wait() and not ignore_error: |
| 95 | die('Command failed: %s' % str(c)) |
Han-Wen Nienhuys | b016d39 | 2007-05-23 17:10:46 -0300 | [diff] [blame] | 96 | |
| 97 | return val |
| 98 | |
Anand Kumria | d942919 | 2008-08-14 23:40:38 +0100 | [diff] [blame] | 99 | def p4_read_pipe(c, ignore_error=False): |
| 100 | real_cmd = p4_build_cmd(c) |
| 101 | return read_pipe(real_cmd, ignore_error) |
Han-Wen Nienhuys | b016d39 | 2007-05-23 17:10:46 -0300 | [diff] [blame] | 102 | |
Han-Wen Nienhuys | bce4c5f | 2007-05-23 17:14:33 -0300 | [diff] [blame] | 103 | def read_pipe_lines(c): |
Han-Wen Nienhuys | 4addad2 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 104 | if verbose: |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 105 | sys.stderr.write('Reading pipe: %s\n' % str(c)) |
| 106 | |
| 107 | expand = isinstance(c, basestring) |
| 108 | p = subprocess.Popen(c, stdout=subprocess.PIPE, shell=expand) |
| 109 | pipe = p.stdout |
Han-Wen Nienhuys | b016d39 | 2007-05-23 17:10:46 -0300 | [diff] [blame] | 110 | val = pipe.readlines() |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 111 | if pipe.close() or p.wait(): |
| 112 | die('Command failed: %s' % str(c)) |
Han-Wen Nienhuys | b016d39 | 2007-05-23 17:10:46 -0300 | [diff] [blame] | 113 | |
| 114 | return val |
Simon Hausmann | caace11 | 2007-05-15 14:57:57 +0200 | [diff] [blame] | 115 | |
Anand Kumria | 2318121 | 2008-08-10 19:26:24 +0100 | [diff] [blame] | 116 | def p4_read_pipe_lines(c): |
| 117 | """Specifically invoke p4 on the command supplied. """ |
Anand Kumria | 155af83 | 2008-08-10 19:26:30 +0100 | [diff] [blame] | 118 | real_cmd = p4_build_cmd(c) |
Anand Kumria | 2318121 | 2008-08-10 19:26:24 +0100 | [diff] [blame] | 119 | return read_pipe_lines(real_cmd) |
| 120 | |
Han-Wen Nienhuys | 6754a29 | 2007-05-23 17:41:50 -0300 | [diff] [blame] | 121 | def system(cmd): |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 122 | expand = isinstance(cmd,basestring) |
Han-Wen Nienhuys | 4addad2 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 123 | if verbose: |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 124 | sys.stderr.write("executing %s\n" % str(cmd)) |
| 125 | subprocess.check_call(cmd, shell=expand) |
Han-Wen Nienhuys | 6754a29 | 2007-05-23 17:41:50 -0300 | [diff] [blame] | 126 | |
Anand Kumria | bf9320f | 2008-08-10 19:26:26 +0100 | [diff] [blame] | 127 | def p4_system(cmd): |
| 128 | """Specifically invoke p4 as the system command. """ |
Anand Kumria | 155af83 | 2008-08-10 19:26:30 +0100 | [diff] [blame] | 129 | real_cmd = p4_build_cmd(cmd) |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 130 | expand = isinstance(real_cmd, basestring) |
| 131 | subprocess.check_call(real_cmd, shell=expand) |
| 132 | |
| 133 | def p4_integrate(src, dest): |
| 134 | p4_system(["integrate", "-Dt", src, dest]) |
| 135 | |
| 136 | def p4_sync(path): |
| 137 | p4_system(["sync", path]) |
| 138 | |
| 139 | def p4_add(f): |
| 140 | p4_system(["add", f]) |
| 141 | |
| 142 | def p4_delete(f): |
| 143 | p4_system(["delete", f]) |
| 144 | |
| 145 | def p4_edit(f): |
| 146 | p4_system(["edit", f]) |
| 147 | |
| 148 | def p4_revert(f): |
| 149 | p4_system(["revert", f]) |
| 150 | |
| 151 | def p4_reopen(type, file): |
| 152 | p4_system(["reopen", "-t", type, file]) |
Anand Kumria | bf9320f | 2008-08-10 19:26:26 +0100 | [diff] [blame] | 153 | |
Pete Wyckoff | 9cffb8c | 2011-10-16 10:45:01 -0400 | [diff] [blame] | 154 | # |
| 155 | # Canonicalize the p4 type and return a tuple of the |
| 156 | # base type, plus any modifiers. See "p4 help filetypes" |
| 157 | # for a list and explanation. |
| 158 | # |
| 159 | def split_p4_type(p4type): |
David Brown | b9fc6ea | 2007-09-19 13:12:48 -0700 | [diff] [blame] | 160 | |
Pete Wyckoff | 9cffb8c | 2011-10-16 10:45:01 -0400 | [diff] [blame] | 161 | p4_filetypes_historical = { |
| 162 | "ctempobj": "binary+Sw", |
| 163 | "ctext": "text+C", |
| 164 | "cxtext": "text+Cx", |
| 165 | "ktext": "text+k", |
| 166 | "kxtext": "text+kx", |
| 167 | "ltext": "text+F", |
| 168 | "tempobj": "binary+FSw", |
| 169 | "ubinary": "binary+F", |
| 170 | "uresource": "resource+F", |
| 171 | "uxbinary": "binary+Fx", |
| 172 | "xbinary": "binary+x", |
| 173 | "xltext": "text+Fx", |
| 174 | "xtempobj": "binary+Swx", |
| 175 | "xtext": "text+x", |
| 176 | "xunicode": "unicode+x", |
| 177 | "xutf16": "utf16+x", |
| 178 | } |
| 179 | if p4type in p4_filetypes_historical: |
| 180 | p4type = p4_filetypes_historical[p4type] |
| 181 | mods = "" |
| 182 | s = p4type.split("+") |
| 183 | base = s[0] |
| 184 | mods = "" |
| 185 | if len(s) > 1: |
| 186 | mods = s[1] |
| 187 | return (base, mods) |
| 188 | |
David Brown | b9fc6ea | 2007-09-19 13:12:48 -0700 | [diff] [blame] | 189 | |
Chris Pettitt | c65b670 | 2007-11-01 20:43:14 -0700 | [diff] [blame] | 190 | def setP4ExecBit(file, mode): |
| 191 | # Reopens an already open file and changes the execute bit to match |
| 192 | # the execute bit setting in the passed in mode. |
| 193 | |
| 194 | p4Type = "+x" |
| 195 | |
| 196 | if not isModeExec(mode): |
| 197 | p4Type = getP4OpenedType(file) |
| 198 | p4Type = re.sub('^([cku]?)x(.*)', '\\1\\2', p4Type) |
| 199 | p4Type = re.sub('(.*?\+.*?)x(.*?)', '\\1\\2', p4Type) |
| 200 | if p4Type[-1] == "+": |
| 201 | p4Type = p4Type[0:-1] |
| 202 | |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 203 | p4_reopen(p4Type, file) |
Chris Pettitt | c65b670 | 2007-11-01 20:43:14 -0700 | [diff] [blame] | 204 | |
| 205 | def getP4OpenedType(file): |
| 206 | # Returns the perforce file type for the given file. |
| 207 | |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 208 | result = p4_read_pipe(["opened", file]) |
Marius Storm-Olsen | f3e5ae4 | 2008-03-28 15:40:40 +0100 | [diff] [blame] | 209 | match = re.match(".*\((.+)\)\r?$", result) |
Chris Pettitt | c65b670 | 2007-11-01 20:43:14 -0700 | [diff] [blame] | 210 | if match: |
| 211 | return match.group(1) |
| 212 | else: |
Marius Storm-Olsen | f3e5ae4 | 2008-03-28 15:40:40 +0100 | [diff] [blame] | 213 | die("Could not determine file type for %s (result: '%s')" % (file, result)) |
Chris Pettitt | c65b670 | 2007-11-01 20:43:14 -0700 | [diff] [blame] | 214 | |
Chris Pettitt | b43b0a3 | 2007-11-01 20:43:13 -0700 | [diff] [blame] | 215 | def diffTreePattern(): |
| 216 | # This is a simple generator for the diff tree regex pattern. This could be |
| 217 | # a class variable if this and parseDiffTreeEntry were a part of a class. |
| 218 | pattern = re.compile(':(\d+) (\d+) (\w+) (\w+) ([A-Z])(\d+)?\t(.*?)((\t(.*))|$)') |
| 219 | while True: |
| 220 | yield pattern |
| 221 | |
| 222 | def parseDiffTreeEntry(entry): |
| 223 | """Parses a single diff tree entry into its component elements. |
| 224 | |
| 225 | See git-diff-tree(1) manpage for details about the format of the diff |
| 226 | output. This method returns a dictionary with the following elements: |
| 227 | |
| 228 | src_mode - The mode of the source file |
| 229 | dst_mode - The mode of the destination file |
| 230 | src_sha1 - The sha1 for the source file |
| 231 | dst_sha1 - The sha1 fr the destination file |
| 232 | status - The one letter status of the diff (i.e. 'A', 'M', 'D', etc) |
| 233 | status_score - The score for the status (applicable for 'C' and 'R' |
| 234 | statuses). This is None if there is no score. |
| 235 | src - The path for the source file. |
| 236 | dst - The path for the destination file. This is only present for |
| 237 | copy or renames. If it is not present, this is None. |
| 238 | |
| 239 | If the pattern is not matched, None is returned.""" |
| 240 | |
| 241 | match = diffTreePattern().next().match(entry) |
| 242 | if match: |
| 243 | return { |
| 244 | 'src_mode': match.group(1), |
| 245 | 'dst_mode': match.group(2), |
| 246 | 'src_sha1': match.group(3), |
| 247 | 'dst_sha1': match.group(4), |
| 248 | 'status': match.group(5), |
| 249 | 'status_score': match.group(6), |
| 250 | 'src': match.group(7), |
| 251 | 'dst': match.group(10) |
| 252 | } |
| 253 | return None |
| 254 | |
Chris Pettitt | c65b670 | 2007-11-01 20:43:14 -0700 | [diff] [blame] | 255 | def isModeExec(mode): |
| 256 | # Returns True if the given git mode represents an executable file, |
| 257 | # otherwise False. |
| 258 | return mode[-3:] == "755" |
| 259 | |
| 260 | def isModeExecChanged(src_mode, dst_mode): |
| 261 | return isModeExec(src_mode) != isModeExec(dst_mode) |
| 262 | |
Luke Diamand | b932705 | 2009-07-30 00:13:46 +0100 | [diff] [blame] | 263 | def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None): |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 264 | |
| 265 | if isinstance(cmd,basestring): |
| 266 | cmd = "-G " + cmd |
| 267 | expand = True |
| 268 | else: |
| 269 | cmd = ["-G"] + cmd |
| 270 | expand = False |
| 271 | |
| 272 | cmd = p4_build_cmd(cmd) |
Han-Wen Nienhuys | 6a49f8e | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 273 | if verbose: |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 274 | sys.stderr.write("Opening pipe: %s\n" % str(cmd)) |
Scott Lamb | 9f90c73 | 2007-07-15 20:58:10 -0700 | [diff] [blame] | 275 | |
| 276 | # Use a temporary file to avoid deadlocks without |
| 277 | # subprocess.communicate(), which would put another copy |
| 278 | # of stdout into memory. |
| 279 | stdin_file = None |
| 280 | if stdin is not None: |
| 281 | stdin_file = tempfile.TemporaryFile(prefix='p4-stdin', mode=stdin_mode) |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 282 | if isinstance(stdin,basestring): |
| 283 | stdin_file.write(stdin) |
| 284 | else: |
| 285 | for i in stdin: |
| 286 | stdin_file.write(i + '\n') |
Scott Lamb | 9f90c73 | 2007-07-15 20:58:10 -0700 | [diff] [blame] | 287 | stdin_file.flush() |
| 288 | stdin_file.seek(0) |
| 289 | |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 290 | p4 = subprocess.Popen(cmd, |
| 291 | shell=expand, |
Scott Lamb | 9f90c73 | 2007-07-15 20:58:10 -0700 | [diff] [blame] | 292 | stdin=stdin_file, |
| 293 | stdout=subprocess.PIPE) |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 294 | |
| 295 | result = [] |
| 296 | try: |
| 297 | while True: |
Scott Lamb | 9f90c73 | 2007-07-15 20:58:10 -0700 | [diff] [blame] | 298 | entry = marshal.load(p4.stdout) |
Andrew Garber | c3f6163 | 2011-04-07 02:01:21 -0400 | [diff] [blame] | 299 | if cb is not None: |
| 300 | cb(entry) |
| 301 | else: |
| 302 | result.append(entry) |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 303 | except EOFError: |
| 304 | pass |
Scott Lamb | 9f90c73 | 2007-07-15 20:58:10 -0700 | [diff] [blame] | 305 | exitCode = p4.wait() |
| 306 | if exitCode != 0: |
Simon Hausmann | ac3e0d7 | 2007-05-23 23:32:32 +0200 | [diff] [blame] | 307 | entry = {} |
| 308 | entry["p4ExitCode"] = exitCode |
| 309 | result.append(entry) |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 310 | |
| 311 | return result |
| 312 | |
| 313 | def p4Cmd(cmd): |
| 314 | list = p4CmdList(cmd) |
| 315 | result = {} |
| 316 | for entry in list: |
| 317 | result.update(entry) |
| 318 | return result; |
| 319 | |
Simon Hausmann | cb2c9db | 2007-03-24 09:15:11 +0100 | [diff] [blame] | 320 | def p4Where(depotPath): |
| 321 | if not depotPath.endswith("/"): |
| 322 | depotPath += "/" |
Tor Arvid Lund | 7f705dc | 2008-12-04 14:37:33 +0100 | [diff] [blame] | 323 | depotPath = depotPath + "..." |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 324 | outputList = p4CmdList(["where", depotPath]) |
Tor Arvid Lund | 7f705dc | 2008-12-04 14:37:33 +0100 | [diff] [blame] | 325 | output = None |
| 326 | for entry in outputList: |
Tor Arvid Lund | 75bc957 | 2008-12-09 16:41:50 +0100 | [diff] [blame] | 327 | if "depotFile" in entry: |
| 328 | if entry["depotFile"] == depotPath: |
| 329 | output = entry |
| 330 | break |
| 331 | elif "data" in entry: |
| 332 | data = entry.get("data") |
| 333 | space = data.find(" ") |
| 334 | if data[:space] == depotPath: |
| 335 | output = entry |
| 336 | break |
Tor Arvid Lund | 7f705dc | 2008-12-04 14:37:33 +0100 | [diff] [blame] | 337 | if output == None: |
| 338 | return "" |
Simon Hausmann | dc52403 | 2007-05-21 09:34:56 +0200 | [diff] [blame] | 339 | if output["code"] == "error": |
| 340 | return "" |
Simon Hausmann | cb2c9db | 2007-03-24 09:15:11 +0100 | [diff] [blame] | 341 | clientPath = "" |
| 342 | if "path" in output: |
| 343 | clientPath = output.get("path") |
| 344 | elif "data" in output: |
| 345 | data = output.get("data") |
| 346 | lastSpace = data.rfind(" ") |
| 347 | clientPath = data[lastSpace + 1:] |
| 348 | |
| 349 | if clientPath.endswith("..."): |
| 350 | clientPath = clientPath[:-3] |
| 351 | return clientPath |
| 352 | |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 353 | def currentGitBranch(): |
Han-Wen Nienhuys | b25b206 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 354 | return read_pipe("git name-rev HEAD").split(" ")[1].strip() |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 355 | |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 356 | def isValidGitDir(path): |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 357 | if (os.path.exists(path + "/HEAD") |
| 358 | and os.path.exists(path + "/refs") and os.path.exists(path + "/objects")): |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 359 | return True; |
| 360 | return False |
| 361 | |
Simon Hausmann | 463e8af | 2007-05-17 09:13:54 +0200 | [diff] [blame] | 362 | def parseRevision(ref): |
Han-Wen Nienhuys | b25b206 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 363 | return read_pipe("git rev-parse %s" % ref).strip() |
Simon Hausmann | 463e8af | 2007-05-17 09:13:54 +0200 | [diff] [blame] | 364 | |
Pete Wyckoff | 28755db | 2011-12-24 21:07:40 -0500 | [diff] [blame^] | 365 | def branchExists(ref): |
| 366 | rev = read_pipe(["git", "rev-parse", "-q", "--verify", ref], |
| 367 | ignore_error=True) |
| 368 | return len(rev) > 0 |
| 369 | |
Simon Hausmann | 6ae8de8 | 2007-03-22 21:10:25 +0100 | [diff] [blame] | 370 | def extractLogMessageFromGitCommit(commit): |
| 371 | logMessage = "" |
Han-Wen Nienhuys | b016d39 | 2007-05-23 17:10:46 -0300 | [diff] [blame] | 372 | |
| 373 | ## fixme: title is first line of commit, not 1st paragraph. |
Simon Hausmann | 6ae8de8 | 2007-03-22 21:10:25 +0100 | [diff] [blame] | 374 | foundTitle = False |
Han-Wen Nienhuys | b016d39 | 2007-05-23 17:10:46 -0300 | [diff] [blame] | 375 | for log in read_pipe_lines("git cat-file commit %s" % commit): |
Simon Hausmann | 6ae8de8 | 2007-03-22 21:10:25 +0100 | [diff] [blame] | 376 | if not foundTitle: |
| 377 | if len(log) == 1: |
Simon Hausmann | 1c09418 | 2007-05-01 23:15:48 +0200 | [diff] [blame] | 378 | foundTitle = True |
Simon Hausmann | 6ae8de8 | 2007-03-22 21:10:25 +0100 | [diff] [blame] | 379 | continue |
| 380 | |
| 381 | logMessage += log |
| 382 | return logMessage |
| 383 | |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 384 | def extractSettingsGitLog(log): |
Simon Hausmann | 6ae8de8 | 2007-03-22 21:10:25 +0100 | [diff] [blame] | 385 | values = {} |
| 386 | for line in log.split("\n"): |
| 387 | line = line.strip() |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 388 | m = re.search (r"^ *\[git-p4: (.*)\]$", line) |
| 389 | if not m: |
| 390 | continue |
Simon Hausmann | 6ae8de8 | 2007-03-22 21:10:25 +0100 | [diff] [blame] | 391 | |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 392 | assignments = m.group(1).split (':') |
| 393 | for a in assignments: |
| 394 | vals = a.split ('=') |
| 395 | key = vals[0].strip() |
| 396 | val = ('='.join (vals[1:])).strip() |
| 397 | if val.endswith ('\"') and val.startswith('"'): |
| 398 | val = val[1:-1] |
| 399 | |
| 400 | values[key] = val |
| 401 | |
Simon Hausmann | 845b42c | 2007-06-07 09:19:34 +0200 | [diff] [blame] | 402 | paths = values.get("depot-paths") |
| 403 | if not paths: |
| 404 | paths = values.get("depot-path") |
Simon Hausmann | a3fdd57 | 2007-06-07 22:54:32 +0200 | [diff] [blame] | 405 | if paths: |
| 406 | values['depot-paths'] = paths.split(',') |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 407 | return values |
Simon Hausmann | 6ae8de8 | 2007-03-22 21:10:25 +0100 | [diff] [blame] | 408 | |
Simon Hausmann | 8136a63 | 2007-03-22 21:27:14 +0100 | [diff] [blame] | 409 | def gitBranchExists(branch): |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 410 | proc = subprocess.Popen(["git", "rev-parse", branch], |
| 411 | stderr=subprocess.PIPE, stdout=subprocess.PIPE); |
Simon Hausmann | caace11 | 2007-05-15 14:57:57 +0200 | [diff] [blame] | 412 | return proc.wait() == 0; |
Simon Hausmann | 8136a63 | 2007-03-22 21:27:14 +0100 | [diff] [blame] | 413 | |
John Chapman | 36bd844 | 2008-11-08 14:22:49 +1100 | [diff] [blame] | 414 | _gitConfig = {} |
Tor Arvid Lund | 99f790f | 2011-03-15 13:08:01 +0100 | [diff] [blame] | 415 | def gitConfig(key, args = None): # set args to "--bool", for instance |
John Chapman | 36bd844 | 2008-11-08 14:22:49 +1100 | [diff] [blame] | 416 | if not _gitConfig.has_key(key): |
Tor Arvid Lund | 99f790f | 2011-03-15 13:08:01 +0100 | [diff] [blame] | 417 | argsFilter = "" |
| 418 | if args != None: |
| 419 | argsFilter = "%s " % args |
| 420 | cmd = "git config %s%s" % (argsFilter, key) |
| 421 | _gitConfig[key] = read_pipe(cmd, ignore_error=True).strip() |
John Chapman | 36bd844 | 2008-11-08 14:22:49 +1100 | [diff] [blame] | 422 | return _gitConfig[key] |
Simon Hausmann | 0126510 | 2007-05-25 10:36:10 +0200 | [diff] [blame] | 423 | |
Vitor Antunes | 7199cf1 | 2011-08-19 00:44:05 +0100 | [diff] [blame] | 424 | def gitConfigList(key): |
| 425 | if not _gitConfig.has_key(key): |
| 426 | _gitConfig[key] = read_pipe("git config --get-all %s" % key, ignore_error=True).strip().split(os.linesep) |
| 427 | return _gitConfig[key] |
| 428 | |
Simon Hausmann | 062410b | 2007-07-18 10:56:31 +0200 | [diff] [blame] | 429 | def p4BranchesInGit(branchesAreInRemotes = True): |
| 430 | branches = {} |
| 431 | |
| 432 | cmdline = "git rev-parse --symbolic " |
| 433 | if branchesAreInRemotes: |
| 434 | cmdline += " --remotes" |
| 435 | else: |
| 436 | cmdline += " --branches" |
| 437 | |
| 438 | for line in read_pipe_lines(cmdline): |
| 439 | line = line.strip() |
| 440 | |
| 441 | ## only import to p4/ |
| 442 | if not line.startswith('p4/') or line == "p4/HEAD": |
| 443 | continue |
| 444 | branch = line |
| 445 | |
| 446 | # strip off p4 |
| 447 | branch = re.sub ("^p4/", "", line) |
| 448 | |
| 449 | branches[branch] = parseRevision(line) |
| 450 | return branches |
| 451 | |
Simon Hausmann | 9ceab36 | 2007-06-22 00:01:57 +0200 | [diff] [blame] | 452 | def findUpstreamBranchPoint(head = "HEAD"): |
Simon Hausmann | 86506fe | 2007-07-18 12:40:12 +0200 | [diff] [blame] | 453 | branches = p4BranchesInGit() |
| 454 | # map from depot-path to branch name |
| 455 | branchByDepotPath = {} |
| 456 | for branch in branches.keys(): |
| 457 | tip = branches[branch] |
| 458 | log = extractLogMessageFromGitCommit(tip) |
| 459 | settings = extractSettingsGitLog(log) |
| 460 | if settings.has_key("depot-paths"): |
| 461 | paths = ",".join(settings["depot-paths"]) |
| 462 | branchByDepotPath[paths] = "remotes/p4/" + branch |
| 463 | |
Simon Hausmann | 27d2d81 | 2007-06-12 14:31:59 +0200 | [diff] [blame] | 464 | settings = None |
Simon Hausmann | 27d2d81 | 2007-06-12 14:31:59 +0200 | [diff] [blame] | 465 | parent = 0 |
| 466 | while parent < 65535: |
Simon Hausmann | 9ceab36 | 2007-06-22 00:01:57 +0200 | [diff] [blame] | 467 | commit = head + "~%s" % parent |
Simon Hausmann | 27d2d81 | 2007-06-12 14:31:59 +0200 | [diff] [blame] | 468 | log = extractLogMessageFromGitCommit(commit) |
| 469 | settings = extractSettingsGitLog(log) |
Simon Hausmann | 86506fe | 2007-07-18 12:40:12 +0200 | [diff] [blame] | 470 | if settings.has_key("depot-paths"): |
| 471 | paths = ",".join(settings["depot-paths"]) |
| 472 | if branchByDepotPath.has_key(paths): |
| 473 | return [branchByDepotPath[paths], settings] |
Simon Hausmann | 27d2d81 | 2007-06-12 14:31:59 +0200 | [diff] [blame] | 474 | |
Simon Hausmann | 86506fe | 2007-07-18 12:40:12 +0200 | [diff] [blame] | 475 | parent = parent + 1 |
Simon Hausmann | 27d2d81 | 2007-06-12 14:31:59 +0200 | [diff] [blame] | 476 | |
Simon Hausmann | 86506fe | 2007-07-18 12:40:12 +0200 | [diff] [blame] | 477 | return ["", settings] |
Simon Hausmann | 27d2d81 | 2007-06-12 14:31:59 +0200 | [diff] [blame] | 478 | |
Simon Hausmann | 5ca4461 | 2007-08-24 17:44:16 +0200 | [diff] [blame] | 479 | def createOrUpdateBranchesFromOrigin(localRefPrefix = "refs/remotes/p4/", silent=True): |
| 480 | if not silent: |
| 481 | print ("Creating/updating branch(es) in %s based on origin branch(es)" |
| 482 | % localRefPrefix) |
| 483 | |
| 484 | originPrefix = "origin/p4/" |
| 485 | |
| 486 | for line in read_pipe_lines("git rev-parse --symbolic --remotes"): |
| 487 | line = line.strip() |
| 488 | if (not line.startswith(originPrefix)) or line.endswith("HEAD"): |
| 489 | continue |
| 490 | |
| 491 | headName = line[len(originPrefix):] |
| 492 | remoteHead = localRefPrefix + headName |
| 493 | originHead = line |
| 494 | |
| 495 | original = extractSettingsGitLog(extractLogMessageFromGitCommit(originHead)) |
| 496 | if (not original.has_key('depot-paths') |
| 497 | or not original.has_key('change')): |
| 498 | continue |
| 499 | |
| 500 | update = False |
| 501 | if not gitBranchExists(remoteHead): |
| 502 | if verbose: |
| 503 | print "creating %s" % remoteHead |
| 504 | update = True |
| 505 | else: |
| 506 | settings = extractSettingsGitLog(extractLogMessageFromGitCommit(remoteHead)) |
| 507 | if settings.has_key('change') > 0: |
| 508 | if settings['depot-paths'] == original['depot-paths']: |
| 509 | originP4Change = int(original['change']) |
| 510 | p4Change = int(settings['change']) |
| 511 | if originP4Change > p4Change: |
| 512 | print ("%s (%s) is newer than %s (%s). " |
| 513 | "Updating p4 branch from origin." |
| 514 | % (originHead, originP4Change, |
| 515 | remoteHead, p4Change)) |
| 516 | update = True |
| 517 | else: |
| 518 | print ("Ignoring: %s was imported from %s while " |
| 519 | "%s was imported from %s" |
| 520 | % (originHead, ','.join(original['depot-paths']), |
| 521 | remoteHead, ','.join(settings['depot-paths']))) |
| 522 | |
| 523 | if update: |
| 524 | system("git update-ref %s %s" % (remoteHead, originHead)) |
| 525 | |
| 526 | def originP4BranchesExist(): |
| 527 | return gitBranchExists("origin") or gitBranchExists("origin/p4") or gitBranchExists("origin/p4/master") |
| 528 | |
Simon Hausmann | 4f6432d | 2007-08-26 15:56:36 +0200 | [diff] [blame] | 529 | def p4ChangesForPaths(depotPaths, changeRange): |
| 530 | assert depotPaths |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 531 | cmd = ['changes'] |
| 532 | for p in depotPaths: |
| 533 | cmd += ["%s...%s" % (p, changeRange)] |
| 534 | output = p4_read_pipe_lines(cmd) |
Simon Hausmann | 4f6432d | 2007-08-26 15:56:36 +0200 | [diff] [blame] | 535 | |
Pete Wyckoff | b4b0ba0 | 2009-02-18 13:12:14 -0500 | [diff] [blame] | 536 | changes = {} |
Simon Hausmann | 4f6432d | 2007-08-26 15:56:36 +0200 | [diff] [blame] | 537 | for line in output: |
Andrew Garber | c3f6163 | 2011-04-07 02:01:21 -0400 | [diff] [blame] | 538 | changeNum = int(line.split(" ")[1]) |
| 539 | changes[changeNum] = True |
Simon Hausmann | 4f6432d | 2007-08-26 15:56:36 +0200 | [diff] [blame] | 540 | |
Pete Wyckoff | b4b0ba0 | 2009-02-18 13:12:14 -0500 | [diff] [blame] | 541 | changelist = changes.keys() |
| 542 | changelist.sort() |
| 543 | return changelist |
Simon Hausmann | 4f6432d | 2007-08-26 15:56:36 +0200 | [diff] [blame] | 544 | |
Tor Arvid Lund | d53de8b | 2011-03-15 13:08:02 +0100 | [diff] [blame] | 545 | def p4PathStartsWith(path, prefix): |
| 546 | # This method tries to remedy a potential mixed-case issue: |
| 547 | # |
| 548 | # If UserA adds //depot/DirA/file1 |
| 549 | # and UserB adds //depot/dira/file2 |
| 550 | # |
| 551 | # we may or may not have a problem. If you have core.ignorecase=true, |
| 552 | # we treat DirA and dira as the same directory |
| 553 | ignorecase = gitConfig("core.ignorecase", "--bool") == "true" |
| 554 | if ignorecase: |
| 555 | return path.lower().startswith(prefix.lower()) |
| 556 | return path.startswith(prefix) |
| 557 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 558 | class Command: |
| 559 | def __init__(self): |
| 560 | self.usage = "usage: %prog [options]" |
Simon Hausmann | 8910ac0 | 2007-03-26 08:18:55 +0200 | [diff] [blame] | 561 | self.needsGit = True |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 562 | |
Luke Diamand | 3ea2cfd | 2011-04-21 20:50:23 +0100 | [diff] [blame] | 563 | class P4UserMap: |
| 564 | def __init__(self): |
| 565 | self.userMapFromPerforceServer = False |
| 566 | |
| 567 | def getUserCacheFilename(self): |
| 568 | home = os.environ.get("HOME", os.environ.get("USERPROFILE")) |
| 569 | return home + "/.gitp4-usercache.txt" |
| 570 | |
| 571 | def getUserMapFromPerforceServer(self): |
| 572 | if self.userMapFromPerforceServer: |
| 573 | return |
| 574 | self.users = {} |
| 575 | self.emails = {} |
| 576 | |
| 577 | for output in p4CmdList("users"): |
| 578 | if not output.has_key("User"): |
| 579 | continue |
| 580 | self.users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">" |
| 581 | self.emails[output["Email"]] = output["User"] |
| 582 | |
| 583 | |
| 584 | s = '' |
| 585 | for (key, val) in self.users.items(): |
| 586 | s += "%s\t%s\n" % (key.expandtabs(1), val.expandtabs(1)) |
| 587 | |
| 588 | open(self.getUserCacheFilename(), "wb").write(s) |
| 589 | self.userMapFromPerforceServer = True |
| 590 | |
| 591 | def loadUserMapFromCache(self): |
| 592 | self.users = {} |
| 593 | self.userMapFromPerforceServer = False |
| 594 | try: |
| 595 | cache = open(self.getUserCacheFilename(), "rb") |
| 596 | lines = cache.readlines() |
| 597 | cache.close() |
| 598 | for line in lines: |
| 599 | entry = line.strip().split("\t") |
| 600 | self.users[entry[0]] = entry[1] |
| 601 | except IOError: |
| 602 | self.getUserMapFromPerforceServer() |
| 603 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 604 | class P4Debug(Command): |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 605 | def __init__(self): |
Simon Hausmann | 6ae8de8 | 2007-03-22 21:10:25 +0100 | [diff] [blame] | 606 | Command.__init__(self) |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 607 | self.options = [ |
Han-Wen Nienhuys | b1ce944 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 608 | optparse.make_option("--verbose", dest="verbose", action="store_true", |
| 609 | default=False), |
Han-Wen Nienhuys | 4addad2 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 610 | ] |
Simon Hausmann | c8c3911 | 2007-03-19 21:02:30 +0100 | [diff] [blame] | 611 | self.description = "A tool to debug the output of p4 -G." |
Simon Hausmann | 8910ac0 | 2007-03-26 08:18:55 +0200 | [diff] [blame] | 612 | self.needsGit = False |
Han-Wen Nienhuys | b1ce944 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 613 | self.verbose = False |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 614 | |
| 615 | def run(self, args): |
Han-Wen Nienhuys | b1ce944 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 616 | j = 0 |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 617 | for output in p4CmdList(args): |
Han-Wen Nienhuys | b1ce944 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 618 | print 'Element: %d' % j |
| 619 | j += 1 |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 620 | print output |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 621 | return True |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 622 | |
Simon Hausmann | 5834684 | 2007-05-21 22:57:06 +0200 | [diff] [blame] | 623 | class P4RollBack(Command): |
| 624 | def __init__(self): |
| 625 | Command.__init__(self) |
| 626 | self.options = [ |
Simon Hausmann | 0c66a78 | 2007-05-23 20:07:57 +0200 | [diff] [blame] | 627 | optparse.make_option("--verbose", dest="verbose", action="store_true"), |
| 628 | optparse.make_option("--local", dest="rollbackLocalBranches", action="store_true") |
Simon Hausmann | 5834684 | 2007-05-21 22:57:06 +0200 | [diff] [blame] | 629 | ] |
| 630 | self.description = "A tool to debug the multi-branch import. Don't use :)" |
Simon Hausmann | 52102d4 | 2007-05-21 23:44:24 +0200 | [diff] [blame] | 631 | self.verbose = False |
Simon Hausmann | 0c66a78 | 2007-05-23 20:07:57 +0200 | [diff] [blame] | 632 | self.rollbackLocalBranches = False |
Simon Hausmann | 5834684 | 2007-05-21 22:57:06 +0200 | [diff] [blame] | 633 | |
| 634 | def run(self, args): |
| 635 | if len(args) != 1: |
| 636 | return False |
| 637 | maxChange = int(args[0]) |
Simon Hausmann | 0c66a78 | 2007-05-23 20:07:57 +0200 | [diff] [blame] | 638 | |
Simon Hausmann | ad192f2 | 2007-05-23 23:44:19 +0200 | [diff] [blame] | 639 | if "p4ExitCode" in p4Cmd("changes -m 1"): |
Simon Hausmann | 66a2f52 | 2007-05-23 23:40:48 +0200 | [diff] [blame] | 640 | die("Problems executing p4"); |
| 641 | |
Simon Hausmann | 0c66a78 | 2007-05-23 20:07:57 +0200 | [diff] [blame] | 642 | if self.rollbackLocalBranches: |
| 643 | refPrefix = "refs/heads/" |
Han-Wen Nienhuys | b016d39 | 2007-05-23 17:10:46 -0300 | [diff] [blame] | 644 | lines = read_pipe_lines("git rev-parse --symbolic --branches") |
Simon Hausmann | 0c66a78 | 2007-05-23 20:07:57 +0200 | [diff] [blame] | 645 | else: |
| 646 | refPrefix = "refs/remotes/" |
Han-Wen Nienhuys | b016d39 | 2007-05-23 17:10:46 -0300 | [diff] [blame] | 647 | lines = read_pipe_lines("git rev-parse --symbolic --remotes") |
Simon Hausmann | 0c66a78 | 2007-05-23 20:07:57 +0200 | [diff] [blame] | 648 | |
| 649 | for line in lines: |
| 650 | 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] | 651 | line = line.strip() |
| 652 | ref = refPrefix + line |
Simon Hausmann | 5834684 | 2007-05-21 22:57:06 +0200 | [diff] [blame] | 653 | log = extractLogMessageFromGitCommit(ref) |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 654 | settings = extractSettingsGitLog(log) |
| 655 | |
| 656 | depotPaths = settings['depot-paths'] |
| 657 | change = settings['change'] |
| 658 | |
Simon Hausmann | 5834684 | 2007-05-21 22:57:06 +0200 | [diff] [blame] | 659 | changed = False |
Simon Hausmann | 52102d4 | 2007-05-21 23:44:24 +0200 | [diff] [blame] | 660 | |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 661 | if len(p4Cmd("changes -m 1 " + ' '.join (['%s...@%s' % (p, maxChange) |
| 662 | for p in depotPaths]))) == 0: |
Simon Hausmann | 52102d4 | 2007-05-21 23:44:24 +0200 | [diff] [blame] | 663 | print "Branch %s did not exist at change %s, deleting." % (ref, maxChange) |
| 664 | system("git update-ref -d %s `git rev-parse %s`" % (ref, ref)) |
| 665 | continue |
| 666 | |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 667 | while change and int(change) > maxChange: |
Simon Hausmann | 5834684 | 2007-05-21 22:57:06 +0200 | [diff] [blame] | 668 | changed = True |
Simon Hausmann | 52102d4 | 2007-05-21 23:44:24 +0200 | [diff] [blame] | 669 | if self.verbose: |
| 670 | print "%s is at %s ; rewinding towards %s" % (ref, change, maxChange) |
Simon Hausmann | 5834684 | 2007-05-21 22:57:06 +0200 | [diff] [blame] | 671 | system("git update-ref %s \"%s^\"" % (ref, ref)) |
| 672 | log = extractLogMessageFromGitCommit(ref) |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 673 | settings = extractSettingsGitLog(log) |
| 674 | |
| 675 | |
| 676 | depotPaths = settings['depot-paths'] |
| 677 | change = settings['change'] |
Simon Hausmann | 5834684 | 2007-05-21 22:57:06 +0200 | [diff] [blame] | 678 | |
| 679 | if changed: |
Simon Hausmann | 52102d4 | 2007-05-21 23:44:24 +0200 | [diff] [blame] | 680 | print "%s rewound to %s" % (ref, change) |
Simon Hausmann | 5834684 | 2007-05-21 22:57:06 +0200 | [diff] [blame] | 681 | |
| 682 | return True |
| 683 | |
Luke Diamand | 3ea2cfd | 2011-04-21 20:50:23 +0100 | [diff] [blame] | 684 | class P4Submit(Command, P4UserMap): |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 685 | def __init__(self): |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 686 | Command.__init__(self) |
Luke Diamand | 3ea2cfd | 2011-04-21 20:50:23 +0100 | [diff] [blame] | 687 | P4UserMap.__init__(self) |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 688 | self.options = [ |
Han-Wen Nienhuys | 4addad2 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 689 | optparse.make_option("--verbose", dest="verbose", action="store_true"), |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 690 | optparse.make_option("--origin", dest="origin"), |
Vitor Antunes | ae90109 | 2011-02-20 01:18:24 +0000 | [diff] [blame] | 691 | optparse.make_option("-M", dest="detectRenames", action="store_true"), |
Luke Diamand | 3ea2cfd | 2011-04-21 20:50:23 +0100 | [diff] [blame] | 692 | # preserve the user, requires relevant p4 permissions |
| 693 | optparse.make_option("--preserve-user", dest="preserveUser", action="store_true"), |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 694 | ] |
| 695 | self.description = "Submit changes from git to the perforce depot." |
Simon Hausmann | c9b50e6 | 2007-03-29 19:15:24 +0200 | [diff] [blame] | 696 | self.usage += " [name of git branch to submit into perforce depot]" |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 697 | self.interactive = True |
Simon Hausmann | 9512497 | 2007-03-23 09:16:07 +0100 | [diff] [blame] | 698 | self.origin = "" |
Vitor Antunes | ae90109 | 2011-02-20 01:18:24 +0000 | [diff] [blame] | 699 | self.detectRenames = False |
Simon Hausmann | b0d10df | 2007-06-07 13:09:14 +0200 | [diff] [blame] | 700 | self.verbose = False |
Luke Diamand | 3ea2cfd | 2011-04-21 20:50:23 +0100 | [diff] [blame] | 701 | self.preserveUser = gitConfig("git-p4.preserveUser").lower() == "true" |
Marius Storm-Olsen | f7baba8 | 2007-06-07 14:07:01 +0200 | [diff] [blame] | 702 | self.isWindows = (platform.system() == "Windows") |
Luke Diamand | 848de9c | 2011-05-13 20:46:00 +0100 | [diff] [blame] | 703 | self.myP4UserId = None |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 704 | |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 705 | def check(self): |
| 706 | if len(p4CmdList("opened ...")) > 0: |
| 707 | die("You have files opened with perforce! Close them before starting the sync.") |
| 708 | |
Simon Hausmann | edae1e2 | 2008-02-19 09:29:06 +0100 | [diff] [blame] | 709 | # replaces everything between 'Description:' and the next P4 submit template field with the |
| 710 | # commit message |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 711 | def prepareLogMessage(self, template, message): |
| 712 | result = "" |
| 713 | |
Simon Hausmann | edae1e2 | 2008-02-19 09:29:06 +0100 | [diff] [blame] | 714 | inDescriptionSection = False |
| 715 | |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 716 | for line in template.split("\n"): |
| 717 | if line.startswith("#"): |
| 718 | result += line + "\n" |
| 719 | continue |
| 720 | |
Simon Hausmann | edae1e2 | 2008-02-19 09:29:06 +0100 | [diff] [blame] | 721 | if inDescriptionSection: |
Michael Horowitz | c9dbab0 | 2011-02-25 21:31:13 -0500 | [diff] [blame] | 722 | if line.startswith("Files:") or line.startswith("Jobs:"): |
Simon Hausmann | edae1e2 | 2008-02-19 09:29:06 +0100 | [diff] [blame] | 723 | inDescriptionSection = False |
| 724 | else: |
| 725 | continue |
| 726 | else: |
| 727 | if line.startswith("Description:"): |
| 728 | inDescriptionSection = True |
| 729 | line += "\n" |
| 730 | for messageLine in message.split("\n"): |
| 731 | line += "\t" + messageLine + "\n" |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 732 | |
Simon Hausmann | edae1e2 | 2008-02-19 09:29:06 +0100 | [diff] [blame] | 733 | result += line + "\n" |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 734 | |
| 735 | return result |
| 736 | |
Luke Diamand | 3ea2cfd | 2011-04-21 20:50:23 +0100 | [diff] [blame] | 737 | def p4UserForCommit(self,id): |
| 738 | # Return the tuple (perforce user,git email) for a given git commit id |
| 739 | self.getUserMapFromPerforceServer() |
| 740 | gitEmail = read_pipe("git log --max-count=1 --format='%%ae' %s" % id) |
| 741 | gitEmail = gitEmail.strip() |
| 742 | if not self.emails.has_key(gitEmail): |
| 743 | return (None,gitEmail) |
| 744 | else: |
| 745 | return (self.emails[gitEmail],gitEmail) |
| 746 | |
| 747 | def checkValidP4Users(self,commits): |
| 748 | # check if any git authors cannot be mapped to p4 users |
| 749 | for id in commits: |
| 750 | (user,email) = self.p4UserForCommit(id) |
| 751 | if not user: |
| 752 | msg = "Cannot find p4 user for email %s in commit %s." % (email, id) |
| 753 | if gitConfig('git-p4.allowMissingP4Users').lower() == "true": |
| 754 | print "%s" % msg |
| 755 | else: |
| 756 | die("Error: %s\nSet git-p4.allowMissingP4Users to true to allow this." % msg) |
| 757 | |
| 758 | def lastP4Changelist(self): |
| 759 | # Get back the last changelist number submitted in this client spec. This |
| 760 | # then gets used to patch up the username in the change. If the same |
| 761 | # client spec is being used by multiple processes then this might go |
| 762 | # wrong. |
| 763 | results = p4CmdList("client -o") # find the current client |
| 764 | client = None |
| 765 | for r in results: |
| 766 | if r.has_key('Client'): |
| 767 | client = r['Client'] |
| 768 | break |
| 769 | if not client: |
| 770 | die("could not get client spec") |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 771 | results = p4CmdList(["changes", "-c", client, "-m", "1"]) |
Luke Diamand | 3ea2cfd | 2011-04-21 20:50:23 +0100 | [diff] [blame] | 772 | for r in results: |
| 773 | if r.has_key('change'): |
| 774 | return r['change'] |
| 775 | die("Could not get changelist number for last submit - cannot patch up user details") |
| 776 | |
| 777 | def modifyChangelistUser(self, changelist, newUser): |
| 778 | # fixup the user field of a changelist after it has been submitted. |
| 779 | changes = p4CmdList("change -o %s" % changelist) |
Luke Diamand | ecdba36 | 2011-05-07 11:19:43 +0100 | [diff] [blame] | 780 | if len(changes) != 1: |
| 781 | die("Bad output from p4 change modifying %s to user %s" % |
| 782 | (changelist, newUser)) |
| 783 | |
| 784 | c = changes[0] |
| 785 | if c['User'] == newUser: return # nothing to do |
| 786 | c['User'] = newUser |
| 787 | input = marshal.dumps(c) |
| 788 | |
Luke Diamand | 3ea2cfd | 2011-04-21 20:50:23 +0100 | [diff] [blame] | 789 | result = p4CmdList("change -f -i", stdin=input) |
| 790 | for r in result: |
| 791 | if r.has_key('code'): |
| 792 | if r['code'] == 'error': |
| 793 | die("Could not modify user field of changelist %s to %s:%s" % (changelist, newUser, r['data'])) |
| 794 | if r.has_key('data'): |
| 795 | print("Updated user field for changelist %s to %s" % (changelist, newUser)) |
| 796 | return |
| 797 | die("Could not modify user field of changelist %s to %s" % (changelist, newUser)) |
| 798 | |
| 799 | def canChangeChangelists(self): |
| 800 | # check to see if we have p4 admin or super-user permissions, either of |
| 801 | # which are required to modify changelists. |
Luke Diamand | ecdba36 | 2011-05-07 11:19:43 +0100 | [diff] [blame] | 802 | results = p4CmdList("protects %s" % self.depotPath) |
Luke Diamand | 3ea2cfd | 2011-04-21 20:50:23 +0100 | [diff] [blame] | 803 | for r in results: |
| 804 | if r.has_key('perm'): |
| 805 | if r['perm'] == 'admin': |
| 806 | return 1 |
| 807 | if r['perm'] == 'super': |
| 808 | return 1 |
| 809 | return 0 |
| 810 | |
Luke Diamand | 848de9c | 2011-05-13 20:46:00 +0100 | [diff] [blame] | 811 | def p4UserId(self): |
| 812 | if self.myP4UserId: |
| 813 | return self.myP4UserId |
| 814 | |
| 815 | results = p4CmdList("user -o") |
| 816 | for r in results: |
| 817 | if r.has_key('User'): |
| 818 | self.myP4UserId = r['User'] |
| 819 | return r['User'] |
| 820 | die("Could not find your p4 user id") |
| 821 | |
| 822 | def p4UserIsMe(self, p4User): |
| 823 | # return True if the given p4 user is actually me |
| 824 | me = self.p4UserId() |
| 825 | if not p4User or p4User != me: |
| 826 | return False |
| 827 | else: |
| 828 | return True |
| 829 | |
Simon Hausmann | ea99c3a | 2007-08-08 17:06:55 +0200 | [diff] [blame] | 830 | def prepareSubmitTemplate(self): |
| 831 | # remove lines in the Files section that show changes to files outside the depot path we're committing into |
| 832 | template = "" |
| 833 | inFilesSection = False |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 834 | for line in p4_read_pipe_lines(['change', '-o']): |
Marius Storm-Olsen | f3e5ae4 | 2008-03-28 15:40:40 +0100 | [diff] [blame] | 835 | if line.endswith("\r\n"): |
| 836 | line = line[:-2] + "\n" |
Simon Hausmann | ea99c3a | 2007-08-08 17:06:55 +0200 | [diff] [blame] | 837 | if inFilesSection: |
| 838 | if line.startswith("\t"): |
| 839 | # path starts and ends with a tab |
| 840 | path = line[1:] |
| 841 | lastTab = path.rfind("\t") |
| 842 | if lastTab != -1: |
| 843 | path = path[:lastTab] |
Tor Arvid Lund | d53de8b | 2011-03-15 13:08:02 +0100 | [diff] [blame] | 844 | if not p4PathStartsWith(path, self.depotPath): |
Simon Hausmann | ea99c3a | 2007-08-08 17:06:55 +0200 | [diff] [blame] | 845 | continue |
| 846 | else: |
| 847 | inFilesSection = False |
| 848 | else: |
| 849 | if line.startswith("Files:"): |
| 850 | inFilesSection = True |
| 851 | |
| 852 | template += line |
| 853 | |
| 854 | return template |
| 855 | |
Pete Wyckoff | 7c766e5 | 2011-12-04 19:22:45 -0500 | [diff] [blame] | 856 | def edit_template(self, template_file): |
| 857 | """Invoke the editor to let the user change the submission |
| 858 | message. Return true if okay to continue with the submit.""" |
| 859 | |
| 860 | # if configured to skip the editing part, just submit |
| 861 | if gitConfig("git-p4.skipSubmitEdit") == "true": |
| 862 | return True |
| 863 | |
| 864 | # look at the modification time, to check later if the user saved |
| 865 | # the file |
| 866 | mtime = os.stat(template_file).st_mtime |
| 867 | |
| 868 | # invoke the editor |
| 869 | if os.environ.has_key("P4EDITOR"): |
| 870 | editor = os.environ.get("P4EDITOR") |
| 871 | else: |
| 872 | editor = read_pipe("git var GIT_EDITOR").strip() |
| 873 | system(editor + " " + template_file) |
| 874 | |
| 875 | # If the file was not saved, prompt to see if this patch should |
| 876 | # be skipped. But skip this verification step if configured so. |
| 877 | if gitConfig("git-p4.skipSubmitEditCheck") == "true": |
| 878 | return True |
| 879 | |
Pete Wyckoff | d165204 | 2011-12-17 12:39:03 -0500 | [diff] [blame] | 880 | # modification time updated means user saved the file |
| 881 | if os.stat(template_file).st_mtime > mtime: |
| 882 | return True |
| 883 | |
| 884 | while True: |
| 885 | response = raw_input("Submit template unchanged. Submit anyway? [y]es, [n]o (skip this patch) ") |
| 886 | if response == 'y': |
| 887 | return True |
| 888 | if response == 'n': |
| 889 | return False |
Pete Wyckoff | 7c766e5 | 2011-12-04 19:22:45 -0500 | [diff] [blame] | 890 | |
Han-Wen Nienhuys | 7cb5cbe | 2007-05-23 16:55:48 -0300 | [diff] [blame] | 891 | def applyCommit(self, id): |
Simon Hausmann | 0e36f2d | 2008-02-19 09:33:08 +0100 | [diff] [blame] | 892 | print "Applying %s" % (read_pipe("git log --max-count=1 --pretty=oneline %s" % id)) |
Vitor Antunes | ae90109 | 2011-02-20 01:18:24 +0000 | [diff] [blame] | 893 | |
Luke Diamand | 848de9c | 2011-05-13 20:46:00 +0100 | [diff] [blame] | 894 | (p4User, gitEmail) = self.p4UserForCommit(id) |
Luke Diamand | 3ea2cfd | 2011-04-21 20:50:23 +0100 | [diff] [blame] | 895 | |
Vitor Antunes | ae90109 | 2011-02-20 01:18:24 +0000 | [diff] [blame] | 896 | if not self.detectRenames: |
| 897 | # If not explicitly set check the config variable |
Vitor Antunes | 0a9feff | 2011-08-22 09:33:05 +0100 | [diff] [blame] | 898 | self.detectRenames = gitConfig("git-p4.detectRenames") |
Vitor Antunes | ae90109 | 2011-02-20 01:18:24 +0000 | [diff] [blame] | 899 | |
Vitor Antunes | 0a9feff | 2011-08-22 09:33:05 +0100 | [diff] [blame] | 900 | if self.detectRenames.lower() == "false" or self.detectRenames == "": |
| 901 | diffOpts = "" |
| 902 | elif self.detectRenames.lower() == "true": |
Vitor Antunes | ae90109 | 2011-02-20 01:18:24 +0000 | [diff] [blame] | 903 | diffOpts = "-M" |
| 904 | else: |
Vitor Antunes | 0a9feff | 2011-08-22 09:33:05 +0100 | [diff] [blame] | 905 | diffOpts = "-M%s" % self.detectRenames |
Vitor Antunes | ae90109 | 2011-02-20 01:18:24 +0000 | [diff] [blame] | 906 | |
Vitor Antunes | 0a9feff | 2011-08-22 09:33:05 +0100 | [diff] [blame] | 907 | detectCopies = gitConfig("git-p4.detectCopies") |
| 908 | if detectCopies.lower() == "true": |
Vitor Antunes | 4fddb41 | 2011-02-20 01:18:25 +0000 | [diff] [blame] | 909 | diffOpts += " -C" |
Vitor Antunes | 0a9feff | 2011-08-22 09:33:05 +0100 | [diff] [blame] | 910 | elif detectCopies != "" and detectCopies.lower() != "false": |
| 911 | diffOpts += " -C%s" % detectCopies |
Vitor Antunes | 4fddb41 | 2011-02-20 01:18:25 +0000 | [diff] [blame] | 912 | |
Vitor Antunes | 68cbcf1 | 2011-08-22 09:33:09 +0100 | [diff] [blame] | 913 | if gitConfig("git-p4.detectCopiesHarder", "--bool") == "true": |
Vitor Antunes | 4fddb41 | 2011-02-20 01:18:25 +0000 | [diff] [blame] | 914 | diffOpts += " --find-copies-harder" |
| 915 | |
Simon Hausmann | 0e36f2d | 2008-02-19 09:33:08 +0100 | [diff] [blame] | 916 | diff = read_pipe_lines("git diff-tree -r %s \"%s^\" \"%s\"" % (diffOpts, id, id)) |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 917 | filesToAdd = set() |
| 918 | filesToDelete = set() |
Simon Hausmann | d336c15 | 2007-05-16 09:41:26 +0200 | [diff] [blame] | 919 | editedFiles = set() |
Chris Pettitt | c65b670 | 2007-11-01 20:43:14 -0700 | [diff] [blame] | 920 | filesToChangeExecBit = {} |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 921 | for line in diff: |
Chris Pettitt | b43b0a3 | 2007-11-01 20:43:13 -0700 | [diff] [blame] | 922 | diff = parseDiffTreeEntry(line) |
| 923 | modifier = diff['status'] |
| 924 | path = diff['src'] |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 925 | if modifier == "M": |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 926 | p4_edit(path) |
Chris Pettitt | c65b670 | 2007-11-01 20:43:14 -0700 | [diff] [blame] | 927 | if isModeExecChanged(diff['src_mode'], diff['dst_mode']): |
| 928 | filesToChangeExecBit[path] = diff['dst_mode'] |
Simon Hausmann | d336c15 | 2007-05-16 09:41:26 +0200 | [diff] [blame] | 929 | editedFiles.add(path) |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 930 | elif modifier == "A": |
| 931 | filesToAdd.add(path) |
Chris Pettitt | c65b670 | 2007-11-01 20:43:14 -0700 | [diff] [blame] | 932 | filesToChangeExecBit[path] = diff['dst_mode'] |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 933 | if path in filesToDelete: |
| 934 | filesToDelete.remove(path) |
| 935 | elif modifier == "D": |
| 936 | filesToDelete.add(path) |
| 937 | if path in filesToAdd: |
| 938 | filesToAdd.remove(path) |
Vitor Antunes | 4fddb41 | 2011-02-20 01:18:25 +0000 | [diff] [blame] | 939 | elif modifier == "C": |
| 940 | src, dest = diff['src'], diff['dst'] |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 941 | p4_integrate(src, dest) |
Vitor Antunes | 4fddb41 | 2011-02-20 01:18:25 +0000 | [diff] [blame] | 942 | if diff['src_sha1'] != diff['dst_sha1']: |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 943 | p4_edit(dest) |
Vitor Antunes | 4fddb41 | 2011-02-20 01:18:25 +0000 | [diff] [blame] | 944 | if isModeExecChanged(diff['src_mode'], diff['dst_mode']): |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 945 | p4_edit(dest) |
Vitor Antunes | 4fddb41 | 2011-02-20 01:18:25 +0000 | [diff] [blame] | 946 | filesToChangeExecBit[dest] = diff['dst_mode'] |
| 947 | os.unlink(dest) |
| 948 | editedFiles.add(dest) |
Chris Pettitt | d9a5f25 | 2007-10-15 22:15:06 -0700 | [diff] [blame] | 949 | elif modifier == "R": |
Chris Pettitt | b43b0a3 | 2007-11-01 20:43:13 -0700 | [diff] [blame] | 950 | src, dest = diff['src'], diff['dst'] |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 951 | p4_integrate(src, dest) |
Vitor Antunes | ae90109 | 2011-02-20 01:18:24 +0000 | [diff] [blame] | 952 | if diff['src_sha1'] != diff['dst_sha1']: |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 953 | p4_edit(dest) |
Chris Pettitt | c65b670 | 2007-11-01 20:43:14 -0700 | [diff] [blame] | 954 | if isModeExecChanged(diff['src_mode'], diff['dst_mode']): |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 955 | p4_edit(dest) |
Chris Pettitt | c65b670 | 2007-11-01 20:43:14 -0700 | [diff] [blame] | 956 | filesToChangeExecBit[dest] = diff['dst_mode'] |
Chris Pettitt | d9a5f25 | 2007-10-15 22:15:06 -0700 | [diff] [blame] | 957 | os.unlink(dest) |
| 958 | editedFiles.add(dest) |
| 959 | filesToDelete.add(src) |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 960 | else: |
| 961 | die("unknown modifier %s for %s" % (modifier, path)) |
| 962 | |
Simon Hausmann | 0e36f2d | 2008-02-19 09:33:08 +0100 | [diff] [blame] | 963 | diffcmd = "git format-patch -k --stdout \"%s^\"..\"%s\"" % (id, id) |
Simon Hausmann | 47a130b | 2007-05-20 16:33:21 +0200 | [diff] [blame] | 964 | patchcmd = diffcmd + " | git apply " |
Simon Hausmann | c1b296b | 2007-05-20 16:55:05 +0200 | [diff] [blame] | 965 | tryPatchCmd = patchcmd + "--check -" |
| 966 | applyPatchCmd = patchcmd + "--check --apply -" |
Simon Hausmann | 51a2640 | 2007-04-15 09:59:56 +0200 | [diff] [blame] | 967 | |
Simon Hausmann | 47a130b | 2007-05-20 16:33:21 +0200 | [diff] [blame] | 968 | if os.system(tryPatchCmd) != 0: |
Simon Hausmann | 51a2640 | 2007-04-15 09:59:56 +0200 | [diff] [blame] | 969 | print "Unfortunately applying the change failed!" |
| 970 | print "What do you want to do?" |
| 971 | response = "x" |
| 972 | while response != "s" and response != "a" and response != "w": |
Han-Wen Nienhuys | cebdf5a | 2007-05-23 16:53:11 -0300 | [diff] [blame] | 973 | response = raw_input("[s]kip this patch / [a]pply the patch forcibly " |
| 974 | "and with .rej files / [w]rite the patch to a file (patch.txt) ") |
Simon Hausmann | 51a2640 | 2007-04-15 09:59:56 +0200 | [diff] [blame] | 975 | if response == "s": |
| 976 | print "Skipping! Good luck with the next patches..." |
Simon Hausmann | 2094714 | 2007-09-13 22:10:18 +0200 | [diff] [blame] | 977 | for f in editedFiles: |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 978 | p4_revert(f) |
Simon Hausmann | 2094714 | 2007-09-13 22:10:18 +0200 | [diff] [blame] | 979 | for f in filesToAdd: |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 980 | os.remove(f) |
Simon Hausmann | 51a2640 | 2007-04-15 09:59:56 +0200 | [diff] [blame] | 981 | return |
| 982 | elif response == "a": |
Simon Hausmann | 47a130b | 2007-05-20 16:33:21 +0200 | [diff] [blame] | 983 | os.system(applyPatchCmd) |
Simon Hausmann | 51a2640 | 2007-04-15 09:59:56 +0200 | [diff] [blame] | 984 | if len(filesToAdd) > 0: |
| 985 | print "You may also want to call p4 add on the following files:" |
| 986 | print " ".join(filesToAdd) |
| 987 | if len(filesToDelete): |
| 988 | print "The following files should be scheduled for deletion with p4 delete:" |
| 989 | print " ".join(filesToDelete) |
Han-Wen Nienhuys | cebdf5a | 2007-05-23 16:53:11 -0300 | [diff] [blame] | 990 | die("Please resolve and submit the conflict manually and " |
| 991 | + "continue afterwards with git-p4 submit --continue") |
Simon Hausmann | 51a2640 | 2007-04-15 09:59:56 +0200 | [diff] [blame] | 992 | elif response == "w": |
| 993 | system(diffcmd + " > patch.txt") |
| 994 | print "Patch saved to patch.txt in %s !" % self.clientPath |
Han-Wen Nienhuys | cebdf5a | 2007-05-23 16:53:11 -0300 | [diff] [blame] | 995 | die("Please resolve and submit the conflict manually and " |
| 996 | "continue afterwards with git-p4 submit --continue") |
Simon Hausmann | 51a2640 | 2007-04-15 09:59:56 +0200 | [diff] [blame] | 997 | |
Simon Hausmann | 47a130b | 2007-05-20 16:33:21 +0200 | [diff] [blame] | 998 | system(applyPatchCmd) |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 999 | |
| 1000 | for f in filesToAdd: |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 1001 | p4_add(f) |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 1002 | for f in filesToDelete: |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 1003 | p4_revert(f) |
| 1004 | p4_delete(f) |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 1005 | |
Chris Pettitt | c65b670 | 2007-11-01 20:43:14 -0700 | [diff] [blame] | 1006 | # Set/clear executable bits |
| 1007 | for f in filesToChangeExecBit.keys(): |
| 1008 | mode = filesToChangeExecBit[f] |
| 1009 | setP4ExecBit(f, mode) |
| 1010 | |
Simon Hausmann | 0e36f2d | 2008-02-19 09:33:08 +0100 | [diff] [blame] | 1011 | logMessage = extractLogMessageFromGitCommit(id) |
Simon Hausmann | 0e36f2d | 2008-02-19 09:33:08 +0100 | [diff] [blame] | 1012 | logMessage = logMessage.strip() |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 1013 | |
Simon Hausmann | ea99c3a | 2007-08-08 17:06:55 +0200 | [diff] [blame] | 1014 | template = self.prepareSubmitTemplate() |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 1015 | |
| 1016 | if self.interactive: |
| 1017 | submitTemplate = self.prepareLogMessage(template, logMessage) |
Luke Diamand | ecdba36 | 2011-05-07 11:19:43 +0100 | [diff] [blame] | 1018 | |
| 1019 | if self.preserveUser: |
| 1020 | submitTemplate = submitTemplate + ("\n######## Actual user %s, modified after commit\n" % p4User) |
| 1021 | |
Shawn Bohrer | 67abd41 | 2008-03-12 19:03:23 -0500 | [diff] [blame] | 1022 | if os.environ.has_key("P4DIFF"): |
| 1023 | del(os.environ["P4DIFF"]) |
Andrew Waters | 8b13026 | 2010-10-22 13:26:02 +0100 | [diff] [blame] | 1024 | diff = "" |
| 1025 | for editedFile in editedFiles: |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 1026 | diff += p4_read_pipe(['diff', '-du', editedFile]) |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 1027 | |
Marius Storm-Olsen | f3e5ae4 | 2008-03-28 15:40:40 +0100 | [diff] [blame] | 1028 | newdiff = "" |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 1029 | for newFile in filesToAdd: |
Marius Storm-Olsen | f3e5ae4 | 2008-03-28 15:40:40 +0100 | [diff] [blame] | 1030 | newdiff += "==== new file ====\n" |
| 1031 | newdiff += "--- /dev/null\n" |
| 1032 | newdiff += "+++ %s\n" % newFile |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 1033 | f = open(newFile, "r") |
| 1034 | for line in f.readlines(): |
Marius Storm-Olsen | f3e5ae4 | 2008-03-28 15:40:40 +0100 | [diff] [blame] | 1035 | newdiff += "+" + line |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 1036 | f.close() |
| 1037 | |
Luke Diamand | 848de9c | 2011-05-13 20:46:00 +0100 | [diff] [blame] | 1038 | if self.checkAuthorship and not self.p4UserIsMe(p4User): |
| 1039 | submitTemplate += "######## git author %s does not match your p4 account.\n" % gitEmail |
| 1040 | submitTemplate += "######## Use git-p4 option --preserve-user to modify authorship\n" |
| 1041 | submitTemplate += "######## Use git-p4 config git-p4.skipUserNameCheck hides this message.\n" |
| 1042 | |
Marius Storm-Olsen | f3e5ae4 | 2008-03-28 15:40:40 +0100 | [diff] [blame] | 1043 | separatorLine = "######## everything below this line is just the diff #######\n" |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 1044 | |
Pete Wyckoff | 7c766e5 | 2011-12-04 19:22:45 -0500 | [diff] [blame] | 1045 | (handle, fileName) = tempfile.mkstemp() |
Simon Hausmann | e96e400 | 2008-01-04 14:27:55 +0100 | [diff] [blame] | 1046 | tmpFile = os.fdopen(handle, "w+") |
Marius Storm-Olsen | f3e5ae4 | 2008-03-28 15:40:40 +0100 | [diff] [blame] | 1047 | if self.isWindows: |
| 1048 | submitTemplate = submitTemplate.replace("\n", "\r\n") |
| 1049 | separatorLine = separatorLine.replace("\n", "\r\n") |
| 1050 | newdiff = newdiff.replace("\n", "\r\n") |
| 1051 | tmpFile.write(submitTemplate + separatorLine + diff + newdiff) |
Simon Hausmann | e96e400 | 2008-01-04 14:27:55 +0100 | [diff] [blame] | 1052 | tmpFile.close() |
Simon Hausmann | cb4f128 | 2007-05-25 22:34:30 +0200 | [diff] [blame] | 1053 | |
Pete Wyckoff | 7c766e5 | 2011-12-04 19:22:45 -0500 | [diff] [blame] | 1054 | if self.edit_template(fileName): |
| 1055 | # read the edited message and submit |
Simon Hausmann | cdc7e38 | 2008-08-27 09:30:29 +0200 | [diff] [blame] | 1056 | tmpFile = open(fileName, "rb") |
| 1057 | message = tmpFile.read() |
| 1058 | tmpFile.close() |
| 1059 | submitTemplate = message[:message.index(separatorLine)] |
| 1060 | if self.isWindows: |
| 1061 | submitTemplate = submitTemplate.replace("\r\n", "\n") |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 1062 | p4_write_pipe(['submit', '-i'], submitTemplate) |
Luke Diamand | 3ea2cfd | 2011-04-21 20:50:23 +0100 | [diff] [blame] | 1063 | |
| 1064 | if self.preserveUser: |
| 1065 | if p4User: |
| 1066 | # Get last changelist number. Cannot easily get it from |
Pete Wyckoff | 7c766e5 | 2011-12-04 19:22:45 -0500 | [diff] [blame] | 1067 | # the submit command output as the output is |
| 1068 | # unmarshalled. |
Luke Diamand | 3ea2cfd | 2011-04-21 20:50:23 +0100 | [diff] [blame] | 1069 | changelist = self.lastP4Changelist() |
| 1070 | self.modifyChangelistUser(changelist, p4User) |
Simon Hausmann | cdc7e38 | 2008-08-27 09:30:29 +0200 | [diff] [blame] | 1071 | else: |
Pete Wyckoff | 7c766e5 | 2011-12-04 19:22:45 -0500 | [diff] [blame] | 1072 | # skip this patch |
Pete Wyckoff | d165204 | 2011-12-17 12:39:03 -0500 | [diff] [blame] | 1073 | print "Submission cancelled, undoing p4 changes." |
Simon Hausmann | cdc7e38 | 2008-08-27 09:30:29 +0200 | [diff] [blame] | 1074 | for f in editedFiles: |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 1075 | p4_revert(f) |
Simon Hausmann | cdc7e38 | 2008-08-27 09:30:29 +0200 | [diff] [blame] | 1076 | for f in filesToAdd: |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 1077 | p4_revert(f) |
| 1078 | os.remove(f) |
Simon Hausmann | cdc7e38 | 2008-08-27 09:30:29 +0200 | [diff] [blame] | 1079 | |
| 1080 | os.remove(fileName) |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 1081 | else: |
| 1082 | fileName = "submit.txt" |
| 1083 | file = open(fileName, "w+") |
| 1084 | file.write(self.prepareLogMessage(template, logMessage)) |
| 1085 | file.close() |
Han-Wen Nienhuys | cebdf5a | 2007-05-23 16:53:11 -0300 | [diff] [blame] | 1086 | print ("Perforce submit template written as %s. " |
| 1087 | + "Please review/edit and then use p4 submit -i < %s to submit directly!" |
| 1088 | % (fileName, fileName)) |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 1089 | |
| 1090 | def run(self, args): |
Simon Hausmann | c9b50e6 | 2007-03-29 19:15:24 +0200 | [diff] [blame] | 1091 | if len(args) == 0: |
| 1092 | self.master = currentGitBranch() |
Simon Hausmann | 4280e53 | 2007-05-25 08:49:18 +0200 | [diff] [blame] | 1093 | if len(self.master) == 0 or not gitBranchExists("refs/heads/%s" % self.master): |
Simon Hausmann | c9b50e6 | 2007-03-29 19:15:24 +0200 | [diff] [blame] | 1094 | die("Detecting current git branch failed!") |
| 1095 | elif len(args) == 1: |
| 1096 | self.master = args[0] |
Pete Wyckoff | 28755db | 2011-12-24 21:07:40 -0500 | [diff] [blame^] | 1097 | if not branchExists(self.master): |
| 1098 | die("Branch %s does not exist" % self.master) |
Simon Hausmann | c9b50e6 | 2007-03-29 19:15:24 +0200 | [diff] [blame] | 1099 | else: |
| 1100 | return False |
| 1101 | |
Jing Xue | 4c2d5d7 | 2008-06-22 14:12:39 -0400 | [diff] [blame] | 1102 | allowSubmit = gitConfig("git-p4.allowSubmit") |
| 1103 | if len(allowSubmit) > 0 and not self.master in allowSubmit.split(","): |
| 1104 | die("%s is not in git-p4.allowSubmit" % self.master) |
| 1105 | |
Simon Hausmann | 27d2d81 | 2007-06-12 14:31:59 +0200 | [diff] [blame] | 1106 | [upstream, settings] = findUpstreamBranchPoint() |
Simon Hausmann | ea99c3a | 2007-08-08 17:06:55 +0200 | [diff] [blame] | 1107 | self.depotPath = settings['depot-paths'][0] |
Simon Hausmann | 27d2d81 | 2007-06-12 14:31:59 +0200 | [diff] [blame] | 1108 | if len(self.origin) == 0: |
| 1109 | self.origin = upstream |
Simon Hausmann | a3fdd57 | 2007-06-07 22:54:32 +0200 | [diff] [blame] | 1110 | |
Luke Diamand | 3ea2cfd | 2011-04-21 20:50:23 +0100 | [diff] [blame] | 1111 | if self.preserveUser: |
| 1112 | if not self.canChangeChangelists(): |
| 1113 | die("Cannot preserve user names without p4 super-user or admin permissions") |
| 1114 | |
Simon Hausmann | a3fdd57 | 2007-06-07 22:54:32 +0200 | [diff] [blame] | 1115 | if self.verbose: |
| 1116 | print "Origin branch is " + self.origin |
Simon Hausmann | 9512497 | 2007-03-23 09:16:07 +0100 | [diff] [blame] | 1117 | |
Simon Hausmann | ea99c3a | 2007-08-08 17:06:55 +0200 | [diff] [blame] | 1118 | if len(self.depotPath) == 0: |
Simon Hausmann | 9512497 | 2007-03-23 09:16:07 +0100 | [diff] [blame] | 1119 | print "Internal error: cannot locate perforce depot path from existing branches" |
| 1120 | sys.exit(128) |
| 1121 | |
Simon Hausmann | ea99c3a | 2007-08-08 17:06:55 +0200 | [diff] [blame] | 1122 | self.clientPath = p4Where(self.depotPath) |
Simon Hausmann | 9512497 | 2007-03-23 09:16:07 +0100 | [diff] [blame] | 1123 | |
Simon Hausmann | 51a2640 | 2007-04-15 09:59:56 +0200 | [diff] [blame] | 1124 | if len(self.clientPath) == 0: |
Simon Hausmann | ea99c3a | 2007-08-08 17:06:55 +0200 | [diff] [blame] | 1125 | print "Error: Cannot locate perforce checkout of %s in client view" % self.depotPath |
Simon Hausmann | 9512497 | 2007-03-23 09:16:07 +0100 | [diff] [blame] | 1126 | sys.exit(128) |
| 1127 | |
Simon Hausmann | ea99c3a | 2007-08-08 17:06:55 +0200 | [diff] [blame] | 1128 | 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] | 1129 | self.oldWorkingDirectory = os.getcwd() |
Simon Hausmann | c1b296b | 2007-05-20 16:55:05 +0200 | [diff] [blame] | 1130 | |
Gary Gibbons | 0591cfa | 2011-12-09 18:48:14 -0500 | [diff] [blame] | 1131 | # ensure the clientPath exists |
| 1132 | if not os.path.exists(self.clientPath): |
| 1133 | os.makedirs(self.clientPath) |
| 1134 | |
Robert Blum | 053fd0c | 2008-08-01 12:50:03 -0700 | [diff] [blame] | 1135 | chdir(self.clientPath) |
Benjamin C Meyer | 6a01298 | 2010-03-19 00:39:10 -0400 | [diff] [blame] | 1136 | print "Synchronizing p4 checkout..." |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 1137 | p4_sync("...") |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 1138 | self.check() |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 1139 | |
Simon Hausmann | 4c750c0 | 2008-02-19 09:37:16 +0100 | [diff] [blame] | 1140 | commits = [] |
| 1141 | for line in read_pipe_lines("git rev-list --no-merges %s..%s" % (self.origin, self.master)): |
| 1142 | commits.append(line.strip()) |
| 1143 | commits.reverse() |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 1144 | |
Luke Diamand | 848de9c | 2011-05-13 20:46:00 +0100 | [diff] [blame] | 1145 | if self.preserveUser or (gitConfig("git-p4.skipUserNameCheck") == "true"): |
| 1146 | self.checkAuthorship = False |
| 1147 | else: |
| 1148 | self.checkAuthorship = True |
| 1149 | |
Luke Diamand | 3ea2cfd | 2011-04-21 20:50:23 +0100 | [diff] [blame] | 1150 | if self.preserveUser: |
| 1151 | self.checkValidP4Users(commits) |
| 1152 | |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 1153 | while len(commits) > 0: |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 1154 | commit = commits[0] |
| 1155 | commits = commits[1:] |
Han-Wen Nienhuys | 7cb5cbe | 2007-05-23 16:55:48 -0300 | [diff] [blame] | 1156 | self.applyCommit(commit) |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 1157 | if not self.interactive: |
| 1158 | break |
| 1159 | |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 1160 | if len(commits) == 0: |
Simon Hausmann | 4c750c0 | 2008-02-19 09:37:16 +0100 | [diff] [blame] | 1161 | print "All changes applied!" |
Robert Blum | 053fd0c | 2008-08-01 12:50:03 -0700 | [diff] [blame] | 1162 | chdir(self.oldWorkingDirectory) |
Simon Hausmann | 14594f4 | 2007-08-22 09:07:15 +0200 | [diff] [blame] | 1163 | |
Simon Hausmann | 4c750c0 | 2008-02-19 09:37:16 +0100 | [diff] [blame] | 1164 | sync = P4Sync() |
| 1165 | sync.run([]) |
Simon Hausmann | 14594f4 | 2007-08-22 09:07:15 +0200 | [diff] [blame] | 1166 | |
Simon Hausmann | 4c750c0 | 2008-02-19 09:37:16 +0100 | [diff] [blame] | 1167 | rebase = P4Rebase() |
| 1168 | rebase.rebase() |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 1169 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1170 | return True |
| 1171 | |
Luke Diamand | 3ea2cfd | 2011-04-21 20:50:23 +0100 | [diff] [blame] | 1172 | class P4Sync(Command, P4UserMap): |
Pete Wyckoff | 56c0934 | 2011-02-19 08:17:57 -0500 | [diff] [blame] | 1173 | delete_actions = ( "delete", "move/delete", "purge" ) |
| 1174 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1175 | def __init__(self): |
| 1176 | Command.__init__(self) |
Luke Diamand | 3ea2cfd | 2011-04-21 20:50:23 +0100 | [diff] [blame] | 1177 | P4UserMap.__init__(self) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1178 | self.options = [ |
| 1179 | optparse.make_option("--branch", dest="branch"), |
| 1180 | optparse.make_option("--detect-branches", dest="detectBranches", action="store_true"), |
| 1181 | optparse.make_option("--changesfile", dest="changesFile"), |
| 1182 | optparse.make_option("--silent", dest="silent", action="store_true"), |
Simon Hausmann | ef48f90 | 2007-05-17 22:17:49 +0200 | [diff] [blame] | 1183 | optparse.make_option("--detect-labels", dest="detectLabels", action="store_true"), |
Simon Hausmann | a028a98 | 2007-05-23 00:03:08 +0200 | [diff] [blame] | 1184 | optparse.make_option("--verbose", dest="verbose", action="store_true"), |
Han-Wen Nienhuys | d2c6dd3 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1185 | optparse.make_option("--import-local", dest="importIntoRemotes", action="store_false", |
| 1186 | help="Import into refs/heads/ , not refs/remotes"), |
Han-Wen Nienhuys | 8b41a97 | 2007-05-23 18:20:53 -0300 | [diff] [blame] | 1187 | optparse.make_option("--max-changes", dest="maxChanges"), |
Han-Wen Nienhuys | 86dff6b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1188 | optparse.make_option("--keep-path", dest="keepRepoPath", action='store_true', |
Tor Arvid Lund | 3a70cdf | 2008-02-18 15:22:08 +0100 | [diff] [blame] | 1189 | help="Keep entire BRANCH/DIR/SUBDIR prefix during import"), |
| 1190 | optparse.make_option("--use-client-spec", dest="useClientSpec", action='store_true', |
| 1191 | help="Only sync files that are included in the Perforce Client Spec") |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1192 | ] |
| 1193 | self.description = """Imports from Perforce into a git repository.\n |
| 1194 | example: |
| 1195 | //depot/my/project/ -- to import the current head |
| 1196 | //depot/my/project/@all -- to import everything |
| 1197 | //depot/my/project/@1,6 -- to import only from revision 1 to 6 |
| 1198 | |
| 1199 | (a ... is not needed in the path p4 specification, it's added implicitly)""" |
| 1200 | |
| 1201 | self.usage += " //depot/path[@revRange]" |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1202 | self.silent = False |
Reilly Grant | 1d7367d | 2009-09-10 00:02:38 -0700 | [diff] [blame] | 1203 | self.createdBranches = set() |
| 1204 | self.committedChanges = set() |
Simon Hausmann | 569d1bd | 2007-03-22 21:34:16 +0100 | [diff] [blame] | 1205 | self.branch = "" |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1206 | self.detectBranches = False |
Simon Hausmann | cb53e1f | 2007-04-08 00:12:02 +0200 | [diff] [blame] | 1207 | self.detectLabels = False |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1208 | self.changesFile = "" |
Simon Hausmann | 0126510 | 2007-05-25 10:36:10 +0200 | [diff] [blame] | 1209 | self.syncWithOrigin = True |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 1210 | self.verbose = False |
Simon Hausmann | a028a98 | 2007-05-23 00:03:08 +0200 | [diff] [blame] | 1211 | self.importIntoRemotes = True |
Simon Hausmann | 01a9c9c | 2007-05-23 00:07:35 +0200 | [diff] [blame] | 1212 | self.maxChanges = "" |
Marius Storm-Olsen | c1f9197 | 2007-05-24 14:07:55 +0200 | [diff] [blame] | 1213 | self.isWindows = (platform.system() == "Windows") |
Han-Wen Nienhuys | 8b41a97 | 2007-05-23 18:20:53 -0300 | [diff] [blame] | 1214 | self.keepRepoPath = False |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1215 | self.depotPaths = None |
Simon Hausmann | 3c69964 | 2007-06-16 13:09:21 +0200 | [diff] [blame] | 1216 | self.p4BranchesInGit = [] |
Tommy Thorn | 354081d | 2008-02-03 10:38:51 -0800 | [diff] [blame] | 1217 | self.cloneExclude = [] |
Tor Arvid Lund | 3a70cdf | 2008-02-18 15:22:08 +0100 | [diff] [blame] | 1218 | self.useClientSpec = False |
| 1219 | self.clientSpecDirs = [] |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1220 | |
Simon Hausmann | 0126510 | 2007-05-25 10:36:10 +0200 | [diff] [blame] | 1221 | if gitConfig("git-p4.syncFromOrigin") == "false": |
| 1222 | self.syncWithOrigin = False |
| 1223 | |
Pete Wyckoff | 084f630 | 2011-02-19 08:18:00 -0500 | [diff] [blame] | 1224 | # |
| 1225 | # P4 wildcards are not allowed in filenames. P4 complains |
| 1226 | # if you simply add them, but you can force it with "-f", in |
| 1227 | # which case it translates them into %xx encoding internally. |
| 1228 | # Search for and fix just these four characters. Do % last so |
| 1229 | # that fixing it does not inadvertently create new %-escapes. |
| 1230 | # |
| 1231 | def wildcard_decode(self, path): |
| 1232 | # Cannot have * in a filename in windows; untested as to |
| 1233 | # what p4 would do in such a case. |
| 1234 | if not self.isWindows: |
| 1235 | path = path.replace("%2A", "*") |
| 1236 | path = path.replace("%23", "#") \ |
| 1237 | .replace("%40", "@") \ |
| 1238 | .replace("%25", "%") |
| 1239 | return path |
| 1240 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1241 | def extractFilesFromCommit(self, commit): |
Tommy Thorn | 354081d | 2008-02-03 10:38:51 -0800 | [diff] [blame] | 1242 | self.cloneExclude = [re.sub(r"\.\.\.$", "", path) |
| 1243 | for path in self.cloneExclude] |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1244 | files = [] |
| 1245 | fnum = 0 |
| 1246 | while commit.has_key("depotFile%s" % fnum): |
| 1247 | path = commit["depotFile%s" % fnum] |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1248 | |
Tommy Thorn | 354081d | 2008-02-03 10:38:51 -0800 | [diff] [blame] | 1249 | if [p for p in self.cloneExclude |
Tor Arvid Lund | d53de8b | 2011-03-15 13:08:02 +0100 | [diff] [blame] | 1250 | if p4PathStartsWith(path, p)]: |
Tommy Thorn | 354081d | 2008-02-03 10:38:51 -0800 | [diff] [blame] | 1251 | found = False |
| 1252 | else: |
| 1253 | found = [p for p in self.depotPaths |
Tor Arvid Lund | d53de8b | 2011-03-15 13:08:02 +0100 | [diff] [blame] | 1254 | if p4PathStartsWith(path, p)] |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1255 | if not found: |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1256 | fnum = fnum + 1 |
| 1257 | continue |
| 1258 | |
| 1259 | file = {} |
| 1260 | file["path"] = path |
| 1261 | file["rev"] = commit["rev%s" % fnum] |
| 1262 | file["action"] = commit["action%s" % fnum] |
| 1263 | file["type"] = commit["type%s" % fnum] |
| 1264 | files.append(file) |
| 1265 | fnum = fnum + 1 |
| 1266 | return files |
| 1267 | |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1268 | def stripRepoPath(self, path, prefixes): |
Ian Wienand | 3952710 | 2011-02-11 16:33:48 -0800 | [diff] [blame] | 1269 | if self.useClientSpec: |
| 1270 | |
| 1271 | # if using the client spec, we use the output directory |
| 1272 | # specified in the client. For example, a view |
| 1273 | # //depot/foo/branch/... //client/branch/foo/... |
| 1274 | # will end up putting all foo/branch files into |
| 1275 | # branch/foo/ |
| 1276 | for val in self.clientSpecDirs: |
| 1277 | if path.startswith(val[0]): |
| 1278 | # replace the depot path with the client path |
| 1279 | path = path.replace(val[0], val[1][1]) |
| 1280 | # now strip out the client (//client/...) |
| 1281 | path = re.sub("^(//[^/]+/)", '', path) |
| 1282 | # the rest is all path |
| 1283 | return path |
| 1284 | |
Han-Wen Nienhuys | 8b41a97 | 2007-05-23 18:20:53 -0300 | [diff] [blame] | 1285 | if self.keepRepoPath: |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1286 | prefixes = [re.sub("^(//[^/]+/).*", r'\1', prefixes[0])] |
Han-Wen Nienhuys | 8b41a97 | 2007-05-23 18:20:53 -0300 | [diff] [blame] | 1287 | |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1288 | for p in prefixes: |
Tor Arvid Lund | d53de8b | 2011-03-15 13:08:02 +0100 | [diff] [blame] | 1289 | if p4PathStartsWith(path, p): |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1290 | path = path[len(p):] |
| 1291 | |
| 1292 | return path |
Han-Wen Nienhuys | 6754a29 | 2007-05-23 17:41:50 -0300 | [diff] [blame] | 1293 | |
Simon Hausmann | 71b112d | 2007-05-19 11:54:11 +0200 | [diff] [blame] | 1294 | def splitFilesIntoBranches(self, commit): |
Simon Hausmann | d590467 | 2007-05-19 11:07:32 +0200 | [diff] [blame] | 1295 | branches = {} |
Simon Hausmann | 71b112d | 2007-05-19 11:54:11 +0200 | [diff] [blame] | 1296 | fnum = 0 |
| 1297 | while commit.has_key("depotFile%s" % fnum): |
| 1298 | path = commit["depotFile%s" % fnum] |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1299 | found = [p for p in self.depotPaths |
Tor Arvid Lund | d53de8b | 2011-03-15 13:08:02 +0100 | [diff] [blame] | 1300 | if p4PathStartsWith(path, p)] |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1301 | if not found: |
Simon Hausmann | 71b112d | 2007-05-19 11:54:11 +0200 | [diff] [blame] | 1302 | fnum = fnum + 1 |
| 1303 | continue |
| 1304 | |
| 1305 | file = {} |
| 1306 | file["path"] = path |
| 1307 | file["rev"] = commit["rev%s" % fnum] |
| 1308 | file["action"] = commit["action%s" % fnum] |
| 1309 | file["type"] = commit["type%s" % fnum] |
| 1310 | fnum = fnum + 1 |
| 1311 | |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1312 | relPath = self.stripRepoPath(path, self.depotPaths) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1313 | |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 1314 | for branch in self.knownBranches.keys(): |
Han-Wen Nienhuys | 6754a29 | 2007-05-23 17:41:50 -0300 | [diff] [blame] | 1315 | |
| 1316 | # add a trailing slash so that a commit into qt/4.2foo doesn't end up in qt/4.2 |
| 1317 | if relPath.startswith(branch + "/"): |
Simon Hausmann | d590467 | 2007-05-19 11:07:32 +0200 | [diff] [blame] | 1318 | if branch not in branches: |
| 1319 | branches[branch] = [] |
Simon Hausmann | 71b112d | 2007-05-19 11:54:11 +0200 | [diff] [blame] | 1320 | branches[branch].append(file) |
Simon Hausmann | 6555b2c | 2007-06-17 11:25:34 +0200 | [diff] [blame] | 1321 | break |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1322 | |
| 1323 | return branches |
| 1324 | |
Luke Diamand | b932705 | 2009-07-30 00:13:46 +0100 | [diff] [blame] | 1325 | # output one file from the P4 stream |
| 1326 | # - helper for streamP4Files |
| 1327 | |
| 1328 | def streamOneP4File(self, file, contents): |
Luke Diamand | b932705 | 2009-07-30 00:13:46 +0100 | [diff] [blame] | 1329 | relPath = self.stripRepoPath(file['depotFile'], self.branchPrefixes) |
Pete Wyckoff | 084f630 | 2011-02-19 08:18:00 -0500 | [diff] [blame] | 1330 | relPath = self.wildcard_decode(relPath) |
Luke Diamand | b932705 | 2009-07-30 00:13:46 +0100 | [diff] [blame] | 1331 | if verbose: |
| 1332 | sys.stderr.write("%s\n" % relPath) |
| 1333 | |
Pete Wyckoff | 9cffb8c | 2011-10-16 10:45:01 -0400 | [diff] [blame] | 1334 | (type_base, type_mods) = split_p4_type(file["type"]) |
| 1335 | |
| 1336 | git_mode = "100644" |
| 1337 | if "x" in type_mods: |
| 1338 | git_mode = "100755" |
| 1339 | if type_base == "symlink": |
| 1340 | git_mode = "120000" |
| 1341 | # p4 print on a symlink contains "target\n"; remove the newline |
Evan Powers | b39c361 | 2010-02-16 00:44:08 -0800 | [diff] [blame] | 1342 | data = ''.join(contents) |
| 1343 | contents = [data[:-1]] |
Luke Diamand | b932705 | 2009-07-30 00:13:46 +0100 | [diff] [blame] | 1344 | |
Pete Wyckoff | 9cffb8c | 2011-10-16 10:45:01 -0400 | [diff] [blame] | 1345 | if type_base == "utf16": |
Pete Wyckoff | 55aa571 | 2011-09-17 19:16:14 -0400 | [diff] [blame] | 1346 | # p4 delivers different text in the python output to -G |
| 1347 | # than it does when using "print -o", or normal p4 client |
| 1348 | # operations. utf16 is converted to ascii or utf8, perhaps. |
| 1349 | # But ascii text saved as -t utf16 is completely mangled. |
| 1350 | # Invoke print -o to get the real contents. |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 1351 | text = p4_read_pipe(['print', '-q', '-o', '-', file['depotFile']]) |
Pete Wyckoff | 55aa571 | 2011-09-17 19:16:14 -0400 | [diff] [blame] | 1352 | contents = [ text ] |
| 1353 | |
Pete Wyckoff | 9f7ef0e | 2011-11-05 13:36:07 -0400 | [diff] [blame] | 1354 | if type_base == "apple": |
| 1355 | # Apple filetype files will be streamed as a concatenation of |
| 1356 | # its appledouble header and the contents. This is useless |
| 1357 | # on both macs and non-macs. If using "print -q -o xx", it |
| 1358 | # will create "xx" with the data, and "%xx" with the header. |
| 1359 | # This is also not very useful. |
| 1360 | # |
| 1361 | # Ideally, someday, this script can learn how to generate |
| 1362 | # appledouble files directly and import those to git, but |
| 1363 | # non-mac machines can never find a use for apple filetype. |
| 1364 | print "\nIgnoring apple filetype file %s" % file['depotFile'] |
| 1365 | return |
| 1366 | |
Pete Wyckoff | 9cffb8c | 2011-10-16 10:45:01 -0400 | [diff] [blame] | 1367 | # Perhaps windows wants unicode, utf16 newlines translated too; |
| 1368 | # but this is not doing it. |
| 1369 | if self.isWindows and type_base == "text": |
Luke Diamand | b932705 | 2009-07-30 00:13:46 +0100 | [diff] [blame] | 1370 | mangled = [] |
| 1371 | for data in contents: |
| 1372 | data = data.replace("\r\n", "\n") |
| 1373 | mangled.append(data) |
| 1374 | contents = mangled |
| 1375 | |
Pete Wyckoff | 55aa571 | 2011-09-17 19:16:14 -0400 | [diff] [blame] | 1376 | # Note that we do not try to de-mangle keywords on utf16 files, |
| 1377 | # even though in theory somebody may want that. |
Pete Wyckoff | 9cffb8c | 2011-10-16 10:45:01 -0400 | [diff] [blame] | 1378 | if type_base in ("text", "unicode", "binary"): |
| 1379 | if "ko" in type_mods: |
Pete Wyckoff | cb585a9 | 2011-10-16 10:46:52 -0400 | [diff] [blame] | 1380 | text = ''.join(contents) |
| 1381 | text = re.sub(r'\$(Id|Header):[^$]*\$', r'$\1$', text) |
| 1382 | contents = [ text ] |
Pete Wyckoff | 9cffb8c | 2011-10-16 10:45:01 -0400 | [diff] [blame] | 1383 | elif "k" in type_mods: |
Pete Wyckoff | cb585a9 | 2011-10-16 10:46:52 -0400 | [diff] [blame] | 1384 | text = ''.join(contents) |
| 1385 | text = re.sub(r'\$(Id|Header|Author|Date|DateTime|Change|File|Revision):[^$]*\$', r'$\1$', text) |
| 1386 | contents = [ text ] |
Luke Diamand | b932705 | 2009-07-30 00:13:46 +0100 | [diff] [blame] | 1387 | |
Pete Wyckoff | 9cffb8c | 2011-10-16 10:45:01 -0400 | [diff] [blame] | 1388 | self.gitStream.write("M %s inline %s\n" % (git_mode, relPath)) |
Luke Diamand | b932705 | 2009-07-30 00:13:46 +0100 | [diff] [blame] | 1389 | |
| 1390 | # total length... |
| 1391 | length = 0 |
| 1392 | for d in contents: |
| 1393 | length = length + len(d) |
| 1394 | |
| 1395 | self.gitStream.write("data %d\n" % length) |
| 1396 | for d in contents: |
| 1397 | self.gitStream.write(d) |
| 1398 | self.gitStream.write("\n") |
| 1399 | |
| 1400 | def streamOneP4Deletion(self, file): |
| 1401 | relPath = self.stripRepoPath(file['path'], self.branchPrefixes) |
| 1402 | if verbose: |
| 1403 | sys.stderr.write("delete %s\n" % relPath) |
| 1404 | self.gitStream.write("D %s\n" % relPath) |
| 1405 | |
| 1406 | # handle another chunk of streaming data |
| 1407 | def streamP4FilesCb(self, marshalled): |
| 1408 | |
Andrew Garber | c3f6163 | 2011-04-07 02:01:21 -0400 | [diff] [blame] | 1409 | if marshalled.has_key('depotFile') and self.stream_have_file_info: |
| 1410 | # start of a new file - output the old one first |
| 1411 | self.streamOneP4File(self.stream_file, self.stream_contents) |
| 1412 | self.stream_file = {} |
| 1413 | self.stream_contents = [] |
| 1414 | self.stream_have_file_info = False |
Luke Diamand | b932705 | 2009-07-30 00:13:46 +0100 | [diff] [blame] | 1415 | |
Andrew Garber | c3f6163 | 2011-04-07 02:01:21 -0400 | [diff] [blame] | 1416 | # pick up the new file information... for the |
| 1417 | # 'data' field we need to append to our array |
| 1418 | for k in marshalled.keys(): |
| 1419 | if k == 'data': |
| 1420 | self.stream_contents.append(marshalled['data']) |
| 1421 | else: |
| 1422 | self.stream_file[k] = marshalled[k] |
Luke Diamand | b932705 | 2009-07-30 00:13:46 +0100 | [diff] [blame] | 1423 | |
Andrew Garber | c3f6163 | 2011-04-07 02:01:21 -0400 | [diff] [blame] | 1424 | self.stream_have_file_info = True |
Luke Diamand | b932705 | 2009-07-30 00:13:46 +0100 | [diff] [blame] | 1425 | |
| 1426 | # Stream directly from "p4 files" into "git fast-import" |
| 1427 | def streamP4Files(self, files): |
Simon Hausmann | 30b5940 | 2008-03-03 11:55:48 +0100 | [diff] [blame] | 1428 | filesForCommit = [] |
| 1429 | filesToRead = [] |
Luke Diamand | b932705 | 2009-07-30 00:13:46 +0100 | [diff] [blame] | 1430 | filesToDelete = [] |
Simon Hausmann | 30b5940 | 2008-03-03 11:55:48 +0100 | [diff] [blame] | 1431 | |
Tor Arvid Lund | 3a70cdf | 2008-02-18 15:22:08 +0100 | [diff] [blame] | 1432 | for f in files: |
Simon Hausmann | 30b5940 | 2008-03-03 11:55:48 +0100 | [diff] [blame] | 1433 | includeFile = True |
Tor Arvid Lund | 3a70cdf | 2008-02-18 15:22:08 +0100 | [diff] [blame] | 1434 | for val in self.clientSpecDirs: |
| 1435 | if f['path'].startswith(val[0]): |
Ian Wienand | 3952710 | 2011-02-11 16:33:48 -0800 | [diff] [blame] | 1436 | if val[1][0] <= 0: |
Simon Hausmann | 30b5940 | 2008-03-03 11:55:48 +0100 | [diff] [blame] | 1437 | includeFile = False |
Tor Arvid Lund | 3a70cdf | 2008-02-18 15:22:08 +0100 | [diff] [blame] | 1438 | break |
| 1439 | |
Simon Hausmann | 30b5940 | 2008-03-03 11:55:48 +0100 | [diff] [blame] | 1440 | if includeFile: |
| 1441 | filesForCommit.append(f) |
Pete Wyckoff | 56c0934 | 2011-02-19 08:17:57 -0500 | [diff] [blame] | 1442 | if f['action'] in self.delete_actions: |
Luke Diamand | b932705 | 2009-07-30 00:13:46 +0100 | [diff] [blame] | 1443 | filesToDelete.append(f) |
Pete Wyckoff | 56c0934 | 2011-02-19 08:17:57 -0500 | [diff] [blame] | 1444 | else: |
| 1445 | filesToRead.append(f) |
Han-Wen Nienhuys | 6a49f8e | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1446 | |
Luke Diamand | b932705 | 2009-07-30 00:13:46 +0100 | [diff] [blame] | 1447 | # deleted files... |
| 1448 | for f in filesToDelete: |
| 1449 | self.streamOneP4Deletion(f) |
| 1450 | |
Simon Hausmann | 30b5940 | 2008-03-03 11:55:48 +0100 | [diff] [blame] | 1451 | if len(filesToRead) > 0: |
Luke Diamand | b932705 | 2009-07-30 00:13:46 +0100 | [diff] [blame] | 1452 | self.stream_file = {} |
| 1453 | self.stream_contents = [] |
| 1454 | self.stream_have_file_info = False |
| 1455 | |
Andrew Garber | c3f6163 | 2011-04-07 02:01:21 -0400 | [diff] [blame] | 1456 | # curry self argument |
| 1457 | def streamP4FilesCbSelf(entry): |
| 1458 | self.streamP4FilesCb(entry) |
Luke Diamand | b932705 | 2009-07-30 00:13:46 +0100 | [diff] [blame] | 1459 | |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 1460 | fileArgs = ['%s#%s' % (f['path'], f['rev']) for f in filesToRead] |
| 1461 | |
| 1462 | p4CmdList(["-x", "-", "print"], |
| 1463 | stdin=fileArgs, |
| 1464 | cb=streamP4FilesCbSelf) |
Han-Wen Nienhuys | f2eda79 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1465 | |
Luke Diamand | b932705 | 2009-07-30 00:13:46 +0100 | [diff] [blame] | 1466 | # do the last chunk |
| 1467 | if self.stream_file.has_key('depotFile'): |
| 1468 | self.streamOneP4File(self.stream_file, self.stream_contents) |
Han-Wen Nienhuys | 6a49f8e | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1469 | |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1470 | def commit(self, details, files, branch, branchPrefixes, parent = ""): |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1471 | epoch = details["time"] |
| 1472 | author = details["user"] |
Andrew Garber | c3f6163 | 2011-04-07 02:01:21 -0400 | [diff] [blame] | 1473 | self.branchPrefixes = branchPrefixes |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1474 | |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 1475 | if self.verbose: |
| 1476 | print "commit into %s" % branch |
| 1477 | |
Han-Wen Nienhuys | 96e07dd | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1478 | # start with reading files; if that fails, we should not |
| 1479 | # create a commit. |
| 1480 | new_files = [] |
| 1481 | for f in files: |
Tor Arvid Lund | d53de8b | 2011-03-15 13:08:02 +0100 | [diff] [blame] | 1482 | if [p for p in branchPrefixes if p4PathStartsWith(f['path'], p)]: |
Han-Wen Nienhuys | 96e07dd | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1483 | new_files.append (f) |
| 1484 | else: |
Tor Arvid Lund | afa1dd9 | 2011-03-15 13:08:03 +0100 | [diff] [blame] | 1485 | sys.stderr.write("Ignoring file outside of prefix: %s\n" % f['path']) |
Han-Wen Nienhuys | 96e07dd | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1486 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1487 | self.gitStream.write("commit %s\n" % branch) |
Han-Wen Nienhuys | 6a49f8e | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1488 | # gitStream.write("mark :%s\n" % details["change"]) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1489 | self.committedChanges.add(int(details["change"])) |
| 1490 | committer = "" |
Simon Hausmann | b607e71 | 2007-05-20 10:55:54 +0200 | [diff] [blame] | 1491 | if author not in self.users: |
| 1492 | self.getUserMapFromPerforceServer() |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1493 | if author in self.users: |
Simon Hausmann | 0828ab1 | 2007-03-20 20:59:30 +0100 | [diff] [blame] | 1494 | committer = "%s %s %s" % (self.users[author], epoch, self.tz) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1495 | else: |
Simon Hausmann | 0828ab1 | 2007-03-20 20:59:30 +0100 | [diff] [blame] | 1496 | committer = "%s <a@b> %s %s" % (author, epoch, self.tz) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1497 | |
| 1498 | self.gitStream.write("committer %s\n" % committer) |
| 1499 | |
| 1500 | self.gitStream.write("data <<EOT\n") |
| 1501 | self.gitStream.write(details["desc"]) |
Simon Hausmann | 6581de0 | 2007-06-11 10:01:58 +0200 | [diff] [blame] | 1502 | self.gitStream.write("\n[git-p4: depot-paths = \"%s\": change = %s" |
| 1503 | % (','.join (branchPrefixes), details["change"])) |
| 1504 | if len(details['options']) > 0: |
| 1505 | self.gitStream.write(": options = %s" % details['options']) |
| 1506 | self.gitStream.write("]\nEOT\n\n") |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1507 | |
| 1508 | if len(parent) > 0: |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 1509 | if self.verbose: |
| 1510 | print "parent %s" % parent |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1511 | self.gitStream.write("from %s\n" % parent) |
| 1512 | |
Luke Diamand | b932705 | 2009-07-30 00:13:46 +0100 | [diff] [blame] | 1513 | self.streamP4Files(new_files) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1514 | self.gitStream.write("\n") |
| 1515 | |
Simon Hausmann | 1f4ba1c | 2007-03-26 22:34:34 +0200 | [diff] [blame] | 1516 | change = int(details["change"]) |
| 1517 | |
Simon Hausmann | 9bda3a8 | 2007-05-19 12:05:40 +0200 | [diff] [blame] | 1518 | if self.labels.has_key(change): |
Simon Hausmann | 1f4ba1c | 2007-03-26 22:34:34 +0200 | [diff] [blame] | 1519 | label = self.labels[change] |
| 1520 | labelDetails = label[0] |
| 1521 | labelRevisions = label[1] |
Simon Hausmann | 71b112d | 2007-05-19 11:54:11 +0200 | [diff] [blame] | 1522 | if self.verbose: |
| 1523 | print "Change %s is labelled %s" % (change, labelDetails) |
Simon Hausmann | 1f4ba1c | 2007-03-26 22:34:34 +0200 | [diff] [blame] | 1524 | |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 1525 | files = p4CmdList(["files"] + ["%s...@%s" % (p, change) |
| 1526 | for p in branchPrefixes]) |
Simon Hausmann | 1f4ba1c | 2007-03-26 22:34:34 +0200 | [diff] [blame] | 1527 | |
| 1528 | if len(files) == len(labelRevisions): |
| 1529 | |
| 1530 | cleanedFiles = {} |
| 1531 | for info in files: |
Pete Wyckoff | 56c0934 | 2011-02-19 08:17:57 -0500 | [diff] [blame] | 1532 | if info["action"] in self.delete_actions: |
Simon Hausmann | 1f4ba1c | 2007-03-26 22:34:34 +0200 | [diff] [blame] | 1533 | continue |
| 1534 | cleanedFiles[info["depotFile"]] = info["rev"] |
| 1535 | |
| 1536 | if cleanedFiles == labelRevisions: |
| 1537 | self.gitStream.write("tag tag_%s\n" % labelDetails["label"]) |
| 1538 | self.gitStream.write("from %s\n" % branch) |
| 1539 | |
| 1540 | owner = labelDetails["Owner"] |
| 1541 | tagger = "" |
| 1542 | if author in self.users: |
| 1543 | tagger = "%s %s %s" % (self.users[owner], epoch, self.tz) |
| 1544 | else: |
| 1545 | tagger = "%s <a@b> %s %s" % (owner, epoch, self.tz) |
| 1546 | self.gitStream.write("tagger %s\n" % tagger) |
| 1547 | self.gitStream.write("data <<EOT\n") |
| 1548 | self.gitStream.write(labelDetails["Description"]) |
| 1549 | self.gitStream.write("EOT\n\n") |
| 1550 | |
| 1551 | else: |
Simon Hausmann | a46668f | 2007-03-28 17:05:38 +0200 | [diff] [blame] | 1552 | if not self.silent: |
Han-Wen Nienhuys | cebdf5a | 2007-05-23 16:53:11 -0300 | [diff] [blame] | 1553 | print ("Tag %s does not match with change %s: files do not match." |
| 1554 | % (labelDetails["label"], change)) |
Simon Hausmann | 1f4ba1c | 2007-03-26 22:34:34 +0200 | [diff] [blame] | 1555 | |
| 1556 | else: |
Simon Hausmann | a46668f | 2007-03-28 17:05:38 +0200 | [diff] [blame] | 1557 | if not self.silent: |
Han-Wen Nienhuys | cebdf5a | 2007-05-23 16:53:11 -0300 | [diff] [blame] | 1558 | print ("Tag %s does not match with change %s: file count is different." |
| 1559 | % (labelDetails["label"], change)) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1560 | |
Simon Hausmann | 1f4ba1c | 2007-03-26 22:34:34 +0200 | [diff] [blame] | 1561 | def getLabels(self): |
| 1562 | self.labels = {} |
| 1563 | |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1564 | l = p4CmdList("labels %s..." % ' '.join (self.depotPaths)) |
Simon Hausmann | 10c3211 | 2007-04-08 10:15:47 +0200 | [diff] [blame] | 1565 | if len(l) > 0 and not self.silent: |
Shun Kei Leung | 183f843 | 2007-11-21 11:01:19 +0800 | [diff] [blame] | 1566 | print "Finding files belonging to labels in %s" % `self.depotPaths` |
Simon Hausmann | 01ce1fe | 2007-04-07 23:46:50 +0200 | [diff] [blame] | 1567 | |
| 1568 | for output in l: |
Simon Hausmann | 1f4ba1c | 2007-03-26 22:34:34 +0200 | [diff] [blame] | 1569 | label = output["label"] |
| 1570 | revisions = {} |
| 1571 | newestChange = 0 |
Simon Hausmann | 71b112d | 2007-05-19 11:54:11 +0200 | [diff] [blame] | 1572 | if self.verbose: |
| 1573 | print "Querying files for label %s" % label |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 1574 | for file in p4CmdList(["files"] + |
| 1575 | ["%s...@%s" % (p, label) |
| 1576 | for p in self.depotPaths]): |
Simon Hausmann | 1f4ba1c | 2007-03-26 22:34:34 +0200 | [diff] [blame] | 1577 | revisions[file["depotFile"]] = file["rev"] |
| 1578 | change = int(file["change"]) |
| 1579 | if change > newestChange: |
| 1580 | newestChange = change |
| 1581 | |
Simon Hausmann | 9bda3a8 | 2007-05-19 12:05:40 +0200 | [diff] [blame] | 1582 | self.labels[newestChange] = [output, revisions] |
| 1583 | |
| 1584 | if self.verbose: |
| 1585 | print "Label changes: %s" % self.labels.keys() |
Simon Hausmann | 1f4ba1c | 2007-03-26 22:34:34 +0200 | [diff] [blame] | 1586 | |
Han-Wen Nienhuys | 86dff6b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1587 | def guessProjectName(self): |
| 1588 | for p in self.depotPaths: |
Simon Hausmann | 6e5295c | 2007-06-11 08:50:57 +0200 | [diff] [blame] | 1589 | if p.endswith("/"): |
| 1590 | p = p[:-1] |
| 1591 | p = p[p.strip().rfind("/") + 1:] |
| 1592 | if not p.endswith("/"): |
| 1593 | p += "/" |
| 1594 | return p |
Han-Wen Nienhuys | 86dff6b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1595 | |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 1596 | def getBranchMapping(self): |
Simon Hausmann | 6555b2c | 2007-06-17 11:25:34 +0200 | [diff] [blame] | 1597 | lostAndFoundBranches = set() |
| 1598 | |
Vitor Antunes | 8ace74c | 2011-08-19 00:44:04 +0100 | [diff] [blame] | 1599 | user = gitConfig("git-p4.branchUser") |
| 1600 | if len(user) > 0: |
| 1601 | command = "branches -u %s" % user |
| 1602 | else: |
| 1603 | command = "branches" |
| 1604 | |
| 1605 | for info in p4CmdList(command): |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 1606 | details = p4Cmd("branch -o %s" % info["branch"]) |
| 1607 | viewIdx = 0 |
| 1608 | while details.has_key("View%s" % viewIdx): |
| 1609 | paths = details["View%s" % viewIdx].split(" ") |
| 1610 | viewIdx = viewIdx + 1 |
| 1611 | # require standard //depot/foo/... //depot/bar/... mapping |
| 1612 | if len(paths) != 2 or not paths[0].endswith("/...") or not paths[1].endswith("/..."): |
| 1613 | continue |
| 1614 | source = paths[0] |
| 1615 | destination = paths[1] |
Simon Hausmann | 6509e19 | 2007-06-07 09:41:53 +0200 | [diff] [blame] | 1616 | ## HACK |
Tor Arvid Lund | d53de8b | 2011-03-15 13:08:02 +0100 | [diff] [blame] | 1617 | if p4PathStartsWith(source, self.depotPaths[0]) and p4PathStartsWith(destination, self.depotPaths[0]): |
Simon Hausmann | 6509e19 | 2007-06-07 09:41:53 +0200 | [diff] [blame] | 1618 | source = source[len(self.depotPaths[0]):-4] |
| 1619 | destination = destination[len(self.depotPaths[0]):-4] |
Simon Hausmann | 6555b2c | 2007-06-17 11:25:34 +0200 | [diff] [blame] | 1620 | |
Simon Hausmann | 1a2edf4 | 2007-06-17 15:10:24 +0200 | [diff] [blame] | 1621 | if destination in self.knownBranches: |
| 1622 | if not self.silent: |
| 1623 | print "p4 branch %s defines a mapping from %s to %s" % (info["branch"], source, destination) |
| 1624 | print "but there exists another mapping from %s to %s already!" % (self.knownBranches[destination], destination) |
| 1625 | continue |
| 1626 | |
Simon Hausmann | 6555b2c | 2007-06-17 11:25:34 +0200 | [diff] [blame] | 1627 | self.knownBranches[destination] = source |
| 1628 | |
| 1629 | lostAndFoundBranches.discard(destination) |
| 1630 | |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 1631 | if source not in self.knownBranches: |
Simon Hausmann | 6555b2c | 2007-06-17 11:25:34 +0200 | [diff] [blame] | 1632 | lostAndFoundBranches.add(source) |
| 1633 | |
Vitor Antunes | 7199cf1 | 2011-08-19 00:44:05 +0100 | [diff] [blame] | 1634 | # Perforce does not strictly require branches to be defined, so we also |
| 1635 | # check git config for a branch list. |
| 1636 | # |
| 1637 | # Example of branch definition in git config file: |
| 1638 | # [git-p4] |
| 1639 | # branchList=main:branchA |
| 1640 | # branchList=main:branchB |
| 1641 | # branchList=branchA:branchC |
| 1642 | configBranches = gitConfigList("git-p4.branchList") |
| 1643 | for branch in configBranches: |
| 1644 | if branch: |
| 1645 | (source, destination) = branch.split(":") |
| 1646 | self.knownBranches[destination] = source |
| 1647 | |
| 1648 | lostAndFoundBranches.discard(destination) |
| 1649 | |
| 1650 | if source not in self.knownBranches: |
| 1651 | lostAndFoundBranches.add(source) |
| 1652 | |
Simon Hausmann | 6555b2c | 2007-06-17 11:25:34 +0200 | [diff] [blame] | 1653 | |
| 1654 | for branch in lostAndFoundBranches: |
| 1655 | self.knownBranches[branch] = branch |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 1656 | |
Simon Hausmann | 38f9f5e | 2007-11-15 10:38:45 +0100 | [diff] [blame] | 1657 | def getBranchMappingFromGitBranches(self): |
| 1658 | branches = p4BranchesInGit(self.importIntoRemotes) |
| 1659 | for branch in branches.keys(): |
| 1660 | if branch == "master": |
| 1661 | branch = "main" |
| 1662 | else: |
| 1663 | branch = branch[len(self.projectName):] |
| 1664 | self.knownBranches[branch] = branch |
| 1665 | |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 1666 | def listExistingP4GitBranches(self): |
Simon Hausmann | 144ff46 | 2007-07-18 17:27:50 +0200 | [diff] [blame] | 1667 | # branches holds mapping from name to commit |
| 1668 | branches = p4BranchesInGit(self.importIntoRemotes) |
| 1669 | self.p4BranchesInGit = branches.keys() |
| 1670 | for branch in branches.keys(): |
| 1671 | self.initialParents[self.refPrefix + branch] = branches[branch] |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 1672 | |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1673 | def updateOptionDict(self, d): |
| 1674 | option_keys = {} |
| 1675 | if self.keepRepoPath: |
| 1676 | option_keys['keepRepoPath'] = 1 |
| 1677 | |
| 1678 | d["options"] = ' '.join(sorted(option_keys.keys())) |
| 1679 | |
| 1680 | def readOptions(self, d): |
| 1681 | self.keepRepoPath = (d.has_key('options') |
| 1682 | and ('keepRepoPath' in d['options'])) |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1683 | |
Simon Hausmann | 8134f69 | 2007-08-26 16:44:55 +0200 | [diff] [blame] | 1684 | def gitRefForBranch(self, branch): |
| 1685 | if branch == "main": |
| 1686 | return self.refPrefix + "master" |
| 1687 | |
| 1688 | if len(branch) <= 0: |
| 1689 | return branch |
| 1690 | |
| 1691 | return self.refPrefix + self.projectName + branch |
| 1692 | |
Simon Hausmann | 1ca3d71 | 2007-08-26 17:36:55 +0200 | [diff] [blame] | 1693 | def gitCommitByP4Change(self, ref, change): |
| 1694 | if self.verbose: |
| 1695 | print "looking in ref " + ref + " for change %s using bisect..." % change |
| 1696 | |
| 1697 | earliestCommit = "" |
| 1698 | latestCommit = parseRevision(ref) |
| 1699 | |
| 1700 | while True: |
| 1701 | if self.verbose: |
| 1702 | print "trying: earliest %s latest %s" % (earliestCommit, latestCommit) |
| 1703 | next = read_pipe("git rev-list --bisect %s %s" % (latestCommit, earliestCommit)).strip() |
| 1704 | if len(next) == 0: |
| 1705 | if self.verbose: |
| 1706 | print "argh" |
| 1707 | return "" |
| 1708 | log = extractLogMessageFromGitCommit(next) |
| 1709 | settings = extractSettingsGitLog(log) |
| 1710 | currentChange = int(settings['change']) |
| 1711 | if self.verbose: |
| 1712 | print "current change %s" % currentChange |
| 1713 | |
| 1714 | if currentChange == change: |
| 1715 | if self.verbose: |
| 1716 | print "found %s" % next |
| 1717 | return next |
| 1718 | |
| 1719 | if currentChange < change: |
| 1720 | earliestCommit = "^%s" % next |
| 1721 | else: |
| 1722 | latestCommit = "%s" % next |
| 1723 | |
| 1724 | return "" |
| 1725 | |
| 1726 | def importNewBranch(self, branch, maxChange): |
| 1727 | # make fast-import flush all changes to disk and update the refs using the checkpoint |
| 1728 | # command so that we can try to find the branch parent in the git history |
| 1729 | self.gitStream.write("checkpoint\n\n"); |
| 1730 | self.gitStream.flush(); |
| 1731 | branchPrefix = self.depotPaths[0] + branch + "/" |
| 1732 | range = "@1,%s" % maxChange |
| 1733 | #print "prefix" + branchPrefix |
| 1734 | changes = p4ChangesForPaths([branchPrefix], range) |
| 1735 | if len(changes) <= 0: |
| 1736 | return False |
| 1737 | firstChange = changes[0] |
| 1738 | #print "first change in branch: %s" % firstChange |
| 1739 | sourceBranch = self.knownBranches[branch] |
| 1740 | sourceDepotPath = self.depotPaths[0] + sourceBranch |
| 1741 | sourceRef = self.gitRefForBranch(sourceBranch) |
| 1742 | #print "source " + sourceBranch |
| 1743 | |
| 1744 | branchParentChange = int(p4Cmd("changes -m 1 %s...@1,%s" % (sourceDepotPath, firstChange))["change"]) |
| 1745 | #print "branch parent: %s" % branchParentChange |
| 1746 | gitParent = self.gitCommitByP4Change(sourceRef, branchParentChange) |
| 1747 | if len(gitParent) > 0: |
| 1748 | self.initialParents[self.gitRefForBranch(branch)] = gitParent |
| 1749 | #print "parent git commit: %s" % gitParent |
| 1750 | |
| 1751 | self.importChanges(changes) |
| 1752 | return True |
| 1753 | |
Simon Hausmann | e87f37a | 2007-08-26 16:00:52 +0200 | [diff] [blame] | 1754 | def importChanges(self, changes): |
| 1755 | cnt = 1 |
| 1756 | for change in changes: |
| 1757 | description = p4Cmd("describe %s" % change) |
| 1758 | self.updateOptionDict(description) |
| 1759 | |
| 1760 | if not self.silent: |
| 1761 | sys.stdout.write("\rImporting revision %s (%s%%)" % (change, cnt * 100 / len(changes))) |
| 1762 | sys.stdout.flush() |
| 1763 | cnt = cnt + 1 |
| 1764 | |
| 1765 | try: |
| 1766 | if self.detectBranches: |
| 1767 | branches = self.splitFilesIntoBranches(description) |
| 1768 | for branch in branches.keys(): |
| 1769 | ## HACK --hwn |
| 1770 | branchPrefix = self.depotPaths[0] + branch + "/" |
| 1771 | |
| 1772 | parent = "" |
| 1773 | |
| 1774 | filesForCommit = branches[branch] |
| 1775 | |
| 1776 | if self.verbose: |
| 1777 | print "branch is %s" % branch |
| 1778 | |
| 1779 | self.updatedBranches.add(branch) |
| 1780 | |
| 1781 | if branch not in self.createdBranches: |
| 1782 | self.createdBranches.add(branch) |
| 1783 | parent = self.knownBranches[branch] |
| 1784 | if parent == branch: |
| 1785 | parent = "" |
Simon Hausmann | 1ca3d71 | 2007-08-26 17:36:55 +0200 | [diff] [blame] | 1786 | else: |
| 1787 | fullBranch = self.projectName + branch |
| 1788 | if fullBranch not in self.p4BranchesInGit: |
| 1789 | if not self.silent: |
| 1790 | print("\n Importing new branch %s" % fullBranch); |
| 1791 | if self.importNewBranch(branch, change - 1): |
| 1792 | parent = "" |
| 1793 | self.p4BranchesInGit.append(fullBranch) |
| 1794 | if not self.silent: |
| 1795 | print("\n Resuming with change %s" % change); |
| 1796 | |
| 1797 | if self.verbose: |
| 1798 | print "parent determined through known branches: %s" % parent |
Simon Hausmann | e87f37a | 2007-08-26 16:00:52 +0200 | [diff] [blame] | 1799 | |
Simon Hausmann | 8134f69 | 2007-08-26 16:44:55 +0200 | [diff] [blame] | 1800 | branch = self.gitRefForBranch(branch) |
| 1801 | parent = self.gitRefForBranch(parent) |
Simon Hausmann | e87f37a | 2007-08-26 16:00:52 +0200 | [diff] [blame] | 1802 | |
| 1803 | if self.verbose: |
| 1804 | print "looking for initial parent for %s; current parent is %s" % (branch, parent) |
| 1805 | |
| 1806 | if len(parent) == 0 and branch in self.initialParents: |
| 1807 | parent = self.initialParents[branch] |
| 1808 | del self.initialParents[branch] |
| 1809 | |
| 1810 | self.commit(description, filesForCommit, branch, [branchPrefix], parent) |
| 1811 | else: |
| 1812 | files = self.extractFilesFromCommit(description) |
| 1813 | self.commit(description, files, self.branch, self.depotPaths, |
| 1814 | self.initialParent) |
| 1815 | self.initialParent = "" |
| 1816 | except IOError: |
| 1817 | print self.gitError.read() |
| 1818 | sys.exit(1) |
| 1819 | |
Simon Hausmann | c208a24 | 2007-08-26 16:07:18 +0200 | [diff] [blame] | 1820 | def importHeadRevision(self, revision): |
| 1821 | print "Doing initial import of %s from revision %s into %s" % (' '.join(self.depotPaths), revision, self.branch) |
| 1822 | |
Pete Wyckoff | 4e2e6ce | 2011-07-31 09:45:55 -0400 | [diff] [blame] | 1823 | details = {} |
| 1824 | details["user"] = "git perforce import user" |
Pete Wyckoff | 1494fcb | 2011-02-19 08:17:56 -0500 | [diff] [blame] | 1825 | 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] | 1826 | % (' '.join(self.depotPaths), revision)) |
| 1827 | details["change"] = revision |
| 1828 | newestRevision = 0 |
| 1829 | |
| 1830 | fileCnt = 0 |
Luke Diamand | 6de040d | 2011-10-16 10:47:52 -0400 | [diff] [blame] | 1831 | fileArgs = ["%s...%s" % (p,revision) for p in self.depotPaths] |
| 1832 | |
| 1833 | for info in p4CmdList(["files"] + fileArgs): |
Simon Hausmann | c208a24 | 2007-08-26 16:07:18 +0200 | [diff] [blame] | 1834 | |
Pete Wyckoff | 68b2859 | 2011-02-19 08:17:55 -0500 | [diff] [blame] | 1835 | if 'code' in info and info['code'] == 'error': |
Simon Hausmann | c208a24 | 2007-08-26 16:07:18 +0200 | [diff] [blame] | 1836 | sys.stderr.write("p4 returned an error: %s\n" |
| 1837 | % info['data']) |
Pete Wyckoff | d88e707 | 2011-02-19 08:17:58 -0500 | [diff] [blame] | 1838 | if info['data'].find("must refer to client") >= 0: |
| 1839 | sys.stderr.write("This particular p4 error is misleading.\n") |
| 1840 | sys.stderr.write("Perhaps the depot path was misspelled.\n"); |
| 1841 | sys.stderr.write("Depot path: %s\n" % " ".join(self.depotPaths)) |
Simon Hausmann | c208a24 | 2007-08-26 16:07:18 +0200 | [diff] [blame] | 1842 | sys.exit(1) |
Pete Wyckoff | 68b2859 | 2011-02-19 08:17:55 -0500 | [diff] [blame] | 1843 | if 'p4ExitCode' in info: |
| 1844 | sys.stderr.write("p4 exitcode: %s\n" % info['p4ExitCode']) |
Simon Hausmann | c208a24 | 2007-08-26 16:07:18 +0200 | [diff] [blame] | 1845 | sys.exit(1) |
| 1846 | |
| 1847 | |
| 1848 | change = int(info["change"]) |
| 1849 | if change > newestRevision: |
| 1850 | newestRevision = change |
| 1851 | |
Pete Wyckoff | 56c0934 | 2011-02-19 08:17:57 -0500 | [diff] [blame] | 1852 | if info["action"] in self.delete_actions: |
Simon Hausmann | c208a24 | 2007-08-26 16:07:18 +0200 | [diff] [blame] | 1853 | # don't increase the file cnt, otherwise details["depotFile123"] will have gaps! |
| 1854 | #fileCnt = fileCnt + 1 |
| 1855 | continue |
| 1856 | |
| 1857 | for prop in ["depotFile", "rev", "action", "type" ]: |
| 1858 | details["%s%s" % (prop, fileCnt)] = info[prop] |
| 1859 | |
| 1860 | fileCnt = fileCnt + 1 |
| 1861 | |
| 1862 | details["change"] = newestRevision |
Pete Wyckoff | 4e2e6ce | 2011-07-31 09:45:55 -0400 | [diff] [blame] | 1863 | |
| 1864 | # Use time from top-most change so that all git-p4 clones of |
| 1865 | # the same p4 repo have the same commit SHA1s. |
| 1866 | res = p4CmdList("describe -s %d" % newestRevision) |
| 1867 | newestTime = None |
| 1868 | for r in res: |
| 1869 | if r.has_key('time'): |
| 1870 | newestTime = int(r['time']) |
| 1871 | if newestTime is None: |
| 1872 | die("\"describe -s\" on newest change %d did not give a time") |
| 1873 | details["time"] = newestTime |
| 1874 | |
Simon Hausmann | c208a24 | 2007-08-26 16:07:18 +0200 | [diff] [blame] | 1875 | self.updateOptionDict(details) |
| 1876 | try: |
| 1877 | self.commit(details, self.extractFilesFromCommit(details), self.branch, self.depotPaths) |
| 1878 | except IOError: |
| 1879 | print "IO error with git fast-import. Is your git version recent enough?" |
| 1880 | print self.gitError.read() |
| 1881 | |
| 1882 | |
Tor Arvid Lund | 3a70cdf | 2008-02-18 15:22:08 +0100 | [diff] [blame] | 1883 | def getClientSpec(self): |
| 1884 | specList = p4CmdList( "client -o" ) |
| 1885 | temp = {} |
| 1886 | for entry in specList: |
| 1887 | for k,v in entry.iteritems(): |
| 1888 | if k.startswith("View"): |
Ian Wienand | 3952710 | 2011-02-11 16:33:48 -0800 | [diff] [blame] | 1889 | |
| 1890 | # p4 has these %%1 to %%9 arguments in specs to |
| 1891 | # reorder paths; which we can't handle (yet :) |
| 1892 | if re.match('%%\d', v) != None: |
| 1893 | print "Sorry, can't handle %%n arguments in client specs" |
| 1894 | sys.exit(1) |
| 1895 | |
Tor Arvid Lund | 3a70cdf | 2008-02-18 15:22:08 +0100 | [diff] [blame] | 1896 | if v.startswith('"'): |
| 1897 | start = 1 |
| 1898 | else: |
| 1899 | start = 0 |
| 1900 | index = v.find("...") |
Ian Wienand | 3952710 | 2011-02-11 16:33:48 -0800 | [diff] [blame] | 1901 | |
| 1902 | # save the "client view"; i.e the RHS of the view |
| 1903 | # line that tells the client where to put the |
| 1904 | # files for this view. |
| 1905 | cv = v[index+3:].strip() # +3 to remove previous '...' |
| 1906 | |
| 1907 | # if the client view doesn't end with a |
| 1908 | # ... wildcard, then we're going to mess up the |
| 1909 | # output directory, so fail gracefully. |
| 1910 | if not cv.endswith('...'): |
| 1911 | print 'Sorry, client view in "%s" needs to end with wildcard' % (k) |
| 1912 | sys.exit(1) |
| 1913 | cv=cv[:-3] |
| 1914 | |
| 1915 | # now save the view; +index means included, -index |
| 1916 | # means it should be filtered out. |
Tor Arvid Lund | 3a70cdf | 2008-02-18 15:22:08 +0100 | [diff] [blame] | 1917 | v = v[start:index] |
| 1918 | if v.startswith("-"): |
| 1919 | v = v[1:] |
Ian Wienand | 3952710 | 2011-02-11 16:33:48 -0800 | [diff] [blame] | 1920 | include = -len(v) |
Tor Arvid Lund | 3a70cdf | 2008-02-18 15:22:08 +0100 | [diff] [blame] | 1921 | else: |
Ian Wienand | 3952710 | 2011-02-11 16:33:48 -0800 | [diff] [blame] | 1922 | include = len(v) |
| 1923 | |
| 1924 | temp[v] = (include, cv) |
| 1925 | |
Tor Arvid Lund | 3a70cdf | 2008-02-18 15:22:08 +0100 | [diff] [blame] | 1926 | self.clientSpecDirs = temp.items() |
Ian Wienand | 3952710 | 2011-02-11 16:33:48 -0800 | [diff] [blame] | 1927 | self.clientSpecDirs.sort( lambda x, y: abs( y[1][0] ) - abs( x[1][0] ) ) |
Tor Arvid Lund | 3a70cdf | 2008-02-18 15:22:08 +0100 | [diff] [blame] | 1928 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1929 | def run(self, args): |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1930 | self.depotPaths = [] |
Simon Hausmann | 179caeb | 2007-03-22 22:17:42 +0100 | [diff] [blame] | 1931 | self.changeRange = "" |
| 1932 | self.initialParent = "" |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1933 | self.previousDepotPaths = [] |
Han-Wen Nienhuys | ce6f33c | 2007-05-23 16:46:29 -0300 | [diff] [blame] | 1934 | |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 1935 | # map from branch depot path to parent branch |
| 1936 | self.knownBranches = {} |
| 1937 | self.initialParents = {} |
Simon Hausmann | 5ca4461 | 2007-08-24 17:44:16 +0200 | [diff] [blame] | 1938 | self.hasOrigin = originP4BranchesExist() |
Simon Hausmann | a43ff00 | 2007-06-11 09:59:27 +0200 | [diff] [blame] | 1939 | if not self.syncWithOrigin: |
| 1940 | self.hasOrigin = False |
Simon Hausmann | 179caeb | 2007-03-22 22:17:42 +0100 | [diff] [blame] | 1941 | |
Simon Hausmann | a028a98 | 2007-05-23 00:03:08 +0200 | [diff] [blame] | 1942 | if self.importIntoRemotes: |
| 1943 | self.refPrefix = "refs/remotes/p4/" |
| 1944 | else: |
Marius Storm-Olsen | db77555 | 2007-06-07 15:13:59 +0200 | [diff] [blame] | 1945 | self.refPrefix = "refs/heads/p4/" |
Simon Hausmann | a028a98 | 2007-05-23 00:03:08 +0200 | [diff] [blame] | 1946 | |
Han-Wen Nienhuys | cebdf5a | 2007-05-23 16:53:11 -0300 | [diff] [blame] | 1947 | if self.syncWithOrigin and self.hasOrigin: |
| 1948 | if not self.silent: |
| 1949 | print "Syncing with origin first by calling git fetch origin" |
| 1950 | system("git fetch origin") |
Simon Hausmann | 10f880f | 2007-05-24 22:28:28 +0200 | [diff] [blame] | 1951 | |
Simon Hausmann | 569d1bd | 2007-03-22 21:34:16 +0100 | [diff] [blame] | 1952 | if len(self.branch) == 0: |
Marius Storm-Olsen | db77555 | 2007-06-07 15:13:59 +0200 | [diff] [blame] | 1953 | self.branch = self.refPrefix + "master" |
Simon Hausmann | a028a98 | 2007-05-23 00:03:08 +0200 | [diff] [blame] | 1954 | if gitBranchExists("refs/heads/p4") and self.importIntoRemotes: |
Simon Hausmann | 48df6fd | 2007-05-17 21:18:53 +0200 | [diff] [blame] | 1955 | system("git update-ref %s refs/heads/p4" % self.branch) |
Simon Hausmann | 48df6fd | 2007-05-17 21:18:53 +0200 | [diff] [blame] | 1956 | system("git branch -D p4"); |
Simon Hausmann | faf1bd2 | 2007-05-21 10:05:30 +0200 | [diff] [blame] | 1957 | # create it /after/ importing, when master exists |
Simon Hausmann | 0058a33 | 2007-08-24 17:46:16 +0200 | [diff] [blame] | 1958 | if not gitBranchExists(self.refPrefix + "HEAD") and self.importIntoRemotes and gitBranchExists(self.branch): |
Simon Hausmann | a3c55c0 | 2007-05-27 15:48:01 +0200 | [diff] [blame] | 1959 | system("git symbolic-ref %sHEAD %s" % (self.refPrefix, self.branch)) |
Simon Hausmann | 179caeb | 2007-03-22 22:17:42 +0100 | [diff] [blame] | 1960 | |
Pete Wyckoff | 09fca77 | 2011-12-24 21:07:39 -0500 | [diff] [blame] | 1961 | if not self.useClientSpec: |
| 1962 | if gitConfig("git-p4.useclientspec", "--bool") == "true": |
| 1963 | self.useClientSpec = True |
| 1964 | if self.useClientSpec: |
Tor Arvid Lund | 3a70cdf | 2008-02-18 15:22:08 +0100 | [diff] [blame] | 1965 | self.getClientSpec() |
| 1966 | |
Han-Wen Nienhuys | 6a49f8e | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1967 | # TODO: should always look at previous commits, |
| 1968 | # merge with previous imports, if possible. |
| 1969 | if args == []: |
Simon Hausmann | d414c74 | 2007-05-25 11:36:42 +0200 | [diff] [blame] | 1970 | if self.hasOrigin: |
Simon Hausmann | 5ca4461 | 2007-08-24 17:44:16 +0200 | [diff] [blame] | 1971 | createOrUpdateBranchesFromOrigin(self.refPrefix, self.silent) |
Simon Hausmann | abcd790 | 2007-05-24 22:25:36 +0200 | [diff] [blame] | 1972 | self.listExistingP4GitBranches() |
| 1973 | |
| 1974 | if len(self.p4BranchesInGit) > 1: |
| 1975 | if not self.silent: |
| 1976 | print "Importing from/into multiple branches" |
| 1977 | self.detectBranches = True |
Simon Hausmann | 967f72e | 2007-03-23 09:30:41 +0100 | [diff] [blame] | 1978 | |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 1979 | if self.verbose: |
| 1980 | print "branches: %s" % self.p4BranchesInGit |
| 1981 | |
| 1982 | p4Change = 0 |
| 1983 | for branch in self.p4BranchesInGit: |
Han-Wen Nienhuys | cebdf5a | 2007-05-23 16:53:11 -0300 | [diff] [blame] | 1984 | logMsg = extractLogMessageFromGitCommit(self.refPrefix + branch) |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1985 | |
| 1986 | settings = extractSettingsGitLog(logMsg) |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 1987 | |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1988 | self.readOptions(settings) |
| 1989 | if (settings.has_key('depot-paths') |
| 1990 | and settings.has_key ('change')): |
| 1991 | change = int(settings['change']) + 1 |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 1992 | p4Change = max(p4Change, change) |
| 1993 | |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1994 | depotPaths = sorted(settings['depot-paths']) |
| 1995 | if self.previousDepotPaths == []: |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1996 | self.previousDepotPaths = depotPaths |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 1997 | else: |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1998 | paths = [] |
| 1999 | for (prev, cur) in zip(self.previousDepotPaths, depotPaths): |
Vitor Antunes | 04d277b | 2011-08-19 00:44:03 +0100 | [diff] [blame] | 2000 | prev_list = prev.split("/") |
| 2001 | cur_list = cur.split("/") |
| 2002 | for i in range(0, min(len(cur_list), len(prev_list))): |
| 2003 | if cur_list[i] <> prev_list[i]: |
Simon Hausmann | 583e170 | 2007-06-07 09:37:13 +0200 | [diff] [blame] | 2004 | i = i - 1 |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2005 | break |
| 2006 | |
Vitor Antunes | 04d277b | 2011-08-19 00:44:03 +0100 | [diff] [blame] | 2007 | paths.append ("/".join(cur_list[:i + 1])) |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2008 | |
| 2009 | self.previousDepotPaths = paths |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 2010 | |
| 2011 | if p4Change > 0: |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2012 | self.depotPaths = sorted(self.previousDepotPaths) |
Simon Hausmann | d590467 | 2007-05-19 11:07:32 +0200 | [diff] [blame] | 2013 | self.changeRange = "@%s,#head" % p4Change |
Simon Hausmann | 330f53b | 2007-06-07 09:39:51 +0200 | [diff] [blame] | 2014 | if not self.detectBranches: |
| 2015 | self.initialParent = parseRevision(self.branch) |
Simon Hausmann | 341dc1c | 2007-05-21 00:39:16 +0200 | [diff] [blame] | 2016 | if not self.silent and not self.detectBranches: |
Simon Hausmann | 967f72e | 2007-03-23 09:30:41 +0100 | [diff] [blame] | 2017 | print "Performing incremental import into %s git branch" % self.branch |
Simon Hausmann | 569d1bd | 2007-03-22 21:34:16 +0100 | [diff] [blame] | 2018 | |
Simon Hausmann | f9162f6 | 2007-05-17 09:02:45 +0200 | [diff] [blame] | 2019 | if not self.branch.startswith("refs/"): |
| 2020 | self.branch = "refs/heads/" + self.branch |
Simon Hausmann | 179caeb | 2007-03-22 22:17:42 +0100 | [diff] [blame] | 2021 | |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2022 | if len(args) == 0 and self.depotPaths: |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 2023 | if not self.silent: |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2024 | print "Depot paths: %s" % ' '.join(self.depotPaths) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 2025 | else: |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2026 | if self.depotPaths and self.depotPaths != args: |
Han-Wen Nienhuys | cebdf5a | 2007-05-23 16:53:11 -0300 | [diff] [blame] | 2027 | 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] | 2028 | "This doesn't work!" % (' '.join (self.depotPaths), |
| 2029 | ' '.join (args))) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 2030 | sys.exit(1) |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2031 | |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2032 | self.depotPaths = sorted(args) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 2033 | |
Simon Hausmann | 1c49fc1 | 2007-08-26 16:04:34 +0200 | [diff] [blame] | 2034 | revision = "" |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 2035 | self.users = {} |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 2036 | |
Pete Wyckoff | 58c8bc7 | 2011-12-24 21:07:35 -0500 | [diff] [blame] | 2037 | # Make sure no revision specifiers are used when --changesfile |
| 2038 | # is specified. |
| 2039 | bad_changesfile = False |
| 2040 | if len(self.changesFile) > 0: |
| 2041 | for p in self.depotPaths: |
| 2042 | if p.find("@") >= 0 or p.find("#") >= 0: |
| 2043 | bad_changesfile = True |
| 2044 | break |
| 2045 | if bad_changesfile: |
| 2046 | die("Option --changesfile is incompatible with revision specifiers") |
| 2047 | |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2048 | newPaths = [] |
| 2049 | for p in self.depotPaths: |
| 2050 | if p.find("@") != -1: |
| 2051 | atIdx = p.index("@") |
| 2052 | self.changeRange = p[atIdx:] |
| 2053 | if self.changeRange == "@all": |
| 2054 | self.changeRange = "" |
Han-Wen Nienhuys | 6a49f8e | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2055 | elif ',' not in self.changeRange: |
Simon Hausmann | 1c49fc1 | 2007-08-26 16:04:34 +0200 | [diff] [blame] | 2056 | revision = self.changeRange |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2057 | self.changeRange = "" |
Han-Wen Nienhuys | 7fcff9d | 2007-07-23 15:56:37 -0700 | [diff] [blame] | 2058 | p = p[:atIdx] |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2059 | elif p.find("#") != -1: |
| 2060 | hashIdx = p.index("#") |
Simon Hausmann | 1c49fc1 | 2007-08-26 16:04:34 +0200 | [diff] [blame] | 2061 | revision = p[hashIdx:] |
Han-Wen Nienhuys | 7fcff9d | 2007-07-23 15:56:37 -0700 | [diff] [blame] | 2062 | p = p[:hashIdx] |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2063 | elif self.previousDepotPaths == []: |
Pete Wyckoff | 58c8bc7 | 2011-12-24 21:07:35 -0500 | [diff] [blame] | 2064 | # pay attention to changesfile, if given, else import |
| 2065 | # the entire p4 tree at the head revision |
| 2066 | if len(self.changesFile) == 0: |
| 2067 | revision = "#head" |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 2068 | |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2069 | p = re.sub ("\.\.\.$", "", p) |
| 2070 | if not p.endswith("/"): |
| 2071 | p += "/" |
| 2072 | |
| 2073 | newPaths.append(p) |
| 2074 | |
| 2075 | self.depotPaths = newPaths |
| 2076 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 2077 | |
Simon Hausmann | b607e71 | 2007-05-20 10:55:54 +0200 | [diff] [blame] | 2078 | self.loadUserMapFromCache() |
Simon Hausmann | cb53e1f | 2007-04-08 00:12:02 +0200 | [diff] [blame] | 2079 | self.labels = {} |
| 2080 | if self.detectLabels: |
| 2081 | self.getLabels(); |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 2082 | |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 2083 | if self.detectBranches: |
Simon Hausmann | df45092 | 2007-06-08 08:49:22 +0200 | [diff] [blame] | 2084 | ## FIXME - what's a P4 projectName ? |
| 2085 | self.projectName = self.guessProjectName() |
| 2086 | |
Simon Hausmann | 38f9f5e | 2007-11-15 10:38:45 +0100 | [diff] [blame] | 2087 | if self.hasOrigin: |
| 2088 | self.getBranchMappingFromGitBranches() |
| 2089 | else: |
| 2090 | self.getBranchMapping() |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 2091 | if self.verbose: |
| 2092 | print "p4-git branches: %s" % self.p4BranchesInGit |
| 2093 | print "initial parents: %s" % self.initialParents |
| 2094 | for b in self.p4BranchesInGit: |
| 2095 | if b != "master": |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2096 | |
| 2097 | ## FIXME |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 2098 | b = b[len(self.projectName):] |
| 2099 | self.createdBranches.add(b) |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 2100 | |
Simon Hausmann | f291b4e | 2007-04-14 11:21:50 +0200 | [diff] [blame] | 2101 | self.tz = "%+03d%02d" % (- time.timezone / 3600, ((- time.timezone % 3600) / 60)) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 2102 | |
Han-Wen Nienhuys | cebdf5a | 2007-05-23 16:53:11 -0300 | [diff] [blame] | 2103 | importProcess = subprocess.Popen(["git", "fast-import"], |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2104 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
| 2105 | stderr=subprocess.PIPE); |
Simon Hausmann | 0848358 | 2007-05-15 14:31:06 +0200 | [diff] [blame] | 2106 | self.gitOutput = importProcess.stdout |
| 2107 | self.gitStream = importProcess.stdin |
| 2108 | self.gitError = importProcess.stderr |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 2109 | |
Simon Hausmann | 1c49fc1 | 2007-08-26 16:04:34 +0200 | [diff] [blame] | 2110 | if revision: |
Simon Hausmann | c208a24 | 2007-08-26 16:07:18 +0200 | [diff] [blame] | 2111 | self.importHeadRevision(revision) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 2112 | else: |
| 2113 | changes = [] |
| 2114 | |
Simon Hausmann | 0828ab1 | 2007-03-20 20:59:30 +0100 | [diff] [blame] | 2115 | if len(self.changesFile) > 0: |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 2116 | output = open(self.changesFile).readlines() |
Reilly Grant | 1d7367d | 2009-09-10 00:02:38 -0700 | [diff] [blame] | 2117 | changeSet = set() |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 2118 | for line in output: |
| 2119 | changeSet.add(int(line)) |
| 2120 | |
| 2121 | for change in changeSet: |
| 2122 | changes.append(change) |
| 2123 | |
| 2124 | changes.sort() |
| 2125 | else: |
Pete Wyckoff | accad8e | 2011-03-16 16:52:46 -0400 | [diff] [blame] | 2126 | # catch "git-p4 sync" with no new branches, in a repo that |
| 2127 | # does not have any existing git-p4 branches |
| 2128 | if len(args) == 0 and not self.p4BranchesInGit: |
Pete Wyckoff | e32e00d | 2011-02-19 08:17:59 -0500 | [diff] [blame] | 2129 | die("No remote p4 branches. Perhaps you never did \"git p4 clone\" in here."); |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 2130 | if self.verbose: |
Han-Wen Nienhuys | 86dff6b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2131 | print "Getting p4 changes for %s...%s" % (', '.join(self.depotPaths), |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2132 | self.changeRange) |
Simon Hausmann | 4f6432d | 2007-08-26 15:56:36 +0200 | [diff] [blame] | 2133 | changes = p4ChangesForPaths(self.depotPaths, self.changeRange) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 2134 | |
Simon Hausmann | 01a9c9c | 2007-05-23 00:07:35 +0200 | [diff] [blame] | 2135 | if len(self.maxChanges) > 0: |
Han-Wen Nienhuys | 7fcff9d | 2007-07-23 15:56:37 -0700 | [diff] [blame] | 2136 | changes = changes[:min(int(self.maxChanges), len(changes))] |
Simon Hausmann | 01a9c9c | 2007-05-23 00:07:35 +0200 | [diff] [blame] | 2137 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 2138 | if len(changes) == 0: |
Simon Hausmann | 0828ab1 | 2007-03-20 20:59:30 +0100 | [diff] [blame] | 2139 | if not self.silent: |
Simon Hausmann | 341dc1c | 2007-05-21 00:39:16 +0200 | [diff] [blame] | 2140 | print "No changes to import!" |
Simon Hausmann | 1f52af6 | 2007-04-08 00:07:02 +0200 | [diff] [blame] | 2141 | return True |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 2142 | |
Simon Hausmann | a9d1a27 | 2007-06-11 23:28:03 +0200 | [diff] [blame] | 2143 | if not self.silent and not self.detectBranches: |
| 2144 | print "Import destination: %s" % self.branch |
| 2145 | |
Simon Hausmann | 341dc1c | 2007-05-21 00:39:16 +0200 | [diff] [blame] | 2146 | self.updatedBranches = set() |
| 2147 | |
Simon Hausmann | e87f37a | 2007-08-26 16:00:52 +0200 | [diff] [blame] | 2148 | self.importChanges(changes) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 2149 | |
Simon Hausmann | 341dc1c | 2007-05-21 00:39:16 +0200 | [diff] [blame] | 2150 | if not self.silent: |
| 2151 | print "" |
| 2152 | if len(self.updatedBranches) > 0: |
| 2153 | sys.stdout.write("Updated branches: ") |
| 2154 | for b in self.updatedBranches: |
| 2155 | sys.stdout.write("%s " % b) |
| 2156 | sys.stdout.write("\n") |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 2157 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 2158 | self.gitStream.close() |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 2159 | if importProcess.wait() != 0: |
| 2160 | die("fast-import failed: %s" % self.gitError.read()) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 2161 | self.gitOutput.close() |
| 2162 | self.gitError.close() |
| 2163 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 2164 | return True |
| 2165 | |
Simon Hausmann | 01ce1fe | 2007-04-07 23:46:50 +0200 | [diff] [blame] | 2166 | class P4Rebase(Command): |
| 2167 | def __init__(self): |
| 2168 | Command.__init__(self) |
Simon Hausmann | 0126510 | 2007-05-25 10:36:10 +0200 | [diff] [blame] | 2169 | self.options = [ ] |
Han-Wen Nienhuys | cebdf5a | 2007-05-23 16:53:11 -0300 | [diff] [blame] | 2170 | self.description = ("Fetches the latest revision from perforce and " |
| 2171 | + "rebases the current work (branch) against it") |
Simon Hausmann | 68c4215 | 2007-06-07 12:51:03 +0200 | [diff] [blame] | 2172 | self.verbose = False |
Simon Hausmann | 01ce1fe | 2007-04-07 23:46:50 +0200 | [diff] [blame] | 2173 | |
| 2174 | def run(self, args): |
| 2175 | sync = P4Sync() |
| 2176 | sync.run([]) |
Simon Hausmann | d7e3868 | 2007-06-12 14:34:46 +0200 | [diff] [blame] | 2177 | |
Simon Hausmann | 14594f4 | 2007-08-22 09:07:15 +0200 | [diff] [blame] | 2178 | return self.rebase() |
| 2179 | |
| 2180 | def rebase(self): |
Simon Hausmann | 36ee4ee | 2008-01-07 14:21:45 +0100 | [diff] [blame] | 2181 | if os.system("git update-index --refresh") != 0: |
| 2182 | 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."); |
| 2183 | if len(read_pipe("git diff-index HEAD --")) > 0: |
| 2184 | die("You have uncommited changes. Please commit them before rebasing or stash them away with git stash."); |
| 2185 | |
Simon Hausmann | d7e3868 | 2007-06-12 14:34:46 +0200 | [diff] [blame] | 2186 | [upstream, settings] = findUpstreamBranchPoint() |
| 2187 | if len(upstream) == 0: |
| 2188 | die("Cannot find upstream branchpoint for rebase") |
| 2189 | |
| 2190 | # the branchpoint may be p4/foo~3, so strip off the parent |
| 2191 | upstream = re.sub("~[0-9]+$", "", upstream) |
| 2192 | |
| 2193 | print "Rebasing the current branch onto %s" % upstream |
Han-Wen Nienhuys | b25b206 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2194 | oldHead = read_pipe("git rev-parse HEAD").strip() |
Simon Hausmann | d7e3868 | 2007-06-12 14:34:46 +0200 | [diff] [blame] | 2195 | system("git rebase %s" % upstream) |
Simon Hausmann | 1f52af6 | 2007-04-08 00:07:02 +0200 | [diff] [blame] | 2196 | system("git diff-tree --stat --summary -M %s HEAD" % oldHead) |
Simon Hausmann | 01ce1fe | 2007-04-07 23:46:50 +0200 | [diff] [blame] | 2197 | return True |
| 2198 | |
Simon Hausmann | f9a3a4f | 2007-04-08 10:08:26 +0200 | [diff] [blame] | 2199 | class P4Clone(P4Sync): |
| 2200 | def __init__(self): |
| 2201 | P4Sync.__init__(self) |
| 2202 | 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] | 2203 | self.usage = "usage: %prog [options] //depot/path[@revRange]" |
Tommy Thorn | 354081d | 2008-02-03 10:38:51 -0800 | [diff] [blame] | 2204 | self.options += [ |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2205 | optparse.make_option("--destination", dest="cloneDestination", |
| 2206 | action='store', default=None, |
Tommy Thorn | 354081d | 2008-02-03 10:38:51 -0800 | [diff] [blame] | 2207 | help="where to leave result of the clone"), |
| 2208 | optparse.make_option("-/", dest="cloneExclude", |
| 2209 | action="append", type="string", |
Pete Wyckoff | 3820007 | 2011-02-19 08:18:01 -0500 | [diff] [blame] | 2210 | help="exclude depot path"), |
| 2211 | optparse.make_option("--bare", dest="cloneBare", |
| 2212 | action="store_true", default=False), |
Tommy Thorn | 354081d | 2008-02-03 10:38:51 -0800 | [diff] [blame] | 2213 | ] |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2214 | self.cloneDestination = None |
Simon Hausmann | f9a3a4f | 2007-04-08 10:08:26 +0200 | [diff] [blame] | 2215 | self.needsGit = False |
Pete Wyckoff | 3820007 | 2011-02-19 08:18:01 -0500 | [diff] [blame] | 2216 | self.cloneBare = False |
Simon Hausmann | f9a3a4f | 2007-04-08 10:08:26 +0200 | [diff] [blame] | 2217 | |
Tommy Thorn | 354081d | 2008-02-03 10:38:51 -0800 | [diff] [blame] | 2218 | # This is required for the "append" cloneExclude action |
| 2219 | def ensure_value(self, attr, value): |
| 2220 | if not hasattr(self, attr) or getattr(self, attr) is None: |
| 2221 | setattr(self, attr, value) |
| 2222 | return getattr(self, attr) |
| 2223 | |
Han-Wen Nienhuys | 6a49f8e | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2224 | def defaultDestination(self, args): |
| 2225 | ## TODO: use common prefix of args? |
| 2226 | depotPath = args[0] |
| 2227 | depotDir = re.sub("(@[^@]*)$", "", depotPath) |
| 2228 | depotDir = re.sub("(#[^#]*)$", "", depotDir) |
Toby Allsopp | 053d9e4 | 2008-02-05 09:41:43 +1300 | [diff] [blame] | 2229 | depotDir = re.sub(r"\.\.\.$", "", depotDir) |
Han-Wen Nienhuys | 6a49f8e | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2230 | depotDir = re.sub(r"/$", "", depotDir) |
| 2231 | return os.path.split(depotDir)[1] |
| 2232 | |
Simon Hausmann | f9a3a4f | 2007-04-08 10:08:26 +0200 | [diff] [blame] | 2233 | def run(self, args): |
| 2234 | if len(args) < 1: |
| 2235 | return False |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2236 | |
| 2237 | if self.keepRepoPath and not self.cloneDestination: |
| 2238 | sys.stderr.write("Must specify destination for --keep-path\n") |
| 2239 | sys.exit(1) |
Simon Hausmann | f9a3a4f | 2007-04-08 10:08:26 +0200 | [diff] [blame] | 2240 | |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2241 | depotPaths = args |
Simon Hausmann | 5e100b5 | 2007-06-07 21:12:25 +0200 | [diff] [blame] | 2242 | |
| 2243 | if not self.cloneDestination and len(depotPaths) > 1: |
| 2244 | self.cloneDestination = depotPaths[-1] |
| 2245 | depotPaths = depotPaths[:-1] |
| 2246 | |
Tommy Thorn | 354081d | 2008-02-03 10:38:51 -0800 | [diff] [blame] | 2247 | self.cloneExclude = ["/"+p for p in self.cloneExclude] |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2248 | for p in depotPaths: |
| 2249 | if not p.startswith("//"): |
| 2250 | return False |
Simon Hausmann | f9a3a4f | 2007-04-08 10:08:26 +0200 | [diff] [blame] | 2251 | |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2252 | if not self.cloneDestination: |
Marius Storm-Olsen | 98ad4fa | 2007-06-07 15:08:33 +0200 | [diff] [blame] | 2253 | self.cloneDestination = self.defaultDestination(args) |
Simon Hausmann | f9a3a4f | 2007-04-08 10:08:26 +0200 | [diff] [blame] | 2254 | |
Han-Wen Nienhuys | 86dff6b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2255 | print "Importing from %s into %s" % (', '.join(depotPaths), self.cloneDestination) |
Pete Wyckoff | 3820007 | 2011-02-19 08:18:01 -0500 | [diff] [blame] | 2256 | |
Kevin Green | c3bf3f1 | 2007-06-11 16:48:07 -0400 | [diff] [blame] | 2257 | if not os.path.exists(self.cloneDestination): |
| 2258 | os.makedirs(self.cloneDestination) |
Robert Blum | 053fd0c | 2008-08-01 12:50:03 -0700 | [diff] [blame] | 2259 | chdir(self.cloneDestination) |
Pete Wyckoff | 3820007 | 2011-02-19 08:18:01 -0500 | [diff] [blame] | 2260 | |
| 2261 | init_cmd = [ "git", "init" ] |
| 2262 | if self.cloneBare: |
| 2263 | init_cmd.append("--bare") |
| 2264 | subprocess.check_call(init_cmd) |
| 2265 | |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2266 | if not P4Sync.run(self, depotPaths): |
Simon Hausmann | f9a3a4f | 2007-04-08 10:08:26 +0200 | [diff] [blame] | 2267 | return False |
Simon Hausmann | f9a3a4f | 2007-04-08 10:08:26 +0200 | [diff] [blame] | 2268 | if self.branch != "master": |
Tor Arvid Lund | e990501 | 2008-08-28 00:36:12 +0200 | [diff] [blame] | 2269 | if self.importIntoRemotes: |
| 2270 | masterbranch = "refs/remotes/p4/master" |
| 2271 | else: |
| 2272 | masterbranch = "refs/heads/p4/master" |
| 2273 | if gitBranchExists(masterbranch): |
| 2274 | system("git branch master %s" % masterbranch) |
Pete Wyckoff | 3820007 | 2011-02-19 08:18:01 -0500 | [diff] [blame] | 2275 | if not self.cloneBare: |
| 2276 | system("git checkout -f") |
Simon Hausmann | 8f9b2e0 | 2007-05-18 22:13:26 +0200 | [diff] [blame] | 2277 | else: |
| 2278 | print "Could not detect main branch. No checkout/master branch created." |
Han-Wen Nienhuys | 86dff6b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2279 | |
Simon Hausmann | f9a3a4f | 2007-04-08 10:08:26 +0200 | [diff] [blame] | 2280 | return True |
| 2281 | |
Simon Hausmann | 09d89de | 2007-06-20 23:10:28 +0200 | [diff] [blame] | 2282 | class P4Branches(Command): |
| 2283 | def __init__(self): |
| 2284 | Command.__init__(self) |
| 2285 | self.options = [ ] |
| 2286 | self.description = ("Shows the git branches that hold imports and their " |
| 2287 | + "corresponding perforce depot paths") |
| 2288 | self.verbose = False |
| 2289 | |
| 2290 | def run(self, args): |
Simon Hausmann | 5ca4461 | 2007-08-24 17:44:16 +0200 | [diff] [blame] | 2291 | if originP4BranchesExist(): |
| 2292 | createOrUpdateBranchesFromOrigin() |
| 2293 | |
Simon Hausmann | 09d89de | 2007-06-20 23:10:28 +0200 | [diff] [blame] | 2294 | cmdline = "git rev-parse --symbolic " |
| 2295 | cmdline += " --remotes" |
| 2296 | |
| 2297 | for line in read_pipe_lines(cmdline): |
| 2298 | line = line.strip() |
| 2299 | |
| 2300 | if not line.startswith('p4/') or line == "p4/HEAD": |
| 2301 | continue |
| 2302 | branch = line |
| 2303 | |
| 2304 | log = extractLogMessageFromGitCommit("refs/remotes/%s" % branch) |
| 2305 | settings = extractSettingsGitLog(log) |
| 2306 | |
| 2307 | print "%s <= %s (%s)" % (branch, ",".join(settings["depot-paths"]), settings["change"]) |
| 2308 | return True |
| 2309 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 2310 | class HelpFormatter(optparse.IndentedHelpFormatter): |
| 2311 | def __init__(self): |
| 2312 | optparse.IndentedHelpFormatter.__init__(self) |
| 2313 | |
| 2314 | def format_description(self, description): |
| 2315 | if description: |
| 2316 | return description + "\n" |
| 2317 | else: |
| 2318 | return "" |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 2319 | |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 2320 | def printUsage(commands): |
| 2321 | print "usage: %s <command> [options]" % sys.argv[0] |
| 2322 | print "" |
| 2323 | print "valid commands: %s" % ", ".join(commands) |
| 2324 | print "" |
| 2325 | print "Try %s <command> --help for command specific help." % sys.argv[0] |
| 2326 | print "" |
| 2327 | |
| 2328 | commands = { |
Han-Wen Nienhuys | b86f737 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2329 | "debug" : P4Debug, |
| 2330 | "submit" : P4Submit, |
Marius Storm-Olsen | a9834f5 | 2007-10-09 16:16:09 +0200 | [diff] [blame] | 2331 | "commit" : P4Submit, |
Han-Wen Nienhuys | b86f737 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2332 | "sync" : P4Sync, |
| 2333 | "rebase" : P4Rebase, |
| 2334 | "clone" : P4Clone, |
Simon Hausmann | 09d89de | 2007-06-20 23:10:28 +0200 | [diff] [blame] | 2335 | "rollback" : P4RollBack, |
| 2336 | "branches" : P4Branches |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 2337 | } |
| 2338 | |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 2339 | |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2340 | def main(): |
| 2341 | if len(sys.argv[1:]) == 0: |
| 2342 | printUsage(commands.keys()) |
| 2343 | sys.exit(2) |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 2344 | |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2345 | cmd = "" |
| 2346 | cmdName = sys.argv[1] |
| 2347 | try: |
Han-Wen Nienhuys | b86f737 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2348 | klass = commands[cmdName] |
| 2349 | cmd = klass() |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2350 | except KeyError: |
| 2351 | print "unknown command %s" % cmdName |
| 2352 | print "" |
| 2353 | printUsage(commands.keys()) |
| 2354 | sys.exit(2) |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 2355 | |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2356 | options = cmd.options |
Han-Wen Nienhuys | b86f737 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2357 | cmd.gitdir = os.environ.get("GIT_DIR", None) |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 2358 | |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2359 | args = sys.argv[2:] |
Simon Hausmann | e20a9e5 | 2007-03-26 00:13:51 +0200 | [diff] [blame] | 2360 | |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2361 | if len(options) > 0: |
Pete Wyckoff | ef86890 | 2011-12-24 21:07:32 -0500 | [diff] [blame] | 2362 | if cmd.needsGit: |
| 2363 | options.append(optparse.make_option("--git-dir", dest="gitdir")) |
Simon Hausmann | e20a9e5 | 2007-03-26 00:13:51 +0200 | [diff] [blame] | 2364 | |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2365 | parser = optparse.OptionParser(cmd.usage.replace("%prog", "%prog " + cmdName), |
| 2366 | options, |
| 2367 | description = cmd.description, |
| 2368 | formatter = HelpFormatter()) |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 2369 | |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2370 | (cmd, args) = parser.parse_args(sys.argv[2:], cmd); |
| 2371 | global verbose |
| 2372 | verbose = cmd.verbose |
| 2373 | if cmd.needsGit: |
Han-Wen Nienhuys | b86f737 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2374 | if cmd.gitdir == None: |
| 2375 | cmd.gitdir = os.path.abspath(".git") |
| 2376 | if not isValidGitDir(cmd.gitdir): |
| 2377 | cmd.gitdir = read_pipe("git rev-parse --git-dir").strip() |
| 2378 | if os.path.exists(cmd.gitdir): |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2379 | cdup = read_pipe("git rev-parse --show-cdup").strip() |
| 2380 | if len(cdup) > 0: |
Robert Blum | 053fd0c | 2008-08-01 12:50:03 -0700 | [diff] [blame] | 2381 | chdir(cdup); |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2382 | |
Han-Wen Nienhuys | b86f737 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2383 | if not isValidGitDir(cmd.gitdir): |
| 2384 | if isValidGitDir(cmd.gitdir + "/.git"): |
| 2385 | cmd.gitdir += "/.git" |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2386 | else: |
Han-Wen Nienhuys | b86f737 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2387 | die("fatal: cannot locate git repository at %s" % cmd.gitdir) |
Simon Hausmann | 8910ac0 | 2007-03-26 08:18:55 +0200 | [diff] [blame] | 2388 | |
Han-Wen Nienhuys | b86f737 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2389 | os.environ["GIT_DIR"] = cmd.gitdir |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 2390 | |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2391 | if not cmd.run(args): |
| 2392 | parser.print_help() |
Pete Wyckoff | 09fca77 | 2011-12-24 21:07:39 -0500 | [diff] [blame] | 2393 | sys.exit(2) |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 2394 | |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 2395 | |
| 2396 | if __name__ == '__main__': |
| 2397 | main() |