@@ -3,7 +3,7 @@ const sgit = require('simple-git/promise')
3
3
const fs = require ( 'fs-extra' )
4
4
const _ = require ( 'lodash' )
5
5
6
- const repoPath = path . join ( WIKI . ROOTPATH , 'data/repo' )
6
+ let repoPath = path . join ( process . cwd ( ) , 'data/repo' )
7
7
8
8
/**
9
9
* Get file extension based on content type
@@ -50,37 +50,87 @@ module.exports = {
50
50
51
51
} ,
52
52
async init ( ) {
53
+ WIKI . logger . info ( '(STORAGE/GIT) Initializing...' )
54
+ repoPath = path . resolve ( WIKI . ROOTPATH , this . config . localRepoPath )
53
55
await fs . ensureDir ( repoPath )
54
56
const git = sgit ( repoPath )
55
57
56
58
// Initialize repo (if needed)
59
+ WIKI . logger . info ( '(STORAGE/GIT) Checking repository state...' )
57
60
const isRepo = await git . checkIsRepo ( )
58
61
if ( ! isRepo ) {
62
+ WIKI . logger . info ( '(STORAGE/GIT) Initializing local repository...' )
59
63
await git . init ( )
60
64
}
61
65
62
- // Checkout branch
63
- await git . checkout ( this . config . branch )
66
+ // Set default author
67
+ await git . raw ( [ 'config' , '--local' , 'user.email' , this . config . defaultEmail ] )
68
+ await git . raw ( [ 'config' , '--local' , 'user.name' , this . config . defaultName ] )
69
+
70
+ // Purge existing remotes
71
+ WIKI . logger . info ( '(STORAGE/GIT) Listing existing remotes...' )
72
+ const remotes = await git . getRemotes ( )
73
+ if ( remotes . length > 0 ) {
74
+ WIKI . logger . info ( '(STORAGE/GIT) Purging existing remotes...' )
75
+ for ( let remote of remotes ) {
76
+ await git . removeRemote ( remote . name )
77
+ }
78
+ }
64
79
65
80
// Add remote
81
+ WIKI . logger . info ( '(STORAGE/GIT) Setting SSL Verification config...' )
66
82
await git . raw ( [ 'config' , '--local' , '--bool' , 'http.sslVerify' , _ . toString ( this . config . verifySSL ) ] )
67
83
switch ( this . config . authType ) {
68
84
case 'ssh' :
85
+ WIKI . logger . info ( '(STORAGE/GIT) Setting SSH Command config...' )
69
86
await git . addConfig ( 'core.sshCommand' , `ssh -i "${ this . config . sshPrivateKeyPath } " -o StrictHostKeyChecking=no` )
87
+ WIKI . logger . info ( '(STORAGE/GIT) Adding origin remote via SSH...' )
70
88
await git . addRemote ( 'origin' , this . config . repoUrl )
71
89
break
72
90
default :
91
+ WIKI . logger . info ( '(STORAGE/GIT) Adding origin remote via HTTPS...' )
73
92
await git . addRemote ( 'origin' , `https://${ this . config . basicUsername } :${ this . config . basicPassword } @${ this . config . repoUrl } ` )
74
93
break
75
94
}
95
+
96
+ // Fetch updates for remote
97
+ WIKI . logger . info ( '(STORAGE/GIT) Fetch updates from remote...' )
98
+ await git . raw ( [ 'remote' , 'update' , 'origin' ] )
99
+
100
+ // Checkout branch
101
+ const branches = await git . branch ( )
102
+ if ( ! _ . includes ( branches . all , this . config . branch ) && ! _ . includes ( branches . all , `remotes/origin/${ this . config . branch } ` ) ) {
103
+ throw new Error ( 'Invalid branch! Make sure it exists on the remote first.' )
104
+ }
105
+ WIKI . logger . info ( `(STORAGE/GIT) Checking out branch ${ this . config . branch } ...` )
106
+ await git . checkout ( this . config . branch )
107
+
108
+ // Pull rebase
109
+ if ( _ . includes ( [ 'sync' , 'pull' ] , this . mode ) ) {
110
+ WIKI . logger . info ( `(STORAGE/GIT) Performing pull rebase from origin on branch ${ this . config . branch } ...` )
111
+ await git . pull ( 'origin' , this . config . branch , [ '--rebase' ] )
112
+ }
113
+
114
+ // Push
115
+ if ( _ . includes ( [ 'sync' , 'push' ] , this . mode ) ) {
116
+ WIKI . logger . info ( `(STORAGE/GIT) Performing push to origin on branch ${ this . config . branch } ...` )
117
+ let pushOpts = [ '--signed=if-asked' ]
118
+ if ( this . mode === 'push' ) {
119
+ pushOpts . push ( '--force' )
120
+ }
121
+ await git . push ( 'origin' , this . config . branch , pushOpts )
122
+ }
123
+
124
+ WIKI . logger . info ( '(STORAGE/GIT) Initialization completed.' )
76
125
} ,
77
126
async created ( ) {
78
127
const fileName = `${ this . page . path } .${ getFileExtension ( this . page . contentType ) } `
79
128
const filePath = path . join ( repoPath , fileName )
80
129
await fs . outputFile ( filePath , injectMetadata ( this . page ) , 'utf8' )
81
130
82
131
const git = sgit ( repoPath )
83
- await git . add ( `./${ fileName } ` ) . commit ( `docs: create ${ this . page . path } ` , fileName , {
132
+ await git . add ( `./${ fileName } ` )
133
+ await git . commit ( `docs: create ${ this . page . path } ` , fileName , {
84
134
'--author' : `"${ this . page . authorName } <${ this . page . authorEmail } >"`
85
135
} )
86
136
} ,
@@ -90,15 +140,17 @@ module.exports = {
90
140
await fs . outputFile ( filePath , injectMetadata ( this . page ) , 'utf8' )
91
141
92
142
const git = sgit ( repoPath )
93
- await git . add ( `./${ fileName } ` ) . commit ( `docs: update ${ this . page . path } ` , fileName , {
143
+ await git . add ( `./${ fileName } ` )
144
+ await git . commit ( `docs: update ${ this . page . path } ` , fileName , {
94
145
'--author' : `"${ this . page . authorName } <${ this . page . authorEmail } >"`
95
146
} )
96
147
} ,
97
148
async deleted ( ) {
98
149
const fileName = `${ this . page . path } .${ getFileExtension ( this . page . contentType ) } `
99
150
100
151
const git = sgit ( repoPath )
101
- await git . rm ( `./${ fileName } ` ) . commit ( `docs: delete ${ this . page . path } ` , fileName , {
152
+ await git . rm ( `./${ fileName } ` )
153
+ await git . commit ( `docs: delete ${ this . page . path } ` , fileName , {
102
154
'--author' : `"${ this . page . authorName } <${ this . page . authorEmail } >"`
103
155
} )
104
156
} ,
@@ -107,7 +159,8 @@ module.exports = {
107
159
const destinationFilePath = `${ this . page . destinationPath } .${ getFileExtension ( this . page . contentType ) } `
108
160
109
161
const git = sgit ( repoPath )
110
- await git . mv ( `./${ sourceFilePath } ` , `./${ destinationFilePath } ` ) . commit ( `docs: rename ${ this . page . sourcePath } to ${ destinationFilePath } ` , destinationFilePath , {
162
+ await git . mv ( `./${ sourceFilePath } ` , `./${ destinationFilePath } ` )
163
+ await git . commit ( `docs: rename ${ this . page . sourcePath } to ${ destinationFilePath } ` , destinationFilePath , {
111
164
'--author' : `"${ this . page . authorName } <${ this . page . authorEmail } >"`
112
165
} )
113
166
}
0 commit comments