Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch all history for all tags and branches when fetch-depth=0 #258

Merged
merged 1 commit into from May 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 10 additions & 27 deletions README.md
Expand Up @@ -6,7 +6,7 @@

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

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.
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.

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.

Expand Down Expand Up @@ -110,16 +110,22 @@ Refer [here](https://github.com/actions/checkout/blob/v1/README.md) for previous

# Scenarios

- [Fetch all history for all tags and branches](#Fetch-all-history-for-all-tags-and-branches)
- [Checkout a different branch](#Checkout-a-different-branch)
- [Checkout HEAD^](#Checkout-HEAD)
- [Checkout multiple repos (side by side)](#Checkout-multiple-repos-side-by-side)
- [Checkout multiple repos (nested)](#Checkout-multiple-repos-nested)
- [Checkout multiple repos (private)](#Checkout-multiple-repos-private)
- [Checkout pull request HEAD commit instead of merge commit](#Checkout-pull-request-HEAD-commit-instead-of-merge-commit)
- [Checkout pull request on closed event](#Checkout-pull-request-on-closed-event)
- [Fetch all tags](#Fetch-all-tags)
- [Fetch all branches](#Fetch-all-branches)
- [Fetch all history for all tags and branches](#Fetch-all-history-for-all-tags-and-branches)

## Fetch all history for all tags and branches
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chrispat fyi

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comment sums up the motivation nicely:

Seems pretty logical and reasonable to me that if I set fetch-depth: 0 then I clearly want everything and history matters to me and that you should not add the --no-tags parameter.


```yaml
- uses: actions/checkout@v2
with:
fetch-depth: 0
```

## Checkout a different branch

Expand Down Expand Up @@ -207,29 +213,6 @@ jobs:
- uses: actions/checkout@v2
```

## Fetch all tags
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bye bye complicated stuff


```yaml
- uses: actions/checkout@v2
- run: git fetch --depth=1 origin +refs/tags/*:refs/tags/*
```

## Fetch all branches

```yaml
- uses: actions/checkout@v2
- run: |
git fetch --no-tags --prune --depth=1 origin +refs/heads/*:refs/remotes/origin/*
```

## Fetch all history for all tags and branches

```yaml
- uses: actions/checkout@v2
- run: |
git fetch --prune --unshallow --tags
```

# License

The scripts and documentation in this project are released under the [MIT License](LICENSE)
2 changes: 2 additions & 0 deletions __test__/git-auth-helper.test.ts
Expand Up @@ -722,9 +722,11 @@ async function setup(testName: string): Promise<void> {
log1: jest.fn(),
remoteAdd: jest.fn(),
removeEnvironmentVariable: jest.fn((name: string) => delete git.env[name]),
revParse: jest.fn(),
setEnvironmentVariable: jest.fn((name: string, value: string) => {
git.env[name] = value
}),
shaExists: jest.fn(),
submoduleForeach: jest.fn(async () => {
return ''
}),
Expand Down
90 changes: 72 additions & 18 deletions __test__/git-directory-helper.test.ts
Expand Up @@ -9,6 +9,7 @@ const testWorkspace = path.join(__dirname, '_temp', 'git-directory-helper')
let repositoryPath: string
let repositoryUrl: string
let clean: boolean
let ref: string
let git: IGitCommandManager

describe('git-directory-helper tests', () => {
Expand Down Expand Up @@ -41,7 +42,8 @@ describe('git-directory-helper tests', () => {
git,
repositoryPath,
repositoryUrl,
clean
clean,
ref
)

// Assert
Expand All @@ -63,7 +65,8 @@ describe('git-directory-helper tests', () => {
git,
repositoryPath,
repositoryUrl,
clean
clean,
ref
)

// Assert
Expand All @@ -88,7 +91,8 @@ describe('git-directory-helper tests', () => {
git,
repositoryPath,
repositoryUrl,
clean
clean,
ref
)

// Assert
Expand All @@ -109,7 +113,8 @@ describe('git-directory-helper tests', () => {
git,
repositoryPath,
repositoryUrl,
clean
clean,
ref
)

// Assert
Expand Down Expand Up @@ -137,7 +142,8 @@ describe('git-directory-helper tests', () => {
git,
repositoryPath,
repositoryUrl,
clean
clean,
ref
)

// Assert
Expand All @@ -163,7 +169,8 @@ describe('git-directory-helper tests', () => {
git,
repositoryPath,
differentRepositoryUrl,
clean
clean,
ref
)

// Assert
Expand All @@ -187,7 +194,8 @@ describe('git-directory-helper tests', () => {
git,
repositoryPath,
repositoryUrl,
clean
clean,
ref
)

// Assert
Expand All @@ -212,7 +220,8 @@ describe('git-directory-helper tests', () => {
git,
repositoryPath,
repositoryUrl,
clean
clean,
ref
)

// Assert
Expand All @@ -236,7 +245,8 @@ describe('git-directory-helper tests', () => {
undefined,
repositoryPath,
repositoryUrl,
clean
clean,
ref
)

// Assert
Expand All @@ -260,7 +270,8 @@ describe('git-directory-helper tests', () => {
git,
repositoryPath,
repositoryUrl,
clean
clean,
ref
)

// Assert
Expand Down Expand Up @@ -290,7 +301,8 @@ describe('git-directory-helper tests', () => {
git,
repositoryPath,
repositoryUrl,
clean
clean,
ref
)

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

const removesRemoteBranches = 'removes local branches'
it(removesRemoteBranches, async () => {
const removesAncestorRemoteBranch = 'removes ancestor remote branch'
it(removesAncestorRemoteBranch, async () => {
// Arrange
await setup(removesRemoteBranches)
await setup(removesAncestorRemoteBranch)
await fs.promises.writeFile(path.join(repositoryPath, 'my-file'), '')
const mockBranchList = git.branchList as jest.Mock<any, any>
mockBranchList.mockImplementation(async (remote: boolean) => {
return remote ? ['remote-branch-1', 'remote-branch-2'] : []
return remote ? ['origin/remote-branch-1', 'origin/remote-branch-2'] : []
})
ref = 'remote-branch-1/conflict'

// Act
await gitDirectoryHelper.prepareExistingDirectory(
git,
repositoryPath,
repositoryUrl,
clean
clean,
ref
)

// Assert
const files = await fs.promises.readdir(repositoryPath)
expect(files.sort()).toEqual(['.git', 'my-file'])
expect(git.branchDelete).toHaveBeenCalledWith(true, 'remote-branch-1')
expect(git.branchDelete).toHaveBeenCalledWith(true, 'remote-branch-2')
expect(git.branchDelete).toHaveBeenCalledTimes(1)
expect(git.branchDelete).toHaveBeenCalledWith(
true,
'origin/remote-branch-1'
)
})

const removesDescendantRemoteBranches = 'removes descendant remote branch'
it(removesDescendantRemoteBranches, async () => {
// Arrange
await setup(removesDescendantRemoteBranches)
await fs.promises.writeFile(path.join(repositoryPath, 'my-file'), '')
const mockBranchList = git.branchList as jest.Mock<any, any>
mockBranchList.mockImplementation(async (remote: boolean) => {
return remote
? ['origin/remote-branch-1/conflict', 'origin/remote-branch-2']
: []
})
ref = 'remote-branch-1'

// Act
await gitDirectoryHelper.prepareExistingDirectory(
git,
repositoryPath,
repositoryUrl,
clean,
ref
)

// Assert
const files = await fs.promises.readdir(repositoryPath)
expect(files.sort()).toEqual(['.git', 'my-file'])
expect(git.branchDelete).toHaveBeenCalledTimes(1)
expect(git.branchDelete).toHaveBeenCalledWith(
true,
'origin/remote-branch-1/conflict'
)
})
})

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

// Ref
ref = ''

// Git command manager
git = {
branchDelete: jest.fn(),
Expand All @@ -364,7 +416,9 @@ async function setup(testName: string): Promise<void> {
log1: jest.fn(),
remoteAdd: jest.fn(),
removeEnvironmentVariable: jest.fn(),
revParse: jest.fn(),
setEnvironmentVariable: jest.fn(),
shaExists: jest.fn(),
submoduleForeach: jest.fn(),
submoduleSync: jest.fn(),
submoduleUpdate: jest.fn(),
Expand Down
2 changes: 1 addition & 1 deletion adrs/0153-checkout-v2.md
Expand Up @@ -70,7 +70,7 @@ We want to take this opportunity to make behavioral changes, from v1. This docum
description: 'Whether to execute `git clean -ffdx && git reset --hard HEAD` before fetching'
default: true
fetch-depth:
description: 'Number of commits to fetch. 0 indicates all history.'
description: 'Number of commits to fetch. 0 indicates all history for all tags and branches.'
default: 1
lfs:
description: 'Whether to download Git-LFS files'
Expand Down