Code

8684e4b20f01e80797e472bada4fdc6a2925cdcf
[git.git] / contrib / fast-import / git-p4
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>
6 # Copyright: 2007 Simon Hausmann <hausmann@kde.org>
7 #            2007 Trolltech ASA
8 # License: MIT <http://www.opensource.org/licenses/mit-license.php>
9 #
11 import optparse, sys, os, marshal, popen2, shelve
12 import tempfile, getopt, sha, os.path, time
13 from sets import Set;
15 gitdir = os.environ.get("GIT_DIR", "")
17 def p4CmdList(cmd):
18     cmd = "p4 -G %s" % cmd
19     pipe = os.popen(cmd, "rb")
21     result = []
22     try:
23         while True:
24             entry = marshal.load(pipe)
25             result.append(entry)
26     except EOFError:
27         pass
28     pipe.close()
30     return result
32 def p4Cmd(cmd):
33     list = p4CmdList(cmd)
34     result = {}
35     for entry in list:
36         result.update(entry)
37     return result;
39 def die(msg):
40     sys.stderr.write(msg + "\n")
41     sys.exit(1)
43 def currentGitBranch():
44     return os.popen("git-name-rev HEAD").read().split(" ")[1][:-1]
46 def 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
51 def system(cmd):
52     if os.system(cmd) != 0:
53         die("command failed: %s" % cmd)
55 def 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
64        logMessage += log
65     return logMessage
67 def 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
84     return values.get("depot-path"), values.get("change")
86 def gitBranchExists(branch):
87     if os.system("git-rev-parse %s 2>/dev/null >/dev/null" % branch) == 0:
88         return True
89     return False
91 class Command:
92     def __init__(self):
93         self.usage = "usage: %prog [options]"
95 class P4Debug(Command):
96     def __init__(self):
97         Command.__init__(self)
98         self.options = [
99         ]
100         self.description = "A tool to debug the output of p4 -G."
102     def run(self, args):
103         for output in p4CmdList(" ".join(args)):
104             print output
105         return True
107 class P4CleanTags(Command):
108     def __init__(self):
109         Command.__init__(self)
110         self.options = [
111 #                optparse.make_option("--branch", dest="branch", default="refs/heads/master")
112         ]
113         self.description = "A tool to remove stale unused tags from incremental perforce imports."
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)
125         try:
126             caretIdx = output.index("^")
127         except:
128             caretIdx = len(output) - 1
129         rev = int(output[tagIdx + 9 : caretIdx])
131         allTags = os.popen("git tag -l p4/").readlines()
132         for i in range(len(allTags)):
133             allTags[i] = int(allTags[i][3:-1])
135         allTags.sort()
137         allTags.remove(rev)
139         for rev in allTags:
140             print os.popen("git tag -d p4/%s" % rev).read()
142         print "%s tags removed." % len(allTags)
143         return True
145 class P4Sync(Command):
146     def __init__(self):
147         Command.__init__(self)
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"),
155                 optparse.make_option("--dry-run", action="store_true"),
156                 optparse.make_option("--apply-as-patch", action="store_true", dest="applyAsPatch")
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 = ""
167         self.applyAsPatch = True
169         self.logSubstitutions = {}
170         self.logSubstitutions["<enter description here>"] = "%log%"
171         self.logSubstitutions["\tDetails:"] = "\tDetails:  %log%"
173     def check(self):
174         if len(p4CmdList("opened ...")) > 0:
175             die("You have files opened with perforce! Close them before starting the sync.")
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)
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()
186         self.config["commits"] = commits
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)
192     def prepareLogMessage(self, template, message):
193         result = ""
195         for line in template.split("\n"):
196             if line.startswith("#"):
197                 result += line + "\n"
198                 continue
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
210             if not substituted:
211                 result += line + "\n"
213         return result
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))
236         if self.applyAsPatch:
237             system("git-diff-tree -p --diff-filter=ACMRTUXB \"%s^\" \"%s\" | patch -p1" % (id, id))
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)
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)
248         logMessage = extractLogMessageFromGitCommit(id)
249         logMessage = logMessage.replace("\n", "\n\t")
250         logMessage = logMessage[:-1]
252         template = os.popen("p4 change -o").read()
254         if self.interactive:
255             submitTemplate = self.prepareLogMessage(template, logMessage)
256             diff = os.popen("p4 diff -du ...").read()
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()
267             separatorLine = "######## everything below this line is just the diff #######\n"
269             response = "e"
270             firstIteration = True
271             while response == "e":
272                 if not firstIteration:
273                     response = raw_input("Do you want to submit this change (y/e/n)? ")
274                 firstIteration = False
275                 if response == "e":
276                     [handle, fileName] = tempfile.mkstemp()
277                     tmpFile = os.fdopen(handle, "w+")
278                     tmpFile.write(submitTemplate + separatorLine + diff)
279                     tmpFile.close()
280                     editor = os.environ.get("EDITOR", "vi")
281                     system(editor + " " + fileName)
282                     tmpFile = open(fileName, "r")
283                     message = tmpFile.read()
284                     tmpFile.close()
285                     os.remove(fileName)
286                     submitTemplate = message[:message.index(separatorLine)]
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)
306     def run(self, args):
307         if self.reset:
308             self.firstTime = True
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]
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!")
320         self.check()
321         self.configFile = gitdir + "/p4-git-sync.cfg"
322         self.config = shelve.open(self.configFile, writeback=True)
324         if self.firstTime:
325             self.start()
327         commits = self.config.get("commits", [])
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
338         self.config.close()
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!"
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")
352             os.remove(self.configFile)
354         return True
356 class 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
374     (a ... is not needed in the path p4 specification, it's added implicitly)"""
376         self.usage += " //depot/path[@revRange]"
378         self.dataCache = False
379         self.commandCache = False
380         self.silent = False
381         self.knownBranches = Set()
382         self.createdBranches = Set()
383         self.committedChanges = Set()
384         self.branch = ""
385         self.detectBranches = False
386         self.changesFile = ""
388     def p4File(self, depotPath):
389         return os.popen("p4 print -q \"%s\"" % depotPath, "rb").read()
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
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
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)] == "/"
418     def branchesForCommit(self, files):
419         branches = Set()
421         for file in files:
422             relativePath = file["path"][len(self.globalPrefix):]
423             # strip off the filename
424             relativePath = relativePath[0:relativePath.rfind("/")]
426     #        if len(branches) == 0:
427     #            branches.add(relativePath)
428     #            knownBranches.add(relativePath)
429     #            continue
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
446             if knownBranch:
447                 continue
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
458             if knownBranch:
459                 continue
461             branches.add(relativePath)
462             self.knownBranches.add(relativePath)
464         return branches
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
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);
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);
487             branchAction = log["how0,0"]
488     #        if branchAction == "branch into" or branchAction == "ignored":
489     #            continue # ignore for branching
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);
496             source = log["file0,0"]
497             if source.startswith(branchPrefix):
498                 continue
500             lastSourceRev = log["erev0,0"]
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]
508             relPath = source[len(self.globalPrefix):]
509             # strip off the filename
510             relPath = relPath[0:relPath.rfind("/")]
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)
519         return ""
521     def commit(self, details, files, branch, branchPrefix, parent = "", merged = ""):
522         epoch = details["time"]
523         author = details["user"]
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:
530             committer = "%s %s %s" % (self.users[author], epoch, self.tz)
531         else:
532             committer = "%s <a@b> %s %s" % (author, epoch, self.tz)
534         self.gitStream.write("committer %s\n" % committer)
536         self.gitStream.write("data <<EOT\n")
537         self.gitStream.write(details["desc"])
538         self.gitStream.write("\n[git-p4: depot-path = \"%s\": change = %s]\n" % (branchPrefix, details["change"]))
539         self.gitStream.write("EOT\n\n")
541         if len(parent) > 0:
542             self.gitStream.write("from %s\n" % parent)
544         if len(merged) > 0:
545             self.gitStream.write("merge %s\n" % merged)
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"]
558             if file["type"] == "apple":
559                 print "\nfile %s is a strange apple file that forks. Ignoring!" % path
560                 continue
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
569                 data = self.p4File(depotPath)
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")
576         self.gitStream.write("\n")
578         self.lastChange = int(details["change"])
580     def extractFilesInCommitToBranch(self, files, branchPrefix):
581         newFiles = []
583         for file in files:
584             path = file["path"]
585             if path.startswith(branchPrefix):
586                 newFiles.append(file)
588         return newFiles
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
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);
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);
609             branchAction = log["how0,0"]
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);
616             source = log["file0,0"]
617             if source.startswith(branchPrefix):
618                 continue
620             lastSourceRev = log["erev0,0"]
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]
628             relPath = source[len(self.globalPrefix):]
629             # strip off the filename
630             relPath = relPath[0:relPath.rfind("/")]
632             for candidate in self.knownBranches:
633                 if self.isSubPathOf(relPath, candidate) and candidate != branch:
634                     return candidate
636         return ""
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
645         destinationFiles = {}
646         for file in p4CmdList("files %s...@%s" % (self.globalPrefix + destinationBranch + "/", change)):
647             destinationFiles[file["depotFile"]] = file
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
666                 if int(integration["change"]) == change:
667                     integrations.append(integration)
668                     continue
669                 if int(integration["change"]) > change:
670                     continue
672                 destRev = int(destFile["rev"])
674                 startRev = integration["startFromRev"][1:]
675                 if startRev == "none":
676                     startRev = 0
677                 else:
678                     startRev = int(startRev)
680                 endRev = integration["endFromRev"][1:]
681                 if endRev == "none":
682                     endRev = 0
683                 else:
684                     endRev = int(endRev)
686                 initialBranch = (destRev == 1 and integration["how"] != "branch into")
687                 inRange = (destRev >= startRev and destRev <= endRev)
688                 newer = (destRev > startRev and destRev > endRev)
690                 if initialBranch or inRange or newer:
691                     integrations.append(integration)
693             if deleted:
694                 continue
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
700         return True
702     def getUserMap(self):
703         self.users = {}
705         for output in p4CmdList("users"):
706             if not output.has_key("User"):
707                 continue
708             self.users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">"
710     def run(self, args):
711         self.globalPrefix = ""
712         self.changeRange = ""
713         self.initialParent = ""
714         self.tagLastChange = True
716         if len(self.branch) == 0:
717             self.branch = "p4"
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)
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
734         self.branch = "refs/heads/" + self.branch
736         if len(self.globalPrefix) == 0:
737             self.globalPrefix = self.previousDepotPath = os.popen("git-repo-config --get p4.depotpath").read()
739         if len(self.globalPrefix) != 0:
740             self.globalPrefix = self.globalPrefix[:-1]
742         if len(args) == 0 and len(self.globalPrefix) != 0:
743             if not self.silent:
744                 print "Depot path: %s" % self.globalPrefix
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]
753         self.revision = ""
754         self.users = {}
755         self.lastChange = 0
756         self.initialTag = ""
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"
774         if self.globalPrefix.endswith("..."):
775             self.globalPrefix = self.globalPrefix[:-3]
777         if not self.globalPrefix.endswith("/"):
778             self.globalPrefix += "/"
780         self.getUserMap()
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
800         self.tz = - time.timezone / 36
801         tzsign = ("%s" % self.tz)[0]
802         if tzsign != '+' and tzsign != '-':
803             self.tz = "+" + ("%s" % self.tz)
805         self.gitOutput, self.gitStream, self.gitError = popen2.popen3("git-fast-import")
807         if len(self.revision) > 0:
808             print "Doing initial import of %s from revision %s" % (self.globalPrefix, self.revision)
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
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
821                 if info["action"] == "delete":
822                     fileCnt = fileCnt + 1
823                     continue
825                 for prop in [ "depotFile", "rev", "action", "type" ]:
826                     details["%s%s" % (prop, fileCnt)] = info[prop]
828                 fileCnt = fileCnt + 1
830             details["change"] = newestRevision
832             try:
833                 self.commit(details, self.extractFilesFromCommit(details), self.branch, self.globalPrefix)
834             except IOError:
835                 print self.gitError.read()
837         else:
838             changes = []
840             if len(self.changesFile) > 0:
841                 output = open(self.changesFile).readlines()
842                 changeSet = Set()
843                 for line in output:
844                     changeSet.add(int(line))
846                 for change in changeSet:
847                     changes.append(change)
849                 changes.sort()
850             else:
851                 output = os.popen("p4 changes %s...%s" % (self.globalPrefix, self.changeRange)).readlines()
853                 for line in output:
854                     changeNum = line.split(" ")[1]
855                     changes.append(changeNum)
857                 changes.reverse()
859             if len(changes) == 0:
860                 if not self.silent:
861                     print "no changes to import!"
862                 sys.exit(1)
864             cnt = 1
865             for change in changes:
866                 description = p4Cmd("describe %s" % change)
868                 if not self.silent:
869                     sys.stdout.write("\rimporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
870                     sys.stdout.flush()
871                 cnt = cnt + 1
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 + "/"
880                             filesForCommit = self.extractFilesInCommitToBranch(files, branchPrefix)
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)
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 = ""
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:
907                         self.commit(description, files, self.branch, self.globalPrefix, self.initialParent)
908                         self.initialParent = ""
909                 except IOError:
910                     print self.gitError.read()
911                     sys.exit(1)
913         if not self.silent:
914             print ""
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);
921         self.gitStream.close()
922         self.gitOutput.close()
923         self.gitError.close()
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()
929         return True
931 class HelpFormatter(optparse.IndentedHelpFormatter):
932     def __init__(self):
933         optparse.IndentedHelpFormatter.__init__(self)
935     def format_description(self, description):
936         if description:
937             return description + "\n"
938         else:
939             return ""
941 def 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 ""
949 commands = {
950     "debug" : P4Debug(),
951     "clean-tags" : P4CleanTags(),
952     "submit" : P4Sync(),
953     "sync" : GitSync()
956 if len(sys.argv[1:]) == 0:
957     printUsage(commands.keys())
958     sys.exit(2)
960 cmd = ""
961 cmdName = sys.argv[1]
962 try:
963     cmd = commands[cmdName]
964 except KeyError:
965     print "unknown command %s" % cmdName
966     print ""
967     printUsage(commands.keys())
968     sys.exit(2)
970 options = cmd.options
971 cmd.gitdir = gitdir
972 options.append(optparse.make_option("--git-dir", dest="gitdir"))
974 parser = optparse.OptionParser(cmd.usage.replace("%prog", "%prog " + cmdName),
975                                options,
976                                description = cmd.description,
977                                formatter = HelpFormatter())
979 (cmd, args) = parser.parse_args(sys.argv[2:], cmd);
981 gitdir = cmd.gitdir
982 if len(gitdir) == 0:
983     gitdir = ".git"
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)
989 if not isValidGitDir(gitdir):
990     if isValidGitDir(gitdir + "/.git"):
991         gitdir += "/.git"
992     else:
993         die("fatal: cannot locate git repository at %s" % gitdir)
995 os.environ["GIT_DIR"] = gitdir
997 if not cmd.run(args):
998     parser.print_help()