@@ -13,10 +13,68 @@ class IntegrateCommand: CommandProtocol {
1313
1414 var description : String = """
1515Add hook to current repository for checking commit messages
16- Add command to your system for creating beautiful commit (`git cmt `)
16+ Add command to your system for creating beautiful commit (`gitcom `)
1717"""
18+
19+ var commitMessageHook : String = """
20+ export PATH= " /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin "
21+ message=`cat $1`
22+ messageWithNewLines=${message//|/ " \\ n " }
23+ gitcom validate " $message "
24+ """
25+
26+ private func copyExecutableToBinFolder( ) {
27+ let executableURLinBin = URL ( fileURLWithPath: " /usr/local/bin " + Constants. executableNamePath)
28+ let currentDirPath = FileManager . default. currentDirectoryPath
29+ let executableURL = URL ( fileURLWithPath: currentDirPath + Constants. executableNamePath)
30+ do {
31+ if FileManager . default. fileExists ( atPath: executableURLinBin. path) {
32+ try FileManager . default. removeItem ( at: executableURLinBin)
33+ }
34+ try FileManager . default. copyItem ( at: executableURL, to: executableURLinBin)
35+ }
36+ catch let error {
37+ print ( error: error. localizedDescription)
38+ exit ( 1 )
39+ }
40+ }
41+
42+ private func changeCommitMessageFile( ) {
43+ let commitMessageSampleFilePath = FileManager . default. currentDirectoryPath + " /.git/hooks/ " + Constants. gitCommitMsgHookSampleFileName
44+ let commitMessageHookFilePath = FileManager . default. currentDirectoryPath + " /.git/hooks/ " + Constants. gitCommitMsgHookFileName
45+ do {
46+ if FileManager . default. fileExists ( atPath: commitMessageHookFilePath) {
47+ print ( error: " \( Constants . gitCommitMsgHookFileName) already exists. " )
48+ exit ( 1 )
49+ }
50+ try FileManager . default. moveItem ( atPath: commitMessageSampleFilePath, toPath: commitMessageHookFilePath)
51+ appendValidationHook ( sampleFilePath: commitMessageSampleFilePath, hookFilePath: commitMessageHookFilePath)
52+ }
53+ catch let error {
54+ print ( error: error. localizedDescription)
55+ exit ( 1 )
56+ }
57+ }
58+
59+ private func appendValidationHook( sampleFilePath: String , hookFilePath: String ) {
60+ guard let data = commitMessageHook. data ( using: . utf8, allowLossyConversion: false ) else { return }
61+
62+ if FileManager . default. fileExists ( atPath: hookFilePath) {
63+ if let fileHandle = FileHandle ( forWritingAtPath: hookFilePath) {
64+ fileHandle. seekToEndOfFile ( )
65+ fileHandle. write ( data)
66+ fileHandle. closeFile ( )
67+ print ( " \( " Integration completed successfully. " . color ( . green) ) \( " " . color ( . default) ) " )
68+ }
69+ else {
70+ print ( error: " Can't open file \( hookFilePath) " )
71+ }
72+ }
73+ }
74+
1875 func perform( arguments: [ String ] ) {
19- print ( error: " NOT READY " )
76+ copyExecutableToBinFolder ( )
77+ changeCommitMessageFile ( )
2078 }
2179
2280}
0 commit comments