Skip to content

Commit 7739b9b

Browse files
Add option to fetch tags even if fetch-depth > 0 (#579)
* Add option to fetch tags even if fetch-depth > 0 * Add jest tests for fetchDepth and fetchTags options
1 parent 96f5310 commit 7739b9b

10 files changed

+214
-8
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ When Git 2.18 or higher is not in your PATH, falls back to the REST API to downl
8787
# Default: 1
8888
fetch-depth: ''
8989

90+
# Whether to fetch tags, even if fetch-depth > 0.
91+
# Default: false
92+
fetch-tags: ''
93+
9094
# Whether to download Git-LFS files
9195
# Default: false
9296
lfs: ''

__test__/git-auth-helper.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,7 @@ async function setup(testName: string): Promise<void> {
805805
sparseCheckout: [],
806806
sparseCheckoutConeMode: true,
807807
fetchDepth: 1,
808+
fetchTags: false,
808809
lfs: false,
809810
submodules: false,
810811
nestedSubmodules: false,

__test__/git-command-manager.test.ts

+176
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,179 @@ describe('git-auth-helper tests', () => {
8888
expect(branches.sort()).toEqual(['foo'].sort())
8989
})
9090
})
91+
92+
describe('Test fetchDepth and fetchTags options', () => {
93+
beforeEach(async () => {
94+
jest.spyOn(fshelper, 'fileExistsSync').mockImplementation(jest.fn())
95+
jest.spyOn(fshelper, 'directoryExistsSync').mockImplementation(jest.fn())
96+
mockExec.mockImplementation((path, args, options) => {
97+
console.log(args, options.listeners.stdout)
98+
99+
if (args.includes('version')) {
100+
options.listeners.stdout(Buffer.from('2.18'))
101+
}
102+
103+
return 0
104+
})
105+
})
106+
107+
afterEach(() => {
108+
jest.restoreAllMocks()
109+
})
110+
111+
it('should call execGit with the correct arguments when fetchDepth is 0 and fetchTags is true', async () => {
112+
jest.spyOn(exec, 'exec').mockImplementation(mockExec)
113+
const workingDirectory = 'test'
114+
const lfs = false
115+
const doSparseCheckout = false
116+
git = await commandManager.createCommandManager(
117+
workingDirectory,
118+
lfs,
119+
doSparseCheckout
120+
)
121+
122+
const refSpec = ['refspec1', 'refspec2']
123+
const options = {
124+
filter: 'filterValue',
125+
fetchDepth: 0,
126+
fetchTags: true
127+
}
128+
129+
await git.fetch(refSpec, options)
130+
131+
expect(mockExec).toHaveBeenCalledWith(
132+
expect.any(String),
133+
[
134+
'-c',
135+
'protocol.version=2',
136+
'fetch',
137+
'--prune',
138+
'--progress',
139+
'--no-recurse-submodules',
140+
'--filter=filterValue',
141+
'origin',
142+
'refspec1',
143+
'refspec2'
144+
],
145+
expect.any(Object)
146+
)
147+
})
148+
149+
it('should call execGit with the correct arguments when fetchDepth is 0 and fetchTags is false', async () => {
150+
jest.spyOn(exec, 'exec').mockImplementation(mockExec)
151+
152+
const workingDirectory = 'test'
153+
const lfs = false
154+
const doSparseCheckout = false
155+
git = await commandManager.createCommandManager(
156+
workingDirectory,
157+
lfs,
158+
doSparseCheckout
159+
)
160+
const refSpec = ['refspec1', 'refspec2']
161+
const options = {
162+
filter: 'filterValue',
163+
fetchDepth: 0,
164+
fetchTags: false
165+
}
166+
167+
await git.fetch(refSpec, options)
168+
169+
expect(mockExec).toHaveBeenCalledWith(
170+
expect.any(String),
171+
[
172+
'-c',
173+
'protocol.version=2',
174+
'fetch',
175+
'--no-tags',
176+
'--prune',
177+
'--progress',
178+
'--no-recurse-submodules',
179+
'--filter=filterValue',
180+
'origin',
181+
'refspec1',
182+
'refspec2'
183+
],
184+
expect.any(Object)
185+
)
186+
})
187+
188+
it('should call execGit with the correct arguments when fetchDepth is 1 and fetchTags is false', async () => {
189+
jest.spyOn(exec, 'exec').mockImplementation(mockExec)
190+
191+
const workingDirectory = 'test'
192+
const lfs = false
193+
const doSparseCheckout = false
194+
git = await commandManager.createCommandManager(
195+
workingDirectory,
196+
lfs,
197+
doSparseCheckout
198+
)
199+
const refSpec = ['refspec1', 'refspec2']
200+
const options = {
201+
filter: 'filterValue',
202+
fetchDepth: 1,
203+
fetchTags: false
204+
}
205+
206+
await git.fetch(refSpec, options)
207+
208+
expect(mockExec).toHaveBeenCalledWith(
209+
expect.any(String),
210+
[
211+
'-c',
212+
'protocol.version=2',
213+
'fetch',
214+
'--no-tags',
215+
'--prune',
216+
'--progress',
217+
'--no-recurse-submodules',
218+
'--filter=filterValue',
219+
'--depth=1',
220+
'origin',
221+
'refspec1',
222+
'refspec2'
223+
],
224+
expect.any(Object)
225+
)
226+
})
227+
228+
it('should call execGit with the correct arguments when fetchDepth is 1 and fetchTags is true', async () => {
229+
jest.spyOn(exec, 'exec').mockImplementation(mockExec)
230+
231+
const workingDirectory = 'test'
232+
const lfs = false
233+
const doSparseCheckout = false
234+
git = await commandManager.createCommandManager(
235+
workingDirectory,
236+
lfs,
237+
doSparseCheckout
238+
)
239+
const refSpec = ['refspec1', 'refspec2']
240+
const options = {
241+
filter: 'filterValue',
242+
fetchDepth: 1,
243+
fetchTags: true
244+
}
245+
246+
await git.fetch(refSpec, options)
247+
248+
expect(mockExec).toHaveBeenCalledWith(
249+
expect.any(String),
250+
[
251+
'-c',
252+
'protocol.version=2',
253+
'fetch',
254+
'--prune',
255+
'--progress',
256+
'--no-recurse-submodules',
257+
'--filter=filterValue',
258+
'--depth=1',
259+
'origin',
260+
'refspec1',
261+
'refspec2'
262+
],
263+
expect.any(Object)
264+
)
265+
})
266+
})

__test__/input-helper.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ describe('input-helper tests', () => {
8282
expect(settings.sparseCheckout).toBe(undefined)
8383
expect(settings.sparseCheckoutConeMode).toBe(true)
8484
expect(settings.fetchDepth).toBe(1)
85+
expect(settings.fetchTags).toBe(false)
8586
expect(settings.lfs).toBe(false)
8687
expect(settings.ref).toBe('refs/heads/some-ref')
8788
expect(settings.repositoryName).toBe('some-repo')

action.yml

+3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ inputs:
6565
fetch-depth:
6666
description: 'Number of commits to fetch. 0 indicates all history for all branches and tags.'
6767
default: 1
68+
fetch-tags:
69+
description: 'Whether to fetch tags, even if fetch-depth > 0.'
70+
default: false
6871
lfs:
6972
description: 'Whether to download Git-LFS files'
7073
default: false

dist/index.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ class GitCommandManager {
637637
fetch(refSpec, options) {
638638
return __awaiter(this, void 0, void 0, function* () {
639639
const args = ['-c', 'protocol.version=2', 'fetch'];
640-
if (!refSpec.some(x => x === refHelper.tagsRefSpec)) {
640+
if (!refSpec.some(x => x === refHelper.tagsRefSpec) && !options.fetchTags) {
641641
args.push('--no-tags');
642642
}
643643
args.push('--prune', '--progress', '--no-recurse-submodules');
@@ -718,8 +718,8 @@ class GitCommandManager {
718718
}
719719
log1(format) {
720720
return __awaiter(this, void 0, void 0, function* () {
721-
var args = format ? ['log', '-1', format] : ['log', '-1'];
722-
var silent = format ? false : true;
721+
const args = format ? ['log', '-1', format] : ['log', '-1'];
722+
const silent = format ? false : true;
723723
const output = yield this.execGit(args, false, silent);
724724
return output.stdout;
725725
});
@@ -1256,6 +1256,7 @@ function getSource(settings) {
12561256
}
12571257
else {
12581258
fetchOptions.fetchDepth = settings.fetchDepth;
1259+
fetchOptions.fetchTags = settings.fetchTags;
12591260
const refSpec = refHelper.getRefSpec(settings.ref, settings.commit);
12601261
yield git.fetch(refSpec, fetchOptions);
12611262
}
@@ -1734,6 +1735,10 @@ function getInputs() {
17341735
result.fetchDepth = 0;
17351736
}
17361737
core.debug(`fetch depth = ${result.fetchDepth}`);
1738+
// Fetch tags
1739+
result.fetchTags =
1740+
(core.getInput('fetch-tags') || 'false').toUpperCase() === 'TRUE';
1741+
core.debug(`fetch tags = ${result.fetchTags}`);
17371742
// LFS
17381743
result.lfs = (core.getInput('lfs') || 'false').toUpperCase() === 'TRUE';
17391744
core.debug(`lfs = ${result.lfs}`);

src/git-command-manager.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export interface IGitCommandManager {
3333
options: {
3434
filter?: string
3535
fetchDepth?: number
36+
fetchTags?: boolean
3637
}
3738
): Promise<void>
3839
getDefaultBranch(repositoryUrl: string): Promise<string>
@@ -240,10 +241,10 @@ class GitCommandManager {
240241

241242
async fetch(
242243
refSpec: string[],
243-
options: {filter?: string; fetchDepth?: number}
244+
options: {filter?: string; fetchDepth?: number; fetchTags?: boolean}
244245
): Promise<void> {
245246
const args = ['-c', 'protocol.version=2', 'fetch']
246-
if (!refSpec.some(x => x === refHelper.tagsRefSpec)) {
247+
if (!refSpec.some(x => x === refHelper.tagsRefSpec) && !options.fetchTags) {
247248
args.push('--no-tags')
248249
}
249250

@@ -333,8 +334,8 @@ class GitCommandManager {
333334
}
334335

335336
async log1(format?: string): Promise<string> {
336-
var args = format ? ['log', '-1', format] : ['log', '-1']
337-
var silent = format ? false : true
337+
const args = format ? ['log', '-1', format] : ['log', '-1']
338+
const silent = format ? false : true
338339
const output = await this.execGit(args, false, silent)
339340
return output.stdout
340341
}

src/git-source-provider.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,11 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
153153

154154
// Fetch
155155
core.startGroup('Fetching the repository')
156-
const fetchOptions: {filter?: string; fetchDepth?: number} = {}
156+
const fetchOptions: {
157+
filter?: string
158+
fetchDepth?: number
159+
fetchTags?: boolean
160+
} = {}
157161
if (settings.sparseCheckout) fetchOptions.filter = 'blob:none'
158162
if (settings.fetchDepth <= 0) {
159163
// Fetch all branches and tags
@@ -171,6 +175,7 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
171175
}
172176
} else {
173177
fetchOptions.fetchDepth = settings.fetchDepth
178+
fetchOptions.fetchTags = settings.fetchTags
174179
const refSpec = refHelper.getRefSpec(settings.ref, settings.commit)
175180
await git.fetch(refSpec, fetchOptions)
176181
}

src/git-source-settings.ts

+5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ export interface IGitSourceSettings {
4444
*/
4545
fetchDepth: number
4646

47+
/**
48+
* Fetch tags, even if fetchDepth > 0 (default: false)
49+
*/
50+
fetchTags: boolean
51+
4752
/**
4853
* Indicates whether to fetch LFS objects
4954
*/

src/input-helper.ts

+5
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ export async function getInputs(): Promise<IGitSourceSettings> {
100100
}
101101
core.debug(`fetch depth = ${result.fetchDepth}`)
102102

103+
// Fetch tags
104+
result.fetchTags =
105+
(core.getInput('fetch-tags') || 'false').toUpperCase() === 'TRUE'
106+
core.debug(`fetch tags = ${result.fetchTags}`)
107+
103108
// LFS
104109
result.lfs = (core.getInput('lfs') || 'false').toUpperCase() === 'TRUE'
105110
core.debug(`lfs = ${result.lfs}`)

0 commit comments

Comments
 (0)