]> git.ipfire.org Git - thirdparty/git.git/blame - contrib/fast-import/git-p4
Brand new smart incremental import that doesn't need tags or git repo-config :)
[thirdparty/git.git] / contrib / fast-import / git-p4
CommitLineData
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#
10
4f5cf76a 11import optparse, sys, os, marshal, popen2, shelve
b984733c
SH
12import tempfile, getopt, sha, os.path, time
13from sets import Set;
4f5cf76a
SH
14
15gitdir = os.environ.get("GIT_DIR", "")
86949eef
SH
16
17def p4CmdList(cmd):
18 cmd = "p4 -G %s" % cmd
19 pipe = os.popen(cmd, "rb")
20
21 result = []
22 try:
23 while True:
24 entry = marshal.load(pipe)
25 result.append(entry)
26 except EOFError:
27 pass
28 pipe.close()
29
30 return result
31
32def p4Cmd(cmd):
33 list = p4CmdList(cmd)
34 result = {}
35 for entry in list:
36 result.update(entry)
37 return result;
38
39def die(msg):
40 sys.stderr.write(msg + "\n")
41 sys.exit(1)
42
43def currentGitBranch():
44 return os.popen("git-name-rev HEAD").read().split(" ")[1][:-1]
45
4f5cf76a
SH
46def isValidGitDir(path):
47 if os.path.exists(path + "/HEAD") and os.path.exists(path + "/refs") and os.path.exists(path + "/objects"):
48 return True;
49 return False
50
51def system(cmd):
52 if os.system(cmd) != 0:
53 die("command failed: %s" % cmd)
54
6ae8de88
SH
55def extractLogMessageFromGitCommit(commit):
56 logMessage = ""
57 foundTitle = False
58 for log in os.popen("git-cat-file commit %s" % commit).readlines():
59 if not foundTitle:
60 if len(log) == 1:
61 foundTitle = 1
62 continue
63
64 logMessage += log
65 return logMessage
66
67def extractDepotPathAndChangeFromGitLog(log):
68 values = {}
69 for line in log.split("\n"):
70 line = line.strip()
71 if line.startswith("[git-p4:") and line.endswith("]"):
72 line = line[8:-1].strip()
73 for assignment in line.split(":"):
74 variable = assignment.strip()
75 value = ""
76 equalPos = assignment.find("=")
77 if equalPos != -1:
78 variable = assignment[:equalPos].strip()
79 value = assignment[equalPos + 1:].strip()
80 if value.startswith("\"") and value.endswith("\""):
81 value = value[1:-1]
82 values[variable] = value
83
84 return values.get("depot-path"), values.get("change")
85
8136a639 86def gitBranchExists(branch):
179caebf
SH
87 if os.system("git-rev-parse %s 2>/dev/null >/dev/null" % branch) == 0:
88 return True
89 return False
8136a639 90
b984733c
SH
91class Command:
92 def __init__(self):
93 self.usage = "usage: %prog [options]"
94
95class P4Debug(Command):
86949eef 96 def __init__(self):
6ae8de88 97 Command.__init__(self)
86949eef
SH
98 self.options = [
99 ]
c8c39116 100 self.description = "A tool to debug the output of p4 -G."
86949eef
SH
101
102 def run(self, args):
103 for output in p4CmdList(" ".join(args)):
104 print output
b984733c 105 return True
86949eef 106
b984733c 107class P4CleanTags(Command):
86949eef 108 def __init__(self):
b984733c 109 Command.__init__(self)
86949eef
SH
110 self.options = [
111# optparse.make_option("--branch", dest="branch", default="refs/heads/master")
112 ]
c8c39116 113 self.description = "A tool to remove stale unused tags from incremental perforce imports."
86949eef
SH
114 def run(self, args):
115 branch = currentGitBranch()
116 print "Cleaning out stale p4 import tags..."
117 sout, sin, serr = popen2.popen3("git-name-rev --tags `git-rev-parse %s`" % branch)
118 output = sout.read()
119 try:
120 tagIdx = output.index(" tags/p4/")
121 except:
122 print "Cannot find any p4/* tag. Nothing to do."
123 sys.exit(0)
124
125 try:
126 caretIdx = output.index("^")
127 except:
128 caretIdx = len(output) - 1
129 rev = int(output[tagIdx + 9 : caretIdx])
130
131 allTags = os.popen("git tag -l p4/").readlines()
132 for i in range(len(allTags)):
133 allTags[i] = int(allTags[i][3:-1])
134
135 allTags.sort()
136
137 allTags.remove(rev)
138
139 for rev in allTags:
140 print os.popen("git tag -d p4/%s" % rev).read()
141
142 print "%s tags removed." % len(allTags)
b984733c 143 return True
86949eef 144
b984733c 145class P4Sync(Command):
4f5cf76a 146 def __init__(self):
b984733c 147 Command.__init__(self)
4f5cf76a
SH
148 self.options = [
149 optparse.make_option("--continue", action="store_false", dest="firstTime"),
150 optparse.make_option("--origin", dest="origin"),
151 optparse.make_option("--reset", action="store_true", dest="reset"),
152 optparse.make_option("--master", dest="master"),
153 optparse.make_option("--log-substitutions", dest="substFile"),
154 optparse.make_option("--noninteractive", action="store_false"),
04219c04
SH
155 optparse.make_option("--dry-run", action="store_true"),
156 optparse.make_option("--apply-as-patch", action="store_true", dest="applyAsPatch")
4f5cf76a
SH
157 ]
158 self.description = "Submit changes from git to the perforce depot."
159 self.firstTime = True
160 self.reset = False
161 self.interactive = True
162 self.dryRun = False
163 self.substFile = ""
164 self.firstTime = True
165 self.origin = "origin"
166 self.master = ""
1932a6ac 167 self.applyAsPatch = True
4f5cf76a
SH
168
169 self.logSubstitutions = {}
170 self.logSubstitutions["<enter description here>"] = "%log%"
171 self.logSubstitutions["\tDetails:"] = "\tDetails: %log%"
172
173 def check(self):
174 if len(p4CmdList("opened ...")) > 0:
175 die("You have files opened with perforce! Close them before starting the sync.")
176
177 def start(self):
178 if len(self.config) > 0 and not self.reset:
179 die("Cannot start sync. Previous sync config found at %s" % self.configFile)
180
181 commits = []
182 for line in os.popen("git-rev-list --no-merges %s..%s" % (self.origin, self.master)).readlines():
183 commits.append(line[:-1])
184 commits.reverse()
185
186 self.config["commits"] = commits
187
04219c04
SH
188 if not self.applyAsPatch:
189 print "Creating temporary p4-sync branch from %s ..." % self.origin
190 system("git checkout -f -b p4-sync %s" % self.origin)
4f5cf76a
SH
191
192 def prepareLogMessage(self, template, message):
193 result = ""
194
195 for line in template.split("\n"):
196 if line.startswith("#"):
197 result += line + "\n"
198 continue
199
200 substituted = False
201 for key in self.logSubstitutions.keys():
202 if line.find(key) != -1:
203 value = self.logSubstitutions[key]
204 value = value.replace("%log%", message)
205 if value != "@remove@":
206 result += line.replace(key, value) + "\n"
207 substituted = True
208 break
209
210 if not substituted:
211 result += line + "\n"
212
213 return result
214
215 def apply(self, id):
216 print "Applying %s" % (os.popen("git-log --max-count=1 --pretty=oneline %s" % id).read())
217 diff = os.popen("git diff-tree -r --name-status \"%s^\" \"%s\"" % (id, id)).readlines()
218 filesToAdd = set()
219 filesToDelete = set()
220 for line in diff:
221 modifier = line[0]
222 path = line[1:].strip()
223 if modifier == "M":
224 system("p4 edit %s" % path)
225 elif modifier == "A":
226 filesToAdd.add(path)
227 if path in filesToDelete:
228 filesToDelete.remove(path)
229 elif modifier == "D":
230 filesToDelete.add(path)
231 if path in filesToAdd:
232 filesToAdd.remove(path)
233 else:
234 die("unknown modifier %s for %s" % (modifier, path))
235
04219c04 236 if self.applyAsPatch:
5d0b6042 237 system("git-diff-tree -p --diff-filter=ACMRTUXB \"%s^\" \"%s\" | patch -p1" % (id, id))
04219c04
SH
238 else:
239 system("git-diff-files --name-only -z | git-update-index --remove -z --stdin")
240 system("git cherry-pick --no-commit \"%s\"" % id)
4f5cf76a
SH
241
242 for f in filesToAdd:
243 system("p4 add %s" % f)
244 for f in filesToDelete:
245 system("p4 revert %s" % f)
246 system("p4 delete %s" % f)
247
6ae8de88
SH
248 logMessage = extractLogMessageFromGitCommit(id)
249 logMessage = logMessage.replace("\n", "\n\t")
250 logMessage = logMessage[:-1]
4f5cf76a
SH
251
252 template = os.popen("p4 change -o").read()
253
254 if self.interactive:
255 submitTemplate = self.prepareLogMessage(template, logMessage)
256 diff = os.popen("p4 diff -du ...").read()
257
258 for newFile in filesToAdd:
259 diff += "==== new file ====\n"
260 diff += "--- /dev/null\n"
261 diff += "+++ %s\n" % newFile
262 f = open(newFile, "r")
263 for line in f.readlines():
264 diff += "+" + line
265 f.close()
266
53150250 267 separatorLine = "######## everything below this line is just the diff #######\n"
4f5cf76a
SH
268
269 response = "e"
53150250 270 firstIteration = True
4f5cf76a 271 while response == "e":
53150250
SH
272 if not firstIteration:
273 response = raw_input("Do you want to submit this change (y/e/n)? ")
274 firstIteration = False
4f5cf76a
SH
275 if response == "e":
276 [handle, fileName] = tempfile.mkstemp()
277 tmpFile = os.fdopen(handle, "w+")
53150250 278 tmpFile.write(submitTemplate + separatorLine + diff)
4f5cf76a
SH
279 tmpFile.close()
280 editor = os.environ.get("EDITOR", "vi")
281 system(editor + " " + fileName)
282 tmpFile = open(fileName, "r")
53150250 283 message = tmpFile.read()
4f5cf76a
SH
284 tmpFile.close()
285 os.remove(fileName)
53150250 286 submitTemplate = message[:message.index(separatorLine)]
4f5cf76a
SH
287
288 if response == "y" or response == "yes":
289 if self.dryRun:
290 print submitTemplate
291 raw_input("Press return to continue...")
292 else:
293 pipe = os.popen("p4 submit -i", "w")
294 pipe.write(submitTemplate)
295 pipe.close()
296 else:
297 print "Not submitting!"
298 self.interactive = False
299 else:
300 fileName = "submit.txt"
301 file = open(fileName, "w+")
302 file.write(self.prepareLogMessage(template, logMessage))
303 file.close()
304 print "Perforce submit template written as %s. Please review/edit and then use p4 submit -i < %s to submit directly!" % (fileName, fileName)
305
306 def run(self, args):
307 if self.reset:
308 self.firstTime = True
309
310 if len(self.substFile) > 0:
311 for line in open(self.substFile, "r").readlines():
312 tokens = line[:-1].split("=")
313 self.logSubstitutions[tokens[0]] = tokens[1]
314
315 if len(self.master) == 0:
316 self.master = currentGitBranch()
317 if len(self.master) == 0 or not os.path.exists("%s/refs/heads/%s" % (gitdir, self.master)):
318 die("Detecting current git branch failed!")
319
320 self.check()
321 self.configFile = gitdir + "/p4-git-sync.cfg"
322 self.config = shelve.open(self.configFile, writeback=True)
323
324 if self.firstTime:
325 self.start()
326
327 commits = self.config.get("commits", [])
328
329 while len(commits) > 0:
330 self.firstTime = False
331 commit = commits[0]
332 commits = commits[1:]
333 self.config["commits"] = commits
334 self.apply(commit)
335 if not self.interactive:
336 break
337
338 self.config.close()
339
340 if len(commits) == 0:
341 if self.firstTime:
342 print "No changes found to apply between %s and current HEAD" % self.origin
343 else:
344 print "All changes applied!"
04219c04
SH
345 if not self.applyAsPatch:
346 print "Deleting temporary p4-sync branch and going back to %s" % self.master
347 system("git checkout %s" % self.master)
348 system("git branch -D p4-sync")
349 print "Cleaning out your perforce checkout by doing p4 edit ... ; p4 revert ..."
350 system("p4 edit ... >/dev/null")
351 system("p4 revert ... >/dev/null")
4f5cf76a
SH
352 os.remove(self.configFile)
353
b984733c
SH
354 return True
355
356class GitSync(Command):
357 def __init__(self):
358 Command.__init__(self)
359 self.options = [
360 optparse.make_option("--branch", dest="branch"),
361 optparse.make_option("--detect-branches", dest="detectBranches", action="store_true"),
362 optparse.make_option("--changesfile", dest="changesFile"),
363 optparse.make_option("--silent", dest="silent", action="store_true"),
364 optparse.make_option("--known-branches", dest="knownBranches"),
365 optparse.make_option("--cache", dest="doCache", action="store_true"),
366 optparse.make_option("--command-cache", dest="commandCache", action="store_true")
367 ]
368 self.description = """Imports from Perforce into a git repository.\n
369 example:
370 //depot/my/project/ -- to import the current head
371 //depot/my/project/@all -- to import everything
372 //depot/my/project/@1,6 -- to import only from revision 1 to 6
373
374 (a ... is not needed in the path p4 specification, it's added implicitly)"""
375
376 self.usage += " //depot/path[@revRange]"
377
378 self.dataCache = False
379 self.commandCache = False
380 self.silent = False
381 self.knownBranches = Set()
382 self.createdBranches = Set()
383 self.committedChanges = Set()
569d1bd4 384 self.branch = ""
b984733c
SH
385 self.detectBranches = False
386 self.changesFile = ""
387
388 def p4File(self, depotPath):
389 return os.popen("p4 print -q \"%s\"" % depotPath, "rb").read()
390
391 def extractFilesFromCommit(self, commit):
392 files = []
393 fnum = 0
394 while commit.has_key("depotFile%s" % fnum):
395 path = commit["depotFile%s" % fnum]
396 if not path.startswith(self.globalPrefix):
397 # if not self.silent:
398 # print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, self.globalPrefix, change)
399 fnum = fnum + 1
400 continue
401
402 file = {}
403 file["path"] = path
404 file["rev"] = commit["rev%s" % fnum]
405 file["action"] = commit["action%s" % fnum]
406 file["type"] = commit["type%s" % fnum]
407 files.append(file)
408 fnum = fnum + 1
409 return files
410
411 def isSubPathOf(self, first, second):
412 if not first.startswith(second):
413 return False
414 if first == second:
415 return True
416 return first[len(second)] == "/"
417
418 def branchesForCommit(self, files):
419 branches = Set()
420
421 for file in files:
422 relativePath = file["path"][len(self.globalPrefix):]
423 # strip off the filename
424 relativePath = relativePath[0:relativePath.rfind("/")]
425
426 # if len(branches) == 0:
427 # branches.add(relativePath)
428 # knownBranches.add(relativePath)
429 # continue
430
431 ###### this needs more testing :)
432 knownBranch = False
433 for branch in branches:
434 if relativePath == branch:
435 knownBranch = True
436 break
437 # if relativePath.startswith(branch):
438 if self.isSubPathOf(relativePath, branch):
439 knownBranch = True
440 break
441 # if branch.startswith(relativePath):
442 if self.isSubPathOf(branch, relativePath):
443 branches.remove(branch)
444 break
445
446 if knownBranch:
447 continue
448
449 for branch in knownBranches:
450 #if relativePath.startswith(branch):
451 if self.isSubPathOf(relativePath, branch):
452 if len(branches) == 0:
453 relativePath = branch
454 else:
455 knownBranch = True
456 break
457
458 if knownBranch:
459 continue
460
461 branches.add(relativePath)
462 self.knownBranches.add(relativePath)
463
464 return branches
465
466 def findBranchParent(self, branchPrefix, files):
467 for file in files:
468 path = file["path"]
469 if not path.startswith(branchPrefix):
470 continue
471 action = file["action"]
472 if action != "integrate" and action != "branch":
473 continue
474 rev = file["rev"]
475 depotPath = path + "#" + rev
476
477 log = p4CmdList("filelog \"%s\"" % depotPath)
478 if len(log) != 1:
479 print "eek! I got confused by the filelog of %s" % depotPath
480 sys.exit(1);
481
482 log = log[0]
483 if log["action0"] != action:
484 print "eek! wrong action in filelog for %s : found %s, expected %s" % (depotPath, log["action0"], action)
485 sys.exit(1);
486
487 branchAction = log["how0,0"]
488 # if branchAction == "branch into" or branchAction == "ignored":
489 # continue # ignore for branching
490
491 if not branchAction.endswith(" from"):
492 continue # ignore for branching
493 # print "eek! file %s was not branched from but instead: %s" % (depotPath, branchAction)
494 # sys.exit(1);
495
496 source = log["file0,0"]
497 if source.startswith(branchPrefix):
498 continue
499
500 lastSourceRev = log["erev0,0"]
501
502 sourceLog = p4CmdList("filelog -m 1 \"%s%s\"" % (source, lastSourceRev))
503 if len(sourceLog) != 1:
504 print "eek! I got confused by the source filelog of %s%s" % (source, lastSourceRev)
505 sys.exit(1);
506 sourceLog = sourceLog[0]
507
508 relPath = source[len(self.globalPrefix):]
509 # strip off the filename
510 relPath = relPath[0:relPath.rfind("/")]
511
512 for branch in self.knownBranches:
513 if self.isSubPathOf(relPath, branch):
514 # print "determined parent branch branch %s due to change in file %s" % (branch, source)
515 return branch
516 # else:
517 # print "%s is not a subpath of branch %s" % (relPath, branch)
518
519 return ""
520
c715706b 521 def commit(self, details, files, branch, branchPrefix, parent = "", merged = ""):
b984733c
SH
522 epoch = details["time"]
523 author = details["user"]
524
525 self.gitStream.write("commit %s\n" % branch)
526 # gitStream.write("mark :%s\n" % details["change"])
527 self.committedChanges.add(int(details["change"]))
528 committer = ""
529 if author in self.users:
0828ab14 530 committer = "%s %s %s" % (self.users[author], epoch, self.tz)
b984733c 531 else:
0828ab14 532 committer = "%s <a@b> %s %s" % (author, epoch, self.tz)
b984733c
SH
533
534 self.gitStream.write("committer %s\n" % committer)
535
536 self.gitStream.write("data <<EOT\n")
537 self.gitStream.write(details["desc"])
6ae8de88 538 self.gitStream.write("\n[git-p4: depot-path = \"%s\": change = %s]\n" % (branchPrefix, details["change"]))
b984733c
SH
539 self.gitStream.write("EOT\n\n")
540
541 if len(parent) > 0:
542 self.gitStream.write("from %s\n" % parent)
543
544 if len(merged) > 0:
545 self.gitStream.write("merge %s\n" % merged)
546
547 for file in files:
548 path = file["path"]
549 if not path.startswith(branchPrefix):
550 # if not silent:
551 # print "\nchanged files: ignoring path %s outside of branch prefix %s in change %s" % (path, branchPrefix, details["change"])
552 continue
553 rev = file["rev"]
554 depotPath = path + "#" + rev
555 relPath = path[len(branchPrefix):]
556 action = file["action"]
557
558 if file["type"] == "apple":
559 print "\nfile %s is a strange apple file that forks. Ignoring!" % path
560 continue
561
562 if action == "delete":
563 self.gitStream.write("D %s\n" % relPath)
564 else:
565 mode = 644
566 if file["type"].startswith("x"):
567 mode = 755
568
569 data = self.p4File(depotPath)
570
571 self.gitStream.write("M %s inline %s\n" % (mode, relPath))
572 self.gitStream.write("data %s\n" % len(data))
573 self.gitStream.write(data)
574 self.gitStream.write("\n")
575
576 self.gitStream.write("\n")
577
578 self.lastChange = int(details["change"])
579
580 def extractFilesInCommitToBranch(self, files, branchPrefix):
581 newFiles = []
582
583 for file in files:
584 path = file["path"]
585 if path.startswith(branchPrefix):
586 newFiles.append(file)
587
588 return newFiles
589
590 def findBranchSourceHeuristic(self, files, branch, branchPrefix):
591 for file in files:
592 action = file["action"]
593 if action != "integrate" and action != "branch":
594 continue
595 path = file["path"]
596 rev = file["rev"]
597 depotPath = path + "#" + rev
598
599 log = p4CmdList("filelog \"%s\"" % depotPath)
600 if len(log) != 1:
601 print "eek! I got confused by the filelog of %s" % depotPath
602 sys.exit(1);
603
604 log = log[0]
605 if log["action0"] != action:
606 print "eek! wrong action in filelog for %s : found %s, expected %s" % (depotPath, log["action0"], action)
607 sys.exit(1);
608
609 branchAction = log["how0,0"]
610
611 if not branchAction.endswith(" from"):
612 continue # ignore for branching
613 # print "eek! file %s was not branched from but instead: %s" % (depotPath, branchAction)
614 # sys.exit(1);
615
616 source = log["file0,0"]
617 if source.startswith(branchPrefix):
618 continue
619
620 lastSourceRev = log["erev0,0"]
621
622 sourceLog = p4CmdList("filelog -m 1 \"%s%s\"" % (source, lastSourceRev))
623 if len(sourceLog) != 1:
624 print "eek! I got confused by the source filelog of %s%s" % (source, lastSourceRev)
625 sys.exit(1);
626 sourceLog = sourceLog[0]
627
628 relPath = source[len(self.globalPrefix):]
629 # strip off the filename
630 relPath = relPath[0:relPath.rfind("/")]
631
632 for candidate in self.knownBranches:
633 if self.isSubPathOf(relPath, candidate) and candidate != branch:
634 return candidate
635
636 return ""
637
638 def changeIsBranchMerge(self, sourceBranch, destinationBranch, change):
639 sourceFiles = {}
640 for file in p4CmdList("files %s...@%s" % (self.globalPrefix + sourceBranch + "/", change)):
641 if file["action"] == "delete":
642 continue
643 sourceFiles[file["depotFile"]] = file
644
645 destinationFiles = {}
646 for file in p4CmdList("files %s...@%s" % (self.globalPrefix + destinationBranch + "/", change)):
647 destinationFiles[file["depotFile"]] = file
648
649 for fileName in sourceFiles.keys():
650 integrations = []
651 deleted = False
652 integrationCount = 0
653 for integration in p4CmdList("integrated \"%s\"" % fileName):
654 toFile = integration["fromFile"] # yes, it's true, it's fromFile
655 if not toFile in destinationFiles:
656 continue
657 destFile = destinationFiles[toFile]
658 if destFile["action"] == "delete":
659 # print "file %s has been deleted in %s" % (fileName, toFile)
660 deleted = True
661 break
662 integrationCount += 1
663 if integration["how"] == "branch from":
664 continue
665
666 if int(integration["change"]) == change:
667 integrations.append(integration)
668 continue
669 if int(integration["change"]) > change:
670 continue
671
672 destRev = int(destFile["rev"])
673
674 startRev = integration["startFromRev"][1:]
675 if startRev == "none":
676 startRev = 0
677 else:
678 startRev = int(startRev)
679
680 endRev = integration["endFromRev"][1:]
681 if endRev == "none":
682 endRev = 0
683 else:
684 endRev = int(endRev)
685
686 initialBranch = (destRev == 1 and integration["how"] != "branch into")
687 inRange = (destRev >= startRev and destRev <= endRev)
688 newer = (destRev > startRev and destRev > endRev)
689
690 if initialBranch or inRange or newer:
691 integrations.append(integration)
692
693 if deleted:
694 continue
695
696 if len(integrations) == 0 and integrationCount > 1:
697 print "file %s was not integrated from %s into %s" % (fileName, sourceBranch, destinationBranch)
698 return False
699
700 return True
701
702 def getUserMap(self):
703 self.users = {}
704
705 for output in p4CmdList("users"):
706 if not output.has_key("User"):
707 continue
708 self.users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">"
709
710 def run(self, args):
179caebf
SH
711 self.globalPrefix = ""
712 self.changeRange = ""
713 self.initialParent = ""
714 self.tagLastChange = True
715
569d1bd4
SH
716 if len(self.branch) == 0:
717 self.branch = "p4"
179caebf
SH
718 if len(args) == 0:
719 if not gitBranchExists(self.branch) and gitBranchExists("origin"):
720 if not self.silent:
721 print "Creating %s branch in git repository based on origin" % self.branch
722 system("git branch %s origin" % self.branch)
723
724 [self.previousDepotPath, p4Change] = extractDepotPathAndChangeFromGitLog(extractLogMessageFromGitCommit(self.branch))
725 if len(self.previousDepotPath) > 0 and len(p4Change) > 0:
726 p4Change = int(p4Change) + 1
727 self.globalPrefix = self.previousDepotPath
728 self.changeRange = "@%s,#head" % p4Change
729 self.initialParent = self.branch
730 self.tagLastChange = False
731 if not self.silent:
732 print "Performing incremental import into %s git branch" % self.branch
569d1bd4 733
b984733c 734 self.branch = "refs/heads/" + self.branch
179caebf
SH
735
736 if len(self.globalPrefix) == 0:
737 self.globalPrefix = self.previousDepotPath = os.popen("git-repo-config --get p4.depotpath").read()
738
b984733c
SH
739 if len(self.globalPrefix) != 0:
740 self.globalPrefix = self.globalPrefix[:-1]
741
742 if len(args) == 0 and len(self.globalPrefix) != 0:
743 if not self.silent:
179caebf 744 print "Depot path: %s" % self.globalPrefix
b984733c
SH
745 elif len(args) != 1:
746 return False
747 else:
748 if len(self.globalPrefix) != 0 and self.globalPrefix != args[0]:
749 print "previous import used depot path %s and now %s was specified. this doesn't work!" % (self.globalPrefix, args[0])
750 sys.exit(1)
751 self.globalPrefix = args[0]
752
b984733c
SH
753 self.revision = ""
754 self.users = {}
b984733c
SH
755 self.lastChange = 0
756 self.initialTag = ""
757
758 if self.globalPrefix.find("@") != -1:
759 atIdx = self.globalPrefix.index("@")
760 self.changeRange = self.globalPrefix[atIdx:]
761 if self.changeRange == "@all":
762 self.changeRange = ""
763 elif self.changeRange.find(",") == -1:
764 self.revision = self.changeRange
765 self.changeRange = ""
766 self.globalPrefix = self.globalPrefix[0:atIdx]
767 elif self.globalPrefix.find("#") != -1:
768 hashIdx = self.globalPrefix.index("#")
769 self.revision = self.globalPrefix[hashIdx:]
770 self.globalPrefix = self.globalPrefix[0:hashIdx]
771 elif len(self.previousDepotPath) == 0:
772 self.revision = "#head"
773
774 if self.globalPrefix.endswith("..."):
775 self.globalPrefix = self.globalPrefix[:-3]
776
777 if not self.globalPrefix.endswith("/"):
778 self.globalPrefix += "/"
779
780 self.getUserMap()
781
782 if len(self.changeRange) == 0:
783 try:
784 sout, sin, serr = popen2.popen3("git-name-rev --tags `git-rev-parse %s`" % self.branch)
785 output = sout.read()
786 if output.endswith("\n"):
787 output = output[:-1]
788 tagIdx = output.index(" tags/p4/")
789 caretIdx = output.find("^")
790 endPos = len(output)
791 if caretIdx != -1:
792 endPos = caretIdx
793 self.rev = int(output[tagIdx + 9 : endPos]) + 1
794 self.changeRange = "@%s,#head" % self.rev
795 self.initialParent = os.popen("git-rev-parse %s" % self.branch).read()[:-1]
796 self.initialTag = "p4/%s" % (int(self.rev) - 1)
797 except:
798 pass
799
0828ab14
SH
800 self.tz = - time.timezone / 36
801 tzsign = ("%s" % self.tz)[0]
b984733c 802 if tzsign != '+' and tzsign != '-':
0828ab14 803 self.tz = "+" + ("%s" % self.tz)
b984733c
SH
804
805 self.gitOutput, self.gitStream, self.gitError = popen2.popen3("git-fast-import")
806
807 if len(self.revision) > 0:
808 print "Doing initial import of %s from revision %s" % (self.globalPrefix, self.revision)
809
810 details = { "user" : "git perforce import user", "time" : int(time.time()) }
811 details["desc"] = "Initial import of %s from the state at revision %s" % (self.globalPrefix, self.revision)
812 details["change"] = self.revision
813 newestRevision = 0
814
815 fileCnt = 0
816 for info in p4CmdList("files %s...%s" % (self.globalPrefix, self.revision)):
817 change = int(info["change"])
818 if change > newestRevision:
819 newestRevision = change
820
821 if info["action"] == "delete":
c715706b 822 fileCnt = fileCnt + 1
b984733c
SH
823 continue
824
825 for prop in [ "depotFile", "rev", "action", "type" ]:
826 details["%s%s" % (prop, fileCnt)] = info[prop]
827
828 fileCnt = fileCnt + 1
829
830 details["change"] = newestRevision
831
832 try:
833 self.commit(details, self.extractFilesFromCommit(details), self.branch, self.globalPrefix)
c715706b 834 except IOError:
b984733c
SH
835 print self.gitError.read()
836
837 else:
838 changes = []
839
0828ab14 840 if len(self.changesFile) > 0:
b984733c
SH
841 output = open(self.changesFile).readlines()
842 changeSet = Set()
843 for line in output:
844 changeSet.add(int(line))
845
846 for change in changeSet:
847 changes.append(change)
848
849 changes.sort()
850 else:
851 output = os.popen("p4 changes %s...%s" % (self.globalPrefix, self.changeRange)).readlines()
852
853 for line in output:
854 changeNum = line.split(" ")[1]
855 changes.append(changeNum)
856
857 changes.reverse()
858
859 if len(changes) == 0:
0828ab14 860 if not self.silent:
b984733c
SH
861 print "no changes to import!"
862 sys.exit(1)
863
864 cnt = 1
865 for change in changes:
866 description = p4Cmd("describe %s" % change)
867
0828ab14 868 if not self.silent:
b984733c
SH
869 sys.stdout.write("\rimporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
870 sys.stdout.flush()
871 cnt = cnt + 1
872
873 try:
874 files = self.extractFilesFromCommit(description)
875 if self.detectBranches:
876 for branch in self.branchesForCommit(files):
877 self.knownBranches.add(branch)
878 branchPrefix = self.globalPrefix + branch + "/"
879
880 filesForCommit = self.extractFilesInCommitToBranch(files, branchPrefix)
881
882 merged = ""
883 parent = ""
884 ########### remove cnt!!!
885 if branch not in self.createdBranches and cnt > 2:
886 self.createdBranches.add(branch)
887 parent = self.findBranchParent(branchPrefix, files)
888 if parent == branch:
889 parent = ""
890 # elif len(parent) > 0:
891 # print "%s branched off of %s" % (branch, parent)
892
893 if len(parent) == 0:
894 merged = self.findBranchSourceHeuristic(filesForCommit, branch, branchPrefix)
895 if len(merged) > 0:
896 print "change %s could be a merge from %s into %s" % (description["change"], merged, branch)
897 if not self.changeIsBranchMerge(merged, branch, int(description["change"])):
898 merged = ""
899
900 branch = "refs/heads/" + branch
901 if len(parent) > 0:
902 parent = "refs/heads/" + parent
903 if len(merged) > 0:
904 merged = "refs/heads/" + merged
905 self.commit(description, files, branch, branchPrefix, parent, merged)
906 else:
0828ab14 907 self.commit(description, files, self.branch, self.globalPrefix, self.initialParent)
b984733c
SH
908 self.initialParent = ""
909 except IOError:
910 print self.gitError.read()
911 sys.exit(1)
912
913 if not self.silent:
914 print ""
915
179caebf
SH
916 if self.tagLastChange:
917 self.gitStream.write("reset refs/tags/p4/%s\n" % self.lastChange)
918 self.gitStream.write("from %s\n\n" % self.branch);
b984733c
SH
919
920
921 self.gitStream.close()
922 self.gitOutput.close()
923 self.gitError.close()
924
925 os.popen("git-repo-config p4.depotpath %s" % self.globalPrefix).read()
926 if len(self.initialTag) > 0:
927 os.popen("git tag -d %s" % self.initialTag).read()
928
929 return True
930
931class HelpFormatter(optparse.IndentedHelpFormatter):
932 def __init__(self):
933 optparse.IndentedHelpFormatter.__init__(self)
934
935 def format_description(self, description):
936 if description:
937 return description + "\n"
938 else:
939 return ""
4f5cf76a 940
86949eef
SH
941def printUsage(commands):
942 print "usage: %s <command> [options]" % sys.argv[0]
943 print ""
944 print "valid commands: %s" % ", ".join(commands)
945 print ""
946 print "Try %s <command> --help for command specific help." % sys.argv[0]
947 print ""
948
949commands = {
950 "debug" : P4Debug(),
4f5cf76a 951 "clean-tags" : P4CleanTags(),
b984733c
SH
952 "submit" : P4Sync(),
953 "sync" : GitSync()
86949eef
SH
954}
955
956if len(sys.argv[1:]) == 0:
957 printUsage(commands.keys())
958 sys.exit(2)
959
960cmd = ""
961cmdName = sys.argv[1]
962try:
963 cmd = commands[cmdName]
964except KeyError:
965 print "unknown command %s" % cmdName
966 print ""
967 printUsage(commands.keys())
968 sys.exit(2)
969
4f5cf76a
SH
970options = cmd.options
971cmd.gitdir = gitdir
972options.append(optparse.make_option("--git-dir", dest="gitdir"))
973
b984733c
SH
974parser = optparse.OptionParser(cmd.usage.replace("%prog", "%prog " + cmdName),
975 options,
976 description = cmd.description,
977 formatter = HelpFormatter())
86949eef
SH
978
979(cmd, args) = parser.parse_args(sys.argv[2:], cmd);
980
4f5cf76a
SH
981gitdir = cmd.gitdir
982if len(gitdir) == 0:
983 gitdir = ".git"
20618650
SH
984 if not isValidGitDir(gitdir):
985 cdup = os.popen("git-rev-parse --show-cdup").read()[:-1]
986 if isValidGitDir(cdup + "/" + gitdir):
987 os.chdir(cdup)
4f5cf76a
SH
988
989if not isValidGitDir(gitdir):
990 if isValidGitDir(gitdir + "/.git"):
991 gitdir += "/.git"
992 else:
05140f34 993 die("fatal: cannot locate git repository at %s" % gitdir)
4f5cf76a
SH
994
995os.environ["GIT_DIR"] = gitdir
996
b984733c
SH
997if not cmd.run(args):
998 parser.print_help()
999