Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
| 2 | # |
| 3 | # Copyright 2020 Google LLC |
| 4 | # |
| 5 | """Handles the main control logic of patman |
| 6 | |
| 7 | This module provides various functions called by the main program to implement |
| 8 | the features of patman. |
| 9 | """ |
| 10 | |
Simon Glass | 3c0196f | 2025-04-29 07:21:58 -0600 | [diff] [blame] | 11 | import re |
Simon Glass | 3c0196f | 2025-04-29 07:21:58 -0600 | [diff] [blame] | 12 | import traceback |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 13 | |
Simon Glass | 3c0196f | 2025-04-29 07:21:58 -0600 | [diff] [blame] | 14 | try: |
| 15 | from importlib import resources |
| 16 | except ImportError: |
| 17 | # for Python 3.6 |
| 18 | import importlib_resources as resources |
| 19 | |
Simon Glass | ba1b3b9 | 2025-02-09 14:26:00 -0700 | [diff] [blame] | 20 | from u_boot_pylib import gitutil |
Simon Glass | 131444f | 2023-02-23 18:18:04 -0700 | [diff] [blame] | 21 | from u_boot_pylib import terminal |
Simon Glass | 3c0196f | 2025-04-29 07:21:58 -0600 | [diff] [blame] | 22 | from u_boot_pylib import tools |
Simon Glass | 6dfb4dd | 2025-05-10 13:05:11 +0200 | [diff] [blame^] | 23 | from u_boot_pylib import tout |
| 24 | from patman import cseries |
| 25 | from patman import cser_helper |
Simon Glass | 3c0196f | 2025-04-29 07:21:58 -0600 | [diff] [blame] | 26 | from patman import patchstream |
Simon Glass | 6dfb4dd | 2025-05-10 13:05:11 +0200 | [diff] [blame^] | 27 | from patman.patchwork import Patchwork |
Simon Glass | c025798 | 2025-04-29 07:22:11 -0600 | [diff] [blame] | 28 | from patman import send |
Simon Glass | aaaff53 | 2025-04-07 22:51:43 +1200 | [diff] [blame] | 29 | from patman import settings |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 30 | |
Maxim Cournoyer | 12f99fd | 2023-10-12 23:06:24 -0400 | [diff] [blame] | 31 | |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 32 | def setup(): |
| 33 | """Do required setup before doing anything""" |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 34 | gitutil.setup() |
Simon Glass | aaaff53 | 2025-04-07 22:51:43 +1200 | [diff] [blame] | 35 | alias_fname = gitutil.get_alias_file() |
| 36 | if alias_fname: |
| 37 | settings.ReadGitAliases(alias_fname) |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 38 | |
Maxim Cournoyer | 12f99fd | 2023-10-12 23:06:24 -0400 | [diff] [blame] | 39 | |
Simon Glass | c025798 | 2025-04-29 07:22:11 -0600 | [diff] [blame] | 40 | def do_send(args): |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 41 | """Create, check and send patches by email |
| 42 | |
| 43 | Args: |
Simon Glass | eb101ac | 2020-07-05 21:41:53 -0600 | [diff] [blame] | 44 | args (argparse.Namespace): Arguments to patman |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 45 | """ |
| 46 | setup() |
Simon Glass | c025798 | 2025-04-29 07:22:11 -0600 | [diff] [blame] | 47 | send.send(args) |
Simon Glass | 2eb4da7 | 2020-07-05 21:41:51 -0600 | [diff] [blame] | 48 | |
Simon Glass | 3db916d | 2020-10-29 21:46:35 -0600 | [diff] [blame] | 49 | |
Simon Glass | 2112d07 | 2020-10-29 21:46:38 -0600 | [diff] [blame] | 50 | def patchwork_status(branch, count, start, end, dest_branch, force, |
Simon Glass | 802eeea | 2025-04-29 07:22:25 -0600 | [diff] [blame] | 51 | show_comments, url, single_thread=False): |
Simon Glass | 3db916d | 2020-10-29 21:46:35 -0600 | [diff] [blame] | 52 | """Check the status of patches in patchwork |
| 53 | |
| 54 | This finds the series in patchwork using the Series-link tag, checks for new |
Simon Glass | 2112d07 | 2020-10-29 21:46:38 -0600 | [diff] [blame] | 55 | comments and review tags, displays then and creates a new branch with the |
| 56 | review tags. |
Simon Glass | 3db916d | 2020-10-29 21:46:35 -0600 | [diff] [blame] | 57 | |
| 58 | Args: |
| 59 | branch (str): Branch to create patches from (None = current) |
| 60 | count (int): Number of patches to produce, or -1 to produce patches for |
| 61 | the current branch back to the upstream commit |
| 62 | start (int): Start partch to use (0=first / top of branch) |
| 63 | end (int): End patch to use (0=last one in series, 1=one before that, |
| 64 | etc.) |
Simon Glass | d0a0a58 | 2020-10-29 21:46:36 -0600 | [diff] [blame] | 65 | dest_branch (str): Name of new branch to create with the updated tags |
| 66 | (None to not create a branch) |
| 67 | force (bool): With dest_branch, force overwriting an existing branch |
Simon Glass | 2112d07 | 2020-10-29 21:46:38 -0600 | [diff] [blame] | 68 | show_comments (bool): True to display snippets from the comments |
| 69 | provided by reviewers |
Simon Glass | 4acc93c | 2020-11-03 13:54:16 -0700 | [diff] [blame] | 70 | url (str): URL of patchwork server, e.g. 'https://patchwork.ozlabs.org'. |
| 71 | This is ignored if the series provides a Series-patchwork-url tag. |
Simon Glass | 3db916d | 2020-10-29 21:46:35 -0600 | [diff] [blame] | 72 | |
| 73 | Raises: |
| 74 | ValueError: if the branch has no Series-link value |
| 75 | """ |
Simon Glass | 6dfb4dd | 2025-05-10 13:05:11 +0200 | [diff] [blame^] | 76 | if not branch: |
| 77 | branch = gitutil.get_branch() |
Simon Glass | 3db916d | 2020-10-29 21:46:35 -0600 | [diff] [blame] | 78 | if count == -1: |
| 79 | # Work out how many patches to send if we can |
Simon Glass | 6dfb4dd | 2025-05-10 13:05:11 +0200 | [diff] [blame^] | 80 | count = gitutil.count_commits_to_branch(branch) - start |
Simon Glass | 3db916d | 2020-10-29 21:46:35 -0600 | [diff] [blame] | 81 | |
| 82 | series = patchstream.get_metadata(branch, start, count - end) |
| 83 | warnings = 0 |
| 84 | for cmt in series.commits: |
| 85 | if cmt.warn: |
| 86 | print('%d warnings for %s:' % (len(cmt.warn), cmt.hash)) |
| 87 | for warn in cmt.warn: |
| 88 | print('\t', warn) |
| 89 | warnings += 1 |
| 90 | print |
| 91 | if warnings: |
| 92 | raise ValueError('Please fix warnings before running status') |
| 93 | links = series.get('links') |
| 94 | if not links: |
| 95 | raise ValueError("Branch has no Series-links value") |
| 96 | |
Simon Glass | 6dfb4dd | 2025-05-10 13:05:11 +0200 | [diff] [blame^] | 97 | _, version = cser_helper.split_name_version(branch) |
| 98 | link = series.get_link_for_version(version, links) |
| 99 | if not link: |
| 100 | raise ValueError('Series-links has no link for v{version}') |
| 101 | tout.debug(f"Link '{link}") |
Simon Glass | 3db916d | 2020-10-29 21:46:35 -0600 | [diff] [blame] | 102 | |
Simon Glass | 4acc93c | 2020-11-03 13:54:16 -0700 | [diff] [blame] | 103 | # Allow the series to override the URL |
| 104 | if 'patchwork_url' in series: |
| 105 | url = series.patchwork_url |
Simon Glass | 6dfb4dd | 2025-05-10 13:05:11 +0200 | [diff] [blame^] | 106 | pwork = Patchwork(url, single_thread=single_thread) |
Simon Glass | 4acc93c | 2020-11-03 13:54:16 -0700 | [diff] [blame] | 107 | |
Simon Glass | 3db916d | 2020-10-29 21:46:35 -0600 | [diff] [blame] | 108 | # Import this here to avoid failing on other commands if the dependencies |
| 109 | # are not present |
| 110 | from patman import status |
Simon Glass | 6dfb4dd | 2025-05-10 13:05:11 +0200 | [diff] [blame^] | 111 | pwork = Patchwork(url) |
| 112 | status.check_and_show_status(series, link, branch, dest_branch, force, |
| 113 | show_comments, False, pwork) |
Simon Glass | 3c0196f | 2025-04-29 07:21:58 -0600 | [diff] [blame] | 114 | |
| 115 | |
| 116 | def do_patman(args): |
Simon Glass | b8a14f9 | 2025-05-08 06:30:14 +0200 | [diff] [blame] | 117 | """Process a patman command |
| 118 | |
| 119 | Args: |
| 120 | args (Namespace): Arguments to process |
| 121 | """ |
| 122 | if args.full_help: |
| 123 | with resources.path('patman', 'README.rst') as readme: |
| 124 | tools.print_full_help(str(readme)) |
| 125 | return 0 |
Simon Glass | 3c0196f | 2025-04-29 07:21:58 -0600 | [diff] [blame] | 126 | if args.cmd == 'send': |
| 127 | # Called from git with a patch filename as argument |
| 128 | # Printout a list of additional CC recipients for this patch |
| 129 | if args.cc_cmd: |
| 130 | re_line = re.compile(r'(\S*) (.*)') |
| 131 | with open(args.cc_cmd, 'r', encoding='utf-8') as inf: |
| 132 | for line in inf.readlines(): |
| 133 | match = re_line.match(line) |
| 134 | if match and match.group(1) == args.patchfiles[0]: |
| 135 | for cca in match.group(2).split('\0'): |
| 136 | cca = cca.strip() |
| 137 | if cca: |
| 138 | print(cca) |
Simon Glass | 3c0196f | 2025-04-29 07:21:58 -0600 | [diff] [blame] | 139 | else: |
| 140 | # If we are not processing tags, no need to warning about bad ones |
| 141 | if not args.process_tags: |
| 142 | args.ignore_bad_tags = True |
Simon Glass | c025798 | 2025-04-29 07:22:11 -0600 | [diff] [blame] | 143 | do_send(args) |
Simon Glass | b8a14f9 | 2025-05-08 06:30:14 +0200 | [diff] [blame] | 144 | return 0 |
Simon Glass | 3c0196f | 2025-04-29 07:21:58 -0600 | [diff] [blame] | 145 | |
Simon Glass | 78ee8f8 | 2025-04-29 07:22:09 -0600 | [diff] [blame] | 146 | ret_code = 0 |
| 147 | try: |
| 148 | # Check status of patches in patchwork |
| 149 | if args.cmd == 'status': |
Simon Glass | 3c0196f | 2025-04-29 07:21:58 -0600 | [diff] [blame] | 150 | patchwork_status(args.branch, args.count, args.start, args.end, |
| 151 | args.dest_branch, args.force, args.show_comments, |
| 152 | args.patchwork_url) |
Simon Glass | 78ee8f8 | 2025-04-29 07:22:09 -0600 | [diff] [blame] | 153 | except Exception as exc: |
| 154 | terminal.tprint(f'patman: {type(exc).__name__}: {exc}', |
| 155 | colour=terminal.Color.RED) |
| 156 | if args.debug: |
| 157 | print() |
| 158 | traceback.print_exc() |
| 159 | ret_code = 1 |
| 160 | return ret_code |