Make it possible to invoke git-p4 from within subdirectories of a git working tree.
[git/git.git] / contrib / fast-import / git-p4.py
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#
5# Author: Simon Hausmann <hausmann@kde.org>
83dce55a
SH
6# Copyright: 2007 Simon Hausmann <hausmann@kde.org>
7# 2007 Trolltech ASA
86949eef
SH
8# License: MIT <http://www.opensource.org/licenses/mit-license.php>
9#
10
4f5cf76a 11import optparse, sys, os, marshal, popen2, shelve
b984733c
SH
12import tempfile, getopt, sha, os.path, time
13from sets import Set;
4f5cf76a
SH
14
15gitdir = os.environ.get("GIT_DIR", "")
86949eef
SH
16
17def p4CmdList(cmd):
18 cmd = "p4 -G %s" % cmd
19 pipe = os.popen(cmd, "rb")
20
21 result = []
22 try:
23 while True:
24 entry = marshal.load(pipe)
25 result.append(entry)
26 except EOFError:
27 pass
28 pipe.close()
29
30 return result
31
32def p4Cmd(cmd):
33 list = p4CmdList(cmd)
34 result = {}
35 for entry in list:
36 result.update(entry)
37 return result;
38
39def die(msg):
40 sys.stderr.write(msg + "\n")
41 sys.exit(1)
42
43def currentGitBranch():
44 return os.popen("git-name-rev HEAD").read().split(" ")[1][:-1]
45
4f5cf76a
SH
46def isValidGitDir(path):
47 if os.path.exists(path + "/HEAD") and os.path.exists(path + "/refs") and os.path.exists(path + "/objects"):
48 return True;
49 return False
50
51def system(cmd):
52 if os.system(cmd) != 0:
53 die("command failed: %s" % cmd)
54
b984733c
SH
55class Command:
56 def __init__(self):
57 self.usage = "usage: %prog [options]"
58
59class P4Debug(Command):
86949eef
SH
60 def __init__(self):
61 self.options = [
62 ]
c8c39116 63 self.description = "A tool to debug the output of p4 -G."
86949eef
SH
64
65 def run(self, args):
66 for output in p4CmdList(" ".join(args)):
67 print output
b984733c 68 return True
86949eef 69
b984733c 70class P4CleanTags(Command):
86949eef 71 def __init__(self):
b984733c 72 Command.__init__(self)
86949eef
SH
73 self.options = [
74# optparse.make_option("--branch", dest="branch", default="refs/heads/master")
75 ]
c8c39116 76 self.description = "A tool to remove stale unused tags from incremental perforce imports."
86949eef
SH
77 def run(self, args):
78 branch = currentGitBranch()
79 print "Cleaning out stale p4 import tags..."
80 sout, sin, serr = popen2.popen3("git-name-rev --tags `git-rev-parse %s`" % branch)
81 output = sout.read()
82 try:
83 tagIdx = output.index(" tags/p4/")
84 except:
85 print "Cannot find any p4/* tag. Nothing to do."
86 sys.exit(0)
87
88 try:
89 caretIdx = output.index("^")
90 except:
91 caretIdx = len(output) - 1
92 rev = int(output[tagIdx + 9 : caretIdx])
93
94 allTags = os.popen("git tag -l p4/").readlines()
95 for i in range(len(allTags)):
96 allTags[i] = int(allTags[i][3:-1])
97
98 allTags.sort()
99
100 allTags.remove(rev)
101
102 for rev in allTags:
103 print os.popen("git tag -d p4/%s" % rev).read()
104
105 print "%s tags removed." % len(allTags)
b984733c 106 return True
86949eef 107
b984733c 108class P4Sync(Command):
4f5cf76a 109 def __init__(self):
b984733c 110 Command.__init__(self)
4f5cf76a
SH
111 self.options = [
112 optparse.make_option("--continue", action="store_false", dest="firstTime"),
113 optparse.make_option("--origin", dest="origin"),
114 optparse.make_option("--reset", action="store_true", dest="reset"),
115 optparse.make_option("--master", dest="master"),
116 optparse.make_option("--log-substitutions", dest="substFile"),
117 optparse.make_option("--noninteractive", action="store_false"),
04219c04
SH
118 optparse.make_option("--dry-run", action="store_true"),
119 optparse.make_option("--apply-as-patch", action="store_true", dest="applyAsPatch")
4f5cf76a
SH
120 ]
121 self.description = "Submit changes from git to the perforce depot."
122 self.firstTime = True
123 self.reset = False
124 self.interactive = True
125 self.dryRun = False
126 self.substFile = ""
127 self.firstTime = True
128 self.origin = "origin"
129 self.master = ""
1932a6ac 130 self.applyAsPatch = True
4f5cf76a
SH
131
132 self.logSubstitutions = {}
133 self.logSubstitutions["<enter description here>"] = "%log%"
134 self.logSubstitutions["\tDetails:"] = "\tDetails: %log%"
135
136 def check(self):
137 if len(p4CmdList("opened ...")) > 0:
138 die("You have files opened with perforce! Close them before starting the sync.")
139
140 def start(self):
141 if len(self.config) > 0 and not self.reset:
142 die("Cannot start sync. Previous sync config found at %s" % self.configFile)
143
144 commits = []
145 for line in os.popen("git-rev-list --no-merges %s..%s" % (self.origin, self.master)).readlines():
146 commits.append(line[:-1])
147 commits.reverse()
148
149 self.config["commits"] = commits
150
04219c04
SH
151 if not self.applyAsPatch:
152 print "Creating temporary p4-sync branch from %s ..." % self.origin
153 system("git checkout -f -b p4-sync %s" % self.origin)
4f5cf76a
SH
154
155 def prepareLogMessage(self, template, message):
156 result = ""
157
158 for line in template.split("\n"):
159 if line.startswith("#"):
160 result += line + "\n"
161 continue
162
163 substituted = False
164 for key in self.logSubstitutions.keys():
165 if line.find(key) != -1:
166 value = self.logSubstitutions[key]
167 value = value.replace("%log%", message)
168 if value != "@remove@":
169 result += line.replace(key, value) + "\n"
170 substituted = True
171 break
172
173 if not substituted:
174 result += line + "\n"
175
176 return result
177
178 def apply(self, id):
179 print "Applying %s" % (os.popen("git-log --max-count=1 --pretty=oneline %s" % id).read())
180 diff = os.popen("git diff-tree -r --name-status \"%s^\" \"%s\"" % (id, id)).readlines()
181 filesToAdd = set()
182 filesToDelete = set()
183 for line in diff:
184 modifier = line[0]
185 path = line[1:].strip()
186 if modifier == "M":
187 system("p4 edit %s" % path)
188 elif modifier == "A":
189 filesToAdd.add(path)
190 if path in filesToDelete:
191 filesToDelete.remove(path)
192 elif modifier == "D":
193 filesToDelete.add(path)
194 if path in filesToAdd:
195 filesToAdd.remove(path)
196 else:
197 die("unknown modifier %s for %s" % (modifier, path))
198
04219c04 199 if self.applyAsPatch:
5d0b6042 200 system("git-diff-tree -p --diff-filter=ACMRTUXB \"%s^\" \"%s\" | patch -p1" % (id, id))
04219c04
SH
201 else:
202 system("git-diff-files --name-only -z | git-update-index --remove -z --stdin")
203 system("git cherry-pick --no-commit \"%s\"" % id)
4f5cf76a
SH
204
205 for f in filesToAdd:
206 system("p4 add %s" % f)
207 for f in filesToDelete:
208 system("p4 revert %s" % f)
209 system("p4 delete %s" % f)
210
211 logMessage = ""
212 foundTitle = False
213 for log in os.popen("git-cat-file commit %s" % id).readlines():
214 if not foundTitle:
215 if len(log) == 1:
216 foundTitle = 1
217 continue
218
219 if len(logMessage) > 0:
220 logMessage += "\t"
221 logMessage += log
222
223 template = os.popen("p4 change -o").read()
224
225 if self.interactive:
226 submitTemplate = self.prepareLogMessage(template, logMessage)
227 diff = os.popen("p4 diff -du ...").read()
228
229 for newFile in filesToAdd:
230 diff += "==== new file ====\n"
231 diff += "--- /dev/null\n"
232 diff += "+++ %s\n" % newFile
233 f = open(newFile, "r")
234 for line in f.readlines():
235 diff += "+" + line
236 f.close()
237
238 pipe = os.popen("less", "w")
239 pipe.write(submitTemplate + diff)
240 pipe.close()
241
242 response = "e"
243 while response == "e":
244 response = raw_input("Do you want to submit this change (y/e/n)? ")
245 if response == "e":
246 [handle, fileName] = tempfile.mkstemp()
247 tmpFile = os.fdopen(handle, "w+")
248 tmpFile.write(submitTemplate)
249 tmpFile.close()
250 editor = os.environ.get("EDITOR", "vi")
251 system(editor + " " + fileName)
252 tmpFile = open(fileName, "r")
253 submitTemplate = tmpFile.read()
254 tmpFile.close()
255 os.remove(fileName)
256
257 if response == "y" or response == "yes":
258 if self.dryRun:
259 print submitTemplate
260 raw_input("Press return to continue...")
261 else:
262 pipe = os.popen("p4 submit -i", "w")
263 pipe.write(submitTemplate)
264 pipe.close()
265 else:
266 print "Not submitting!"
267 self.interactive = False
268 else:
269 fileName = "submit.txt"
270 file = open(fileName, "w+")
271 file.write(self.prepareLogMessage(template, logMessage))
272 file.close()
273 print "Perforce submit template written as %s. Please review/edit and then use p4 submit -i < %s to submit directly!" % (fileName, fileName)
274
275 def run(self, args):
276 if self.reset:
277 self.firstTime = True
278
279 if len(self.substFile) > 0:
280 for line in open(self.substFile, "r").readlines():
281 tokens = line[:-1].split("=")
282 self.logSubstitutions[tokens[0]] = tokens[1]
283
284 if len(self.master) == 0:
285 self.master = currentGitBranch()
286 if len(self.master) == 0 or not os.path.exists("%s/refs/heads/%s" % (gitdir, self.master)):
287 die("Detecting current git branch failed!")
288
289 self.check()
290 self.configFile = gitdir + "/p4-git-sync.cfg"
291 self.config = shelve.open(self.configFile, writeback=True)
292
293 if self.firstTime:
294 self.start()
295
296 commits = self.config.get("commits", [])
297
298 while len(commits) > 0:
299 self.firstTime = False
300 commit = commits[0]
301 commits = commits[1:]
302 self.config["commits"] = commits
303 self.apply(commit)
304 if not self.interactive:
305 break
306
307 self.config.close()
308
309 if len(commits) == 0:
310 if self.firstTime:
311 print "No changes found to apply between %s and current HEAD" % self.origin
312 else:
313 print "All changes applied!"
04219c04
SH
314 if not self.applyAsPatch:
315 print "Deleting temporary p4-sync branch and going back to %s" % self.master
316 system("git checkout %s" % self.master)
317 system("git branch -D p4-sync")
318 print "Cleaning out your perforce checkout by doing p4 edit ... ; p4 revert ..."
319 system("p4 edit ... >/dev/null")
320 system("p4 revert ... >/dev/null")
4f5cf76a
SH
321 os.remove(self.configFile)
322
b984733c
SH
323 return True
324
325class GitSync(Command):
326 def __init__(self):
327 Command.__init__(self)
328 self.options = [
329 optparse.make_option("--branch", dest="branch"),
330 optparse.make_option("--detect-branches", dest="detectBranches", action="store_true"),
331 optparse.make_option("--changesfile", dest="changesFile"),
332 optparse.make_option("--silent", dest="silent", action="store_true"),
333 optparse.make_option("--known-branches", dest="knownBranches"),
334 optparse.make_option("--cache", dest="doCache", action="store_true"),
335 optparse.make_option("--command-cache", dest="commandCache", action="store_true")
336 ]
337 self.description = """Imports from Perforce into a git repository.\n
338 example:
339 //depot/my/project/ -- to import the current head
340 //depot/my/project/@all -- to import everything
341 //depot/my/project/@1,6 -- to import only from revision 1 to 6
342
343 (a ... is not needed in the path p4 specification, it's added implicitly)"""
344
345 self.usage += " //depot/path[@revRange]"
346
347 self.dataCache = False
348 self.commandCache = False
349 self.silent = False
350 self.knownBranches = Set()
351 self.createdBranches = Set()
352 self.committedChanges = Set()
353 self.branch = "master"
354 self.detectBranches = False
355 self.changesFile = ""
356
357 def p4File(self, depotPath):
358 return os.popen("p4 print -q \"%s\"" % depotPath, "rb").read()
359
360 def extractFilesFromCommit(self, commit):
361 files = []
362 fnum = 0
363 while commit.has_key("depotFile%s" % fnum):
364 path = commit["depotFile%s" % fnum]
365 if not path.startswith(self.globalPrefix):
366 # if not self.silent:
367 # print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, self.globalPrefix, change)
368 fnum = fnum + 1
369 continue
370
371 file = {}
372 file["path"] = path
373 file["rev"] = commit["rev%s" % fnum]
374 file["action"] = commit["action%s" % fnum]
375 file["type"] = commit["type%s" % fnum]
376 files.append(file)
377 fnum = fnum + 1
378 return files
379
380 def isSubPathOf(self, first, second):
381 if not first.startswith(second):
382 return False
383 if first == second:
384 return True
385 return first[len(second)] == "/"
386
387 def branchesForCommit(self, files):
388 branches = Set()
389
390 for file in files:
391 relativePath = file["path"][len(self.globalPrefix):]
392 # strip off the filename
393 relativePath = relativePath[0:relativePath.rfind("/")]
394
395 # if len(branches) == 0:
396 # branches.add(relativePath)
397 # knownBranches.add(relativePath)
398 # continue
399
400 ###### this needs more testing :)
401 knownBranch = False
402 for branch in branches:
403 if relativePath == branch:
404 knownBranch = True
405 break
406 # if relativePath.startswith(branch):
407 if self.isSubPathOf(relativePath, branch):
408 knownBranch = True
409 break
410 # if branch.startswith(relativePath):
411 if self.isSubPathOf(branch, relativePath):
412 branches.remove(branch)
413 break
414
415 if knownBranch:
416 continue
417
418 for branch in knownBranches:
419 #if relativePath.startswith(branch):
420 if self.isSubPathOf(relativePath, branch):
421 if len(branches) == 0:
422 relativePath = branch
423 else:
424 knownBranch = True
425 break
426
427 if knownBranch:
428 continue
429
430 branches.add(relativePath)
431 self.knownBranches.add(relativePath)
432
433 return branches
434
435 def findBranchParent(self, branchPrefix, files):
436 for file in files:
437 path = file["path"]
438 if not path.startswith(branchPrefix):
439 continue
440 action = file["action"]
441 if action != "integrate" and action != "branch":
442 continue
443 rev = file["rev"]
444 depotPath = path + "#" + rev
445
446 log = p4CmdList("filelog \"%s\"" % depotPath)
447 if len(log) != 1:
448 print "eek! I got confused by the filelog of %s" % depotPath
449 sys.exit(1);
450
451 log = log[0]
452 if log["action0"] != action:
453 print "eek! wrong action in filelog for %s : found %s, expected %s" % (depotPath, log["action0"], action)
454 sys.exit(1);
455
456 branchAction = log["how0,0"]
457 # if branchAction == "branch into" or branchAction == "ignored":
458 # continue # ignore for branching
459
460 if not branchAction.endswith(" from"):
461 continue # ignore for branching
462 # print "eek! file %s was not branched from but instead: %s" % (depotPath, branchAction)
463 # sys.exit(1);
464
465 source = log["file0,0"]
466 if source.startswith(branchPrefix):
467 continue
468
469 lastSourceRev = log["erev0,0"]
470
471 sourceLog = p4CmdList("filelog -m 1 \"%s%s\"" % (source, lastSourceRev))
472 if len(sourceLog) != 1:
473 print "eek! I got confused by the source filelog of %s%s" % (source, lastSourceRev)
474 sys.exit(1);
475 sourceLog = sourceLog[0]
476
477 relPath = source[len(self.globalPrefix):]
478 # strip off the filename
479 relPath = relPath[0:relPath.rfind("/")]
480
481 for branch in self.knownBranches:
482 if self.isSubPathOf(relPath, branch):
483 # print "determined parent branch branch %s due to change in file %s" % (branch, source)
484 return branch
485 # else:
486 # print "%s is not a subpath of branch %s" % (relPath, branch)
487
488 return ""
489
c715706b 490 def commit(self, details, files, branch, branchPrefix, parent = "", merged = ""):
b984733c
SH
491 epoch = details["time"]
492 author = details["user"]
493
494 self.gitStream.write("commit %s\n" % branch)
495 # gitStream.write("mark :%s\n" % details["change"])
496 self.committedChanges.add(int(details["change"]))
497 committer = ""
498 if author in self.users:
0828ab14 499 committer = "%s %s %s" % (self.users[author], epoch, self.tz)
b984733c 500 else:
0828ab14 501 committer = "%s <a@b> %s %s" % (author, epoch, self.tz)
b984733c
SH
502
503 self.gitStream.write("committer %s\n" % committer)
504
505 self.gitStream.write("data <<EOT\n")
506 self.gitStream.write(details["desc"])
507 self.gitStream.write("\n[ imported from %s; change %s ]\n" % (branchPrefix, details["change"]))
508 self.gitStream.write("EOT\n\n")
509
510 if len(parent) > 0:
511 self.gitStream.write("from %s\n" % parent)
512
513 if len(merged) > 0:
514 self.gitStream.write("merge %s\n" % merged)
515
516 for file in files:
517 path = file["path"]
518 if not path.startswith(branchPrefix):
519 # if not silent:
520 # print "\nchanged files: ignoring path %s outside of branch prefix %s in change %s" % (path, branchPrefix, details["change"])
521 continue
522 rev = file["rev"]
523 depotPath = path + "#" + rev
524 relPath = path[len(branchPrefix):]
525 action = file["action"]
526
527 if file["type"] == "apple":
528 print "\nfile %s is a strange apple file that forks. Ignoring!" % path
529 continue
530
531 if action == "delete":
532 self.gitStream.write("D %s\n" % relPath)
533 else:
534 mode = 644
535 if file["type"].startswith("x"):
536 mode = 755
537
538 data = self.p4File(depotPath)
539
540 self.gitStream.write("M %s inline %s\n" % (mode, relPath))
541 self.gitStream.write("data %s\n" % len(data))
542 self.gitStream.write(data)
543 self.gitStream.write("\n")
544
545 self.gitStream.write("\n")
546
547 self.lastChange = int(details["change"])
548
549 def extractFilesInCommitToBranch(self, files, branchPrefix):
550 newFiles = []
551
552 for file in files:
553 path = file["path"]
554 if path.startswith(branchPrefix):
555 newFiles.append(file)
556
557 return newFiles
558
559 def findBranchSourceHeuristic(self, files, branch, branchPrefix):
560 for file in files:
561 action = file["action"]
562 if action != "integrate" and action != "branch":
563 continue
564 path = file["path"]
565 rev = file["rev"]
566 depotPath = path + "#" + rev
567
568 log = p4CmdList("filelog \"%s\"" % depotPath)
569 if len(log) != 1:
570 print "eek! I got confused by the filelog of %s" % depotPath
571 sys.exit(1);
572
573 log = log[0]
574 if log["action0"] != action:
575 print "eek! wrong action in filelog for %s : found %s, expected %s" % (depotPath, log["action0"], action)
576 sys.exit(1);
577
578 branchAction = log["how0,0"]
579
580 if not branchAction.endswith(" from"):
581 continue # ignore for branching
582 # print "eek! file %s was not branched from but instead: %s" % (depotPath, branchAction)
583 # sys.exit(1);
584
585 source = log["file0,0"]
586 if source.startswith(branchPrefix):
587 continue
588
589 lastSourceRev = log["erev0,0"]
590
591 sourceLog = p4CmdList("filelog -m 1 \"%s%s\"" % (source, lastSourceRev))
592 if len(sourceLog) != 1:
593 print "eek! I got confused by the source filelog of %s%s" % (source, lastSourceRev)
594 sys.exit(1);
595 sourceLog = sourceLog[0]
596
597 relPath = source[len(self.globalPrefix):]
598 # strip off the filename
599 relPath = relPath[0:relPath.rfind("/")]
600
601 for candidate in self.knownBranches:
602 if self.isSubPathOf(relPath, candidate) and candidate != branch:
603 return candidate
604
605 return ""
606
607 def changeIsBranchMerge(self, sourceBranch, destinationBranch, change):
608 sourceFiles = {}
609 for file in p4CmdList("files %s...@%s" % (self.globalPrefix + sourceBranch + "/", change)):
610 if file["action"] == "delete":
611 continue
612 sourceFiles[file["depotFile"]] = file
613
614 destinationFiles = {}
615 for file in p4CmdList("files %s...@%s" % (self.globalPrefix + destinationBranch + "/", change)):
616 destinationFiles[file["depotFile"]] = file
617
618 for fileName in sourceFiles.keys():
619 integrations = []
620 deleted = False
621 integrationCount = 0
622 for integration in p4CmdList("integrated \"%s\"" % fileName):
623 toFile = integration["fromFile"] # yes, it's true, it's fromFile
624 if not toFile in destinationFiles:
625 continue
626 destFile = destinationFiles[toFile]
627 if destFile["action"] == "delete":
628 # print "file %s has been deleted in %s" % (fileName, toFile)
629 deleted = True
630 break
631 integrationCount += 1
632 if integration["how"] == "branch from":
633 continue
634
635 if int(integration["change"]) == change:
636 integrations.append(integration)
637 continue
638 if int(integration["change"]) > change:
639 continue
640
641 destRev = int(destFile["rev"])
642
643 startRev = integration["startFromRev"][1:]
644 if startRev == "none":
645 startRev = 0
646 else:
647 startRev = int(startRev)
648
649 endRev = integration["endFromRev"][1:]
650 if endRev == "none":
651 endRev = 0
652 else:
653 endRev = int(endRev)
654
655 initialBranch = (destRev == 1 and integration["how"] != "branch into")
656 inRange = (destRev >= startRev and destRev <= endRev)
657 newer = (destRev > startRev and destRev > endRev)
658
659 if initialBranch or inRange or newer:
660 integrations.append(integration)
661
662 if deleted:
663 continue
664
665 if len(integrations) == 0 and integrationCount > 1:
666 print "file %s was not integrated from %s into %s" % (fileName, sourceBranch, destinationBranch)
667 return False
668
669 return True
670
671 def getUserMap(self):
672 self.users = {}
673
674 for output in p4CmdList("users"):
675 if not output.has_key("User"):
676 continue
677 self.users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">"
678
679 def run(self, args):
680 self.branch = "refs/heads/" + self.branch
681 self.globalPrefix = self.previousDepotPath = os.popen("git-repo-config --get p4.depotpath").read()
682 if len(self.globalPrefix) != 0:
683 self.globalPrefix = self.globalPrefix[:-1]
684
685 if len(args) == 0 and len(self.globalPrefix) != 0:
686 if not self.silent:
687 print "[using previously specified depot path %s]" % self.globalPrefix
688 elif len(args) != 1:
689 return False
690 else:
691 if len(self.globalPrefix) != 0 and self.globalPrefix != args[0]:
692 print "previous import used depot path %s and now %s was specified. this doesn't work!" % (self.globalPrefix, args[0])
693 sys.exit(1)
694 self.globalPrefix = args[0]
695
696 self.changeRange = ""
697 self.revision = ""
698 self.users = {}
699 self.initialParent = ""
700 self.lastChange = 0
701 self.initialTag = ""
702
703 if self.globalPrefix.find("@") != -1:
704 atIdx = self.globalPrefix.index("@")
705 self.changeRange = self.globalPrefix[atIdx:]
706 if self.changeRange == "@all":
707 self.changeRange = ""
708 elif self.changeRange.find(",") == -1:
709 self.revision = self.changeRange
710 self.changeRange = ""
711 self.globalPrefix = self.globalPrefix[0:atIdx]
712 elif self.globalPrefix.find("#") != -1:
713 hashIdx = self.globalPrefix.index("#")
714 self.revision = self.globalPrefix[hashIdx:]
715 self.globalPrefix = self.globalPrefix[0:hashIdx]
716 elif len(self.previousDepotPath) == 0:
717 self.revision = "#head"
718
719 if self.globalPrefix.endswith("..."):
720 self.globalPrefix = self.globalPrefix[:-3]
721
722 if not self.globalPrefix.endswith("/"):
723 self.globalPrefix += "/"
724
725 self.getUserMap()
726
727 if len(self.changeRange) == 0:
728 try:
729 sout, sin, serr = popen2.popen3("git-name-rev --tags `git-rev-parse %s`" % self.branch)
730 output = sout.read()
731 if output.endswith("\n"):
732 output = output[:-1]
733 tagIdx = output.index(" tags/p4/")
734 caretIdx = output.find("^")
735 endPos = len(output)
736 if caretIdx != -1:
737 endPos = caretIdx
738 self.rev = int(output[tagIdx + 9 : endPos]) + 1
739 self.changeRange = "@%s,#head" % self.rev
740 self.initialParent = os.popen("git-rev-parse %s" % self.branch).read()[:-1]
741 self.initialTag = "p4/%s" % (int(self.rev) - 1)
742 except:
743 pass
744
0828ab14
SH
745 self.tz = - time.timezone / 36
746 tzsign = ("%s" % self.tz)[0]
b984733c 747 if tzsign != '+' and tzsign != '-':
0828ab14 748 self.tz = "+" + ("%s" % self.tz)
b984733c
SH
749
750 self.gitOutput, self.gitStream, self.gitError = popen2.popen3("git-fast-import")
751
752 if len(self.revision) > 0:
753 print "Doing initial import of %s from revision %s" % (self.globalPrefix, self.revision)
754
755 details = { "user" : "git perforce import user", "time" : int(time.time()) }
756 details["desc"] = "Initial import of %s from the state at revision %s" % (self.globalPrefix, self.revision)
757 details["change"] = self.revision
758 newestRevision = 0
759
760 fileCnt = 0
761 for info in p4CmdList("files %s...%s" % (self.globalPrefix, self.revision)):
762 change = int(info["change"])
763 if change > newestRevision:
764 newestRevision = change
765
766 if info["action"] == "delete":
c715706b 767 fileCnt = fileCnt + 1
b984733c
SH
768 continue
769
770 for prop in [ "depotFile", "rev", "action", "type" ]:
771 details["%s%s" % (prop, fileCnt)] = info[prop]
772
773 fileCnt = fileCnt + 1
774
775 details["change"] = newestRevision
776
777 try:
778 self.commit(details, self.extractFilesFromCommit(details), self.branch, self.globalPrefix)
c715706b 779 except IOError:
b984733c
SH
780 print self.gitError.read()
781
782 else:
783 changes = []
784
0828ab14 785 if len(self.changesFile) > 0:
b984733c
SH
786 output = open(self.changesFile).readlines()
787 changeSet = Set()
788 for line in output:
789 changeSet.add(int(line))
790
791 for change in changeSet:
792 changes.append(change)
793
794 changes.sort()
795 else:
796 output = os.popen("p4 changes %s...%s" % (self.globalPrefix, self.changeRange)).readlines()
797
798 for line in output:
799 changeNum = line.split(" ")[1]
800 changes.append(changeNum)
801
802 changes.reverse()
803
804 if len(changes) == 0:
0828ab14 805 if not self.silent:
b984733c
SH
806 print "no changes to import!"
807 sys.exit(1)
808
809 cnt = 1
810 for change in changes:
811 description = p4Cmd("describe %s" % change)
812
0828ab14 813 if not self.silent:
b984733c
SH
814 sys.stdout.write("\rimporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
815 sys.stdout.flush()
816 cnt = cnt + 1
817
818 try:
819 files = self.extractFilesFromCommit(description)
820 if self.detectBranches:
821 for branch in self.branchesForCommit(files):
822 self.knownBranches.add(branch)
823 branchPrefix = self.globalPrefix + branch + "/"
824
825 filesForCommit = self.extractFilesInCommitToBranch(files, branchPrefix)
826
827 merged = ""
828 parent = ""
829 ########### remove cnt!!!
830 if branch not in self.createdBranches and cnt > 2:
831 self.createdBranches.add(branch)
832 parent = self.findBranchParent(branchPrefix, files)
833 if parent == branch:
834 parent = ""
835 # elif len(parent) > 0:
836 # print "%s branched off of %s" % (branch, parent)
837
838 if len(parent) == 0:
839 merged = self.findBranchSourceHeuristic(filesForCommit, branch, branchPrefix)
840 if len(merged) > 0:
841 print "change %s could be a merge from %s into %s" % (description["change"], merged, branch)
842 if not self.changeIsBranchMerge(merged, branch, int(description["change"])):
843 merged = ""
844
845 branch = "refs/heads/" + branch
846 if len(parent) > 0:
847 parent = "refs/heads/" + parent
848 if len(merged) > 0:
849 merged = "refs/heads/" + merged
850 self.commit(description, files, branch, branchPrefix, parent, merged)
851 else:
0828ab14 852 self.commit(description, files, self.branch, self.globalPrefix, self.initialParent)
b984733c
SH
853 self.initialParent = ""
854 except IOError:
855 print self.gitError.read()
856 sys.exit(1)
857
858 if not self.silent:
859 print ""
860
861 self.gitStream.write("reset refs/tags/p4/%s\n" % self.lastChange)
862 self.gitStream.write("from %s\n\n" % self.branch);
863
864
865 self.gitStream.close()
866 self.gitOutput.close()
867 self.gitError.close()
868
869 os.popen("git-repo-config p4.depotpath %s" % self.globalPrefix).read()
870 if len(self.initialTag) > 0:
871 os.popen("git tag -d %s" % self.initialTag).read()
872
873 return True
874
875class HelpFormatter(optparse.IndentedHelpFormatter):
876 def __init__(self):
877 optparse.IndentedHelpFormatter.__init__(self)
878
879 def format_description(self, description):
880 if description:
881 return description + "\n"
882 else:
883 return ""
4f5cf76a 884
86949eef
SH
885def printUsage(commands):
886 print "usage: %s <command> [options]" % sys.argv[0]
887 print ""
888 print "valid commands: %s" % ", ".join(commands)
889 print ""
890 print "Try %s <command> --help for command specific help." % sys.argv[0]
891 print ""
892
893commands = {
894 "debug" : P4Debug(),
4f5cf76a 895 "clean-tags" : P4CleanTags(),
b984733c
SH
896 "submit" : P4Sync(),
897 "sync" : GitSync()
86949eef
SH
898}
899
900if len(sys.argv[1:]) == 0:
901 printUsage(commands.keys())
902 sys.exit(2)
903
904cmd = ""
905cmdName = sys.argv[1]
906try:
907 cmd = commands[cmdName]
908except KeyError:
909 print "unknown command %s" % cmdName
910 print ""
911 printUsage(commands.keys())
912 sys.exit(2)
913
4f5cf76a
SH
914options = cmd.options
915cmd.gitdir = gitdir
916options.append(optparse.make_option("--git-dir", dest="gitdir"))
917
b984733c
SH
918parser = optparse.OptionParser(cmd.usage.replace("%prog", "%prog " + cmdName),
919 options,
920 description = cmd.description,
921 formatter = HelpFormatter())
86949eef
SH
922
923(cmd, args) = parser.parse_args(sys.argv[2:], cmd);
924
4f5cf76a
SH
925gitdir = cmd.gitdir
926if len(gitdir) == 0:
927 gitdir = ".git"
20618650
SH
928 if not isValidGitDir(gitdir):
929 cdup = os.popen("git-rev-parse --show-cdup").read()[:-1]
930 if isValidGitDir(cdup + "/" + gitdir):
931 os.chdir(cdup)
4f5cf76a
SH
932
933if not isValidGitDir(gitdir):
934 if isValidGitDir(gitdir + "/.git"):
935 gitdir += "/.git"
936 else:
05140f34 937 die("fatal: cannot locate git repository at %s" % gitdir)
4f5cf76a
SH
938
939os.environ["GIT_DIR"] = gitdir
940
b984733c
SH
941if not cmd.run(args):
942 parser.print_help()
943