@@ -107,20 +107,72 @@ function removeInvite (pubkey) {
107107 * @returns {boolean } True if the request was handled, false otherwise
108108 */
109109function handleGitRequest ( req , res , rootDir , urlPath ) {
110- // We only intercept URLs that *begin* with "/something.git"
111- const match = urlPath . match ( / ^ \/ ( [ ^ / ] + \. g i t ) ( \/ .* ) ? $ / ) ;
112- if ( ! match ) return false ; // not a Git path → fall through
110+ // Check for git service requests (info/refs, git-upload-pack, git-receive-pack)
111+ const isGitService = urlPath . includes ( '/info/refs' ) ||
112+ urlPath . includes ( '/git-upload-pack' ) ||
113+ urlPath . includes ( '/git-receive-pack' ) ;
114+
115+ if ( ! isGitService ) {
116+ // Also check for URLs ending with .git
117+ const gitRepoMatch = urlPath . match ( / ^ \/ ( [ ^ / ] + \. g i t ) ( \/ .* ) ? $ / ) ;
118+ if ( ! gitRepoMatch ) return false ; // not a Git path → fall through
119+ }
120+
121+ // Extract repository path - handle both regular directories and .git suffixed ones
122+ let repoRelative , actualUrlPath ;
123+
124+ if ( urlPath . includes ( '.git' ) ) {
125+ // Handle .git suffixed URLs
126+ const match = urlPath . match ( / ^ \/ ( [ ^ / ] + \. g i t ) ( \/ .* ) ? $ / ) ;
127+ if ( match ) {
128+ repoRelative = match [ 1 ] ; // "my-repo.git"
129+ actualUrlPath = urlPath ;
130+ } else {
131+ return false ;
132+ }
133+ } else {
134+ // Handle regular directory URLs with git services
135+ const pathParts = urlPath . split ( '/' ) . filter ( part => part ) ;
136+ if ( pathParts . length < 2 ) return false ;
137+
138+ repoRelative = pathParts [ 0 ] ; // The directory name
139+ actualUrlPath = urlPath ;
140+ }
113141
114- const repoRelative = match [ 1 ] ; // "my-repo.git"
115142 const repoAbs = path . join ( rootDir , repoRelative ) ;
116143
117- // Does the requested repo actually exist on disk?
118- if ( ! fs . existsSync ( repoAbs ) || ! fs . statSync ( repoAbs ) . isDirectory ( ) ) {
144+ // Check if this is a git repository (either bare or regular)
145+ let gitDir = repoAbs ;
146+ let isRegularRepo = false ;
147+
148+ if ( ! fs . existsSync ( repoAbs ) ) {
149+ res . statusCode = 404 ;
150+ res . end ( 'Repository not found' ) ;
151+ return true ;
152+ }
153+
154+ if ( ! fs . statSync ( repoAbs ) . isDirectory ( ) ) {
119155 res . statusCode = 404 ;
120156 res . end ( 'Repository not found' ) ;
121157 return true ;
122158 }
123159
160+ // Check if it's a regular repo with .git subdirectory
161+ const dotGitPath = path . join ( repoAbs , '.git' ) ;
162+ if ( fs . existsSync ( dotGitPath ) && fs . statSync ( dotGitPath ) . isDirectory ( ) ) {
163+ gitDir = dotGitPath ;
164+ isRegularRepo = true ;
165+ } else {
166+ // Check if it's a bare repository by looking for git objects
167+ const objectsPath = path . join ( repoAbs , 'objects' ) ;
168+ const refsPath = path . join ( repoAbs , 'refs' ) ;
169+ if ( ! fs . existsSync ( objectsPath ) || ! fs . existsSync ( refsPath ) ) {
170+ res . statusCode = 404 ;
171+ res . end ( 'Not a git repository' ) ;
172+ return true ;
173+ }
174+ }
175+
124176 /* Each Git request (info/refs, git-upload-pack, git-receive-pack, etc.) is
125177 delegated to `git http-backend`, exactly the same CGI
126178 program Apache/Nginx use. */
@@ -129,14 +181,19 @@ function handleGitRequest (req, res, rootDir, urlPath) {
129181 GIT_PROJECT_ROOT : rootDir ,
130182 GIT_HTTP_EXPORT_ALL : '' , // allow read-only
131183 GIT_HTTP_RECEIVE_PACK : 'true' , // enable push support
132- PATH_INFO : urlPath ,
184+ PATH_INFO : actualUrlPath ,
133185 REQUEST_METHOD : req . method ,
134186 CONTENT_TYPE : req . headers [ 'content-type' ] || '' ,
135187 QUERY_STRING : req . url . split ( '?' ) [ 1 ] || '' ,
136188 REMOTE_USER : '' , // anonymous
137189 CONTENT_LENGTH : req . headers [ 'content-length' ] || '0' ,
138190 } ;
139191
192+ // For regular repositories, set GIT_DIR to point to the .git directory
193+ if ( isRegularRepo ) {
194+ env . GIT_DIR = gitDir ;
195+ }
196+
140197 const child = spawn ( 'git' , [ 'http-backend' ] , { env } ) ;
141198
142199 let buffer = Buffer . alloc ( 0 ) ;
0 commit comments