Skip to content

Commit 7012a08

Browse files
committed
scripts: new-problem: create a branch
This change modifies the 'new-problem' script to also create a new branch. - stash any uncommitted changes - switch to the master branch - fetch and pull - checkout a new branch using the template 'issue/{issue_number}/{problem_name}' Fixes #288 Signed-off-by: Christopher Friedt <[email protected]>
1 parent 8dd46fb commit 7012a08

File tree

4 files changed

+84
-4
lines changed

4 files changed

+84
-4
lines changed

.github/workflows/ci.yml

+14-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ jobs:
1414

1515
env:
1616
PYTHONPATH: ${{ github.workspace }}/.scripts
17+
# ideally there would be a simple way to append / prepend to the path outside of jobs, but no
18+
PATH: /home/runner/.local/bin:/home/linuxbrew/.linuxbrew/bin:/home/linuxbrew/.linuxbrew/sbin:/opt/pipx_bin:/usr/share/rust/.cargo/bin:/home/runner/.config/composer/vendor/bin:/home/runner/.dotnet/tools:/snap/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
1719

1820
steps:
1921

@@ -22,15 +24,24 @@ jobs:
2224

2325
- name: Setup Python Environment
2426
run: |
25-
sudo apt install -y python3-pytest
27+
pip3 install --upgrade pip
28+
pip3 install -r .scripts/requirements.txt
2629
2730
- name: Test Python
2831
run: |
29-
pytest-3 .scripts/*_test.py
32+
# Both problem.py and problem_test.py assume that the 'master' branch is available
33+
# but GitHub Actions only check out 1 ref (e.g. 'pull/289/merge', a GitHub-managed tag).
34+
# Here we create the master branch if it does not already exist.
35+
if [ "$(git branch --list master)" == "" ]; then
36+
REF="$(git rev-parse HEAD)"
37+
git fetch origin
38+
git checkout -b master origin/master
39+
git checkout ${REF}
40+
fi
41+
pytest .scripts/*_test.py
3042
3143
- name: Setup C++ CEnvironment
3244
run: |
33-
sudo apt-get update
3445
sudo apt install -y cmake clang clang-tidy gcovr
3546
sudo sh .scripts/build-and-install-libgtest-libraries.sh
3647

.scripts/problem.py

+23
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from datetime import datetime
88
import enum
9+
from git import Repo
910
import os
1011
import re
1112
import sys
@@ -84,6 +85,7 @@ def __init__(self, args):
8485

8586
# github attributes
8687
self._issue = args.issue
88+
self._branch = 'issue/{}/{}'.format(args.issue, args.name)
8789

8890
# problem attributes
8991
# self._leetcode_number = -1
@@ -192,6 +194,26 @@ def create_impl_and_test(self):
192194

193195
raise
194196

197+
def checkout_branch(self, create=True):
198+
repo = Repo(os.getcwd())
199+
git = repo.git
200+
201+
try:
202+
# stash any uncommitted changes
203+
git.add('*')
204+
git.stash()
205+
except:
206+
pass
207+
208+
git.checkout('master')
209+
git.fetch('-p')
210+
git.pull()
211+
212+
if create:
213+
git.checkout('-b', self._branch)
214+
else:
215+
git.checkout(self._branch)
216+
195217
def new_problem():
196218
args = Problem.default_args()
197219

@@ -219,6 +241,7 @@ def new_problem():
219241
args.problem_template += line
220242

221243
p = Problem(args)
244+
p.checkout_branch()
222245
p.create_impl_and_test()
223246

224247
return p

.scripts/problem_test.py

+44-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#
55
# SPDX-License-Identifier: MIT
66

7+
import git
8+
from git import Repo
79
import io
810
import os
911
import requests
@@ -15,7 +17,6 @@
1517
from problem import Problem
1618
from problem import Difficulty
1719

18-
1920
def test_init_happy_path():
2021
args = Problem.default_args()
2122
args.name = '3sum'
@@ -86,6 +87,10 @@ def test_init_invalid_name():
8687

8788
def test_create_problem(monkeypatch):
8889

90+
repo = Repo(os.getcwd())
91+
assert(repo.bare == False)
92+
git = repo.git
93+
8994
solution = \
9095
'''class Solution {
9196
public:
@@ -99,12 +104,18 @@ def test_create_problem(monkeypatch):
99104
my_stdin = 'TEST-foo-bar\n42\n3\n' + solution
100105
my_stdout = ''
101106

107+
try:
108+
git.branch('-D', 'issue/42/TEST-foo-bar')
109+
except:
110+
pass
111+
102112
monkeypatch.setattr('sys.stdin', io.StringIO(my_stdin))
103113
p = problem.new_problem()
104114

105115
assert(p._name == 'TEST-foo-bar')
106116
assert(p._camel == 'TESTFooBar')
107117
assert(p._issue == 42)
118+
assert(p._branch == 'issue/42/TEST-foo-bar')
108119
assert(p._difficulty == Difficulty.HARD)
109120
assert(p._problem_template + '\n' == solution)
110121

@@ -119,9 +130,15 @@ def test_create_problem(monkeypatch):
119130
if not os.path.exists(test_cpp):
120131
io_fail = True
121132

133+
git.checkout('master')
134+
git.branch('-D', p._branch)
135+
122136
if io_fail:
123137
try:
124138
os.remove(impl_cpp)
139+
except:
140+
pass
141+
try:
125142
os.remove(test_cpp)
126143
except:
127144
pass
@@ -148,3 +165,29 @@ def test_create_problem(monkeypatch):
148165
assert(p._space == -1)
149166
assert(p._space_rank == -1)
150167
assert(p._space_complexity == '')
168+
169+
170+
def test_checkout_branch():
171+
172+
repo = Repo(os.getcwd())
173+
assert(repo.bare == False)
174+
git = repo.git
175+
176+
git.checkout('master')
177+
assert('master' == '{}'.format(repo.active_branch))
178+
179+
args = Problem.default_args()
180+
args.issue = 123
181+
args.name = 'foo-bar'
182+
p = Problem(args)
183+
184+
p.checkout_branch()
185+
branch_name = repo.active_branch
186+
187+
git.checkout('master')
188+
try:
189+
git.branch('-D', branch_name)
190+
except:
191+
pass
192+
193+
assert('{}'.format(branch_name) == 'issue/{}/{}'.format(args.issue, args.name))

.scripts/requirements.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pytest
2+
gitpython
3+

0 commit comments

Comments
 (0)