Code

Removed unused p4cat function and added helper function for the perforce python inter...
[git.git] / contrib / fast-import / p4-fast-export.py
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:
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 #       - support p4 submit (hah!)
14 #       - don't hardcode the import to master
15 #
16 import os, string, sys, time
17 import marshal
19 if len(sys.argv) != 2:
20     sys.stderr.write("usage: %s //depot/path[@revRange]\n" % sys.argv[0]);
21     sys.stderr.write("\n    example:\n");
22     sys.stderr.write("    %s //depot/my/project/ -- to import everything\n");
23     sys.stderr.write("    %s //depot/my/project/@1,6 -- to import only from revision 1 to 6\n");
24     sys.stderr.write("\n");
25     sys.stderr.write("    (a ... is not needed in the path p4 specification, it's added implicitly)\n");
26     sys.stderr.write("\n");
27     sys.exit(1)
29 prefix = sys.argv[1]
30 changeRange = ""
31 try:
32     atIdx = prefix.index("@")
33     changeRange = prefix[atIdx:]
34     prefix = prefix[0:atIdx]
35 except ValueError:
36     changeRange = ""
38 if not prefix.endswith("/"):
39     prefix += "/"
41 def p4Cmd(cmd):
42     pipe = os.popen("p4 -G %s" % cmd, "rb")
43     result = {}
44     try:
45         while True:
46             entry = marshal.load(pipe)
47             result.update(entry)
48     except EOFError:
49         pass
50     pipe.close()
51     return result
53 def describe(change):
54     output = os.popen("p4 describe %s" % change).readlines()
56     firstLine = output[0]
58     splitted = firstLine.split(" ")
59     author = splitted[3]
60     author = author[:author.find("@")]
61     tm = time.strptime(splitted[5] + " " + splitted[6], "%Y/%m/%d %H:%M:%S ")
62     epoch = int(time.mktime(tm))
64     filesSection = 0
65     try:
66         filesSection = output.index("Affected files ...\n")
67     except ValueError:
68         sys.stderr.write("Change %s doesn't seem to affect any files. Weird.\n" % change)
69         return [], [], [], [], []
71     differencesSection = 0
72     try:
73         differencesSection = output.index("Differences ...\n")
74     except ValueError:
75         sys.stderr.write("Change %s doesn't seem to have a differences section. Weird.\n" % change)
76         return [], [], [], [], []
78     log = output[2:filesSection - 1]
80     lines = output[filesSection + 2:differencesSection - 1]
82     changed = []
83     removed = []
85     for line in lines:
86         # chop off "... " and trailing newline
87         line = line[4:len(line) - 1]
89         lastSpace = line.rfind(" ")
90         if lastSpace == -1:
91             sys.stderr.write("trouble parsing line %s, skipping!\n" % line)
92             continue
94         operation = line[lastSpace + 1:]
95         path = line[:lastSpace]
97         if operation == "delete":
98             removed.append(path)
99         else:
100             changed.append(path)
102     return author, log, epoch, changed, removed
104 def p4Stat(path):
105     output = os.popen("p4 fstat -Ol \"%s\"" % path).readlines()
106     fileSize = 0
107     mode = 644
108     for line in output:
109         if line.startswith("... headType x"):
110             mode = 755
111         elif line.startswith("... fileSize "):
112             fileSize = long(line[12:])
113     return mode, fileSize
115 def stripRevision(path):
116     hashPos = path.rindex("#")
117     return path[:hashPos]
119 def getUserMap():
120     users = {}
121     output = os.popen("p4 users")
122     for line in output:
123         firstSpace = line.index(" ")
124         secondSpace = line.index(" ", firstSpace + 1)
125         key = line[:firstSpace]
126         email = line[firstSpace + 1:secondSpace]
127         openParenPos = line.index("(", secondSpace)
128         closedParenPos = line.index(")", openParenPos)
129         name = line[openParenPos + 1:closedParenPos]
131         users[key] = name + " " + email
133     return users
135 users = getUserMap()
137 output = os.popen("p4 changes %s...%s" % (prefix, changeRange)).readlines()
139 changes = []
140 for line in output:
141     changeNum = line.split(" ")[1]
142     changes.append(changeNum)
144 changes.reverse()
146 sys.stderr.write("\n")
148 tz = - time.timezone / 36
150 cnt = 1
151 for change in changes:
152     [ author, log, epoch, changedFiles, removedFiles ] = describe(change)
153     sys.stderr.write("\rimporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
154     cnt = cnt + 1
156     print "commit refs/heads/master"
157     if author in users:
158         print "committer %s %s %s" % (users[author], epoch, tz)
159     else:
160         print "committer %s <a@b> %s %s" % (author, epoch, tz)
161     print "data <<EOT"
162     for l in log:
163         print l[:len(l) - 1]
164     print "EOT"
166     print ""
168     for f in changedFiles:
169         if not f.startswith(prefix):
170             sys.stderr.write("\nchanged files: ignoring path %s outside of %s in change %s\n" % (f, prefix, change))
171             continue
172         relpath = f[len(prefix):]
174         [mode, fileSize] = p4Stat(f)
176         print "M %s inline %s" % (mode, stripRevision(relpath))
177         print "data %s" % fileSize
178         sys.stdout.flush();
179         os.system("p4 print -q \"%s\"" % f)
180         print ""
182     for f in removedFiles:
183         if not f.startswith(prefix):
184             sys.stderr.write("\ndeleted files: ignoring path %s outside of %s in change %s\n" % (f, prefix, change))
185             continue
186         relpath = f[len(prefix):]
187         print "D %s" % stripRevision(relpath)
189     print ""
191 sys.stderr.write("\n")