Initial import of a python script to import changesets from Perforce into git.
Signed-off-by: Simon Hausmann <shausman@trolltech.com>
Simon Hausmann committed
Jan 31, 2007 at 00:16 UTC
16d6b8ab6fd7f68bfd9f4d312965cb99e8ad911b
1 file changed
+150
contrib/fast-import/p4-fast-export.py
new
+150
@@ -0,0 +1,150 @@
1
+#!/usr/bin/python
2
+#
3
+# p4-fast-export.py
4
+#
5
+# Author: Simon Hausmann <hausmann@kde.org>
6
+# License: MIT <http://www.opensource.org/licenses/mit-license.php>
7
+#
8
+# TODO: - fix date parsing (how hard can it be?)
9
+# - support integrations (at least p4i)
10
+# - support incremental imports
11
+# - create tags
12
+# - instead of reading all files into a variable try to pipe from
13
+# - p4 print directly to stdout. need to figure out file size somehow
14
+# though.
15
+# - support p4 submit (hah!)
16
+# - don't hardcode the import to master
17
+#
18
+import os, string, sys
19
+
20
+# yep, that's hardcoded right. will fix to a commandline option rsn :)
21
+prefix = "//depot/qt/main/"
22
+# that's in revision range syntax, for example @2342,523634
23
+changeRange = ""
24
+
25
+def describe(change):
26
+ output = os.popen("p4 describe %s" % change).readlines()
27
+
28
+ firstLine = output[0]
29
+
30
+ author = firstLine.split(" ")[3]
31
+ author = author[:author.find("@")]
32
+
33
+ filesSection = 0
34
+ try:
35
+ filesSection = output.index("Affected files ...\n")
36
+ except ValueError:
37
+ sys.stderr.write("Change %s doesn't seem to affect any files. Weird.\n" % change)
38
+ return [], [], [], []
39
+
40
+ differencesSection = 0
41
+ try:
42
+ differencesSection = output.index("Differences ...\n")
43
+ except ValueError:
44
+ sys.stderr.write("Change %s doesn't seem to have a differences section. Weird.\n" % change)
45
+ return [], [], [], []
46
+
47
+ log = output[2:filesSection - 1]
48
+
49
+ lines = output[filesSection + 2:differencesSection - 1]
50
+
51
+ changed = []
52
+ removed = []
53
+
54
+ for line in lines:
55
+ # chop off "... " and trailing newline
56
+ line = line[4:len(line) - 1]
57
+
58
+ lastSpace = line.rfind(" ")
59
+ if lastSpace == -1:
60
+ sys.stderr.write("trouble parsing line %s, skipping!\n" % line)
61
+ continue
62
+
63
+ operation = line[lastSpace + 1:]
64
+ path = line[:lastSpace]
65
+
66
+ if operation == "delete":
67
+ removed.append(path)
68
+ else:
69
+ changed.append(path)
70
+
71
+ return author, log, changed, removed
72
+
73
+def p4cat(path):
74
+ return os.popen("p4 print -q \"%s\"" % path).read()
75
+
76
+def stripRevision(path):
77
+ hashPos = path.rindex("#")
78
+ return path[:hashPos]
79
+
80
+def getUserMap():
81
+ users = {}
82
+ output = os.popen("p4 users")
83
+ for line in output:
84
+ firstSpace = line.index(" ")
85
+ secondSpace = line.index(" ", firstSpace + 1)
86
+ key = line[:firstSpace]
87
+ email = line[firstSpace + 1:secondSpace]
88
+ openParenPos = line.index("(", secondSpace)
89
+ closedParenPos = line.index(")", openParenPos)
90
+ name = line[openParenPos + 1:closedParenPos]
91
+
92
+ users[key] = name + " " + email
93
+
94
+ return users
95
+
96
+
97
+users = getUserMap()
98
+
99
+output = os.popen("p4 changes %s...%s" % (prefix, changeRange)).readlines()
100
+
101
+changes = []
102
+for line in output:
103
+ changeNum = line.split(" ")[1]
104
+ changes.append(changeNum)
105
+
106
+changes.reverse()
107
+
108
+sys.stderr.write("\n")
109
+
110
+cnt = 0
111
+for change in changes:
112
+ [ author, log, changedFiles, removedFiles ] = describe(change)
113
+ sys.stderr.write("\rimporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
114
+ cnt = cnt + 1
115
+# sys.stderr.write("%s\n" % log)
116
+# sys.stderr.write("%s\n" % changedFiles)
117
+# sys.stderr.write("%s\n" % removedFiles)
118
+
119
+ print "commit refs/heads/master"
120
+ if author in users:
121
+ print "committer %s 1 2" % users[author]
122
+ else:
123
+ print "committer %s <a@b> 1 2" % author
124
+ print "data <<EOT"
125
+ for l in log:
126
+ print l[:len(l) - 1]
127
+ print "EOT"
128
+
129
+ print ""
130
+
131
+ for f in changedFiles:
132
+ if not f.startswith(prefix):
133
+ sys.stderr.write("\nchanged files: ignoring path %s outside of %s in change %s\n" % (f, prefix, change))
134
+ continue
135
+ relpath = f[len(prefix):]
136
+ print "M 644 inline %s" % stripRevision(relpath)
137
+ data = p4cat(f)
138
+ print "data %s" % len(data)
139
+ sys.stdout.write(data)
140
+ print ""
141
+
142
+ for f in removedFiles:
143
+ if not f.startswith(prefix):
144
+ sys.stderr.write("\ndeleted files: ignoring path %s outside of %s in change %s\n" % (f, prefix, change))
145
+ continue
146
+ relpath = f[len(prefix):]
147
+ print "D %s" % stripRevision(relpath)
148
+
149
+ print ""
150
+