Merge branch 'ls/p4-translation-failure' into maint
authorJunio C Hamano <gitster@pobox.com>
Tue, 3 Nov 2015 23:32:31 +0000 (15:32 -0800)
committerJunio C Hamano <gitster@pobox.com>
Tue, 3 Nov 2015 23:32:32 +0000 (15:32 -0800)
Work around "git p4" failing when the P4 depot records the contents
in UTF-16 without UTF-16 BOM.

* ls/p4-translation-failure:
git-p4: handle "Translation of file content failed"
git-p4: add test case for "Translation of file content failed" error

1  2 
git-p4.py
diff --combined git-p4.py
index 2677c89c064d7152451798d29d69b277ede3cbad,3c323510004a7531c84ed84ec36ab5a4903220a8..36c5c55b61b6dc16c27c219099654611a5b085f5
+++ b/git-p4.py
@@@ -134,13 -134,11 +134,11 @@@ def read_pipe(c, ignore_error=False)
          sys.stderr.write('Reading pipe: %s\n' % str(c))
  
      expand = isinstance(c,basestring)
-     p = subprocess.Popen(c, stdout=subprocess.PIPE, shell=expand)
-     pipe = p.stdout
-     val = pipe.read()
-     if p.wait() and not ignore_error:
-         die('Command failed: %s' % str(c))
-     return val
+     p = subprocess.Popen(c, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=expand)
+     (out, err) = p.communicate()
+     if p.returncode != 0 and not ignore_error:
+         die('Command failed: %s\nError: %s' % (str(c), err))
+     return out
  
  def p4_read_pipe(c, ignore_error=False):
      real_cmd = p4_build_cmd(c)
@@@ -2193,10 -2191,17 +2191,17 @@@ class P4Sync(Command, P4UserMap)
              # them back too.  This is not needed to the cygwin windows version,
              # just the native "NT" type.
              #
-             text = p4_read_pipe(['print', '-q', '-o', '-', "%s@%s" % (file['depotFile'], file['change']) ])
-             if p4_version_string().find("/NT") >= 0:
-                 text = text.replace("\r\n", "\n")
-             contents = [ text ]
+             try:
+                 text = p4_read_pipe(['print', '-q', '-o', '-', '%s@%s' % (file['depotFile'], file['change'])])
+             except Exception as e:
+                 if 'Translation of file content failed' in str(e):
+                     type_base = 'binary'
+                 else:
+                     raise e
+             else:
+                 if p4_version_string().find('/NT') >= 0:
+                     text = text.replace('\r\n', '\n')
+                 contents = [ text ]
  
          if type_base == "apple":
              # Apple filetype files will be streamed as a concatenation of
          else:
              return "%s <a@b>" % userid
  
 -    # Stream a p4 tag
      def streamTag(self, gitStream, labelName, labelDetails, commit, epoch):
 +        """ Stream a p4 tag.
 +        commit is either a git commit, or a fast-import mark, ":<p4commit>"
 +        """
 +
          if verbose:
              print "writing tag %s for commit %s" % (labelName, commit)
          gitStream.write("tag %s\n" % labelName)
              self.clientSpecDirs.update_client_spec_path_cache(files)
  
          self.gitStream.write("commit %s\n" % branch)
 -#        gitStream.write("mark :%s\n" % details["change"])
 +        self.gitStream.write("mark :%s\n" % details["change"])
          self.committedChanges.add(int(details["change"]))
          committer = ""
          if author not in self.users:
              if change.has_key('change'):
                  # find the corresponding git commit; take the oldest commit
                  changelist = int(change['change'])
 -                gitCommit = read_pipe(["git", "rev-list", "--max-count=1",
 -                     "--reverse", ":/\[git-p4:.*change = %d\]" % changelist])
 -                if len(gitCommit) == 0:
 -                    print "could not find git commit for changelist %d" % changelist
 -                else:
 -                    gitCommit = gitCommit.strip()
 +                if changelist in self.committedChanges:
 +                    gitCommit = ":%d" % changelist       # use a fast-import mark
                      commitFound = True
 +                else:
 +                    gitCommit = read_pipe(["git", "rev-list", "--max-count=1",
 +                        "--reverse", ":/\[git-p4:.*change = %d\]" % changelist], ignore_error=True)
 +                    if len(gitCommit) == 0:
 +                        print "importing label %s: could not find git commit for changelist %d" % (name, changelist)
 +                    else:
 +                        commitFound = True
 +                        gitCommit = gitCommit.strip()
 +
 +                if commitFound:
                      # Convert from p4 time format
                      try:
                          tmwhen = time.strptime(labelDetails['Update'], "%Y/%m/%d %H:%M:%S")