Skip to content

Commit 6bc9768

Browse files
committed
scripts: create a commit-solution script
This change adds a script to commit messages based on a template. Fixes #294 Signed-off-by: Christopher Friedt <[email protected]>
1 parent 85280f0 commit 6bc9768

File tree

4 files changed

+254
-33
lines changed

4 files changed

+254
-33
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ jobs:
3838
git checkout -b master origin/master
3939
git checkout ${REF}
4040
fi
41+
# problem.py requires 'git config user.name' and 'git config user.email' to work
42+
git config user.name 'GitHub Actions'
43+
git config user.email '[email protected]'
4144
pytest .scripts/*_test.py
4245
4346
- name: Setup C++ CEnvironment

.scripts/commit-solution

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
5+
import problem
6+
7+
if __name__ == '__main__':
8+
problem.commit_solution()
9+
sys.exit(0)

.scripts/problem.py

Lines changed: 142 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@
1010
import os
1111
import re
1212
import sys
13+
import tempfile
1314
import types
1415

16+
repo = Repo()
17+
assert(repo.bare == False)
18+
git = repo.git
1519

1620
LEETCODE_URL_BASE = 'https://leetcode.com/problems'
1721
GITHUB_URL_BASE = 'https://github.com/cfriedt/leetcode'
1822
SPDX_LICENSE_IDENTIFIER = 'MIT'
19-
COPYRIGHT_HOLDER = 'Christopher Friedt'
23+
GIT_USER_NAME = git.config('user.name')
24+
GIT_USER_EMAIL = git.config('user.email')
2025

2126
NAME_RE = re.compile(r'^[A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9]+$')
2227

@@ -127,7 +132,7 @@ def create_impl_and_test(self):
127132

128133
# write the copyright and license info in SPDX format
129134
f.write('/*\n')
130-
f.write(' * Copyright (c) {}, {}\n'.format(year, COPYRIGHT_HOLDER))
135+
f.write(' * Copyright (c) {}, {}\n'.format(year, GIT_USER_NAME))
131136
f.write(' *\n')
132137
f.write(
133138
' * SPDX-License-Identifier: {}\n'.format(SPDX_LICENSE_IDENTIFIER))
@@ -159,7 +164,7 @@ def create_impl_and_test(self):
159164

160165
# write the copyright and license info in SPDX format
161166
f.write('/*\n')
162-
f.write(' * Copyright (c) {}, {}\n'.format(year, COPYRIGHT_HOLDER))
167+
f.write(' * Copyright (c) {}, {}\n'.format(year, GIT_USER_NAME))
163168
f.write(' *\n')
164169
f.write(
165170
' * SPDX-License-Identifier: {}\n'.format(SPDX_LICENSE_IDENTIFIER))
@@ -195,9 +200,6 @@ def create_impl_and_test(self):
195200
raise
196201

197202
def checkout_branch(self, create=True):
198-
repo = Repo(os.getcwd())
199-
git = repo.git
200-
201203
try:
202204
# stash any uncommitted changes
203205
git.add('*')
@@ -206,14 +208,59 @@ def checkout_branch(self, create=True):
206208
pass
207209

208210
git.checkout('master')
209-
git.fetch('-p')
210-
git.pull()
211+
212+
in_ci = False
213+
try:
214+
in_ci = os.environ['CI']
215+
except:
216+
pass
217+
218+
if not in_ci:
219+
git.fetch()
220+
git.pull()
211221

212222
if create:
213223
git.checkout('-b', self._branch)
214224
else:
215225
git.checkout(self._branch)
216226

227+
def create_commit_message(self):
228+
msg = '''\
229+
Implement {}
230+
231+
name: {}
232+
url: {}
233+
difficulty: {}
234+
235+
time: {} ms
236+
time-rank: {} %
237+
time-complexity: {}
238+
239+
space: {} MB
240+
space-rank: {} %
241+
space-complexity: {}
242+
243+
Fixes #{}
244+
245+
Signed-off-by: {} <{}>
246+
'''.format(self._name, self._name, self._url, self._difficulty, self._time, self._time_rank, self._time_complexity, self._space, self._space_rank, self._space_complexity, self._issue, GIT_USER_NAME, GIT_USER_EMAIL)
247+
return msg
248+
249+
250+
def new_problem_args(name, issue, difficulty, tmpl):
251+
args = Problem.default_args()
252+
args.name = name
253+
args.issue = issue
254+
args.difficulty = difficulty
255+
args.problem_template = tmpl
256+
257+
p = Problem(args)
258+
p.checkout_branch()
259+
p.create_impl_and_test()
260+
261+
return p
262+
263+
217264
def new_problem():
218265
args = Problem.default_args()
219266

@@ -240,8 +287,92 @@ def new_problem():
240287
break
241288
args.problem_template += line
242289

243-
p = Problem(args)
244-
p.checkout_branch()
245-
p.create_impl_and_test()
290+
p = new_problem_args(args.name, args.issue,
291+
args.difficulty, args.problem_template)
246292

247293
return p
294+
295+
296+
def commit_solution_args(issue, name, difficulty, time, time_rank, time_complexity, space, space_rank, space_complexity):
297+
298+
args = Problem.default_args()
299+
args.issue = issue
300+
args.name = name
301+
args.difficulty = difficulty
302+
args.time = time
303+
args.time_rank = time_rank
304+
args.time_complexity = time_complexity
305+
args.space = space
306+
args.space_rank = space_rank
307+
args.space_complexity = space_complexity
308+
309+
p = Problem(args)
310+
311+
msg = p.create_commit_message()
312+
f = tempfile.NamedTemporaryFile(mode='w', delete=False)
313+
try:
314+
f.write(msg)
315+
f.close()
316+
git.commit('-F', f.name)
317+
os.remove(f.name)
318+
except Exception as e:
319+
try:
320+
os.remove(f.name)
321+
except:
322+
pass
323+
raise e
324+
325+
326+
def commit_solution():
327+
args = Problem.default_args()
328+
329+
branch = git.rev_parse('--abbrev-ref', 'HEAD')
330+
331+
if not branch.startswith('issue/'):
332+
raise ValueError('invalid branch name "{}"'.format(branch))
333+
334+
if len(branch.split('/')) != 3:
335+
raise ValueError('invalid branch name "{}"'.format(branch))
336+
337+
args.issue = branch.split('/')[1]
338+
args.name = branch.split('/')[2]
339+
340+
impl_cpp = args.name + '.cpp'
341+
342+
with open(impl_cpp) as f:
343+
for line in f.readlines():
344+
if line.startswith('// difficulty: '):
345+
args.difficulty = line.replace('// difficulty: ', '')
346+
347+
print('Enter the time (ms): ', end='', flush=True)
348+
for line in sys.stdin:
349+
args.time = int(line.rstrip())
350+
break
351+
352+
print('Enter the time-rank (%): ', end='', flush=True)
353+
for line in sys.stdin:
354+
args.time = float(line.rstrip())
355+
break
356+
357+
print('Enter the time-complexity: ', end='', flush=True)
358+
for line in sys.stdin:
359+
args.time_complexity = line.rstrip()
360+
break
361+
362+
print('Enter the space (MB): ', end='', flush=True)
363+
for line in sys.stdin:
364+
args.space = float(line.rstrip())
365+
break
366+
367+
print('Enter the space-rank (%): ', end='', flush=True)
368+
for line in sys.stdin:
369+
args.space_rank = float(line.rstrip())
370+
break
371+
372+
print('Enter the space-complexity: ', end='', flush=True)
373+
for line in sys.stdin:
374+
args.space_complexity = line.rstrip()
375+
break
376+
377+
commit_solution_args(args.issue, args.name, args.difficulty, args.time, args.time_rank,
378+
args.time_complexity, args.space, args.space_rank, args.space_complexity)

0 commit comments

Comments
 (0)