1
- import { GitRepository , AppContext } from './types.js' ;
2
1
import { simpleGit , SimpleGitProgressEvent } from 'simple-git' ;
3
- import { createLogger } from './logger.js' ;
4
- import { GitConfig } from "@sourcebot/schemas/v2/index.type"
5
- import path from 'path' ;
6
-
7
- const logger = createLogger ( 'git' ) ;
8
2
9
3
export const cloneRepository = async ( cloneURL : string , path : string , gitConfig ?: Record < string , string > , onProgress ?: ( event : SimpleGitProgressEvent ) => void ) => {
10
4
const git = simpleGit ( {
@@ -45,80 +39,3 @@ export const fetchRepository = async (path: string, onProgress?: (event: SimpleG
45
39
]
46
40
) ;
47
41
}
48
-
49
- const isValidGitRepo = async ( url : string ) : Promise < boolean > => {
50
- const git = simpleGit ( ) ;
51
- try {
52
- await git . listRemote ( [ url ] ) ;
53
- return true ;
54
- } catch ( error ) {
55
- logger . debug ( `Error checking if ${ url } is a valid git repo: ${ error } ` ) ;
56
- return false ;
57
- }
58
- }
59
-
60
- const stripProtocolAndGitSuffix = ( url : string ) : string => {
61
- return url . replace ( / ^ [ a - z A - Z ] + : \/ \/ / , '' ) . replace ( / \. g i t $ / , '' ) ;
62
- }
63
-
64
- const getRepoNameFromUrl = ( url : string ) : string => {
65
- const strippedUrl = stripProtocolAndGitSuffix ( url ) ;
66
- return strippedUrl . split ( '/' ) . slice ( - 2 ) . join ( '/' ) ;
67
- }
68
-
69
- export const getGitRepoFromConfig = async ( config : GitConfig , ctx : AppContext ) => {
70
- const repoValid = await isValidGitRepo ( config . url ) ;
71
- if ( ! repoValid ) {
72
- logger . error ( `Git repo provided in config with url ${ config . url } is not valid` ) ;
73
- return null ;
74
- }
75
-
76
- const cloneUrl = config . url ;
77
- const repoId = stripProtocolAndGitSuffix ( cloneUrl ) ;
78
- const repoName = getRepoNameFromUrl ( config . url ) ;
79
- const repoPath = path . resolve ( path . join ( ctx . reposPath , `${ repoId } .git` ) ) ;
80
- const repo : GitRepository = {
81
- vcs : 'git' ,
82
- id : repoId ,
83
- name : repoName ,
84
- path : repoPath ,
85
- isStale : false ,
86
- cloneUrl : cloneUrl ,
87
- branches : [ ] ,
88
- tags : [ ] ,
89
- }
90
-
91
- if ( config . revisions ) {
92
- if ( config . revisions . branches ) {
93
- const branchGlobs = config . revisions . branches ;
94
- const git = simpleGit ( ) ;
95
- const branchList = await git . listRemote ( [ '--heads' , cloneUrl ] ) ;
96
- const branches = branchList
97
- . split ( '\n' )
98
- . map ( line => line . split ( '\t' ) [ 1 ] )
99
- . filter ( Boolean )
100
- . map ( branch => branch . replace ( 'refs/heads/' , '' ) ) ;
101
-
102
- repo . branches = branches . filter ( branch =>
103
- branchGlobs . some ( glob => new RegExp ( glob ) . test ( branch ) )
104
- ) ;
105
- }
106
-
107
- if ( config . revisions . tags ) {
108
- const tagGlobs = config . revisions . tags ;
109
- const git = simpleGit ( ) ;
110
- const tagList = await git . listRemote ( [ '--tags' , cloneUrl ] ) ;
111
- const tags = tagList
112
- . split ( '\n' )
113
- . map ( line => line . split ( '\t' ) [ 1 ] )
114
- . filter ( Boolean )
115
- . map ( tag => tag . replace ( 'refs/tags/' , '' ) ) ;
116
-
117
- repo . tags = tags . filter ( tag =>
118
- tagGlobs . some ( glob => new RegExp ( glob ) . test ( tag ) )
119
- ) ;
120
- }
121
- }
122
-
123
- return repo ;
124
- }
0 commit comments