Skip to content

Commit 31b9e2f

Browse files
Merge pull request #114 from gitcd-io/stash-changes
Stash changes while create new feature branches
2 parents 7663cb3 + 2b4bc67 commit 31b9e2f

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

.github/tests/git-cd-start.sh

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,33 @@ set -e
33
# change workdir to travis-gitcd
44
cd ~/build/gitcd-io/travis-gitcd
55

6-
git-cd start github-$GITHUB_RUN_NUMBER-$PYTHON_VERSION
6+
# Create an uncommitted change to test stash functionality
7+
echo "Test uncommitted change for stash" >> README.rst
8+
9+
# Start a new feature branch with expect to handle the stash prompt
10+
/usr/bin/expect <<EOD
11+
set timeout 30
12+
spawn git-cd start github-${GITHUB_RUN_NUMBER}-${PYTHON_VERSION}
13+
expect {
14+
"You have uncommitted changes. Do you want me to stash them and re-apply after creating the new branch?" {
15+
send "yes\r"
16+
exp_continue
17+
}
18+
eof
19+
}
20+
EOD
721

822
# assert that new feature branch exists remote
923
git branch -a | grep "origin/feature/github-$GITHUB_RUN_NUMBER-$PYTHON_VERSION"
1024

25+
# assert the uncommitted changes are still there (stash pop was successful)
26+
if ! git status --porcelain | grep -q "README.rst"; then
27+
echo "Expected uncommitted changes to be restored but found none"
28+
exit 1
29+
fi
30+
31+
# clean up: discard the uncommitted changes
32+
git checkout -- README.rst
33+
1134
# change back to original workdir
1235
cd -

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ jobs:
108108

109109
- name: Test git-cd start
110110
run: bash .github/tests/git-cd-start.sh
111-
111+
112112
- name: Add test build
113113
run: bash .github/tests/git-add-build.sh
114-
114+
115115
- name: Test git-cd refresh
116116
run: bash .github/tests/git-cd-refresh.sh
117117

gitcd/git/repository.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ def hasUncommitedChanges(self) -> bool:
158158

159159
return True
160160

161+
def stash(self) -> bool:
162+
self.verboseCli.execute("git stash")
163+
return True
164+
165+
def stashPop(self) -> bool:
166+
self.verboseCli.execute("git stash pop")
167+
return True
168+
161169
def update(self) -> bool:
162170
self.cli.execute('git fetch -p')
163171

gitcd/interface/cli/start.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,32 @@ def run(self, branch: Branch):
5757
testBranch = self.config.getTest()
5858
testBranchAsString = self.config.getString(testBranch)
5959

60+
# Check for uncommitted changes and offer to stash
61+
stashed = False
62+
if self.repository.hasUncommitedChanges():
63+
stashChanges = self.interface.askFor(
64+
"You have uncommitted changes." +
65+
" Do you want me to stash them and re-apply after" +
66+
" creating the new branch?",
67+
["yes", "no"],
68+
"yes"
69+
)
70+
71+
if stashChanges == "yes":
72+
self.repository.stash()
73+
stashed = True
74+
else:
75+
abort = self.interface.askFor(
76+
"Do you want me to abort and let you" +
77+
" commit or discard your changes first?",
78+
["yes", "no"],
79+
"yes"
80+
)
81+
82+
if abort == "yes":
83+
import sys
84+
sys.exit(1)
85+
6086
# few checks on the new feature branch
6187
if '%s%s' % (featurePrefixAsString, branch.getName()) == masterBranch:
6288
# maybe i should use while here
@@ -83,3 +109,7 @@ def run(self, branch: Branch):
83109
branch = self.checkDoubleFeaturePrefix(branch)
84110

85111
remote.createFeature(branch)
112+
113+
# Re-apply stashed changes if we stashed them earlier
114+
if stashed:
115+
self.repository.stashPop()

0 commit comments

Comments
 (0)