]>
Commit | Line | Data |
---|---|---|
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 | ||
9 | import optparse, sys, os, marshal, popen2 | |
10 | ||
11 | def 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 | ||
26 | def p4Cmd(cmd): | |
27 | list = p4CmdList(cmd) | |
28 | result = {} | |
29 | for entry in list: | |
30 | result.update(entry) | |
31 | return result; | |
32 | ||
33 | def die(msg): | |
34 | sys.stderr.write(msg + "\n") | |
35 | sys.exit(1) | |
36 | ||
37 | def currentGitBranch(): | |
38 | return os.popen("git-name-rev HEAD").read().split(" ")[1][:-1] | |
39 | ||
40 | class P4Debug: | |
41 | def __init__(self): | |
42 | self.options = [ | |
43 | ] | |
c8c39116 | 44 | self.description = "A tool to debug the output of p4 -G." |
86949eef SH |
45 | |
46 | def run(self, args): | |
47 | for output in p4CmdList(" ".join(args)): | |
48 | print output | |
49 | ||
50 | class P4CleanTags: | |
51 | def __init__(self): | |
52 | self.options = [ | |
53 | # optparse.make_option("--branch", dest="branch", default="refs/heads/master") | |
54 | ] | |
c8c39116 | 55 | self.description = "A tool to remove stale unused tags from incremental perforce imports." |
86949eef SH |
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) | |
66 | ||
67 | try: | |
68 | caretIdx = output.index("^") | |
69 | except: | |
70 | caretIdx = len(output) - 1 | |
71 | rev = int(output[tagIdx + 9 : caretIdx]) | |
72 | ||
73 | allTags = os.popen("git tag -l p4/").readlines() | |
74 | for i in range(len(allTags)): | |
75 | allTags[i] = int(allTags[i][3:-1]) | |
76 | ||
77 | allTags.sort() | |
78 | ||
79 | allTags.remove(rev) | |
80 | ||
81 | for rev in allTags: | |
82 | print os.popen("git tag -d p4/%s" % rev).read() | |
83 | ||
84 | print "%s tags removed." % len(allTags) | |
85 | ||
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 "" | |
93 | ||
94 | commands = { | |
95 | "debug" : P4Debug(), | |
96 | "clean-tags" : P4CleanTags() | |
97 | } | |
98 | ||
99 | if len(sys.argv[1:]) == 0: | |
100 | printUsage(commands.keys()) | |
101 | sys.exit(2) | |
102 | ||
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) | |
112 | ||
c8c39116 SH |
113 | parser = optparse.OptionParser("usage: %prog " + cmdName + " [options]", cmd.options, |
114 | description = cmd.description) | |
86949eef SH |
115 | |
116 | (cmd, args) = parser.parse_args(sys.argv[2:], cmd); | |
117 | ||
118 | cmd.run(args) |