Merge branch 'ak/p4'
authorJunio C Hamano <gitster@pobox.com>
Sun, 17 Aug 2008 17:53:57 +0000 (10:53 -0700)
committerJunio C Hamano <gitster@pobox.com>
Sun, 17 Aug 2008 17:53:57 +0000 (10:53 -0700)
* ak/p4:
Utilise our new p4_read_pipe and p4_write_pipe wrappers
Add p4 read_pipe and write_pipe wrappers
Put in the two other configuration elements found in the source
Put some documentation in about the parameters that have been added
Move git-p4.syncFromOrigin into a configuration parameters section
Consistently use 'git-p4' for the configuration entries
If the user has configured various parameters, use them.
Switch to using 'p4_build_cmd'
If we are in verbose mode, output what we are about to run (or return)
Add a single command that will be used to construct the 'p4' command
Utilise the new 'p4_system' function.
Have a command that specifically invokes 'p4' (via system)
Utilise the new 'p4_read_pipe_lines' command
Create a specific version of the read_pipe_lines command for p4 invocations

Conflicts:
contrib/fast-import/git-p4

1  2 
contrib/fast-import/git-p4
index 3f2303dcf0267d17b66e37f0c06da60cef8fe70b,12fa9d3fd81ce83f53f36178bc3111edddfd2173..f9865b444fdf8b21211b04e9dba2999dd90cbc23
@@@ -16,11 -16,41 +16,46 @@@ from sets import Set
  
  verbose = False
  
+ def p4_build_cmd(cmd):
+     """Build a suitable p4 command line.
+     This consolidates building and returning a p4 command line into one
+     location. It means that hooking into the environment, or other configuration
+     can be done more easily.
+     """
+     real_cmd = "%s " % "p4"
+     user = gitConfig("git-p4.user")
+     if len(user) > 0:
+         real_cmd += "-u %s " % user
+     password = gitConfig("git-p4.password")
+     if len(password) > 0:
+         real_cmd += "-P %s " % password
+     port = gitConfig("git-p4.port")
+     if len(port) > 0:
+         real_cmd += "-p %s " % port
+     host = gitConfig("git-p4.host")
+     if len(host) > 0:
+         real_cmd += "-h %s " % host
+     client = gitConfig("git-p4.client")
+     if len(client) > 0:
+         real_cmd += "-c %s " % client
+     real_cmd += "%s" % (cmd)
+     if verbose:
+         print real_cmd
+     return real_cmd
 +def chdir(dir):
 +    if os.name == 'nt':
 +        os.environ['PWD']=dir
 +    os.chdir(dir)
 +
  def die(msg):
      if verbose:
          raise Exception(msg)
@@@ -39,6 -69,10 +74,10 @@@ def write_pipe(c, str)
  
      return val
  
+ def p4_write_pipe(c, str):
+     real_cmd = p4_build_cmd(c)
+     return write_pipe(c, str)
  def read_pipe(c, ignore_error=False):
      if verbose:
          sys.stderr.write('Reading pipe: %s\n' % c)
@@@ -50,6 -84,9 +89,9 @@@
  
      return val
  
+ def p4_read_pipe(c, ignore_error=False):
+     real_cmd = p4_build_cmd(c)
+     return read_pipe(real_cmd, ignore_error)
  
  def read_pipe_lines(c):
      if verbose:
  
      return val
  
+ def p4_read_pipe_lines(c):
+     """Specifically invoke p4 on the command supplied. """
+     real_cmd = p4_build_cmd(c)
+     return read_pipe_lines(real_cmd)
  def system(cmd):
      if verbose:
          sys.stderr.write("executing %s\n" % cmd)
      if os.system(cmd) != 0:
          die("command failed: %s" % cmd)
  
+ def p4_system(cmd):
+     """Specifically invoke p4 as the system command. """
+     real_cmd = p4_build_cmd(cmd)
+     return system(real_cmd)
  def isP4Exec(kind):
      """Determine if a Perforce 'kind' should have execute permission
  
@@@ -89,12 -136,12 +141,12 @@@ def setP4ExecBit(file, mode)
          if p4Type[-1] == "+":
              p4Type = p4Type[0:-1]
  
-     system("p4 reopen -t %s %s" % (p4Type, file))
+     p4_system("reopen -t %s %s" % (p4Type, file))
  
  def getP4OpenedType(file):
      # Returns the perforce file type for the given file.
  
-     result = read_pipe("p4 opened %s" % file)
+     result = p4_read_pipe("opened %s" % file)
      match = re.match(".*\((.+)\)\r?$", result)
      if match:
          return match.group(1)
@@@ -150,7 -197,7 +202,7 @@@ def isModeExecChanged(src_mode, dst_mod
      return isModeExec(src_mode) != isModeExec(dst_mode)
  
  def p4CmdList(cmd, stdin=None, stdin_mode='w+b'):
-     cmd = "p4 -G %s" % cmd
+     cmd = p4_build_cmd("-G %s" % (cmd))
      if verbose:
          sys.stderr.write("Opening pipe: %s\n" % cmd)
  
@@@ -369,7 -416,7 +421,7 @@@ def originP4BranchesExist()
  
  def p4ChangesForPaths(depotPaths, changeRange):
      assert depotPaths
-     output = read_pipe_lines("p4 changes " + ' '.join (["%s...%s" % (p, changeRange)
+     output = p4_read_pipe_lines("changes " + ' '.join (["%s...%s" % (p, changeRange)
                                                          for p in depotPaths]))
  
      changes = []
@@@ -517,7 -564,7 +569,7 @@@ class P4Submit(Command)
          # remove lines in the Files section that show changes to files outside the depot path we're committing into
          template = ""
          inFilesSection = False
-         for line in read_pipe_lines("p4 change -o"):
+         for line in p4_read_pipe_lines("change -o"):
              if line.endswith("\r\n"):
                  line = line[:-2] + "\n"
              if inFilesSection:
              modifier = diff['status']
              path = diff['src']
              if modifier == "M":
-                 system("p4 edit \"%s\"" % path)
+                 p4_system("edit \"%s\"" % path)
                  if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
                      filesToChangeExecBit[path] = diff['dst_mode']
                  editedFiles.add(path)
                      filesToAdd.remove(path)
              elif modifier == "R":
                  src, dest = diff['src'], diff['dst']
-                 system("p4 integrate -Dt \"%s\" \"%s\"" % (src, dest))
-                 system("p4 edit \"%s\"" % (dest))
+                 p4_system("integrate -Dt \"%s\" \"%s\"" % (src, dest))
+                 p4_system("edit \"%s\"" % (dest))
                  if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
                      filesToChangeExecBit[dest] = diff['dst_mode']
                  os.unlink(dest)
              if response == "s":
                  print "Skipping! Good luck with the next patches..."
                  for f in editedFiles:
-                     system("p4 revert \"%s\"" % f);
+                     p4_system("revert \"%s\"" % f);
                  for f in filesToAdd:
                      system("rm %s" %f)
                  return
          system(applyPatchCmd)
  
          for f in filesToAdd:
-             system("p4 add \"%s\"" % f)
+             p4_system("add \"%s\"" % f)
          for f in filesToDelete:
-             system("p4 revert \"%s\"" % f)
-             system("p4 delete \"%s\"" % f)
+             p4_system("revert \"%s\"" % f)
+             p4_system("delete \"%s\"" % f)
  
          # Set/clear executable bits
          for f in filesToChangeExecBit.keys():
              submitTemplate = self.prepareLogMessage(template, logMessage)
              if os.environ.has_key("P4DIFF"):
                  del(os.environ["P4DIFF"])
-             diff = read_pipe("p4 diff -du ...")
+             diff = p4_read_pipe("diff -du ...")
  
              newdiff = ""
              for newFile in filesToAdd:
              if self.isWindows:
                  submitTemplate = submitTemplate.replace("\r\n", "\n")
  
-             write_pipe("p4 submit -i", submitTemplate)
+             p4_write_pipe("submit -i", submitTemplate)
          else:
              fileName = "submit.txt"
              file = open(fileName, "w+")
          print "Perforce checkout for depot path %s located at %s" % (self.depotPath, self.clientPath)
          self.oldWorkingDirectory = os.getcwd()
  
 -        os.chdir(self.clientPath)
 +        chdir(self.clientPath)
          print "Syncronizing p4 checkout..."
-         system("p4 sync ...")
+         p4_system("sync ...")
  
          self.check()
  
  
          if len(commits) == 0:
              print "All changes applied!"
 -            os.chdir(self.oldWorkingDirectory)
 +            chdir(self.oldWorkingDirectory)
  
              sync = P4Sync()
              sync.run([])
@@@ -1404,7 -1451,7 +1456,7 @@@ class P4Sync(Command)
              if not gitBranchExists(self.refPrefix + "HEAD") and self.importIntoRemotes and gitBranchExists(self.branch):
                  system("git symbolic-ref %sHEAD %s" % (self.refPrefix, self.branch))
  
-         if self.useClientSpec or gitConfig("p4.useclientspec") == "true":
+         if self.useClientSpec or gitConfig("git-p4.useclientspec") == "true":
              self.getClientSpec()
  
          # TODO: should always look at previous commits,
@@@ -1675,7 -1722,7 +1727,7 @@@ class P4Clone(P4Sync)
          print "Importing from %s into %s" % (', '.join(depotPaths), self.cloneDestination)
          if not os.path.exists(self.cloneDestination):
              os.makedirs(self.cloneDestination)
 -        os.chdir(self.cloneDestination)
 +        chdir(self.cloneDestination)
          system("git init")
          self.gitdir = os.getcwd() + "/.git"
          if not P4Sync.run(self, depotPaths):
@@@ -1787,7 -1834,7 +1839,7 @@@ def main()
                  if os.path.exists(cmd.gitdir):
                      cdup = read_pipe("git rev-parse --show-cdup").strip()
                      if len(cdup) > 0:
 -                        os.chdir(cdup);
 +                        chdir(cdup);
  
          if not isValidGitDir(cmd.gitdir):
              if isValidGitDir(cmd.gitdir + "/.git"):