blob: 3e1aa276cff93172cb476c54369c35fd255647b8 [file] [log] [blame]
Simon Hausmann86949ee2007-03-19 20:59:12 +01001#!/usr/bin/env python
2#
3# git-p4.py -- A tool for bidirectional operation between a Perforce depot and git.
4#
Simon Hausmannc8cbbee2007-05-28 14:43:25 +02005# Author: Simon Hausmann <simon@lst.de>
6# Copyright: 2007 Simon Hausmann <simon@lst.de>
Simon Hausmann83dce552007-03-19 22:26:36 +01007# 2007 Trolltech ASA
Simon Hausmann86949ee2007-03-19 20:59:12 +01008# License: MIT <http://www.opensource.org/licenses/mit-license.php>
9#
10
Reilly Grant1d7367d2009-09-10 00:02:38 -070011import optparse, sys, os, marshal, subprocess, shelve
12import tempfile, getopt, os.path, time, platform
Han-Wen Nienhuysce6f33c2007-05-23 16:46:29 -030013import re
Han-Wen Nienhuys8b41a972007-05-23 18:20:53 -030014
Han-Wen Nienhuys4addad22007-05-23 18:49:35 -030015verbose = False
Simon Hausmann86949ee2007-03-19 20:59:12 +010016
Anand Kumria21a50752008-08-10 19:26:28 +010017
18def 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 Diamand6de040d2011-10-16 10:47:52 -040025 real_cmd = ["p4"]
Anand Kumriaabcaf072008-08-10 19:26:31 +010026
27 user = gitConfig("git-p4.user")
28 if len(user) > 0:
Luke Diamand6de040d2011-10-16 10:47:52 -040029 real_cmd += ["-u",user]
Anand Kumriaabcaf072008-08-10 19:26:31 +010030
31 password = gitConfig("git-p4.password")
32 if len(password) > 0:
Luke Diamand6de040d2011-10-16 10:47:52 -040033 real_cmd += ["-P", password]
Anand Kumriaabcaf072008-08-10 19:26:31 +010034
35 port = gitConfig("git-p4.port")
36 if len(port) > 0:
Luke Diamand6de040d2011-10-16 10:47:52 -040037 real_cmd += ["-p", port]
Anand Kumriaabcaf072008-08-10 19:26:31 +010038
39 host = gitConfig("git-p4.host")
40 if len(host) > 0:
Luke Diamand6de040d2011-10-16 10:47:52 -040041 real_cmd += ["-h", host]
Anand Kumriaabcaf072008-08-10 19:26:31 +010042
43 client = gitConfig("git-p4.client")
44 if len(client) > 0:
Luke Diamand6de040d2011-10-16 10:47:52 -040045 real_cmd += ["-c", client]
Anand Kumriaabcaf072008-08-10 19:26:31 +010046
Luke Diamand6de040d2011-10-16 10:47:52 -040047
48 if isinstance(cmd,basestring):
49 real_cmd = ' '.join(real_cmd) + ' ' + cmd
50 else:
51 real_cmd += cmd
Anand Kumria21a50752008-08-10 19:26:28 +010052 return real_cmd
53
Robert Blum053fd0c2008-08-01 12:50:03 -070054def chdir(dir):
Luke Diamand6de040d2011-10-16 10:47:52 -040055 # P4 uses the PWD environment variable rather than getcwd(). Since we're
Gary Gibbonsbf1d68f2011-12-09 18:48:16 -050056 # 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 Blum053fd0c2008-08-01 12:50:03 -070058 os.chdir(dir)
Gary Gibbonsbf1d68f2011-12-09 18:48:16 -050059 os.environ['PWD'] = os.getcwd()
Robert Blum053fd0c2008-08-01 12:50:03 -070060
Han-Wen Nienhuys86dff6b2007-05-23 18:49:35 -030061def die(msg):
62 if verbose:
63 raise Exception(msg)
64 else:
65 sys.stderr.write(msg + "\n")
66 sys.exit(1)
67
Luke Diamand6de040d2011-10-16 10:47:52 -040068def write_pipe(c, stdin):
Han-Wen Nienhuys4addad22007-05-23 18:49:35 -030069 if verbose:
Luke Diamand6de040d2011-10-16 10:47:52 -040070 sys.stderr.write('Writing pipe: %s\n' % str(c))
Han-Wen Nienhuysb016d392007-05-23 17:10:46 -030071
Luke Diamand6de040d2011-10-16 10:47:52 -040072 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 Nienhuysb016d392007-05-23 17:10:46 -030079
80 return val
81
Luke Diamand6de040d2011-10-16 10:47:52 -040082def p4_write_pipe(c, stdin):
Anand Kumriad9429192008-08-14 23:40:38 +010083 real_cmd = p4_build_cmd(c)
Luke Diamand6de040d2011-10-16 10:47:52 -040084 return write_pipe(real_cmd, stdin)
Anand Kumriad9429192008-08-14 23:40:38 +010085
Han-Wen Nienhuys4addad22007-05-23 18:49:35 -030086def read_pipe(c, ignore_error=False):
87 if verbose:
Luke Diamand6de040d2011-10-16 10:47:52 -040088 sys.stderr.write('Reading pipe: %s\n' % str(c))
Han-Wen Nienhuys8b41a972007-05-23 18:20:53 -030089
Luke Diamand6de040d2011-10-16 10:47:52 -040090 expand = isinstance(c,basestring)
91 p = subprocess.Popen(c, stdout=subprocess.PIPE, shell=expand)
92 pipe = p.stdout
Han-Wen Nienhuysb016d392007-05-23 17:10:46 -030093 val = pipe.read()
Luke Diamand6de040d2011-10-16 10:47:52 -040094 if p.wait() and not ignore_error:
95 die('Command failed: %s' % str(c))
Han-Wen Nienhuysb016d392007-05-23 17:10:46 -030096
97 return val
98
Anand Kumriad9429192008-08-14 23:40:38 +010099def p4_read_pipe(c, ignore_error=False):
100 real_cmd = p4_build_cmd(c)
101 return read_pipe(real_cmd, ignore_error)
Han-Wen Nienhuysb016d392007-05-23 17:10:46 -0300102
Han-Wen Nienhuysbce4c5f2007-05-23 17:14:33 -0300103def read_pipe_lines(c):
Han-Wen Nienhuys4addad22007-05-23 18:49:35 -0300104 if verbose:
Luke Diamand6de040d2011-10-16 10:47:52 -0400105 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 Nienhuysb016d392007-05-23 17:10:46 -0300110 val = pipe.readlines()
Luke Diamand6de040d2011-10-16 10:47:52 -0400111 if pipe.close() or p.wait():
112 die('Command failed: %s' % str(c))
Han-Wen Nienhuysb016d392007-05-23 17:10:46 -0300113
114 return val
Simon Hausmanncaace112007-05-15 14:57:57 +0200115
Anand Kumria23181212008-08-10 19:26:24 +0100116def p4_read_pipe_lines(c):
117 """Specifically invoke p4 on the command supplied. """
Anand Kumria155af832008-08-10 19:26:30 +0100118 real_cmd = p4_build_cmd(c)
Anand Kumria23181212008-08-10 19:26:24 +0100119 return read_pipe_lines(real_cmd)
120
Han-Wen Nienhuys6754a292007-05-23 17:41:50 -0300121def system(cmd):
Luke Diamand6de040d2011-10-16 10:47:52 -0400122 expand = isinstance(cmd,basestring)
Han-Wen Nienhuys4addad22007-05-23 18:49:35 -0300123 if verbose:
Luke Diamand6de040d2011-10-16 10:47:52 -0400124 sys.stderr.write("executing %s\n" % str(cmd))
125 subprocess.check_call(cmd, shell=expand)
Han-Wen Nienhuys6754a292007-05-23 17:41:50 -0300126
Anand Kumriabf9320f2008-08-10 19:26:26 +0100127def p4_system(cmd):
128 """Specifically invoke p4 as the system command. """
Anand Kumria155af832008-08-10 19:26:30 +0100129 real_cmd = p4_build_cmd(cmd)
Luke Diamand6de040d2011-10-16 10:47:52 -0400130 expand = isinstance(real_cmd, basestring)
131 subprocess.check_call(real_cmd, shell=expand)
132
133def p4_integrate(src, dest):
134 p4_system(["integrate", "-Dt", src, dest])
135
136def p4_sync(path):
137 p4_system(["sync", path])
138
139def p4_add(f):
140 p4_system(["add", f])
141
142def p4_delete(f):
143 p4_system(["delete", f])
144
145def p4_edit(f):
146 p4_system(["edit", f])
147
148def p4_revert(f):
149 p4_system(["revert", f])
150
151def p4_reopen(type, file):
152 p4_system(["reopen", "-t", type, file])
Anand Kumriabf9320f2008-08-10 19:26:26 +0100153
Pete Wyckoff9cffb8c2011-10-16 10:45:01 -0400154#
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#
159def split_p4_type(p4type):
David Brownb9fc6ea2007-09-19 13:12:48 -0700160
Pete Wyckoff9cffb8c2011-10-16 10:45:01 -0400161 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 Brownb9fc6ea2007-09-19 13:12:48 -0700189
Chris Pettittc65b6702007-11-01 20:43:14 -0700190def 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 Diamand6de040d2011-10-16 10:47:52 -0400203 p4_reopen(p4Type, file)
Chris Pettittc65b6702007-11-01 20:43:14 -0700204
205def getP4OpenedType(file):
206 # Returns the perforce file type for the given file.
207
Luke Diamand6de040d2011-10-16 10:47:52 -0400208 result = p4_read_pipe(["opened", file])
Marius Storm-Olsenf3e5ae42008-03-28 15:40:40 +0100209 match = re.match(".*\((.+)\)\r?$", result)
Chris Pettittc65b6702007-11-01 20:43:14 -0700210 if match:
211 return match.group(1)
212 else:
Marius Storm-Olsenf3e5ae42008-03-28 15:40:40 +0100213 die("Could not determine file type for %s (result: '%s')" % (file, result))
Chris Pettittc65b6702007-11-01 20:43:14 -0700214
Chris Pettittb43b0a32007-11-01 20:43:13 -0700215def 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
222def 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 Pettittc65b6702007-11-01 20:43:14 -0700255def isModeExec(mode):
256 # Returns True if the given git mode represents an executable file,
257 # otherwise False.
258 return mode[-3:] == "755"
259
260def isModeExecChanged(src_mode, dst_mode):
261 return isModeExec(src_mode) != isModeExec(dst_mode)
262
Luke Diamandb9327052009-07-30 00:13:46 +0100263def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None):
Luke Diamand6de040d2011-10-16 10:47:52 -0400264
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 Nienhuys6a49f8e2007-05-23 18:49:35 -0300273 if verbose:
Luke Diamand6de040d2011-10-16 10:47:52 -0400274 sys.stderr.write("Opening pipe: %s\n" % str(cmd))
Scott Lamb9f90c732007-07-15 20:58:10 -0700275
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 Diamand6de040d2011-10-16 10:47:52 -0400282 if isinstance(stdin,basestring):
283 stdin_file.write(stdin)
284 else:
285 for i in stdin:
286 stdin_file.write(i + '\n')
Scott Lamb9f90c732007-07-15 20:58:10 -0700287 stdin_file.flush()
288 stdin_file.seek(0)
289
Luke Diamand6de040d2011-10-16 10:47:52 -0400290 p4 = subprocess.Popen(cmd,
291 shell=expand,
Scott Lamb9f90c732007-07-15 20:58:10 -0700292 stdin=stdin_file,
293 stdout=subprocess.PIPE)
Simon Hausmann86949ee2007-03-19 20:59:12 +0100294
295 result = []
296 try:
297 while True:
Scott Lamb9f90c732007-07-15 20:58:10 -0700298 entry = marshal.load(p4.stdout)
Andrew Garberc3f61632011-04-07 02:01:21 -0400299 if cb is not None:
300 cb(entry)
301 else:
302 result.append(entry)
Simon Hausmann86949ee2007-03-19 20:59:12 +0100303 except EOFError:
304 pass
Scott Lamb9f90c732007-07-15 20:58:10 -0700305 exitCode = p4.wait()
306 if exitCode != 0:
Simon Hausmannac3e0d72007-05-23 23:32:32 +0200307 entry = {}
308 entry["p4ExitCode"] = exitCode
309 result.append(entry)
Simon Hausmann86949ee2007-03-19 20:59:12 +0100310
311 return result
312
313def p4Cmd(cmd):
314 list = p4CmdList(cmd)
315 result = {}
316 for entry in list:
317 result.update(entry)
318 return result;
319
Simon Hausmanncb2c9db2007-03-24 09:15:11 +0100320def p4Where(depotPath):
321 if not depotPath.endswith("/"):
322 depotPath += "/"
Tor Arvid Lund7f705dc2008-12-04 14:37:33 +0100323 depotPath = depotPath + "..."
Luke Diamand6de040d2011-10-16 10:47:52 -0400324 outputList = p4CmdList(["where", depotPath])
Tor Arvid Lund7f705dc2008-12-04 14:37:33 +0100325 output = None
326 for entry in outputList:
Tor Arvid Lund75bc9572008-12-09 16:41:50 +0100327 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 Lund7f705dc2008-12-04 14:37:33 +0100337 if output == None:
338 return ""
Simon Hausmanndc524032007-05-21 09:34:56 +0200339 if output["code"] == "error":
340 return ""
Simon Hausmanncb2c9db2007-03-24 09:15:11 +0100341 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 Hausmann86949ee2007-03-19 20:59:12 +0100353def currentGitBranch():
Han-Wen Nienhuysb25b2062007-05-23 18:49:35 -0300354 return read_pipe("git name-rev HEAD").split(" ")[1].strip()
Simon Hausmann86949ee2007-03-19 20:59:12 +0100355
Simon Hausmann4f5cf762007-03-19 22:25:17 +0100356def isValidGitDir(path):
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -0300357 if (os.path.exists(path + "/HEAD")
358 and os.path.exists(path + "/refs") and os.path.exists(path + "/objects")):
Simon Hausmann4f5cf762007-03-19 22:25:17 +0100359 return True;
360 return False
361
Simon Hausmann463e8af2007-05-17 09:13:54 +0200362def parseRevision(ref):
Han-Wen Nienhuysb25b2062007-05-23 18:49:35 -0300363 return read_pipe("git rev-parse %s" % ref).strip()
Simon Hausmann463e8af2007-05-17 09:13:54 +0200364
Pete Wyckoff28755db2011-12-24 21:07:40 -0500365def branchExists(ref):
366 rev = read_pipe(["git", "rev-parse", "-q", "--verify", ref],
367 ignore_error=True)
368 return len(rev) > 0
369
Simon Hausmann6ae8de82007-03-22 21:10:25 +0100370def extractLogMessageFromGitCommit(commit):
371 logMessage = ""
Han-Wen Nienhuysb016d392007-05-23 17:10:46 -0300372
373 ## fixme: title is first line of commit, not 1st paragraph.
Simon Hausmann6ae8de82007-03-22 21:10:25 +0100374 foundTitle = False
Han-Wen Nienhuysb016d392007-05-23 17:10:46 -0300375 for log in read_pipe_lines("git cat-file commit %s" % commit):
Simon Hausmann6ae8de82007-03-22 21:10:25 +0100376 if not foundTitle:
377 if len(log) == 1:
Simon Hausmann1c094182007-05-01 23:15:48 +0200378 foundTitle = True
Simon Hausmann6ae8de82007-03-22 21:10:25 +0100379 continue
380
381 logMessage += log
382 return logMessage
383
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -0300384def extractSettingsGitLog(log):
Simon Hausmann6ae8de82007-03-22 21:10:25 +0100385 values = {}
386 for line in log.split("\n"):
387 line = line.strip()
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -0300388 m = re.search (r"^ *\[git-p4: (.*)\]$", line)
389 if not m:
390 continue
Simon Hausmann6ae8de82007-03-22 21:10:25 +0100391
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -0300392 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 Hausmann845b42c2007-06-07 09:19:34 +0200402 paths = values.get("depot-paths")
403 if not paths:
404 paths = values.get("depot-path")
Simon Hausmanna3fdd572007-06-07 22:54:32 +0200405 if paths:
406 values['depot-paths'] = paths.split(',')
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -0300407 return values
Simon Hausmann6ae8de82007-03-22 21:10:25 +0100408
Simon Hausmann8136a632007-03-22 21:27:14 +0100409def gitBranchExists(branch):
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -0300410 proc = subprocess.Popen(["git", "rev-parse", branch],
411 stderr=subprocess.PIPE, stdout=subprocess.PIPE);
Simon Hausmanncaace112007-05-15 14:57:57 +0200412 return proc.wait() == 0;
Simon Hausmann8136a632007-03-22 21:27:14 +0100413
John Chapman36bd8442008-11-08 14:22:49 +1100414_gitConfig = {}
Tor Arvid Lund99f790f2011-03-15 13:08:01 +0100415def gitConfig(key, args = None): # set args to "--bool", for instance
John Chapman36bd8442008-11-08 14:22:49 +1100416 if not _gitConfig.has_key(key):
Tor Arvid Lund99f790f2011-03-15 13:08:01 +0100417 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 Chapman36bd8442008-11-08 14:22:49 +1100422 return _gitConfig[key]
Simon Hausmann01265102007-05-25 10:36:10 +0200423
Vitor Antunes7199cf12011-08-19 00:44:05 +0100424def 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 Hausmann062410b2007-07-18 10:56:31 +0200429def 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 Hausmann9ceab362007-06-22 00:01:57 +0200452def findUpstreamBranchPoint(head = "HEAD"):
Simon Hausmann86506fe2007-07-18 12:40:12 +0200453 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 Hausmann27d2d812007-06-12 14:31:59 +0200464 settings = None
Simon Hausmann27d2d812007-06-12 14:31:59 +0200465 parent = 0
466 while parent < 65535:
Simon Hausmann9ceab362007-06-22 00:01:57 +0200467 commit = head + "~%s" % parent
Simon Hausmann27d2d812007-06-12 14:31:59 +0200468 log = extractLogMessageFromGitCommit(commit)
469 settings = extractSettingsGitLog(log)
Simon Hausmann86506fe2007-07-18 12:40:12 +0200470 if settings.has_key("depot-paths"):
471 paths = ",".join(settings["depot-paths"])
472 if branchByDepotPath.has_key(paths):
473 return [branchByDepotPath[paths], settings]
Simon Hausmann27d2d812007-06-12 14:31:59 +0200474
Simon Hausmann86506fe2007-07-18 12:40:12 +0200475 parent = parent + 1
Simon Hausmann27d2d812007-06-12 14:31:59 +0200476
Simon Hausmann86506fe2007-07-18 12:40:12 +0200477 return ["", settings]
Simon Hausmann27d2d812007-06-12 14:31:59 +0200478
Simon Hausmann5ca44612007-08-24 17:44:16 +0200479def 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
526def originP4BranchesExist():
527 return gitBranchExists("origin") or gitBranchExists("origin/p4") or gitBranchExists("origin/p4/master")
528
Simon Hausmann4f6432d2007-08-26 15:56:36 +0200529def p4ChangesForPaths(depotPaths, changeRange):
530 assert depotPaths
Luke Diamand6de040d2011-10-16 10:47:52 -0400531 cmd = ['changes']
532 for p in depotPaths:
533 cmd += ["%s...%s" % (p, changeRange)]
534 output = p4_read_pipe_lines(cmd)
Simon Hausmann4f6432d2007-08-26 15:56:36 +0200535
Pete Wyckoffb4b0ba02009-02-18 13:12:14 -0500536 changes = {}
Simon Hausmann4f6432d2007-08-26 15:56:36 +0200537 for line in output:
Andrew Garberc3f61632011-04-07 02:01:21 -0400538 changeNum = int(line.split(" ")[1])
539 changes[changeNum] = True
Simon Hausmann4f6432d2007-08-26 15:56:36 +0200540
Pete Wyckoffb4b0ba02009-02-18 13:12:14 -0500541 changelist = changes.keys()
542 changelist.sort()
543 return changelist
Simon Hausmann4f6432d2007-08-26 15:56:36 +0200544
Tor Arvid Lundd53de8b2011-03-15 13:08:02 +0100545def 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 Hausmannb9847332007-03-20 20:54:23 +0100558class Command:
559 def __init__(self):
560 self.usage = "usage: %prog [options]"
Simon Hausmann8910ac02007-03-26 08:18:55 +0200561 self.needsGit = True
Simon Hausmannb9847332007-03-20 20:54:23 +0100562
Luke Diamand3ea2cfd2011-04-21 20:50:23 +0100563class 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 Hausmannb9847332007-03-20 20:54:23 +0100604class P4Debug(Command):
Simon Hausmann86949ee2007-03-19 20:59:12 +0100605 def __init__(self):
Simon Hausmann6ae8de82007-03-22 21:10:25 +0100606 Command.__init__(self)
Simon Hausmann86949ee2007-03-19 20:59:12 +0100607 self.options = [
Han-Wen Nienhuysb1ce9442007-05-23 18:49:35 -0300608 optparse.make_option("--verbose", dest="verbose", action="store_true",
609 default=False),
Han-Wen Nienhuys4addad22007-05-23 18:49:35 -0300610 ]
Simon Hausmannc8c39112007-03-19 21:02:30 +0100611 self.description = "A tool to debug the output of p4 -G."
Simon Hausmann8910ac02007-03-26 08:18:55 +0200612 self.needsGit = False
Han-Wen Nienhuysb1ce9442007-05-23 18:49:35 -0300613 self.verbose = False
Simon Hausmann86949ee2007-03-19 20:59:12 +0100614
615 def run(self, args):
Han-Wen Nienhuysb1ce9442007-05-23 18:49:35 -0300616 j = 0
Luke Diamand6de040d2011-10-16 10:47:52 -0400617 for output in p4CmdList(args):
Han-Wen Nienhuysb1ce9442007-05-23 18:49:35 -0300618 print 'Element: %d' % j
619 j += 1
Simon Hausmann86949ee2007-03-19 20:59:12 +0100620 print output
Simon Hausmannb9847332007-03-20 20:54:23 +0100621 return True
Simon Hausmann86949ee2007-03-19 20:59:12 +0100622
Simon Hausmann58346842007-05-21 22:57:06 +0200623class P4RollBack(Command):
624 def __init__(self):
625 Command.__init__(self)
626 self.options = [
Simon Hausmann0c66a782007-05-23 20:07:57 +0200627 optparse.make_option("--verbose", dest="verbose", action="store_true"),
628 optparse.make_option("--local", dest="rollbackLocalBranches", action="store_true")
Simon Hausmann58346842007-05-21 22:57:06 +0200629 ]
630 self.description = "A tool to debug the multi-branch import. Don't use :)"
Simon Hausmann52102d42007-05-21 23:44:24 +0200631 self.verbose = False
Simon Hausmann0c66a782007-05-23 20:07:57 +0200632 self.rollbackLocalBranches = False
Simon Hausmann58346842007-05-21 22:57:06 +0200633
634 def run(self, args):
635 if len(args) != 1:
636 return False
637 maxChange = int(args[0])
Simon Hausmann0c66a782007-05-23 20:07:57 +0200638
Simon Hausmannad192f22007-05-23 23:44:19 +0200639 if "p4ExitCode" in p4Cmd("changes -m 1"):
Simon Hausmann66a2f522007-05-23 23:40:48 +0200640 die("Problems executing p4");
641
Simon Hausmann0c66a782007-05-23 20:07:57 +0200642 if self.rollbackLocalBranches:
643 refPrefix = "refs/heads/"
Han-Wen Nienhuysb016d392007-05-23 17:10:46 -0300644 lines = read_pipe_lines("git rev-parse --symbolic --branches")
Simon Hausmann0c66a782007-05-23 20:07:57 +0200645 else:
646 refPrefix = "refs/remotes/"
Han-Wen Nienhuysb016d392007-05-23 17:10:46 -0300647 lines = read_pipe_lines("git rev-parse --symbolic --remotes")
Simon Hausmann0c66a782007-05-23 20:07:57 +0200648
649 for line in lines:
650 if self.rollbackLocalBranches or (line.startswith("p4/") and line != "p4/HEAD\n"):
Han-Wen Nienhuysb25b2062007-05-23 18:49:35 -0300651 line = line.strip()
652 ref = refPrefix + line
Simon Hausmann58346842007-05-21 22:57:06 +0200653 log = extractLogMessageFromGitCommit(ref)
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -0300654 settings = extractSettingsGitLog(log)
655
656 depotPaths = settings['depot-paths']
657 change = settings['change']
658
Simon Hausmann58346842007-05-21 22:57:06 +0200659 changed = False
Simon Hausmann52102d42007-05-21 23:44:24 +0200660
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -0300661 if len(p4Cmd("changes -m 1 " + ' '.join (['%s...@%s' % (p, maxChange)
662 for p in depotPaths]))) == 0:
Simon Hausmann52102d42007-05-21 23:44:24 +0200663 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 Nienhuysbb6e09b2007-05-23 18:49:35 -0300667 while change and int(change) > maxChange:
Simon Hausmann58346842007-05-21 22:57:06 +0200668 changed = True
Simon Hausmann52102d42007-05-21 23:44:24 +0200669 if self.verbose:
670 print "%s is at %s ; rewinding towards %s" % (ref, change, maxChange)
Simon Hausmann58346842007-05-21 22:57:06 +0200671 system("git update-ref %s \"%s^\"" % (ref, ref))
672 log = extractLogMessageFromGitCommit(ref)
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -0300673 settings = extractSettingsGitLog(log)
674
675
676 depotPaths = settings['depot-paths']
677 change = settings['change']
Simon Hausmann58346842007-05-21 22:57:06 +0200678
679 if changed:
Simon Hausmann52102d42007-05-21 23:44:24 +0200680 print "%s rewound to %s" % (ref, change)
Simon Hausmann58346842007-05-21 22:57:06 +0200681
682 return True
683
Luke Diamand3ea2cfd2011-04-21 20:50:23 +0100684class P4Submit(Command, P4UserMap):
Simon Hausmann4f5cf762007-03-19 22:25:17 +0100685 def __init__(self):
Simon Hausmannb9847332007-03-20 20:54:23 +0100686 Command.__init__(self)
Luke Diamand3ea2cfd2011-04-21 20:50:23 +0100687 P4UserMap.__init__(self)
Simon Hausmann4f5cf762007-03-19 22:25:17 +0100688 self.options = [
Han-Wen Nienhuys4addad22007-05-23 18:49:35 -0300689 optparse.make_option("--verbose", dest="verbose", action="store_true"),
Simon Hausmann4f5cf762007-03-19 22:25:17 +0100690 optparse.make_option("--origin", dest="origin"),
Vitor Antunesae901092011-02-20 01:18:24 +0000691 optparse.make_option("-M", dest="detectRenames", action="store_true"),
Luke Diamand3ea2cfd2011-04-21 20:50:23 +0100692 # preserve the user, requires relevant p4 permissions
693 optparse.make_option("--preserve-user", dest="preserveUser", action="store_true"),
Simon Hausmann4f5cf762007-03-19 22:25:17 +0100694 ]
695 self.description = "Submit changes from git to the perforce depot."
Simon Hausmannc9b50e62007-03-29 19:15:24 +0200696 self.usage += " [name of git branch to submit into perforce depot]"
Simon Hausmann4f5cf762007-03-19 22:25:17 +0100697 self.interactive = True
Simon Hausmann95124972007-03-23 09:16:07 +0100698 self.origin = ""
Vitor Antunesae901092011-02-20 01:18:24 +0000699 self.detectRenames = False
Simon Hausmannb0d10df2007-06-07 13:09:14 +0200700 self.verbose = False
Luke Diamand3ea2cfd2011-04-21 20:50:23 +0100701 self.preserveUser = gitConfig("git-p4.preserveUser").lower() == "true"
Marius Storm-Olsenf7baba82007-06-07 14:07:01 +0200702 self.isWindows = (platform.system() == "Windows")
Luke Diamand848de9c2011-05-13 20:46:00 +0100703 self.myP4UserId = None
Simon Hausmann4f5cf762007-03-19 22:25:17 +0100704
Simon Hausmann4f5cf762007-03-19 22:25:17 +0100705 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 Hausmannedae1e22008-02-19 09:29:06 +0100709 # replaces everything between 'Description:' and the next P4 submit template field with the
710 # commit message
Simon Hausmann4f5cf762007-03-19 22:25:17 +0100711 def prepareLogMessage(self, template, message):
712 result = ""
713
Simon Hausmannedae1e22008-02-19 09:29:06 +0100714 inDescriptionSection = False
715
Simon Hausmann4f5cf762007-03-19 22:25:17 +0100716 for line in template.split("\n"):
717 if line.startswith("#"):
718 result += line + "\n"
719 continue
720
Simon Hausmannedae1e22008-02-19 09:29:06 +0100721 if inDescriptionSection:
Michael Horowitzc9dbab02011-02-25 21:31:13 -0500722 if line.startswith("Files:") or line.startswith("Jobs:"):
Simon Hausmannedae1e22008-02-19 09:29:06 +0100723 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 Hausmann4f5cf762007-03-19 22:25:17 +0100732
Simon Hausmannedae1e22008-02-19 09:29:06 +0100733 result += line + "\n"
Simon Hausmann4f5cf762007-03-19 22:25:17 +0100734
735 return result
736
Luke Diamand3ea2cfd2011-04-21 20:50:23 +0100737 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 Diamand6de040d2011-10-16 10:47:52 -0400771 results = p4CmdList(["changes", "-c", client, "-m", "1"])
Luke Diamand3ea2cfd2011-04-21 20:50:23 +0100772 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 Diamandecdba362011-05-07 11:19:43 +0100780 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 Diamand3ea2cfd2011-04-21 20:50:23 +0100789 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 Diamandecdba362011-05-07 11:19:43 +0100802 results = p4CmdList("protects %s" % self.depotPath)
Luke Diamand3ea2cfd2011-04-21 20:50:23 +0100803 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 Diamand848de9c2011-05-13 20:46:00 +0100811 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 Hausmannea99c3a2007-08-08 17:06:55 +0200830 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 Diamand6de040d2011-10-16 10:47:52 -0400834 for line in p4_read_pipe_lines(['change', '-o']):
Marius Storm-Olsenf3e5ae42008-03-28 15:40:40 +0100835 if line.endswith("\r\n"):
836 line = line[:-2] + "\n"
Simon Hausmannea99c3a2007-08-08 17:06:55 +0200837 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 Lundd53de8b2011-03-15 13:08:02 +0100844 if not p4PathStartsWith(path, self.depotPath):
Simon Hausmannea99c3a2007-08-08 17:06:55 +0200845 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 Wyckoff7c766e52011-12-04 19:22:45 -0500856 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 Wyckoffd1652042011-12-17 12:39:03 -0500880 # 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 Wyckoff7c766e52011-12-04 19:22:45 -0500890
Han-Wen Nienhuys7cb5cbe2007-05-23 16:55:48 -0300891 def applyCommit(self, id):
Simon Hausmann0e36f2d2008-02-19 09:33:08 +0100892 print "Applying %s" % (read_pipe("git log --max-count=1 --pretty=oneline %s" % id))
Vitor Antunesae901092011-02-20 01:18:24 +0000893
Luke Diamand848de9c2011-05-13 20:46:00 +0100894 (p4User, gitEmail) = self.p4UserForCommit(id)
Luke Diamand3ea2cfd2011-04-21 20:50:23 +0100895
Vitor Antunesae901092011-02-20 01:18:24 +0000896 if not self.detectRenames:
897 # If not explicitly set check the config variable
Vitor Antunes0a9feff2011-08-22 09:33:05 +0100898 self.detectRenames = gitConfig("git-p4.detectRenames")
Vitor Antunesae901092011-02-20 01:18:24 +0000899
Vitor Antunes0a9feff2011-08-22 09:33:05 +0100900 if self.detectRenames.lower() == "false" or self.detectRenames == "":
901 diffOpts = ""
902 elif self.detectRenames.lower() == "true":
Vitor Antunesae901092011-02-20 01:18:24 +0000903 diffOpts = "-M"
904 else:
Vitor Antunes0a9feff2011-08-22 09:33:05 +0100905 diffOpts = "-M%s" % self.detectRenames
Vitor Antunesae901092011-02-20 01:18:24 +0000906
Vitor Antunes0a9feff2011-08-22 09:33:05 +0100907 detectCopies = gitConfig("git-p4.detectCopies")
908 if detectCopies.lower() == "true":
Vitor Antunes4fddb412011-02-20 01:18:25 +0000909 diffOpts += " -C"
Vitor Antunes0a9feff2011-08-22 09:33:05 +0100910 elif detectCopies != "" and detectCopies.lower() != "false":
911 diffOpts += " -C%s" % detectCopies
Vitor Antunes4fddb412011-02-20 01:18:25 +0000912
Vitor Antunes68cbcf12011-08-22 09:33:09 +0100913 if gitConfig("git-p4.detectCopiesHarder", "--bool") == "true":
Vitor Antunes4fddb412011-02-20 01:18:25 +0000914 diffOpts += " --find-copies-harder"
915
Simon Hausmann0e36f2d2008-02-19 09:33:08 +0100916 diff = read_pipe_lines("git diff-tree -r %s \"%s^\" \"%s\"" % (diffOpts, id, id))
Simon Hausmann4f5cf762007-03-19 22:25:17 +0100917 filesToAdd = set()
918 filesToDelete = set()
Simon Hausmannd336c152007-05-16 09:41:26 +0200919 editedFiles = set()
Chris Pettittc65b6702007-11-01 20:43:14 -0700920 filesToChangeExecBit = {}
Simon Hausmann4f5cf762007-03-19 22:25:17 +0100921 for line in diff:
Chris Pettittb43b0a32007-11-01 20:43:13 -0700922 diff = parseDiffTreeEntry(line)
923 modifier = diff['status']
924 path = diff['src']
Simon Hausmann4f5cf762007-03-19 22:25:17 +0100925 if modifier == "M":
Luke Diamand6de040d2011-10-16 10:47:52 -0400926 p4_edit(path)
Chris Pettittc65b6702007-11-01 20:43:14 -0700927 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
928 filesToChangeExecBit[path] = diff['dst_mode']
Simon Hausmannd336c152007-05-16 09:41:26 +0200929 editedFiles.add(path)
Simon Hausmann4f5cf762007-03-19 22:25:17 +0100930 elif modifier == "A":
931 filesToAdd.add(path)
Chris Pettittc65b6702007-11-01 20:43:14 -0700932 filesToChangeExecBit[path] = diff['dst_mode']
Simon Hausmann4f5cf762007-03-19 22:25:17 +0100933 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 Antunes4fddb412011-02-20 01:18:25 +0000939 elif modifier == "C":
940 src, dest = diff['src'], diff['dst']
Luke Diamand6de040d2011-10-16 10:47:52 -0400941 p4_integrate(src, dest)
Vitor Antunes4fddb412011-02-20 01:18:25 +0000942 if diff['src_sha1'] != diff['dst_sha1']:
Luke Diamand6de040d2011-10-16 10:47:52 -0400943 p4_edit(dest)
Vitor Antunes4fddb412011-02-20 01:18:25 +0000944 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
Luke Diamand6de040d2011-10-16 10:47:52 -0400945 p4_edit(dest)
Vitor Antunes4fddb412011-02-20 01:18:25 +0000946 filesToChangeExecBit[dest] = diff['dst_mode']
947 os.unlink(dest)
948 editedFiles.add(dest)
Chris Pettittd9a5f252007-10-15 22:15:06 -0700949 elif modifier == "R":
Chris Pettittb43b0a32007-11-01 20:43:13 -0700950 src, dest = diff['src'], diff['dst']
Luke Diamand6de040d2011-10-16 10:47:52 -0400951 p4_integrate(src, dest)
Vitor Antunesae901092011-02-20 01:18:24 +0000952 if diff['src_sha1'] != diff['dst_sha1']:
Luke Diamand6de040d2011-10-16 10:47:52 -0400953 p4_edit(dest)
Chris Pettittc65b6702007-11-01 20:43:14 -0700954 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
Luke Diamand6de040d2011-10-16 10:47:52 -0400955 p4_edit(dest)
Chris Pettittc65b6702007-11-01 20:43:14 -0700956 filesToChangeExecBit[dest] = diff['dst_mode']
Chris Pettittd9a5f252007-10-15 22:15:06 -0700957 os.unlink(dest)
958 editedFiles.add(dest)
959 filesToDelete.add(src)
Simon Hausmann4f5cf762007-03-19 22:25:17 +0100960 else:
961 die("unknown modifier %s for %s" % (modifier, path))
962
Simon Hausmann0e36f2d2008-02-19 09:33:08 +0100963 diffcmd = "git format-patch -k --stdout \"%s^\"..\"%s\"" % (id, id)
Simon Hausmann47a130b2007-05-20 16:33:21 +0200964 patchcmd = diffcmd + " | git apply "
Simon Hausmannc1b296b2007-05-20 16:55:05 +0200965 tryPatchCmd = patchcmd + "--check -"
966 applyPatchCmd = patchcmd + "--check --apply -"
Simon Hausmann51a26402007-04-15 09:59:56 +0200967
Simon Hausmann47a130b2007-05-20 16:33:21 +0200968 if os.system(tryPatchCmd) != 0:
Simon Hausmann51a26402007-04-15 09:59:56 +0200969 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 Nienhuyscebdf5a2007-05-23 16:53:11 -0300973 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 Hausmann51a26402007-04-15 09:59:56 +0200975 if response == "s":
976 print "Skipping! Good luck with the next patches..."
Simon Hausmann20947142007-09-13 22:10:18 +0200977 for f in editedFiles:
Luke Diamand6de040d2011-10-16 10:47:52 -0400978 p4_revert(f)
Simon Hausmann20947142007-09-13 22:10:18 +0200979 for f in filesToAdd:
Luke Diamand6de040d2011-10-16 10:47:52 -0400980 os.remove(f)
Simon Hausmann51a26402007-04-15 09:59:56 +0200981 return
982 elif response == "a":
Simon Hausmann47a130b2007-05-20 16:33:21 +0200983 os.system(applyPatchCmd)
Simon Hausmann51a26402007-04-15 09:59:56 +0200984 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 Nienhuyscebdf5a2007-05-23 16:53:11 -0300990 die("Please resolve and submit the conflict manually and "
991 + "continue afterwards with git-p4 submit --continue")
Simon Hausmann51a26402007-04-15 09:59:56 +0200992 elif response == "w":
993 system(diffcmd + " > patch.txt")
994 print "Patch saved to patch.txt in %s !" % self.clientPath
Han-Wen Nienhuyscebdf5a2007-05-23 16:53:11 -0300995 die("Please resolve and submit the conflict manually and "
996 "continue afterwards with git-p4 submit --continue")
Simon Hausmann51a26402007-04-15 09:59:56 +0200997
Simon Hausmann47a130b2007-05-20 16:33:21 +0200998 system(applyPatchCmd)
Simon Hausmann4f5cf762007-03-19 22:25:17 +0100999
1000 for f in filesToAdd:
Luke Diamand6de040d2011-10-16 10:47:52 -04001001 p4_add(f)
Simon Hausmann4f5cf762007-03-19 22:25:17 +01001002 for f in filesToDelete:
Luke Diamand6de040d2011-10-16 10:47:52 -04001003 p4_revert(f)
1004 p4_delete(f)
Simon Hausmann4f5cf762007-03-19 22:25:17 +01001005
Chris Pettittc65b6702007-11-01 20:43:14 -07001006 # Set/clear executable bits
1007 for f in filesToChangeExecBit.keys():
1008 mode = filesToChangeExecBit[f]
1009 setP4ExecBit(f, mode)
1010
Simon Hausmann0e36f2d2008-02-19 09:33:08 +01001011 logMessage = extractLogMessageFromGitCommit(id)
Simon Hausmann0e36f2d2008-02-19 09:33:08 +01001012 logMessage = logMessage.strip()
Simon Hausmann4f5cf762007-03-19 22:25:17 +01001013
Simon Hausmannea99c3a2007-08-08 17:06:55 +02001014 template = self.prepareSubmitTemplate()
Simon Hausmann4f5cf762007-03-19 22:25:17 +01001015
1016 if self.interactive:
1017 submitTemplate = self.prepareLogMessage(template, logMessage)
Luke Diamandecdba362011-05-07 11:19:43 +01001018
1019 if self.preserveUser:
1020 submitTemplate = submitTemplate + ("\n######## Actual user %s, modified after commit\n" % p4User)
1021
Shawn Bohrer67abd412008-03-12 19:03:23 -05001022 if os.environ.has_key("P4DIFF"):
1023 del(os.environ["P4DIFF"])
Andrew Waters8b130262010-10-22 13:26:02 +01001024 diff = ""
1025 for editedFile in editedFiles:
Luke Diamand6de040d2011-10-16 10:47:52 -04001026 diff += p4_read_pipe(['diff', '-du', editedFile])
Simon Hausmann4f5cf762007-03-19 22:25:17 +01001027
Marius Storm-Olsenf3e5ae42008-03-28 15:40:40 +01001028 newdiff = ""
Simon Hausmann4f5cf762007-03-19 22:25:17 +01001029 for newFile in filesToAdd:
Marius Storm-Olsenf3e5ae42008-03-28 15:40:40 +01001030 newdiff += "==== new file ====\n"
1031 newdiff += "--- /dev/null\n"
1032 newdiff += "+++ %s\n" % newFile
Simon Hausmann4f5cf762007-03-19 22:25:17 +01001033 f = open(newFile, "r")
1034 for line in f.readlines():
Marius Storm-Olsenf3e5ae42008-03-28 15:40:40 +01001035 newdiff += "+" + line
Simon Hausmann4f5cf762007-03-19 22:25:17 +01001036 f.close()
1037
Luke Diamand848de9c2011-05-13 20:46:00 +01001038 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-Olsenf3e5ae42008-03-28 15:40:40 +01001043 separatorLine = "######## everything below this line is just the diff #######\n"
Simon Hausmann4f5cf762007-03-19 22:25:17 +01001044
Pete Wyckoff7c766e52011-12-04 19:22:45 -05001045 (handle, fileName) = tempfile.mkstemp()
Simon Hausmanne96e4002008-01-04 14:27:55 +01001046 tmpFile = os.fdopen(handle, "w+")
Marius Storm-Olsenf3e5ae42008-03-28 15:40:40 +01001047 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 Hausmanne96e4002008-01-04 14:27:55 +01001052 tmpFile.close()
Simon Hausmanncb4f1282007-05-25 22:34:30 +02001053
Pete Wyckoff7c766e52011-12-04 19:22:45 -05001054 if self.edit_template(fileName):
1055 # read the edited message and submit
Simon Hausmanncdc7e382008-08-27 09:30:29 +02001056 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 Diamand6de040d2011-10-16 10:47:52 -04001062 p4_write_pipe(['submit', '-i'], submitTemplate)
Luke Diamand3ea2cfd2011-04-21 20:50:23 +01001063
1064 if self.preserveUser:
1065 if p4User:
1066 # Get last changelist number. Cannot easily get it from
Pete Wyckoff7c766e52011-12-04 19:22:45 -05001067 # the submit command output as the output is
1068 # unmarshalled.
Luke Diamand3ea2cfd2011-04-21 20:50:23 +01001069 changelist = self.lastP4Changelist()
1070 self.modifyChangelistUser(changelist, p4User)
Simon Hausmanncdc7e382008-08-27 09:30:29 +02001071 else:
Pete Wyckoff7c766e52011-12-04 19:22:45 -05001072 # skip this patch
Pete Wyckoffd1652042011-12-17 12:39:03 -05001073 print "Submission cancelled, undoing p4 changes."
Simon Hausmanncdc7e382008-08-27 09:30:29 +02001074 for f in editedFiles:
Luke Diamand6de040d2011-10-16 10:47:52 -04001075 p4_revert(f)
Simon Hausmanncdc7e382008-08-27 09:30:29 +02001076 for f in filesToAdd:
Luke Diamand6de040d2011-10-16 10:47:52 -04001077 p4_revert(f)
1078 os.remove(f)
Simon Hausmanncdc7e382008-08-27 09:30:29 +02001079
1080 os.remove(fileName)
Simon Hausmann4f5cf762007-03-19 22:25:17 +01001081 else:
1082 fileName = "submit.txt"
1083 file = open(fileName, "w+")
1084 file.write(self.prepareLogMessage(template, logMessage))
1085 file.close()
Han-Wen Nienhuyscebdf5a2007-05-23 16:53:11 -03001086 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 Hausmann4f5cf762007-03-19 22:25:17 +01001089
1090 def run(self, args):
Simon Hausmannc9b50e62007-03-29 19:15:24 +02001091 if len(args) == 0:
1092 self.master = currentGitBranch()
Simon Hausmann4280e532007-05-25 08:49:18 +02001093 if len(self.master) == 0 or not gitBranchExists("refs/heads/%s" % self.master):
Simon Hausmannc9b50e62007-03-29 19:15:24 +02001094 die("Detecting current git branch failed!")
1095 elif len(args) == 1:
1096 self.master = args[0]
Pete Wyckoff28755db2011-12-24 21:07:40 -05001097 if not branchExists(self.master):
1098 die("Branch %s does not exist" % self.master)
Simon Hausmannc9b50e62007-03-29 19:15:24 +02001099 else:
1100 return False
1101
Jing Xue4c2d5d72008-06-22 14:12:39 -04001102 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 Hausmann27d2d812007-06-12 14:31:59 +02001106 [upstream, settings] = findUpstreamBranchPoint()
Simon Hausmannea99c3a2007-08-08 17:06:55 +02001107 self.depotPath = settings['depot-paths'][0]
Simon Hausmann27d2d812007-06-12 14:31:59 +02001108 if len(self.origin) == 0:
1109 self.origin = upstream
Simon Hausmanna3fdd572007-06-07 22:54:32 +02001110
Luke Diamand3ea2cfd2011-04-21 20:50:23 +01001111 if self.preserveUser:
1112 if not self.canChangeChangelists():
1113 die("Cannot preserve user names without p4 super-user or admin permissions")
1114
Simon Hausmanna3fdd572007-06-07 22:54:32 +02001115 if self.verbose:
1116 print "Origin branch is " + self.origin
Simon Hausmann95124972007-03-23 09:16:07 +01001117
Simon Hausmannea99c3a2007-08-08 17:06:55 +02001118 if len(self.depotPath) == 0:
Simon Hausmann95124972007-03-23 09:16:07 +01001119 print "Internal error: cannot locate perforce depot path from existing branches"
1120 sys.exit(128)
1121
Simon Hausmannea99c3a2007-08-08 17:06:55 +02001122 self.clientPath = p4Where(self.depotPath)
Simon Hausmann95124972007-03-23 09:16:07 +01001123
Simon Hausmann51a26402007-04-15 09:59:56 +02001124 if len(self.clientPath) == 0:
Simon Hausmannea99c3a2007-08-08 17:06:55 +02001125 print "Error: Cannot locate perforce checkout of %s in client view" % self.depotPath
Simon Hausmann95124972007-03-23 09:16:07 +01001126 sys.exit(128)
1127
Simon Hausmannea99c3a2007-08-08 17:06:55 +02001128 print "Perforce checkout for depot path %s located at %s" % (self.depotPath, self.clientPath)
Simon Hausmann7944f142007-05-21 11:04:26 +02001129 self.oldWorkingDirectory = os.getcwd()
Simon Hausmannc1b296b2007-05-20 16:55:05 +02001130
Gary Gibbons0591cfa2011-12-09 18:48:14 -05001131 # ensure the clientPath exists
1132 if not os.path.exists(self.clientPath):
1133 os.makedirs(self.clientPath)
1134
Robert Blum053fd0c2008-08-01 12:50:03 -07001135 chdir(self.clientPath)
Benjamin C Meyer6a012982010-03-19 00:39:10 -04001136 print "Synchronizing p4 checkout..."
Luke Diamand6de040d2011-10-16 10:47:52 -04001137 p4_sync("...")
Simon Hausmann4f5cf762007-03-19 22:25:17 +01001138 self.check()
Simon Hausmann4f5cf762007-03-19 22:25:17 +01001139
Simon Hausmann4c750c02008-02-19 09:37:16 +01001140 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 Hausmann4f5cf762007-03-19 22:25:17 +01001144
Luke Diamand848de9c2011-05-13 20:46:00 +01001145 if self.preserveUser or (gitConfig("git-p4.skipUserNameCheck") == "true"):
1146 self.checkAuthorship = False
1147 else:
1148 self.checkAuthorship = True
1149
Luke Diamand3ea2cfd2011-04-21 20:50:23 +01001150 if self.preserveUser:
1151 self.checkValidP4Users(commits)
1152
Simon Hausmann4f5cf762007-03-19 22:25:17 +01001153 while len(commits) > 0:
Simon Hausmann4f5cf762007-03-19 22:25:17 +01001154 commit = commits[0]
1155 commits = commits[1:]
Han-Wen Nienhuys7cb5cbe2007-05-23 16:55:48 -03001156 self.applyCommit(commit)
Simon Hausmann4f5cf762007-03-19 22:25:17 +01001157 if not self.interactive:
1158 break
1159
Simon Hausmann4f5cf762007-03-19 22:25:17 +01001160 if len(commits) == 0:
Simon Hausmann4c750c02008-02-19 09:37:16 +01001161 print "All changes applied!"
Robert Blum053fd0c2008-08-01 12:50:03 -07001162 chdir(self.oldWorkingDirectory)
Simon Hausmann14594f42007-08-22 09:07:15 +02001163
Simon Hausmann4c750c02008-02-19 09:37:16 +01001164 sync = P4Sync()
1165 sync.run([])
Simon Hausmann14594f42007-08-22 09:07:15 +02001166
Simon Hausmann4c750c02008-02-19 09:37:16 +01001167 rebase = P4Rebase()
1168 rebase.rebase()
Simon Hausmann4f5cf762007-03-19 22:25:17 +01001169
Simon Hausmannb9847332007-03-20 20:54:23 +01001170 return True
1171
Pete Wyckoffecb7cf92012-01-02 18:05:53 -05001172class View(object):
1173 """Represent a p4 view ("p4 help views"), and map files in a
1174 repo according to the view."""
1175
1176 class Path(object):
1177 """A depot or client path, possibly containing wildcards.
1178 The only one supported is ... at the end, currently.
1179 Initialize with the full path, with //depot or //client."""
1180
1181 def __init__(self, path, is_depot):
1182 self.path = path
1183 self.is_depot = is_depot
1184 self.find_wildcards()
1185 # remember the prefix bit, useful for relative mappings
1186 m = re.match("(//[^/]+/)", self.path)
1187 if not m:
1188 die("Path %s does not start with //prefix/" % self.path)
1189 prefix = m.group(1)
1190 if not self.is_depot:
1191 # strip //client/ on client paths
1192 self.path = self.path[len(prefix):]
1193
1194 def find_wildcards(self):
1195 """Make sure wildcards are valid, and set up internal
1196 variables."""
1197
1198 self.ends_triple_dot = False
1199 # There are three wildcards allowed in p4 views
1200 # (see "p4 help views"). This code knows how to
1201 # handle "..." (only at the end), but cannot deal with
1202 # "%%n" or "*". Only check the depot_side, as p4 should
1203 # validate that the client_side matches too.
1204 if re.search(r'%%[1-9]', self.path):
1205 die("Can't handle %%n wildcards in view: %s" % self.path)
1206 if self.path.find("*") >= 0:
1207 die("Can't handle * wildcards in view: %s" % self.path)
1208 triple_dot_index = self.path.find("...")
1209 if triple_dot_index >= 0:
1210 if not self.path.endswith("..."):
1211 die("Can handle ... wildcard only at end of path: %s" %
1212 self.path)
1213 self.ends_triple_dot = True
1214
1215 def ensure_compatible(self, other_path):
1216 """Make sure the wildcards agree."""
1217 if self.ends_triple_dot != other_path.ends_triple_dot:
1218 die("Both paths must end with ... if either does;\n" +
1219 "paths: %s %s" % (self.path, other_path.path))
1220
1221 def match_wildcards(self, test_path):
1222 """See if this test_path matches us, and fill in the value
1223 of the wildcards if so. Returns a tuple of
1224 (True|False, wildcards[]). For now, only the ... at end
1225 is supported, so at most one wildcard."""
1226 if self.ends_triple_dot:
1227 dotless = self.path[:-3]
1228 if test_path.startswith(dotless):
1229 wildcard = test_path[len(dotless):]
1230 return (True, [ wildcard ])
1231 else:
1232 if test_path == self.path:
1233 return (True, [])
1234 return (False, [])
1235
1236 def match(self, test_path):
1237 """Just return if it matches; don't bother with the wildcards."""
1238 b, _ = self.match_wildcards(test_path)
1239 return b
1240
1241 def fill_in_wildcards(self, wildcards):
1242 """Return the relative path, with the wildcards filled in
1243 if there are any."""
1244 if self.ends_triple_dot:
1245 return self.path[:-3] + wildcards[0]
1246 else:
1247 return self.path
1248
1249 class Mapping(object):
1250 def __init__(self, depot_side, client_side, overlay, exclude):
1251 # depot_side is without the trailing /... if it had one
1252 self.depot_side = View.Path(depot_side, is_depot=True)
1253 self.client_side = View.Path(client_side, is_depot=False)
1254 self.overlay = overlay # started with "+"
1255 self.exclude = exclude # started with "-"
1256 assert not (self.overlay and self.exclude)
1257 self.depot_side.ensure_compatible(self.client_side)
1258
1259 def __str__(self):
1260 c = " "
1261 if self.overlay:
1262 c = "+"
1263 if self.exclude:
1264 c = "-"
1265 return "View.Mapping: %s%s -> %s" % \
1266 (c, self.depot_side, self.client_side)
1267
1268 def map_depot_to_client(self, depot_path):
1269 """Calculate the client path if using this mapping on the
1270 given depot path; does not consider the effect of other
1271 mappings in a view. Even excluded mappings are returned."""
1272 matches, wildcards = self.depot_side.match_wildcards(depot_path)
1273 if not matches:
1274 return ""
1275 client_path = self.client_side.fill_in_wildcards(wildcards)
1276 return client_path
1277
1278 #
1279 # View methods
1280 #
1281 def __init__(self):
1282 self.mappings = []
1283
1284 def append(self, view_line):
1285 """Parse a view line, splitting it into depot and client
1286 sides. Append to self.mappings, preserving order."""
1287
1288 # Split the view line into exactly two words. P4 enforces
1289 # structure on these lines that simplifies this quite a bit.
1290 #
1291 # Either or both words may be double-quoted.
1292 # Single quotes do not matter.
1293 # Double-quote marks cannot occur inside the words.
1294 # A + or - prefix is also inside the quotes.
1295 # There are no quotes unless they contain a space.
1296 # The line is already white-space stripped.
1297 # The two words are separated by a single space.
1298 #
1299 if view_line[0] == '"':
1300 # First word is double quoted. Find its end.
1301 close_quote_index = view_line.find('"', 1)
1302 if close_quote_index <= 0:
1303 die("No first-word closing quote found: %s" % view_line)
1304 depot_side = view_line[1:close_quote_index]
1305 # skip closing quote and space
1306 rhs_index = close_quote_index + 1 + 1
1307 else:
1308 space_index = view_line.find(" ")
1309 if space_index <= 0:
1310 die("No word-splitting space found: %s" % view_line)
1311 depot_side = view_line[0:space_index]
1312 rhs_index = space_index + 1
1313
1314 if view_line[rhs_index] == '"':
1315 # Second word is double quoted. Make sure there is a
1316 # double quote at the end too.
1317 if not view_line.endswith('"'):
1318 die("View line with rhs quote should end with one: %s" %
1319 view_line)
1320 # skip the quotes
1321 client_side = view_line[rhs_index+1:-1]
1322 else:
1323 client_side = view_line[rhs_index:]
1324
1325 # prefix + means overlay on previous mapping
1326 overlay = False
1327 if depot_side.startswith("+"):
1328 overlay = True
1329 depot_side = depot_side[1:]
1330
1331 # prefix - means exclude this path
1332 exclude = False
1333 if depot_side.startswith("-"):
1334 exclude = True
1335 depot_side = depot_side[1:]
1336
1337 m = View.Mapping(depot_side, client_side, overlay, exclude)
1338 self.mappings.append(m)
1339
1340 def map_in_client(self, depot_path):
1341 """Return the relative location in the client where this
1342 depot file should live. Returns "" if the file should
1343 not be mapped in the client."""
1344
1345 paths_filled = []
1346 client_path = ""
1347
1348 # look at later entries first
1349 for m in self.mappings[::-1]:
1350
1351 # see where will this path end up in the client
1352 p = m.map_depot_to_client(depot_path)
1353
1354 if p == "":
1355 # Depot path does not belong in client. Must remember
1356 # this, as previous items should not cause files to
1357 # exist in this path either. Remember that the list is
1358 # being walked from the end, which has higher precedence.
1359 # Overlap mappings do not exclude previous mappings.
1360 if not m.overlay:
1361 paths_filled.append(m.client_side)
1362
1363 else:
1364 # This mapping matched; no need to search any further.
1365 # But, the mapping could be rejected if the client path
1366 # has already been claimed by an earlier mapping.
1367 already_mapped_in_client = False
1368 for f in paths_filled:
1369 # this is View.Path.match
1370 if f.match(p):
1371 already_mapped_in_client = True
1372 break
1373 if not already_mapped_in_client:
1374 # Include this file, unless it is from a line that
1375 # explicitly said to exclude it.
1376 if not m.exclude:
1377 client_path = p
1378
1379 # a match, even if rejected, always stops the search
1380 break
1381
1382 return client_path
1383
Luke Diamand3ea2cfd2011-04-21 20:50:23 +01001384class P4Sync(Command, P4UserMap):
Pete Wyckoff56c09342011-02-19 08:17:57 -05001385 delete_actions = ( "delete", "move/delete", "purge" )
1386
Simon Hausmannb9847332007-03-20 20:54:23 +01001387 def __init__(self):
1388 Command.__init__(self)
Luke Diamand3ea2cfd2011-04-21 20:50:23 +01001389 P4UserMap.__init__(self)
Simon Hausmannb9847332007-03-20 20:54:23 +01001390 self.options = [
1391 optparse.make_option("--branch", dest="branch"),
1392 optparse.make_option("--detect-branches", dest="detectBranches", action="store_true"),
1393 optparse.make_option("--changesfile", dest="changesFile"),
1394 optparse.make_option("--silent", dest="silent", action="store_true"),
Simon Hausmannef48f902007-05-17 22:17:49 +02001395 optparse.make_option("--detect-labels", dest="detectLabels", action="store_true"),
Simon Hausmanna028a982007-05-23 00:03:08 +02001396 optparse.make_option("--verbose", dest="verbose", action="store_true"),
Han-Wen Nienhuysd2c6dd32007-05-23 18:49:35 -03001397 optparse.make_option("--import-local", dest="importIntoRemotes", action="store_false",
1398 help="Import into refs/heads/ , not refs/remotes"),
Han-Wen Nienhuys8b41a972007-05-23 18:20:53 -03001399 optparse.make_option("--max-changes", dest="maxChanges"),
Han-Wen Nienhuys86dff6b2007-05-23 18:49:35 -03001400 optparse.make_option("--keep-path", dest="keepRepoPath", action='store_true',
Tor Arvid Lund3a70cdf2008-02-18 15:22:08 +01001401 help="Keep entire BRANCH/DIR/SUBDIR prefix during import"),
1402 optparse.make_option("--use-client-spec", dest="useClientSpec", action='store_true',
1403 help="Only sync files that are included in the Perforce Client Spec")
Simon Hausmannb9847332007-03-20 20:54:23 +01001404 ]
1405 self.description = """Imports from Perforce into a git repository.\n
1406 example:
1407 //depot/my/project/ -- to import the current head
1408 //depot/my/project/@all -- to import everything
1409 //depot/my/project/@1,6 -- to import only from revision 1 to 6
1410
1411 (a ... is not needed in the path p4 specification, it's added implicitly)"""
1412
1413 self.usage += " //depot/path[@revRange]"
Simon Hausmannb9847332007-03-20 20:54:23 +01001414 self.silent = False
Reilly Grant1d7367d2009-09-10 00:02:38 -07001415 self.createdBranches = set()
1416 self.committedChanges = set()
Simon Hausmann569d1bd2007-03-22 21:34:16 +01001417 self.branch = ""
Simon Hausmannb9847332007-03-20 20:54:23 +01001418 self.detectBranches = False
Simon Hausmanncb53e1f2007-04-08 00:12:02 +02001419 self.detectLabels = False
Simon Hausmannb9847332007-03-20 20:54:23 +01001420 self.changesFile = ""
Simon Hausmann01265102007-05-25 10:36:10 +02001421 self.syncWithOrigin = True
Simon Hausmann4b97ffb2007-05-18 21:45:23 +02001422 self.verbose = False
Simon Hausmanna028a982007-05-23 00:03:08 +02001423 self.importIntoRemotes = True
Simon Hausmann01a9c9c2007-05-23 00:07:35 +02001424 self.maxChanges = ""
Marius Storm-Olsenc1f91972007-05-24 14:07:55 +02001425 self.isWindows = (platform.system() == "Windows")
Han-Wen Nienhuys8b41a972007-05-23 18:20:53 -03001426 self.keepRepoPath = False
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03001427 self.depotPaths = None
Simon Hausmann3c699642007-06-16 13:09:21 +02001428 self.p4BranchesInGit = []
Tommy Thorn354081d2008-02-03 10:38:51 -08001429 self.cloneExclude = []
Tor Arvid Lund3a70cdf2008-02-18 15:22:08 +01001430 self.useClientSpec = False
Pete Wyckoffecb7cf92012-01-02 18:05:53 -05001431 self.clientSpecDirs = None
Simon Hausmannb9847332007-03-20 20:54:23 +01001432
Simon Hausmann01265102007-05-25 10:36:10 +02001433 if gitConfig("git-p4.syncFromOrigin") == "false":
1434 self.syncWithOrigin = False
1435
Pete Wyckoff084f6302011-02-19 08:18:00 -05001436 #
1437 # P4 wildcards are not allowed in filenames. P4 complains
1438 # if you simply add them, but you can force it with "-f", in
1439 # which case it translates them into %xx encoding internally.
1440 # Search for and fix just these four characters. Do % last so
1441 # that fixing it does not inadvertently create new %-escapes.
1442 #
1443 def wildcard_decode(self, path):
1444 # Cannot have * in a filename in windows; untested as to
1445 # what p4 would do in such a case.
1446 if not self.isWindows:
1447 path = path.replace("%2A", "*")
1448 path = path.replace("%23", "#") \
1449 .replace("%40", "@") \
1450 .replace("%25", "%")
1451 return path
1452
Simon Hausmannb9847332007-03-20 20:54:23 +01001453 def extractFilesFromCommit(self, commit):
Tommy Thorn354081d2008-02-03 10:38:51 -08001454 self.cloneExclude = [re.sub(r"\.\.\.$", "", path)
1455 for path in self.cloneExclude]
Simon Hausmannb9847332007-03-20 20:54:23 +01001456 files = []
1457 fnum = 0
1458 while commit.has_key("depotFile%s" % fnum):
1459 path = commit["depotFile%s" % fnum]
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03001460
Tommy Thorn354081d2008-02-03 10:38:51 -08001461 if [p for p in self.cloneExclude
Tor Arvid Lundd53de8b2011-03-15 13:08:02 +01001462 if p4PathStartsWith(path, p)]:
Tommy Thorn354081d2008-02-03 10:38:51 -08001463 found = False
1464 else:
1465 found = [p for p in self.depotPaths
Tor Arvid Lundd53de8b2011-03-15 13:08:02 +01001466 if p4PathStartsWith(path, p)]
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03001467 if not found:
Simon Hausmannb9847332007-03-20 20:54:23 +01001468 fnum = fnum + 1
1469 continue
1470
1471 file = {}
1472 file["path"] = path
1473 file["rev"] = commit["rev%s" % fnum]
1474 file["action"] = commit["action%s" % fnum]
1475 file["type"] = commit["type%s" % fnum]
1476 files.append(file)
1477 fnum = fnum + 1
1478 return files
1479
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03001480 def stripRepoPath(self, path, prefixes):
Ian Wienand39527102011-02-11 16:33:48 -08001481 if self.useClientSpec:
Pete Wyckoffecb7cf92012-01-02 18:05:53 -05001482 return self.clientSpecDirs.map_in_client(path)
Ian Wienand39527102011-02-11 16:33:48 -08001483
Han-Wen Nienhuys8b41a972007-05-23 18:20:53 -03001484 if self.keepRepoPath:
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03001485 prefixes = [re.sub("^(//[^/]+/).*", r'\1', prefixes[0])]
Han-Wen Nienhuys8b41a972007-05-23 18:20:53 -03001486
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03001487 for p in prefixes:
Tor Arvid Lundd53de8b2011-03-15 13:08:02 +01001488 if p4PathStartsWith(path, p):
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03001489 path = path[len(p):]
1490
1491 return path
Han-Wen Nienhuys6754a292007-05-23 17:41:50 -03001492
Simon Hausmann71b112d2007-05-19 11:54:11 +02001493 def splitFilesIntoBranches(self, commit):
Simon Hausmannd5904672007-05-19 11:07:32 +02001494 branches = {}
Simon Hausmann71b112d2007-05-19 11:54:11 +02001495 fnum = 0
1496 while commit.has_key("depotFile%s" % fnum):
1497 path = commit["depotFile%s" % fnum]
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03001498 found = [p for p in self.depotPaths
Tor Arvid Lundd53de8b2011-03-15 13:08:02 +01001499 if p4PathStartsWith(path, p)]
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03001500 if not found:
Simon Hausmann71b112d2007-05-19 11:54:11 +02001501 fnum = fnum + 1
1502 continue
1503
1504 file = {}
1505 file["path"] = path
1506 file["rev"] = commit["rev%s" % fnum]
1507 file["action"] = commit["action%s" % fnum]
1508 file["type"] = commit["type%s" % fnum]
1509 fnum = fnum + 1
1510
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03001511 relPath = self.stripRepoPath(path, self.depotPaths)
Simon Hausmannb9847332007-03-20 20:54:23 +01001512
Simon Hausmann4b97ffb2007-05-18 21:45:23 +02001513 for branch in self.knownBranches.keys():
Han-Wen Nienhuys6754a292007-05-23 17:41:50 -03001514
1515 # add a trailing slash so that a commit into qt/4.2foo doesn't end up in qt/4.2
1516 if relPath.startswith(branch + "/"):
Simon Hausmannd5904672007-05-19 11:07:32 +02001517 if branch not in branches:
1518 branches[branch] = []
Simon Hausmann71b112d2007-05-19 11:54:11 +02001519 branches[branch].append(file)
Simon Hausmann6555b2c2007-06-17 11:25:34 +02001520 break
Simon Hausmannb9847332007-03-20 20:54:23 +01001521
1522 return branches
1523
Luke Diamandb9327052009-07-30 00:13:46 +01001524 # output one file from the P4 stream
1525 # - helper for streamP4Files
1526
1527 def streamOneP4File(self, file, contents):
Luke Diamandb9327052009-07-30 00:13:46 +01001528 relPath = self.stripRepoPath(file['depotFile'], self.branchPrefixes)
Pete Wyckoff084f6302011-02-19 08:18:00 -05001529 relPath = self.wildcard_decode(relPath)
Luke Diamandb9327052009-07-30 00:13:46 +01001530 if verbose:
1531 sys.stderr.write("%s\n" % relPath)
1532
Pete Wyckoff9cffb8c2011-10-16 10:45:01 -04001533 (type_base, type_mods) = split_p4_type(file["type"])
1534
1535 git_mode = "100644"
1536 if "x" in type_mods:
1537 git_mode = "100755"
1538 if type_base == "symlink":
1539 git_mode = "120000"
1540 # p4 print on a symlink contains "target\n"; remove the newline
Evan Powersb39c3612010-02-16 00:44:08 -08001541 data = ''.join(contents)
1542 contents = [data[:-1]]
Luke Diamandb9327052009-07-30 00:13:46 +01001543
Pete Wyckoff9cffb8c2011-10-16 10:45:01 -04001544 if type_base == "utf16":
Pete Wyckoff55aa5712011-09-17 19:16:14 -04001545 # p4 delivers different text in the python output to -G
1546 # than it does when using "print -o", or normal p4 client
1547 # operations. utf16 is converted to ascii or utf8, perhaps.
1548 # But ascii text saved as -t utf16 is completely mangled.
1549 # Invoke print -o to get the real contents.
Luke Diamand6de040d2011-10-16 10:47:52 -04001550 text = p4_read_pipe(['print', '-q', '-o', '-', file['depotFile']])
Pete Wyckoff55aa5712011-09-17 19:16:14 -04001551 contents = [ text ]
1552
Pete Wyckoff9f7ef0e2011-11-05 13:36:07 -04001553 if type_base == "apple":
1554 # Apple filetype files will be streamed as a concatenation of
1555 # its appledouble header and the contents. This is useless
1556 # on both macs and non-macs. If using "print -q -o xx", it
1557 # will create "xx" with the data, and "%xx" with the header.
1558 # This is also not very useful.
1559 #
1560 # Ideally, someday, this script can learn how to generate
1561 # appledouble files directly and import those to git, but
1562 # non-mac machines can never find a use for apple filetype.
1563 print "\nIgnoring apple filetype file %s" % file['depotFile']
1564 return
1565
Pete Wyckoff9cffb8c2011-10-16 10:45:01 -04001566 # Perhaps windows wants unicode, utf16 newlines translated too;
1567 # but this is not doing it.
1568 if self.isWindows and type_base == "text":
Luke Diamandb9327052009-07-30 00:13:46 +01001569 mangled = []
1570 for data in contents:
1571 data = data.replace("\r\n", "\n")
1572 mangled.append(data)
1573 contents = mangled
1574
Pete Wyckoff55aa5712011-09-17 19:16:14 -04001575 # Note that we do not try to de-mangle keywords on utf16 files,
1576 # even though in theory somebody may want that.
Pete Wyckoff9cffb8c2011-10-16 10:45:01 -04001577 if type_base in ("text", "unicode", "binary"):
1578 if "ko" in type_mods:
Pete Wyckoffcb585a92011-10-16 10:46:52 -04001579 text = ''.join(contents)
1580 text = re.sub(r'\$(Id|Header):[^$]*\$', r'$\1$', text)
1581 contents = [ text ]
Pete Wyckoff9cffb8c2011-10-16 10:45:01 -04001582 elif "k" in type_mods:
Pete Wyckoffcb585a92011-10-16 10:46:52 -04001583 text = ''.join(contents)
1584 text = re.sub(r'\$(Id|Header|Author|Date|DateTime|Change|File|Revision):[^$]*\$', r'$\1$', text)
1585 contents = [ text ]
Luke Diamandb9327052009-07-30 00:13:46 +01001586
Pete Wyckoff9cffb8c2011-10-16 10:45:01 -04001587 self.gitStream.write("M %s inline %s\n" % (git_mode, relPath))
Luke Diamandb9327052009-07-30 00:13:46 +01001588
1589 # total length...
1590 length = 0
1591 for d in contents:
1592 length = length + len(d)
1593
1594 self.gitStream.write("data %d\n" % length)
1595 for d in contents:
1596 self.gitStream.write(d)
1597 self.gitStream.write("\n")
1598
1599 def streamOneP4Deletion(self, file):
1600 relPath = self.stripRepoPath(file['path'], self.branchPrefixes)
1601 if verbose:
1602 sys.stderr.write("delete %s\n" % relPath)
1603 self.gitStream.write("D %s\n" % relPath)
1604
1605 # handle another chunk of streaming data
1606 def streamP4FilesCb(self, marshalled):
1607
Andrew Garberc3f61632011-04-07 02:01:21 -04001608 if marshalled.has_key('depotFile') and self.stream_have_file_info:
1609 # start of a new file - output the old one first
1610 self.streamOneP4File(self.stream_file, self.stream_contents)
1611 self.stream_file = {}
1612 self.stream_contents = []
1613 self.stream_have_file_info = False
Luke Diamandb9327052009-07-30 00:13:46 +01001614
Andrew Garberc3f61632011-04-07 02:01:21 -04001615 # pick up the new file information... for the
1616 # 'data' field we need to append to our array
1617 for k in marshalled.keys():
1618 if k == 'data':
1619 self.stream_contents.append(marshalled['data'])
1620 else:
1621 self.stream_file[k] = marshalled[k]
Luke Diamandb9327052009-07-30 00:13:46 +01001622
Andrew Garberc3f61632011-04-07 02:01:21 -04001623 self.stream_have_file_info = True
Luke Diamandb9327052009-07-30 00:13:46 +01001624
1625 # Stream directly from "p4 files" into "git fast-import"
1626 def streamP4Files(self, files):
Simon Hausmann30b59402008-03-03 11:55:48 +01001627 filesForCommit = []
1628 filesToRead = []
Luke Diamandb9327052009-07-30 00:13:46 +01001629 filesToDelete = []
Simon Hausmann30b59402008-03-03 11:55:48 +01001630
Tor Arvid Lund3a70cdf2008-02-18 15:22:08 +01001631 for f in files:
Pete Wyckoffecb7cf92012-01-02 18:05:53 -05001632 # if using a client spec, only add the files that have
1633 # a path in the client
1634 if self.clientSpecDirs:
1635 if self.clientSpecDirs.map_in_client(f['path']) == "":
1636 continue
Tor Arvid Lund3a70cdf2008-02-18 15:22:08 +01001637
Pete Wyckoffecb7cf92012-01-02 18:05:53 -05001638 filesForCommit.append(f)
1639 if f['action'] in self.delete_actions:
1640 filesToDelete.append(f)
1641 else:
1642 filesToRead.append(f)
Han-Wen Nienhuys6a49f8e2007-05-23 18:49:35 -03001643
Luke Diamandb9327052009-07-30 00:13:46 +01001644 # deleted files...
1645 for f in filesToDelete:
1646 self.streamOneP4Deletion(f)
1647
Simon Hausmann30b59402008-03-03 11:55:48 +01001648 if len(filesToRead) > 0:
Luke Diamandb9327052009-07-30 00:13:46 +01001649 self.stream_file = {}
1650 self.stream_contents = []
1651 self.stream_have_file_info = False
1652
Andrew Garberc3f61632011-04-07 02:01:21 -04001653 # curry self argument
1654 def streamP4FilesCbSelf(entry):
1655 self.streamP4FilesCb(entry)
Luke Diamandb9327052009-07-30 00:13:46 +01001656
Luke Diamand6de040d2011-10-16 10:47:52 -04001657 fileArgs = ['%s#%s' % (f['path'], f['rev']) for f in filesToRead]
1658
1659 p4CmdList(["-x", "-", "print"],
1660 stdin=fileArgs,
1661 cb=streamP4FilesCbSelf)
Han-Wen Nienhuysf2eda792007-05-23 18:49:35 -03001662
Luke Diamandb9327052009-07-30 00:13:46 +01001663 # do the last chunk
1664 if self.stream_file.has_key('depotFile'):
1665 self.streamOneP4File(self.stream_file, self.stream_contents)
Han-Wen Nienhuys6a49f8e2007-05-23 18:49:35 -03001666
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03001667 def commit(self, details, files, branch, branchPrefixes, parent = ""):
Simon Hausmannb9847332007-03-20 20:54:23 +01001668 epoch = details["time"]
1669 author = details["user"]
Andrew Garberc3f61632011-04-07 02:01:21 -04001670 self.branchPrefixes = branchPrefixes
Simon Hausmannb9847332007-03-20 20:54:23 +01001671
Simon Hausmann4b97ffb2007-05-18 21:45:23 +02001672 if self.verbose:
1673 print "commit into %s" % branch
1674
Han-Wen Nienhuys96e07dd2007-05-23 18:49:35 -03001675 # start with reading files; if that fails, we should not
1676 # create a commit.
1677 new_files = []
1678 for f in files:
Tor Arvid Lundd53de8b2011-03-15 13:08:02 +01001679 if [p for p in branchPrefixes if p4PathStartsWith(f['path'], p)]:
Han-Wen Nienhuys96e07dd2007-05-23 18:49:35 -03001680 new_files.append (f)
1681 else:
Tor Arvid Lundafa1dd92011-03-15 13:08:03 +01001682 sys.stderr.write("Ignoring file outside of prefix: %s\n" % f['path'])
Han-Wen Nienhuys96e07dd2007-05-23 18:49:35 -03001683
Simon Hausmannb9847332007-03-20 20:54:23 +01001684 self.gitStream.write("commit %s\n" % branch)
Han-Wen Nienhuys6a49f8e2007-05-23 18:49:35 -03001685# gitStream.write("mark :%s\n" % details["change"])
Simon Hausmannb9847332007-03-20 20:54:23 +01001686 self.committedChanges.add(int(details["change"]))
1687 committer = ""
Simon Hausmannb607e712007-05-20 10:55:54 +02001688 if author not in self.users:
1689 self.getUserMapFromPerforceServer()
Simon Hausmannb9847332007-03-20 20:54:23 +01001690 if author in self.users:
Simon Hausmann0828ab12007-03-20 20:59:30 +01001691 committer = "%s %s %s" % (self.users[author], epoch, self.tz)
Simon Hausmannb9847332007-03-20 20:54:23 +01001692 else:
Simon Hausmann0828ab12007-03-20 20:59:30 +01001693 committer = "%s <a@b> %s %s" % (author, epoch, self.tz)
Simon Hausmannb9847332007-03-20 20:54:23 +01001694
1695 self.gitStream.write("committer %s\n" % committer)
1696
1697 self.gitStream.write("data <<EOT\n")
1698 self.gitStream.write(details["desc"])
Simon Hausmann6581de02007-06-11 10:01:58 +02001699 self.gitStream.write("\n[git-p4: depot-paths = \"%s\": change = %s"
1700 % (','.join (branchPrefixes), details["change"]))
1701 if len(details['options']) > 0:
1702 self.gitStream.write(": options = %s" % details['options'])
1703 self.gitStream.write("]\nEOT\n\n")
Simon Hausmannb9847332007-03-20 20:54:23 +01001704
1705 if len(parent) > 0:
Simon Hausmann4b97ffb2007-05-18 21:45:23 +02001706 if self.verbose:
1707 print "parent %s" % parent
Simon Hausmannb9847332007-03-20 20:54:23 +01001708 self.gitStream.write("from %s\n" % parent)
1709
Luke Diamandb9327052009-07-30 00:13:46 +01001710 self.streamP4Files(new_files)
Simon Hausmannb9847332007-03-20 20:54:23 +01001711 self.gitStream.write("\n")
1712
Simon Hausmann1f4ba1c2007-03-26 22:34:34 +02001713 change = int(details["change"])
1714
Simon Hausmann9bda3a82007-05-19 12:05:40 +02001715 if self.labels.has_key(change):
Simon Hausmann1f4ba1c2007-03-26 22:34:34 +02001716 label = self.labels[change]
1717 labelDetails = label[0]
1718 labelRevisions = label[1]
Simon Hausmann71b112d2007-05-19 11:54:11 +02001719 if self.verbose:
1720 print "Change %s is labelled %s" % (change, labelDetails)
Simon Hausmann1f4ba1c2007-03-26 22:34:34 +02001721
Luke Diamand6de040d2011-10-16 10:47:52 -04001722 files = p4CmdList(["files"] + ["%s...@%s" % (p, change)
1723 for p in branchPrefixes])
Simon Hausmann1f4ba1c2007-03-26 22:34:34 +02001724
1725 if len(files) == len(labelRevisions):
1726
1727 cleanedFiles = {}
1728 for info in files:
Pete Wyckoff56c09342011-02-19 08:17:57 -05001729 if info["action"] in self.delete_actions:
Simon Hausmann1f4ba1c2007-03-26 22:34:34 +02001730 continue
1731 cleanedFiles[info["depotFile"]] = info["rev"]
1732
1733 if cleanedFiles == labelRevisions:
1734 self.gitStream.write("tag tag_%s\n" % labelDetails["label"])
1735 self.gitStream.write("from %s\n" % branch)
1736
1737 owner = labelDetails["Owner"]
1738 tagger = ""
1739 if author in self.users:
1740 tagger = "%s %s %s" % (self.users[owner], epoch, self.tz)
1741 else:
1742 tagger = "%s <a@b> %s %s" % (owner, epoch, self.tz)
1743 self.gitStream.write("tagger %s\n" % tagger)
1744 self.gitStream.write("data <<EOT\n")
1745 self.gitStream.write(labelDetails["Description"])
1746 self.gitStream.write("EOT\n\n")
1747
1748 else:
Simon Hausmanna46668f2007-03-28 17:05:38 +02001749 if not self.silent:
Han-Wen Nienhuyscebdf5a2007-05-23 16:53:11 -03001750 print ("Tag %s does not match with change %s: files do not match."
1751 % (labelDetails["label"], change))
Simon Hausmann1f4ba1c2007-03-26 22:34:34 +02001752
1753 else:
Simon Hausmanna46668f2007-03-28 17:05:38 +02001754 if not self.silent:
Han-Wen Nienhuyscebdf5a2007-05-23 16:53:11 -03001755 print ("Tag %s does not match with change %s: file count is different."
1756 % (labelDetails["label"], change))
Simon Hausmannb9847332007-03-20 20:54:23 +01001757
Simon Hausmann1f4ba1c2007-03-26 22:34:34 +02001758 def getLabels(self):
1759 self.labels = {}
1760
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03001761 l = p4CmdList("labels %s..." % ' '.join (self.depotPaths))
Simon Hausmann10c32112007-04-08 10:15:47 +02001762 if len(l) > 0 and not self.silent:
Shun Kei Leung183f8432007-11-21 11:01:19 +08001763 print "Finding files belonging to labels in %s" % `self.depotPaths`
Simon Hausmann01ce1fe2007-04-07 23:46:50 +02001764
1765 for output in l:
Simon Hausmann1f4ba1c2007-03-26 22:34:34 +02001766 label = output["label"]
1767 revisions = {}
1768 newestChange = 0
Simon Hausmann71b112d2007-05-19 11:54:11 +02001769 if self.verbose:
1770 print "Querying files for label %s" % label
Luke Diamand6de040d2011-10-16 10:47:52 -04001771 for file in p4CmdList(["files"] +
1772 ["%s...@%s" % (p, label)
1773 for p in self.depotPaths]):
Simon Hausmann1f4ba1c2007-03-26 22:34:34 +02001774 revisions[file["depotFile"]] = file["rev"]
1775 change = int(file["change"])
1776 if change > newestChange:
1777 newestChange = change
1778
Simon Hausmann9bda3a82007-05-19 12:05:40 +02001779 self.labels[newestChange] = [output, revisions]
1780
1781 if self.verbose:
1782 print "Label changes: %s" % self.labels.keys()
Simon Hausmann1f4ba1c2007-03-26 22:34:34 +02001783
Han-Wen Nienhuys86dff6b2007-05-23 18:49:35 -03001784 def guessProjectName(self):
1785 for p in self.depotPaths:
Simon Hausmann6e5295c2007-06-11 08:50:57 +02001786 if p.endswith("/"):
1787 p = p[:-1]
1788 p = p[p.strip().rfind("/") + 1:]
1789 if not p.endswith("/"):
1790 p += "/"
1791 return p
Han-Wen Nienhuys86dff6b2007-05-23 18:49:35 -03001792
Simon Hausmann4b97ffb2007-05-18 21:45:23 +02001793 def getBranchMapping(self):
Simon Hausmann6555b2c2007-06-17 11:25:34 +02001794 lostAndFoundBranches = set()
1795
Vitor Antunes8ace74c2011-08-19 00:44:04 +01001796 user = gitConfig("git-p4.branchUser")
1797 if len(user) > 0:
1798 command = "branches -u %s" % user
1799 else:
1800 command = "branches"
1801
1802 for info in p4CmdList(command):
Simon Hausmann4b97ffb2007-05-18 21:45:23 +02001803 details = p4Cmd("branch -o %s" % info["branch"])
1804 viewIdx = 0
1805 while details.has_key("View%s" % viewIdx):
1806 paths = details["View%s" % viewIdx].split(" ")
1807 viewIdx = viewIdx + 1
1808 # require standard //depot/foo/... //depot/bar/... mapping
1809 if len(paths) != 2 or not paths[0].endswith("/...") or not paths[1].endswith("/..."):
1810 continue
1811 source = paths[0]
1812 destination = paths[1]
Simon Hausmann6509e192007-06-07 09:41:53 +02001813 ## HACK
Tor Arvid Lundd53de8b2011-03-15 13:08:02 +01001814 if p4PathStartsWith(source, self.depotPaths[0]) and p4PathStartsWith(destination, self.depotPaths[0]):
Simon Hausmann6509e192007-06-07 09:41:53 +02001815 source = source[len(self.depotPaths[0]):-4]
1816 destination = destination[len(self.depotPaths[0]):-4]
Simon Hausmann6555b2c2007-06-17 11:25:34 +02001817
Simon Hausmann1a2edf42007-06-17 15:10:24 +02001818 if destination in self.knownBranches:
1819 if not self.silent:
1820 print "p4 branch %s defines a mapping from %s to %s" % (info["branch"], source, destination)
1821 print "but there exists another mapping from %s to %s already!" % (self.knownBranches[destination], destination)
1822 continue
1823
Simon Hausmann6555b2c2007-06-17 11:25:34 +02001824 self.knownBranches[destination] = source
1825
1826 lostAndFoundBranches.discard(destination)
1827
Simon Hausmann29bdbac2007-05-19 10:23:12 +02001828 if source not in self.knownBranches:
Simon Hausmann6555b2c2007-06-17 11:25:34 +02001829 lostAndFoundBranches.add(source)
1830
Vitor Antunes7199cf12011-08-19 00:44:05 +01001831 # Perforce does not strictly require branches to be defined, so we also
1832 # check git config for a branch list.
1833 #
1834 # Example of branch definition in git config file:
1835 # [git-p4]
1836 # branchList=main:branchA
1837 # branchList=main:branchB
1838 # branchList=branchA:branchC
1839 configBranches = gitConfigList("git-p4.branchList")
1840 for branch in configBranches:
1841 if branch:
1842 (source, destination) = branch.split(":")
1843 self.knownBranches[destination] = source
1844
1845 lostAndFoundBranches.discard(destination)
1846
1847 if source not in self.knownBranches:
1848 lostAndFoundBranches.add(source)
1849
Simon Hausmann6555b2c2007-06-17 11:25:34 +02001850
1851 for branch in lostAndFoundBranches:
1852 self.knownBranches[branch] = branch
Simon Hausmann29bdbac2007-05-19 10:23:12 +02001853
Simon Hausmann38f9f5e2007-11-15 10:38:45 +01001854 def getBranchMappingFromGitBranches(self):
1855 branches = p4BranchesInGit(self.importIntoRemotes)
1856 for branch in branches.keys():
1857 if branch == "master":
1858 branch = "main"
1859 else:
1860 branch = branch[len(self.projectName):]
1861 self.knownBranches[branch] = branch
1862
Simon Hausmann29bdbac2007-05-19 10:23:12 +02001863 def listExistingP4GitBranches(self):
Simon Hausmann144ff462007-07-18 17:27:50 +02001864 # branches holds mapping from name to commit
1865 branches = p4BranchesInGit(self.importIntoRemotes)
1866 self.p4BranchesInGit = branches.keys()
1867 for branch in branches.keys():
1868 self.initialParents[self.refPrefix + branch] = branches[branch]
Simon Hausmann4b97ffb2007-05-18 21:45:23 +02001869
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03001870 def updateOptionDict(self, d):
1871 option_keys = {}
1872 if self.keepRepoPath:
1873 option_keys['keepRepoPath'] = 1
1874
1875 d["options"] = ' '.join(sorted(option_keys.keys()))
1876
1877 def readOptions(self, d):
1878 self.keepRepoPath = (d.has_key('options')
1879 and ('keepRepoPath' in d['options']))
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03001880
Simon Hausmann8134f692007-08-26 16:44:55 +02001881 def gitRefForBranch(self, branch):
1882 if branch == "main":
1883 return self.refPrefix + "master"
1884
1885 if len(branch) <= 0:
1886 return branch
1887
1888 return self.refPrefix + self.projectName + branch
1889
Simon Hausmann1ca3d712007-08-26 17:36:55 +02001890 def gitCommitByP4Change(self, ref, change):
1891 if self.verbose:
1892 print "looking in ref " + ref + " for change %s using bisect..." % change
1893
1894 earliestCommit = ""
1895 latestCommit = parseRevision(ref)
1896
1897 while True:
1898 if self.verbose:
1899 print "trying: earliest %s latest %s" % (earliestCommit, latestCommit)
1900 next = read_pipe("git rev-list --bisect %s %s" % (latestCommit, earliestCommit)).strip()
1901 if len(next) == 0:
1902 if self.verbose:
1903 print "argh"
1904 return ""
1905 log = extractLogMessageFromGitCommit(next)
1906 settings = extractSettingsGitLog(log)
1907 currentChange = int(settings['change'])
1908 if self.verbose:
1909 print "current change %s" % currentChange
1910
1911 if currentChange == change:
1912 if self.verbose:
1913 print "found %s" % next
1914 return next
1915
1916 if currentChange < change:
1917 earliestCommit = "^%s" % next
1918 else:
1919 latestCommit = "%s" % next
1920
1921 return ""
1922
1923 def importNewBranch(self, branch, maxChange):
1924 # make fast-import flush all changes to disk and update the refs using the checkpoint
1925 # command so that we can try to find the branch parent in the git history
1926 self.gitStream.write("checkpoint\n\n");
1927 self.gitStream.flush();
1928 branchPrefix = self.depotPaths[0] + branch + "/"
1929 range = "@1,%s" % maxChange
1930 #print "prefix" + branchPrefix
1931 changes = p4ChangesForPaths([branchPrefix], range)
1932 if len(changes) <= 0:
1933 return False
1934 firstChange = changes[0]
1935 #print "first change in branch: %s" % firstChange
1936 sourceBranch = self.knownBranches[branch]
1937 sourceDepotPath = self.depotPaths[0] + sourceBranch
1938 sourceRef = self.gitRefForBranch(sourceBranch)
1939 #print "source " + sourceBranch
1940
1941 branchParentChange = int(p4Cmd("changes -m 1 %s...@1,%s" % (sourceDepotPath, firstChange))["change"])
1942 #print "branch parent: %s" % branchParentChange
1943 gitParent = self.gitCommitByP4Change(sourceRef, branchParentChange)
1944 if len(gitParent) > 0:
1945 self.initialParents[self.gitRefForBranch(branch)] = gitParent
1946 #print "parent git commit: %s" % gitParent
1947
1948 self.importChanges(changes)
1949 return True
1950
Simon Hausmanne87f37a2007-08-26 16:00:52 +02001951 def importChanges(self, changes):
1952 cnt = 1
1953 for change in changes:
1954 description = p4Cmd("describe %s" % change)
1955 self.updateOptionDict(description)
1956
1957 if not self.silent:
1958 sys.stdout.write("\rImporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
1959 sys.stdout.flush()
1960 cnt = cnt + 1
1961
1962 try:
1963 if self.detectBranches:
1964 branches = self.splitFilesIntoBranches(description)
1965 for branch in branches.keys():
1966 ## HACK --hwn
1967 branchPrefix = self.depotPaths[0] + branch + "/"
1968
1969 parent = ""
1970
1971 filesForCommit = branches[branch]
1972
1973 if self.verbose:
1974 print "branch is %s" % branch
1975
1976 self.updatedBranches.add(branch)
1977
1978 if branch not in self.createdBranches:
1979 self.createdBranches.add(branch)
1980 parent = self.knownBranches[branch]
1981 if parent == branch:
1982 parent = ""
Simon Hausmann1ca3d712007-08-26 17:36:55 +02001983 else:
1984 fullBranch = self.projectName + branch
1985 if fullBranch not in self.p4BranchesInGit:
1986 if not self.silent:
1987 print("\n Importing new branch %s" % fullBranch);
1988 if self.importNewBranch(branch, change - 1):
1989 parent = ""
1990 self.p4BranchesInGit.append(fullBranch)
1991 if not self.silent:
1992 print("\n Resuming with change %s" % change);
1993
1994 if self.verbose:
1995 print "parent determined through known branches: %s" % parent
Simon Hausmanne87f37a2007-08-26 16:00:52 +02001996
Simon Hausmann8134f692007-08-26 16:44:55 +02001997 branch = self.gitRefForBranch(branch)
1998 parent = self.gitRefForBranch(parent)
Simon Hausmanne87f37a2007-08-26 16:00:52 +02001999
2000 if self.verbose:
2001 print "looking for initial parent for %s; current parent is %s" % (branch, parent)
2002
2003 if len(parent) == 0 and branch in self.initialParents:
2004 parent = self.initialParents[branch]
2005 del self.initialParents[branch]
2006
2007 self.commit(description, filesForCommit, branch, [branchPrefix], parent)
2008 else:
2009 files = self.extractFilesFromCommit(description)
2010 self.commit(description, files, self.branch, self.depotPaths,
2011 self.initialParent)
2012 self.initialParent = ""
2013 except IOError:
2014 print self.gitError.read()
2015 sys.exit(1)
2016
Simon Hausmannc208a242007-08-26 16:07:18 +02002017 def importHeadRevision(self, revision):
2018 print "Doing initial import of %s from revision %s into %s" % (' '.join(self.depotPaths), revision, self.branch)
2019
Pete Wyckoff4e2e6ce2011-07-31 09:45:55 -04002020 details = {}
2021 details["user"] = "git perforce import user"
Pete Wyckoff1494fcb2011-02-19 08:17:56 -05002022 details["desc"] = ("Initial import of %s from the state at revision %s\n"
Simon Hausmannc208a242007-08-26 16:07:18 +02002023 % (' '.join(self.depotPaths), revision))
2024 details["change"] = revision
2025 newestRevision = 0
2026
2027 fileCnt = 0
Luke Diamand6de040d2011-10-16 10:47:52 -04002028 fileArgs = ["%s...%s" % (p,revision) for p in self.depotPaths]
2029
2030 for info in p4CmdList(["files"] + fileArgs):
Simon Hausmannc208a242007-08-26 16:07:18 +02002031
Pete Wyckoff68b28592011-02-19 08:17:55 -05002032 if 'code' in info and info['code'] == 'error':
Simon Hausmannc208a242007-08-26 16:07:18 +02002033 sys.stderr.write("p4 returned an error: %s\n"
2034 % info['data'])
Pete Wyckoffd88e7072011-02-19 08:17:58 -05002035 if info['data'].find("must refer to client") >= 0:
2036 sys.stderr.write("This particular p4 error is misleading.\n")
2037 sys.stderr.write("Perhaps the depot path was misspelled.\n");
2038 sys.stderr.write("Depot path: %s\n" % " ".join(self.depotPaths))
Simon Hausmannc208a242007-08-26 16:07:18 +02002039 sys.exit(1)
Pete Wyckoff68b28592011-02-19 08:17:55 -05002040 if 'p4ExitCode' in info:
2041 sys.stderr.write("p4 exitcode: %s\n" % info['p4ExitCode'])
Simon Hausmannc208a242007-08-26 16:07:18 +02002042 sys.exit(1)
2043
2044
2045 change = int(info["change"])
2046 if change > newestRevision:
2047 newestRevision = change
2048
Pete Wyckoff56c09342011-02-19 08:17:57 -05002049 if info["action"] in self.delete_actions:
Simon Hausmannc208a242007-08-26 16:07:18 +02002050 # don't increase the file cnt, otherwise details["depotFile123"] will have gaps!
2051 #fileCnt = fileCnt + 1
2052 continue
2053
2054 for prop in ["depotFile", "rev", "action", "type" ]:
2055 details["%s%s" % (prop, fileCnt)] = info[prop]
2056
2057 fileCnt = fileCnt + 1
2058
2059 details["change"] = newestRevision
Pete Wyckoff4e2e6ce2011-07-31 09:45:55 -04002060
2061 # Use time from top-most change so that all git-p4 clones of
2062 # the same p4 repo have the same commit SHA1s.
2063 res = p4CmdList("describe -s %d" % newestRevision)
2064 newestTime = None
2065 for r in res:
2066 if r.has_key('time'):
2067 newestTime = int(r['time'])
2068 if newestTime is None:
2069 die("\"describe -s\" on newest change %d did not give a time")
2070 details["time"] = newestTime
2071
Simon Hausmannc208a242007-08-26 16:07:18 +02002072 self.updateOptionDict(details)
2073 try:
2074 self.commit(details, self.extractFilesFromCommit(details), self.branch, self.depotPaths)
2075 except IOError:
2076 print "IO error with git fast-import. Is your git version recent enough?"
2077 print self.gitError.read()
2078
2079
Tor Arvid Lund3a70cdf2008-02-18 15:22:08 +01002080 def getClientSpec(self):
Pete Wyckoffecb7cf92012-01-02 18:05:53 -05002081 specList = p4CmdList("client -o")
2082 if len(specList) != 1:
2083 die('Output from "client -o" is %d lines, expecting 1' %
2084 len(specList))
Ian Wienand39527102011-02-11 16:33:48 -08002085
Pete Wyckoffecb7cf92012-01-02 18:05:53 -05002086 # dictionary of all client parameters
2087 entry = specList[0]
Ian Wienand39527102011-02-11 16:33:48 -08002088
Pete Wyckoffecb7cf92012-01-02 18:05:53 -05002089 # just the keys that start with "View"
2090 view_keys = [ k for k in entry.keys() if k.startswith("View") ]
Ian Wienand39527102011-02-11 16:33:48 -08002091
Pete Wyckoffecb7cf92012-01-02 18:05:53 -05002092 # hold this new View
2093 view = View()
Ian Wienand39527102011-02-11 16:33:48 -08002094
Pete Wyckoffecb7cf92012-01-02 18:05:53 -05002095 # append the lines, in order, to the view
2096 for view_num in range(len(view_keys)):
2097 k = "View%d" % view_num
2098 if k not in view_keys:
2099 die("Expected view key %s missing" % k)
2100 view.append(entry[k])
Ian Wienand39527102011-02-11 16:33:48 -08002101
Pete Wyckoffecb7cf92012-01-02 18:05:53 -05002102 self.clientSpecDirs = view
Gary Gibbonsdf5ed902012-01-02 18:05:51 -05002103 if self.verbose:
Pete Wyckoffecb7cf92012-01-02 18:05:53 -05002104 for i, m in enumerate(self.clientSpecDirs.mappings):
2105 print "clientSpecDirs %d: %s" % (i, str(m))
Tor Arvid Lund3a70cdf2008-02-18 15:22:08 +01002106
Simon Hausmannb9847332007-03-20 20:54:23 +01002107 def run(self, args):
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03002108 self.depotPaths = []
Simon Hausmann179caeb2007-03-22 22:17:42 +01002109 self.changeRange = ""
2110 self.initialParent = ""
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03002111 self.previousDepotPaths = []
Han-Wen Nienhuysce6f33c2007-05-23 16:46:29 -03002112
Simon Hausmann29bdbac2007-05-19 10:23:12 +02002113 # map from branch depot path to parent branch
2114 self.knownBranches = {}
2115 self.initialParents = {}
Simon Hausmann5ca44612007-08-24 17:44:16 +02002116 self.hasOrigin = originP4BranchesExist()
Simon Hausmanna43ff002007-06-11 09:59:27 +02002117 if not self.syncWithOrigin:
2118 self.hasOrigin = False
Simon Hausmann179caeb2007-03-22 22:17:42 +01002119
Simon Hausmanna028a982007-05-23 00:03:08 +02002120 if self.importIntoRemotes:
2121 self.refPrefix = "refs/remotes/p4/"
2122 else:
Marius Storm-Olsendb775552007-06-07 15:13:59 +02002123 self.refPrefix = "refs/heads/p4/"
Simon Hausmanna028a982007-05-23 00:03:08 +02002124
Han-Wen Nienhuyscebdf5a2007-05-23 16:53:11 -03002125 if self.syncWithOrigin and self.hasOrigin:
2126 if not self.silent:
2127 print "Syncing with origin first by calling git fetch origin"
2128 system("git fetch origin")
Simon Hausmann10f880f2007-05-24 22:28:28 +02002129
Simon Hausmann569d1bd2007-03-22 21:34:16 +01002130 if len(self.branch) == 0:
Marius Storm-Olsendb775552007-06-07 15:13:59 +02002131 self.branch = self.refPrefix + "master"
Simon Hausmanna028a982007-05-23 00:03:08 +02002132 if gitBranchExists("refs/heads/p4") and self.importIntoRemotes:
Simon Hausmann48df6fd2007-05-17 21:18:53 +02002133 system("git update-ref %s refs/heads/p4" % self.branch)
Simon Hausmann48df6fd2007-05-17 21:18:53 +02002134 system("git branch -D p4");
Simon Hausmannfaf1bd22007-05-21 10:05:30 +02002135 # create it /after/ importing, when master exists
Simon Hausmann0058a332007-08-24 17:46:16 +02002136 if not gitBranchExists(self.refPrefix + "HEAD") and self.importIntoRemotes and gitBranchExists(self.branch):
Simon Hausmanna3c55c02007-05-27 15:48:01 +02002137 system("git symbolic-ref %sHEAD %s" % (self.refPrefix, self.branch))
Simon Hausmann179caeb2007-03-22 22:17:42 +01002138
Pete Wyckoff09fca772011-12-24 21:07:39 -05002139 if not self.useClientSpec:
2140 if gitConfig("git-p4.useclientspec", "--bool") == "true":
2141 self.useClientSpec = True
2142 if self.useClientSpec:
Tor Arvid Lund3a70cdf2008-02-18 15:22:08 +01002143 self.getClientSpec()
2144
Han-Wen Nienhuys6a49f8e2007-05-23 18:49:35 -03002145 # TODO: should always look at previous commits,
2146 # merge with previous imports, if possible.
2147 if args == []:
Simon Hausmannd414c742007-05-25 11:36:42 +02002148 if self.hasOrigin:
Simon Hausmann5ca44612007-08-24 17:44:16 +02002149 createOrUpdateBranchesFromOrigin(self.refPrefix, self.silent)
Simon Hausmannabcd7902007-05-24 22:25:36 +02002150 self.listExistingP4GitBranches()
2151
2152 if len(self.p4BranchesInGit) > 1:
2153 if not self.silent:
2154 print "Importing from/into multiple branches"
2155 self.detectBranches = True
Simon Hausmann967f72e2007-03-23 09:30:41 +01002156
Simon Hausmann29bdbac2007-05-19 10:23:12 +02002157 if self.verbose:
2158 print "branches: %s" % self.p4BranchesInGit
2159
2160 p4Change = 0
2161 for branch in self.p4BranchesInGit:
Han-Wen Nienhuyscebdf5a2007-05-23 16:53:11 -03002162 logMsg = extractLogMessageFromGitCommit(self.refPrefix + branch)
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03002163
2164 settings = extractSettingsGitLog(logMsg)
Simon Hausmann29bdbac2007-05-19 10:23:12 +02002165
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03002166 self.readOptions(settings)
2167 if (settings.has_key('depot-paths')
2168 and settings.has_key ('change')):
2169 change = int(settings['change']) + 1
Simon Hausmann29bdbac2007-05-19 10:23:12 +02002170 p4Change = max(p4Change, change)
2171
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03002172 depotPaths = sorted(settings['depot-paths'])
2173 if self.previousDepotPaths == []:
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03002174 self.previousDepotPaths = depotPaths
Simon Hausmann29bdbac2007-05-19 10:23:12 +02002175 else:
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03002176 paths = []
2177 for (prev, cur) in zip(self.previousDepotPaths, depotPaths):
Vitor Antunes04d277b2011-08-19 00:44:03 +01002178 prev_list = prev.split("/")
2179 cur_list = cur.split("/")
2180 for i in range(0, min(len(cur_list), len(prev_list))):
2181 if cur_list[i] <> prev_list[i]:
Simon Hausmann583e1702007-06-07 09:37:13 +02002182 i = i - 1
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03002183 break
2184
Vitor Antunes04d277b2011-08-19 00:44:03 +01002185 paths.append ("/".join(cur_list[:i + 1]))
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03002186
2187 self.previousDepotPaths = paths
Simon Hausmann29bdbac2007-05-19 10:23:12 +02002188
2189 if p4Change > 0:
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03002190 self.depotPaths = sorted(self.previousDepotPaths)
Simon Hausmannd5904672007-05-19 11:07:32 +02002191 self.changeRange = "@%s,#head" % p4Change
Simon Hausmann330f53b2007-06-07 09:39:51 +02002192 if not self.detectBranches:
2193 self.initialParent = parseRevision(self.branch)
Simon Hausmann341dc1c2007-05-21 00:39:16 +02002194 if not self.silent and not self.detectBranches:
Simon Hausmann967f72e2007-03-23 09:30:41 +01002195 print "Performing incremental import into %s git branch" % self.branch
Simon Hausmann569d1bd2007-03-22 21:34:16 +01002196
Simon Hausmannf9162f62007-05-17 09:02:45 +02002197 if not self.branch.startswith("refs/"):
2198 self.branch = "refs/heads/" + self.branch
Simon Hausmann179caeb2007-03-22 22:17:42 +01002199
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03002200 if len(args) == 0 and self.depotPaths:
Simon Hausmannb9847332007-03-20 20:54:23 +01002201 if not self.silent:
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03002202 print "Depot paths: %s" % ' '.join(self.depotPaths)
Simon Hausmannb9847332007-03-20 20:54:23 +01002203 else:
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03002204 if self.depotPaths and self.depotPaths != args:
Han-Wen Nienhuyscebdf5a2007-05-23 16:53:11 -03002205 print ("previous import used depot path %s and now %s was specified. "
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03002206 "This doesn't work!" % (' '.join (self.depotPaths),
2207 ' '.join (args)))
Simon Hausmannb9847332007-03-20 20:54:23 +01002208 sys.exit(1)
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03002209
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03002210 self.depotPaths = sorted(args)
Simon Hausmannb9847332007-03-20 20:54:23 +01002211
Simon Hausmann1c49fc12007-08-26 16:04:34 +02002212 revision = ""
Simon Hausmannb9847332007-03-20 20:54:23 +01002213 self.users = {}
Simon Hausmannb9847332007-03-20 20:54:23 +01002214
Pete Wyckoff58c8bc72011-12-24 21:07:35 -05002215 # Make sure no revision specifiers are used when --changesfile
2216 # is specified.
2217 bad_changesfile = False
2218 if len(self.changesFile) > 0:
2219 for p in self.depotPaths:
2220 if p.find("@") >= 0 or p.find("#") >= 0:
2221 bad_changesfile = True
2222 break
2223 if bad_changesfile:
2224 die("Option --changesfile is incompatible with revision specifiers")
2225
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03002226 newPaths = []
2227 for p in self.depotPaths:
2228 if p.find("@") != -1:
2229 atIdx = p.index("@")
2230 self.changeRange = p[atIdx:]
2231 if self.changeRange == "@all":
2232 self.changeRange = ""
Han-Wen Nienhuys6a49f8e2007-05-23 18:49:35 -03002233 elif ',' not in self.changeRange:
Simon Hausmann1c49fc12007-08-26 16:04:34 +02002234 revision = self.changeRange
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03002235 self.changeRange = ""
Han-Wen Nienhuys7fcff9d2007-07-23 15:56:37 -07002236 p = p[:atIdx]
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03002237 elif p.find("#") != -1:
2238 hashIdx = p.index("#")
Simon Hausmann1c49fc12007-08-26 16:04:34 +02002239 revision = p[hashIdx:]
Han-Wen Nienhuys7fcff9d2007-07-23 15:56:37 -07002240 p = p[:hashIdx]
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03002241 elif self.previousDepotPaths == []:
Pete Wyckoff58c8bc72011-12-24 21:07:35 -05002242 # pay attention to changesfile, if given, else import
2243 # the entire p4 tree at the head revision
2244 if len(self.changesFile) == 0:
2245 revision = "#head"
Simon Hausmannb9847332007-03-20 20:54:23 +01002246
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03002247 p = re.sub ("\.\.\.$", "", p)
2248 if not p.endswith("/"):
2249 p += "/"
2250
2251 newPaths.append(p)
2252
2253 self.depotPaths = newPaths
2254
Simon Hausmannb9847332007-03-20 20:54:23 +01002255
Simon Hausmannb607e712007-05-20 10:55:54 +02002256 self.loadUserMapFromCache()
Simon Hausmanncb53e1f2007-04-08 00:12:02 +02002257 self.labels = {}
2258 if self.detectLabels:
2259 self.getLabels();
Simon Hausmannb9847332007-03-20 20:54:23 +01002260
Simon Hausmann4b97ffb2007-05-18 21:45:23 +02002261 if self.detectBranches:
Simon Hausmanndf450922007-06-08 08:49:22 +02002262 ## FIXME - what's a P4 projectName ?
2263 self.projectName = self.guessProjectName()
2264
Simon Hausmann38f9f5e2007-11-15 10:38:45 +01002265 if self.hasOrigin:
2266 self.getBranchMappingFromGitBranches()
2267 else:
2268 self.getBranchMapping()
Simon Hausmann29bdbac2007-05-19 10:23:12 +02002269 if self.verbose:
2270 print "p4-git branches: %s" % self.p4BranchesInGit
2271 print "initial parents: %s" % self.initialParents
2272 for b in self.p4BranchesInGit:
2273 if b != "master":
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03002274
2275 ## FIXME
Simon Hausmann29bdbac2007-05-19 10:23:12 +02002276 b = b[len(self.projectName):]
2277 self.createdBranches.add(b)
Simon Hausmann4b97ffb2007-05-18 21:45:23 +02002278
Simon Hausmannf291b4e2007-04-14 11:21:50 +02002279 self.tz = "%+03d%02d" % (- time.timezone / 3600, ((- time.timezone % 3600) / 60))
Simon Hausmannb9847332007-03-20 20:54:23 +01002280
Han-Wen Nienhuyscebdf5a2007-05-23 16:53:11 -03002281 importProcess = subprocess.Popen(["git", "fast-import"],
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03002282 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
2283 stderr=subprocess.PIPE);
Simon Hausmann08483582007-05-15 14:31:06 +02002284 self.gitOutput = importProcess.stdout
2285 self.gitStream = importProcess.stdin
2286 self.gitError = importProcess.stderr
Simon Hausmannb9847332007-03-20 20:54:23 +01002287
Simon Hausmann1c49fc12007-08-26 16:04:34 +02002288 if revision:
Simon Hausmannc208a242007-08-26 16:07:18 +02002289 self.importHeadRevision(revision)
Simon Hausmannb9847332007-03-20 20:54:23 +01002290 else:
2291 changes = []
2292
Simon Hausmann0828ab12007-03-20 20:59:30 +01002293 if len(self.changesFile) > 0:
Simon Hausmannb9847332007-03-20 20:54:23 +01002294 output = open(self.changesFile).readlines()
Reilly Grant1d7367d2009-09-10 00:02:38 -07002295 changeSet = set()
Simon Hausmannb9847332007-03-20 20:54:23 +01002296 for line in output:
2297 changeSet.add(int(line))
2298
2299 for change in changeSet:
2300 changes.append(change)
2301
2302 changes.sort()
2303 else:
Pete Wyckoffaccad8e2011-03-16 16:52:46 -04002304 # catch "git-p4 sync" with no new branches, in a repo that
2305 # does not have any existing git-p4 branches
2306 if len(args) == 0 and not self.p4BranchesInGit:
Pete Wyckoffe32e00d2011-02-19 08:17:59 -05002307 die("No remote p4 branches. Perhaps you never did \"git p4 clone\" in here.");
Simon Hausmann29bdbac2007-05-19 10:23:12 +02002308 if self.verbose:
Han-Wen Nienhuys86dff6b2007-05-23 18:49:35 -03002309 print "Getting p4 changes for %s...%s" % (', '.join(self.depotPaths),
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03002310 self.changeRange)
Simon Hausmann4f6432d2007-08-26 15:56:36 +02002311 changes = p4ChangesForPaths(self.depotPaths, self.changeRange)
Simon Hausmannb9847332007-03-20 20:54:23 +01002312
Simon Hausmann01a9c9c2007-05-23 00:07:35 +02002313 if len(self.maxChanges) > 0:
Han-Wen Nienhuys7fcff9d2007-07-23 15:56:37 -07002314 changes = changes[:min(int(self.maxChanges), len(changes))]
Simon Hausmann01a9c9c2007-05-23 00:07:35 +02002315
Simon Hausmannb9847332007-03-20 20:54:23 +01002316 if len(changes) == 0:
Simon Hausmann0828ab12007-03-20 20:59:30 +01002317 if not self.silent:
Simon Hausmann341dc1c2007-05-21 00:39:16 +02002318 print "No changes to import!"
Simon Hausmann1f52af62007-04-08 00:07:02 +02002319 return True
Simon Hausmannb9847332007-03-20 20:54:23 +01002320
Simon Hausmanna9d1a272007-06-11 23:28:03 +02002321 if not self.silent and not self.detectBranches:
2322 print "Import destination: %s" % self.branch
2323
Simon Hausmann341dc1c2007-05-21 00:39:16 +02002324 self.updatedBranches = set()
2325
Simon Hausmanne87f37a2007-08-26 16:00:52 +02002326 self.importChanges(changes)
Simon Hausmannb9847332007-03-20 20:54:23 +01002327
Simon Hausmann341dc1c2007-05-21 00:39:16 +02002328 if not self.silent:
2329 print ""
2330 if len(self.updatedBranches) > 0:
2331 sys.stdout.write("Updated branches: ")
2332 for b in self.updatedBranches:
2333 sys.stdout.write("%s " % b)
2334 sys.stdout.write("\n")
Simon Hausmannb9847332007-03-20 20:54:23 +01002335
Simon Hausmannb9847332007-03-20 20:54:23 +01002336 self.gitStream.close()
Simon Hausmann29bdbac2007-05-19 10:23:12 +02002337 if importProcess.wait() != 0:
2338 die("fast-import failed: %s" % self.gitError.read())
Simon Hausmannb9847332007-03-20 20:54:23 +01002339 self.gitOutput.close()
2340 self.gitError.close()
2341
Simon Hausmannb9847332007-03-20 20:54:23 +01002342 return True
2343
Simon Hausmann01ce1fe2007-04-07 23:46:50 +02002344class P4Rebase(Command):
2345 def __init__(self):
2346 Command.__init__(self)
Simon Hausmann01265102007-05-25 10:36:10 +02002347 self.options = [ ]
Han-Wen Nienhuyscebdf5a2007-05-23 16:53:11 -03002348 self.description = ("Fetches the latest revision from perforce and "
2349 + "rebases the current work (branch) against it")
Simon Hausmann68c42152007-06-07 12:51:03 +02002350 self.verbose = False
Simon Hausmann01ce1fe2007-04-07 23:46:50 +02002351
2352 def run(self, args):
2353 sync = P4Sync()
2354 sync.run([])
Simon Hausmannd7e38682007-06-12 14:34:46 +02002355
Simon Hausmann14594f42007-08-22 09:07:15 +02002356 return self.rebase()
2357
2358 def rebase(self):
Simon Hausmann36ee4ee2008-01-07 14:21:45 +01002359 if os.system("git update-index --refresh") != 0:
2360 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.");
2361 if len(read_pipe("git diff-index HEAD --")) > 0:
2362 die("You have uncommited changes. Please commit them before rebasing or stash them away with git stash.");
2363
Simon Hausmannd7e38682007-06-12 14:34:46 +02002364 [upstream, settings] = findUpstreamBranchPoint()
2365 if len(upstream) == 0:
2366 die("Cannot find upstream branchpoint for rebase")
2367
2368 # the branchpoint may be p4/foo~3, so strip off the parent
2369 upstream = re.sub("~[0-9]+$", "", upstream)
2370
2371 print "Rebasing the current branch onto %s" % upstream
Han-Wen Nienhuysb25b2062007-05-23 18:49:35 -03002372 oldHead = read_pipe("git rev-parse HEAD").strip()
Simon Hausmannd7e38682007-06-12 14:34:46 +02002373 system("git rebase %s" % upstream)
Simon Hausmann1f52af62007-04-08 00:07:02 +02002374 system("git diff-tree --stat --summary -M %s HEAD" % oldHead)
Simon Hausmann01ce1fe2007-04-07 23:46:50 +02002375 return True
2376
Simon Hausmannf9a3a4f2007-04-08 10:08:26 +02002377class P4Clone(P4Sync):
2378 def __init__(self):
2379 P4Sync.__init__(self)
2380 self.description = "Creates a new git repository and imports from Perforce into it"
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03002381 self.usage = "usage: %prog [options] //depot/path[@revRange]"
Tommy Thorn354081d2008-02-03 10:38:51 -08002382 self.options += [
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03002383 optparse.make_option("--destination", dest="cloneDestination",
2384 action='store', default=None,
Tommy Thorn354081d2008-02-03 10:38:51 -08002385 help="where to leave result of the clone"),
2386 optparse.make_option("-/", dest="cloneExclude",
2387 action="append", type="string",
Pete Wyckoff38200072011-02-19 08:18:01 -05002388 help="exclude depot path"),
2389 optparse.make_option("--bare", dest="cloneBare",
2390 action="store_true", default=False),
Tommy Thorn354081d2008-02-03 10:38:51 -08002391 ]
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03002392 self.cloneDestination = None
Simon Hausmannf9a3a4f2007-04-08 10:08:26 +02002393 self.needsGit = False
Pete Wyckoff38200072011-02-19 08:18:01 -05002394 self.cloneBare = False
Simon Hausmannf9a3a4f2007-04-08 10:08:26 +02002395
Tommy Thorn354081d2008-02-03 10:38:51 -08002396 # This is required for the "append" cloneExclude action
2397 def ensure_value(self, attr, value):
2398 if not hasattr(self, attr) or getattr(self, attr) is None:
2399 setattr(self, attr, value)
2400 return getattr(self, attr)
2401
Han-Wen Nienhuys6a49f8e2007-05-23 18:49:35 -03002402 def defaultDestination(self, args):
2403 ## TODO: use common prefix of args?
2404 depotPath = args[0]
2405 depotDir = re.sub("(@[^@]*)$", "", depotPath)
2406 depotDir = re.sub("(#[^#]*)$", "", depotDir)
Toby Allsopp053d9e42008-02-05 09:41:43 +13002407 depotDir = re.sub(r"\.\.\.$", "", depotDir)
Han-Wen Nienhuys6a49f8e2007-05-23 18:49:35 -03002408 depotDir = re.sub(r"/$", "", depotDir)
2409 return os.path.split(depotDir)[1]
2410
Simon Hausmannf9a3a4f2007-04-08 10:08:26 +02002411 def run(self, args):
2412 if len(args) < 1:
2413 return False
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03002414
2415 if self.keepRepoPath and not self.cloneDestination:
2416 sys.stderr.write("Must specify destination for --keep-path\n")
2417 sys.exit(1)
Simon Hausmannf9a3a4f2007-04-08 10:08:26 +02002418
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03002419 depotPaths = args
Simon Hausmann5e100b52007-06-07 21:12:25 +02002420
2421 if not self.cloneDestination and len(depotPaths) > 1:
2422 self.cloneDestination = depotPaths[-1]
2423 depotPaths = depotPaths[:-1]
2424
Tommy Thorn354081d2008-02-03 10:38:51 -08002425 self.cloneExclude = ["/"+p for p in self.cloneExclude]
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03002426 for p in depotPaths:
2427 if not p.startswith("//"):
2428 return False
Simon Hausmannf9a3a4f2007-04-08 10:08:26 +02002429
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03002430 if not self.cloneDestination:
Marius Storm-Olsen98ad4fa2007-06-07 15:08:33 +02002431 self.cloneDestination = self.defaultDestination(args)
Simon Hausmannf9a3a4f2007-04-08 10:08:26 +02002432
Han-Wen Nienhuys86dff6b2007-05-23 18:49:35 -03002433 print "Importing from %s into %s" % (', '.join(depotPaths), self.cloneDestination)
Pete Wyckoff38200072011-02-19 08:18:01 -05002434
Kevin Greenc3bf3f12007-06-11 16:48:07 -04002435 if not os.path.exists(self.cloneDestination):
2436 os.makedirs(self.cloneDestination)
Robert Blum053fd0c2008-08-01 12:50:03 -07002437 chdir(self.cloneDestination)
Pete Wyckoff38200072011-02-19 08:18:01 -05002438
2439 init_cmd = [ "git", "init" ]
2440 if self.cloneBare:
2441 init_cmd.append("--bare")
2442 subprocess.check_call(init_cmd)
2443
Han-Wen Nienhuys6326aa52007-05-23 18:49:35 -03002444 if not P4Sync.run(self, depotPaths):
Simon Hausmannf9a3a4f2007-04-08 10:08:26 +02002445 return False
Simon Hausmannf9a3a4f2007-04-08 10:08:26 +02002446 if self.branch != "master":
Tor Arvid Lunde9905012008-08-28 00:36:12 +02002447 if self.importIntoRemotes:
2448 masterbranch = "refs/remotes/p4/master"
2449 else:
2450 masterbranch = "refs/heads/p4/master"
2451 if gitBranchExists(masterbranch):
2452 system("git branch master %s" % masterbranch)
Pete Wyckoff38200072011-02-19 08:18:01 -05002453 if not self.cloneBare:
2454 system("git checkout -f")
Simon Hausmann8f9b2e02007-05-18 22:13:26 +02002455 else:
2456 print "Could not detect main branch. No checkout/master branch created."
Han-Wen Nienhuys86dff6b2007-05-23 18:49:35 -03002457
Simon Hausmannf9a3a4f2007-04-08 10:08:26 +02002458 return True
2459
Simon Hausmann09d89de2007-06-20 23:10:28 +02002460class P4Branches(Command):
2461 def __init__(self):
2462 Command.__init__(self)
2463 self.options = [ ]
2464 self.description = ("Shows the git branches that hold imports and their "
2465 + "corresponding perforce depot paths")
2466 self.verbose = False
2467
2468 def run(self, args):
Simon Hausmann5ca44612007-08-24 17:44:16 +02002469 if originP4BranchesExist():
2470 createOrUpdateBranchesFromOrigin()
2471
Simon Hausmann09d89de2007-06-20 23:10:28 +02002472 cmdline = "git rev-parse --symbolic "
2473 cmdline += " --remotes"
2474
2475 for line in read_pipe_lines(cmdline):
2476 line = line.strip()
2477
2478 if not line.startswith('p4/') or line == "p4/HEAD":
2479 continue
2480 branch = line
2481
2482 log = extractLogMessageFromGitCommit("refs/remotes/%s" % branch)
2483 settings = extractSettingsGitLog(log)
2484
2485 print "%s <= %s (%s)" % (branch, ",".join(settings["depot-paths"]), settings["change"])
2486 return True
2487
Simon Hausmannb9847332007-03-20 20:54:23 +01002488class HelpFormatter(optparse.IndentedHelpFormatter):
2489 def __init__(self):
2490 optparse.IndentedHelpFormatter.__init__(self)
2491
2492 def format_description(self, description):
2493 if description:
2494 return description + "\n"
2495 else:
2496 return ""
Simon Hausmann4f5cf762007-03-19 22:25:17 +01002497
Simon Hausmann86949ee2007-03-19 20:59:12 +01002498def printUsage(commands):
2499 print "usage: %s <command> [options]" % sys.argv[0]
2500 print ""
2501 print "valid commands: %s" % ", ".join(commands)
2502 print ""
2503 print "Try %s <command> --help for command specific help." % sys.argv[0]
2504 print ""
2505
2506commands = {
Han-Wen Nienhuysb86f7372007-05-23 18:49:35 -03002507 "debug" : P4Debug,
2508 "submit" : P4Submit,
Marius Storm-Olsena9834f52007-10-09 16:16:09 +02002509 "commit" : P4Submit,
Han-Wen Nienhuysb86f7372007-05-23 18:49:35 -03002510 "sync" : P4Sync,
2511 "rebase" : P4Rebase,
2512 "clone" : P4Clone,
Simon Hausmann09d89de2007-06-20 23:10:28 +02002513 "rollback" : P4RollBack,
2514 "branches" : P4Branches
Simon Hausmann86949ee2007-03-19 20:59:12 +01002515}
2516
Simon Hausmann86949ee2007-03-19 20:59:12 +01002517
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03002518def main():
2519 if len(sys.argv[1:]) == 0:
2520 printUsage(commands.keys())
2521 sys.exit(2)
Simon Hausmann86949ee2007-03-19 20:59:12 +01002522
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03002523 cmd = ""
2524 cmdName = sys.argv[1]
2525 try:
Han-Wen Nienhuysb86f7372007-05-23 18:49:35 -03002526 klass = commands[cmdName]
2527 cmd = klass()
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03002528 except KeyError:
2529 print "unknown command %s" % cmdName
2530 print ""
2531 printUsage(commands.keys())
2532 sys.exit(2)
Simon Hausmann4f5cf762007-03-19 22:25:17 +01002533
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03002534 options = cmd.options
Han-Wen Nienhuysb86f7372007-05-23 18:49:35 -03002535 cmd.gitdir = os.environ.get("GIT_DIR", None)
Simon Hausmann86949ee2007-03-19 20:59:12 +01002536
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03002537 args = sys.argv[2:]
Simon Hausmanne20a9e52007-03-26 00:13:51 +02002538
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03002539 if len(options) > 0:
Pete Wyckoffef868902011-12-24 21:07:32 -05002540 if cmd.needsGit:
2541 options.append(optparse.make_option("--git-dir", dest="gitdir"))
Simon Hausmanne20a9e52007-03-26 00:13:51 +02002542
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03002543 parser = optparse.OptionParser(cmd.usage.replace("%prog", "%prog " + cmdName),
2544 options,
2545 description = cmd.description,
2546 formatter = HelpFormatter())
Simon Hausmann86949ee2007-03-19 20:59:12 +01002547
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03002548 (cmd, args) = parser.parse_args(sys.argv[2:], cmd);
2549 global verbose
2550 verbose = cmd.verbose
2551 if cmd.needsGit:
Han-Wen Nienhuysb86f7372007-05-23 18:49:35 -03002552 if cmd.gitdir == None:
2553 cmd.gitdir = os.path.abspath(".git")
2554 if not isValidGitDir(cmd.gitdir):
2555 cmd.gitdir = read_pipe("git rev-parse --git-dir").strip()
2556 if os.path.exists(cmd.gitdir):
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03002557 cdup = read_pipe("git rev-parse --show-cdup").strip()
2558 if len(cdup) > 0:
Robert Blum053fd0c2008-08-01 12:50:03 -07002559 chdir(cdup);
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03002560
Han-Wen Nienhuysb86f7372007-05-23 18:49:35 -03002561 if not isValidGitDir(cmd.gitdir):
2562 if isValidGitDir(cmd.gitdir + "/.git"):
2563 cmd.gitdir += "/.git"
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03002564 else:
Han-Wen Nienhuysb86f7372007-05-23 18:49:35 -03002565 die("fatal: cannot locate git repository at %s" % cmd.gitdir)
Simon Hausmann8910ac02007-03-26 08:18:55 +02002566
Han-Wen Nienhuysb86f7372007-05-23 18:49:35 -03002567 os.environ["GIT_DIR"] = cmd.gitdir
Simon Hausmann4f5cf762007-03-19 22:25:17 +01002568
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03002569 if not cmd.run(args):
2570 parser.print_help()
Pete Wyckoff09fca772011-12-24 21:07:39 -05002571 sys.exit(2)
Simon Hausmann4f5cf762007-03-19 22:25:17 +01002572
Han-Wen Nienhuysbb6e09b2007-05-23 18:49:35 -03002573
2574if __name__ == '__main__':
2575 main()