Skip to content

Commit 892e753

Browse files
committed
AddNewlineAction: New action for GitCommitBear
This adds a new action which adds a newline between shortlog and body of commit message when applied. This also make changes in CommitBear.py to pass AddNewlineAction when Result is yielded.
1 parent c21a87e commit 892e753

File tree

5 files changed

+134
-1
lines changed

5 files changed

+134
-1
lines changed

bears/vcs/CommitBear.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from coalib.settings.FunctionMetadata import FunctionMetadata
1515
from dependency_management.requirements.PipRequirement import PipRequirement
1616
from bears.vcs.actions.EditCommitMessageAction import EditCommitMessageAction
17+
from bears.vcs.actions.AddNewlineAction import AddNewlineAction
1718

1819

1920
class _CommitBear(GlobalBear):
@@ -280,7 +281,8 @@ def check_body(self, body,
280281
yield Result(self,
281282
'No newline found between shortlog and body at '
282283
'HEAD commit. Please add one.',
283-
actions=[EditCommitMessageAction()])
284+
actions=[EditCommitMessageAction(),
285+
AddNewlineAction()])
284286
return
285287

286288
if body_regex and not re.fullmatch(body_regex, body.strip()):
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from coalib.misc.Shell import run_shell_command
2+
from coalib.results.result_actions.ResultAction import ResultAction
3+
4+
5+
class AddNewlineAction(ResultAction):
6+
"""
7+
Adds a newline between shortlog and body of the commit message
8+
of the HEAD commit.
9+
"""
10+
11+
SUCCESS_MESSAGE = 'New Line added successfully.'
12+
13+
def is_applicable(self,
14+
result,
15+
original_file_dict,
16+
file_diff_dict,
17+
applied_actions=()):
18+
new_message, _ = run_shell_command('git log -1 --pretty=%B')
19+
new_message = new_message.rstrip('\n')
20+
pos = new_message.find('\n')
21+
self.shortlog = new_message[:pos] if pos != -1 else new_message
22+
self.body = new_message[pos+1:] if pos != -1 else ''
23+
if self.body[0] != '\n':
24+
return True
25+
else:
26+
return False
27+
28+
def apply(self, result, original_file_dict, file_diff_dict):
29+
"""
30+
Add New(L)ine [Note: This may rewrite your commit history]
31+
"""
32+
new_commit_message = '{}\n\n{}'.format(self.shortlog, self.body)
33+
command = 'git commit -o --amend -m "{}"'.format(new_commit_message)
34+
stdout, err = run_shell_command(command)
35+
return file_diff_dict

bears/vcs/actions/__init__.py

Whitespace-only changes.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import unittest
2+
import os
3+
import platform
4+
import shutil
5+
from tempfile import mkdtemp, mkstemp
6+
from unittest.mock import Mock
7+
8+
from coalib.results.Result import Result
9+
from bears.vcs.actions.AddNewlineAction import AddNewlineAction
10+
from coala_utils.ContextManagers import retrieve_stdout
11+
from coalib.misc.Shell import run_shell_command
12+
13+
14+
class AddNewlineActionTest(unittest.TestCase):
15+
16+
@staticmethod
17+
def run_git_command(*args, stdin=None):
18+
return run_shell_command(' '.join(('git',) + args), stdin)
19+
20+
def setUp(self):
21+
self.shortlog = 'file.py: Add something'
22+
self.body = ('Added something, wrote some things\n'
23+
'Wrote tests\n'
24+
'\n'
25+
'Fixes #issue')
26+
self.uut = AddNewlineAction()
27+
self.result = Result('origin', 'message')
28+
29+
# Creating a temporary git repository and
30+
# adding a commit to test
31+
self._old_cwd = os.getcwd()
32+
self.gitdir = mkdtemp()
33+
os.chdir(self.gitdir)
34+
self.gitfile = mkstemp(dir=self.gitdir)
35+
self.run_git_command('init')
36+
self.run_git_command('config', 'user.email [email protected]')
37+
self.run_git_command('config', 'user.name coala')
38+
self.msg = self.shortlog + '\n' + self.body
39+
self.run_git_command('add .')
40+
self.run_git_command('commit',
41+
'--file=-',
42+
stdin=self.msg)
43+
44+
def tearDown(self):
45+
# Deleting the temporary repository
46+
os.chdir(self._old_cwd)
47+
if platform.system() == 'Windows':
48+
onerror = self._windows_rmtree_remove_readonly
49+
else:
50+
onerror = None
51+
shutil.rmtree(self.gitdir, onerror=onerror)
52+
53+
def test_is_applicable_apply(self):
54+
# Applicable because there is no newline between shortlog and body
55+
self.assertTrue(self.uut.is_applicable(self.result, {}, {}))
56+
57+
with retrieve_stdout() as stdout:
58+
self.uut.apply(self.result, {}, {})
59+
new_message, _ = run_shell_command('git log -1 --pretty=%B')
60+
new_message = new_message.rstrip('\n')
61+
self.assertEqual(new_message,
62+
self.shortlog + '\n\n' + self.body)
63+
self.assertEqual(stdout.getvalue(), '')
64+
65+
# Not applicable after action is applied
66+
self.assertFalse(self.uut.is_applicable(self.result, {}, {}))
67+
68+
# Undoing the amend done by applying the action
69+
self.run_git_command('commit',
70+
'--amend',
71+
'--file=-',
72+
stdin=self.msg)
73+
74+
def test_is_applicable_edited_message(self):
75+
# Applicable because there is no newline between shortlog and body
76+
self.assertTrue(self.uut.is_applicable(self.result, {}, {}))
77+
78+
# Mocking EditCommitMessageAction to test cases where user first
79+
# changes commit message by appying EditCommitMessageAction, then
80+
# checking the applicability of AddNewlineAction
81+
EditCommitMessageAction = Mock()
82+
edited_msg1 = ('This is new commit message\n'
83+
'Still no new line')
84+
edited_msg2 = ('This is lastest commit message\n'
85+
'\n'
86+
'Finally a new line!!')
87+
88+
EditCommitMessageAction.apply.side_effect = self.run_git_command(
89+
'commit', '--amend', '--file=-', stdin=edited_msg1)
90+
EditCommitMessageAction.apply()
91+
self.assertTrue(self.uut.is_applicable(self.result, {}, {}))
92+
93+
EditCommitMessageAction.apply.side_effect = self.run_git_command(
94+
'commit', '--amend', '--file=-', stdin=edited_msg2)
95+
EditCommitMessageAction.apply()
96+
self.assertFalse(self.uut.is_applicable(self.result, {}, {}))

tests/vcs/actions/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)