diff options
author | Remy Bohmer <github@bohmer.net> | 2020-08-01 18:36:44 +0200 |
---|---|---|
committer | Mike Frysinger <vapier@google.com> | 2020-11-23 09:59:16 +0000 |
commit | 7f7acfe9fd93cfd4a697f2bc851d1b8182f6336e (patch) | |
tree | 646e53f6ccbedd55105017797b6f5b883909a3dc /subcmds/upload.py | |
parent | 169b0218b384f04426d7509757a8684f957967bf (diff) | |
download | git-repo-7f7acfe9fd93cfd4a697f2bc851d1b8182f6336e.tar.gz |
Concentrate the RepoHook knowledge in the RepoHook class
The knowledge about running hooks and all its exception handling
is scattered over multiple files. This makes the code harder
to read, but also it requires duplication of logic in case
other RepoHooks are added to different commands.
This refactoring also creates uniform behavior of the hooks
across multiple commands and it guarantees the re-use of the same
arguments on all of them.
Signed-off-by: Remy Bohmer <github@bohmer.net>
Change-Id: Ia4d90eab429e4af00943306e89faec8db35ba29d
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/277562
Tested-by: Remy Bohmer <oss@bohmer.net>
Reviewed-by: Mike Frysinger <vapier@google.com>
Diffstat (limited to 'subcmds/upload.py')
-rw-r--r-- | subcmds/upload.py | 64 |
1 files changed, 11 insertions, 53 deletions
diff --git a/subcmds/upload.py b/subcmds/upload.py index f441aae4..6196fe4c 100644 --- a/subcmds/upload.py +++ b/subcmds/upload.py | |||
@@ -21,7 +21,7 @@ import sys | |||
21 | 21 | ||
22 | from command import InteractiveCommand | 22 | from command import InteractiveCommand |
23 | from editor import Editor | 23 | from editor import Editor |
24 | from error import HookError, UploadError | 24 | from error import UploadError |
25 | from git_command import GitCommand | 25 | from git_command import GitCommand |
26 | from git_refs import R_HEADS | 26 | from git_refs import R_HEADS |
27 | from hooks import RepoHook | 27 | from hooks import RepoHook |
@@ -205,33 +205,7 @@ Gerrit Code Review: https://www.gerritcodereview.com/ | |||
205 | p.add_option('--no-cert-checks', | 205 | p.add_option('--no-cert-checks', |
206 | dest='validate_certs', action='store_false', default=True, | 206 | dest='validate_certs', action='store_false', default=True, |
207 | help='Disable verifying ssl certs (unsafe).') | 207 | help='Disable verifying ssl certs (unsafe).') |
208 | 208 | RepoHook.AddOptionGroup(p, 'pre-upload') | |
209 | # Options relating to upload hook. Note that verify and no-verify are NOT | ||
210 | # opposites of each other, which is why they store to different locations. | ||
211 | # We are using them to match 'git commit' syntax. | ||
212 | # | ||
213 | # Combinations: | ||
214 | # - no-verify=False, verify=False (DEFAULT): | ||
215 | # If stdout is a tty, can prompt about running upload hooks if needed. | ||
216 | # If user denies running hooks, the upload is cancelled. If stdout is | ||
217 | # not a tty and we would need to prompt about upload hooks, upload is | ||
218 | # cancelled. | ||
219 | # - no-verify=False, verify=True: | ||
220 | # Always run upload hooks with no prompt. | ||
221 | # - no-verify=True, verify=False: | ||
222 | # Never run upload hooks, but upload anyway (AKA bypass hooks). | ||
223 | # - no-verify=True, verify=True: | ||
224 | # Invalid | ||
225 | g = p.add_option_group('Upload hooks') | ||
226 | g.add_option('--no-verify', | ||
227 | dest='bypass_hooks', action='store_true', | ||
228 | help='Do not run the upload hook.') | ||
229 | g.add_option('--verify', | ||
230 | dest='allow_all_hooks', action='store_true', | ||
231 | help='Run the upload hook without prompting.') | ||
232 | g.add_option('--ignore-hooks', | ||
233 | dest='ignore_hooks', action='store_true', | ||
234 | help='Do not abort uploading if upload hooks fail.') | ||
235 | 209 | ||
236 | def _SingleBranch(self, opt, branch, people): | 210 | def _SingleBranch(self, opt, branch, people): |
237 | project = branch.project | 211 | project = branch.project |
@@ -572,31 +546,15 @@ Gerrit Code Review: https://www.gerritcodereview.com/ | |||
572 | (branch,), file=sys.stderr) | 546 | (branch,), file=sys.stderr) |
573 | return 1 | 547 | return 1 |
574 | 548 | ||
575 | if not opt.bypass_hooks: | 549 | pending_proj_names = [project.name for (project, available) in pending] |
576 | hook = RepoHook('pre-upload', self.manifest.repo_hooks_project, | 550 | pending_worktrees = [project.worktree for (project, available) in pending] |
577 | self.manifest.topdir, | 551 | hook = RepoHook.FromSubcmd( |
578 | self.manifest.manifestProject.GetRemote('origin').url, | 552 | hook_type='pre-upload', manifest=self.manifest, |
579 | abort_if_user_denies=True) | 553 | opt=opt, abort_if_user_denies=True) |
580 | pending_proj_names = [project.name for (project, available) in pending] | 554 | if not hook.Run( |
581 | pending_worktrees = [project.worktree for (project, available) in pending] | 555 | project_list=pending_proj_names, |
582 | passed = True | 556 | worktree_list=pending_worktrees): |
583 | try: | 557 | return 1 |
584 | hook.Run(opt.allow_all_hooks, project_list=pending_proj_names, | ||
585 | worktree_list=pending_worktrees) | ||
586 | except SystemExit: | ||
587 | passed = False | ||
588 | if not opt.ignore_hooks: | ||
589 | raise | ||
590 | except HookError as e: | ||
591 | passed = False | ||
592 | print("ERROR: %s" % str(e), file=sys.stderr) | ||
593 | |||
594 | if not passed: | ||
595 | if opt.ignore_hooks: | ||
596 | print('\nWARNING: pre-upload hooks failed, but uploading anyways.', | ||
597 | file=sys.stderr) | ||
598 | else: | ||
599 | return 1 | ||
600 | 558 | ||
601 | if opt.reviewers: | 559 | if opt.reviewers: |
602 | reviewers = _SplitEmails(opt.reviewers) | 560 | reviewers = _SplitEmails(opt.reviewers) |