@matheusbsbs / matheus-bagliano / commits / 6a49f8e2e0
Fork 0

Read p4 files in one batch.

Signed-off-by: Han-Wen Nienhuys <hanwen@google.com>

Han-Wen Nienhuys committed May 23, 2007 at 18:49 UTC 6a49f8e2e04317175060576d85a5d2062ebb43a4
1 file changed +63 -26
contrib/fast-import/git-p4
+63 -26
@@ -61,6 +61,8 @@ def system(cmd):
61
62 def p4CmdList(cmd):
63 cmd = "p4 -G %s" % cmd
64 + if verbose:
65 + sys.stderr.write("Opening pipe: %s\n" % cmd)
66 pipe = os.popen(cmd, "rb")
67
68 result = []
@@ -609,9 +611,6 @@ class P4Sync(Command):
611 if gitConfig("git-p4.syncFromOrigin") == "false":
612 self.syncWithOrigin = False
613
612 - def p4File(self, depotPath):
613 - return read_pipe("p4 print -q \"%s\"" % depotPath)
614 -
614 def extractFilesFromCommit(self, commit):
615 files = []
616 fnum = 0
@@ -673,6 +672,39 @@ class P4Sync(Command):
672
673 return branches
674
675 + ## Should move this out, doesn't use SELF.
676 + def readP4Files(self, files):
677 + specs = [(f['path'] + "#" + f['rev'], f) for f in files]
678 +
679 + data = read_pipe('p4 print %s' % ' '.join(['"%s"' % spec
680 + for (spec, info) in specs]))
681 +
682 + idx = 0
683 + for j in range(0, len(specs)):
684 + filespec, info = specs[j]
685 +
686 + assert idx < len(data)
687 + if data[idx:idx + len(filespec)] != filespec:
688 + assert False
689 + idx = data.find ('\n', idx)
690 + assert idx > 0
691 + idx += 1
692 +
693 + start = idx
694 +
695 + end = -1
696 + if j < len(specs)-1:
697 + next_spec, next_info = specs[j+1]
698 + end = data.find(next_spec, start)
699 +
700 + assert end >= 0
701 + else:
702 + end = len(specs)
703 +
704 +
705 + info['data'] = data[start:end]
706 + idx = end
707 +
708 def commit(self, details, files, branch, branchPrefixes, parent = ""):
709 epoch = details["time"]
710 author = details["user"]
@@ -681,7 +713,7 @@ class P4Sync(Command):
713 print "commit into %s" % branch
714
715 self.gitStream.write("commit %s\n" % branch)
684 - # gitStream.write("mark :%s\n" % details["change"])
716 +# gitStream.write("mark :%s\n" % details["change"])
717 self.committedChanges.add(int(details["change"]))
718 committer = ""
719 if author not in self.users:
@@ -707,29 +739,30 @@ class P4Sync(Command):
739 print "parent %s" % parent
740 self.gitStream.write("from %s\n" % parent)
741
710 - for file in files:
711 - path = file["path"]
742
743 + new_files = []
744 + for f in files:
745 + if [p for p in branchPrefixes if f['path'].startswith(p)]:
746 + new_files.append (f)
747 + else:
748 + sys.stderr.write("Ignoring file outside of prefix: %s\n" % path)
749 + files = new_files
750
714 - if not [p for p in branchPrefixes if path.startswith(p)]:
715 - continue
716 - rev = file["rev"]
717 - depotPath = path + "#" + rev
718 - relPath = self.stripRepoPath(path, branchPrefixes)
719 - action = file["action"]
720 -
751 + self.readP4Files(files)
752 + for file in files:
753 if file["type"] == "apple":
722 - print "\nfile %s is a strange apple file that forks. Ignoring!" % path
754 + print "\nfile %s is a strange apple file that forks. Ignoring!" % file['path']
755 continue
756
725 - if action == "delete":
757 + relPath = self.stripRepoPath(file['path'], branchPrefixes)
758 + if file["action"] == "delete":
759 self.gitStream.write("D %s\n" % relPath)
760 else:
761 mode = 644
762 if file["type"].startswith("x"):
763 mode = 755
764
732 - data = self.p4File(depotPath)
765 + data = file['data']
766
767 if self.isWindows and file["type"].endswith("text"):
768 data = data.replace("\r\n", "\n")
@@ -971,8 +1004,9 @@ class P4Sync(Command):
1004 if not gitBranchExists(self.refPrefix + "HEAD") and self.importIntoRemotes:
1005 system("git symbolic-ref %sHEAD %s" % (self.refPrefix, self.branch))
1006
974 - ### FIXME
975 - if 1:
1007 + # TODO: should always look at previous commits,
1008 + # merge with previous imports, if possible.
1009 + if args == []:
1010 if self.hasOrigin:
1011 self.createOrUpdateBranchesFromOrigin()
1012 self.listExistingP4GitBranches()
@@ -1046,7 +1080,7 @@ class P4Sync(Command):
1080 self.changeRange = p[atIdx:]
1081 if self.changeRange == "@all":
1082 self.changeRange = ""
1049 - elif self.changeRange.find(",") == -1:
1083 + elif ',' not in self.changeRange:
1084 self.revision = self.changeRange
1085 self.changeRange = ""
1086 p = p[0:atIdx]
@@ -1279,6 +1313,15 @@ class P4Clone(P4Sync):
1313 self.cloneDestination = None
1314 self.needsGit = False
1315
1316 + def defaultDestination(self, args):
1317 + ## TODO: use common prefix of args?
1318 + depotPath = args[0]
1319 + depotDir = re.sub("(@[^@]*)$", "", depotPath)
1320 + depotDir = re.sub("(#[^#]*)$", "", depotDir)
1321 + depotDir = re.sub(r"\.\.\.$,", "", depotDir)
1322 + depotDir = re.sub(r"/$", "", depotDir)
1323 + return os.path.split(depotDir)[1]
1324 +
1325 def run(self, args):
1326 if len(args) < 1:
1327 return False
@@ -1293,13 +1336,7 @@ class P4Clone(P4Sync):
1336 return False
1337
1338 if not self.cloneDestination:
1296 - depotPath = args[0]
1297 - depotDir = re.sub("(@[^@]*)$", "", depotPath)
1298 - depotDir = re.sub("(#[^#]*)$", "", depotDir)
1299 - depotDir = re.sub(r"\.\.\.$,", "", depotDir)
1300 - depotDir = re.sub(r"/$", "", depotDir)
1301 -
1302 - self.cloneDestination = os.path.split(depotDir)[1]
1339 + self.cloneDestination = self.defaultDestination()
1340
1341 print "Importing from %s into %s" % (`depotPaths`, self.cloneDestination)
1342 os.makedirs(self.cloneDestination)