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