Skip to content

Commit 4d96539

Browse files
add normal and bare repos
1 parent e97e255 commit 4d96539

4 files changed

Lines changed: 67 additions & 140 deletions

File tree

‎GIT_USAGE.md‎

Lines changed: 0 additions & 130 deletions
This file was deleted.

‎index.js‎

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,20 +107,72 @@ function removeInvite (pubkey) {
107107
* @returns {boolean} True if the request was handled, false otherwise
108108
*/
109109
function handleGitRequest (req, res, rootDir, urlPath) {
110-
// We only intercept URLs that *begin* with "/something.git"
111-
const match = urlPath.match(/^\/([^/]+\.git)(\/.*)?$/);
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(/^\/([^/]+\.git)(\/.*)?$/);
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(/^\/([^/]+\.git)(\/.*)?$/);
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);

‎package-lock.json‎

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nosdav-server",
3-
"version": "0.0.59",
3+
"version": "0.0.60",
44
"description": "nosdav server",
55
"main": "index.js",
66
"type": "module",

0 commit comments

Comments
 (0)