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

[ci-visibility] Improve git unshallow command #3664

Merged
merged 2 commits into from
Oct 2, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions packages/dd-trace/src/plugins/util/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,37 @@ function unshallowRepository () {
}
const defaultRemoteName = sanitizedExec('git', ['config', '--default', 'origin', '--get', 'clone.defaultRemoteName'])
const revParseHead = sanitizedExec('git', ['rev-parse', 'HEAD'])
sanitizedExec('git', [

const baseGitOptions = [
'fetch',
'--shallow-since="1 month ago"',
'--update-shallow',
'--filter=blob:none',
'--recurse-submodules=no',
defaultRemoteName,
revParseHead
])
defaultRemoteName
]

try {
execFileSync('git', [
...baseGitOptions,
revParseHead
])
} catch (e) {
// If the local HEAD is a commit that has not been pushed to the remote, the above command will fail.
log.error(e)
const upstreamRemote = sanitizedExec('git', ['rev-parse', '--abbrev-ref', '--symbolic-full-name', '@{upstream}'])
try {
execFileSync('git', [
...baseGitOptions,
upstreamRemote
])
} catch (e) {
// If the CI is working on a detached HEAD or branch tracking hasn’t been set up, the above command will fail.
log.error(e)
// We use sanitizedExec here because if this last option fails, we'll give up.
sanitizedExec('git', baseGitOptions)
}
}
}

function getRepositoryUrl () {
Expand Down
86 changes: 84 additions & 2 deletions packages/dd-trace/test/plugins/util/git.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const path = require('path')
const { GIT_REV_LIST_MAX_BUFFER } = require('../../../src/plugins/util/git')
const proxyquire = require('proxyquire')
const sanitizedExecStub = sinon.stub().returns('')
const execFileSyncStub = sinon.stub().returns('')

const {
GIT_COMMIT_SHA,
Expand All @@ -26,10 +27,13 @@ const {
CI_WORKSPACE_PATH
} = require('../../../src/plugins/util/tags')

const { getGitMetadata } = proxyquire('../../../src/plugins/util/git',
const { getGitMetadata, unshallowRepository } = proxyquire('../../../src/plugins/util/git',
{
'./exec': {
'sanitizedExec': sanitizedExecStub
sanitizedExec: sanitizedExecStub
},
'child_process': {
execFileSync: execFileSyncStub
}
}
)
Expand Down Expand Up @@ -239,3 +243,81 @@ describe('generatePackFilesForCommits', () => {
expect(packFilesToUpload).to.eql([])
})
})

describe('unshallowRepository', () => {
afterEach(() => {
sanitizedExecStub.reset()
execFileSyncStub.reset()
})
it('works for the usual case', () => {
sanitizedExecStub
.onCall(0).returns(
'git version 2.39.0'
)
.onCall(1).returns('origin')
.onCall(2).returns('daede5785233abb1a3cb76b9453d4eb5b98290b3')

const options = [
'fetch',
'--shallow-since="1 month ago"',
'--update-shallow',
'--filter=blob:none',
'--recurse-submodules=no',
'origin',
'daede5785233abb1a3cb76b9453d4eb5b98290b3'
]

unshallowRepository()
expect(execFileSyncStub).to.have.been.calledWith('git', options)
})
it('works if the local HEAD is a commit that has not been pushed to the remote', () => {
sanitizedExecStub
.onCall(0).returns(
'git version 2.39.0'
)
.onCall(1).returns('origin')
.onCall(2).returns('daede5785233abb1a3cb76b9453d4eb5b98290b3')
.onCall(3).returns('origin/master')

execFileSyncStub
.onCall(0).throws()

const options = [
'fetch',
'--shallow-since="1 month ago"',
'--update-shallow',
'--filter=blob:none',
'--recurse-submodules=no',
'origin',
'origin/master'
]

unshallowRepository()
expect(execFileSyncStub).to.have.been.calledWith('git', options)
})
it('works if the CI is working on a detached HEAD or branch tracking hasn’t been set up', () => {
sanitizedExecStub
.onCall(0).returns(
'git version 2.39.0'
)
.onCall(1).returns('origin')
.onCall(2).returns('daede5785233abb1a3cb76b9453d4eb5b98290b3')
.onCall(3).returns('origin/master')

execFileSyncStub
.onCall(0).throws()
.onCall(1).throws()

const options = [
'fetch',
'--shallow-since="1 month ago"',
'--update-shallow',
'--filter=blob:none',
'--recurse-submodules=no',
'origin'
]

unshallowRepository()
expect(sanitizedExecStub).to.have.been.calledWith('git', options)
})
})
Loading