gabrielcsapo / node-git-server

🎡 A configurable git server written in Node.js
https://gabrielcsapo.github.io/node-git-server
MIT License
253 stars 73 forks source link

How to collect repositories by user folders? #112

Closed Denis-Zhukov closed 1 year ago

Denis-Zhukov commented 1 year ago

Hello. How to collect repositories by user folders? I use node-git-server and express together. Code:

import express from "express";
import path from "path";
import {Git} from 'node-git-server';

const __dirname = path.resolve();
const reposPath = path.join(__dirname, 'repos');

const app = express();

const repos = new Git(reposPath, {
    autoCreate: true,
});

repos.on('push', (push) => {
    console.log(`push ${push.repo}/${push.commit} ( ${push.branch} )`);
    push.accept();
});

repos.on('fetch', (fetch) => {
    console.log(`fetch ${fetch.commit}`);
    fetch.accept();
});

app.use((req, res, next) => {
    console.log(req.url)
    next();
})

app.use("/:account/:repo", (req, res) => {
    repos.handle(req, res);
})

app.listen(8000, () => {
    console.log(`Express http server listening`);
});

It doesn't work. image The request comes and goes to handle, but something goes wrong there?

It works:

app.use("/:repo", (req, res) => {
    repos.handle(req, res);
})
git push http://localhost:8000/git/beep master

But it without account name and url has superfluous endpoint "/git" I will add the user to the path without changing the endpoint

git push http://localhost:8000/git/somebody/beep master

image It works! But "/git" need it. Lets try delete it.

git push http://localhost:8000/somebody/beep master

Now repo inside repos, not inside the user folder. It's bad ( image

Lets try change endpoint:

app.use("/:account/:repo", (req, res) => {
    repos.handle(req, res);
})
git push http://localhost:8000/somebody/beep master

It does't work (

It doens't work too:

git push http://localhost:8000/git/somebody/beep master

Help me please. I want to make a git server where the user will be. And user repositories should be in folders with their names.

Denis-Zhukov commented 1 year ago

Use without endpoint:

app.use((req, res) => {
    repos.handle(req, res);
})
git push http://localhost:8000/somebody/beep master

It works. But... image 2 folders are created. One is empty. And the global endpoint is not very suitable

Also I try use this endpoint:

app.use("/git/:repo", (req, res) => {
    repos.handle(req, res);
})
git push http://localhost:8000/git/somebody/beep master

User folder not created =(

Denis-Zhukov commented 1 year ago

Some one? =(

verside2 commented 1 year ago

Some one? =(

Use translator from Russian to English if you need.

Данный странный эффект проявляется из-за использования url.parse(req.url) в исходных кодах библиотеки, который объявлен Deprecated в документации nodejs: по каким-то причинам данная функция превращает путь /git/beep/info/refs?service=git-receive-pack в /beep/info/refs?service=git-receive-pack, т. е. урезая первую часть. В причины я уже не стал углубляться, но решение может быть таким (если не менять исходные коды самой библиотеки):

app.use("/:account/:repo", (req, res) => {
    req.url = req.originalUrl;

    repos.handle(req, res);
});
Denis-Zhukov commented 1 year ago

Спасибо, это помогло =)