blob: fbf368cb2fa77d7fdbcbd5a5b547b43f1f3bcc20 [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
11import os
Simon Glass3c0196f2025-04-29 07:21:58 -060012import re
Simon Glass24725af2020-07-05 21:41:49 -060013import sys
Simon Glass3c0196f2025-04-29 07:21:58 -060014import traceback
Simon Glass24725af2020-07-05 21:41:49 -060015
Simon Glass3c0196f2025-04-29 07:21:58 -060016try:
17 from importlib import resources
18except ImportError:
19 # for Python 3.6
20 import importlib_resources as resources
21
Simon Glassba1b3b92025-02-09 14:26:00 -070022from u_boot_pylib import gitutil
Simon Glass131444f2023-02-23 18:18:04 -070023from u_boot_pylib import terminal
Simon Glass3c0196f2025-04-29 07:21:58 -060024from u_boot_pylib import tools
25from patman import checkpatch
26from patman import patchstream
Simon Glass25b91c12025-04-29 07:22:19 -060027from patman 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 """
76 if count == -1:
77 # Work out how many patches to send if we can
Simon Glass761648b2022-01-29 14:14:11 -070078 count = (gitutil.count_commits_to_branch(branch) - start)
Simon Glass3db916d2020-10-29 21:46:35 -060079
80 series = patchstream.get_metadata(branch, start, count - end)
81 warnings = 0
82 for cmt in series.commits:
83 if cmt.warn:
84 print('%d warnings for %s:' % (len(cmt.warn), cmt.hash))
85 for warn in cmt.warn:
86 print('\t', warn)
87 warnings += 1
88 print
89 if warnings:
90 raise ValueError('Please fix warnings before running status')
91 links = series.get('links')
92 if not links:
93 raise ValueError("Branch has no Series-links value")
94
95 # Find the link without a version number (we don't support versions yet)
96 found = [link for link in links.split() if not ':' in link]
97 if not found:
98 raise ValueError('Series-links has no current version (without :)')
99
Simon Glass4acc93c2020-11-03 13:54:16 -0700100 # Allow the series to override the URL
101 if 'patchwork_url' in series:
102 url = series.patchwork_url
Simon Glass802eeea2025-04-29 07:22:25 -0600103 pwork = patchwork.Patchwork(url, single_thread=single_thread)
Simon Glass4acc93c2020-11-03 13:54:16 -0700104
Simon Glass3db916d2020-10-29 21:46:35 -0600105 # Import this here to avoid failing on other commands if the dependencies
106 # are not present
107 from patman import status
Simon Glassc100b262025-04-29 07:22:16 -0600108 status.check_and_show_status(series, found[0], branch, dest_branch, force,
Simon Glass25b91c12025-04-29 07:22:19 -0600109 show_comments, pwork)
Simon Glass3c0196f2025-04-29 07:21:58 -0600110
111
112def do_patman(args):
113 if args.cmd == 'send':
114 # Called from git with a patch filename as argument
115 # Printout a list of additional CC recipients for this patch
116 if args.cc_cmd:
117 re_line = re.compile(r'(\S*) (.*)')
118 with open(args.cc_cmd, 'r', encoding='utf-8') as inf:
119 for line in inf.readlines():
120 match = re_line.match(line)
121 if match and match.group(1) == args.patchfiles[0]:
122 for cca in match.group(2).split('\0'):
123 cca = cca.strip()
124 if cca:
125 print(cca)
126
127 elif args.full_help:
128 with resources.path('patman', 'README.rst') as readme:
129 tools.print_full_help(str(readme))
130 else:
131 # If we are not processing tags, no need to warning about bad ones
132 if not args.process_tags:
133 args.ignore_bad_tags = True
Simon Glassc0257982025-04-29 07:22:11 -0600134 do_send(args)
Simon Glass3c0196f2025-04-29 07:21:58 -0600135
Simon Glass78ee8f82025-04-29 07:22:09 -0600136 ret_code = 0
137 try:
138 # Check status of patches in patchwork
139 if args.cmd == 'status':
Simon Glass3c0196f2025-04-29 07:21:58 -0600140 patchwork_status(args.branch, args.count, args.start, args.end,
141 args.dest_branch, args.force, args.show_comments,
142 args.patchwork_url)
Simon Glass78ee8f82025-04-29 07:22:09 -0600143 except Exception as exc:
144 terminal.tprint(f'patman: {type(exc).__name__}: {exc}',
145 colour=terminal.Color.RED)
146 if args.debug:
147 print()
148 traceback.print_exc()
149 ret_code = 1
150 return ret_code