Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
G
git-hooks-test
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
shangbj
git-hooks-test
Commits
c738610d
Commit
c738610d
authored
Mar 15, 2020
by
shangbj
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update
parent
5b194d0c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
28 deletions
+52
-28
commit-hook.js
build/commit-hook.js
+51
-27
package.json
package.json
+1
-1
No files found.
build/commit-hook.js
View file @
c738610d
const
fs
=
require
(
'fs'
)
const
path
=
require
(
'path'
)
const
chalk
=
require
(
'chalk'
)
const
logSymbols
=
require
(
'log-symbols'
);
console
.
log
(
logSymbols
.
success
,
chalk
.
red
(
'Hello world!'
));
// process.exit(1)
// const execSync = require('child_process').execSync
// // git 对所有冲突的地方都会生成下面这种格式的信息,所以写个检测冲突文件的正则
// const isConflictRegular = "^<<<<<<<\\s|^=======$|^>>>>>>>\\s"
// let results
// try {
// // git grep 命令会执行 perl 的正则匹配所有满足冲突条件的文件
// results = execSync(`git grep -n -P "${isConflictRegular}"`, {encoding: 'utf-8'})
// } catch (e) {
// console.log('没有发现冲突,等待 commit')
// process.exit(0)
// }
// if(results) {
// console.error('发现冲突,请解决后再提交,冲突文件:')
// console.error(results.trim())
// process.exit(1)
// }
const
logSymbols
=
require
(
'log-symbols'
)
const
gitPath
=
path
.
join
(
process
.
cwd
(),
process
.
env
.
HUSKY_GIT_PARAMS
);
const
mergeMsgPath
=
path
.
join
(
gitPath
,
'../'
,
'MERGE_MSG'
)
function
mergeCheck
(){
const
mergeMsg
=
fs
.
readFileSync
(
mergeMsgPath
,
'utf-8'
);
console
.
log
(
mergeMsg
)
}
function
commitMsgCheck
(){
const
commitMsg
=
fs
.
readFileSync
(
gitPath
,
'utf-8'
);
if
(
!
commitMsg
)
{
console
.
log
(
logSymbols
.
error
,
chalk
.
red
(
'no message'
))
process
.
exit
(
1
)
}
}
function
conflictCheck
()
{
console
.
log
(
logSymbols
.
info
,
chalk
.
blue
(
'开始冲突检测...'
))
const
execSync
=
require
(
'child_process'
).
execSync
// git 对所有冲突的地方都会生成下面这种格式的信息,所以写个检测冲突文件的正则
const
isConflictRegular
=
"^<<<<<<<
\\
s|^=======$|^>>>>>>>
\\
s"
let
results
try
{
// git grep 命令会执行 perl 的正则匹配所有满足冲突条件的文件
results
=
execSync
(
`git grep -n -P "
${
isConflictRegular
}
"`
,
{
encoding
:
'utf-8'
})
}
catch
(
e
)
{
process
.
exit
(
0
)
}
if
(
results
)
{
console
.
log
(
logSymbols
.
error
,
chalk
.
red
(
'发现冲突,请解决后再提交,冲突文件:'
))
console
.
log
(
results
.
trim
())
process
.
exit
(
1
)
}
}
if
(
fs
.
existsSync
(
mergeMsgPath
)){
mergeCheck
()
}
else
{
commitMsgCheck
()
}
conflictCheck
()
process
.
exit
(
0
)
\ No newline at end of file
package.json
View file @
c738610d
...
...
@@ -12,7 +12,7 @@
},
"husky"
:
{
"hooks"
:
{
"commit-msg"
:
"node ./build/commit-hook.js"
"commit-msg"
:
"node ./build/commit-hook.js
$HUSKY_GIT_PARAMS
"
}
},
"keywords"
:
[],
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment