blob: a7327a20665aca731ef6a339fd82d2078bab497d [file] [log] [blame]
Simon Glass22ce6412023-11-04 10:25:20 -06001# SPDX-License-Identifier: GPL-2.0+
2#
3# Copyright 2023 Google LLC
4#
5
6"""Handles parsing of buildman arguments
7
8This creates the argument parser and uses it to parse the arguments passed in
9"""
10
11import argparse
12import os
13import pathlib
14import sys
15
Simon Glass22ce6412023-11-04 10:25:20 -060016from patman import project
Simon Glassba1b3b92025-02-09 14:26:00 -070017from u_boot_pylib import gitutil
Simon Glass22ce6412023-11-04 10:25:20 -060018from patman import settings
19
20PATMAN_DIR = pathlib.Path(__file__).parent
21HAS_TESTS = os.path.exists(PATMAN_DIR / "func_test.py")
22
23def parse_args():
24 """Parse command line arguments from sys.argv[]
25
26 Returns:
27 tuple containing:
28 options: command line options
29 args: command lin arguments
30 """
31 epilog = '''Create patches from commits in a branch, check them and email
32 them as specified by tags you place in the commits. Use -n to do a dry
33 run first.'''
34
35 parser = argparse.ArgumentParser(epilog=epilog)
36 parser.add_argument('-b', '--branch', type=str,
37 help="Branch to process (by default, the current branch)")
38 parser.add_argument('-c', '--count', dest='count', type=int,
39 default=-1, help='Automatically create patches from top n commits')
40 parser.add_argument('-e', '--end', type=int, default=0,
41 help='Commits to skip at end of patch list')
42 parser.add_argument('-D', '--debug', action='store_true',
43 help='Enabling debugging (provides a full traceback on error)')
Simon Glassed831d12025-04-29 07:22:10 -060044 parser.add_argument(
45 '-N', '--no-capture', action='store_true',
46 help='Disable capturing of console output in tests')
Simon Glass22ce6412023-11-04 10:25:20 -060047 parser.add_argument('-p', '--project', default=project.detect_project(),
48 help="Project name; affects default option values and "
49 "aliases [default: %(default)s]")
50 parser.add_argument('-P', '--patchwork-url',
51 default='https://patchwork.ozlabs.org',
52 help='URL of patchwork server [default: %(default)s]')
53 parser.add_argument('-s', '--start', dest='start', type=int,
54 default=0, help='Commit to start creating patches from (0 = HEAD)')
55 parser.add_argument(
56 '-v', '--verbose', action='store_true', dest='verbose', default=False,
57 help='Verbose output of errors and warnings')
58 parser.add_argument(
Simon Glassed831d12025-04-29 07:22:10 -060059 '-X', '--test-preserve-dirs', action='store_true',
60 help='Preserve and display test-created directories')
61 parser.add_argument(
Simon Glass22ce6412023-11-04 10:25:20 -060062 '-H', '--full-help', action='store_true', dest='full_help',
63 default=False, help='Display the README file')
64
65 subparsers = parser.add_subparsers(dest='cmd')
66 send = subparsers.add_parser(
67 'send', help='Format, check and email patches (default command)')
68 send.add_argument('-i', '--ignore-errors', action='store_true',
69 dest='ignore_errors', default=False,
70 help='Send patches email even if patch errors are found')
71 send.add_argument('-l', '--limit-cc', dest='limit', type=int, default=None,
72 help='Limit the cc list to LIMIT entries [default: %(default)s]')
73 send.add_argument('-m', '--no-maintainers', action='store_false',
74 dest='add_maintainers', default=True,
75 help="Don't cc the file maintainers automatically")
76 send.add_argument(
77 '--get-maintainer-script', dest='get_maintainer_script', type=str,
78 action='store',
79 default=os.path.join(gitutil.get_top_level(), 'scripts',
80 'get_maintainer.pl') + ' --norolestats',
81 help='File name of the get_maintainer.pl (or compatible) script.')
82 send.add_argument('-n', '--dry-run', action='store_true', dest='dry_run',
83 default=False, help="Do a dry run (create but don't email patches)")
84 send.add_argument('-r', '--in-reply-to', type=str, action='store',
85 help="Message ID that this series is in reply to")
86 send.add_argument('-t', '--ignore-bad-tags', action='store_true',
87 default=False,
88 help='Ignore bad tags / aliases (default=warn)')
89 send.add_argument('-T', '--thread', action='store_true', dest='thread',
90 default=False, help='Create patches as a single thread')
91 send.add_argument('--cc-cmd', dest='cc_cmd', type=str, action='store',
92 default=None, help='Output cc list for patch file (used by git)')
93 send.add_argument('--no-binary', action='store_true', dest='ignore_binary',
94 default=False,
95 help="Do not output contents of changes in binary files")
96 send.add_argument('--no-check', action='store_false', dest='check_patch',
97 default=True,
98 help="Don't check for patch compliance")
99 send.add_argument(
100 '--tree', dest='check_patch_use_tree', default=False,
101 action='store_true',
102 help=("Set `tree` to True. If `tree` is False then we'll pass "
103 "'--no-tree' to checkpatch (default: tree=%(default)s)"))
104 send.add_argument('--no-tree', dest='check_patch_use_tree',
105 action='store_false', help="Set `tree` to False")
106 send.add_argument(
107 '--no-tags', action='store_false', dest='process_tags', default=True,
108 help="Don't process subject tags as aliases")
109 send.add_argument('--no-signoff', action='store_false', dest='add_signoff',
110 default=True, help="Don't add Signed-off-by to patches")
111 send.add_argument('--smtp-server', type=str,
112 help="Specify the SMTP server to 'git send-email'")
113 send.add_argument('--keep-change-id', action='store_true',
114 help='Preserve Change-Id tags in patches to send.')
115
116 send.add_argument('patchfiles', nargs='*')
117
118 # Only add the 'test' action if the test data files are available.
119 if HAS_TESTS:
120 test_parser = subparsers.add_parser('test', help='Run tests')
121 test_parser.add_argument('testname', type=str, default=None, nargs='?',
122 help="Specify the test to run")
123
124 status = subparsers.add_parser('status',
125 help='Check status of patches in patchwork')
126 status.add_argument('-C', '--show-comments', action='store_true',
127 help='Show comments from each patch')
128 status.add_argument(
129 '-d', '--dest-branch', type=str,
130 help='Name of branch to create with collected responses')
131 status.add_argument('-f', '--force', action='store_true',
132 help='Force overwriting an existing branch')
133
134 # Parse options twice: first to get the project and second to handle
135 # defaults properly (which depends on project)
136 # Use parse_known_args() in case 'cmd' is omitted
137 argv = sys.argv[1:]
138 args, rest = parser.parse_known_args(argv)
139 if hasattr(args, 'project'):
140 settings.Setup(parser, args.project)
141 args, rest = parser.parse_known_args(argv)
142
143 # If we have a command, it is safe to parse all arguments
144 if args.cmd:
145 args = parser.parse_args(argv)
146 else:
147 # No command, so insert it after the known arguments and before the ones
148 # that presumably relate to the 'send' subcommand
149 nargs = len(rest)
150 argv = argv[:-nargs] + ['send'] + rest
151 args = parser.parse_args(argv)
152
153 return args