Provide a tiny little bit of help
[fast-export.git] / git-p4.py
blob42efc7d9aa29a31e2d45f4d6705e0984dc9fd35c
1 #!/usr/bin/env python
3 # git-p4.py -- A tool for bidirectional operation between a Perforce depot and git.
5 # Author: Simon Hausmann <hausmann@kde.org>
6 # License: MIT <http://www.opensource.org/licenses/mit-license.php>
9 import optparse, sys, os, marshal, popen2
11 def p4CmdList(cmd):
12 cmd = "p4 -G %s" % cmd
13 pipe = os.popen(cmd, "rb")
15 result = []
16 try:
17 while True:
18 entry = marshal.load(pipe)
19 result.append(entry)
20 except EOFError:
21 pass
22 pipe.close()
24 return result
26 def p4Cmd(cmd):
27 list = p4CmdList(cmd)
28 result = {}
29 for entry in list:
30 result.update(entry)
31 return result;
33 def die(msg):
34 sys.stderr.write(msg + "\n")
35 sys.exit(1)
37 def currentGitBranch():
38 return os.popen("git-name-rev HEAD").read().split(" ")[1][:-1]
40 class P4Debug:
41 def __init__(self):
42 self.options = [
44 self.description = "A tool to debug the output of p4 -G."
46 def run(self, args):
47 for output in p4CmdList(" ".join(args)):
48 print output
50 class P4CleanTags:
51 def __init__(self):
52 self.options = [
53 # optparse.make_option("--branch", dest="branch", default="refs/heads/master")
55 self.description = "A tool to remove stale unused tags from incremental perforce imports."
56 def run(self, args):
57 branch = currentGitBranch()
58 print "Cleaning out stale p4 import tags..."
59 sout, sin, serr = popen2.popen3("git-name-rev --tags `git-rev-parse %s`" % branch)
60 output = sout.read()
61 try:
62 tagIdx = output.index(" tags/p4/")
63 except:
64 print "Cannot find any p4/* tag. Nothing to do."
65 sys.exit(0)
67 try:
68 caretIdx = output.index("^")
69 except:
70 caretIdx = len(output) - 1
71 rev = int(output[tagIdx + 9 : caretIdx])
73 allTags = os.popen("git tag -l p4/").readlines()
74 for i in range(len(allTags)):
75 allTags[i] = int(allTags[i][3:-1])
77 allTags.sort()
79 allTags.remove(rev)
81 for rev in allTags:
82 print os.popen("git tag -d p4/%s" % rev).read()
84 print "%s tags removed." % len(allTags)
86 def printUsage(commands):
87 print "usage: %s <command> [options]" % sys.argv[0]
88 print ""
89 print "valid commands: %s" % ", ".join(commands)
90 print ""
91 print "Try %s <command> --help for command specific help." % sys.argv[0]
92 print ""
94 commands = {
95 "debug" : P4Debug(),
96 "clean-tags" : P4CleanTags()
99 if len(sys.argv[1:]) == 0:
100 printUsage(commands.keys())
101 sys.exit(2)
103 cmd = ""
104 cmdName = sys.argv[1]
105 try:
106 cmd = commands[cmdName]
107 except KeyError:
108 print "unknown command %s" % cmdName
109 print ""
110 printUsage(commands.keys())
111 sys.exit(2)
113 parser = optparse.OptionParser("usage: %prog " + cmdName + " [options]", cmd.options,
114 description = cmd.description)
116 (cmd, args) = parser.parse_args(sys.argv[2:], cmd);
118 cmd.run(args)