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

git-p4.py
t/t9830-git-p4-symlink-dir.sh [new file with mode: 0755]
index ccfb68105f6f8f626f563c44c8a977d9f667914b..85e8a149cc257e3d42cc75dd77079a55292e46a5 100755 (executable)
--- a/git-p4.py
+++ b/git-p4.py
@@ -25,6 +25,7 @@
 import zipfile
 import zlib
 import ctypes
+import errno
 
 try:
     from subprocess import CalledProcessError
@@ -1543,7 +1544,7 @@ def edit_template(self, template_file):
             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"])
@@ -1558,10 +1559,17 @@ def get_diff_description(self, editedFiles, filesToAdd):
             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')
 
@@ -1579,6 +1587,7 @@ def applyCommit(self, id):
         filesToDelete = set()
         editedFiles = set()
         pureRenameCopy = set()
+        symlinks = set()
         filesToChangeExecBit = {}
 
         for line in diff:
@@ -1595,6 +1604,11 @@ def applyCommit(self, id):
                 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:
@@ -1732,7 +1746,7 @@ def applyCommit(self, id):
         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")
diff --git a/t/t9830-git-p4-symlink-dir.sh b/t/t9830-git-p4-symlink-dir.sh
new file mode 100755 (executable)
index 0000000..3dc528b
--- /dev/null
@@ -0,0 +1,43 @@
+#!/bin/sh
+
+test_description='git p4 symlinked directories'
+
+. ./lib-git-p4.sh
+
+test_expect_success 'start p4d' '
+       start_p4d
+'
+
+test_expect_success 'symlinked directory' '
+       (
+               cd "$cli" &&
+               : >first_file.t &&
+               p4 add first_file.t &&
+               p4 submit -d "first change"
+       ) &&
+       git p4 clone --dest "$git" //depot &&
+       (
+               cd "$git" &&
+               mkdir -p some/sub/directory &&
+               mkdir -p other/subdir2 &&
+               : > other/subdir2/file.t &&
+               (cd some/sub/directory && ln -s ../../../other/subdir2 .) &&
+               git add some other &&
+               git commit -m "symlinks" &&
+               git config git-p4.skipSubmitEdit true &&
+               git p4 submit -v
+       ) &&
+       (
+               cd "$cli" &&
+               p4 sync &&
+               test -L some/sub/directory/subdir2
+               test_path_is_file some/sub/directory/subdir2/file.t
+       )
+
+'
+
+test_expect_success 'kill p4d' '
+       kill_p4d
+'
+
+test_done