Skip to content

Commit

Permalink
Cherry-pick 277213@main (3608a48). rdar://118901053
Browse files Browse the repository at this point in the history
    [webkitscmpy] Optionally include commit message in diff
    https://bugs.webkit.org/show_bug.cgi?id=265481
    rdar://118901053

    Reviewed by Dewei Zhu.

    Programs generating diffs may want to include commit messages
    in patch format.

    * Tools/Scripts/libraries/webkitscmpy/webkitscmpy/local/git.py:
    (Git.diff): Return lines in a diff, use format-patch if caller
    requests commit message be included.
    (Git.diff_lines): Renamed 'diff'.
    * Tools/Scripts/libraries/webkitscmpy/webkitscmpy/local/scm.py:
    (Scm.diff): Added.
    * Tools/Scripts/libraries/webkitscmpy/webkitscmpy/mocks/local/git.py:
    * Tools/Scripts/libraries/webkitscmpy/webkitscmpy/program/land.py:
    (Land.main): Call 'diff' instead of 'diff_lines'
    * Tools/Scripts/libraries/webkitscmpy/webkitscmpy/test/git_unittest.py:

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

Canonical link: https://commits.webkit.org/272448.903@safari-7618-branch
  • Loading branch information
JonWBedard committed Apr 10, 2024
1 parent 907ee0e commit 84e6be5
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 12 deletions.
27 changes: 23 additions & 4 deletions Tools/Scripts/libraries/webkitscmpy/webkitscmpy/local/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -1280,16 +1280,34 @@ def modified(self, staged=None):
return staged
return staged + self.modified(staged=False)

def diff_lines(self, base, head=None):
base = self._to_git_ref(base)
head = self._to_git_ref(head)
def diff(self, head='HEAD', base=None, include_log=False):
head = head if head == 'HEAD' else self._to_git_ref(head)
if not base:
base = head if head == 'HEAD' else self._to_git_ref('{}~1'.format(head))
else:
base = self._to_git_ref(base)

if head == base and head != 'HEAD':
sys.stderr.write("'{}' provided as both head and base\n".format(head))
return

if include_log and head == 'HEAD':
for line in self.diff(head=head, base='HEAD', include_log=False):
yield line

if head == base:
command = [self.executable(), 'diff', '{}'.format(head)]
elif include_log:
command = [self.executable(), 'format-patch', '{}..{}'.format(base, head), '--stdout']
else:
command = [self.executable(), 'diff', '{}..{}'.format(base, head)]

kwargs = dict()
if sys.version_info >= (3, 6):
kwargs = dict(encoding='utf-8')
target = '{}..{}'.format(base, head) if head else base
proc = subprocess.Popen(
[self.executable(), 'diff', target],
command,
cwd=self.root_path,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
Expand All @@ -1298,6 +1316,7 @@ def diff_lines(self, base, head=None):

if proc.poll():
sys.stderr.write("Failed to generate diff for '{}'\n".format(target))
return

line = proc.stdout.readline()
while line:
Expand Down
3 changes: 3 additions & 0 deletions Tools/Scripts/libraries/webkitscmpy/webkitscmpy/local/scm.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,6 @@ def checkout(self, argument):

def pull(self):
raise NotImplementedError()

def diff(self, head='HEAD', base=None, include_log=False):
raise NotImplementedError()
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,32 @@ def __init__(
returncode=0,
stdout='\n'.join([
'--- a/ChangeLog\n+++ b/ChangeLog\n@@ -1,0 +1,0 @@\n{}'.format(
'\n'.join(['+ {}'.format(line) for line in commit.message.splitlines()])
'\n'.join(['+{}'.format(line) for line in commit.message.splitlines()])
) for commit in list(self.rev_list(args[2] if '..' in args[2] else '{}..HEAD'.format(args[2])))
])
)
), mocks.Subprocess.Route(
self.executable, 'format-patch', re.compile(r'.+'),
cwd=self.path,
generator=lambda *args, **kwargs: mocks.ProcessCompletion(
returncode=0,
stdout='\n'.join([
'From {hash}\n'
'From: {author} <{email}>\n'
'Date: {date}\n'
'Subject: [PATCH] {message}\n'
'---\n'
'diff --git a/ChangeLog b/ChangeLog\n'
'--- a/ChangeLog\n'
'+++ b/ChangeLog\n'
'@@ -1,0 +1,0 @@\n'
'{content}'.format(
hash=commit.hash,
author=commit.author.name,
email=commit.author.email,
date=datetime.utcfromtimestamp(commit.timestamp + time.timezone).strftime('%a %b %d %H:%M:%S %Y +0000'),
message=commit.message.rstrip(),
content='\n'.join(['+{}'.format(line) for line in commit.message.splitlines()]),
) for commit in list(self.rev_list(args[2] if '..' in args[2] else '{}..HEAD'.format(args[2])))
])
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def main(cls, args, repository, identifier_template=None, canonical_svn=False, h
return 1

if not args.oops:
for line in repository.diff_lines(branch_point.hash, source_branch):
for line in repository.diff(base=branch_point.hash, head=source_branch):
if cls.OOPS_RE.search(line):
sys.stderr.write("Found '(OOPS!)' in commit diff, please resolve before committing\n")
return 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,20 +491,40 @@ def test_rebase(self):
self.assertEqual(repo.rebase(target='main', base='main', head='branch-a', recommit=False), 0)
self.assertEqual(str(repo.commit(branch='branch-a')), '5.2@branch-a')

def test_diff_lines(self):
def test_diff(self):
with mocks.local.Git(self.path), OutputCapture():
repo = local.Git(self.path)
self.assertEqual(
['--- a/ChangeLog', '+++ b/ChangeLog', '@@ -1,0 +1,0 @@', '+ Patch Series'],
list(repo.diff_lines(base='bae5d1e90999d4f916a8a15810ccfa43f37a2fd6'))
['--- a/ChangeLog', '+++ b/ChangeLog', '@@ -1,0 +1,0 @@', '+Patch Series'],
list(repo.diff(base='bae5d1e90999d4f916a8a15810ccfa43f37a2fd6'))
)

def test_diff_identifier(self):
with mocks.local.Git(self.path):
repo = local.Git(self.path)
self.assertEqual(
['--- a/ChangeLog', '+++ b/ChangeLog', '@@ -1,0 +1,0 @@', '+8th commit'],
list(repo.diff(base='3@main', head='4@main'))
)

def test_diff_lines_identifier(self):
def test_diff_with_commit_message(self):
self.maxDiff = None
with mocks.local.Git(self.path), OutputCapture():
repo = local.Git(self.path)
self.assertEqual(
['--- a/ChangeLog', '+++ b/ChangeLog', '@@ -1,0 +1,0 @@', '+ 8th commit'],
list(repo.diff_lines(base='3@main', head='4@main'))
[
'From bae5d1e90999d4f916a8a15810ccfa43f37a2fd6',
'From: Jonathan Bedard <jbedard@apple.com>',
'Date: {} +0000'.format(datetime.utcfromtimestamp(1601668000 + time.timezone).strftime('%a %b %d %H:%M:%S %Y')),
'Subject: [PATCH] 8th commit',
'---',
'diff --git a/ChangeLog b/ChangeLog',
'--- a/ChangeLog',
'+++ b/ChangeLog',
'@@ -1,0 +1,0 @@',
'+8th commit',
],
list(repo.diff(base='3@main', head='4@main', include_log=True))
)

def test_pull(self):
Expand Down

0 comments on commit 84e6be5

Please sign in to comment.