Skip to content

Commit

Permalink
fix: apply memoize to git config ops
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Mar 28, 2023
1 parent 0c21030 commit b46bec0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
23 changes: 20 additions & 3 deletions src/main/js/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ export const pushCommit = async ({cwd, from, to, branch, origin, msg, ignoreFile
if (from) await copy({baseFrom: cwd, from, baseTo: _cwd, to, ignoreFiles, cwd})

try {
await $`git config user.name ${gitCommitterName} &&
git config user.email ${gitCommitterEmail} &&
git add . &&
await setUserConfig(_cwd, gitCommitterName, gitCommitterEmail)
await $`git add . &&
git commit -m ${msg}`
} catch {
log({level: 'warn'})(`no changes to commit to ${branch}`)
Expand Down Expand Up @@ -99,3 +98,21 @@ export const getTags = async (cwd, ref = '') =>
(await $.o({cwd})`git tag -l ${ref}`)
.toString()
.split('\n')

export const pushTag = async ({cwd, tag, gitCommitterName, gitCommitterEmail}) => {
await setUserConfig(cwd, gitCommitterName, gitCommitterEmail)
await $.o({cwd})`
git tag -m ${tag} ${tag} &&
git push origin ${tag}`
}

// Memoize prevents .git/config lock
// https://github.com/qiwi/packasso/actions/runs/4539987310/jobs/8000403413#step:7:282
export const setUserConfig = memoizeBy(async(cwd, gitCommitterName, gitCommitterEmail) => $.o({cwd})`
git config user.name ${gitCommitterName} &&
git config user.email ${gitCommitterEmail}
`)

export const unsetUserConfig = async(cwd) => $.o({cwd})`
git config --unset user.name &&
git config --unset user.email`
17 changes: 7 additions & 10 deletions src/main/js/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,21 @@

import {Buffer} from 'node:buffer'
import {queuefy} from 'queuefy'
import {ctx, semver, $, fs, path} from 'zx-extra'
import {semver, $, fs, path} from 'zx-extra'
import {log} from './log.js'
import {fetchRepo, pushCommit, getTags as getGitTags} from './git.js'
import {fetchRepo, pushCommit, getTags as getGitTags, pushTag} from './git.js'
import {fetchManifest} from './npm.js'

export const pushTag = (pkg) => ctx(async ($) => {
const {absPath: cwd, name, version, config: {gitCommitterEmail, gitCommitterName}} = pkg
export const pushReleaseTag = async (pkg) => {
const {name, version, config: {gitCommitterEmail, gitCommitterName}} = pkg
const tag = formatTag({name, version})
const cwd = pkg.context.git.root

pkg.context.git.tag = tag
log({pkg})(`push release tag ${tag}`)

$.cwd = cwd
await $`git config user.name ${gitCommitterName}`
await $`git config user.email ${gitCommitterEmail}`
await $`git tag -m ${tag} ${tag}`
await $`git push origin ${tag}`
})
await pushTag({cwd, tag, gitCommitterEmail, gitCommitterName})
}

export const pushMeta = queuefy(async (pkg) => {
log({pkg})('push artifact to branch \'meta\'')
Expand Down
8 changes: 5 additions & 3 deletions src/main/js/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {pushChangelog} from './changelog.js'
import {getPkgConfig} from './config.js'
import {topo, traverseDeps, traverseQueue} from './deps.js'
import {ghPages, ghRelease} from './gh.js'
import {getRoot, getSha} from './git.js'
import {getRoot, getSha, unsetUserConfig} from './git.js'
import {log, createReport} from './log.js'
import {getLatest, pushMeta, pushTag} from './meta.js'
import {getLatest, pushMeta, pushReleaseTag} from './meta.js'
import {fetchPkg, npmPublish} from './npm.js'
import {memoizeBy, tpl} from './util.js'

Expand Down Expand Up @@ -62,6 +62,8 @@ export const run = async ({cwd = process.cwd(), env, flags = {}} = {}) => within
.set('error', e)
.setStatus('failure')
throw e
} finally {
await unsetUserConfig(cwd)
}
report
.setStatus('success')
Expand Down Expand Up @@ -140,7 +142,7 @@ const publish = memoizeBy(async (pkg, run = runCmd) => within(async () => {
}

fs.writeJsonSync(pkg.manifestPath, pkg.manifest, {spaces: 2})
await pushTag(pkg)
await pushReleaseTag(pkg)

await Promise.all([
pushMeta(pkg),
Expand Down

0 comments on commit b46bec0

Please sign in to comment.