]> git.ipfire.org Git - thirdparty/git.git/blame - contrib/fast-import/git-p4.py
Start moving the git-p4 tools into one single script.
[thirdparty/git.git] / contrib / fast-import / git-p4.py
CommitLineData
86949eef
SH
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# License: MIT <http://www.opensource.org/licenses/mit-license.php>
7#
8
9import optparse, sys, os, marshal, popen2
10
11def p4CmdList(cmd):
12 cmd = "p4 -G %s" % cmd
13 pipe = os.popen(cmd, "rb")
14
15 result = []
16 try:
17 while True:
18 entry = marshal.load(pipe)
19 result.append(entry)
20 except EOFError:
21 pass
22 pipe.close()
23
24 return result
25
26def p4Cmd(cmd):
27 list = p4CmdList(cmd)
28 result = {}
29 for entry in list:
30 result.update(entry)
31 return result;
32
33def die(msg):
34 sys.stderr.write(msg + "\n")
35 sys.exit(1)
36
37def currentGitBranch():
38 return os.popen("git-name-rev HEAD").read().split(" ")[1][:-1]
39
40class P4Debug:
41 def __init__(self):
42 self.options = [
43 ]
44
45 def run(self, args):
46 for output in p4CmdList(" ".join(args)):
47 print output
48
49class P4CleanTags:
50 def __init__(self):
51 self.options = [
52# optparse.make_option("--branch", dest="branch", default="refs/heads/master")
53 ]
54 def run(self, args):
55 branch = currentGitBranch()
56 print "Cleaning out stale p4 import tags..."
57 sout, sin, serr = popen2.popen3("git-name-rev --tags `git-rev-parse %s`" % branch)
58 output = sout.read()
59 try:
60 tagIdx = output.index(" tags/p4/")
61 except:
62 print "Cannot find any p4/* tag. Nothing to do."
63 sys.exit(0)
64
65 try:
66 caretIdx = output.index("^")
67 except:
68 caretIdx = len(output) - 1
69 rev = int(output[tagIdx + 9 : caretIdx])
70
71 allTags = os.popen("git tag -l p4/").readlines()
72 for i in range(len(allTags)):
73 allTags[i] = int(allTags[i][3:-1])
74
75 allTags.sort()
76
77 allTags.remove(rev)
78
79 for rev in allTags:
80 print os.popen("git tag -d p4/%s" % rev).read()
81
82 print "%s tags removed." % len(allTags)
83
84def printUsage(commands):
85 print "usage: %s <command> [options]" % sys.argv[0]
86 print ""
87 print "valid commands: %s" % ", ".join(commands)
88 print ""
89 print "Try %s <command> --help for command specific help." % sys.argv[0]
90 print ""
91
92commands = {
93 "debug" : P4Debug(),
94 "clean-tags" : P4CleanTags()
95}
96
97if len(sys.argv[1:]) == 0:
98 printUsage(commands.keys())
99 sys.exit(2)
100
101cmd = ""
102cmdName = sys.argv[1]
103try:
104 cmd = commands[cmdName]
105except KeyError:
106 print "unknown command %s" % cmdName
107 print ""
108 printUsage(commands.keys())
109 sys.exit(2)
110
111parser = optparse.OptionParser("usage: %prog " + cmdName + " [options]", cmd.options)
112
113(cmd, args) = parser.parse_args(sys.argv[2:], cmd);
114
115cmd.run(args)