]>
Commit | Line | Data |
---|---|---|
86949eef SH |
1 | #!/usr/bin/env python |
2 | # | |
3 | # git-p4.py -- A tool for bidirectional operation between a Perforce depot and git. | |
4 | # | |
5 | # Author: Simon Hausmann <hausmann@kde.org> | |
83dce55a SH |
6 | # Copyright: 2007 Simon Hausmann <hausmann@kde.org> |
7 | # 2007 Trolltech ASA | |
86949eef SH |
8 | # License: MIT <http://www.opensource.org/licenses/mit-license.php> |
9 | # | |
24f7b53f SH |
10 | # TODO: * implement git-p4 rollback <perforce change number> for debugging |
11 | # to roll back all p4 remote branches to a commit older or equal to | |
12 | # the specified change. | |
341dc1c1 SH |
13 | # * Consider making --with-origin the default, assuming that the git |
14 | # protocol is always more efficient. (needs manual testing first :) | |
24f7b53f | 15 | # |
86949eef | 16 | |
08483580 | 17 | import optparse, sys, os, marshal, popen2, subprocess, shelve |
25df95cc | 18 | import tempfile, getopt, sha, os.path, time, platform |
b984733c | 19 | from sets import Set; |
4f5cf76a SH |
20 | |
21 | gitdir = os.environ.get("GIT_DIR", "") | |
86949eef | 22 | |
caace111 SH |
23 | def mypopen(command): |
24 | return os.popen(command, "rb"); | |
25 | ||
86949eef SH |
26 | def p4CmdList(cmd): |
27 | cmd = "p4 -G %s" % cmd | |
28 | pipe = os.popen(cmd, "rb") | |
29 | ||
30 | result = [] | |
31 | try: | |
32 | while True: | |
33 | entry = marshal.load(pipe) | |
34 | result.append(entry) | |
35 | except EOFError: | |
36 | pass | |
37 | pipe.close() | |
38 | ||
39 | return result | |
40 | ||
41 | def p4Cmd(cmd): | |
42 | list = p4CmdList(cmd) | |
43 | result = {} | |
44 | for entry in list: | |
45 | result.update(entry) | |
46 | return result; | |
47 | ||
cb2c9db5 SH |
48 | def p4Where(depotPath): |
49 | if not depotPath.endswith("/"): | |
50 | depotPath += "/" | |
51 | output = p4Cmd("where %s..." % depotPath) | |
dc524036 SH |
52 | if output["code"] == "error": |
53 | return "" | |
cb2c9db5 SH |
54 | clientPath = "" |
55 | if "path" in output: | |
56 | clientPath = output.get("path") | |
57 | elif "data" in output: | |
58 | data = output.get("data") | |
59 | lastSpace = data.rfind(" ") | |
60 | clientPath = data[lastSpace + 1:] | |
61 | ||
62 | if clientPath.endswith("..."): | |
63 | clientPath = clientPath[:-3] | |
64 | return clientPath | |
65 | ||
86949eef SH |
66 | def die(msg): |
67 | sys.stderr.write(msg + "\n") | |
68 | sys.exit(1) | |
69 | ||
70 | def currentGitBranch(): | |
caace111 | 71 | return mypopen("git name-rev HEAD").read().split(" ")[1][:-1] |
86949eef | 72 | |
4f5cf76a SH |
73 | def isValidGitDir(path): |
74 | if os.path.exists(path + "/HEAD") and os.path.exists(path + "/refs") and os.path.exists(path + "/objects"): | |
75 | return True; | |
76 | return False | |
77 | ||
463e8af6 SH |
78 | def parseRevision(ref): |
79 | return mypopen("git rev-parse %s" % ref).read()[:-1] | |
80 | ||
4f5cf76a SH |
81 | def system(cmd): |
82 | if os.system(cmd) != 0: | |
83 | die("command failed: %s" % cmd) | |
84 | ||
6ae8de88 SH |
85 | def extractLogMessageFromGitCommit(commit): |
86 | logMessage = "" | |
87 | foundTitle = False | |
caace111 | 88 | for log in mypopen("git cat-file commit %s" % commit).readlines(): |
6ae8de88 SH |
89 | if not foundTitle: |
90 | if len(log) == 1: | |
1c094184 | 91 | foundTitle = True |
6ae8de88 SH |
92 | continue |
93 | ||
94 | logMessage += log | |
95 | return logMessage | |
96 | ||
97 | def extractDepotPathAndChangeFromGitLog(log): | |
98 | values = {} | |
99 | for line in log.split("\n"): | |
100 | line = line.strip() | |
101 | if line.startswith("[git-p4:") and line.endswith("]"): | |
102 | line = line[8:-1].strip() | |
103 | for assignment in line.split(":"): | |
104 | variable = assignment.strip() | |
105 | value = "" | |
106 | equalPos = assignment.find("=") | |
107 | if equalPos != -1: | |
108 | variable = assignment[:equalPos].strip() | |
109 | value = assignment[equalPos + 1:].strip() | |
110 | if value.startswith("\"") and value.endswith("\""): | |
111 | value = value[1:-1] | |
112 | values[variable] = value | |
113 | ||
114 | return values.get("depot-path"), values.get("change") | |
115 | ||
8136a639 | 116 | def gitBranchExists(branch): |
caace111 SH |
117 | proc = subprocess.Popen(["git", "rev-parse", branch], stderr=subprocess.PIPE, stdout=subprocess.PIPE); |
118 | return proc.wait() == 0; | |
8136a639 | 119 | |
b984733c SH |
120 | class Command: |
121 | def __init__(self): | |
122 | self.usage = "usage: %prog [options]" | |
8910ac0e | 123 | self.needsGit = True |
b984733c SH |
124 | |
125 | class P4Debug(Command): | |
86949eef | 126 | def __init__(self): |
6ae8de88 | 127 | Command.__init__(self) |
86949eef SH |
128 | self.options = [ |
129 | ] | |
c8c39116 | 130 | self.description = "A tool to debug the output of p4 -G." |
8910ac0e | 131 | self.needsGit = False |
86949eef SH |
132 | |
133 | def run(self, args): | |
134 | for output in p4CmdList(" ".join(args)): | |
135 | print output | |
b984733c | 136 | return True |
86949eef | 137 | |
711544b0 | 138 | class P4Submit(Command): |
4f5cf76a | 139 | def __init__(self): |
b984733c | 140 | Command.__init__(self) |
4f5cf76a SH |
141 | self.options = [ |
142 | optparse.make_option("--continue", action="store_false", dest="firstTime"), | |
143 | optparse.make_option("--origin", dest="origin"), | |
144 | optparse.make_option("--reset", action="store_true", dest="reset"), | |
4f5cf76a SH |
145 | optparse.make_option("--log-substitutions", dest="substFile"), |
146 | optparse.make_option("--noninteractive", action="store_false"), | |
04219c04 | 147 | optparse.make_option("--dry-run", action="store_true"), |
c1b296b9 | 148 | optparse.make_option("--direct", dest="directSubmit", action="store_true"), |
4f5cf76a SH |
149 | ] |
150 | self.description = "Submit changes from git to the perforce depot." | |
c9b50e63 | 151 | self.usage += " [name of git branch to submit into perforce depot]" |
4f5cf76a SH |
152 | self.firstTime = True |
153 | self.reset = False | |
154 | self.interactive = True | |
155 | self.dryRun = False | |
156 | self.substFile = "" | |
157 | self.firstTime = True | |
9512497b | 158 | self.origin = "" |
c1b296b9 | 159 | self.directSubmit = False |
4f5cf76a SH |
160 | |
161 | self.logSubstitutions = {} | |
162 | self.logSubstitutions["<enter description here>"] = "%log%" | |
163 | self.logSubstitutions["\tDetails:"] = "\tDetails: %log%" | |
164 | ||
165 | def check(self): | |
166 | if len(p4CmdList("opened ...")) > 0: | |
167 | die("You have files opened with perforce! Close them before starting the sync.") | |
168 | ||
169 | def start(self): | |
170 | if len(self.config) > 0 and not self.reset: | |
c3c46244 | 171 | die("Cannot start sync. Previous sync config found at %s\nIf you want to start submitting again from scratch maybe you want to call git-p4 submit --reset" % self.configFile) |
4f5cf76a SH |
172 | |
173 | commits = [] | |
c1b296b9 SH |
174 | if self.directSubmit: |
175 | commits.append("0") | |
176 | else: | |
177 | for line in mypopen("git rev-list --no-merges %s..%s" % (self.origin, self.master)).readlines(): | |
178 | commits.append(line[:-1]) | |
179 | commits.reverse() | |
4f5cf76a SH |
180 | |
181 | self.config["commits"] = commits | |
182 | ||
4f5cf76a SH |
183 | def prepareLogMessage(self, template, message): |
184 | result = "" | |
185 | ||
186 | for line in template.split("\n"): | |
187 | if line.startswith("#"): | |
188 | result += line + "\n" | |
189 | continue | |
190 | ||
191 | substituted = False | |
192 | for key in self.logSubstitutions.keys(): | |
193 | if line.find(key) != -1: | |
194 | value = self.logSubstitutions[key] | |
195 | value = value.replace("%log%", message) | |
196 | if value != "@remove@": | |
197 | result += line.replace(key, value) + "\n" | |
198 | substituted = True | |
199 | break | |
200 | ||
201 | if not substituted: | |
202 | result += line + "\n" | |
203 | ||
204 | return result | |
205 | ||
206 | def apply(self, id): | |
c1b296b9 SH |
207 | if self.directSubmit: |
208 | print "Applying local change in working directory/index" | |
209 | diff = self.diffStatus | |
210 | else: | |
211 | print "Applying %s" % (mypopen("git log --max-count=1 --pretty=oneline %s" % id).read()) | |
212 | diff = mypopen("git diff-tree -r --name-status \"%s^\" \"%s\"" % (id, id)).readlines() | |
4f5cf76a SH |
213 | filesToAdd = set() |
214 | filesToDelete = set() | |
d336c158 | 215 | editedFiles = set() |
4f5cf76a SH |
216 | for line in diff: |
217 | modifier = line[0] | |
218 | path = line[1:].strip() | |
219 | if modifier == "M": | |
d336c158 SH |
220 | system("p4 edit \"%s\"" % path) |
221 | editedFiles.add(path) | |
4f5cf76a SH |
222 | elif modifier == "A": |
223 | filesToAdd.add(path) | |
224 | if path in filesToDelete: | |
225 | filesToDelete.remove(path) | |
226 | elif modifier == "D": | |
227 | filesToDelete.add(path) | |
228 | if path in filesToAdd: | |
229 | filesToAdd.remove(path) | |
230 | else: | |
231 | die("unknown modifier %s for %s" % (modifier, path)) | |
232 | ||
c1b296b9 SH |
233 | if self.directSubmit: |
234 | diffcmd = "cat \"%s\"" % self.diffFile | |
235 | else: | |
236 | diffcmd = "git format-patch -k --stdout \"%s^\"..\"%s\"" % (id, id) | |
47a130b7 | 237 | patchcmd = diffcmd + " | git apply " |
c1b296b9 SH |
238 | tryPatchCmd = patchcmd + "--check -" |
239 | applyPatchCmd = patchcmd + "--check --apply -" | |
51a2640a | 240 | |
47a130b7 | 241 | if os.system(tryPatchCmd) != 0: |
51a2640a SH |
242 | print "Unfortunately applying the change failed!" |
243 | print "What do you want to do?" | |
244 | response = "x" | |
245 | while response != "s" and response != "a" and response != "w": | |
246 | response = raw_input("[s]kip this patch / [a]pply the patch forcibly and with .rej files / [w]rite the patch to a file (patch.txt) ") | |
247 | if response == "s": | |
248 | print "Skipping! Good luck with the next patches..." | |
249 | return | |
250 | elif response == "a": | |
47a130b7 | 251 | os.system(applyPatchCmd) |
51a2640a SH |
252 | if len(filesToAdd) > 0: |
253 | print "You may also want to call p4 add on the following files:" | |
254 | print " ".join(filesToAdd) | |
255 | if len(filesToDelete): | |
256 | print "The following files should be scheduled for deletion with p4 delete:" | |
257 | print " ".join(filesToDelete) | |
258 | die("Please resolve and submit the conflict manually and continue afterwards with git-p4 submit --continue") | |
259 | elif response == "w": | |
260 | system(diffcmd + " > patch.txt") | |
261 | print "Patch saved to patch.txt in %s !" % self.clientPath | |
262 | die("Please resolve and submit the conflict manually and continue afterwards with git-p4 submit --continue") | |
263 | ||
47a130b7 | 264 | system(applyPatchCmd) |
4f5cf76a SH |
265 | |
266 | for f in filesToAdd: | |
267 | system("p4 add %s" % f) | |
268 | for f in filesToDelete: | |
269 | system("p4 revert %s" % f) | |
270 | system("p4 delete %s" % f) | |
271 | ||
c1b296b9 SH |
272 | logMessage = "" |
273 | if not self.directSubmit: | |
274 | logMessage = extractLogMessageFromGitCommit(id) | |
275 | logMessage = logMessage.replace("\n", "\n\t") | |
276 | logMessage = logMessage[:-1] | |
4f5cf76a | 277 | |
caace111 | 278 | template = mypopen("p4 change -o").read() |
4f5cf76a SH |
279 | |
280 | if self.interactive: | |
281 | submitTemplate = self.prepareLogMessage(template, logMessage) | |
caace111 | 282 | diff = mypopen("p4 diff -du ...").read() |
4f5cf76a SH |
283 | |
284 | for newFile in filesToAdd: | |
285 | diff += "==== new file ====\n" | |
286 | diff += "--- /dev/null\n" | |
287 | diff += "+++ %s\n" % newFile | |
288 | f = open(newFile, "r") | |
289 | for line in f.readlines(): | |
290 | diff += "+" + line | |
291 | f.close() | |
292 | ||
25df95cc SH |
293 | separatorLine = "######## everything below this line is just the diff #######" |
294 | if platform.system() == "Windows": | |
295 | separatorLine += "\r" | |
296 | separatorLine += "\n" | |
4f5cf76a SH |
297 | |
298 | response = "e" | |
53150250 | 299 | firstIteration = True |
4f5cf76a | 300 | while response == "e": |
53150250 | 301 | if not firstIteration: |
d336c158 | 302 | response = raw_input("Do you want to submit this change? [y]es/[e]dit/[n]o/[s]kip ") |
53150250 | 303 | firstIteration = False |
4f5cf76a SH |
304 | if response == "e": |
305 | [handle, fileName] = tempfile.mkstemp() | |
306 | tmpFile = os.fdopen(handle, "w+") | |
53150250 | 307 | tmpFile.write(submitTemplate + separatorLine + diff) |
4f5cf76a | 308 | tmpFile.close() |
25df95cc SH |
309 | defaultEditor = "vi" |
310 | if platform.system() == "Windows": | |
311 | defaultEditor = "notepad" | |
312 | editor = os.environ.get("EDITOR", defaultEditor); | |
4f5cf76a | 313 | system(editor + " " + fileName) |
25df95cc | 314 | tmpFile = open(fileName, "rb") |
53150250 | 315 | message = tmpFile.read() |
4f5cf76a SH |
316 | tmpFile.close() |
317 | os.remove(fileName) | |
53150250 | 318 | submitTemplate = message[:message.index(separatorLine)] |
4f5cf76a SH |
319 | |
320 | if response == "y" or response == "yes": | |
321 | if self.dryRun: | |
322 | print submitTemplate | |
323 | raw_input("Press return to continue...") | |
324 | else: | |
7944f142 SH |
325 | if self.directSubmit: |
326 | print "Submitting to git first" | |
327 | os.chdir(self.oldWorkingDirectory) | |
328 | pipe = os.popen("git commit -a -F -", "wb") | |
329 | pipe.write(submitTemplate) | |
330 | pipe.close() | |
331 | os.chdir(self.clientPath) | |
332 | ||
333 | pipe = os.popen("p4 submit -i", "wb") | |
334 | pipe.write(submitTemplate) | |
335 | pipe.close() | |
d336c158 SH |
336 | elif response == "s": |
337 | for f in editedFiles: | |
338 | system("p4 revert \"%s\"" % f); | |
339 | for f in filesToAdd: | |
340 | system("p4 revert \"%s\"" % f); | |
341 | system("rm %s" %f) | |
342 | for f in filesToDelete: | |
343 | system("p4 delete \"%s\"" % f); | |
344 | return | |
4f5cf76a SH |
345 | else: |
346 | print "Not submitting!" | |
347 | self.interactive = False | |
348 | else: | |
349 | fileName = "submit.txt" | |
350 | file = open(fileName, "w+") | |
351 | file.write(self.prepareLogMessage(template, logMessage)) | |
352 | file.close() | |
353 | print "Perforce submit template written as %s. Please review/edit and then use p4 submit -i < %s to submit directly!" % (fileName, fileName) | |
354 | ||
355 | def run(self, args): | |
9512497b SH |
356 | global gitdir |
357 | # make gitdir absolute so we can cd out into the perforce checkout | |
358 | gitdir = os.path.abspath(gitdir) | |
359 | os.environ["GIT_DIR"] = gitdir | |
c9b50e63 SH |
360 | |
361 | if len(args) == 0: | |
362 | self.master = currentGitBranch() | |
363 | if len(self.master) == 0 or not os.path.exists("%s/refs/heads/%s" % (gitdir, self.master)): | |
364 | die("Detecting current git branch failed!") | |
365 | elif len(args) == 1: | |
366 | self.master = args[0] | |
367 | else: | |
368 | return False | |
369 | ||
9512497b SH |
370 | depotPath = "" |
371 | if gitBranchExists("p4"): | |
372 | [depotPath, dummy] = extractDepotPathAndChangeFromGitLog(extractLogMessageFromGitCommit("p4")) | |
373 | if len(depotPath) == 0 and gitBranchExists("origin"): | |
374 | [depotPath, dummy] = extractDepotPathAndChangeFromGitLog(extractLogMessageFromGitCommit("origin")) | |
375 | ||
376 | if len(depotPath) == 0: | |
377 | print "Internal error: cannot locate perforce depot path from existing branches" | |
378 | sys.exit(128) | |
379 | ||
51a2640a | 380 | self.clientPath = p4Where(depotPath) |
9512497b | 381 | |
51a2640a | 382 | if len(self.clientPath) == 0: |
9512497b SH |
383 | print "Error: Cannot locate perforce checkout of %s in client view" % depotPath |
384 | sys.exit(128) | |
385 | ||
51a2640a | 386 | print "Perforce checkout for depot path %s located at %s" % (depotPath, self.clientPath) |
7944f142 | 387 | self.oldWorkingDirectory = os.getcwd() |
c1b296b9 SH |
388 | |
389 | if self.directSubmit: | |
390 | self.diffStatus = mypopen("git diff -r --name-status HEAD").readlines() | |
cbf5efa6 SH |
391 | if len(self.diffStatus) == 0: |
392 | print "No changes in working directory to submit." | |
393 | return True | |
c1b296b9 SH |
394 | patch = mypopen("git diff -p --binary --diff-filter=ACMRTUXB HEAD").read() |
395 | self.diffFile = gitdir + "/p4-git-diff" | |
396 | f = open(self.diffFile, "wb") | |
397 | f.write(patch) | |
398 | f.close(); | |
399 | ||
51a2640a SH |
400 | os.chdir(self.clientPath) |
401 | response = raw_input("Do you want to sync %s with p4 sync? [y]es/[n]o " % self.clientPath) | |
9512497b SH |
402 | if response == "y" or response == "yes": |
403 | system("p4 sync ...") | |
404 | ||
405 | if len(self.origin) == 0: | |
406 | if gitBranchExists("p4"): | |
407 | self.origin = "p4" | |
408 | else: | |
409 | self.origin = "origin" | |
410 | ||
4f5cf76a SH |
411 | if self.reset: |
412 | self.firstTime = True | |
413 | ||
414 | if len(self.substFile) > 0: | |
415 | for line in open(self.substFile, "r").readlines(): | |
416 | tokens = line[:-1].split("=") | |
417 | self.logSubstitutions[tokens[0]] = tokens[1] | |
418 | ||
4f5cf76a SH |
419 | self.check() |
420 | self.configFile = gitdir + "/p4-git-sync.cfg" | |
421 | self.config = shelve.open(self.configFile, writeback=True) | |
422 | ||
423 | if self.firstTime: | |
424 | self.start() | |
425 | ||
426 | commits = self.config.get("commits", []) | |
427 | ||
428 | while len(commits) > 0: | |
429 | self.firstTime = False | |
430 | commit = commits[0] | |
431 | commits = commits[1:] | |
432 | self.config["commits"] = commits | |
433 | self.apply(commit) | |
434 | if not self.interactive: | |
435 | break | |
436 | ||
437 | self.config.close() | |
438 | ||
c1b296b9 SH |
439 | if self.directSubmit: |
440 | os.remove(self.diffFile) | |
441 | ||
4f5cf76a SH |
442 | if len(commits) == 0: |
443 | if self.firstTime: | |
444 | print "No changes found to apply between %s and current HEAD" % self.origin | |
445 | else: | |
446 | print "All changes applied!" | |
7944f142 SH |
447 | os.chdir(self.oldWorkingDirectory) |
448 | response = raw_input("Do you want to sync from Perforce now using git-p4 rebase? [y]es/[n]o ") | |
80b5910f | 449 | if response == "y" or response == "yes": |
80b5910f SH |
450 | rebase = P4Rebase() |
451 | rebase.run([]) | |
4f5cf76a SH |
452 | os.remove(self.configFile) |
453 | ||
b984733c SH |
454 | return True |
455 | ||
711544b0 | 456 | class P4Sync(Command): |
b984733c SH |
457 | def __init__(self): |
458 | Command.__init__(self) | |
459 | self.options = [ | |
460 | optparse.make_option("--branch", dest="branch"), | |
461 | optparse.make_option("--detect-branches", dest="detectBranches", action="store_true"), | |
462 | optparse.make_option("--changesfile", dest="changesFile"), | |
463 | optparse.make_option("--silent", dest="silent", action="store_true"), | |
ef48f909 | 464 | optparse.make_option("--detect-labels", dest="detectLabels", action="store_true"), |
4b97ffb1 SH |
465 | optparse.make_option("--with-origin", dest="syncWithOrigin", action="store_true"), |
466 | optparse.make_option("--verbose", dest="verbose", action="store_true") | |
b984733c SH |
467 | ] |
468 | self.description = """Imports from Perforce into a git repository.\n | |
469 | example: | |
470 | //depot/my/project/ -- to import the current head | |
471 | //depot/my/project/@all -- to import everything | |
472 | //depot/my/project/@1,6 -- to import only from revision 1 to 6 | |
473 | ||
474 | (a ... is not needed in the path p4 specification, it's added implicitly)""" | |
475 | ||
476 | self.usage += " //depot/path[@revRange]" | |
477 | ||
b984733c | 478 | self.silent = False |
b984733c SH |
479 | self.createdBranches = Set() |
480 | self.committedChanges = Set() | |
569d1bd4 | 481 | self.branch = "" |
b984733c | 482 | self.detectBranches = False |
cb53e1f8 | 483 | self.detectLabels = False |
b984733c | 484 | self.changesFile = "" |
ef48f909 | 485 | self.syncWithOrigin = False |
4b97ffb1 | 486 | self.verbose = False |
b984733c SH |
487 | |
488 | def p4File(self, depotPath): | |
489 | return os.popen("p4 print -q \"%s\"" % depotPath, "rb").read() | |
490 | ||
491 | def extractFilesFromCommit(self, commit): | |
492 | files = [] | |
493 | fnum = 0 | |
494 | while commit.has_key("depotFile%s" % fnum): | |
495 | path = commit["depotFile%s" % fnum] | |
8f872531 | 496 | if not path.startswith(self.depotPath): |
b984733c | 497 | # if not self.silent: |
8f872531 | 498 | # print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, self.depotPath, change) |
b984733c SH |
499 | fnum = fnum + 1 |
500 | continue | |
501 | ||
502 | file = {} | |
503 | file["path"] = path | |
504 | file["rev"] = commit["rev%s" % fnum] | |
505 | file["action"] = commit["action%s" % fnum] | |
506 | file["type"] = commit["type%s" % fnum] | |
507 | files.append(file) | |
508 | fnum = fnum + 1 | |
509 | return files | |
510 | ||
71b112d4 | 511 | def splitFilesIntoBranches(self, commit): |
d5904674 | 512 | branches = {} |
b984733c | 513 | |
71b112d4 SH |
514 | fnum = 0 |
515 | while commit.has_key("depotFile%s" % fnum): | |
516 | path = commit["depotFile%s" % fnum] | |
517 | if not path.startswith(self.depotPath): | |
518 | # if not self.silent: | |
519 | # print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, self.depotPath, change) | |
520 | fnum = fnum + 1 | |
521 | continue | |
522 | ||
523 | file = {} | |
524 | file["path"] = path | |
525 | file["rev"] = commit["rev%s" % fnum] | |
526 | file["action"] = commit["action%s" % fnum] | |
527 | file["type"] = commit["type%s" % fnum] | |
528 | fnum = fnum + 1 | |
529 | ||
530 | relPath = path[len(self.depotPath):] | |
b984733c | 531 | |
4b97ffb1 | 532 | for branch in self.knownBranches.keys(): |
71b112d4 | 533 | if relPath.startswith(branch): |
d5904674 SH |
534 | if branch not in branches: |
535 | branches[branch] = [] | |
71b112d4 | 536 | branches[branch].append(file) |
b984733c SH |
537 | |
538 | return branches | |
539 | ||
4b97ffb1 | 540 | def commit(self, details, files, branch, branchPrefix, parent = ""): |
b984733c SH |
541 | epoch = details["time"] |
542 | author = details["user"] | |
543 | ||
4b97ffb1 SH |
544 | if self.verbose: |
545 | print "commit into %s" % branch | |
546 | ||
b984733c SH |
547 | self.gitStream.write("commit %s\n" % branch) |
548 | # gitStream.write("mark :%s\n" % details["change"]) | |
549 | self.committedChanges.add(int(details["change"])) | |
550 | committer = "" | |
b607e71e SH |
551 | if author not in self.users: |
552 | self.getUserMapFromPerforceServer() | |
b984733c | 553 | if author in self.users: |
0828ab14 | 554 | committer = "%s %s %s" % (self.users[author], epoch, self.tz) |
b984733c | 555 | else: |
0828ab14 | 556 | committer = "%s <a@b> %s %s" % (author, epoch, self.tz) |
b984733c SH |
557 | |
558 | self.gitStream.write("committer %s\n" % committer) | |
559 | ||
560 | self.gitStream.write("data <<EOT\n") | |
561 | self.gitStream.write(details["desc"]) | |
6ae8de88 | 562 | self.gitStream.write("\n[git-p4: depot-path = \"%s\": change = %s]\n" % (branchPrefix, details["change"])) |
b984733c SH |
563 | self.gitStream.write("EOT\n\n") |
564 | ||
565 | if len(parent) > 0: | |
4b97ffb1 SH |
566 | if self.verbose: |
567 | print "parent %s" % parent | |
b984733c SH |
568 | self.gitStream.write("from %s\n" % parent) |
569 | ||
b984733c SH |
570 | for file in files: |
571 | path = file["path"] | |
572 | if not path.startswith(branchPrefix): | |
573 | # if not silent: | |
574 | # print "\nchanged files: ignoring path %s outside of branch prefix %s in change %s" % (path, branchPrefix, details["change"]) | |
575 | continue | |
576 | rev = file["rev"] | |
577 | depotPath = path + "#" + rev | |
578 | relPath = path[len(branchPrefix):] | |
579 | action = file["action"] | |
580 | ||
581 | if file["type"] == "apple": | |
582 | print "\nfile %s is a strange apple file that forks. Ignoring!" % path | |
583 | continue | |
584 | ||
585 | if action == "delete": | |
586 | self.gitStream.write("D %s\n" % relPath) | |
587 | else: | |
588 | mode = 644 | |
589 | if file["type"].startswith("x"): | |
590 | mode = 755 | |
591 | ||
592 | data = self.p4File(depotPath) | |
593 | ||
594 | self.gitStream.write("M %s inline %s\n" % (mode, relPath)) | |
595 | self.gitStream.write("data %s\n" % len(data)) | |
596 | self.gitStream.write(data) | |
597 | self.gitStream.write("\n") | |
598 | ||
599 | self.gitStream.write("\n") | |
600 | ||
1f4ba1cb SH |
601 | change = int(details["change"]) |
602 | ||
9bda3a85 | 603 | if self.labels.has_key(change): |
1f4ba1cb SH |
604 | label = self.labels[change] |
605 | labelDetails = label[0] | |
606 | labelRevisions = label[1] | |
71b112d4 SH |
607 | if self.verbose: |
608 | print "Change %s is labelled %s" % (change, labelDetails) | |
1f4ba1cb SH |
609 | |
610 | files = p4CmdList("files %s...@%s" % (branchPrefix, change)) | |
611 | ||
612 | if len(files) == len(labelRevisions): | |
613 | ||
614 | cleanedFiles = {} | |
615 | for info in files: | |
616 | if info["action"] == "delete": | |
617 | continue | |
618 | cleanedFiles[info["depotFile"]] = info["rev"] | |
619 | ||
620 | if cleanedFiles == labelRevisions: | |
621 | self.gitStream.write("tag tag_%s\n" % labelDetails["label"]) | |
622 | self.gitStream.write("from %s\n" % branch) | |
623 | ||
624 | owner = labelDetails["Owner"] | |
625 | tagger = "" | |
626 | if author in self.users: | |
627 | tagger = "%s %s %s" % (self.users[owner], epoch, self.tz) | |
628 | else: | |
629 | tagger = "%s <a@b> %s %s" % (owner, epoch, self.tz) | |
630 | self.gitStream.write("tagger %s\n" % tagger) | |
631 | self.gitStream.write("data <<EOT\n") | |
632 | self.gitStream.write(labelDetails["Description"]) | |
633 | self.gitStream.write("EOT\n\n") | |
634 | ||
635 | else: | |
a46668fa | 636 | if not self.silent: |
1f4ba1cb SH |
637 | print "Tag %s does not match with change %s: files do not match." % (labelDetails["label"], change) |
638 | ||
639 | else: | |
a46668fa | 640 | if not self.silent: |
1f4ba1cb | 641 | print "Tag %s does not match with change %s: file count is different." % (labelDetails["label"], change) |
b984733c | 642 | |
b607e71e | 643 | def getUserMapFromPerforceServer(self): |
b984733c SH |
644 | self.users = {} |
645 | ||
646 | for output in p4CmdList("users"): | |
647 | if not output.has_key("User"): | |
648 | continue | |
649 | self.users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">" | |
650 | ||
b607e71e SH |
651 | cache = open(gitdir + "/p4-usercache.txt", "wb") |
652 | for user in self.users.keys(): | |
653 | cache.write("%s\t%s\n" % (user, self.users[user])) | |
654 | cache.close(); | |
655 | ||
656 | def loadUserMapFromCache(self): | |
657 | self.users = {} | |
658 | try: | |
659 | cache = open(gitdir + "/p4-usercache.txt", "rb") | |
660 | lines = cache.readlines() | |
661 | cache.close() | |
662 | for line in lines: | |
663 | entry = line[:-1].split("\t") | |
664 | self.users[entry[0]] = entry[1] | |
665 | except IOError: | |
666 | self.getUserMapFromPerforceServer() | |
667 | ||
1f4ba1cb SH |
668 | def getLabels(self): |
669 | self.labels = {} | |
670 | ||
8f872531 | 671 | l = p4CmdList("labels %s..." % self.depotPath) |
10c3211b | 672 | if len(l) > 0 and not self.silent: |
8f872531 | 673 | print "Finding files belonging to labels in %s" % self.depotPath |
01ce1fe9 SH |
674 | |
675 | for output in l: | |
1f4ba1cb SH |
676 | label = output["label"] |
677 | revisions = {} | |
678 | newestChange = 0 | |
71b112d4 SH |
679 | if self.verbose: |
680 | print "Querying files for label %s" % label | |
681 | for file in p4CmdList("files %s...@%s" % (self.depotPath, label)): | |
1f4ba1cb SH |
682 | revisions[file["depotFile"]] = file["rev"] |
683 | change = int(file["change"]) | |
684 | if change > newestChange: | |
685 | newestChange = change | |
686 | ||
9bda3a85 SH |
687 | self.labels[newestChange] = [output, revisions] |
688 | ||
689 | if self.verbose: | |
690 | print "Label changes: %s" % self.labels.keys() | |
1f4ba1cb | 691 | |
4b97ffb1 | 692 | def getBranchMapping(self): |
29bdbac1 | 693 | self.projectName = self.depotPath[self.depotPath[:-1].rfind("/") + 1:] |
4b97ffb1 SH |
694 | |
695 | for info in p4CmdList("branches"): | |
696 | details = p4Cmd("branch -o %s" % info["branch"]) | |
697 | viewIdx = 0 | |
698 | while details.has_key("View%s" % viewIdx): | |
699 | paths = details["View%s" % viewIdx].split(" ") | |
700 | viewIdx = viewIdx + 1 | |
701 | # require standard //depot/foo/... //depot/bar/... mapping | |
702 | if len(paths) != 2 or not paths[0].endswith("/...") or not paths[1].endswith("/..."): | |
703 | continue | |
704 | source = paths[0] | |
705 | destination = paths[1] | |
706 | if source.startswith(self.depotPath) and destination.startswith(self.depotPath): | |
707 | source = source[len(self.depotPath):-4] | |
708 | destination = destination[len(self.depotPath):-4] | |
29bdbac1 SH |
709 | if destination not in self.knownBranches: |
710 | self.knownBranches[destination] = source | |
711 | if source not in self.knownBranches: | |
712 | self.knownBranches[source] = source | |
713 | ||
714 | def listExistingP4GitBranches(self): | |
715 | self.p4BranchesInGit = [] | |
716 | ||
717 | for line in mypopen("git rev-parse --symbolic --remotes").readlines(): | |
718 | if line.startswith("p4/") and line != "p4/HEAD\n": | |
719 | branch = line[3:-1] | |
720 | self.p4BranchesInGit.append(branch) | |
721 | self.initialParents["refs/remotes/p4/" + branch] = parseRevision(line[:-1]) | |
4b97ffb1 | 722 | |
b984733c | 723 | def run(self, args): |
8f872531 | 724 | self.depotPath = "" |
179caebf SH |
725 | self.changeRange = "" |
726 | self.initialParent = "" | |
cd6cc0d3 | 727 | self.previousDepotPath = "" |
29bdbac1 SH |
728 | # map from branch depot path to parent branch |
729 | self.knownBranches = {} | |
730 | self.initialParents = {} | |
731 | ||
faf1bd20 SH |
732 | createP4HeadRef = False; |
733 | ||
29bdbac1 SH |
734 | if self.syncWithOrigin and gitBranchExists("origin") and gitBranchExists("refs/remotes/p4/master") and not self.detectBranches: |
735 | ### needs to be ported to multi branch import | |
179caebf | 736 | |
ef48f909 SH |
737 | print "Syncing with origin first as requested by calling git fetch origin" |
738 | system("git fetch origin") | |
739 | [originPreviousDepotPath, originP4Change] = extractDepotPathAndChangeFromGitLog(extractLogMessageFromGitCommit("origin")) | |
740 | [p4PreviousDepotPath, p4Change] = extractDepotPathAndChangeFromGitLog(extractLogMessageFromGitCommit("p4")) | |
741 | if len(originPreviousDepotPath) > 0 and len(originP4Change) > 0 and len(p4Change) > 0: | |
742 | if originPreviousDepotPath == p4PreviousDepotPath: | |
743 | originP4Change = int(originP4Change) | |
744 | p4Change = int(p4Change) | |
745 | if originP4Change > p4Change: | |
746 | print "origin (%s) is newer than p4 (%s). Updating p4 branch from origin." % (originP4Change, p4Change) | |
747 | system("git update-ref refs/remotes/p4/master origin"); | |
748 | else: | |
749 | print "Cannot sync with origin. It was imported from %s while remotes/p4 was imported from %s" % (originPreviousDepotPath, p4PreviousDepotPath) | |
750 | ||
569d1bd4 | 751 | if len(self.branch) == 0: |
48df6fd8 | 752 | self.branch = "refs/remotes/p4/master" |
c6d44cb1 | 753 | if gitBranchExists("refs/heads/p4"): |
48df6fd8 | 754 | system("git update-ref %s refs/heads/p4" % self.branch) |
48df6fd8 | 755 | system("git branch -D p4"); |
faf1bd20 | 756 | # create it /after/ importing, when master exists |
05094f98 | 757 | if not gitBranchExists("refs/remotes/p4/HEAD"): |
faf1bd20 | 758 | createP4HeadRef = True |
967f72e2 | 759 | |
33be3e65 SH |
760 | # this needs to be called after the conversion from heads/p4 to remotes/p4/master |
761 | self.listExistingP4GitBranches() | |
762 | if len(self.p4BranchesInGit) > 1 and not self.silent: | |
763 | print "Importing from/into multiple branches" | |
764 | self.detectBranches = True | |
765 | ||
967f72e2 | 766 | if len(args) == 0: |
29bdbac1 SH |
767 | if not gitBranchExists(self.branch) and gitBranchExists("origin") and not self.detectBranches: |
768 | ### needs to be ported to multi branch import | |
967f72e2 SH |
769 | if not self.silent: |
770 | print "Creating %s branch in git repository based on origin" % self.branch | |
8ead4fda SH |
771 | branch = self.branch |
772 | if not branch.startswith("refs"): | |
773 | branch = "refs/heads/" + branch | |
774 | system("git update-ref %s origin" % branch) | |
967f72e2 | 775 | |
29bdbac1 SH |
776 | if self.verbose: |
777 | print "branches: %s" % self.p4BranchesInGit | |
778 | ||
779 | p4Change = 0 | |
780 | for branch in self.p4BranchesInGit: | |
781 | depotPath, change = extractDepotPathAndChangeFromGitLog(extractLogMessageFromGitCommit("refs/remotes/p4/" + branch)) | |
782 | ||
783 | if self.verbose: | |
784 | print "path %s change %s" % (depotPath, change) | |
785 | ||
786 | if len(depotPath) > 0 and len(change) > 0: | |
787 | change = int(change) + 1 | |
788 | p4Change = max(p4Change, change) | |
789 | ||
790 | if len(self.previousDepotPath) == 0: | |
791 | self.previousDepotPath = depotPath | |
792 | else: | |
793 | i = 0 | |
794 | l = min(len(self.previousDepotPath), len(depotPath)) | |
795 | while i < l and self.previousDepotPath[i] == depotPath[i]: | |
796 | i = i + 1 | |
797 | self.previousDepotPath = self.previousDepotPath[:i] | |
798 | ||
799 | if p4Change > 0: | |
8f872531 | 800 | self.depotPath = self.previousDepotPath |
d5904674 | 801 | self.changeRange = "@%s,#head" % p4Change |
463e8af6 | 802 | self.initialParent = parseRevision(self.branch) |
341dc1c1 | 803 | if not self.silent and not self.detectBranches: |
967f72e2 | 804 | print "Performing incremental import into %s git branch" % self.branch |
569d1bd4 | 805 | |
f9162f6a SH |
806 | if not self.branch.startswith("refs/"): |
807 | self.branch = "refs/heads/" + self.branch | |
179caebf | 808 | |
8f872531 SH |
809 | if len(self.depotPath) != 0: |
810 | self.depotPath = self.depotPath[:-1] | |
b984733c | 811 | |
8f872531 | 812 | if len(args) == 0 and len(self.depotPath) != 0: |
b984733c | 813 | if not self.silent: |
8f872531 | 814 | print "Depot path: %s" % self.depotPath |
b984733c SH |
815 | elif len(args) != 1: |
816 | return False | |
817 | else: | |
8f872531 SH |
818 | if len(self.depotPath) != 0 and self.depotPath != args[0]: |
819 | print "previous import used depot path %s and now %s was specified. this doesn't work!" % (self.depotPath, args[0]) | |
b984733c | 820 | sys.exit(1) |
8f872531 | 821 | self.depotPath = args[0] |
b984733c | 822 | |
b984733c SH |
823 | self.revision = "" |
824 | self.users = {} | |
b984733c | 825 | |
8f872531 SH |
826 | if self.depotPath.find("@") != -1: |
827 | atIdx = self.depotPath.index("@") | |
828 | self.changeRange = self.depotPath[atIdx:] | |
b984733c SH |
829 | if self.changeRange == "@all": |
830 | self.changeRange = "" | |
831 | elif self.changeRange.find(",") == -1: | |
832 | self.revision = self.changeRange | |
833 | self.changeRange = "" | |
8f872531 SH |
834 | self.depotPath = self.depotPath[0:atIdx] |
835 | elif self.depotPath.find("#") != -1: | |
836 | hashIdx = self.depotPath.index("#") | |
837 | self.revision = self.depotPath[hashIdx:] | |
838 | self.depotPath = self.depotPath[0:hashIdx] | |
b984733c SH |
839 | elif len(self.previousDepotPath) == 0: |
840 | self.revision = "#head" | |
841 | ||
8f872531 SH |
842 | if self.depotPath.endswith("..."): |
843 | self.depotPath = self.depotPath[:-3] | |
b984733c | 844 | |
8f872531 SH |
845 | if not self.depotPath.endswith("/"): |
846 | self.depotPath += "/" | |
b984733c | 847 | |
b607e71e | 848 | self.loadUserMapFromCache() |
cb53e1f8 SH |
849 | self.labels = {} |
850 | if self.detectLabels: | |
851 | self.getLabels(); | |
b984733c | 852 | |
4b97ffb1 SH |
853 | if self.detectBranches: |
854 | self.getBranchMapping(); | |
29bdbac1 SH |
855 | if self.verbose: |
856 | print "p4-git branches: %s" % self.p4BranchesInGit | |
857 | print "initial parents: %s" % self.initialParents | |
858 | for b in self.p4BranchesInGit: | |
859 | if b != "master": | |
860 | b = b[len(self.projectName):] | |
861 | self.createdBranches.add(b) | |
4b97ffb1 | 862 | |
f291b4e3 | 863 | self.tz = "%+03d%02d" % (- time.timezone / 3600, ((- time.timezone % 3600) / 60)) |
b984733c | 864 | |
08483580 SH |
865 | importProcess = subprocess.Popen(["git", "fast-import"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE); |
866 | self.gitOutput = importProcess.stdout | |
867 | self.gitStream = importProcess.stdin | |
868 | self.gitError = importProcess.stderr | |
b984733c SH |
869 | |
870 | if len(self.revision) > 0: | |
8f872531 | 871 | print "Doing initial import of %s from revision %s" % (self.depotPath, self.revision) |
b984733c SH |
872 | |
873 | details = { "user" : "git perforce import user", "time" : int(time.time()) } | |
8f872531 | 874 | details["desc"] = "Initial import of %s from the state at revision %s" % (self.depotPath, self.revision) |
b984733c SH |
875 | details["change"] = self.revision |
876 | newestRevision = 0 | |
877 | ||
878 | fileCnt = 0 | |
8f872531 | 879 | for info in p4CmdList("files %s...%s" % (self.depotPath, self.revision)): |
b984733c SH |
880 | change = int(info["change"]) |
881 | if change > newestRevision: | |
882 | newestRevision = change | |
883 | ||
884 | if info["action"] == "delete": | |
c45b1cfe SH |
885 | # don't increase the file cnt, otherwise details["depotFile123"] will have gaps! |
886 | #fileCnt = fileCnt + 1 | |
b984733c SH |
887 | continue |
888 | ||
889 | for prop in [ "depotFile", "rev", "action", "type" ]: | |
890 | details["%s%s" % (prop, fileCnt)] = info[prop] | |
891 | ||
892 | fileCnt = fileCnt + 1 | |
893 | ||
894 | details["change"] = newestRevision | |
895 | ||
896 | try: | |
8f872531 | 897 | self.commit(details, self.extractFilesFromCommit(details), self.branch, self.depotPath) |
c715706b | 898 | except IOError: |
fd4ca86a | 899 | print "IO error with git fast-import. Is your git version recent enough?" |
b984733c SH |
900 | print self.gitError.read() |
901 | ||
902 | else: | |
903 | changes = [] | |
904 | ||
0828ab14 | 905 | if len(self.changesFile) > 0: |
b984733c SH |
906 | output = open(self.changesFile).readlines() |
907 | changeSet = Set() | |
908 | for line in output: | |
909 | changeSet.add(int(line)) | |
910 | ||
911 | for change in changeSet: | |
912 | changes.append(change) | |
913 | ||
914 | changes.sort() | |
915 | else: | |
29bdbac1 SH |
916 | if self.verbose: |
917 | print "Getting p4 changes for %s...%s" % (self.depotPath, self.changeRange) | |
caace111 | 918 | output = mypopen("p4 changes %s...%s" % (self.depotPath, self.changeRange)).readlines() |
b984733c SH |
919 | |
920 | for line in output: | |
921 | changeNum = line.split(" ")[1] | |
922 | changes.append(changeNum) | |
923 | ||
924 | changes.reverse() | |
925 | ||
926 | if len(changes) == 0: | |
0828ab14 | 927 | if not self.silent: |
341dc1c1 | 928 | print "No changes to import!" |
1f52af6c | 929 | return True |
b984733c | 930 | |
341dc1c1 SH |
931 | self.updatedBranches = set() |
932 | ||
b984733c SH |
933 | cnt = 1 |
934 | for change in changes: | |
935 | description = p4Cmd("describe %s" % change) | |
936 | ||
0828ab14 | 937 | if not self.silent: |
341dc1c1 | 938 | sys.stdout.write("\rImporting revision %s (%s%%)" % (change, cnt * 100 / len(changes))) |
b984733c SH |
939 | sys.stdout.flush() |
940 | cnt = cnt + 1 | |
941 | ||
942 | try: | |
b984733c | 943 | if self.detectBranches: |
71b112d4 | 944 | branches = self.splitFilesIntoBranches(description) |
d5904674 | 945 | for branch in branches.keys(): |
8f872531 | 946 | branchPrefix = self.depotPath + branch + "/" |
b984733c | 947 | |
b984733c | 948 | parent = "" |
4b97ffb1 | 949 | |
d5904674 | 950 | filesForCommit = branches[branch] |
4b97ffb1 | 951 | |
29bdbac1 SH |
952 | if self.verbose: |
953 | print "branch is %s" % branch | |
954 | ||
341dc1c1 SH |
955 | self.updatedBranches.add(branch) |
956 | ||
8f9b2e08 | 957 | if branch not in self.createdBranches: |
b984733c | 958 | self.createdBranches.add(branch) |
4b97ffb1 | 959 | parent = self.knownBranches[branch] |
b984733c SH |
960 | if parent == branch: |
961 | parent = "" | |
29bdbac1 SH |
962 | elif self.verbose: |
963 | print "parent determined through known branches: %s" % parent | |
b984733c | 964 | |
8f9b2e08 SH |
965 | # main branch? use master |
966 | if branch == "main": | |
967 | branch = "master" | |
968 | else: | |
29bdbac1 | 969 | branch = self.projectName + branch |
8f9b2e08 SH |
970 | |
971 | if parent == "main": | |
972 | parent = "master" | |
973 | elif len(parent) > 0: | |
29bdbac1 | 974 | parent = self.projectName + parent |
8f9b2e08 | 975 | |
4b97ffb1 | 976 | branch = "refs/remotes/p4/" + branch |
b984733c | 977 | if len(parent) > 0: |
4b97ffb1 | 978 | parent = "refs/remotes/p4/" + parent |
29bdbac1 SH |
979 | |
980 | if self.verbose: | |
981 | print "looking for initial parent for %s; current parent is %s" % (branch, parent) | |
982 | ||
983 | if len(parent) == 0 and branch in self.initialParents: | |
984 | parent = self.initialParents[branch] | |
985 | del self.initialParents[branch] | |
986 | ||
71b112d4 | 987 | self.commit(description, filesForCommit, branch, branchPrefix, parent) |
b984733c | 988 | else: |
71b112d4 | 989 | files = self.extractFilesFromCommit(description) |
8f872531 | 990 | self.commit(description, files, self.branch, self.depotPath, self.initialParent) |
b984733c SH |
991 | self.initialParent = "" |
992 | except IOError: | |
993 | print self.gitError.read() | |
994 | sys.exit(1) | |
995 | ||
341dc1c1 SH |
996 | if not self.silent: |
997 | print "" | |
998 | if len(self.updatedBranches) > 0: | |
999 | sys.stdout.write("Updated branches: ") | |
1000 | for b in self.updatedBranches: | |
1001 | sys.stdout.write("%s " % b) | |
1002 | sys.stdout.write("\n") | |
b984733c | 1003 | |
b984733c SH |
1004 | |
1005 | self.gitStream.close() | |
29bdbac1 SH |
1006 | if importProcess.wait() != 0: |
1007 | die("fast-import failed: %s" % self.gitError.read()) | |
b984733c SH |
1008 | self.gitOutput.close() |
1009 | self.gitError.close() | |
1010 | ||
faf1bd20 SH |
1011 | if createP4HeadRef: |
1012 | system("git symbolic-ref refs/remotes/p4/HEAD %s" % self.branch) | |
1013 | ||
b984733c SH |
1014 | return True |
1015 | ||
01ce1fe9 SH |
1016 | class P4Rebase(Command): |
1017 | def __init__(self): | |
1018 | Command.__init__(self) | |
ef48f909 | 1019 | self.options = [ optparse.make_option("--with-origin", dest="syncWithOrigin", action="store_true") ] |
01ce1fe9 | 1020 | self.description = "Fetches the latest revision from perforce and rebases the current work (branch) against it" |
ef48f909 | 1021 | self.syncWithOrigin = False |
01ce1fe9 SH |
1022 | |
1023 | def run(self, args): | |
1024 | sync = P4Sync() | |
ef48f909 | 1025 | sync.syncWithOrigin = self.syncWithOrigin |
01ce1fe9 SH |
1026 | sync.run([]) |
1027 | print "Rebasing the current branch" | |
caace111 | 1028 | oldHead = mypopen("git rev-parse HEAD").read()[:-1] |
01ce1fe9 | 1029 | system("git rebase p4") |
1f52af6c | 1030 | system("git diff-tree --stat --summary -M %s HEAD" % oldHead) |
01ce1fe9 SH |
1031 | return True |
1032 | ||
f9a3a4f7 SH |
1033 | class P4Clone(P4Sync): |
1034 | def __init__(self): | |
1035 | P4Sync.__init__(self) | |
1036 | self.description = "Creates a new git repository and imports from Perforce into it" | |
1037 | self.usage = "usage: %prog [options] //depot/path[@revRange] [directory]" | |
1038 | self.needsGit = False | |
f9a3a4f7 SH |
1039 | |
1040 | def run(self, args): | |
59fa4171 SH |
1041 | global gitdir |
1042 | ||
f9a3a4f7 SH |
1043 | if len(args) < 1: |
1044 | return False | |
1045 | depotPath = args[0] | |
1046 | dir = "" | |
1047 | if len(args) == 2: | |
1048 | dir = args[1] | |
1049 | elif len(args) > 2: | |
1050 | return False | |
1051 | ||
1052 | if not depotPath.startswith("//"): | |
1053 | return False | |
1054 | ||
1055 | if len(dir) == 0: | |
1056 | dir = depotPath | |
1057 | atPos = dir.rfind("@") | |
1058 | if atPos != -1: | |
1059 | dir = dir[0:atPos] | |
1060 | hashPos = dir.rfind("#") | |
1061 | if hashPos != -1: | |
1062 | dir = dir[0:hashPos] | |
1063 | ||
1064 | if dir.endswith("..."): | |
1065 | dir = dir[:-3] | |
1066 | ||
1067 | if dir.endswith("/"): | |
1068 | dir = dir[:-1] | |
1069 | ||
1070 | slashPos = dir.rfind("/") | |
1071 | if slashPos != -1: | |
1072 | dir = dir[slashPos + 1:] | |
1073 | ||
1074 | print "Importing from %s into %s" % (depotPath, dir) | |
1075 | os.makedirs(dir) | |
1076 | os.chdir(dir) | |
1077 | system("git init") | |
64ffb06a | 1078 | gitdir = os.getcwd() + "/.git" |
f9a3a4f7 SH |
1079 | if not P4Sync.run(self, [depotPath]): |
1080 | return False | |
f9a3a4f7 | 1081 | if self.branch != "master": |
8f9b2e08 SH |
1082 | if gitBranchExists("refs/remotes/p4/master"): |
1083 | system("git branch master refs/remotes/p4/master") | |
1084 | system("git checkout -f") | |
1085 | else: | |
1086 | print "Could not detect main branch. No checkout/master branch created." | |
f9a3a4f7 SH |
1087 | return True |
1088 | ||
b984733c SH |
1089 | class HelpFormatter(optparse.IndentedHelpFormatter): |
1090 | def __init__(self): | |
1091 | optparse.IndentedHelpFormatter.__init__(self) | |
1092 | ||
1093 | def format_description(self, description): | |
1094 | if description: | |
1095 | return description + "\n" | |
1096 | else: | |
1097 | return "" | |
4f5cf76a | 1098 | |
86949eef SH |
1099 | def printUsage(commands): |
1100 | print "usage: %s <command> [options]" % sys.argv[0] | |
1101 | print "" | |
1102 | print "valid commands: %s" % ", ".join(commands) | |
1103 | print "" | |
1104 | print "Try %s <command> --help for command specific help." % sys.argv[0] | |
1105 | print "" | |
1106 | ||
1107 | commands = { | |
1108 | "debug" : P4Debug(), | |
711544b0 | 1109 | "submit" : P4Submit(), |
01ce1fe9 | 1110 | "sync" : P4Sync(), |
f9a3a4f7 SH |
1111 | "rebase" : P4Rebase(), |
1112 | "clone" : P4Clone() | |
86949eef SH |
1113 | } |
1114 | ||
1115 | if len(sys.argv[1:]) == 0: | |
1116 | printUsage(commands.keys()) | |
1117 | sys.exit(2) | |
1118 | ||
1119 | cmd = "" | |
1120 | cmdName = sys.argv[1] | |
1121 | try: | |
1122 | cmd = commands[cmdName] | |
1123 | except KeyError: | |
1124 | print "unknown command %s" % cmdName | |
1125 | print "" | |
1126 | printUsage(commands.keys()) | |
1127 | sys.exit(2) | |
1128 | ||
4f5cf76a SH |
1129 | options = cmd.options |
1130 | cmd.gitdir = gitdir | |
4f5cf76a | 1131 | |
e20a9e53 | 1132 | args = sys.argv[2:] |
86949eef | 1133 | |
e20a9e53 SH |
1134 | if len(options) > 0: |
1135 | options.append(optparse.make_option("--git-dir", dest="gitdir")) | |
1136 | ||
1137 | parser = optparse.OptionParser(cmd.usage.replace("%prog", "%prog " + cmdName), | |
1138 | options, | |
1139 | description = cmd.description, | |
1140 | formatter = HelpFormatter()) | |
1141 | ||
1142 | (cmd, args) = parser.parse_args(sys.argv[2:], cmd); | |
86949eef | 1143 | |
8910ac0e SH |
1144 | if cmd.needsGit: |
1145 | gitdir = cmd.gitdir | |
1146 | if len(gitdir) == 0: | |
1147 | gitdir = ".git" | |
1148 | if not isValidGitDir(gitdir): | |
81f2373f | 1149 | gitdir = mypopen("git rev-parse --git-dir").read()[:-1] |
dc1a93b6 | 1150 | if os.path.exists(gitdir): |
5c4153e4 SH |
1151 | cdup = mypopen("git rev-parse --show-cdup").read()[:-1]; |
1152 | if len(cdup) > 0: | |
1153 | os.chdir(cdup); | |
4f5cf76a | 1154 | |
8910ac0e SH |
1155 | if not isValidGitDir(gitdir): |
1156 | if isValidGitDir(gitdir + "/.git"): | |
1157 | gitdir += "/.git" | |
1158 | else: | |
1159 | die("fatal: cannot locate git repository at %s" % gitdir) | |
4f5cf76a | 1160 | |
8910ac0e | 1161 | os.environ["GIT_DIR"] = gitdir |
4f5cf76a | 1162 | |
b984733c SH |
1163 | if not cmd.run(args): |
1164 | parser.print_help() | |
1165 |