blob: a7d381c30084222c7372246130209a7c2bd75967 [file] [log] [blame]
Simon Glass24725af2020-07-05 21:41:49 -06001# SPDX-License-Identifier: GPL-2.0+
2#
3# Copyright 2020 Google LLC
4#
5"""Handles the main control logic of patman
6
7This module provides various functions called by the main program to implement
8the features of patman.
9"""
10
Simon Glass3c0196f2025-04-29 07:21:58 -060011import re
Simon Glass3c0196f2025-04-29 07:21:58 -060012import traceback
Simon Glass24725af2020-07-05 21:41:49 -060013
Simon Glass3c0196f2025-04-29 07:21:58 -060014try:
15 from importlib import resources
16except ImportError:
17 # for Python 3.6
18 import importlib_resources as resources
19
Simon Glassba1b3b92025-02-09 14:26:00 -070020from u_boot_pylib import gitutil
Simon Glass131444f2023-02-23 18:18:04 -070021from u_boot_pylib import terminal
Simon Glass3c0196f2025-04-29 07:21:58 -060022from u_boot_pylib import tools
Simon Glass6dfb4dd2025-05-10 13:05:11 +020023from u_boot_pylib import tout
24from patman import cseries
25from patman import cser_helper
Simon Glass3c0196f2025-04-29 07:21:58 -060026from patman import patchstream
Simon Glass6dfb4dd2025-05-10 13:05:11 +020027from patman.patchwork import Patchwork
Simon Glassc0257982025-04-29 07:22:11 -060028from patman import send
Simon Glassaaaff532025-04-07 22:51:43 +120029from patman import settings
Simon Glass24725af2020-07-05 21:41:49 -060030
Maxim Cournoyer12f99fd2023-10-12 23:06:24 -040031
Simon Glass24725af2020-07-05 21:41:49 -060032def setup():
33 """Do required setup before doing anything"""
Simon Glass761648b2022-01-29 14:14:11 -070034 gitutil.setup()
Simon Glassaaaff532025-04-07 22:51:43 +120035 alias_fname = gitutil.get_alias_file()
36 if alias_fname:
37 settings.ReadGitAliases(alias_fname)
Simon Glass24725af2020-07-05 21:41:49 -060038
Maxim Cournoyer12f99fd2023-10-12 23:06:24 -040039
Simon Glassc0257982025-04-29 07:22:11 -060040def do_send(args):
Simon Glass24725af2020-07-05 21:41:49 -060041 """Create, check and send patches by email
42
43 Args:
Simon Glasseb101ac2020-07-05 21:41:53 -060044 args (argparse.Namespace): Arguments to patman
Simon Glass24725af2020-07-05 21:41:49 -060045 """
46 setup()
Simon Glassc0257982025-04-29 07:22:11 -060047 send.send(args)
Simon Glass2eb4da72020-07-05 21:41:51 -060048
Simon Glass3db916d2020-10-29 21:46:35 -060049
Simon Glass2112d072020-10-29 21:46:38 -060050def patchwork_status(branch, count, start, end, dest_branch, force,
Simon Glass802eeea2025-04-29 07:22:25 -060051 show_comments, url, single_thread=False):
Simon Glass3db916d2020-10-29 21:46:35 -060052 """Check the status of patches in patchwork
53
54 This finds the series in patchwork using the Series-link tag, checks for new
Simon Glass2112d072020-10-29 21:46:38 -060055 comments and review tags, displays then and creates a new branch with the
56 review tags.
Simon Glass3db916d2020-10-29 21:46:35 -060057
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 Glassd0a0a582020-10-29 21:46:36 -060065 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 Glass2112d072020-10-29 21:46:38 -060068 show_comments (bool): True to display snippets from the comments
69 provided by reviewers
Simon Glass4acc93c2020-11-03 13:54:16 -070070 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 Glass3db916d2020-10-29 21:46:35 -060072
73 Raises:
74 ValueError: if the branch has no Series-link value
75 """
Simon Glass6dfb4dd2025-05-10 13:05:11 +020076 if not branch:
77 branch = gitutil.get_branch()
Simon Glass3db916d2020-10-29 21:46:35 -060078 if count == -1:
79 # Work out how many patches to send if we can
Simon Glass6dfb4dd2025-05-10 13:05:11 +020080 count = gitutil.count_commits_to_branch(branch) - start
Simon Glass3db916d2020-10-29 21:46:35 -060081
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 Glass6dfb4dd2025-05-10 13:05:11 +020097 _, 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 Glass3db916d2020-10-29 21:46:35 -0600102
Simon Glass4acc93c2020-11-03 13:54:16 -0700103 # Allow the series to override the URL
104 if 'patchwork_url' in series:
105 url = series.patchwork_url
Simon Glass6dfb4dd2025-05-10 13:05:11 +0200106 pwork = Patchwork(url, single_thread=single_thread)
Simon Glass4acc93c2020-11-03 13:54:16 -0700107
Simon Glass3db916d2020-10-29 21:46:35 -0600108 # Import this here to avoid failing on other commands if the dependencies
109 # are not present
110 from patman import status
Simon Glass6dfb4dd2025-05-10 13:05:11 +0200111 pwork = Patchwork(url)
112 status.check_and_show_status(series, link, branch, dest_branch, force,
113 show_comments, False, pwork)
Simon Glass3c0196f2025-04-29 07:21:58 -0600114
115
Simon Glassbe7e6c02025-05-10 13:05:12 +0200116def patchwork(args, test_db=None, pwork=None):
117 """Process a 'patchwork' subcommand
118 Args:
119 args (Namespace): Arguments to process
120 test_db (str or None): Directory containing the test database, None to
121 use the normal one
122 pwork (Patchwork): Patchwork object to use
123 """
124 cser = cseries.Cseries(test_db)
125 try:
126 cser.open_database()
127 if args.subcmd == 'set-project':
128 if not pwork:
129 pwork = Patchwork(args.patchwork_url)
130 cser.project_set(pwork, args.project_name)
131 elif args.subcmd == 'get-project':
132 info = cser.project_get()
133 if not info:
134 raise ValueError("Project has not been set; use 'patman patchwork set-project'")
135 name, pwid, link_name = info
136 print(f"Project '{name}' patchwork-ID {pwid} link-name {link_name}")
137 else:
138 raise ValueError(f"Unknown patchwork subcommand '{args.subcmd}'")
139 finally:
140 cser.close_database()
141
142def do_patman(args, test_db=None, pwork=None, cser=None):
Simon Glassb8a14f92025-05-08 06:30:14 +0200143 """Process a patman command
144
145 Args:
146 args (Namespace): Arguments to process
Simon Glassbe7e6c02025-05-10 13:05:12 +0200147 test_db (str or None): Directory containing the test database, None to
148 use the normal one
149 pwork (Patchwork): Patchwork object to use, or None to create one
150 cser (Cseries): Cseries object to use when executing the command,
151 or None to create one
Simon Glassb8a14f92025-05-08 06:30:14 +0200152 """
153 if args.full_help:
154 with resources.path('patman', 'README.rst') as readme:
155 tools.print_full_help(str(readme))
156 return 0
Simon Glass3c0196f2025-04-29 07:21:58 -0600157 if args.cmd == 'send':
158 # Called from git with a patch filename as argument
159 # Printout a list of additional CC recipients for this patch
160 if args.cc_cmd:
161 re_line = re.compile(r'(\S*) (.*)')
162 with open(args.cc_cmd, 'r', encoding='utf-8') as inf:
163 for line in inf.readlines():
164 match = re_line.match(line)
165 if match and match.group(1) == args.patchfiles[0]:
166 for cca in match.group(2).split('\0'):
167 cca = cca.strip()
168 if cca:
169 print(cca)
Simon Glass3c0196f2025-04-29 07:21:58 -0600170 else:
171 # If we are not processing tags, no need to warning about bad ones
172 if not args.process_tags:
173 args.ignore_bad_tags = True
Simon Glassc0257982025-04-29 07:22:11 -0600174 do_send(args)
Simon Glassb8a14f92025-05-08 06:30:14 +0200175 return 0
Simon Glass3c0196f2025-04-29 07:21:58 -0600176
Simon Glass78ee8f82025-04-29 07:22:09 -0600177 ret_code = 0
178 try:
179 # Check status of patches in patchwork
180 if args.cmd == 'status':
Simon Glass3c0196f2025-04-29 07:21:58 -0600181 patchwork_status(args.branch, args.count, args.start, args.end,
182 args.dest_branch, args.force, args.show_comments,
183 args.patchwork_url)
Simon Glassbe7e6c02025-05-10 13:05:12 +0200184 elif args.cmd == 'patchwork':
185 patchwork(args, test_db, pwork)
Simon Glass78ee8f82025-04-29 07:22:09 -0600186 except Exception as exc:
187 terminal.tprint(f'patman: {type(exc).__name__}: {exc}',
188 colour=terminal.Color.RED)
189 if args.debug:
190 print()
191 traceback.print_exc()
192 ret_code = 1
193 return ret_code