Skip to content

Commit

Permalink
fix: lint-all better change tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Jul 22, 2024
1 parent 50334bf commit 4714798
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 34 deletions.
2 changes: 0 additions & 2 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#!/bin/sh
[ -n "$CI" ] && exit 0
. "$(dirname "$0")/_/husky.sh"

yarn commitlint-def $1
# exit 1 # uncomment to debug
2 changes: 0 additions & 2 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/bin/sh
[ -n "$CI" ] && exit 0
. "$(dirname "$0")/_/husky.sh"

yarn lint-staged-def
50 changes: 20 additions & 30 deletions src/cmd/lint-all.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
execVoidCommandSync,
getLastGitCommitMsg,
gitCommitAll,
gitHasUncommittedChanges,
gitPull,
gitPush,
} from '@naturalcycles/nodejs-lib'
Expand All @@ -33,12 +32,10 @@ export async function lintAllCommand(): Promise<void> {
},
}).argv

// todo: produce a cleaner "list of changed files"
let gitStatusAtStart: string | undefined
const hadChangesBefore = gitHasUncommittedChanges()
if ((commitOnChanges || failOnChanges) && hadChangesBefore) {
const needToTrackChanges = commitOnChanges || failOnChanges
const gitStatusAtStart = gitStatus()
if (needToTrackChanges && gitStatusAtStart) {
console.log('lint-all: git shows changes before run:')
gitStatusAtStart = gitStatus()
console.log(gitStatusAtStart)
}

Expand All @@ -60,29 +57,22 @@ export async function lintAllCommand(): Promise<void> {

console.log(`${boldGrey('lint-all')} ${dimGrey(`took ` + _since(started))}`)

if (commitOnChanges || failOnChanges) {
// detect changes
const hasChanges = gitHasUncommittedChanges()
if (hasChanges) {
const gitStatusAfter = gitStatus()
if (gitStatusAfter === gitStatusAtStart) {
console.log(`lint-all: git status is the same as before the run, will not commit`)
} else {
const msg =
'style(ci): ' + _truncate(commitMessageToTitleMessage(getLastGitCommitMsg()), 60)

// pull, commit, push changes
gitPull()
gitCommitAll(msg)
gitPush()
}

// fail on changes
if (failOnChanges) {
console.log(gitStatusAfter)
console.log('lint-all failOnChanges: exiting with status 1')
process.exitCode = 1
}
if (needToTrackChanges) {
const gitStatusAfter = gitStatus()
const hasChanges = gitStatusAfter !== gitStatusAtStart
if (!hasChanges) return
const msg = 'style(ci): ' + _truncate(commitMessageToTitleMessage(getLastGitCommitMsg()), 60)

// pull, commit, push changes
gitPull()
gitCommitAll(msg)
gitPush()

// fail on changes
if (failOnChanges) {
console.log(gitStatusAfter)
console.log('lint-all failOnChanges: exiting with status 1')
process.exitCode = 1
}
}
}
Expand Down Expand Up @@ -120,7 +110,7 @@ function canRunBinary(name: string): boolean {

function gitStatus(): string | undefined {
try {
return execSync('git status', {
return execSync('git status -s', {
encoding: 'utf8',
})
} catch {}
Expand Down

0 comments on commit 4714798

Please sign in to comment.