blob: 06a9dfd2bca0cc84c30bed8970d98e3b14321f8c [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 Glassc0257982025-04-29 07:22:11 -060027from patman import send
Simon Glass24725af2020-07-05 21:41:49 -060028
Maxim Cournoyer12f99fd2023-10-12 23:06:24 -040029
Simon Glass24725af2020-07-05 21:41:49 -060030def setup():
31 """Do required setup before doing anything"""
Simon Glass761648b2022-01-29 14:14:11 -070032 gitutil.setup()
Simon Glass24725af2020-07-05 21:41:49 -060033
Maxim Cournoyer12f99fd2023-10-12 23:06:24 -040034
Simon Glassc0257982025-04-29 07:22:11 -060035def do_send(args):
Simon Glass24725af2020-07-05 21:41:49 -060036 """Create, check and send patches by email
37
38 Args:
Simon Glasseb101ac2020-07-05 21:41:53 -060039 args (argparse.Namespace): Arguments to patman
Simon Glass24725af2020-07-05 21:41:49 -060040 """
41 setup()
Simon Glassc0257982025-04-29 07:22:11 -060042 send.send(args)
Simon Glass2eb4da72020-07-05 21:41:51 -060043
Simon Glass3db916d2020-10-29 21:46:35 -060044
Simon Glass2112d072020-10-29 21:46:38 -060045def patchwork_status(branch, count, start, end, dest_branch, force,
Simon Glassf9b03cf2020-11-03 13:54:14 -070046 show_comments, url):
Simon Glass3db916d2020-10-29 21:46:35 -060047 """Check the status of patches in patchwork
48
49 This finds the series in patchwork using the Series-link tag, checks for new
Simon Glass2112d072020-10-29 21:46:38 -060050 comments and review tags, displays then and creates a new branch with the
51 review tags.
Simon Glass3db916d2020-10-29 21:46:35 -060052
53 Args:
54 branch (str): Branch to create patches from (None = current)
55 count (int): Number of patches to produce, or -1 to produce patches for
56 the current branch back to the upstream commit
57 start (int): Start partch to use (0=first / top of branch)
58 end (int): End patch to use (0=last one in series, 1=one before that,
59 etc.)
Simon Glassd0a0a582020-10-29 21:46:36 -060060 dest_branch (str): Name of new branch to create with the updated tags
61 (None to not create a branch)
62 force (bool): With dest_branch, force overwriting an existing branch
Simon Glass2112d072020-10-29 21:46:38 -060063 show_comments (bool): True to display snippets from the comments
64 provided by reviewers
Simon Glass4acc93c2020-11-03 13:54:16 -070065 url (str): URL of patchwork server, e.g. 'https://patchwork.ozlabs.org'.
66 This is ignored if the series provides a Series-patchwork-url tag.
Simon Glass3db916d2020-10-29 21:46:35 -060067
68 Raises:
69 ValueError: if the branch has no Series-link value
70 """
71 if count == -1:
72 # Work out how many patches to send if we can
Simon Glass761648b2022-01-29 14:14:11 -070073 count = (gitutil.count_commits_to_branch(branch) - start)
Simon Glass3db916d2020-10-29 21:46:35 -060074
75 series = patchstream.get_metadata(branch, start, count - end)
76 warnings = 0
77 for cmt in series.commits:
78 if cmt.warn:
79 print('%d warnings for %s:' % (len(cmt.warn), cmt.hash))
80 for warn in cmt.warn:
81 print('\t', warn)
82 warnings += 1
83 print
84 if warnings:
85 raise ValueError('Please fix warnings before running status')
86 links = series.get('links')
87 if not links:
88 raise ValueError("Branch has no Series-links value")
89
90 # Find the link without a version number (we don't support versions yet)
91 found = [link for link in links.split() if not ':' in link]
92 if not found:
93 raise ValueError('Series-links has no current version (without :)')
94
Simon Glass4acc93c2020-11-03 13:54:16 -070095 # Allow the series to override the URL
96 if 'patchwork_url' in series:
97 url = series.patchwork_url
98
Simon Glass3db916d2020-10-29 21:46:35 -060099 # Import this here to avoid failing on other commands if the dependencies
100 # are not present
101 from patman import status
Simon Glassc100b262025-04-29 07:22:16 -0600102 status.check_and_show_status(series, found[0], branch, dest_branch, force,
103 show_comments, url)
Simon Glass3c0196f2025-04-29 07:21:58 -0600104
105
106def do_patman(args):
107 if args.cmd == 'send':
108 # Called from git with a patch filename as argument
109 # Printout a list of additional CC recipients for this patch
110 if args.cc_cmd:
111 re_line = re.compile(r'(\S*) (.*)')
112 with open(args.cc_cmd, 'r', encoding='utf-8') as inf:
113 for line in inf.readlines():
114 match = re_line.match(line)
115 if match and match.group(1) == args.patchfiles[0]:
116 for cca in match.group(2).split('\0'):
117 cca = cca.strip()
118 if cca:
119 print(cca)
120
121 elif args.full_help:
122 with resources.path('patman', 'README.rst') as readme:
123 tools.print_full_help(str(readme))
124 else:
125 # If we are not processing tags, no need to warning about bad ones
126 if not args.process_tags:
127 args.ignore_bad_tags = True
Simon Glassc0257982025-04-29 07:22:11 -0600128 do_send(args)
Simon Glass3c0196f2025-04-29 07:21:58 -0600129
Simon Glass78ee8f82025-04-29 07:22:09 -0600130 ret_code = 0
131 try:
132 # Check status of patches in patchwork
133 if args.cmd == 'status':
Simon Glass3c0196f2025-04-29 07:21:58 -0600134 patchwork_status(args.branch, args.count, args.start, args.end,
135 args.dest_branch, args.force, args.show_comments,
136 args.patchwork_url)
Simon Glass78ee8f82025-04-29 07:22:09 -0600137 except Exception as exc:
138 terminal.tprint(f'patman: {type(exc).__name__}: {exc}',
139 colour=terminal.Color.RED)
140 if args.debug:
141 print()
142 traceback.print_exc()
143 ret_code = 1
144 return ret_code