Start moving the git-p4 tools into one single script.
Signed-off-by: Simon Hausmann <hausmann@kde.org>
Simon Hausmann committed
Mar 19, 2007 at 20:59 UTC
86949eef4088d7b57fe7433568d573a926816f5c
3 files changed
+115
-68
contrib/fast-import/git-p4.py
new
+115
@@ -0,0 +1,115 @@
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
+ ]
44
+
45
+ def run(self, args):
46
+ for output in p4CmdList(" ".join(args)):
47
+ print output
48
+
49
+class 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
+
84
+def 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
+
92
+commands = {
93
+ "debug" : P4Debug(),
94
+ "clean-tags" : P4CleanTags()
95
+}
96
+
97
+if len(sys.argv[1:]) == 0:
98
+ printUsage(commands.keys())
99
+ sys.exit(2)
100
+
101
+cmd = ""
102
+cmdName = sys.argv[1]
103
+try:
104
+ cmd = commands[cmdName]
105
+except KeyError:
106
+ print "unknown command %s" % cmdName
107
+ print ""
108
+ printUsage(commands.keys())
109
+ sys.exit(2)
110
+
111
+parser = optparse.OptionParser("usage: %prog " + cmdName + " [options]", cmd.options)
112
+
113
+(cmd, args) = parser.parse_args(sys.argv[2:], cmd);
114
+
115
+cmd.run(args)
contrib/fast-import/p4-clean-tags.py
deleted
-43
@@ -1,43 +0,0 @@
1
-#!/usr/bin/python
2
-#
3
-# p4-debug.py
4
-#
5
-# Author: Simon Hausmann <hausmann@kde.org>
6
-# License: MIT <http://www.opensource.org/licenses/mit-license.php>
7
-#
8
-# removes unused p4 import tags
9
-#
10
-import os, string, sys
11
-import popen2, getopt
12
-
13
-branch = "refs/heads/master"
14
-
15
-try:
16
- opts, args = getopt.getopt(sys.argv[1:], "", [ "branch=" ])
17
-except getopt.GetoptError:
18
- print "fixme, syntax error"
19
- sys.exit(1)
20
-
21
-for o, a in opts:
22
- if o == "--branch":
23
- branch = "refs/heads/" + a
24
-
25
-sout, sin, serr = popen2.popen3("git-name-rev --tags `git-rev-parse %s`" % branch)
26
-output = sout.read()
27
-tagIdx = output.index(" tags/p4/")
28
-try:
29
- caretIdx = output.index("^")
30
-except:
31
- caretIdx = len(output) - 1
32
-rev = int(output[tagIdx + 9 : caretIdx])
33
-
34
-allTags = os.popen("git tag -l p4/").readlines()
35
-for i in range(len(allTags)):
36
- allTags[i] = int(allTags[i][3:-1])
37
-
38
-allTags.sort()
39
-
40
-allTags.remove(rev)
41
-
42
-for rev in allTags:
43
- print os.popen("git tag -d p4/%s" % rev).read()
contrib/fast-import/p4-debug.py
deleted
-25
@@ -1,25 +0,0 @@
1
-#!/usr/bin/python
2
-#
3
-# p4-debug.py
4
-#
5
-# Author: Simon Hausmann <hausmann@kde.org>
6
-# License: MIT <http://www.opensource.org/licenses/mit-license.php>
7
-#
8
-# executes a p4 command with -G and prints the resulting python dicts
9
-#
10
-import os, string, sys
11
-import marshal, popen2
12
-
13
-cmd = ""
14
-for arg in sys.argv[1:]:
15
- cmd += arg + " "
16
-
17
-pipe = os.popen("p4 -G %s" % cmd, "rb")
18
-try:
19
- while True:
20
- entry = marshal.load(pipe)
21
- print entry
22
-except EOFError:
23
- pass
24
-pipe.close()
25
-