1 #! /usr/bin/python
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License version 2
5 # as published by the Free Software Foundation.
6 #
7 # This program is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 # GNU General Public License for more details.
11 #
12 # You should have received a copy of the GNU General Public License
13 # along with this program; if not, write to the Free Software
14 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 #
16
17 #
18 # Copyright 2009 Grigale, Inc. All rights reserved.
19 # Use is subject to license terms.
20 #
21
22 '''
23 Create a wx-style active list on stdout based on a Git
24 workspace in support of webrev's Git support.
25 '''
26
27 #
28 # NB: This assumes the normal onbld directory structure
29 #
30 import os
31 import sys
32 import optparse
33 import subprocess
34
35 def execCmd(cmd):
36 '''Executes external command'''
37
38 p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
39 stderr=subprocess.PIPE)
40 (out, err) = p.communicate()
41
42 if out == None:
43 out = ""
44
45 return (p.returncode, out.splitlines(), err.splitlines())
46
47 def usage():
48 sys.stderr.write("usage: %s [-p parent] -w workspace\n" %
49 os.path.basename(__file__))
50 sys.exit(2)
51
52 def main(argv):
53
54 parser = optparse.OptionParser(version='%prog 1.0')
55 parser.add_option("-p", dest="parentpath", default="", help="parent repo")
56 parser.add_option("-w", dest="wspath", default="", help="workspace")
57 parser.add_option("-o", dest="outputfile", help="output file")
58 parser.disable_interspersed_args()
59
60 (options, args) = parser.parse_args()
61
62 if not options.wspath:
63 usage()
64
65 fh = None
66 if options.outputfile:
67 try:
68 fh = open(options.outputfile, 'w')
69 except EnvironmentError, e:
70 sys.stderr.write("could not open output file: %s\n" % e)
71 sys.exit(1)
72 else:
73 fh = sys.stdout
74
75 cmd = ["git", "--git-dir=%s" % options.wspath, "log", "--name-only",
76 "--parents", "--reverse", "--pretty=short", "master.."]
77 (rc, out, err) = execCmd(cmd)
78
79 #print rc, out, err
80
81 if "commit" in out[0]:
82 parent = out[0].split()[2]
83
84 files = {}
85 comment = None
86 for i in out:
87 if "commit" in i:
88 comment = None
89 continue
90 if i == "" or i.startswith("Author"):
91 continue
92 if i.startswith(" "):
93 comment = i.strip()
94 continue
95 if comment:
96 if i not in files:
97 files[i] = []
98 files[i].append(comment)
99
100 fh.write("GIT_PARENT=%s\n" % parent)
101
102 for file in files:
103 #if entry.is_renamed():
104 # fh.write("%s %s\n" % (entry.name, entry.parentname))
105 #else:
106 # fh.write("%s\n" % entry.name)
107 fh.write("%s\n\n" % file)
108 fh.write("%s\n\n" % '\n'.join(files[file]))
109
110 #try:
111 # Version.check_version()
112 #except Version.VersionMismatch, e:
113 # sys.stderr.write("Error: %s\n" % e)
114 # sys.exit(1)
115
116 if __name__ == '__main__':
117 try:
118 main(sys.argv[1:])
119 except KeyboardInterrupt:
120 sys.exit(1)
121 # except util.Abort, msg:
122 # sys.stderr.write("Abort: %s\n" % msg)
123 # sys.exit(1)