@@ -116,6 +116,40 @@ module.exports = {
116
116
ctx . body = { message : err . message , ...err }
117
117
}
118
118
} ,
119
+ fileDetail : async ( ctx ) => {
120
+ const { owner, repo, ref } = ctx . params ;
121
+ const { reposPath } = ctx . state . conf ;
122
+ const currentRepoPath = PATH . join ( reposPath , owner , `${ repo } .git` ) ;
123
+ const filePath = ctx . params [ 0 ] ;
124
+ try {
125
+ const porps = { } ;
126
+ const gitRepo = await Git . Repository . open ( currentRepoPath ) ;
127
+ const reqRef = await gitRepo . getReference ( ref ) ;
128
+ const obj = await reqRef . peel ( Git . Object . TYPE . COMMIT ) ;
129
+ const commitlookup = await Git . Commit . lookup ( gitRepo , obj . id ( ) ) ;
130
+ const tree = await commitlookup . getTree ( ) ;
131
+ const entry = await tree . getEntry ( filePath ) ;
132
+ porps . id = entry . oid ( ) ;
133
+ porps . ref = reqRef . name ( ) ;
134
+
135
+ const blob = await entry . getBlob ( )
136
+ ctx . body = {
137
+ ...porps ,
138
+ content : blob . toString ( ) ,
139
+ parsePath : PATH . parse ( filePath ) ,
140
+ path : filePath ,
141
+ has : blob . id ,
142
+ refName : blob . id ,
143
+ filemode : blob . filemode ( ) ,
144
+ rawsize : blob . rawsize ( ) ,
145
+ }
146
+ blob . free ( ) ;
147
+ tree . free ( ) ;
148
+ } catch ( err ) {
149
+ ctx . response . status = err . statusCode || err . status || 500 ;
150
+ ctx . body = { message : err . message , ...err }
151
+ }
152
+ } ,
119
153
reposTree : async ( ctx ) => {
120
154
const { id } = ctx . params ;
121
155
const { userInfo } = ctx . session ;
@@ -142,7 +176,7 @@ module.exports = {
142
176
const { reposPath } = ctx . state . conf ;
143
177
const currentRepoPath = PATH . join ( reposPath , projects . namespace . name , `${ projects . name } .git` ) ;
144
178
const gitRepo = await Git . Repository . open ( currentRepoPath ) ;
145
-
179
+
146
180
// 空仓库返回 README.md 说明内容
147
181
let emptyRepoReadme = await readFile ( PATH . join ( __dirname , 'EmptyRepo.md' ) ) ;
148
182
if ( gitRepo . isEmpty ( ) === 1 ) {
@@ -184,14 +218,33 @@ module.exports = {
184
218
185
219
// 参数 path 处理,存储库中的路径
186
220
if ( ctx . query . path ) {
221
+ body . parsePath = PATH . parse ( ctx . query . path ) ;
187
222
let treeObj = await commit . getEntry ( ctx . query . path ) ;
188
223
if ( treeObj . isFile ( ) ) {
189
- body . isFile = treeObj . isFile ( ) ;
190
- treeObj = await treeObj . getBlob ( ) ;
224
+ // try {
225
+ // // repo.getCommit("bf1da765e357a9b936d6d511f2c7b78e0de53632");
226
+ // // const commit = await Git.Commit.lookup(gitRepo, treeObj.oid())
227
+ // const commits = await gitRepo.getCommit(treeObj.oid())
228
+ // console.log('entry:', commits.__proto__)
229
+ // } catch (error) {
230
+ // console.log('error:', error)
231
+ // }
232
+ const entry = await commit . getEntry ( treeObj . path ( ) ) ;
233
+ const local = await treeObj . toObject ( gitRepo )
234
+ // const aa = await local.lookupByPath(treeObj.path(), Git.Object.TYPE.BLOB)
235
+ // console.log('treeObj:', treeObj.__proto__)
236
+ // console.log('local:', local.__proto__)
237
+ // console.log('local:', local.owner())
238
+ // console.log('local:', local.lookupByPath(treeObj.path(), Git.Object.TYPE.BLOB))
239
+ // console.log('treeObj:', treeObj.path())
240
+ const blob = await Git . Blob . lookup ( gitRepo , treeObj . oid ( ) ) ;
241
+ body . rawsize = blob . rawsize ( ) ;
242
+ body . readmeContent = blob . content ( ) . toString ( ) ;
243
+ body . isFile = true ;
191
244
body . path = ctx . query . path ;
192
245
body . tree = [ ] ;
193
- body . readmeContent = treeObj . toString ( ) ;
194
246
ctx . body = body ;
247
+ blob . free ( )
195
248
return ;
196
249
}
197
250
treeObj = await treeObj . getTree ( treeObj . sha ( ) ) ;
0 commit comments