patman: Allow skipping patches at the end
[platform/kernel/u-boot.git] / tools / patman / main.py
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: GPL-2.0+
3 #
4 # Copyright (c) 2011 The Chromium OS Authors.
5 #
6
7 """See README for more information"""
8
9 from optparse import OptionParser
10 import os
11 import re
12 import sys
13 import unittest
14
15 if __name__ == "__main__":
16     # Allow 'from patman import xxx to work'
17     our_path = os.path.dirname(os.path.realpath(__file__))
18     sys.path.append(os.path.join(our_path, '..'))
19
20 # Our modules
21 from patman import command
22 from patman import control
23 from patman import gitutil
24 from patman import project
25 from patman import settings
26 from patman import terminal
27 from patman import test_util
28 from patman import test_checkpatch
29
30
31 parser = OptionParser()
32 parser.add_option('-H', '--full-help', action='store_true', dest='full_help',
33        default=False, help='Display the README file')
34 parser.add_option('-b', '--branch', type='str',
35                   help="Branch to process (by default, the current branch)")
36 parser.add_option('-c', '--count', dest='count', type='int',
37        default=-1, help='Automatically create patches from top n commits')
38 parser.add_option('-e', '--end', type='int', default=0,
39                   help='Commits to skip at end of patch list')
40 parser.add_option('-i', '--ignore-errors', action='store_true',
41        dest='ignore_errors', default=False,
42        help='Send patches email even if patch errors are found')
43 parser.add_option('-l', '--limit-cc', dest='limit', type='int',
44        default=None, help='Limit the cc list to LIMIT entries [default: %default]')
45 parser.add_option('-m', '--no-maintainers', action='store_false',
46        dest='add_maintainers', default=True,
47        help="Don't cc the file maintainers automatically")
48 parser.add_option('-n', '--dry-run', action='store_true', dest='dry_run',
49        default=False, help="Do a dry run (create but don't email patches)")
50 parser.add_option('-p', '--project', default=project.DetectProject(),
51                   help="Project name; affects default option values and "
52                   "aliases [default: %default]")
53 parser.add_option('-r', '--in-reply-to', type='string', action='store',
54                   help="Message ID that this series is in reply to")
55 parser.add_option('-s', '--start', dest='start', type='int',
56        default=0, help='Commit to start creating patches from (0 = HEAD)')
57 parser.add_option('-t', '--ignore-bad-tags', action='store_true',
58                   default=False, help='Ignore bad tags / aliases')
59 parser.add_option('-v', '--verbose', action='store_true', dest='verbose',
60        default=False, help='Verbose output of errors and warnings')
61 parser.add_option('-T', '--thread', action='store_true', dest='thread',
62                   default=False, help='Create patches as a single thread')
63 parser.add_option('--cc-cmd', dest='cc_cmd', type='string', action='store',
64        default=None, help='Output cc list for patch file (used by git)')
65 parser.add_option('--no-binary', action='store_true', dest='ignore_binary',
66                   default=False,
67                   help="Do not output contents of changes in binary files")
68 parser.add_option('--no-check', action='store_false', dest='check_patch',
69                   default=True,
70                   help="Don't check for patch compliance")
71 parser.add_option('--no-tags', action='store_false', dest='process_tags',
72                   default=True, help="Don't process subject tags as aliases")
73 parser.add_option('--smtp-server', type='str',
74                   help="Specify the SMTP server to 'git send-email'")
75 parser.add_option('--test', action='store_true', dest='test',
76                   default=False, help='run tests')
77
78 parser.usage += """
79
80 Create patches from commits in a branch, check them and email them as
81 specified by tags you place in the commits. Use -n to do a dry run first."""
82
83
84 # Parse options twice: first to get the project and second to handle
85 # defaults properly (which depends on project).
86 (options, args) = parser.parse_args()
87 settings.Setup(gitutil, parser, options.project, '')
88 (options, args) = parser.parse_args()
89
90 if __name__ != "__main__":
91     pass
92
93 # Run our meagre tests
94 elif options.test:
95     import doctest
96     from patman import func_test
97
98     sys.argv = [sys.argv[0]]
99     result = unittest.TestResult()
100     for module in (test_checkpatch.TestPatch, func_test.TestFunctional):
101         suite = unittest.TestLoader().loadTestsFromTestCase(module)
102         suite.run(result)
103
104     for module in ['gitutil', 'settings', 'terminal']:
105         suite = doctest.DocTestSuite(module)
106         suite.run(result)
107
108     sys.exit(test_util.ReportResult('patman', None, result))
109
110 # Called from git with a patch filename as argument
111 # Printout a list of additional CC recipients for this patch
112 elif options.cc_cmd:
113     fd = open(options.cc_cmd, 'r')
114     re_line = re.compile('(\S*) (.*)')
115     for line in fd.readlines():
116         match = re_line.match(line)
117         if match and match.group(1) == args[0]:
118             for cc in match.group(2).split('\0'):
119                 cc = cc.strip()
120                 if cc:
121                     print(cc)
122     fd.close()
123
124 elif options.full_help:
125     pager = os.getenv('PAGER')
126     if not pager:
127         pager = 'more'
128     fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
129                          'README')
130     command.Run(pager, fname)
131
132 # Process commits, produce patches files, check them, email them
133 else:
134     control.send(options)