use p4CmdList() to get file contents in Python dicts. This is more robust.
[gitweb.git] / contrib / fast-import / git-p4
index bd1afb296431a30814b46cb25c9b3b6e1491d922..1d799708f6969bc0da91adeea6364176db8b9988 100755 (executable)
@@ -580,7 +580,8 @@ class P4Sync(Command):
                 optparse.make_option("--silent", dest="silent", action="store_true"),
                 optparse.make_option("--detect-labels", dest="detectLabels", action="store_true"),
                 optparse.make_option("--verbose", dest="verbose", action="store_true"),
-                optparse.make_option("--import-local", dest="importIntoRemotes", action="store_false"),
+                optparse.make_option("--import-local", dest="importIntoRemotes", action="store_false",
+                                     help="Import into refs/heads/ , not refs/remotes"),
                 optparse.make_option("--max-changes", dest="maxChanges"),
                 optparse.make_option("--keep-path", dest="keepRepoPath", action='store_true')
         ]
@@ -674,36 +675,28 @@ class P4Sync(Command):
 
     ## Should move this out, doesn't use SELF.
     def readP4Files(self, files):
-        specs = [(f['path'] + "#" + f['rev'], f)  for f in files]
+        specs = [(f['path'] + "#" + f['rev'], f)  for f in files
+                 if f['action'] != 'delete']
 
-        data = read_pipe('p4 print %s' % ' '.join(['"%s"' % spec
-                                                   for (spec, info) in specs]))
-
-        idx = 0
-        for j in range(0, len(specs)):
-            filespec, info = specs[j]
+        if not specs:
+            return
 
-            assert idx < len(data)
-            if data[idx:idx + len(filespec)] != filespec:
-                assert False
-            idx = data.find ('\n', idx)
-            assert idx > 0
-            idx += 1
+        filedata = p4CmdList('print %s' % ' '.join(['"%s"' % path
+                                                for (path, info) in specs]))
 
-            start = idx
+        j = 0;
+        contents = {}
+        while filedata[j:]:
+            stat = filedata[j]
+            text = filedata[j+1]
+            j += 2
 
-            end = -1
-            if j < len(specs)-1:
-                next_spec, next_info = specs[j+1]
-                end = data.find(next_spec, start)
+            assert stat['code'] == 'stat' and text['code'] == 'text'
+            contents[stat['depotFile']] = text['data']
 
-                assert end >= 0
-            else:
-                end = len(data)
-
-            info['data'] = data[start:end]
-            idx = end
-        assert idx == len(data)
+        for f in files:
+            assert not f.has_key('data')
+            f['data'] = contents[f['path']]
 
     def commit(self, details, files, branch, branchPrefixes, parent = ""):
         epoch = details["time"]
@@ -712,6 +705,20 @@ class P4Sync(Command):
         if self.verbose:
             print "commit into %s" % branch
 
+        # start with reading files; if that fails, we should not
+        # create a commit.
+        new_files = []
+        for f in files:
+            if [p for p in branchPrefixes if f['path'].startswith(p)]:
+                new_files.append (f)
+            else:
+                sys.stderr.write("Ignoring file outside of prefix: %s\n" % path)
+        files = new_files
+        self.readP4Files(files)
+
+
+
+
         self.gitStream.write("commit %s\n" % branch)
 #        gitStream.write("mark :%s\n" % details["change"])
         self.committedChanges.add(int(details["change"]))
@@ -739,16 +746,6 @@ class P4Sync(Command):
                 print "parent %s" % parent
             self.gitStream.write("from %s\n" % parent)
 
-
-        new_files = []
-        for f in files:
-            if [p for p in branchPrefixes if f['path'].startswith(p)]:
-                new_files.append (f)
-            else:
-                sys.stderr.write("Ignoring file outside of prefix: %s\n" % path)
-        files = new_files
-
-        self.readP4Files(files)
         for file in files:
             if file["type"] == "apple":
                 print "\nfile %s is a strange apple file that forks. Ignoring!" % file['path']
@@ -820,7 +817,7 @@ class P4Sync(Command):
                            % (labelDetails["label"], change))
 
     def getUserCacheFilename(self):
-        return os.environ["HOME"] + "/.gitp4-usercache.txt")
+        return os.environ["HOME"] + "/.gitp4-usercache.txt"
 
     def getUserMapFromPerforceServer(self):
         if self.userMapFromPerforceServer:
@@ -1030,9 +1027,6 @@ class P4Sync(Command):
 
                 settings = extractSettingsGitLog(logMsg)
 
-                if self.verbose:
-                    print "path %s change %s" % (','.join(depotPaths), change)
-
                 self.readOptions(settings)
                 if (settings.has_key('depot-paths')
                     and settings.has_key ('change')):
@@ -1145,6 +1139,13 @@ class P4Sync(Command):
                                   +  ' '.join(["%s...%s"
                                                % (p, self.revision)
                                                for p in self.depotPaths])):
+
+                if info['code'] == 'error':
+                    sys.stderr.write("p4 returned an error: %s\n"
+                                     % info['data'])
+                    sys.exit(1)
+
+
                 change = int(info["change"])
                 if change > newestRevision:
                     newestRevision = change
@@ -1154,7 +1155,7 @@ class P4Sync(Command):
                     #fileCnt = fileCnt + 1
                     continue
 
-                for prop in [ "depotFile", "rev", "action", "type" ]:
+                for prop in ["depotFile", "rev", "action", "type" ]:
                     details["%s%s" % (prop, fileCnt)] = info[prop]
 
                 fileCnt = fileCnt + 1