@@ -4,38 +4,7 @@ const FS = require('fs-extra')
4
4
const Models = require ( '../../../../conf/sequelize' ) ;
5
5
const { readFile } = require ( '../../../utils/fsExtra' ) ;
6
6
7
- /**
8
- * 获取目录
9
- * @param {Object } treeWalker
10
- * @param {Boolean } [recursive=false] 以广度优先顺序递归地遍历树。
11
- */
12
- const getFiles = ( treeWalker , recursive = false ) => {
13
- const trees = [ ] ;
14
- return new Promise ( ( resolve , reject ) => {
15
- treeWalker . on ( "entry" , ( entry ) => {
16
- let type = '' ;
17
- if ( entry . isBlob ( ) ) type = 'blob' ;
18
- if ( entry . isDirectory ( ) ) type = 'tree' ;
19
- if ( entry . isSubmodule ( ) ) type = 'commit' ;
20
- trees . push ( {
21
- id : entry . sha ( ) ,
22
- name : entry . name ( ) ,
23
- path : entry . path ( ) ,
24
- filemodeRaw : entry . filemodeRaw ( ) ,
25
- mode : entry . filemode ( ) ,
26
- type,
27
- } ) ;
28
- } ) ;
29
- treeWalker . on ( "end" , ( entries ) => {
30
- if ( recursive ) resolve ( trees ) ;
31
- } ) ;
32
- treeWalker . on ( "error" , ( error ) => {
33
- reject ( error )
34
- } ) ;
35
- treeWalker . start ( ) ;
36
- if ( ! recursive ) resolve ( trees ) ;
37
- } ) ;
38
- }
7
+ const { getFiles } = require ( './util' ) ;
39
8
40
9
module . exports = {
41
10
created : async ( ctx ) => {
@@ -134,23 +103,9 @@ module.exports = {
134
103
readme : async ( ctx ) => {
135
104
const { owner, repo } = ctx . params ;
136
105
const { reposPath } = ctx . state . conf ;
137
- const { userInfo } = ctx . session ;
138
106
const currentRepoPath = PATH . join ( reposPath , owner , `${ repo } .git` ) ;
139
107
try {
140
108
const gitRepo = await Git . Repository . open ( currentRepoPath ) ;
141
- let emptyRepoReadme = await readFile ( PATH . join ( __dirname , 'EmptyRepo.md' ) ) ;
142
- if ( gitRepo . isEmpty ( ) === 1 ) {
143
- if ( userInfo . username !== owner ) emptyRepoReadme = '' ;
144
- if ( userInfo . username === owner ) {
145
- emptyRepoReadme = emptyRepoReadme
146
- . replace ( / \{ \{ u s e r n a m e \} \} / g, owner )
147
- . replace ( / \{ \{ r e p o s \} \} / g, repo )
148
- . replace ( / \{ \{ h o s t \} \} / g, ctx . hostname )
149
- . replace ( / \{ \{ e m a i l \} \} / g, userInfo . email )
150
- }
151
- ctx . body = { content : emptyRepoReadme } ;
152
- return ;
153
- }
154
109
let refCommit = await gitRepo . getReferenceCommit ( 'refs/heads/master' ) ;
155
110
refCommit = await gitRepo . getCommit ( refCommit . sha ( ) ) ;
156
111
refCommit = await refCommit . getEntry ( "README.md" ) ;
@@ -163,7 +118,8 @@ module.exports = {
163
118
} ,
164
119
reposTree : async ( ctx ) => {
165
120
const { id } = ctx . params ;
166
- let { recursive = false , branch } = ctx . query ;
121
+ const { userInfo } = ctx . session ;
122
+ let { recursive = false , branch = 'master' } = ctx . query ;
167
123
try {
168
124
const projects = await Models . projects . findOne ( {
169
125
where : { id } ,
@@ -178,20 +134,30 @@ module.exports = {
178
134
} ) ;
179
135
if ( ! projects ) ctx . throw ( 404 , `Owner ${ id } does not exist` ) ;
180
136
137
+ const repoDetail = await Models . projects . findOne ( {
138
+ where : { id } ,
139
+ include : [ { model : Models . users , as : 'owner' , attributes : { exclude : [ 'password' ] } } ]
140
+ } ) ;
141
+
181
142
const { reposPath } = ctx . state . conf ;
182
143
const currentRepoPath = PATH . join ( reposPath , projects . namespace . name , `${ projects . name } .git` ) ;
183
144
const gitRepo = await Git . Repository . open ( currentRepoPath ) ;
145
+
146
+ // 空仓库返回 README.md 说明内容
147
+ let emptyRepoReadme = await readFile ( PATH . join ( __dirname , 'EmptyRepo.md' ) ) ;
184
148
if ( gitRepo . isEmpty ( ) === 1 ) {
185
- ctx . body = { tree : [ ] } ;
149
+ if ( userInfo . username !== repoDetail . owner . username ) emptyRepoReadme = '' ;
150
+ if ( userInfo . username === repoDetail . owner . username ) {
151
+ emptyRepoReadme = emptyRepoReadme
152
+ . replace ( / \{ \{ u s e r n a m e \} \} / g, repoDetail . owner . username )
153
+ . replace ( / \{ \{ r e p o s \} \} / g, repoDetail . name )
154
+ . replace ( / \{ \{ h o s t \} \} / g, ctx . hostname )
155
+ . replace ( / \{ \{ e m a i l \} \} / g, userInfo . email )
156
+ }
157
+ ctx . body = { tree : [ ] , readmeContent : emptyRepoReadme } ;
186
158
return ;
187
159
}
188
- let commit ;
189
- if ( branch ) {
190
- commit = await gitRepo . getReferenceCommit ( branch ) ;
191
- } else {
192
- commit = await gitRepo . getMasterCommit ( ) ;
193
- }
194
-
160
+ let commit = await gitRepo . getReferenceCommit ( branch ) ;
195
161
const body = { }
196
162
body . sha = commit . sha ( ) ;
197
163
// body.toString = commit.toString();
@@ -210,22 +176,43 @@ module.exports = {
210
176
body . date = commit . date ( ) ;
211
177
body . timeMs = commit . timeMs ( ) ;
212
178
body . timeOffset = commit . timeOffset ( ) ;
179
+ body . isFile = false ;
213
180
214
181
// const treeId = Git.Oid.fromString('5d08433fcb6ecbfe3d1709013452f3d41ffa1ced');
215
182
const treeId = Git . Oid . fromString ( body . sha ) ;
216
183
commit = await commit . getTree ( treeId ) ;
217
184
218
185
// 参数 path 处理,存储库中的路径
219
186
if ( ctx . query . path ) {
220
- let dirTree = await commit . getEntry ( ctx . query . path ) ;
221
- dirTree = await dirTree . getTree ( dirTree . sha ( ) ) ;
222
- commit = dirTree ;
187
+ let treeObj = await commit . getEntry ( ctx . query . path ) ;
188
+ if ( treeObj . isFile ( ) ) {
189
+ body . isFile = treeObj . isFile ( ) ;
190
+ treeObj = await treeObj . getBlob ( ) ;
191
+ body . path = ctx . query . path ;
192
+ body . tree = [ ] ;
193
+ body . readmeContent = treeObj . toString ( ) ;
194
+ ctx . body = body ;
195
+ return ;
196
+ }
197
+ treeObj = await treeObj . getTree ( treeObj . sha ( ) ) ;
198
+ commit = treeObj ;
223
199
}
224
200
225
201
body . path = commit . path ( ) ;
226
202
body . entryCount = commit . entryCount ( ) ;
203
+ body . readmeContent = '' ; // 默认目录下的 README.md 文件内容为空
227
204
commit = await commit . walk ( recursive ) ;
228
- body . tree = await getFiles ( commit , recursive ) ;
205
+ const treeArray = await getFiles ( commit , recursive ) ;
206
+ // 过滤 entry 对象
207
+ const oldTree = [ ...treeArray ] . map ( ( { entry, ...otherProps } ) => otherProps ) ;
208
+ // 读取默认目录中的 README.md 文件内容
209
+ let readme = treeArray . filter ( item => item . type === 'blob' && / r e a d m e .m d $ / . test ( item . path . toLocaleLowerCase ( ) ) ) ;
210
+ if ( readme && readme . length > 0 ) {
211
+ readme = readme [ 0 ] ;
212
+ const blob = await readme . entry . getBlob ( ) ;
213
+ body . readmeContent = await blob . toString ( ) ;
214
+ }
215
+ body . tree = [ ...oldTree ] ;
229
216
ctx . body = body ;
230
217
} catch ( err ) {
231
218
ctx . response . status = err . statusCode || err . status || 500 ;
0 commit comments