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