1+ const fs = require ( 'fs' ) ;
2+ const path = require ( 'path' ) ;
3+ const Git = require ( "nodegit" ) ;
4+
5+ const workdir = '/tmp' ;
6+ const biocontainers = 'https://github.com/BioContainers/containers.git' ;
7+ const bioconda = 'https://github.com/bioconda/bioconda-recipes.git' ;
8+
9+ const getRepoFiles = function ( dirPath , arrayOfFiles ) {
10+ let files = fs . readdirSync ( dirPath )
11+ arrayOfFiles = arrayOfFiles || [ ]
12+
13+ files . forEach ( function ( file ) {
14+ if ( fs . statSync ( dirPath + "/" + file ) . isDirectory ( ) ) {
15+ arrayOfFiles = getRepoFiles ( dirPath + "/" + file , arrayOfFiles )
16+ } else {
17+ let dirElt = dirPath . split ( '/' ) ;
18+ if ( file == 'Dockerfile' || ( file == 'meta.yaml' && dirElt [ dirElt . length - 2 ] == 'recipes' ) ) {
19+ arrayOfFiles . push ( path . join ( dirPath , "/" , file ) )
20+ }
21+ }
22+ } )
23+
24+ return arrayOfFiles
25+ }
26+
27+ async function repoFiles ( kind = 'biocontainers' ) {
28+ let destDir = `${ workdir } /${ kind } ` ;
29+ let lastCommit = null ;
30+ if ( fs . existsSync ( destDir ) ) {
31+ let existingRepo = await Git . Repository . open ( destDir ) ;
32+ let bc = await existingRepo . getBranchCommit ( 'master' ) ;
33+ lastCommit = bc . sha ( ) ;
34+ console . log ( 'existing, last commit' , lastCommit ) ;
35+ fs . rmSync ( destDir , { recursive : true } ) ;
36+ }
37+ let repoUrl = kind == 'biocontainers' ? biocontainers : bioconda ;
38+ await Git . Clone ( repoUrl , destDir ) ;
39+ let repo = await Git . Repository . open ( destDir ) ;
40+ let headc = await repo . getBranchCommit ( 'master' ) ;
41+ let headCommit = headc . sha ( ) ;
42+ console . log ( 'last commit' , lastCommit ) ;
43+ console . log ( 'head commit' , headCommit ) ;
44+
45+ let files = [ ] ;
46+ if ( lastCommit && lastCommit == headCommit ) {
47+ console . log ( 'nothing to do' ) ;
48+ return [ ] ;
49+ }
50+ if ( lastCommit ) {
51+ console . log ( 'get diff' ) ;
52+ let cold = await repo . getCommit ( lastCommit ) ;
53+ let coldTree = await cold . getTree ( ) ;
54+ let cnew = await repo . getCommit ( headCommit ) ;
55+ let cnewTree = await cnew . getTree ( ) ;
56+ const diff = await cnewTree . diff ( coldTree ) ;
57+ const patches = await diff . patches ( ) ;
58+
59+ for ( const patch of patches ) {
60+ files . push ( patch . newFile ( ) . path ( ) ) ;
61+ }
62+ return files ;
63+
64+ }
65+ console . log ( 'take all' ) ;
66+ let allFiles = getRepoFiles ( destDir ) ;
67+ for ( let i = 0 ; i < allFiles . length ; i ++ ) {
68+ allFiles [ i ] = allFiles [ i ] . replace ( destDir + '/' , '' ) ;
69+ }
70+ return allFiles ;
71+ }
72+
73+ repoFiles ( 'biocontainers' ) . then ( files => {
74+ console . log ( 'files' , files . length , files [ 0 ] ) ;
75+ process . exit ( 0 ) ;
76+ } ) . catch ( err => {
77+ console . error ( err ) ;
78+ process . exit ( 1 ) ;
79+ } )
0 commit comments