@matheusbsbs / matheus-bagliano / commits / 68f1336fe3
Fork 0

Code cleanups, move the code to create a commit with fast-import into a separate function out of the main loop.

Signed-off-by: Simon Hausmann <hausmann@kde.org>

Simon Hausmann committed Feb 1, 2007 at 17:42 UTC 68f1336fe370ca2153d691631876b0f85a3fb17f
1 file changed +67 -59
contrib/fast-import/p4-fast-export.py
+67 -59
@@ -25,6 +25,9 @@ if len(sys.argv) != 2:
25 branch = "refs/heads/p4"
26 prefix = sys.argv[1]
27 changeRange = ""
28 +users = {}
29 +initialParent = ""
30 +
31 try:
32 atIdx = prefix.index("@")
33 changeRange = prefix[atIdx:]
@@ -57,6 +60,68 @@ def p4Cmd(cmd):
60 result.update(entry)
61 return result;
62
63 +def commit(details):
64 + global initialParent
65 + global users
66 +
67 + epoch = details["time"]
68 + author = details["user"]
69 +
70 + gitStream.write("commit %s\n" % branch)
71 + committer = ""
72 + if author in users:
73 + committer = "%s %s %s" % (users[author], epoch, tz)
74 + else:
75 + committer = "%s <a@b> %s %s" % (author, epoch, tz)
76 +
77 + gitStream.write("committer %s\n" % committer)
78 +
79 + gitStream.write("data <<EOT\n")
80 + gitStream.write(details["desc"])
81 + gitStream.write("\n[ imported from %s; change %s ]\n" % (prefix, change))
82 + gitStream.write("EOT\n\n")
83 +
84 + if len(initialParent) > 0:
85 + gitStream.write("merge %s\n" % initialParent)
86 + initialParent = ""
87 +
88 + fnum = 0
89 + while details.has_key("depotFile%s" % fnum):
90 + path = details["depotFile%s" % fnum]
91 + if not path.startswith(prefix):
92 + print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, prefix, change)
93 + fnum = fnum + 1
94 + continue
95 +
96 + rev = details["rev%s" % fnum]
97 + depotPath = path + "#" + rev
98 + relPath = path[len(prefix):]
99 + action = details["action%s" % fnum]
100 +
101 + if action == "delete":
102 + gitStream.write("D %s\n" % relPath)
103 + else:
104 + mode = 644
105 + if details["type%s" % fnum].startswith("x"):
106 + mode = 755
107 +
108 + data = os.popen("p4 print -q \"%s\"" % depotPath, "rb").read()
109 +
110 + gitStream.write("M %s inline %s\n" % (mode, relPath))
111 + gitStream.write("data %s\n" % len(data))
112 + gitStream.write(data)
113 + gitStream.write("\n")
114 +
115 + fnum = fnum + 1
116 +
117 + gitStream.write("\n")
118 +
119 + gitStream.write("tag p4/%s\n" % change)
120 + gitStream.write("from %s\n" % branch);
121 + gitStream.write("tagger %s\n" % committer);
122 + gitStream.write("data 0\n\n")
123 +
124 +
125 def getUserMap():
126 users = {}
127
@@ -67,7 +132,6 @@ def getUserMap():
132 return users
133
134 users = getUserMap()
70 -topMerge = ""
135
136 if len(changeRange) == 0:
137 try:
@@ -77,7 +141,7 @@ if len(changeRange) == 0:
141 caretIdx = output.index("^")
142 revision = int(output[tagIdx + 9 : caretIdx]) + 1
143 changeRange = "@%s,#head" % revision
80 - topMerge = os.popen("git-rev-parse %s" % branch).read()[:-1]
144 + initialParent = os.popen("git-rev-parse %s" % branch).read()[:-1]
145 except:
146 pass
147
@@ -108,63 +172,7 @@ for change in changes:
172 sys.stdout.flush()
173 cnt = cnt + 1
174
111 - epoch = description["time"]
112 - author = description["user"]
113 -
114 - gitStream.write("commit %s\n" % branch)
115 - committer = ""
116 - if author in users:
117 - committer = "%s %s %s" % (users[author], epoch, tz)
118 - else:
119 - committer = "%s <a@b> %s %s" % (author, epoch, tz)
120 -
121 - gitStream.write("committer %s\n" % committer)
122 -
123 - gitStream.write("data <<EOT\n")
124 - gitStream.write(description["desc"])
125 - gitStream.write("\n[ imported from %s; change %s ]\n" % (prefix, change))
126 - gitStream.write("EOT\n\n")
127 -
128 - if len(topMerge) > 0:
129 - gitStream.write("merge %s\n" % topMerge)
130 - topMerge = ""
131 -
132 - fnum = 0
133 - while description.has_key("depotFile%s" % fnum):
134 - path = description["depotFile%s" % fnum]
135 - if not path.startswith(prefix):
136 - print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, prefix, change)
137 - fnum = fnum + 1
138 - continue
139 -
140 - rev = description["rev%s" % fnum]
141 - depotPath = path + "#" + rev
142 - relPath = path[len(prefix):]
143 - action = description["action%s" % fnum]
144 -
145 - if action == "delete":
146 - gitStream.write("D %s\n" % relPath)
147 - else:
148 - mode = 644
149 - if description["type%s" % fnum].startswith("x"):
150 - mode = 755
151 -
152 - data = os.popen("p4 print -q \"%s\"" % depotPath, "rb").read()
153 -
154 - gitStream.write("M %s inline %s\n" % (mode, relPath))
155 - gitStream.write("data %s\n" % len(data))
156 - gitStream.write(data)
157 - gitStream.write("\n")
158 -
159 - fnum = fnum + 1
160 -
161 - gitStream.write("\n")
162 -
163 - gitStream.write("tag p4/%s\n" % change)
164 - gitStream.write("from %s\n" % branch);
165 - gitStream.write("tagger %s\n" % committer);
166 - gitStream.write("data 0\n\n")
167 -
175 + commit(description)
176
177 gitStream.close()
178 gitOutput.close()