Merge branch 'ld/p4-compare-dir-vs-symlink' into maint
authorJunio C Hamano <gitster@pobox.com>
Tue, 17 Jan 2017 23:11:07 +0000 (15:11 -0800)
committerJunio C Hamano <gitster@pobox.com>
Tue, 17 Jan 2017 23:11:08 +0000 (15:11 -0800)
"git p4" misbehaved when swapping a directory and a symbolic link.

* ld/p4-compare-dir-vs-symlink:
git-p4: avoid crash adding symlinked directory

1  2 
git-p4.py
diff --combined git-p4.py
index ccfb68105f6f8f626f563c44c8a977d9f667914b,16d0b8a6ee505ed91245f898597e844496dc5182..85e8a149cc257e3d42cc75dd77079a55292e46a5
+++ b/git-p4.py
@@@ -25,6 -25,7 +25,7 @@@ import sta
  import zipfile
  import zlib
  import ctypes
+ import errno
  
  try:
      from subprocess import CalledProcessError
@@@ -1005,20 -1006,18 +1006,20 @@@ class LargeFileSystem(object)
             steps."""
          if self.exceedsLargeFileThreshold(relPath, contents) or self.hasLargeFileExtension(relPath):
              contentTempFile = self.generateTempFile(contents)
 -            (git_mode, contents, localLargeFile) = self.generatePointer(contentTempFile)
 -
 -            # Move temp file to final location in large file system
 -            largeFileDir = os.path.dirname(localLargeFile)
 -            if not os.path.isdir(largeFileDir):
 -                os.makedirs(largeFileDir)
 -            shutil.move(contentTempFile, localLargeFile)
 -            self.addLargeFile(relPath)
 -            if gitConfigBool('git-p4.largeFilePush'):
 -                self.pushFile(localLargeFile)
 -            if verbose:
 -                sys.stderr.write("%s moved to large file system (%s)\n" % (relPath, localLargeFile))
 +            (pointer_git_mode, contents, localLargeFile) = self.generatePointer(contentTempFile)
 +            if pointer_git_mode:
 +                git_mode = pointer_git_mode
 +            if localLargeFile:
 +                # Move temp file to final location in large file system
 +                largeFileDir = os.path.dirname(localLargeFile)
 +                if not os.path.isdir(largeFileDir):
 +                    os.makedirs(largeFileDir)
 +                shutil.move(contentTempFile, localLargeFile)
 +                self.addLargeFile(relPath)
 +                if gitConfigBool('git-p4.largeFilePush'):
 +                    self.pushFile(localLargeFile)
 +                if verbose:
 +                    sys.stderr.write("%s moved to large file system (%s)\n" % (relPath, localLargeFile))
          return (git_mode, contents)
  
  class MockLFS(LargeFileSystem):
@@@ -1058,9 -1057,6 +1059,9 @@@ class GitLFS(LargeFileSystem)
             the actual content. Return also the new location of the actual
             content.
             """
 +        if os.path.getsize(contentFile) == 0:
 +            return (None, '', None)
 +
          pointerProcess = subprocess.Popen(
              ['git', 'lfs', 'pointer', '--file=' + contentFile],
              stdout=subprocess.PIPE
@@@ -1543,7 -1539,7 +1544,7 @@@ class P4Submit(Command, P4UserMap)
              if response == 'n':
                  return False
  
-     def get_diff_description(self, editedFiles, filesToAdd):
+     def get_diff_description(self, editedFiles, filesToAdd, symlinks):
          # diff
          if os.environ.has_key("P4DIFF"):
              del(os.environ["P4DIFF"])
              newdiff += "==== new file ====\n"
              newdiff += "--- /dev/null\n"
              newdiff += "+++ %s\n" % newFile
-             f = open(newFile, "r")
-             for line in f.readlines():
-                 newdiff += "+" + line
-             f.close()
+             is_link = os.path.islink(newFile)
+             expect_link = newFile in symlinks
+             if is_link and expect_link:
+                 newdiff += "+%s\n" % os.readlink(newFile)
+             else:
+                 f = open(newFile, "r")
+                 for line in f.readlines():
+                     newdiff += "+" + line
+                 f.close()
  
          return (diff + newdiff).replace('\r\n', '\n')
  
          filesToDelete = set()
          editedFiles = set()
          pureRenameCopy = set()
+         symlinks = set()
          filesToChangeExecBit = {}
  
          for line in diff:
                  filesToChangeExecBit[path] = diff['dst_mode']
                  if path in filesToDelete:
                      filesToDelete.remove(path)
+                 dst_mode = int(diff['dst_mode'], 8)
+                 if dst_mode == 0120000:
+                     symlinks.add(path)
              elif modifier == "D":
                  filesToDelete.add(path)
                  if path in filesToAdd:
          separatorLine = "######## everything below this line is just the diff #######\n"
          if not self.prepare_p4_only:
              submitTemplate += separatorLine
-             submitTemplate += self.get_diff_description(editedFiles, filesToAdd)
+             submitTemplate += self.get_diff_description(editedFiles, filesToAdd, symlinks)
  
          (handle, fileName) = tempfile.mkstemp()
          tmpFile = os.fdopen(handle, "w+b")