Code

Fix typo in listExistingP4Branches that broke sync.
[git.git] / contrib / fast-import / git-p4
index 400edce459107aea7667d3008f81c9c0e9254589..794286ee8e43394c93c0aae66d540efc2da8d26e 100755 (executable)
 import optparse, sys, os, marshal, popen2, subprocess, shelve
 import tempfile, getopt, sha, os.path, time, platform
 import re
+
 from sets import Set;
 
 gitdir = os.environ.get("GIT_DIR", "")
+silent = True
+
+def write_pipe(c, str):
+    if not silent:
+        sys.stderr.write('writing pipe: %s\n' % c)
+
+    pipe = os.popen(c, 'w')
+    val = pipe.write(str)
+    if pipe.close():
+        sys.stderr.write('Command failed: %s' % c)
+        sys.exit(1)
+
+    return val
+
+def read_pipe(c):
+    if not silent:
+        sys.stderr.write('reading pipe: %s\n' % c)
+
+    pipe = os.popen(c, 'rb')
+    val = pipe.read()
+    if pipe.close():
+        sys.stderr.write('Command failed: %s' % c)
+        sys.exit(1)
+
+    return val
 
-def mypopen(command):
-    return os.popen(command, "rb");
+
+def read_pipe_lines(c):
+    if not silent:
+        sys.stderr.write('reading pipe: %s\n' % c)
+    ## todo: check return status
+    pipe = os.popen(c, 'rb')
+    val = pipe.readlines()
+    if pipe.close():
+        sys.stderr.write('Command failed: %s' % c)
+        sys.exit(1)
+
+    return val
+
+def system(cmd):
+    if not silent:
+        sys.stderr.write("executing %s" % cmd)
+    if os.system(cmd) != 0:
+        die("command failed: %s" % cmd)
 
 def p4CmdList(cmd):
     cmd = "p4 -G %s" % cmd
@@ -67,7 +109,7 @@ def die(msg):
     sys.exit(1)
 
 def currentGitBranch():
-    return mypopen("git name-rev HEAD").read().split(" ")[1][:-1]
+    return read_pipe("git name-rev HEAD").split(" ")[1].strip()
 
 def isValidGitDir(path):
     if os.path.exists(path + "/HEAD") and os.path.exists(path + "/refs") and os.path.exists(path + "/objects"):
@@ -75,16 +117,14 @@ def isValidGitDir(path):
     return False
 
 def parseRevision(ref):
-    return mypopen("git rev-parse %s" % ref).read()[:-1]
-
-def system(cmd):
-    if os.system(cmd) != 0:
-        die("command failed: %s" % cmd)
+    return read_pipe("git rev-parse %s" % ref).strip()
 
 def extractLogMessageFromGitCommit(commit):
     logMessage = ""
+
+    ## fixme: title is first line of commit, not 1st paragraph.
     foundTitle = False
-    for log in mypopen("git cat-file commit %s" % commit).readlines():
+    for log in read_pipe_lines("git cat-file commit %s" % commit):
        if not foundTitle:
            if len(log) == 1:
                foundTitle = True
@@ -117,7 +157,7 @@ def gitBranchExists(branch):
     return proc.wait() == 0;
 
 def gitConfig(key):
-    return mypopen("git config %s" % key).read()[:-1]
+    return os.popen("git config %s" % key, "rb").read()[:-1]
 
 class Command:
     def __init__(self):
@@ -158,14 +198,15 @@ class P4RollBack(Command):
 
         if self.rollbackLocalBranches:
             refPrefix = "refs/heads/"
-            lines = mypopen("git rev-parse --symbolic --branches").readlines()
+            lines = read_pipe_lines("git rev-parse --symbolic --branches")
         else:
             refPrefix = "refs/remotes/"
-            lines = mypopen("git rev-parse --symbolic --remotes").readlines()
+            lines = read_pipe_lines("git rev-parse --symbolic --remotes")
 
         for line in lines:
             if self.rollbackLocalBranches or (line.startswith("p4/") and line != "p4/HEAD\n"):
-                ref = refPrefix + line[:-1]
+                line = line.strip()
+                ref = refPrefix + line
                 log = extractLogMessageFromGitCommit(ref)
                 depotPath, change = extractDepotPathAndChangeFromGitLog(log)
                 changed = False
@@ -230,8 +271,8 @@ class P4Submit(Command):
         if self.directSubmit:
             commits.append("0")
         else:
-            for line in mypopen("git rev-list --no-merges %s..%s" % (self.origin, self.master)).readlines():
-                commits.append(line[:-1])
+            for line in read_pipe_lines("git rev-list --no-merges %s..%s" % (self.origin, self.master)):
+                commits.append(line.strip())
             commits.reverse()
 
         self.config["commits"] = commits
@@ -264,8 +305,8 @@ class P4Submit(Command):
             print "Applying local change in working directory/index"
             diff = self.diffStatus
         else:
-            print "Applying %s" % (mypopen("git log --max-count=1 --pretty=oneline %s" % id).read())
-            diff = mypopen("git diff-tree -r --name-status \"%s^\" \"%s\"" % (id, id)).readlines()
+            print "Applying %s" % (read_pipe("git log --max-count=1 --pretty=oneline %s" % id))
+            diff = read_pipe_lines("git diff-tree -r --name-status \"%s^\" \"%s\"" % (id, id))
         filesToAdd = set()
         filesToDelete = set()
         editedFiles = set()
@@ -332,13 +373,13 @@ class P4Submit(Command):
         if not self.directSubmit:
             logMessage = extractLogMessageFromGitCommit(id)
             logMessage = logMessage.replace("\n", "\n\t")
-            logMessage = logMessage[:-1]
+            logMessage = logMessage.strip()
 
-        template = mypopen("p4 change -o").read()
+        template = read_pipe("p4 change -o")
 
         if self.interactive:
             submitTemplate = self.prepareLogMessage(template, logMessage)
-            diff = mypopen("p4 diff -du ...").read()
+            diff = read_pipe("p4 diff -du ...")
 
             for newFile in filesToAdd:
                 diff += "==== new file ====\n"
@@ -387,14 +428,10 @@ class P4Submit(Command):
                    if self.directSubmit:
                        print "Submitting to git first"
                        os.chdir(self.oldWorkingDirectory)
-                       pipe = os.popen("git commit -a -F -", "wb")
-                       pipe.write(submitTemplate)
-                       pipe.close()
+                       write_pipe("git commit -a -F -", submitTemplate)
                        os.chdir(self.clientPath)
 
-                   pipe = os.popen("p4 submit -i", "wb")
-                   pipe.write(submitTemplate)
-                   pipe.close()
+                   write_pipe("p4 submit -i", submitTemplate)
             elif response == "s":
                 for f in editedFiles:
                     system("p4 revert \"%s\"" % f);
@@ -451,11 +488,11 @@ class P4Submit(Command):
         self.oldWorkingDirectory = os.getcwd()
 
         if self.directSubmit:
-            self.diffStatus = mypopen("git diff -r --name-status HEAD").readlines()
+            self.diffStatus = read_pipe_lines("git diff -r --name-status HEAD")
             if len(self.diffStatus) == 0:
                 print "No changes in working directory to submit."
                 return True
-            patch = mypopen("git diff -p --binary --diff-filter=ACMRTUXB HEAD").read()
+            patch = read_pipe("git diff -p --binary --diff-filter=ACMRTUXB HEAD")
             self.diffFile = gitdir + "/p4-git-diff"
             f = open(self.diffFile, "wb")
             f.write(patch)
@@ -477,7 +514,7 @@ class P4Submit(Command):
 
         if len(self.substFile) > 0:
             for line in open(self.substFile, "r").readlines():
-                tokens = line[:-1].split("=")
+                tokens = line.strip().split("=")
                 self.logSubstitutions[tokens[0]] = tokens[1]
 
         self.check()
@@ -528,7 +565,8 @@ class P4Sync(Command):
                 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("--max-changes", dest="maxChanges")
+                optparse.make_option("--max-changes", dest="maxChanges"),
+                optparse.make_option("--keep-path", dest="keepRepoPath")
         ]
         self.description = """Imports from Perforce into a git repository.\n
     example:
@@ -539,7 +577,6 @@ class P4Sync(Command):
     (a ... is not needed in the path p4 specification, it's added implicitly)"""
 
         self.usage += " //depot/path[@revRange]"
-
         self.silent = False
         self.createdBranches = Set()
         self.committedChanges = Set()
@@ -552,12 +589,14 @@ class P4Sync(Command):
         self.importIntoRemotes = True
         self.maxChanges = ""
         self.isWindows = (platform.system() == "Windows")
+        self.depotPath = None
+        self.keepRepoPath = False
 
         if gitConfig("git-p4.syncFromOrigin") == "false":
             self.syncWithOrigin = False
 
     def p4File(self, depotPath):
-        return os.popen("p4 print -q \"%s\"" % depotPath, "rb").read()
+        return read_pipe("p4 print -q \"%s\"" % depotPath)
 
     def extractFilesFromCommit(self, commit):
         files = []
@@ -579,9 +618,14 @@ class P4Sync(Command):
             fnum = fnum + 1
         return files
 
+    def stripRepoPath(self, path, prefix):
+        if self.keepRepoPath:
+            prefix = re.sub("^(//[^/]+/).*", r'\1', prefix)
+
+        return path[len(prefix):]
+
     def splitFilesIntoBranches(self, commit):
         branches = {}
-
         fnum = 0
         while commit.has_key("depotFile%s" % fnum):
             path =  commit["depotFile%s" % fnum]
@@ -598,10 +642,12 @@ class P4Sync(Command):
             file["type"] = commit["type%s" % fnum]
             fnum = fnum + 1
 
-            relPath = path[len(self.depotPath):]
+            relPath = self.stripRepoPath(path, self.depotPath)
 
             for branch in self.knownBranches.keys():
-                if relPath.startswith(branch + "/"): # add a trailing slash so that a commit into qt/4.2foo doesn't end up in qt/4.2
+
+                # add a trailing slash so that a commit into qt/4.2foo doesn't end up in qt/4.2
+                if relPath.startswith(branch + "/"):
                     if branch not in branches:
                         branches[branch] = []
                     branches[branch].append(file)
@@ -645,7 +691,7 @@ class P4Sync(Command):
                 continue
             rev = file["rev"]
             depotPath = path + "#" + rev
-            relPath = path[len(branchPrefix):]
+            relPath = self.stripRepoPath(path, branchPrefix)
             action = file["action"]
 
             if file["type"] == "apple":
@@ -739,7 +785,7 @@ class P4Sync(Command):
             lines = cache.readlines()
             cache.close()
             for line in lines:
-                entry = line[:-1].split("\t")
+                entry = line.strip().split("\t")
                 self.users[entry[0]] = entry[1]
         except IOError:
             self.getUserMapFromPerforceServer()
@@ -769,7 +815,7 @@ class P4Sync(Command):
             print "Label changes: %s" % self.labels.keys()
 
     def getBranchMapping(self):
-        self.projectName = self.depotPath[self.depotPath[:-1].rfind("/") + 1:]
+        self.projectName = self.depotPath[self.depotPath.strip().rfind("/") + 1:]
 
         for info in p4CmdList("branches"):
             details = p4Cmd("branch -o %s" % info["branch"])
@@ -799,22 +845,23 @@ class P4Sync(Command):
         else:
             cmdline += " --branches"
 
-        for line in mypopen(cmdline).readlines():
-            if self.importIntoRemotes and ((not line.startswith("p4/")) or line == "p4/HEAD\n"):
+        for line in read_pipe_lines(cmdline):
+            line = line.strip()
+            if self.importIntoRemotes and ((not line.startswith("p4/")) or line == "p4/HEAD"):
                 continue
+
             if self.importIntoRemotes:
                 # strip off p4
-                branch = line[3:-1]
-            else:
-                branch = line[:-1]
+                branch = re.sub ("^p4/", "", line)
+
             self.p4BranchesInGit.append(branch)
-            self.initialParents[self.refPrefix + branch] = parseRevision(line[:-1])
+            self.initialParents[self.refPrefix + branch] = parseRevision(line)
 
     def createOrUpdateBranchesFromOrigin(self):
         if not self.silent:
             print "Creating/updating branch(es) in %s based on origin branch(es)" % self.refPrefix
 
-        for line in mypopen("git rev-parse --symbolic --remotes"):
+        for line in read_pipe_lines("git rev-parse --symbolic --remotes"):
             if (not line.startswith("origin/")) or line.endswith("HEAD\n"):
                 continue
 
@@ -921,7 +968,7 @@ class P4Sync(Command):
             self.branch = "refs/heads/" + self.branch
 
         if len(self.depotPath) != 0:
-            self.depotPath = self.depotPath[:-1]
+            self.depotPath = self.depotPath.strip()
 
         if len(args) == 0 and len(self.depotPath) != 0:
             if not self.silent:
@@ -954,9 +1001,7 @@ class P4Sync(Command):
         elif len(self.previousDepotPath) == 0:
             self.revision = "#head"
 
-        if self.depotPath.endswith("..."):
-            self.depotPath = self.depotPath[:-3]
-
+        self.depotPath = re.sub ("\.\.\.$", "", self.depotPath)
         if not self.depotPath.endswith("/"):
             self.depotPath += "/"
 
@@ -1032,7 +1077,7 @@ class P4Sync(Command):
             else:
                 if self.verbose:
                     print "Getting p4 changes for %s...%s" % (self.depotPath, self.changeRange)
-                output = mypopen("p4 changes %s...%s" % (self.depotPath, self.changeRange)).readlines()
+                output = read_pipe_lines("p4 changes %s...%s" % (self.depotPath, self.changeRange))
 
                 for line in output:
                     changeNum = line.split(" ")[1]
@@ -1141,7 +1186,7 @@ class P4Rebase(Command):
         sync = P4Sync()
         sync.run([])
         print "Rebasing the current branch"
-        oldHead = mypopen("git rev-parse HEAD").read()[:-1]
+        oldHead = read_pipe("git rev-parse HEAD").strip()
         system("git rebase p4")
         system("git diff-tree --stat --summary -M %s HEAD" % oldHead)
         return True
@@ -1174,7 +1219,7 @@ class P4Clone(P4Sync):
         depotDir = re.sub(r"/$", "", depotDir)
 
         if not destination:
-            destination = os.path.split(depotDir)[-1]
+            destination = os.path.split(depotDir)[1]
 
         print "Importing from %s into %s" % (depotPath, destination)
         os.makedirs(destination)
@@ -1252,9 +1297,9 @@ if cmd.needsGit:
     if len(gitdir) == 0:
         gitdir = ".git"
         if not isValidGitDir(gitdir):
-            gitdir = mypopen("git rev-parse --git-dir").read()[:-1]
+            gitdir = read_pipe("git rev-parse --git-dir").strip()
             if os.path.exists(gitdir):
-                cdup = mypopen("git rev-parse --show-cdup").read()[:-1];
+                cdup = read_pipe("git rev-parse --show-cdup").strip()
                 if len(cdup) > 0:
                     os.chdir(cdup);
 
@@ -1268,4 +1313,3 @@ if cmd.needsGit:
 
 if not cmd.run(args):
     parser.print_help()
-