Skip to content

Commit 445350e

Browse files
Merge pull request #229 from technote-space/release/v1.0.6
Release/v1.0.6
2 parents 0f92767 + a143255 commit 445350e

File tree

3 files changed

+47
-39
lines changed

3 files changed

+47
-39
lines changed

__tests__/git-helper.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,29 +716,39 @@ describe('GitHelper', () => {
716716
await helper.clone(workDir, context({
717717
ref: 'refs/heads/test',
718718
}));
719+
await helper.switchBranch(workDir, 'abc');
719720
helper.useOrigin(true);
720721
await helper.clone(workDir, context({
721722
ref: 'refs/heads/test',
722723
}));
724+
await helper.switchBranch(workDir, 'abc');
723725
helper.useOrigin('test');
724726
await helper.clone(workDir, context({
725727
ref: 'refs/heads/test',
726728
}));
729+
await helper.switchBranch(workDir, 'abc');
727730
helper.useOrigin(true, false);
728731
await helper.clone(workDir, context({
729732
ref: 'refs/heads/test',
730733
}));
734+
await helper.switchBranch(workDir, 'abc');
731735
helper.useOrigin(false);
732736
await helper.clone(workDir, context({
733737
ref: 'refs/heads/test',
734738
}));
739+
await helper.switchBranch(workDir, 'abc');
735740

736741
execCalledWith(mockExec, [
737742
'git clone \'--branch=test\' \'--depth=3\' \'https://octocat:[email protected]/hello/world.git\' \'.\' > /dev/null 2>&1 || :',
743+
'git checkout -b abc origin/abc || :',
738744
'git clone \'--branch=test\' \'--depth=3\' origin \'.\' > /dev/null 2>&1 || :',
745+
'git checkout -b abc origin/abc || :',
739746
'git clone \'--branch=test\' \'--depth=3\' test \'.\' > /dev/null 2>&1 || :',
747+
'git checkout -b abc test/abc || :',
740748
'git clone \'--branch=test\' \'--depth=3\' origin \'.\' || :',
749+
'git checkout -b abc origin/abc || :',
741750
'git clone \'--branch=test\' \'--depth=3\' \'https://octocat:[email protected]/hello/world.git\' \'.\' > /dev/null 2>&1 || :',
751+
'git checkout -b abc origin/abc || :',
742752
]);
743753
});
744754
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@technote-space/github-action-helper",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "Helper to filter GitHub Action.",
55
"author": {
66
"name": "Technote",

src/git-helper.ts

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,27 @@ export default class GitHelper {
9999
await this.runCommand(workDir, {command: 'git init', args: ['.']});
100100
};
101101

102+
/**
103+
* @param {string|boolean} origin origin
104+
* @param {boolean} quiet quiet?
105+
*/
106+
public useOrigin = (origin: string | boolean, quiet?: boolean): void => {
107+
this.origin = typeof origin === 'boolean' ? (origin ? 'origin' : undefined) : origin;
108+
if (quiet !== undefined) {
109+
this.quietIfNotOrigin = quiet;
110+
}
111+
};
112+
113+
/**
114+
* @return {string} origin name
115+
*/
116+
public getRemoteName = (): string | never => this.origin ?? 'origin';
117+
102118
/**
103119
* @param {Context} context context
104120
* @return {string} origin
105121
*/
106-
private getOrigin = (context: Context): string => this.origin ?? getGitUrlWithToken(context, this.token);
122+
private getRemote = (context: Context): string => this.origin ?? getGitUrlWithToken(context, this.token);
107123

108124
/**
109125
* @param {string} workDir work dir
@@ -114,24 +130,13 @@ export default class GitHelper {
114130
await this.initialize(workDir, false);
115131
await this.runCommand(workDir, {
116132
command: 'git remote add',
117-
args: ['origin', getGitUrlWithToken(context, this.token)],
133+
args: [this.getRemoteName(), getGitUrlWithToken(context, this.token)],
118134
quiet: this.isQuiet(),
119-
altCommand: 'git remote add origin',
135+
altCommand: `git remote add ${this.getRemoteName()}`,
120136
suppressError: true,
121137
});
122138
};
123139

124-
/**
125-
* @param {string|boolean} origin origin
126-
* @param {boolean} quiet quiet?
127-
*/
128-
public useOrigin = (origin: string | boolean, quiet?: boolean): void => {
129-
this.origin = typeof origin === 'boolean' ? (origin ? 'origin' : undefined) : origin;
130-
if (quiet !== undefined) {
131-
this.quietIfNotOrigin = quiet;
132-
}
133-
};
134-
135140
/**
136141
* @return {boolean} is quiet?
137142
*/
@@ -160,10 +165,9 @@ export default class GitHelper {
160165
* @return {Promise<void>} void
161166
*/
162167
public cloneBranch = async(workDir: string, branch: string, context: Context): Promise<void> => {
163-
const url = this.getOrigin(context);
164168
await this.runCommand(workDir, {
165169
command: 'git clone',
166-
args: [`--branch=${branch}`, this.cloneDepth, url, '.'],
170+
args: [`--branch=${branch}`, this.cloneDepth, this.getRemote(context), '.'],
167171
quiet: this.isQuiet(),
168172
altCommand: `git clone --branch=${branch}`,
169173
suppressError: true,
@@ -176,20 +180,19 @@ export default class GitHelper {
176180
* @return {Promise<void>} void
177181
*/
178182
private clonePR = async(workDir: string, context: Context): Promise<void> => {
179-
const url = this.getOrigin(context);
180183
await this.runCommand(workDir, [
181184
{
182185
command: 'git clone',
183-
args: [this.cloneDepth, url, '.'],
186+
args: [this.cloneDepth, this.getRemote(context), '.'],
184187
quiet: this.isQuiet(),
185188
altCommand: 'git clone',
186189
suppressError: true,
187190
},
188191
{
189192
command: 'git fetch',
190-
args: [url, `+${context.ref}`],
193+
args: [this.getRemote(context), `+${context.ref}`],
191194
quiet: this.isQuiet(),
192-
altCommand: `git fetch origin ${context.ref}`,
195+
altCommand: `git fetch ${this.getRemoteName()} ${context.ref}`,
193196
stderrToStdout: true,
194197
},
195198
{
@@ -241,7 +244,7 @@ export default class GitHelper {
241244
command: 'git fetch',
242245
args: [
243246
...(options ?? []),
244-
'origin',
247+
this.getRemoteName(),
245248
...(refspec ?? []),
246249
],
247250
suppressError: true,
@@ -272,13 +275,12 @@ export default class GitHelper {
272275
* @return {Promise<void>} void
273276
*/
274277
public fetchBranch = async(workDir: string, branch: string, context: Context): Promise<void> => {
275-
const url = this.getOrigin(context);
276278
const branchName = getBranch(branch, false);
277279
await this.runCommand(workDir, {
278280
command: 'git fetch',
279-
args: ['--prune', '--no-recurse-submodules', this.cloneDepth, url, `+refs/heads/${branchName}:refs/remotes/origin/${branchName}`],
281+
args: ['--prune', '--no-recurse-submodules', this.cloneDepth, this.getRemote(context), `+refs/heads/${branchName}:refs/remotes/${this.getRemoteName()}/${branchName}`],
280282
quiet: this.isQuiet(),
281-
altCommand: `git fetch --prune --no-recurse-submodules${this.cloneDepth} origin +refs/heads/${branchName}:refs/remotes/origin/${branchName}`,
283+
altCommand: `git fetch --prune --no-recurse-submodules${this.cloneDepth} ${this.getRemoteName()} +refs/heads/${branchName}:refs/remotes/${this.getRemoteName()}/${branchName}`,
282284
suppressError: true,
283285
});
284286
};
@@ -300,7 +302,7 @@ export default class GitHelper {
300302
public switchBranch = async(workDir: string, branch: string): Promise<void> => {
301303
await this.runCommand(workDir, {
302304
command: 'git checkout',
303-
args: ['-b', branch, `origin/${branch}`],
305+
args: ['-b', branch, `${this.getRemoteName()}/${branch}`],
304306
suppressError: true,
305307
stderrToStdout: true,
306308
});
@@ -346,7 +348,7 @@ export default class GitHelper {
346348
public getRefDiff = async(workDir: string, baseRef: string, compareRef: string, diffFilter?: string, dot?: '..' | '...'): Promise<string[]> => {
347349
const toDiffRef = (ref: string): string =>
348350
'HEAD' === ref ? 'HEAD' : (
349-
isPrRef(ref) ? ref.replace(/^refs\//, '') : `origin/${getBranch(ref, false)}`
351+
isPrRef(ref) ? ref.replace(/^refs\//, '') : `${this.getRemoteName()}/${getBranch(ref, false)}`
350352
);
351353
return (await this.runCommand(workDir, {
352354
command: 'git diff',
@@ -416,7 +418,6 @@ export default class GitHelper {
416418
* @see https://qiita.com/ngyuki/items/ca7bed067d7e538fd0cd
417419
*/
418420
public fetchTags = async(workDir: string, context: Context, splitSize = 20): Promise<void> => { // eslint-disable-line no-magic-numbers
419-
const url = this.getOrigin(context);
420421
await this.runCommand(workDir, [
421422
...arrayChunk(await this.getTags(workDir), splitSize).map(tags => ({
422423
command: 'git tag',
@@ -426,9 +427,9 @@ export default class GitHelper {
426427
})),
427428
{
428429
command: 'git fetch',
429-
args: [url, '--tags'],
430+
args: [this.getRemote(context), '--tags'],
430431
quiet: this.isQuiet(),
431-
altCommand: 'git fetch origin --tags',
432+
altCommand: `git fetch ${this.getRemoteName()} --tags`,
432433
},
433434
]);
434435
};
@@ -441,16 +442,15 @@ export default class GitHelper {
441442
* @return {Promise<void>} void
442443
*/
443444
public deleteTag = async(workDir: string, tags: string | string[], context: Context, splitSize = 20): Promise<void> => { // eslint-disable-line no-magic-numbers
444-
const url = this.getOrigin(context);
445445
const regexp = /^(refs\/)?tags\//;
446446
const getTagRef = (tag: string): string => regexp.test(tag) ? tag : `tags/${tag}`;
447447
const getTag = (tag: string): string => tag.replace(regexp, '');
448448
await this.runCommand(workDir, [
449449
...arrayChunk((typeof tags === 'string' ? [tags] : tags).map(getTagRef), splitSize).map(tags => ({
450450
command: 'git push',
451-
args: [url, '--delete', ...tags],
451+
args: [this.getRemote(context), '--delete', ...tags],
452452
quiet: this.isQuiet(),
453-
altCommand: `git push origin --delete ${tags.join(' ')}`,
453+
altCommand: `git push ${this.getRemoteName()} --delete ${tags.join(' ')}`,
454454
suppressError: true,
455455
})),
456456
...arrayChunk((typeof tags === 'string' ? [tags] : tags).map(getTag), splitSize).map(tags => ({
@@ -470,7 +470,6 @@ export default class GitHelper {
470470
* @return {Promise<void>} void
471471
*/
472472
public copyTag = async(workDir: string, newTag: string, fromTag: string, context: Context): Promise<void> => {
473-
const url = this.getOrigin(context);
474473
await this.deleteTag(workDir, newTag, context);
475474
await this.runCommand(workDir, [
476475
{
@@ -479,9 +478,9 @@ export default class GitHelper {
479478
},
480479
{
481480
command: 'git push',
482-
args: [url, `refs/tags/${newTag}`],
481+
args: [this.getRemote(context), `refs/tags/${newTag}`],
483482
quiet: this.isQuiet(),
484-
altCommand: `git push origin refs/tags/${newTag}`,
483+
altCommand: `git push ${this.getRemoteName()} refs/tags/${newTag}`,
485484
},
486485
]);
487486
};
@@ -509,7 +508,6 @@ export default class GitHelper {
509508
* @return {Promise<void>} void
510509
*/
511510
public push = async(workDir: string, branch: string, context: Context, options?: { withTag?: boolean; force?: boolean; args?: Array<string> }): Promise<void> => {
512-
const url = this.getOrigin(context);
513511
const args: Array<string> = [];
514512
if (options?.withTag) {
515513
args.push('--tags');
@@ -522,9 +520,9 @@ export default class GitHelper {
522520
}
523521
await this.runCommand(workDir, {
524522
command: 'git push',
525-
args: args.concat([url, `${branch}:refs/heads/${branch}`]),
523+
args: args.concat([this.getRemote(context), `${branch}:refs/heads/${branch}`]),
526524
quiet: this.isQuiet(),
527-
altCommand: `git push ${args.concat(['origin', `${branch}:refs/heads/${branch}`]).join(' ')}`,
525+
altCommand: `git push ${args.concat([this.getRemoteName(), `${branch}:refs/heads/${branch}`]).join(' ')}`,
528526
suppressError: true,
529527
});
530528
};

0 commit comments

Comments
 (0)