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

git-p4: Add a helper function to parse the full git diff-tree output.

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 b43b0a3c5c313619397b2f81d5a4acad2c3cb4a9
1 file changed +45 -4
contrib/fast-import/git-p4
+45 -4
@@ -71,6 +71,46 @@ def isP4Exec(kind):
71 a plus sign, it is also executable"""
72 return (re.search(r"(^[cku]?x)|\+.*x", kind) != None)
73
74 +def diffTreePattern():
75 + # This is a simple generator for the diff tree regex pattern. This could be
76 + # a class variable if this and parseDiffTreeEntry were a part of a class.
77 + pattern = re.compile(':(\d+) (\d+) (\w+) (\w+) ([A-Z])(\d+)?\t(.*?)((\t(.*))|$)')
78 + while True:
79 + yield pattern
80 +
81 +def parseDiffTreeEntry(entry):
82 + """Parses a single diff tree entry into its component elements.
83 +
84 + See git-diff-tree(1) manpage for details about the format of the diff
85 + output. This method returns a dictionary with the following elements:
86 +
87 + src_mode - The mode of the source file
88 + dst_mode - The mode of the destination file
89 + src_sha1 - The sha1 for the source file
90 + dst_sha1 - The sha1 fr the destination file
91 + status - The one letter status of the diff (i.e. 'A', 'M', 'D', etc)
92 + status_score - The score for the status (applicable for 'C' and 'R'
93 + statuses). This is None if there is no score.
94 + src - The path for the source file.
95 + dst - The path for the destination file. This is only present for
96 + copy or renames. If it is not present, this is None.
97 +
98 + If the pattern is not matched, None is returned."""
99 +
100 + match = diffTreePattern().next().match(entry)
101 + if match:
102 + return {
103 + 'src_mode': match.group(1),
104 + 'dst_mode': match.group(2),
105 + 'src_sha1': match.group(3),
106 + 'dst_sha1': match.group(4),
107 + 'status': match.group(5),
108 + 'status_score': match.group(6),
109 + 'src': match.group(7),
110 + 'dst': match.group(10)
111 + }
112 + return None
113 +
114 def p4CmdList(cmd, stdin=None, stdin_mode='w+b'):
115 cmd = "p4 -G %s" % cmd
116 if verbose:
@@ -494,13 +534,14 @@ class P4Submit(Command):
534 else:
535 print "Applying %s" % (read_pipe("git log --max-count=1 --pretty=oneline %s" % id))
536 diffOpts = ("", "-M")[self.detectRename]
497 - diff = read_pipe_lines("git diff-tree -r --name-status %s \"%s^\" \"%s\"" % (diffOpts, id, id))
537 + diff = read_pipe_lines("git diff-tree -r %s \"%s^\" \"%s\"" % (diffOpts, id, id))
538 filesToAdd = set()
539 filesToDelete = set()
540 editedFiles = set()
541 for line in diff:
502 - modifier = line[0]
503 - path = line[1:].strip()
542 + diff = parseDiffTreeEntry(line)
543 + modifier = diff['status']
544 + path = diff['src']
545 if modifier == "M":
546 system("p4 edit \"%s\"" % path)
547 editedFiles.add(path)
@@ -513,7 +554,7 @@ class P4Submit(Command):
554 if path in filesToAdd:
555 filesToAdd.remove(path)
556 elif modifier == "R":
516 - src, dest = line.strip().split("\t")[1:3]
557 + src, dest = diff['src'], diff['dst']
558 system("p4 integrate -Dt \"%s\" \"%s\"" % (src, dest))
559 system("p4 edit \"%s\"" % (dest))
560 os.unlink(dest)