3333import tempfile
3434import shlex
3535
36- HEAD_BRANCH_NAME = "__tmp-localonly-head"
37- BASE_BRANCH_NAME = "__tmp-localonly-base"
3836OUTPUT_PATH = "tmp-codegen-diff/"
3937
4038COMMIT_AUTHOR_NAME = "GitHub Action (generated codegen diff)"
@@ -138,9 +136,11 @@ def write_html_template(title, subtitle, tmp_file):
138136 tmp_file .flush ()
139137
140138
141- def make_diff (title , path_to_diff , base_sha , head_sha , suffix , ignore_whitespace ):
139+ def make_diff (opts , title , path_to_diff , suffix , ignore_whitespace ):
140+ base_sha = opts .base_sha
141+ head_sha = opts .head_sha
142142 ws_flag = "-b" if ignore_whitespace else ""
143- diff_exists = get_cmd_status (f"git diff --quiet { ws_flag } { BASE_BRANCH_NAME } { HEAD_BRANCH_NAME } -- { path_to_diff } " )
143+ diff_exists = get_cmd_status (f"git diff --quiet { ws_flag } { opts . base_branch } { opts . head_branch } -- { path_to_diff } " )
144144
145145 if diff_exists == 0 :
146146 eprint (f"No diff output for { base_sha } ..{ head_sha } " )
@@ -156,7 +156,7 @@ def make_diff(title, path_to_diff, base_sha, head_sha, suffix, ignore_whitespace
156156 # All arguments after the first `--` go to the `git diff` command.
157157 diff_cmd = f"diff2html -s line -f html -d word -i command --hwt " \
158158 f"{ tmp_file .name } -F { OUTPUT_PATH } /{ dest_path } -- " \
159- f"-U20 { ws_flag } { BASE_BRANCH_NAME } { HEAD_BRANCH_NAME } -- { path_to_diff } "
159+ f"-U20 { ws_flag } { opts . base_branch } { opts . head_branch } -- { path_to_diff } "
160160 eprint (f"Running diff cmd: { diff_cmd } " )
161161 run (diff_cmd )
162162 return dest_path
@@ -169,29 +169,46 @@ def diff_link(diff_text, empty_diff_text, diff_location, alternate_text, alterna
169169 return f"[{ diff_text } ]({ CDN_URL } /codegen-diff/{ diff_location } ) ([{ alternate_text } ]({ CDN_URL } /codegen-diff/{ alternate_location } ))"
170170
171171
172- def make_diffs (base_sha , head_sha ):
173- sdk_ws = make_diff ('AWS SDK' , f'{ OUTPUT_PATH } /services' , base_sha , head_sha , 'aws-sdk' , ignore_whitespace = False )
174- sdk_no_ws = make_diff ('AWS SDK' , f'{ OUTPUT_PATH } /services' , base_sha , head_sha , 'aws-sdk-ignore-ws' ,
175- ignore_whitespace = True )
176-
172+ def make_diffs (opts ):
173+ sdk_ws = make_diff (opts , 'AWS SDK' , f'{ OUTPUT_PATH } /services' , 'aws-sdk' , ignore_whitespace = False )
174+ sdk_no_ws = make_diff (opts , 'AWS SDK' , f'{ OUTPUT_PATH } /services' , 'aws-sdk-ignore-ws' , ignore_whitespace = True )
177175 sdk_links = diff_link ('AWS SDK' , 'No codegen difference in the AWS SDK' , sdk_ws , 'ignoring whitespace' , sdk_no_ws )
178176
179177 return f'A new generated diff is ready to view.\\ n\\ n- { sdk_links } \\ n'
180178
181179
180+ def _codegen_cmd (opts ):
181+ generate_and_commit_generated_code (opts .head_sha , opts .bootstrap )
182+
183+
184+ def _generate_diffs_cmd (opts ):
185+ bot_message = make_diffs (opts )
186+ with open (f"{ OUTPUT_PATH } /bot-message" , 'w' ) as f :
187+ f .write (bot_message )
188+
189+
182190def create_cli ():
183191 parser = argparse .ArgumentParser (
184192 prog = "codegen-diff-revisions" ,
185193 description = "Generate HTML diffs of codegen output" ,
186194 formatter_class = argparse .ArgumentDefaultsHelpFormatter
187195 )
188196
189- parser .add_argument ("repo_root" , help = "repository root" )
190- parser .add_argument ("base_sha" , help = "base commit to diff against (SHA-like)" )
191- parser .add_argument ("--bootstrap" , help = "services to pass to bootstrap and include in diff output" ,
192- default = "+dynamodb,+codebuild,+sts,+ec2,+polly,+s3" )
193197 parser .add_argument ("--head-sha" , help = "head commit to use (defaults to whatever current HEAD) is" )
194198
199+ subparsers = parser .add_subparsers ()
200+ codegen = subparsers .add_parser ("codegen" , help = "generate and commit generated code" )
201+ codegen .add_argument ("--bootstrap" , help = "services to pass to bootstrap and include in diff output" ,
202+ default = "+dynamodb,+codebuild,+sts,+ec2,+polly,+s3" )
203+ codegen .set_defaults (cmd = _codegen_cmd )
204+
205+ generate_diffs = subparsers .add_parser ("generate-diffs" ,
206+ help = "generate diffs between two branches and output bot message" )
207+ generate_diffs .add_argument ("--base-sha" , help = "base commit to diff against (SHA-like)" )
208+ generate_diffs .add_argument ("base_branch" , help = "name of the base branch to diff against" )
209+ generate_diffs .add_argument ("head_branch" , help = "name of the head branch to diff against" )
210+ generate_diffs .set_defaults (cmd = _generate_diffs_cmd )
211+
195212 return parser
196213
197214
@@ -200,39 +217,12 @@ def main():
200217 opts = cli .parse_args ()
201218 print (opts )
202219
203- os .chdir (opts .repo_root )
204-
205220 if opts .head_sha is None :
206- head_sha = get_cmd_output ("git rev-parse HEAD" )
207- else :
208- head_sha = opts .head_sha
209-
210- print (f"using head sha is { head_sha } " )
211-
212- # Make sure the working tree is clean
213- if get_cmd_status ("git diff --quiet" ) != 0 :
214- eprint ("working tree is not clean. aborting" )
215- sys .exit (1 )
221+ opts .head_sha = get_cmd_output ("git rev-parse HEAD" )
216222
217- # Generate code for HEAD
218- print (f"Creating temporary branch with generated code for the HEAD revision { head_sha } " )
219- run (f"git checkout { head_sha } -b { HEAD_BRANCH_NAME } " )
220- generate_and_commit_generated_code (head_sha , opts .bootstrap )
221-
222- # Generate code for base
223- print (f"Creating temporary branch with generated code for the base revision { opts .base_sha } " )
224- run (f"git checkout { opts .base_sha } -b { BASE_BRANCH_NAME } " )
225- generate_and_commit_generated_code (opts .base_sha , opts .bootstrap )
226-
227- bot_message = make_diffs (opts .base_sha , head_sha )
228- with open (f"{ OUTPUT_PATH } /bot-message" , 'w' ) as f :
229- f .write (bot_message )
223+ print (f"using head sha: { opts .head_sha } " )
230224
231- # cleanup
232- if not running_in_github_action ():
233- run (f"git checkout main" )
234- run (f"git branch -D { BASE_BRANCH_NAME } " )
235- run (f"git branch -D { HEAD_BRANCH_NAME } " )
225+ opts .cmd (opts )
236226
237227
238228if __name__ == '__main__' :
0 commit comments