Skip to content

Commit 6b9d5cc

Browse files
chore: tweaks
1 parent 8ad44c3 commit 6b9d5cc

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

__tests__/git-helper.test.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,6 @@ describe('GitHelper', () => {
110110
await helper.checkout(workDir, context());
111111

112112
execCalledWith(mockExec, [
113-
'rm -rdf \'.work\'',
114-
'git init \'.\'',
115113
'git remote add origin \'https://octocat:[email protected]/hello/world.git\' > /dev/null 2>&1 || :',
116114
'git fetch --no-tags origin \'refs/heads/test-ref:refs/remotes/origin/test-ref\' || :',
117115
'git checkout -qf test-sha',
@@ -124,8 +122,6 @@ describe('GitHelper', () => {
124122
await helper.checkout(workDir, context({ref: 'refs/pull/123/merge'}));
125123

126124
execCalledWith(mockExec, [
127-
'rm -rdf \'.work\'',
128-
'git init \'.\'',
129125
'git remote add origin \'https://octocat:[email protected]/hello/world.git\' > /dev/null 2>&1 || :',
130126
'git fetch --no-tags origin \'refs/pull/123/merge:refs/pull/123/merge\' || :',
131127
'git checkout -qf test-sha',
@@ -138,8 +134,6 @@ describe('GitHelper', () => {
138134
await helper.checkout(workDir, context({ref: 'refs/tags/v1.2.3'}));
139135

140136
execCalledWith(mockExec, [
141-
'rm -rdf \'.work\'',
142-
'git init \'.\'',
143137
'git remote add origin \'https://octocat:[email protected]/hello/world.git\' > /dev/null 2>&1 || :',
144138
'git fetch --no-tags origin \'refs/tags/v1.2.3:refs/tags/v1.2.3\' || :',
145139
'git checkout -qf test-sha',
@@ -179,6 +173,17 @@ describe('GitHelper', () => {
179173

180174
await helper.addOrigin(workDir, context());
181175

176+
execCalledWith(mockExec, [
177+
'git remote add origin \'https://octocat:[email protected]/hello/world.git\' > /dev/null 2>&1 || :',
178+
]);
179+
});
180+
181+
it('should run git init and git remote add', async() => {
182+
const mockExec = spyOnExec();
183+
setExists([false, true]);
184+
185+
await helper.addOrigin(workDir, context());
186+
182187
execCalledWith(mockExec, [
183188
`rm -rdf '${workDir}'`,
184189
'git init \'.\'',
@@ -194,8 +199,6 @@ describe('GitHelper', () => {
194199
await helper.fetchOrigin(workDir, context());
195200

196201
execCalledWith(mockExec, [
197-
`rm -rdf '${workDir}'`,
198-
'git init \'.\'',
199202
'git remote add origin \'https://octocat:[email protected]/hello/world.git\' > /dev/null 2>&1 || :',
200203
'git fetch origin || :',
201204
]);
@@ -207,8 +210,6 @@ describe('GitHelper', () => {
207210
await helper.fetchOrigin(workDir, context(), ['--no-tags']);
208211

209212
execCalledWith(mockExec, [
210-
`rm -rdf '${workDir}'`,
211-
'git init \'.\'',
212213
'git remote add origin \'https://octocat:[email protected]/hello/world.git\' > /dev/null 2>&1 || :',
213214
'git fetch --no-tags origin || :',
214215
]);
@@ -220,8 +221,6 @@ describe('GitHelper', () => {
220221
await helper.fetchOrigin(workDir, context(), undefined, ['+refs/pull/*/merge:refs/remotes/pull/*/merge', '+refs/heads/hoge:refs/remotes/origin/hoge']);
221222

222223
execCalledWith(mockExec, [
223-
`rm -rdf '${workDir}'`,
224-
'git init \'.\'',
225224
'git remote add origin \'https://octocat:[email protected]/hello/world.git\' > /dev/null 2>&1 || :',
226225
'git fetch origin \'+refs/pull/*/merge:refs/remotes/pull/*/merge\' \'+refs/heads/hoge:refs/remotes/origin/hoge\' || :',
227226
]);

src/git-helper.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,14 @@ export default class GitHelper {
8484

8585
/**
8686
* @param {string} workDir work dir
87+
* @param {boolean} refresh refresh?
8788
* @return {Promise<void>} void
8889
*/
89-
private initialize = async(workDir: string): Promise<void> => {
90+
private initialize = async(workDir: string, refresh = true): Promise<void> => {
91+
if (isCloned(workDir) && !refresh) {
92+
return;
93+
}
94+
9095
if (fs.existsSync(workDir)) {
9196
await this.runCommand(workDir, {command: 'rm', args: ['-rdf', workDir]});
9297
}
@@ -106,7 +111,7 @@ export default class GitHelper {
106111
* @return {Promise<void>} void
107112
*/
108113
public addOrigin = async(workDir: string, context: Context): Promise<void> => {
109-
await this.initialize(workDir);
114+
await this.initialize(workDir, false);
110115
await this.runCommand(workDir, {
111116
command: 'git remote add',
112117
args: ['origin', getGitUrlWithToken(context, this.token)],

0 commit comments

Comments
 (0)