Skip to content

Commit

Permalink
Cherry-pick 263641@main (2425de1). rdar://107629444
Browse files Browse the repository at this point in the history
    [git-webkit] Cache filed bug
    https://bugs.webkit.org/show_bug.cgi?id=255007
    rdar://107629444

    Reviewed by Elliott Williams.

    git-webkit branch will file a bug. However, that bug is only picked up
    in a commit message through environment variables, which means that
    bugs filed by `git-webkit branch` aren't very useful except when happening
    through `git-webkit pr`'s invocation of `git-webkit branch`. We have a
    similar problem when crafting cherry-picks or reverting changes.

    * Tools/Scripts/hooks/prepare-commit-msg: Pull title, bug and cherry-pick message from
    `git config` if they aren't available in the environment.
    * Tools/Scripts/libraries/webkitscmpy/setup.py: Bump version.
    * Tools/Scripts/libraries/webkitscmpy/webkitscmpy/__init__.py: Ditto.
    * Tools/Scripts/libraries/webkitscmpy/webkitscmpy/mocks/local/git.py: Add ability to get
    and set `git config` values.
    * Tools/Scripts/libraries/webkitscmpy/webkitscmpy/program/branch.py:
    (Branch.main): Save filed bug into `git config` environment.
    * Tools/Scripts/libraries/webkitscmpy/webkitscmpy/program/cherry_pick.py:
    (CherryPick.main): Save cherry-pick pattern and bug details into `git config`, use
    COMMIT_MESSAGE_BUG instead of GIT_WEBKIT_BUG.
    * Tools/Scripts/libraries/webkitscmpy/webkitscmpy/program/command.py:
    (Command.write_branch_variables): Write variable to `git config`'s branch specific namespace.
    * Tools/Scripts/libraries/webkitscmpy/webkitscmpy/program/revert.py:
    (Revert.revert_commit): Save filed bug into `git config` environment.

    Canonical link: https://commits.webkit.org/263641@main

Canonical link: https://commits.webkit.org/259548.731@safari-7615-branch
  • Loading branch information
JonWBedard committed May 4, 2023
1 parent c123784 commit 22f78dd
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 14 deletions.
31 changes: 25 additions & 6 deletions Tools/Scripts/hooks/prepare-commit-msg
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,39 @@ from webkitpy.common.checkout.diff_parser import DiffParser
from webkitbugspy import radar


BRANCH = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD'], **ENCODING_KWARGS).strip()
for name, variable in dict(
title='COMMIT_MESSAGE_TITLE',
bug='COMMIT_MESSAGE_BUG',
cherry_picked='GIT_WEBKIT_CHERRY_PICKED',
).items():
if variable in os.environ:
continue
try:
value = subprocess.check_output(
['git', 'config', '--get-all', 'branch.{}.{}'.format(BRANCH, name)],
**ENCODING_KWARGS
).strip()
if not value:
continue
os.environ[variable] = value
except subprocess.CalledProcessError:
continue


def get_bugs_string():
"""Determines what bug URLs to fill or suggest in a WebKit commit message."""
env = os.environ
need_the_bug_url = 'Need the bug URL (OOPS!).'
need_the_radar = 'Include a Radar link (OOPS!).'
has_radar = any([bool(regex.match(line))
for line in env.get('COMMIT_MESSAGE_BUG', '').splitlines()
for line in os.environ.get('COMMIT_MESSAGE_BUG', '').splitlines()
for regex in radar.Tracker.RES])

if env.get('COMMIT_MESSAGE_BUG'):
if os.environ.get('COMMIT_MESSAGE_BUG'):
if has_radar or not bool(radar.Tracker.radarclient()):
return env['COMMIT_MESSAGE_BUG']
return os.environ['COMMIT_MESSAGE_BUG']
else:
return env['COMMIT_MESSAGE_BUG'] + '\n' + need_the_radar
return os.environ['COMMIT_MESSAGE_BUG'] + '\n' + need_the_radar

bugs_string = need_the_bug_url
if bool(radar.Tracker.radarclient()):
Expand Down Expand Up @@ -133,7 +152,7 @@ def source_branches():

def cherry_pick(content):
cherry_picked = os.environ.get('GIT_WEBKIT_CHERRY_PICKED')
bug = os.environ.get('GIT_WEBKIT_BUG')
bug = os.environ.get('COMMIT_MESSAGE_BUG', '').splitlines()[0]

if PREFER_RADAR and not bug:
RADAR_RES = [
Expand Down
2 changes: 1 addition & 1 deletion Tools/Scripts/libraries/webkitscmpy/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def readme():

setup(
name='webkitscmpy',
version='6.3.1',
version='6.4.0',
description='Library designed to interact with git and svn repositories.',
long_description=readme(),
classifiers=[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def _maybe_add_webkitcorepy_path():
"Please install webkitcorepy with `pip install webkitcorepy --extra-index-url <package index URL>`"
)

version = Version(6, 3, 1)
version = Version(6, 4, 0)

AutoInstall.register(Package('fasteners', Version(0, 15, 0)))
AutoInstall.register(Package('jinja2', Version(2, 11, 3)))
Expand Down
17 changes: 14 additions & 3 deletions Tools/Scripts/libraries/webkitscmpy/webkitscmpy/mocks/local/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,16 @@ def __init__(
returncode=0,
stdout='\n'.join(['{}={}'.format(key, value) for key, value in Git.config().items()])
),
), mocks.Subprocess.Route(
self.executable, 'config', '--add', re.compile(r'.+'), re.compile(r'.+'),
cwd=self.path,
generator=lambda *args, **kwargs:
self.edit_config(args[3], args[4]),
), mocks.Subprocess.Route(
self.executable, 'config', '--unset', re.compile(r'.+'),
cwd=self.path,
generator=lambda *args, **kwargs:
self.edit_config(args[3], value=None),
), mocks.Subprocess.Route(
self.executable, 'config', re.compile(r'.+'), re.compile(r'.+'),
cwd=self.path,
Expand Down Expand Up @@ -971,10 +981,11 @@ def edit_config(self, key, value):
match = self.RE_MULTI_TOP.match(line)
if not match or '{}.{}'.format(match.group('keya'), match.group('keyb')) != key_a:
continue
configfile.write('\t{}={}\n'.format(key_b, value))
if value is not None:
configfile.write('\t{}={}\n'.format(key_b, value))
did_print = True

if not did_print:
if not did_print and value is not None:
configfile.write('[{}]\n'.format(key_a))
configfile.write('\t{}={}\n'.format(key_b, value))

Expand Down Expand Up @@ -1080,7 +1091,7 @@ def cherry_pick(self, hash, env=None):
branch_point=self.head.branch_point or self.head.identifier,
message='Cherry-pick {}. {}\n {}\n'.format(
env.get('GIT_WEBKIT_CHERRY_PICKED', '') or commit.hash,
env.get('GIT_WEBKIT_BUG', '') or '<bug>',
env.get('COMMIT_MESSAGE_BUG', '') or '<bug>',
'\n '.join(commit.message.splitlines()),
),
)
Expand Down
10 changes: 10 additions & 0 deletions Tools/Scripts/libraries/webkitscmpy/webkitscmpy/program/branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@ def main(cls, args, repository, why=None, redact=False, target_remote='fork', **
sys.stderr.write("'{}' is an invalid branch name, cannot create it\n".format(args.issue))
return 1

bug_urls = getattr(args, '_bug_urls', None) or ''
if isinstance(bug_urls, (list, tuple)):
bug_urls = '\n'.join(bug_urls)
title = getattr(args, '_title', None) or ''
cls.write_branch_variables(
repository, args.issue,
title=title,
bug=bug_urls,
)

if args.issue in repository.branches_for(remote=target_remote):
if not args.delete_existing:
sys.stderr.write("'{}' exists on the remote '{}' and will be overwritten by a push\n".format(args.issue, target_remote))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,14 @@ def main(cls, args, repository, **kwargs):

env = os.environ.copy()
env['GIT_WEBKIT_CHERRY_PICKED'] = cherry_pick_string
env['GIT_WEBKIT_BUG'] = issue.link if issue else ''
env['COMMIT_MESSAGE_BUG'] = issue.link if issue else ''

cls.write_branch_variables(
repository, repository.branch,
cherry_pick=cherry_pick_string,
bug=[issue.link] if issue else [],
)

return run(
[repository.executable(), 'cherry-pick', '-e', commit.hash],
cwd=repository.root_path,
Expand Down
19 changes: 17 additions & 2 deletions Tools/Scripts/libraries/webkitscmpy/webkitscmpy/program/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,30 @@
import sys
import time

from webkitcorepy import Terminal
from webkitscmpy.commit import Commit
from webkitcorepy import Terminal, run
from webkitscmpy import local, Commit


class Command(object):
name = None
aliases = []
help = None

@classmethod
def write_branch_variables(cls, repository, branch, **variables):
if not isinstance(repository, local.Git):
return False
result = True
for key, value in variables.items():
if not value:
continue
for v in value if isinstance(value, (list, tuple)) else [value]:
result &= run(
[repository.executable(), 'config', '--add', 'branch.{}.{}'.format(branch, key), str(v)],
cwd=repository.root_path, capture_output=True,
).returncode == 0
return result

@classmethod
def parser(cls, parser, loggers=None):
if cls.name is None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ def revert_commit(cls, args, repository, **kwargs):
env = os.environ
env['COMMIT_MESSAGE_TITLE'] = cls.REVERT_TITLE_TEMPLATE.format(commit, commit_title)
env['COMMIT_MESSAGE_BUG'] = '\n'.join(bug_urls)

cls.write_branch_variables(
repository, repository.branch,
title=cls.REVERT_TITLE_TEMPLATE.format(commit, commit_title),
bug=bug_urls,
)

result = run([repository.executable(), 'commit', '--date=now'], cwd=repository.root_path, env=env)
if result.returncode:
run([repository.executable(), 'revert', '--abort'], cwd=repository.root_path)
Expand Down

0 comments on commit 22f78dd

Please sign in to comment.