Completely untested "merge" of p4-fast-export.py into git-p4.py
Signed-off-by: Simon Hausmann <hausmann@kde.org>
Simon Hausmann committed
Mar 20, 2007 at 20:54 UTC
b984733c8048423537540fdca681ddc448b60fcc
1 file changed
+582
-8
contrib/fast-import/git-p4.py
+582
-8
@@ -9,7 +9,8 @@
9
#
10
11
import optparse, sys, os, marshal, popen2, shelve
12
-import tempfile
12
+import tempfile, getopt, sha, os.path, time
13
+from sets import Set;
14
15
gitdir = os.environ.get("GIT_DIR", "")
16
@@ -51,7 +52,11 @@ def system(cmd):
52
if os.system(cmd) != 0:
53
die("command failed: %s" % cmd)
54
54
-class P4Debug:
55
+class Command:
56
+ def __init__(self):
57
+ self.usage = "usage: %prog [options]"
58
+
59
+class P4Debug(Command):
60
def __init__(self):
61
self.options = [
62
]
@@ -60,9 +65,11 @@ class P4Debug:
65
def run(self, args):
66
for output in p4CmdList(" ".join(args)):
67
print output
68
+ return True
69
64
-class P4CleanTags:
70
+class P4CleanTags(Command):
71
def __init__(self):
72
+ Command.__init__(self)
73
self.options = [
74
# optparse.make_option("--branch", dest="branch", default="refs/heads/master")
75
]
@@ -96,9 +103,11 @@ class P4CleanTags:
103
print os.popen("git tag -d p4/%s" % rev).read()
104
105
print "%s tags removed." % len(allTags)
106
+ return True
107
100
-class P4Sync:
108
+class P4Sync(Command):
109
def __init__(self):
110
+ Command.__init__(self)
111
self.options = [
112
optparse.make_option("--continue", action="store_false", dest="firstTime"),
113
optparse.make_option("--origin", dest="origin"),
@@ -304,6 +313,566 @@ class P4Sync:
313
system("p4 revert ... >/dev/null")
314
os.remove(self.configFile)
315
316
+ return True
317
+
318
+class GitSync(Command):
319
+ def __init__(self):
320
+ Command.__init__(self)
321
+ self.options = [
322
+ optparse.make_option("--branch", dest="branch"),
323
+ optparse.make_option("--detect-branches", dest="detectBranches", action="store_true"),
324
+ optparse.make_option("--changesfile", dest="changesFile"),
325
+ optparse.make_option("--silent", dest="silent", action="store_true"),
326
+ optparse.make_option("--known-branches", dest="knownBranches"),
327
+ optparse.make_option("--cache", dest="doCache", action="store_true"),
328
+ optparse.make_option("--command-cache", dest="commandCache", action="store_true")
329
+ ]
330
+ self.description = """Imports from Perforce into a git repository.\n
331
+ example:
332
+ //depot/my/project/ -- to import the current head
333
+ //depot/my/project/@all -- to import everything
334
+ //depot/my/project/@1,6 -- to import only from revision 1 to 6
335
+
336
+ (a ... is not needed in the path p4 specification, it's added implicitly)"""
337
+
338
+ self.usage += " //depot/path[@revRange]"
339
+
340
+ self.dataCache = False
341
+ self.commandCache = False
342
+ self.silent = False
343
+ self.knownBranches = Set()
344
+ self.createdBranches = Set()
345
+ self.committedChanges = Set()
346
+ self.branch = "master"
347
+ self.detectBranches = False
348
+ self.changesFile = ""
349
+
350
+ def p4File(self, depotPath):
351
+ return os.popen("p4 print -q \"%s\"" % depotPath, "rb").read()
352
+
353
+ def extractFilesFromCommit(self, commit):
354
+ files = []
355
+ fnum = 0
356
+ while commit.has_key("depotFile%s" % fnum):
357
+ path = commit["depotFile%s" % fnum]
358
+ if not path.startswith(self.globalPrefix):
359
+ # if not self.silent:
360
+ # print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, self.globalPrefix, change)
361
+ fnum = fnum + 1
362
+ continue
363
+
364
+ file = {}
365
+ file["path"] = path
366
+ file["rev"] = commit["rev%s" % fnum]
367
+ file["action"] = commit["action%s" % fnum]
368
+ file["type"] = commit["type%s" % fnum]
369
+ files.append(file)
370
+ fnum = fnum + 1
371
+ return files
372
+
373
+ def isSubPathOf(self, first, second):
374
+ if not first.startswith(second):
375
+ return False
376
+ if first == second:
377
+ return True
378
+ return first[len(second)] == "/"
379
+
380
+ def branchesForCommit(self, files):
381
+ branches = Set()
382
+
383
+ for file in files:
384
+ relativePath = file["path"][len(self.globalPrefix):]
385
+ # strip off the filename
386
+ relativePath = relativePath[0:relativePath.rfind("/")]
387
+
388
+ # if len(branches) == 0:
389
+ # branches.add(relativePath)
390
+ # knownBranches.add(relativePath)
391
+ # continue
392
+
393
+ ###### this needs more testing :)
394
+ knownBranch = False
395
+ for branch in branches:
396
+ if relativePath == branch:
397
+ knownBranch = True
398
+ break
399
+ # if relativePath.startswith(branch):
400
+ if self.isSubPathOf(relativePath, branch):
401
+ knownBranch = True
402
+ break
403
+ # if branch.startswith(relativePath):
404
+ if self.isSubPathOf(branch, relativePath):
405
+ branches.remove(branch)
406
+ break
407
+
408
+ if knownBranch:
409
+ continue
410
+
411
+ for branch in knownBranches:
412
+ #if relativePath.startswith(branch):
413
+ if self.isSubPathOf(relativePath, branch):
414
+ if len(branches) == 0:
415
+ relativePath = branch
416
+ else:
417
+ knownBranch = True
418
+ break
419
+
420
+ if knownBranch:
421
+ continue
422
+
423
+ branches.add(relativePath)
424
+ self.knownBranches.add(relativePath)
425
+
426
+ return branches
427
+
428
+ def findBranchParent(self, branchPrefix, files):
429
+ for file in files:
430
+ path = file["path"]
431
+ if not path.startswith(branchPrefix):
432
+ continue
433
+ action = file["action"]
434
+ if action != "integrate" and action != "branch":
435
+ continue
436
+ rev = file["rev"]
437
+ depotPath = path + "#" + rev
438
+
439
+ log = p4CmdList("filelog \"%s\"" % depotPath)
440
+ if len(log) != 1:
441
+ print "eek! I got confused by the filelog of %s" % depotPath
442
+ sys.exit(1);
443
+
444
+ log = log[0]
445
+ if log["action0"] != action:
446
+ print "eek! wrong action in filelog for %s : found %s, expected %s" % (depotPath, log["action0"], action)
447
+ sys.exit(1);
448
+
449
+ branchAction = log["how0,0"]
450
+ # if branchAction == "branch into" or branchAction == "ignored":
451
+ # continue # ignore for branching
452
+
453
+ if not branchAction.endswith(" from"):
454
+ continue # ignore for branching
455
+ # print "eek! file %s was not branched from but instead: %s" % (depotPath, branchAction)
456
+ # sys.exit(1);
457
+
458
+ source = log["file0,0"]
459
+ if source.startswith(branchPrefix):
460
+ continue
461
+
462
+ lastSourceRev = log["erev0,0"]
463
+
464
+ sourceLog = p4CmdList("filelog -m 1 \"%s%s\"" % (source, lastSourceRev))
465
+ if len(sourceLog) != 1:
466
+ print "eek! I got confused by the source filelog of %s%s" % (source, lastSourceRev)
467
+ sys.exit(1);
468
+ sourceLog = sourceLog[0]
469
+
470
+ relPath = source[len(self.globalPrefix):]
471
+ # strip off the filename
472
+ relPath = relPath[0:relPath.rfind("/")]
473
+
474
+ for branch in self.knownBranches:
475
+ if self.isSubPathOf(relPath, branch):
476
+ # print "determined parent branch branch %s due to change in file %s" % (branch, source)
477
+ return branch
478
+ # else:
479
+ # print "%s is not a subpath of branch %s" % (relPath, branch)
480
+
481
+ return ""
482
+
483
+ def commit(self, details, files, branch, branchPrefix, parent, merged = ""):
484
+ epoch = details["time"]
485
+ author = details["user"]
486
+
487
+ self.gitStream.write("commit %s\n" % branch)
488
+ # gitStream.write("mark :%s\n" % details["change"])
489
+ self.committedChanges.add(int(details["change"]))
490
+ committer = ""
491
+ if author in self.users:
492
+ committer = "%s %s %s" % (self.users[author], epoch, tz)
493
+ else:
494
+ committer = "%s <a@b> %s %s" % (author, epoch, tz)
495
+
496
+ self.gitStream.write("committer %s\n" % committer)
497
+
498
+ self.gitStream.write("data <<EOT\n")
499
+ self.gitStream.write(details["desc"])
500
+ self.gitStream.write("\n[ imported from %s; change %s ]\n" % (branchPrefix, details["change"]))
501
+ self.gitStream.write("EOT\n\n")
502
+
503
+ if len(parent) > 0:
504
+ self.gitStream.write("from %s\n" % parent)
505
+
506
+ if len(merged) > 0:
507
+ self.gitStream.write("merge %s\n" % merged)
508
+
509
+ for file in files:
510
+ path = file["path"]
511
+ if not path.startswith(branchPrefix):
512
+ # if not silent:
513
+ # print "\nchanged files: ignoring path %s outside of branch prefix %s in change %s" % (path, branchPrefix, details["change"])
514
+ continue
515
+ rev = file["rev"]
516
+ depotPath = path + "#" + rev
517
+ relPath = path[len(branchPrefix):]
518
+ action = file["action"]
519
+
520
+ if file["type"] == "apple":
521
+ print "\nfile %s is a strange apple file that forks. Ignoring!" % path
522
+ continue
523
+
524
+ if action == "delete":
525
+ self.gitStream.write("D %s\n" % relPath)
526
+ else:
527
+ mode = 644
528
+ if file["type"].startswith("x"):
529
+ mode = 755
530
+
531
+ data = self.p4File(depotPath)
532
+
533
+ self.gitStream.write("M %s inline %s\n" % (mode, relPath))
534
+ self.gitStream.write("data %s\n" % len(data))
535
+ self.gitStream.write(data)
536
+ self.gitStream.write("\n")
537
+
538
+ self.gitStream.write("\n")
539
+
540
+ self.lastChange = int(details["change"])
541
+
542
+ def extractFilesInCommitToBranch(self, files, branchPrefix):
543
+ newFiles = []
544
+
545
+ for file in files:
546
+ path = file["path"]
547
+ if path.startswith(branchPrefix):
548
+ newFiles.append(file)
549
+
550
+ return newFiles
551
+
552
+ def findBranchSourceHeuristic(self, files, branch, branchPrefix):
553
+ for file in files:
554
+ action = file["action"]
555
+ if action != "integrate" and action != "branch":
556
+ continue
557
+ path = file["path"]
558
+ rev = file["rev"]
559
+ depotPath = path + "#" + rev
560
+
561
+ log = p4CmdList("filelog \"%s\"" % depotPath)
562
+ if len(log) != 1:
563
+ print "eek! I got confused by the filelog of %s" % depotPath
564
+ sys.exit(1);
565
+
566
+ log = log[0]
567
+ if log["action0"] != action:
568
+ print "eek! wrong action in filelog for %s : found %s, expected %s" % (depotPath, log["action0"], action)
569
+ sys.exit(1);
570
+
571
+ branchAction = log["how0,0"]
572
+
573
+ if not branchAction.endswith(" from"):
574
+ continue # ignore for branching
575
+ # print "eek! file %s was not branched from but instead: %s" % (depotPath, branchAction)
576
+ # sys.exit(1);
577
+
578
+ source = log["file0,0"]
579
+ if source.startswith(branchPrefix):
580
+ continue
581
+
582
+ lastSourceRev = log["erev0,0"]
583
+
584
+ sourceLog = p4CmdList("filelog -m 1 \"%s%s\"" % (source, lastSourceRev))
585
+ if len(sourceLog) != 1:
586
+ print "eek! I got confused by the source filelog of %s%s" % (source, lastSourceRev)
587
+ sys.exit(1);
588
+ sourceLog = sourceLog[0]
589
+
590
+ relPath = source[len(self.globalPrefix):]
591
+ # strip off the filename
592
+ relPath = relPath[0:relPath.rfind("/")]
593
+
594
+ for candidate in self.knownBranches:
595
+ if self.isSubPathOf(relPath, candidate) and candidate != branch:
596
+ return candidate
597
+
598
+ return ""
599
+
600
+ def changeIsBranchMerge(self, sourceBranch, destinationBranch, change):
601
+ sourceFiles = {}
602
+ for file in p4CmdList("files %s...@%s" % (self.globalPrefix + sourceBranch + "/", change)):
603
+ if file["action"] == "delete":
604
+ continue
605
+ sourceFiles[file["depotFile"]] = file
606
+
607
+ destinationFiles = {}
608
+ for file in p4CmdList("files %s...@%s" % (self.globalPrefix + destinationBranch + "/", change)):
609
+ destinationFiles[file["depotFile"]] = file
610
+
611
+ for fileName in sourceFiles.keys():
612
+ integrations = []
613
+ deleted = False
614
+ integrationCount = 0
615
+ for integration in p4CmdList("integrated \"%s\"" % fileName):
616
+ toFile = integration["fromFile"] # yes, it's true, it's fromFile
617
+ if not toFile in destinationFiles:
618
+ continue
619
+ destFile = destinationFiles[toFile]
620
+ if destFile["action"] == "delete":
621
+ # print "file %s has been deleted in %s" % (fileName, toFile)
622
+ deleted = True
623
+ break
624
+ integrationCount += 1
625
+ if integration["how"] == "branch from":
626
+ continue
627
+
628
+ if int(integration["change"]) == change:
629
+ integrations.append(integration)
630
+ continue
631
+ if int(integration["change"]) > change:
632
+ continue
633
+
634
+ destRev = int(destFile["rev"])
635
+
636
+ startRev = integration["startFromRev"][1:]
637
+ if startRev == "none":
638
+ startRev = 0
639
+ else:
640
+ startRev = int(startRev)
641
+
642
+ endRev = integration["endFromRev"][1:]
643
+ if endRev == "none":
644
+ endRev = 0
645
+ else:
646
+ endRev = int(endRev)
647
+
648
+ initialBranch = (destRev == 1 and integration["how"] != "branch into")
649
+ inRange = (destRev >= startRev and destRev <= endRev)
650
+ newer = (destRev > startRev and destRev > endRev)
651
+
652
+ if initialBranch or inRange or newer:
653
+ integrations.append(integration)
654
+
655
+ if deleted:
656
+ continue
657
+
658
+ if len(integrations) == 0 and integrationCount > 1:
659
+ print "file %s was not integrated from %s into %s" % (fileName, sourceBranch, destinationBranch)
660
+ return False
661
+
662
+ return True
663
+
664
+ def getUserMap(self):
665
+ self.users = {}
666
+
667
+ for output in p4CmdList("users"):
668
+ if not output.has_key("User"):
669
+ continue
670
+ self.users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">"
671
+
672
+ def run(self, args):
673
+ self.branch = "refs/heads/" + self.branch
674
+ self.globalPrefix = self.previousDepotPath = os.popen("git-repo-config --get p4.depotpath").read()
675
+ if len(self.globalPrefix) != 0:
676
+ self.globalPrefix = self.globalPrefix[:-1]
677
+
678
+ if len(args) == 0 and len(self.globalPrefix) != 0:
679
+ if not self.silent:
680
+ print "[using previously specified depot path %s]" % self.globalPrefix
681
+ elif len(args) != 1:
682
+ return False
683
+ else:
684
+ if len(self.globalPrefix) != 0 and self.globalPrefix != args[0]:
685
+ print "previous import used depot path %s and now %s was specified. this doesn't work!" % (self.globalPrefix, args[0])
686
+ sys.exit(1)
687
+ self.globalPrefix = args[0]
688
+
689
+ self.changeRange = ""
690
+ self.revision = ""
691
+ self.users = {}
692
+ self.initialParent = ""
693
+ self.lastChange = 0
694
+ self.initialTag = ""
695
+
696
+ if self.globalPrefix.find("@") != -1:
697
+ atIdx = self.globalPrefix.index("@")
698
+ self.changeRange = self.globalPrefix[atIdx:]
699
+ if self.changeRange == "@all":
700
+ self.changeRange = ""
701
+ elif self.changeRange.find(",") == -1:
702
+ self.revision = self.changeRange
703
+ self.changeRange = ""
704
+ self.globalPrefix = self.globalPrefix[0:atIdx]
705
+ elif self.globalPrefix.find("#") != -1:
706
+ hashIdx = self.globalPrefix.index("#")
707
+ self.revision = self.globalPrefix[hashIdx:]
708
+ self.globalPrefix = self.globalPrefix[0:hashIdx]
709
+ elif len(self.previousDepotPath) == 0:
710
+ self.revision = "#head"
711
+
712
+ if self.globalPrefix.endswith("..."):
713
+ self.globalPrefix = self.globalPrefix[:-3]
714
+
715
+ if not self.globalPrefix.endswith("/"):
716
+ self.globalPrefix += "/"
717
+
718
+ self.getUserMap()
719
+
720
+ if len(self.changeRange) == 0:
721
+ try:
722
+ sout, sin, serr = popen2.popen3("git-name-rev --tags `git-rev-parse %s`" % self.branch)
723
+ output = sout.read()
724
+ if output.endswith("\n"):
725
+ output = output[:-1]
726
+ tagIdx = output.index(" tags/p4/")
727
+ caretIdx = output.find("^")
728
+ endPos = len(output)
729
+ if caretIdx != -1:
730
+ endPos = caretIdx
731
+ self.rev = int(output[tagIdx + 9 : endPos]) + 1
732
+ self.changeRange = "@%s,#head" % self.rev
733
+ self.initialParent = os.popen("git-rev-parse %s" % self.branch).read()[:-1]
734
+ self.initialTag = "p4/%s" % (int(self.rev) - 1)
735
+ except:
736
+ pass
737
+
738
+ tz = - time.timezone / 36
739
+ tzsign = ("%s" % tz)[0]
740
+ if tzsign != '+' and tzsign != '-':
741
+ tz = "+" + ("%s" % tz)
742
+
743
+ self.gitOutput, self.gitStream, self.gitError = popen2.popen3("git-fast-import")
744
+
745
+ if len(self.revision) > 0:
746
+ print "Doing initial import of %s from revision %s" % (self.globalPrefix, self.revision)
747
+
748
+ details = { "user" : "git perforce import user", "time" : int(time.time()) }
749
+ details["desc"] = "Initial import of %s from the state at revision %s" % (self.globalPrefix, self.revision)
750
+ details["change"] = self.revision
751
+ newestRevision = 0
752
+
753
+ fileCnt = 0
754
+ for info in p4CmdList("files %s...%s" % (self.globalPrefix, self.revision)):
755
+ change = int(info["change"])
756
+ if change > newestRevision:
757
+ newestRevision = change
758
+
759
+ if info["action"] == "delete":
760
+ continue
761
+
762
+ for prop in [ "depotFile", "rev", "action", "type" ]:
763
+ details["%s%s" % (prop, fileCnt)] = info[prop]
764
+
765
+ fileCnt = fileCnt + 1
766
+
767
+ details["change"] = newestRevision
768
+
769
+ try:
770
+ self.commit(details, self.extractFilesFromCommit(details), self.branch, self.globalPrefix)
771
+ except:
772
+ print self.gitError.read()
773
+
774
+ else:
775
+ changes = []
776
+
777
+ if len(changesFile) > 0:
778
+ output = open(self.changesFile).readlines()
779
+ changeSet = Set()
780
+ for line in output:
781
+ changeSet.add(int(line))
782
+
783
+ for change in changeSet:
784
+ changes.append(change)
785
+
786
+ changes.sort()
787
+ else:
788
+ output = os.popen("p4 changes %s...%s" % (self.globalPrefix, self.changeRange)).readlines()
789
+
790
+ for line in output:
791
+ changeNum = line.split(" ")[1]
792
+ changes.append(changeNum)
793
+
794
+ changes.reverse()
795
+
796
+ if len(changes) == 0:
797
+ if not silent:
798
+ print "no changes to import!"
799
+ sys.exit(1)
800
+
801
+ cnt = 1
802
+ for change in changes:
803
+ description = p4Cmd("describe %s" % change)
804
+
805
+ if not silent:
806
+ sys.stdout.write("\rimporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
807
+ sys.stdout.flush()
808
+ cnt = cnt + 1
809
+
810
+ try:
811
+ files = self.extractFilesFromCommit(description)
812
+ if self.detectBranches:
813
+ for branch in self.branchesForCommit(files):
814
+ self.knownBranches.add(branch)
815
+ branchPrefix = self.globalPrefix + branch + "/"
816
+
817
+ filesForCommit = self.extractFilesInCommitToBranch(files, branchPrefix)
818
+
819
+ merged = ""
820
+ parent = ""
821
+ ########### remove cnt!!!
822
+ if branch not in self.createdBranches and cnt > 2:
823
+ self.createdBranches.add(branch)
824
+ parent = self.findBranchParent(branchPrefix, files)
825
+ if parent == branch:
826
+ parent = ""
827
+ # elif len(parent) > 0:
828
+ # print "%s branched off of %s" % (branch, parent)
829
+
830
+ if len(parent) == 0:
831
+ merged = self.findBranchSourceHeuristic(filesForCommit, branch, branchPrefix)
832
+ if len(merged) > 0:
833
+ print "change %s could be a merge from %s into %s" % (description["change"], merged, branch)
834
+ if not self.changeIsBranchMerge(merged, branch, int(description["change"])):
835
+ merged = ""
836
+
837
+ branch = "refs/heads/" + branch
838
+ if len(parent) > 0:
839
+ parent = "refs/heads/" + parent
840
+ if len(merged) > 0:
841
+ merged = "refs/heads/" + merged
842
+ self.commit(description, files, branch, branchPrefix, parent, merged)
843
+ else:
844
+ self.commit(description, files, branch, globalPrefix, initialParent)
845
+ self.initialParent = ""
846
+ except IOError:
847
+ print self.gitError.read()
848
+ sys.exit(1)
849
+
850
+ if not self.silent:
851
+ print ""
852
+
853
+ self.gitStream.write("reset refs/tags/p4/%s\n" % self.lastChange)
854
+ self.gitStream.write("from %s\n\n" % self.branch);
855
+
856
+
857
+ self.gitStream.close()
858
+ self.gitOutput.close()
859
+ self.gitError.close()
860
+
861
+ os.popen("git-repo-config p4.depotpath %s" % self.globalPrefix).read()
862
+ if len(self.initialTag) > 0:
863
+ os.popen("git tag -d %s" % self.initialTag).read()
864
+
865
+ return True
866
+
867
+class HelpFormatter(optparse.IndentedHelpFormatter):
868
+ def __init__(self):
869
+ optparse.IndentedHelpFormatter.__init__(self)
870
+
871
+ def format_description(self, description):
872
+ if description:
873
+ return description + "\n"
874
+ else:
875
+ return ""
876
877
def printUsage(commands):
878
print "usage: %s <command> [options]" % sys.argv[0]
@@ -316,7 +885,8 @@ def printUsage(commands):
885
commands = {
886
"debug" : P4Debug(),
887
"clean-tags" : P4CleanTags(),
319
- "submit" : P4Sync()
888
+ "submit" : P4Sync(),
889
+ "sync" : GitSync()
890
}
891
892
if len(sys.argv[1:]) == 0:
@@ -337,8 +907,10 @@ options = cmd.options
907
cmd.gitdir = gitdir
908
options.append(optparse.make_option("--git-dir", dest="gitdir"))
909
340
-parser = optparse.OptionParser("usage: %prog " + cmdName + " [options]", options,
341
- description = cmd.description)
910
+parser = optparse.OptionParser(cmd.usage.replace("%prog", "%prog " + cmdName),
911
+ options,
912
+ description = cmd.description,
913
+ formatter = HelpFormatter())
914
915
(cmd, args) = parser.parse_args(sys.argv[2:], cmd);
916
@@ -354,4 +926,6 @@ if not isValidGitDir(gitdir):
926
927
os.environ["GIT_DIR"] = gitdir
928
357
-cmd.run(args)
929
+if not cmd.run(args):
930
+ parser.print_help()
931
+