Skip to content

Commit b9de40a

Browse files
committedMar 6, 2025
Add objectFormat setting to allow init()ing a repo with sha256
1 parent 85e6279 commit b9de40a

9 files changed

+81
-8
lines changed
 

‎README.md

+14
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ Please refer to the [release page](https://github.com/actions/checkout/releases/
126126
# running from unless specified. Example URLs are https://github.com or
127127
# https://my-ghes-server.example.com
128128
github-server-url: ''
129+
130+
# Use the given object format when creating local repository. Specifically, use
131+
# 'sha256' to checkout a SHA-256 repository.
132+
# Default: null
133+
object-format: ''
129134
```
130135
<!-- end usage -->
131136
@@ -144,6 +149,7 @@ Please refer to the [release page](https://github.com/actions/checkout/releases/
144149
- [Checkout pull request on closed event](#Checkout-pull-request-on-closed-event)
145150
- [Push a commit using the built-in token](#Push-a-commit-using-the-built-in-token)
146151
- [Push a commit to a PR using the built-in token](#Push-a-commit-to-a-PR-using-the-built-in-token)
152+
- [Checkout SHA-256 repository](#checkout-sha-256-repository)
147153

148154
## Fetch only the root files
149155

@@ -323,6 +329,14 @@ permissions:
323329
contents: read
324330
```
325331

332+
## Checkout SHA-256 repository
333+
334+
```yaml
335+
- uses: actions/checkout@v4
336+
with:
337+
object-format: sha256
338+
```
339+
326340
# License
327341

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

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,8 @@ async function setup(testName: string): Promise<void> {
824824
sshUser: '',
825825
workflowOrganizationId: 123456,
826826
setSafeDirectory: true,
827-
githubServerUrl: githubServerUrl
827+
githubServerUrl: githubServerUrl,
828+
objectFormat: undefined
828829
}
829830
}
830831

‎__test__/git-command-manager.test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -375,4 +375,25 @@ describe('Test fetchDepth and fetchTags options', () => {
375375
expect.any(Object)
376376
)
377377
})
378+
379+
it('should call execGit with the correct arguments when sha256 is used', async () => {
380+
jest.spyOn(exec, 'exec').mockImplementation(mockExec)
381+
382+
const workingDirectory = 'test'
383+
const lfs = false
384+
const doSparseCheckout = false
385+
git = await commandManager.createCommandManager(
386+
workingDirectory,
387+
lfs,
388+
doSparseCheckout
389+
)
390+
391+
await git.init({objectFormat: 'sha256'})
392+
393+
expect(mockExec).toHaveBeenCalledWith(
394+
expect.any(String),
395+
['init', '--object-format=sha256', 'test'],
396+
expect.any(Object)
397+
)
398+
})
378399
})

‎action.yml

+5
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ inputs:
9898
github-server-url:
9999
description: The base URL for the GitHub instance that you are trying to clone from, will use environment defaults to fetch from the same instance that the workflow is running from unless specified. Example URLs are https://github.com or https://my-ghes-server.example.com
100100
required: false
101+
object-format:
102+
description: >
103+
Use the given object format when creating local repository. Specifically, use
104+
'sha256' to checkout a SHA-256 repository.
105+
default: null
101106
outputs:
102107
ref:
103108
description: 'The branch, tag or SHA that was checked out'

‎dist/index.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -709,9 +709,13 @@ class GitCommandManager {
709709
getWorkingDirectory() {
710710
return this.workingDirectory;
711711
}
712-
init() {
712+
init(options) {
713713
return __awaiter(this, void 0, void 0, function* () {
714-
yield this.execGit(['init', this.workingDirectory]);
714+
yield this.execGit([
715+
'init',
716+
...((options === null || options === void 0 ? void 0 : options.objectFormat) ? [`--object-format=${options.objectFormat}`] : []),
717+
this.workingDirectory
718+
]);
715719
});
716720
}
717721
isDetached() {
@@ -1236,7 +1240,7 @@ function getSource(settings) {
12361240
// Initialize the repository
12371241
if (!fsHelper.directoryExistsSync(path.join(settings.repositoryPath, '.git'))) {
12381242
core.startGroup('Initializing the repository');
1239-
yield git.init();
1243+
yield git.init({ objectFormat: settings.objectFormat });
12401244
yield git.remoteAdd('origin', repositoryUrl);
12411245
core.endGroup();
12421246
}
@@ -1831,6 +1835,14 @@ function getInputs() {
18311835
// Determine the GitHub URL that the repository is being hosted from
18321836
result.githubServerUrl = core.getInput('github-server-url');
18331837
core.debug(`GitHub Host URL = ${result.githubServerUrl}`);
1838+
// Object format
1839+
const objectFormat = core.getInput('object-format');
1840+
if (objectFormat) {
1841+
if (objectFormat != 'sha1' && objectFormat != 'sha256') {
1842+
throw Error(`Invalid object format '${objectFormat}'`);
1843+
}
1844+
result.objectFormat = objectFormat;
1845+
}
18341846
return result;
18351847
});
18361848
}

‎src/git-command-manager.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export interface IGitCommandManager {
4242
): Promise<void>
4343
getDefaultBranch(repositoryUrl: string): Promise<string>
4444
getWorkingDirectory(): string
45-
init(): Promise<void>
45+
init(options?: {objectFormat?: string}): Promise<void>
4646
isDetached(): Promise<boolean>
4747
lfsFetch(ref: string): Promise<void>
4848
lfsInstall(): Promise<void>
@@ -327,8 +327,14 @@ class GitCommandManager {
327327
return this.workingDirectory
328328
}
329329

330-
async init(): Promise<void> {
331-
await this.execGit(['init', this.workingDirectory])
330+
async init(options?: {objectFormat?: string}): Promise<void> {
331+
await this.execGit([
332+
'init',
333+
...(options?.objectFormat
334+
? [`--object-format=${options.objectFormat}`]
335+
: []),
336+
this.workingDirectory
337+
])
332338
}
333339

334340
async isDetached(): Promise<boolean> {

‎src/git-source-provider.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
110110
!fsHelper.directoryExistsSync(path.join(settings.repositoryPath, '.git'))
111111
) {
112112
core.startGroup('Initializing the repository')
113-
await git.init()
113+
await git.init({objectFormat: settings.objectFormat})
114114
await git.remoteAdd('origin', repositoryUrl)
115115
core.endGroup()
116116
}

‎src/git-source-settings.ts

+5
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,9 @@ export interface IGitSourceSettings {
118118
* User override on the GitHub Server/Host URL that hosts the repository to be cloned
119119
*/
120120
githubServerUrl: string | undefined
121+
122+
/**
123+
* Object format used for the repo, if it is not default
124+
*/
125+
objectFormat: 'sha1' | 'sha256' | undefined
121126
}

‎src/input-helper.ts

+9
Original file line numberDiff line numberDiff line change
@@ -161,5 +161,14 @@ export async function getInputs(): Promise<IGitSourceSettings> {
161161
result.githubServerUrl = core.getInput('github-server-url')
162162
core.debug(`GitHub Host URL = ${result.githubServerUrl}`)
163163

164+
// Object format
165+
const objectFormat = core.getInput('object-format')
166+
if (objectFormat) {
167+
if (objectFormat != 'sha1' && objectFormat != 'sha256') {
168+
throw Error(`Invalid object format '${objectFormat}'`)
169+
}
170+
result.objectFormat = objectFormat
171+
}
172+
164173
return result
165174
}

0 commit comments

Comments
 (0)
Failed to load comments.