VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/FirmwareNew/BaseTools/Scripts/SetupGit.py@ 85718

Last change on this file since 85718 was 85718, checked in by vboxsync, 5 years ago

Devices/EFI: Merge edk-stable202005 and make it build, bugref:4643

  • Property svn:eol-style set to native
File size: 8.7 KB
Line 
1## @file
2# Set up the git configuration for contributing to TianoCore projects
3#
4# Copyright (c) 2019, Linaro Ltd. All rights reserved.<BR>
5# Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
6#
7# SPDX-License-Identifier: BSD-2-Clause-Patent
8#
9
10from __future__ import print_function
11import argparse
12import os.path
13import re
14import sys
15
16try:
17 import git
18except ImportError:
19 print('Unable to load gitpython module - please install and try again.')
20 sys.exit(1)
21
22try:
23 # Try Python 2 'ConfigParser' module first since helpful lib2to3 will
24 # otherwise automagically load it with the name 'configparser'
25 import ConfigParser
26except ImportError:
27 # Otherwise, try loading the Python 3 'configparser' under an alias
28 try:
29 import configparser as ConfigParser
30 except ImportError:
31 print("Unable to load configparser/ConfigParser module - please install and try again!")
32 sys.exit(1)
33
34
35# Assumptions: Script is in edk2/BaseTools/Scripts,
36# templates in edk2/BaseTools/Conf
37CONFDIR = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))),
38 'Conf')
39
40UPSTREAMS = [
41 {'name': 'edk2',
42 'repo': 'https://github.com/tianocore/edk2.git',
43 'list': '[email protected]'},
44 {'name': 'edk2-platforms',
45 'repo': 'https://github.com/tianocore/edk2-platforms.git',
46 'list': '[email protected]', 'prefix': 'edk2-platforms'},
47 {'name': 'edk2-non-osi',
48 'repo': 'https://github.com/tianocore/edk2-non-osi.git',
49 'list': '[email protected]', 'prefix': 'edk2-non-osi'}
50 ]
51
52# The minimum version required for all of the below options to work
53MIN_GIT_VERSION = (1, 9, 0)
54
55# Set of options to be set identically for all repositories
56OPTIONS = [
57 {'section': 'am', 'option': 'keepcr', 'value': True},
58 {'section': 'am', 'option': 'signoff', 'value': True},
59 {'section': 'cherry-pick', 'option': 'signoff', 'value': True},
60 {'section': 'color', 'option': 'diff', 'value': True},
61 {'section': 'color', 'option': 'grep', 'value': 'auto'},
62 {'section': 'commit', 'option': 'signoff', 'value': True},
63 {'section': 'core', 'option': 'abbrev', 'value': 12},
64 {'section': 'core', 'option': 'attributesFile',
65 'value': os.path.join(CONFDIR, 'gitattributes').replace('\\', '/')},
66 {'section': 'core', 'option': 'whitespace', 'value': 'cr-at-eol'},
67 {'section': 'diff', 'option': 'algorithm', 'value': 'patience'},
68 {'section': 'diff', 'option': 'orderFile',
69 'value': os.path.join(CONFDIR, 'diff.order').replace('\\', '/')},
70 {'section': 'diff', 'option': 'renames', 'value': 'copies'},
71 {'section': 'diff', 'option': 'statGraphWidth', 'value': '20'},
72 {'section': 'diff "ini"', 'option': 'xfuncname',
73 'value': '^\\\\[[A-Za-z0-9_., ]+]'},
74 {'section': 'format', 'option': 'coverLetter', 'value': True},
75 {'section': 'format', 'option': 'numbered', 'value': True},
76 {'section': 'format', 'option': 'signoff', 'value': False},
77 {'section': 'log', 'option': 'mailmap', 'value': True},
78 {'section': 'notes', 'option': 'rewriteRef', 'value': 'refs/notes/commits'},
79 {'section': 'sendemail', 'option': 'chainreplyto', 'value': False},
80 {'section': 'sendemail', 'option': 'thread', 'value': True},
81 {'section': 'sendemail', 'option': 'transferEncoding', 'value': '8bit'},
82 ]
83
84
85def locate_repo():
86 """Opens a Repo object for the current tree, searching upwards in the directory hierarchy."""
87 try:
88 repo = git.Repo(path='.', search_parent_directories=True)
89 except (git.InvalidGitRepositoryError, git.NoSuchPathError):
90 print("It doesn't look like we're inside a git repository - aborting.")
91 sys.exit(2)
92 return repo
93
94
95def fuzzy_match_repo_url(one, other):
96 """Compares two repository URLs, ignoring protocol and optional trailing '.git'."""
97 oneresult = re.match(r'.*://(?P<oneresult>.*?)(\.git)*$', one)
98 otherresult = re.match(r'.*://(?P<otherresult>.*?)(\.git)*$', other)
99
100 if oneresult and otherresult:
101 onestring = oneresult.group('oneresult')
102 otherstring = otherresult.group('otherresult')
103 if onestring == otherstring:
104 return True
105
106 return False
107
108
109def get_upstream(url, name):
110 """Extracts the dict for the current repo origin."""
111 for upstream in UPSTREAMS:
112 if (fuzzy_match_repo_url(upstream['repo'], url) or
113 upstream['name'] == name):
114 return upstream
115 print("Unknown upstream '%s' - aborting!" % url)
116 sys.exit(3)
117
118
119def check_versions():
120 """Checks versions of dependencies."""
121 version = git.cmd.Git().version_info
122
123 if version < MIN_GIT_VERSION:
124 print('Need git version %d.%d or later!' % (version[0], version[1]))
125 sys.exit(4)
126
127
128def write_config_value(repo, section, option, data):
129 """."""
130 with repo.config_writer(config_level='repository') as configwriter:
131 configwriter.set_value(section, option, data)
132
133
134if __name__ == '__main__':
135 check_versions()
136
137 PARSER = argparse.ArgumentParser(
138 description='Sets up a git repository according to TianoCore rules.')
139 PARSER.add_argument('-c', '--check',
140 help='check current config only, printing what would be changed',
141 action='store_true',
142 required=False)
143 PARSER.add_argument('-f', '--force',
144 help='overwrite existing settings conflicting with program defaults',
145 action='store_true',
146 required=False)
147 PARSER.add_argument('-n', '--name', type=str, metavar='repo',
148 choices=['edk2', 'edk2-platforms', 'edk2-non-osi'],
149 help='set the repo name to configure for, if not '
150 'detected automatically',
151 required=False)
152 PARSER.add_argument('-v', '--verbose',
153 help='enable more detailed output',
154 action='store_true',
155 required=False)
156 ARGS = PARSER.parse_args()
157
158 REPO = locate_repo()
159 if REPO.bare:
160 print('Bare repo - please check out an upstream one!')
161 sys.exit(6)
162
163 URL = REPO.remotes.origin.url
164
165 UPSTREAM = get_upstream(URL, ARGS.name)
166 if not UPSTREAM:
167 print("Upstream '%s' unknown, aborting!" % URL)
168 sys.exit(7)
169
170 # Set a list email address if our upstream wants it
171 if 'list' in UPSTREAM:
172 OPTIONS.append({'section': 'sendemail', 'option': 'to',
173 'value': UPSTREAM['list']})
174 # Append a subject prefix entry to OPTIONS if our upstream wants it
175 if 'prefix' in UPSTREAM:
176 OPTIONS.append({'section': 'format', 'option': 'subjectPrefix',
177 'value': "PATCH " + UPSTREAM['prefix']})
178
179 CONFIG = REPO.config_reader(config_level='repository')
180
181 for entry in OPTIONS:
182 exists = False
183 try:
184 # Make sure to read boolean/int settings as real type rather than strings
185 if isinstance(entry['value'], bool):
186 value = CONFIG.getboolean(entry['section'], entry['option'])
187 elif isinstance(entry['value'], int):
188 value = CONFIG.getint(entry['section'], entry['option'])
189 else:
190 value = CONFIG.get(entry['section'], entry['option'])
191
192 exists = True
193 # Don't bail out from options not already being set
194 except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
195 pass
196
197 if exists:
198 if value == entry['value']:
199 if ARGS.verbose:
200 print("%s.%s already set (to '%s')" % (entry['section'],
201 entry['option'], value))
202 else:
203 if ARGS.force:
204 write_config_value(REPO, entry['section'], entry['option'], entry['value'])
205 else:
206 print("Not overwriting existing %s.%s value:" % (entry['section'],
207 entry['option']))
208 print(" '%s' != '%s'" % (value, entry['value']))
209 print(" add '-f' to command line to force overwriting existing settings")
210 else:
211 print("%s.%s => '%s'" % (entry['section'], entry['option'], entry['value']))
212 if not ARGS.check:
213 write_config_value(REPO, entry['section'], entry['option'], entry['value'])
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette