OLD | NEW |
| (Empty) |
1 #!/usr/bin/python | |
2 | |
3 # Repo lives in ~evanm/projects/git-try -- feel free to send patches. | |
4 | |
5 import getpass | |
6 import optparse | |
7 import os | |
8 import subprocess | |
9 import tempfile | |
10 import traceback | |
11 import urllib | |
12 import sys | |
13 | |
14 | |
15 def Backquote(cmd): | |
16 """Like running `cmd` in a shell script.""" | |
17 return subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0].strip() | |
18 | |
19 | |
20 def GetTryServerConfig(): | |
21 """Returns the dictionary of try server options or None if they | |
22 cannot be found.""" | |
23 script_path = 'tools/tryserver/tryserver.py' | |
24 root_dir = Backquote(['git', 'rev-parse', '--show-cdup']) | |
25 try: | |
26 script_file = open(os.path.join(root_dir, script_path)) | |
27 except IOError: | |
28 return None | |
29 locals = {} | |
30 try: | |
31 exec(script_file, locals) | |
32 except Exception, e: | |
33 return None | |
34 return locals | |
35 | |
36 | |
37 def GetBranchName(): | |
38 """Return name of current git branch.""" | |
39 branch = Backquote(['git', 'symbolic-ref', 'HEAD']) | |
40 if not branch.startswith('refs/heads/'): | |
41 raise "Couldn't figure out branch name" | |
42 branch = branch[len('refs/heads/'):] | |
43 return branch | |
44 | |
45 | |
46 def GetPatchName(): | |
47 """Construct a name for this patch.""" | |
48 short_sha = Backquote(['git', 'rev-parse', '--short=4', 'HEAD']) | |
49 return GetBranchName() + '-' + short_sha | |
50 | |
51 | |
52 def GetRevision(): | |
53 """Get the latest Subversion revision number.""" | |
54 for line in Backquote(['git', 'svn', 'info']).split('\n'): | |
55 if line.startswith('Revision:'): | |
56 return line[len('Revision:'):].strip() | |
57 raise "Couldn't figure out latest revision" | |
58 | |
59 | |
60 def GetRietveldIssueNumber(): | |
61 return Backquote(['git', 'config', | |
62 'branch.%s.rietveldissue' % GetBranchName()]) | |
63 | |
64 | |
65 def GetRietveldPatchsetNumber(): | |
66 return Backquote(['git', 'config', | |
67 'branch.%s.rietveldpatchset' % GetBranchName()]) | |
68 | |
69 | |
70 def GetMungedDiff(branch): | |
71 """Get the diff we'll send to the try server. We munge paths to match svn.""" | |
72 # Make the following changes: | |
73 # - Prepend "src/" to paths as svn is expecting | |
74 # - In the case of added files, replace /dev/null with the path to the file | |
75 # being added. | |
76 output = [] | |
77 if not branch: | |
78 # Try to guess the upstream branch. | |
79 branch = Backquote(['git', 'cl', 'upstream']) | |
80 diff = subprocess.Popen(['git', 'diff-tree', '-p', '--no-prefix', | |
81 branch, 'HEAD'], | |
82 stdout=subprocess.PIPE).stdout.readlines() | |
83 for i in range(len(diff)): | |
84 line = diff[i] | |
85 if line.startswith('--- /dev/null'): | |
86 line = '--- %s' % diff[i+1][4:] | |
87 elif line.startswith('--- ') or line.startswith('+++ '): | |
88 line = line[0:4] + 'src/' + line[4:] | |
89 output.append(line) | |
90 | |
91 return ''.join(output) | |
92 | |
93 | |
94 def GetEmail(): | |
95 # TODO: check for errors here? | |
96 return Backquote(['git', 'config', 'user.email']) | |
97 | |
98 | |
99 def TryChange(args): | |
100 """Put a patch on the try server using SVN.""" | |
101 # TODO: figure out a better way to load trychange | |
102 script_path = '../depot_tools/release' | |
103 root_dir = Backquote(['git', 'rev-parse', '--show-cdup']) | |
104 sys.path.append(os.path.join(root_dir, script_path)) | |
105 import trychange | |
106 trychange.checkout_root = os.path.abspath(root_dir) | |
107 trychange.TryChange(args, None, False) | |
108 | |
109 | |
110 def WriteTryDiffHTTP(config, patch_name, diff, options): | |
111 """Put a patch on the try server.""" | |
112 params = { | |
113 'user': getpass.getuser(), | |
114 'name': patch_name, | |
115 'patch': diff | |
116 } | |
117 | |
118 if options.bot: | |
119 params['bot'] = options.bot | |
120 | |
121 if options.clobber: | |
122 params['clobber'] = 'true' | |
123 | |
124 url = 'http://%s:%s/send_try_patch' % (config['try_server_http_host'], | |
125 config['try_server_http_port']) | |
126 connection = urllib.urlopen(url, urllib.urlencode(params)) | |
127 response = connection.read() | |
128 if (response != 'OK'): | |
129 print "Error posting to", url | |
130 print response | |
131 assert False | |
132 | |
133 | |
134 if __name__ == '__main__': | |
135 parser = optparse.OptionParser( | |
136 usage='git try [branch]', | |
137 description='Upload the current diff of branch...HEAD to the try server.') | |
138 parser.add_option("-b", "--bot", | |
139 help="Force the use of a specific build slave (eg mac, " | |
140 "win, or linux)") | |
141 parser.add_option("-c", "--clobber", action="store_true", | |
142 help="Make the try run use be a clobber build") | |
143 (options, args) = parser.parse_args(sys.argv) | |
144 | |
145 branch = None | |
146 if len(args) > 1: | |
147 branch = args[1] | |
148 | |
149 patch_name = GetPatchName() | |
150 diff = GetMungedDiff(branch) | |
151 | |
152 # Send directly to try server if we can parse the config, otherwise | |
153 # upload via SVN. | |
154 config = GetTryServerConfig() | |
155 if config is not None: | |
156 print "Sending %s using HTTP..." % patch_name | |
157 WriteTryDiffHTTP(config=config, patch_name=patch_name, diff=diff, | |
158 options=options) | |
159 else: | |
160 print "Sending %s using SVN..." % patch_name | |
161 | |
162 # Write the diff out to a temporary file | |
163 diff_file = tempfile.NamedTemporaryFile() | |
164 diff_file.write(diff) | |
165 diff_file.flush() | |
166 | |
167 email = GetEmail() | |
168 user = email.partition('@')[0] | |
169 args = [ | |
170 '--use_svn', | |
171 '--svn_repo', 'svn://svn.chromium.org/chrome-try/try', | |
172 '-u', user, | |
173 '-e', email, | |
174 '-n', patch_name, | |
175 '-r', GetRevision(), | |
176 '--diff', diff_file.name, | |
177 ] | |
178 if options.bot: | |
179 args.extend(['--bot', options.bot]) | |
180 if options.clobber: | |
181 args.append('--clobber') | |
182 if GetRietveldPatchsetNumber(): | |
183 args.extend([ | |
184 '--issue', GetRietveldIssueNumber(), | |
185 '--patchset', GetRietveldPatchsetNumber(), | |
186 ]) | |
187 TryChange(args=args) | |
OLD | NEW |