git-p4: support single file p4 client view maps
[git/git.git] / contrib / fast-import / git-p4
CommitLineData
86949eef
SH
1#!/usr/bin/env python
2#
3# git-p4.py -- A tool for bidirectional operation between a Perforce depot and git.
4#
c8cbbee9
SH
5# Author: Simon Hausmann <simon@lst.de>
6# Copyright: 2007 Simon Hausmann <simon@lst.de>
83dce55a 7# 2007 Trolltech ASA
86949eef
SH
8# License: MIT <http://www.opensource.org/licenses/mit-license.php>
9#
10
1d7367dc
RG
11import optparse, sys, os, marshal, subprocess, shelve
12import tempfile, getopt, os.path, time, platform
ce6f33c8 13import re
8b41a97f 14
4addad22 15verbose = False
86949eef 16
21a50753
AK
17
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 """
6de040df 25 real_cmd = ["p4"]
abcaf073
AK
26
27 user = gitConfig("git-p4.user")
28 if len(user) > 0:
6de040df 29 real_cmd += ["-u",user]
abcaf073
AK
30
31 password = gitConfig("git-p4.password")
32 if len(password) > 0:
6de040df 33 real_cmd += ["-P", password]
abcaf073
AK
34
35 port = gitConfig("git-p4.port")
36 if len(port) > 0:
6de040df 37 real_cmd += ["-p", port]
abcaf073
AK
38
39 host = gitConfig("git-p4.host")
40 if len(host) > 0:
6de040df 41 real_cmd += ["-h", host]
abcaf073
AK
42
43 client = gitConfig("git-p4.client")
44 if len(client) > 0:
6de040df 45 real_cmd += ["-c", client]
abcaf073 46
6de040df
LD
47
48 if isinstance(cmd,basestring):
49 real_cmd = ' '.join(real_cmd) + ' ' + cmd
50 else:
51 real_cmd += cmd
21a50753
AK
52 return real_cmd
53
053fd0c1 54def chdir(dir):
6de040df 55 # P4 uses the PWD environment variable rather than getcwd(). Since we're
bf1d68ff
GG
56 # not using the shell, we have to set it ourselves. This path could
57 # be relative, so go there first, then figure out where we ended up.
053fd0c1 58 os.chdir(dir)
bf1d68ff 59 os.environ['PWD'] = os.getcwd()
053fd0c1 60
86dff6b6
HWN
61def die(msg):
62 if verbose:
63 raise Exception(msg)
64 else:
65 sys.stderr.write(msg + "\n")
66 sys.exit(1)
67
6de040df 68def write_pipe(c, stdin):
4addad22 69 if verbose:
6de040df 70 sys.stderr.write('Writing pipe: %s\n' % str(c))
b016d397 71
6de040df
LD
72 expand = isinstance(c,basestring)
73 p = subprocess.Popen(c, stdin=subprocess.PIPE, shell=expand)
74 pipe = p.stdin
75 val = pipe.write(stdin)
76 pipe.close()
77 if p.wait():
78 die('Command failed: %s' % str(c))
b016d397
HWN
79
80 return val
81
6de040df 82def p4_write_pipe(c, stdin):
d9429194 83 real_cmd = p4_build_cmd(c)
6de040df 84 return write_pipe(real_cmd, stdin)
d9429194 85
4addad22
HWN
86def read_pipe(c, ignore_error=False):
87 if verbose:
6de040df 88 sys.stderr.write('Reading pipe: %s\n' % str(c))
8b41a97f 89
6de040df
LD
90 expand = isinstance(c,basestring)
91 p = subprocess.Popen(c, stdout=subprocess.PIPE, shell=expand)
92 pipe = p.stdout
b016d397 93 val = pipe.read()
6de040df
LD
94 if p.wait() and not ignore_error:
95 die('Command failed: %s' % str(c))
b016d397
HWN
96
97 return val
98
d9429194
AK
99def p4_read_pipe(c, ignore_error=False):
100 real_cmd = p4_build_cmd(c)
101 return read_pipe(real_cmd, ignore_error)
b016d397 102
bce4c5fc 103def read_pipe_lines(c):
4addad22 104 if verbose:
6de040df
LD
105 sys.stderr.write('Reading pipe: %s\n' % str(c))
106
107 expand = isinstance(c, basestring)
108 p = subprocess.Popen(c, stdout=subprocess.PIPE, shell=expand)
109 pipe = p.stdout
b016d397 110 val = pipe.readlines()
6de040df
LD
111 if pipe.close() or p.wait():
112 die('Command failed: %s' % str(c))
b016d397
HWN
113
114 return val
caace111 115
2318121b
AK
116def p4_read_pipe_lines(c):
117 """Specifically invoke p4 on the command supplied. """
155af834 118 real_cmd = p4_build_cmd(c)
2318121b
AK
119 return read_pipe_lines(real_cmd)
120
6754a299 121def system(cmd):
6de040df 122 expand = isinstance(cmd,basestring)
4addad22 123 if verbose:
6de040df
LD
124 sys.stderr.write("executing %s\n" % str(cmd))
125 subprocess.check_call(cmd, shell=expand)
6754a299 126
bf9320f1
AK
127def p4_system(cmd):
128 """Specifically invoke p4 as the system command. """
155af834 129 real_cmd = p4_build_cmd(cmd)
6de040df
LD
130 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])
bf9320f1 153
9cffb8c8
PW
154#
155# Canonicalize the p4 type and return a tuple of the
156# base type, plus any modifiers. See "p4 help filetypes"
157# for a list and explanation.
158#
159def split_p4_type(p4type):
160
161 p4_filetypes_historical = {
162 "ctempobj": "binary+Sw",
163 "ctext": "text+C",
164 "cxtext": "text+Cx",
165 "ktext": "text+k",
166 "kxtext": "text+kx",
167 "ltext": "text+F",
168 "tempobj": "binary+FSw",
169 "ubinary": "binary+F",
170 "uresource": "resource+F",
171 "uxbinary": "binary+Fx",
172 "xbinary": "binary+x",
173 "xltext": "text+Fx",
174 "xtempobj": "binary+Swx",
175 "xtext": "text+x",
176 "xunicode": "unicode+x",
177 "xutf16": "utf16+x",
178 }
179 if p4type in p4_filetypes_historical:
180 p4type = p4_filetypes_historical[p4type]
181 mods = ""
182 s = p4type.split("+")
183 base = s[0]
184 mods = ""
185 if len(s) > 1:
186 mods = s[1]
187 return (base, mods)
b9fc6ea9 188
b9fc6ea9 189
c65b670e
CP
190def 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
6de040df 203 p4_reopen(p4Type, file)
c65b670e
CP
204
205def getP4OpenedType(file):
206 # Returns the perforce file type for the given file.
207
6de040df 208 result = p4_read_pipe(["opened", file])
f3e5ae4f 209 match = re.match(".*\((.+)\)\r?$", result)
c65b670e
CP
210 if match:
211 return match.group(1)
212 else:
f3e5ae4f 213 die("Could not determine file type for %s (result: '%s')" % (file, result))
c65b670e 214
b43b0a3c
CP
215def 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
c65b670e
CP
255def 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
b932705b 263def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None):
6de040df
LD
264
265 if isinstance(cmd,basestring):
266 cmd = "-G " + cmd
267 expand = True
268 else:
269 cmd = ["-G"] + cmd
270 expand = False
271
272 cmd = p4_build_cmd(cmd)
6a49f8e2 273 if verbose:
6de040df 274 sys.stderr.write("Opening pipe: %s\n" % str(cmd))
9f90c733
SL
275
276 # Use a temporary file to avoid deadlocks without
277 # subprocess.communicate(), which would put another copy
278 # of stdout into memory.
279 stdin_file = None
280 if stdin is not None:
281 stdin_file = tempfile.TemporaryFile(prefix='p4-stdin', mode=stdin_mode)
6de040df
LD
282 if isinstance(stdin,basestring):
283 stdin_file.write(stdin)
284 else:
285 for i in stdin:
286 stdin_file.write(i + '\n')
9f90c733
SL
287 stdin_file.flush()
288 stdin_file.seek(0)
289
6de040df
LD
290 p4 = subprocess.Popen(cmd,
291 shell=expand,
9f90c733
SL
292 stdin=stdin_file,
293 stdout=subprocess.PIPE)
86949eef
SH
294
295 result = []
296 try:
297 while True:
9f90c733 298 entry = marshal.load(p4.stdout)
c3f6163b
AG
299 if cb is not None:
300 cb(entry)
301 else:
302 result.append(entry)
86949eef
SH
303 except EOFError:
304 pass
9f90c733
SL
305 exitCode = p4.wait()
306 if exitCode != 0:
ac3e0d79
SH
307 entry = {}
308 entry["p4ExitCode"] = exitCode
309 result.append(entry)
86949eef
SH
310
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
cb2c9db5
SH
320def p4Where(depotPath):
321 if not depotPath.endswith("/"):
322 depotPath += "/"
7f705dc3 323 depotPath = depotPath + "..."
6de040df 324 outputList = p4CmdList(["where", depotPath])
7f705dc3
TAL
325 output = None
326 for entry in outputList:
75bc9573
TAL
327 if "depotFile" in entry:
328 if entry["depotFile"] == depotPath:
329 output = entry
330 break
331 elif "data" in entry:
332 data = entry.get("data")
333 space = data.find(" ")
334 if data[:space] == depotPath:
335 output = entry
336 break
7f705dc3
TAL
337 if output == None:
338 return ""
dc524036
SH
339 if output["code"] == "error":
340 return ""
cb2c9db5
SH
341 clientPath = ""
342 if "path" in output:
343 clientPath = output.get("path")
344 elif "data" in output:
345 data = output.get("data")
346 lastSpace = data.rfind(" ")
347 clientPath = data[lastSpace + 1:]
348
349 if clientPath.endswith("..."):
350 clientPath = clientPath[:-3]
351 return clientPath
352
86949eef 353def currentGitBranch():
b25b2065 354 return read_pipe("git name-rev HEAD").split(" ")[1].strip()
86949eef 355
4f5cf76a 356def isValidGitDir(path):
bb6e09b2
HWN
357 if (os.path.exists(path + "/HEAD")
358 and os.path.exists(path + "/refs") and os.path.exists(path + "/objects")):
4f5cf76a
SH
359 return True;
360 return False
361
463e8af6 362def parseRevision(ref):
b25b2065 363 return read_pipe("git rev-parse %s" % ref).strip()
463e8af6 364
28755dba
PW
365def branchExists(ref):
366 rev = read_pipe(["git", "rev-parse", "-q", "--verify", ref],
367 ignore_error=True)
368 return len(rev) > 0
369
6ae8de88
SH
370def extractLogMessageFromGitCommit(commit):
371 logMessage = ""
b016d397
HWN
372
373 ## fixme: title is first line of commit, not 1st paragraph.
6ae8de88 374 foundTitle = False
b016d397 375 for log in read_pipe_lines("git cat-file commit %s" % commit):
6ae8de88
SH
376 if not foundTitle:
377 if len(log) == 1:
1c094184 378 foundTitle = True
6ae8de88
SH
379 continue
380
381 logMessage += log
382 return logMessage
383
bb6e09b2 384def extractSettingsGitLog(log):
6ae8de88
SH
385 values = {}
386 for line in log.split("\n"):
387 line = line.strip()
6326aa58
HWN
388 m = re.search (r"^ *\[git-p4: (.*)\]$", line)
389 if not m:
390 continue
391
392 assignments = m.group(1).split (':')
393 for a in assignments:
394 vals = a.split ('=')
395 key = vals[0].strip()
396 val = ('='.join (vals[1:])).strip()
397 if val.endswith ('\"') and val.startswith('"'):
398 val = val[1:-1]
399
400 values[key] = val
401
845b42cb
SH
402 paths = values.get("depot-paths")
403 if not paths:
404 paths = values.get("depot-path")
a3fdd579
SH
405 if paths:
406 values['depot-paths'] = paths.split(',')
bb6e09b2 407 return values
6ae8de88 408
8136a639 409def gitBranchExists(branch):
bb6e09b2
HWN
410 proc = subprocess.Popen(["git", "rev-parse", branch],
411 stderr=subprocess.PIPE, stdout=subprocess.PIPE);
caace111 412 return proc.wait() == 0;
8136a639 413
36bd8446 414_gitConfig = {}
99f790f2 415def gitConfig(key, args = None): # set args to "--bool", for instance
36bd8446 416 if not _gitConfig.has_key(key):
99f790f2
TAL
417 argsFilter = ""
418 if args != None:
419 argsFilter = "%s " % args
420 cmd = "git config %s%s" % (argsFilter, key)
421 _gitConfig[key] = read_pipe(cmd, ignore_error=True).strip()
36bd8446 422 return _gitConfig[key]
01265103 423
7199cf13
VA
424def 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
062410bb
SH
429def 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
9ceab363 452def findUpstreamBranchPoint(head = "HEAD"):
86506fe5
SH
453 branches = p4BranchesInGit()
454 # map from depot-path to branch name
455 branchByDepotPath = {}
456 for branch in branches.keys():
457 tip = branches[branch]
458 log = extractLogMessageFromGitCommit(tip)
459 settings = extractSettingsGitLog(log)
460 if settings.has_key("depot-paths"):
461 paths = ",".join(settings["depot-paths"])
462 branchByDepotPath[paths] = "remotes/p4/" + branch
463
27d2d811 464 settings = None
27d2d811
SH
465 parent = 0
466 while parent < 65535:
9ceab363 467 commit = head + "~%s" % parent
27d2d811
SH
468 log = extractLogMessageFromGitCommit(commit)
469 settings = extractSettingsGitLog(log)
86506fe5
SH
470 if settings.has_key("depot-paths"):
471 paths = ",".join(settings["depot-paths"])
472 if branchByDepotPath.has_key(paths):
473 return [branchByDepotPath[paths], settings]
27d2d811 474
86506fe5 475 parent = parent + 1
27d2d811 476
86506fe5 477 return ["", settings]
27d2d811 478
5ca44617
SH
479def 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
4f6432d8
SH
529def p4ChangesForPaths(depotPaths, changeRange):
530 assert depotPaths
6de040df
LD
531 cmd = ['changes']
532 for p in depotPaths:
533 cmd += ["%s...%s" % (p, changeRange)]
534 output = p4_read_pipe_lines(cmd)
4f6432d8 535
b4b0ba06 536 changes = {}
4f6432d8 537 for line in output:
c3f6163b
AG
538 changeNum = int(line.split(" ")[1])
539 changes[changeNum] = True
4f6432d8 540
b4b0ba06
PW
541 changelist = changes.keys()
542 changelist.sort()
543 return changelist
4f6432d8 544
d53de8b9
TAL
545def 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
b984733c
SH
558class Command:
559 def __init__(self):
560 self.usage = "usage: %prog [options]"
8910ac0e 561 self.needsGit = True
b984733c 562
3ea2cfd4
LD
563class 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
b984733c 604class P4Debug(Command):
86949eef 605 def __init__(self):
6ae8de88 606 Command.__init__(self)
86949eef 607 self.options = [
b1ce9447
HWN
608 optparse.make_option("--verbose", dest="verbose", action="store_true",
609 default=False),
4addad22 610 ]
c8c39116 611 self.description = "A tool to debug the output of p4 -G."
8910ac0e 612 self.needsGit = False
b1ce9447 613 self.verbose = False
86949eef
SH
614
615 def run(self, args):
b1ce9447 616 j = 0
6de040df 617 for output in p4CmdList(args):
b1ce9447
HWN
618 print 'Element: %d' % j
619 j += 1
86949eef 620 print output
b984733c 621 return True
86949eef 622
5834684d
SH
623class P4RollBack(Command):
624 def __init__(self):
625 Command.__init__(self)
626 self.options = [
0c66a783
SH
627 optparse.make_option("--verbose", dest="verbose", action="store_true"),
628 optparse.make_option("--local", dest="rollbackLocalBranches", action="store_true")
5834684d
SH
629 ]
630 self.description = "A tool to debug the multi-branch import. Don't use :)"
52102d47 631 self.verbose = False
0c66a783 632 self.rollbackLocalBranches = False
5834684d
SH
633
634 def run(self, args):
635 if len(args) != 1:
636 return False
637 maxChange = int(args[0])
0c66a783 638
ad192f28 639 if "p4ExitCode" in p4Cmd("changes -m 1"):
66a2f523
SH
640 die("Problems executing p4");
641
0c66a783
SH
642 if self.rollbackLocalBranches:
643 refPrefix = "refs/heads/"
b016d397 644 lines = read_pipe_lines("git rev-parse --symbolic --branches")
0c66a783
SH
645 else:
646 refPrefix = "refs/remotes/"
b016d397 647 lines = read_pipe_lines("git rev-parse --symbolic --remotes")
0c66a783
SH
648
649 for line in lines:
650 if self.rollbackLocalBranches or (line.startswith("p4/") and line != "p4/HEAD\n"):
b25b2065
HWN
651 line = line.strip()
652 ref = refPrefix + line
5834684d 653 log = extractLogMessageFromGitCommit(ref)
bb6e09b2
HWN
654 settings = extractSettingsGitLog(log)
655
656 depotPaths = settings['depot-paths']
657 change = settings['change']
658
5834684d 659 changed = False
52102d47 660
6326aa58
HWN
661 if len(p4Cmd("changes -m 1 " + ' '.join (['%s...@%s' % (p, maxChange)
662 for p in depotPaths]))) == 0:
52102d47
SH
663 print "Branch %s did not exist at change %s, deleting." % (ref, maxChange)
664 system("git update-ref -d %s `git rev-parse %s`" % (ref, ref))
665 continue
666
bb6e09b2 667 while change and int(change) > maxChange:
5834684d 668 changed = True
52102d47
SH
669 if self.verbose:
670 print "%s is at %s ; rewinding towards %s" % (ref, change, maxChange)
5834684d
SH
671 system("git update-ref %s \"%s^\"" % (ref, ref))
672 log = extractLogMessageFromGitCommit(ref)
bb6e09b2
HWN
673 settings = extractSettingsGitLog(log)
674
675
676 depotPaths = settings['depot-paths']
677 change = settings['change']
5834684d
SH
678
679 if changed:
52102d47 680 print "%s rewound to %s" % (ref, change)
5834684d
SH
681
682 return True
683
3ea2cfd4 684class P4Submit(Command, P4UserMap):
4f5cf76a 685 def __init__(self):
b984733c 686 Command.__init__(self)
3ea2cfd4 687 P4UserMap.__init__(self)
4f5cf76a 688 self.options = [
4addad22 689 optparse.make_option("--verbose", dest="verbose", action="store_true"),
4f5cf76a 690 optparse.make_option("--origin", dest="origin"),
ae901090 691 optparse.make_option("-M", dest="detectRenames", action="store_true"),
3ea2cfd4
LD
692 # preserve the user, requires relevant p4 permissions
693 optparse.make_option("--preserve-user", dest="preserveUser", action="store_true"),
4f5cf76a
SH
694 ]
695 self.description = "Submit changes from git to the perforce depot."
c9b50e63 696 self.usage += " [name of git branch to submit into perforce depot]"
4f5cf76a 697 self.interactive = True
9512497b 698 self.origin = ""
ae901090 699 self.detectRenames = False
b0d10df7 700 self.verbose = False
3ea2cfd4 701 self.preserveUser = gitConfig("git-p4.preserveUser").lower() == "true"
f7baba8b 702 self.isWindows = (platform.system() == "Windows")
848de9c3 703 self.myP4UserId = None
4f5cf76a 704
4f5cf76a
SH
705 def check(self):
706 if len(p4CmdList("opened ...")) > 0:
707 die("You have files opened with perforce! Close them before starting the sync.")
708
edae1e2f
SH
709 # replaces everything between 'Description:' and the next P4 submit template field with the
710 # commit message
4f5cf76a
SH
711 def prepareLogMessage(self, template, message):
712 result = ""
713
edae1e2f
SH
714 inDescriptionSection = False
715
4f5cf76a
SH
716 for line in template.split("\n"):
717 if line.startswith("#"):
718 result += line + "\n"
719 continue
720
edae1e2f 721 if inDescriptionSection:
c9dbab04 722 if line.startswith("Files:") or line.startswith("Jobs:"):
edae1e2f
SH
723 inDescriptionSection = False
724 else:
725 continue
726 else:
727 if line.startswith("Description:"):
728 inDescriptionSection = True
729 line += "\n"
730 for messageLine in message.split("\n"):
731 line += "\t" + messageLine + "\n"
732
733 result += line + "\n"
4f5cf76a
SH
734
735 return result
736
3ea2cfd4
LD
737 def p4UserForCommit(self,id):
738 # Return the tuple (perforce user,git email) for a given git commit id
739 self.getUserMapFromPerforceServer()
740 gitEmail = read_pipe("git log --max-count=1 --format='%%ae' %s" % id)
741 gitEmail = gitEmail.strip()
742 if not self.emails.has_key(gitEmail):
743 return (None,gitEmail)
744 else:
745 return (self.emails[gitEmail],gitEmail)
746
747 def checkValidP4Users(self,commits):
748 # check if any git authors cannot be mapped to p4 users
749 for id in commits:
750 (user,email) = self.p4UserForCommit(id)
751 if not user:
752 msg = "Cannot find p4 user for email %s in commit %s." % (email, id)
753 if gitConfig('git-p4.allowMissingP4Users').lower() == "true":
754 print "%s" % msg
755 else:
756 die("Error: %s\nSet git-p4.allowMissingP4Users to true to allow this." % msg)
757
758 def lastP4Changelist(self):
759 # Get back the last changelist number submitted in this client spec. This
760 # then gets used to patch up the username in the change. If the same
761 # client spec is being used by multiple processes then this might go
762 # wrong.
763 results = p4CmdList("client -o") # find the current client
764 client = None
765 for r in results:
766 if r.has_key('Client'):
767 client = r['Client']
768 break
769 if not client:
770 die("could not get client spec")
6de040df 771 results = p4CmdList(["changes", "-c", client, "-m", "1"])
3ea2cfd4
LD
772 for r in results:
773 if r.has_key('change'):
774 return r['change']
775 die("Could not get changelist number for last submit - cannot patch up user details")
776
777 def modifyChangelistUser(self, changelist, newUser):
778 # fixup the user field of a changelist after it has been submitted.
779 changes = p4CmdList("change -o %s" % changelist)
ecdba36d
LD
780 if len(changes) != 1:
781 die("Bad output from p4 change modifying %s to user %s" %
782 (changelist, newUser))
783
784 c = changes[0]
785 if c['User'] == newUser: return # nothing to do
786 c['User'] = newUser
787 input = marshal.dumps(c)
788
3ea2cfd4
LD
789 result = p4CmdList("change -f -i", stdin=input)
790 for r in result:
791 if r.has_key('code'):
792 if r['code'] == 'error':
793 die("Could not modify user field of changelist %s to %s:%s" % (changelist, newUser, r['data']))
794 if r.has_key('data'):
795 print("Updated user field for changelist %s to %s" % (changelist, newUser))
796 return
797 die("Could not modify user field of changelist %s to %s" % (changelist, newUser))
798
799 def canChangeChangelists(self):
800 # check to see if we have p4 admin or super-user permissions, either of
801 # which are required to modify changelists.
ecdba36d 802 results = p4CmdList("protects %s" % self.depotPath)
3ea2cfd4
LD
803 for r in results:
804 if r.has_key('perm'):
805 if r['perm'] == 'admin':
806 return 1
807 if r['perm'] == 'super':
808 return 1
809 return 0
810
848de9c3
LD
811 def p4UserId(self):
812 if self.myP4UserId:
813 return self.myP4UserId
814
815 results = p4CmdList("user -o")
816 for r in results:
817 if r.has_key('User'):
818 self.myP4UserId = r['User']
819 return r['User']
820 die("Could not find your p4 user id")
821
822 def p4UserIsMe(self, p4User):
823 # return True if the given p4 user is actually me
824 me = self.p4UserId()
825 if not p4User or p4User != me:
826 return False
827 else:
828 return True
829
ea99c3ae
SH
830 def prepareSubmitTemplate(self):
831 # remove lines in the Files section that show changes to files outside the depot path we're committing into
832 template = ""
833 inFilesSection = False
6de040df 834 for line in p4_read_pipe_lines(['change', '-o']):
f3e5ae4f
MSO
835 if line.endswith("\r\n"):
836 line = line[:-2] + "\n"
ea99c3ae
SH
837 if inFilesSection:
838 if line.startswith("\t"):
839 # path starts and ends with a tab
840 path = line[1:]
841 lastTab = path.rfind("\t")
842 if lastTab != -1:
843 path = path[:lastTab]
d53de8b9 844 if not p4PathStartsWith(path, self.depotPath):
ea99c3ae
SH
845 continue
846 else:
847 inFilesSection = False
848 else:
849 if line.startswith("Files:"):
850 inFilesSection = True
851
852 template += line
853
854 return template
855
7c766e57
PW
856 def edit_template(self, template_file):
857 """Invoke the editor to let the user change the submission
858 message. Return true if okay to continue with the submit."""
859
860 # if configured to skip the editing part, just submit
861 if gitConfig("git-p4.skipSubmitEdit") == "true":
862 return True
863
864 # look at the modification time, to check later if the user saved
865 # the file
866 mtime = os.stat(template_file).st_mtime
867
868 # invoke the editor
869 if os.environ.has_key("P4EDITOR"):
870 editor = os.environ.get("P4EDITOR")
871 else:
872 editor = read_pipe("git var GIT_EDITOR").strip()
873 system(editor + " " + template_file)
874
875 # If the file was not saved, prompt to see if this patch should
876 # be skipped. But skip this verification step if configured so.
877 if gitConfig("git-p4.skipSubmitEditCheck") == "true":
878 return True
879
d1652049
PW
880 # modification time updated means user saved the file
881 if os.stat(template_file).st_mtime > mtime:
882 return True
883
884 while True:
885 response = raw_input("Submit template unchanged. Submit anyway? [y]es, [n]o (skip this patch) ")
886 if response == 'y':
887 return True
888 if response == 'n':
889 return False
7c766e57 890
7cb5cbef 891 def applyCommit(self, id):
0e36f2d7 892 print "Applying %s" % (read_pipe("git log --max-count=1 --pretty=oneline %s" % id))
ae901090 893
848de9c3 894 (p4User, gitEmail) = self.p4UserForCommit(id)
3ea2cfd4 895
ae901090
VA
896 if not self.detectRenames:
897 # If not explicitly set check the config variable
0a9feffc 898 self.detectRenames = gitConfig("git-p4.detectRenames")
ae901090 899
0a9feffc
VA
900 if self.detectRenames.lower() == "false" or self.detectRenames == "":
901 diffOpts = ""
902 elif self.detectRenames.lower() == "true":
ae901090
VA
903 diffOpts = "-M"
904 else:
0a9feffc 905 diffOpts = "-M%s" % self.detectRenames
ae901090 906
0a9feffc
VA
907 detectCopies = gitConfig("git-p4.detectCopies")
908 if detectCopies.lower() == "true":
4fddb41b 909 diffOpts += " -C"
0a9feffc
VA
910 elif detectCopies != "" and detectCopies.lower() != "false":
911 diffOpts += " -C%s" % detectCopies
4fddb41b 912
68cbcf1b 913 if gitConfig("git-p4.detectCopiesHarder", "--bool") == "true":
4fddb41b
VA
914 diffOpts += " --find-copies-harder"
915
0e36f2d7 916 diff = read_pipe_lines("git diff-tree -r %s \"%s^\" \"%s\"" % (diffOpts, id, id))
4f5cf76a
SH
917 filesToAdd = set()
918 filesToDelete = set()
d336c158 919 editedFiles = set()
c65b670e 920 filesToChangeExecBit = {}
4f5cf76a 921 for line in diff:
b43b0a3c
CP
922 diff = parseDiffTreeEntry(line)
923 modifier = diff['status']
924 path = diff['src']
4f5cf76a 925 if modifier == "M":
6de040df 926 p4_edit(path)
c65b670e
CP
927 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
928 filesToChangeExecBit[path] = diff['dst_mode']
d336c158 929 editedFiles.add(path)
4f5cf76a
SH
930 elif modifier == "A":
931 filesToAdd.add(path)
c65b670e 932 filesToChangeExecBit[path] = diff['dst_mode']
4f5cf76a
SH
933 if path in filesToDelete:
934 filesToDelete.remove(path)
935 elif modifier == "D":
936 filesToDelete.add(path)
937 if path in filesToAdd:
938 filesToAdd.remove(path)
4fddb41b
VA
939 elif modifier == "C":
940 src, dest = diff['src'], diff['dst']
6de040df 941 p4_integrate(src, dest)
4fddb41b 942 if diff['src_sha1'] != diff['dst_sha1']:
6de040df 943 p4_edit(dest)
4fddb41b 944 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
6de040df 945 p4_edit(dest)
4fddb41b
VA
946 filesToChangeExecBit[dest] = diff['dst_mode']
947 os.unlink(dest)
948 editedFiles.add(dest)
d9a5f25b 949 elif modifier == "R":
b43b0a3c 950 src, dest = diff['src'], diff['dst']
6de040df 951 p4_integrate(src, dest)
ae901090 952 if diff['src_sha1'] != diff['dst_sha1']:
6de040df 953 p4_edit(dest)
c65b670e 954 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
6de040df 955 p4_edit(dest)
c65b670e 956 filesToChangeExecBit[dest] = diff['dst_mode']
d9a5f25b
CP
957 os.unlink(dest)
958 editedFiles.add(dest)
959 filesToDelete.add(src)
4f5cf76a
SH
960 else:
961 die("unknown modifier %s for %s" % (modifier, path))
962
0e36f2d7 963 diffcmd = "git format-patch -k --stdout \"%s^\"..\"%s\"" % (id, id)
47a130b7 964 patchcmd = diffcmd + " | git apply "
c1b296b9
SH
965 tryPatchCmd = patchcmd + "--check -"
966 applyPatchCmd = patchcmd + "--check --apply -"
51a2640a 967
47a130b7 968 if os.system(tryPatchCmd) != 0:
51a2640a
SH
969 print "Unfortunately applying the change failed!"
970 print "What do you want to do?"
971 response = "x"
972 while response != "s" and response != "a" and response != "w":
cebdf5af
HWN
973 response = raw_input("[s]kip this patch / [a]pply the patch forcibly "
974 "and with .rej files / [w]rite the patch to a file (patch.txt) ")
51a2640a
SH
975 if response == "s":
976 print "Skipping! Good luck with the next patches..."
20947149 977 for f in editedFiles:
6de040df 978 p4_revert(f)
20947149 979 for f in filesToAdd:
6de040df 980 os.remove(f)
51a2640a
SH
981 return
982 elif response == "a":
47a130b7 983 os.system(applyPatchCmd)
51a2640a
SH
984 if len(filesToAdd) > 0:
985 print "You may also want to call p4 add on the following files:"
986 print " ".join(filesToAdd)
987 if len(filesToDelete):
988 print "The following files should be scheduled for deletion with p4 delete:"
989 print " ".join(filesToDelete)
cebdf5af
HWN
990 die("Please resolve and submit the conflict manually and "
991 + "continue afterwards with git-p4 submit --continue")
51a2640a
SH
992 elif response == "w":
993 system(diffcmd + " > patch.txt")
994 print "Patch saved to patch.txt in %s !" % self.clientPath
cebdf5af
HWN
995 die("Please resolve and submit the conflict manually and "
996 "continue afterwards with git-p4 submit --continue")
51a2640a 997
47a130b7 998 system(applyPatchCmd)
4f5cf76a
SH
999
1000 for f in filesToAdd:
6de040df 1001 p4_add(f)
4f5cf76a 1002 for f in filesToDelete:
6de040df
LD
1003 p4_revert(f)
1004 p4_delete(f)
4f5cf76a 1005
c65b670e
CP
1006 # Set/clear executable bits
1007 for f in filesToChangeExecBit.keys():
1008 mode = filesToChangeExecBit[f]
1009 setP4ExecBit(f, mode)
1010
0e36f2d7 1011 logMessage = extractLogMessageFromGitCommit(id)
0e36f2d7 1012 logMessage = logMessage.strip()
4f5cf76a 1013
ea99c3ae 1014 template = self.prepareSubmitTemplate()
4f5cf76a
SH
1015
1016 if self.interactive:
1017 submitTemplate = self.prepareLogMessage(template, logMessage)
ecdba36d
LD
1018
1019 if self.preserveUser:
1020 submitTemplate = submitTemplate + ("\n######## Actual user %s, modified after commit\n" % p4User)
1021
67abd417
SB
1022 if os.environ.has_key("P4DIFF"):
1023 del(os.environ["P4DIFF"])
8b130262
AW
1024 diff = ""
1025 for editedFile in editedFiles:
6de040df 1026 diff += p4_read_pipe(['diff', '-du', editedFile])
4f5cf76a 1027
f3e5ae4f 1028 newdiff = ""
4f5cf76a 1029 for newFile in filesToAdd:
f3e5ae4f
MSO
1030 newdiff += "==== new file ====\n"
1031 newdiff += "--- /dev/null\n"
1032 newdiff += "+++ %s\n" % newFile
4f5cf76a
SH
1033 f = open(newFile, "r")
1034 for line in f.readlines():
f3e5ae4f 1035 newdiff += "+" + line
4f5cf76a
SH
1036 f.close()
1037
848de9c3
LD
1038 if self.checkAuthorship and not self.p4UserIsMe(p4User):
1039 submitTemplate += "######## git author %s does not match your p4 account.\n" % gitEmail
1040 submitTemplate += "######## Use git-p4 option --preserve-user to modify authorship\n"
1041 submitTemplate += "######## Use git-p4 config git-p4.skipUserNameCheck hides this message.\n"
1042
f3e5ae4f 1043 separatorLine = "######## everything below this line is just the diff #######\n"
4f5cf76a 1044
7c766e57 1045 (handle, fileName) = tempfile.mkstemp()
e96e400f 1046 tmpFile = os.fdopen(handle, "w+")
f3e5ae4f
MSO
1047 if self.isWindows:
1048 submitTemplate = submitTemplate.replace("\n", "\r\n")
1049 separatorLine = separatorLine.replace("\n", "\r\n")
1050 newdiff = newdiff.replace("\n", "\r\n")
1051 tmpFile.write(submitTemplate + separatorLine + diff + newdiff)
e96e400f 1052 tmpFile.close()
e96e400f 1053
7c766e57
PW
1054 if self.edit_template(fileName):
1055 # read the edited message and submit
cdc7e388
SH
1056 tmpFile = open(fileName, "rb")
1057 message = tmpFile.read()
1058 tmpFile.close()
1059 submitTemplate = message[:message.index(separatorLine)]
1060 if self.isWindows:
1061 submitTemplate = submitTemplate.replace("\r\n", "\n")
6de040df 1062 p4_write_pipe(['submit', '-i'], submitTemplate)
3ea2cfd4
LD
1063
1064 if self.preserveUser:
1065 if p4User:
1066 # Get last changelist number. Cannot easily get it from
7c766e57
PW
1067 # the submit command output as the output is
1068 # unmarshalled.
3ea2cfd4
LD
1069 changelist = self.lastP4Changelist()
1070 self.modifyChangelistUser(changelist, p4User)
cdc7e388 1071 else:
7c766e57 1072 # skip this patch
d1652049 1073 print "Submission cancelled, undoing p4 changes."
cdc7e388 1074 for f in editedFiles:
6de040df 1075 p4_revert(f)
cdc7e388 1076 for f in filesToAdd:
6de040df
LD
1077 p4_revert(f)
1078 os.remove(f)
cdc7e388
SH
1079
1080 os.remove(fileName)
4f5cf76a
SH
1081 else:
1082 fileName = "submit.txt"
1083 file = open(fileName, "w+")
1084 file.write(self.prepareLogMessage(template, logMessage))
1085 file.close()
cebdf5af
HWN
1086 print ("Perforce submit template written as %s. "
1087 + "Please review/edit and then use p4 submit -i < %s to submit directly!"
1088 % (fileName, fileName))
4f5cf76a
SH
1089
1090 def run(self, args):
c9b50e63
SH
1091 if len(args) == 0:
1092 self.master = currentGitBranch()
4280e533 1093 if len(self.master) == 0 or not gitBranchExists("refs/heads/%s" % self.master):
c9b50e63
SH
1094 die("Detecting current git branch failed!")
1095 elif len(args) == 1:
1096 self.master = args[0]
28755dba
PW
1097 if not branchExists(self.master):
1098 die("Branch %s does not exist" % self.master)
c9b50e63
SH
1099 else:
1100 return False
1101
4c2d5d72
JX
1102 allowSubmit = gitConfig("git-p4.allowSubmit")
1103 if len(allowSubmit) > 0 and not self.master in allowSubmit.split(","):
1104 die("%s is not in git-p4.allowSubmit" % self.master)
1105
27d2d811 1106 [upstream, settings] = findUpstreamBranchPoint()
ea99c3ae 1107 self.depotPath = settings['depot-paths'][0]
27d2d811
SH
1108 if len(self.origin) == 0:
1109 self.origin = upstream
a3fdd579 1110
3ea2cfd4
LD
1111 if self.preserveUser:
1112 if not self.canChangeChangelists():
1113 die("Cannot preserve user names without p4 super-user or admin permissions")
1114
a3fdd579
SH
1115 if self.verbose:
1116 print "Origin branch is " + self.origin
9512497b 1117
ea99c3ae 1118 if len(self.depotPath) == 0:
9512497b
SH
1119 print "Internal error: cannot locate perforce depot path from existing branches"
1120 sys.exit(128)
1121
ea99c3ae 1122 self.clientPath = p4Where(self.depotPath)
9512497b 1123
51a2640a 1124 if len(self.clientPath) == 0:
ea99c3ae 1125 print "Error: Cannot locate perforce checkout of %s in client view" % self.depotPath
9512497b
SH
1126 sys.exit(128)
1127
ea99c3ae 1128 print "Perforce checkout for depot path %s located at %s" % (self.depotPath, self.clientPath)
7944f142 1129 self.oldWorkingDirectory = os.getcwd()
c1b296b9 1130
0591cfa8
GG
1131 # ensure the clientPath exists
1132 if not os.path.exists(self.clientPath):
1133 os.makedirs(self.clientPath)
1134
053fd0c1 1135 chdir(self.clientPath)
6a01298a 1136 print "Synchronizing p4 checkout..."
6de040df 1137 p4_sync("...")
4f5cf76a 1138 self.check()
4f5cf76a 1139
4c750c0d
SH
1140 commits = []
1141 for line in read_pipe_lines("git rev-list --no-merges %s..%s" % (self.origin, self.master)):
1142 commits.append(line.strip())
1143 commits.reverse()
4f5cf76a 1144
848de9c3
LD
1145 if self.preserveUser or (gitConfig("git-p4.skipUserNameCheck") == "true"):
1146 self.checkAuthorship = False
1147 else:
1148 self.checkAuthorship = True
1149
3ea2cfd4
LD
1150 if self.preserveUser:
1151 self.checkValidP4Users(commits)
1152
4f5cf76a 1153 while len(commits) > 0:
4f5cf76a
SH
1154 commit = commits[0]
1155 commits = commits[1:]
7cb5cbef 1156 self.applyCommit(commit)
4f5cf76a
SH
1157 if not self.interactive:
1158 break
1159
4f5cf76a 1160 if len(commits) == 0:
4c750c0d 1161 print "All changes applied!"
053fd0c1 1162 chdir(self.oldWorkingDirectory)
14594f4b 1163
4c750c0d
SH
1164 sync = P4Sync()
1165 sync.run([])
14594f4b 1166
4c750c0d
SH
1167 rebase = P4Rebase()
1168 rebase.rebase()
4f5cf76a 1169
b984733c
SH
1170 return True
1171
3ea2cfd4 1172class P4Sync(Command, P4UserMap):
56c09345
PW
1173 delete_actions = ( "delete", "move/delete", "purge" )
1174
b984733c
SH
1175 def __init__(self):
1176 Command.__init__(self)
3ea2cfd4 1177 P4UserMap.__init__(self)
b984733c
SH
1178 self.options = [
1179 optparse.make_option("--branch", dest="branch"),
1180 optparse.make_option("--detect-branches", dest="detectBranches", action="store_true"),
1181 optparse.make_option("--changesfile", dest="changesFile"),
1182 optparse.make_option("--silent", dest="silent", action="store_true"),
ef48f909 1183 optparse.make_option("--detect-labels", dest="detectLabels", action="store_true"),
a028a98e 1184 optparse.make_option("--verbose", dest="verbose", action="store_true"),
d2c6dd30
HWN
1185 optparse.make_option("--import-local", dest="importIntoRemotes", action="store_false",
1186 help="Import into refs/heads/ , not refs/remotes"),
8b41a97f 1187 optparse.make_option("--max-changes", dest="maxChanges"),
86dff6b6 1188 optparse.make_option("--keep-path", dest="keepRepoPath", action='store_true',
3a70cdfa
TAL
1189 help="Keep entire BRANCH/DIR/SUBDIR prefix during import"),
1190 optparse.make_option("--use-client-spec", dest="useClientSpec", action='store_true',
1191 help="Only sync files that are included in the Perforce Client Spec")
b984733c
SH
1192 ]
1193 self.description = """Imports from Perforce into a git repository.\n
1194 example:
1195 //depot/my/project/ -- to import the current head
1196 //depot/my/project/@all -- to import everything
1197 //depot/my/project/@1,6 -- to import only from revision 1 to 6
1198
1199 (a ... is not needed in the path p4 specification, it's added implicitly)"""
1200
1201 self.usage += " //depot/path[@revRange]"
b984733c 1202 self.silent = False
1d7367dc
RG
1203 self.createdBranches = set()
1204 self.committedChanges = set()
569d1bd4 1205 self.branch = ""
b984733c 1206 self.detectBranches = False
cb53e1f8 1207 self.detectLabels = False
b984733c 1208 self.changesFile = ""
01265103 1209 self.syncWithOrigin = True
4b97ffb1 1210 self.verbose = False
a028a98e 1211 self.importIntoRemotes = True
01a9c9c5 1212 self.maxChanges = ""
c1f9197f 1213 self.isWindows = (platform.system() == "Windows")
8b41a97f 1214 self.keepRepoPath = False
6326aa58 1215 self.depotPaths = None
3c699645 1216 self.p4BranchesInGit = []
354081d5 1217 self.cloneExclude = []
3a70cdfa
TAL
1218 self.useClientSpec = False
1219 self.clientSpecDirs = []
e3e68643 1220 self.haveSingleFileClientViews = False
b984733c 1221
01265103
SH
1222 if gitConfig("git-p4.syncFromOrigin") == "false":
1223 self.syncWithOrigin = False
1224
084f6306
PW
1225 #
1226 # P4 wildcards are not allowed in filenames. P4 complains
1227 # if you simply add them, but you can force it with "-f", in
1228 # which case it translates them into %xx encoding internally.
1229 # Search for and fix just these four characters. Do % last so
1230 # that fixing it does not inadvertently create new %-escapes.
1231 #
1232 def wildcard_decode(self, path):
1233 # Cannot have * in a filename in windows; untested as to
1234 # what p4 would do in such a case.
1235 if not self.isWindows:
1236 path = path.replace("%2A", "*")
1237 path = path.replace("%23", "#") \
1238 .replace("%40", "@") \
1239 .replace("%25", "%")
1240 return path
1241
b984733c 1242 def extractFilesFromCommit(self, commit):
354081d5
TT
1243 self.cloneExclude = [re.sub(r"\.\.\.$", "", path)
1244 for path in self.cloneExclude]
b984733c
SH
1245 files = []
1246 fnum = 0
1247 while commit.has_key("depotFile%s" % fnum):
1248 path = commit["depotFile%s" % fnum]
6326aa58 1249
354081d5 1250 if [p for p in self.cloneExclude
d53de8b9 1251 if p4PathStartsWith(path, p)]:
354081d5
TT
1252 found = False
1253 else:
1254 found = [p for p in self.depotPaths
d53de8b9 1255 if p4PathStartsWith(path, p)]
6326aa58 1256 if not found:
b984733c
SH
1257 fnum = fnum + 1
1258 continue
1259
1260 file = {}
1261 file["path"] = path
1262 file["rev"] = commit["rev%s" % fnum]
1263 file["action"] = commit["action%s" % fnum]
1264 file["type"] = commit["type%s" % fnum]
1265 files.append(file)
1266 fnum = fnum + 1
1267 return files
1268
6326aa58 1269 def stripRepoPath(self, path, prefixes):
3952710b
IW
1270 if self.useClientSpec:
1271
1272 # if using the client spec, we use the output directory
1273 # specified in the client. For example, a view
1274 # //depot/foo/branch/... //client/branch/foo/...
1275 # will end up putting all foo/branch files into
1276 # branch/foo/
1277 for val in self.clientSpecDirs:
e3e68643
GG
1278 if self.haveSingleFileClientViews and len(path) == abs(val[1][0]) and path == val[0]:
1279 # since 'path' is a depot file path, if it matches the LeftMap,
1280 # then the View is a single file mapping, so use the entire rightMap
1281 # first two tests above avoid the last == test for common cases
1282 path = val[1][1]
1283 # now strip out the client (//client/...)
1284 path = re.sub("^(//[^/]+/)", '', path)
1285 # the rest is local client path
1286 return path
1287
3952710b
IW
1288 if path.startswith(val[0]):
1289 # replace the depot path with the client path
1290 path = path.replace(val[0], val[1][1])
1291 # now strip out the client (//client/...)
1292 path = re.sub("^(//[^/]+/)", '', path)
1293 # the rest is all path
1294 return path
1295
8b41a97f 1296 if self.keepRepoPath:
6326aa58
HWN
1297 prefixes = [re.sub("^(//[^/]+/).*", r'\1', prefixes[0])]
1298
1299 for p in prefixes:
d53de8b9 1300 if p4PathStartsWith(path, p):
6326aa58 1301 path = path[len(p):]
8b41a97f 1302
6326aa58 1303 return path
6754a299 1304
71b112d4 1305 def splitFilesIntoBranches(self, commit):
d5904674 1306 branches = {}
71b112d4
SH
1307 fnum = 0
1308 while commit.has_key("depotFile%s" % fnum):
1309 path = commit["depotFile%s" % fnum]
6326aa58 1310 found = [p for p in self.depotPaths
d53de8b9 1311 if p4PathStartsWith(path, p)]
6326aa58 1312 if not found:
71b112d4
SH
1313 fnum = fnum + 1
1314 continue
1315
1316 file = {}
1317 file["path"] = path
1318 file["rev"] = commit["rev%s" % fnum]
1319 file["action"] = commit["action%s" % fnum]
1320 file["type"] = commit["type%s" % fnum]
1321 fnum = fnum + 1
1322
6326aa58 1323 relPath = self.stripRepoPath(path, self.depotPaths)
b984733c 1324
4b97ffb1 1325 for branch in self.knownBranches.keys():
6754a299
HWN
1326
1327 # add a trailing slash so that a commit into qt/4.2foo doesn't end up in qt/4.2
1328 if relPath.startswith(branch + "/"):
d5904674
SH
1329 if branch not in branches:
1330 branches[branch] = []
71b112d4 1331 branches[branch].append(file)
6555b2cc 1332 break
b984733c
SH
1333
1334 return branches
1335
b932705b
LD
1336 # output one file from the P4 stream
1337 # - helper for streamP4Files
1338
1339 def streamOneP4File(self, file, contents):
b932705b 1340 relPath = self.stripRepoPath(file['depotFile'], self.branchPrefixes)
084f6306 1341 relPath = self.wildcard_decode(relPath)
b932705b
LD
1342 if verbose:
1343 sys.stderr.write("%s\n" % relPath)
1344
9cffb8c8
PW
1345 (type_base, type_mods) = split_p4_type(file["type"])
1346
1347 git_mode = "100644"
1348 if "x" in type_mods:
1349 git_mode = "100755"
1350 if type_base == "symlink":
1351 git_mode = "120000"
1352 # p4 print on a symlink contains "target\n"; remove the newline
b39c3612
EP
1353 data = ''.join(contents)
1354 contents = [data[:-1]]
b932705b 1355
9cffb8c8 1356 if type_base == "utf16":
55aa5714
PW
1357 # p4 delivers different text in the python output to -G
1358 # than it does when using "print -o", or normal p4 client
1359 # operations. utf16 is converted to ascii or utf8, perhaps.
1360 # But ascii text saved as -t utf16 is completely mangled.
1361 # Invoke print -o to get the real contents.
6de040df 1362 text = p4_read_pipe(['print', '-q', '-o', '-', file['depotFile']])
55aa5714
PW
1363 contents = [ text ]
1364
9f7ef0ea
PW
1365 if type_base == "apple":
1366 # Apple filetype files will be streamed as a concatenation of
1367 # its appledouble header and the contents. This is useless
1368 # on both macs and non-macs. If using "print -q -o xx", it
1369 # will create "xx" with the data, and "%xx" with the header.
1370 # This is also not very useful.
1371 #
1372 # Ideally, someday, this script can learn how to generate
1373 # appledouble files directly and import those to git, but
1374 # non-mac machines can never find a use for apple filetype.
1375 print "\nIgnoring apple filetype file %s" % file['depotFile']
1376 return
1377
9cffb8c8
PW
1378 # Perhaps windows wants unicode, utf16 newlines translated too;
1379 # but this is not doing it.
1380 if self.isWindows and type_base == "text":
b932705b
LD
1381 mangled = []
1382 for data in contents:
1383 data = data.replace("\r\n", "\n")
1384 mangled.append(data)
1385 contents = mangled
1386
55aa5714
PW
1387 # Note that we do not try to de-mangle keywords on utf16 files,
1388 # even though in theory somebody may want that.
9cffb8c8
PW
1389 if type_base in ("text", "unicode", "binary"):
1390 if "ko" in type_mods:
cb585a9c
PW
1391 text = ''.join(contents)
1392 text = re.sub(r'\$(Id|Header):[^$]*\$', r'$\1$', text)
1393 contents = [ text ]
9cffb8c8 1394 elif "k" in type_mods:
cb585a9c
PW
1395 text = ''.join(contents)
1396 text = re.sub(r'\$(Id|Header|Author|Date|DateTime|Change|File|Revision):[^$]*\$', r'$\1$', text)
1397 contents = [ text ]
b932705b 1398
9cffb8c8 1399 self.gitStream.write("M %s inline %s\n" % (git_mode, relPath))
b932705b
LD
1400
1401 # total length...
1402 length = 0
1403 for d in contents:
1404 length = length + len(d)
1405
1406 self.gitStream.write("data %d\n" % length)
1407 for d in contents:
1408 self.gitStream.write(d)
1409 self.gitStream.write("\n")
1410
1411 def streamOneP4Deletion(self, file):
1412 relPath = self.stripRepoPath(file['path'], self.branchPrefixes)
1413 if verbose:
1414 sys.stderr.write("delete %s\n" % relPath)
1415 self.gitStream.write("D %s\n" % relPath)
1416
1417 # handle another chunk of streaming data
1418 def streamP4FilesCb(self, marshalled):
1419
c3f6163b
AG
1420 if marshalled.has_key('depotFile') and self.stream_have_file_info:
1421 # start of a new file - output the old one first
1422 self.streamOneP4File(self.stream_file, self.stream_contents)
1423 self.stream_file = {}
1424 self.stream_contents = []
1425 self.stream_have_file_info = False
b932705b 1426
c3f6163b
AG
1427 # pick up the new file information... for the
1428 # 'data' field we need to append to our array
1429 for k in marshalled.keys():
1430 if k == 'data':
1431 self.stream_contents.append(marshalled['data'])
1432 else:
1433 self.stream_file[k] = marshalled[k]
b932705b 1434
c3f6163b 1435 self.stream_have_file_info = True
b932705b
LD
1436
1437 # Stream directly from "p4 files" into "git fast-import"
1438 def streamP4Files(self, files):
30b5940b
SH
1439 filesForCommit = []
1440 filesToRead = []
b932705b 1441 filesToDelete = []
30b5940b 1442
3a70cdfa 1443 for f in files:
30b5940b 1444 includeFile = True
3a70cdfa
TAL
1445 for val in self.clientSpecDirs:
1446 if f['path'].startswith(val[0]):
3952710b 1447 if val[1][0] <= 0:
30b5940b 1448 includeFile = False
3a70cdfa
TAL
1449 break
1450
30b5940b
SH
1451 if includeFile:
1452 filesForCommit.append(f)
56c09345 1453 if f['action'] in self.delete_actions:
b932705b 1454 filesToDelete.append(f)
56c09345
PW
1455 else:
1456 filesToRead.append(f)
6a49f8e2 1457
b932705b
LD
1458 # deleted files...
1459 for f in filesToDelete:
1460 self.streamOneP4Deletion(f)
1b9a4684 1461
b932705b
LD
1462 if len(filesToRead) > 0:
1463 self.stream_file = {}
1464 self.stream_contents = []
1465 self.stream_have_file_info = False
8ff45f2a 1466
c3f6163b
AG
1467 # curry self argument
1468 def streamP4FilesCbSelf(entry):
1469 self.streamP4FilesCb(entry)
6a49f8e2 1470
6de040df
LD
1471 fileArgs = ['%s#%s' % (f['path'], f['rev']) for f in filesToRead]
1472
1473 p4CmdList(["-x", "-", "print"],
1474 stdin=fileArgs,
1475 cb=streamP4FilesCbSelf)
30b5940b 1476
b932705b
LD
1477 # do the last chunk
1478 if self.stream_file.has_key('depotFile'):
1479 self.streamOneP4File(self.stream_file, self.stream_contents)
6a49f8e2 1480
6326aa58 1481 def commit(self, details, files, branch, branchPrefixes, parent = ""):
b984733c
SH
1482 epoch = details["time"]
1483 author = details["user"]
c3f6163b 1484 self.branchPrefixes = branchPrefixes
b984733c 1485
4b97ffb1
SH
1486 if self.verbose:
1487 print "commit into %s" % branch
1488
96e07dd2
HWN
1489 # start with reading files; if that fails, we should not
1490 # create a commit.
1491 new_files = []
1492 for f in files:
d53de8b9 1493 if [p for p in branchPrefixes if p4PathStartsWith(f['path'], p)]:
96e07dd2
HWN
1494 new_files.append (f)
1495 else:
afa1dd9a 1496 sys.stderr.write("Ignoring file outside of prefix: %s\n" % f['path'])
96e07dd2 1497
b984733c 1498 self.gitStream.write("commit %s\n" % branch)
6a49f8e2 1499# gitStream.write("mark :%s\n" % details["change"])
b984733c
SH
1500 self.committedChanges.add(int(details["change"]))
1501 committer = ""
b607e71e
SH
1502 if author not in self.users:
1503 self.getUserMapFromPerforceServer()
b984733c 1504 if author in self.users:
0828ab14 1505 committer = "%s %s %s" % (self.users[author], epoch, self.tz)
b984733c 1506 else:
0828ab14 1507 committer = "%s <a@b> %s %s" % (author, epoch, self.tz)
b984733c
SH
1508
1509 self.gitStream.write("committer %s\n" % committer)
1510
1511 self.gitStream.write("data <<EOT\n")
1512 self.gitStream.write(details["desc"])
6581de09
SH
1513 self.gitStream.write("\n[git-p4: depot-paths = \"%s\": change = %s"
1514 % (','.join (branchPrefixes), details["change"]))
1515 if len(details['options']) > 0:
1516 self.gitStream.write(": options = %s" % details['options'])
1517 self.gitStream.write("]\nEOT\n\n")
b984733c
SH
1518
1519 if len(parent) > 0:
4b97ffb1
SH
1520 if self.verbose:
1521 print "parent %s" % parent
b984733c
SH
1522 self.gitStream.write("from %s\n" % parent)
1523
b932705b 1524 self.streamP4Files(new_files)
b984733c
SH
1525 self.gitStream.write("\n")
1526
1f4ba1cb
SH
1527 change = int(details["change"])
1528
9bda3a85 1529 if self.labels.has_key(change):
1f4ba1cb
SH
1530 label = self.labels[change]
1531 labelDetails = label[0]
1532 labelRevisions = label[1]
71b112d4
SH
1533 if self.verbose:
1534 print "Change %s is labelled %s" % (change, labelDetails)
1f4ba1cb 1535
6de040df
LD
1536 files = p4CmdList(["files"] + ["%s...@%s" % (p, change)
1537 for p in branchPrefixes])
1f4ba1cb
SH
1538
1539 if len(files) == len(labelRevisions):
1540
1541 cleanedFiles = {}
1542 for info in files:
56c09345 1543 if info["action"] in self.delete_actions:
1f4ba1cb
SH
1544 continue
1545 cleanedFiles[info["depotFile"]] = info["rev"]
1546
1547 if cleanedFiles == labelRevisions:
1548 self.gitStream.write("tag tag_%s\n" % labelDetails["label"])
1549 self.gitStream.write("from %s\n" % branch)
1550
1551 owner = labelDetails["Owner"]
1552 tagger = ""
1553 if author in self.users:
1554 tagger = "%s %s %s" % (self.users[owner], epoch, self.tz)
1555 else:
1556 tagger = "%s <a@b> %s %s" % (owner, epoch, self.tz)
1557 self.gitStream.write("tagger %s\n" % tagger)
1558 self.gitStream.write("data <<EOT\n")
1559 self.gitStream.write(labelDetails["Description"])
1560 self.gitStream.write("EOT\n\n")
1561
1562 else:
a46668fa 1563 if not self.silent:
cebdf5af
HWN
1564 print ("Tag %s does not match with change %s: files do not match."
1565 % (labelDetails["label"], change))
1f4ba1cb
SH
1566
1567 else:
a46668fa 1568 if not self.silent:
cebdf5af
HWN
1569 print ("Tag %s does not match with change %s: file count is different."
1570 % (labelDetails["label"], change))
b984733c 1571
1f4ba1cb
SH
1572 def getLabels(self):
1573 self.labels = {}
1574
6326aa58 1575 l = p4CmdList("labels %s..." % ' '.join (self.depotPaths))
10c3211b 1576 if len(l) > 0 and not self.silent:
183f8436 1577 print "Finding files belonging to labels in %s" % `self.depotPaths`
01ce1fe9
SH
1578
1579 for output in l:
1f4ba1cb
SH
1580 label = output["label"]
1581 revisions = {}
1582 newestChange = 0
71b112d4
SH
1583 if self.verbose:
1584 print "Querying files for label %s" % label
6de040df
LD
1585 for file in p4CmdList(["files"] +
1586 ["%s...@%s" % (p, label)
1587 for p in self.depotPaths]):
1f4ba1cb
SH
1588 revisions[file["depotFile"]] = file["rev"]
1589 change = int(file["change"])
1590 if change > newestChange:
1591 newestChange = change
1592
9bda3a85
SH
1593 self.labels[newestChange] = [output, revisions]
1594
1595 if self.verbose:
1596 print "Label changes: %s" % self.labels.keys()
1f4ba1cb 1597
86dff6b6
HWN
1598 def guessProjectName(self):
1599 for p in self.depotPaths:
6e5295c4
SH
1600 if p.endswith("/"):
1601 p = p[:-1]
1602 p = p[p.strip().rfind("/") + 1:]
1603 if not p.endswith("/"):
1604 p += "/"
1605 return p
86dff6b6 1606
4b97ffb1 1607 def getBranchMapping(self):
6555b2cc
SH
1608 lostAndFoundBranches = set()
1609
8ace74c0
VA
1610 user = gitConfig("git-p4.branchUser")
1611 if len(user) > 0:
1612 command = "branches -u %s" % user
1613 else:
1614 command = "branches"
1615
1616 for info in p4CmdList(command):
4b97ffb1
SH
1617 details = p4Cmd("branch -o %s" % info["branch"])
1618 viewIdx = 0
1619 while details.has_key("View%s" % viewIdx):
1620 paths = details["View%s" % viewIdx].split(" ")
1621 viewIdx = viewIdx + 1
1622 # require standard //depot/foo/... //depot/bar/... mapping
1623 if len(paths) != 2 or not paths[0].endswith("/...") or not paths[1].endswith("/..."):
1624 continue
1625 source = paths[0]
1626 destination = paths[1]
6509e19c 1627 ## HACK
d53de8b9 1628 if p4PathStartsWith(source, self.depotPaths[0]) and p4PathStartsWith(destination, self.depotPaths[0]):
6509e19c
SH
1629 source = source[len(self.depotPaths[0]):-4]
1630 destination = destination[len(self.depotPaths[0]):-4]
6555b2cc 1631
1a2edf4e
SH
1632 if destination in self.knownBranches:
1633 if not self.silent:
1634 print "p4 branch %s defines a mapping from %s to %s" % (info["branch"], source, destination)
1635 print "but there exists another mapping from %s to %s already!" % (self.knownBranches[destination], destination)
1636 continue
1637
6555b2cc
SH
1638 self.knownBranches[destination] = source
1639
1640 lostAndFoundBranches.discard(destination)
1641
29bdbac1 1642 if source not in self.knownBranches:
6555b2cc
SH
1643 lostAndFoundBranches.add(source)
1644
7199cf13
VA
1645 # Perforce does not strictly require branches to be defined, so we also
1646 # check git config for a branch list.
1647 #
1648 # Example of branch definition in git config file:
1649 # [git-p4]
1650 # branchList=main:branchA
1651 # branchList=main:branchB
1652 # branchList=branchA:branchC
1653 configBranches = gitConfigList("git-p4.branchList")
1654 for branch in configBranches:
1655 if branch:
1656 (source, destination) = branch.split(":")
1657 self.knownBranches[destination] = source
1658
1659 lostAndFoundBranches.discard(destination)
1660
1661 if source not in self.knownBranches:
1662 lostAndFoundBranches.add(source)
1663
6555b2cc
SH
1664
1665 for branch in lostAndFoundBranches:
1666 self.knownBranches[branch] = branch
29bdbac1 1667
38f9f5ec
SH
1668 def getBranchMappingFromGitBranches(self):
1669 branches = p4BranchesInGit(self.importIntoRemotes)
1670 for branch in branches.keys():
1671 if branch == "master":
1672 branch = "main"
1673 else:
1674 branch = branch[len(self.projectName):]
1675 self.knownBranches[branch] = branch
1676
29bdbac1 1677 def listExistingP4GitBranches(self):
144ff46b
SH
1678 # branches holds mapping from name to commit
1679 branches = p4BranchesInGit(self.importIntoRemotes)
1680 self.p4BranchesInGit = branches.keys()
1681 for branch in branches.keys():
1682 self.initialParents[self.refPrefix + branch] = branches[branch]
4b97ffb1 1683
bb6e09b2
HWN
1684 def updateOptionDict(self, d):
1685 option_keys = {}
1686 if self.keepRepoPath:
1687 option_keys['keepRepoPath'] = 1
1688
1689 d["options"] = ' '.join(sorted(option_keys.keys()))
1690
1691 def readOptions(self, d):
1692 self.keepRepoPath = (d.has_key('options')
1693 and ('keepRepoPath' in d['options']))
6326aa58 1694
8134f69c
SH
1695 def gitRefForBranch(self, branch):
1696 if branch == "main":
1697 return self.refPrefix + "master"
1698
1699 if len(branch) <= 0:
1700 return branch
1701
1702 return self.refPrefix + self.projectName + branch
1703
1ca3d710
SH
1704 def gitCommitByP4Change(self, ref, change):
1705 if self.verbose:
1706 print "looking in ref " + ref + " for change %s using bisect..." % change
1707
1708 earliestCommit = ""
1709 latestCommit = parseRevision(ref)
1710
1711 while True:
1712 if self.verbose:
1713 print "trying: earliest %s latest %s" % (earliestCommit, latestCommit)
1714 next = read_pipe("git rev-list --bisect %s %s" % (latestCommit, earliestCommit)).strip()
1715 if len(next) == 0:
1716 if self.verbose:
1717 print "argh"
1718 return ""
1719 log = extractLogMessageFromGitCommit(next)
1720 settings = extractSettingsGitLog(log)
1721 currentChange = int(settings['change'])
1722 if self.verbose:
1723 print "current change %s" % currentChange
1724
1725 if currentChange == change:
1726 if self.verbose:
1727 print "found %s" % next
1728 return next
1729
1730 if currentChange < change:
1731 earliestCommit = "^%s" % next
1732 else:
1733 latestCommit = "%s" % next
1734
1735 return ""
1736
1737 def importNewBranch(self, branch, maxChange):
1738 # make fast-import flush all changes to disk and update the refs using the checkpoint
1739 # command so that we can try to find the branch parent in the git history
1740 self.gitStream.write("checkpoint\n\n");
1741 self.gitStream.flush();
1742 branchPrefix = self.depotPaths[0] + branch + "/"
1743 range = "@1,%s" % maxChange
1744 #print "prefix" + branchPrefix
1745 changes = p4ChangesForPaths([branchPrefix], range)
1746 if len(changes) <= 0:
1747 return False
1748 firstChange = changes[0]
1749 #print "first change in branch: %s" % firstChange
1750 sourceBranch = self.knownBranches[branch]
1751 sourceDepotPath = self.depotPaths[0] + sourceBranch
1752 sourceRef = self.gitRefForBranch(sourceBranch)
1753 #print "source " + sourceBranch
1754
1755 branchParentChange = int(p4Cmd("changes -m 1 %s...@1,%s" % (sourceDepotPath, firstChange))["change"])
1756 #print "branch parent: %s" % branchParentChange
1757 gitParent = self.gitCommitByP4Change(sourceRef, branchParentChange)
1758 if len(gitParent) > 0:
1759 self.initialParents[self.gitRefForBranch(branch)] = gitParent
1760 #print "parent git commit: %s" % gitParent
1761
1762 self.importChanges(changes)
1763 return True
1764
e87f37ae
SH
1765 def importChanges(self, changes):
1766 cnt = 1
1767 for change in changes:
1768 description = p4Cmd("describe %s" % change)
1769 self.updateOptionDict(description)
1770
1771 if not self.silent:
1772 sys.stdout.write("\rImporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
1773 sys.stdout.flush()
1774 cnt = cnt + 1
1775
1776 try:
1777 if self.detectBranches:
1778 branches = self.splitFilesIntoBranches(description)
1779 for branch in branches.keys():
1780 ## HACK --hwn
1781 branchPrefix = self.depotPaths[0] + branch + "/"
1782
1783 parent = ""
1784
1785 filesForCommit = branches[branch]
1786
1787 if self.verbose:
1788 print "branch is %s" % branch
1789
1790 self.updatedBranches.add(branch)
1791
1792 if branch not in self.createdBranches:
1793 self.createdBranches.add(branch)
1794 parent = self.knownBranches[branch]
1795 if parent == branch:
1796 parent = ""
1ca3d710
SH
1797 else:
1798 fullBranch = self.projectName + branch
1799 if fullBranch not in self.p4BranchesInGit:
1800 if not self.silent:
1801 print("\n Importing new branch %s" % fullBranch);
1802 if self.importNewBranch(branch, change - 1):
1803 parent = ""
1804 self.p4BranchesInGit.append(fullBranch)
1805 if not self.silent:
1806 print("\n Resuming with change %s" % change);
1807
1808 if self.verbose:
1809 print "parent determined through known branches: %s" % parent
e87f37ae 1810
8134f69c
SH
1811 branch = self.gitRefForBranch(branch)
1812 parent = self.gitRefForBranch(parent)
e87f37ae
SH
1813
1814 if self.verbose:
1815 print "looking for initial parent for %s; current parent is %s" % (branch, parent)
1816
1817 if len(parent) == 0 and branch in self.initialParents:
1818 parent = self.initialParents[branch]
1819 del self.initialParents[branch]
1820
1821 self.commit(description, filesForCommit, branch, [branchPrefix], parent)
1822 else:
1823 files = self.extractFilesFromCommit(description)
1824 self.commit(description, files, self.branch, self.depotPaths,
1825 self.initialParent)
1826 self.initialParent = ""
1827 except IOError:
1828 print self.gitError.read()
1829 sys.exit(1)
1830
c208a243
SH
1831 def importHeadRevision(self, revision):
1832 print "Doing initial import of %s from revision %s into %s" % (' '.join(self.depotPaths), revision, self.branch)
1833
4e2e6ce4
PW
1834 details = {}
1835 details["user"] = "git perforce import user"
1494fcbb 1836 details["desc"] = ("Initial import of %s from the state at revision %s\n"
c208a243
SH
1837 % (' '.join(self.depotPaths), revision))
1838 details["change"] = revision
1839 newestRevision = 0
1840
1841 fileCnt = 0
6de040df
LD
1842 fileArgs = ["%s...%s" % (p,revision) for p in self.depotPaths]
1843
1844 for info in p4CmdList(["files"] + fileArgs):
c208a243 1845
68b28593 1846 if 'code' in info and info['code'] == 'error':
c208a243
SH
1847 sys.stderr.write("p4 returned an error: %s\n"
1848 % info['data'])
d88e707f
PW
1849 if info['data'].find("must refer to client") >= 0:
1850 sys.stderr.write("This particular p4 error is misleading.\n")
1851 sys.stderr.write("Perhaps the depot path was misspelled.\n");
1852 sys.stderr.write("Depot path: %s\n" % " ".join(self.depotPaths))
c208a243 1853 sys.exit(1)
68b28593
PW
1854 if 'p4ExitCode' in info:
1855 sys.stderr.write("p4 exitcode: %s\n" % info['p4ExitCode'])
c208a243
SH
1856 sys.exit(1)
1857
1858
1859 change = int(info["change"])
1860 if change > newestRevision:
1861 newestRevision = change
1862
56c09345 1863 if info["action"] in self.delete_actions:
c208a243
SH
1864 # don't increase the file cnt, otherwise details["depotFile123"] will have gaps!
1865 #fileCnt = fileCnt + 1
1866 continue
1867
1868 for prop in ["depotFile", "rev", "action", "type" ]:
1869 details["%s%s" % (prop, fileCnt)] = info[prop]
1870
1871 fileCnt = fileCnt + 1
1872
1873 details["change"] = newestRevision
4e2e6ce4
PW
1874
1875 # Use time from top-most change so that all git-p4 clones of
1876 # the same p4 repo have the same commit SHA1s.
1877 res = p4CmdList("describe -s %d" % newestRevision)
1878 newestTime = None
1879 for r in res:
1880 if r.has_key('time'):
1881 newestTime = int(r['time'])
1882 if newestTime is None:
1883 die("\"describe -s\" on newest change %d did not give a time")
1884 details["time"] = newestTime
1885
c208a243
SH
1886 self.updateOptionDict(details)
1887 try:
1888 self.commit(details, self.extractFilesFromCommit(details), self.branch, self.depotPaths)
1889 except IOError:
1890 print "IO error with git fast-import. Is your git version recent enough?"
1891 print self.gitError.read()
1892
1893
3a70cdfa
TAL
1894 def getClientSpec(self):
1895 specList = p4CmdList( "client -o" )
1896 temp = {}
1897 for entry in specList:
1898 for k,v in entry.iteritems():
1899 if k.startswith("View"):
3952710b
IW
1900
1901 # p4 has these %%1 to %%9 arguments in specs to
1902 # reorder paths; which we can't handle (yet :)
b10048d0 1903 if re.search('%%\d', v) != None:
3952710b
IW
1904 print "Sorry, can't handle %%n arguments in client specs"
1905 sys.exit(1)
b10048d0
GG
1906 if re.search('\*', v) != None:
1907 print "Sorry, can't handle * mappings in client specs"
1908 sys.exit(1)
3952710b 1909
3a70cdfa
TAL
1910 if v.startswith('"'):
1911 start = 1
1912 else:
1913 start = 0
1914 index = v.find("...")
3952710b
IW
1915
1916 # save the "client view"; i.e the RHS of the view
1917 # line that tells the client where to put the
1918 # files for this view.
3952710b 1919
e3e68643
GG
1920 # check for individual file mappings - those which have no '...'
1921 if index < 0 :
1922 v,cv = v.strip().split()
1923 v = v[start:]
1924 self.haveSingleFileClientViews = True
1925 else:
1926 cv = v[index+3:].strip() # +3 to remove previous '...'
1927 cv=cv[:-3]
1928 v = v[start:index]
3952710b
IW
1929
1930 # now save the view; +index means included, -index
1931 # means it should be filtered out.
3a70cdfa
TAL
1932 if v.startswith("-"):
1933 v = v[1:]
3952710b 1934 include = -len(v)
3a70cdfa 1935 else:
3952710b
IW
1936 include = len(v)
1937
df5ed907
GG
1938 # store the View #number for sorting
1939 # and the View string itself (this last for documentation)
1940 temp[v] = (include, cv, int(k[4:]),k)
3952710b 1941
3a70cdfa 1942 self.clientSpecDirs = temp.items()
df5ed907
GG
1943 # Perforce ViewNN with higher #numbers override those with lower
1944 # reverse sort on the View #number
1945 self.clientSpecDirs.sort( lambda x, y: y[1][2] - x[1][2] )
1946 if self.verbose:
1947 for val in self.clientSpecDirs:
1948 print "clientSpecDirs: %s %s" % (val[0],val[1])
3a70cdfa 1949
b984733c 1950 def run(self, args):
6326aa58 1951 self.depotPaths = []
179caebf
SH
1952 self.changeRange = ""
1953 self.initialParent = ""
6326aa58 1954 self.previousDepotPaths = []
ce6f33c8 1955
29bdbac1
SH
1956 # map from branch depot path to parent branch
1957 self.knownBranches = {}
1958 self.initialParents = {}
5ca44617 1959 self.hasOrigin = originP4BranchesExist()
a43ff00c
SH
1960 if not self.syncWithOrigin:
1961 self.hasOrigin = False
29bdbac1 1962
a028a98e
SH
1963 if self.importIntoRemotes:
1964 self.refPrefix = "refs/remotes/p4/"
1965 else:
db775559 1966 self.refPrefix = "refs/heads/p4/"
a028a98e 1967
cebdf5af
HWN
1968 if self.syncWithOrigin and self.hasOrigin:
1969 if not self.silent:
1970 print "Syncing with origin first by calling git fetch origin"
1971 system("git fetch origin")
10f880f8 1972
569d1bd4 1973 if len(self.branch) == 0:
db775559 1974 self.branch = self.refPrefix + "master"
a028a98e 1975 if gitBranchExists("refs/heads/p4") and self.importIntoRemotes:
48df6fd8 1976 system("git update-ref %s refs/heads/p4" % self.branch)
48df6fd8 1977 system("git branch -D p4");
faf1bd20 1978 # create it /after/ importing, when master exists
0058a33a 1979 if not gitBranchExists(self.refPrefix + "HEAD") and self.importIntoRemotes and gitBranchExists(self.branch):
a3c55c09 1980 system("git symbolic-ref %sHEAD %s" % (self.refPrefix, self.branch))
967f72e2 1981
09fca77b
PW
1982 if not self.useClientSpec:
1983 if gitConfig("git-p4.useclientspec", "--bool") == "true":
1984 self.useClientSpec = True
1985 if self.useClientSpec:
3a70cdfa
TAL
1986 self.getClientSpec()
1987
6a49f8e2
HWN
1988 # TODO: should always look at previous commits,
1989 # merge with previous imports, if possible.
1990 if args == []:
d414c74a 1991 if self.hasOrigin:
5ca44617 1992 createOrUpdateBranchesFromOrigin(self.refPrefix, self.silent)
abcd790f
SH
1993 self.listExistingP4GitBranches()
1994
1995 if len(self.p4BranchesInGit) > 1:
1996 if not self.silent:
1997 print "Importing from/into multiple branches"
1998 self.detectBranches = True
967f72e2 1999
29bdbac1
SH
2000 if self.verbose:
2001 print "branches: %s" % self.p4BranchesInGit
2002
2003 p4Change = 0
2004 for branch in self.p4BranchesInGit:
cebdf5af 2005 logMsg = extractLogMessageFromGitCommit(self.refPrefix + branch)
bb6e09b2
HWN
2006
2007 settings = extractSettingsGitLog(logMsg)
29bdbac1 2008
bb6e09b2
HWN
2009 self.readOptions(settings)
2010 if (settings.has_key('depot-paths')
2011 and settings.has_key ('change')):
2012 change = int(settings['change']) + 1
29bdbac1
SH
2013 p4Change = max(p4Change, change)
2014
bb6e09b2
HWN
2015 depotPaths = sorted(settings['depot-paths'])
2016 if self.previousDepotPaths == []:
6326aa58 2017 self.previousDepotPaths = depotPaths
29bdbac1 2018 else:
6326aa58
HWN
2019 paths = []
2020 for (prev, cur) in zip(self.previousDepotPaths, depotPaths):
04d277b3
VA
2021 prev_list = prev.split("/")
2022 cur_list = cur.split("/")
2023 for i in range(0, min(len(cur_list), len(prev_list))):
2024 if cur_list[i] <> prev_list[i]:
583e1707 2025 i = i - 1
6326aa58
HWN
2026 break
2027
04d277b3 2028 paths.append ("/".join(cur_list[:i + 1]))
6326aa58
HWN
2029
2030 self.previousDepotPaths = paths
29bdbac1
SH
2031
2032 if p4Change > 0:
bb6e09b2 2033 self.depotPaths = sorted(self.previousDepotPaths)
d5904674 2034 self.changeRange = "@%s,#head" % p4Change
330f53b8
SH
2035 if not self.detectBranches:
2036 self.initialParent = parseRevision(self.branch)
341dc1c1 2037 if not self.silent and not self.detectBranches:
967f72e2 2038 print "Performing incremental import into %s git branch" % self.branch
569d1bd4 2039
f9162f6a
SH
2040 if not self.branch.startswith("refs/"):
2041 self.branch = "refs/heads/" + self.branch
179caebf 2042
6326aa58 2043 if len(args) == 0 and self.depotPaths:
b984733c 2044 if not self.silent:
6326aa58 2045 print "Depot paths: %s" % ' '.join(self.depotPaths)
b984733c 2046 else:
6326aa58 2047 if self.depotPaths and self.depotPaths != args:
cebdf5af 2048 print ("previous import used depot path %s and now %s was specified. "
6326aa58
HWN
2049 "This doesn't work!" % (' '.join (self.depotPaths),
2050 ' '.join (args)))
b984733c 2051 sys.exit(1)
6326aa58 2052
bb6e09b2 2053 self.depotPaths = sorted(args)
b984733c 2054
1c49fc19 2055 revision = ""
b984733c 2056 self.users = {}
b984733c 2057
58c8bc7c
PW
2058 # Make sure no revision specifiers are used when --changesfile
2059 # is specified.
2060 bad_changesfile = False
2061 if len(self.changesFile) > 0:
2062 for p in self.depotPaths:
2063 if p.find("@") >= 0 or p.find("#") >= 0:
2064 bad_changesfile = True
2065 break
2066 if bad_changesfile:
2067 die("Option --changesfile is incompatible with revision specifiers")
2068
6326aa58
HWN
2069 newPaths = []
2070 for p in self.depotPaths:
2071 if p.find("@") != -1:
2072 atIdx = p.index("@")
2073 self.changeRange = p[atIdx:]
2074 if self.changeRange == "@all":
2075 self.changeRange = ""
6a49f8e2 2076 elif ',' not in self.changeRange:
1c49fc19 2077 revision = self.changeRange
6326aa58 2078 self.changeRange = ""
7fcff9de 2079 p = p[:atIdx]
6326aa58
HWN
2080 elif p.find("#") != -1:
2081 hashIdx = p.index("#")
1c49fc19 2082 revision = p[hashIdx:]
7fcff9de 2083 p = p[:hashIdx]
6326aa58 2084 elif self.previousDepotPaths == []:
58c8bc7c
PW
2085 # pay attention to changesfile, if given, else import
2086 # the entire p4 tree at the head revision
2087 if len(self.changesFile) == 0:
2088 revision = "#head"
6326aa58
HWN
2089
2090 p = re.sub ("\.\.\.$", "", p)
2091 if not p.endswith("/"):
2092 p += "/"
2093
2094 newPaths.append(p)
2095
2096 self.depotPaths = newPaths
2097
b984733c 2098
b607e71e 2099 self.loadUserMapFromCache()
cb53e1f8
SH
2100 self.labels = {}
2101 if self.detectLabels:
2102 self.getLabels();
b984733c 2103
4b97ffb1 2104 if self.detectBranches:
df450923
SH
2105 ## FIXME - what's a P4 projectName ?
2106 self.projectName = self.guessProjectName()
2107
38f9f5ec
SH
2108 if self.hasOrigin:
2109 self.getBranchMappingFromGitBranches()
2110 else:
2111 self.getBranchMapping()
29bdbac1
SH
2112 if self.verbose:
2113 print "p4-git branches: %s" % self.p4BranchesInGit
2114 print "initial parents: %s" % self.initialParents
2115 for b in self.p4BranchesInGit:
2116 if b != "master":
6326aa58
HWN
2117
2118 ## FIXME
29bdbac1
SH
2119 b = b[len(self.projectName):]
2120 self.createdBranches.add(b)
4b97ffb1 2121
f291b4e3 2122 self.tz = "%+03d%02d" % (- time.timezone / 3600, ((- time.timezone % 3600) / 60))
b984733c 2123
cebdf5af 2124 importProcess = subprocess.Popen(["git", "fast-import"],
6326aa58
HWN
2125 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
2126 stderr=subprocess.PIPE);
08483580
SH
2127 self.gitOutput = importProcess.stdout
2128 self.gitStream = importProcess.stdin
2129 self.gitError = importProcess.stderr
b984733c 2130
1c49fc19 2131 if revision:
c208a243 2132 self.importHeadRevision(revision)
b984733c
SH
2133 else:
2134 changes = []
2135
0828ab14 2136 if len(self.changesFile) > 0:
b984733c 2137 output = open(self.changesFile).readlines()
1d7367dc 2138 changeSet = set()
b984733c
SH
2139 for line in output:
2140 changeSet.add(int(line))
2141
2142 for change in changeSet:
2143 changes.append(change)
2144
2145 changes.sort()
2146 else:
accad8e0
PW
2147 # catch "git-p4 sync" with no new branches, in a repo that
2148 # does not have any existing git-p4 branches
2149 if len(args) == 0 and not self.p4BranchesInGit:
e32e00dc 2150 die("No remote p4 branches. Perhaps you never did \"git p4 clone\" in here.");
29bdbac1 2151 if self.verbose:
86dff6b6 2152 print "Getting p4 changes for %s...%s" % (', '.join(self.depotPaths),
6326aa58 2153 self.changeRange)
4f6432d8 2154 changes = p4ChangesForPaths(self.depotPaths, self.changeRange)
b984733c 2155
01a9c9c5 2156 if len(self.maxChanges) > 0:
7fcff9de 2157 changes = changes[:min(int(self.maxChanges), len(changes))]
01a9c9c5 2158
b984733c 2159 if len(changes) == 0:
0828ab14 2160 if not self.silent:
341dc1c1 2161 print "No changes to import!"
1f52af6c 2162 return True
b984733c 2163
a9d1a27a
SH
2164 if not self.silent and not self.detectBranches:
2165 print "Import destination: %s" % self.branch
2166
341dc1c1
SH
2167 self.updatedBranches = set()
2168
e87f37ae 2169 self.importChanges(changes)
b984733c 2170
341dc1c1
SH
2171 if not self.silent:
2172 print ""
2173 if len(self.updatedBranches) > 0:
2174 sys.stdout.write("Updated branches: ")
2175 for b in self.updatedBranches:
2176 sys.stdout.write("%s " % b)
2177 sys.stdout.write("\n")
b984733c 2178
b984733c 2179 self.gitStream.close()
29bdbac1
SH
2180 if importProcess.wait() != 0:
2181 die("fast-import failed: %s" % self.gitError.read())
b984733c
SH
2182 self.gitOutput.close()
2183 self.gitError.close()
2184
b984733c
SH
2185 return True
2186
01ce1fe9
SH
2187class P4Rebase(Command):
2188 def __init__(self):
2189 Command.__init__(self)
01265103 2190 self.options = [ ]
cebdf5af
HWN
2191 self.description = ("Fetches the latest revision from perforce and "
2192 + "rebases the current work (branch) against it")
68c42153 2193 self.verbose = False
01ce1fe9
SH
2194
2195 def run(self, args):
2196 sync = P4Sync()
2197 sync.run([])
d7e3868c 2198
14594f4b
SH
2199 return self.rebase()
2200
2201 def rebase(self):
36ee4ee4
SH
2202 if os.system("git update-index --refresh") != 0:
2203 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.");
2204 if len(read_pipe("git diff-index HEAD --")) > 0:
2205 die("You have uncommited changes. Please commit them before rebasing or stash them away with git stash.");
2206
d7e3868c
SH
2207 [upstream, settings] = findUpstreamBranchPoint()
2208 if len(upstream) == 0:
2209 die("Cannot find upstream branchpoint for rebase")
2210
2211 # the branchpoint may be p4/foo~3, so strip off the parent
2212 upstream = re.sub("~[0-9]+$", "", upstream)
2213
2214 print "Rebasing the current branch onto %s" % upstream
b25b2065 2215 oldHead = read_pipe("git rev-parse HEAD").strip()
d7e3868c 2216 system("git rebase %s" % upstream)
1f52af6c 2217 system("git diff-tree --stat --summary -M %s HEAD" % oldHead)
01ce1fe9
SH
2218 return True
2219
f9a3a4f7
SH
2220class P4Clone(P4Sync):
2221 def __init__(self):
2222 P4Sync.__init__(self)
2223 self.description = "Creates a new git repository and imports from Perforce into it"
bb6e09b2 2224 self.usage = "usage: %prog [options] //depot/path[@revRange]"
354081d5 2225 self.options += [
bb6e09b2
HWN
2226 optparse.make_option("--destination", dest="cloneDestination",
2227 action='store', default=None,
354081d5
TT
2228 help="where to leave result of the clone"),
2229 optparse.make_option("-/", dest="cloneExclude",
2230 action="append", type="string",
38200076
PW
2231 help="exclude depot path"),
2232 optparse.make_option("--bare", dest="cloneBare",
2233 action="store_true", default=False),
354081d5 2234 ]
bb6e09b2 2235 self.cloneDestination = None
f9a3a4f7 2236 self.needsGit = False
38200076 2237 self.cloneBare = False
f9a3a4f7 2238
354081d5
TT
2239 # This is required for the "append" cloneExclude action
2240 def ensure_value(self, attr, value):
2241 if not hasattr(self, attr) or getattr(self, attr) is None:
2242 setattr(self, attr, value)
2243 return getattr(self, attr)
2244
6a49f8e2
HWN
2245 def defaultDestination(self, args):
2246 ## TODO: use common prefix of args?
2247 depotPath = args[0]
2248 depotDir = re.sub("(@[^@]*)$", "", depotPath)
2249 depotDir = re.sub("(#[^#]*)$", "", depotDir)
053d9e43 2250 depotDir = re.sub(r"\.\.\.$", "", depotDir)
6a49f8e2
HWN
2251 depotDir = re.sub(r"/$", "", depotDir)
2252 return os.path.split(depotDir)[1]
2253
f9a3a4f7
SH
2254 def run(self, args):
2255 if len(args) < 1:
2256 return False
bb6e09b2
HWN
2257
2258 if self.keepRepoPath and not self.cloneDestination:
2259 sys.stderr.write("Must specify destination for --keep-path\n")
2260 sys.exit(1)
f9a3a4f7 2261
6326aa58 2262 depotPaths = args
5e100b5c
SH
2263
2264 if not self.cloneDestination and len(depotPaths) > 1:
2265 self.cloneDestination = depotPaths[-1]
2266 depotPaths = depotPaths[:-1]
2267
354081d5 2268 self.cloneExclude = ["/"+p for p in self.cloneExclude]
6326aa58
HWN
2269 for p in depotPaths:
2270 if not p.startswith("//"):
2271 return False
f9a3a4f7 2272
bb6e09b2 2273 if not self.cloneDestination:
98ad4faf 2274 self.cloneDestination = self.defaultDestination(args)
f9a3a4f7 2275
86dff6b6 2276 print "Importing from %s into %s" % (', '.join(depotPaths), self.cloneDestination)
38200076 2277
c3bf3f13
KG
2278 if not os.path.exists(self.cloneDestination):
2279 os.makedirs(self.cloneDestination)
053fd0c1 2280 chdir(self.cloneDestination)
38200076
PW
2281
2282 init_cmd = [ "git", "init" ]
2283 if self.cloneBare:
2284 init_cmd.append("--bare")
2285 subprocess.check_call(init_cmd)
2286
6326aa58 2287 if not P4Sync.run(self, depotPaths):
f9a3a4f7 2288 return False
f9a3a4f7 2289 if self.branch != "master":
e9905013
TAL
2290 if self.importIntoRemotes:
2291 masterbranch = "refs/remotes/p4/master"
2292 else:
2293 masterbranch = "refs/heads/p4/master"
2294 if gitBranchExists(masterbranch):
2295 system("git branch master %s" % masterbranch)
38200076
PW
2296 if not self.cloneBare:
2297 system("git checkout -f")
8f9b2e08
SH
2298 else:
2299 print "Could not detect main branch. No checkout/master branch created."
86dff6b6 2300
f9a3a4f7
SH
2301 return True
2302
09d89de2
SH
2303class P4Branches(Command):
2304 def __init__(self):
2305 Command.__init__(self)
2306 self.options = [ ]
2307 self.description = ("Shows the git branches that hold imports and their "
2308 + "corresponding perforce depot paths")
2309 self.verbose = False
2310
2311 def run(self, args):
5ca44617
SH
2312 if originP4BranchesExist():
2313 createOrUpdateBranchesFromOrigin()
2314
09d89de2
SH
2315 cmdline = "git rev-parse --symbolic "
2316 cmdline += " --remotes"
2317
2318 for line in read_pipe_lines(cmdline):
2319 line = line.strip()
2320
2321 if not line.startswith('p4/') or line == "p4/HEAD":
2322 continue
2323 branch = line
2324
2325 log = extractLogMessageFromGitCommit("refs/remotes/%s" % branch)
2326 settings = extractSettingsGitLog(log)
2327
2328 print "%s <= %s (%s)" % (branch, ",".join(settings["depot-paths"]), settings["change"])
2329 return True
2330
b984733c
SH
2331class HelpFormatter(optparse.IndentedHelpFormatter):
2332 def __init__(self):
2333 optparse.IndentedHelpFormatter.__init__(self)
2334
2335 def format_description(self, description):
2336 if description:
2337 return description + "\n"
2338 else:
2339 return ""
4f5cf76a 2340
86949eef
SH
2341def printUsage(commands):
2342 print "usage: %s <command> [options]" % sys.argv[0]
2343 print ""
2344 print "valid commands: %s" % ", ".join(commands)
2345 print ""
2346 print "Try %s <command> --help for command specific help." % sys.argv[0]
2347 print ""
2348
2349commands = {
b86f7378
HWN
2350 "debug" : P4Debug,
2351 "submit" : P4Submit,
a9834f58 2352 "commit" : P4Submit,
b86f7378
HWN
2353 "sync" : P4Sync,
2354 "rebase" : P4Rebase,
2355 "clone" : P4Clone,
09d89de2
SH
2356 "rollback" : P4RollBack,
2357 "branches" : P4Branches
86949eef
SH
2358}
2359
86949eef 2360
bb6e09b2
HWN
2361def main():
2362 if len(sys.argv[1:]) == 0:
2363 printUsage(commands.keys())
2364 sys.exit(2)
4f5cf76a 2365
bb6e09b2
HWN
2366 cmd = ""
2367 cmdName = sys.argv[1]
2368 try:
b86f7378
HWN
2369 klass = commands[cmdName]
2370 cmd = klass()
bb6e09b2
HWN
2371 except KeyError:
2372 print "unknown command %s" % cmdName
2373 print ""
2374 printUsage(commands.keys())
2375 sys.exit(2)
2376
2377 options = cmd.options
b86f7378 2378 cmd.gitdir = os.environ.get("GIT_DIR", None)
bb6e09b2
HWN
2379
2380 args = sys.argv[2:]
2381
2382 if len(options) > 0:
ef86890c
PW
2383 if cmd.needsGit:
2384 options.append(optparse.make_option("--git-dir", dest="gitdir"))
bb6e09b2
HWN
2385
2386 parser = optparse.OptionParser(cmd.usage.replace("%prog", "%prog " + cmdName),
2387 options,
2388 description = cmd.description,
2389 formatter = HelpFormatter())
2390
2391 (cmd, args) = parser.parse_args(sys.argv[2:], cmd);
2392 global verbose
2393 verbose = cmd.verbose
2394 if cmd.needsGit:
b86f7378
HWN
2395 if cmd.gitdir == None:
2396 cmd.gitdir = os.path.abspath(".git")
2397 if not isValidGitDir(cmd.gitdir):
2398 cmd.gitdir = read_pipe("git rev-parse --git-dir").strip()
2399 if os.path.exists(cmd.gitdir):
bb6e09b2
HWN
2400 cdup = read_pipe("git rev-parse --show-cdup").strip()
2401 if len(cdup) > 0:
053fd0c1 2402 chdir(cdup);
e20a9e53 2403
b86f7378
HWN
2404 if not isValidGitDir(cmd.gitdir):
2405 if isValidGitDir(cmd.gitdir + "/.git"):
2406 cmd.gitdir += "/.git"
bb6e09b2 2407 else:
b86f7378 2408 die("fatal: cannot locate git repository at %s" % cmd.gitdir)
e20a9e53 2409
b86f7378 2410 os.environ["GIT_DIR"] = cmd.gitdir
86949eef 2411
bb6e09b2
HWN
2412 if not cmd.run(args):
2413 parser.print_help()
09fca77b 2414 sys.exit(2)
4f5cf76a 2415
4f5cf76a 2416
bb6e09b2
HWN
2417if __name__ == '__main__':
2418 main()