@matheusbsbs / matheus-bagliano / commits / c65b670e85
Fork 0

git-p4: Detect changes to executable bit and include them in p4 submit.

This changeset takes advantage of the new parseDiffTreeEntry(...) function to detect changes to the execute bit in the git repository. During submit, git-p4 now looks for changes to the executable bit and if it finds them it "reopens" the file in perforce, which allows it to change the file type. The logic for adding the executable bit in perforce is straightforward: the +x modifier can be used. Removing the executable bit in perforce requires that the entire filetype be redefined (there is no way to join remove the bit with a -x modifier, for example). This changeset includes logic to remove the executable bit from the full file type while preserving the base file type and other modifiers. Signed-off-by: Chris Pettitt <cpettitt@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Chris Pettitt committed Nov 1, 2007 at 20:43 UTC c65b670e858a0fc010c8b8bb3cbf064bb22e1839
1 file changed +44
contrib/fast-import/git-p4
+44
@@ -71,6 +71,31 @@ def isP4Exec(kind):
71 a plus sign, it is also executable"""
72 return (re.search(r"(^[cku]?x)|\+.*x", kind) != None)
73
74 +def setP4ExecBit(file, mode):
75 + # Reopens an already open file and changes the execute bit to match
76 + # the execute bit setting in the passed in mode.
77 +
78 + p4Type = "+x"
79 +
80 + if not isModeExec(mode):
81 + p4Type = getP4OpenedType(file)
82 + p4Type = re.sub('^([cku]?)x(.*)', '\\1\\2', p4Type)
83 + p4Type = re.sub('(.*?\+.*?)x(.*?)', '\\1\\2', p4Type)
84 + if p4Type[-1] == "+":
85 + p4Type = p4Type[0:-1]
86 +
87 + system("p4 reopen -t %s %s" % (p4Type, file))
88 +
89 +def getP4OpenedType(file):
90 + # Returns the perforce file type for the given file.
91 +
92 + result = read_pipe("p4 opened %s" % file)
93 + match = re.match(".*\((.+)\)$", result)
94 + if match:
95 + return match.group(1)
96 + else:
97 + die("Could not determine file type for %s" % file)
98 +
99 def diffTreePattern():
100 # This is a simple generator for the diff tree regex pattern. This could be
101 # a class variable if this and parseDiffTreeEntry were a part of a class.
@@ -111,6 +136,14 @@ def parseDiffTreeEntry(entry):
136 }
137 return None
138
139 +def isModeExec(mode):
140 + # Returns True if the given git mode represents an executable file,
141 + # otherwise False.
142 + return mode[-3:] == "755"
143 +
144 +def isModeExecChanged(src_mode, dst_mode):
145 + return isModeExec(src_mode) != isModeExec(dst_mode)
146 +
147 def p4CmdList(cmd, stdin=None, stdin_mode='w+b'):
148 cmd = "p4 -G %s" % cmd
149 if verbose:
@@ -538,15 +571,19 @@ class P4Submit(Command):
571 filesToAdd = set()
572 filesToDelete = set()
573 editedFiles = set()
574 + filesToChangeExecBit = {}
575 for line in diff:
576 diff = parseDiffTreeEntry(line)
577 modifier = diff['status']
578 path = diff['src']
579 if modifier == "M":
580 system("p4 edit \"%s\"" % path)
581 + if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
582 + filesToChangeExecBit[path] = diff['dst_mode']
583 editedFiles.add(path)
584 elif modifier == "A":
585 filesToAdd.add(path)
586 + filesToChangeExecBit[path] = diff['dst_mode']
587 if path in filesToDelete:
588 filesToDelete.remove(path)
589 elif modifier == "D":
@@ -557,6 +594,8 @@ class P4Submit(Command):
594 src, dest = diff['src'], diff['dst']
595 system("p4 integrate -Dt \"%s\" \"%s\"" % (src, dest))
596 system("p4 edit \"%s\"" % (dest))
597 + if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
598 + filesToChangeExecBit[dest] = diff['dst_mode']
599 os.unlink(dest)
600 editedFiles.add(dest)
601 filesToDelete.add(src)
@@ -609,6 +648,11 @@ class P4Submit(Command):
648 system("p4 revert \"%s\"" % f)
649 system("p4 delete \"%s\"" % f)
650
651 + # Set/clear executable bits
652 + for f in filesToChangeExecBit.keys():
653 + mode = filesToChangeExecBit[f]
654 + setP4ExecBit(f, mode)
655 +
656 logMessage = ""
657 if not self.directSubmit:
658 logMessage = extractLogMessageFromGitCommit(id)