<commit_before>#!/usr/bin/env python
#
# git-p4.py -- A tool for bidirectional operation between a Perforce depot and git.
#
# Author: <NAME> <<EMAIL>>
# Copyright: 2007 <NAME> <<EMAIL>>
#            2007 Trolltech ASA
# License: MIT <http://www.opensource.org/licenses/mit-license.php>
#

import optparse, sys, os, marshal, popen2, shelve
import tempfile, getopt, sha, os.path, time
from sets import Set;

gitdir = os.environ.get("GIT_DIR", "")

def p4CmdList(cmd):
    cmd = "p4 -G %s" % cmd
    pipe = os.popen(cmd, "rb")

    result = []
    try:
        while True:
            entry = marshal.load(pipe)
            result.append(entry)
    except EOFError:
        pass
    pipe.close()

    return result

def p4Cmd(cmd):
    list = p4CmdList(cmd)
    result = {}
    for entry in list:
        result.update(entry)
    return result;

def die(msg):
    sys.stderr.write(msg + "\n")
    sys.exit(1)

def currentGitBranch():
    return os.popen("git-name-rev HEAD").read().split(" ")[1][:-1]

def isValidGitDir(path):
    if os.path.exists(path + "/HEAD") and os.path.exists(path + "/refs") and os.path.exists(path + "/objects"):
        return True;
    return False

def system(cmd):
    if os.system(cmd) != 0:
        die("command failed: %s" % cmd)

class Command:
    def __init__(self):
        self.usage = "usage: %prog [options]"

class P4Debug(Command):
    def __init__(self):
        self.options = [
        ]
        self.description = "A tool to debug the output of p4 -G."

    def run(self, args):
        for output in p4CmdList(" ".join(args)):
            print output
        return True

class P4CleanTags(Command):
    def __init__(self):
        Command.__init__(self)
        self.options = [
#                optparse.make_option("--branch", dest="branch", default="refs/heads/master")
        ]
        self.description = "A tool to remove stale unused tags from incremental perforce imports."
    def run(self, args):
        branch = currentGitBranch()
        print "Cleaning out stale p4 import tags..."
        sout, sin, serr = popen2.popen3("git-name-rev --tags `git-rev-parse %s`" % branch)
        output = sout.read()
        try:
            tagIdx = output.index(" tags/p4/")
        except:
            print "Cannot find any p4/* tag. Nothing to do."
            sys.exit(0)

        try:
            caretIdx = output.index("^")
        except:
            caretIdx = len(output) - 1
        rev = int(output[tagIdx + 9 : caretIdx])

        allTags = os.popen("git tag -l p4/").readlines()
        for i in range(len(allTags)):
            allTags[i] = int(allTags[i][3:-1])

        allTags.sort()

        allTags.remove(rev)

        for rev in allTags:
            print os.popen("git tag -d p4/%s" % rev).read()

        print "%s tags removed." % len(allTags)
        return True

class P4Sync(Command):
    def __init__(self):
        Command.__init__(self)
        self.options = [
                optparse.make_option("--continue", action="store_false", dest="firstTime"),
                optparse.make_option("--origin", dest="origin"),
                optparse.make_option("--reset", action="store_true", dest="reset"),
                optparse.make_option("--master", dest="master"),
                optparse.make_option("--log-substitutions", dest="substFile"),
                optparse.make_option("--noninteractive", action="store_false"),
                optparse.make_option("--dry-run", action="store_true"),
                optparse.make_option("--apply-as-patch", action="store_true", dest="applyAsPatch")
        ]
        self.description = "Submit changes from git to the perforce depot."
        self.firstTime = True
        self.reset = False
        self.interactive = True
        self.dryRun = False
        self.substFile = ""
        self.firstTime = True
        self.origin = "origin"
        self.master = ""
        self.applyAsPatch = False

        self.logSubstitutions = {}
        self.logSubstitutions["<enter description here>"] = "%log%"
        self.logSubstitutions["\tDetails:"] = "\tDetails:  %log%"

    def check(self):
        if len(p4CmdList("opened ...")) > 0:
            die("You have files opened with perforce! Close them before starting the sync.")

    def start(self):
        if len(self.config) > 0 and not self.reset:
            die("Cannot start sync. Previous sync config found at %s" % self.configFile)

        commits = []
        for line in os.popen("git-rev-list --no-merges %s..%s" % (self.origin, self.master)).readlines():
            commits.append(line[:-1])
        commits.reverse()

        self.config["commits"] = commits

        if not self.applyAsPatch:
            print "Creating temporary p4-sync branch from %s ..." % self.origin
            system("git checkout -f -b p4-sync %s" % self.origin)
<commit_msg>Made --apply-as-patch the default for git-p4 submit as it's significantly faster.<commit_after>#!/usr/bin/env python
#
# git-p4.py -- A tool for bidirectional operation between a Perforce depot and git.
#
# Author: <NAME> <<EMAIL>>
# Copyright: 2007 <NAME> <<EMAIL>>
#            2007 Trolltech ASA
# License: MIT <http://www.opensource.org/licenses/mit-license.php>
#

import optparse, sys, os, marshal, popen2, shelve
import tempfile, getopt, sha, os.path, time
from sets import Set;

gitdir = os.environ.get("GIT_DIR", "")

def p4CmdList(cmd):
    cmd = "p4 -G %s" % cmd
    pipe = os.popen(cmd, "rb")

    result = []
    try:
        while True:
            entry = marshal.load(pipe)
            result.append(entry)
    except EOFError:
        pass
    pipe.close()

    return result

def p4Cmd(cmd):
    list = p4CmdList(cmd)
    result = {}
    for entry in list:
        result.update(entry)
    return result;

def die(msg):
    sys.stderr.write(msg + "\n")
    sys.exit(1)

def currentGitBranch():
    return os.popen("git-name-rev HEAD").read().split(" ")[1][:-1]

def isValidGitDir(path):
    if os.path.exists(path + "/HEAD") and os.path.exists(path + "/refs") and os.path.exists(path + "/objects"):
        return True;
    return False

def system(cmd):
    if os.system(cmd) != 0:
        die("command failed: %s" % cmd)

class Command:
    def __init__(self):
        self.usage = "usage: %prog [options]"

class P4Debug(Command):
    def __init__(self):
        self.options = [
        ]
        self.description = "A tool to debug the output of p4 -G."

    def run(self, args):
        for output in p4CmdList(" ".join(args)):
            print output
        return True

class P4CleanTags(Command):
    def __init__(self):
        Command.__init__(self)
        self.options = [
#                optparse.make_option("--branch", dest="branch", default="refs/heads/master")
        ]
        self.description = "A tool to remove stale unused tags from incremental perforce imports."
    def run(self, args):
        branch = currentGitBranch()
        print "Cleaning out stale p4 import tags..."
        sout, sin, serr = popen2.popen3("git-name-rev --tags `git-rev-parse %s`" % branch)
        output = sout.read()
        try:
            tagIdx = output.index(" tags/p4/")
        except:
            print "Cannot find any p4/* tag. Nothing to do."
            sys.exit(0)

        try:
            caretIdx = output.index("^")
        except:
            caretIdx = len(output) - 1
        rev = int(output[tagIdx + 9 : caretIdx])

        allTags = os.popen("git tag -l p4/").readlines()
        for i in range(len(allTags)):
            allTags[i] = int(allTags[i][3:-1])

        allTags.sort()

        allTags.remove(rev)

        for rev in allTags:
            print os.popen("git tag -d p4/%s" % rev).read()

        print "%s tags removed." % len(allTags)
        return True

class P4Sync(Command):
    def __init__(self):
        Command.__init__(self)
        self.options = [
                optparse.make_option("--continue", action="store_false", dest="firstTime"),
                optparse.make_option("--origin", dest="origin"),
                optparse.make_option("--reset", action="store_true", dest="reset"),
                optparse.make_option("--master", dest="master"),
                optparse.make_option("--log-substitutions", dest="substFile"),
                optparse.make_option("--noninteractive", action="store_false"),
                optparse.make_option("--dry-run", action="store_true"),
                optparse.make_option("--apply-as-patch", action="store_true", dest="applyAsPatch")
        ]
        self.description = "Submit changes from git to the perforce depot."
        self.firstTime = True
        self.reset = False
        self.interactive = True
        self.dryRun = False
        self.substFile = ""
        self.firstTime = True
        self.origin = "origin"
        self.master = ""
        self.applyAsPatch = True

        self.logSubstitutions = {}
        self.logSubstitutions["<enter description here>"] = "%log%"
        self.logSubstitutions["\tDetails:"] = "\tDetails:  %log%"

    def check(self):
        if len(p4CmdList("opened ...")) > 0:
            die("You have files opened with perforce! Close them before starting the sync.")

    def start(self):
        if len(self.config) > 0 and not self.reset:
            die("Cannot start sync. Previous sync config found at %s" % self.configFile)

        commits = []
        for line in os.popen("git-rev-list --no-merges %s..%s" % (self.origin, self.master)).readlines():
            commits.append(line[:-1])
        commits.reverse()

        self.config["commits"] = commits

        if not self.applyAsPatch:
            print "Creating temporary p4-sync branch from %s ..." % self.origin
            system("git checkout -f -b p4-sync %s" % self.origin)

{'fraction_non_alphanumeric': 0.14063300215097818, 'fraction_numerical': 0.00850148519922155, 'mean_word_length': 2.1939810271508016, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4125122', 'n_tokens_mistral': 3104, 'n_tokens_neox': 2981, 'n_words': 760}