Skip to content

Commit e52d022

Browse files
authoredMay 27, 2020
Fetch all history for all tags and branches when fetch-depth=0 (#258)
1 parent 2ff2fbd commit e52d022

9 files changed

+338
-86
lines changed
 

‎README.md

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

77
This action checks-out your repository under `$GITHUB_WORKSPACE`, so your workflow can access it.
88

9-
Only a single commit is fetched by default, for the ref/SHA that triggered the workflow. Set `fetch-depth` to fetch more history. Refer [here](https://help.github.com/en/articles/events-that-trigger-workflows) to learn which commit `$GITHUB_SHA` points to for different events.
9+
Only a single commit is fetched by default, for the ref/SHA that triggered the workflow. Set `fetch-depth: 0` to fetch all history for all branches and tags. Refer [here](https://help.github.com/en/articles/events-that-trigger-workflows) to learn which commit `$GITHUB_SHA` points to for different events.
1010

1111
The auth token is persisted in the local git config. This enables your scripts to run authenticated git commands. The token is removed during post-job cleanup. Set `persist-credentials: false` to opt-out.
1212

@@ -110,16 +110,22 @@ Refer [here](https://github.com/actions/checkout/blob/v1/README.md) for previous
110110
111111
# Scenarios
112112
113+
- [Fetch all history for all tags and branches](#Fetch-all-history-for-all-tags-and-branches)
113114
- [Checkout a different branch](#Checkout-a-different-branch)
114115
- [Checkout HEAD^](#Checkout-HEAD)
115116
- [Checkout multiple repos (side by side)](#Checkout-multiple-repos-side-by-side)
116117
- [Checkout multiple repos (nested)](#Checkout-multiple-repos-nested)
117118
- [Checkout multiple repos (private)](#Checkout-multiple-repos-private)
118119
- [Checkout pull request HEAD commit instead of merge commit](#Checkout-pull-request-HEAD-commit-instead-of-merge-commit)
119120
- [Checkout pull request on closed event](#Checkout-pull-request-on-closed-event)
120-
- [Fetch all tags](#Fetch-all-tags)
121-
- [Fetch all branches](#Fetch-all-branches)
122-
- [Fetch all history for all tags and branches](#Fetch-all-history-for-all-tags-and-branches)
121+
122+
## Fetch all history for all tags and branches
123+
124+
```yaml
125+
- uses: actions/checkout@v2
126+
with:
127+
fetch-depth: 0
128+
```
123129
124130
## Checkout a different branch
125131
@@ -207,29 +213,6 @@ jobs:
207213
- uses: actions/checkout@v2
208214
```
209215

210-
## Fetch all tags
211-
212-
```yaml
213-
- uses: actions/checkout@v2
214-
- run: git fetch --depth=1 origin +refs/tags/*:refs/tags/*
215-
```
216-
217-
## Fetch all branches
218-
219-
```yaml
220-
- uses: actions/checkout@v2
221-
- run: |
222-
git fetch --no-tags --prune --depth=1 origin +refs/heads/*:refs/remotes/origin/*
223-
```
224-
225-
## Fetch all history for all tags and branches
226-
227-
```yaml
228-
- uses: actions/checkout@v2
229-
- run: |
230-
git fetch --prune --unshallow --tags
231-
```
232-
233216
# License
234217

235218
The scripts and documentation in this project are released under the [MIT License](LICENSE)

‎__test__/git-auth-helper.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -722,9 +722,11 @@ async function setup(testName: string): Promise<void> {
722722
log1: jest.fn(),
723723
remoteAdd: jest.fn(),
724724
removeEnvironmentVariable: jest.fn((name: string) => delete git.env[name]),
725+
revParse: jest.fn(),
725726
setEnvironmentVariable: jest.fn((name: string, value: string) => {
726727
git.env[name] = value
727728
}),
729+
shaExists: jest.fn(),
728730
submoduleForeach: jest.fn(async () => {
729731
return ''
730732
}),

‎__test__/git-directory-helper.test.ts

+72-18
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const testWorkspace = path.join(__dirname, '_temp', 'git-directory-helper')
99
let repositoryPath: string
1010
let repositoryUrl: string
1111
let clean: boolean
12+
let ref: string
1213
let git: IGitCommandManager
1314

1415
describe('git-directory-helper tests', () => {
@@ -41,7 +42,8 @@ describe('git-directory-helper tests', () => {
4142
git,
4243
repositoryPath,
4344
repositoryUrl,
44-
clean
45+
clean,
46+
ref
4547
)
4648

4749
// Assert
@@ -63,7 +65,8 @@ describe('git-directory-helper tests', () => {
6365
git,
6466
repositoryPath,
6567
repositoryUrl,
66-
clean
68+
clean,
69+
ref
6770
)
6871

6972
// Assert
@@ -88,7 +91,8 @@ describe('git-directory-helper tests', () => {
8891
git,
8992
repositoryPath,
9093
repositoryUrl,
91-
clean
94+
clean,
95+
ref
9296
)
9397

9498
// Assert
@@ -109,7 +113,8 @@ describe('git-directory-helper tests', () => {
109113
git,
110114
repositoryPath,
111115
repositoryUrl,
112-
clean
116+
clean,
117+
ref
113118
)
114119

115120
// Assert
@@ -137,7 +142,8 @@ describe('git-directory-helper tests', () => {
137142
git,
138143
repositoryPath,
139144
repositoryUrl,
140-
clean
145+
clean,
146+
ref
141147
)
142148

143149
// Assert
@@ -163,7 +169,8 @@ describe('git-directory-helper tests', () => {
163169
git,
164170
repositoryPath,
165171
differentRepositoryUrl,
166-
clean
172+
clean,
173+
ref
167174
)
168175

169176
// Assert
@@ -187,7 +194,8 @@ describe('git-directory-helper tests', () => {
187194
git,
188195
repositoryPath,
189196
repositoryUrl,
190-
clean
197+
clean,
198+
ref
191199
)
192200

193201
// Assert
@@ -212,7 +220,8 @@ describe('git-directory-helper tests', () => {
212220
git,
213221
repositoryPath,
214222
repositoryUrl,
215-
clean
223+
clean,
224+
ref
216225
)
217226

218227
// Assert
@@ -236,7 +245,8 @@ describe('git-directory-helper tests', () => {
236245
undefined,
237246
repositoryPath,
238247
repositoryUrl,
239-
clean
248+
clean,
249+
ref
240250
)
241251

242252
// Assert
@@ -260,7 +270,8 @@ describe('git-directory-helper tests', () => {
260270
git,
261271
repositoryPath,
262272
repositoryUrl,
263-
clean
273+
clean,
274+
ref
264275
)
265276

266277
// Assert
@@ -290,7 +301,8 @@ describe('git-directory-helper tests', () => {
290301
git,
291302
repositoryPath,
292303
repositoryUrl,
293-
clean
304+
clean,
305+
ref
294306
)
295307

296308
// Assert
@@ -305,29 +317,66 @@ describe('git-directory-helper tests', () => {
305317
expect(git.tryReset).not.toHaveBeenCalled()
306318
})
307319

308-
const removesRemoteBranches = 'removes local branches'
309-
it(removesRemoteBranches, async () => {
320+
const removesAncestorRemoteBranch = 'removes ancestor remote branch'
321+
it(removesAncestorRemoteBranch, async () => {
310322
// Arrange
311-
await setup(removesRemoteBranches)
323+
await setup(removesAncestorRemoteBranch)
312324
await fs.promises.writeFile(path.join(repositoryPath, 'my-file'), '')
313325
const mockBranchList = git.branchList as jest.Mock<any, any>
314326
mockBranchList.mockImplementation(async (remote: boolean) => {
315-
return remote ? ['remote-branch-1', 'remote-branch-2'] : []
327+
return remote ? ['origin/remote-branch-1', 'origin/remote-branch-2'] : []
316328
})
329+
ref = 'remote-branch-1/conflict'
317330

318331
// Act
319332
await gitDirectoryHelper.prepareExistingDirectory(
320333
git,
321334
repositoryPath,
322335
repositoryUrl,
323-
clean
336+
clean,
337+
ref
324338
)
325339

326340
// Assert
327341
const files = await fs.promises.readdir(repositoryPath)
328342
expect(files.sort()).toEqual(['.git', 'my-file'])
329-
expect(git.branchDelete).toHaveBeenCalledWith(true, 'remote-branch-1')
330-
expect(git.branchDelete).toHaveBeenCalledWith(true, 'remote-branch-2')
343+
expect(git.branchDelete).toHaveBeenCalledTimes(1)
344+
expect(git.branchDelete).toHaveBeenCalledWith(
345+
true,
346+
'origin/remote-branch-1'
347+
)
348+
})
349+
350+
const removesDescendantRemoteBranches = 'removes descendant remote branch'
351+
it(removesDescendantRemoteBranches, async () => {
352+
// Arrange
353+
await setup(removesDescendantRemoteBranches)
354+
await fs.promises.writeFile(path.join(repositoryPath, 'my-file'), '')
355+
const mockBranchList = git.branchList as jest.Mock<any, any>
356+
mockBranchList.mockImplementation(async (remote: boolean) => {
357+
return remote
358+
? ['origin/remote-branch-1/conflict', 'origin/remote-branch-2']
359+
: []
360+
})
361+
ref = 'remote-branch-1'
362+
363+
// Act
364+
await gitDirectoryHelper.prepareExistingDirectory(
365+
git,
366+
repositoryPath,
367+
repositoryUrl,
368+
clean,
369+
ref
370+
)
371+
372+
// Assert
373+
const files = await fs.promises.readdir(repositoryPath)
374+
expect(files.sort()).toEqual(['.git', 'my-file'])
375+
expect(git.branchDelete).toHaveBeenCalledTimes(1)
376+
expect(git.branchDelete).toHaveBeenCalledWith(
377+
true,
378+
'origin/remote-branch-1/conflict'
379+
)
331380
})
332381
})
333382

@@ -344,6 +393,9 @@ async function setup(testName: string): Promise<void> {
344393
// Clean
345394
clean = true
346395

396+
// Ref
397+
ref = ''
398+
347399
// Git command manager
348400
git = {
349401
branchDelete: jest.fn(),
@@ -364,7 +416,9 @@ async function setup(testName: string): Promise<void> {
364416
log1: jest.fn(),
365417
remoteAdd: jest.fn(),
366418
removeEnvironmentVariable: jest.fn(),
419+
revParse: jest.fn(),
367420
setEnvironmentVariable: jest.fn(),
421+
shaExists: jest.fn(),
368422
submoduleForeach: jest.fn(),
369423
submoduleSync: jest.fn(),
370424
submoduleUpdate: jest.fn(),

‎adrs/0153-checkout-v2.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ We want to take this opportunity to make behavioral changes, from v1. This docum
7070
description: 'Whether to execute `git clean -ffdx && git reset --hard HEAD` before fetching'
7171
default: true
7272
fetch-depth:
73-
description: 'Number of commits to fetch. 0 indicates all history.'
73+
description: 'Number of commits to fetch. 0 indicates all history for all tags and branches.'
7474
default: 1
7575
lfs:
7676
description: 'Whether to download Git-LFS files'

0 commit comments

Comments
 (0)
Please sign in to comment.