patman: Allow different commands
[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 argparse import ArgumentParser
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 epilog = '''Create patches from commits in a branch, check them and email them
31 as specified by tags you place in the commits. Use -n to do a dry run first.'''
32
33 parser = ArgumentParser(epilog=epilog)
34 subparsers = parser.add_subparsers(dest='cmd')
35 send = subparsers.add_parser('send')
36 send.add_argument('-H', '--full-help', action='store_true', dest='full_help',
37        default=False, help='Display the README file')
38 send.add_argument('-b', '--branch', type=str,
39                   help="Branch to process (by default, the current branch)")
40 send.add_argument('-c', '--count', dest='count', type=int,
41        default=-1, help='Automatically create patches from top n commits')
42 send.add_argument('-e', '--end', type=int, default=0,
43                   help='Commits to skip at end of patch list')
44 send.add_argument('-i', '--ignore-errors', action='store_true',
45        dest='ignore_errors', default=False,
46        help='Send patches email even if patch errors are found')
47 send.add_argument('-l', '--limit-cc', dest='limit', type=int, default=None,
48        help='Limit the cc list to LIMIT entries [default: %(default)s]')
49 send.add_argument('-m', '--no-maintainers', action='store_false',
50        dest='add_maintainers', default=True,
51        help="Don't cc the file maintainers automatically")
52 send.add_argument('-n', '--dry-run', action='store_true', dest='dry_run',
53        default=False, help="Do a dry run (create but don't email patches)")
54 send.add_argument('-p', '--project', default=project.DetectProject(),
55                   help="Project name; affects default option values and "
56                   "aliases [default: %(default)s]")
57 send.add_argument('-r', '--in-reply-to', type=str, action='store',
58                   help="Message ID that this series is in reply to")
59 send.add_argument('-s', '--start', dest='start', type=int,
60        default=0, help='Commit to start creating patches from (0 = HEAD)')
61 send.add_argument('-t', '--ignore-bad-tags', action='store_true',
62                   default=False, help='Ignore bad tags / aliases')
63 send.add_argument('-v', '--verbose', action='store_true', dest='verbose',
64        default=False, help='Verbose output of errors and warnings')
65 send.add_argument('-T', '--thread', action='store_true', dest='thread',
66                   default=False, help='Create patches as a single thread')
67 send.add_argument('--cc-cmd', dest='cc_cmd', type=str, action='store',
68        default=None, help='Output cc list for patch file (used by git)')
69 send.add_argument('--no-binary', action='store_true', dest='ignore_binary',
70                   default=False,
71                   help="Do not output contents of changes in binary files")
72 send.add_argument('--no-check', action='store_false', dest='check_patch',
73                   default=True,
74                   help="Don't check for patch compliance")
75 send.add_argument('--no-tags', action='store_false', dest='process_tags',
76                   default=True, help="Don't process subject tags as aliases")
77 send.add_argument('--smtp-server', type=str,
78                   help="Specify the SMTP server to 'git send-email'")
79 send.add_argument('--test', action='store_true', dest='test',
80                   default=False, help='run tests')
81
82 send.add_argument('patchfiles', nargs='*')
83
84 # Parse options twice: first to get the project and second to handle
85 # defaults properly (which depends on project).
86 argv = sys.argv[1:]
87 if len(argv) < 1 or argv[0].startswith('-'):
88     argv = ['send'] + argv
89 args = parser.parse_args(argv)
90 if hasattr(args, 'project'):
91     settings.Setup(gitutil, send, args.project, '')
92     args = parser.parse_args(argv)
93
94 if __name__ != "__main__":
95     pass
96
97 # Run our meagre tests
98 elif args.test:
99     import doctest
100     from patman import func_test
101
102     sys.argv = [sys.argv[0]]
103     result = unittest.TestResult()
104     for module in (test_checkpatch.TestPatch, func_test.TestFunctional):
105         suite = unittest.TestLoader().loadTestsFromTestCase(module)
106         suite.run(result)
107
108     for module in ['gitutil', 'settings', 'terminal']:
109         suite = doctest.DocTestSuite(module)
110         suite.run(result)
111
112     sys.exit(test_util.ReportResult('patman', None, result))
113
114 # Called from git with a patch filename as argument
115 # Printout a list of additional CC recipients for this patch
116 elif args.cc_cmd:
117     fd = open(args.cc_cmd, 'r')
118     re_line = re.compile('(\S*) (.*)')
119     for line in fd.readlines():
120         match = re_line.match(line)
121         if match and match.group(1) == args.patchfiles[0]:
122             for cc in match.group(2).split('\0'):
123                 cc = cc.strip()
124                 if cc:
125                     print(cc)
126     fd.close()
127
128 elif args.full_help:
129     pager = os.getenv('PAGER')
130     if not pager:
131         pager = 'more'
132     fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
133                          'README')
134     command.Run(pager, fname)
135
136 # Process commits, produce patches files, check them, email them
137 else:
138     control.send(args)