From 86949eef4088d7b57fe7433568d573a926816f5c Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Mon, 19 Mar 2007 20:59:12 +0100 Subject: [PATCH] Start moving the git-p4 tools into one single script. Signed-off-by: Simon Hausmann --- contrib/fast-import/git-p4.py | 115 +++++++++++++++++++++++++++ contrib/fast-import/p4-clean-tags.py | 43 ---------- contrib/fast-import/p4-debug.py | 25 ------ 3 files changed, 115 insertions(+), 68 deletions(-) create mode 100755 contrib/fast-import/git-p4.py delete mode 100755 contrib/fast-import/p4-clean-tags.py delete mode 100755 contrib/fast-import/p4-debug.py diff --git a/contrib/fast-import/git-p4.py b/contrib/fast-import/git-p4.py new file mode 100755 index 000000000..800815604 --- /dev/null +++ b/contrib/fast-import/git-p4.py @@ -0,0 +1,115 @@ +#!/usr/bin/env python +# +# git-p4.py -- A tool for bidirectional operation between a Perforce depot and git. +# +# Author: Simon Hausmann +# License: MIT +# + +import optparse, sys, os, marshal, popen2 + +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] + +class P4Debug: + def __init__(self): + self.options = [ + ] + + def run(self, args): + for output in p4CmdList(" ".join(args)): + print output + +class P4CleanTags: + def __init__(self): + self.options = [ +# optparse.make_option("--branch", dest="branch", default="refs/heads/master") + ] + 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) + +def printUsage(commands): + print "usage: %s [options]" % sys.argv[0] + print "" + print "valid commands: %s" % ", ".join(commands) + print "" + print "Try %s --help for command specific help." % sys.argv[0] + print "" + +commands = { + "debug" : P4Debug(), + "clean-tags" : P4CleanTags() +} + +if len(sys.argv[1:]) == 0: + printUsage(commands.keys()) + sys.exit(2) + +cmd = "" +cmdName = sys.argv[1] +try: + cmd = commands[cmdName] +except KeyError: + print "unknown command %s" % cmdName + print "" + printUsage(commands.keys()) + sys.exit(2) + +parser = optparse.OptionParser("usage: %prog " + cmdName + " [options]", cmd.options) + +(cmd, args) = parser.parse_args(sys.argv[2:], cmd); + +cmd.run(args) diff --git a/contrib/fast-import/p4-clean-tags.py b/contrib/fast-import/p4-clean-tags.py deleted file mode 100755 index 924ff89cc..000000000 --- a/contrib/fast-import/p4-clean-tags.py +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/python -# -# p4-debug.py -# -# Author: Simon Hausmann -# License: MIT -# -# removes unused p4 import tags -# -import os, string, sys -import popen2, getopt - -branch = "refs/heads/master" - -try: - opts, args = getopt.getopt(sys.argv[1:], "", [ "branch=" ]) -except getopt.GetoptError: - print "fixme, syntax error" - sys.exit(1) - -for o, a in opts: - if o == "--branch": - branch = "refs/heads/" + a - -sout, sin, serr = popen2.popen3("git-name-rev --tags `git-rev-parse %s`" % branch) -output = sout.read() -tagIdx = output.index(" tags/p4/") -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() diff --git a/contrib/fast-import/p4-debug.py b/contrib/fast-import/p4-debug.py deleted file mode 100755 index 8fb159fd6..000000000 --- a/contrib/fast-import/p4-debug.py +++ /dev/null @@ -1,25 +0,0 @@ -#!/usr/bin/python -# -# p4-debug.py -# -# Author: Simon Hausmann -# License: MIT -# -# executes a p4 command with -G and prints the resulting python dicts -# -import os, string, sys -import marshal, popen2 - -cmd = "" -for arg in sys.argv[1:]: - cmd += arg + " " - -pipe = os.popen("p4 -G %s" % cmd, "rb") -try: - while True: - entry = marshal.load(pipe) - print entry -except EOFError: - pass -pipe.close() - -- 2.30.2