]> git.ipfire.org Git - thirdparty/git.git/blobdiff - contrib/fast-import/git-p4
Exclude the HEAD symbolic ref from the list of known branches
[thirdparty/git.git] / contrib / fast-import / git-p4
index 5dae1f19ae9180a2a4b365295352e491936dcecb..965b391cda870ecd7d174adb00151ccbbcdf36d4 100755 (executable)
@@ -153,7 +153,10 @@ def extractSettingsGitLog(log):
 
             values[key] = val
 
-    values['depot-paths'] = values.get("depot-paths").split(',')
+    paths = values.get("depot-paths")
+    if not paths:
+        paths = values.get("depot-path")
+    values['depot-paths'] = paths.split(',')
     return values
 
 def gitBranchExists(branch):
@@ -173,13 +176,18 @@ class P4Debug(Command):
     def __init__(self):
         Command.__init__(self)
         self.options = [
-            optparse.make_option("--verbose", dest="verbose", action="store_true"),
+            optparse.make_option("--verbose", dest="verbose", action="store_true",
+                                 default=False),
             ]
         self.description = "A tool to debug the output of p4 -G."
         self.needsGit = False
+        self.verbose = False
 
     def run(self, args):
+        j = 0
         for output in p4CmdList(" ".join(args)):
+            print 'Element: %d' % j
+            j += 1
             print output
         return True
 
@@ -268,6 +276,8 @@ class P4Submit(Command):
         self.origin = ""
         self.directSubmit = False
         self.trustMeLikeAFool = False
+        self.verbose = False
+        self.isWindows = (platform.system() == "Windows")
 
         self.logSubstitutions = {}
         self.logSubstitutions["<enter description here>"] = "%log%"
@@ -389,6 +399,8 @@ class P4Submit(Command):
         if not self.directSubmit:
             logMessage = extractLogMessageFromGitCommit(id)
             logMessage = logMessage.replace("\n", "\n\t")
+            if self.isWindows:
+                logMessage = logMessage.replace("\n", "\r\n")
             logMessage = logMessage.strip()
 
         template = read_pipe("p4 change -o")
@@ -435,6 +447,8 @@ class P4Submit(Command):
                     tmpFile.close()
                     os.remove(fileName)
                     submitTemplate = message[:message.index(separatorLine)]
+                    if self.isWindows:
+                        submitTemplate = submitTemplate.replace("\r\n", "\n")
 
             if response == "y" or response == "yes":
                if self.dryRun:
@@ -470,9 +484,6 @@ class P4Submit(Command):
                    % (fileName, fileName))
 
     def run(self, args):
-        # make gitdir absolute so we can cd out into the perforce checkout
-        os.environ["GIT_DIR"] = gitdir
-
         if len(args) == 0:
             self.master = currentGitBranch()
             if len(self.master) == 0 or not gitBranchExists("refs/heads/%s" % self.master):
@@ -488,7 +499,7 @@ class P4Submit(Command):
             settings = extractSettingsGitLog(extractLogMessageFromGitCommit("p4"))
         if len(depotPath) == 0 and gitBranchExists("origin"):
             settings = extractSettingsGitLog(extractLogMessageFromGitCommit("origin"))
-        depotPaths = settings['depot-paths']
+        depotPath = settings['depot-paths'][0]
 
         if len(depotPath) == 0:
             print "Internal error: cannot locate perforce depot path from existing branches"
@@ -676,24 +687,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
+        files = [f for f in files
                  if f['action'] != 'delete']
 
-        if not specs:
+        if not files:
             return
 
-        filedata = p4CmdList('print %s' % ' '.join(['"%s"' % path
-                                                for (path, info) in specs]))
+        filedata = p4CmdList('print %s' % ' '.join(['"%s#%s"' % (f['path'],
+                                                                 f['rev'])
+                                                    for f in files]))
 
         j = 0;
         contents = {}
-        while filedata[j:]:
+        while j < len(filedata):
             stat = filedata[j]
-            text = filedata[j+1]
-            j += 2
+            j += 1
+            text = ''
+            while j < len(filedata) and filedata[j]['code'] in ('text',
+                                                                'binary'):
+                text += filedata[j]['data']
+                j += 1
 
-            assert stat['code'] == 'stat' and text['code'] == 'text'
-            contents[stat['depotFile']] = text['data']
+            contents[stat['depotFile']] = text
 
         for f in files:
             assert not f.has_key('data')
@@ -765,7 +780,7 @@ class P4Sync(Command):
                 if self.isWindows and file["type"].endswith("text"):
                     data = data.replace("\r\n", "\n")
 
-                self.gitStream.write("M %s inline %s\n" % (mode, relPath))
+                self.gitStream.write("M %d inline %s\n" % (mode, relPath))
                 self.gitStream.write("data %s\n" % len(data))
                 self.gitStream.write(data)
                 self.gitStream.write("\n")
@@ -897,9 +912,10 @@ class P4Sync(Command):
                     continue
                 source = paths[0]
                 destination = paths[1]
-                if source.startswith(self.depotPath) and destination.startswith(self.depotPath):
-                    source = source[len(self.depotPath):-4]
-                    destination = destination[len(self.depotPath):-4]
+                ## HACK
+                if source.startswith(self.depotPaths[0]) and destination.startswith(self.depotPaths[0]):
+                    source = source[len(self.depotPaths[0]):-4]
+                    destination = destination[len(self.depotPaths[0]):-4]
                     if destination not in self.knownBranches:
                         self.knownBranches[destination] = source
                     if source not in self.knownBranches:
@@ -918,7 +934,7 @@ class P4Sync(Command):
             line = line.strip()
 
             ## only import to p4/
-            if not line.startswith('p4/'):
+            if not line.startswith('p4/') or line == "p4/HEAD":
                 continue
             branch = line
             if self.importIntoRemotes:
@@ -998,7 +1014,7 @@ class P4Sync(Command):
         if self.importIntoRemotes:
             self.refPrefix = "refs/remotes/p4/"
         else:
-            self.refPrefix = "refs/heads/"
+            self.refPrefix = "refs/heads/p4/"
 
         if self.syncWithOrigin and self.hasOrigin:
             if not self.silent:
@@ -1006,7 +1022,7 @@ class P4Sync(Command):
             system("git fetch origin")
 
         if len(self.branch) == 0:
-            self.branch = self.refPrefix + "p4/master"
+            self.branch = self.refPrefix + "master"
             if gitBranchExists("refs/heads/p4") and self.importIntoRemotes:
                 system("git update-ref %s refs/heads/p4" % self.branch)
                 system("git branch -D p4");
@@ -1047,18 +1063,20 @@ class P4Sync(Command):
                     else:
                         paths = []
                         for (prev, cur) in zip(self.previousDepotPaths, depotPaths):
-                            for i in range(0, max(len(cur), len(prev))):
+                            for i in range(0, min(len(cur), len(prev))):
                                 if cur[i] <> prev[i]:
+                                    i = i - 1
                                     break
 
-                            paths.append (cur[:i])
+                            paths.append (cur[:i + 1])
 
                         self.previousDepotPaths = paths
 
             if p4Change > 0:
                 self.depotPaths = sorted(self.previousDepotPaths)
                 self.changeRange = "@%s,#head" % p4Change
-                self.initialParent = parseRevision(self.branch)
+                if not self.detectBranches:
+                    self.initialParent = parseRevision(self.branch)
                 if not self.silent and not self.detectBranches:
                     print "Performing incremental import into %s git branch" % self.branch
 
@@ -1305,6 +1323,7 @@ class P4Rebase(Command):
         self.options = [ ]
         self.description = ("Fetches the latest revision from perforce and "
                             + "rebases the current work (branch) against it")
+        self.verbose = False
 
     def run(self, args):
         sync = P4Sync()
@@ -1350,7 +1369,7 @@ class P4Clone(P4Sync):
                 return False
 
         if not self.cloneDestination:
-            self.cloneDestination = self.defaultDestination()
+            self.cloneDestination = self.defaultDestination(args)
 
         print "Importing from %s into %s" % (', '.join(depotPaths), self.cloneDestination)
         os.makedirs(self.cloneDestination)